diff options
| author | Mark Wielaard <[email protected]> | 2013-11-27 16:45:44 +0100 |
|---|---|---|
| committer | Mark Wielaard <[email protected]> | 2013-11-27 16:45:44 +0100 |
| commit | 819c349f6339512d6961a6172c539fdf2c2f1328 (patch) | |
| tree | c7be311a37ac6ebae7d27b10f401d131d7d8be0d /tests | |
| parent | 16bc4569a9d4c86aac4de749ccf27fa7b1ae24d5 (diff) | |
libdwfl: Add dwfl_module_addrsym_elf and dwfl_module_getsym_elf.
Introduce two new functions that also return the elf associated with a
symbol to make symbol section indexing work for non-special sections.
Simplify code by removing dwfl_file where appropriate and just track Elf
directly. Document limitations of shndx with existing dwfl_module_addrsym
and dwfl_module_getsym. Extend dwflsyms testcase to check some more symbol
and section (index) properties.
Signed-off-by: Mark Wielaard <[email protected]>
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/ChangeLog | 10 | ||||
| -rw-r--r-- | tests/dwflsyms.c | 66 | ||||
| -rwxr-xr-x | tests/run-dwflsyms.sh | 110 |
3 files changed, 127 insertions, 59 deletions
diff --git a/tests/ChangeLog b/tests/ChangeLog index 8a6c4d22..6c0ec364 100644 --- a/tests/ChangeLog +++ b/tests/ChangeLog @@ -1,3 +1,13 @@ +2013-11-27 Mark Wielaard <[email protected]> + + * dwflsyms.c (gelf_bind_order): New function. + (elf_section_name): Likewise. + (addr_in_section): Likewise. + (list_syms): Use dwfl_module_getsym_elf and dwfl_module_addrsym_elf. + Refine assert using gelf_bind_order. Print elf_section_name. Check + bias with addr_in_section. + * run-dwflsyms.sh: Add section names to expected output. + 2013-11-26 Mark Wielaard <[email protected]> * Makefile.am (EXTRA_DIST): Add run-funcretval.sh. diff --git a/tests/dwflsyms.c b/tests/dwflsyms.c index 55f2653c..10c01f1f 100644 --- a/tests/dwflsyms.c +++ b/tests/dwflsyms.c @@ -69,6 +69,42 @@ gelf_bind (GElf_Sym *sym) } static int +gelf_bind_order (GElf_Sym *sym) +{ + switch (GELF_ST_BIND (sym->st_info)) + { + case STB_LOCAL: + return 1; + case STB_WEAK: + return 2; + case STB_GLOBAL: + return 3; + default: + return 0; + } +} + +static const char * +elf_section_name (Elf *elf, GElf_Word shndx) +{ + GElf_Ehdr ehdr; + GElf_Shdr shdr; + Elf_Scn *scn = elf_getscn (elf, shndx); + gelf_getshdr (scn, &shdr); + gelf_getehdr (elf, &ehdr); + return elf_strptr (elf, ehdr.e_shstrndx, shdr.sh_name); +} + +bool +addr_in_section (Elf *elf, GElf_Word shndx, GElf_Addr addr) +{ + GElf_Shdr shdr; + Elf_Scn *scn = elf_getscn (elf, shndx); + gelf_getshdr (scn, &shdr); + return addr >= shdr.sh_addr && addr < shdr.sh_addr + shdr.sh_size; +} + +static int list_syms (struct Dwfl_Module *mod, void **user __attribute__ ((unused)), const char *mod_name __attribute__ ((unused)), @@ -82,7 +118,10 @@ list_syms (struct Dwfl_Module *mod, { GElf_Sym sym; GElf_Word shndxp; - const char *name = dwfl_module_getsym (mod, ndx, &sym, &shndxp); + Elf *elf; + Dwarf_Addr bias; + const char *name = dwfl_module_getsym_elf (mod, ndx, &sym, &shndxp, + &elf, &bias); printf("%4d: %s\t%s\t%s (%" PRIu64 ") %#" PRIx64, ndx, gelf_type (&sym), gelf_bind (&sym), name, sym.st_size, sym.st_value); @@ -92,15 +131,34 @@ list_syms (struct Dwfl_Module *mod, dwfl_module_getsym (). */ if (GELF_ST_TYPE (sym.st_info) == STT_FUNC && shndxp != SHN_UNDEF) { + /* Make sure the adjusted value really falls in the elf section. */ + assert (addr_in_section (elf, shndxp, sym.st_value - bias)); + GElf_Addr addr = sym.st_value; GElf_Sym asym; GElf_Word ashndxp; - const char *aname = dwfl_module_addrsym (mod, addr, &asym, &ashndxp); - assert (strcmp (name, aname) == 0); + Elf *aelf; + Dwarf_Addr abias; + const char *aname = dwfl_module_addrsym_elf (mod, addr, &asym, + &ashndxp, &aelf, &abias); + + /* Make sure the adjusted value really falls in the elf section. */ + assert (addr_in_section (aelf, ashndxp, asym.st_value - abias)); + + /* Either they are the same symbol (name), the binding of + asym is "stronger" (or equal) to sym or asym is more specific + (has a lower address) than sym. */ + assert ((strcmp (name, aname) == 0 + || gelf_bind_order (&asym) >= gelf_bind_order (&sym)) + && asym.st_value <= sym.st_value); int res = dwfl_module_relocate_address (mod, &addr); assert (res != -1); - printf(", rel: %#" PRIx64 "", addr); + if (shndxp < SHN_LORESERVE) + printf(", rel: %#" PRIx64 " (%s)", addr, + elf_section_name (elf, shndxp)); + else + printf(", rel: %#" PRIx64 "", addr); } printf ("\n"); } diff --git a/tests/run-dwflsyms.sh b/tests/run-dwflsyms.sh index 2adec5a3..3cd7bf36 100755 --- a/tests/run-dwflsyms.sh +++ b/tests/run-dwflsyms.sh @@ -72,17 +72,17 @@ cat > testfile.symtab.in <<\EOF 32: SECTION LOCAL (0) 0 33: FILE LOCAL crtstuff.c (0) 0 34: OBJECT LOCAL __JCR_LIST__ (0) 0x200de0 - 35: FUNC LOCAL deregister_tm_clones (0) 0x710, rel: 0x710 - 36: FUNC LOCAL register_tm_clones (0) 0x740, rel: 0x740 - 37: FUNC LOCAL __do_global_dtors_aux (0) 0x780, rel: 0x780 + 35: FUNC LOCAL deregister_tm_clones (0) 0x710, rel: 0x710 (.text) + 36: FUNC LOCAL register_tm_clones (0) 0x740, rel: 0x740 (.text) + 37: FUNC LOCAL __do_global_dtors_aux (0) 0x780, rel: 0x780 (.text) 38: OBJECT LOCAL completed.6137 (1) 0x20103c 39: OBJECT LOCAL __do_global_dtors_aux_fini_array_entry (0) 0x200dd8 - 40: FUNC LOCAL frame_dummy (0) 0x7c0, rel: 0x7c0 + 40: FUNC LOCAL frame_dummy (0) 0x7c0, rel: 0x7c0 (.text) 41: OBJECT LOCAL __frame_dummy_init_array_entry (0) 0x200dd0 42: FILE LOCAL foo.c (0) 0 43: FILE LOCAL bar.c (0) 0 44: OBJECT LOCAL b1 (4) 0x201034 - 45: FUNC LOCAL foo (20) 0x814, rel: 0x814 + 45: FUNC LOCAL foo (20) 0x814, rel: 0x814 (.text) 46: FILE LOCAL crtstuff.c (0) 0 47: OBJECT LOCAL __FRAME_END__ (0) 0xa58 48: OBJECT LOCAL __JCR_END__ (0) 0x200de0 @@ -91,28 +91,28 @@ cat > testfile.symtab.in <<\EOF 51: OBJECT LOCAL _DYNAMIC (0) 0x200df0 52: NOTYPE LOCAL __init_array_start (0) 0x200dd0 53: OBJECT LOCAL _GLOBAL_OFFSET_TABLE_ (0) 0x201000 - 54: FUNC GLOBAL __libc_csu_fini (2) 0x8f0, rel: 0x8f0 + 54: FUNC GLOBAL __libc_csu_fini (2) 0x8f0, rel: 0x8f0 (.text) 55: NOTYPE WEAK _ITM_deregisterTMCloneTable (0) 0 56: NOTYPE WEAK data_start (0) 0x201030 57: NOTYPE GLOBAL _edata (0) 0x20103c - 58: FUNC GLOBAL bar (44) 0x828, rel: 0x828 - 59: FUNC GLOBAL _fini (0) 0x8f4, rel: 0x8f4 + 58: FUNC GLOBAL bar (44) 0x828, rel: 0x828 (.text) + 59: FUNC GLOBAL _fini (0) 0x8f4, rel: 0x8f4 (.fini) 60: FUNC GLOBAL __libc_start_main@@GLIBC_2.2.5 (0) 0 61: NOTYPE GLOBAL __data_start (0) 0x201030 62: NOTYPE WEAK __gmon_start__ (0) 0 63: OBJECT GLOBAL __dso_handle (0) 0x200de8 64: OBJECT GLOBAL _IO_stdin_used (4) 0x900 65: OBJECT GLOBAL b2 (4) 0x201038 - 66: FUNC GLOBAL __libc_csu_init (137) 0x860, rel: 0x860 + 66: FUNC GLOBAL __libc_csu_init (137) 0x860, rel: 0x860 (.text) 67: NOTYPE GLOBAL _end (0) 0x201040 - 68: FUNC GLOBAL _start (0) 0x6e0, rel: 0x6e0 + 68: FUNC GLOBAL _start (0) 0x6e0, rel: 0x6e0 (.text) 69: NOTYPE GLOBAL __bss_start (0) 0x20103c - 70: FUNC GLOBAL main (35) 0x7f0, rel: 0x7f0 + 70: FUNC GLOBAL main (35) 0x7f0, rel: 0x7f0 (.text) 71: NOTYPE WEAK _Jv_RegisterClasses (0) 0 72: OBJECT GLOBAL __TMC_END__ (0) 0x201040 73: NOTYPE WEAK _ITM_registerTMCloneTable (0) 0 74: FUNC WEAK __cxa_finalize@@GLIBC_2.2.5 (0) 0 - 75: FUNC GLOBAL _init (0) 0x680, rel: 0x680 + 75: FUNC GLOBAL _init (0) 0x680, rel: 0x680 (.init) EOF cat > testfile.symtab_pl.in <<\EOF @@ -151,17 +151,17 @@ cat > testfile.symtab_pl.in <<\EOF 32: SECTION LOCAL (0) 0 33: FILE LOCAL crtstuff.c (0) 0 34: OBJECT LOCAL __JCR_LIST__ (0) 0x3000200de0 - 35: FUNC LOCAL deregister_tm_clones (0) 0x3000000710, rel: 0x710 - 36: FUNC LOCAL register_tm_clones (0) 0x3000000740, rel: 0x740 - 37: FUNC LOCAL __do_global_dtors_aux (0) 0x3000000780, rel: 0x780 + 35: FUNC LOCAL deregister_tm_clones (0) 0x3000000710, rel: 0x710 (.text) + 36: FUNC LOCAL register_tm_clones (0) 0x3000000740, rel: 0x740 (.text) + 37: FUNC LOCAL __do_global_dtors_aux (0) 0x3000000780, rel: 0x780 (.text) 38: OBJECT LOCAL completed.6137 (1) 0x300020103c 39: OBJECT LOCAL __do_global_dtors_aux_fini_array_entry (0) 0x3000200dd8 - 40: FUNC LOCAL frame_dummy (0) 0x30000007c0, rel: 0x7c0 + 40: FUNC LOCAL frame_dummy (0) 0x30000007c0, rel: 0x7c0 (.text) 41: OBJECT LOCAL __frame_dummy_init_array_entry (0) 0x3000200dd0 42: FILE LOCAL foo.c (0) 0 43: FILE LOCAL bar.c (0) 0 44: OBJECT LOCAL b1 (4) 0x3000201034 - 45: FUNC LOCAL foo (20) 0x3000000814, rel: 0x814 + 45: FUNC LOCAL foo (20) 0x3000000814, rel: 0x814 (.text) 46: FILE LOCAL crtstuff.c (0) 0 47: OBJECT LOCAL __FRAME_END__ (0) 0x3000000a58 48: OBJECT LOCAL __JCR_END__ (0) 0x3000200de0 @@ -170,28 +170,28 @@ cat > testfile.symtab_pl.in <<\EOF 51: OBJECT LOCAL _DYNAMIC (0) 0x3000200df0 52: NOTYPE LOCAL __init_array_start (0) 0x3000200dd0 53: OBJECT LOCAL _GLOBAL_OFFSET_TABLE_ (0) 0x3000201000 - 54: FUNC GLOBAL __libc_csu_fini (2) 0x30000008f0, rel: 0x8f0 + 54: FUNC GLOBAL __libc_csu_fini (2) 0x30000008f0, rel: 0x8f0 (.text) 55: NOTYPE WEAK _ITM_deregisterTMCloneTable (0) 0 56: NOTYPE WEAK data_start (0) 0x3000201030 57: NOTYPE GLOBAL _edata (0) 0x300020103c - 58: FUNC GLOBAL bar (44) 0x3000000828, rel: 0x828 - 59: FUNC GLOBAL _fini (0) 0x30000008f4, rel: 0x8f4 + 58: FUNC GLOBAL bar (44) 0x3000000828, rel: 0x828 (.text) + 59: FUNC GLOBAL _fini (0) 0x30000008f4, rel: 0x8f4 (.fini) 60: FUNC GLOBAL __libc_start_main@@GLIBC_2.2.5 (0) 0 61: NOTYPE GLOBAL __data_start (0) 0x3000201030 62: NOTYPE WEAK __gmon_start__ (0) 0 63: OBJECT GLOBAL __dso_handle (0) 0x3000200de8 64: OBJECT GLOBAL _IO_stdin_used (4) 0x3000000900 65: OBJECT GLOBAL b2 (4) 0x3000201038 - 66: FUNC GLOBAL __libc_csu_init (137) 0x3000000860, rel: 0x860 + 66: FUNC GLOBAL __libc_csu_init (137) 0x3000000860, rel: 0x860 (.text) 67: NOTYPE GLOBAL _end (0) 0x3000201040 - 68: FUNC GLOBAL _start (0) 0x30000006e0, rel: 0x6e0 + 68: FUNC GLOBAL _start (0) 0x30000006e0, rel: 0x6e0 (.text) 69: NOTYPE GLOBAL __bss_start (0) 0x300020103c - 70: FUNC GLOBAL main (35) 0x30000007f0, rel: 0x7f0 + 70: FUNC GLOBAL main (35) 0x30000007f0, rel: 0x7f0 (.text) 71: NOTYPE WEAK _Jv_RegisterClasses (0) 0 72: OBJECT GLOBAL __TMC_END__ (0) 0x3000201040 73: NOTYPE WEAK _ITM_registerTMCloneTable (0) 0 74: FUNC WEAK __cxa_finalize@@GLIBC_2.2.5 (0) 0 - 75: FUNC GLOBAL _init (0) 0x3000000680, rel: 0x680 + 75: FUNC GLOBAL _init (0) 0x3000000680, rel: 0x680 (.init) EOF cat > testfile.dynsym.in <<\EOF @@ -205,22 +205,22 @@ cat > testfile.dynsym.in <<\EOF 7: FUNC WEAK __cxa_finalize (0) 0 8: NOTYPE GLOBAL _edata (0) 0x20103c 9: NOTYPE GLOBAL _end (0) 0x201040 - 10: FUNC GLOBAL __libc_csu_init (137) 0x860, rel: 0x860 + 10: FUNC GLOBAL __libc_csu_init (137) 0x860, rel: 0x860 (.text) 11: NOTYPE GLOBAL __bss_start (0) 0x20103c - 12: FUNC GLOBAL main (35) 0x7f0, rel: 0x7f0 - 13: FUNC GLOBAL __libc_csu_fini (2) 0x8f0, rel: 0x8f0 + 12: FUNC GLOBAL main (35) 0x7f0, rel: 0x7f0 (.text) + 13: FUNC GLOBAL __libc_csu_fini (2) 0x8f0, rel: 0x8f0 (.text) EOF cat > testfile.minsym.in <<\EOF 0: NOTYPE LOCAL (0) 0 1: SECTION LOCAL (0) 0x238 - 2: FUNC LOCAL deregister_tm_clones (0) 0x710, rel: 0x710 - 3: FUNC LOCAL register_tm_clones (0) 0x740, rel: 0x740 - 4: FUNC LOCAL __do_global_dtors_aux (0) 0x780, rel: 0x780 + 2: FUNC LOCAL deregister_tm_clones (0) 0x710, rel: 0x710 (.text) + 3: FUNC LOCAL register_tm_clones (0) 0x740, rel: 0x740 (.text) + 4: FUNC LOCAL __do_global_dtors_aux (0) 0x780, rel: 0x780 (.text) 5: OBJECT LOCAL __do_global_dtors_aux_fini_array_entry (0) 0x200dd8 - 6: FUNC LOCAL frame_dummy (0) 0x7c0, rel: 0x7c0 + 6: FUNC LOCAL frame_dummy (0) 0x7c0, rel: 0x7c0 (.text) 7: OBJECT LOCAL __frame_dummy_init_array_entry (0) 0x200dd0 - 8: FUNC LOCAL foo (20) 0x814, rel: 0x814 + 8: FUNC LOCAL foo (20) 0x814, rel: 0x814 (.text) 9: NOTYPE LOCAL __init_array_end (0) 0x200dd8 10: NOTYPE LOCAL __init_array_start (0) 0x200dd0 11: SECTION LOCAL (0) 0x238 @@ -257,26 +257,26 @@ cat > testfile.minsym.in <<\EOF 42: FUNC WEAK __cxa_finalize (0) 0 43: NOTYPE GLOBAL _edata (0) 0x20103c 44: NOTYPE GLOBAL _end (0) 0x201040 - 45: FUNC GLOBAL __libc_csu_init (137) 0x860, rel: 0x860 + 45: FUNC GLOBAL __libc_csu_init (137) 0x860, rel: 0x860 (.text) 46: NOTYPE GLOBAL __bss_start (0) 0x20103c - 47: FUNC GLOBAL main (35) 0x7f0, rel: 0x7f0 - 48: FUNC GLOBAL __libc_csu_fini (2) 0x8f0, rel: 0x8f0 - 49: FUNC GLOBAL bar (44) 0x828, rel: 0x828 - 50: FUNC GLOBAL _fini (0) 0x8f4, rel: 0x8f4 - 51: FUNC GLOBAL _start (0) 0x6e0, rel: 0x6e0 - 52: FUNC GLOBAL _init (0) 0x680, rel: 0x680 + 47: FUNC GLOBAL main (35) 0x7f0, rel: 0x7f0 (.text) + 48: FUNC GLOBAL __libc_csu_fini (2) 0x8f0, rel: 0x8f0 (.text) + 49: FUNC GLOBAL bar (44) 0x828, rel: 0x828 (.text) + 50: FUNC GLOBAL _fini (0) 0x8f4, rel: 0x8f4 (.fini) + 51: FUNC GLOBAL _start (0) 0x6e0, rel: 0x6e0 (.text) + 52: FUNC GLOBAL _init (0) 0x680, rel: 0x680 (.init) EOF cat > testfile.minsym_pl.in <<\EOF 0: NOTYPE LOCAL (0) 0 1: SECTION LOCAL (0) 0x3000000238 - 2: FUNC LOCAL deregister_tm_clones (0) 0x3000000710, rel: 0x710 - 3: FUNC LOCAL register_tm_clones (0) 0x3000000740, rel: 0x740 - 4: FUNC LOCAL __do_global_dtors_aux (0) 0x3000000780, rel: 0x780 + 2: FUNC LOCAL deregister_tm_clones (0) 0x3000000710, rel: 0x710 (.text) + 3: FUNC LOCAL register_tm_clones (0) 0x3000000740, rel: 0x740 (.text) + 4: FUNC LOCAL __do_global_dtors_aux (0) 0x3000000780, rel: 0x780 (.text) 5: OBJECT LOCAL __do_global_dtors_aux_fini_array_entry (0) 0x3000200dd8 - 6: FUNC LOCAL frame_dummy (0) 0x30000007c0, rel: 0x7c0 + 6: FUNC LOCAL frame_dummy (0) 0x30000007c0, rel: 0x7c0 (.text) 7: OBJECT LOCAL __frame_dummy_init_array_entry (0) 0x3000200dd0 - 8: FUNC LOCAL foo (20) 0x3000000814, rel: 0x814 + 8: FUNC LOCAL foo (20) 0x3000000814, rel: 0x814 (.text) 9: NOTYPE LOCAL __init_array_end (0) 0x3000200dd8 10: NOTYPE LOCAL __init_array_start (0) 0x3000200dd0 11: SECTION LOCAL (0) 0x3000000238 @@ -313,14 +313,14 @@ cat > testfile.minsym_pl.in <<\EOF 42: FUNC WEAK __cxa_finalize (0) 0 43: NOTYPE GLOBAL _edata (0) 0x300020103c 44: NOTYPE GLOBAL _end (0) 0x3000201040 - 45: FUNC GLOBAL __libc_csu_init (137) 0x3000000860, rel: 0x860 + 45: FUNC GLOBAL __libc_csu_init (137) 0x3000000860, rel: 0x860 (.text) 46: NOTYPE GLOBAL __bss_start (0) 0x300020103c - 47: FUNC GLOBAL main (35) 0x30000007f0, rel: 0x7f0 - 48: FUNC GLOBAL __libc_csu_fini (2) 0x30000008f0, rel: 0x8f0 - 49: FUNC GLOBAL bar (44) 0x3000000828, rel: 0x828 - 50: FUNC GLOBAL _fini (0) 0x30000008f4, rel: 0x8f4 - 51: FUNC GLOBAL _start (0) 0x30000006e0, rel: 0x6e0 - 52: FUNC GLOBAL _init (0) 0x3000000680, rel: 0x680 + 47: FUNC GLOBAL main (35) 0x30000007f0, rel: 0x7f0 (.text) + 48: FUNC GLOBAL __libc_csu_fini (2) 0x30000008f0, rel: 0x8f0 (.text) + 49: FUNC GLOBAL bar (44) 0x3000000828, rel: 0x828 (.text) + 50: FUNC GLOBAL _fini (0) 0x30000008f4, rel: 0x8f4 (.fini) + 51: FUNC GLOBAL _start (0) 0x30000006e0, rel: 0x6e0 (.text) + 52: FUNC GLOBAL _init (0) 0x3000000680, rel: 0x680 (.init) EOF cat testfile.symtab.in \ @@ -352,14 +352,14 @@ sed s/0x3000/0x4200/g testfile.minsym_pl.in \ testrun_compare ${abs_builddir}/dwflsyms -e testfilebasmin <<\EOF 0: NOTYPE LOCAL (0) 0 - 1: FUNC LOCAL foo (18) 0x400168, rel: 0x400168 + 1: FUNC LOCAL foo (18) 0x400168, rel: 0x400168 (.text) 2: SECTION LOCAL (0) 0x400120 3: SECTION LOCAL (0) 0x400144 4: SECTION LOCAL (0) 0x4001c0 5: SECTION LOCAL (0) 0x600258 - 6: FUNC GLOBAL _start (21) 0x4001a8, rel: 0x4001a8 - 7: FUNC GLOBAL main (33) 0x400144, rel: 0x400144 - 8: FUNC GLOBAL bar (44) 0x40017a, rel: 0x40017a + 6: FUNC GLOBAL _start (21) 0x4001a8, rel: 0x4001a8 (.text) + 7: FUNC GLOBAL main (33) 0x400144, rel: 0x400144 (.text) + 8: FUNC GLOBAL bar (44) 0x40017a, rel: 0x40017a (.text) EOF exit 0 |
