forked from rui314/mold
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmdline.cc
862 lines (802 loc) · 32.3 KB
/
cmdline.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
#include "mold.h"
#include <sstream>
#include <sys/stat.h>
#include <sys/types.h>
#include <tbb/global_control.h>
#include <unistd.h>
#include <unordered_set>
static const char helpmsg[] = R"(
Options:
--help Report usage information
-v, --version Report version information
-V Report version and target information
-(, --start-group Ignored
-), --end-group Ignored
-C DIR, --directory DIR Change to DIR before doing anything
-E, --export-dynamic Put symbols in the dynamic symbol table
--no-export-dynamic
-F LIBNAME, --filter LIBNAME
Set DT_FILTER to the specified value
-I FILE, --dynamic-linker FILE
Set dynamic linker path
--no-dynamic-linker
-L DIR, --library-path DIR Add DIR to library search path
-M, --print-map Write map file to stdout
-N, --omagic Do not page align data, do not make text readonly
--no-omagic
-O NUMBER Ignored
-S, --strip-debug Strip .debug_* sections
-T FILE, --script FILE Read linker script
-X, --discard-locals Discard temporary local symbols
-e SYMBOL, --entry SYMBOL Set program entry point
-f SHLIB, --auxiliary SHLIB Set DT_AUXILIARY to the specified value
-h LIBNAME, --soname LIBNAME
Set shared library name
-l LIBNAME Search for a given library
-m EMULATION Ignored
-o FILE, --output FILE Set output filename
-r, --relocatable Generate relocatable output
-s, --strip-all Strip .symtab section
-u SYMBOL, --undefined SYMBOL
Force to resolve SYMBOL
--Bdynamic Link against shared libraries (default)
--Bstatic Do not link against shared libraries
--Bsymbolic Bind global symbols locally
--Bsymbolic-functions Bind global functions locally
--Map FILE Write map file to a given file
--allow-multiple-definition Allow multiple definitions
--as-needed Only set DT_NEEDED if used
--no-as-needed
--build-id [none,md5,sha1,sha256,uuid,HEXSTRING]
Generate build ID
--no-build-id
--chroot DIR Set a given path to root directory
--color-diagnostics Ignored
--compress-debug-sections [none,zlib,zlib-gabi,zlib-gnu]
Compress .debug_* sections
--demangle Demangle C++ symbols in log messages (default)
--no-demangle
--disable-new-dtags Ignored
--dynamic-list Read a list of dynamic symbols
--eh-frame-hdr Create .eh_frame_hdr section
--no-eh-frame-hdr
--enable-new-dtags Ignored
--exclude-libs LIB,LIB,.. Mark all symbols in given libraries hidden
--fatal-warnings Ignored
--no-fatal-warnings Ignored
--fini SYMBOL Call SYMBOL at unload-time
--fork Spawn a child process (default)
--no-fork
--gc-sections Remove unreferenced sections
--no-gc-sections
--gdb-index Ignored
--hash-style [sysv,gnu,both]
Set hash style
--icf Fold identical code
--no-icf
--image-base ADDR Set the base address to a given value
--init SYMBOL Call SYMBOl at load-time
--no-undefined Report undefined symbols (even with --shared)
--perf Print performance statistics
--pie, --pic-executable Create a position independent executable
--no-pie, --no-pic-executable
--plugin Ignored
--plugin-opt Ignored
--pop-state Pop state of flags governing input file handling
--preload
--print-gc-sections Print removed unreferenced sections
--no-print-gc-sections
--print-icf-sections Print folded identical sections
--no-print-icf-sections
--push-state Pop state of flags governing input file handling
--quick-exit Use quick_exit to exit (default)
--no-quick-exit
--relax Optimize instructions (default)
--no-relax
--repro Embed input files to .repro section
--retain-symbols-file FILE Keep only symbols listed in FILE
--rpath DIR Add DIR to runtime search path
--rpath-link DIR Ignored
--run COMMAND ARG... Run COMMAND with mold as /usr/bin/ld
--shared, --Bshareable Create a share library
--sort-common Ignored
--sort-section Ignored
--spare-dynamic-tags NUMBER Reserve give number of tags in .dynamic section
--static Do not link against shared libraries
--stats Print input statistics
--sysroot DIR Set target system root directory
--thread-count COUNT Use COUNT number of threads
--threads Use multiple threads (default)
--no-threads
--trace Print name of each input file
--unique PATTERN Don't merge input sections that match a given pattern
--unresolved-symbols [report-all,ignore-all,ignore-in-object-files,ignore-in-shared-libs]
How to handle unresolved symbols
--version-script FILE Read version script
--warn-common Warn about common symbols
--no-warn-common
--warn-unresolved-symbols Report unresolved symbols as warnings
--error-unresolved-symbols
Report unresolved symbols as errors (default)
--whole-archive Include all objects from static archives
--no-whole-archive
--wrap SYMBOL Use wrapper function for a given symbol
-z defs Report undefined symbols (even with --shared)
-z nodefs
-z execstack Require executable stack
-z noexecstack
-z initfirst Mark DSO to be initialized first at runtime
-z interpose Mark object to interpose all DSOs but executable
-z keep-text-section-prefix Keep .text.{hot,unknown,unlikely,startup,exit} as separate sections in the final binary
-z nokeep-text-section-prefix
-z lazy Enable lazy function resolution (default)
-z nocopyreloc Do not create copy relocations
-z nodelete Mark DSO non-deletable at runtime
-z nodlopen Mark DSO not available to dlopen
-z nodump Mark DSO not available to dldump
-z now Disable lazy function resolution
-z origin Mark object requiring immediate $ORIGIN processing at runtime
-z relro Make some sections read-only after relocation (default)
-z norelro
-z text Report error if DT_TEXTREL is set
-z notext
-z textoff
mold: supported targets: elf32-i386 elf64-x86-64
mold: supported emulations: elf_i386 elf_x86_64)";
template <typename E>
static std::vector<std::string_view>
read_response_file(Context<E> &ctx, std::string_view path) {
std::vector<std::string_view> vec;
MemoryMappedFile<E> *mb =
MemoryMappedFile<E>::must_open(ctx, std::string(path));
u8 *data = mb->data(ctx);
auto read_quoted = [&](i64 i, char quote) {
std::string buf;
while (i < mb->size() && data[i] != quote) {
if (data[i] == '\\') {
buf.append(1, data[i + 1]);
i += 2;
} else {
buf.append(1, data[i++]);
}
}
if (i >= mb->size())
Fatal(ctx) << path << ": premature end of input";
vec.push_back(save_string(ctx, buf));
return i + 1;
};
auto read_unquoted = [&](i64 i) {
std::string buf;
while (i < mb->size() && !isspace(data[i]))
buf.append(1, data[i++]);
vec.push_back(save_string(ctx, buf));
return i;
};
for (i64 i = 0; i < mb->size();) {
if (isspace(data[i]))
i++;
else if (data[i] == '\'')
i = read_quoted(i + 1, '\'');
else if (data[i] == '\"')
i = read_quoted(i + 1, '\"');
else
i = read_unquoted(i);
}
return vec;
}
template <typename E>
std::vector<std::string_view>
expand_response_files(Context<E> &ctx, char **argv) {
std::vector<std::string_view> vec;
for (i64 i = 0; argv[i]; i++) {
if (argv[i][0] == '@')
append(vec, read_response_file(ctx, argv[i] + 1));
else
vec.push_back(argv[i]);
}
return vec;
}
static std::vector<std::string> add_dashes(std::string name) {
// Multi-letter linker options can be preceded by either a single
// dash or double dashes except ones starting with "o", which must
// be preceded by double dashes. For example, "-omagic" is
// interpreted as "-o magic". If you really want to specify the
// "omagic" option, you have to pass "--omagic".
if (name[0] == 'o')
return {"--" + name};
return {"-" + name, "--" + name};
}
template <typename E>
bool read_arg(Context<E> &ctx, std::span<std::string_view> &args,
std::string_view &arg, std::string name) {
if (name.size() == 1) {
if (args[0] == "-" + name) {
if (args.size() == 1)
Fatal(ctx) << "option -" << name << ": argument missing";
arg = args[1];
args = args.subspan(2);
return true;
}
if (args[0].starts_with("-" + name)) {
arg = args[0].substr(name.size() + 1);
args = args.subspan(1);
return true;
}
return false;
}
for (std::string opt : add_dashes(name)) {
if (args[0] == opt) {
if (args.size() == 1)
Fatal(ctx) << "option -" << name << ": argument missing";
arg = args[1];
args = args.subspan(2);
return true;
}
if (args[0].starts_with(opt + "=")) {
arg = args[0].substr(opt.size() + 1);
args = args.subspan(1);
return true;
}
}
return false;
}
bool read_flag(std::span<std::string_view> &args, std::string name) {
for (std::string opt : add_dashes(name)) {
if (args[0] == opt) {
args = args.subspan(1);
return true;
}
}
return false;
}
static bool read_z_flag(std::span<std::string_view> &args, std::string name) {
if (args.size() >= 2 && args[0] == "-z" && args[1] == name) {
args = args.subspan(2);
return true;
}
if (!args.empty() && args[0] == "-z" + name) {
args = args.subspan(1);
return true;
}
return false;
}
template <typename E>
std::string create_response_file(Context<E> &ctx) {
std::string buf;
std::stringstream out;
std::string cwd = get_current_dir();
out << "-C " << cwd.substr(1) << "\n";
if (cwd != "/") {
out << "--chroot ..";
i64 depth = std::count(cwd.begin(), cwd.end(), '/');
for (i64 i = 1; i < depth; i++)
out << "/..";
out << "\n";
}
for (i64 i = 1; i < ctx.cmdline_args.size(); i++) {
std::string_view arg = ctx.cmdline_args[i];
if (arg == "-repro" || arg == "--repro") {
i++;
continue;
}
out << arg << "\n";
}
return out.str();
}
template <typename E>
static i64 parse_hex(Context<E> &ctx, std::string opt, std::string_view value) {
if (!value.starts_with("0x") && !value.starts_with("0X"))
Fatal(ctx) << "option -" << opt << ": not a hexadecimal number";
value = value.substr(2);
if (value.find_first_not_of("0123456789abcdefABCDEF") != value.npos)
Fatal(ctx) << "option -" << opt << ": not a hexadecimal number";
return std::stol(std::string(value), nullptr, 16);
}
template <typename E>
static i64 parse_number(Context<E> &ctx, std::string opt,
std::string_view value) {
size_t nread;
i64 ret = std::stol(std::string(value), &nread, 0);
if (value.size() != nread)
Fatal(ctx) << "option -" << opt << ": not a number: " << value;
return ret;
}
template <typename E>
static std::vector<u8> parse_hex_build_id(Context<E> &ctx,
std::string_view arg) {
ASSERT(arg.starts_with("0x") || arg.starts_with("0X"));
if (arg.size() % 2)
Fatal(ctx) << "invalid build-id: " << arg;
if (arg.substr(2).find_first_not_of("0123456789abcdefABCDEF") != arg.npos)
Fatal(ctx) << "invalid build-id: " << arg;
arg = arg.substr(2);
auto fn = [](char c) {
if ('0' <= c && c <= '9')
return c - '0';
if ('a' <= c && c <= 'f')
return c - 'a' + 10;
ASSERT('A' <= c && c <= 'F');
return c - 'A' + 10;
};
std::vector<u8> vec(arg.size() / 2);
for (i64 i = 0; i < vec.size(); i++)
vec[i] = (fn(arg[i * 2]) << 4) | fn(arg[i * 2 + 1]);
return vec;
}
static std::vector<std::string_view>
split_by_comma_or_colon(std::string_view str) {
std::vector<std::string_view> vec;
for (;;) {
i64 pos = str.find_first_of(",:");
if (pos == str.npos) {
vec.push_back(str);
break;
}
vec.push_back(str.substr(0, pos));
str = str.substr(pos);
}
return vec;
}
static i64 get_default_thread_count() {
// mold doesn't scale above 32 threads.
int n = tbb::global_control::active_value(
tbb::global_control::max_allowed_parallelism);
return std::min(n, 32);
}
static std::string_view trim(std::string_view str) {
size_t pos = str.find_first_not_of(" \t");
if (pos == str.npos)
return "";
str = str.substr(pos);
pos = str.find_last_not_of(" \t");
if (pos == str.npos)
return str;
return str.substr(0, pos + 1);
}
template <typename E>
static void read_retain_symbols_file(Context<E> &ctx, std::string_view path) {
MemoryMappedFile<E> *mb =
MemoryMappedFile<E>::must_open(ctx, std::string(path));
std::string_view data((char *)mb->data(ctx), mb->size());
ctx.arg.retain_symbols_file.reset(new std::unordered_set<std::string_view>);
while (!data.empty()) {
size_t pos = data.find('\n');
std::string_view name;
if (pos == data.npos) {
name = data;
data = "";
} else {
name = data.substr(0, pos);
data = data.substr(pos + 1);
}
name = trim(name);
if (!name.empty())
ctx.arg.retain_symbols_file->insert(name);
}
}
static bool is_file(std::string_view path) {
struct stat st;
return stat(std::string(path).c_str(), &st) == 0 &&
(st.st_mode & S_IFMT) != S_IFDIR;
}
template <typename E>
void parse_nonpositional_args(Context<E> &ctx,
std::vector<std::string_view> &remaining) {
std::span<std::string_view> args = ctx.cmdline_args;
args = args.subspan(1);
ctx.arg.thread_count = get_default_thread_count();
bool version_shown = false;
while (!args.empty()) {
std::string_view arg;
if (read_flag(args, "help")) {
SyncOut(ctx) << "Usage: " << ctx.cmdline_args[0]
<< " [options] file...\n" << helpmsg;
exit(0);
}
if (read_arg(ctx, args, arg, "o") || read_arg(ctx, args, arg, "output")) {
ctx.arg.output = arg;
} else if (read_arg(ctx, args, arg, "dynamic-linker") ||
read_arg(ctx, args, arg, "I")) {
ctx.arg.dynamic_linker = arg;
} else if (read_arg(ctx, args, arg, "no-dynamic-linker")) {
ctx.arg.dynamic_linker = "";
} else if (read_flag(args, "v")) {
SyncOut(ctx) << get_version_string();
version_shown = true;
} else if (read_flag(args, "version")) {
SyncOut(ctx) << get_version_string();
exit(0);
} else if (read_flag(args, "V")) {
SyncOut(ctx) << get_version_string()
<< "\n Supported emulations:\n elf_x86_64\n elf_i386";
version_shown = true;
} else if (read_flag(args, "export-dynamic") || read_flag(args, "E")) {
ctx.arg.export_dynamic = true;
} else if (read_flag(args, "no-export-dynamic")) {
ctx.arg.export_dynamic = false;
} else if (read_flag(args, "Bsymbolic")) {
ctx.arg.Bsymbolic = true;
} else if (read_flag(args, "Bsymbolic-functions")) {
ctx.arg.Bsymbolic_functions = true;
} else if (read_arg(ctx, args, arg, "exclude-libs")) {
append(ctx.arg.exclude_libs, split_by_comma_or_colon(arg));
} else if (read_arg(ctx, args, arg, "e") ||
read_arg(ctx, args, arg, "entry")) {
ctx.arg.entry = arg;
} else if (read_arg(ctx, args, arg, "Map")) {
ctx.arg.Map = arg;
ctx.arg.print_map = true;
} else if (read_flag(args, "print-map") || read_flag(args, "M")) {
ctx.arg.print_map = true;
} else if (read_flag(args, "static") || read_flag(args, "Bstatic")) {
ctx.arg.is_static = true;
remaining.push_back("-Bstatic");
} else if (read_flag(args, "Bdynamic")) {
ctx.arg.is_static = false;
remaining.push_back("-Bdynamic");
} else if (read_flag(args, "shared") || read_flag(args, "Bshareable")) {
ctx.arg.shared = true;
} else if (read_arg(ctx, args, arg, "spare-dynamic-tags")) {
ctx.arg.spare_dynamic_tags = parse_number(ctx, "spare-dynamic-tags", arg);
} else if (read_flag(args, "demangle")) {
ctx.arg.demangle = true;
} else if (read_flag(args, "no-demangle")) {
ctx.arg.demangle = false;
} else if (read_arg(ctx, args, arg, "y") ||
read_arg(ctx, args, arg, "trace-symbol")) {
ctx.arg.trace_symbol.push_back(arg);
} else if (read_arg(ctx, args, arg, "filler")) {
ctx.arg.filler = parse_hex(ctx, "filler", arg);
} else if (read_arg(ctx, args, arg, "L") ||
read_arg(ctx, args, arg, "library-path")) {
ctx.arg.library_paths.push_back(std::string(arg));
} else if (read_arg(ctx, args, arg, "sysroot")) {
ctx.arg.sysroot = arg;
} else if (read_arg(ctx, args, arg, "unique")) {
ctx.arg.unique.reset(new std::regex(glob_to_regex(arg)));
} else if (read_arg(ctx, args, arg, "unresolved-symbols")) {
if (arg == "report-all" || arg == "ignore-in-shared-libs")
ctx.arg.unresolved_symbols = UnresolvedKind::ERROR;
else if (arg == "ignore-all" || arg == "ignore-in-object-files")
ctx.arg.unresolved_symbols = UnresolvedKind::IGNORE;
else
Fatal(ctx) << "unknown --unresolved-symbols argument: " << arg;
} else if (read_arg(ctx, args, arg, "u") ||
read_arg(ctx, args, arg, "undefined")) {
ctx.arg.undefined.push_back(arg);
} else if (read_arg(ctx, args, arg, "init")) {
ctx.arg.init = arg;
} else if (read_arg(ctx, args, arg, "fini")) {
ctx.arg.fini = arg;
} else if (read_arg(ctx, args, arg, "hash-style")) {
if (arg == "sysv") {
ctx.arg.hash_style_sysv = true;
ctx.arg.hash_style_gnu = false;
} else if (arg == "gnu") {
ctx.arg.hash_style_sysv = false;
ctx.arg.hash_style_gnu = true;
} else if (arg == "both") {
ctx.arg.hash_style_sysv = true;
ctx.arg.hash_style_gnu = true;
} else {
Fatal(ctx) << "invalid --hash-style argument: " << arg;
}
} else if (read_arg(ctx, args, arg, "soname") ||
read_arg(ctx, args, arg, "h")) {
ctx.arg.soname = arg;
} else if (read_flag(args, "allow-multiple-definition")) {
ctx.arg.allow_multiple_definition = true;
} else if (read_flag(args, "trace")) {
ctx.arg.trace = true;
} else if (read_flag(args, "eh-frame-hdr")) {
ctx.arg.eh_frame_hdr = true;
} else if (read_flag(args, "no-eh-frame-hdr")) {
ctx.arg.eh_frame_hdr = false;
} else if (read_flag(args, "pie") || read_flag(args, "pic-executable")) {
ctx.arg.pic = true;
ctx.arg.pie = true;
} else if (read_flag(args, "no-pie") ||
read_flag(args, "no-pic-executable")) {
ctx.arg.pic = false;
ctx.arg.pie = false;
} else if (read_flag(args, "relax")) {
ctx.arg.relax = true;
} else if (read_flag(args, "no-relax")) {
ctx.arg.relax = false;
} else if (read_flag(args, "r") || read_flag(args, "relocatable")) {
ctx.arg.relocatable = true;
} else if (read_flag(args, "perf")) {
ctx.arg.perf = true;
} else if (read_flag(args, "stats")) {
ctx.arg.stats = true;
Counter::enabled = true;
} else if (read_arg(ctx, args, arg, "C") ||
read_arg(ctx, args, arg, "directory")) {
ctx.arg.directory = arg;
} else if (read_arg(ctx, args, arg, "chroot")) {
ctx.arg.chroot = arg;
} else if (read_flag(args, "warn-common")) {
ctx.arg.warn_common = true;
} else if (read_flag(args, "no-warn-common")) {
ctx.arg.warn_common = false;
} else if (read_arg(ctx, args, arg, "compress-debug-sections")) {
if (arg == "zlib" || arg == "zlib-gabi")
ctx.arg.compress_debug_sections = COMPRESS_GABI;
else if (arg == "zlib-gnu")
ctx.arg.compress_debug_sections = COMPRESS_GNU;
else if (arg == "none")
ctx.arg.compress_debug_sections = COMPRESS_NONE;
else
Fatal(ctx) << "invalid --compress-debug-sections argument: " << arg;
} else if (read_arg(ctx, args, arg, "wrap")) {
ctx.arg.wrap.insert(arg);
} else if (read_flag(args, "omagic") || read_flag(args, "N")) {
ctx.arg.omagic = true;
ctx.arg.is_static = true;
} else if (read_flag(args, "no-omagic")) {
ctx.arg.omagic = false;
} else if (read_arg(ctx, args, arg, "retain-symbols-file")) {
read_retain_symbols_file(ctx, arg);
} else if (read_flag(args, "repro")) {
ctx.arg.repro = true;
} else if (read_z_flag(args, "now")) {
ctx.arg.z_now = true;
} else if (read_z_flag(args, "lazy")) {
ctx.arg.z_now = false;
} else if (read_z_flag(args, "execstack")) {
ctx.arg.z_execstack = true;
} else if (read_z_flag(args, "noexecstack")) {
ctx.arg.z_execstack = false;
} else if (read_z_flag(args, "relro")) {
ctx.arg.z_relro = true;
} else if (read_z_flag(args, "norelro")) {
ctx.arg.z_relro = false;
} else if (read_z_flag(args, "defs")) {
ctx.arg.z_defs = true;
} else if (read_z_flag(args, "nodefs")) {
ctx.arg.z_defs = false;
} else if (read_z_flag(args, "nodlopen")) {
ctx.arg.z_dlopen = false;
} else if (read_z_flag(args, "nodelete")) {
ctx.arg.z_delete = false;
} else if (read_z_flag(args, "nocopyreloc")) {
ctx.arg.z_copyreloc = false;
} else if (read_z_flag(args, "nodump")) {
ctx.arg.z_dump = false;
} else if (read_z_flag(args, "initfirst")) {
ctx.arg.z_initfirst = true;
} else if (read_z_flag(args, "interpose")) {
ctx.arg.z_interpose = true;
} else if (read_z_flag(args, "muldefs")) {
ctx.arg.allow_multiple_definition = true;
} else if (read_z_flag(args, "keep-text-section-prefix")) {
ctx.arg.z_keep_text_section_prefix = true;
} else if (read_z_flag(args, "nokeep-text-section-prefix")) {
ctx.arg.z_keep_text_section_prefix = false;
} else if (read_z_flag(args, "text")) {
ctx.arg.z_text = true;
} else if (read_z_flag(args, "notext") || read_z_flag(args, "textoff")) {
ctx.arg.z_text = false;
} else if (read_z_flag(args, "origin")) {
ctx.arg.z_origin = true;
} else if (read_flag(args, "no-undefined")) {
ctx.arg.z_defs = true;
} else if (read_flag(args, "fatal-warnings")) {
ctx.arg.fatal_warnings = true;
} else if (read_flag(args, "no-fatal-warnings")) {
ctx.arg.fatal_warnings = false;
} else if (read_flag(args, "fork")) {
ctx.arg.fork = true;
} else if (read_flag(args, "no-fork")) {
ctx.arg.fork = false;
} else if (read_flag(args, "gc-sections")) {
ctx.arg.gc_sections = true;
} else if (read_flag(args, "no-gc-sections")) {
ctx.arg.gc_sections = false;
} else if (read_flag(args, "print-gc-sections")) {
ctx.arg.print_gc_sections = true;
} else if (read_flag(args, "no-print-gc-sections")) {
ctx.arg.print_gc_sections = false;
} else if (read_arg(ctx, args, arg, "icf")) {
if (arg == "all")
ctx.arg.icf = true;
else if (arg == "none")
ctx.arg.icf = false;
else
Fatal(ctx) << "unknown --icf argument: " << arg;
} else if (read_flag(args, "no-icf")) {
ctx.arg.icf = false;
} else if (read_arg(ctx, args, arg, "image-base")) {
ctx.arg.image_base = parse_number(ctx, "image-base", arg);
} else if (read_flag(args, "quick-exit")) {
ctx.arg.quick_exit = true;
} else if (read_flag(args, "no-quick-exit")) {
ctx.arg.quick_exit = false;
} else if (read_flag(args, "print-icf-sections")) {
ctx.arg.print_icf_sections = true;
} else if (read_flag(args, "no-print-icf-sections")) {
ctx.arg.print_icf_sections = false;
} else if (read_flag(args, "quick-exit")) {
ctx.arg.quick_exit = true;
} else if (read_flag(args, "no-quick-exit")) {
ctx.arg.quick_exit = false;
} else if (read_arg(ctx, args, arg, "thread-count")) {
ctx.arg.thread_count = parse_number(ctx, "thread-count", arg);
} else if (read_flag(args, "threads")) {
ctx.arg.thread_count = get_default_thread_count();
} else if (read_flag(args, "no-threads")) {
ctx.arg.thread_count = 1;
} else if (read_flag(args, "discard-all") || read_flag(args, "x")) {
ctx.arg.discard_all = true;
} else if (read_flag(args, "discard-locals") || read_flag(args, "X")) {
ctx.arg.discard_locals = true;
} else if (read_flag(args, "strip-all") || read_flag(args, "s")) {
ctx.arg.strip_all = true;
} else if (read_flag(args, "strip-debug") || read_flag(args, "S")) {
ctx.arg.strip_all = true;
} else if (read_flag(args, "warn-unresolved-symbols")) {
ctx.arg.unresolved_symbols = UnresolvedKind::WARN;
} else if (read_flag(args, "error-unresolved-symbols")) {
ctx.arg.unresolved_symbols = UnresolvedKind::ERROR;
} else if (read_arg(ctx, args, arg, "rpath")) {
if (!ctx.arg.rpaths.empty())
ctx.arg.rpaths += ":";
ctx.arg.rpaths += arg;
} else if (read_arg(ctx, args, arg, "R")) {
if (is_file(arg))
Fatal(ctx) << "-R" << arg
<< ": -R as an alias for --just-symbols is not supported";
if (!ctx.arg.rpaths.empty())
ctx.arg.rpaths += ":";
ctx.arg.rpaths += arg;
} else if (read_flag(args, "build-id")) {
ctx.arg.build_id.kind = BuildId::HASH;
ctx.arg.build_id.hash_size = 20;
} else if (read_arg(ctx, args, arg, "build-id")) {
if (arg == "none") {
ctx.arg.build_id.kind = BuildId::NONE;
} else if (arg == "uuid") {
ctx.arg.build_id.kind = BuildId::UUID;
} else if (arg == "md5") {
ctx.arg.build_id.kind = BuildId::HASH;
ctx.arg.build_id.hash_size = 16;
} else if (arg == "sha1") {
ctx.arg.build_id.kind = BuildId::HASH;
ctx.arg.build_id.hash_size = 20;
} else if (arg == "sha256") {
ctx.arg.build_id.kind = BuildId::HASH;
ctx.arg.build_id.hash_size = 32;
} else if (arg.starts_with("0x") || arg.starts_with("0X")) {
ctx.arg.build_id.kind = BuildId::HEX;
ctx.arg.build_id.value = parse_hex_build_id(ctx, arg);
} else {
Fatal(ctx) << "invalid --build-id argument: " << arg;
}
} else if (read_flag(args, "no-build-id")) {
ctx.arg.build_id.kind = BuildId::NONE;
} else if (read_arg(ctx, args, arg, "auxiliary") ||
read_arg(ctx, args, arg, "f")) {
ctx.arg.auxiliary.push_back(arg);
} else if (read_arg(ctx, args, arg, "filter") ||
read_arg(ctx, args, arg, "F")) {
ctx.arg.filter.push_back(arg);
} else if (read_flag(args, "preload")) {
ctx.arg.preload = true;
} else if (read_arg(ctx, args, arg, "O")) {
} else if (read_flag(args, "O0")) {
} else if (read_flag(args, "O1")) {
} else if (read_flag(args, "O2")) {
} else if (read_flag(args, "verbose")) {
} else if (read_arg(ctx, args, arg, "plugin")) {
} else if (read_arg(ctx, args, arg, "plugin-opt")) {
} else if (read_flag(args, "color-diagnostics")) {
} else if (read_flag(args, "gdb-index")) {
} else if (read_arg(ctx, args, arg, "m")) {
} else if (read_flag(args, "eh-frame-hdr")) {
} else if (read_flag(args, "start-group")) {
} else if (read_flag(args, "end-group")) {
} else if (read_flag(args, "(")) {
} else if (read_flag(args, ")")) {
} else if (read_flag(args, "fatal-warnings")) {
} else if (read_flag(args, "enable-new-dtags")) {
} else if (read_flag(args, "disable-new-dtags")) {
} else if (read_flag(args, "nostdlib")) {
} else if (read_flag(args, "allow-shlib-undefined")) {
} else if (read_flag(args, "no-allow-shlib-undefined")) {
} else if (read_flag(args, "no-add-needed")) {
} else if (read_flag(args, "no-copy-dt-needed-entries")) {
} else if (read_flag(args, "no-undefined-version")) {
} else if (read_arg(ctx, args, arg, "sort-section")) {
} else if (read_flag(args, "sort-common")) {
} else if (read_flag(args, "fix-cortex-a53-843419")) {
} else if (read_flag(args, "EL")) {
} else if (read_flag(args, "warn-once")) {
} else if (read_flag(args, "nodefaultlibs")) {
} else if (read_flag(args, "warn-constructors")) {
} else if (read_arg(ctx, args, arg, "rpath-link")) {
} else if (read_z_flag(args, "combreloc")) {
} else if (read_z_flag(args, "nocombreloc")) {
} else if (read_arg(ctx, args, arg, "version-script")) {
remaining.push_back("--version-script");
remaining.push_back(arg);
} else if (read_arg(ctx, args, arg, "dynamic-list")) {
remaining.push_back("--dynamic-list");
remaining.push_back(arg);
} else if (read_flag(args, "as-needed")) {
remaining.push_back("-as-needed");
} else if (read_flag(args, "no-as-needed")) {
remaining.push_back("-no-as-needed");
} else if (read_flag(args, "whole-archive")) {
remaining.push_back("-whole-archive");
} else if (read_flag(args, "no-whole-archive")) {
remaining.push_back("-no-whole-archive");
} else if (read_arg(ctx, args, arg, "l")) {
remaining.push_back("-l");
remaining.push_back(arg);
} else if (read_arg(ctx, args, arg, "script") ||
read_arg(ctx, args, arg, "T")) {
remaining.push_back(arg);
} else if (read_flag(args, "push-state")) {
remaining.push_back("-push-state");
} else if (read_flag(args, "pop-state")) {
remaining.push_back("-pop-state");
} else {
if (args[0] == "-z" && args.size() >= 2)
Fatal(ctx) << "mold: unknown command line option: -z " << args[1];
if (args[0][0] == '-')
Fatal(ctx) << "mold: unknown command line option: " << args[0];
remaining.push_back(args[0]);
args = args.subspan(1);
}
}
if (!ctx.arg.sysroot.empty()) {
for (std::string &path : ctx.arg.library_paths) {
if (std::string_view(path).starts_with('='))
path = std::string(ctx.arg.sysroot) + path.substr(1);
else if (std::string_view(path).starts_with("$SYSROOT"))
path = std::string(ctx.arg.sysroot) + path.substr(8);
}
}
if (ctx.arg.shared) {
ctx.arg.pic = true;
ctx.arg.dynamic_linker = "";
}
if (ctx.arg.pic)
ctx.arg.image_base = 0;
if (ctx.arg.retain_symbols_file) {
ctx.arg.strip_all = false;
ctx.arg.discard_all = false;
}
if (ctx.arg.relocatable)
ctx.arg.is_static = true;
if (!ctx.arg.shared) {
if (!ctx.arg.filter.empty())
Fatal(ctx) << "-filter may not be used without -shared";
if (!ctx.arg.auxiliary.empty())
Fatal(ctx) << "-auxiliary may not be used without -shared";
}
if (char *env = getenv("MOLD_REPRO"); env && env[0])
ctx.arg.repro = true;
if (ctx.arg.output.empty())
ctx.arg.output = "a.out";
if (version_shown && remaining.empty())
exit(0);
}
#define INSTANTIATE(E) \
template \
std::vector<std::string_view> \
expand_response_files(Context<E> &ctx, char **argv); \
\
template \
bool read_arg(Context<E> &ctx, std::span<std::string_view> &args, \
std::string_view &arg, \
std::string name); \
\
template std::string create_response_file(Context<E> &ctx); \
\
template \
void parse_nonpositional_args(Context<E> &ctx, \
std::vector<std::string_view> &remaining)
INSTANTIATE(X86_64);
INSTANTIATE(I386);
INSTANTIATE(AARCH64);