Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions include/eld/ScriptParser/ScriptParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ class ScriptParser final : ScriptLexer {

void addFile(llvm::StringRef Name);

/// Expand "=" or "$SYSROOT" prefix in Name to sysroot path.
/// If sysroot is not set, the marker is stripped and the remaining path is
/// used.
std::string expandSysrootMarkers(llvm::StringRef Name) const;

/// Reads AS_NEEDED(...) subcommand.
void readAsNeeded();

Expand Down
21 changes: 19 additions & 2 deletions lib/ScriptParser/ScriptParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -739,14 +739,31 @@ void ScriptParser::readAsNeeded() {
ThisScriptFile.setAsNeeded(false);
}

std::string ScriptParser::expandSysrootMarkers(StringRef Name) const {
StringRef Suffix;
if (Name.starts_with("="))
Suffix = Name.substr(1);
else if (Name.starts_with("$SYSROOT"))
Suffix = Name.substr(8); // strlen("$SYSROOT") == 8
Copy link
Contributor

Choose a reason for hiding this comment

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

Name.substr(strlen("$SYSROOT")) or create a function to get the suffix.

else
return Name.str();

auto &SearchDirs = ThisConfig.directories();
if (SearchDirs.hasSysRoot())
return (SearchDirs.sysroot().native() + Suffix).str();
return Suffix.str();
}

void ScriptParser::addFile(StringRef Name) {
StrToken *InputStrTok = nullptr;
if (Name.consume_front("-l"))
InputStrTok = ThisScriptFile.createNameSpecToken(Name.str(),
Copy link
Contributor

Choose a reason for hiding this comment

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

What does ld for namespec files ?

ThisScriptFile.asNeeded());
else
else {
std::string ExpandedName = expandSysrootMarkers(Name);
InputStrTok =
ThisScriptFile.createFileToken(Name.str(), ThisScriptFile.asNeeded());
ThisScriptFile.createFileToken(ExpandedName, ThisScriptFile.asNeeded());
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This needs to be done in resolvePath

For example

ld.bfd --sysroot=/tmp/xxx/ =main.o

I could not find this expansion of $SYSROOT for input files, may be I am not using the right version of ld.

ThisScriptFile.getCurrentStringList()->pushBack(InputStrTok);
}

Expand Down
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 @@
GROUP(=/lib64/lib1.so)
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 add GROUP($SYSROOT) as well?

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 @@
INPUT(=/lib64/lib1.so)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
INPUT($SYSROOT/lib64/lib1.so)
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#---SysrootExpansion.test---------------- Linker Script ----------------#
#BEGIN_COMMENT
# Validate sysroot expansion for "=" and "$SYSROOT" markers in INPUT/GROUP
# script commands.
# When a path starts with "=" or "$SYSROOT", it should be expanded to the
# sysroot path regardless of where the linker script is located.
#END_COMMENT
#START_TEST

RUN: rm -rf %t.dir && mkdir -p %t.dir/lib64
RUN: %clang %clangopts -o %t.1.o %p/Inputs/1.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.out1 --sysroot=%t.dir -T %p/Inputs/script_equals.t %t.main.o --verbose 2>&1 | %filecheck %s --check-prefix=EQUALS
RUN: %link %linkopts -o %t.out2 --sysroot=%t.dir -T %p/Inputs/script_sysroot.t %t.main.o --verbose 2>&1 | %filecheck %s --check-prefix=SYSROOT
RUN: %link %linkopts -o %t.out3 --sysroot=%t.dir -T %p/Inputs/group_equals.t %t.main.o --verbose 2>&1 | %filecheck %s --check-prefix=GROUP
RUN: %not %link %linkopts -o %t.out4 -T %p/Inputs/script_equals.t %t.main.o 2>&1 | %filecheck %s --check-prefix=NOSYSROOT
#END_TEST
Copy link
Contributor

Choose a reason for hiding this comment

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

Need verbose output to show how the files are resolved.

Also please annotate map file to show what files are being expanded.


EQUALS: Verbose: Mapping input file '{{.*}}dir/lib64/lib1.so' into memory
SYSROOT: Verbose: Mapping input file '{{.*}}dir/lib64/lib1.so' into memory
GROUP: Verbose: Mapping input file '{{.*}}dir/lib64/lib1.so' into memory
NOSYSROOT: Fatal: cannot read file /lib64/lib1.so
Loading