<div dir="ltr"><div>This patch changes the existing broken semantics of the linker with a new, simplified one. So, I'll copy the comment that I added in this patch here for those who are interested in linker's weak undefined symbol semantics when dynamic linking involved.</div><div><br></div><div>======</div><div><br></div><div>Exported weak symbols are not representable unless they are resolved</div><div>through GOT. We simply export weak symbols only when -shared or -pie</div><div>were given.</div><div><br></div><div>As a result, if an weak symbol cannot be resolved within the current</div><div>output file, and if no -shared nor -pie were given, the symbol is</div><div>resolved to zero at link-time. If you want to give it a second chance</div><div>to be resolved at load-time, you need to pass -shared or -pie.</div><div><br></div><div>So, why are they not representable? To understand that, assume the</div><div>following code:</div><div><br></div><div>  __attribute__((weak)) int foo();</div><div>  void bar() { if (foo) foo(); }</div><div><br></div><div>If this code is compiled without -fPIC, function pointer foo in the</div><div>"if" is compiled to a direct reference to a function. The linker would</div><div>create a PLT entry for foo and use its address as a function pointer</div><div>value for foo. It would usually work, because when you jump to foo's</div><div>PLT, it in turn jumps to foo's real function definition.</div><div><br></div><div>However, that mechanism can result in a bad combination of pointer</div><div>values if the symbol is weak. Symbol foo's function pointer value is</div><div>always non-zero because it is resolved to a linker-synthesized foo's</div><div>PLT entry (that means the "if" condition is always true). But, if the</div><div>loader cannot resolve foo at load-time, it leaves foo's PLT entry zero.</div><div>That means when you jump there, it then jumps to an unexpected</div><div>address, and the program crashes. As a result, if foo cannot be</div><div>resolved at load-time, the program crashes. This is apparently what</div><div>users would not expect.</div><div><br></div><div>If all references to symbol foo go through GOT, weak symbols are</div><div>representable. If the loader cannot resolve foo, it leaves foo's GOT</div><div>entry zero, and the "if" condition simply becomes false.</div><div><br></div><div>So, we want to export weak symbols only when we know they are</div><div>referenced through GOT.</div><div><br></div><div>(The decision we are making here is not perfect; even if -shared or</div><div>-pie are given to the linker, input object files may have direct</div><div>references to weak symbols. We'll report them as errors in</div><div>scanRelocations. Likewise, even if -shared nor -pie were not given,</div><div>all relocations to weak symbols could go through GOT. Even so, we</div><div>won't export them, because it is hard to detect it ahead of time. It</div><div>is also generally preferable to let users explicitly control the</div><div>linker's behavior via command line options rather than changing</div><div>the behavior implicitly depending on existence or absense of some</div><div>relocations.)</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Nov 4, 2017 at 5:31 PM, Rui Ueyama <span dir="ltr"><<a href="mailto:ruiu@google.com" target="_blank">ruiu@google.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">This is the first step to simplify and improve relocation handling in lld. I'd like to submit it so that I can make further changes. Can you take a look?</div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Nov 4, 2017 at 5:27 PM, Rui Ueyama via Phabricator <span dir="ltr"><<a href="mailto:reviews@reviews.llvm.org" target="_blank">reviews@reviews.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">ruiu updated this revision to Diff 121606.<br>
ruiu added a comment.<br>
Herald added a subscriber: arichardson.<br>
<br>
- Rebased<br>
<div class="m_782278603155448966HOEnZb"><div class="m_782278603155448966h5"><br>
<br>
<a href="https://reviews.llvm.org/D39392" rel="noreferrer" target="_blank">https://reviews.llvm.org/D3939<wbr>2</a><br>
<br>
Files:<br>
  lld/ELF/Relocations.cpp<br>
  lld/ELF/Symbols.cpp<br>
  lld/test/ELF/weak-undef-export<wbr>.s<br>
  lld/test/ELF/weak-undef-pic-no<wbr>pic.s<br>
<br>
</div></div></blockquote></div><br></div>
</div></div></blockquote></div><br></div>