[PATCH] D29273: [ELF] - Added partial support for --emit-relocs (no --gc-section case)

George Rimar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 6 05:09:08 PST 2017


grimar added inline comments.


================
Comment at: ELF/LinkerScript.cpp:266
+
+    // If we discard a section, we also should discard a dependent section.
+    InputSection<ELFT> *IS = dyn_cast<InputSection<ELFT>>(S);
----------------
ruiu wrote:
> grimar wrote:
> > ruiu wrote:
> > > grimar wrote:
> > > > ruiu wrote:
> > > > > Please describe not what we are doing but why we are doing.
> > > > Done.
> > > I still do not understand why you need this. Well, I do understand what you are trying to do here and what your intention is, but I wonder why you specifically have to care about this case.
> > > 
> > > Let's say you have sections A and B. A defines symbol X, and B has an undefined symbol X. Naturally, the B's undefined symbol will be resolved using A. But, what if you discard section A using a linker script? What will happen? Isn't this essentially the same situation as this?
> > Linux kernel contains exactly the situation I show in testcase.
> > It discards all debug sections:
> > 
> > ```
> > /DISCARD/ : { *(.debug*) }
> > ```
> > 
> > And without that code we just crash for this situation. Because we will try to emit .rela.debug* and crash.
> > That is the only real situation I care here about.
> Do not focus too much on the Linux kernel. Please attempt to find the fundamental issue and a generic solution. If you reach a conclusion that this still needs special handling after a careful examination, that is fine, but adding a piece of code just to link the Linux kernel is not ok.
>Let's say you have sections A and B. A defines symbol X, and B has an undefined symbol X. Naturally, the B's undefined symbol will be resolved >using A. But, what if you discard section A using a linker script? What will happen? Isn't this essentially the same situation as this?

So we have next.
```
SECTIONS { . = 0x1000; .A : { *(.A) } /DISCARD/ : { *(.B*) } }
```
```
.section .A,"a"
.quad foo

.section .B,"a"
foo:
```

What will happen? We will not fail on inputs above. BFD will, gold also does not. We resolve symbol VA to zero in that case.

But how it is can be relative to this patch ?

Both sections A and B apply relocations using InputSectionBase<ELFT>::relocate. If A refers to symbol defined in B and we discard B, it still should be possible to know about "issue", because we apply relocations and can find out that symbol is in discarded section during relocation loop.

In my case for --emit-relocs lets say I have A and .rela.A. And script discards A. 
How .rela.A will know that it should be discarded then ?
I can iterate over all SHT_REL[A] sections separatelly and check that target sections are still alive. But is it better than use of DependentSection approach ?

One another solution I though about was:
copyRelocations() is called too late for SHT_REL[A] sections. At this step all I can do while iterating the relocations is
to switch them into R_X86_64_NONE, for example. That way we end up with relocation section with NONE relocations,
but this section should reference some target section. And that target section is discarded. That does not look an option.




https://reviews.llvm.org/D29273





More information about the llvm-commits mailing list