-
Notifications
You must be signed in to change notification settings - Fork 49
Correct sysroot-prepend conditions for script inputs #809
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -102,8 +102,7 @@ bool Input::resolvePath(const LinkerConfig &PConfig) { | |
| } | ||
|
|
||
| if (Type == Input::Script) { | ||
| if (PSearchDirs.hasSysRoot() && | ||
| (FileName.size() > 0 && FileName[0] == '/')) { | ||
| if (shouldPrependSysrootToScriptInput(PConfig)) { | ||
| ResolvedPath = PSearchDirs.sysroot(); | ||
| ResolvedPath->append(FileName); | ||
| } | ||
|
|
@@ -151,6 +150,30 @@ bool Input::resolvePath(const LinkerConfig &PConfig) { | |
| return true; | ||
| } | ||
|
|
||
| bool Input::shouldPrependSysrootToScriptInput( | ||
| const LinkerConfig &Config) const { | ||
| auto &searchDirs = Config.directories(); | ||
| if (!searchDirs.hasSysRoot()) | ||
| return false; | ||
| if (Type != Input::Script) | ||
| return false; | ||
| if (FileName.empty() || FileName[0] != '/') | ||
| return false; | ||
|
|
||
| // Only apply sysroot for INPUT/GROUP entries when we know which script they | ||
| // came from and that script is inside sysroot. | ||
| if (!ParentScriptFile) | ||
| return false; | ||
|
|
||
| Input *scriptInput = ParentScriptFile->getInput(); | ||
|
|
||
| std::string scriptPath = scriptInput->getResolvedPath().getFullPath(); | ||
| std::string sysrootPath = searchDirs.sysroot().getFullPath(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will this work with reproduce option ?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I think so. We already had this code. Previously, we used to unconditionally prepend the sysroot for script inputs that began with
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please check ?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just checked. The linker crashes :'). However, this is not a regression with this patch. We crash without this patch as well. Reproducer (nightly linker crashes with this): #!/usr/bin/env bash
mkdir -p A/lib64
mkdir -p A/script
cat >1.c <<\EOF
int foo() { return 1; }
EOF
cat >script.t <<\EOF
GROUP(/lib64/lib1.so)
EOF
cat >main.c <<\EOF
int main() { return 0; }
EOF
clang -o lib1.so -target x86_64-unknown-elf 1.c -shared -fPIC
clang -o main.o -target x86_64-unknown-elf main.c -c
mv lib1.so A/lib64/lib1.so
mv script.t A/script.t
ld.eld --sysroot=A -T A/script.t main.o --reproduce a.tar
rm -rf unpacked && mkdir unpacked
tar -xvf a.tar -C unpacked --strip-components=1
cd unpacked
bash -x response.txtI believe this should be treated as a separate issue.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have created an issue to track this #818 |
||
|
|
||
| return scriptPath.size() >= sysrootPath.size() && | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what about a hypothetical
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here we just need to determine whether the script is within or outside the sysroot. If a script is contained within /sdk/sysroot_extra and the sysroot is /sdk/sysroot, then as per the linker the script is correctly outside the sysroot. The behavior seems correct to me. Can you please let me know if I have missed something? |
||
| scriptPath.compare(0, sysrootPath.size(), sysrootPath) == 0; | ||
| } | ||
|
|
||
| void Input::setInputFile(InputFile *Inp) { IF = Inp; } | ||
|
|
||
| void Input::overrideInputFile(InputFile *Inp) { IF = Inp; } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -56,6 +56,9 @@ void InputCmd::dump(llvm::raw_ostream &Outs) const { | |
| } | ||
|
|
||
| eld::Expected<void> InputCmd::activate(Module &CurModule) { | ||
| // Prefer ScriptCommand context (handles included scripts). | ||
| InputFile *parentScriptFile = &ThisScriptFile.getLinkerScriptFile(); | ||
|
|
||
| for (auto &S : ThisStringList) { | ||
| InputToken *Token = llvm::cast<InputToken>(S); | ||
| Token = ThisScriptFile.findResolvedFilename(Token); | ||
|
|
@@ -64,9 +67,11 @@ eld::Expected<void> InputCmd::activate(Module &CurModule) { | |
| else | ||
| ThisBuilder.getAttributes().unsetAsNeeded(); | ||
| switch (Token->type()) { | ||
| case InputToken::File: | ||
| ThisBuilder.createDeferredInput(Token->name(), Input::Script); | ||
| case InputToken::File: { | ||
| Input *I = ThisBuilder.createDeferredInput(Token->name(), Input::Script); | ||
| I->setParentScriptFile(parentScriptFile); | ||
| break; | ||
| } | ||
| case InputToken::NameSpec: { | ||
| ThisBuilder.createDeferredInput(Token->name(), Input::Namespec); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add the parent script file in all cases, and check if the command is INPUT or GROUP ?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please explain this in more detail? Currently, we only add parent script for INPUT and GROUP inputs. If I understand correctly, then for the other cases, we do not have any associated parent script.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see. Do we need to add this for InputToken::NameSpec too ?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, we do not need this for NameSpec. But we do need to fix our behavior for NameSpec with sysroot. GNU LD finds the NameSpec library in all the standard paths inside sysroot. (Paths such as /usr/lib, /lib, /lib64, and so on).
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please open a follow up ticket on this, We do have this code in
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have created an issue to track this: #819 |
||
| break; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| int foo() { return 1; } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| int bar() { return 2; } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| int main() { return 0; } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| GROUP(/lib64/lib1.so) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| INPUT(/lib64/lib2.so) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| #---SysrootInputGroup.test---------------- Linker Script ----------------# | ||
| #BEGIN_COMMENT | ||
| # Validate sysroot handling for INPUT/GROUP script commands (issue #808). | ||
| # Sysroot is prepended for "/..." only when the linker script itself is within | ||
| # the sysroot directory. | ||
| #END_COMMENT | ||
| #START_TEST | ||
|
|
||
| RUN: rm -rf %t.dir && mkdir -p %t.dir/lib64 %t.dir/scripts | ||
| RUN: %clang %clangopts -o %t.1.o %p/Inputs/1.c -c -fPIC | ||
| RUN: %clang %clangopts -o %t.2.o %p/Inputs/2.c -c -fPIC | ||
| RUN: %clang %clangopts -o %t.main.o %p/Inputs/main.c -c | ||
| RUN: %link %linkopts -o %t.dir/lib64/lib1.so -shared %t.1.o | ||
| RUN: %link %linkopts -o %t.dir/lib64/lib2.so -shared %t.2.o | ||
|
|
||
| # Script outside sysroot: do not prepend sysroot for "/...". | ||
| RUN: %not %link %linkopts -o %t.out1 --sysroot=%t.dir -T %p/Inputs/script1.t %t.main.o 2>&1 | %filecheck %s --check-prefix=ERR1 | ||
| RUN: %not %link %linkopts -o %t.out2 --sysroot=%t.dir -T %p/Inputs/script2.t %t.main.o 2>&1 | %filecheck %s --check-prefix=ERR2 | ||
|
|
||
| # Script in sysroot: prepend sysroot for "/...". | ||
| RUN: %cp %p/Inputs/script1.t %t.dir/scripts/script1.t | ||
| RUN: %cp %p/Inputs/script2.t %t.dir/scripts/script2.t | ||
| RUN: %link %linkopts -o %t.out3 --sysroot=%t.dir -T %t.dir/scripts/script1.t %t.main.o --verbose 2>&1 | %filecheck %s --check-prefix=SYSROOT1 | ||
| RUN: %link %linkopts -o %t.out4 --sysroot=%t.dir -T %t.dir/scripts/script2.t %t.main.o --verbose 2>&1 | %filecheck %s --check-prefix=SYSROOT2 | ||
| #END_TEST | ||
|
|
||
| ERR1: Fatal: cannot read file /lib64/lib1.so | ||
| ERR2: Fatal: cannot read file /lib64/lib2.so | ||
|
|
||
| SYSROOT1: Verbose: Mapping input file '{{.*}}dir/lib64/lib1.so' into memory | ||
| SYSROOT2: Verbose: Mapping input file '{{.*}}dir/lib64/lib2.so' into memory | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| // Simple library with exported functions | ||
| int exported_func_v1() { return 1; } | ||
| int exported_func_v2() { return 2; } | ||
| int hidden_func() { return 3; } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| exported_func_v1; | ||
| exported_func_v2; | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| global: | ||
| exported_func_v1; | ||
| local: | ||
| *; | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| #---VersionAndDynamicScripts.test---- sysRoot ----------------# | ||
|
|
||
| #BEGIN_COMMENT | ||
| # Test that the version scripts and dynamic lists paths are properly handled | ||
| # when sysroot is used. | ||
| #END_COMMENT | ||
| #START_TEST | ||
| RUN: %clang %clangopts -o %t1.1.o %p/Inputs/1.c -c | ||
| RUN: %rm -rf %t1.sysroot %t1.dir | ||
| RUN: %mkdir %t.sysroot | ||
| RUN: %link %linkopts -o %t1.lib1.so %t1.1.o -shared --sysroot=%t.sysroot -L=/ \ | ||
| RUN: --version-script=%p/Inputs/version.script --verbose 2>&1 \ | ||
| RUN: | %filecheck %s --check-prefix VersionScriptAbs | ||
| RUN: %link %linkopts -o %t1.lib2.so %t1.1.o -shared --sysroot=%t.sysroot -L=/ \ | ||
| RUN: --dynamic-list=%p/Inputs/dynamic.list --verbose 2>&1 \ | ||
| RUN: | %filecheck %s --check-prefix DynScriptAbs | ||
| RUN: %mkdir %t1.dir | ||
| RUN: cd %t1.dir | ||
| RUN: %link %linkopts -o %t1.lib1.so %t1.1.o -shared --sysroot=%p/Inputs -L=/ \ | ||
| RUN: --version-script=version.script --verbose 2>&1 \ | ||
| RUN: | %filecheck %s --check-prefix VersionScriptSysRoot | ||
| RUN: %link %linkopts -o %t1.lib2.so %t1.1.o -shared --sysroot=%p/Inputs -L=/ \ | ||
| RUN: --dynamic-list=dynamic.list --verbose 2>&1 \ | ||
| RUN: | %filecheck %s --check-prefix DynScriptSysRoot | ||
| RUN: %cp %p/Inputs/version.script version.script | ||
| RUN: %cp %p/Inputs/dynamic.list dynamic.list | ||
| RUN: %link %linkopts -o %t1.lib1.so %t1.1.o -shared --sysroot=%p/Inputs -L=/ \ | ||
| RUN: --version-script=version.script --verbose 2>&1 \ | ||
| RUN: | %filecheck %s --check-prefix VersionScriptRel | ||
| RUN: %link %linkopts -o %t1.lib2.so %t1.1.o -shared --sysroot=%p/Inputs -L=/ \ | ||
| RUN: --dynamic-list=dynamic.list --verbose 2>&1 \ | ||
| RUN: | %filecheck %s --check-prefix DynScriptSysRel | ||
| #END_TEST | ||
|
|
||
| VersionScriptAbs: Verbose: Parsing version script | ||
|
|
||
| DynScriptAbs: Verbose: Dynamic List[{{.*}}] : exported_func_v1 | ||
| DynScriptAbs: Verbose: Dynamic List[{{.*}}] : exported_func_v2 | ||
|
|
||
| VersionScriptSysRoot: Verbose: Trying to open input `{{.*}}/Inputs//version.script' of type `linker script' for namespec `version.script': found | ||
| DynScriptSysRoot: Verbose: Trying to open input `{{.*}}/Inputs//dynamic.list' of type `linker script' for namespec `dynamic.list': found | ||
|
|
||
| VersionScriptRel: Verbose: Mapping input file 'version.script' into memory | ||
| DynScriptSysRel: Verbose: Mapping input file 'dynamic.list' into memory |
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.
I am not sure how this will be effective on windows. Needs to be tested on windows too.
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.
Okay!