Skip to content

Commit 0f6d720

Browse files
committed
[MachO] Properly reset global state
We need to reset global state between runs, similar to the other ports. There's some file-static state which needs to be reset as well and we need to add some new helpers for that. With this change, most LLD Mach-O tests pass with `LLD_IN_TEST=2` (which runs the linker twice on each test). Some tests will be fixed by the remainder of this stack, and the rest are fundamentally incompatible with that mode (e.g. they intentionally throw fatal errors). Fixes PR52070. Reviewed By: #lld-macho, int3 Differential Revision: https://reviews.llvm.org/D112878
1 parent f964ca8 commit 0f6d720

File tree

8 files changed

+38
-2
lines changed

8 files changed

+38
-2
lines changed

lld/MachO/Driver.cpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,7 +1068,25 @@ bool macho::link(ArrayRef<const char *> argsArr, bool canExitEarly,
10681068
lld::stdoutOS = &stdoutOS;
10691069
lld::stderrOS = &stderrOS;
10701070

1071-
errorHandler().cleanupCallback = []() { freeArena(); };
1071+
errorHandler().cleanupCallback = []() {
1072+
freeArena();
1073+
1074+
concatOutputSections.clear();
1075+
inputFiles.clear();
1076+
inputSections.clear();
1077+
loadedArchives.clear();
1078+
syntheticSections.clear();
1079+
thunkMap.clear();
1080+
1081+
firstTLVDataSection = nullptr;
1082+
tar = nullptr;
1083+
memset(&in, 0, sizeof(in));
1084+
1085+
resetLoadedDylibs();
1086+
resetOutputSegments();
1087+
resetWriter();
1088+
InputFile::resetIdCount();
1089+
};
10721090

10731091
errorHandler().logName = args::getFilenameWithoutExe(argsArr[0]);
10741092
stderrOS.enable_colors(stderrOS.has_colors());
@@ -1392,6 +1410,8 @@ bool macho::link(ArrayRef<const char *> argsArr, bool canExitEarly,
13921410
reexportHandler(arg, extensions);
13931411
}
13941412

1413+
cl::ResetAllOptionOccurrences();
1414+
13951415
// Parse LTO options.
13961416
if (const Arg *arg = args.getLastArg(OPT_mcpu))
13971417
parseClangOption(saver.save("-mcpu=" + StringRef(arg->getValue())),
@@ -1476,5 +1496,7 @@ bool macho::link(ArrayRef<const char *> argsArr, bool canExitEarly,
14761496
if (canExitEarly)
14771497
exitLld(errorCount() ? 1 : 0);
14781498

1479-
return !errorCount();
1499+
bool ret = errorCount() == 0;
1500+
errorHandler().reset();
1501+
return ret;
14801502
}

lld/MachO/Driver.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ llvm::Optional<StringRef> resolveDylibPath(llvm::StringRef path);
5656

5757
DylibFile *loadDylib(llvm::MemoryBufferRef mbref, DylibFile *umbrella = nullptr,
5858
bool isBundleLoader = false);
59+
void resetLoadedDylibs();
5960

6061
// Search for all possible combinations of `{root}/{name}.{extension}`.
6162
// If \p extensions are not specified, then just search for `{root}/{name}`.

lld/MachO/DriverUtils.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@ DylibFile *macho::loadDylib(MemoryBufferRef mbref, DylibFile *umbrella,
246246
return newFile;
247247
}
248248

249+
void macho::resetLoadedDylibs() { loadedDylibs.clear(); }
250+
249251
Optional<StringRef>
250252
macho::findPathCombination(const Twine &name,
251253
const std::vector<StringRef> &roots,

lld/MachO/InputFiles.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class InputFile {
6969
virtual ~InputFile() = default;
7070
Kind kind() const { return fileKind; }
7171
StringRef getName() const { return name; }
72+
static void resetIdCount() { idCount = 0; }
7273

7374
MemoryBufferRef mb;
7475

lld/MachO/OutputSegment.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,11 @@ void macho::sortOutputSegments() {
161161
static DenseMap<StringRef, OutputSegment *> nameToOutputSegment;
162162
std::vector<OutputSegment *> macho::outputSegments;
163163

164+
void macho::resetOutputSegments() {
165+
outputSegments.clear();
166+
nameToOutputSegment.clear();
167+
}
168+
164169
static StringRef maybeRenameSegment(StringRef name) {
165170
auto newName = config->segmentRenameMap.find(name);
166171
if (newName != config->segmentRenameMap.end())

lld/MachO/OutputSegment.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class OutputSegment {
6868
extern std::vector<OutputSegment *> outputSegments;
6969

7070
void sortOutputSegments();
71+
void resetOutputSegments();
7172

7273
OutputSegment *getOrCreateOutputSegment(StringRef name);
7374

lld/MachO/Writer.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ class LCDylib final : public LoadCommand {
343343
}
344344

345345
static uint32_t getInstanceCount() { return instanceCount; }
346+
static void resetInstanceCount() { instanceCount = 0; }
346347

347348
private:
348349
LoadCommandType type;
@@ -1153,6 +1154,8 @@ template <class LP> void Writer::run() {
11531154

11541155
template <class LP> void macho::writeResult() { Writer().run<LP>(); }
11551156

1157+
void macho::resetWriter() { LCDylib::resetInstanceCount(); }
1158+
11561159
void macho::createSyntheticSections() {
11571160
in.header = make<MachHeaderSection>();
11581161
if (config->dedupLiterals) {

lld/MachO/Writer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class LoadCommand {
2626
};
2727

2828
template <class LP> void writeResult();
29+
void resetWriter();
2930

3031
void createSyntheticSections();
3132

0 commit comments

Comments
 (0)