Skip to content

Commit fc91059

Browse files
committed
rustc_trans: make .note.rustc look more like debug info
Mark the global variable as const and private so the resulting section is not flagged as writable and to avoid putting an unnecessary symbol in the dynamic table of shared objects. Unfortunately there doesn't seem to be a way to avoid the section being marked SHF_ALLOC when declared as a variable in LLVM. Hack around that by using objcopy to clear the flags on the section before the final link. This places the section at the end of the executable so it can be stripped later without rearranging important code/data sections.
1 parent a59de37 commit fc91059

File tree

8 files changed

+41
-1
lines changed

8 files changed

+41
-1
lines changed

mk/cfg/arm-unknown-linux-gnueabi.mk

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CC_arm-unknown-linux-gnueabi=gcc
44
CXX_arm-unknown-linux-gnueabi=g++
55
CPP_arm-unknown-linux-gnueabi=gcc -E
66
AR_arm-unknown-linux-gnueabi=ar
7+
OBJCOPY_arm-unknown-linux-gnueabi=objcopy
78
CFG_LIB_NAME_arm-unknown-linux-gnueabi=lib$(1).so
89
CFG_STATIC_LIB_NAME_arm-unknown-linux-gnueabi=lib$(1).a
910
CFG_LIB_GLOB_arm-unknown-linux-gnueabi=lib$(1)-*.so

mk/cfg/arm-unknown-linux-gnueabihf.mk

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CC_arm-unknown-linux-gnueabihf=gcc
44
CXX_arm-unknown-linux-gnueabihf=g++
55
CPP_arm-unknown-linux-gnueabihf=gcc -E
66
AR_arm-unknown-linux-gnueabihf=ar
7+
OBJCOPY_arm-unknown-linux-gnueabihf=objcopy
78
CFG_LIB_NAME_arm-unknown-linux-gnueabihf=lib$(1).so
89
CFG_STATIC_LIB_NAME_arm-unknown-linux-gnueabihf=lib$(1).a
910
CFG_LIB_GLOB_arm-unknown-linux-gnueabihf=lib$(1)-*.so

mk/cfg/i686-unknown-linux-gnu.mk

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ CC_i686-unknown-linux-gnu=$(CC)
33
CXX_i686-unknown-linux-gnu=$(CXX)
44
CPP_i686-unknown-linux-gnu=$(CPP)
55
AR_i686-unknown-linux-gnu=$(AR)
6+
OBJCOPY_i686-unknown-linux-gnu=$(OBJCOPY)
67
CFG_LIB_NAME_i686-unknown-linux-gnu=lib$(1).so
78
CFG_STATIC_LIB_NAME_i686-unknown-linux-gnu=lib$(1).a
89
CFG_LIB_GLOB_i686-unknown-linux-gnu=lib$(1)-*.so

mk/cfg/x86_64-unknown-linux-gnu.mk

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ CC_x86_64-unknown-linux-gnu=$(CC)
33
CXX_x86_64-unknown-linux-gnu=$(CXX)
44
CPP_x86_64-unknown-linux-gnu=$(CPP)
55
AR_x86_64-unknown-linux-gnu=$(AR)
6+
OBJCOPY_x86_64-unknown-linux-gnu=$(OBJCOPY)
67
CFG_LIB_NAME_x86_64-unknown-linux-gnu=lib$(1).so
78
CFG_STATIC_LIB_NAME_x86_64-unknown-linux-gnu=lib$(1).a
89
CFG_LIB_GLOB_x86_64-unknown-linux-gnu=lib$(1)-*.so

mk/platform.mk

+1
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ define CFG_MAKE_TOOLCHAIN
149149
CPP_$(1)=$(CROSS_PREFIX_$(1))$(CPP_$(1))
150150
AR_$(1)=$(CROSS_PREFIX_$(1))$(AR_$(1))
151151
RUSTC_CROSS_FLAGS_$(1)=-C linker=$$(call FIND_COMPILER,$$(CC_$(1))) \
152+
-C objcopy=$$(call FIND_COMPILER,$$(OBJCOPY_$(1))) \
152153
-C ar=$$(call FIND_COMPILER,$$(AR_$(1))) $(RUSTC_CROSS_FLAGS_$(1))
153154

