From e215b6d8432675d33426a6e5bce4276e2d3a9bfd Mon Sep 17 00:00:00 2001 From: Luke Street Date: Mon, 7 Aug 2023 20:04:40 -0400 Subject: [PATCH] Fix write_elf for common symbols & only write filename in file symbol --- src/util/elf.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/util/elf.rs b/src/util/elf.rs index 35e3acc..9f35ee6 100644 --- a/src/util/elf.rs +++ b/src/util/elf.rs @@ -393,8 +393,15 @@ pub fn write_elf(obj: &ObjInfo) -> Result> { let mut num_local = 0; // Add file symbol + let obj_name; if !obj.name.is_empty() { - let name_index = writer.add_string(obj.name.as_bytes()); + // Only write filename + obj_name = Path::new(&obj.name) + .file_name() + .map(|s| s.to_string_lossy().to_string()) + .unwrap_or_else(|| obj.name.clone()); + + let name_index = writer.add_string(obj_name.as_bytes()); let index = writer.reserve_symbol_index(None); out_symbols.push(OutSymbol { index, @@ -462,13 +469,7 @@ pub fn write_elf(obj: &ObjInfo) -> Result> { let st_type = match symbol.kind { ObjSymbolKind::Unknown => elf::STT_NOTYPE, ObjSymbolKind::Function => elf::STT_FUNC, - ObjSymbolKind::Object => { - if symbol.flags.is_common() { - elf::STT_COMMON - } else { - elf::STT_OBJECT - } - } + ObjSymbolKind::Object => elf::STT_OBJECT, ObjSymbolKind::Section => elf::STT_SECTION, }; let st_bind = if symbol.flags.is_weak() { @@ -483,6 +484,8 @@ pub fn write_elf(obj: &ObjInfo) -> Result> { st_other: if symbol.flags.is_hidden() { elf::STV_HIDDEN } else { elf::STV_DEFAULT }, st_shndx: if section_index.is_some() { 0 + } else if symbol.flags.is_common() { + elf::SHN_COMMON } else if symbol.address != 0 { elf::SHN_ABS } else {