llvm-project

Форк
0
/
Driver.cpp 
2029 строк · 75.1 Кб
1
//===- Driver.cpp ---------------------------------------------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8

9
#include "Driver.h"
10
#include "Config.h"
11
#include "ICF.h"
12
#include "InputFiles.h"
13
#include "LTO.h"
14
#include "MarkLive.h"
15
#include "ObjC.h"
16
#include "OutputSection.h"
17
#include "OutputSegment.h"
18
#include "SectionPriorities.h"
19
#include "SymbolTable.h"
20
#include "Symbols.h"
21
#include "SyntheticSections.h"
22
#include "Target.h"
23
#include "UnwindInfoSection.h"
24
#include "Writer.h"
25

26
#include "lld/Common/Args.h"
27
#include "lld/Common/CommonLinkerContext.h"
28
#include "lld/Common/Driver.h"
29
#include "lld/Common/ErrorHandler.h"
30
#include "lld/Common/LLVM.h"
31
#include "lld/Common/Memory.h"
32
#include "lld/Common/Reproduce.h"
33
#include "lld/Common/Version.h"
34
#include "llvm/ADT/DenseSet.h"
35
#include "llvm/ADT/StringExtras.h"
36
#include "llvm/ADT/StringRef.h"
37
#include "llvm/BinaryFormat/MachO.h"
38
#include "llvm/BinaryFormat/Magic.h"
39
#include "llvm/Config/llvm-config.h"
40
#include "llvm/LTO/LTO.h"
41
#include "llvm/Object/Archive.h"
42
#include "llvm/Option/ArgList.h"
43
#include "llvm/Support/CommandLine.h"
44
#include "llvm/Support/FileSystem.h"
45
#include "llvm/Support/MemoryBuffer.h"
46
#include "llvm/Support/Parallel.h"
47
#include "llvm/Support/Path.h"
48
#include "llvm/Support/TarWriter.h"
49
#include "llvm/Support/TargetSelect.h"
50
#include "llvm/Support/TimeProfiler.h"
51
#include "llvm/TargetParser/Host.h"
52
#include "llvm/TextAPI/PackedVersion.h"
53

54
#include <algorithm>
55

56
using namespace llvm;
57
using namespace llvm::MachO;
58
using namespace llvm::object;
59
using namespace llvm::opt;
60
using namespace llvm::sys;
61
using namespace lld;
62
using namespace lld::macho;
63

64
std::unique_ptr<Configuration> macho::config;
65
std::unique_ptr<DependencyTracker> macho::depTracker;
66

67
static HeaderFileType getOutputType(const InputArgList &args) {
68
  // TODO: -r, -dylinker, -preload...
69
  Arg *outputArg = args.getLastArg(OPT_bundle, OPT_dylib, OPT_execute);
70
  if (outputArg == nullptr)
71
    return MH_EXECUTE;
72

73
  switch (outputArg->getOption().getID()) {
74
  case OPT_bundle:
75
    return MH_BUNDLE;
76
  case OPT_dylib:
77
    return MH_DYLIB;
78
  case OPT_execute:
79
    return MH_EXECUTE;
80
  default:
81
    llvm_unreachable("internal error");
82
  }
83
}
84

85
static DenseMap<CachedHashStringRef, StringRef> resolvedLibraries;
86
static std::optional<StringRef> findLibrary(StringRef name) {
87
  CachedHashStringRef key(name);
88
  auto entry = resolvedLibraries.find(key);
89
  if (entry != resolvedLibraries.end())
90
    return entry->second;
91

92
  auto doFind = [&] {
93
    // Special case for Csu support files required for Mac OS X 10.7 and older
94
    // (crt1.o)
95
    if (name.ends_with(".o"))
96
      return findPathCombination(name, config->librarySearchPaths, {""});
97
    if (config->searchDylibsFirst) {
98
      if (std::optional<StringRef> path =
99
              findPathCombination("lib" + name, config->librarySearchPaths,
100
                                  {".tbd", ".dylib", ".so"}))
101
        return path;
102
      return findPathCombination("lib" + name, config->librarySearchPaths,
103
                                 {".a"});
104
    }
105
    return findPathCombination("lib" + name, config->librarySearchPaths,
106
                               {".tbd", ".dylib", ".so", ".a"});
107
  };
108

109
  std::optional<StringRef> path = doFind();
110
  if (path)
111
    resolvedLibraries[key] = *path;
112

113
  return path;
114
}
115

116
static DenseMap<CachedHashStringRef, StringRef> resolvedFrameworks;
117
static std::optional<StringRef> findFramework(StringRef name) {
118
  CachedHashStringRef key(name);
119
  auto entry = resolvedFrameworks.find(key);
120
  if (entry != resolvedFrameworks.end())
121
    return entry->second;
122

123
  SmallString<260> symlink;
124
  StringRef suffix;
125
  std::tie(name, suffix) = name.split(",");
126
  for (StringRef dir : config->frameworkSearchPaths) {
127
    symlink = dir;
128
    path::append(symlink, name + ".framework", name);
129

130
    if (!suffix.empty()) {
131
      // NOTE: we must resolve the symlink before trying the suffixes, because
132
      // there are no symlinks for the suffixed paths.
133
      SmallString<260> location;
134
      if (!fs::real_path(symlink, location)) {
135
        // only append suffix if realpath() succeeds
136
        Twine suffixed = location + suffix;
137
        if (fs::exists(suffixed))
138
          return resolvedFrameworks[key] = saver().save(suffixed.str());
139
      }
140
      // Suffix lookup failed, fall through to the no-suffix case.
141
    }
142

143
    if (std::optional<StringRef> path = resolveDylibPath(symlink.str()))
144
      return resolvedFrameworks[key] = *path;
145
  }
146
  return {};
147
}
148

149
static bool warnIfNotDirectory(StringRef option, StringRef path) {
150
  if (!fs::exists(path)) {
151
    warn("directory not found for option -" + option + path);
152
    return false;
153
  } else if (!fs::is_directory(path)) {
154
    warn("option -" + option + path + " references a non-directory path");
155
    return false;
156
  }
157
  return true;
158
}
159

160
static std::vector<StringRef>
161
getSearchPaths(unsigned optionCode, InputArgList &args,
162
               const std::vector<StringRef> &roots,
163
               const SmallVector<StringRef, 2> &systemPaths) {
164
  std::vector<StringRef> paths;
165
  StringRef optionLetter{optionCode == OPT_F ? "F" : "L"};
166
  for (StringRef path : args::getStrings(args, optionCode)) {
167
    // NOTE: only absolute paths are re-rooted to syslibroot(s)
168
    bool found = false;
169
    if (path::is_absolute(path, path::Style::posix)) {
170
      for (StringRef root : roots) {
171
        SmallString<261> buffer(root);
172
        path::append(buffer, path);
173
        // Do not warn about paths that are computed via the syslib roots
174
        if (fs::is_directory(buffer)) {
175
          paths.push_back(saver().save(buffer.str()));
176
          found = true;
177
        }
178
      }
179
    }
180
    if (!found && warnIfNotDirectory(optionLetter, path))
181
      paths.push_back(path);
182
  }
183

184
  // `-Z` suppresses the standard "system" search paths.
185
  if (args.hasArg(OPT_Z))
186
    return paths;
187

188
  for (const StringRef &path : systemPaths) {
189
    for (const StringRef &root : roots) {
190
      SmallString<261> buffer(root);
191
      path::append(buffer, path);
192
      if (fs::is_directory(buffer))
193
        paths.push_back(saver().save(buffer.str()));
194
    }
195
  }
196
  return paths;
197
}
198

199
static std::vector<StringRef> getSystemLibraryRoots(InputArgList &args) {
200
  std::vector<StringRef> roots;
201
  for (const Arg *arg : args.filtered(OPT_syslibroot))
202
    roots.push_back(arg->getValue());
203
  // NOTE: the final `-syslibroot` being `/` will ignore all roots
204
  if (!roots.empty() && roots.back() == "/")
205
    roots.clear();
206
  // NOTE: roots can never be empty - add an empty root to simplify the library
207
  // and framework search path computation.
208
  if (roots.empty())
209
    roots.emplace_back("");
210
  return roots;
211
}
212

213
static std::vector<StringRef>
214
getLibrarySearchPaths(InputArgList &args, const std::vector<StringRef> &roots) {
215
  return getSearchPaths(OPT_L, args, roots, {"/usr/lib", "/usr/local/lib"});
216
}
217

218
static std::vector<StringRef>
219
getFrameworkSearchPaths(InputArgList &args,
220
                        const std::vector<StringRef> &roots) {
221
  return getSearchPaths(OPT_F, args, roots,
222
                        {"/Library/Frameworks", "/System/Library/Frameworks"});
223
}
224

225
static llvm::CachePruningPolicy getLTOCachePolicy(InputArgList &args) {
226
  SmallString<128> ltoPolicy;
227
  auto add = [&ltoPolicy](Twine val) {
228
    if (!ltoPolicy.empty())
229
      ltoPolicy += ":";
230
    val.toVector(ltoPolicy);
231
  };
232
  for (const Arg *arg :
233
       args.filtered(OPT_thinlto_cache_policy_eq, OPT_prune_interval_lto,
234
                     OPT_prune_after_lto, OPT_max_relative_cache_size_lto)) {
235
    switch (arg->getOption().getID()) {
236
    case OPT_thinlto_cache_policy_eq:
237
      add(arg->getValue());
238
      break;
239
    case OPT_prune_interval_lto:
240
      if (!strcmp("-1", arg->getValue()))
241
        add("prune_interval=87600h"); // 10 years
242
      else
243
        add(Twine("prune_interval=") + arg->getValue() + "s");
244
      break;
245
    case OPT_prune_after_lto:
246
      add(Twine("prune_after=") + arg->getValue() + "s");
247
      break;
248
    case OPT_max_relative_cache_size_lto:
249
      add(Twine("cache_size=") + arg->getValue() + "%");
250
      break;
251
    }
252
  }
253
  return CHECK(parseCachePruningPolicy(ltoPolicy), "invalid LTO cache policy");
254
}
255

256
// What caused a given library to be loaded. Only relevant for archives.
257
// Note that this does not tell us *how* we should load the library, i.e.
258
// whether we should do it lazily or eagerly (AKA force loading). The "how" is
259
// decided within addFile().
260
enum class LoadType {
261
  CommandLine,      // Library was passed as a regular CLI argument
262
  CommandLineForce, // Library was passed via `-force_load`
263
  LCLinkerOption,   // Library was passed via LC_LINKER_OPTIONS
264
};
265

266
struct ArchiveFileInfo {
267
  ArchiveFile *file;
268
  bool isCommandLineLoad;
269
};
270

271
static DenseMap<StringRef, ArchiveFileInfo> loadedArchives;
272

