【发布时间】:2019-03-15 18:28:32
【问题描述】:
我正在尝试使用符号和相关的行号为 ARM 构建一个基本项目,以便在项目在 QEMU 中运行时可以轻松地从 GDB Multiarch 调试项目。
我有两个文件,一个 C 源文件和一些程序集。在这个例子中,它们非常简单:
cmain.c:
int add_numbers(int a, int b) {
return a + b;
}
int cmain() {
int a = 3;
int b = 4;
int c = add_numbers(a, b);
}
main.s:
.section .init
.global _start
_start:
.extern cmain
mov sp, #0x8000
bl cmain
另外,这里是链接器文件,kernel.ld:
SECTIONS {
.init 0x8000 : {
*(.init)
}
.text : {
*(.text)
}
.data : {
*(.data)
*(.bss)
*(.rodata*)
*(.COMMON)
}
/DISCARD/ : {
*(*)
}
}
然后,我使用以下 shell 脚本使用调试符号构建这些项目。简而言之,它将文件组装和编译成目标文件,然后将它们链接到 ELF 和 objcopy 到 IMG。
rm -r build
mkdir -p build
arm-none-eabi-as -I . main.s -o build/main.o
arm-none-eabi-gcc -ffreestanding -fno-builtin -march=armv7-a -MD -MP -g -c cmain.c -o build/cmain.o
arm-none-eabi-ld build/main.o build/cmain.o -L/usr/lib/gcc/arm-none-eabi/6.3.1/ -lgcc --no-undefined -o build/output.elf -T kernel.ld
arm-none-eabi-objcopy build/output.elf -O binary build/kernel.img --keep-file-symbols
对于 GDB 调试器步进,我需要 ELF 为 C 源代码提供行号。 (请注意,实际项目有更多的 C 文件。)行号存在于 C 目标文件中,但 不 在 ELF 中。
$ arm-none-eabi-nm build/cmain.o --line-numbers
00000000 T add_numbers /home/aaron/Desktop/arm-mcve/cmain.c:1
00000030 T cmain /home/aaron/Desktop/arm-mcve/cmain.c:5
$ arm-none-eabi-nm build/output.elf --line-numbers
00008008 T add_numbers
00008038 T cmain
00008000 T _start
为什么ELF中没有行号信息,如何添加让GDB单步执行?
【问题讨论】:
标签: c gcc arm gdb debug-symbols