Skip to content

Commit a7e8bdd

Browse files
committed
[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.
1 parent 30fa011 commit a7e8bdd

File tree

4 files changed

+25
-18
lines changed

4 files changed

+25
-18
lines changed

lld/ELF/ScriptLexer.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,32 @@
2727
//===----------------------------------------------------------------------===//
2828

2929
#include "ScriptLexer.h"
30+
#include "Config.h"
3031
#include "lld/Common/ErrorHandler.h"
3132
#include "llvm/ADT/Twine.h"
3233
#include "llvm/Support/ErrorHandling.h"
34+
#include "llvm/Support/FileSystem.h"
35+
#include "llvm/Support/Path.h"
3336
#include <algorithm>
3437

3538
using namespace llvm;
3639
using namespace lld;
3740
using namespace lld::elf;
3841

42+
ScriptLexer::Buffer::Buffer(MemoryBufferRef mb)
43+
: s(mb.getBuffer()), filename(mb.getBufferIdentifier()),
44+
begin(mb.getBufferStart()) {
45+
if (config->sysroot == "")
46+
return;
47+
StringRef path = filename;
48+
for (; !path.empty(); path = sys::path::parent_path(path)) {
49+
if (!sys::fs::equivalent(config->sysroot, path))
50+
continue;
51+
isUnderSysroot = true;
52+
return;
53+
}
54+
}
55+
3956
ScriptLexer::ScriptLexer(MemoryBufferRef mb) : curBuf(mb), mbs(1, mb) {
4057
activeFilenames.insert(mb.getBufferIdentifier());
4158
}

lld/ELF/ScriptLexer.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ class ScriptLexer {
2525
StringRef s, filename;
2626
const char *begin = nullptr;
2727
size_t lineNumber = 1;
28+
// True if the script is opened as an absolute path under the --sysroot
29+
// directory.
30+
bool isUnderSysroot = false;
31+
2832
Buffer() = default;
29-
Buffer(MemoryBufferRef mb)
30-
: s(mb.getBuffer()), filename(mb.getBufferIdentifier()),
31-
begin(mb.getBufferStart()) {}
33+
Buffer(MemoryBufferRef mb);
3234
};
3335
// The current buffer and parent buffers due to INCLUDE.
3436
Buffer curBuf;

lld/ELF/ScriptParser.cpp

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,6 @@ namespace {
4848
class ScriptParser final : ScriptLexer {
4949
public:
5050
ScriptParser(MemoryBufferRef mb) : ScriptLexer(mb) {
51-
// Initialize IsUnderSysroot
52-
if (config->sysroot == "")
53-
return;
54-
StringRef path = mb.getBufferIdentifier();
55-
for (; !path.empty(); path = sys::path::parent_path(path)) {
56-
if (!sys::fs::equivalent(config->sysroot, path))
57-
continue;
58-
isUnderSysroot = true;
59-
return;
60-
}
6151
}
6252

6353
void readLinkerScript();
@@ -135,9 +125,6 @@ class ScriptParser final : ScriptLexer {
135125
std::pair<SmallVector<SymbolVersion, 0>, SmallVector<SymbolVersion, 0>>
136126
readSymbols();
137127

138-
// True if a script being read is in the --sysroot directory.
139-
bool isUnderSysroot = false;
140-
141128
// If we are currently parsing a PROVIDE|PROVIDE_HIDDEN command,
142129
// then this member is set to the PROVIDE symbol name.
143130
std::optional<llvm::StringRef> activeProvideSym;
@@ -319,7 +306,7 @@ void ScriptParser::readNoCrossRefs(bool to) {
319306
}
320307

321308
void ScriptParser::addFile(StringRef s) {
322-
if (isUnderSysroot && s.starts_with("/")) {
309+
if (curBuf.isUnderSysroot && s.starts_with("/")) {
323310
SmallString<128> pathData;
324311
StringRef path = (config->sysroot + s).toStringRef(pathData);
325312
if (sys::fs::exists(path))

lld/test/ELF/linkerscript/group.s

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
# RUN: echo 'GROUP(AS_NEEDED("a.o"))INPUT(/libb.a)' > %t.dir/3a.t
2525
# RUN: ld.lld 3.t --sysroot=%t.dir
2626
# RUN: llvm-nm a.out | FileCheck %s
27-
# RUN: not ld.lld 3i.t --sysroot=%t.dir 2>&1 | FileCheck %s --check-prefix=CANNOT_OPEN -DFILE=/libb.a
27+
# RUN: ld.lld 3i.t --sysroot=%t.dir
28+
# RUN: llvm-nm a.out | FileCheck %s
2829

2930
# RUN: echo 'GROUP("%t.dir/4a.t")INPUT(/libb.a)' > 4.t
3031
# RUN: echo 'GROUP(AS_NEEDED("a.o"))' > %t.dir/4a.t

0 commit comments

Comments
 (0)