我假设这里的问题是GDB不知何故没有读到符号
有点.问题是GDB不知道大约any个动态加载的代码--它从动态加载程序itself得到关于这些代码的通知(以及代码在内存中的哪个位置加载,从哪个文件加载,等等).
您想要做的是使用starti
命令,该命令应该在第一个用户空间指令时停止.对于动态链接的程序,这将是动态加载器中的start
符号.从那里,您应该能够设置您想要的任何断点.
PS ./build.py --ld -t tests/hello.c
在Fedora 37上失败,出现以下错误:
/usr/bin/ld: /tmp/loader-self-reloc-47a291.o: relocation R_X86_64_32S against `.rodata.str1.16' can not be used when making a shared object; recompile with -fPIC
需要进行以下修复才能构建它:
diff --git a/build.py b/build.py
index 7c30718..bb4ffbf 100755
--- a/build.py
+++ b/build.py
@@ -88,7 +88,7 @@ def compile_obj(output, inputs, additional_flags=[]):
compile(output, inputs, ['-c -fpic'] + additional_flags)
def compile_exe(output, inputs, additional_flags=[]):
- compile(output, inputs, ['-pie'] + additional_flags)
+ compile(output, inputs, ['-pie', '-fPIE'] + additional_flags)
def compile_shared(output, inputs, additional_flags=[]):
compile(output, inputs, ['-shared'] + additional_flags)
@@ -134,6 +134,7 @@ cc_includes.append('include/linux')
cc_flags_push()
cc_defines_push()
cc_flags.extend([
+ '-fPIC',
'-fno-stack-protector',
'-fno-builtin',
'-Wl,-e,_dlstart',
使用上述修复后,GDB会话如下所示:
(gdb) starti
Starting program: /home/ciabatta/a
warning: Unable to find dynamic linker breakpoint function.
Program stopped.
0x00007ffff7ff70c0 in _dlstart () from lib/ld-cia.so
(gdb) b ld_stage1_entry
Breakpoint 1 at 0x7ffff7ff70f3: file loader/loader-self-reloc.c, line 30.
(gdb) c
Continuing.
Breakpoint 1, ld_stage1_entry (sp=0x7fffffffdea0, dynv=0x7ffff7ffded0) at loader/loader-self-reloc.c:30
30 _dbg_print_string("Entered dynamic loader\n");
(gdb)