273
static InputFile *addFile(StringRef path, LoadType loadType,
274
                          bool isLazy = false, bool isExplicit = true,
275
                          bool isBundleLoader = false,
276
                          bool isForceHidden = false) {
277
  std::optional<MemoryBufferRef> buffer = readFile(path);
278
  if (!buffer)
279
    return nullptr;
280
  MemoryBufferRef mbref = *buffer;
281
  InputFile *newFile = nullptr;
282

283
  file_magic magic = identify_magic(mbref.getBuffer());
284
  switch (magic) {
285
  case file_magic::archive: {
286
    bool isCommandLineLoad = loadType != LoadType::LCLinkerOption;
287
    // Avoid loading archives twice. If the archives are being force-loaded,
288
    // loading them twice would create duplicate symbol errors. In the
289
    // non-force-loading case, this is just a minor performance optimization.
290
    // We don't take a reference to cachedFile here because the
291
    // loadArchiveMember() call below may recursively call addFile() and
292
    // invalidate this reference.
293
    auto entry = loadedArchives.find(path);
294

295
    ArchiveFile *file;
296
    if (entry == loadedArchives.end()) {
297
      // No cached archive, we need to create a new one
298
      std::unique_ptr<object::Archive> archive = CHECK(
299
          object::Archive::create(mbref), path + ": failed to parse archive");
300

301
      if (!archive->isEmpty() && !archive->hasSymbolTable())
302
        error(path + ": archive has no index; run ranlib to add one");
303
      file = make<ArchiveFile>(std::move(archive), isForceHidden);
304
    } else {
305
      file = entry->second.file;
306
      // Command-line loads take precedence. If file is previously loaded via
307
      // command line, or is loaded via LC_LINKER_OPTION and being loaded via
308
      // LC_LINKER_OPTION again, using the cached archive is enough.
309
      if (entry->second.isCommandLineLoad || !isCommandLineLoad)
310
        return file;
311
    }
312

313
    bool isLCLinkerForceLoad = loadType == LoadType::LCLinkerOption &&
314
                               config->forceLoadSwift &&
315
                               path::filename(path).starts_with("libswift");
316
    if ((isCommandLineLoad && config->allLoad) ||
317
        loadType == LoadType::CommandLineForce || isLCLinkerForceLoad) {
318
      if (readFile(path)) {
319
        Error e = Error::success();
320
        for (const object::Archive::Child &c : file->getArchive().children(e)) {
321
          StringRef reason;
322
          switch (loadType) {
323
            case LoadType::LCLinkerOption:
324
              reason = "LC_LINKER_OPTION";
325
              break;
326
            case LoadType::CommandLineForce:
327
              reason = "-force_load";
328
              break;
329
            case LoadType::CommandLine:
330
              reason = "-all_load";
331
              break;
332
          }
333
          if (Error e = file->fetch(c, reason))
334
            error(toString(file) + ": " + reason +
335
                  " failed to load archive member: " + toString(std::move(e)));
336
        }
337
        if (e)
338
          error(toString(file) +
339
                ": Archive::children failed: " + toString(std::move(e)));
340
      }
341
    } else if (isCommandLineLoad && config->forceLoadObjC) {
342
      for (const object::Archive::Symbol &sym : file->getArchive().symbols())
343
        if (sym.getName().starts_with(objc::symbol_names::klass))
344
          file->fetch(sym);
345

346
      // TODO: no need to look for ObjC sections for a given archive member if
347
      // we already found that it contains an ObjC symbol.
348
      if (readFile(path)) {
349
        Error e = Error::success();
350
        for (const object::Archive::Child &c : file->getArchive().children(e)) {
351
          Expected<MemoryBufferRef> mb = c.getMemoryBufferRef();
352
          if (!mb || !hasObjCSection(*mb))
353
            continue;
354
          if (Error e = file->fetch(c, "-ObjC"))
355
            error(toString(file) + ": -ObjC failed to load archive member: " +
356
                  toString(std::move(e)));
357
        }
358
        if (e)
359
          error(toString(file) +
360
                ": Archive::children failed: " + toString(std::move(e)));
361
      }
362
    }
363

364
    file->addLazySymbols();
365
    loadedArchives[path] = ArchiveFileInfo{file, isCommandLineLoad};
366
    newFile = file;
367
    break;
368
  }
369
  case file_magic::macho_object:
370
    newFile = make<ObjFile>(mbref, getModTime(path), "", isLazy);
371
    break;
372
  case file_magic::macho_dynamically_linked_shared_lib:
373
  case file_magic::macho_dynamically_linked_shared_lib_stub:
374
  case file_magic::tapi_file:
375
    if (DylibFile *dylibFile =
376
            loadDylib(mbref, nullptr, /*isBundleLoader=*/false, isExplicit))
377
      newFile = dylibFile;
378
    break;
379
  case file_magic::bitcode:
380
    newFile = make<BitcodeFile>(mbref, "", 0, isLazy);
381
    break;
382
  case file_magic::macho_executable:
383
  case file_magic::macho_bundle:
384
    // We only allow executable and bundle type here if it is used
385
    // as a bundle loader.
386
    if (!isBundleLoader)
387
      error(path + ": unhandled file type");
388
    if (DylibFile *dylibFile = loadDylib(mbref, nullptr, isBundleLoader))
389
      newFile = dylibFile;
390
    break;
391
  default:
392
    error(path + ": unhandled file type");
393
  }
394
  if (newFile && !isa<DylibFile>(newFile)) {
395
    if ((isa<ObjFile>(newFile) || isa<BitcodeFile>(newFile)) && newFile->lazy &&
396
        config->forceLoadObjC) {
397
      for (Symbol *sym : newFile->symbols)
398
        if (sym && sym->getName().starts_with(objc::symbol_names::klass)) {
399
          extract(*newFile, "-ObjC");
400
          break;
401
        }
402
      if (newFile->lazy && hasObjCSection(mbref))
403
        extract(*newFile, "-ObjC");
404
    }
405

406
    // printArchiveMemberLoad() prints both .a and .o names, so no need to
407
    // print the .a name here. Similarly skip lazy files.
408
    if (config->printEachFile && magic != file_magic::archive && !isLazy)
409
      message(toString(newFile));
410
    inputFiles.insert(newFile);
411
  }
412
  return newFile;
413
}
414

415
static std::vector<StringRef> missingAutolinkWarnings;
416
static void addLibrary(StringRef name, bool isNeeded, bool isWeak,
417
                       bool isReexport, bool isHidden, bool isExplicit,
418
                       LoadType loadType) {
419
  if (std::optional<StringRef> path = findLibrary(name)) {
420
    if (auto *dylibFile = dyn_cast_or_null<DylibFile>(
421
            addFile(*path, loadType, /*isLazy=*/false, isExplicit,
422
                    /*isBundleLoader=*/false, isHidden))) {
423
      if (isNeeded)
424
        dylibFile->forceNeeded = true;
425
      if (isWeak)
426
        dylibFile->forceWeakImport = true;
427
      if (isReexport) {
428
        config->hasReexports = true;
429
        dylibFile->reexport = true;
430
      }
431
    }
432
    return;
433
  }
434
  if (loadType == LoadType::LCLinkerOption) {
435
    missingAutolinkWarnings.push_back(
436
        saver().save("auto-linked library not found for -l" + name));
437
    return;
438
  }
439
  error("library not found for -l" + name);
440
}
441

442
static DenseSet<StringRef> loadedObjectFrameworks;
443
static void addFramework(StringRef name, bool isNeeded, bool isWeak,
444
                         bool isReexport, bool isExplicit, LoadType loadType) {
445
  if (std::optional<StringRef> path = findFramework(name)) {
446
    if (loadedObjectFrameworks.contains(*path))
447
      return;
448

449
    InputFile *file =
450
        addFile(*path, loadType, /*isLazy=*/false, isExplicit, false);
451
    if (auto *dylibFile = dyn_cast_or_null<DylibFile>(file)) {
452
      if (isNeeded)
453
        dylibFile->forceNeeded = true;
454
      if (isWeak)
455
        dylibFile->forceWeakImport = true;
456
      if (isReexport) {
457
        config->hasReexports = true;
458
        dylibFile->reexport = true;
459
      }
460
    } else if (isa_and_nonnull<ObjFile>(file) ||
461
               isa_and_nonnull<BitcodeFile>(file)) {
462
      // Cache frameworks containing object or bitcode files to avoid duplicate
463
      // symbols. Frameworks containing static archives are cached separately
464
      // in addFile() to share caching with libraries, and frameworks
465
      // containing dylibs should allow overwriting of attributes such as
466
      // forceNeeded by subsequent loads
467
      loadedObjectFrameworks.insert(*path);
468
    }
469
    return;
470
  }
471
  if (loadType == LoadType::LCLinkerOption) {
472
    missingAutolinkWarnings.push_back(
473
        saver().save("auto-linked framework not found for -framework " + name));
474
    return;
475
  }
476
  error("framework not found for -framework " + name);
477
}
478

479
// Parses LC_LINKER_OPTION contents, which can add additional command line
480
// flags. This directly parses the flags instead of using the standard argument
481
// parser to improve performance.
482
void macho::parseLCLinkerOption(
483
    llvm::SmallVectorImpl<StringRef> &LCLinkerOptions, InputFile *f,
484
    unsigned argc, StringRef data) {
485
  if (config->ignoreAutoLink)
486
    return;
487

488
  SmallVector<StringRef, 4> argv;
489
  size_t offset = 0;
490
  for (unsigned i = 0; i < argc && offset < data.size(); ++i) {
491
    argv.push_back(data.data() + offset);
492
    offset += strlen(data.data() + offset) + 1;
493
  }
494
  if (argv.size() != argc || offset > data.size())
495
    fatal(toString(f) + ": invalid LC_LINKER_OPTION");
496

497
  unsigned i = 0;
498
  StringRef arg = argv[i];
499
  if (arg.consume_front("-l")) {
500
    if (config->ignoreAutoLinkOptions.contains(arg))
501
      return;
502
  } else if (arg == "-framework") {
503
    StringRef name = argv[++i];
504
    if (config->ignoreAutoLinkOptions.contains(name))
505
      return;
506
  } else {
507
    error(arg + " is not allowed in LC_LINKER_OPTION");
508
  }
509

510
  LCLinkerOptions.append(argv);
511
}
512

513
void macho::resolveLCLinkerOptions() {
514
  while (!unprocessedLCLinkerOptions.empty()) {
515
    SmallVector<StringRef> LCLinkerOptions(unprocessedLCLinkerOptions);
516
    unprocessedLCLinkerOptions.clear();
517

518
    for (unsigned i = 0; i < LCLinkerOptions.size(); ++i) {
519
      StringRef arg = LCLinkerOptions[i];
520
      if (arg.consume_front("-l")) {
521
        assert(!config->ignoreAutoLinkOptions.contains(arg));
522
        addLibrary(arg, /*isNeeded=*/false, /*isWeak=*/false,
523
                   /*isReexport=*/false, /*isHidden=*/false,
524
                   /*isExplicit=*/false, LoadType::LCLinkerOption);
525
      } else if (arg == "-framework") {
526
        StringRef name = LCLinkerOptions[++i];
527
        assert(!config->ignoreAutoLinkOptions.contains(name));
528
        addFramework(name, /*isNeeded=*/false, /*isWeak=*/false,
529
                     /*isReexport=*/false, /*isExplicit=*/false,
530
                     LoadType::LCLinkerOption);
531
      } else {
532
        error(arg + " is not allowed in LC_LINKER_OPTION");
533
      }
534
    }
535
  }
536
}
537

538
static void addFileList(StringRef path, bool isLazy) {
539
  std::optional<MemoryBufferRef> buffer = readFile(path);
540
  if (!buffer)
541
    return;
542
  MemoryBufferRef mbref = *buffer;
543
  for (StringRef path : args::getLines(mbref))
544
    addFile(rerootPath(path), LoadType::CommandLine, isLazy);
545
}
546

547
// We expect sub-library names of the form "libfoo", which will match a dylib
548
// with a path of .*/libfoo.{dylib, tbd}.
549
// XXX ld64 seems to ignore the extension entirely when matching sub-libraries;
550
// I'm not sure what the use case for that is.
551
static bool markReexport(StringRef searchName, ArrayRef<StringRef> extensions) {
552
  for (InputFile *file : inputFiles) {
553
    if (auto *dylibFile = dyn_cast<DylibFile>(file)) {
554
      StringRef filename = path::filename(dylibFile->getName());
555
      if (filename.consume_front(searchName) &&
556
          (filename.empty() || llvm::is_contained(extensions, filename))) {
557
        dylibFile->reexport = true;
558
        return true;
559
      }
560
    }
561
  }
562
  return false;
563
}
564