154155
RUSTC_FLAGS_$(1)=$$(RUSTC_CROSS_FLAGS_$(1)) $(RUSTC_FLAGS_$(1))

src/librustc/session/config.rs

+2
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,8 @@ options! {CodegenOptions, CodegenSetter, basic_codegen_options,
478478
CG_OPTIONS, cg_type_desc, cgsetters,
479479
ar: Option<String> = (None, parse_opt_string,
480480
"tool to assemble archives with"),
481+
objcopy: String = ("objcopy".to_string(), parse_string,
482+
"objcopy"),
481483
linker: Option<String> = (None, parse_opt_string,
482484
"system linker to link outputs with"),
483485
link_args: Option<Vec<String>> = (None, parse_opt_list,

src/librustc_trans/back/link.rs

+31-1
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,33 @@ fn link_natively(sess: &Session, trans: &CrateTranslation, dylib: bool,
849849
}
850850
}
851851

852+
fn fix_meta_section_attributes(sess: &Session, meta_name: &PathBuf) {
853+
// First, fix up the note section attributes. We want the SHF_ALLOC and
854+
// SHF_WRITE flags to be unset so the section will get placed near the
855+
// end along with the debug info. This allows the section to be
856+
// stripped later without renumbering important sections that contain
857+
// code and data.
858+
let mut o_cmd = Command::new(&sess.opts.cg.objcopy);
859+
o_cmd.arg("--rename-section")
860+
.arg(".note.rustc=.note.rustc,contents,noload,readonly")
861+
.arg(&meta_name);
862+
// Invoke objcopy
863+
info!("{:?}", o_cmd);
864+
match o_cmd.status() {
865+
Ok(exitstatus) => {
866+
if !exitstatus.success() {
867+
sess.err(&format!("objcopy failed with exit code {:?}", exitstatus.code()));
868+
sess.note(&format!("{:?}", &o_cmd));
869+
}
870+
},
871+
Err(exitstatus) => {
872+
sess.err(&format!("objcopy failed: {}", exitstatus));
873+
sess.note(&format!("{:?}", &o_cmd));
874+
}
875+
}
876+
sess.abort_if_errors();
877+
}
878+
852879
fn link_args(cmd: &mut Command,
853880
sess: &Session,
854881
dylib: bool,
@@ -901,7 +928,10 @@ fn link_args(cmd: &mut Command,
901928
// executable. This metadata is in a separate object file from the main
902929
// object file, so we link that in here.
903930
if dylib {
904-
cmd.arg(&obj_filename.with_extension("metadata.o"));
931+
let meta_name = obj_filename.with_extension("metadata.o");
932+
933+
fix_meta_section_attributes(sess, &meta_name);
934+
cmd.arg(&meta_name);
905935
}
906936

907937
if t.options.is_like_osx {

src/librustc_trans/trans/base.rs

+3
Original file line numberDiff line numberDiff line change
@@ -2520,6 +2520,9 @@ pub fn write_metadata(cx: &SharedCrateContext, krate: &ast::Crate) -> Vec<u8> {
25202520
};
25212521
unsafe {
25222522
llvm::LLVMSetInitializer(llglobal, llconst);
2523+
llvm::LLVMSetGlobalConstant(llglobal, llvm::True);
2524+
llvm::LLVMSetUnnamedAddr(llglobal, llvm::True);
2525+
llvm::SetLinkage(llglobal, llvm::Linkage::PrivateLinkage);
25232526
let name = loader::meta_section_name(cx.sess().target.target.options.is_like_osx);
25242527
let name = CString::new(name).unwrap();
25252528
llvm::LLVMSetSection(llglobal, name.as_ptr())

0 commit comments

Comments
 (0)