开发者

Simple way to reorder ELF file sections

开发者 https://www.devze.com 2023-01-07 06:15 出处:网络
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.

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 .targetSe开发者_JS百科ction 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?


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.


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.


You may look at ELFIO library. It contains WriteObj and Writer examples. By using the library, you will be able to create/modify ELF binary file programmatically.


I'm afraid override the default link script is the simple solution.

Since you worried about it might not be flexible (even I think the link script does change that often), you could write a script to generate a link script based on host system's default ld script ("ld --verbose") and insert your special sections into it.

0

精彩评论

暂无评论...
验证码 换一张
取 消