Skip to content

Commit 68e088b

Browse files
committed
Go back to previous more contained thin archive bitcode members scheme
1 parent 46934b6 commit 68e088b

File tree

4 files changed

+43
-36
lines changed

4 files changed

+43
-36
lines changed

lld/ELF/Config.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,7 @@ class LinkerDriver {
166166
template <class ELFT> void link(llvm::opt::InputArgList &args);
167167
template <class ELFT> void compileBitcodeFiles(bool skipLinkedOutput);
168168
bool tryAddFatLTOFile(MemoryBufferRef mb, StringRef archiveName,
169-
uint64_t offsetInArchive, bool isThinArchive,
170-
bool lazy);
169+
uint64_t offsetInArchive, bool lazy);
171170
// True if we are in --whole-archive and --no-whole-archive.
172171
bool inWholeArchive = false;
173172

lld/ELF/Driver.cpp

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -231,16 +231,15 @@ static bool isBitcode(MemoryBufferRef mb) {
231231
}
232232

233233
bool LinkerDriver::tryAddFatLTOFile(MemoryBufferRef mb, StringRef archiveName,
234-
uint64_t offsetInArchive,
235-
bool isThinArchive, bool lazy) {
234+
uint64_t offsetInArchive, bool lazy) {
236235
if (!ctx.arg.fatLTOObjects)
237236
return false;
238237
Expected<MemoryBufferRef> fatLTOData =
239238
IRObjectFile::findBitcodeInMemBuffer(mb);
240239
if (errorToBool(fatLTOData.takeError()))
241240
return false;
242-
files.push_back(std::make_unique<BitcodeFile>(
243-
ctx, *fatLTOData, archiveName, offsetInArchive, isThinArchive, lazy));
241+
files.push_back(std::make_unique<BitcodeFile>(ctx, *fatLTOData, archiveName,
242+
offsetInArchive, lazy));
244243
return true;
245244
}
246245

