admin管理员组

文章数量:1389496

I'm working on an educational project where I want to implement a position-independent executable (PIE) on a Cortex-M microcontroller, using the ARM GNU toolchain. The goal is to write a simple loader that can load such a binary at runtime and relocate it to any address in memory.

I compile my code with -fPIE and link it with -pie. As a result, the ELF file includes several sections that are relevant for relocation, such as .rel.dyn, .got, .got.plt, and .dynamic.

My current plan is not to parse the ELF directly, instead, my loader will load a raw binary image, which contains all the sections. For each of these sections, I have external information (such as offsets and sizes), so the loader knows where each section starts, how large it is, and how to map it into memory.

From what I understand so far:

  • The .rel.dyn section contains relocation entries. In my builds, all the entries are of type R_ARM_RELATIVE (0x17), which, as far as I know, means that the loader just needs to add the base load address to the values at the specified offsets.
  • The .got section is also present and seems to be involved in relocations, but there are no relocation entries for .got.plt. I'm not sure whether .got.plt needs to be updated manually in this scenario.
  • There is also a .dynamic section, which I assume is used for dynamic linking. Since I'm not performing dynamic linking on Cortex-M, I'm wondering if it has any relevance to my loader, or if it can be ignored.

My objective is to understand how to implement a minimal loader for a PIE executable on Cortex-M:

  • Which steps are necessary for the loader to relocate the binary properly.
  • Which sections I need to process, and what operations are required to initialize them.

Any guidance or example code on how to build such a loader would be highly appreciated.

本文标签: gccwriting a loader for position independent executablesStack Overflow