<div dir="ltr"><div dir="ltr"><div dir="ltr">The final size of jumps is determined very late during binary emission after all Machine passes have run. It's an iterative process as changing the size of one jump can effect the size needed for other jumps that cross it. This process is known as relaxation. The opcode starts as the instruction known as JMP_1, JNE_1, JA_1, etc. and it is changed by the MC layer to JMP_4, JNE_4, JA_4, etc. if the relative distance won't fit in one byte.<div><br></div><div>I think you can force all jumps to be 5 bytes by passing -mrelax-all to clang.</div><div><br></div><div>There's a informative blog post about assembly relaxation here <a href="https://eli.thegreenplace.net/2013/01/03/assembler-relaxation">https://eli.thegreenplace.net/2013/01/03/assembler-relaxation</a></div><div><br><div><div><div dir="ltr" class="gmail_signature">~Craig</div></div><br></div></div></div></div></div><br><div class="gmail_quote"><div dir="ltr">On Thu, Oct 25, 2018 at 12:31 PM K Jelesnianski via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Dear Paul,<div><br></div><div>I guess I used the wrong term when I said "alignment". I exactly am looking to ensure that all the instructions I am transforming (specifically jumps: JMP64r, JMP64m, etc) to become 5 bytes long, with JMP the length can vary from 2 bytes long to 5 from what I am seeing when I objdump the executable I am trying to instrument. I want to get the size of the MachineInstr I have created so that I know how many extra NOOPs I should insert afterwards to maintain a 5 byte total size. </div></div><br><div class="gmail_quote"><div dir="ltr">On Thu, Oct 25, 2018 at 2:58 PM <<a href="mailto:paul.robinson@sony.com" target="_blank">paul.robinson@sony.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div lang="EN-US" link="blue" vlink="purple">
<div class="m_4131882971086958785m_-2323080522071985635WordSection1">
<p class="MsoNormal"><a name="m_4131882971086958785_m_-2323080522071985635__MailEndCompose"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">There should be a way for you to specify a desired alignment, either on the machine instruction itself, or as a directive inserted
prior to the instruction. You should not need to compute the exact number of bytes yourself, the MC assembler phase knows how to insert appropriate padding.<u></u><u></u></span></a></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">--paulr<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<div style="border:none;border-left:solid blue 1.5pt;padding:0in 0in 0in 4.0pt">
<div>
<div style="border:none;border-top:solid #b5c4df 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif"">From:</span></b><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif""> llvm-dev [mailto:<a href="mailto:llvm-dev-bounces@lists.llvm.org" target="_blank">llvm-dev-bounces@lists.llvm.org</a>]
<b>On Behalf Of </b>K Jelesnianski via llvm-dev<br>
<b>Sent:</b> Thursday, October 25, 2018 10:19 AM<br>
<b>To:</b> llvm-dev<br>
<b>Subject:</b> [llvm-dev] How to get emitted size of MachineInstr in MachineFunction pass?<u></u><u></u></span></p>
</div>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<div>
<div>
<div>
<p class="MsoNormal">Dear dev-list,<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">I currently have a X86 machineFunctionPass and realized there are more corner cases than I realized. I need to keep my custom jump transformations aligned and therefore would like to fill the rest of the space with NOOPs.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">I understand that hexcode code ASM is emitted/encoded by the ASMPrinter. In the past I have gotten the size via 2 compilations (1 to collect asm size data, 2nd to perform fixup alignment where needed), but am looking for a cleaner self
contained solution.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">In the past I used this code, creating a function in X86AsmPrinter class<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="margin-bottom:12.0pt">unsigned int instSize;<br>
SmallString<256> Code;<br>
SmallVector<MCFixup, 4> Fixups; <br>
raw_svector_ostream VecOS(Code); <br>
CodeEmitter->encodeInstruction(Inst, VecOS, Fixups, STI);<br>
instSize = Code.size();<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">So far, I have already tried to get the size of the opcode at least via:<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">size= MachineInstr->getDesc().getSize();<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">but it returns zero, indicating that the encoding size cannot be determined yet according to the LLVM doxygen, but why? We are already know our target ISA is X86 and if I do a dump of the MachineInstr, the opcodes and operands/registers
seem already finalized....<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">So the questions are:<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">1) Is it possible to temporarily encode a given MachineInstr to a temporary buffer in a machineFunctionPass?? If so, how?<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">2) Is it possible to create an instance of CodeEmitter within a machineFunctionPass to make it possible to run the above code? If so, how?<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">Thanks in advance.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">Sincerely,<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">K Jelesnianski<u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</blockquote></div>
_______________________________________________<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/mailman/listinfo/llvm-dev</a><br>
</blockquote></div>