<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:"MS Gothic";
        panose-1:2 11 6 9 7 2 5 8 2 4;}
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:"\@MS Gothic";
        panose-1:2 11 6 9 7 2 5 8 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
span.EmailStyle18
        {mso-style-type:personal-reply;
        font-family:"Calibri",sans-serif;
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style>
</head>
<body lang="EN-US" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal">Oops, this got buried while I was out. Sorry for the super late reply, and thanks for looking into it in detail. I agree with you that ld’s behavior doesn’t always work correctly either, and implementing something similar in LLD would be
 tricky. I’ll look into this more if I have a more pressing need for it, but for now I was able to work around it a different way.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Thanks,<o:p></o:p></p>
<p class="MsoNormal">Shoaib<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div style="border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b><span style="font-size:12.0pt;color:black">From: </span></b><span style="font-size:12.0pt;color:black">Fāng-ruì Sòng <maskray@google.com><br>
<b>Date: </b>Thursday, December 5, 2019 at 2:17 PM<br>
<b>To: </b>Shoaib Meenai <smeenai@fb.com><br>
<b>Cc: </b>Peter Smith <peter.smith@linaro.org>, "llvm-dev@lists.llvm.org" <llvm-dev@lists.llvm.org>, Petr Hosek <phosek@google.com>, George Rimar <grimar@accesssoftek.com>, Rui Ueyama <ruiu@google.com><br>
<b>Subject: </b>Re: [llvm-dev] GC for defsym'd symbols in LLD<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">I have made some further investigation. My conclusion is that GNU ld does not do better than lld. Making the --defsym behavior ideal is difficult in the current framework.<br>
<br>
GNU ld has some unintended behaviors.<br>
<br>
ld.bfd a.o --defsym 'd=foo' --gc-sections -o a => GNU ld retains .text_foo<br>
ld.bfd a.o --defsym 'd=foo+3' --gc-sections -o a => GNU ld drops .text_foo<br>
ld.bfd a.o --defsym 'd=bar-bar+foo' --gc-sections -o a => GNU ld drops .text_foo<br>
<br>
I traced its logic under a debugger. Here is the stack trace:<br>
<br>
ld/ldlang.c:lang_gc_sections<br>
bfd/elflink.c:bfd_elf_gc_sections<br>
bfd/elflink.c:_bfd_elf_gc_mark_reloc<br>
...<br>
bfd/elflink.c:_bfd_elf_gc_mark_hook<br>
<br>
asection *<br>
_bfd_elf_gc_mark_hook (asection *sec,<br>
  ...<br>
  case bfd_link_hash_defined:<br>
  case bfd_link_hash_defweak:<br>
    // It points to .text_foo for --defsym d=foo, but *ABS* for --defsym d=bar-bar+foo or --defsym d=foo+3<br>
    return h->root.u.def.section;<br>
<br>
GNU ld evaluates symbol assignments in many passes, the representation of a symbol (section+offset) can vary among passes.<br>
In the GC pass, its rule only works for simple expressions like --defsym d=foo, but not any slightly complex expressions.<br>
<br>
In lld, it would be difficult to drop the following rule in MarkLive.cpp:<br>
<br>
  for (StringRef s : script->referencedSymbols)<br>
    markSymbol(symtab->find(s));<br>
<br>
The issue can be demonstrated by the following call tree:<br>
<br>
LinkerDriver::link<br>
  markLive<br>
    ...<br>
    resolveReloc<br>
      // Defined::section is nullptr for `d` because the assignment d=foo hasn't been evaluated yet.<br>
  writeResult<br>
    Writer<ELFT>::run<br>
      Writer<ELFT>::finalizeSections<br>
        LinkerScript::processSymbolAssignments<br>
          // Symbol section+offset are evaluated here.<o:p></o:p></p>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">It seems that github issues may be a good place to record the problem. I just created <a href="https://github.com/llvm/llvm-project/issues/52">https://github.com/llvm/llvm-project/issues/52</a><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">I wanted to mark it low priority, but there is no such label.<o:p></o:p></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<p class="MsoNormal">On Wed, Dec 4, 2019 at 8:51 AM Shoaib Meenai <<a href="mailto:smeenai@fb.com">smeenai@fb.com</a>> wrote:<o:p></o:p></p>
</div>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-right:0in">
<p class="MsoNormal" style="margin-bottom:12.0pt">I completely agree that --defsym foo=bar should keep bar (or more precisely the section containing bar) alive if foo is referenced.<br>
<br>
My mental model of how --defsym foo=bar behaves is that (assuming bar is a defined symbol) we create a symbol foo that points to the same location as bar (as in it has the same section + address within that section). Any reference to foo should therefore prevent
 that section from getting garbage collected. bar doesn't need to enter the picture directly (and we don't need to store any sort of explicit link between foo and bar); its section getting preserved just naturally falls out of foo getting preserved.<br>
<br>
For example, in Fāng-ruì's movabs example, the symbol _start (which is the entry point and therefore a GC root) will have a relocation against d, so d will be kept alive too. With --defsym d=foo, the symbol d should point to the same section as foo, so that
 section will be preserved; it doesn't matter if the symbol foo itself is preserved (unless there are other non-dead references to it, of course, but then those references should cause foo to be marked alive as well).<br>
<br>
I haven't actually studied how LLD models a defsym though, so my mental model might be way off. I apologize for not having done so before replying, but it'll be at least a few days before I have the chance to get to that. If my mental model is accurate, preserving
 the needed section for defsym should just fall out naturally from it (without needing to give the target of a defsym any special treatment), but if not, the whole thing might be much more complicated and not worth it.<br>
<br>
On 12/4/19, 1:35 AM, "Peter Smith" <<a href="mailto:peter.smith@linaro.org" target="_blank">peter.smith@linaro.org</a>> wrote:<br>
<br>
    On Wed, 4 Dec 2019 at 07:05, Fāng-ruì Sòng <<a href="mailto:maskray@google.com" target="_blank">maskray@google.com</a>> wrote:<br>
    ><br>
    > On Tue, Dec 3, 2019 at 7:02 PM Shoaib Meenai via llvm-dev<br>
    > <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br>
    > ><br>
    > > LLD treats any symbol referenced from a linker script as a GC root, which makes sense. Unfortunately, it also processes --defsym as a linker script fragment internally, so all target symbols of a --defsym also get treated as GC roots (i.e., if you have
 something like --defsym SRC=TGT, TGT will become a GC root). I believe this to be unnecessary for defsym specifically, since you're just aliasing a symbol, and if the original or aliased symbols are referenced from anywhere, the symbol's section will get preserved
 anyway. (There's also cases where the defsym target can be an expression instead of just a symbol name, which I admittedly haven't thought about too hard, but I believe the same logic  should hold in terms of any needed sections getting preserved regardless.)
 I want to change defsym targets specifically to not be considered as GC roots, so that they can be dead code eliminated. Does anyone foresee any issues with this?<br>
    ><br>
    > % cat a.s<br>
    > .globl _start, foo, bar<br>
    > .text; _start: movabs $d, %rax<br>
    > .section .text_foo,"ax"; foo: ret<br>
    > .section .text_bar,"ax"; bar: nop<br>
    > % as a.s -o a.o<br>
    ><br>
    > % ld.bfd a.o --defsym d=foo --gc-sections -o a => .text_foo is retained<br>
    > % ld.bfd a.o --defsym d=bar --gc-sections -o a => .text_bar is retained<br>
    > % ld.bfd a.o --defsym d=1 --gc-sections -o a => Neither .text_foo nor<br>
    > .text_bar is retained<br>
    > % ld.bfd a.o --defsym c=foo --defsym d=1 --gc-sections -o a => Neither<br>
    > .text_foo nor .text_bar is retained; lld will retain .text_foo.<br>
    ><br>
    > For --defsym from=an_expression_with_to, GNU ld appears to add a<br>
    > reference from 'from' to 'to'. lld's behavior<br>
    > (<a href="https://urldefense.proofpoint.com/v2/url?u=https-3A__reviews.llvm.org_D34195&d=DwIFaQ&c=5VD0RTtNlTh3ycd41b3MUw&r=o3kDXzdBUE3ljQXKeTWOMw&m=MpiPCWMhZJFZg0s-e1lhHtcCr-BLzG6zbJ44d0isoMc&s=7j_hrwm8LBMCPNgU_IXbhye_YKPQFgGJlU3YMAtWGLE&e=" target="_blank">https://urldefense.proofpoint.com/v2/url?u=https-3A__reviews.llvm.org_D34195&d=DwIFaQ&c=5VD0RTtNlTh3ycd41b3MUw&r=o3kDXzdBUE3ljQXKeTWOMw&m=MpiPCWMhZJFZg0s-e1lhHtcCr-BLzG6zbJ44d0isoMc&s=7j_hrwm8LBMCPNgU_IXbhye_YKPQFgGJlU3YMAtWGLE&e=</a>
 ) is more conservative.<br>
    ><br>
    > If we stop treating script->referencedSymbols as GC roots,<br>
    > instructions like `movabs $d, %rax` will no longer be able to access<br>
    > the intended section. We can tweak our behavior to be like GNU ld, but<br>
    > the additional complexity may not be worthwhile.<br>
<br>
    I think it would be a step too far for defsym symbol=expression to<br>
    have no effect on GC. I'd expect that something like defsym foo=bar is<br>
    used because some live code refers to foo, but does not refer to bar,<br>
    so ideally we'd like defsym foo=bar to keep bar live. I've seen this<br>
    idiom used in embedded systems in the presence of binary only<br>
    libraries. It is true that the programmer can always go the extra mile<br>
    to force bar to be marked live, however I think the expectation would<br>
    be defsym foo=bar would do it.<br>
<br>
    I think the GNU ld behaviour is reasonable. If nothing refers to<br>
    either foo or bar then there is no reason to mark them live. On the<br>
    implementation cost-benefit trade off I guess we won't know until<br>
    there is a prototype, and some idea of what implementing it will save<br>
    on a real example.<br>
<br>
    Peter<br>
<br>
<o:p></o:p></p>
</blockquote>
</div>
<p class="MsoNormal"><br clear="all">
<o:p></o:p></p>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<p class="MsoNormal">-- <o:p></o:p></p>
<div>
<div>
<p class="MsoNormal"><span style="font-family:"MS Gothic"">宋方睿</span><o:p></o:p></p>
</div>
</div>
</div>
</body>
</html>