blob: 9818667c585b03282054637e401211fe3b5e8b8a [file] [log] [blame]
Michael J. Spencer84487f12015-07-24 21:03:07 +00001//===- 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
Rui Ueyamaafff74e22015-08-05 23:24:46 +000010#include "Driver.h"
Rafael Espindola192e1fa2015-08-06 15:08:23 +000011#include "Config.h"
12#include "Error.h"
Rui Ueyamaafff74e22015-08-05 23:24:46 +000013#include "InputFiles.h"
14#include "SymbolTable.h"
Rui Ueyamaff777682015-10-09 21:12:40 +000015#include "Target.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000016#include "Writer.h"
17#include "llvm/ADT/STLExtras.h"
Rafael Espindola2e9eac12015-09-11 21:18:56 +000018#include "llvm/ADT/StringExtras.h"
Rui Ueyamaa4672402015-10-11 02:03:03 +000019#include "llvm/Support/raw_ostream.h"
Rui Ueyamacacf9672015-10-11 02:22:31 +000020#include <utility>
Michael J. Spencer84487f12015-07-24 21:03:07 +000021
22using namespace llvm;
Denis Protivensky1ef7b3f2015-10-07 09:13:03 +000023using namespace llvm::ELF;
Rui Ueyama3ce825e2015-10-09 21:07:25 +000024using namespace llvm::object;
Michael J. Spencer84487f12015-07-24 21:03:07 +000025
26using namespace lld;
27using namespace lld::elf2;
28
Rui Ueyama983ed2b2015-10-01 15:23:09 +000029Configuration *lld::elf2::Config;
30LinkerDriver *lld::elf2::Driver;
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +000031
Rui Ueyama983ed2b2015-10-01 15:23:09 +000032void lld::elf2::link(ArrayRef<const char *> Args) {
Rui Ueyama570752c2015-08-18 09:13:25 +000033 Configuration C;
Rui Ueyama983ed2b2015-10-01 15:23:09 +000034 LinkerDriver D;
Rui Ueyama570752c2015-08-18 09:13:25 +000035 Config = &C;
Rui Ueyama983ed2b2015-10-01 15:23:09 +000036 Driver = &D;
Rui Ueyama3ce825e2015-10-09 21:07:25 +000037 Driver->main(Args.slice(1));
Michael J. Spencer84487f12015-07-24 21:03:07 +000038}
39
Rui Ueyamacacf9672015-10-11 02:22:31 +000040static std::pair<ELFKind, uint16_t> parseEmulation(StringRef S) {
41 if (S == "elf32btsmip") return {ELF32BEKind, EM_MIPS};
42 if (S == "elf32ltsmip") return {ELF32LEKind, EM_MIPS};
43 if (S == "elf32ppc") return {ELF32BEKind, EM_PPC};
44 if (S == "elf64ppc") return {ELF64BEKind, EM_PPC64};
45 if (S == "elf_i386") return {ELF32LEKind, EM_386};
46 if (S == "elf_x86_64") return {ELF64LEKind, EM_X86_64};
47 error("Unknown emulation: " + S);
Denis Protivensky1ef7b3f2015-10-07 09:13:03 +000048}
49
Rui Ueyamaff777682015-10-09 21:12:40 +000050static TargetInfo *createTarget() {
51 switch (Config->EMachine) {
52 case EM_386:
53 return new X86TargetInfo();
54 case EM_AARCH64:
55 return new AArch64TargetInfo();
56 case EM_ARM:
57 return new ARMTargetInfo();
58 case EM_MIPS:
59 return new MipsTargetInfo();
60 case EM_PPC:
61 return new PPCTargetInfo();
62 case EM_PPC64:
63 return new PPC64TargetInfo();
64 case EM_X86_64:
65 return new X86_64TargetInfo();
66 }
67 error("Unknown target machine");
68}
69
Rui Ueyama983ed2b2015-10-01 15:23:09 +000070// Opens and parses a file. Path has to be resolved already.
71// Newly created memory buffers are owned by this driver.
72void LinkerDriver::addFile(StringRef Path) {
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +000073 using namespace llvm::sys::fs;
Rui Ueyamaa4672402015-10-11 02:03:03 +000074 if (Config->Verbose)
75 llvm::outs() << Path << "\n";
Rui Ueyama983ed2b2015-10-01 15:23:09 +000076 auto MBOrErr = MemoryBuffer::getFile(Path);
77 error(MBOrErr, Twine("cannot open ") + Path);
78 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
79 MemoryBufferRef MBRef = MB->getMemBufferRef();
80 OwningMBs.push_back(std::move(MB)); // take MB ownership
81
82 switch (identify_magic(MBRef.getBuffer())) {
83 case file_magic::unknown:
Rui Ueyamaa47ee682015-10-11 01:53:04 +000084 readLinkerScript(&Alloc, MBRef);
Rui Ueyama983ed2b2015-10-01 15:23:09 +000085 return;
86 case file_magic::archive:
Rui Ueyama3ce825e2015-10-09 21:07:25 +000087 if (WholeArchive) {
88 auto File = make_unique<ArchiveFile>(MBRef);
89 for (MemoryBufferRef &MB : File->getMembers())
90 Files.push_back(createELFFile<ObjectFile>(MB));
91 OwningArchives.emplace_back(std::move(File));
92 return;
93 }
94 Files.push_back(make_unique<ArchiveFile>(MBRef));
Rui Ueyama983ed2b2015-10-01 15:23:09 +000095 return;
96 case file_magic::elf_shared_object:
Rui Ueyama3ce825e2015-10-09 21:07:25 +000097 Files.push_back(createELFFile<SharedFile>(MBRef));
Rui Ueyama983ed2b2015-10-01 15:23:09 +000098 return;
99 default:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000100 Files.push_back(createELFFile<ObjectFile>(MBRef));
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000101 }
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +0000102}
103
Rui Ueyama2cac5842015-10-07 19:34:51 +0000104static StringRef
105getString(opt::InputArgList &Args, unsigned Key, StringRef Default = "") {
106 if (auto *Arg = Args.getLastArg(Key))
107 return Arg->getValue();
108 return Default;
109}
110
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000111void LinkerDriver::main(ArrayRef<const char *> ArgsArr) {
Rui Ueyama9ea49c72015-10-07 23:46:11 +0000112 initSymbols();
113
Rui Ueyamab9732562015-10-11 01:53:07 +0000114 opt::InputArgList Args = ArgParser(&Alloc).parse(ArgsArr);
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000115 createFiles(Args);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000116
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000117 switch (Config->ElfKind) {
118 case ELF32LEKind:
119 link<ELF32LE>(Args);
120 return;
121 case ELF32BEKind:
122 link<ELF32BE>(Args);
123 return;
124 case ELF64LEKind:
125 link<ELF64LE>(Args);
126 return;
127 case ELF64BEKind:
128 link<ELF64BE>(Args);
129 return;
130 default:
131 error("-m or at least a .o file required");
132 }
133}
134
135void LinkerDriver::createFiles(opt::InputArgList &Args) {
Rui Ueyama2cac5842015-10-07 19:34:51 +0000136 for (auto *Arg : Args.filtered(OPT_L))
Rui Ueyama52a15092015-10-11 03:28:42 +0000137 Config->SearchPaths.push_back(Arg->getValue());
Igor Kudrin1309fc02015-09-28 15:01:59 +0000138
Rafael Espindola2e9eac12015-09-11 21:18:56 +0000139 std::vector<StringRef> RPaths;
140 for (auto *Arg : Args.filtered(OPT_rpath))
141 RPaths.push_back(Arg->getValue());
142 if (!RPaths.empty())
143 Config->RPath = llvm::join(RPaths.begin(), RPaths.end(), ":");
144
Rui Ueyamacacf9672015-10-11 02:22:31 +0000145 if (auto *Arg = Args.getLastArg(OPT_m)) {
146 std::pair<ELFKind, uint16_t> P = parseEmulation(Arg->getValue());
147 Config->ElfKind = P.first;
148 Config->EMachine = P.second;
149 }
Denis Protivensky1ef7b3f2015-10-07 09:13:03 +0000150
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000151 Config->AllowMultipleDefinition = Args.hasArg(OPT_allow_multiple_definition);
152 Config->DiscardAll = Args.hasArg(OPT_discard_all);
153 Config->DiscardLocals = Args.hasArg(OPT_discard_locals);
154 Config->DiscardNone = Args.hasArg(OPT_discard_none);
Davide Italianoc39c75d2015-10-06 16:20:00 +0000155 Config->EnableNewDtags = !Args.hasArg(OPT_disable_new_dtags);
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000156 Config->ExportDynamic = Args.hasArg(OPT_export_dynamic);
157 Config->NoInhibitExec = Args.hasArg(OPT_noinhibit_exec);
George Rimar57e40de2015-10-01 20:14:45 +0000158 Config->NoUndefined = Args.hasArg(OPT_no_undefined);
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000159 Config->Shared = Args.hasArg(OPT_shared);
Rui Ueyamaa4672402015-10-11 02:03:03 +0000160 Config->Verbose = Args.hasArg(OPT_verbose);
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000161
Rui Ueyama2cac5842015-10-07 19:34:51 +0000162 Config->DynamicLinker = getString(Args, OPT_dynamic_linker);
163 Config->Entry = getString(Args, OPT_entry);
164 Config->Fini = getString(Args, OPT_fini, "_fini");
165 Config->Init = getString(Args, OPT_init, "_init");
Rui Ueyama964ffb32015-10-09 00:33:44 +0000166 Config->OutputFile = getString(Args, OPT_o);
Rui Ueyama2cac5842015-10-07 19:34:51 +0000167 Config->SoName = getString(Args, OPT_soname);
168 Config->Sysroot = getString(Args, OPT_sysroot);
169
Rui Ueyama58d7d702015-10-07 18:22:46 +0000170 for (auto *Arg : Args.filtered(OPT_z))
George Rimar97aad172015-10-07 15:00:21 +0000171 if (Arg->getValue() == StringRef("now"))
172 Config->ZNow = true;
George Rimar97aad172015-10-07 15:00:21 +0000173
Igor Kudrind912ee92015-10-01 16:42:03 +0000174 for (auto *Arg : Args) {
175 switch (Arg->getOption().getID()) {
176 case OPT_l:
177 addFile(searchLibrary(Arg->getValue()));
178 break;
179 case OPT_INPUT:
180 addFile(Arg->getValue());
181 break;
182 case OPT_Bstatic:
183 Config->Static = true;
184 break;
185 case OPT_Bdynamic:
186 Config->Static = false;
187 break;
Igor Kudrin2696bbe2015-10-01 18:02:21 +0000188 case OPT_whole_archive:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000189 WholeArchive = true;
Igor Kudrin2696bbe2015-10-01 18:02:21 +0000190 break;
191 case OPT_no_whole_archive:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000192 WholeArchive = false;
Igor Kudrin2696bbe2015-10-01 18:02:21 +0000193 break;
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +0000194 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000195 }
196
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000197 if (Files.empty())
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +0000198 error("no input files.");
199
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000200 // Set machine type if -m is not given.
201 if (Config->ElfKind == ELFNoneKind) {
202 for (std::unique_ptr<InputFile> &File : Files) {
203 auto *F = dyn_cast<ELFFileBase>(File.get());
204 if (!F)
205 continue;
206 Config->ElfKind = F->getELFKind();
207 Config->EMachine = F->getEMachine();
208 break;
209 }
210 }
211
212 // Check if all files are for the same machine type.
213 for (std::unique_ptr<InputFile> &File : Files) {
214 auto *F = dyn_cast<ELFFileBase>(File.get());
215 if (!F)
216 continue;
217 if (F->getELFKind() == Config->ElfKind &&
218 F->getEMachine() == Config->EMachine)
219 continue;
220 StringRef A = F->getName();
221 StringRef B = Files[0]->getName();
222 if (auto *Arg = Args.getLastArg(OPT_m))
223 B = Arg->getValue();
224 error(A + " is incompatible with " + B);
225 }
226}
227
228template <class ELFT> void LinkerDriver::link(opt::InputArgList &Args) {
229 SymbolTable<ELFT> Symtab;
Rui Ueyamaff777682015-10-09 21:12:40 +0000230 Target.reset(createTarget());
231
232 if (!Config->Shared) {
233 // Add entry symbol.
234 Config->EntrySym = Symtab.addUndefined(
235 Config->Entry.empty() ? Target->getDefaultEntry() : Config->Entry);
236
237 // In the assembly for 32 bit x86 the _GLOBAL_OFFSET_TABLE_ symbol
238 // is magical and is used to produce a R_386_GOTPC relocation.
239 // The R_386_GOTPC relocation value doesn't actually depend on the
240 // symbol value, so it could use an index of STN_UNDEF which, according
241 // to the spec, means the symbol value is 0.
242 // Unfortunately both gas and MC keep the _GLOBAL_OFFSET_TABLE_ symbol in
243 // the object file.
244 // The situation is even stranger on x86_64 where the assembly doesn't
245 // need the magical symbol, but gas still puts _GLOBAL_OFFSET_TABLE_ as
246 // an undefined symbol in the .o files.
247 // Given that the symbol is effectively unused, we just create a dummy
248 // hidden one to avoid the undefined symbol error.
249 Symtab.addIgnoredSym("_GLOBAL_OFFSET_TABLE_");
250 }
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000251
252 for (std::unique_ptr<InputFile> &F : Files)
253 Symtab.addFile(std::move(F));
254
Denis Protivensky22220d52015-10-05 09:43:57 +0000255 for (auto *Arg : Args.filtered(OPT_undefined))
Rui Ueyamaff777682015-10-09 21:12:40 +0000256 Symtab.addUndefinedOpt(Arg->getValue());
Denis Protivensky22220d52015-10-05 09:43:57 +0000257
Rui Ueyamaee592822015-10-07 00:25:09 +0000258 if (Config->OutputFile.empty())
259 Config->OutputFile = "a.out";
260
Rui Ueyama75230392015-10-07 18:29:51 +0000261 // Write the result to the file.
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000262 writeResult<ELFT>(&Symtab);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000263}