565
// This function is called on startup. We need this for LTO since
566
// LTO calls LLVM functions to compile bitcode files to native code.
567
// Technically this can be delayed until we read bitcode files, but
568
// we don't bother to do lazily because the initialization is fast.
569
static void initLLVM() {
570
  InitializeAllTargets();
571
  InitializeAllTargetMCs();
572
  InitializeAllAsmPrinters();
573
  InitializeAllAsmParsers();
574
}
575

576
static bool compileBitcodeFiles() {
577
  TimeTraceScope timeScope("LTO");
578
  auto *lto = make<BitcodeCompiler>();
579
  for (InputFile *file : inputFiles)
580
    if (auto *bitcodeFile = dyn_cast<BitcodeFile>(file))
581
      if (!file->lazy)
582
        lto->add(*bitcodeFile);
583

584
  std::vector<ObjFile *> compiled = lto->compile();
585
  for (ObjFile *file : compiled)
586
    inputFiles.insert(file);
587

588
  return !compiled.empty();
589
}
590

591
// Replaces common symbols with defined symbols residing in __common sections.
592
// This function must be called after all symbol names are resolved (i.e. after
593
// all InputFiles have been loaded.) As a result, later operations won't see
594
// any CommonSymbols.
595
static void replaceCommonSymbols() {
596
  TimeTraceScope timeScope("Replace common symbols");
597
  ConcatOutputSection *osec = nullptr;
598
  for (Symbol *sym : symtab->getSymbols()) {
599
    auto *common = dyn_cast<CommonSymbol>(sym);
600
    if (common == nullptr)
601
      continue;
602

603
    // Casting to size_t will truncate large values on 32-bit architectures,
604
    // but it's not really worth supporting the linking of 64-bit programs on
605
    // 32-bit archs.
606
    ArrayRef<uint8_t> data = {nullptr, static_cast<size_t>(common->size)};
607
    // FIXME avoid creating one Section per symbol?
608
    auto *section =
609
        make<Section>(common->getFile(), segment_names::data,
610
                      section_names::common, S_ZEROFILL, /*addr=*/0);
611
    auto *isec = make<ConcatInputSection>(*section, data, common->align);
612
    if (!osec)
613
      osec = ConcatOutputSection::getOrCreateForInput(isec);
614
    isec->parent = osec;
615
    addInputSection(isec);
616

617
    // FIXME: CommonSymbol should store isReferencedDynamically, noDeadStrip
618
    // and pass them on here.
619
    replaceSymbol<Defined>(
620
        sym, sym->getName(), common->getFile(), isec, /*value=*/0, common->size,
621
        /*isWeakDef=*/false, /*isExternal=*/true, common->privateExtern,
622
        /*includeInSymtab=*/true, /*isReferencedDynamically=*/false,
623
        /*noDeadStrip=*/false);
624
  }
625
}
626

627
static void initializeSectionRenameMap() {
628
  if (config->dataConst) {
629
    SmallVector<StringRef> v{section_names::got,
630
                             section_names::authGot,
631
                             section_names::authPtr,
632
                             section_names::nonLazySymbolPtr,
633
                             section_names::const_,
634
                             section_names::cfString,
635
                             section_names::moduleInitFunc,
636
                             section_names::moduleTermFunc,
637
                             section_names::objcClassList,
638
                             section_names::objcNonLazyClassList,
639
                             section_names::objcCatList,
640
                             section_names::objcNonLazyCatList,
641
                             section_names::objcProtoList,
642
                             section_names::objCImageInfo};
643
    for (StringRef s : v)
644
      config->sectionRenameMap[{segment_names::data, s}] = {
645
          segment_names::dataConst, s};
646
  }
647
  config->sectionRenameMap[{segment_names::text, section_names::staticInit}] = {
648
      segment_names::text, section_names::text};
649
  config->sectionRenameMap[{segment_names::import, section_names::pointers}] = {
650
      config->dataConst ? segment_names::dataConst : segment_names::data,
651
      section_names::nonLazySymbolPtr};
652
}
653

654
static inline char toLowerDash(char x) {
655
  if (x >= 'A' && x <= 'Z')
656
    return x - 'A' + 'a';
657
  else if (x == ' ')
658
    return '-';
659
  return x;
660
}
661

662
static std::string lowerDash(StringRef s) {
663
  return std::string(map_iterator(s.begin(), toLowerDash),
664
                     map_iterator(s.end(), toLowerDash));
665
}
666

667
struct PlatformVersion {
668
  PlatformType platform = PLATFORM_UNKNOWN;
669
  llvm::VersionTuple minimum;
670
  llvm::VersionTuple sdk;
671
};
672

673
static PlatformVersion parsePlatformVersion(const Arg *arg) {
674
  assert(arg->getOption().getID() == OPT_platform_version);
675
  StringRef platformStr = arg->getValue(0);
676
  StringRef minVersionStr = arg->getValue(1);
677
  StringRef sdkVersionStr = arg->getValue(2);
678

679
  PlatformVersion platformVersion;
680

681
  // TODO(compnerd) see if we can generate this case list via XMACROS
682
  platformVersion.platform =
683
      StringSwitch<PlatformType>(lowerDash(platformStr))
684
          .Cases("macos", "1", PLATFORM_MACOS)
685
          .Cases("ios", "2", PLATFORM_IOS)
686
          .Cases("tvos", "3", PLATFORM_TVOS)
687
          .Cases("watchos", "4", PLATFORM_WATCHOS)
688
          .Cases("bridgeos", "5", PLATFORM_BRIDGEOS)
689
          .Cases("mac-catalyst", "6", PLATFORM_MACCATALYST)
690
          .Cases("ios-simulator", "7", PLATFORM_IOSSIMULATOR)
691
          .Cases("tvos-simulator", "8", PLATFORM_TVOSSIMULATOR)
692
          .Cases("watchos-simulator", "9", PLATFORM_WATCHOSSIMULATOR)
693
          .Cases("driverkit", "10", PLATFORM_DRIVERKIT)
694
          .Cases("xros", "11", PLATFORM_XROS)
695
          .Cases("xros-simulator", "12", PLATFORM_XROS_SIMULATOR)
696
          .Default(PLATFORM_UNKNOWN);
697
  if (platformVersion.platform == PLATFORM_UNKNOWN)
698
    error(Twine("malformed platform: ") + platformStr);
699
  // TODO: check validity of version strings, which varies by platform
700
  // NOTE: ld64 accepts version strings with 5 components
701
  // llvm::VersionTuple accepts no more than 4 components
702
  // Has Apple ever published version strings with 5 components?
703
  if (platformVersion.minimum.tryParse(minVersionStr))
704
    error(Twine("malformed minimum version: ") + minVersionStr);
705
  if (platformVersion.sdk.tryParse(sdkVersionStr))
706
    error(Twine("malformed sdk version: ") + sdkVersionStr);
707
  return platformVersion;
708
}
709

710
// Has the side-effect of setting Config::platformInfo and
711
// potentially Config::secondaryPlatformInfo.
712
static void setPlatformVersions(StringRef archName, const ArgList &args) {
713
  std::map<PlatformType, PlatformVersion> platformVersions;
714
  const PlatformVersion *lastVersionInfo = nullptr;
715
  for (const Arg *arg : args.filtered(OPT_platform_version)) {
716
    PlatformVersion version = parsePlatformVersion(arg);
717

718
    // For each platform, the last flag wins:
719
    // `-platform_version macos 2 3 -platform_version macos 4 5` has the same
720
    // effect as just passing `-platform_version macos 4 5`.
721
    // FIXME: ld64 warns on multiple flags for one platform. Should we?
722
    platformVersions[version.platform] = version;
723
    lastVersionInfo = &platformVersions[version.platform];
724
  }
725

726
  if (platformVersions.empty()) {
727
    error("must specify -platform_version");
728
    return;
729
  }
730
  if (platformVersions.size() > 2) {
731
    error("must specify -platform_version at most twice");
732
    return;
733
  }
734
  if (platformVersions.size() == 2) {
735
    bool isZipperedCatalyst = platformVersions.count(PLATFORM_MACOS) &&
736
                              platformVersions.count(PLATFORM_MACCATALYST);
737

738
    if (!isZipperedCatalyst) {
739
      error("lld supports writing zippered outputs only for "
740
            "macos and mac-catalyst");
741
    } else if (config->outputType != MH_DYLIB &&
742
               config->outputType != MH_BUNDLE) {
743
      error("writing zippered outputs only valid for -dylib and -bundle");
744
    }
745

746
    config->platformInfo = {
747
        MachO::Target(getArchitectureFromName(archName), PLATFORM_MACOS,
748
                      platformVersions[PLATFORM_MACOS].minimum),
749
        platformVersions[PLATFORM_MACOS].sdk};
750
    config->secondaryPlatformInfo = {
751
        MachO::Target(getArchitectureFromName(archName), PLATFORM_MACCATALYST,
752
                      platformVersions[PLATFORM_MACCATALYST].minimum),
753
        platformVersions[PLATFORM_MACCATALYST].sdk};
754
    return;
755
  }
756

757
  config->platformInfo = {MachO::Target(getArchitectureFromName(archName),
758
                                        lastVersionInfo->platform,
759
                                        lastVersionInfo->minimum),
760
                          lastVersionInfo->sdk};
761
}
762

763
// Has the side-effect of setting Config::target.
764
static TargetInfo *createTargetInfo(InputArgList &args) {
765
  StringRef archName = args.getLastArgValue(OPT_arch);
766
  if (archName.empty()) {
767
    error("must specify -arch");
768
    return nullptr;
769
  }
770

771
  setPlatformVersions(archName, args);
772
  auto [cpuType, cpuSubtype] = getCPUTypeFromArchitecture(config->arch());
773
  switch (cpuType) {
774
  case CPU_TYPE_X86_64:
775
    return createX86_64TargetInfo();
776
  case CPU_TYPE_ARM64:
777
    return createARM64TargetInfo();
778
  case CPU_TYPE_ARM64_32:
779
    return createARM64_32TargetInfo();
780
  default:
781
    error("missing or unsupported -arch " + archName);
782
    return nullptr;
783
  }
784
}
785

786
static UndefinedSymbolTreatment
787
getUndefinedSymbolTreatment(const ArgList &args) {
788
  StringRef treatmentStr = args.getLastArgValue(OPT_undefined);
789
  auto treatment =
790
      StringSwitch<UndefinedSymbolTreatment>(treatmentStr)
791
          .Cases("error", "", UndefinedSymbolTreatment::error)
792
          .Case("warning", UndefinedSymbolTreatment::warning)
793
          .Case("suppress", UndefinedSymbolTreatment::suppress)
794
          .Case("dynamic_lookup", UndefinedSymbolTreatment::dynamic_lookup)
795
          .Default(UndefinedSymbolTreatment::unknown);
796
  if (treatment == UndefinedSymbolTreatment::unknown) {
797
    warn(Twine("unknown -undefined TREATMENT '") + treatmentStr +
798
         "', defaulting to 'error'");
799
    treatment = UndefinedSymbolTreatment::error;
800
  } else if (config->namespaceKind == NamespaceKind::twolevel &&
801
             (treatment == UndefinedSymbolTreatment::warning ||
802
              treatment == UndefinedSymbolTreatment::suppress)) {
803
    if (treatment == UndefinedSymbolTreatment::warning)
804
      fatal("'-undefined warning' only valid with '-flat_namespace'");
805
    else
806
      fatal("'-undefined suppress' only valid with '-flat_namespace'");
807
    treatment = UndefinedSymbolTreatment::error;
808
  }
809
  return treatment;
810
}
811

