Skip to content

Commit d5f6506

Browse files
committed
[WebAssembly] Refactor config setting and checking. NFC.
This matches the way the ELF backend does it. Differential Revision: https://reviews.llvm.org/D54559 llvm-svn: 346972
1 parent 642c8d3 commit d5f6506

File tree

2 files changed

+66
-53
lines changed

2 files changed

+66
-53
lines changed

lld/ELF/Driver.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ static void initLLVM() {
275275

276276
// Some command line options or some combinations of them are not allowed.
277277
// This function checks for such errors.
278-
static void checkOptions(opt::InputArgList &Args) {
278+
static void checkOptions() {
279279
// The MIPS ABI as of 2016 does not support the GNU-style symbol lookup
280280
// table which is a relatively new feature.
281281
if (Config->EMachine == EM_MIPS && Config->GnuHash)
@@ -429,7 +429,7 @@ void LinkerDriver::main(ArrayRef<const char *> ArgsArr) {
429429

430430
inferMachineType();
431431
setConfigs(Args);
432-
checkOptions(Args);
432+
checkOptions();
433433
if (errorCount())
434434
return;
435435

lld/wasm/Driver.cpp

Lines changed: 64 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -325,49 +325,11 @@ static void handleWeakUndefines() {
325325
}
326326
}
327327

328-
// Force Sym to be entered in the output. Used for -u or equivalent.
329-
static Symbol *handleUndefined(StringRef Name) {
330-
Symbol *Sym = Symtab->find(Name);
331-
if (!Sym)
332-
return nullptr;
333-
334-
// Since symbol S may not be used inside the program, LTO may
335-
// eliminate it. Mark the symbol as "used" to prevent it.
336-
Sym->IsUsedInRegularObj = true;
337-
338-
if (auto *LazySym = dyn_cast<LazySymbol>(Sym))
339-
LazySym->fetch();
340-
341-
return Sym;
342-
}
343-
344-
void LinkerDriver::link(ArrayRef<const char *> ArgsArr) {
345-
WasmOptTable Parser;
346-
opt::InputArgList Args = Parser.parse(ArgsArr.slice(1));
347-
348-
// Handle --help
349-
if (Args.hasArg(OPT_help)) {
350-
Parser.PrintHelp(outs(),
351-
(std::string(ArgsArr[0]) + " [options] file...").c_str(),
352-
"LLVM Linker", false);
353-
return;
354-
}
355-
356-
// Handle --version
357-
if (Args.hasArg(OPT_version) || Args.hasArg(OPT_v)) {
358-
outs() << getLLDVersion() << "\n";
359-
return;
360-
}
361-
362-
// Parse and evaluate -mllvm options.
363-
std::vector<const char *> V;
364-
V.push_back("wasm-ld (LLVM option parsing)");
365-
for (auto *Arg : Args.filtered(OPT_mllvm))
366-
V.push_back(Arg->getValue());
367-
cl::ParseCommandLineOptions(V.size(), V.data());
368-
369-
errorHandler().ErrorLimit = args::getInteger(Args, OPT_error_limit, 20);
370-
328+
// Some Config members do not directly correspond to any particular
329+
// command line options, but computed based on other Config values.
330+
// This function initialize such members. See Config.h for the details
331+
// of these values.
332+
static void setConfigs(opt::InputArgList &Args) {
371333
Config->AllowUndefined = Args.hasArg(OPT_allow_undefined);
372334
Config->CompressRelocations = Args.hasArg(OPT_compress_relocations);
373335
Config->Demangle = Args.hasFlag(OPT_demangle, OPT_no_demangle, true);
@@ -414,7 +376,11 @@ void LinkerDriver::link(ArrayRef<const char *> ArgsArr) {
414376
Config->MaxMemory = args::getInteger(Args, OPT_max_memory, 0);
415377
Config->ZStackSize =
416378
args::getZOptionValue(Args, OPT_z, "stack-size", WasmPageSize);
379+
}
417380

381+
// Some command line options or some combinations of them are not allowed.
382+
// This function checks for such errors.
383+
static void checkOptions(opt::InputArgList &Args) {
418384
if (!Config->StripDebug && !Config->StripAll && Config->CompressRelocations)
419385
error("--compress-relocations is incompatible with output debug"
420386
" information. Please pass --strip-debug or --strip-all");
@@ -426,14 +392,6 @@ void LinkerDriver::link(ArrayRef<const char *> ArgsArr) {
426392
if (Config->ThinLTOJobs == 0)
427393
error("--thinlto-jobs: number of threads must be > 0");
428394

429-
if (auto *Arg = Args.getLastArg(OPT_allow_undefined_file))
430-
readImportFile(Arg->getValue());
431-
432-
if (!Args.hasArg(OPT_INPUT)) {
433-
error("no input files");
434-
return;
435-
}
436-
437395
if (Config->Pie && Config->Shared)
438396
error("-shared and -pie may not be used together");
439397

@@ -455,6 +413,61 @@ void LinkerDriver::link(ArrayRef<const char *> ArgsArr) {
455413
if (Config->Pie)
456414
error("-r and -pie may not be used together");
457415
}
416+
}
417+
418+
// Force Sym to be entered in the output. Used for -u or equivalent.
419+
static Symbol *handleUndefined(StringRef Name) {
420+
Symbol *Sym = Symtab->find(Name);
421+
if (!Sym)
422+
return nullptr;
423+
424+
// Since symbol S may not be used inside the program, LTO may
425+
// eliminate it. Mark the symbol as "used" to prevent it.
426+
Sym->IsUsedInRegularObj = true;
427+
428+
if (auto *LazySym = dyn_cast<LazySymbol>(Sym))
429+
LazySym->fetch();
430+
431+
return Sym;
432+
}
433+
434+
void LinkerDriver::link(ArrayRef<const char *> ArgsArr) {
435+
WasmOptTable Parser;
436+
opt::InputArgList Args = Parser.parse(ArgsArr.slice(1));
437+
438+
// Handle --help
439+
if (Args.hasArg(OPT_help)) {
440+
Parser.PrintHelp(outs(),
441+
(std::string(ArgsArr[0]) + " [options] file...").c_str(),
442+
"LLVM Linker", false);
443+
return;
444+
}
445+
446+
// Handle --version
447+
if (Args.hasArg(OPT_version) || Args.hasArg(OPT_v)) {
448+
outs() << getLLDVersion() << "\n";
449+
return;
450+
}
451+
452+
// Parse and evaluate -mllvm options.
453+
std::vector<const char *> V;
454+
V.push_back("wasm-ld (LLVM option parsing)");
455+
for (auto *Arg : Args.filtered(OPT_mllvm))
456+
V.push_back(Arg->getValue());
457+
cl::ParseCommandLineOptions(V.size(), V.data());
458+
459+
errorHandler().ErrorLimit = args::getInteger(Args, OPT_error_limit, 20);
460+
461+
setConfigs(Args);
462+
checkOptions(Args);
463+
464+
if (auto *Arg = Args.getLastArg(OPT_allow_undefined_file))
465+
readImportFile(Arg->getValue());
466+
467+
if (!Args.hasArg(OPT_INPUT)) {
468+
error("no input files");
469+
return;
470+
}
458471

459472
Config->Pic = Config->Pie || Config->Shared;
460473

0 commit comments

Comments
 (0)