0

I have disassembled a .so file (ELF file) with IDA Demo and I got stuck with this instruction

ADD R4, PC ; _GLOBAL_OFFSET_TABLE_

What does it mean? I know that PC is an variable holding next instruction address but what's the purpose of adding it to R4?

Thanks in advance.

Updated:

PUSH.W {R4 - R11, LR}
LDR R4, =(_GLOBAL_OFFSET_TABLE_ - 0x11FACC)
LDR.W R11, =0x4D4
MOV R9, R3
ADD R4, PC
SUB SP, SP, #0x34
ADD R7, SP, #0xC
MOV.W R8, 0
Nnez
  • 41
  • 1
  • 9

2 Answers2

2

The idea is position independent code.

When a module is loaded, it gets an arbitrary base address. All global objects (i. e. variables and functions) reside at some virtual addresses; at build time, you don't know what those addresses would be. But the linker knows the relative offsets between them; those don't change at load time.

So to avoid relocations and thus speed up module loading, and for other beneficial reasons, position independent code is being generated. In your example, R4 initially contains the difference in addresses between the current PC and some global object of interest. Said difference is a link-time constant - it's calculated by the linker and never changes. By adding PC to the said difference, the code gets the absolute address of that global object. What is it - you can know that by checking what happens to R4 next; whether it's being dereferenced or branched to.

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
  • See: [Process linkage table and GOT](http://stackoverflow.com/questions/9688076/process-linkage-table-and-global-offset-table). The idea is more than *position independent code*; it is that **plus** separate/private *data sections* per user or instance. Code pages are the same for multiple users of a shared library, but the global data is different for each user. (what Seva said in different words). – artless noise Mar 25 '14 at 17:32
0

This is done for Position Independent Code (PIC). The

LDR R4, =(_GLOBAL_OFFSET_TABLE_ - 0x11FACC)

loads a value from the global offset-table. This value is the difference between two positions in your code. The PC then is added as a base, the get the actual location you want to access.

This is mainly done for libraries, because that way different programs can map the library into different address-spaces.

Without PIC the library would have to be relocated depending on the base address it gets loaded to for every program. Which would now allow sharing the actual memory-pages.

Nico Erfurth
  • 3,362
  • 22
  • 26