PIE executables on bare-metal Cortex-M: -fpie vs -fPIE and relocation handling

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi all,

I'm working on an educational project to implement a custom loader for a
position-independent executable (PIE) running on a bare-metal Cortex-M
microcontroller, using the ARM GNU toolchain (gcc and binutils).

Context:
- I compile my C source files with -fPIE and link with -pie.
- The resulting ELF file contains sections like .rel.dyn, .got, .got.plt,
  and .dynamic.
- I extract the raw binary, which includes all sections marked with the
  ALLOC flag, and I load it to an arbitrary address at runtime.
- My loader reads .rel.dyn and applies the relocations. So far, I only see
  R_ARM_RELATIVE entries in this section.

Questions:
1. Are there practical differences between -fpie and -fPIE on Cortex-M
   bare-metal targets?
   - My executable is about 140KB, so it's relatively small.
   - Does -fpie affect the type of relocations generated or their handling
     by the loader?
   - Are there any downsides or advantages to using -fpie rather than -fPIE
     in this context?

2. Regarding the .rel.dyn section:
   - On my builds, all entries are R_ARM_RELATIVE.
     Is this always expected when using -fPIE/-pie on ARM Cortex-M?
     Or could other relocation types appear, depending on compiler flags or
     code patterns?
   - Does using -fpie instead of -fPIE change the relocation entries or
     reduce the need for relocations?

3. On .got.plt and .dynamic sections:
   - In my ELF files, I see .got.plt but no relocation entries referencing
     it. On Cortex-M (without dynamic linking), is it safe to ignore
     .got.plt, or should it be processed explicitly?
   - The .dynamic section is present, but I assume it's only relevant for
     dynamic linking. Can I safely ignore .dynamic in a bare-metal PIE
     loader?

Goal:
I want to implement a minimal and robust loader for PIE executables on
Cortex-M:
- The loader loads the raw binary into RAM and applies relocations.
- I'm trying to understand which sections I need to process and what
  relocation handling is necessary, especially in the -fpie vs -fPIE
  scenario.

Any guidance, clarifications, or pointers to documentation would be greatly
appreciated!

Thanks in advance,
Max


-- 
Et nunc, auxilium solis, vincam!
Oppugnatio solaris!
VIS!

Massimiliano Cialdi



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux