如何使用 gdb 调试原始二进制文件

Joh*_*ica 6 gdb arm

我有一个嵌入式设备的可执行文件。
它没有 gdb 识别的头信息,而是使用供应商指定的专有头信息。

我可以使用 IDA-pro 很好地分析文件,但我想运行一些代码来看看它的作用。

可执行文件加载在地址 0x52000000

但是,如果我只是使用加载文件

exec-file myfile
Run Code Online (Sandbox Code Playgroud)

我得到

“myfile”:不是可执行格式:无法识别文件格式

如果我使用以下方法在正确的位置恢复内存:

restore myfile 52000000
Run Code Online (Sandbox Code Playgroud)

我得到:

如果没有调试过程,您就无法做到这一点。

我如何摆脱这个先有鸡还是先有蛋的问题?

我只想跳到代码中间,将一些寄存器设置为预定值并运行一些代码以查看会发生什么。
请注意,我使用的是 ARM 本身的 gdb ARM 工具链。

Joh*_*ica 3

根据@artless_noise的建议,我做了以下事情:

objcopy.exe 
--output-target=elf32-bigarm 
--input-target=binary 
--change-start=0x52000000 
INPUTFILE OUTPUTFILE
Run Code Online (Sandbox Code Playgroud)

这会elf向文件添加一个标头。
然而,它并不能解决整个问题。
的输出

readelf.exe -a OUTPUTFILE 
Run Code Online (Sandbox Code Playgroud)

给出:

ELF Header:
  Magic:   7f 45 4c 46 01 02 01 61 00 00 00 00 00 00 00 00
  Class:                             ELF32
  Data:                              2's complement, big endian
  Version:                           1 (current)
  OS/ABI:                            ARM
  ABI Version:                       0
  Type:                              REL (Relocatable file)
  Machine:                           ARM
  Version:                           0x1
  Entry point address:               0x52000000
  Start of program headers:          0 (bytes into file)
  Start of section headers:          57316 (bytes into file)
  Flags:                             0x0
  Size of this header:               52 (bytes)
  Size of program headers:           0 (bytes)
  Number of program headers:         0
  Size of section headers:           40 (bytes)
  Number of section headers:         5
  Section header string table index: 2

Section Headers:
  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            00000000 000000 000000 00      0   0  0
  [ 1] .data             PROGBITS        00000000 000034 00df8c 00  WA  0   0  1
.....
Run Code Online (Sandbox Code Playgroud)

请注意,该.data部分的地址仍然是0x00000000。这应该是0x52000000
为了解决这个问题,我在地址 0xdf8c 处打开了一个十六进制编辑器。
这靠近节标题所在的位置。节标题的结构如下,以及我期望在那里的数据。

typedef struct {
  Elf32_Word sh_name;    
  Elf32_Word sh_type;    = 1 {.data}
  Elf32_Word sh_flags;   = ?
  Elf32_Addr sh_addr;    = 0x00000000
  Elf32_Off sh_offset;   = 0x00000034
  Elf32_Word sh_size;    = 0x0000df8c
  Elf32_Word sh_link;
  Elf32_Word sh_info;
  Elf32_Word sh_addralign;
  Elf32_Word sh_entsize;
} Elf32_Shdr;
Run Code Online (Sandbox Code Playgroud)

第一个标头始终全为零,第二个标头是部分.data。所以我寻找幻数并填写起始地址,保存文件并将其重新加载到 gdb 中。

现在可以了