<div dir="ltr"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span style="font-size:12.8px">For example the gnu linker provides the section with the stub code and the offset for injection.</span></blockquote><div>Meant to say dlltool here not gnu linker :) </div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Jul 25, 2015 at 11:17 AM, Martell Malone <span dir="ltr"><<a href="mailto:martellmalone@gmail.com" target="_blank">martellmalone@gmail.com</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">Hey guys,<div><br></div><div>So I was able to modify dlltool to produce the exact same layout as lib.exe with the same section numbers etc.</div><div>I've first managed to first create the correct section so that lld gives me link errors and then resolve those errors to create an exe.</div><div><br></div><div>This is one thing that is still missing that sticks out like a sore thumb</div><div>In the actual imports of the functions the objects are very different</div><div><br></div><div>As you can see lib.exe generates a very simple function import object.</div><div><br></div><div><div>  IMPORT_OBJECT[1-N]</div><div>    -> contains an IMPORT_OBJECT_HEADER and DATA</div><div>    data example -> _MessageBoxA.USER32.dll</div></div><div><br></div><div>dlltool however doesn't create an import object but infact has a complete IMAGE_FILE</div><div><br></div><div><div>  IMPORT_OBJECT[1-N]</div><div>    -> contains an IMAGE_FILE_HEADER and is a full object with SECTIONS</div><div>    the .text section has the stub code.</div><div>    the .idata$6 section has the name of the function</div><div>    it starts with 0x01 0x4D then MessageBoxA without an underscore</div><div>    The dll is not referenced and is assumed to be the one listed in the first object in .idata$6</div></div><div><br></div><div><br></div><div>This is where the actual problem lies because the dll functions never get resolved</div><div>Have you guys got any thoughts on weither we should support this or not within lld and if so how ?</div><div><br></div><div>There are some interesting things here</div><div>For example the gnu linker provides the section with the stub code and the offset for injection.</div><div>Should we be using the stub code provided in the text section or should we just ignore it and use the one in lld</div><div>They are the same anyway but its just interesting how the dlltool version gives us that option.</div><div><br></div><div>Kind Regards</div><span class="HOEnZb"><font color="#888888"><div>Martell <br></div><div><br></div></font></span></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jul 24, 2015 at 10:52 AM, Martell Malone <span dir="ltr"><<a href="mailto:martellmalone@gmail.com" target="_blank">martellmalone@gmail.com</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"><div><div>After some more digging and creating a few testcases in lld I have narrowed it down to<br><br></div>The fact that dlltool generates <br><br><pre>Contents of section .idata$7:
 0000 55534552 33322e64 6c6c0000           USER32.dll..<br><br></pre><pre>Where as lld expects<br><br>Contents of section .idata$6:
 0000 55534552 33322e64 6c6c0000           USER32.dll..<br><br></pre><pre>I recreated the hello64.test using dlltool for the lib and here is the asm dump of the final exe<br><br>hello64gnu.exe:      file format COFF-x86-64<br><br>Disassembly of section .text:<br>.text:<br>    3000:       ff 25 26 f0 ff ff       jmpq    *-4058(%rip)<br>    3006:       90      nop<br>    3007:       90      nop<br>    3008:       ff 25 26 f0 ff ff       jmpq    *-4058(%rip)<br>    300e:       90      nop<br>    300f:       90      nop<br>    3010:       48 83 ec 28     subq    $40, %rsp<br>    3014:       48 c7 c1 00 00 00 00    movq    $0, %rcx<br>    301b:       48 8d 15 e4 df ff ff    leaq    -8220(%rip), %rdx<br>    3022:       4c 8d 05 d7 df ff ff    leaq    -8233(%rip), %r8<br>    3029:       41 b9 00 00 00 00       movl    $0, %r9d<br>    302f:       e8 d4 ff ff ff  callq   -44<br>    3034:       b9 00 00 00 00  movl    $0, %ecx<br>    3039:       e8 c2 ff ff ff  callq   -62<br><br></pre><pre>So I am fairly certain this is the cause.<br></pre><br><pre><br></pre><pre>Many Thanks<span><font color="#888888"><br></font></span></pre><span><font color="#888888"><pre>Martell<br></pre></font></span></div></div><div><div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Jul 23, 2015 at 4:22 PM, Martell Malone <span dir="ltr"><<a href="mailto:martellmalone@gmail.com" target="_blank">martellmalone@gmail.com</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">I forgot to attach the notes.txt with the objdump.<br><br></div><div><div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Jul 23, 2015 at 3:55 PM, Martell Malone <span dir="ltr"><<a href="mailto:martellmalone@gmail.com" target="_blank">martellmalone@gmail.com</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">Hi again rui, :)<div><br></div><div>I've got all the patches into llvm and clang for supporting mingw-w64 via compiler-rt and now we are able to build a full mingw-w64 toolchain without gcc :)</div><div>With great help from yaron and rnk.</div><div><br></div><div>I've CC'd them as they might have interest in seeing this target through with me to the end :) </div><div><br></div><div>So I have again turned my attention to LLD so that we can also remove ld as a depend for this target </div><div><br></div><div>Previously I raised the issue that while lld would infact link exe's for me the issue was the sections were malformed and thus crashed on launch.</div><div><br></div><div>I've done some debugging reguarding this and discovered it is due to differences between what MSVC's lib.exe and binutils dlltool produces</div><div><br></div><div>I have attached a very simple .def file and generated libs via dlltool along with an objdump -s log of the lib.exe version</div><div><br></div><div>I don't know too much about the internals of lld to be able to tell the exact cause of the problem but there are 3 specific differences I noted.</div><div><br></div><div><div>1. With dlltool each object has the name of a compiled .o file this is the one it generates when building the code section</div><div>With lib each object has the dll name </div><div><br></div><div>2. With dlltool section 7 is used for the dll name and the function names with lib section 6 is used</div><div><br></div><div>3. Other differences are that there is no debug section with dlltool and the text section comes first rather than last with lib (that probably doesn't matter much though)</div></div><div><br></div><div>Please see attached notes.txt with objdump's of both libs</div><div><br></div><div>Can could you give me your thoughts on this and how best we support both layouts in COFF/PECOFF ?</div><div><br></div><div>What source files should I be looking at to patch this or is this something you can support with very little effort ?</div><div><br></div><div>I don't know if you would be opposed to supporting this or not being honest dlltool should generate libs using the same section number as lib.exe but we can't exactly change that now because existing toolchains and libs have already been built with this and changing that would break them.</div><div><br></div><div>If you are opposed I would like you to still tell me what I have to change to get it working after I do that I can narrow the scope for getting a patch up streamed into binutils to fix this :)</div><div><br></div><div>Kind Regards</div><span><font color="#888888"><div>Martell</div></font></span></div>
</blockquote></div><br></div>
</div></div></blockquote></div><br></div>
</div></div></blockquote></div><br></div>
</div></div></blockquote></div><br></div>