GCC不能包括CRT*.O的调试信息吗?

发布于 2025-02-08 20:20:04 字数 1085 浏览 1 评论 0原文

我正在使用Ghidra进行一些二进制分析,但它不支持矮人5:

ghidra.app.util.bin.format.dwarf4.dwarfexception:当前仅支持矮版2、3或4个信息。

好的,我会用-gdwarf-4 -gstrict-dwarf ...重建所有内容...

ghidra.app.util.bin.format.dwarf4.dwarfexception:当前仅支持矮版2、3或4个信息。

嗯,这是怎么回事?

  $ cat foo.c
int main(){
        返回0;
}
$ gcc -gdwarf -4 -gstrict -dwarf foo.c -o foo
$ readelf -w foo
...
  汇编单元 @ offset 0x0:
   长度:0x24(32位)
   版本:5
   单元类型:DW_UT_COMPILE(1)
   Abbrev偏移:0x0
   指针尺寸:8
 < 0>< c>:abbrev编号:1(dw_tag_compile_unit)
    < d> dw_at_stmt_list:0x0
    < 11> DW_AT_LOW_PC:0x1020
    < 19> DW_AT_HIGH_PC:38
    < 1a> dw_at_name :(间接字符串,偏移:0x0):../ sysdeps/x86_64/start.s
    < 1e> dw_at_comp_dir :(间接字符串,偏移:0x1a):/build/build/glibc/src/glibc/csu
    < 22> dw_at_producer :(间接字符串,偏移:0x35):gnu as 2.38
...
 

啊,来自crti.o和朋友有一堆矮人5调试信息。我可以告诉GCC或LD在保留自己的调试信息的同时从二进制中省略它们吗?

I am using Ghidra for some binary analysis, but it doesn't support DWARF 5:

ghidra.app.util.bin.format.dwarf4.DWARFException: Only DWARF version 2, 3, or 4 information is currently supported.

Okay fine, I'll rebuild everything with -gdwarf-4 -gstrict-dwarf ...

ghidra.app.util.bin.format.dwarf4.DWARFException: Only DWARF version 2, 3, or 4 information is currently supported.

Hmm, what's going on?

$ cat foo.c
int main() {
        return 0;
}
$ gcc -gdwarf-4 -gstrict-dwarf foo.c -o foo
$ readelf -w foo
...
  Compilation Unit @ offset 0x0:
   Length:        0x24 (32-bit)
   Version:       5
   Unit Type:     DW_UT_compile (1)
   Abbrev Offset: 0x0
   Pointer Size:  8
 <0><c>: Abbrev Number: 1 (DW_TAG_compile_unit)
    <d>   DW_AT_stmt_list   : 0x0
    <11>   DW_AT_low_pc      : 0x1020
    <19>   DW_AT_high_pc     : 38
    <1a>   DW_AT_name        : (indirect string, offset: 0x0): ../sysdeps/x86_64/start.S
    <1e>   DW_AT_comp_dir    : (indirect string, offset: 0x1a): /build/glibc/src/glibc/csu
    <22>   DW_AT_producer    : (indirect string, offset: 0x35): GNU AS 2.38
...

Ah, there's a bunch of DWARF 5 debug info from crti.o and friends. Can I tell gcc or ld to omit them from the binary, while keeping my own debug info?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁把谁当真 2025-02-15 20:20:04

我想到了这种骇人听闻的方法:首先,首先,通过将-v传递给链接器的链接到哪些CRT对象:

$ gcc -Wl,-v foo.c -o foo
collect2 version 12.1.0
/usr/bin/ld -plugin /usr/lib/gcc/x86_64-pc-linux-gnu/12.1.0/liblto_plugin.so -plugin-opt=/usr/lib/gcc/x86_64-pc-linux-gnu/12.1.0/lto-wrapper -plugin-opt=-fresolution=/tmp/ccsoyfj1.res -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s --build-id --eh-frame-hdr --hash-style=gnu -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -pie -o foo /usr/lib/gcc/x86_64-pc-linux-gnu/12.1.0/../../../../lib/Scrt1.o /usr/lib/gcc/x86_64-pc-linux-gnu/12.1.0/../../../../lib/crti.o /usr/lib/gcc/x86_64-pc-linux-gnu/12.1.0/crtbeginS.o -L/usr/lib/gcc/x86_64-pc-linux-gnu/12.1.0 -L/usr/lib/gcc/x86_64-pc-linux-gnu/12.1.0/../../../../lib -L/lib/../lib -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-pc-linux-gnu/12.1.0/../../.. -v /tmp/ccDNayjM.o -lgcc --push-state --as-needed -lgcc_s --pop-state -lc -lgcc --push-state --as-needed -lgcc_s --pop-state /usr/lib/gcc/x86_64-pc-linux-gnu/12.1.0/crtendS.o /usr/lib/gcc/x86_64-pc-linux-gnu/12.1.0/../../../../lib/crtn.o
GNU ld (GNU Binutils) 2.38

