Fix write_elf for common symbols & only write filename in file symbol

This commit is contained in:
Luke Street 2023-08-07 20:04:40 -04:00
parent e802b61001
commit e215b6d843
1 changed files with 11 additions and 8 deletions

View File

@ -393,8 +393,15 @@ pub fn write_elf(obj: &ObjInfo) -> Result<Vec<u8>> {
let mut num_local = 0; let mut num_local = 0;
// Add file symbol // Add file symbol
let obj_name;
if !obj.name.is_empty() { 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); let index = writer.reserve_symbol_index(None);
out_symbols.push(OutSymbol { out_symbols.push(OutSymbol {
index, index,
@ -462,13 +469,7 @@ pub fn write_elf(obj: &ObjInfo) -> Result<Vec<u8>> {
let st_type = match symbol.kind { let st_type = match symbol.kind {
ObjSymbolKind::Unknown => elf::STT_NOTYPE, ObjSymbolKind::Unknown => elf::STT_NOTYPE,
ObjSymbolKind::Function => elf::STT_FUNC, ObjSymbolKind::Function => elf::STT_FUNC,
ObjSymbolKind::Object => { ObjSymbolKind::Object => elf::STT_OBJECT,
if symbol.flags.is_common() {
elf::STT_COMMON
} else {
elf::STT_OBJECT
}
}
ObjSymbolKind::Section => elf::STT_SECTION, ObjSymbolKind::Section => elf::STT_SECTION,
}; };
let st_bind = if symbol.flags.is_weak() { let st_bind = if symbol.flags.is_weak() {
@ -483,6 +484,8 @@ pub fn write_elf(obj: &ObjInfo) -> Result<Vec<u8>> {
st_other: if symbol.flags.is_hidden() { elf::STV_HIDDEN } else { elf::STV_DEFAULT }, st_other: if symbol.flags.is_hidden() { elf::STV_HIDDEN } else { elf::STV_DEFAULT },
st_shndx: if section_index.is_some() { st_shndx: if section_index.is_some() {
0 0
} else if symbol.flags.is_common() {
elf::SHN_COMMON
} else if symbol.address != 0 { } else if symbol.address != 0 {
elf::SHN_ABS elf::SHN_ABS
} else { } else {