This is an archive of the discontinued LLVM Phabricator instance.

[ELF2] Add -init and -fini switches
ClosedPublic

Authored by ikudrin on Oct 2 2015, 7:49 AM.

Details

Summary

Add DT_INIT and DT_FINI entries to the dynamic table section if corresponding symbols are defined, "_init" and "_fini" respectively.
Symbol names can be overridden by using -init and -fini command line switches.

Diff Detail

Event Timeline

ikudrin updated this revision to Diff 36364.Oct 2 2015, 7:49 AM
ikudrin retitled this revision from to [ELF2] Add -init and -fini switches.
ikudrin updated this object.
ikudrin added reviewers: rafael, ruiu.
ikudrin added a project: lld.
ikudrin added a subscriber: llvm-commits.
ruiu accepted this revision.Oct 2 2015, 10:51 AM
ruiu edited edge metadata.

What happens if the symbol given by -fini is in an archive? Does linker have to resolve the symbol by extracting and parsing the object file from the archive?

ELF/OutputSections.cpp
221–232

Lambda doesn't seem to help improve readability here. This would be better.

if (Symbol *S = Symtab.getSymbols().lookup(Config->Init)))
  InitSym = dyn_cast<ELFSymbolBody<ELFT>>(S->Body);
if (Symbol *S = Symtab.getSymbols().lookup(Config->Fini)))
  FiniSym = dyn_cast<ELFSymbolBody<ELFT>>(S->Body);

if (InitSym)
  ++NumEntries; // DT_FINI
if (FiniSym)
  ++NumEntries; // DT_INIT
ELF/OutputSections.h
334–335

InitSymbolBody -> InitSym
Likewise Fini.

334–335

These symbols type should be Defined<ELFT>, no? They can't be Undefined<ELFT> which is a subclass of ELFSymbolBody<ELFT>

This revision is now accepted and ready to land.Oct 2 2015, 10:51 AM
emaste added a subscriber: emaste.Oct 3 2015, 3:46 AM
In D13385#258775, @ruiu wrote:

What happens if the symbol given by -fini is in an archive? Does linker have to resolve the symbol by extracting and parsing the object file from the archive?

I investigated this case with ld. These switches don't force the linker to resolve these symbols. Moreover, there is even no warning if the symbols stay unresolved.

ikudrin added inline comments.Oct 5 2015, 2:24 AM
ELF/OutputSections.h
334–335

These symbols type should be Defined<ELFT>, no? They can't be Undefined<ELFT> which is a subclass of ELFSymbolBody<ELFT>

It sounds strange, but:

$ cat > x.s
.globl _start,_init,_fini;
_start:
^C
$ llvm-mc -filetype=obj -triple=x86_64-pc-linux -o x.o x.s
$ ld -shared -o x.out x.o
$ llvm-readobj -dynamic-table -symbols x.out

File: x.out
Format: ELF64-x86-64
Arch: x86_64
AddressSize: 64bit
LoadName: 
Symbols [
  ...
  Symbol {
    Name: _init (32)
    Value: 0x0
    Size: 0
    Binding: Global (0x1)
    Type: None (0x0)
    Other: 0
    Section: Undefined (0x0)
  }
  ...
  Symbol {
    Name: _fini (57)
    Value: 0x0
    Size: 0
    Binding: Global (0x1)
    Type: None (0x0)
    Other: 0
    Section: Undefined (0x0)
  }
  ...
]
DynamicSection [ (8 entries)
  Tag                Type                 Name/Value
  0x000000000000000C INIT                 0x0
  0x000000000000000D FINI                 0x0
  0x0000000000000004 HASH                 0xE8
  0x0000000000000005 STRTAB               0x1C0
  0x0000000000000006 SYMTAB               0x118
  0x000000000000000A STRSZ                37 (bytes)
  0x000000000000000B SYMENT               24 (bytes)
  0x0000000000000000 NULL                 0x0
]
ikudrin marked 2 inline comments as done.Oct 5 2015, 3:10 AM
This revision was automatically updated to reflect the committed changes.
ruiu added a comment.Oct 5 2015, 8:37 AM

Thank you for the investigation. LGTM

lld/trunk/ELF/OutputSections.cpp
232 ↗(On Diff #36496)

Remove this blank line.

ikudrin marked an inline comment as done.Oct 5 2015, 9:07 AM