<div dir="ltr">Apologies - I apparently remembered part of the issue incorrectly, so this ended up quite confusing. The problem comes when referencing labels in a different section of the binary. To clarify, if I assemble the code:<div><br></div><div><font face="monospace">.data</font></div><div><font face="monospace">foo BYTE 5</font></div><div><font face="monospace">.code</font></div><div><font face="monospace">mov eax, foo</font></div><div><br></div><div>with Microsoft's ml64.exe, it emits an object file disassembling to:</div><div></div><div><br></div><font face="monospace">       0:       8b 05 00 00 00 00       mov     eax, dword ptr [rip]<br></font><div><font face="monospace">                000000000000000b:  IMAGE_REL_AMD64_REL32        foo</font></div><div><br></div><div>On the other hand, if I use my current local draft of llvm-ml, I get a different result. I actually get the same result as I do for llvm-mc, using the corresponding code:</div><div></div><div><font face="monospace"><br></font></div><div><font face="monospace">.data</font></div><div><font face="monospace">foo:</font></div><div><font face="monospace">.byte 5</font></div><div><font face="monospace">.text</font></div><div><font face="monospace">.intel_syntax</font></div><div><font face="monospace">mov eax, foo</font></div><div><br></div><div>Either way, LLVM emits an object file with disassembly (and relocation) as follows:</div><div><font face="monospace"><br></font></div><div><font face="monospace">       0:       8b 04 25 00 00 00 00    mov     eax, dword ptr [0]<br>                0000000000000003:  IMAGE_REL_AMD64_ADDR32       foo</font><br></div><div><br></div><div><span style="font-family:arial,sans-serif">To replicate the results from ml64.exe with LLVM, I instead need to use</span><br></div><div><font face="arial, sans-serif"><br></font></div><div><font face="monospace">mov eax, [foo + rip]</font></div><div><font face="arial, sans-serif"><br></font></div><div><font face="arial, sans-serif">in place of </font><font face="monospace">mov eax, foo</font><font face="arial, sans-serif">. At least when building with llvm-ml, we need to mimic ml.exe's approach; a reference to a symbol in another section should use the relative addressing mode.</font></div><div><font face="arial, sans-serif"><br></font></div><div><font face="arial, sans-serif">My first attempt to fix this was very clumsy - when in MASM mode, I forced all expressions without a base register to presume RIP. Unfortunately, that breaks any attempt to use "jcc", since it turns label references into absolute memory references with a base register (and the "jcc" family doesn't accept absolute memory operands). Any suggestions for how I can fix the issue described here without breaking "jcc"?</font></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Jan 21, 2020 at 3:43 PM Eli Friedman <<a href="mailto:efriedma@quicinc.com" target="_blank">efriedma@quicinc.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">





<div lang="EN-US">
<div>
<p class="MsoNormal">All immediate jump instructions on x86 (call/jmp/jcc) have a relative offset operand.  The destination is, in some sense, “rip-relative”, but we don’t represent it like that in LLVM.  If you look at the TableGen descriptions, jumps use
 brtarget32, and calls use i32imm_pcrel.  In both Microsoft and GNU assembly syntax, this is something like “call baz”.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">“call”/”jmp” also have a register/memory form, for indirect calls.  In 64-bit, this allows rip-relative references, to call a function pointer stored in a global variable.  In Microsoft assembly syntax, this is “call QWORD PTR baz”. In
 GNU assembly syntax, this is “call *baz(%rip)”.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">For 64-bit x86, any reference to a global has to be a rip-relative address (since all 64-bit programs are position-independent), but on 32-bit x86, it’s also possible to refer to the address of a variable using something like “add eax,
 OFFSET baz”.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">For globals which are explicitly labeled “PTR” or “OFFSET”, the correct representation should be unambiguous, and it should be easy to print appropriate error messages.  For other cases, I’m not sure what the inference rules are.  It might
 vary depending on the opcode.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">-Eli<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<div style="border-top:none;border-right:none;border-bottom:none;border-left:1.5pt solid blue;padding:0in 0in 0in 4pt">
<div>
<div style="border-right:none;border-bottom:none;border-left:none;border-top:1pt solid rgb(225,225,225);padding:3pt 0in 0in">
<p class="MsoNormal"><b>From:</b> llvm-dev <<a href="mailto:llvm-dev-bounces@lists.llvm.org" target="_blank">llvm-dev-bounces@lists.llvm.org</a>> <b>On Behalf Of
</b>Eric Astor via llvm-dev<br>
<b>Sent:</b> Monday, January 20, 2020 6:26 PM<br>
<b>To:</b> LLVM-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>><br>
<b>Subject:</b> [EXT] [llvm-dev] MASM & RIP-relative addressing<u></u><u></u></p>
</div>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<p class="MsoNormal">Hi all,<u></u><u></u></p>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">Continuing work on llvm-ml (a MASM assembler)... and my latest obstacle is in enabling MASM's convention that (unless specified) all memory location references should be RIP-relative. Without it, we emit the wrong instructions for "call",
 "jmp", etc., and anything we build fails at the linking stage.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">My best attempt at this so far is a small patch to X86AsmParser.cpp - just taking any Intel expression with no specified base register and switching it to use RIP - and this works alright. There's at least one exception: it breaks the "jcc"
 instructions, at least "jcc <label>". The issue seems to be that the "jcc" family exclusively takes a relative offset, never an absolute reference... so adding a base register causes the operand not to match. ("jcc" is always RIP-relative anyway.)<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">I'm not very familiar with the operand-matching logic, and am still pretty new to LLVM as a whole. Are there more X86 instructions this will interact badly with? Any thoughts on how this could be handled better?<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">If this is mostly a valid approach, might there be a way to change the operand type of "jcc" to accept offset(base) operands, as long as base == X86::RIP, then ignore the RIP bit?<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">Thanks,<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">- Eric<u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>

</blockquote></div>