Skip to content

Fix lld link issue for OHOS #118192

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

Merged
merged 2 commits into from
Dec 12, 2024
Merged

Fix lld link issue for OHOS #118192

merged 2 commits into from
Dec 12, 2024

Conversation

phuang
Copy link
Contributor

@phuang phuang commented Dec 1, 2024

For ohos targets, libclang_rt.builtins.a, clang_rt.crtbegin.o and clang_rt.crtend.o are installed in
clang/20/lib/${arch}-unknown-linux-ohos. However OHOS toolchain search them in clang/20/lib/${arch}-linux-ohos folder. It causes link error. Fix the problem by seaching both folders.

Copy link

github-actions bot commented Dec 1, 2024

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' labels Dec 1, 2024
@llvmbot
Copy link
Member

llvmbot commented Dec 1, 2024

@llvm/pr-subscribers-clang-driver

Author: Peng Huang (phuang)

Changes

For ohos targets, libclang_rt.builtins.a, clang_rt.crtbegin.o and clang_rt.crtend.o are installed in
clang/20/lib/${arch}-unknown-linux-ohos. However OHOS toolchain search them in clang/20/lib/${arch}-linux-ohos folder. It causes link error. Fix the problem by seaching both folders.


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

1 Files Affected:

  • (modified) clang/lib/Driver/ToolChains/OHOS.cpp (+22-34)
diff --git a/clang/lib/Driver/ToolChains/OHOS.cpp b/clang/lib/Driver/ToolChains/OHOS.cpp
index 6e1a09ae908b2f..9a52fbf9b70959 100644
--- a/clang/lib/Driver/ToolChains/OHOS.cpp
+++ b/clang/lib/Driver/ToolChains/OHOS.cpp
@@ -19,8 +19,8 @@
 #include "llvm/ProfileData/InstrProf.h"
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/Path.h"
-#include "llvm/Support/VirtualFileSystem.h"
 #include "llvm/Support/ScopedPrinter.h"
+#include "llvm/Support/VirtualFileSystem.h"
 
 using namespace clang::driver;
 using namespace clang::driver::toolchains;
@@ -58,11 +58,9 @@ static bool findOHOSMuslMultilibs(const Driver &D,
   return false;
 }
 
