I am writing a C program without the standard library, which is loaded into memory by an elf loader and then executes. I wish for this C program to also be able to have its dwarf debugging sections loaded into memory so it can print a backtrace at runtime.
To try to achieve this, I've placed in my C program:
extern char __my_old_debug_abbrev_start[];
extern char __my_old_debug_abbrev_end[];
extern char __my_old_debug_info_start[];
extern char __my_old_debug_info_end[];
extern char __my_old_debug_str_start[];
extern char __my_old_debug_str_end[];
So it can figure out where the sections are. Then to actually provide the locations, I have a linker script which looks like:
SECTIONS
{
.debug_abbrev : {
__my_old_debug_abbrev_start = .;
KEEP (*(.debug_abbrev)) *(.debug_abbrev)
__my_old_debug_abbrev_end = .;
}
.debug_info : {
__my_old_debug_info_start = .;
KEEP (*(.debug_info .gnu.linkonce.wi.*)) *(.debug_info .gnu.linkonce.wi.*)
__my_old_debug_info_end = .;
}
.debug_str : {
__my_old_debug_str_start = .;
KEEP (*(.debug_str)) *(.debug_str)
__my_old_debug_str_end = .;
}
}
INSERT AFTER .rodata;
First, I compile the C program into a libtest.a and then use objcopy to set the sections to be alloc and load.
objcopy --set-section-flags '.debug_abbrev=alloc,load' libtest.a
objcopy --set-section-flags '.debug_info=alloc,load' libtest.a
objcopy --set-section-flags '.debug_str=alloc,load' libtest.a
objcopy --set-section-flags '.gnu.linkonce.wi.*=alloc,load' libtest.a
Then, I run gcc on the archive to compile it into an executable, along the lines of:
gcc libtest.a -o test -T test.lds -static
This produces errors:
/usr/bin/x86_64-linux-gnu-ld: section .debug_info LMA [0000000000000000,0000000000066291] overlaps section .debug_abbrev LMA [0000000000000000,0000000000007cce]
/usr/bin/x86_64-linux-gnu-ld: section .debug_str LMA [0000000000000000,000000000009d264] overlaps section .debug_info LMA [0000000000000000,0000000000066291]
I'm not sure how to fix this as the sections only really exist after linking(?) and then maybe I can adjust the lma with objcopy(?) but then I'm not sure where I would place them.
I have seen https://stackoverflow.com/a/31126336/3492895 but I am not sure how I would create the "hole" before linking so I can use objcopy to adjust things.