[ELF] Respect --sysroot for INCLUDE

If an included script is under the sysroot directory, when it opens an
absolute path file (`INPUT` or `GROUP`), add sysroot before the absolute
path. When the included script ends, the `isUnderSysroot` state is
restored.
This commit is contained in:
Fangrui Song
2024-07-28 11:43:27 -07:00
parent 30fa011413
commit a7e8bddfc1
4 changed files with 25 additions and 18 deletions

View File

@@ -48,16 +48,6 @@ namespace {
class ScriptParser final : ScriptLexer {
public:
ScriptParser(MemoryBufferRef mb) : ScriptLexer(mb) {
// Initialize IsUnderSysroot
if (config->sysroot == "")
return;
StringRef path = mb.getBufferIdentifier();
for (; !path.empty(); path = sys::path::parent_path(path)) {
if (!sys::fs::equivalent(config->sysroot, path))
continue;
isUnderSysroot = true;
return;
}
}
void readLinkerScript();
@@ -135,9 +125,6 @@ private:
std::pair<SmallVector<SymbolVersion, 0>, SmallVector<SymbolVersion, 0>>
readSymbols();
// True if a script being read is in the --sysroot directory.
bool isUnderSysroot = false;
// If we are currently parsing a PROVIDE|PROVIDE_HIDDEN command,
// then this member is set to the PROVIDE symbol name.
std::optional<llvm::StringRef> activeProvideSym;
@@ -319,7 +306,7 @@ void ScriptParser::readNoCrossRefs(bool to) {
}
void ScriptParser::addFile(StringRef s) {
if (isUnderSysroot && s.starts_with("/")) {
if (curBuf.isUnderSysroot && s.starts_with("/")) {
SmallString<128> pathData;
StringRef path = (config->sysroot + s).toStringRef(pathData);
if (sys::fs::exists(path))