Skip to content

[Clang][Driver] Override Generic_ELF::buildLinker() to avoid calling gcc to link #149681

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang/lib/Driver/ToolChains/Gnu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3375,3 +3375,5 @@ void Generic_ELF::addClangTargetOptions(const ArgList &DriverArgs,
options::OPT_fno_use_init_array, true))
CC1Args.push_back("-fno-use-init-array");
}

Tool *Generic_ELF::buildLinker() const { return new tools::gnutools::Linker(*this); }
3 changes: 3 additions & 0 deletions clang/lib/Driver/ToolChains/Gnu.h
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,9 @@ class LLVM_LIBRARY_VISIBILITY Generic_ELF : public Generic_GCC {
}

virtual void addExtraOpts(llvm::opt::ArgStringList &CmdArgs) const {}

protected:
Tool *buildLinker() const override;
};

} // end namespace toolchains
Expand Down
13 changes: 13 additions & 0 deletions clang/test/Driver/baremetal-ld.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,16 @@

// RUN: %clang -### --target=armv7-unknown-none-eabi -mcpu=cortex-m4 --sysroot= -fuse-ld=ld -flto -O3 %s 2>&1 | FileCheck --check-prefix=LTO %s
// LTO: {{".*ld.*"}} {{.*}} "-plugin-opt=mcpu=cortex-m4" "-plugin-opt=O3"

// Ensure that, for freestanding -none targets, the linker does not call into gcc.
// We do this by checking if clang is trying to pass "-fuse-ld=bfd" to the linker command.
// RUN: %clang --target=aarch64-unknown-none-elf -ccc-print-bindings %s 2>&1 | FileCheck --check-prefix=LDAARCH64 %s
// LDAARCH64: "baremetal::Linker"
// RUN: %clang --target=loongarch64-unknown-none-elf -ccc-print-bindings %s 2>&1 | FileCheck --check-prefix=LDLOONGARCH64 %s
// LDLOONGARCH64: "GNU::Linker"
// RUN: %clang --target=riscv64-unknown-none-elf -ccc-print-bindings %s 2>&1 | FileCheck --check-prefix=LDRISCV64 %s
// LDRISCV64: "baremetal::Linker"
// RUN: %clang --target=x86_64-unknown-none-elf -ccc-print-bindings %s 2>&1 | FileCheck --check-prefix=LDX8664 %s
// LDX8664: "GNU::Linker"
// RUN: %clang --target=i386-unknown-none-elf -ccc-print-bindings %s 2>&1 | FileCheck --check-prefix=LDI386 %s
// LDI386: "GNU::Linker"
2 changes: 1 addition & 1 deletion clang/test/Driver/bindings.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// RUN: %clang -target i386-unknown-unknown -ccc-print-bindings -no-integrated-as %s 2>&1 | FileCheck %s --check-prefix=CHECK01
// CHECK01: "clang", inputs: ["{{.*}}bindings.c"], output: "{{.*}}.s"
// CHECK01: "GNU::Assembler", inputs: ["{{.*}}.s"], output: "{{.*}}.o"
// CHECK01: "gcc::Linker", inputs: ["{{.*}}.o"], output: "a.out"
// CHECK01: "GNU::Linker", inputs: ["{{.*}}.o"], output: "a.out"

// Clang control options

Expand Down