An easy way to reorder sections of ELF files

I am looking for an easy way to reorder ELF file sections. I have a sequence of user partitions that I would like everyone to be aligned in a specific order.

The only way I found is to use the linker script. However, the documentation states that specifying a custom linker script overrides the default value. The default linker of the script has a lot of content in it, which I don’t want to duplicate in my custom script in order to get the three sections always together in a certain order. It does not seem very flexible to hard code the linker behavior in this way.

Why do I want to do this? I have a data section in which I need to know the location of memory at runtime (start and end). Therefore, I created two additional sections and introduced control variables in them. Then I want to use the memory cells of these variables to find out the size of an unknown section in memory.

.markerA int markerA; .targetSection ... Lots of variables ... .markerB int markerB; 

In the above example, I would know that the data in .targetSection is between the address of marker A and marker B.

Is there any other way to do this? Are there libraries that will allow me to read the current executable ELF image and determine the location and size of the partition?

+6
linux elf
source share
4 answers

You can get the addresses of the loaded partitions by analyzing the ELF file format. Details can be found, for example. in

  • Tool Interface Standard (TIS) Portable Formats Specification, Version 1.2 (Http://refspecs.freestandards.org/elf/elf.pdf)

for a short impression of which information is available, take a look at readelf

 readelf -S <filename> 

returns a list of all sections contained in.

  • Sections that were mapped to memory were printed by PROGBITS.
  • The address you are looking for is displayed in the Addr column.
  • To get the memory location, you must add the download address of your executable / shared object

There are several ways to determine the boot address of your executable / shared object:

  • you can parse / proc / [pid] / maps (the first column contains the download address). [pid] is the process id
  • If you know one function contained in your file, you can use dlsym to get a pointer to the function. This pointer is an input parameter to dladdr that returns a Dl_info structure containing the requested load address

To get some ELF information, library

  • libelf

can be a useful companion (I discovered it after studying the above TIS, so I just looked at it briefly and I don't know more details)

I hope this sketch of a possible solution helps.

+4
source share

You can use GCC initializers to refer to variables that would otherwise go into a separate section and support all their pointers in the array. I recommend using initializers because this file works independently.

+1
source share

You can look at the ELFIO library . It contains examples of WriteObj and Writer. Using the library, you can programmatically create / modify the binary ELF file.

0
source share

I am afraid to override the default link script - this is a simple solution.

Since you are worried about this, it may not be flexible (even I think the script link changes often), you can write a script to create a script link based on the default host system ld script ("ld --verbose") and insert special sections into it.

0
source share

All Articles