1

I'm building an ELF SO for bada on ARM using GCC. The compiler options include -fpic. Yet in the built file, when I do readelf -r, there's a whole lot of relocation records, of following types:

  • R_ARM_RELATIVE
  • R_ARM_REL32
  • R_ARM_ABS32
  • R_ARM_GLOB_DAT
  • R_ARM_JUMP_SLOT

What am I misunderstanding here?

EDIT: from what I can see, the PIC implementation in the compiler doesn't use GOT. Instead, they use PC-relative addressing with stored constants being offsets from point of use to the symbol address; that's resolved by the linker. Like this, to read a global variable:

    ldr r12, OffsetToVar
PointOfUse:
    ldr r0, [r12, pc] 
# r0 now has the value of MyVar

#...

# At function's end...
OffsetToVar:
    .long MyVar-PointOfUse-8
# Compiler can't resolve this, since it doesn't know
# the address of MyVar, but linker can

Similar idea for cross-module function calls. When a project mixes ARM and Thumb code though, the latter may misfire. But I've worked around that.

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
  • Just a guess, do you access global data? If so the code may be a PIC, but the data is still bound to a possibly changing address? – Alexis Wilke Feb 27 '14 at 04:34
  • PIC means Position Independent Code. Instructions are chosen so that that chunk of code will execute properly even if loaded at an address space it was not linked for. – old_timer Feb 27 '14 at 14:57
  • I will take a guess at what you want (but not an answer to the question). See `-fixed-REG`, use [`-msingle-pic-base`](http://gcc.gnu.org/ml/gcc-patches/1999-07n/msg00573.html), and `-mpic-register` where your scheduler must set `r9` to point to global data. So for instance, without an MMU you can run multiple code instances and replace `r9` as a pointer to globals for that task/thread/process. This is a mechanism to avoid data relocation. Also, it is common for the compiler/linker to emit relocations that are normally not needed. – artless noise Feb 27 '14 at 17:22

1 Answers1

5

Doesn't PIC mean no relocations?

No, it does not.

It just means no relocations against .text section (so the .text can be shared between multiple processes).

Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • Is that true? "-fpic Generate position-independent code... Such code accesses all constant addresses through a global offset table (GOT)." http://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html – auselen Feb 27 '14 at 08:39
  • There is `-pic` and [`-pie`](http://stackoverflow.com/questions/2463150/fpie-position-independent-executable-option-gcc-ld). Really, it is going to depend on the `gcc` configuration. I think this answer is correct for an *ARM Linux glibc* type environment. For instance, there is a *static base* in the ARM EABI where all globals could reference to this register. Also, sometime the *relocation* records don't actually do anything; Ie, the compiler/linker outputs an entry, but no actual fix-up is needed at load time. – artless noise Feb 27 '14 at 16:34
  • Judging by offsets, there are some relocs in the text section, too. Is there a way to investigate further? – Seva Alekseyev Feb 27 '14 at 20:42
  • On x86-64, text relocations are allowed at least in PIE executables. Maybe also in `gcc -shared` actual shared libraries. (e.g. for `movabs rsi, offset symbol` to use a 64-bit immediate absolute, instead of the more efficient RIP-relative `lea`.) – Peter Cordes Mar 25 '18 at 12:46