PIE executables on bare-metal Cortex-M: relocation handling and -fpie vs -fPIE differences
Massimiliano Cialdi
cialdi@gmail.com
Sat Mar 22 13:25:04 GMT 2025
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. I'm 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?
I'm also curious whether different linker scripts or relocation models
(e.g., --emit-relocs, -pie variants) impact the relocation types in
.rel.dyn when targeting Cortex-M.
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://sourceware.org/pipermail/binutils/attachments/20250322/394f12fe/attachment.htm>
More information about the Binutils
mailing list