<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Jan 19, 2017 at 1:56 PM, Dmitry Golovin 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 all!<br>
<br>
Recently there was a discussion on this mailing list about linking Linux and GRUB with LLD.<br>
<br>
I have actually tried to link Linux kernel with LLD and (with some modifications) it linked successfully (but didn't run well).<br></blockquote><div><br></div><div>Awesome! Thanks for digging into this!<br><br></div><div>When Michael, Davide, and I were debugging the issues with running the LLD-linked FreeBSD kernel, we found that there was really no alternative but to get familiar with the bootloader code and do printf debugging to understand what the bootloader didn't like. (even hooking into qemu's debugger integration didn't work; it is just too early for the usual debugger integration to work I think (interrupts and stuff not in a "sane" state etc.)). I'm happy to share more info about that experience if it would be useful to you.</div><div><br></div><div>I'm guessing Ed (CC'd) also had some similar experiences getting the FreeBSD bootloaders working with LLD and might have some advice as well w.r.t. a good workflow and what tools to use.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
Here is the list of modifications I had to do in order to link the kernel (I have used llvmlinux with clang and mainline with gcc, the results are similar):<br>
<br>
1. LLD patches:<br>
- D28094 (Implemented support for R_386_PC8/R_386_8 relocations)<br>
- D28857 (Allow relative relocations to a absolute value defined in linker script)<br>
- [not actually used] D28612 (Added support for --emit-relocs)<br>
<br>
2. Kernel configuration. I tried "make tinyconfig" and then changed to CONFIG_64BIT=y with menuconfig. I wanted the minimal kernel configuration for x86_64. After I get it working, I will try defconfig.<br>
<br>
3. In arch/x86/Makefile changed the M16_CFLAGS to just "-m16". I think that cc-option should test if compiler supports '-m16' option and if it does, use it, if it doesn't use '-m32 -Wa,./arch/x86/boot/code16gcc.<wbr>h' instead. I don't know why, but this test failed for me and LLD didn't accept this header, so I just simply changed the flag to '-m16' because I didn't want to investigate why did the test failed. It's just a quick workaround, but it works.<br></blockquote><div><br></div><div>These seem to be compiler flags. It is probably best to use GCC as the compiler for now (i.e. only swap out the linker). That will allow focusing on the LLD issues. Clang (and other LLVM tools) have their own issues with GNU compatibility, but things will be more difficult to debug when there are more moving pieces.</div><div><br></div><div>-- Sean Silva</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
3. In arch/x86/boot/Makefile and arch/x86/realmode/rm/Makefile added "-m elf_i386" to LDFLAGS_setup.elf and LDFLAGS_realmode.elf accordingly. The problem is that the linker is invoked as "ld.lld -m elf_x86_64" and it ignores the OUTPUT_FORMAT from linker scripts. Being invoked as "ld.lld -m elf_x86_64 -m elf_i386" doesn't break anything and LLD even produces the right format of the binaries. Again a quick workaround, but it works.<br>
<br>
4. In arch/x86/realmode/rm/Makefile removed "--emit-relocs" from LDFLAGS_realmode.elf. This is the problem with D28612 — I applied the patch to support --emit-relocs option, but LLD segfaulted. Reported here: <a href="https://llvm.org/bugs/show_bug.cgi?id=31579#c8" rel="noreferrer" target="_blank">https://llvm.org/bugs/show_<wbr>bug.cgi?id=31579#c8</a><br>
<br>
5. In arch/x86/kernel/vmlinux.lds.S commented out the "CONSTRUCTORS", because LLD doesn't support it.<br>
<br>
6. In arch/x86/boot/setup.ld replaced 5*512 with precalculated value of 2560 because it doesn't seem that LLD supports math inside ASSERT in linker scripts.<br>
<br>
7. In arch/x86/boot/setup.ld commented out the line ". = ASSERT(_end <= 0x8000, "Setup too big!");" because it seems to always fire this assertion, and the kernel is not going to be working anyway, so I just want it linked even if it will be unusable.<br>
<br>
8. In arch/x86/lib/copy_user_64.S removed the line "bad_from_user:" (probably it was just a mistake), in arch/x86/kernel/vmlinux.lds.S removed semicolon after "*(.apicdrivers)" (also probably just a mistake).<br>
<br>
Finally the kernel was built and it obviously didn't run (only printed out "No setup signature found..." but this is some result as well). Probably, the result could be better if the --emit-relocs option didn't fail and CONSTRUCTORS were supported. I really don't know what to do about the assertion that I have commented out.<br>
<br>
I also tried using tools provided by LLVM instead of binutils (HOSTCC=clang HOSTLD=ld.lld CC=clang LD=ld.lld AR=llvm-ar NM=llvm-nm), it seems to work with llvmlinux, but clang will not work with the mainline. Clang doesn't seem to use integrated assembler, so I had to write a simple wrapper script around llvm-mc to use instead GNU as. I also have few patches to make llvm-objcopy to work with latest LLVM and support needed subset of flags, will publish it soon.<br>
<br>
You can find my workarounds as a patch (the kernel will not work with this applied!) and my kernel config attached.<br>
<br>
Regards,<br>
Dmitry<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></div>