@@ -263,15 +262,13 @@ void LinkerDriver::addFile(StringRef path, bool withLOption) {
263262
readLinkerScript(ctx, mbref);
264263
return;
265264
case file_magic::archive: {
266-
bool isThinArchive = mbref.getBuffer().starts_with(ThinArchiveMagic);
267265
auto members = getArchiveMembers(ctx, mbref);
268266
if (inWholeArchive) {
269267
for (const std::pair<MemoryBufferRef, uint64_t> &p : members) {
270268
if (isBitcode(p.first))
271-
files.push_back(std::make_unique<BitcodeFile>(
272-
ctx, p.first, path, p.second, isThinArchive, false));
273-
else if (!tryAddFatLTOFile(p.first, path, p.second, isThinArchive,
274-
false))
269+
files.push_back(std::make_unique<BitcodeFile>(ctx, p.first, path,
270+
p.second, false));
271+
else if (!tryAddFatLTOFile(p.first, path, p.second, false))
275272
files.push_back(createObjFile(ctx, p.first, path));
276273
}
277274
return;
@@ -295,11 +292,11 @@ void LinkerDriver::addFile(StringRef path, bool withLOption) {
295292
for (const std::pair<MemoryBufferRef, uint64_t> &p : members) {
296293
auto magic = identify_magic(p.first.getBuffer());
297294
if (magic == file_magic::elf_relocatable) {
298-
if (!tryAddFatLTOFile(p.first, path, p.second, isThinArchive, true))
295+
if (!tryAddFatLTOFile(p.first, path, p.second, true))
299296
files.push_back(createObjFile(ctx, p.first, path, true));
300297
} else if (magic == file_magic::bitcode)
301-
files.push_back(std::make_unique<BitcodeFile>(
302-
ctx, p.first, path, p.second, isThinArchive, true));
298+
files.push_back(
299+
std::make_unique<BitcodeFile>(ctx, p.first, path, p.second, true));
303300
else
304301
Warn(ctx) << path << ": archive member '"
305302
<< p.first.getBufferIdentifier()
@@ -327,11 +324,10 @@ void LinkerDriver::addFile(StringRef path, bool withLOption) {
327324
return;
328325
}
329326
case file_magic::bitcode:
330-
files.push_back(
331-
std::make_unique<BitcodeFile>(ctx, mbref, "", 0, false, inLib));
327+
files.push_back(std::make_unique<BitcodeFile>(ctx, mbref, "", 0, inLib));
332328
break;
333329
case file_magic::elf_relocatable:
334-
if (!tryAddFatLTOFile(mbref, "", 0, false, inLib))
330+
if (!tryAddFatLTOFile(mbref, "", 0, inLib))
335331
files.push_back(createObjFile(ctx, mbref, "", inLib));
336332
break;
337333
default:

lld/ELF/InputFiles.cpp

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1703,49 +1703,61 @@ static uint8_t getOsAbi(const Triple &t) {
17031703
}
17041704
}
17051705

1706-
static SmallString<64> dtltoGetMemberPathIfThinArchive(StringRef archivePath,
1707-
StringRef memberPath) {
1706+
// For DTLTO, bitcode member names must be a valid path to a bitcode file on
1707+
// disk. For thin archives, adjust `memberPath` to the full file path of the
1708+
// archive member. Returns true if an adjustment was made; false otherwise.
1709+
// Non-thin archives are not yet supported.
1710+
static bool dtltoAdjustMemberPathIfThinArchive(Ctx &ctx, StringRef archivePath,
1711+
std::string &memberPath) {
17081712
assert(!archivePath.empty());
1713+
assert(!ctx.arg.dtltoDistributor.empty());
1714+
1715+
// Check if the archive file is a thin archive by reading its header.
1716+
auto memBufferOrError =
1717+
MemoryBuffer::getFileSlice(archivePath, sizeof(ThinArchiveMagic) - 1, 0);
1718+
if (std::error_code ec = memBufferOrError.getError()) {
1719+
ErrAlways(ctx) << "cannot open " << archivePath << ": " << ec.message();
1720+
return false;
1721+
}
1722+
MemoryBufferRef memBufRef = *memBufferOrError.get();
1723+
if (!memBufRef.getBuffer().starts_with(ThinArchiveMagic))
1724+
return false;
1725+
17091726
SmallString<64> archiveMemberPath;
17101727
if (path::is_relative(memberPath)) {
17111728
archiveMemberPath = path::parent_path(archivePath);
17121729
path::append(archiveMemberPath, memberPath);
17131730
} else
17141731
archiveMemberPath = memberPath;
1732+
17151733
path::remove_dots(archiveMemberPath, /*remove_dot_dot=*/true);
1716-
return archiveMemberPath;
1734+
memberPath = archiveMemberPath.str();
1735+
return true;
17171736
}
17181737

17191738
BitcodeFile::BitcodeFile(Ctx &ctx, MemoryBufferRef mb, StringRef archiveName,
1720-
uint64_t offsetInArchive, bool isThinArchive,
1721-
bool lazy)
1739+
uint64_t offsetInArchive, bool lazy)
17221740
: InputFile(ctx, BitcodeKind, mb) {
17231741
this->archiveName = archiveName;
17241742
this->lazy = lazy;
17251743

17261744
std::string path = mb.getBufferIdentifier().str();
17271745
if (ctx.arg.thinLTOIndexOnly)
17281746
path = replaceThinLTOSuffix(ctx, mb.getBufferIdentifier());
1729-
1730-
// For DTLTO the member name needs to be a valid path to a bitcode file on
1731-
// disk. For ThinArchives we compute that. Non-thin archives are not yet
1732-
// supported.
1733-
bool dtltoAdjustThinArchivePath = !archiveName.empty() && isThinArchive &&
1734-
!ctx.arg.dtltoDistributor.empty();
1735-
if (dtltoAdjustThinArchivePath)
1736-
path = dtltoGetMemberPathIfThinArchive(archiveName, path).str();
1737-
17381747
// ThinLTO assumes that all MemoryBufferRefs given to it have a unique
17391748
// name. If two archives define two members with the same name, this
17401749
// causes a collision which result in only one of the objects being taken
17411750
// into consideration at LTO time (which very likely causes undefined
17421751
// symbols later in the link stage). So we append file offset to make
17431752
// filename unique.
17441753
StringSaver &ss = ctx.saver;
1745-
StringRef name = (archiveName.empty() || dtltoAdjustThinArchivePath)
1746-
? ss.save(path)
1747-
: ss.save(archiveName + "(" + path::filename(path) +
1748-
" at " + utostr(offsetInArchive) + ")");
1754+
StringRef name =
1755+
(archiveName.empty() ||
1756+
(!ctx.arg.dtltoDistributor.empty() &&
1757+
dtltoAdjustMemberPathIfThinArchive(ctx, archiveName, path)))
1758+
? ss.save(path)
1759+
: ss.save(archiveName + "(" + path::filename(path) + " at " +
1760+
utostr(offsetInArchive) + ")");
17491761
MemoryBufferRef mbref(mb.getBuffer(), name);
17501762

17511763
obj = CHECK2(lto::InputFile::create(mbref), this);

lld/ELF/InputFiles.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ template <class ELFT> class ObjFile : public ELFFileBase {
324324
class BitcodeFile : public InputFile {
325325
public:
326326
BitcodeFile(Ctx &, MemoryBufferRef m, StringRef archiveName,
327-
uint64_t offsetInArchive, bool isThinArchive, bool lazy);
327+
uint64_t offsetInArchive, bool lazy);
328328
static bool classof(const InputFile *f) { return f->kind() == BitcodeKind; }
329329
void parse();
330330
void parseLazy();

0 commit comments

Comments
 (0)