LD: Cannot add content to ctors section

Ben Hirschberg bhirschb@cyberarmor.io
Wed May 20 09:15:42 GMT 2020


Hi!

I have a setup (Alpine Linux with Go executables) where I cannot rely on contents of ".init_array" to be called, but ".ctors" are called by musl-libc's loader.

I am trying to add my constructors (C or ASM) to the ".ctors" section, but after linking my shared object they seem to be moved to ".init_array" (however in Alpine the ".ctors" section stays in the final image but it is empty)

Why is this happening? Can make my array stay in ".ctors" section?

Thanks
Ben

Here is my ASM code:

       .intel_syntax noprefix
       .text

       .section      .rodata
.Message:
       .string "Constructor called!"

       .text
Constructor:
       lea    rdi, .Message[rip]
       jmp    puts@PLT

       .section      .ctors,"a"
__CTOR_LIST__:
       .quad  Constructor

I run compile and link it but the ctors is empty!

$ gcc ctors.s -c -o ctors.o
$ ld -shared ctors.o -o ctors.so
$ readelf -x 14 /libctors.so
Hex dump of section '.ctors':
  0x00003df8 ffffffff ffffffff 00000000 00000000 ................


The content of this email is confidential and intended for the recipient specified in message only. If you received this message by mistake, please reply to this message and follow with its deletion, so that we can ensure such a mistake does not occur in the future.


More information about the Binutils mailing list