Skip to content

Commit e24457a

Browse files
committed
[ELF] Migrate away from global ctx
1 parent b4adce0 commit e24457a

File tree

9 files changed

+66
-56
lines changed

9 files changed

+66
-56
lines changed

lld/ELF/Arch/MipsArchTree.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ static uint32_t getMiscFlags(ArrayRef<FileFlags> files) {
102102
return ret;
103103
}
104104

105-
static uint32_t getPicFlags(ArrayRef<FileFlags> files) {
105+
static uint32_t getPicFlags(Ctx &ctx, ArrayRef<FileFlags> files) {
106106
// Check PIC/non-PIC compatibility.
107107
bool isPic = files[0].flags & (EF_MIPS_PIC | EF_MIPS_CPIC);
108108
for (const FileFlags &f : files.slice(1)) {
@@ -274,7 +274,7 @@ static std::string getFullArchName(uint32_t flags) {
274274
// Output file gets EF_MIPS_ARCH_2 flag. From the other side mips3 and mips32
275275
// are incompatible because nor mips3 is a parent for misp32, nor mips32
276276
// is a parent for mips3.
277-
static uint32_t getArchFlags(ArrayRef<FileFlags> files) {
277+
static uint32_t getArchFlags(Ctx &ctx, ArrayRef<FileFlags> files) {
278278
uint32_t ret = files[0].flags & (EF_MIPS_ARCH | EF_MIPS_MACH);
279279

280280
for (const FileFlags &f : files.slice(1)) {
@@ -307,7 +307,7 @@ template <class ELFT> uint32_t elf::calcMipsEFlags(Ctx &ctx) {
307307
return ctx.arg.mipsN32Abi ? EF_MIPS_ABI2 : EF_MIPS_ABI_O32;
308308
}
309309
checkFlags(ctx, v);
310-
return getMiscFlags(v) | getPicFlags(v) | getArchFlags(v);
310+
return getMiscFlags(v) | getPicFlags(ctx, v) | getArchFlags(ctx, v);
311311
}
312312

313313
static int compareMipsFpAbi(uint8_t fpA, uint8_t fpB) {
@@ -350,7 +350,7 @@ static StringRef getMipsFpAbiName(uint8_t fpAbi) {
350350
}
351351
}
352352

353-
uint8_t elf::getMipsFpAbiFlag(uint8_t oldFlag, uint8_t newFlag,
353+
uint8_t elf::getMipsFpAbiFlag(Ctx &ctx, uint8_t oldFlag, uint8_t newFlag,
354354
StringRef fileName) {
355355
if (compareMipsFpAbi(newFlag, oldFlag) >= 0)
356356
return newFlag;

lld/ELF/Driver.cpp

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ static uint8_t getZStartStopVisibility(opt::InputArgList &args) {
600600
return ret;
601601
}
602602

603-
static GcsPolicy getZGcs(opt::InputArgList &args) {
603+
static GcsPolicy getZGcs(Ctx &ctx, opt::InputArgList &args) {
604604
GcsPolicy ret = GcsPolicy::Implicit;
605605
for (auto *arg : args.filtered(OPT_z)) {
606606
std::pair<StringRef, StringRef> kv = StringRef(arg->getValue()).split('=');
@@ -620,7 +620,7 @@ static GcsPolicy getZGcs(opt::InputArgList &args) {
620620
}
621621

622622
// Report a warning for an unknown -z option.
623-
static void checkZOptions(opt::InputArgList &args) {
623+
static void checkZOptions(Ctx &ctx, opt::InputArgList &args) {
624624
// This function is called before getTarget(), when certain options are not
625625
// initialized yet. Claim them here.
626626
args::getZOptionValue(args, OPT_z, "max-page-size", 0);
@@ -639,7 +639,7 @@ LinkerDriver::LinkerDriver(Ctx &ctx) : ctx(ctx) {}
639639

640640
void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
641641
ELFOptTable parser;
642-
opt::InputArgList args = parser.parse(argsArr.slice(1));
642+
opt::InputArgList args = parser.parse(ctx, argsArr.slice(1));
643643

644644
// Interpret these flags early because error()/warn() depend on them.
645645
errorHandler().errorLimit = args::getInteger(args, OPT_error_limit, 20);
@@ -689,7 +689,7 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
689689
}
690690

691691
readConfigs(ctx, args);
692-
checkZOptions(args);
692+
checkZOptions(ctx, args);
693693

694694
// The behavior of -v or --version is a bit strange, but this is
695695
// needed for compatibility with GNU linkers.
@@ -790,7 +790,7 @@ static void setUnresolvedSymbolPolicy(Ctx &ctx, opt::InputArgList &args) {
790790
diagShlib ? errorOrWarn : UnresolvedPolicy::Ignore;
791791
}
792792

793-
static Target2Policy getTarget2(opt::InputArgList &args) {
793+
static Target2Policy getTarget2(Ctx &ctx, opt::InputArgList &args) {
794794
StringRef s = args.getLastArgValue(OPT_target2, "got-rel");
795795
if (s == "rel")
796796
return Target2Policy::Rel;
@@ -802,7 +802,7 @@ static Target2Policy getTarget2(opt::InputArgList &args) {
802802
return Target2Policy::GotRel;
803803
}
804804

805-
static bool isOutputFormatBinary(opt::InputArgList &args) {
805+
static bool isOutputFormatBinary(Ctx &ctx, opt::InputArgList &args) {
806806
StringRef s = args.getLastArgValue(OPT_oformat, "elf");
807807
if (s == "binary")
808808
return true;
@@ -882,7 +882,8 @@ static StripPolicy getStrip(Ctx &ctx, opt::InputArgList &args) {
882882
return StripPolicy::Debug;
883883
}
884884

885-
static uint64_t parseSectionAddress(StringRef s, opt::InputArgList &args,
885+
static uint64_t parseSectionAddress(Ctx &ctx, StringRef s,
886+
opt::InputArgList &args,
886887
const opt::Arg &arg) {
887888
uint64_t va = 0;
888889
s.consume_front("0x");
@@ -891,25 +892,26 @@ static uint64_t parseSectionAddress(StringRef s, opt::InputArgList &args,
891892
return va;
892893
}
893894

894-
static StringMap<uint64_t> getSectionStartMap(opt::InputArgList &args) {
895+
static StringMap<uint64_t> getSectionStartMap(Ctx &ctx,
896+
opt::InputArgList &args) {
895897
StringMap<uint64_t> ret;
896898
for (auto *arg : args.filtered(OPT_section_start)) {
897899
StringRef name;
898900
StringRef addr;
899901
std::tie(name, addr) = StringRef(arg->getValue()).split('=');
900-
ret[name] = parseSectionAddress(addr, args, *arg);
902+
ret[name] = parseSectionAddress(ctx, addr, args, *arg);
901903
}
902904

903905
if (auto *arg = args.getLastArg(OPT_Ttext))
904-
ret[".text"] = parseSectionAddress(arg->getValue(), args, *arg);
906+
ret[".text"] = parseSectionAddress(ctx, arg->getValue(), args, *arg);
905907
if (auto *arg = args.getLastArg(OPT_Tdata))
906-
ret[".data"] = parseSectionAddress(arg->getValue(), args, *arg);
908+
ret[".data"] = parseSectionAddress(ctx, arg->getValue(), args, *arg);
907909
if (auto *arg = args.getLastArg(OPT_Tbss))
908-
ret[".bss"] = parseSectionAddress(arg->getValue(), args, *arg);
910+
ret[".bss"] = parseSectionAddress(ctx, arg->getValue(), args, *arg);
909911
return ret;
910912
}
911913

912-
static SortSectionPolicy getSortSection(opt::InputArgList &args) {
914+
static SortSectionPolicy getSortSection(Ctx &ctx, opt::InputArgList &args) {
913915
StringRef s = args.getLastArgValue(OPT_sort_section);
914916
if (s == "alignment")
915917
return SortSectionPolicy::Alignment;
@@ -920,7 +922,8 @@ static SortSectionPolicy getSortSection(opt::InputArgList &args) {
920922
return SortSectionPolicy::Default;
921923
}
922924

923-
static OrphanHandlingPolicy getOrphanHandling(opt::InputArgList &args) {
925+
static OrphanHandlingPolicy getOrphanHandling(Ctx &ctx,
926+
opt::InputArgList &args) {
924927
StringRef s = args.getLastArgValue(OPT_orphan_handling, "place");
925928
if (s == "warn")
926929
return OrphanHandlingPolicy::Warn;
@@ -935,7 +938,7 @@ static OrphanHandlingPolicy getOrphanHandling(opt::InputArgList &args) {
935938
// synonym for "sha1" because all our hash functions including
936939
// --build-id=sha1 are actually tree hashes for performance reasons.
937940
static std::pair<BuildIdKind, SmallVector<uint8_t, 0>>
938-
getBuildId(opt::InputArgList &args) {
941+
getBuildId(Ctx &ctx, opt::InputArgList &args) {
939942
auto *arg = args.getLastArg(OPT_build_id);
940943
if (!arg)
941944
return {BuildIdKind::None, {}};
@@ -957,7 +960,8 @@ getBuildId(opt::InputArgList &args) {
957960
return {BuildIdKind::None, {}};
958961
}
959962

960-
static std::pair<bool, bool> getPackDynRelocs(opt::InputArgList &args) {
963+
static std::pair<bool, bool> getPackDynRelocs(Ctx &ctx,
964+
opt::InputArgList &args) {
961965
StringRef s = args.getLastArgValue(OPT_pack_dyn_relocs, "none");
962966
if (s == "android")
963967
return {true, false};
@@ -1152,7 +1156,8 @@ static void ltoValidateAllVtablesHaveTypeInfos(Ctx &ctx,
11521156
}
11531157
}
11541158

1155-
static CGProfileSortKind getCGProfileSortKind(opt::InputArgList &args) {
1159+
static CGProfileSortKind getCGProfileSortKind(Ctx &ctx,
1160+
opt::InputArgList &args) {
11561161
StringRef s = args.getLastArgValue(OPT_call_graph_profile_sort, "cdsort");
11571162
if (s == "hfsort")
11581163
return CGProfileSortKind::Hfsort;
@@ -1163,7 +1168,8 @@ static CGProfileSortKind getCGProfileSortKind(opt::InputArgList &args) {
11631168
return CGProfileSortKind::None;
11641169
}
11651170

1166-
static DebugCompressionType getCompressionType(StringRef s, StringRef option) {
1171+
static DebugCompressionType getCompressionType(Ctx &ctx, StringRef s,
1172+
StringRef option) {
11671173
DebugCompressionType type = StringSwitch<DebugCompressionType>(s)
11681174
.Case("zlib", DebugCompressionType::Zlib)
11691175
.Case("zstd", DebugCompressionType::Zstd)
@@ -1184,8 +1190,8 @@ static StringRef getAliasSpelling(opt::Arg *arg) {
11841190
return arg->getSpelling();
11851191
}
11861192

1187-
static std::pair<StringRef, StringRef> getOldNewOptions(opt::InputArgList &args,
1188-
unsigned id) {
1193+
static std::pair<StringRef, StringRef>
1194+
getOldNewOptions(Ctx &ctx, opt::InputArgList &args, unsigned id) {
11891195
auto *arg = args.getLastArg(id);
11901196
if (!arg)
11911197
return {"", ""};
@@ -1200,8 +1206,8 @@ static std::pair<StringRef, StringRef> getOldNewOptions(opt::InputArgList &args,
12001206

12011207
// Parse options of the form "old;new[;extra]".
12021208
static std::tuple<StringRef, StringRef, StringRef>
1203-
getOldNewOptionsExtra(opt::InputArgList &args, unsigned id) {
1204-
auto [oldDir, second] = getOldNewOptions(args, id);
1209+
getOldNewOptionsExtra(Ctx &ctx, opt::InputArgList &args, unsigned id) {
1210+
auto [oldDir, second] = getOldNewOptions(ctx, args, id);
12051211
auto [newDir, extraDir] = second.split(';');
12061212
return {oldDir, newDir, extraDir};
12071213
}
@@ -1303,13 +1309,13 @@ static void readConfigs(Ctx &ctx, opt::InputArgList &args) {
13031309
else if (arg->getOption().matches(OPT_Bsymbolic))
13041310
ctx.arg.bsymbolic = BsymbolicKind::All;
13051311
}
1306-
ctx.arg.callGraphProfileSort = getCGProfileSortKind(args);
1312+
ctx.arg.callGraphProfileSort = getCGProfileSortKind(ctx, args);
13071313
ctx.arg.checkSections =
13081314
args.hasFlag(OPT_check_sections, OPT_no_check_sections, true);
13091315
ctx.arg.chroot = args.getLastArgValue(OPT_chroot);
13101316
if (auto *arg = args.getLastArg(OPT_compress_debug_sections)) {
13111317
ctx.arg.compressDebugSections =
1312-
getCompressionType(arg->getValue(), "--compress-debug-sections");
1318+
getCompressionType(ctx, arg->getValue(), "--compress-debug-sections");
13131319
}
13141320
ctx.arg.cref = args.hasArg(OPT_cref);
13151321
ctx.arg.optimizeBBJumps =
@@ -1406,7 +1412,7 @@ static void readConfigs(Ctx &ctx, opt::InputArgList &args) {
14061412
ctx.arg.nmagic = args.hasFlag(OPT_nmagic, OPT_no_nmagic, false);
14071413
ctx.arg.noinhibitExec = args.hasArg(OPT_noinhibit_exec);
14081414
ctx.arg.nostdlib = args.hasArg(OPT_nostdlib);
1409-
ctx.arg.oFormatBinary = isOutputFormatBinary(args);
1415+
ctx.arg.oFormatBinary = isOutputFormatBinary(ctx, args);
14101416
ctx.arg.omagic = args.hasFlag(OPT_omagic, OPT_no_omagic, false);
14111417
ctx.arg.optRemarksFilename = args.getLastArgValue(OPT_opt_remarks_filename);
14121418
ctx.arg.optStatsFilename = args.getLastArgValue(OPT_plugin_opt_stats_file);
@@ -1426,7 +1432,7 @@ static void readConfigs(Ctx &ctx, opt::InputArgList &args) {
14261432
ctx.arg.optRemarksWithHotness = args.hasArg(OPT_opt_remarks_with_hotness);
14271433
ctx.arg.optRemarksFormat = args.getLastArgValue(OPT_opt_remarks_format);
14281434
ctx.arg.optimize = args::getInteger(args, OPT_O, 1);
1429-
ctx.arg.orphanHandling = getOrphanHandling(args);
1435+
ctx.arg.orphanHandling = getOrphanHandling(ctx, args);
14301436
ctx.arg.outputFile = args.getLastArgValue(OPT_o);
14311437
ctx.arg.packageMetadata = args.getLastArgValue(OPT_package_metadata);
14321438
ctx.arg.pie = args.hasFlag(OPT_pie, OPT_no_pie, false);
@@ -1460,19 +1466,19 @@ static void readConfigs(Ctx &ctx, opt::InputArgList &args) {
14601466
}
14611467

14621468
ctx.arg.searchPaths = args::getStrings(args, OPT_library_path);
1463-
ctx.arg.sectionStartMap = getSectionStartMap(args);
1469+
ctx.arg.sectionStartMap = getSectionStartMap(ctx, args);
14641470
ctx.arg.shared = args.hasArg(OPT_shared);
14651471
ctx.arg.singleRoRx = !args.hasFlag(OPT_rosegment, OPT_no_rosegment, true);
14661472
ctx.arg.soName = args.getLastArgValue(OPT_soname);
1467-
ctx.arg.sortSection = getSortSection(args);
1473+
ctx.arg.sortSection = getSortSection(ctx, args);
14681474
ctx.arg.splitStackAdjustSize =
14691475
args::getInteger(args, OPT_split_stack_adjust_size, 16384);
14701476
ctx.arg.zSectionHeader =
14711477
getZFlag(args, "sectionheader", "nosectionheader", true);
14721478
ctx.arg.strip = getStrip(ctx, args); // needs zSectionHeader
14731479
ctx.arg.sysroot = args.getLastArgValue(OPT_sysroot);
14741480
ctx.arg.target1Rel = args.hasFlag(OPT_target1_rel, OPT_target1_abs, false);
1475-
ctx.arg.target2 = getTarget2(args);
1481+
ctx.arg.target2 = getTarget2(ctx, args);
14761482
ctx.arg.thinLTOCacheDir = args.getLastArgValue(OPT_thinlto_cache_dir);
14771483
ctx.arg.thinLTOCachePolicy = CHECK(
14781484
parseCachePruningPolicy(args.getLastArgValue(OPT_thinlto_cache_policy)),
@@ -1485,10 +1491,10 @@ static void readConfigs(Ctx &ctx, opt::InputArgList &args) {
14851491
args.hasArg(OPT_thinlto_index_only_eq);
14861492
ctx.arg.thinLTOIndexOnlyArg = args.getLastArgValue(OPT_thinlto_index_only_eq);
14871493
ctx.arg.thinLTOObjectSuffixReplace =
1488-
getOldNewOptions(args, OPT_thinlto_object_suffix_replace_eq);
1494+
getOldNewOptions(ctx, args, OPT_thinlto_object_suffix_replace_eq);
14891495
std::tie(ctx.arg.thinLTOPrefixReplaceOld, ctx.arg.thinLTOPrefixReplaceNew,
14901496
ctx.arg.thinLTOPrefixReplaceNativeObject) =
1491-
getOldNewOptionsExtra(args, OPT_thinlto_prefix_replace_eq);
1497+
getOldNewOptionsExtra(ctx, args, OPT_thinlto_prefix_replace_eq);
14921498
if (ctx.arg.thinLTOEmitIndexFiles && !ctx.arg.thinLTOIndexOnly) {
14931499
if (args.hasArg(OPT_thinlto_object_suffix_replace_eq))
14941500
ErrAlways(ctx) << "--thinlto-object-suffix-replace is not supported with "
@@ -1525,7 +1531,7 @@ static void readConfigs(Ctx &ctx, opt::InputArgList &args) {
15251531
ctx.arg.zCopyreloc = getZFlag(args, "copyreloc", "nocopyreloc", true);
15261532
ctx.arg.zForceBti = hasZOption(args, "force-bti");
15271533
ctx.arg.zForceIbt = hasZOption(args, "force-ibt");
1528-
ctx.arg.zGcs = getZGcs(args);
1534+
ctx.arg.zGcs = getZGcs(ctx, args);
15291535
ctx.arg.zGlobal = hasZOption(args, "global");
15301536
ctx.arg.zGnustack = getZGnuStack(args);
15311537
ctx.arg.zHazardplt = hasZOption(args, "hazardplt");
@@ -1627,7 +1633,7 @@ static void readConfigs(Ctx &ctx, opt::InputArgList &args) {
16271633
continue;
16281634
}
16291635
auto [typeStr, levelStr] = fields[1].split(':');
1630-
auto type = getCompressionType(typeStr, arg->getSpelling());
1636+
auto type = getCompressionType(ctx, typeStr, arg->getSpelling());
16311637
unsigned level = 0;
16321638
if (fields[1].size() != typeStr.size() &&
16331639
!llvm::to_integer(levelStr, level)) {
@@ -1781,14 +1787,14 @@ static void readConfigs(Ctx &ctx, opt::InputArgList &args) {
17811787
if (ctx.arg.nmagic || ctx.arg.omagic || ctx.arg.relocatable)
17821788
ctx.arg.zRelro = false;
17831789

1784-
std::tie(ctx.arg.buildId, ctx.arg.buildIdVector) = getBuildId(args);
1790+
std::tie(ctx.arg.buildId, ctx.arg.buildIdVector) = getBuildId(ctx, args);
17851791

17861792
if (getZFlag(args, "pack-relative-relocs", "nopack-relative-relocs", false)) {
17871793
ctx.arg.relrGlibc = true;
17881794
ctx.arg.relrPackDynRelocs = true;
17891795
} else {
17901796
std::tie(ctx.arg.androidPackDynRelocs, ctx.arg.relrPackDynRelocs) =
1791-
getPackDynRelocs(args);
1797+
getPackDynRelocs(ctx, args);
17921798
}
17931799

17941800
if (auto *arg = args.getLastArg(OPT_symbol_ordering_file)){
@@ -1949,7 +1955,7 @@ static void setConfigs(Ctx &ctx, opt::InputArgList &args) {
19491955
}
19501956
}
19511957

1952-
static bool isFormatBinary(StringRef s) {
1958+
static bool isFormatBinary(Ctx &ctx, StringRef s) {
19531959
if (s == "binary")
19541960
return true;
19551961
if (s == "elf" || s == "default")
@@ -2006,7 +2012,7 @@ void LinkerDriver::createFiles(opt::InputArgList &args) {
20062012
ctx.arg.asNeeded = true;
20072013
break;
20082014
case OPT_format:
2009-
ctx.arg.formatBinary = isFormatBinary(arg->getValue());
2015+
ctx.arg.formatBinary = isFormatBinary(ctx, arg->getValue());
20102016
break;
20112017
case OPT_no_as_needed:
20122018
ctx.arg.asNeeded = false;

lld/ELF/Driver.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ struct Ctx;
2121
class ELFOptTable : public llvm::opt::GenericOptTable {
2222
public:
2323
ELFOptTable();
24-
llvm::opt::InputArgList parse(ArrayRef<const char *> argv);
24+
llvm::opt::InputArgList parse(Ctx &, ArrayRef<const char *> argv);
2525
};
2626

2727
// Create enum with OPT_xxx values for each option in Options.td

lld/ELF/DriverUtils.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ static void handleColorDiagnostics(opt::InputArgList &args) {
6565
ErrAlways(ctx) << "unknown option: --color-diagnostics=" << s;
6666
}
6767

68-
static cl::TokenizerCallback getQuotingStyle(opt::InputArgList &args) {
68+
static cl::TokenizerCallback getQuotingStyle(Ctx &ctx,
69+
opt::InputArgList &args) {
6970
if (auto *arg = args.getLastArg(OPT_rsp_quoting)) {
7071
StringRef s = arg->getValue();
7172
if (s != "windows" && s != "posix")
@@ -103,7 +104,7 @@ static void concatLTOPluginOptions(SmallVectorImpl<const char *> &args) {
103104
}
104105

105106
// Parses a given list of options.
106-
opt::InputArgList ELFOptTable::parse(ArrayRef<const char *> argv) {
107+
opt::InputArgList ELFOptTable::parse(Ctx &ctx, ArrayRef<const char *> argv) {
107108
// Make InputArgList from string vectors.
108109
unsigned missingIndex;
109110
unsigned missingCount;
@@ -116,7 +117,7 @@ opt::InputArgList ELFOptTable::parse(ArrayRef<const char *> argv) {
116117

117118
// Expand response files (arguments in the form of @<filename>)
118119
// and then parse the argument again.
119-
cl::ExpandResponseFiles(saver(), getQuotingStyle(args), vec);
120+
cl::ExpandResponseFiles(saver(), getQuotingStyle(ctx, args), vec);
120121
concatLTOPluginOptions(vec);
121122
args = this->ParseArgs(vec, missingIndex, missingCount);
122123

lld/ELF/ScriptLexer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ ScriptLexer::Buffer::Buffer(Ctx &ctx, MemoryBufferRef mb)
5454
}
5555

5656
ScriptLexer::ScriptLexer(Ctx &ctx, MemoryBufferRef mb)
57-
: curBuf(ctx, mb), mbs(1, mb) {
57+
: ctx(ctx), curBuf(ctx, mb), mbs(1, mb) {
5858
activeFilenames.insert(mb.getBufferIdentifier());
5959
}
6060

lld/ELF/ScriptLexer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class ScriptLexer {
3333
Buffer() = default;
3434
Buffer(Ctx &ctx, MemoryBufferRef mb);
3535
};
36+
Ctx &ctx;
3637
// The current buffer and parent buffers due to INCLUDE.
3738
Buffer curBuf;
3839
SmallVector<Buffer, 0> buffers;

0 commit comments

Comments
 (0)