812
static ICFLevel getICFLevel(const ArgList &args) {
813
  StringRef icfLevelStr = args.getLastArgValue(OPT_icf_eq);
814
  auto icfLevel = StringSwitch<ICFLevel>(icfLevelStr)
815
                      .Cases("none", "", ICFLevel::none)
816
                      .Case("safe", ICFLevel::safe)
817
                      .Case("all", ICFLevel::all)
818
                      .Default(ICFLevel::unknown);
819
  if (icfLevel == ICFLevel::unknown) {
820
    warn(Twine("unknown --icf=OPTION `") + icfLevelStr +
821
         "', defaulting to `none'");
822
    icfLevel = ICFLevel::none;
823
  }
824
  return icfLevel;
825
}
826

827
static ObjCStubsMode getObjCStubsMode(const ArgList &args) {
828
  const Arg *arg = args.getLastArg(OPT_objc_stubs_fast, OPT_objc_stubs_small);
829
  if (!arg)
830
    return ObjCStubsMode::fast;
831

832
  if (arg->getOption().getID() == OPT_objc_stubs_small) {
833
    if (is_contained({AK_arm64e, AK_arm64}, config->arch()))
834
      return ObjCStubsMode::small;
835
    else
836
      warn("-objc_stubs_small is not yet implemented, defaulting to "
837
           "-objc_stubs_fast");
838
  }
839
  return ObjCStubsMode::fast;
840
}
841

842
static void warnIfDeprecatedOption(const Option &opt) {
843
  if (!opt.getGroup().isValid())
844
    return;
845
  if (opt.getGroup().getID() == OPT_grp_deprecated) {
846
    warn("Option `" + opt.getPrefixedName() + "' is deprecated in ld64:");
847
    warn(opt.getHelpText());
848
  }
849
}
850

851
static void warnIfUnimplementedOption(const Option &opt) {
852
  if (!opt.getGroup().isValid() || !opt.hasFlag(DriverFlag::HelpHidden))
853
    return;
854
  switch (opt.getGroup().getID()) {
855
  case OPT_grp_deprecated:
856
    // warn about deprecated options elsewhere
857
    break;
858
  case OPT_grp_undocumented:
859
    warn("Option `" + opt.getPrefixedName() +
860
         "' is undocumented. Should lld implement it?");
861
    break;
862
  case OPT_grp_obsolete:
863
    warn("Option `" + opt.getPrefixedName() +
864
         "' is obsolete. Please modernize your usage.");
865
    break;
866
  case OPT_grp_ignored:
867
    warn("Option `" + opt.getPrefixedName() + "' is ignored.");
868
    break;
869
  case OPT_grp_ignored_silently:
870
    break;
871
  default:
872
    warn("Option `" + opt.getPrefixedName() +
873
         "' is not yet implemented. Stay tuned...");
874
    break;
875
  }
876
}
877

878
static const char *getReproduceOption(InputArgList &args) {
879
  if (const Arg *arg = args.getLastArg(OPT_reproduce))
880
    return arg->getValue();
881
  return getenv("LLD_REPRODUCE");
882
}
883

884
// Parse options of the form "old;new".
885
static std::pair<StringRef, StringRef> getOldNewOptions(opt::InputArgList &args,
886
                                                        unsigned id) {
887
  auto *arg = args.getLastArg(id);
888
  if (!arg)
889
    return {"", ""};
890

891
  StringRef s = arg->getValue();
892
  std::pair<StringRef, StringRef> ret = s.split(';');
893
  if (ret.second.empty())
894
    error(arg->getSpelling() + " expects 'old;new' format, but got " + s);
895
  return ret;
896
}
897

898
// Parse options of the form "old;new[;extra]".
899
static std::tuple<StringRef, StringRef, StringRef>
900
getOldNewOptionsExtra(opt::InputArgList &args, unsigned id) {
901
  auto [oldDir, second] = getOldNewOptions(args, id);
902
  auto [newDir, extraDir] = second.split(';');
903
  return {oldDir, newDir, extraDir};
904
}
905

906
static void parseClangOption(StringRef opt, const Twine &msg) {
907
  std::string err;
908
  raw_string_ostream os(err);
909

910
  const char *argv[] = {"lld", opt.data()};
911
  if (cl::ParseCommandLineOptions(2, argv, "", &os))
912
    return;
913
  os.flush();
914
  error(msg + ": " + StringRef(err).trim());
915
}
916

917
static uint32_t parseDylibVersion(const ArgList &args, unsigned id) {
918
  const Arg *arg = args.getLastArg(id);
919
  if (!arg)
920
    return 0;
921

922
  if (config->outputType != MH_DYLIB) {
923
    error(arg->getAsString(args) + ": only valid with -dylib");
924
    return 0;
925
  }
926

927
  PackedVersion version;
928
  if (!version.parse32(arg->getValue())) {
929
    error(arg->getAsString(args) + ": malformed version");
930
    return 0;
931
  }
932

933
  return version.rawValue();
934
}
935

936
static uint32_t parseProtection(StringRef protStr) {
937
  uint32_t prot = 0;
938
  for (char c : protStr) {
939
    switch (c) {
940
    case 'r':
941
      prot |= VM_PROT_READ;
942
      break;
943
    case 'w':
944
      prot |= VM_PROT_WRITE;
945
      break;
946
    case 'x':
947
      prot |= VM_PROT_EXECUTE;
948
      break;
949
    case '-':
950
      break;
951
    default:
952
      error("unknown -segprot letter '" + Twine(c) + "' in " + protStr);
953
      return 0;
954
    }
955
  }
956
  return prot;
957
}
958

959
static std::vector<SectionAlign> parseSectAlign(const opt::InputArgList &args) {
960
  std::vector<SectionAlign> sectAligns;
961
  for (const Arg *arg : args.filtered(OPT_sectalign)) {
962
    StringRef segName = arg->getValue(0);
963
    StringRef sectName = arg->getValue(1);
964
    StringRef alignStr = arg->getValue(2);
965
    alignStr.consume_front_insensitive("0x");
966
    uint32_t align;
967
    if (alignStr.getAsInteger(16, align)) {
968
      error("-sectalign: failed to parse '" + StringRef(arg->getValue(2)) +
969
            "' as number");
970
      continue;
971
    }
972
    if (!isPowerOf2_32(align)) {
973
      error("-sectalign: '" + StringRef(arg->getValue(2)) +
974
            "' (in base 16) not a power of two");
975
      continue;
976
    }
977
    sectAligns.push_back({segName, sectName, align});
978
  }
979
  return sectAligns;
980
}
981

982
PlatformType macho::removeSimulator(PlatformType platform) {
983
  switch (platform) {
984
  case PLATFORM_IOSSIMULATOR:
985
    return PLATFORM_IOS;
986
  case PLATFORM_TVOSSIMULATOR:
987
    return PLATFORM_TVOS;
988
  case PLATFORM_WATCHOSSIMULATOR:
989
    return PLATFORM_WATCHOS;
990
  case PLATFORM_XROS_SIMULATOR:
991
    return PLATFORM_XROS;
992
  default:
993
    return platform;
994
  }
995
}
996

997
static bool supportsNoPie() {
998
  return !(config->arch() == AK_arm64 || config->arch() == AK_arm64e ||
999
           config->arch() == AK_arm64_32);
1000
}
1001

1002
static bool shouldAdhocSignByDefault(Architecture arch, PlatformType platform) {
1003
  if (arch != AK_arm64 && arch != AK_arm64e)
1004
    return false;
1005

1006
  return platform == PLATFORM_MACOS || platform == PLATFORM_IOSSIMULATOR ||
1007
         platform == PLATFORM_TVOSSIMULATOR ||
1008
         platform == PLATFORM_WATCHOSSIMULATOR ||
1009
         platform == PLATFORM_XROS_SIMULATOR;
1010
}
1011

1012
static bool dataConstDefault(const InputArgList &args) {
1013
  static const std::array<std::pair<PlatformType, VersionTuple>, 6> minVersion =
1014
      {{{PLATFORM_MACOS, VersionTuple(10, 15)},
1015
        {PLATFORM_IOS, VersionTuple(13, 0)},
1016
        {PLATFORM_TVOS, VersionTuple(13, 0)},
1017
        {PLATFORM_WATCHOS, VersionTuple(6, 0)},
1018
        {PLATFORM_XROS, VersionTuple(1, 0)},
1019
        {PLATFORM_BRIDGEOS, VersionTuple(4, 0)}}};
1020
  PlatformType platform = removeSimulator(config->platformInfo.target.Platform);
1021
  auto it = llvm::find_if(minVersion,
1022
                          [&](const auto &p) { return p.first == platform; });
1023
  if (it != minVersion.end())
1024
    if (config->platformInfo.target.MinDeployment < it->second)
1025
      return false;
1026

1027
  switch (config->outputType) {
1028
  case MH_EXECUTE:
1029
    return !(args.hasArg(OPT_no_pie) && supportsNoPie());
1030
  case MH_BUNDLE:
1031
    // FIXME: return false when -final_name ...
1032
    // has prefix "/System/Library/UserEventPlugins/"
1033
    // or matches "/usr/libexec/locationd" "/usr/libexec/terminusd"
1034
    return true;
1035
  case MH_DYLIB:
1036
    return true;
1037
  case MH_OBJECT:
1038
    return false;
1039
  default:
1040
    llvm_unreachable(
1041
        "unsupported output type for determining data-const default");
1042
  }
1043
  return false;
1044
}
1045

1046
static bool shouldEmitChainedFixups(const InputArgList &args) {
1047
  const Arg *arg = args.getLastArg(OPT_fixup_chains, OPT_no_fixup_chains);
1048
  if (arg && arg->getOption().matches(OPT_no_fixup_chains))
1049
    return false;
1050

1051
  bool isRequested = arg != nullptr;
1052

1053
  // Version numbers taken from the Xcode 13.3 release notes.
1054
  static const std::array<std::pair<PlatformType, VersionTuple>, 5> minVersion =
1055
      {{{PLATFORM_MACOS, VersionTuple(11, 0)},
1056
        {PLATFORM_IOS, VersionTuple(13, 4)},
1057
        {PLATFORM_TVOS, VersionTuple(14, 0)},
1058
        {PLATFORM_WATCHOS, VersionTuple(7, 0)},
1059
        {PLATFORM_XROS, VersionTuple(1, 0)}}};
1060
  PlatformType platform = removeSimulator(config->platformInfo.target.Platform);
1061
  auto it = llvm::find_if(minVersion,
1062
                          [&](const auto &p) { return p.first == platform; });
1063
  if (it != minVersion.end() &&
1064
      it->second > config->platformInfo.target.MinDeployment) {
1065
    if (!isRequested)
1066
      return false;
1067

1068
    warn("-fixup_chains requires " + getPlatformName(config->platform()) + " " +
1069
         it->second.getAsString() + ", which is newer than target minimum of " +
1070
         config->platformInfo.target.MinDeployment.getAsString());
1071
  }
1072

1073
  if (!is_contained({AK_x86_64, AK_x86_64h, AK_arm64}, config->arch())) {
1074
    if (isRequested)
1075
      error("-fixup_chains is only supported on x86_64 and arm64 targets");
1076
    return false;
1077
  }
1078

1079
  if (!config->isPic) {
1080
    if (isRequested)
1081
      error("-fixup_chains is incompatible with -no_pie");
1082
    return false;
1083
  }
1084

1085
  // TODO: Enable by default once stable.
1086
  return isRequested;
1087
}
1088

