Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple way to reorder ELF file sections

Tags:

linux

elf

I'm looking for a simple way to reorder the ELF file sections. I've got a sequence of custom sections that I would like all to be aligned in a certain order.

The only way I've found how to do it is to use a Linker script. However the documentation indicates that specifying a custom linker script overrides the default. The default linker script has a lot of content in it that I don't want to have to duplicate in my custom script just to get three sections always together in a certain order. It does not seem very flexible to hard code the linker behavior like that.

Why do I want to do this? I have a section of data that I need to know the run-time memory location of (beginning and end). So I've created two additional sections and put sentinel variables in them. I then want to use the memory locations of those variables to know the extent of the 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 markerA and markerB.

Is there another way to accomplish this? Are there libraries that would let me read in the currently executing ELF image and determine section location and size?

like image 971
MrSlippers Avatar asked Jul 16 '10 14:07

MrSlippers


2 Answers

You can obtain addresses of loaded sections by analyzing the ELF-File format. Details may be found e.g. in

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

for a short impression which information is available its worth to take a look at readelf

readelf -S <filename> 

returns a list of all sections contained in .

  1. The sections which were mapped into memory were typed PROGBITS.
  2. The address your are looking for is displayed in the column Addr.
  3. To obtain the memory location you have to add the load address of your executable / shared object

There are a few ways to determine the load adress of your executable/shared object:

  1. you may parse /proc/[pid]/maps (the first column contains the load address). [pid] is the process id
  2. if you know one function contained in your file you can apply dlsym to receive a pointer to the function. That pointer is the input parameter for dladdr returning a Dl_info struct containing the requested load address

To get some ELF information the library

  • libelf

may be a helpful companian (I detected it after studying the above mentioned TIS so I only took a short look at it and I don't know deeper details)

I hope this sketch of a possible solution will help.

like image 197
Uhli Avatar answered Nov 11 '22 14:11

Uhli


You may consider using GCC's initializers to reference the variables which would go into a separate section otherwise and maintain all their pointers in an array. I recommend using initializers because this works file-independently.

like image 27
asdf Avatar answered Nov 11 '22 16:11

asdf