I'm debugging a boot loader (syslinux) with gdb and the gdb-stub of qemu. At some point the main file load a shared object ldlinux.elf
.
I would like to add the symbols in gdb for that file. The command add-symbol-file
seems like the way to go. However, as a relocatable file, I have to specify the memory address it has been loaded at. And here comes the problem.
Although I know the base address at which the LOAD
segment has been loaded at, add-symbol-file
works section-wise and want me to specify the address at which each section has been loaded.
Can I tell gdb to load all the symbols of all the sections provided that I specify the base address of the file in memory?
Does the behavior of gdb make sens? The section headers aren't used for running an ELF and are even optional. I can't see a use case where specifying the load address of the sections would be useful.
Example
Here are the program headers and section headers of the shared object.
Elf file type is DYN (Shared object file) Entry point 0x4c60 There are 3 program headers, starting at offset 52 Program Headers: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align LOAD 0x000000 0x00000000 0x00000000 0x1db10 0x20bfc RWE 0x1000 DYNAMIC 0x01d618 0x0001d618 0x0001d618 0x00098 0x00098 RW 0x4 GNU_STACK 0x000000 0x00000000 0x00000000 0x00000 0x00000 RWE 0x10 Section to Segment mapping: Segment Sections... 00 .gnu.hash .dynsym .dynstr .rel.dyn .rel.plt .plt .text .rodata .ctors .dtors .data.rel.ro .dynamic .got .got.plt .data .bss 01 .dynamic 02
There are 29 section headers, starting at offset 0x78618: Section Headers: [Nr] Name Type Addr Off Size ES Flg Lk Inf Al [ 0] NULL 00000000 000000 000000 00 0 0 0 [ 1] .gnu.hash GNU_HASH 00000094 000094 0007e0 04 A 2 0 4 [ 2] .dynsym DYNSYM 00000874 000874 0015c0 10 A 3 1 4 [ 3] .dynstr STRTAB 00001e34 001e34 0010f4 00 A 0 0 1 [ 4] .rel.dyn REL 00002f28 002f28 000ce8 08 A 2 0 4 [ 5] .rel.plt REL 00003c10 003c10 000568 08 AI 2 6 4 [ 6] .plt PROGBITS 00004180 004180 000ae0 04 AX 0 0 16 [ 7] .text PROGBITS 00004c60 004c60 013816 00 AX 0 0 4 [ 8] .rodata PROGBITS 00018480 018480 00462f 00 A 0 0 32 [ 9] .ctors INIT_ARRAY 0001cab0 01cab0 000010 00 WA 0 0 4 [10] .dtors FINI_ARRAY 0001cac0 01cac0 000004 00 WA 0 0 4 [11] .data.rel.ro PROGBITS 0001cae0 01cae0 000b38 00 WA 0 0 32 [12] .dynamic DYNAMIC 0001d618 01d618 000098 08 WA 3 0 4 [13] .got PROGBITS 0001d6b0 01d6b0 0000d0 04 WA 0 0 4 [14] .got.plt PROGBITS 0001d780 01d780 0002c0 04 WA 0 0 4 [15] .data PROGBITS 0001da40 01da40 0000d0 00 WA 0 0 32 [16] .bss NOBITS 0001db20 01db10 0030dc 00 WA 0 0 32 [17] .comment PROGBITS 00000000 01db10 000026 01 MS 0 0 1 [18] .debug_aranges PROGBITS 00000000 01db38 0010c0 00 0 0 8 [19] .debug_info PROGBITS 00000000 01ebf8 021ada 00 0 0 1 [20] .debug_abbrev PROGBITS 00000000 0406d2 009647 00 0 0 1 [21] .debug_line PROGBITS 00000000 049d19 00bd3a 00 0 0 1 [22] .debug_frame PROGBITS 00000000 055a54 004574 00 0 0 4 [23] .debug_str PROGBITS 00000000 059fc8 00538c 01 MS 0 0 1 [24] .debug_loc PROGBITS 00000000 05f354 01312d 00 0 0 1 [25] .debug_ranges PROGBITS 00000000 072481 0005d0 00 0 0 1 [26] .shstrtab STRTAB 00000000 072a51 000101 00 0 0 1 [27] .symtab SYMTAB 00000000 072b54 003530 10 28 504 4 [28] .strtab STRTAB 00000000 076084 002593 00 0 0 1 Key to Flags: W (write), A (alloc), X (execute), M (merge), S (strings) I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown) O (extra OS processing required) o (OS specific), p (processor specific)
If I try to load the file at the address 0x7fab000
then it will relocate the symbols so that the .text
section starts at 0x7fab000.
(gdb) add-symbol-file bios/com32/elflink/ldlinux/ldlinux.elf 0x7fab000 add symbol table from file "bios/com32/elflink/ldlinux/ldlinux.elf" at .text_addr = 0x7fab000 (y or n) y Reading symbols from bios/com32/elflink/ldlinux/ldlinux.elf...done.
And then all the symbols are off by 0x4c60 bytes.
-W
when you call readelf because there are libraries with large section names that get truncated (e.g. /lib/x86_64-linux-gnu/libnss_compat-2.27.so -> .note.gnu.build-id). – Hypocrisy