<div dir="ltr">When I was at PlayStation, I remember that we had a proprietary linker with the subsections-via-symbols behavior. To avoid private patches in the LLVM backend due to this, Rafael came up with a design (and I think did quite a bit of refactoring upstream to enable it, though I think it applied only to ELF) where subsections-via-symbols could be implemented in terms of -ffunctions-sections/-fdata-sections.<div><br></div><div>IIRC, essentially the model was that everything was done the same as in -ffunction-sections/-fdata-sections but at the very end only a single section header was emitted (and things were inherently laid out in such a way that this was a valid thing to do). In otherwords, the subsections-via-symbols behavior was clearly implemented as basically an optimization to eliminate the need for large numbers of sections (when the linker could be assumed to do subsections-via-symbols), but otherwise didn't affect the rest of the implementation.<div><div><br></div><div>-- Sean Silva</div></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Mar 6, 2017 at 7:56 PM, James Y Knight via llvm-dev <span dir="ltr"><<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto"><div dir="ltr">Oh, that's great that it's possible to implement properly, now. Does it actually work for <div><br></div><div>It'd be cool if LLVM hooked up its generic section handling support to this feature now, so that the only global symbols that *didn't* get marked as .alt_entry were those at the beginning of what llvm would consider sections.</div><div><br></div><div></div><div>Then apple platforms could behave sanely, like all other platforms do, only with -f{function,data}-sections defaulted to on instead of off.</div><div><br></div><div>(That is, if you specify -fno-function-sections -fno-data-sections, it could mark nearly *everything* as .alt_entry -- except the first symbol in the object file)</div><div><br></div></div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Mar 6, 2017 at 2:35 PM, Peter Collingbourne <span dir="ltr"><<a href="mailto:peter@pcc.me.uk" target="_blank">peter@pcc.me.uk</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">That is in theory what omitting the .subsections_via_symbols directive is supposed to do, but in an experiment I ran a year or two ago I found that the Mach-O linker was still dead stripping on symbol boundaries with this directive omitted.<div><br></div><div>In any case, a more precise approach has more recently (~a few months ago) become possible. There is a relatively new asm directive called .altentry that, as I understand it, tells the linker to disregard a given symbol as a section boundary (LLVM already uses this for aliases pointing into the middle of a global). So what you would do is to use .altentry on the function symbol, with an internal symbol appearing before the prefix data to ensure that it is not considered part of the body of the previous function.</div><div><br></div><div>Peter</div></div><div class="gmail_extra"><div><div class="m_-3064663435976188714m_37750768197969627h5"><br><div class="gmail_quote">On Mon, Mar 6, 2017 at 11:19 AM, James Y Knight via llvm-dev <span dir="ltr"><<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</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">AFAIK, this cannot actually work on Apple platforms, because its object file format (Mach-O) doesn't use sections to determine the ranges of code/data to keep together, but instead _infers_ boundaries based on the range between global symbols in the symbol table.<div><div><br></div><div>So, the symbol pointing to the beginning of @main *necessarily* makes that be a section boundary.</div></div><div><br></div><div>I think the best that could be done in LLVM is to not emit the ".subsections_via_symbols" asm directive (effectively disabling dead stripping on that object) if any prefix data exists. Currently it emits that flag unconditionally for MachO.</div></div><div class="m_-3064663435976188714m_37750768197969627m_-2098018759435103914HOEnZb"><div class="m_-3064663435976188714m_37750768197969627m_-2098018759435103914h5"><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Mar 6, 2017 at 4:40 AM, Moritz Angermann via llvm-dev <span dir="ltr"><<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br>
<br>
I just came across a rather annoying behavior with llvm 3.9. Assuming the following<br>
samle code in test.ll:<br>
<br>
; Lets have some global int x = 4<br>
@x = global i32 10, align 4<br>
; and two strings "p = %d\n" for the prefix data,<br>
; as well as "x = %d\n" to print the (global) x value.<br>
@.str = private unnamed_addr constant [8 x i8] c"x = %d\0A\00", align 1<br>
@.str2 = private unnamed_addr constant [8 x i8] c"p = %d\0A\00", align 1<br>
<br>
; declare printf, we'll use this later for printf style debugging.<br>
declare i32 @printf(i8*, ...)<br>
<br>
; define a main function.<br>
define i32 @main() prefix i32 123 {<br>
; obtain a i32 pointer to the main function.<br>
; the prefix data is right before that pointer.<br>
%main = bitcast i32 ()* @main to i32*<br>
<br>
; use the gep, to cmpute the start of the prefix data.<br>
%prefix_ptr = getelementptr inbounds i32, i32* %main, i32 -1<br>
; and load it.<br>
%prefix_val = load i32, i32* %prefix_ptr<br>
<br>
; print that value.<br>
%ret = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str2, i32 0, i32 0), i32 %prefix_val)<br>
<br>
; similarly let's do the same with the global x.<br>
%1 = alloca i32, align 4<br>
store i32 0, i32* %1, align 4<br>
%2 = load i32, i32* @x, align 4<br>
%3 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i32 0, i32 0), i32 %2)<br>
ret i32 0<br>
}<br>
<br>
gives the following result (expected)<br>
<br>
$ clang test.ll<br>
$ ./a.out<br>
p = 123<br>
x = 10<br>
<br>
however, with -dead_strip on macOS, we see the following:<br>
<br>
$ clang test.ll -dead_strip<br>
$ ./a.out<br>
p = 0<br>
x = 10<br>
<br>
Thus I believe we are incorrectly stripping prefix data when linking with -dead_strip on macOS.<br>
<br>
As I do not have a bugzilla account, and hence cannot post this as a proper bug report.<br>
<br>
Cheers,<br>
Moritz<br>
______________________________<wbr>_________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-dev</a><br>
</blockquote></div><br></div>
</div></div><br>______________________________<wbr>_________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-dev</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div></div></div><span class="m_-3064663435976188714m_37750768197969627HOEnZb"><font color="#888888">-- <br><div class="m_-3064663435976188714m_37750768197969627m_-2098018759435103914gmail_signature" data-smartmail="gmail_signature"><div dir="ltr">-- <div>Peter</div></div></div>
</font></span></div>
</blockquote></div><br></div>
</div></div><br>______________________________<wbr>_________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-dev</a><br>
<br></blockquote></div><br></div>