This is an archive of the discontinued LLVM Phabricator instance.

[ELF] Make -init/-fini options compatible with the gnu linker
ClosedPublic

Authored by atanasyan on Dec 8 2014, 11:05 PM.

Details

Summary

The LLD linker searches initializer and finalizer function names and emits DT_INIT/DT_FINI dynamic table tags to point to these symbols. The -init/-fini command line options override initializer ("_init") and finalizer ("_fini") function names used by default.

Now the -init/-fini options do not affect .init_array/.fini_array sections. The corresponding code has been removed.

Diff Detail

Repository
rL LLVM

Event Timeline

atanasyan updated this revision to Diff 17071.Dec 8 2014, 11:05 PM
atanasyan retitled this revision from to [ELF] Make -init/-fini options compatible with the gnu linker.
atanasyan updated this object.
atanasyan edited the test plan for this revision. (Show Details)
atanasyan added reviewers: shankarke, ruiu, Bigcheese, joerg.
atanasyan added a project: lld.
atanasyan added a subscriber: Unknown Object (MLST).
ruiu edited edge metadata.Dec 8 2014, 11:43 PM

LGTM, nice. Please wait for a while to wait for other reviewers.

What's the right behaviour for static linking ?

What's the right behaviour for static linking ?

These options do not affect static linking. Please correct me if I make a mistake. Here is a quote from the ld manpage:

-init=name
When creating an ELF executable or shared object, call NAME when the executable or shared object is loaded,
by setting DT_INIT to the address of the function. By default, the linker uses _init as the function to call.

As far as I remember (that was five years ago or so) SunOS linker had -z initarray=<symbol> command line option. This option adds symbol to the .initarray section. We can do something like that if necessary.

$ ld.gold --help|grep ctors

--ctors-in-init-array       Use DT_INIT_ARRAY for all constructors (default)
--no-ctors-in-init-array    Handle constructors as directed by compiler.

If this is the case I recommend using old code so that ctors and init/fini are placed in init array sections or fini array sections.

$ ld.gold --help|grep ctors

--ctors-in-init-array       Use DT_INIT_ARRAY for all constructors (default)
--no-ctors-in-init-array    Handle constructors as directed by compiler.

If this is the case I recommend using old code so that ctors and init/fini are placed in init array sections or fini array sections.

The old code puts to the .init_array/.fini_array sections only the symbols specified by the -init/-fini command line options. It does nothing with incoming .ctors and .init sections. I can keep xxxInitAtom / xxxFiniAtom classes but they will be a really dead code. Sure we can/need to implement --ctors-in-init-array default semantics. But I think it is a separate task.

shankarke edited edge metadata.Dec 9 2014, 6:01 AM

We should not keep dead code. Lets remove it.

I am not so sure only about the static linking case and whats the user expectation when init/fini option is used with static linking ?

Joerg/Will ?

joerg accepted this revision.Dec 9 2014, 6:06 AM
joerg edited edge metadata.
joerg added inline comments.
include/lld/ReaderWriter/ELFLinkingContext.h
220 ↗(On Diff #17071)

While here, please move this to the corresponding set function.

224 ↗(On Diff #17071)

Same.

test/elf/initfini-options.test
8 ↗(On Diff #17071)

Personally, I'd prefer to have a separate file for each of the cases.

This revision is now accepted and ready to land.Dec 9 2014, 6:06 AM
joerg added a comment.Dec 9 2014, 7:07 AM

I don't think we should do anything special for static linking. It's up to the CRT code to handle .init_array / .fini_array in that case. I don't know if crt1.o on Linux does it, but they discourage static linking anyway.

Always using .init_array breaks platforms using traditional SysV .init. It should only be enabled for Linux and ARM EABI (IIRC). You can't convert .init to .init_array.

atanasyan closed this revision.Dec 9 2014, 9:39 PM
atanasyan updated this revision to Diff 17114.

Closed by commit rL223917 (authored by @atanasyan).

lld/trunk/test/elf/Hexagon/initfini-option.test