接下来,手动剥离它们:

#!/bin/bash

crt=(
    /usr/lib/gcc/x86_64-pc-linux-gnu/12.1.0/../../../../lib/Scrt1.o
    /usr/lib/gcc/x86_64-pc-linux-gnu/12.1.0/../../../../lib/crti.o
    /usr/lib/gcc/x86_64-pc-linux-gnu/12.1.0/crtbeginS.o
    /usr/lib/gcc/x86_64-pc-linux-gnu/12.1.0/crtendS.o
    /usr/lib/gcc/x86_64-pc-linux-gnu/12.1.0/../../../../lib/crtn.o
)

for obj in "${crt[@]}"; do
    objcopy -R .debug_info -R .debug_aranges -R .debug_line "$obj" "./$(basename "$obj")"
done

gcc -gdwarf-4 -gstrict-dwarf -c foo.c -o foo.o
gcc -nostartfiles Scrt1.o crti.o crtbeginS.o foo.o crtendS.o crtn.o -o foo

它有效!

$ readelf -w foo | grep -i version
  Version:               1
  Version:                  2
   Version:       4
  DWARF Version:               4

I came up with this hacky approach: first, first, figure out which crt objects are being linked by passing -v to the linker:

$ gcc -Wl,-v foo.c -o foo
collect2 version 12.1.0
/usr/bin/ld -plugin /usr/lib/gcc/x86_64-pc-linux-gnu/12.1.0/liblto_plugin.so -plugin-opt=/usr/lib/gcc/x86_64-pc-linux-gnu/12.1.0/lto-wrapper -plugin-opt=-fresolution=/tmp/ccsoyfj1.res -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s --build-id --eh-frame-hdr --hash-style=gnu -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -pie -o foo /usr/lib/gcc/x86_64-pc-linux-gnu/12.1.0/../../../../lib/Scrt1.o /usr/lib/gcc/x86_64-pc-linux-gnu/12.1.0/../../../../lib/crti.o /usr/lib/gcc/x86_64-pc-linux-gnu/12.1.0/crtbeginS.o -L/usr/lib/gcc/x86_64-pc-linux-gnu/12.1.0 -L/usr/lib/gcc/x86_64-pc-linux-gnu/12.1.0/../../../../lib -L/lib/../lib -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-pc-linux-gnu/12.1.0/../../.. -v /tmp/ccDNayjM.o -lgcc --push-state --as-needed -lgcc_s --pop-state -lc -lgcc --push-state --as-needed -lgcc_s --pop-state /usr/lib/gcc/x86_64-pc-linux-gnu/12.1.0/crtendS.o /usr/lib/gcc/x86_64-pc-linux-gnu/12.1.0/../../../../lib/crtn.o
GNU ld (GNU Binutils) 2.38

Next, strip them manually:

#!/bin/bash

crt=(
    /usr/lib/gcc/x86_64-pc-linux-gnu/12.1.0/../../../../lib/Scrt1.o
    /usr/lib/gcc/x86_64-pc-linux-gnu/12.1.0/../../../../lib/crti.o
    /usr/lib/gcc/x86_64-pc-linux-gnu/12.1.0/crtbeginS.o
    /usr/lib/gcc/x86_64-pc-linux-gnu/12.1.0/crtendS.o
    /usr/lib/gcc/x86_64-pc-linux-gnu/12.1.0/../../../../lib/crtn.o
)

for obj in "${crt[@]}"; do
    objcopy -R .debug_info -R .debug_aranges -R .debug_line "$obj" "./$(basename "$obj")"
done

gcc -gdwarf-4 -gstrict-dwarf -c foo.c -o foo.o
gcc -nostartfiles Scrt1.o crti.o crtbeginS.o foo.o crtendS.o crtn.o -o foo

And it works!

$ readelf -w foo | grep -i version
  Version:               1
  Version:                  2
   Version:       4
  DWARF Version:               4
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文