comparison lld/MachO/Driver.cpp @ 207:2e18cbf3894f

LLVM12
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 08 Jun 2021 06:07:14 +0900
parents 0572611fdcc8
children 5f17cb93ff66
comparison
equal deleted inserted replaced
173:0572611fdcc8 207:2e18cbf3894f
7 //===----------------------------------------------------------------------===// 7 //===----------------------------------------------------------------------===//
8 8
9 #include "Driver.h" 9 #include "Driver.h"
10 #include "Config.h" 10 #include "Config.h"
11 #include "InputFiles.h" 11 #include "InputFiles.h"
12 #include "LTO.h"
13 #include "MarkLive.h"
14 #include "ObjC.h"
12 #include "OutputSection.h" 15 #include "OutputSection.h"
13 #include "OutputSegment.h" 16 #include "OutputSegment.h"
14 #include "SymbolTable.h" 17 #include "SymbolTable.h"
15 #include "Symbols.h" 18 #include "Symbols.h"
19 #include "SyntheticSections.h"
16 #include "Target.h" 20 #include "Target.h"
17 #include "Writer.h" 21 #include "Writer.h"
18 22
19 #include "lld/Common/Args.h" 23 #include "lld/Common/Args.h"
20 #include "lld/Common/Driver.h" 24 #include "lld/Common/Driver.h"
21 #include "lld/Common/ErrorHandler.h" 25 #include "lld/Common/ErrorHandler.h"
22 #include "lld/Common/LLVM.h" 26 #include "lld/Common/LLVM.h"
23 #include "lld/Common/Memory.h" 27 #include "lld/Common/Memory.h"
28 #include "lld/Common/Reproduce.h"
24 #include "lld/Common/Version.h" 29 #include "lld/Common/Version.h"
25 #include "llvm/ADT/DenseSet.h" 30 #include "llvm/ADT/DenseSet.h"
26 #include "llvm/ADT/StringExtras.h" 31 #include "llvm/ADT/StringExtras.h"
27 #include "llvm/ADT/StringRef.h" 32 #include "llvm/ADT/StringRef.h"
28 #include "llvm/BinaryFormat/MachO.h" 33 #include "llvm/BinaryFormat/MachO.h"
29 #include "llvm/BinaryFormat/Magic.h" 34 #include "llvm/BinaryFormat/Magic.h"
35 #include "llvm/Config/llvm-config.h"
36 #include "llvm/LTO/LTO.h"
30 #include "llvm/Object/Archive.h" 37 #include "llvm/Object/Archive.h"
31 #include "llvm/Option/ArgList.h" 38 #include "llvm/Option/ArgList.h"
32 #include "llvm/Option/Option.h" 39 #include "llvm/Support/CommandLine.h"
40 #include "llvm/Support/FileSystem.h"
41 #include "llvm/Support/Host.h"
33 #include "llvm/Support/MemoryBuffer.h" 42 #include "llvm/Support/MemoryBuffer.h"
43 #include "llvm/Support/Parallel.h"
34 #include "llvm/Support/Path.h" 44 #include "llvm/Support/Path.h"
45 #include "llvm/Support/TarWriter.h"
46 #include "llvm/Support/TargetSelect.h"
47 #include "llvm/Support/TimeProfiler.h"
48 #include "llvm/TextAPI/PackedVersion.h"
49
50 #include <algorithm>
35 51
36 using namespace llvm; 52 using namespace llvm;
37 using namespace llvm::MachO; 53 using namespace llvm::MachO;
54 using namespace llvm::object;
55 using namespace llvm::opt;
38 using namespace llvm::sys; 56 using namespace llvm::sys;
39 using namespace lld; 57 using namespace lld;
40 using namespace lld::macho; 58 using namespace lld::macho;
41 59
42 Configuration *lld::macho::config; 60 Configuration *macho::config;
43 61 DependencyTracker *macho::depTracker;
44 // Create prefix string literals used in Options.td 62
45 #define PREFIX(NAME, VALUE) const char *NAME[] = VALUE; 63 static HeaderFileType getOutputType(const InputArgList &args) {
46 #include "Options.inc" 64 // TODO: -r, -dylinker, -preload...
47 #undef PREFIX 65 Arg *outputArg = args.getLastArg(OPT_bundle, OPT_dylib, OPT_execute);
48 66 if (outputArg == nullptr)
49 // Create table mapping all options defined in Options.td 67 return MH_EXECUTE;
50 static const opt::OptTable::Info optInfo[] = { 68
51 #define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X7, X8, X9, X10, X11, X12) \ 69 switch (outputArg->getOption().getID()) {
52 {X1, X2, X10, X11, OPT_##ID, opt::Option::KIND##Class, \ 70 case OPT_bundle:
53 X9, X8, OPT_##GROUP, OPT_##ALIAS, X7, X12}, 71 return MH_BUNDLE;
54 #include "Options.inc" 72 case OPT_dylib:
55 #undef OPTION 73 return MH_DYLIB;
74 case OPT_execute:
75 return MH_EXECUTE;
76 default:
77 llvm_unreachable("internal error");
78 }
79 }
80
81 static Optional<StringRef> findLibrary(StringRef name) {
82 if (config->searchDylibsFirst) {
83 if (Optional<StringRef> path = findPathCombination(
84 "lib" + name, config->librarySearchPaths, {".tbd", ".dylib"}))
85 return path;
86 return findPathCombination("lib" + name, config->librarySearchPaths,
87 {".a"});
88 }
89 return findPathCombination("lib" + name, config->librarySearchPaths,
90 {".tbd", ".dylib", ".a"});
91 }
92
93 static Optional<std::string> findFramework(StringRef name) {
94 SmallString<260> symlink;
95 StringRef suffix;
96 std::tie(name, suffix) = name.split(",");
97 for (StringRef dir : config->frameworkSearchPaths) {
98 symlink = dir;
99 path::append(symlink, name + ".framework", name);
100
101 if (!suffix.empty()) {
102 // NOTE: we must resolve the symlink before trying the suffixes, because
103 // there are no symlinks for the suffixed paths.
104 SmallString<260> location;
105 if (!fs::real_path(symlink, location)) {
106 // only append suffix if realpath() succeeds
107 Twine suffixed = location + suffix;
108 if (fs::exists(suffixed))
109 return suffixed.str();
110 }
111 // Suffix lookup failed, fall through to the no-suffix case.
112 }
113
114 if (Optional<std::string> path = resolveDylibPath(symlink))
115 return path;
116 }
117 return {};
118 }
119
120 static bool warnIfNotDirectory(StringRef option, StringRef path) {
121 if (!fs::exists(path)) {
122 warn("directory not found for option -" + option + path);
123 return false;
124 } else if (!fs::is_directory(path)) {
125 warn("option -" + option + path + " references a non-directory path");
126 return false;
127 }
128 return true;
129 }
130
131 static std::vector<StringRef>
132 getSearchPaths(unsigned optionCode, InputArgList &args,
133 const std::vector<StringRef> &roots,
134 const SmallVector<StringRef, 2> &systemPaths) {
135 std::vector<StringRef> paths;
136 StringRef optionLetter{optionCode == OPT_F ? "F" : "L"};
137 for (StringRef path : args::getStrings(args, optionCode)) {
138 // NOTE: only absolute paths are re-rooted to syslibroot(s)
139 bool found = false;
140 if (path::is_absolute(path, path::Style::posix)) {
141 for (StringRef root : roots) {
142 SmallString<261> buffer(root);
143 path::append(buffer, path);
144 // Do not warn about paths that are computed via the syslib roots
145 if (fs::is_directory(buffer)) {
146 paths.push_back(saver.save(buffer.str()));
147 found = true;
148 }
149 }
150 }
151 if (!found && warnIfNotDirectory(optionLetter, path))
152 paths.push_back(path);
153 }
154
155 // `-Z` suppresses the standard "system" search paths.
156 if (args.hasArg(OPT_Z))
157 return paths;
158
159 for (const StringRef &path : systemPaths) {
160 for (const StringRef &root : roots) {
161 SmallString<261> buffer(root);
162 path::append(buffer, path);
163 if (fs::is_directory(buffer))
164 paths.push_back(saver.save(buffer.str()));
165 }
166 }
167 return paths;
168 }
169
170 static std::vector<StringRef> getSystemLibraryRoots(InputArgList &args) {
171 std::vector<StringRef> roots;
172 for (const Arg *arg : args.filtered(OPT_syslibroot))
173 roots.push_back(arg->getValue());
174 // NOTE: the final `-syslibroot` being `/` will ignore all roots
175 if (roots.size() && roots.back() == "/")
176 roots.clear();
177 // NOTE: roots can never be empty - add an empty root to simplify the library
178 // and framework search path computation.
179 if (roots.empty())
180 roots.emplace_back("");
181 return roots;
182 }
183
184 static std::vector<StringRef>
185 getLibrarySearchPaths(InputArgList &args, const std::vector<StringRef> &roots) {
186 return getSearchPaths(OPT_L, args, roots, {"/usr/lib", "/usr/local/lib"});
187 }
188
189 static std::vector<StringRef>
190 getFrameworkSearchPaths(InputArgList &args,
191 const std::vector<StringRef> &roots) {
192 return getSearchPaths(OPT_F, args, roots,
193 {"/Library/Frameworks", "/System/Library/Frameworks"});
194 }
195
196 namespace {
197 struct ArchiveMember {
198 MemoryBufferRef mbref;
199 uint32_t modTime;
56 }; 200 };
57 201 } // namespace
58 MachOOptTable::MachOOptTable() : OptTable(optInfo) {} 202
59 203 // Returns slices of MB by parsing MB as an archive file.
60 opt::InputArgList MachOOptTable::parse(ArrayRef<const char *> argv) { 204 // Each slice consists of a member file in the archive.
61 // Make InputArgList from string vectors. 205 static std::vector<ArchiveMember> getArchiveMembers(MemoryBufferRef mb) {
62 unsigned missingIndex; 206 std::unique_ptr<Archive> file =
63 unsigned missingCount; 207 CHECK(Archive::create(mb),
64 SmallVector<const char *, 256> vec(argv.data(), argv.data() + argv.size()); 208 mb.getBufferIdentifier() + ": failed to parse archive");
65 209 Archive *archive = file.get();
66 opt::InputArgList args = ParseArgs(vec, missingIndex, missingCount); 210 make<std::unique_ptr<Archive>>(std::move(file)); // take ownership
67 211
212 std::vector<ArchiveMember> v;
213 Error err = Error::success();
214
215 // Thin archives refer to .o files, so --reproduce needs the .o files too.
216 bool addToTar = archive->isThin() && tar;
217
218 for (const Archive::Child &c : archive->children(err)) {
219 MemoryBufferRef mbref =
220 CHECK(c.getMemoryBufferRef(),
221 mb.getBufferIdentifier() +
222 ": could not get the buffer for a child of the archive");
223 if (addToTar)
224 tar->append(relativeToRoot(check(c.getFullName())), mbref.getBuffer());
225 uint32_t modTime = toTimeT(
226 CHECK(c.getLastModified(), mb.getBufferIdentifier() +
227 ": could not get the modification "
228 "time for a child of the archive"));
229 v.push_back({mbref, modTime});
230 }
231 if (err)
232 fatal(mb.getBufferIdentifier() +
233 ": Archive::children failed: " + toString(std::move(err)));
234
235 return v;
236 }
237
238 static InputFile *addFile(StringRef path, bool forceLoadArchive,
239 bool isExplicit = true,
240 bool isBundleLoader = false) {
241 Optional<MemoryBufferRef> buffer = readFile(path);
242 if (!buffer)
243 return nullptr;
244 MemoryBufferRef mbref = *buffer;
245 InputFile *newFile = nullptr;
246
247 file_magic magic = identify_magic(mbref.getBuffer());
248 switch (magic) {
249 case file_magic::archive: {
250 std::unique_ptr<object::Archive> file = CHECK(
251 object::Archive::create(mbref), path + ": failed to parse archive");
252
253 if (!file->isEmpty() && !file->hasSymbolTable())
254 error(path + ": archive has no index; run ranlib to add one");
255
256 if (config->allLoad || forceLoadArchive) {
257 if (Optional<MemoryBufferRef> buffer = readFile(path)) {
258 for (const ArchiveMember &member : getArchiveMembers(*buffer)) {
259 if (Optional<InputFile *> file = loadArchiveMember(
260 member.mbref, member.modTime, path, /*objCOnly=*/false)) {
261 inputFiles.insert(*file);
262 printArchiveMemberLoad(
263 (forceLoadArchive ? "-force_load" : "-all_load"),
264 inputFiles.back());
265 }
266 }
267 }
268 } else if (config->forceLoadObjC) {
269 for (const object::Archive::Symbol &sym : file->symbols())
270 if (sym.getName().startswith(objc::klass))
271 symtab->addUndefined(sym.getName(), /*file=*/nullptr,
272 /*isWeakRef=*/false);
273
274 // TODO: no need to look for ObjC sections for a given archive member if
275 // we already found that it contains an ObjC symbol. We should also
276 // consider creating a LazyObjFile class in order to avoid double-loading
277 // these files here and below (as part of the ArchiveFile).
278 if (Optional<MemoryBufferRef> buffer = readFile(path)) {
279 for (const ArchiveMember &member : getArchiveMembers(*buffer)) {
280 if (Optional<InputFile *> file = loadArchiveMember(
281 member.mbref, member.modTime, path, /*objCOnly=*/true)) {
282 inputFiles.insert(*file);
283 printArchiveMemberLoad("-ObjC", inputFiles.back());
284 }
285 }
286 }
287 }
288
289 newFile = make<ArchiveFile>(std::move(file));
290 break;
291 }
292 case file_magic::macho_object:
293 newFile = make<ObjFile>(mbref, getModTime(path), "");
294 break;
295 case file_magic::macho_dynamically_linked_shared_lib:
296 case file_magic::macho_dynamically_linked_shared_lib_stub:
297 case file_magic::tapi_file:
298 if (DylibFile * dylibFile = loadDylib(mbref)) {
299 if (isExplicit)
300 dylibFile->explicitlyLinked = true;
301 newFile = dylibFile;
302 }
303 break;
304 case file_magic::bitcode:
305 newFile = make<BitcodeFile>(mbref);
306 break;
307 case file_magic::macho_executable:
308 case file_magic::macho_bundle:
309 // We only allow executable and bundle type here if it is used
310 // as a bundle loader.
311 if (!isBundleLoader)
312 error(path + ": unhandled file type");
313 if (DylibFile *dylibFile = loadDylib(mbref, nullptr, isBundleLoader))
314 newFile = dylibFile;
315 break;
316 default:
317 error(path + ": unhandled file type");
318 }
319 if (newFile) {
320 // printArchiveMemberLoad() prints both .a and .o names, so no need to
321 // print the .a name here.
322 if (config->printEachFile && magic != file_magic::archive &&
323 !isa<DylibFile>(newFile))
324 message(toString(newFile));
325 inputFiles.insert(newFile);
326 }
327 return newFile;
328 }
329
330 static void addLibrary(StringRef name, bool isNeeded, bool isWeak,
331 bool isReexport, bool isExplicit) {
332 if (Optional<StringRef> path = findLibrary(name)) {
333 if (auto *dylibFile = dyn_cast_or_null<DylibFile>(
334 addFile(*path, /*forceLoadArchive=*/false, isExplicit))) {
335 if (isNeeded)
336 dylibFile->forceNeeded = true;
337 if (isWeak)
338 dylibFile->forceWeakImport = true;
339 if (isReexport) {
340 config->hasReexports = true;
341 dylibFile->reexport = true;
342 }
343 }
344 return;
345 }
346 error("library not found for -l" + name);
347 }
348
349 static void addFramework(StringRef name, bool isNeeded, bool isWeak,
350 bool isReexport, bool isExplicit) {
351 if (Optional<std::string> path = findFramework(name)) {
352 if (auto *dylibFile = dyn_cast_or_null<DylibFile>(
353 addFile(*path, /*forceLoadArchive=*/false, isExplicit))) {
354 if (isNeeded)
355 dylibFile->forceNeeded = true;
356 if (isWeak)
357 dylibFile->forceWeakImport = true;
358 if (isReexport) {
359 config->hasReexports = true;
360 dylibFile->reexport = true;
361 }
362 }
363 return;
364 }
365 error("framework not found for -framework " + name);
366 }
367
368 // Parses LC_LINKER_OPTION contents, which can add additional command line
369 // flags.
370 void macho::parseLCLinkerOption(InputFile *f, unsigned argc, StringRef data) {
371 SmallVector<const char *, 4> argv;
372 size_t offset = 0;
373 for (unsigned i = 0; i < argc && offset < data.size(); ++i) {
374 argv.push_back(data.data() + offset);
375 offset += strlen(data.data() + offset) + 1;
376 }
377 if (argv.size() != argc || offset > data.size())
378 fatal(toString(f) + ": invalid LC_LINKER_OPTION");
379
380 MachOOptTable table;
381 unsigned missingIndex, missingCount;
382 InputArgList args = table.ParseArgs(argv, missingIndex, missingCount);
68 if (missingCount) 383 if (missingCount)
69 error(Twine(args.getArgString(missingIndex)) + ": missing argument"); 384 fatal(Twine(args.getArgString(missingIndex)) + ": missing argument");
70 385 for (const Arg *arg : args.filtered(OPT_UNKNOWN))
71 for (opt::Arg *arg : args.filtered(OPT_UNKNOWN)) 386 error("unknown argument: " + arg->getAsString(args));
72 error("unknown argument: " + arg->getSpelling()); 387
73 return args; 388 for (const Arg *arg : args) {
74 } 389 switch (arg->getOption().getID()) {
75 390 case OPT_l:
76 // This is for -lfoo. We'll look for libfoo.dylib from search paths. 391 addLibrary(arg->getValue(), /*isNeeded=*/false, /*isWeak=*/false,
77 static Optional<std::string> findDylib(StringRef name) { 392 /*isReexport=*/false, /*isExplicit=*/false);
78 for (StringRef dir : config->searchPaths) { 393 break;
79 std::string path = (dir + "/lib" + name + ".dylib").str(); 394 case OPT_framework:
80 if (fs::exists(path)) 395 addFramework(arg->getValue(), /*isNeeded=*/false, /*isWeak=*/false,
81 return path; 396 /*isReexport=*/false, /*isExplicit=*/false);
82 } 397 break;
83 error("library not found for -l" + name); 398 default:
84 return None; 399 error(arg->getSpelling() + " is not allowed in LC_LINKER_OPTION");
85 } 400 }
86 401 }
87 static TargetInfo *createTargetInfo(opt::InputArgList &args) { 402 }
88 StringRef s = args.getLastArgValue(OPT_arch, "x86_64"); 403
89 if (s != "x86_64") 404 static void addFileList(StringRef path) {
90 error("missing or unsupported -arch " + s);
91 return createX86_64TargetInfo();
92 }
93
94 static std::vector<StringRef> getSearchPaths(opt::InputArgList &args) {
95 std::vector<StringRef> ret{args::getStrings(args, OPT_L)};
96 if (!args.hasArg(OPT_Z)) {
97 ret.push_back("/usr/lib");
98 ret.push_back("/usr/local/lib");
99 }
100 return ret;
101 }
102
103 static void addFile(StringRef path) {
104 Optional<MemoryBufferRef> buffer = readFile(path); 405 Optional<MemoryBufferRef> buffer = readFile(path);
105 if (!buffer) 406 if (!buffer)
106 return; 407 return;
107 MemoryBufferRef mbref = *buffer; 408 MemoryBufferRef mbref = *buffer;
108 409 for (StringRef path : args::getLines(mbref))
109 switch (identify_magic(mbref.getBuffer())) { 410 addFile(rerootPath(path), /*forceLoadArchive=*/false);
110 case file_magic::archive: {
111 std::unique_ptr<object::Archive> file = CHECK(
112 object::Archive::create(mbref), path + ": failed to parse archive");
113
114 if (!file->isEmpty() && !file->hasSymbolTable())
115 error(path + ": archive has no index; run ranlib to add one");
116
117 inputFiles.push_back(make<ArchiveFile>(std::move(file)));
118 break;
119 }
120 case file_magic::macho_object:
121 inputFiles.push_back(make<ObjFile>(mbref));
122 break;
123 case file_magic::macho_dynamically_linked_shared_lib:
124 inputFiles.push_back(make<DylibFile>(mbref));
125 break;
126 default:
127 error(path + ": unhandled file type");
128 }
129 }
130
131 static std::array<StringRef, 6> archNames{"arm", "arm64", "i386",
132 "x86_64", "ppc", "ppc64"};
133 static bool isArchString(StringRef s) {
134 static DenseSet<StringRef> archNamesSet(archNames.begin(), archNames.end());
135 return archNamesSet.find(s) != archNamesSet.end();
136 } 411 }
137 412
138 // An order file has one entry per line, in the following format: 413 // An order file has one entry per line, in the following format:
139 // 414 //
140 // <arch>:<object file>:<symbol name> 415 // <cpu>:<object file>:<symbol name>
141 // 416 //
142 // <arch> and <object file> are optional. If not specified, then that entry 417 // <cpu> and <object file> are optional. If not specified, then that entry
143 // matches any symbol of that name. 418 // matches any symbol of that name. Parsing this format is not quite
419 // straightforward because the symbol name itself can contain colons, so when
420 // encountering a colon, we consider the preceding characters to decide if it
421 // can be a valid CPU type or file path.
144 // 422 //
145 // If a symbol is matched by multiple entries, then it takes the lowest-ordered 423 // If a symbol is matched by multiple entries, then it takes the lowest-ordered
146 // entry (the one nearest to the front of the list.) 424 // entry (the one nearest to the front of the list.)
147 // 425 //
148 // The file can also have line comments that start with '#'. 426 // The file can also have line comments that start with '#'.
149 void parseOrderFile(StringRef path) { 427 static void parseOrderFile(StringRef path) {
150 Optional<MemoryBufferRef> buffer = readFile(path); 428 Optional<MemoryBufferRef> buffer = readFile(path);
151 if (!buffer) { 429 if (!buffer) {
152 error("Could not read order file at " + path); 430 error("Could not read order file at " + path);
153 return; 431 return;
154 } 432 }
155 433
156 MemoryBufferRef mbref = *buffer; 434 MemoryBufferRef mbref = *buffer;
157 size_t priority = std::numeric_limits<size_t>::max(); 435 size_t priority = std::numeric_limits<size_t>::max();
158 for (StringRef rest : args::getLines(mbref)) { 436 for (StringRef line : args::getLines(mbref)) {
159 StringRef arch, objectFile, symbol; 437 StringRef objectFile, symbol;
160 438 line = line.take_until([](char c) { return c == '#'; }); // ignore comments
161 std::array<StringRef, 3> fields; 439 line = line.ltrim();
162 uint8_t fieldCount = 0; 440
163 while (rest != "" && fieldCount < 3) { 441 CPUType cpuType = StringSwitch<CPUType>(line)
164 std::pair<StringRef, StringRef> p = getToken(rest, ": \t\n\v\f\r"); 442 .StartsWith("i386:", CPU_TYPE_I386)
165 StringRef tok = p.first; 443 .StartsWith("x86_64:", CPU_TYPE_X86_64)
166 rest = p.second; 444 .StartsWith("arm:", CPU_TYPE_ARM)
167 445 .StartsWith("arm64:", CPU_TYPE_ARM64)
168 // Check if we have a comment 446 .StartsWith("ppc:", CPU_TYPE_POWERPC)
169 if (tok == "" || tok[0] == '#') 447 .StartsWith("ppc64:", CPU_TYPE_POWERPC64)
448 .Default(CPU_TYPE_ANY);
449
450 if (cpuType != CPU_TYPE_ANY && cpuType != target->cpuType)
451 continue;
452
453 // Drop the CPU type as well as the colon
454 if (cpuType != CPU_TYPE_ANY)
455 line = line.drop_until([](char c) { return c == ':'; }).drop_front();
456
457 constexpr std::array<StringRef, 2> fileEnds = {".o:", ".o):"};
458 for (StringRef fileEnd : fileEnds) {
459 size_t pos = line.find(fileEnd);
460 if (pos != StringRef::npos) {
461 // Split the string around the colon
462 objectFile = line.take_front(pos + fileEnd.size() - 1);
463 line = line.drop_front(pos + fileEnd.size());
170 break; 464 break;
171
172 fields[fieldCount++] = tok;
173 }
174
175 switch (fieldCount) {
176 case 3:
177 arch = fields[0];
178 objectFile = fields[1];
179 symbol = fields[2];
180 break;
181 case 2:
182 (isArchString(fields[0]) ? arch : objectFile) = fields[0];
183 symbol = fields[1];
184 break;
185 case 1:
186 symbol = fields[0];
187 break;
188 case 0:
189 break;
190 default:
191 llvm_unreachable("too many fields in order file");
192 }
193
194 if (!arch.empty()) {
195 if (!isArchString(arch)) {
196 error("invalid arch \"" + arch + "\" in order file: expected one of " +
197 llvm::join(archNames, ", "));
198 continue;
199 } 465 }
200 466 }
201 // TODO: Update when we extend support for other archs 467 symbol = line.trim();
202 if (arch != "x86_64")
203 continue;
204 }
205
206 if (!objectFile.empty() && !objectFile.endswith(".o")) {
207 error("invalid object file name \"" + objectFile +
208 "\" in order file: should end with .o");
209 continue;
210 }
211 468
212 if (!symbol.empty()) { 469 if (!symbol.empty()) {
213 SymbolPriorityEntry &entry = config->priorities[symbol]; 470 SymbolPriorityEntry &entry = config->priorities[symbol];
214 if (!objectFile.empty()) 471 if (!objectFile.empty())
215 entry.objectFiles.insert(std::make_pair(objectFile, priority)); 472 entry.objectFiles.insert(std::make_pair(objectFile, priority));
220 --priority; 477 --priority;
221 } 478 }
222 } 479 }
223 480
224 // We expect sub-library names of the form "libfoo", which will match a dylib 481 // We expect sub-library names of the form "libfoo", which will match a dylib
225 // with a path of .*/libfoo.dylib. 482 // with a path of .*/libfoo.{dylib, tbd}.
226 static bool markSubLibrary(StringRef searchName) { 483 // XXX ld64 seems to ignore the extension entirely when matching sub-libraries;
484 // I'm not sure what the use case for that is.
485 static bool markReexport(StringRef searchName, ArrayRef<StringRef> extensions) {
227 for (InputFile *file : inputFiles) { 486 for (InputFile *file : inputFiles) {
228 if (auto *dylibFile = dyn_cast<DylibFile>(file)) { 487 if (auto *dylibFile = dyn_cast<DylibFile>(file)) {
229 StringRef filename = path::filename(dylibFile->getName()); 488 StringRef filename = path::filename(dylibFile->getName());
230 if (filename.consume_front(searchName) && filename == ".dylib") { 489 if (filename.consume_front(searchName) &&
490 (filename.empty() ||
491 find(extensions, filename) != extensions.end())) {
231 dylibFile->reexport = true; 492 dylibFile->reexport = true;
232 return true; 493 return true;
233 } 494 }
234 } 495 }
235 } 496 }
236 return false; 497 return false;
237 } 498 }
238 499
239 static void handlePlatformVersion(opt::ArgList::iterator &it, 500 // This function is called on startup. We need this for LTO since
240 const opt::ArgList::iterator &end) { 501 // LTO calls LLVM functions to compile bitcode files to native code.
241 // -platform_version takes 3 args, which LLVM's option library doesn't 502 // Technically this can be delayed until we read bitcode files, but
242 // support directly. So this explicitly handles that. 503 // we don't bother to do lazily because the initialization is fast.
243 // FIXME: stash skipped args for later use. 504 static void initLLVM() {
244 for (int i = 0; i < 3; ++i) { 505 InitializeAllTargets();
245 ++it; 506 InitializeAllTargetMCs();
246 if (it == end || (*it)->getOption().getID() != OPT_INPUT) 507 InitializeAllAsmPrinters();
247 fatal("usage: -platform_version platform min_version sdk_version"); 508 InitializeAllAsmParsers();
248 } 509 }
249 } 510
250 511 static void compileBitcodeFiles() {
251 bool macho::link(llvm::ArrayRef<const char *> argsArr, bool canExitEarly, 512 TimeTraceScope timeScope("LTO");
513 auto *lto = make<BitcodeCompiler>();
514 for (InputFile *file : inputFiles)
515 if (auto *bitcodeFile = dyn_cast<BitcodeFile>(file))
516 lto->add(*bitcodeFile);
517
518 for (ObjFile *file : lto->compile())
519 inputFiles.insert(file);
520 }
521
522 // Replaces common symbols with defined symbols residing in __common sections.
523 // This function must be called after all symbol names are resolved (i.e. after
524 // all InputFiles have been loaded.) As a result, later operations won't see
525 // any CommonSymbols.
526 static void replaceCommonSymbols() {
527 TimeTraceScope timeScope("Replace common symbols");
528 for (Symbol *sym : symtab->getSymbols()) {
529 auto *common = dyn_cast<CommonSymbol>(sym);
530 if (common == nullptr)
531 continue;
532
533 auto *isec = make<InputSection>();
534 isec->file = common->getFile();
535 isec->name = section_names::common;
536 isec->segname = segment_names::data;
537 isec->align = common->align;
538 // Casting to size_t will truncate large values on 32-bit architectures,
539 // but it's not really worth supporting the linking of 64-bit programs on
540 // 32-bit archs.
541 isec->data = {nullptr, static_cast<size_t>(common->size)};
542 isec->flags = S_ZEROFILL;
543 inputSections.push_back(isec);
544
545 // FIXME: CommonSymbol should store isReferencedDynamically, noDeadStrip
546 // and pass them on here.
547 replaceSymbol<Defined>(sym, sym->getName(), isec->file, isec, /*value=*/0,
548 /*size=*/0,
549 /*isWeakDef=*/false,
550 /*isExternal=*/true, common->privateExtern,
551 /*isThumb=*/false,
552 /*isReferencedDynamically=*/false,
553 /*noDeadStrip=*/false);
554 }
555 }
556
557 static void initializeSectionRenameMap() {
558 if (config->dataConst) {
559 SmallVector<StringRef> v{section_names::got,
560 section_names::authGot,
561 section_names::authPtr,
562 section_names::nonLazySymbolPtr,
563 section_names::const_,
564 section_names::cfString,
565 section_names::moduleInitFunc,
566 section_names::moduleTermFunc,
567 section_names::objcClassList,
568 section_names::objcNonLazyClassList,
569 section_names::objcCatList,
570 section_names::objcNonLazyCatList,
571 section_names::objcProtoList,
572 section_names::objcImageInfo};
573 for (StringRef s : v)
574 config->sectionRenameMap[{segment_names::data, s}] = {
575 segment_names::dataConst, s};
576 }
577 config->sectionRenameMap[{segment_names::text, section_names::staticInit}] = {
578 segment_names::text, section_names::text};
579 config->sectionRenameMap[{segment_names::import, section_names::pointers}] = {
580 config->dataConst ? segment_names::dataConst : segment_names::data,
581 section_names::nonLazySymbolPtr};
582 }
583
584 static inline char toLowerDash(char x) {
585 if (x >= 'A' && x <= 'Z')
586 return x - 'A' + 'a';
587 else if (x == ' ')
588 return '-';
589 return x;
590 }
591
592 static std::string lowerDash(StringRef s) {
593 return std::string(map_iterator(s.begin(), toLowerDash),
594 map_iterator(s.end(), toLowerDash));
595 }
596
597 // Has the side-effect of setting Config::platformInfo.
598 static PlatformKind parsePlatformVersion(const ArgList &args) {
599 const Arg *arg = args.getLastArg(OPT_platform_version);
600 if (!arg) {
601 error("must specify -platform_version");
602 return PlatformKind::unknown;
603 }
604
605 StringRef platformStr = arg->getValue(0);
606 StringRef minVersionStr = arg->getValue(1);
607 StringRef sdkVersionStr = arg->getValue(2);
608
609 // TODO(compnerd) see if we can generate this case list via XMACROS
610 PlatformKind platform =
611 StringSwitch<PlatformKind>(lowerDash(platformStr))
612 .Cases("macos", "1", PlatformKind::macOS)
613 .Cases("ios", "2", PlatformKind::iOS)
614 .Cases("tvos", "3", PlatformKind::tvOS)
615 .Cases("watchos", "4", PlatformKind::watchOS)
616 .Cases("bridgeos", "5", PlatformKind::bridgeOS)
617 .Cases("mac-catalyst", "6", PlatformKind::macCatalyst)
618 .Cases("ios-simulator", "7", PlatformKind::iOSSimulator)
619 .Cases("tvos-simulator", "8", PlatformKind::tvOSSimulator)
620 .Cases("watchos-simulator", "9", PlatformKind::watchOSSimulator)
621 .Cases("driverkit", "10", PlatformKind::driverKit)
622 .Default(PlatformKind::unknown);
623 if (platform == PlatformKind::unknown)
624 error(Twine("malformed platform: ") + platformStr);
625 // TODO: check validity of version strings, which varies by platform
626 // NOTE: ld64 accepts version strings with 5 components
627 // llvm::VersionTuple accepts no more than 4 components
628 // Has Apple ever published version strings with 5 components?
629 if (config->platformInfo.minimum.tryParse(minVersionStr))
630 error(Twine("malformed minimum version: ") + minVersionStr);
631 if (config->platformInfo.sdk.tryParse(sdkVersionStr))
632 error(Twine("malformed sdk version: ") + sdkVersionStr);
633 return platform;
634 }
635
636 // Has the side-effect of setting Config::target.
637 static TargetInfo *createTargetInfo(InputArgList &args) {
638 StringRef archName = args.getLastArgValue(OPT_arch);
639 if (archName.empty())
640 fatal("must specify -arch");
641 PlatformKind platform = parsePlatformVersion(args);
642
643 config->platformInfo.target =
644 MachO::Target(getArchitectureFromName(archName), platform);
645
646 uint32_t cpuType;
647 uint32_t cpuSubtype;
648 std::tie(cpuType, cpuSubtype) = getCPUTypeFromArchitecture(config->arch());
649
650 switch (cpuType) {
651 case CPU_TYPE_X86_64:
652 return createX86_64TargetInfo();
653 case CPU_TYPE_ARM64:
654 return createARM64TargetInfo();
655 case CPU_TYPE_ARM64_32:
656 return createARM64_32TargetInfo();
657 case CPU_TYPE_ARM:
658 return createARMTargetInfo(cpuSubtype);
659 default:
660 fatal("missing or unsupported -arch " + archName);
661 }
662 }
663
664 static UndefinedSymbolTreatment
665 getUndefinedSymbolTreatment(const ArgList &args) {
666 StringRef treatmentStr = args.getLastArgValue(OPT_undefined);
667 auto treatment =
668 StringSwitch<UndefinedSymbolTreatment>(treatmentStr)
669 .Cases("error", "", UndefinedSymbolTreatment::error)
670 .Case("warning", UndefinedSymbolTreatment::warning)
671 .Case("suppress", UndefinedSymbolTreatment::suppress)
672 .Case("dynamic_lookup", UndefinedSymbolTreatment::dynamic_lookup)
673 .Default(UndefinedSymbolTreatment::unknown);
674 if (treatment == UndefinedSymbolTreatment::unknown) {
675 warn(Twine("unknown -undefined TREATMENT '") + treatmentStr +
676 "', defaulting to 'error'");
677 treatment = UndefinedSymbolTreatment::error;
678 } else if (config->namespaceKind == NamespaceKind::twolevel &&
679 (treatment == UndefinedSymbolTreatment::warning ||
680 treatment == UndefinedSymbolTreatment::suppress)) {
681 if (treatment == UndefinedSymbolTreatment::warning)
682 error("'-undefined warning' only valid with '-flat_namespace'");
683 else
684 error("'-undefined suppress' only valid with '-flat_namespace'");
685 treatment = UndefinedSymbolTreatment::error;
686 }
687 return treatment;
688 }
689
690 static void warnIfDeprecatedOption(const Option &opt) {
691 if (!opt.getGroup().isValid())
692 return;
693 if (opt.getGroup().getID() == OPT_grp_deprecated) {
694 warn("Option `" + opt.getPrefixedName() + "' is deprecated in ld64:");
695 warn(opt.getHelpText());
696 }
697 }
698
699 static void warnIfUnimplementedOption(const Option &opt) {
700 if (!opt.getGroup().isValid() || !opt.hasFlag(DriverFlag::HelpHidden))
701 return;
702 switch (opt.getGroup().getID()) {
703 case OPT_grp_deprecated:
704 // warn about deprecated options elsewhere
705 break;
706 case OPT_grp_undocumented:
707 warn("Option `" + opt.getPrefixedName() +
708 "' is undocumented. Should lld implement it?");
709 break;
710 case OPT_grp_obsolete:
711 warn("Option `" + opt.getPrefixedName() +
712 "' is obsolete. Please modernize your usage.");
713 break;
714 case OPT_grp_ignored:
715 warn("Option `" + opt.getPrefixedName() + "' is ignored.");
716 break;
717 default:
718 warn("Option `" + opt.getPrefixedName() +
719 "' is not yet implemented. Stay tuned...");
720 break;
721 }
722 }
723
724 static const char *getReproduceOption(InputArgList &args) {
725 if (const Arg *arg = args.getLastArg(OPT_reproduce))
726 return arg->getValue();
727 return getenv("LLD_REPRODUCE");
728 }
729
730 static void parseClangOption(StringRef opt, const Twine &msg) {
731 std::string err;
732 raw_string_ostream os(err);
733
734 const char *argv[] = {"lld", opt.data()};
735 if (cl::ParseCommandLineOptions(2, argv, "", &os))
736 return;
737 os.flush();
738 error(msg + ": " + StringRef(err).trim());
739 }
740
741 static uint32_t parseDylibVersion(const ArgList &args, unsigned id) {
742 const Arg *arg = args.getLastArg(id);
743 if (!arg)
744 return 0;
745
746 if (config->outputType != MH_DYLIB) {
747 error(arg->getAsString(args) + ": only valid with -dylib");
748 return 0;
749 }
750
751 PackedVersion version;
752 if (!version.parse32(arg->getValue())) {
753 error(arg->getAsString(args) + ": malformed version");
754 return 0;
755 }
756
757 return version.rawValue();
758 }
759
760 static uint32_t parseProtection(StringRef protStr) {
761 uint32_t prot = 0;
762 for (char c : protStr) {
763 switch (c) {
764 case 'r':
765 prot |= VM_PROT_READ;
766 break;
767 case 'w':
768 prot |= VM_PROT_WRITE;
769 break;
770 case 'x':
771 prot |= VM_PROT_EXECUTE;
772 break;
773 case '-':
774 break;
775 default:
776 error("unknown -segprot letter '" + Twine(c) + "' in " + protStr);
777 return 0;
778 }
779 }
780 return prot;
781 }
782
783 static std::vector<SectionAlign> parseSectAlign(const opt::InputArgList &args) {
784 std::vector<SectionAlign> sectAligns;
785 for (const Arg *arg : args.filtered(OPT_sectalign)) {
786 StringRef segName = arg->getValue(0);
787 StringRef sectName = arg->getValue(1);
788 StringRef alignStr = arg->getValue(2);
789 if (alignStr.startswith("0x") || alignStr.startswith("0X"))
790 alignStr = alignStr.drop_front(2);
791 uint32_t align;
792 if (alignStr.getAsInteger(16, align)) {
793 error("-sectalign: failed to parse '" + StringRef(arg->getValue(2)) +
794 "' as number");
795 continue;
796 }
797 if (!isPowerOf2_32(align)) {
798 error("-sectalign: '" + StringRef(arg->getValue(2)) +
799 "' (in base 16) not a power of two");
800 continue;
801 }
802 sectAligns.push_back({segName, sectName, align});
803 }
804 return sectAligns;
805 }
806
807 static bool dataConstDefault(const InputArgList &args) {
808 switch (config->outputType) {
809 case MH_EXECUTE:
810 return !args.hasArg(OPT_no_pie);
811 case MH_BUNDLE:
812 // FIXME: return false when -final_name ...
813 // has prefix "/System/Library/UserEventPlugins/"
814 // or matches "/usr/libexec/locationd" "/usr/libexec/terminusd"
815 return true;
816 case MH_DYLIB:
817 return true;
818 case MH_OBJECT:
819 return false;
820 default:
821 llvm_unreachable(
822 "unsupported output type for determining data-const default");
823 }
824 return false;
825 }
826
827 void SymbolPatterns::clear() {
828 literals.clear();
829 globs.clear();
830 }
831
832 void SymbolPatterns::insert(StringRef symbolName) {
833 if (symbolName.find_first_of("*?[]") == StringRef::npos)
834 literals.insert(CachedHashStringRef(symbolName));
835 else if (Expected<GlobPattern> pattern = GlobPattern::create(symbolName))
836 globs.emplace_back(*pattern);
837 else
838 error("invalid symbol-name pattern: " + symbolName);
839 }
840
841 bool SymbolPatterns::matchLiteral(StringRef symbolName) const {
842 return literals.contains(CachedHashStringRef(symbolName));
843 }
844
845 bool SymbolPatterns::matchGlob(StringRef symbolName) const {
846 for (const llvm::GlobPattern &glob : globs)
847 if (glob.match(symbolName))
848 return true;
849 return false;
850 }
851
852 bool SymbolPatterns::match(StringRef symbolName) const {
853 return matchLiteral(symbolName) || matchGlob(symbolName);
854 }
855
856 static void handleSymbolPatterns(InputArgList &args,
857 SymbolPatterns &symbolPatterns,
858 unsigned singleOptionCode,
859 unsigned listFileOptionCode) {
860 for (const Arg *arg : args.filtered(singleOptionCode))
861 symbolPatterns.insert(arg->getValue());
862 for (const Arg *arg : args.filtered(listFileOptionCode)) {
863 StringRef path = arg->getValue();
864 Optional<MemoryBufferRef> buffer = readFile(path);
865 if (!buffer) {
866 error("Could not read symbol file: " + path);
867 continue;
868 }
869 MemoryBufferRef mbref = *buffer;
870 for (StringRef line : args::getLines(mbref)) {
871 line = line.take_until([](char c) { return c == '#'; }).trim();
872 if (!line.empty())
873 symbolPatterns.insert(line);
874 }
875 }
876 }
877
878 void createFiles(const InputArgList &args) {
879 TimeTraceScope timeScope("Load input files");
880 // This loop should be reserved for options whose exact ordering matters.
881 // Other options should be handled via filtered() and/or getLastArg().
882 for (const Arg *arg : args) {
883 const Option &opt = arg->getOption();
884 warnIfDeprecatedOption(opt);
885 warnIfUnimplementedOption(opt);
886
887 switch (opt.getID()) {
888 case OPT_INPUT:
889 addFile(rerootPath(arg->getValue()), /*forceLoadArchive=*/false);
890 break;
891 case OPT_needed_library:
892 if (auto *dylibFile = dyn_cast_or_null<DylibFile>(
893 addFile(rerootPath(arg->getValue()), false)))
894 dylibFile->forceNeeded = true;
895 break;
896 case OPT_reexport_library:
897 if (auto *dylibFile = dyn_cast_or_null<DylibFile>(addFile(
898 rerootPath(arg->getValue()), /*forceLoadArchive=*/false))) {
899 config->hasReexports = true;
900 dylibFile->reexport = true;
901 }
902 break;
903 case OPT_weak_library:
904 if (auto *dylibFile = dyn_cast_or_null<DylibFile>(
905 addFile(rerootPath(arg->getValue()), /*forceLoadArchive=*/false)))
906 dylibFile->forceWeakImport = true;
907 break;
908 case OPT_filelist:
909 addFileList(arg->getValue());
910 break;
911 case OPT_force_load:
912 addFile(rerootPath(arg->getValue()), /*forceLoadArchive=*/true);
913 break;
914 case OPT_l:
915 case OPT_needed_l:
916 case OPT_reexport_l:
917 case OPT_weak_l:
918 addLibrary(arg->getValue(), opt.getID() == OPT_needed_l,
919 opt.getID() == OPT_weak_l, opt.getID() == OPT_reexport_l,
920 /*isExplicit=*/true);
921 break;
922 case OPT_framework:
923 case OPT_needed_framework:
924 case OPT_reexport_framework:
925 case OPT_weak_framework:
926 addFramework(arg->getValue(), opt.getID() == OPT_needed_framework,
927 opt.getID() == OPT_weak_framework,
928 opt.getID() == OPT_reexport_framework, /*isExplicit=*/true);
929 break;
930 default:
931 break;
932 }
933 }
934 }
935
936 bool macho::link(ArrayRef<const char *> argsArr, bool canExitEarly,
252 raw_ostream &stdoutOS, raw_ostream &stderrOS) { 937 raw_ostream &stdoutOS, raw_ostream &stderrOS) {
253 lld::stdoutOS = &stdoutOS; 938 lld::stdoutOS = &stdoutOS;
254 lld::stderrOS = &stderrOS; 939 lld::stderrOS = &stderrOS;
255 940
941 errorHandler().cleanupCallback = []() { freeArena(); };
942
943 errorHandler().logName = args::getFilenameWithoutExe(argsArr[0]);
256 stderrOS.enable_colors(stderrOS.has_colors()); 944 stderrOS.enable_colors(stderrOS.has_colors());
257 // TODO: Set up error handler properly, e.g. the errorLimitExceededMsg
258 945
259 MachOOptTable parser; 946 MachOOptTable parser;
260 opt::InputArgList args = parser.parse(argsArr.slice(1)); 947 InputArgList args = parser.parse(argsArr.slice(1));
948
949 errorHandler().errorLimitExceededMsg =
950 "too many errors emitted, stopping now "
951 "(use --error-limit=0 to see all errors)";
952 errorHandler().errorLimit = args::getInteger(args, OPT_error_limit_eq, 20);
953 errorHandler().verbose = args.hasArg(OPT_verbose);
954
955 if (args.hasArg(OPT_help_hidden)) {
956 parser.printHelp(argsArr[0], /*showHidden=*/true);
957 return true;
958 }
959 if (args.hasArg(OPT_help)) {
960 parser.printHelp(argsArr[0], /*showHidden=*/false);
961 return true;
962 }
963 if (args.hasArg(OPT_version)) {
964 message(getLLDVersion());
965 return true;
966 }
261 967
262 config = make<Configuration>(); 968 config = make<Configuration>();
263 symtab = make<SymbolTable>(); 969 symtab = make<SymbolTable>();
264 target = createTargetInfo(args); 970 target = createTargetInfo(args);
265 971 depTracker =
266 config->entry = symtab->addUndefined(args.getLastArgValue(OPT_e, "_main")); 972 make<DependencyTracker>(args.getLastArgValue(OPT_dependency_info));
973
974 // Must be set before any InputSections and Symbols are created.
975 config->deadStrip = args.hasArg(OPT_dead_strip);
976
977 config->systemLibraryRoots = getSystemLibraryRoots(args);
978 if (const char *path = getReproduceOption(args)) {
979 // Note that --reproduce is a debug option so you can ignore it
980 // if you are trying to understand the whole picture of the code.
981 Expected<std::unique_ptr<TarWriter>> errOrWriter =
982 TarWriter::create(path, path::stem(path));
983 if (errOrWriter) {
984 tar = std::move(*errOrWriter);
985 tar->append("response.txt", createResponseFile(args));
986 tar->append("version.txt", getLLDVersion() + "\n");
987 } else {
988 error("--reproduce: " + toString(errOrWriter.takeError()));
989 }
990 }
991
992 if (auto *arg = args.getLastArg(OPT_threads_eq)) {
993 StringRef v(arg->getValue());
994 unsigned threads = 0;
995 if (!llvm::to_integer(v, threads, 0) || threads == 0)
996 error(arg->getSpelling() + ": expected a positive integer, but got '" +
997 arg->getValue() + "'");
998 parallel::strategy = hardware_concurrency(threads);
999 config->thinLTOJobs = v;
1000 }
1001 if (auto *arg = args.getLastArg(OPT_thinlto_jobs_eq))
1002 config->thinLTOJobs = arg->getValue();
1003 if (!get_threadpool_strategy(config->thinLTOJobs))
1004 error("--thinlto-jobs: invalid job count: " + config->thinLTOJobs);
1005
1006 for (const Arg *arg : args.filtered(OPT_u)) {
1007 config->explicitUndefineds.push_back(symtab->addUndefined(
1008 arg->getValue(), /*file=*/nullptr, /*isWeakRef=*/false));
1009 }
1010
1011 for (const Arg *arg : args.filtered(OPT_U))
1012 symtab->addDynamicLookup(arg->getValue());
1013
1014 config->mapFile = args.getLastArgValue(OPT_map);
267 config->outputFile = args.getLastArgValue(OPT_o, "a.out"); 1015 config->outputFile = args.getLastArgValue(OPT_o, "a.out");
268 config->installName = 1016 config->astPaths = args.getAllArgValues(OPT_add_ast_path);
269 args.getLastArgValue(OPT_install_name, config->outputFile); 1017 config->headerPad = args::getHex(args, OPT_headerpad, /*Default=*/32);
270 config->searchPaths = getSearchPaths(args); 1018 config->headerPadMaxInstallNames =
271 config->outputType = args.hasArg(OPT_dylib) ? MH_DYLIB : MH_EXECUTE; 1019 args.hasArg(OPT_headerpad_max_install_names);
1020 config->printEachFile = args.hasArg(OPT_t);
1021 config->printWhyLoad = args.hasArg(OPT_why_load);
1022 config->outputType = getOutputType(args);
1023 if (const Arg *arg = args.getLastArg(OPT_bundle_loader)) {
1024 if (config->outputType != MH_BUNDLE)
1025 error("-bundle_loader can only be used with MachO bundle output");
1026 addFile(arg->getValue(), /*forceLoadArchive=*/false, /*isExplicit=*/false,
1027 /*isBundleLoader=*/true);
1028 }
1029 config->ltoObjPath = args.getLastArgValue(OPT_object_path_lto);
1030 config->ltoNewPassManager =
1031 args.hasFlag(OPT_no_lto_legacy_pass_manager, OPT_lto_legacy_pass_manager,
1032 LLVM_ENABLE_NEW_PASS_MANAGER);
1033 config->runtimePaths = args::getStrings(args, OPT_rpath);
1034 config->allLoad = args.hasArg(OPT_all_load);
1035 config->forceLoadObjC = args.hasArg(OPT_ObjC);
1036 config->deadStripDylibs = args.hasArg(OPT_dead_strip_dylibs);
1037 config->demangle = args.hasArg(OPT_demangle);
1038 config->implicitDylibs = !args.hasArg(OPT_no_implicit_dylibs);
1039 config->emitFunctionStarts = !args.hasArg(OPT_no_function_starts);
1040 config->emitBitcodeBundle = args.hasArg(OPT_bitcode_bundle);
1041
1042 // FIXME: Add a commandline flag for this too.
1043 config->zeroModTime = getenv("ZERO_AR_DATE");
1044
1045 std::array<PlatformKind, 3> encryptablePlatforms{
1046 PlatformKind::iOS, PlatformKind::watchOS, PlatformKind::tvOS};
1047 config->emitEncryptionInfo =
1048 args.hasFlag(OPT_encryptable, OPT_no_encryption,
1049 is_contained(encryptablePlatforms, config->platform()));
1050
1051 #ifndef LLVM_HAVE_LIBXAR
1052 if (config->emitBitcodeBundle)
1053 error("-bitcode_bundle unsupported because LLD wasn't built with libxar");
1054 #endif
1055
1056 if (const Arg *arg = args.getLastArg(OPT_install_name)) {
1057 if (config->outputType != MH_DYLIB)
1058 warn(arg->getAsString(args) + ": ignored, only has effect with -dylib");
1059 else
1060 config->installName = arg->getValue();
1061 } else if (config->outputType == MH_DYLIB) {
1062 config->installName = config->outputFile;
1063 }
1064
1065 if (args.hasArg(OPT_mark_dead_strippable_dylib)) {
1066 if (config->outputType != MH_DYLIB)
1067 warn("-mark_dead_strippable_dylib: ignored, only has effect with -dylib");
1068 else
1069 config->markDeadStrippableDylib = true;
1070 }
1071
1072 if (const Arg *arg = args.getLastArg(OPT_static, OPT_dynamic))
1073 config->staticLink = (arg->getOption().getID() == OPT_static);
1074
1075 if (const Arg *arg =
1076 args.getLastArg(OPT_flat_namespace, OPT_twolevel_namespace))
1077 config->namespaceKind = arg->getOption().getID() == OPT_twolevel_namespace
1078 ? NamespaceKind::twolevel
1079 : NamespaceKind::flat;
1080
1081 config->undefinedSymbolTreatment = getUndefinedSymbolTreatment(args);
1082
1083 if (config->outputType == MH_EXECUTE)
1084 config->entry = symtab->addUndefined(args.getLastArgValue(OPT_e, "_main"),
1085 /*file=*/nullptr,
1086 /*isWeakRef=*/false);
1087
1088 config->librarySearchPaths =
1089 getLibrarySearchPaths(args, config->systemLibraryRoots);
1090 config->frameworkSearchPaths =
1091 getFrameworkSearchPaths(args, config->systemLibraryRoots);
1092 if (const Arg *arg =
1093 args.getLastArg(OPT_search_paths_first, OPT_search_dylibs_first))
1094 config->searchDylibsFirst =
1095 arg->getOption().getID() == OPT_search_dylibs_first;
1096
1097 config->dylibCompatibilityVersion =
1098 parseDylibVersion(args, OPT_compatibility_version);
1099 config->dylibCurrentVersion = parseDylibVersion(args, OPT_current_version);
1100
1101 config->dataConst =
1102 args.hasFlag(OPT_data_const, OPT_no_data_const, dataConstDefault(args));
1103 // Populate config->sectionRenameMap with builtin default renames.
1104 // Options -rename_section and -rename_segment are able to override.
1105 initializeSectionRenameMap();
1106 // Reject every special character except '.' and '$'
1107 // TODO(gkm): verify that this is the proper set of invalid chars
1108 StringRef invalidNameChars("!\"#%&'()*+,-/:;<=>?@[\\]^`{|}~");
1109 auto validName = [invalidNameChars](StringRef s) {
1110 if (s.find_first_of(invalidNameChars) != StringRef::npos)
1111 error("invalid name for segment or section: " + s);
1112 return s;
1113 };
1114 for (const Arg *arg : args.filtered(OPT_rename_section)) {
1115 config->sectionRenameMap[{validName(arg->getValue(0)),
1116 validName(arg->getValue(1))}] = {
1117 validName(arg->getValue(2)), validName(arg->getValue(3))};
1118 }
1119 for (const Arg *arg : args.filtered(OPT_rename_segment)) {
1120 config->segmentRenameMap[validName(arg->getValue(0))] =
1121 validName(arg->getValue(1));
1122 }
1123
1124 config->sectionAlignments = parseSectAlign(args);
1125
1126 for (const Arg *arg : args.filtered(OPT_segprot)) {
1127 StringRef segName = arg->getValue(0);
1128 uint32_t maxProt = parseProtection(arg->getValue(1));
1129 uint32_t initProt = parseProtection(arg->getValue(2));
1130 if (maxProt != initProt && config->arch() != AK_i386)
1131 error("invalid argument '" + arg->getAsString(args) +
1132 "': max and init must be the same for non-i386 archs");
1133 if (segName == segment_names::linkEdit)
1134 error("-segprot cannot be used to change __LINKEDIT's protections");
1135 config->segmentProtections.push_back({segName, maxProt, initProt});
1136 }
1137
1138 handleSymbolPatterns(args, config->exportedSymbols, OPT_exported_symbol,
1139 OPT_exported_symbols_list);
1140 handleSymbolPatterns(args, config->unexportedSymbols, OPT_unexported_symbol,
1141 OPT_unexported_symbols_list);
1142 if (!config->exportedSymbols.empty() && !config->unexportedSymbols.empty()) {
1143 error("cannot use both -exported_symbol* and -unexported_symbol* options\n"
1144 ">>> ignoring unexports");
1145 config->unexportedSymbols.clear();
1146 }
1147 // Explicitly-exported literal symbols must be defined, but might
1148 // languish in an archive if unreferenced elsewhere. Light a fire
1149 // under those lazy symbols!
1150 for (const CachedHashStringRef &cachedName : config->exportedSymbols.literals)
1151 symtab->addUndefined(cachedName.val(), /*file=*/nullptr,
1152 /*isWeakRef=*/false);
1153
1154 config->saveTemps = args.hasArg(OPT_save_temps);
1155
1156 config->adhocCodesign = args.hasFlag(
1157 OPT_adhoc_codesign, OPT_no_adhoc_codesign,
1158 (config->arch() == AK_arm64 || config->arch() == AK_arm64e) &&
1159 config->platform() == PlatformKind::macOS);
272 1160
273 if (args.hasArg(OPT_v)) { 1161 if (args.hasArg(OPT_v)) {
274 message(getLLDVersion()); 1162 message(getLLDVersion());
275 std::vector<StringRef> &searchPaths = config->searchPaths; 1163 message(StringRef("Library search paths:") +
276 message("Library search paths:\n" + 1164 (config->librarySearchPaths.empty()
277 llvm::join(searchPaths.begin(), searchPaths.end(), "\n")); 1165 ? ""
278 freeArena(); 1166 : "\n\t" + join(config->librarySearchPaths, "\n\t")));
279 return !errorCount(); 1167 message(StringRef("Framework search paths:") +
280 } 1168 (config->frameworkSearchPaths.empty()
281 1169 ? ""
282 for (opt::ArgList::iterator it = args.begin(), end = args.end(); it != end; 1170 : "\n\t" + join(config->frameworkSearchPaths, "\n\t")));
283 ++it) { 1171 }
284 const opt::Arg *arg = *it; 1172
285 switch (arg->getOption().getID()) { 1173 config->progName = argsArr[0];
286 case OPT_INPUT: 1174
287 addFile(arg->getValue()); 1175 config->timeTraceEnabled = args.hasArg(
288 break; 1176 OPT_time_trace, OPT_time_trace_granularity_eq, OPT_time_trace_file_eq);
289 case OPT_l: 1177 config->timeTraceGranularity =
290 if (Optional<std::string> path = findDylib(arg->getValue())) 1178 args::getInteger(args, OPT_time_trace_granularity_eq, 500);
291 addFile(*path); 1179
292 break; 1180 // Initialize time trace profiler.
293 case OPT_platform_version: { 1181 if (config->timeTraceEnabled)
294 handlePlatformVersion(it, end); // Can advance "it". 1182 timeTraceProfilerInitialize(config->timeTraceGranularity, config->progName);
295 break; 1183
296 } 1184 {
297 } 1185 TimeTraceScope timeScope("ExecuteLinker");
298 } 1186
299 1187 initLLVM(); // must be run before any call to addFile()
300 // Now that all dylibs have been loaded, search for those that should be 1188 createFiles(args);
301 // re-exported. 1189
302 for (opt::Arg *arg : args.filtered(OPT_sub_library)) { 1190 config->isPic = config->outputType == MH_DYLIB ||
303 config->hasReexports = true; 1191 config->outputType == MH_BUNDLE ||
304 StringRef searchName = arg->getValue(); 1192 (config->outputType == MH_EXECUTE &&
305 if (!markSubLibrary(searchName)) 1193 args.hasFlag(OPT_pie, OPT_no_pie, true));
306 error("-sub_library " + searchName + " does not match a supplied dylib"); 1194
307 } 1195 // Now that all dylibs have been loaded, search for those that should be
308 1196 // re-exported.
309 StringRef orderFile = args.getLastArgValue(OPT_order_file); 1197 {
310 if (!orderFile.empty()) 1198 auto reexportHandler = [](const Arg *arg,
311 parseOrderFile(orderFile); 1199 const std::vector<StringRef> &extensions) {
312 1200 config->hasReexports = true;
313 // dyld requires us to load libSystem. Since we may run tests on non-OSX 1201 StringRef searchName = arg->getValue();
314 // systems which do not have libSystem, we mock it out here. 1202 if (!markReexport(searchName, extensions))
315 // TODO: Replace this with a stub tbd file once we have TAPI support. 1203 error(arg->getSpelling() + " " + searchName +
316 if (StringRef(getenv("LLD_IN_TEST")) == "1" && 1204 " does not match a supplied dylib");
317 config->outputType == MH_EXECUTE) { 1205 };
318 inputFiles.push_back(DylibFile::createLibSystemMock()); 1206 std::vector<StringRef> extensions = {".tbd"};
319 } 1207 for (const Arg *arg : args.filtered(OPT_sub_umbrella))
320 1208 reexportHandler(arg, extensions);
321 if (config->outputType == MH_EXECUTE && !isa<Defined>(config->entry)) { 1209
322 error("undefined symbol: " + config->entry->getName()); 1210 extensions.push_back(".dylib");
323 return false; 1211 for (const Arg *arg : args.filtered(OPT_sub_library))
324 } 1212 reexportHandler(arg, extensions);
325 1213 }
326 createSyntheticSections(); 1214
327 1215 // Parse LTO options.
328 // Initialize InputSections. 1216 if (const Arg *arg = args.getLastArg(OPT_mcpu))
329 for (InputFile *file : inputFiles) { 1217 parseClangOption(saver.save("-mcpu=" + StringRef(arg->getValue())),
330 for (SubsectionMap &map : file->subsections) { 1218 arg->getSpelling());
331 for (auto &p : map) { 1219
332 InputSection *isec = p.second; 1220 for (const Arg *arg : args.filtered(OPT_mllvm))
333 inputSections.push_back(isec); 1221 parseClangOption(arg->getValue(), arg->getSpelling());
1222
1223 compileBitcodeFiles();
1224 replaceCommonSymbols();
1225
1226 StringRef orderFile = args.getLastArgValue(OPT_order_file);
1227 if (!orderFile.empty())
1228 parseOrderFile(orderFile);
1229
1230 if (config->entry)
1231 if (auto *undefined = dyn_cast<Undefined>(config->entry))
1232 treatUndefinedSymbol(*undefined, "the entry point");
1233
1234 // FIXME: This prints symbols that are undefined both in input files and
1235 // via -u flag twice.
1236 for (const Symbol *sym : config->explicitUndefineds) {
1237 if (const auto *undefined = dyn_cast<Undefined>(sym))
1238 treatUndefinedSymbol(*undefined, "-u");
1239 }
1240 // Literal exported-symbol names must be defined, but glob
1241 // patterns need not match.
1242 for (const CachedHashStringRef &cachedName :
1243 config->exportedSymbols.literals) {
1244 if (const Symbol *sym = symtab->find(cachedName))
1245 if (const auto *undefined = dyn_cast<Undefined>(sym))
1246 treatUndefinedSymbol(*undefined, "-exported_symbol(s_list)");
1247 }
1248
1249 // FIXME: should terminate the link early based on errors encountered so
1250 // far?
1251
1252 createSyntheticSections();
1253 createSyntheticSymbols();
1254
1255 if (!config->exportedSymbols.empty()) {
1256 for (Symbol *sym : symtab->getSymbols()) {
1257 if (auto *defined = dyn_cast<Defined>(sym)) {
1258 StringRef symbolName = defined->getName();
1259 if (config->exportedSymbols.match(symbolName)) {
1260 if (defined->privateExtern) {
1261 error("cannot export hidden symbol " + symbolName +
1262 "\n>>> defined in " + toString(defined->getFile()));
1263 }
1264 } else {
1265 defined->privateExtern = true;
1266 }
1267 }
334 } 1268 }
335 } 1269 } else if (!config->unexportedSymbols.empty()) {
336 } 1270 for (Symbol *sym : symtab->getSymbols())
337 1271 if (auto *defined = dyn_cast<Defined>(sym))
338 // Write to an output file. 1272 if (config->unexportedSymbols.match(defined->getName()))
339 writeResult(); 1273 defined->privateExtern = true;
1274 }
1275
1276 for (const Arg *arg : args.filtered(OPT_sectcreate)) {
1277 StringRef segName = arg->getValue(0);
1278 StringRef sectName = arg->getValue(1);
1279 StringRef fileName = arg->getValue(2);
1280 Optional<MemoryBufferRef> buffer = readFile(fileName);
1281 if (buffer)
1282 inputFiles.insert(make<OpaqueFile>(*buffer, segName, sectName));
1283 }
1284
1285 {
1286 TimeTraceScope timeScope("Gathering input sections");
1287 // Gather all InputSections into one vector.
1288 for (const InputFile *file : inputFiles) {
1289 for (const SubsectionMap &map : file->subsections)
1290 for (const SubsectionEntry &subsectionEntry : map)
1291 inputSections.push_back(subsectionEntry.isec);
1292 }
1293 }
1294
1295 if (config->deadStrip)
1296 markLive();
1297
1298 // Write to an output file.
1299 if (target->wordSize == 8)
1300 writeResult<LP64>();
1301 else
1302 writeResult<ILP32>();
1303
1304 depTracker->write(getLLDVersion(), inputFiles, config->outputFile);
1305 }
1306
1307 if (config->timeTraceEnabled) {
1308 if (auto E = timeTraceProfilerWrite(
1309 args.getLastArgValue(OPT_time_trace_file_eq).str(),
1310 config->outputFile)) {
1311 handleAllErrors(std::move(E),
1312 [&](const StringError &SE) { error(SE.getMessage()); });
1313 }
1314
1315 timeTraceProfilerCleanup();
1316 }
340 1317
341 if (canExitEarly) 1318 if (canExitEarly)
342 exitLld(errorCount() ? 1 : 0); 1319 exitLld(errorCount() ? 1 : 0);
343 1320
344 freeArena();
345 return !errorCount(); 1321 return !errorCount();
346 } 1322 }