1089
static bool shouldEmitRelativeMethodLists(const InputArgList &args) {
1090
  const Arg *arg = args.getLastArg(OPT_objc_relative_method_lists,
1091
                                   OPT_no_objc_relative_method_lists);
1092
  if (arg && arg->getOption().getID() == OPT_objc_relative_method_lists)
1093
    return true;
1094
  if (arg && arg->getOption().getID() == OPT_no_objc_relative_method_lists)
1095
    return false;
1096

1097
  // TODO: If no flag is specified, don't default to false, but instead:
1098
  //   - default false on   <   ios14
1099
  //   - default true  on   >=  ios14
1100
  // For now, until this feature is confirmed stable, default to false if no
1101
  // flag is explicitly specified
1102
  return false;
1103
}
1104

1105
void SymbolPatterns::clear() {
1106
  literals.clear();
1107
  globs.clear();
1108
}
1109

1110
void SymbolPatterns::insert(StringRef symbolName) {
1111
  if (symbolName.find_first_of("*?[]") == StringRef::npos)
1112
    literals.insert(CachedHashStringRef(symbolName));
1113
  else if (Expected<GlobPattern> pattern = GlobPattern::create(symbolName))
1114
    globs.emplace_back(*pattern);
1115
  else
1116
    error("invalid symbol-name pattern: " + symbolName);
1117
}
1118

1119
bool SymbolPatterns::matchLiteral(StringRef symbolName) const {
1120
  return literals.contains(CachedHashStringRef(symbolName));
1121
}
1122

1123
bool SymbolPatterns::matchGlob(StringRef symbolName) const {
1124
  for (const GlobPattern &glob : globs)
1125
    if (glob.match(symbolName))
1126
      return true;
1127
  return false;
1128
}
1129

1130
bool SymbolPatterns::match(StringRef symbolName) const {
1131
  return matchLiteral(symbolName) || matchGlob(symbolName);
1132
}
1133

1134
static void parseSymbolPatternsFile(const Arg *arg,
1135
                                    SymbolPatterns &symbolPatterns) {
1136
  StringRef path = arg->getValue();
1137
  std::optional<MemoryBufferRef> buffer = readFile(path);
1138
  if (!buffer) {
1139
    error("Could not read symbol file: " + path);
1140
    return;
1141
  }
1142
  MemoryBufferRef mbref = *buffer;
1143
  for (StringRef line : args::getLines(mbref)) {
1144
    line = line.take_until([](char c) { return c == '#'; }).trim();
1145
    if (!line.empty())
1146
      symbolPatterns.insert(line);
1147
  }
1148
}
1149

1150
static void handleSymbolPatterns(InputArgList &args,
1151
                                 SymbolPatterns &symbolPatterns,
1152
                                 unsigned singleOptionCode,
1153
                                 unsigned listFileOptionCode) {
1154
  for (const Arg *arg : args.filtered(singleOptionCode))
1155
    symbolPatterns.insert(arg->getValue());
1156
  for (const Arg *arg : args.filtered(listFileOptionCode))
1157
    parseSymbolPatternsFile(arg, symbolPatterns);
1158
}
1159

1160
static void createFiles(const InputArgList &args) {
1161
  TimeTraceScope timeScope("Load input files");
1162
  // This loop should be reserved for options whose exact ordering matters.
1163
  // Other options should be handled via filtered() and/or getLastArg().
1164
  bool isLazy = false;
1165
  // If we've processed an opening --start-lib, without a matching --end-lib
1166
  bool inLib = false;
1167
  for (const Arg *arg : args) {
1168
    const Option &opt = arg->getOption();
1169
    warnIfDeprecatedOption(opt);
1170
    warnIfUnimplementedOption(opt);
1171

1172
    switch (opt.getID()) {
1173
    case OPT_INPUT:
1174
      addFile(rerootPath(arg->getValue()), LoadType::CommandLine, isLazy);
1175
      break;
1176
    case OPT_needed_library:
1177
      if (auto *dylibFile = dyn_cast_or_null<DylibFile>(
1178
              addFile(rerootPath(arg->getValue()), LoadType::CommandLine)))
1179
        dylibFile->forceNeeded = true;
1180
      break;
1181
    case OPT_reexport_library:
1182
      if (auto *dylibFile = dyn_cast_or_null<DylibFile>(
1183
              addFile(rerootPath(arg->getValue()), LoadType::CommandLine))) {
1184
        config->hasReexports = true;
1185
        dylibFile->reexport = true;
1186
      }
1187
      break;
1188
    case OPT_weak_library:
1189
      if (auto *dylibFile = dyn_cast_or_null<DylibFile>(
1190
              addFile(rerootPath(arg->getValue()), LoadType::CommandLine)))
1191
        dylibFile->forceWeakImport = true;
1192
      break;
1193
    case OPT_filelist:
1194
      addFileList(arg->getValue(), isLazy);
1195
      break;
1196
    case OPT_force_load:
1197
      addFile(rerootPath(arg->getValue()), LoadType::CommandLineForce);
1198
      break;
1199
    case OPT_load_hidden:
1200
      addFile(rerootPath(arg->getValue()), LoadType::CommandLine,
1201
              /*isLazy=*/false, /*isExplicit=*/true, /*isBundleLoader=*/false,
1202
              /*isForceHidden=*/true);
1203
      break;
1204
    case OPT_l:
1205
    case OPT_needed_l:
1206
    case OPT_reexport_l:
1207
    case OPT_weak_l:
1208
    case OPT_hidden_l:
1209
      addLibrary(arg->getValue(), opt.getID() == OPT_needed_l,
1210
                 opt.getID() == OPT_weak_l, opt.getID() == OPT_reexport_l,
1211
                 opt.getID() == OPT_hidden_l,
1212
                 /*isExplicit=*/true, LoadType::CommandLine);
1213
      break;
1214
    case OPT_framework:
1215
    case OPT_needed_framework:
1216
    case OPT_reexport_framework:
1217
    case OPT_weak_framework:
1218
      addFramework(arg->getValue(), opt.getID() == OPT_needed_framework,
1219
                   opt.getID() == OPT_weak_framework,
1220
                   opt.getID() == OPT_reexport_framework, /*isExplicit=*/true,
1221
                   LoadType::CommandLine);
1222
      break;
1223
    case OPT_start_lib:
1224
      if (inLib)
1225
        error("nested --start-lib");
1226
      inLib = true;
1227
      if (!config->allLoad)
1228
        isLazy = true;
1229
      break;
1230
    case OPT_end_lib:
1231
      if (!inLib)
1232
        error("stray --end-lib");
1233
      inLib = false;
1234
      isLazy = false;
1235
      break;
1236
    default:
1237
      break;
1238
    }
1239
  }
1240
}
1241

1242
static void gatherInputSections() {
1243
  TimeTraceScope timeScope("Gathering input sections");
1244
  for (const InputFile *file : inputFiles) {
1245
    for (const Section *section : file->sections) {
1246
      // Compact unwind entries require special handling elsewhere. (In
1247
      // contrast, EH frames are handled like regular ConcatInputSections.)
1248
      if (section->name == section_names::compactUnwind)
1249
        continue;
1250
      for (const Subsection &subsection : section->subsections)
1251
        addInputSection(subsection.isec);
1252
    }
1253
    if (!file->objCImageInfo.empty())
1254
      in.objCImageInfo->addFile(file);
1255
  }
1256
}
1257

1258
static void foldIdenticalLiterals() {
1259
  TimeTraceScope timeScope("Fold identical literals");
1260
  // We always create a cStringSection, regardless of whether dedupLiterals is
1261
  // true. If it isn't, we simply create a non-deduplicating CStringSection.
1262
  // Either way, we must unconditionally finalize it here.
1263
  in.cStringSection->finalizeContents();
1264
  in.objcMethnameSection->finalizeContents();
1265
  in.wordLiteralSection->finalizeContents();
1266
}
1267

1268
static void addSynthenticMethnames() {
1269
  std::string &data = *make<std::string>();
1270
  llvm::raw_string_ostream os(data);
1271
  for (Symbol *sym : symtab->getSymbols())
1272
    if (isa<Undefined>(sym))
1273
      if (ObjCStubsSection::isObjCStubSymbol(sym))
1274
        os << ObjCStubsSection::getMethname(sym) << '\0';
1275

1276
  if (data.empty())
1277
    return;
1278

1279
  const auto *buf = reinterpret_cast<const uint8_t *>(data.c_str());
1280
  Section &section = *make<Section>(/*file=*/nullptr, segment_names::text,
1281
                                    section_names::objcMethname,
1282
                                    S_CSTRING_LITERALS, /*addr=*/0);
1283

1284
  auto *isec =
1285
      make<CStringInputSection>(section, ArrayRef<uint8_t>{buf, data.size()},
1286
                                /*align=*/1, /*dedupLiterals=*/true);
1287
  isec->splitIntoPieces();
1288
  for (auto &piece : isec->pieces)
1289
    piece.live = true;
1290
  section.subsections.push_back({0, isec});
1291
  in.objcMethnameSection->addInput(isec);
1292
  in.objcMethnameSection->isec->markLive(0);
1293
}
1294

1295
static void referenceStubBinder() {
1296
  bool needsStubHelper = config->outputType == MH_DYLIB ||
1297
                         config->outputType == MH_EXECUTE ||
1298
                         config->outputType == MH_BUNDLE;
1299
  if (!needsStubHelper || !symtab->find("dyld_stub_binder"))
1300
    return;
1301

1302
  // dyld_stub_binder is used by dyld to resolve lazy bindings. This code here
1303
  // adds a opportunistic reference to dyld_stub_binder if it happens to exist.
1304
  // dyld_stub_binder is in libSystem.dylib, which is usually linked in. This
1305
  // isn't needed for correctness, but the presence of that symbol suppresses
1306
  // "no symbols" diagnostics from `nm`.
1307
  // StubHelperSection::setUp() adds a reference and errors out if
1308
  // dyld_stub_binder doesn't exist in case it is actually needed.
1309
  symtab->addUndefined("dyld_stub_binder", /*file=*/nullptr, /*isWeak=*/false);
1310
}
1311

1312
static void createAliases() {
1313
  for (const auto &pair : config->aliasedSymbols) {
1314
    if (const auto &sym = symtab->find(pair.first)) {
1315
      if (const auto &defined = dyn_cast<Defined>(sym)) {
1316
        symtab->aliasDefined(defined, pair.second, defined->getFile())
1317
            ->noDeadStrip = true;
1318
      } else {
1319
        error("TODO: support aliasing to symbols of kind " +
1320
              Twine(sym->kind()));
1321
      }
1322
    } else {
1323
      warn("undefined base symbol '" + pair.first + "' for alias '" +
1324
           pair.second + "'\n");
1325
    }
1326
  }
1327

1328
  for (const InputFile *file : inputFiles) {
1329
    if (auto *objFile = dyn_cast<ObjFile>(file)) {
1330
      for (const AliasSymbol *alias : objFile->aliases) {
1331
        if (const auto &aliased = symtab->find(alias->getAliasedName())) {
1332
          if (const auto &defined = dyn_cast<Defined>(aliased)) {
1333
            symtab->aliasDefined(defined, alias->getName(), alias->getFile(),
1334
                                 alias->privateExtern);
1335
          } else {
1336
            // Common, dylib, and undefined symbols are all valid alias
1337
            // referents (undefineds can become valid Defined symbols later on
1338
            // in the link.)
1339
            error("TODO: support aliasing to symbols of kind " +
1340
                  Twine(aliased->kind()));
1341
          }
1342
        } else {
1343
          // This shouldn't happen since MC generates undefined symbols to
1344
          // represent the alias referents. Thus we fatal() instead of just
1345
          // warning here.
1346
          fatal("unable to find alias referent " + alias->getAliasedName() +
1347
                " for " + alias->getName());
1348
        }
1349
      }
1350
    }
1351
  }
1352
}
1353

