簡體   English   中英

如何在 Linux kernel 模塊中為地址找到合適的 DWARF 符號?

[英]How can I find a proper DWARF symbol for address in a Linux kernel module?

我有一個帶有 DWARF 調試信息的 kernel 模塊。 它的所有 ELF 部分都具有零起始地址。 它的 DWARF 信息包含多個重疊的代碼和數據符號。 它還有許多 DW_AT_low_pc 地址為零的編譯單元。

有沒有辦法在二進制文件中的某個位置找到正確的 DWARF 符號?

它還有許多 DW_AT_low_pc 地址為零的編譯單元。

kernel 模塊只是一個ET_REL object 文件。 kernel 知道如何將其鏈接到其地址空間。

DW_AT_low_pc全部為 0 的原因是因為有重定位條目會告訴ld如果ld要執行鏈接,如何重定位它們。

您可以使用readelf -Wr module.ko檢查這些條目,但要求 GDB 這樣做要容易得多。

例子:

// f.c
int foo(int x)
{
  return x;
}

int bar(int x)
{
  return foo(x);
}

gcc -c -g f.c -ffunction-sections

結果fo的所有內容都為 0:

nm f.o
0000000000000000 T bar
0000000000000000 T foo

readelf -wi f.o | grep low_pc
    <1d>   DW_AT_low_pc      : 0x0
    <35>   DW_AT_low_pc      : 0x0
    <6c>   DW_AT_low_pc      : 0x0

但是當加載到 GDB 中時,您可以看到重新定位的條目(因為 GDB 知道如何應用它們):

gdb -q f.o
Reading symbols from f.o...

(gdb) p &foo
$1 = (int (*)(int)) 0x0 <foo>
(gdb) p &bar
$2 = (int (*)(int)) 0xc <bar>    <<=== not 0

(gdb) disas/s foo
Dump of assembler code for function foo:
f.c:
2       {
   0x0000000000000000 <+0>:     push   %rbp
   0x0000000000000001 <+1>:     mov    %rsp,%rbp
   0x0000000000000004 <+4>:     mov    %edi,-0x4(%rbp)

3         return x;
   0x0000000000000007 <+7>:     mov    -0x4(%rbp),%eax

4       }
   0x000000000000000a <+10>:    pop    %rbp
   0x000000000000000b <+11>:    retq
End of assembler dump.

(gdb) disas/s bar
Dump of assembler code for function bar:
f.c:
7       {
   0x000000000000000c <+0>:     push   %rbp
   0x000000000000000d <+1>:     mov    %rsp,%rbp
   0x0000000000000010 <+4>:     sub    $0x8,%rsp
   0x0000000000000014 <+8>:     mov    %edi,-0x4(%rbp)

8         return foo(x);                                <<=== correct line
   0x0000000000000017 <+11>:    mov    -0x4(%rbp),%eax
   0x000000000000001a <+14>:    mov    %eax,%edi
   0x000000000000001c <+16>:    callq  0x21 <bar+21>

9       }
   0x0000000000000021 <+21>:    leaveq
   0x0000000000000022 <+22>:    retq
End of assembler dump.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM