Skip to content

Commit 991f506

Browse files
committed
rustc: Fix permission denied error in 'ar' when lto is enabled
The reason that 'ar' can fail with permission denied is that when link-time optimizations are enabled, rustc copies libraries into a temporary directory, preserving file permissions, and subsequently modifies them using 'ar'. The modification can fail because some package managers may install libraries in system directories as read-only files, which means the temporary file also becomes read-only when it is copied. I have fixed this by giving the temporary file's owner read+write permissions after the copy. I have also added a regression test for this issue.
1 parent 40b86ba commit 991f506

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

src/librustc/back/link.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,6 +1319,18 @@ fn add_upstream_rust_crates(cmd: &mut Command, sess: &Session,
13191319
sess.abort_if_errors();
13201320
}
13211321
}
1322+
// Fix up permissions of the copy, as fs::copy() preserves
1323+
// permissions, but the original file may have been installed
1324+
// by a package manager and may be read-only.
1325+
match fs::chmod(&dst, io::UserRead | io::UserWrite) {
1326+
Ok(..) => {}
1327+
Err(e) => {
1328+
sess.err(format!("failed to chmod {} when preparing \
1329+
for LTO: {}", dst.display(),
1330+
e).as_slice());
1331+
sess.abort_if_errors();
1332+
}
1333+
}
13221334
let handler = &sess.diagnostic().handler;
13231335
let config = ArchiveConfig {
13241336
handler: handler,
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-include ../tools.mk
2+
3+
all:
4+
$(RUSTC) lib.rs
5+
6+
# the compiler needs to copy and modify the rlib file when performing
7+
# LTO, so we should ensure that it can cope with the original rlib
8+
# being read-only.
9+
chmod 444 $(TMPDIR)/*.rlib
10+
11+
$(RUSTC) main.rs -C lto
12+
$(call RUN,main)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![crate_type = "rlib"]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
extern crate lib;
12+
13+
fn main() {}

0 commit comments

Comments
 (0)