<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Sep 21, 2017, at 10:10 PM, Saleem Abdulrasool &lt;<a href="mailto:compnerd@compnerd.org" class="">compnerd@compnerd.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">On Thu, Sep 21, 2017 at 5:18 PM, John McCall <span dir="ltr" class="">&lt;<a href="mailto:rjmccall@apple.com" target="_blank" class="">rjmccall@apple.com</a>&gt;</span> wrote:<br class=""><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class=""><span class=""><div class=""><blockquote type="cite" class=""><div class="">On Sep 21, 2017, at 1:26 PM, Saleem Abdulrasool via swift-dev &lt;<a href="mailto:swift-dev@swift.org" target="_blank" class="">swift-dev@swift.org</a>&gt; wrote:</div><div class=""><div dir="ltr" class="">On Thu, Sep 21, 2017 at 12:04 PM, Joe Groff <span dir="ltr" class="">&lt;<a href="mailto:jgroff@apple.com" target="_blank" class="">jgroff@apple.com</a>&gt;</span> wrote:<br class=""><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word;line-break:after-white-space" class=""><br class=""><div class=""><span class=""><br class=""><blockquote type="cite" class=""><div class="">On Sep 21, 2017, at 11:49 AM, Saleem Abdulrasool &lt;<a href="mailto:compnerd@compnerd.org" target="_blank" class="">compnerd@compnerd.org</a>&gt; wrote:</div><br class="m_-249900170251401570m_-763627060659209996Apple-interchange-newline"><div class=""><div dir="ltr" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class="">On Thu, Sep 21, 2017 at 10:53 AM, Joe Groff<span class="m_-249900170251401570m_-763627060659209996Apple-converted-space">&nbsp;</span><span dir="ltr" class="">&lt;<a href="mailto:jgroff@apple.com" target="_blank" class="">jgroff@apple.com</a>&gt;</span><span class="m_-249900170251401570m_-763627060659209996Apple-converted-space">&nbsp;</span>wrote<wbr class="">:<br class=""><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word;line-break:after-white-space" class=""><span class=""><br class=""><div class=""><br class=""><blockquote type="cite" class=""><div class="">On Sep 21, 2017, at 9:32 AM, Saleem Abdulrasool via swift-dev &lt;<a href="mailto:swift-dev@swift.org" target="_blank" class="">swift-dev@swift.org</a>&gt; wrote:</div><br class="m_-249900170251401570m_-763627060659209996m_-1276541751748296627Apple-interchange-newline"><div class=""><div dir="ltr" class="">Hello,<div class=""><br class=""></div><div class="">The current layout for the swift metadata for structure types, as emitted, seems to be unrepresentable in PE/COFF (at least for x86_64).&nbsp; There is a partial listing of the generated code following the message for reference.</div><div class=""><br class=""></div><div class="">When building the standard library, LLVM encounters a relocation which cannot be represented.&nbsp; Tracking down the relocation led to the type metadata for SwiftNSOperatingSystemVersion.<wbr class="">&nbsp;<span class="m_-249900170251401570m_-763627060659209996Apple-converted-space">&nbsp;</span>The metadata here is&nbsp;<span style="font-family:monospace,monospace" class="">_T0SC30_SwiftNSOperatingSys<wbr class="">temVersionVN</span><font face="arial, helvetica, sans-serif" class="">.&nbsp; At +32-bytes we find the Kind (1).&nbsp; So, this is a struct metadata type.&nbsp; Thus at Offset 1 (+40 bytes) we have the nominal type descriptor reference.&nbsp; This is the relocation which we fail to represent correctly.&nbsp; If I'm not mistaken, it seems that the field is supposed to be a relative offset to the nominal type descriptor.&nbsp; However, currently, the nominal type descriptor is emitted in a different section (.rodata) as opposed to the type descriptor (.data).&nbsp; This cross-section relocation cannot be represented in the file format.</font></div><div class=""><font face="arial, helvetica, sans-serif" class=""><br class=""></font></div><div class=""><font face="arial, helvetica, sans-serif" class="">My understanding is that the type metadata will be adjusted during the load for the field offsets.&nbsp; Furthermore, my guess is that the relative offset is used to encode the location to avoid a relocation for the load address base.&nbsp; In the case of windows, the based relocations are a given, and I'm not sure if there is a better approach to be taken.&nbsp; There are a couple of solutions which immediately spring to mind: moving the nominal type descriptor into the (RW) data segment and the other is to adjust the ABI to use an absolute relocation which would be rebased.&nbsp; Given that the type metadata may be adjusted means that we cannot emit it into the RO data segment.&nbsp; Is there another solution that I am overlooking which may be simpler or better?</font></div></div></div></blockquote><br class=""></div></span><div class="">IIRC, this came up when someone was trying to port Swift to Windows on ARM as well, and they were able to conditionalize the code so that we used absolute pointers on Windows/ARM, and we may have to do the same on Windows in general. It may be somewhat more complicated on Win64 since we generally assume that relative references can be 32-bit, whereas an absolute reference will be 64-bit, so some formats may have to change layout to make this work too. I believe Windows' executable loader still ultimately maps the final PE image contiguously, so alternatively, you could conceivably build a Swift toolchain that used ELF or Mach-O or some other format with better support for PIC as the intermediate object format and still linked a final PE executable. Using relative references should still be a win on Windows both because of the size benefit of being 32-bit and the fact that they don't need to be slid when running under ASLR or when a DLL needs to be rebased.</div><span class="m_-249900170251401570m_-763627060659209996HOEnZb"><font color="#888888" class=""><div class=""><br class=""></div></font></span></div></blockquote><div class=""><br class=""></div><div class="">Yeah, I tracked down the relativePointer thing.&nbsp; There is a nice subtle little warning that it is not fully portable :-).&nbsp; Would you happen to have a pointer to where the adjustment for the absolute pointers on WoA is?</div><div class=""><br class=""></div><div class="">You are correct that the image should be contiugously mapped on Windows.&nbsp; The idea of MachO as an intermediatary is rather intriguing.&nbsp; Thinking longer term, maybe we want to use that as a global solution?&nbsp; It would also provide a nicer autolinking mechanism for ELF which is the one target which currently is missing this functionality.&nbsp; However, if Im not mistaken, this would require a MachO linker (and the only current viable MachO linker would be ld64).&nbsp; The MachO binary would then need to be converted into ELF or COFF.&nbsp; This seems like it could take a while to implement though, but would not really break ABI, so pushing that off to later may be wise.</div></div></div></div></div></blockquote><div class=""><br class=""></div></span><div class="">Intriguingly, LLVM does support `*-*-win32-macho` as a target triple already, though I don't know what Mach-O to PE linker (if any) that's intended to be used with. We implemented relative references using current-position-relative offsets for Darwin and Linux both because that still allows for a fairly convenient pointer-like C++ API for working with relative offsets, and because the established toolchains on those platforms already have to support PIC so had most of the relocations we needed to make them work already; is there another base we could use for relative offsets on Windows that would fit in the set of relocations supported by standard COFF linkers?</div></div></div></blockquote><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Yes, the `-windows-macho` target is used for UEFI :-).&nbsp; The MachO binary is translated later to PE/COFF as required by the UEFI specification.</div><div class=""><br class=""></div><div class="">There are only two relocation types which can be used for relative displacements: __ImageBase relative (IMAGE_REL_*_ADDR32NB) and section relative (IMAGE_REL_*_SECREL) which are relative to the beginning of the section.&nbsp; The latter is why I mentioned that moving them into the same section could be a solution as that would allow the relative distance to be encoded.&nbsp; Unfortunately, the section relative relocation is relative to the section within which the symbol is.</div></div></div></div></div></blockquote><div class=""><br class=""></div></div></span>What's wrong with IMAGE_REL_AMD64_REL32?&nbsp; We'd have to adjust the relative-pointer logic to store an offset from the end of the relative pointer instead of the beginning, but it doesn't seem to have a section requirement.</div></blockquote><div class=""><br class=""></div><div class="">Hmm, is it possible to use RIP relative addressing in data?&nbsp; If so, yes, that could work.</div></div></div></div></div></blockquote><div><br class=""></div></div>There's no inherent reason, but I wouldn't put it past the linker to fall over and die. &nbsp;But it should at least be section-agnostic about the target, since this is likely to be used for all sorts of PC-relative addressing.<div class=""><br class=""></div><div class="">John.</div></body></html>