-static bool findOHOSMultilibs(const Driver &D,
-                                      const ToolChain &TC,
-                                      const llvm::Triple &TargetTriple,
-                                      StringRef Path, const ArgList &Args,
-                                      DetectedMultilibs &Result) {
+static bool findOHOSMultilibs(const Driver &D, const ToolChain &TC,
+                              const llvm::Triple &TargetTriple, StringRef Path,
+                              const ArgList &Args, DetectedMultilibs &Result) {
   Multilib::flags_list Flags;
   bool IsA7 = false;
   if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
@@ -172,8 +170,7 @@ OHOS::OHOS(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
       Paths);
 }
 
-ToolChain::RuntimeLibType OHOS::GetRuntimeLibType(
-    const ArgList &Args) const {
+ToolChain::RuntimeLibType OHOS::GetRuntimeLibType(const ArgList &Args) const {
   if (Arg *A = Args.getLastArg(clang::driver::options::OPT_rtlib_EQ)) {
     StringRef Value = A->getValue();
     if (Value != "compiler-rt")
@@ -184,20 +181,19 @@ ToolChain::RuntimeLibType OHOS::GetRuntimeLibType(
   return ToolChain::RLT_CompilerRT;
 }
 
-ToolChain::CXXStdlibType
-OHOS::GetCXXStdlibType(const ArgList &Args) const {
+ToolChain::CXXStdlibType OHOS::GetCXXStdlibType(const ArgList &Args) const {
   if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
     StringRef Value = A->getValue();
     if (Value != "libc++")
       getDriver().Diag(diag::err_drv_invalid_stdlib_name)
-        << A->getAsString(Args);
+          << A->getAsString(Args);
   }
 
   return ToolChain::CST_Libcxx;
 }
 
 void OHOS::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
-                                        ArgStringList &CC1Args) const {
+                                     ArgStringList &CC1Args) const {
   const Driver &D = getDriver();
   const llvm::Triple &Triple = getTriple();
   std::string SysRoot = computeSysRoot();
@@ -258,7 +254,7 @@ void OHOS::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
 }
 
 void OHOS::AddCXXStdlibLibArgs(const ArgList &Args,
-                                  ArgStringList &CmdArgs) const {
+                               ArgStringList &CmdArgs) const {
   switch (GetCXXStdlibType(Args)) {
   case ToolChain::CST_Libcxx:
     CmdArgs.push_back("-lc++");
@@ -291,7 +287,8 @@ ToolChain::path_list OHOS::getRuntimePaths() const {
 
   // First try the triple passed to driver as --target=<triple>.
   P.assign(D.ResourceDir);
-  llvm::sys::path::append(P, "lib", D.getTargetTriple(), SelectedMultilib.gccSuffix());
+  llvm::sys::path::append(P, "lib", D.getTargetTriple(),
+                          SelectedMultilib.gccSuffix());
   Paths.push_back(P.c_str());
 
   // Second try the normalized triple.
@@ -340,26 +337,16 @@ std::string OHOS::getDynamicLinker(const ArgList &Args) const {
 
 std::string OHOS::getCompilerRT(const ArgList &Args, StringRef Component,
                                 FileType Type) const {
+  std::string CRTBasename =
+      buildCompilerRTBasename(Args, Component, Type, /*AddArch=*/false);
+
   SmallString<128> Path(getDriver().ResourceDir);
   llvm::sys::path::append(Path, "lib", getMultiarchTriple(getTriple()),
-                          SelectedMultilib.gccSuffix());
-  const char *Prefix =
-      Type == ToolChain::FT_Object ? "" : "lib";
-  const char *Suffix;
-  switch (Type) {
-  case ToolChain::FT_Object:
-    Suffix = ".o";
-    break;
-  case ToolChain::FT_Static:
-    Suffix = ".a";
-    break;
-  case ToolChain::FT_Shared:
-    Suffix = ".so";
-    break;
-  }
-  llvm::sys::path::append(
-      Path, Prefix + Twine("clang_rt.") + Component + Suffix);
-  return static_cast<std::string>(Path.str());
+                          SelectedMultilib.gccSuffix(), CRTBasename);
+  if (getVFS().exists(Path))
+      return std::string(Path);
+
+  return ToolChain::getCompilerRT(Args, Component, Type);
 }
 
 void OHOS::addExtraOpts(llvm::opt::ArgStringList &CmdArgs) const {
@@ -396,7 +383,7 @@ SanitizerMask OHOS::getSupportedSanitizers() const {
 
 // TODO: Make a base class for Linux and OHOS and move this there.
 void OHOS::addProfileRTLibs(const llvm::opt::ArgList &Args,
-                             llvm::opt::ArgStringList &CmdArgs) const {
+                            llvm::opt::ArgStringList &CmdArgs) const {
   // Add linker option -u__llvm_profile_runtime to cause runtime
   // initialization module to be linked in.
   if (needsProfileRT(Args))
@@ -413,7 +400,8 @@ ToolChain::path_list OHOS::getArchSpecificLibPaths() const {
   return Paths;
 }
 
-ToolChain::UnwindLibType OHOS::GetUnwindLibType(const llvm::opt::ArgList &Args) const {
+ToolChain::UnwindLibType
+OHOS::GetUnwindLibType(const llvm::opt::ArgList &Args) const {
   if (Args.getLastArg(options::OPT_unwindlib_EQ))
     return Generic_ELF::GetUnwindLibType(Args);
   return GetDefaultUnwindLibType();

@llvmbot
Copy link
Member

llvmbot commented Dec 1, 2024

@llvm/pr-subscribers-clang

Author: Peng Huang (phuang)

Changes

For ohos targets, libclang_rt.builtins.a, clang_rt.crtbegin.o and clang_rt.crtend.o are installed in
clang/20/lib/${arch}-unknown-linux-ohos. However OHOS toolchain search them in clang/20/lib/${arch}-linux-ohos folder. It causes link error. Fix the problem by seaching both folders.


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

1 Files Affected:

  • (modified) clang/lib/Driver/ToolChains/OHOS.cpp (+22-34)
diff --git a/clang/lib/Driver/ToolChains/OHOS.cpp b/clang/lib/Driver/ToolChains/OHOS.cpp
index 6e1a09ae908b2f..9a52fbf9b70959 100644
--- a/clang/lib/Driver/ToolChains/OHOS.cpp
+++ b/clang/lib/Driver/ToolChains/OHOS.cpp
@@ -19,8 +19,8 @@
 #include "llvm/ProfileData/InstrProf.h"
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/Path.h"
-#include "llvm/Support/VirtualFileSystem.h"
 #include "llvm/Support/ScopedPrinter.h"
+#include "llvm/Support/VirtualFileSystem.h"
 
 using namespace clang::driver;
 using namespace clang::driver::toolchains;
@@ -58,11 +58,9 @@ static bool findOHOSMuslMultilibs(const Driver &D,
   return false;
 }
 
-static bool findOHOSMultilibs(const Driver &D,
-                                      const ToolChain &TC,
-                                      const llvm::Triple &TargetTriple,
-                                      StringRef Path, const ArgList &Args,
-                                      DetectedMultilibs &Result) {
+static bool findOHOSMultilibs(const Driver &D, const ToolChain &TC,
+                              const llvm::Triple &TargetTriple, StringRef Path,
+                              const ArgList &Args, DetectedMultilibs &Result) {
   Multilib::flags_list Flags;
   bool IsA7 = false;
   if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
@@ -172,8 +170,7 @@ OHOS::OHOS(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
       Paths);
 }
 
-ToolChain::RuntimeLibType OHOS::GetRuntimeLibType(
-    const ArgList &Args) const {
+ToolChain::RuntimeLibType OHOS::GetRuntimeLibType(const ArgList &Args) const {
   if (Arg *A = Args.getLastArg(clang::driver::options::OPT_rtlib_EQ)) {
     StringRef Value = A->getValue();
     if (Value != "compiler-rt")
@@ -184,20 +181,19 @@ ToolChain::RuntimeLibType OHOS::GetRuntimeLibType(
   return ToolChain::RLT_CompilerRT;
 }
 
-ToolChain::CXXStdlibType
-OHOS::GetCXXStdlibType(const ArgList &Args) const {
+ToolChain::CXXStdlibType OHOS::GetCXXStdlibType(const ArgList &Args) const {
   if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
     StringRef Value = A->getValue();
     if (Value != "libc++")
       getDriver().Diag(diag::err_drv_invalid_stdlib_name)
-        << A->getAsString(Args);
+          << A->getAsString(Args);
   }
 
   return ToolChain::CST_Libcxx;
 }
 
 void OHOS::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
-                                        ArgStringList &CC1Args) const {
+                                     ArgStringList &CC1Args) const {
   const Driver &D = getDriver();
   const llvm::Triple &Triple = getTriple();
   std::string SysRoot = computeSysRoot();
@@ -258,7 +254,7 @@ void OHOS::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
 }
 
 void OHOS::AddCXXStdlibLibArgs(const ArgList &Args,
-                                  ArgStringList &CmdArgs) const {
+                               ArgStringList &CmdArgs) const {
   switch (GetCXXStdlibType(Args)) {
   case ToolChain::CST_Libcxx:
     CmdArgs.push_back("-lc++");
@@ -291,7 +287,8 @@ ToolChain::path_list OHOS::getRuntimePaths() const {
 
   // First try the triple passed to driver as --target=<triple>.
   P.assign(D.ResourceDir);
-  llvm::sys::path::append(P, "lib", D.getTargetTriple(), SelectedMultilib.gccSuffix());
+  llvm::sys::path::append(P, "lib", D.getTargetTriple(),
+                          SelectedMultilib.gccSuffix());
   Paths.push_back(P.c_str());
 
   // Second try the normalized triple.
@@ -340,26 +337,16 @@ std::string OHOS::getDynamicLinker(const ArgList &Args) const {
 
 std::string OHOS::getCompilerRT(const ArgList &Args, StringRef Component,
                                 FileType Type) const {
+  std::string CRTBasename =
+      buildCompilerRTBasename(Args, Component, Type, /*AddArch=*/false);
+
   SmallString<128> Path(getDriver().ResourceDir);
   llvm::sys::path::append(Path, "lib", getMultiarchTriple(getTriple()),
-                          SelectedMultilib.gccSuffix());
-  const char *Prefix =
-      Type == ToolChain::FT_Object ? "" : "lib";
-  const char *Suffix;
-  switch (Type) {
-  case ToolChain::FT_Object:
-    Suffix = ".o";
-    break;
-  case ToolChain::FT_Static:
-    Suffix = ".a";
-    break;
-  case ToolChain::FT_Shared:
-    Suffix = ".so";
-    break;
-  }
-  llvm::sys::path::append(
-      Path, Prefix + Twine("clang_rt.") + Component + Suffix);
-  return static_cast<std::string>(Path.str());
+                          SelectedMultilib.gccSuffix(), CRTBasename);
+  if (getVFS().exists(Path))
+      return std::string(Path);
+
+  return ToolChain::getCompilerRT(Args, Component, Type);
 }
 
 void OHOS::addExtraOpts(llvm::opt::ArgStringList &CmdArgs) const {
@@ -396,7 +383,7 @@ SanitizerMask OHOS::getSupportedSanitizers() const {
 
 // TODO: Make a base class for Linux and OHOS and move this there.
 void OHOS::addProfileRTLibs(const llvm::opt::ArgList &Args,
-                             llvm::opt::ArgStringList &CmdArgs) const {
+                            llvm::opt::ArgStringList &CmdArgs) const {
   // Add linker option -u__llvm_profile_runtime to cause runtime
   // initialization module to be linked in.
   if (needsProfileRT(Args))
@@ -413,7 +400,8 @@ ToolChain::path_list OHOS::getArchSpecificLibPaths() const {
   return Paths;
 }
 
-ToolChain::UnwindLibType OHOS::GetUnwindLibType(const llvm::opt::ArgList &Args) const {
+ToolChain::UnwindLibType
+OHOS::GetUnwindLibType(const llvm::opt::ArgList &Args) const {
   if (Args.getLastArg(options::OPT_unwindlib_EQ))
     return Generic_ELF::GetUnwindLibType(Args);
   return GetDefaultUnwindLibType();

@phuang phuang changed the title Fix build error Fix build error for OHOS Dec 1, 2024
For ohos targets, libclang_rt.builtins.a, clang_rt.crtbegin.o and
clang_rt.crtend.o are installed in
clang/20/lib/${arch}-unknown-linux-ohos. However OHOS toolchain
search them in clang/20/lib/${arch}-linux-ohos folder. It causes
link error. Fix the problem by seaching both folders.
@phuang phuang force-pushed the ohos_pull_request branch from 28314d9 to 206f8f8 Compare December 1, 2024 04:28
@phuang phuang changed the title Fix build error for OHOS Fix lld link issue for OHOS Dec 4, 2024
@asl asl requested a review from kpdev December 4, 2024 23:49
@phuang
Copy link
Contributor Author

phuang commented Dec 6, 2024

friendly ping.

@kpdev
Copy link
Member

kpdev commented Dec 7, 2024

@phuang Hi! Thank you for the patch. Will take a look in the nearest pair of days

Copy link

github-actions bot commented Dec 7, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@kpdev
Copy link
Member

kpdev commented Dec 8, 2024

LGTM

@phuang
Copy link
Contributor Author

phuang commented Dec 8, 2024

Thanks for reviewing it.

@phuang
Copy link
Contributor Author

phuang commented Dec 9, 2024

LGTM

Could you please help merge the PR? I don't see how to do it from my side. Thanks.

@kpdev kpdev merged commit bc28be0 into llvm:main Dec 12, 2024
8 checks passed
Copy link

@phuang Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@MaskRay
Copy link
Member

MaskRay commented Dec 12, 2024

In general, functional changes need tests. When the initial https://reviews.llvm.org/D145227 landed, I did not have a chance to look, and the tests may have gap. When adding new functionality, ensure that the test coverage gets improved.

@kpdev
Copy link
Member

kpdev commented Dec 12, 2024

Agree. Tests would be useful here. Thanks for mentioning it

@nico
Copy link
Contributor

nico commented Dec 13, 2024

This broke check-clang on my linux box: http://45.33.8.238/linux/155432/step_6.txt

That bot uses the GN build – are there any prerequisite changes elsewhere that I need to port to it?

@kpdev
Copy link
Member

kpdev commented Dec 13, 2024

Hm... No, it should work. @phuang May I kindly ask you to take a look at this issue? http://45.33.8.238/linux/155432/step_6.txt

@phuang
Copy link
Contributor Author

phuang commented Dec 13, 2024

I am out of town this week. I will take a look when I return.

@kpdev
Copy link
Member

kpdev commented Dec 13, 2024

@nico Should I revert this patch for now?

@nico
Copy link
Contributor

nico commented Dec 14, 2024

If all other bots are happy, then I'm doing something wrong probably. If you don't mind reverting, it'd be nice since then I can catch other, actual problems, with the bot, but it's not a requirement.

kpdev added a commit that referenced this pull request Dec 14, 2024
This reverts commit bc28be0.

Some issues were discovered with GN buildbot http://45.33.8.238/linux/155432/step_6.txt
Need to investigate it
@kpdev
Copy link
Member

kpdev commented Dec 14, 2024

@phuang @nico Reverted. I will reland it after investigating the issue with GN build bot

@phuang
Copy link
Contributor Author

phuang commented Dec 16, 2024

are there any prerequisite changes elsewhere that I need to port to it?

HI @nico, how to repro the problem locally? Is there a doc for it? or could you please share steps? thanks

@phuang
Copy link
Contributor Author

phuang commented Dec 16, 2024

I found out how to run the test, but I cannot reproduce the problem with my change. I run the test with below steps:

1: cmake -S llvm -B build -G Ninja -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_PROJECTS="clang;lld"
2: ninja -C build
3: ./build/bin/llvm-lit ./clang/test/Driver/ohos.c -a -v

I checked the compile log at http://45.33.8.238/linux/155432/step_3.txt , it only compiles around 1000 files. But in my tree, it compiles 5329 files. @nico do you know why? what's your build steps?

phuang added a commit to phuang/llvm-project that referenced this pull request Dec 16, 2024
@phuang
Copy link
Contributor Author

phuang commented Dec 16, 2024

I still cannot repro the problem locally, but base on th log in http://45.33.8.238/linux/155432/step_6.txt . Looks like clang tries to search runtime library at "/usr/local/google/home/thakis/src/llvm-project/out/gn/lib/clang/20/lib/linux/libclang_rt.builtins-x86_64.a" (It is the OldPath in https://github.com/llvm/llvm-project/blob/main/clang/lib/Driver/ToolChain.cpp#L771). Not sure why the file does exist on the gn bot. Maybe it is not a clean build. I workarounded the problem by adding "-ohos" suffix in the runtime library like android.

@kpdev @nico , could you please take a look the new PR #120159

Thanks.

@nico
Copy link
Contributor

nico commented Dec 17, 2024

Aha! Yes, that bot does do incremental builds. (As do many devs.)

Copy link
Member

@MaskRay MaskRay left a comment

Choose a reason for hiding this comment

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

We need a test to clang/test/Driver/ohos.c similar to linux-ld.c clang_rt.crtbegin.o.

You can add UNSUPPORTED: system-windows to ohos.c so that we don't need backslashes.

MaskRay pushed a commit that referenced this pull request Jan 1, 2025
The problem in original change is because OHOS::getCompilerRT()
pickes a wrong builtin runtime
`./lib/clang/20/lib/linux/libclang_rt.builtins-x86_64.a`,
if `./lib/clang/20/lib/linux/libclang_rt.builtins-x86_64.a` exist on the
test filesystem. It shouldn't happen with a clean build.
nico added a commit that referenced this pull request Jan 2, 2025
#120159)"

This reverts commit bd154e8.
Test fails with -DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=OFF, see
#120159 (comment)
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.

5 participants