1354
static void handleExplicitExports() {
1355
  static constexpr int kMaxWarnings = 3;
1356
  if (config->hasExplicitExports) {
1357
    std::atomic<uint64_t> warningsCount{0};
1358
    parallelForEach(symtab->getSymbols(), [&warningsCount](Symbol *sym) {
1359
      if (auto *defined = dyn_cast<Defined>(sym)) {
1360
        if (config->exportedSymbols.match(sym->getName())) {
1361
          if (defined->privateExtern) {
1362
            if (defined->weakDefCanBeHidden) {
1363
              // weak_def_can_be_hidden symbols behave similarly to
1364
              // private_extern symbols in most cases, except for when
1365
              // it is explicitly exported.
1366
              // The former can be exported but the latter cannot.
1367
              defined->privateExtern = false;
1368
            } else {
1369
              // Only print the first 3 warnings verbosely, and
1370
              // shorten the rest to avoid crowding logs.
1371
              if (warningsCount.fetch_add(1, std::memory_order_relaxed) <
1372
                  kMaxWarnings)
1373
                warn("cannot export hidden symbol " + toString(*defined) +
1374
                     "\n>>> defined in " + toString(defined->getFile()));
1375
            }
1376
          }
1377
        } else {
1378
          defined->privateExtern = true;
1379
        }
1380
      } else if (auto *dysym = dyn_cast<DylibSymbol>(sym)) {
1381
        dysym->shouldReexport = config->exportedSymbols.match(sym->getName());
1382
      }
1383
    });
1384
    if (warningsCount > kMaxWarnings)
1385
      warn("<... " + Twine(warningsCount - kMaxWarnings) +
1386
           " more similar warnings...>");
1387
  } else if (!config->unexportedSymbols.empty()) {
1388
    parallelForEach(symtab->getSymbols(), [](Symbol *sym) {
1389
      if (auto *defined = dyn_cast<Defined>(sym))
1390
        if (config->unexportedSymbols.match(defined->getName()))
1391
          defined->privateExtern = true;
1392
    });
1393
  }
1394
}
1395

1396
static void eraseInitializerSymbols() {
1397
  for (ConcatInputSection *isec : in.initOffsets->inputs())
1398
    for (Defined *sym : isec->symbols)
1399
      sym->used = false;
1400
}
1401

