This is an archive of the discontinued LLVM Phabricator instance.

[lld/ELF] Don't reclaim .ctors/.dtors
ClosedPublic

Authored by davide on Dec 24 2015, 1:08 AM.

Details

Summary

FreeBSD expects these to not be stripped even if not referenced to work. If they're not present/incomplete, the dynamic linker gets confused. This change allows lld/llvm/clang to self-host on FreeBSD/amd64. Looks like benchmarking season will start soon.

Diff Detail

Repository
rL LLVM

Event Timeline

davide updated this revision to Diff 43588.Dec 24 2015, 1:08 AM
davide retitled this revision from to [lld/ELF] Don't reclaim .ctors/.dtors.
davide updated this object.
davide added reviewers: rafael, ruiu.
davide added a subscriber: llvm-commits.
ruiu edited edge metadata.Dec 24 2015, 1:16 AM

The code looks OK, but I'm not sure if this is the right thing to do. .ctors and .dtors are used to construct and destruct statically-allocated objects. This patch is to keep them regardless of their usage, which in turn the data referenced by .ctors or .dtors always live even if they are not used.

I'm wondering why we need this for FreeBSD. What's the difference from Linux?

hmm, OK, maybe this is a sledgehammer.
rtld expects .ctors containing -1 (0xffffffff), and a .ctors section containing the correct bits is provided to the linker as input (/usr/lib/crtbegin.o)

Contents of section .ctors:
0000 ffffffff ffffffff ........

So, my understading is that this is just a marker and it's never referenced. rtld uses it to stop walking the .ctor section on startup. In an non-broken executable (or similarly, on an lld-generated executable without --gc-sections) these bits are included in the final output, while without this patch, they're not if I pass --gc-sections.

Could it be a bug in the garbage collector?

Both gold and ld keep the section in the output even if non-referenced and if empty.
Example:

davide@rabbit1:/exps/llvm-lld/build/bin % objdump -s -j .ctors ./blah

./blah: file format elf64-x86-64-freebsd

Contents of section .ctors:
4019f0 ffffffff ffffffff 00000000 00000000 ................
davide@rabbit1:/exps/llvm-lld/build/bin %
davide@rabbit1:/exps/llvm-lld/build/bin % cat blah.c
blah.c blah.core blah.cpp
davide@rabbit1:/exps/llvm-lld/build/bin % cat blah.c
int
main(void)
{

return (0);

}

I'd love to strip it, but the overhead of keeping these bits around (if unused) should be minimal, FWIW.

ruiu added a comment.Dec 24 2015, 1:44 AM

LGTM.

Yup. I was looking at gold and found that Relobj::is_section_name_included in object.cc makes .ctors and .dtors GC-root sections. Looks like there's no choice other than copying that behavior since existing object files depend on that.

ruiu accepted this revision.Dec 24 2015, 1:44 AM
ruiu edited edge metadata.
This revision is now accepted and ready to land.Dec 24 2015, 1:44 AM
This revision was automatically updated to reflect the committed changes.