diff --git a/crates/examples/testfiles/coff/import_msvc.lib.objdump b/crates/examples/testfiles/coff/import_msvc.lib.objdump index 383269f..ccc470a 100644 --- a/crates/examples/testfiles/coff/import_msvc.lib.objdump +++ b/crates/examples/testfiles/coff/import_msvc.lib.objdump @@ -53,7 +53,7 @@ Symbols Dynamic symbols Symbol map -0x0-0x0 "test_x64_NULL_THUNK_DATA" +0x0-0x8 "test_x64_NULL_THUNK_DATA" test_x64.dll: Format: Coff Little-endian 32-bit @@ -74,7 +74,7 @@ Symbols Dynamic symbols Symbol map -0x0-0x0 "__NULL_IMPORT_DESCRIPTOR" +0x0-0x14 "__NULL_IMPORT_DESCRIPTOR" test_x64.dll: Format: Coff Little-endian 32-bit @@ -108,7 +108,7 @@ Symbols Dynamic symbols Symbol map -0x0-0x0 "__IMPORT_DESCRIPTOR_test_x64" +0x0-0xe "__IMPORT_DESCRIPTOR_test_x64" test_x86.dll: Format: Short Import File @@ -139,7 +139,7 @@ Symbols Dynamic symbols Symbol map -0x0-0x0 "test_x86_NULL_THUNK_DATA" +0x0-0x4 "test_x86_NULL_THUNK_DATA" test_x86.dll: Format: Coff Little-endian 32-bit @@ -160,7 +160,7 @@ Symbols Dynamic symbols Symbol map -0x0-0x0 "__NULL_IMPORT_DESCRIPTOR" +0x0-0x14 "__NULL_IMPORT_DESCRIPTOR" test_x86.dll: Format: Coff Little-endian 32-bit @@ -194,7 +194,7 @@ Symbols Dynamic symbols Symbol map -0x0-0x0 "__IMPORT_DESCRIPTOR_test_x86" +0x0-0xe "__IMPORT_DESCRIPTOR_test_x86" test_arm64ec.dll: Format: Short Import File @@ -226,7 +226,7 @@ Symbols Dynamic symbols Symbol map -0x0-0x0 "test_arm64ec_NULL_THUNK_DATA" +0x0-0x8 "test_arm64ec_NULL_THUNK_DATA" test_arm64ec.dll: Format: Coff Little-endian 32-bit @@ -247,7 +247,7 @@ Symbols Dynamic symbols Symbol map -0x0-0x0 "__NULL_IMPORT_DESCRIPTOR" +0x0-0x14 "__NULL_IMPORT_DESCRIPTOR" test_arm64ec.dll: Format: Coff Little-endian 32-bit @@ -281,4 +281,4 @@ Symbols Dynamic symbols Symbol map -0x0-0x0 "__IMPORT_DESCRIPTOR_test_arm64ec" +0x0-0x12 "__IMPORT_DESCRIPTOR_test_arm64ec" diff --git a/crates/examples/testfiles/elf/base-aarch64.objdump b/crates/examples/testfiles/elf/base-aarch64.objdump index 4787052..787f5c6 100644 --- a/crates/examples/testfiles/elf/base-aarch64.objdump +++ b/crates/examples/testfiles/elf/base-aarch64.objdump @@ -160,7 +160,7 @@ Import { library: "", name: "_ITM_registerTMCloneTable" } Import { library: "libc.so.6", name: "printf" } Symbol map -0x598-0x620 "_init" +0x598-0x5ac "_init" 0x620-0x658 "_start" 0x658-0x66c "call_weak_fn" 0x670-0x6a0 "deregister_tm_clones" @@ -170,10 +170,10 @@ Symbol map 0x724-0x744 "main" 0x748-0x7c8 "__libc_csu_init" 0x7c8-0x7cc "__libc_csu_fini" -0x7cc-0x7e0 "_fini" +0x7cc-0x7dc "_fini" 0x7e0-0x7e4 "_IO_stdin_used" -0x7f8-0x10d80 "__FRAME_END__" +0x7f8-0x7fc "__FRAME_END__" 0x10d80-0x10d88 "__frame_dummy_init_array_entry" -0x10d88-0x11008 "__do_global_dtors_aux_fini_array_entry" +0x10d88-0x10d90 "__do_global_dtors_aux_fini_array_entry" 0x11008-0x11010 "__dso_handle" 0x11010-0x11011 "completed.8500" diff --git a/crates/examples/testfiles/elf/base-relr-i686.objdump b/crates/examples/testfiles/elf/base-relr-i686.objdump index cfe51a1..4db6460 100644 --- a/crates/examples/testfiles/elf/base-relr-i686.objdump +++ b/crates/examples/testfiles/elf/base-relr-i686.objdump @@ -108,7 +108,7 @@ Export { name: "_IO_stdin_used", address: 2004 } Symbol map 0x1cc-0x1ec "__abi_tag" -0x1000-0x1060 "_init" +0x1000-0x1020 "_init" 0x1060-0x108c "_start" 0x1090-0x1094 "__x86.get_pc_thunk.bx" 0x10a0-0x10e0 "deregister_tm_clones" @@ -117,14 +117,14 @@ Symbol map 0x1180-0x1189 "frame_dummy" 0x1189-0x118d "__x86.get_pc_thunk.dx" 0x118d-0x11cd "main" -0x11cd-0x11d4 "__x86.get_pc_thunk.ax" -0x11d4-0x2000 "_fini" +0x11cd-0x11d1 "__x86.get_pc_thunk.ax" +0x11d4-0x11e8 "_fini" 0x2000-0x2004 "_fp_hw" 0x2004-0x2008 "_IO_stdin_used" -0x2110-0x3ec0 "__FRAME_END__" +0x2110-0x2114 "__FRAME_END__" 0x3ec0-0x3ec4 "__frame_dummy_init_array_entry" 0x3ec4-0x3ec8 "__do_global_dtors_aux_fini_array_entry" 0x3ec8-0x3fd8 "_DYNAMIC" -0x3fd8-0x4004 "_GLOBAL_OFFSET_TABLE_" +0x3fd8-0x4000 "_GLOBAL_OFFSET_TABLE_" 0x4004-0x4008 "__dso_handle" 0x4008-0x4009 "completed.0" diff --git a/crates/examples/testfiles/elf/base-relr-x86_64.objdump b/crates/examples/testfiles/elf/base-relr-x86_64.objdump index f26e25c..00cb247 100644 --- a/crates/examples/testfiles/elf/base-relr-x86_64.objdump +++ b/crates/examples/testfiles/elf/base-relr-x86_64.objdump @@ -103,19 +103,19 @@ Import { library: "libc.so.6", name: "__cxa_finalize" } Symbol map 0x38c-0x3ac "__abi_tag" -0x1000-0x1060 "_init" +0x1000-0x101b "_init" 0x1060-0x1086 "_start" 0x1090-0x10c0 "deregister_tm_clones" 0x10c0-0x1100 "register_tm_clones" 0x1100-0x1140 "__do_global_dtors_aux" 0x1140-0x1149 "frame_dummy" 0x1149-0x116c "main" -0x116c-0x2000 "_fini" +0x116c-0x1179 "_fini" 0x2000-0x2004 "_IO_stdin_used" -0x20f0-0x3d88 "__FRAME_END__" +0x20f0-0x20f4 "__FRAME_END__" 0x3d88-0x3d90 "__frame_dummy_init_array_entry" 0x3d90-0x3d98 "__do_global_dtors_aux_fini_array_entry" 0x3d98-0x3fb8 "_DYNAMIC" -0x3fb8-0x4008 "_GLOBAL_OFFSET_TABLE_" +0x3fb8-0x4000 "_GLOBAL_OFFSET_TABLE_" 0x4008-0x4010 "__dso_handle" 0x4010-0x4011 "completed.0" diff --git a/crates/examples/testfiles/elf/base.objdump b/crates/examples/testfiles/elf/base.objdump index 7315512..597c164 100644 --- a/crates/examples/testfiles/elf/base.objdump +++ b/crates/examples/testfiles/elf/base.objdump @@ -129,21 +129,21 @@ Import { library: "", name: "_ITM_registerTMCloneTable" } Import { library: "libc.so.6", name: "__cxa_finalize" } Symbol map -0x520-0x570 "_init" +0x520-0x537 "_init" 0x570-0x59b "_start" 0x5a0-0x5e0 "deregister_tm_clones" -0x5e0-0x630 "register_tm_clones" +0x5e0-0x600 "register_tm_clones" 0x630-0x670 "__do_global_dtors_aux" 0x670-0x67a "frame_dummy" 0x67a-0x696 "main" 0x6a0-0x705 "__libc_csu_init" 0x710-0x712 "__libc_csu_fini" -0x714-0x720 "_fini" +0x714-0x71d "_fini" 0x720-0x724 "_IO_stdin_used" -0x874-0x200da8 "__FRAME_END__" +0x874-0x878 "__FRAME_END__" 0x200da8-0x200db0 "__frame_dummy_init_array_entry" 0x200db0-0x200db8 "__do_global_dtors_aux_fini_array_entry" 0x200db8-0x200fb8 "_DYNAMIC" -0x200fb8-0x201008 "_GLOBAL_OFFSET_TABLE_" +0x200fb8-0x201000 "_GLOBAL_OFFSET_TABLE_" 0x201008-0x201010 "__dso_handle" 0x201010-0x201011 "completed.7698" diff --git a/crates/examples/testfiles/macho/base-aarch64-debug.o.objdump b/crates/examples/testfiles/macho/base-aarch64-debug.o.objdump index 4e7044e..041bd34 100644 --- a/crates/examples/testfiles/macho/base-aarch64-debug.o.objdump +++ b/crates/examples/testfiles/macho/base-aarch64-debug.o.objdump @@ -48,4 +48,4 @@ Export { name: "_main", address: 0 } Symbol map 0x0-0x34 "_main" -0x34-0x34 "l_.str" +0x34-0x41 "l_.str" diff --git a/crates/examples/testfiles/macho/base-aarch64.o.objdump b/crates/examples/testfiles/macho/base-aarch64.o.objdump index 89a836c..297fe87 100644 --- a/crates/examples/testfiles/macho/base-aarch64.o.objdump +++ b/crates/examples/testfiles/macho/base-aarch64.o.objdump @@ -33,4 +33,4 @@ Export { name: "_main", address: 0 } Symbol map 0x0-0x34 "_main" -0x34-0x34 "l_.str" +0x34-0x41 "l_.str" diff --git a/crates/examples/testfiles/macho/base-aarch64.objdump b/crates/examples/testfiles/macho/base-aarch64.objdump index d7a017e..c5c6248 100644 --- a/crates/examples/testfiles/macho/base-aarch64.objdump +++ b/crates/examples/testfiles/macho/base-aarch64.objdump @@ -29,4 +29,4 @@ Export { name: "_main", address: 100003f68 } Symbol map 0x100000000-0x100003f68 "__mh_execute_header" -0x100003f68-0x100003f68 "_main" +0x100003f68-0x100003f9c "_main" diff --git a/crates/examples/testfiles/macho/base-x86_64-debug.o.objdump b/crates/examples/testfiles/macho/base-x86_64-debug.o.objdump index 0a476ae..38239ea 100644 --- a/crates/examples/testfiles/macho/base-x86_64-debug.o.objdump +++ b/crates/examples/testfiles/macho/base-x86_64-debug.o.objdump @@ -43,4 +43,4 @@ Import { library: "", name: "_printf" } Export { name: "_main", address: 0 } Symbol map -0x0-0x0 "_main" +0x0-0x25 "_main" diff --git a/crates/examples/testfiles/macho/base-x86_64.o.objdump b/crates/examples/testfiles/macho/base-x86_64.o.objdump index c5c27ec..7620bd8 100644 --- a/crates/examples/testfiles/macho/base-x86_64.o.objdump +++ b/crates/examples/testfiles/macho/base-x86_64.o.objdump @@ -28,4 +28,4 @@ Import { library: "", name: "_printf" } Export { name: "_main", address: 0 } Symbol map -0x0-0x0 "_main" +0x0-0x25 "_main" diff --git a/crates/examples/testfiles/macho/base-x86_64.objdump b/crates/examples/testfiles/macho/base-x86_64.objdump index 9ba05c4..030154e 100644 --- a/crates/examples/testfiles/macho/base-x86_64.objdump +++ b/crates/examples/testfiles/macho/base-x86_64.objdump @@ -36,5 +36,5 @@ Export { name: "_main", address: 100003f60 } Symbol map 0x100000000-0x100003f60 "__mh_execute_header" -0x100003f60-0x100008008 "_main" -0x100008008-0x100008008 "__dyld_private" +0x100003f60-0x100003f85 "_main" +0x100008008-0x100008010 "__dyld_private" diff --git a/crates/examples/testfiles/macho/reloc-aarch64.o.objdump b/crates/examples/testfiles/macho/reloc-aarch64.o.objdump index 763f9c8..4cc9a5c 100644 --- a/crates/examples/testfiles/macho/reloc-aarch64.o.objdump +++ b/crates/examples/testfiles/macho/reloc-aarch64.o.objdump @@ -40,4 +40,4 @@ Import { library: "", name: "_g0" } Import { library: "", name: "_g1" } Symbol map -0x28-0x28 "_data" +0x28-0x48 "_data" diff --git a/crates/examples/testfiles/macho/reloc-arm.o.objdump b/crates/examples/testfiles/macho/reloc-arm.o.objdump index 711b5b6..7d17614 100644 --- a/crates/examples/testfiles/macho/reloc-arm.o.objdump +++ b/crates/examples/testfiles/macho/reloc-arm.o.objdump @@ -40,4 +40,4 @@ Export { name: "_g2", address: 14 } Symbol map 0x0-0x14 "_g1" -0x14-0x14 "_g2" +0x14-0x28 "_g2" diff --git a/crates/examples/testfiles/macho/reloc-i386.o.objdump b/crates/examples/testfiles/macho/reloc-i386.o.objdump index 484f9af..eb7419c 100644 --- a/crates/examples/testfiles/macho/reloc-i386.o.objdump +++ b/crates/examples/testfiles/macho/reloc-i386.o.objdump @@ -30,4 +30,4 @@ Export { name: "_g2", address: 10 } Symbol map 0x0-0x10 "_g1" -0x10-0x10 "_g2" +0x10-0x11 "_g2" diff --git a/crates/examples/testfiles/macho/reloc-x86_64.o.objdump b/crates/examples/testfiles/macho/reloc-x86_64.o.objdump index 0a2efeb..df59163 100644 --- a/crates/examples/testfiles/macho/reloc-x86_64.o.objdump +++ b/crates/examples/testfiles/macho/reloc-x86_64.o.objdump @@ -34,4 +34,4 @@ Dynamic symbols Symbol map 0x0-0x5 "_g1" -0x5-0x5 "_g2" +0x5-0x53 "_g2" diff --git a/crates/examples/testfiles/pe/base-bigobj.o.objdump b/crates/examples/testfiles/pe/base-bigobj.o.objdump index 39f4dad..37aa107 100644 --- a/crates/examples/testfiles/pe/base-bigobj.o.objdump +++ b/crates/examples/testfiles/pe/base-bigobj.o.objdump @@ -51,5 +51,5 @@ Symbols Dynamic symbols Symbol map -0x0-0x51 "printf" -0x51-0x51 "main" +0x0-0x10 "printf" +0x51-0x80 "main" diff --git a/crates/examples/testfiles/pe/base-bigobj.obj.objdump b/crates/examples/testfiles/pe/base-bigobj.obj.objdump index 8056b24..5718b9f 100644 --- a/crates/examples/testfiles/pe/base-bigobj.obj.objdump +++ b/crates/examples/testfiles/pe/base-bigobj.obj.objdump @@ -62,4 +62,4 @@ Symbols Dynamic symbols Symbol map -0x0-0x0 "_main" +0x0-0xa "_main" diff --git a/crates/examples/testfiles/pe/base-gnu.exe.objdump b/crates/examples/testfiles/pe/base-gnu.exe.objdump index 9bfa311..b3b77ea 100644 --- a/crates/examples/testfiles/pe/base-gnu.exe.objdump +++ b/crates/examples/testfiles/pe/base-gnu.exe.objdump @@ -1526,7 +1526,7 @@ Symbol map 0x140007bb8-0x140007bc0 "DeleteCriticalSection" 0x140007bc0-0x140007bd0 "register_frame_ctor" 0x140007bd0-0x140007be8 "___CTOR_LIST__" -0x140007be8-0x140008000 "__DTOR_LIST__" +0x140007be8-0x140007bf8 "__DTOR_LIST__" 0x140008000-0x140008010 "__mingw_winmain_nShowCmd" 0x140008010-0x140008020 "p.0" 0x140008020-0x140008024 "__native_vcclrit_reason" @@ -1580,7 +1580,7 @@ Symbol map 0x140009820-0x140009830 ".refptr._gnu_exception_handler" 0x140009830-0x140009840 ".refptr._matherr" 0x140009840-0x14000a000 ".refptr._newmode" -0x14000a000-0x14000c000 "__RUNTIME_PSEUDO_RELOC_LIST_END__" +0x14000a000-0x14000a474 "__RUNTIME_PSEUDO_RELOC_LIST_END__" 0x14000c000-0x14000c008 "__bss_start__" 0x14000c008-0x14000c010 "__mingw_winmain_lpCmdLine" 0x14000c010-0x14000c018 "__mingw_winmain_hInstance" @@ -1819,7 +1819,7 @@ Symbol map 0x14000d6f8-0x14000d6fc ".idata$7" 0x14000d6fc-0x14000d700 ".idata$7" 0x14000d700-0x14000d704 ".idata$7" -0x14000d704-0x14000e000 "__lib64_libmsvcrt_os_a_iname" +0x14000d704-0x14000d710 "__lib64_libmsvcrt_os_a_iname" 0x14000e000-0x14000e008 "__xc_a" 0x14000e008-0x14000e010 "__mingw_pcppinit" 0x14000e010-0x14000e018 "__xc_z" @@ -1831,8 +1831,8 @@ Symbol map 0x14000e040-0x14000e048 "__xl_d" 0x14000e048-0x14000e050 "__xl_z" 0x14000e050-0x14000e058 "___crt_xt_end__" -0x14000e058-0x14000f000 "__xd_z" +0x14000e058-0x14000e060 "__xd_z" 0x14000f000-0x14000f008 "_tls_start" 0x14000f008-0x14000f010 "_tls_end" 0x14000f010-0x140010000 "___tls_end__" -0x140010000-0x140010000 ".rsrc" +0x140010000-0x1400104e8 ".rsrc" diff --git a/crates/examples/testfiles/pe/base.o.objdump b/crates/examples/testfiles/pe/base.o.objdump index 9d64017..38dd156 100644 --- a/crates/examples/testfiles/pe/base.o.objdump +++ b/crates/examples/testfiles/pe/base.o.objdump @@ -51,5 +51,5 @@ Symbols Dynamic symbols Symbol map -0x0-0x51 "printf" -0x51-0x51 "main" +0x0-0x10 "printf" +0x51-0x80 "main" diff --git a/crates/examples/testfiles/pe/base.obj.objdump b/crates/examples/testfiles/pe/base.obj.objdump index 8056b24..5718b9f 100644 --- a/crates/examples/testfiles/pe/base.obj.objdump +++ b/crates/examples/testfiles/pe/base.obj.objdump @@ -62,4 +62,4 @@ Symbols Dynamic symbols Symbol map -0x0-0x0 "_main" +0x0-0xa "_main" diff --git a/crates/examples/testfiles/pe/weak-extern.o.objdump b/crates/examples/testfiles/pe/weak-extern.o.objdump index 72ca5c9..01b0c59 100644 --- a/crates/examples/testfiles/pe/weak-extern.o.objdump +++ b/crates/examples/testfiles/pe/weak-extern.o.objdump @@ -36,4 +36,4 @@ Symbols Dynamic symbols Symbol map -0x0-0x0 ".weak.weak_symbol." +0x0-0x8 ".weak.weak_symbol." diff --git a/src/read/symbol_map.rs b/src/read/symbol_map.rs index 103dafb..be2be80 100644 --- a/src/read/symbol_map.rs +++ b/src/read/symbol_map.rs @@ -1,6 +1,6 @@ use alloc::vec::Vec; -use crate::read::{Object, ObjectSymbol, ObjectSymbolTable}; +use crate::read::{Object, ObjectSection, ObjectSymbol, ObjectSymbolTable}; use crate::{SymbolKind, SymbolScope}; /// An entry in a [`SymbolMap`]. @@ -268,7 +268,14 @@ impl SymbolMapBuilder { /// The map will only contain defined text and data symbols. /// The dynamic symbol table will only be used if there are no debugging symbols. /// - /// If symbol sizes are unknown then we guess the size based on the next symbol. + /// If symbol sizes are unknown then we guess the size based on the next symbol + /// or end of section. + /// + /// This does not work well if multiple sections use the same base address, which is + /// common for relocatable object files. The symbols will be overlapping, and the + /// symbol returned by lookups may be indeterministic. Additionally, if the symbol + /// size is unknown then we may use a symbol or section end address from a different + /// section to guess its size. pub fn build<'data, O>(self, object: &O) -> SymbolMap> where O: Object<'data> + ?Sized, @@ -324,6 +331,13 @@ impl SymbolMapBuilder { // Tuple is ordered for sort. all_symbols.push((symbol.address(), priority, index, size, name)); } + + // Add end of sections to improve guesses for unknown sizes. + for section in object.sections() { + let address = section.address().saturating_add(section.size()); + all_symbols.push((address, !0, !0, !0, "")); + } + // Unstable sort is okay because tuple includes index. all_symbols.sort_unstable(); @@ -344,6 +358,9 @@ impl SymbolMapBuilder { } previous_address = symbol.address; } + + // Remove the entries for sections. + symbols.retain(|x| !x.name.is_empty()); } SymbolMap::new(symbols) }