【问题标题】:relocation entry is not shown in objdump output of given sample program给定示例程序的 objdump 输出中未显示重定位条目
【发布时间】:2016-06-21 20:04:31
【问题描述】:

我有以下示例代码。我想查看目标文件中的重定位表条目。为此,我使用了

objdump -r test.o

示例代码:

#include <stdio.h>
char * myfunction ();
int x=20;
int main()
{

  printf (" \n Inside main ");
  char * p = myfunction ();
  printf (" \n My string is %s ", p);
}

char * myfunction ()
{
  char * key ="jaka";
  return key;
}

Objdump -r test.o 的输出

test.o:     file format elf64-x86-64

RELOCATION RECORDS FOR [.text]:
OFFSET           TYPE              VALUE 
0000000000000009 R_X86_64_32       .rodata
0000000000000013 R_X86_64_PC32     printf-0x0000000000000004
000000000000001d R_X86_64_PC32     myfunction-0x0000000000000004
000000000000002d R_X86_64_32       .rodata+0x0000000000000010
0000000000000037 R_X86_64_PC32     printf-0x0000000000000004
0000000000000045 R_X86_64_32S      .rodata+0x0000000000000024


RELOCATION RECORDS FOR [.eh_frame]:
OFFSET           TYPE              VALUE 
0000000000000020 R_X86_64_PC32     .text
0000000000000040 R_X86_64_PC32     .text+0x000000000000003d

问题:

根据我的理解,全局变量“x”应该在重定位表中的某个地方,我无法找到。

如果我在这里忽略了什么,请帮助我?

【问题讨论】:

    标签: c linker objdump


    【解决方案1】:

    不应该有任何与x 相关的重定位条目,因为您的代码不使用它。不过它会出现在符号表中,您可以通过objdump -t 进行检查

    简而言之-重定位条目是帮助您的代码引用某个对象并链接它们的东西。因此,如果您不在代码中引用 x - 将不会有任何 x 目标重定位。

    要检查它 - 例如,在 main() 中添加 x = 40;,您将得到下一个:

    test.o:     file format elf64-x86-64
    
    RELOCATION RECORDS FOR [.text]:
    OFFSET           TYPE              VALUE 
    0000000000000009 R_X86_64_32       .rodata
    0000000000000013 R_X86_64_PC32     printf-0x0000000000000004
    000000000000001d R_X86_64_PC32     myfunction-0x0000000000000004
    000000000000002d R_X86_64_32       .rodata+0x0000000000000010
    0000000000000037 R_X86_64_PC32     printf-0x0000000000000004
    000000000000003d R_X86_64_PC32     x-0x0000000000000008         # bingo!
    000000000000004f R_X86_64_32S      .rodata+0x0000000000000024
    
    
    RELOCATION RECORDS FOR [.eh_frame]:
    OFFSET           TYPE              VALUE 
    0000000000000020 R_X86_64_PC32     .text
    0000000000000040 R_X86_64_PC32     .text+0x0000000000000047
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-02-21
      • 2011-11-12
      • 1970-01-01
      • 1970-01-01
      • 2017-04-01
      • 2020-07-09
      • 2011-11-14
      相关资源
      最近更新 更多