4

I'd like to merge multiple .o files into a single .o while also merging some of the sections.

If I execute

ld -r first.o second.o -o result.o

then it properly merges the object files but I also need to merge the various .text sections. This section merging functionality is available by the default linker script in ld when you are linking a shared object but I did not manage to force the usage of a custom linker script while using -r. ld seems to ignore any script I've tried. I've tried to modify the default linker script and replaced the .text section block with this

.text           :
{
    *(.text.unlikely .text.*_unlikely)
    *(.text.exit .text.exit.*)
    *(.text.startup .text.startup.*)
    *(.text.hot .text.hot.*)
    *(.text .stub .text.* .gnu.linkonce.t.*)
    /* .gnu.warning sections are handled specially by elf32.em.  */
    *(.gnu.warning)
} =0x90909090

How can I merge the object files while also merge the .text sections using ld?

Kamu Flazs
  • 41
  • 1
  • 2
  • 1
    How did you come to (likely incorrect) conclusion that `ld -r` is *not* merging `.text` sections? Also, which version of `ld` on what platform did you use? – Employed Russian Apr 18 '15 at 03:27
  • I use the following ld version: GNU ld (GNU Binutils) 2.20.0.20100122-0.7.9. Tried newer versions as well, with the same result. I'm not talking about ld not merging the `.text` sections of the different object files, but not merging _all_ `.text*` sections of the object files, like `.text`, `.text._ZN13somenamespace3FooD1Ev`, `.text._ZN13somenamespace3FooD0Ev`, `.text._ZNSt8_Rb_treeIPN13somenamespace3BarES2_St9_IdentityIS2_ENS1_6isLessESaIS2_EE8_M_eraseEPSt13_Rb_tree_nodeIS2_E` etc. – Kamu Flazs Apr 18 '15 at 07:36

1 Answers1

0

but not merging all .text* sections of the object files, like .text, .text._ZN13somenamespace3FooD1Ev

The presence of .text._ZN13somenamespace3FooD1Ev likely means that you are compiling with -ffunction-sections, and indeed the default linker script will not combine such sections: if you wanted them combined, you shouldn't have used -ffunction-sections in the first place!

That said, the following worked for me to combine all .text sections:

ld -r -o t.o t1.o t2.o --verbose > /tmp/script

Note: the linker script for ld -r link is different from normal linker script (which is what you appear to have modified).

Edit /tmp/script to remove non-script parts, and also to change:

  .text         0 :
  {
    *(.text .stub)
    /* .gnu.warning sections are handled specially by elf32.em.  */
    *(.gnu.warning)
  }

To this:

  .text         0 :
  {
    *(.text .stub .text.*)
    /* .gnu.warning sections are handled specially by elf32.em.  */
    *(.gnu.warning)
  }

Finally,

ld -r -o t.o t1.o t2.o -T /tmp/script

gives me t.o with all .text sections merged.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • 1
    I do not use `-ffunction-sections` option while compiling. Most of the functions go into the `.text` section, but some virtual and template functions have their own sections. I tried to find a gcc option which disables this behavior but failed. Your suggestion is basically the same that I was experimenting with. Anyway, I gave a try to your version (adding `.text.*` only), but it's not working. – Kamu Flazs Apr 18 '15 at 17:09