LD: Cannot add content to ctors section

Ben Hirschberg bhirschb@cyberarmor.io
Wed May 20 11:52:12 GMT 2020


Yeah, well, if life was so easy...

The original solution was using C with attribute constructor. It it is not working in this scenario because it adds the function to the ".init_array" section and not the ".ctors".

-----Original Message-----
From: H.J. Lu <hjl.tools@gmail.com>
Sent: Wednesday, May 20, 2020 2:45 PM
To: Ben Hirschberg <bhirschb@cyberarmor.io>
Cc: binutils@sourceware.org
Subject: Re: LD: Cannot add content to ctors section

On Wed, May 20, 2020 at 2:40 AM Ben Hirschberg <bhirschb@cyberarmor.io> wrote:
>
> 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
>

Do it in C with constructor attribute.

--
H.J.
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