Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Injecting sections into GNU ld script; script compatibility between versions of binutils.

I'm building something like in the question How to collect data from different .a files into one array? How to keep sections in .a files with ld script?, i.e. arrays composed during link-time out of elements from different object files.

In my case, there are several arrays, each one going into its own section, .ld_comp_array_*, where * matches the name of the array. Then I take the default linker script using ld --verbose and modify it by putting all these sections (sorted, so that elements of different arrays don't get mixed) into an output section:

KEEP (*(SORT_BY_NAME(.ld_comp_array*)))

and everything works fine.

Then things get a tiny bit more complicated, because application(s) using this feature may be built for various platforms - so far, I've successfully tried AVR Xmega as target platform, as well as Windows 32-bit and Linux 32- and 64-bit for unit testing, and the list is open (new platforms are likely to be added in near future).

However, for each particular platform the default linker scripts is different than on other platforms, and currently I insert the .ld_comp_array* sections manually - would it be possible to do it somehow automatically? The only solution I thought of was parsing the default script and pasting the above input section description, but this seems way too heavy.

I could keep it done manually if there's no relatively simple solution, but I'm not sure if the default scripts obtained from a local version of ld may break on different version of binutils. Can anyone clarify whether this is safe or not?

In case it can be done automatically, is it ok to "inject" the input section specification always directly into .text section, assuming arrays are supposed to be "immutable"?

like image 545
epij Avatar asked Jul 29 '11 19:07

epij


1 Answers

I found a satisfying solution for that problem. GNU ld has the INSERT option which makes the externally supported script not override the default script, but simply add new section at position relative to some section that exists in the default script.

So in my case, the script passed to the linker may be as simple as:

SECTIONS
{
  .rodata.ld_comp_array :
  {
    *(SORT_BY_NAME(.ld_comp_array*))
  }
}
INSERT AFTER .rodata;

More on the INSERT option: http://sourceware.org/binutils/docs/ld/Miscellaneous-Commands.html#Miscellaneous-Commands

like image 143
epij Avatar answered Nov 06 '22 10:11

epij