Skip to content

Commit 7d01eb6

Browse files
committed
Expand =/$SYSROOT for INPUT/GROUP script commands
This commit improves the script parser to expand the = and $SYSROOT prefix in INPUT/GROUP inputs. Resolves #927 Signed-off-by: Parth Arora <partaror@qti.qualcomm.com>
1 parent e8e577e commit 7d01eb6

13 files changed

Lines changed: 115 additions & 6 deletions

File tree

docs/userguide/CommandLineOptionsSupplements/sysroot.rst.in

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ The files specified using the INPUT and GROUP command are searched inside the sy
66
- The file begins with the ``/`` character, and
77
- The script containing the INPUT/GROUP command is located within the sysroot directory.
88

9+
Additionally, paths in INPUT/GROUP commands or on the command line can explicitly
10+
force sysroot expansion by using the ``=`` or ``$SYSROOT`` prefix:
11+
12+
- ``=/path/to/file`` expands to ``<sysroot>/path/to/file``
13+
- ``$SYSROOT/path/to/file`` expands to ``<sysroot>/path/to/file``
14+
15+
This works for both linker script commands and command-line input files.
16+
If no sysroot is configured, the prefix is stripped and the remaining path is used.
17+
918
:option:`-L` ``<search_directory>``
1019

1120
When ``<search_directory>`` is prefixed with ``=``, then ``=`` is expanded to the sysroot.

include/eld/Diagnostics/DiagVerbose.inc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,5 @@ DIAG(verbose_loaded_plugin_config, DiagnosticEngine::Verbose,
168168
DIAG(verbose_infer_target, DiagnosticEngine::Verbose,
169169
"Inferred target : %0")
170170
DIAG(verbose_sframe_log, DiagnosticEngine::Verbose, "SFrame Handling : %0")
171+
DIAG(verbose_sysroot_expansion, DiagnosticEngine::Verbose,
172+
"Expanded sysroot marker in path. '%0' -> '%1'")

include/eld/Input/Input.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ class Input {
6262
/// Return a user-facing text representation of input type.
6363
static llvm::StringRef toString(InputType);
6464

65+
/// Expand '=' or '$SYSROOT' prefix in the path using the configured sysroot.
66+
/// Returns the expanded path, or the original if no marker is present.
67+
/// If a marker is present but no sysroot is configured, the marker is
68+
/// stripped. Emits verbose_sysroot_expansion diagnostic when expansion occurs.
69+
static std::string expandSysrootMarkers(llvm::StringRef Name,
70+
const SearchDirs &PSearchDirs,
71+
DiagnosticEngine &DiagEngine);
72+
6573
/// getFileName returns the FileName passed to the driver otherwise.
6674
const std::string getFileName() const { return FileName; }
6775

lib/Input/Input.cpp

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,40 @@ bool Input::isPathValid(const std::string &Path) const {
8585
return true;
8686
}
8787

88+
std::string Input::expandSysrootMarkers(llvm::StringRef Name,
89+
const SearchDirs &PSearchDirs,
90+
DiagnosticEngine &DiagEngine) {
91+
llvm::StringRef Suffix;
92+
if (Name.starts_with("="))
93+
Suffix = Name.substr(1);
94+
else if (Name.starts_with("$SYSROOT"))
95+
Suffix = Name.substr(strlen("$SYSROOT"));
96+
else
97+
return Name.str();
98+
99+
std::string ExpandedPath = Suffix.str();
100+
if (PSearchDirs.hasSysRoot())
101+
ExpandedPath = (PSearchDirs.sysroot().native() + Suffix).str();
102+
103+
DiagEngine.raise(Diag::verbose_sysroot_expansion) << Name << ExpandedPath;
104+
return ExpandedPath;
105+
}
106+
88107
/// \return True if path able to be resolved, otherwise false
89108
bool Input::resolvePath(const LinkerConfig &PConfig) {
90109
if (ResolvedPath)
91110
return true;
92111
if (PConfig.options().hasMappingFile() && !isInternal())
93112
return resolvePathMappingFile(PConfig);
94113
auto &PSearchDirs = PConfig.directories();
114+
115+
std::string ExpandedFileName = FileName;
116+
if (Type == Input::InputType::Script || Type == Input::InputType::Default)
117+
ExpandedFileName = expandSysrootMarkers(FileName, PSearchDirs, *DiagEngine);
118+
95119
switch (Type) {
96120
default:
97-
ResolvedPath = eld::sys::fs::Path(FileName);
121+
ResolvedPath = eld::sys::fs::Path(ExpandedFileName);
98122
break;
99123
case Input::Internal:
100124
ResolvedPath = eld::sys::fs::Path(FileName);
@@ -103,10 +127,11 @@ bool Input::resolvePath(const LinkerConfig &PConfig) {
103127
if (Type == Input::Script) {
104128
if (shouldPrependSysrootToScriptInput(PConfig)) {
105129
ResolvedPath = PSearchDirs.sysroot();
106-
ResolvedPath->append(FileName);
130+
ResolvedPath->append(ExpandedFileName);
107131
}
108132
if (!llvm::sys::fs::exists(ResolvedPath->native())) {
109-
const sys::fs::Path *P = PSearchDirs.find(FileName, Input::Script);
133+
const sys::fs::Path *P =
134+
PSearchDirs.find(ExpandedFileName, Input::InputType::Script);
110135
if (P != nullptr)
111136
ResolvedPath = *P;
112137
}

lib/Input/InputAction.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,15 @@ InputFileAction::InputFileAction(std::string Name,
4444
: InputAction(K, Printer), Name(Name), I(nullptr) {}
4545

4646
bool InputFileAction::activate(InputBuilder &PBuilder) {
47+
const LinkerConfig &Config = PBuilder.getLinkerConfig();
48+
std::string ExpandedName = Input::expandSysrootMarkers(
49+
Name, Config.directories(), *Config.getDiagEngine());
50+
4751
std::ifstream Is;
48-
Is.open(Name.c_str(), std::ifstream::in);
52+
Is.open(ExpandedName.c_str(), std::ifstream::in);
4953
if (!Is.good()) {
50-
PBuilder.getDiagEngine()->raise(Diag::fatal_cannot_read_input) << Name;
54+
PBuilder.getDiagEngine()->raise(Diag::fatal_cannot_read_input)
55+
<< ExpandedName;
5156
return false;
5257
}
5358
Is.close();

lib/ScriptParser/ScriptParser.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,9 +744,10 @@ void ScriptParser::addFile(StringRef Name) {
744744
if (Name.consume_front("-l"))
745745
InputStrTok = ThisScriptFile.createNameSpecToken(Name.str(),
746746
ThisScriptFile.asNeeded());
747-
else
747+
else {
748748
InputStrTok =
749749
ThisScriptFile.createFileToken(Name.str(), ThisScriptFile.asNeeded());
750+
}
750751
ThisScriptFile.getCurrentStringList()->pushBack(InputStrTok);
751752
}
752753

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
int foo() { return 1; }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
GROUP(=/lib64/lib1.so)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
GROUP($SYSROOT/lib64/lib1.so)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
int main() { return 0; }

0 commit comments

Comments
 (0)