[LLVMdev] [lld] Handling multiple -init/-fini command line options

Shankar Easwaran shankare at codeaurora.org
Mon Dec 8 08:21:49 PST 2014


This is a bug with the Gnu linker, IMO. There is also some behavioral 
difference for static executables when the -init/-fini options are used too.

The DT_INIT/DT_FINI correspond to one initializer function,where as 
DT_INIT_ARRAY/DT_FINI_ARRAY is used when there is more than one 
initalizer function/finalizer function respectively.

You could ask this same question in the binutils forums, but I think we 
should use DT_INIT_ARRAY/DT_FINI_ARRAY if more than one init/fini 
function is supplied by the user.

Shankar Easwaran

On 12/8/2014 12:22 AM, Simon Atanasyan wrote:
> Hi,
>
> The LLD linker in gnu flavor mode accepts multiple -init/-fini command
> line options. For _all_ symbols specified by these options the linker
> creates appropriate entries in the .init_array/.fini_array sections.
> But it looks like LD and Gold linkers do not support this feature and
> take in account only the last -init/-fini options.
>
> % cat foo.c
> int a = 0;
> void foo() { a += 1; }
> void bar() { a += 2; }
>
> % cat main.c
> extern int a;
> int printf(const char *, ...);
> int main() { printf("a: %d\n", a); }
>
> % gcc -fPIC -shared -o libfoo.so -Wl,-init,foo -Wl,-init,bar foo.c
> % gcc main.c -L. -lfoo -Wl,-rpath,.
> % ./a.out
> 2
>
> % gcc -fPIC -shared -o libfoo.so -Wl,-init,bar -Wl,-init,foo foo.c
> % ./a.out
> 1
>
> What is the reason of this incompatibility? The question is caused by
> attempt to support DT_INIT/DT_FINI dynamic table tags. The table can
> contain no more than one DT_INIT/DT_FINI tags. The LD and Gold linker
> look up either default symbols (_init/_fini) or symbols specified by
> the -init/-fini options and put their values to the tags.
>


-- 
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, hosted by the Linux Foundation




More information about the llvm-dev mailing list