Skip to content

Correct sysroot-prepend conditions for script inputs#809

Open
parth-07 wants to merge 1 commit intoqualcomm:mainfrom
parth-07:FixSysrootPrepend
Open

Correct sysroot-prepend conditions for script inputs#809
parth-07 wants to merge 1 commit intoqualcomm:mainfrom
parth-07:FixSysrootPrepend

Conversation

@parth-07
Copy link
Contributor

This commit fixes the logic that determines when the script input path should be prepended with sysroot. The summary of the new behavior:

  • sysroot is only prepended to the script inputs when all the below constraints are true:
    • the input is specified in INPUT/GROUP linker script command.
    • The input path begins with '/'.
    • The linker script that contains this INPUT/GROUP command is within the sysroot directory.
  • In all other cases, sysroot is not prepended to the script inputs.

Resolves #786
Resolves #808

Copy link
Contributor

@quic-seaswara quic-seaswara left a comment

Choose a reason for hiding this comment

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

Please update docs as well.

Please look into this change if verbose logging would be useful.

Map file diagnostics could be a follow up.

Input *scriptInput = ParentScriptFile->getInput();

std::string scriptPath = scriptInput->getResolvedPath().getFullPath();
std::string sysrootPath = searchDirs.sysroot().getFullPath();
Copy link
Contributor

Choose a reason for hiding this comment

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

Will this work with reproduce option ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 /. Now, we only prepend sysroot for script inputs that begins with / if the parent script is within the sysroot directory.

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you please check ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.txt

I believe this should be treated as a separate issue.

Copy link
Contributor Author

@parth-07 parth-07 Feb 12, 2026

Choose a reason for hiding this comment

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

I have created an issue to track this #818

return false;
if (Type != Input::Script)
return false;
if (FileName.empty() || FileName[0] != '/')
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay!

break;
}
case InputToken::NameSpec: {
ThisBuilder.createDeferredInput(Token->name(), Input::Namespec);
Copy link
Contributor

Choose a reason for hiding this comment

The 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 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

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

I see. Do we need to add this for InputToken::NameSpec too ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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).

Copy link
Contributor

Choose a reason for hiding this comment

The 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 eld::ELDEmulateELF but that code is not getting triggered.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have created an issue to track this: #819

@parth-07
Copy link
Contributor Author

Please update docs as well.

Is it okay if I push docs as follow-up? I am looking into a framework that will allow us to add extra information to https://qualcomm.github.io/eld/documentation/options/options.html. Currently, this page is automatically generated by parsing the tablegen option files.

Please look into this change if verbose logging would be useful.

Currently, we do have logs of the type Trying to open input file .... I think those logs are enough for this case as well. Please let me know your thoughts on this.

@quic-seaswara
Copy link
Contributor

Please update docs as well.

Is it okay if I push docs as follow-up? I am looking into a framework that will allow us to add extra information to https://qualcomm.github.io/eld/documentation/options/options.html. Currently, this page is automatically generated by parsing the tablegen option files.

Please look into this change if verbose logging would be useful.

Currently, we do have logs of the type Trying to open input file .... I think those logs are enough for this case as well. Please let me know your thoughts on this.

Yes, this can be a follow up, but the document needs more than autogen treatment to explain the INPUT/GROUP commands appropriately.

std::string scriptPath = scriptInput->getResolvedPath().getFullPath();
std::string sysrootPath = searchDirs.sysroot().getFullPath();

return scriptPath.size() >= sysrootPath.size() &&
Copy link
Contributor

@quic-areg quic-areg Feb 12, 2026

Choose a reason for hiding this comment

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

what about a hypothetical /sdk/sysroot and a sibling path /sdk/sysroot_extra? we should probably use llvm::sys::fs::equivalent or similar instead of a raw prefix check

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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?

This commit fixes the logic that determines when the script input path
should be prepended with sysroot. The summary of the new behavior:

- sysroot is only prepended to the script inputs when all the below
  constraints are true:
  - the input is specified in INPUT/GROUP linker script command.
  - The input path begins with '/'.
  - The linker script that contains this INPUT/GROUP command is
    within the sysroot directory.
- In all other cases, sysroot is not prepended to the script inputs.

Resolves qualcomm#786
Resolves qualcomm#808

Signed-off-by: Parth Arora <partaror@qti.qualcomm.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants