-
Notifications
You must be signed in to change notification settings - Fork 14.4k
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
Fix lld link issue for OHOS #118192
Conversation
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 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. |
@llvm/pr-subscribers-clang-driver Author: Peng Huang (phuang) ChangesFor ohos targets, libclang_rt.builtins.a, clang_rt.crtbegin.o and clang_rt.crtend.o are installed in Full diff: https://github.com/llvm/llvm-project/pull/118192.diff 1 Files Affected:
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();
|
@llvm/pr-subscribers-clang Author: Peng Huang (phuang) ChangesFor ohos targets, libclang_rt.builtins.a, clang_rt.crtbegin.o and clang_rt.crtend.o are installed in Full diff: https://github.com/llvm/llvm-project/pull/118192.diff 1 Files Affected:
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();
|
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.
28314d9
to
206f8f8
Compare
friendly ping. |
@phuang Hi! Thank you for the patch. Will take a look in the nearest pair of days |
✅ With the latest revision this PR passed the C/C++ code formatter. |
LGTM |
Thanks for reviewing it. |
Could you please help merge the PR? I don't see how to do it from my side. Thanks. |
@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! |
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. |
Agree. Tests would be useful here. Thanks for mentioning it |
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? |
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 |
I am out of town this week. I will take a look when I return. |
@nico Should I revert this patch for now? |
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. |
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
HI @nico, how to repro the problem locally? Is there a doc for it? or could you please share steps? thanks |
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: 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? |
This reverts commit 1911919.
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. |
Aha! Yes, that bot does do incremental builds. (As do many devs.) |
There was a problem hiding this 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.
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.
#120159)" This reverts commit bd154e8. Test fails with -DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=OFF, see #120159 (comment)
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.