Skip to content

[linker-script] Find namespec libraries in sysroot standard dirs#1158

Open
deepakshirkem wants to merge 1 commit into
qualcomm:mainfrom
deepakshirkem:fix/sysroot-namespec-search-819
Open

[linker-script] Find namespec libraries in sysroot standard dirs#1158
deepakshirkem wants to merge 1 commit into
qualcomm:mainfrom
deepakshirkem:fix/sysroot-namespec-search-819

Conversation

@deepakshirkem
Copy link
Copy Markdown
Contributor

@deepakshirkem deepakshirkem commented May 11, 2026

Problem

ELD fails to find namespec libraries (e.g. GROUP(-lfoo)) in standard directories under the sysroot such as /lib, /lib64, /usr/lib, and so on.

Testing

Added test/Common/standalone/SysrootNamespec — verifies that GROUP(-lfoo) finds a library placed in the sysroot lib64 directory. Test passes on all targets.

Output

Screenshot from 2026-05-12 00-29-07

Fixes #819.

@deepakshirkem
Copy link
Copy Markdown
Contributor Author

Hi @parth-07 / @quic-areg / @quic-seaswara, please review this PR. During investigation I found two root causes ELDEmulateELF was missing standard sysroot dirs . When no -m flag is passed, initEmulator is deferred until the first ELF object is read. Since linker scripts are processed before objects, ELDEmulateELF never ran before namespec search happened.
I referred to commit c960b2c ("Set emulation mode based on input files") by @quic-seaswara and followed the same pattern of using isBackendInitialized() + getMachine() + initializeTarget(). The fix scans ahead in the input list for the first ELF object when a script is encountered, initializes the target early, and then processes the script with sysroot dirs already set.

Please let me know if the approach is correct or if there's a better way to handle this.

initializeTarget(IF);
return;
}
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why this change ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hi @quic-seaswara, I am also not fully happy about adding this function, and my theory might be wrong. What I observed while reproducing #819 is that when we run the command ld.eld --sysroot=A A/script.t main.o, the code reads A/script.t before the target is initialized. While reading it, the process fails, which is why the code path you mentioned in the discussion is not getting triggered.

That is why I added this function. The function first tries to identify the target using the object file, similar to what I mentioned in the description.

I am not fully aware of the complete codebase yet, but I tried to find a place where we can get the target before reading any object files. It is possible that my intuition is wrong, and if we can determine the target earlier, then this function may not be required.

Your views @quic-areg, @partaror?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hi @quic-seaswara, I am also not fully happy about adding this function, and my theory might be wrong. What I observed while reproducing #819 is that when we run the command ld.eld --sysroot=A A/script.t main.o, the code reads A/script.t before the target is initialized. While reading it, the process fails, which is why the code path you mentioned in the discussion is not getting triggered.

That is why I added this function. The function first tries to identify the target using the object file, similar to what I mentioned in the description.

I am not fully aware of the complete codebase yet, but I tried to find a place where we can get the target before reading any object files. It is possible that my intuition is wrong, and if we can determine the target earlier, then this function may not be required.

Your views @quic-areg, @partaror?

Hi @quic-seaswara, I am also not fully happy about adding this function, and my theory might be wrong. What I observed while reproducing #819 is that when we run the command ld.eld --sysroot=A A/script.t main.o, the code reads A/script.t before the target is initialized. While reading it, the process fails, which is why the code path you mentioned in the discussion is not getting triggered.

That is why I added this function. The function first tries to identify the target using the object file, similar to what I mentioned in the description.

I am not fully aware of the complete codebase yet, but I tried to find a place where we can get the target before reading any object files. It is possible that my intuition is wrong, and if we can determine the target earlier, then this function may not be required.

Your views @quic-areg, @partaror?

is the path expansion target specific ?

Copy link
Copy Markdown
Contributor Author

@deepakshirkem deepakshirkem May 13, 2026

Choose a reason for hiding this comment

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

Yes. In the ELFEmulation.cpp, the sysroot search paths (/lib, /lib64, /usr/lib, /usr/lib64) are the same for all targets in ELD

auto EldDir = ELDDirectory(Dir, Sysroot);
if (llvm::sys::fs::is_directory(EldDir.name()))
pConfig.directories().insert(Dir);
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

-nostdlib should not add these search directories I think.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes your right its working added test cases for this case also.

RUN: mkdir -p %t.sysroot/lib64
RUN: %clang %clangopts -o %t1.o %p/Inputs/1.c -c
RUN: %clang %clangopts -o %t.lib1.o %p/Inputs/lib1.c -c
RUN: %ar cr %aropts %t.sysroot/lib64/lib1.a %t.lib1.o
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What about INPUT ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes its working also added test case.

@deepakshirkem deepakshirkem force-pushed the fix/sysroot-namespec-search-819 branch from c91b968 to f8afc4d Compare May 13, 2026 14:20
ELDEmulateELF only searched =/lib and =/usr/lib under sysroot, missing
=/lib64, =/usr/lib64, =/usr/local/lib and =/usr/local/lib64. Also,
when no target is known upfront, initEmulator is deferred until the
first ELF object is read, causing GROUP(-lfoo) namespec search to fail
before any sysroot standard dirs are added.

Fixes qualcomm#819.

Signed-off-by: deepakshirkem <deepakshirke509@gmail.com>
@deepakshirkem deepakshirkem force-pushed the fix/sysroot-namespec-search-819 branch from f8afc4d to 39156e4 Compare May 14, 2026 17:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

eld does not find namespec libraries in the standard directories within the sysroot

2 participants