blob: 98fb933f4739bc2109c9b9e6f2bd218b100427c1 [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"
Rafael Espindolaf98d6d82015-09-03 20:03:54 +000019#include "llvm/Support/FileSystem.h"
Rafael Espindolaabb7b282015-09-28 12:52:21 +000020#include "llvm/Support/Path.h"
Rui Ueyamaa4672402015-10-11 02:03:03 +000021#include "llvm/Support/raw_ostream.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000022
23using namespace llvm;
Denis Protivensky1ef7b3f2015-10-07 09:13:03 +000024using namespace llvm::ELF;
Rui Ueyama3ce825e2015-10-09 21:07:25 +000025using namespace llvm::object;
Michael J. Spencer84487f12015-07-24 21:03:07 +000026
27using namespace lld;
28using namespace lld::elf2;
29
Rui Ueyama983ed2b2015-10-01 15:23:09 +000030Configuration *lld::elf2::Config;
31LinkerDriver *lld::elf2::Driver;
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +000032
Rui Ueyama983ed2b2015-10-01 15:23:09 +000033void lld::elf2::link(ArrayRef<const char *> Args) {
Rui Ueyama570752c2015-08-18 09:13:25 +000034 Configuration C;
Rui Ueyama983ed2b2015-10-01 15:23:09 +000035 LinkerDriver D;
Rui Ueyama570752c2015-08-18 09:13:25 +000036 Config = &C;
Rui Ueyama983ed2b2015-10-01 15:23:09 +000037 Driver = &D;
Rui Ueyama3ce825e2015-10-09 21:07:25 +000038 Driver->main(Args.slice(1));
Michael J. Spencer84487f12015-07-24 21:03:07 +000039}
40
Denis Protivensky1ef7b3f2015-10-07 09:13:03 +000041static void setELFType(StringRef Emul) {
42 if (Emul == "elf_i386") {
43 Config->ElfKind = ELF32LEKind;
44 Config->EMachine = EM_386;
45 return;
46 }
47 if (Emul == "elf_x86_64") {
48 Config->ElfKind = ELF64LEKind;
49 Config->EMachine = EM_X86_64;
50 return;
51 }
Simon Atanasyan456bd052015-10-08 12:13:38 +000052 if (Emul == "elf32ltsmip") {
53 Config->ElfKind = ELF32LEKind;
54 Config->EMachine = EM_MIPS;
55 return;
56 }
57 if (Emul == "elf32btsmip") {
58 Config->ElfKind = ELF32BEKind;
59 Config->EMachine = EM_MIPS;
60 return;
61 }
Denis Protivensky1ef7b3f2015-10-07 09:13:03 +000062 if (Emul == "elf32ppc") {
63 Config->ElfKind = ELF32BEKind;
64 Config->EMachine = EM_PPC;
65 return;
66 }
67 if (Emul == "elf64ppc") {
68 Config->ElfKind = ELF64BEKind;
69 Config->EMachine = EM_PPC64;
70 return;
71 }
72 error(Twine("Unknown emulation: ") + Emul);
73}
74
Rui Ueyamaff777682015-10-09 21:12:40 +000075static TargetInfo *createTarget() {
76 switch (Config->EMachine) {
77 case EM_386:
78 return new X86TargetInfo();
79 case EM_AARCH64:
80 return new AArch64TargetInfo();
81 case EM_ARM:
82 return new ARMTargetInfo();
83 case EM_MIPS:
84 return new MipsTargetInfo();
85 case EM_PPC:
86 return new PPCTargetInfo();
87 case EM_PPC64:
88 return new PPC64TargetInfo();
89 case EM_X86_64:
90 return new X86_64TargetInfo();
91 }
92 error("Unknown target machine");
93}
94
Igor Kudrin1309fc02015-09-28 15:01:59 +000095// Makes a path by concatenating Dir and File.
Igor Kudrinf03d2b42015-09-30 10:39:37 +000096// If Dir starts with '=' the result will be preceded by Sysroot,
Igor Kudrin1309fc02015-09-28 15:01:59 +000097// which can be set with --sysroot command line switch.
98static std::string buildSysrootedPath(StringRef Dir, StringRef File) {
99 SmallString<128> Path;
Igor Kudrinf03d2b42015-09-30 10:39:37 +0000100 if (Dir.startswith("="))
101 sys::path::append(Path, Config->Sysroot, Dir.substr(1), File);
102 else
103 sys::path::append(Path, Dir, File);
Igor Kudrin1309fc02015-09-28 15:01:59 +0000104 return Path.str().str();
105}
106
Rafael Espindolaabb7b282015-09-28 12:52:21 +0000107// Searches a given library from input search paths, which are filled
108// from -L command line switches. Returns a path to an existent library file.
109static std::string searchLibrary(StringRef Path) {
110 std::vector<std::string> Names;
111 if (Path[0] == ':') {
112 Names.push_back(Path.drop_front().str());
113 } else {
Igor Kudrind912ee92015-10-01 16:42:03 +0000114 if (!Config->Static)
115 Names.push_back((Twine("lib") + Path + ".so").str());
Rafael Espindolaabb7b282015-09-28 12:52:21 +0000116 Names.push_back((Twine("lib") + Path + ".a").str());
117 }
Rafael Espindolaabb7b282015-09-28 12:52:21 +0000118 for (StringRef Dir : Config->InputSearchPaths) {
119 for (const std::string &Name : Names) {
Igor Kudrin1309fc02015-09-28 15:01:59 +0000120 std::string FullPath = buildSysrootedPath(Dir, Name);
121 if (sys::fs::exists(FullPath))
122 return FullPath;
Rafael Espindolaabb7b282015-09-28 12:52:21 +0000123 }
124 }
125 error(Twine("Unable to find library -l") + Path);
126}
127
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000128// Opens and parses a file. Path has to be resolved already.
129// Newly created memory buffers are owned by this driver.
130void LinkerDriver::addFile(StringRef Path) {
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +0000131 using namespace llvm::sys::fs;
Rui Ueyamaa4672402015-10-11 02:03:03 +0000132 if (Config->Verbose)
133 llvm::outs() << Path << "\n";
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000134 auto MBOrErr = MemoryBuffer::getFile(Path);
135 error(MBOrErr, Twine("cannot open ") + Path);
136 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
137 MemoryBufferRef MBRef = MB->getMemBufferRef();
138 OwningMBs.push_back(std::move(MB)); // take MB ownership
139
140 switch (identify_magic(MBRef.getBuffer())) {
141 case file_magic::unknown:
Rui Ueyamaa47ee682015-10-11 01:53:04 +0000142 readLinkerScript(&Alloc, MBRef);
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000143 return;
144 case file_magic::archive:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000145 if (WholeArchive) {
146 auto File = make_unique<ArchiveFile>(MBRef);
147 for (MemoryBufferRef &MB : File->getMembers())
148 Files.push_back(createELFFile<ObjectFile>(MB));
149 OwningArchives.emplace_back(std::move(File));
150 return;
151 }
152 Files.push_back(make_unique<ArchiveFile>(MBRef));
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000153 return;
154 case file_magic::elf_shared_object:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000155 Files.push_back(createELFFile<SharedFile>(MBRef));
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000156 return;
157 default:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000158 Files.push_back(createELFFile<ObjectFile>(MBRef));
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000159 }
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +0000160}
161
Rui Ueyama2cac5842015-10-07 19:34:51 +0000162static StringRef
163getString(opt::InputArgList &Args, unsigned Key, StringRef Default = "") {
164 if (auto *Arg = Args.getLastArg(Key))
165 return Arg->getValue();
166 return Default;
167}
168
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000169void LinkerDriver::main(ArrayRef<const char *> ArgsArr) {
Rui Ueyama9ea49c72015-10-07 23:46:11 +0000170 initSymbols();
171
Rui Ueyamab9732562015-10-11 01:53:07 +0000172 opt::InputArgList Args = ArgParser(&Alloc).parse(ArgsArr);
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000173 createFiles(Args);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000174
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000175 switch (Config->ElfKind) {
176 case ELF32LEKind:
177 link<ELF32LE>(Args);
178 return;
179 case ELF32BEKind:
180 link<ELF32BE>(Args);
181 return;
182 case ELF64LEKind:
183 link<ELF64LE>(Args);
184 return;
185 case ELF64BEKind:
186 link<ELF64BE>(Args);
187 return;
188 default:
189 error("-m or at least a .o file required");
190 }
191}
192
193void LinkerDriver::createFiles(opt::InputArgList &Args) {
Rui Ueyama2cac5842015-10-07 19:34:51 +0000194 for (auto *Arg : Args.filtered(OPT_L))
195 Config->InputSearchPaths.push_back(Arg->getValue());
Igor Kudrin1309fc02015-09-28 15:01:59 +0000196
Rafael Espindola2e9eac12015-09-11 21:18:56 +0000197 std::vector<StringRef> RPaths;
198 for (auto *Arg : Args.filtered(OPT_rpath))
199 RPaths.push_back(Arg->getValue());
200 if (!RPaths.empty())
201 Config->RPath = llvm::join(RPaths.begin(), RPaths.end(), ":");
202
Denis Protivensky1ef7b3f2015-10-07 09:13:03 +0000203 if (auto *Arg = Args.getLastArg(OPT_m))
204 setELFType(Arg->getValue());
205
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000206 Config->AllowMultipleDefinition = Args.hasArg(OPT_allow_multiple_definition);
207 Config->DiscardAll = Args.hasArg(OPT_discard_all);
208 Config->DiscardLocals = Args.hasArg(OPT_discard_locals);
209 Config->DiscardNone = Args.hasArg(OPT_discard_none);
Davide Italianoc39c75d2015-10-06 16:20:00 +0000210 Config->EnableNewDtags = !Args.hasArg(OPT_disable_new_dtags);
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000211 Config->ExportDynamic = Args.hasArg(OPT_export_dynamic);
212 Config->NoInhibitExec = Args.hasArg(OPT_noinhibit_exec);
George Rimar57e40de2015-10-01 20:14:45 +0000213 Config->NoUndefined = Args.hasArg(OPT_no_undefined);
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000214 Config->Shared = Args.hasArg(OPT_shared);
Rui Ueyamaa4672402015-10-11 02:03:03 +0000215 Config->Verbose = Args.hasArg(OPT_verbose);
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000216
Rui Ueyama2cac5842015-10-07 19:34:51 +0000217 Config->DynamicLinker = getString(Args, OPT_dynamic_linker);
218 Config->Entry = getString(Args, OPT_entry);
219 Config->Fini = getString(Args, OPT_fini, "_fini");
220 Config->Init = getString(Args, OPT_init, "_init");
Rui Ueyama964ffb32015-10-09 00:33:44 +0000221 Config->OutputFile = getString(Args, OPT_o);
Rui Ueyama2cac5842015-10-07 19:34:51 +0000222 Config->SoName = getString(Args, OPT_soname);
223 Config->Sysroot = getString(Args, OPT_sysroot);
224
Rui Ueyama58d7d702015-10-07 18:22:46 +0000225 for (auto *Arg : Args.filtered(OPT_z))
George Rimar97aad172015-10-07 15:00:21 +0000226 if (Arg->getValue() == StringRef("now"))
227 Config->ZNow = true;
George Rimar97aad172015-10-07 15:00:21 +0000228
Igor Kudrind912ee92015-10-01 16:42:03 +0000229 for (auto *Arg : Args) {
230 switch (Arg->getOption().getID()) {
231 case OPT_l:
232 addFile(searchLibrary(Arg->getValue()));
233 break;
234 case OPT_INPUT:
235 addFile(Arg->getValue());
236 break;
237 case OPT_Bstatic:
238 Config->Static = true;
239 break;
240 case OPT_Bdynamic:
241 Config->Static = false;
242 break;
Igor Kudrin2696bbe2015-10-01 18:02:21 +0000243 case OPT_whole_archive:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000244 WholeArchive = true;
Igor Kudrin2696bbe2015-10-01 18:02:21 +0000245 break;
246 case OPT_no_whole_archive:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000247 WholeArchive = false;
Igor Kudrin2696bbe2015-10-01 18:02:21 +0000248 break;
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +0000249 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000250 }
251
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000252 if (Files.empty())
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +0000253 error("no input files.");
254
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000255 // Set machine type if -m is not given.
256 if (Config->ElfKind == ELFNoneKind) {
257 for (std::unique_ptr<InputFile> &File : Files) {
258 auto *F = dyn_cast<ELFFileBase>(File.get());
259 if (!F)
260 continue;
261 Config->ElfKind = F->getELFKind();
262 Config->EMachine = F->getEMachine();
263 break;
264 }
265 }
266
267 // Check if all files are for the same machine type.
268 for (std::unique_ptr<InputFile> &File : Files) {
269 auto *F = dyn_cast<ELFFileBase>(File.get());
270 if (!F)
271 continue;
272 if (F->getELFKind() == Config->ElfKind &&
273 F->getEMachine() == Config->EMachine)
274 continue;
275 StringRef A = F->getName();
276 StringRef B = Files[0]->getName();
277 if (auto *Arg = Args.getLastArg(OPT_m))
278 B = Arg->getValue();
279 error(A + " is incompatible with " + B);
280 }
281}
282
283template <class ELFT> void LinkerDriver::link(opt::InputArgList &Args) {
284 SymbolTable<ELFT> Symtab;
Rui Ueyamaff777682015-10-09 21:12:40 +0000285 Target.reset(createTarget());
286
287 if (!Config->Shared) {
288 // Add entry symbol.
289 Config->EntrySym = Symtab.addUndefined(
290 Config->Entry.empty() ? Target->getDefaultEntry() : Config->Entry);
291
292 // In the assembly for 32 bit x86 the _GLOBAL_OFFSET_TABLE_ symbol
293 // is magical and is used to produce a R_386_GOTPC relocation.
294 // The R_386_GOTPC relocation value doesn't actually depend on the
295 // symbol value, so it could use an index of STN_UNDEF which, according
296 // to the spec, means the symbol value is 0.
297 // Unfortunately both gas and MC keep the _GLOBAL_OFFSET_TABLE_ symbol in
298 // the object file.
299 // The situation is even stranger on x86_64 where the assembly doesn't
300 // need the magical symbol, but gas still puts _GLOBAL_OFFSET_TABLE_ as
301 // an undefined symbol in the .o files.
302 // Given that the symbol is effectively unused, we just create a dummy
303 // hidden one to avoid the undefined symbol error.
304 Symtab.addIgnoredSym("_GLOBAL_OFFSET_TABLE_");
305 }
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000306
307 for (std::unique_ptr<InputFile> &F : Files)
308 Symtab.addFile(std::move(F));
309
Denis Protivensky22220d52015-10-05 09:43:57 +0000310 for (auto *Arg : Args.filtered(OPT_undefined))
Rui Ueyamaff777682015-10-09 21:12:40 +0000311 Symtab.addUndefinedOpt(Arg->getValue());
Denis Protivensky22220d52015-10-05 09:43:57 +0000312
Rui Ueyamaee592822015-10-07 00:25:09 +0000313 if (Config->OutputFile.empty())
314 Config->OutputFile = "a.out";
315
Rui Ueyama75230392015-10-07 18:29:51 +0000316 // Write the result to the file.
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000317 writeResult<ELFT>(&Symtab);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000318}