[PATCH] D42748: [ELF] Don't create a .dynamic section when linking with -Bstatic

Alexander Richardson via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed May 22 02:52:19 PDT 2019


arichardson added a comment.

In D42748#1511604 <https://reviews.llvm.org/D42748#1511604>, @kib wrote:

> In D42748#1511335 <https://reviews.llvm.org/D42748#1511335>, @MaskRay wrote:
>
> > From the description
> >
> > > This also causes a .dynamic section, the _DYNAMIC symtbol and a PT_DYNAMIC header to be added to the output file. This causes problems for example when trying to run such a binary on FreeBSD MIPS.
> >
> > What I know is just that the presence of `_DYNAMIC` caused a problem but I don't have more information why it caused the problem. Without more information I can only conjecture. My intuition says it is more likely a problem if the dynamic is absent in some scenarios, I don't understand how the presence (though probably unexpected by you) caused a problem.
>
>
> The C Runtime (cumulative of crt1.o and libc.so/libc.a) contains a lot of code like `if (&_DYNAMIC != NULL)` used as a test for the static/dynamic situation.  Look at the https://github.com/freebsd/freebsd/blob/master/lib/csu/amd64/crt1.c#L62, there are many more.  I believe it was mentioned many times in the discussion.
>
> > 
> > 
> >> Perhaps first you should define what features of static binaries you depend on.  In fact PT_INTERP is not absolutely needed to have any of the dynamic feature, in principle rtld.c can be linked from crt1.o.  And if we (FreeBSD) decide to support dlopen(3) from static binaries, this is what would happen.  We already got a small dynamic linker in crt1.o to support ifunc relocations.
> >> 
> >> Absence of the DYNAMIC segment is rather good indication that a lot of dynamic features are indeed not used, so &_DYNAMIC == 0 is probably quite good check except that it is broken.
> > 
> > @kib So to answer your question, I need more information.
> > 
> >> And if we (FreeBSD) decide to support dlopen(3) from static binaries
> > 
> > And if you decide to support static pie, you also need `_DYNAMIC`.
>
> Are you sure about this ?  For static PIE, as I understand, we miss some kind of relocator in  csu.  For the relocator to work, all we need is to find the relocation section' boundaries.  See for instance https://github.com/freebsd/freebsd/blob/master/lib/csu/common/ignore_init.c#L52 how we find iplt relocations in static binaries.  I would expect that a similar approach works for non-iplt.


I don't think that would for two reasons (at least not for every architecture). First lld does not emit a symbol pointing to the start of the relocation section. And second, with PIE we don't know the real address prior to relocation. It might work for architectures with pc-relative addressing but some others would need to process the relative relocations first. 
Therefore you need to look at the .dynamic contents to find REL/RELA.
Regarding this patch: I don't think _DYNAMIC would be useful for -static-pie since we would need to relocate it first so _DYNAMIC could still be undefined and used to check for dynamic linker presence.

I had a quick look at glibc's static-pie code and it seems like glibc loads the first GOT entry for most architectures to get the address of the dynamic section.


Repository:
  rLLD LLVM Linker

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D42748/new/

https://reviews.llvm.org/D42748





More information about the llvm-commits mailing list