Skip to content

[clang] Add option for -nolibc in Driver/ToolChains/Baremetal.cpp #145700

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

saturn691
Copy link
Contributor

@saturn691 saturn691 commented Jun 25, 2025

Some tests in LLVM-libc require this flag (#145349), which requires compiler-rt to be linked in, but not the C library. With this change, the -nolibc flag will not be ignored.

Minor changes: clang-format

Some tests in LLVM-libc require this flag, so compiler-rt is still
linked in, but not the C library. With this change, it will not be
ignored.

Minor changes: clang-format
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' labels Jun 25, 2025
@llvmbot
Copy link
Member

llvmbot commented Jun 25, 2025

@llvm/pr-subscribers-clang

Author: William Huynh (saturn691)

Changes

Some tests in LLVM-libc require this flag, so compiler-rt is still linked in, but not the C library. With this change, it will not be ignored.

Minor changes: clang-format


Full diff: https://github.com/llvm/llvm-project/pull/145700.diff

1 Files Affected:

  • (modified) clang/lib/Driver/ToolChains/BareMetal.cpp (+12-10)
diff --git a/clang/lib/Driver/ToolChains/BareMetal.cpp b/clang/lib/Driver/ToolChains/BareMetal.cpp
index d8168ed15febd..b87427110d7de 100644
--- a/clang/lib/Driver/ToolChains/BareMetal.cpp
+++ b/clang/lib/Driver/ToolChains/BareMetal.cpp
@@ -339,15 +339,15 @@ void BareMetal::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
   };
 
   switch (GetCXXStdlibType(DriverArgs)) {
-    case ToolChain::CST_Libcxx: {
-      SmallString<128> P(D.Dir);
-      llvm::sys::path::append(P, "..", "include");
-      AddCXXIncludePath(P);
-      break;
-    }
-    case ToolChain::CST_Libstdcxx:
-      // We only support libc++ toolchain installation.
-      break;
+  case ToolChain::CST_Libcxx: {
+    SmallString<128> P(D.Dir);
+    llvm::sys::path::append(P, "..", "include");
+    AddCXXIncludePath(P);
+    break;
+  }
+  case ToolChain::CST_Libstdcxx:
+    // We only support libc++ toolchain installation.
+    break;
   }
 
   std::string SysRoot(computeSysRoot());
@@ -498,7 +498,9 @@ void baremetal::Linker::ConstructJob(Compilation &C, const JobAction &JA,
   if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
     AddRunTimeLibs(TC, D, CmdArgs, Args);
 
-    CmdArgs.push_back("-lc");
+    if (!Args.hasArg(options::OPT_nolibc)) {
+      CmdArgs.push_back("-lc");
+    }
   }
 
   if (D.isUsingLTO())

@llvmbot
Copy link
Member

llvmbot commented Jun 25, 2025

@llvm/pr-subscribers-clang-driver

Author: William Huynh (saturn691)

Changes

Some tests in LLVM-libc require this flag, so compiler-rt is still linked in, but not the C library. With this change, it will not be ignored.

Minor changes: clang-format


Full diff: https://github.com/llvm/llvm-project/pull/145700.diff

1 Files Affected:

  • (modified) clang/lib/Driver/ToolChains/BareMetal.cpp (+12-10)
diff --git a/clang/lib/Driver/ToolChains/BareMetal.cpp b/clang/lib/Driver/ToolChains/BareMetal.cpp
index d8168ed15febd..b87427110d7de 100644
--- a/clang/lib/Driver/ToolChains/BareMetal.cpp
+++ b/clang/lib/Driver/ToolChains/BareMetal.cpp
@@ -339,15 +339,15 @@ void BareMetal::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
   };
 
   switch (GetCXXStdlibType(DriverArgs)) {
-    case ToolChain::CST_Libcxx: {
-      SmallString<128> P(D.Dir);
-      llvm::sys::path::append(P, "..", "include");
-      AddCXXIncludePath(P);
-      break;
-    }
-    case ToolChain::CST_Libstdcxx:
-      // We only support libc++ toolchain installation.
-      break;
+  case ToolChain::CST_Libcxx: {
+    SmallString<128> P(D.Dir);
+    llvm::sys::path::append(P, "..", "include");
+    AddCXXIncludePath(P);
+    break;
+  }
+  case ToolChain::CST_Libstdcxx:
+    // We only support libc++ toolchain installation.
+    break;
   }
 
   std::string SysRoot(computeSysRoot());
@@ -498,7 +498,9 @@ void baremetal::Linker::ConstructJob(Compilation &C, const JobAction &JA,
   if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
     AddRunTimeLibs(TC, D, CmdArgs, Args);
 
-    CmdArgs.push_back("-lc");
+    if (!Args.hasArg(options::OPT_nolibc)) {
+      CmdArgs.push_back("-lc");
+    }
   }
 
   if (D.isUsingLTO())

@saturn691 saturn691 marked this pull request as draft June 25, 2025 13:52
@saturn691 saturn691 marked this pull request as ready for review June 26, 2025 09:19
@vhscampos
Copy link
Member

The change LGTM. Please just fix a typo in the PR description: "requre"

// CHECK-NOLIBC-NOT: "-lc"
// CHECK-NOLIBC-NOT: "-lm"
// CHECK-NOLIBC-NOT: "-lc++"
// CHECK-NOLIBC: "{{[^"]*}}libclang_rt.builtins.a"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The runtime library is not relevant to this test so I'd omit this line.

Suggested change
// CHECK-NOLIBC: "{{[^"]*}}libclang_rt.builtins.a"

Copy link
Contributor Author

@saturn691 saturn691 Jun 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thing I'm trying to test is the follows, I think it still makes sense to test libclang_rt.builtings.a (replace libgcc with compiler-rt):

-nolibc
Do not use the C library or system libraries tightly coupled with it when linking. Still link with the startup files, libgcc or toolchain provided language support libraries such as libgnat, libgfortran or libstdc++ unless options preventing their inclusion are used as well.

-nostdlib
Do not use the standard system startup files or libraries when linking. No startup files and only the libraries you specify are passed to the linker, and options specifying linkage of the system libraries, such as -static-libgcc or -shared-libgcc, are ignored.

The compiler may generate calls to memcmp, memset, memcpy and memmove. These entries are usually resolved by entries in libc. These entry points should be supplied through some other mechanism when this option is specified.

One of the standard libraries bypassed by -nostdlib and -nodefaultlibs is libgcc.a, a library of internal subroutines which GCC uses to overcome shortcomings of particular machines, or special needs for some languages.

Reference: https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html#index-nolibc

// CHECK-NOSTDLIB-NOT: "-lc"
// CHECK-NOSTDLIB-NOT: "-lm"
// CHECK-NOSTDLIB-NOT: "-lc++"
// CHECK-NOSTDLIB-NOT: "{{[^"]*}}libclang_rt.builtins.a"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto here.

Suggested change
// CHECK-NOSTDLIB-NOT: "{{[^"]*}}libclang_rt.builtins.a"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto previous comment

saturn691 and others added 2 commits June 27, 2025 09:43
Co-authored-by: Petr Hosek <[email protected]>
Co-authored-by: Petr Hosek <[email protected]>
@saturn691 saturn691 requested a review from petrhosek June 27, 2025 09:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants