[PATCH] D105414: Add x86 and x86_64 to the BareMetal toolchain
agvallejo created this revision. Herald added subscribers: pengfei, s.egerton, abidh, simoncook. agvallejo requested review of this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits. x86_64-unknown-elf should provide an freestanding environment usable in kernel development. Currently it defaults to Generic_ELF, which inherits from Generic_GCC, which is tied to the host platform. The Baremetal toolchain seems like a better choice to add support for x86 kernel cross-compilation without adding a lot of overrides on the command line Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D105414 Files: clang/lib/Driver/ToolChains/BareMetal.cpp Index: clang/lib/Driver/ToolChains/BareMetal.cpp === --- clang/lib/Driver/ToolChains/BareMetal.cpp +++ clang/lib/Driver/ToolChains/BareMetal.cpp @@ -139,6 +139,20 @@ return Triple.getEnvironmentName() == "elf"; } +static bool isX86BareMetal(const llvm::Triple &Triple) { + if (Triple.getArch() != llvm::Triple::x86 && + Triple.getArch() != llvm::Triple::x86_64) +return false; + + if (Triple.getVendor() != llvm::Triple::UnknownVendor) +return false; + + if (Triple.getOS() != llvm::Triple::UnknownOS) +return false; + + return Triple.getEnvironmentName() == "elf"; +} + void BareMetal::findMultilibs(const Driver &D, const llvm::Triple &Triple, const ArgList &Args) { DetectedMultilibs Result; @@ -151,7 +165,9 @@ } bool BareMetal::handlesTarget(const llvm::Triple &Triple) { - return isARMBareMetal(Triple) || isRISCVBareMetal(Triple); + return isARMBareMetal(Triple) || + isRISCVBareMetal(Triple) || + isX86BareMetal(Triple); } Tool *BareMetal::buildLinker() const { Index: clang/lib/Driver/ToolChains/BareMetal.cpp === --- clang/lib/Driver/ToolChains/BareMetal.cpp +++ clang/lib/Driver/ToolChains/BareMetal.cpp @@ -139,6 +139,20 @@ return Triple.getEnvironmentName() == "elf"; } +static bool isX86BareMetal(const llvm::Triple &Triple) { + if (Triple.getArch() != llvm::Triple::x86 && + Triple.getArch() != llvm::Triple::x86_64) +return false; + + if (Triple.getVendor() != llvm::Triple::UnknownVendor) +return false; + + if (Triple.getOS() != llvm::Triple::UnknownOS) +return false; + + return Triple.getEnvironmentName() == "elf"; +} + void BareMetal::findMultilibs(const Driver &D, const llvm::Triple &Triple, const ArgList &Args) { DetectedMultilibs Result; @@ -151,7 +165,9 @@ } bool BareMetal::handlesTarget(const llvm::Triple &Triple) { - return isARMBareMetal(Triple) || isRISCVBareMetal(Triple); + return isARMBareMetal(Triple) || + isRISCVBareMetal(Triple) || + isX86BareMetal(Triple); } Tool *BareMetal::buildLinker() const { ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D105414: Add x86 and x86_64 to the BareMetal toolchain
agvallejo added a comment. I'm still testing this patch, but so far it seems to do the trick. I'm able to compile with clang, link with lld and the resulting ELF file appears to be correctly free from host dependencies. The command lines (seen with clang -v) don't throw the host include paths, host libraries or a dynamic linker into the mix, so that's also good. I'm new to the LLVM codebase, so please bear with me if this has unintended side-effects Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D105414/new/ https://reviews.llvm.org/D105414 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D105414: Add x86 and x86_64 to the BareMetal toolchain
agvallejo updated this revision to Diff 357070. agvallejo added a comment. Fulfill merge requirements -Fixed clang-format complaint -Added CLI processing tests for i686 and x86_64 (freestanding+nostdlib only) Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D105414/new/ https://reviews.llvm.org/D105414 Files: clang/lib/Driver/ToolChains/BareMetal.cpp clang/test/Driver/baremetal.cpp Index: clang/test/Driver/baremetal.cpp === --- clang/test/Driver/baremetal.cpp +++ clang/test/Driver/baremetal.cpp @@ -355,3 +355,33 @@ // CHECK-RV32IMAFC-NEXT: "{{[^"]*}}ld{{(\.(lld|bfd|gold))?}}{{(\.exe)?}}" "{{.*}}.o" "-Bstatic" // CHECK-RV32IMAFC-SAME: "-L[[SYSROOT:[^"]+]]{{[/\\]+}}rv32imafc{{[/\\]+}}ilp32f{{[/\\]+}}lib" // CHECK-RV32IMAFC-SAME: "-L[[RESOURCE_DIR:[^"]+]]{{[/\\]+}}lib{{[/\\]+}}baremetal{{[/\\]+}}rv32imafc{{[/\\]+}}ilp32f" + +//--- +// freestanding+nostdlib i686-unknown-elf doesn't use gcc for linking nor adds any libraries +//--- +// RUN: %clang -no-canonical-prefixes %s -### -o %t.o 2>&1 \ +// RUN: -target i686-unknown-elf \ +// RUN: -ffreestanding \ +// RUN: -nostdlib \ +// RUN: | FileCheck --check-prefix=CHECK-X86 %s +// CHECK-X86: "[[PREFIX_DIR:.*]]{{[/\\]+}}{{[^/^\\]+}}{{[/\\]+}}clang{{.*}}" "-cc1" "-triple" "i686-unknown-unknown-elf" +// CHECK-X86-SAME: "-resource-dir" "[[RESOURCE_DIR:[^"]+]]" +// CHECK-X86-NEXT: "{{[^"]*}}ld{{(\.(lld|bfd|gold))?}}{{(\.exe)?}}" "{{.*}}.o" "-Bstatic" +// CHECK-X86-SAME: "-L[[RESOURCE_DIR:[^"]+]]{{[/\\]+}}lib{{[/\\]+}}baremetal" +// CHECK-X86-NOT: "-l{{.*}}" +// CHECK-X86-SAME: "-o" "{{.*}}.o" + +//--- +// freestanding+nostdlib x86_64-unknown-elf doesn't use gcc for linking nor adds any libraries +//--- +// RUN: %clang -no-canonical-prefixes %s -### -o %t.o 2>&1 \ +// RUN: -target x86_64-unknown-elf \ +// RUN: -ffreestanding \ +// RUN: -nostdlib \ +// RUN: | FileCheck --check-prefix=CHECK-X86_64 %s +// CHECK-X86_64: "[[PREFIX_DIR:.*]]{{[/\\]+}}{{[^/^\\]+}}{{[/\\]+}}clang{{.*}}" "-cc1" "-triple" "x86_64-unknown-unknown-elf" +// CHECK-X86_64-SAME: "-resource-dir" "[[RESOURCE_DIR:[^"]+]]" +// CHECK-X86_64-NEXT: "{{[^"]*}}ld{{(\.(lld|bfd|gold))?}}{{(\.exe)?}}" "{{.*}}.o" "-Bstatic" +// CHECK-X86_64-SAME: "-L[[RESOURCE_DIR:[^"]+]]{{[/\\]+}}lib{{[/\\]+}}baremetal" +// CHECK-X86_64-NOT: "-l{{.*}}" +// CHECK-X86_64-SAME: "-o" "{{.*}}.o" Index: clang/lib/Driver/ToolChains/BareMetal.cpp === --- clang/lib/Driver/ToolChains/BareMetal.cpp +++ clang/lib/Driver/ToolChains/BareMetal.cpp @@ -139,6 +139,20 @@ return Triple.getEnvironmentName() == "elf"; } +static bool isX86BareMetal(const llvm::Triple &Triple) { + if (Triple.getArch() != llvm::Triple::x86 && + Triple.getArch() != llvm::Triple::x86_64) +return false; + + if (Triple.getVendor() != llvm::Triple::UnknownVendor) +return false; + + if (Triple.getOS() != llvm::Triple::UnknownOS) +return false; + + return Triple.getEnvironmentName() == "elf"; +} + void BareMetal::findMultilibs(const Driver &D, const llvm::Triple &Triple, const ArgList &Args) { DetectedMultilibs Result; @@ -151,7 +165,8 @@ } bool BareMetal::handlesTarget(const llvm::Triple &Triple) { - return isARMBareMetal(Triple) || isRISCVBareMetal(Triple); + return isARMBareMetal(Triple) || isRISCVBareMetal(Triple) || + isX86BareMetal(Triple); } Tool *BareMetal::buildLinker() const { Index: clang/test/Driver/baremetal.cpp === --- clang/test/Driver/baremetal.cpp +++ clang/test/Driver/baremetal.cpp @@ -355,3 +355,33 @@ // CHECK-RV32IMAFC-NEXT: "{{[^"]*}}ld{{(\.(lld|bfd|gold))?}}{{(\.exe)?}}" "{{.*}}.o" "-Bstatic" // CHECK-RV32IMAFC-SAME: "-L[[SYSROOT:[^"]+]]{{[/\\]+}}rv32imafc{{[/\\]+}}ilp32f{{[/\\]+}}lib" // CHECK-RV32IMAFC-SAME: "-L[[RESOURCE_DIR:[^"]+]]{{[/\\]+}}lib{{[/\\]+}}baremetal{{[/\\]+}}rv32imafc{{[/\\]+}}ilp32f" + +//--- +// freestanding+nostdlib i686-unknown-elf doesn't use gcc for linking nor adds any libraries +//--- +// RUN: %clang -no-canonical-prefixes %s -### -o %t.o 2>&1 \ +// RUN: -target i686-unknown-elf \ +// RUN: -ffreestanding \ +// RUN: -nostdlib \ +// RUN: | FileCheck --check-prefix=CHECK-X86 %s +// CHECK-X86: "[[PREFIX_DIR:.*]
[PATCH] D105414: Add x86 and x86_64 to the BareMetal toolchain
agvallejo added a reviewer: abidh. agvallejo added a comment. Herald added a subscriber: ki.stfu. It took me a while to get the gist of FileCheck, but it finally clicked. @abidh: Thanks for the pointer! Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D105414/new/ https://reviews.llvm.org/D105414 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits