xref: /llvm-project-15.0.7/lld/ELF/Driver.cpp (revision df04e79f)
1 //===- Driver.cpp ---------------------------------------------------------===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "Driver.h"
11 #include "Config.h"
12 #include "Error.h"
13 #include "ICF.h"
14 #include "InputFiles.h"
15 #include "InputSection.h"
16 #include "LinkerScript.h"
17 #include "SymbolListFile.h"
18 #include "SymbolTable.h"
19 #include "Target.h"
20 #include "Writer.h"
21 #include "lld/Driver/Driver.h"
22 #include "llvm/ADT/StringExtras.h"
23 #include "llvm/ADT/StringSwitch.h"
24 #include "llvm/Support/TargetSelect.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include <utility>
27 
28 using namespace llvm;
29 using namespace llvm::ELF;
30 using namespace llvm::object;
31 using namespace llvm::sys;
32 
33 using namespace lld;
34 using namespace lld::elf;
35 
36 Configuration *elf::Config;
37 LinkerDriver *elf::Driver;
38 
39 bool elf::link(ArrayRef<const char *> Args, raw_ostream &Error) {
40   HasError = false;
41   ErrorOS = &Error;
42 
43   Configuration C;
44   LinkerDriver D;
45   ScriptConfiguration SC;
46   Config = &C;
47   Driver = &D;
48   ScriptConfig = &SC;
49 
50   Driver->main(Args);
51   return !HasError;
52 }
53 
54 // Parses a linker -m option.
55 static std::pair<ELFKind, uint16_t> parseEmulation(StringRef S) {
56   if (S.endswith("_fbsd"))
57     S = S.drop_back(5);
58 
59   std::pair<ELFKind, uint16_t> Ret =
60       StringSwitch<std::pair<ELFKind, uint16_t>>(S)
61           .Case("aarch64linux", {ELF64LEKind, EM_AARCH64})
62           .Case("armelf_linux_eabi", {ELF32LEKind, EM_ARM})
63           .Case("elf32btsmip", {ELF32BEKind, EM_MIPS})
64           .Case("elf32ltsmip", {ELF32LEKind, EM_MIPS})
65           .Case("elf32ppc", {ELF32BEKind, EM_PPC})
66           .Case("elf64btsmip", {ELF64BEKind, EM_MIPS})
67           .Case("elf64ltsmip", {ELF64LEKind, EM_MIPS})
68           .Case("elf64ppc", {ELF64BEKind, EM_PPC64})
69           .Case("elf_i386", {ELF32LEKind, EM_386})
70           .Case("elf_x86_64", {ELF64LEKind, EM_X86_64})
71           .Default({ELFNoneKind, EM_NONE});
72 
73   if (Ret.first == ELFNoneKind) {
74     if (S == "i386pe" || S == "i386pep" || S == "thumb2pe")
75       error("Windows targets are not supported on the ELF frontend: " + S);
76     else
77       error("unknown emulation: " + S);
78   }
79   return Ret;
80 }
81 
82 // Returns slices of MB by parsing MB as an archive file.
83 // Each slice consists of a member file in the archive.
84 std::vector<MemoryBufferRef>
85 LinkerDriver::getArchiveMembers(MemoryBufferRef MB) {
86   std::unique_ptr<Archive> File =
87       check(Archive::create(MB), "failed to parse archive");
88 
89   std::vector<MemoryBufferRef> V;
90   for (const ErrorOr<Archive::Child> &COrErr : File->children()) {
91     Archive::Child C = check(COrErr, "could not get the child of the archive " +
92                                          File->getFileName());
93     MemoryBufferRef MBRef =
94         check(C.getMemoryBufferRef(),
95               "could not get the buffer for a child of the archive " +
96                   File->getFileName());
97     V.push_back(MBRef);
98   }
99 
100   // Take ownership of memory buffers created for members of thin archives.
101   for (std::unique_ptr<MemoryBuffer> &MB : File->takeThinBuffers())
102     OwningMBs.push_back(std::move(MB));
103 
104   return V;
105 }
106 
107 // Opens and parses a file. Path has to be resolved already.
108 // Newly created memory buffers are owned by this driver.
109 void LinkerDriver::addFile(StringRef Path) {
110   using namespace sys::fs;
111   if (Config->Verbose)
112     outs() << Path << "\n";
113 
114   Optional<MemoryBufferRef> Buffer = readFile(Path);
115   if (!Buffer.hasValue())
116     return;
117   MemoryBufferRef MBRef = *Buffer;
118 
119   switch (identify_magic(MBRef.getBuffer())) {
120   case file_magic::unknown:
121     readLinkerScript(MBRef);
122     return;
123   case file_magic::archive:
124     if (WholeArchive) {
125       for (MemoryBufferRef MB : getArchiveMembers(MBRef))
126         Files.push_back(createObjectFile(MB, Path));
127       return;
128     }
129     Files.push_back(make_unique<ArchiveFile>(MBRef));
130     return;
131   case file_magic::elf_shared_object:
132     if (Config->Relocatable) {
133       error("attempted static link of dynamic object " + Path);
134       return;
135     }
136     Files.push_back(createSharedFile(MBRef));
137     return;
138   default:
139     if (InLib)
140       Files.push_back(make_unique<LazyObjectFile>(MBRef));
141     else
142       Files.push_back(createObjectFile(MBRef));
143   }
144 }
145 
146 Optional<MemoryBufferRef> LinkerDriver::readFile(StringRef Path) {
147   auto MBOrErr = MemoryBuffer::getFile(Path);
148   error(MBOrErr, "cannot open " + Path);
149   if (HasError)
150     return None;
151   std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
152   MemoryBufferRef MBRef = MB->getMemBufferRef();
153   OwningMBs.push_back(std::move(MB)); // take MB ownership
154 
155   if (Cpio)
156     Cpio->append(relativeToRoot(Path), MBRef.getBuffer());
157 
158   return MBRef;
159 }
160 
161 // Add a given library by searching it from input search paths.
162 void LinkerDriver::addLibrary(StringRef Name) {
163   std::string Path = searchLibrary(Name);
164   if (Path.empty())
165     error("unable to find library -l" + Name);
166   else
167     addFile(Path);
168 }
169 
170 // This function is called on startup. We need this for LTO since
171 // LTO calls LLVM functions to compile bitcode files to native code.
172 // Technically this can be delayed until we read bitcode files, but
173 // we don't bother to do lazily because the initialization is fast.
174 static void initLLVM(opt::InputArgList &Args) {
175   InitializeAllTargets();
176   InitializeAllTargetMCs();
177   InitializeAllAsmPrinters();
178   InitializeAllAsmParsers();
179 
180   // This is a flag to discard all but GlobalValue names.
181   // We want to enable it by default because it saves memory.
182   // Disable it only when a developer option (-save-temps) is given.
183   Driver->Context.setDiscardValueNames(!Config->SaveTemps);
184   Driver->Context.enableDebugTypeODRUniquing();
185 
186   // Parse and evaluate -mllvm options.
187   std::vector<const char *> V;
188   V.push_back("lld (LLVM option parsing)");
189   for (auto *Arg : Args.filtered(OPT_mllvm))
190     V.push_back(Arg->getValue());
191   cl::ParseCommandLineOptions(V.size(), V.data());
192 }
193 
194 // Some command line options or some combinations of them are not allowed.
195 // This function checks for such errors.
196 static void checkOptions(opt::InputArgList &Args) {
197   // The MIPS ABI as of 2016 does not support the GNU-style symbol lookup
198   // table which is a relatively new feature.
199   if (Config->EMachine == EM_MIPS && Config->GnuHash)
200     error("the .gnu.hash section is not compatible with the MIPS target.");
201 
202   if (Config->EMachine == EM_AMDGPU && !Config->Entry.empty())
203     error("-e option is not valid for AMDGPU.");
204 
205   if (Config->Pie && Config->Shared)
206     error("-shared and -pie may not be used together");
207 
208   if (Config->Relocatable) {
209     if (Config->Shared)
210       error("-r and -shared may not be used together");
211     if (Config->GcSections)
212       error("-r and --gc-sections may not be used together");
213     if (Config->ICF)
214       error("-r and --icf may not be used together");
215     if (Config->Pie)
216       error("-r and -pie may not be used together");
217   }
218 }
219 
220 static StringRef
221 getString(opt::InputArgList &Args, unsigned Key, StringRef Default = "") {
222   if (auto *Arg = Args.getLastArg(Key))
223     return Arg->getValue();
224   return Default;
225 }
226 
227 static int getInteger(opt::InputArgList &Args, unsigned Key, int Default) {
228   int V = Default;
229   if (auto *Arg = Args.getLastArg(Key)) {
230     StringRef S = Arg->getValue();
231     if (S.getAsInteger(10, V))
232       error(Arg->getSpelling() + ": number expected, but got " + S);
233   }
234   return V;
235 }
236 
237 static bool hasZOption(opt::InputArgList &Args, StringRef Key) {
238   for (auto *Arg : Args.filtered(OPT_z))
239     if (Key == Arg->getValue())
240       return true;
241   return false;
242 }
243 
244 void LinkerDriver::main(ArrayRef<const char *> ArgsArr) {
245   ELFOptTable Parser;
246   opt::InputArgList Args = Parser.parse(ArgsArr.slice(1));
247   if (Args.hasArg(OPT_help)) {
248     printHelp(ArgsArr[0]);
249     return;
250   }
251   if (Args.hasArg(OPT_version)) {
252     outs() << getVersionString();
253     return;
254   }
255 
256   if (auto *Arg = Args.getLastArg(OPT_reproduce)) {
257     // Note that --reproduce is a debug option so you can ignore it
258     // if you are trying to understand the whole picture of the code.
259     Cpio.reset(CpioFile::create(Arg->getValue()));
260     if (Cpio) {
261       Cpio->append("response.txt", createResponseFile(Args));
262       Cpio->append("version.txt", getVersionString());
263     }
264   }
265 
266   readConfigs(Args);
267   initLLVM(Args);
268   createFiles(Args);
269   checkOptions(Args);
270   if (HasError)
271     return;
272 
273   switch (Config->EKind) {
274   case ELF32LEKind:
275     link<ELF32LE>(Args);
276     return;
277   case ELF32BEKind:
278     link<ELF32BE>(Args);
279     return;
280   case ELF64LEKind:
281     link<ELF64LE>(Args);
282     return;
283   case ELF64BEKind:
284     link<ELF64BE>(Args);
285     return;
286   default:
287     error("-m or at least a .o file required");
288   }
289 }
290 
291 // Initializes Config members by the command line options.
292 void LinkerDriver::readConfigs(opt::InputArgList &Args) {
293   for (auto *Arg : Args.filtered(OPT_L))
294     Config->SearchPaths.push_back(Arg->getValue());
295 
296   std::vector<StringRef> RPaths;
297   for (auto *Arg : Args.filtered(OPT_rpath))
298     RPaths.push_back(Arg->getValue());
299   if (!RPaths.empty())
300     Config->RPath = llvm::join(RPaths.begin(), RPaths.end(), ":");
301 
302   if (auto *Arg = Args.getLastArg(OPT_m)) {
303     // Parse ELF{32,64}{LE,BE} and CPU type.
304     StringRef S = Arg->getValue();
305     std::tie(Config->EKind, Config->EMachine) = parseEmulation(S);
306     Config->Emulation = S;
307   }
308 
309   if (Config->EMachine == EM_MIPS && Config->EKind == ELF64LEKind)
310     Config->Mips64EL = true;
311 
312   Config->AllowMultipleDefinition = Args.hasArg(OPT_allow_multiple_definition);
313   Config->Bsymbolic = Args.hasArg(OPT_Bsymbolic);
314   Config->BsymbolicFunctions = Args.hasArg(OPT_Bsymbolic_functions);
315   Config->Demangle = !Args.hasArg(OPT_no_demangle);
316   Config->DisableVerify = Args.hasArg(OPT_disable_verify);
317   Config->DiscardAll = Args.hasArg(OPT_discard_all);
318   Config->DiscardLocals = Args.hasArg(OPT_discard_locals);
319   Config->DiscardNone = Args.hasArg(OPT_discard_none);
320   Config->EhFrameHdr = Args.hasArg(OPT_eh_frame_hdr);
321   Config->EnableNewDtags = !Args.hasArg(OPT_disable_new_dtags);
322   Config->ExportDynamic = Args.hasArg(OPT_export_dynamic);
323   Config->GcSections = Args.hasArg(OPT_gc_sections);
324   Config->ICF = Args.hasArg(OPT_icf);
325   Config->NoGnuUnique = Args.hasArg(OPT_no_gnu_unique);
326   Config->NoUndefined = Args.hasArg(OPT_no_undefined);
327   Config->NoinhibitExec = Args.hasArg(OPT_noinhibit_exec);
328   Config->Pie = Args.hasArg(OPT_pie);
329   Config->PrintGcSections = Args.hasArg(OPT_print_gc_sections);
330   Config->Relocatable = Args.hasArg(OPT_relocatable);
331   Config->SaveTemps = Args.hasArg(OPT_save_temps);
332   Config->Shared = Args.hasArg(OPT_shared);
333   Config->StripAll = Args.hasArg(OPT_strip_all);
334   Config->StripDebug = Args.hasArg(OPT_strip_debug);
335   Config->Threads = Args.hasArg(OPT_threads);
336   Config->Trace = Args.hasArg(OPT_trace);
337   Config->Verbose = Args.hasArg(OPT_verbose);
338   Config->WarnCommon = Args.hasArg(OPT_warn_common);
339 
340   Config->DynamicLinker = getString(Args, OPT_dynamic_linker);
341   Config->Entry = getString(Args, OPT_entry);
342   Config->Fini = getString(Args, OPT_fini, "_fini");
343   Config->Init = getString(Args, OPT_init, "_init");
344   Config->LtoAAPipeline = getString(Args, OPT_lto_aa_pipeline);
345   Config->LtoNewPmPasses = getString(Args, OPT_lto_newpm_passes);
346   Config->OutputFile = getString(Args, OPT_o);
347   Config->SoName = getString(Args, OPT_soname);
348   Config->Sysroot = getString(Args, OPT_sysroot);
349 
350   Config->Optimize = getInteger(Args, OPT_O, 1);
351   Config->LtoO = getInteger(Args, OPT_lto_O, 2);
352   if (Config->LtoO > 3)
353     error("invalid optimization level for LTO: " + getString(Args, OPT_lto_O));
354   Config->LtoJobs = getInteger(Args, OPT_lto_jobs, 1);
355   if (Config->LtoJobs == 0)
356     error("number of threads must be > 0");
357 
358   Config->ZCombreloc = !hasZOption(Args, "nocombreloc");
359   Config->ZDefs = hasZOption(Args, "defs");
360   Config->ZExecStack = hasZOption(Args, "execstack");
361   Config->ZNodelete = hasZOption(Args, "nodelete");
362   Config->ZNow = hasZOption(Args, "now");
363   Config->ZOrigin = hasZOption(Args, "origin");
364   Config->ZRelro = !hasZOption(Args, "norelro");
365 
366   if (Config->Relocatable)
367     Config->StripAll = false;
368 
369   // --strip-all implies --strip-debug.
370   if (Config->StripAll)
371     Config->StripDebug = true;
372 
373   // Config->Pic is true if we are generating position-independent code.
374   Config->Pic = Config->Pie || Config->Shared;
375 
376   if (auto *Arg = Args.getLastArg(OPT_hash_style)) {
377     StringRef S = Arg->getValue();
378     if (S == "gnu") {
379       Config->GnuHash = true;
380       Config->SysvHash = false;
381     } else if (S == "both") {
382       Config->GnuHash = true;
383     } else if (S != "sysv")
384       error("unknown hash style: " + S);
385   }
386 
387   // Parse --build-id or --build-id=<style>.
388   if (Args.hasArg(OPT_build_id))
389     Config->BuildId = BuildIdKind::Fnv1;
390   if (auto *Arg = Args.getLastArg(OPT_build_id_eq)) {
391     StringRef S = Arg->getValue();
392     if (S == "md5") {
393       Config->BuildId = BuildIdKind::Md5;
394     } else if (S == "sha1") {
395       Config->BuildId = BuildIdKind::Sha1;
396     } else if (S == "none") {
397       Config->BuildId = BuildIdKind::None;
398     } else if (S.startswith("0x")) {
399       Config->BuildId = BuildIdKind::Hexstring;
400       Config->BuildIdVector = parseHexstring(S.substr(2));
401     } else {
402       error("unknown --build-id style: " + S);
403     }
404   }
405 
406   for (auto *Arg : Args.filtered(OPT_undefined))
407     Config->Undefined.push_back(Arg->getValue());
408 
409   if (auto *Arg = Args.getLastArg(OPT_dynamic_list))
410     if (Optional<MemoryBufferRef> Buffer = readFile(Arg->getValue()))
411       parseDynamicList(*Buffer);
412 
413   for (auto *Arg : Args.filtered(OPT_export_dynamic_symbol))
414     Config->DynamicList.push_back(Arg->getValue());
415 
416   if (auto *Arg = Args.getLastArg(OPT_version_script)) {
417     Config->VersionScript = true;
418     if (Optional<MemoryBufferRef> Buffer = readFile(Arg->getValue()))
419       parseVersionScript(*Buffer);
420   }
421 }
422 
423 void LinkerDriver::createFiles(opt::InputArgList &Args) {
424   for (auto *Arg : Args) {
425     switch (Arg->getOption().getID()) {
426     case OPT_l:
427       addLibrary(Arg->getValue());
428       break;
429     case OPT_alias_script_T:
430     case OPT_INPUT:
431     case OPT_script:
432       addFile(Arg->getValue());
433       break;
434     case OPT_as_needed:
435       Config->AsNeeded = true;
436       break;
437     case OPT_no_as_needed:
438       Config->AsNeeded = false;
439       break;
440     case OPT_Bstatic:
441       Config->Static = true;
442       break;
443     case OPT_Bdynamic:
444       Config->Static = false;
445       break;
446     case OPT_whole_archive:
447       WholeArchive = true;
448       break;
449     case OPT_no_whole_archive:
450       WholeArchive = false;
451       break;
452     case OPT_start_lib:
453       InLib = true;
454       break;
455     case OPT_end_lib:
456       InLib = false;
457       break;
458     }
459   }
460 
461   if (Files.empty() && !HasError)
462     error("no input files.");
463 }
464 
465 // Do actual linking. Note that when this function is called,
466 // all linker scripts have already been parsed.
467 template <class ELFT> void LinkerDriver::link(opt::InputArgList &Args) {
468   SymbolTable<ELFT> Symtab;
469   elf::Symtab<ELFT>::X = &Symtab;
470 
471   std::unique_ptr<TargetInfo> TI(createTarget());
472   Target = TI.get();
473   LinkerScript<ELFT> LS;
474   Script<ELFT>::X = &LS;
475 
476   Config->Rela = ELFT::Is64Bits;
477 
478   // Add entry symbol. Note that AMDGPU binaries have no entry points.
479   if (Config->Entry.empty() && !Config->Shared && !Config->Relocatable &&
480       Config->EMachine != EM_AMDGPU)
481     Config->Entry = (Config->EMachine == EM_MIPS) ? "__start" : "_start";
482 
483   // Default output filename is "a.out" by the Unix tradition.
484   if (Config->OutputFile.empty())
485     Config->OutputFile = "a.out";
486 
487   // Set either EntryAddr (if S is a number) or EntrySym (otherwise).
488   if (!Config->Entry.empty()) {
489     StringRef S = Config->Entry;
490     if (S.getAsInteger(0, Config->EntryAddr))
491       Config->EntrySym = Symtab.addUndefined(S);
492   }
493 
494   for (std::unique_ptr<InputFile> &F : Files)
495     Symtab.addFile(std::move(F));
496   if (HasError)
497     return; // There were duplicate symbols or incompatible files
498 
499   Symtab.scanUndefinedFlags();
500   Symtab.scanShlibUndefined();
501   Symtab.scanDynamicList();
502   Symtab.scanVersionScript();
503 
504   Symtab.addCombinedLtoObject();
505   if (HasError)
506     return;
507 
508   for (auto *Arg : Args.filtered(OPT_wrap))
509     Symtab.wrap(Arg->getValue());
510 
511   // Write the result to the file.
512   if (Config->GcSections)
513     markLive<ELFT>();
514   if (Config->ICF)
515     doIcf<ELFT>();
516 
517   // MergeInputSection::splitIntoPieces needs to be called before
518   // any call of MergeInputSection::getOffset. Do that.
519   for (const std::unique_ptr<elf::ObjectFile<ELFT>> &F :
520        Symtab.getObjectFiles())
521     for (InputSectionBase<ELFT> *S : F->getSections())
522       if (S && S != &InputSection<ELFT>::Discarded && S->Live)
523         if (auto *MS = dyn_cast<MergeInputSection<ELFT>>(S))
524           MS->splitIntoPieces();
525 
526   writeResult<ELFT>(&Symtab);
527 }
528