1402
namespace lld {
1403
namespace macho {
1404
bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
1405
          llvm::raw_ostream &stderrOS, bool exitEarly, bool disableOutput) {
1406
  // This driver-specific context will be freed later by lldMain().
1407
  auto *ctx = new CommonLinkerContext;
1408

1409
  ctx->e.initialize(stdoutOS, stderrOS, exitEarly, disableOutput);
1410
  ctx->e.cleanupCallback = []() {
1411
    resolvedFrameworks.clear();
1412
    resolvedLibraries.clear();
1413
    cachedReads.clear();
1414
    concatOutputSections.clear();
1415
    inputFiles.clear();
1416
    inputSections.clear();
1417
    inputSectionsOrder = 0;
1418
    loadedArchives.clear();
1419
    loadedObjectFrameworks.clear();
1420
    missingAutolinkWarnings.clear();
1421
    syntheticSections.clear();
1422
    thunkMap.clear();
1423
    unprocessedLCLinkerOptions.clear();
1424
    ObjCSelRefsHelper::cleanup();
1425

1426
    firstTLVDataSection = nullptr;
1427
    tar = nullptr;
1428
    memset(&in, 0, sizeof(in));
1429

1430
    resetLoadedDylibs();
1431
    resetOutputSegments();
1432
    resetWriter();
1433
    InputFile::resetIdCount();
1434

1435
    objc::doCleanup();
1436
  };
1437

1438
  ctx->e.logName = args::getFilenameWithoutExe(argsArr[0]);
1439

1440
  MachOOptTable parser;
1441
  InputArgList args = parser.parse(argsArr.slice(1));
1442

1443
  ctx->e.errorLimitExceededMsg = "too many errors emitted, stopping now "
1444
                                 "(use --error-limit=0 to see all errors)";
1445
  ctx->e.errorLimit = args::getInteger(args, OPT_error_limit_eq, 20);
1446
  ctx->e.verbose = args.hasArg(OPT_verbose);
1447

1448
  if (args.hasArg(OPT_help_hidden)) {
1449
    parser.printHelp(argsArr[0], /*showHidden=*/true);
1450
    return true;
1451
  }
1452
  if (args.hasArg(OPT_help)) {
1453
    parser.printHelp(argsArr[0], /*showHidden=*/false);
1454
    return true;
1455
  }
1456
  if (args.hasArg(OPT_version)) {
1457
    message(getLLDVersion());
1458
    return true;
1459
  }
1460

1461
  config = std::make_unique<Configuration>();
1462
  symtab = std::make_unique<SymbolTable>();
1463
  config->outputType = getOutputType(args);
1464
  target = createTargetInfo(args);
1465
  depTracker = std::make_unique<DependencyTracker>(
1466
      args.getLastArgValue(OPT_dependency_info));
1467

1468
  config->ltoo = args::getInteger(args, OPT_lto_O, 2);
1469
  if (config->ltoo > 3)
1470
    error("--lto-O: invalid optimization level: " + Twine(config->ltoo));
1471
  unsigned ltoCgo =
1472
      args::getInteger(args, OPT_lto_CGO, args::getCGOptLevel(config->ltoo));
1473
  if (auto level = CodeGenOpt::getLevel(ltoCgo))
1474
    config->ltoCgo = *level;
1475
  else
1476
    error("--lto-CGO: invalid codegen optimization level: " + Twine(ltoCgo));
1477

1478
  if (errorCount())
1479
    return false;
1480

1481
  if (args.hasArg(OPT_pagezero_size)) {
1482
    uint64_t pagezeroSize = args::getHex(args, OPT_pagezero_size, 0);
1483

1484
    // ld64 does something really weird. It attempts to realign the value to the
1485
    // page size, but assumes the page size is 4K. This doesn't work with most
1486
    // of Apple's ARM64 devices, which use a page size of 16K. This means that
1487
    // it will first 4K align it by rounding down, then round up to 16K.  This
1488
    // probably only happened because no one using this arg with anything other
1489
    // then 0, so no one checked if it did what is what it says it does.
1490

1491
    // So we are not copying this weird behavior and doing the it in a logical
1492
    // way, by always rounding down to page size.
1493
    if (!isAligned(Align(target->getPageSize()), pagezeroSize)) {
1494
      pagezeroSize -= pagezeroSize % target->getPageSize();
1495
      warn("__PAGEZERO size is not page aligned, rounding down to 0x" +
1496
           Twine::utohexstr(pagezeroSize));
1497
    }
1498

1499
    target->pageZeroSize = pagezeroSize;
1500
  }
1501

1502
  config->osoPrefix = args.getLastArgValue(OPT_oso_prefix);
1503
  if (!config->osoPrefix.empty()) {
1504
    // Expand special characters, such as ".", "..", or  "~", if present.
1505
    // Note: LD64 only expands "." and not other special characters.
1506
    // That seems silly to imitate so we will not try to follow it, but rather
1507
    // just use real_path() to do it.
1508

1509
    // The max path length is 4096, in theory. However that seems quite long
1510
    // and seems unlikely that any one would want to strip everything from the
1511
    // path. Hence we've picked a reasonably large number here.
1512
    SmallString<1024> expanded;
1513
    if (!fs::real_path(config->osoPrefix, expanded,
1514
                       /*expand_tilde=*/true)) {
1515
      // Note: LD64 expands "." to be `<current_dir>/`
1516
      // (ie., it has a slash suffix) whereas real_path() doesn't.
1517
      // So we have to append '/' to be consistent.
1518
      StringRef sep = sys::path::get_separator();
1519
      // real_path removes trailing slashes as part of the normalization, but
1520
      // these are meaningful for our text based stripping
1521
      if (config->osoPrefix == "." || config->osoPrefix.ends_with(sep))
1522
        expanded += sep;
1523
      config->osoPrefix = saver().save(expanded.str());
1524
    }
1525
  }
1526

1527
  bool pie = args.hasFlag(OPT_pie, OPT_no_pie, true);
1528
  if (!supportsNoPie() && !pie) {
1529
    warn("-no_pie ignored for arm64");
1530
    pie = true;
1531
  }
1532

1533
  config->isPic = config->outputType == MH_DYLIB ||
1534
                  config->outputType == MH_BUNDLE ||
1535
                  (config->outputType == MH_EXECUTE && pie);
1536

1537
  // Must be set before any InputSections and Symbols are created.
1538
  config->deadStrip = args.hasArg(OPT_dead_strip);
1539

1540
  config->systemLibraryRoots = getSystemLibraryRoots(args);
1541
  if (const char *path = getReproduceOption(args)) {
1542
    // Note that --reproduce is a debug option so you can ignore it
1543
    // if you are trying to understand the whole picture of the code.
1544
    Expected<std::unique_ptr<TarWriter>> errOrWriter =
1545
        TarWriter::create(path, path::stem(path));
1546
    if (errOrWriter) {
1547
      tar = std::move(*errOrWriter);
1548
      tar->append("response.txt", createResponseFile(args));
1549
      tar->append("version.txt", getLLDVersion() + "\n");
1550
    } else {
1551
      error("--reproduce: " + toString(errOrWriter.takeError()));
1552
    }
1553
  }
1554

1555
  if (auto *arg = args.getLastArg(OPT_threads_eq)) {
1556
    StringRef v(arg->getValue());
1557
    unsigned threads = 0;
1558
    if (!llvm::to_integer(v, threads, 0) || threads == 0)
1559
      error(arg->getSpelling() + ": expected a positive integer, but got '" +
1560
            arg->getValue() + "'");
1561
    parallel::strategy = hardware_concurrency(threads);
1562
    config->thinLTOJobs = v;
1563
  }
1564
  if (auto *arg = args.getLastArg(OPT_thinlto_jobs_eq))
1565
    config->thinLTOJobs = arg->getValue();
1566
  if (!get_threadpool_strategy(config->thinLTOJobs))
1567
    error("--thinlto-jobs: invalid job count: " + config->thinLTOJobs);
1568

1569
  for (const Arg *arg : args.filtered(OPT_u)) {
1570
    config->explicitUndefineds.push_back(symtab->addUndefined(
1571
        arg->getValue(), /*file=*/nullptr, /*isWeakRef=*/false));
1572
  }
1573

1574
  for (const Arg *arg : args.filtered(OPT_U))
1575
    config->explicitDynamicLookups.insert(arg->getValue());
1576

1577
  config->mapFile = args.getLastArgValue(OPT_map);
1578
  config->optimize = args::getInteger(args, OPT_O, 1);
1579
  config->outputFile = args.getLastArgValue(OPT_o, "a.out");
1580
  config->finalOutput =
1581
      args.getLastArgValue(OPT_final_output, config->outputFile);
1582
  config->astPaths = args.getAllArgValues(OPT_add_ast_path);
1583
  config->headerPad = args::getHex(args, OPT_headerpad, /*Default=*/32);
1584
  config->headerPadMaxInstallNames =
1585
      args.hasArg(OPT_headerpad_max_install_names);
1586
  config->printDylibSearch =
1587
      args.hasArg(OPT_print_dylib_search) || getenv("RC_TRACE_DYLIB_SEARCHING");
1588
  config->printEachFile = args.hasArg(OPT_t);
1589
  config->printWhyLoad = args.hasArg(OPT_why_load);
1590
  config->omitDebugInfo = args.hasArg(OPT_S);
1591
  config->errorForArchMismatch = args.hasArg(OPT_arch_errors_fatal);
1592
  if (const Arg *arg = args.getLastArg(OPT_bundle_loader)) {
1593
    if (config->outputType != MH_BUNDLE)
1594
      error("-bundle_loader can only be used with MachO bundle output");
1595
    addFile(arg->getValue(), LoadType::CommandLine, /*isLazy=*/false,
1596
            /*isExplicit=*/false, /*isBundleLoader=*/true);
1597
  }
1598
  for (auto *arg : args.filtered(OPT_dyld_env)) {
1599
    StringRef envPair(arg->getValue());
1600
    if (!envPair.contains('='))
1601
      error("-dyld_env's argument is  malformed. Expected "
1602
            "-dyld_env <ENV_VAR>=<VALUE>, got `" +
1603
            envPair + "`");
1604
    config->dyldEnvs.push_back(envPair);
1605
  }
1606
  if (!config->dyldEnvs.empty() && config->outputType != MH_EXECUTE)
1607
    error("-dyld_env can only be used when creating executable output");
1608

1609
  if (const Arg *arg = args.getLastArg(OPT_umbrella)) {
1610
    if (config->outputType != MH_DYLIB)
1611
      warn("-umbrella used, but not creating dylib");
1612
    config->umbrella = arg->getValue();
1613
  }
1614
  config->ltoObjPath = args.getLastArgValue(OPT_object_path_lto);
1615
  config->thinLTOCacheDir = args.getLastArgValue(OPT_cache_path_lto);
1616
  config->thinLTOCachePolicy = getLTOCachePolicy(args);
1617
  config->thinLTOEmitImportsFiles = args.hasArg(OPT_thinlto_emit_imports_files);
1618
  config->thinLTOEmitIndexFiles = args.hasArg(OPT_thinlto_emit_index_files) ||
1619
                                  args.hasArg(OPT_thinlto_index_only) ||
1620
                                  args.hasArg(OPT_thinlto_index_only_eq);
1621
  config->thinLTOIndexOnly = args.hasArg(OPT_thinlto_index_only) ||
1622
                             args.hasArg(OPT_thinlto_index_only_eq);
1623
  config->thinLTOIndexOnlyArg = args.getLastArgValue(OPT_thinlto_index_only_eq);
1624
  config->thinLTOObjectSuffixReplace =
1625
      getOldNewOptions(args, OPT_thinlto_object_suffix_replace_eq);
1626
  std::tie(config->thinLTOPrefixReplaceOld, config->thinLTOPrefixReplaceNew,
1627
           config->thinLTOPrefixReplaceNativeObject) =
1628
      getOldNewOptionsExtra(args, OPT_thinlto_prefix_replace_eq);
1629
  if (config->thinLTOEmitIndexFiles && !config->thinLTOIndexOnly) {
1630
    if (args.hasArg(OPT_thinlto_object_suffix_replace_eq))
1631
      error("--thinlto-object-suffix-replace is not supported with "
1632
            "--thinlto-emit-index-files");
1633
    else if (args.hasArg(OPT_thinlto_prefix_replace_eq))
1634
      error("--thinlto-prefix-replace is not supported with "
1635
            "--thinlto-emit-index-files");
1636
  }
1637
  if (!config->thinLTOPrefixReplaceNativeObject.empty() &&
1638
      config->thinLTOIndexOnlyArg.empty()) {
1639
    error("--thinlto-prefix-replace=old_dir;new_dir;obj_dir must be used with "
1640
          "--thinlto-index-only=");
1641
  }
1642
  config->runtimePaths = args::getStrings(args, OPT_rpath);
1643
  config->allLoad = args.hasFlag(OPT_all_load, OPT_noall_load, false);
1644
  config->archMultiple = args.hasArg(OPT_arch_multiple);
1645
  config->applicationExtension = args.hasFlag(
1646
      OPT_application_extension, OPT_no_application_extension, false);
1647
  config->exportDynamic = args.hasArg(OPT_export_dynamic);
1648
  config->forceLoadObjC = args.hasArg(OPT_ObjC);
1649
  config->forceLoadSwift = args.hasArg(OPT_force_load_swift_libs);
1650
  config->deadStripDylibs = args.hasArg(OPT_dead_strip_dylibs);
1651
  config->demangle = args.hasArg(OPT_demangle);
1652
  config->implicitDylibs = !args.hasArg(OPT_no_implicit_dylibs);
1653
  config->emitFunctionStarts =
1654
      args.hasFlag(OPT_function_starts, OPT_no_function_starts, true);
1655
  config->emitDataInCodeInfo =
1656
      args.hasFlag(OPT_data_in_code_info, OPT_no_data_in_code_info, true);
1657
  config->emitChainedFixups = shouldEmitChainedFixups(args);
1658
  config->emitInitOffsets =
1659
      config->emitChainedFixups || args.hasArg(OPT_init_offsets);
1660
  config->emitRelativeMethodLists = shouldEmitRelativeMethodLists(args);
1661
  config->icfLevel = getICFLevel(args);
1662
  config->keepICFStabs = args.hasArg(OPT_keep_icf_stabs);
1663
  config->dedupStrings =
1664
      args.hasFlag(OPT_deduplicate_strings, OPT_no_deduplicate_strings, true);
1665
  config->deadStripDuplicates = args.hasArg(OPT_dead_strip_duplicates);
1666
  config->warnDylibInstallName = args.hasFlag(
1667
      OPT_warn_dylib_install_name, OPT_no_warn_dylib_install_name, false);
1668
  config->ignoreOptimizationHints = args.hasArg(OPT_ignore_optimization_hints);
1669
  config->callGraphProfileSort = args.hasFlag(
1670
      OPT_call_graph_profile_sort, OPT_no_call_graph_profile_sort, true);
1671
  config->printSymbolOrder = args.getLastArgValue(OPT_print_symbol_order_eq);
1672
  config->forceExactCpuSubtypeMatch =
1673
      getenv("LD_DYLIB_CPU_SUBTYPES_MUST_MATCH");
1674
  config->objcStubsMode = getObjCStubsMode(args);
1675
  config->ignoreAutoLink = args.hasArg(OPT_ignore_auto_link);
1676
  for (const Arg *arg : args.filtered(OPT_ignore_auto_link_option))
1677
    config->ignoreAutoLinkOptions.insert(arg->getValue());
1678
  config->strictAutoLink = args.hasArg(OPT_strict_auto_link);
1679
  config->ltoDebugPassManager = args.hasArg(OPT_lto_debug_pass_manager);
1680
  config->csProfileGenerate = args.hasArg(OPT_cs_profile_generate);
1681
  config->csProfilePath = args.getLastArgValue(OPT_cs_profile_path);
1682
  config->pgoWarnMismatch =
1683
      args.hasFlag(OPT_pgo_warn_mismatch, OPT_no_pgo_warn_mismatch, true);
1684
  config->generateUuid = !args.hasArg(OPT_no_uuid);
1685

1686
  for (const Arg *arg : args.filtered(OPT_alias)) {
1687
    config->aliasedSymbols.push_back(
1688
        std::make_pair(arg->getValue(0), arg->getValue(1)));
1689
  }
1690

1691
  if (const char *zero = getenv("ZERO_AR_DATE"))
1692
    config->zeroModTime = strcmp(zero, "0") != 0;
1693
  if (args.getLastArg(OPT_reproducible))
1694
    config->zeroModTime = true;
1695

1696
  std::array<PlatformType, 4> encryptablePlatforms{
1697
      PLATFORM_IOS, PLATFORM_WATCHOS, PLATFORM_TVOS, PLATFORM_XROS};
1698
  config->emitEncryptionInfo =
1699
      args.hasFlag(OPT_encryptable, OPT_no_encryption,
1700
                   is_contained(encryptablePlatforms, config->platform()));
1701

1702
  if (const Arg *arg = args.getLastArg(OPT_install_name)) {
1703
    if (config->warnDylibInstallName && config->outputType != MH_DYLIB)
1704
      warn(
1705
          arg->getAsString(args) +
1706
          ": ignored, only has effect with -dylib [--warn-dylib-install-name]");
1707
    else
1708
      config->installName = arg->getValue();
1709
  } else if (config->outputType == MH_DYLIB) {
1710
    config->installName = config->finalOutput;
1711
  }
1712

1713
  if (args.hasArg(OPT_mark_dead_strippable_dylib)) {
1714
    if (config->outputType != MH_DYLIB)
1715
      warn("-mark_dead_strippable_dylib: ignored, only has effect with -dylib");
1716
    else
1717
      config->markDeadStrippableDylib = true;
1718
  }
1719

1720
  if (const Arg *arg = args.getLastArg(OPT_static, OPT_dynamic))
1721
    config->staticLink = (arg->getOption().getID() == OPT_static);
1722

1723
  if (const Arg *arg =
1724
          args.getLastArg(OPT_flat_namespace, OPT_twolevel_namespace))
1725
    config->namespaceKind = arg->getOption().getID() == OPT_twolevel_namespace
1726
                                ? NamespaceKind::twolevel
1727
                                : NamespaceKind::flat;
1728

1729
  config->undefinedSymbolTreatment = getUndefinedSymbolTreatment(args);
1730

1731
  if (config->outputType == MH_EXECUTE)
1732
    config->entry = symtab->addUndefined(args.getLastArgValue(OPT_e, "_main"),
1733
                                         /*file=*/nullptr,
1734
                                         /*isWeakRef=*/false);
1735

1736
  config->librarySearchPaths =
1737
      getLibrarySearchPaths(args, config->systemLibraryRoots);
1738
  config->frameworkSearchPaths =
1739
      getFrameworkSearchPaths(args, config->systemLibraryRoots);
1740
  if (const Arg *arg =
1741
          args.getLastArg(OPT_search_paths_first, OPT_search_dylibs_first))
1742
    config->searchDylibsFirst =
1743
        arg->getOption().getID() == OPT_search_dylibs_first;
1744

1745
  config->dylibCompatibilityVersion =
1746
      parseDylibVersion(args, OPT_compatibility_version);
1747
  config->dylibCurrentVersion = parseDylibVersion(args, OPT_current_version);
1748

1749
  config->dataConst =
1750
      args.hasFlag(OPT_data_const, OPT_no_data_const, dataConstDefault(args));
1751
  // Populate config->sectionRenameMap with builtin default renames.
1752
  // Options -rename_section and -rename_segment are able to override.
1753
  initializeSectionRenameMap();
1754
  // Reject every special character except '.' and '$'
1755
  // TODO(gkm): verify that this is the proper set of invalid chars
1756
  StringRef invalidNameChars("!\"#%&'()*+,-/:;<=>?@[\\]^`{|}~");
1757
  auto validName = [invalidNameChars](StringRef s) {
1758
    if (s.find_first_of(invalidNameChars) != StringRef::npos)
1759
      error("invalid name for segment or section: " + s);
1760
    return s;
1761
  };
1762
  for (const Arg *arg : args.filtered(OPT_rename_section)) {
1763
    config->sectionRenameMap[{validName(arg->getValue(0)),
1764
                              validName(arg->getValue(1))}] = {
1765
        validName(arg->getValue(2)), validName(arg->getValue(3))};
1766
  }
1767
  for (const Arg *arg : args.filtered(OPT_rename_segment)) {
1768
    config->segmentRenameMap[validName(arg->getValue(0))] =
1769
        validName(arg->getValue(1));
1770
  }
1771

1772
  config->sectionAlignments = parseSectAlign(args);
1773

1774
  for (const Arg *arg : args.filtered(OPT_segprot)) {
1775
    StringRef segName = arg->getValue(0);
1776
    uint32_t maxProt = parseProtection(arg->getValue(1));
1777
    uint32_t initProt = parseProtection(arg->getValue(2));
1778
    if (maxProt != initProt && config->arch() != AK_i386)
1779
      error("invalid argument '" + arg->getAsString(args) +
1780
            "': max and init must be the same for non-i386 archs");
1781
    if (segName == segment_names::linkEdit)
1782
      error("-segprot cannot be used to change __LINKEDIT's protections");
1783
    config->segmentProtections.push_back({segName, maxProt, initProt});
1784
  }
1785

1786
  config->hasExplicitExports =
1787
      args.hasArg(OPT_no_exported_symbols) ||
1788
      args.hasArgNoClaim(OPT_exported_symbol, OPT_exported_symbols_list);
1789
  handleSymbolPatterns(args, config->exportedSymbols, OPT_exported_symbol,
1790
                       OPT_exported_symbols_list);
1791
  handleSymbolPatterns(args, config->unexportedSymbols, OPT_unexported_symbol,
1792
                       OPT_unexported_symbols_list);
1793
  if (config->hasExplicitExports && !config->unexportedSymbols.empty())
1794
    error("cannot use both -exported_symbol* and -unexported_symbol* options");
1795

1796
  if (args.hasArg(OPT_no_exported_symbols) && !config->exportedSymbols.empty())
1797
    error("cannot use both -exported_symbol* and -no_exported_symbols options");
1798

1799
  // Imitating LD64's:
1800
  // -non_global_symbols_no_strip_list and -non_global_symbols_strip_list can't
1801
  // both be present.
1802
  // But -x can be used with either of these two, in which case, the last arg
1803
  // takes effect.
1804
  // (TODO: This is kind of confusing - considering disallowing using them
1805
  // together for a more straightforward behaviour)
1806
  {
1807
    bool includeLocal = false;
1808
    bool excludeLocal = false;
1809
    for (const Arg *arg :
1810
         args.filtered(OPT_x, OPT_non_global_symbols_no_strip_list,
1811
                       OPT_non_global_symbols_strip_list)) {
1812
      switch (arg->getOption().getID()) {
1813
      case OPT_x:
1814
        config->localSymbolsPresence = SymtabPresence::None;
1815
        break;
1816
      case OPT_non_global_symbols_no_strip_list:
1817
        if (excludeLocal) {
1818
          error("cannot use both -non_global_symbols_no_strip_list and "
1819
                "-non_global_symbols_strip_list");
1820
        } else {
1821
          includeLocal = true;
1822
          config->localSymbolsPresence = SymtabPresence::SelectivelyIncluded;
1823
          parseSymbolPatternsFile(arg, config->localSymbolPatterns);
1824
        }
1825
        break;
1826
      case OPT_non_global_symbols_strip_list:
1827
        if (includeLocal) {
1828
          error("cannot use both -non_global_symbols_no_strip_list and "
1829
                "-non_global_symbols_strip_list");
1830
        } else {
1831
          excludeLocal = true;
1832
          config->localSymbolsPresence = SymtabPresence::SelectivelyExcluded;
1833
          parseSymbolPatternsFile(arg, config->localSymbolPatterns);
1834
        }
1835
        break;
1836
      default:
1837
        llvm_unreachable("unexpected option");
1838
      }
1839
    }
1840
  }
1841
  // Explicitly-exported literal symbols must be defined, but might
1842
  // languish in an archive if unreferenced elsewhere or if they are in the
1843
  // non-global strip list. Light a fire under those lazy symbols!
1844
  for (const CachedHashStringRef &cachedName : config->exportedSymbols.literals)
1845
    symtab->addUndefined(cachedName.val(), /*file=*/nullptr,
1846
                         /*isWeakRef=*/false);
1847

1848
  for (const Arg *arg : args.filtered(OPT_why_live))
1849
    config->whyLive.insert(arg->getValue());
1850
  if (!config->whyLive.empty() && !config->deadStrip)
1851
    warn("-why_live has no effect without -dead_strip, ignoring");
1852

1853
  config->saveTemps = args.hasArg(OPT_save_temps);
1854

1855
  config->adhocCodesign = args.hasFlag(
1856
      OPT_adhoc_codesign, OPT_no_adhoc_codesign,
1857
      shouldAdhocSignByDefault(config->arch(), config->platform()));
1858

1859
  if (args.hasArg(OPT_v)) {
1860
    message(getLLDVersion(), lld::errs());
1861
    message(StringRef("Library search paths:") +
1862
                (config->librarySearchPaths.empty()
1863
                     ? ""
1864
                     : "\n\t" + join(config->librarySearchPaths, "\n\t")),
1865
            lld::errs());
1866
    message(StringRef("Framework search paths:") +
1867
                (config->frameworkSearchPaths.empty()
1868
                     ? ""
1869
                     : "\n\t" + join(config->frameworkSearchPaths, "\n\t")),
1870
            lld::errs());
1871
  }
1872

1873
  config->progName = argsArr[0];
1874

1875
  config->timeTraceEnabled = args.hasArg(OPT_time_trace_eq);
1876
  config->timeTraceGranularity =
1877
      args::getInteger(args, OPT_time_trace_granularity_eq, 500);
1878

1879
  // Initialize time trace profiler.
1880
  if (config->timeTraceEnabled)
1881
    timeTraceProfilerInitialize(config->timeTraceGranularity, config->progName);
1882

1883
  {
1884
    TimeTraceScope timeScope("ExecuteLinker");
1885

1886
    initLLVM(); // must be run before any call to addFile()
1887
    createFiles(args);
1888

1889
    // Now that all dylibs have been loaded, search for those that should be
1890
    // re-exported.
1891
    {
1892
      auto reexportHandler = [](const Arg *arg,
1893
                                const std::vector<StringRef> &extensions) {
1894
        config->hasReexports = true;
1895
        StringRef searchName = arg->getValue();
1896
        if (!markReexport(searchName, extensions))
1897
          error(arg->getSpelling() + " " + searchName +
1898
                " does not match a supplied dylib");
1899
      };
1900
      std::vector<StringRef> extensions = {".tbd"};
1901
      for (const Arg *arg : args.filtered(OPT_sub_umbrella))
1902
        reexportHandler(arg, extensions);
1903

1904
      extensions.push_back(".dylib");
1905
      for (const Arg *arg : args.filtered(OPT_sub_library))
1906
        reexportHandler(arg, extensions);
1907
    }
1908

1909
    cl::ResetAllOptionOccurrences();
1910

1911
    // Parse LTO options.
1912
    if (const Arg *arg = args.getLastArg(OPT_mcpu))
1913
      parseClangOption(saver().save("-mcpu=" + StringRef(arg->getValue())),
1914
                       arg->getSpelling());
1915

1916
    for (const Arg *arg : args.filtered(OPT_mllvm)) {
1917
      parseClangOption(arg->getValue(), arg->getSpelling());
1918
      config->mllvmOpts.emplace_back(arg->getValue());
1919
    }
1920

1921
    createSyntheticSections();
1922
    createSyntheticSymbols();
1923
    addSynthenticMethnames();
1924

1925
    createAliases();
1926
    // If we are in "explicit exports" mode, hide everything that isn't
1927
    // explicitly exported. Do this before running LTO so that LTO can better
1928
    // optimize.
1929
    handleExplicitExports();
1930

1931
    bool didCompileBitcodeFiles = compileBitcodeFiles();
1932

1933
    resolveLCLinkerOptions();
1934

1935
    // If --thinlto-index-only is given, we should create only "index
1936
    // files" and not object files. Index file creation is already done
1937
    // in compileBitcodeFiles, so we are done if that's the case.
1938
    if (config->thinLTOIndexOnly)
1939
      return errorCount() == 0;
1940

1941
    // LTO may emit a non-hidden (extern) object file symbol even if the
1942
    // corresponding bitcode symbol is hidden. In particular, this happens for
1943
    // cross-module references to hidden symbols under ThinLTO. Thus, if we
1944
    // compiled any bitcode files, we must redo the symbol hiding.
1945
    if (didCompileBitcodeFiles)
1946
      handleExplicitExports();
1947
    replaceCommonSymbols();
1948

1949
    StringRef orderFile = args.getLastArgValue(OPT_order_file);
1950
    if (!orderFile.empty())
1951
      priorityBuilder.parseOrderFile(orderFile);
1952

1953
    referenceStubBinder();
1954

1955
    // FIXME: should terminate the link early based on errors encountered so
1956
    // far?
1957

1958
    for (const Arg *arg : args.filtered(OPT_sectcreate)) {
1959
      StringRef segName = arg->getValue(0);
1960
      StringRef sectName = arg->getValue(1);
1961
      StringRef fileName = arg->getValue(2);
1962
      std::optional<MemoryBufferRef> buffer = readFile(fileName);
1963
      if (buffer)
1964
        inputFiles.insert(make<OpaqueFile>(*buffer, segName, sectName));
1965
    }
1966

1967
    for (const Arg *arg : args.filtered(OPT_add_empty_section)) {
1968
      StringRef segName = arg->getValue(0);
1969
      StringRef sectName = arg->getValue(1);
1970
      inputFiles.insert(make<OpaqueFile>(MemoryBufferRef(), segName, sectName));
1971
    }
1972

1973
    gatherInputSections();
1974
    if (config->callGraphProfileSort)
1975
      priorityBuilder.extractCallGraphProfile();
1976

1977
    if (config->deadStrip)
1978
      markLive();
1979

1980
    // Ensure that no symbols point inside __mod_init_func sections if they are
1981
    // removed due to -init_offsets. This must run after dead stripping.
1982
    if (config->emitInitOffsets)
1983
      eraseInitializerSymbols();
1984

1985
    // Categories are not subject to dead-strip. The __objc_catlist section is
1986
    // marked as NO_DEAD_STRIP and that propagates into all category data.
1987
    if (args.hasArg(OPT_check_category_conflicts))
1988
      objc::checkCategories();
1989

1990
    // Category merging uses "->live = false" to erase old category data, so
1991
    // it has to run after dead-stripping (markLive).
1992
    if (args.hasArg(OPT_objc_category_merging, OPT_no_objc_category_merging))
1993
      objc::mergeCategories();
1994

1995
    // ICF assumes that all literals have been folded already, so we must run
1996
    // foldIdenticalLiterals before foldIdenticalSections.
1997
    foldIdenticalLiterals();
1998
    if (config->icfLevel != ICFLevel::none) {
1999
      if (config->icfLevel == ICFLevel::safe)
2000
        markAddrSigSymbols();
2001
      foldIdenticalSections(/*onlyCfStrings=*/false);
2002
    } else if (config->dedupStrings) {
2003
      foldIdenticalSections(/*onlyCfStrings=*/true);
2004
    }
2005

2006
    // Write to an output file.
2007
    if (target->wordSize == 8)
2008
      writeResult<LP64>();
2009
    else
2010
      writeResult<ILP32>();
2011

2012
    depTracker->write(getLLDVersion(), inputFiles, config->outputFile);
2013
  }
2014

2015
  if (config->timeTraceEnabled) {
2016
    checkError(timeTraceProfilerWrite(
2017
        args.getLastArgValue(OPT_time_trace_eq).str(), config->outputFile));
2018

2019
    timeTraceProfilerCleanup();
2020
  }
2021

2022
  if (errorCount() != 0 || config->strictAutoLink)
2023
    for (const auto &warning : missingAutolinkWarnings)
2024
      warn(warning);
2025

2026
  return errorCount() == 0;
2027
}
2028
} // namespace macho
2029
} // namespace lld
2030

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.