<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Fri, Oct 13, 2017 at 11:27 PM, Rafael Avila de Espindola <span dir="ltr"><<a href="mailto:rafael.espindola@gmail.com" target="_blank">rafael.espindola@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">Rui Ueyama <<a href="mailto:ruiu@google.com">ruiu@google.com</a>> writes:<br>
<br>
> I think the current behavior is bad. I'd like to propose the following<br>
> changes:<br>
><br>
> 1. If a linker is creating a non-PIC ELF binary, and if it finds a DSO<br>
</span>> symbol foo for an undefined weak symbol foo, then it adds foo as a *strong*<br>
<span class="">> undefined symbol to the dynamic symbol table. This prevents the above crash<br>
> because the program fails to start if foo is not found at load-time,<br>
> instead of crashing at run-time.<br>
><br>
</span>> 2. If a linker is creating a non-PIC ELF binary, and if it *cannot* find a<br>
> DSO symbol foo for an undefined weak symbol foo, then it *does not* add foo to<br>
<span class="">> the dynamic symbol table, and it sets foo's value to zero.<br>
<br>
</span>I would not phrase this as pic/non-pic. From the linker point of view<br>
there are just relocations. I assume then that the intention is:<br></blockquote><div><br></div><div>We have -shared/-pie options, so my intention was to use these flags. We could use relocations to make a decision whether we should export an weak undefined symbols or not, but I think there are a few issues with that:</div><div><br></div><div>1. We cannot make a decision until we visit all relocations, but we need a decision beforehand in order to create GOT entries or report errors.</div><div><br></div><div>2. Sometimes we could get mixed signals -- for example, if some object file contains a direct reference to a weak symbol, and other object file contains a GOTPCREL reference to the same symbol, they are somewhat conflicting.</div><div><br></div><div>So, just using -pie/-shared flags is simple, I guess?</div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
------------------------------<wbr>------------------------------<wbr>-----<br>
Sometimes a linker has to create a symbol in the main binary so that it<br>
is preempted from a shared library at runtime. That symbol is then used<br>
with a copy relocation if it is an object or a special plt entry if it<br>
is a function.<br>
<br>
If the symbol in question was a weak undefined:<br>
<br>
* If the symbol was found in a .so the resulting undefined reference<br>
  will be strong.<br>
* If the symbol was not found in a .so, it is resolved to 0 and there is<br>
  no undefined reference.<br>
<br>
If no relocation requires the symbol to be preempted to the main<br>
executable (all relocations use a got for example) then there will still<br>
be an weak undefined reference since the dynamic linker will be able to<br>
handle the symbol existing or not.<br>
------------------------------<wbr>------------------------------<wbr>-----<br>
<br>
I agree that that is probably a good change.<br>
<br>
Cheers,<br>
Rafael<br>
</blockquote></div><br></div></div>