blob: 74c6ecf0f0e4d80dfa18aef19aee367daac64ccf [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"
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
Denis Protivensky1ef7b3f2015-10-07 09:13:03 +000040static void setELFType(StringRef Emul) {
41 if (Emul == "elf_i386") {
42 Config->ElfKind = ELF32LEKind;
43 Config->EMachine = EM_386;
44 return;
45 }
46 if (Emul == "elf_x86_64") {
47 Config->ElfKind = ELF64LEKind;
48 Config->EMachine = EM_X86_64;
49 return;
50 }
Simon Atanasyan456bd052015-10-08 12:13:38 +000051 if (Emul == "elf32ltsmip") {
52 Config->ElfKind = ELF32LEKind;
53 Config->EMachine = EM_MIPS;
54 return;
55 }
56 if (Emul == "elf32btsmip") {
57 Config->ElfKind = ELF32BEKind;
58 Config->EMachine = EM_MIPS;
59 return;
60 }
Denis Protivensky1ef7b3f2015-10-07 09:13:03 +000061 if (Emul == "elf32ppc") {
62 Config->ElfKind = ELF32BEKind;
63 Config->EMachine = EM_PPC;
64 return;
65 }
66 if (Emul == "elf64ppc") {
67 Config->ElfKind = ELF64BEKind;
68 Config->EMachine = EM_PPC64;
69 return;
70 }
71 error(Twine("Unknown emulation: ") + Emul);
72}
73
Rui Ueyamaff777682015-10-09 21:12:40 +000074static TargetInfo *createTarget() {
75 switch (Config->EMachine) {
76 case EM_386:
77 return new X86TargetInfo();
78 case EM_AARCH64:
79 return new AArch64TargetInfo();
80 case EM_ARM:
81 return new ARMTargetInfo();
82 case EM_MIPS:
83 return new MipsTargetInfo();
84 case EM_PPC:
85 return new PPCTargetInfo();
86 case EM_PPC64:
87 return new PPC64TargetInfo();
88 case EM_X86_64:
89 return new X86_64TargetInfo();
90 }
91 error("Unknown target machine");
92}
93
Igor Kudrin1309fc02015-09-28 15:01:59 +000094// Makes a path by concatenating Dir and File.
Igor Kudrinf03d2b42015-09-30 10:39:37 +000095// If Dir starts with '=' the result will be preceded by Sysroot,
Igor Kudrin1309fc02015-09-28 15:01:59 +000096// which can be set with --sysroot command line switch.
97static std::string buildSysrootedPath(StringRef Dir, StringRef File) {
98 SmallString<128> Path;
Igor Kudrinf03d2b42015-09-30 10:39:37 +000099 if (Dir.startswith("="))
100 sys::path::append(Path, Config->Sysroot, Dir.substr(1), File);
101 else
102 sys::path::append(Path, Dir, File);
Igor Kudrin1309fc02015-09-28 15:01:59 +0000103 return Path.str().str();
104}
105
Rafael Espindolaabb7b282015-09-28 12:52:21 +0000106// Searches a given library from input search paths, which are filled
107// from -L command line switches. Returns a path to an existent library file.
108static std::string searchLibrary(StringRef Path) {
109 std::vector<std::string> Names;
110 if (Path[0] == ':') {
111 Names.push_back(Path.drop_front().str());
112 } else {
Igor Kudrind912ee92015-10-01 16:42:03 +0000113 if (!Config->Static)
114 Names.push_back((Twine("lib") + Path + ".so").str());
Rafael Espindolaabb7b282015-09-28 12:52:21 +0000115 Names.push_back((Twine("lib") + Path + ".a").str());
116 }
Rafael Espindolaabb7b282015-09-28 12:52:21 +0000117 for (StringRef Dir : Config->InputSearchPaths) {
118 for (const std::string &Name : Names) {
Igor Kudrin1309fc02015-09-28 15:01:59 +0000119 std::string FullPath = buildSysrootedPath(Dir, Name);
120 if (sys::fs::exists(FullPath))
121 return FullPath;
Rafael Espindolaabb7b282015-09-28 12:52:21 +0000122 }
123 }
124 error(Twine("Unable to find library -l") + Path);
125}
126
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000127// Opens and parses a file. Path has to be resolved already.
128// Newly created memory buffers are owned by this driver.
129void LinkerDriver::addFile(StringRef Path) {
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +0000130 using namespace llvm::sys::fs;
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000131 auto MBOrErr = MemoryBuffer::getFile(Path);
132 error(MBOrErr, Twine("cannot open ") + Path);
133 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
134 MemoryBufferRef MBRef = MB->getMemBufferRef();
135 OwningMBs.push_back(std::move(MB)); // take MB ownership
136
137 switch (identify_magic(MBRef.getBuffer())) {
138 case file_magic::unknown:
139 readLinkerScript(MBRef);
140 return;
141 case file_magic::archive:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000142 if (WholeArchive) {
143 auto File = make_unique<ArchiveFile>(MBRef);
144 for (MemoryBufferRef &MB : File->getMembers())
145 Files.push_back(createELFFile<ObjectFile>(MB));
146 OwningArchives.emplace_back(std::move(File));
147 return;
148 }
149 Files.push_back(make_unique<ArchiveFile>(MBRef));
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000150 return;
151 case file_magic::elf_shared_object:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000152 Files.push_back(createELFFile<SharedFile>(MBRef));
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000153 return;
154 default:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000155 Files.push_back(createELFFile<ObjectFile>(MBRef));
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000156 }
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +0000157}
158
Rui Ueyama2cac5842015-10-07 19:34:51 +0000159static StringRef
160getString(opt::InputArgList &Args, unsigned Key, StringRef Default = "") {
161 if (auto *Arg = Args.getLastArg(Key))
162 return Arg->getValue();
163 return Default;
164}
165
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000166void LinkerDriver::main(ArrayRef<const char *> ArgsArr) {
Rui Ueyama9ea49c72015-10-07 23:46:11 +0000167 initSymbols();
168
Michael J. Spencer84487f12015-07-24 21:03:07 +0000169 opt::InputArgList Args = Parser.parse(ArgsArr);
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000170 createFiles(Args);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000171
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000172 switch (Config->ElfKind) {
173 case ELF32LEKind:
174 link<ELF32LE>(Args);
175 return;
176 case ELF32BEKind:
177 link<ELF32BE>(Args);
178 return;
179 case ELF64LEKind:
180 link<ELF64LE>(Args);
181 return;
182 case ELF64BEKind:
183 link<ELF64BE>(Args);
184 return;
185 default:
186 error("-m or at least a .o file required");
187 }
188}
189
190void LinkerDriver::createFiles(opt::InputArgList &Args) {
Rui Ueyama2cac5842015-10-07 19:34:51 +0000191 for (auto *Arg : Args.filtered(OPT_L))
192 Config->InputSearchPaths.push_back(Arg->getValue());
Igor Kudrin1309fc02015-09-28 15:01:59 +0000193
Rafael Espindola2e9eac12015-09-11 21:18:56 +0000194 std::vector<StringRef> RPaths;
195 for (auto *Arg : Args.filtered(OPT_rpath))
196 RPaths.push_back(Arg->getValue());
197 if (!RPaths.empty())
198 Config->RPath = llvm::join(RPaths.begin(), RPaths.end(), ":");
199
Denis Protivensky1ef7b3f2015-10-07 09:13:03 +0000200 if (auto *Arg = Args.getLastArg(OPT_m))
201 setELFType(Arg->getValue());
202
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000203 Config->AllowMultipleDefinition = Args.hasArg(OPT_allow_multiple_definition);
204 Config->DiscardAll = Args.hasArg(OPT_discard_all);
205 Config->DiscardLocals = Args.hasArg(OPT_discard_locals);
206 Config->DiscardNone = Args.hasArg(OPT_discard_none);
Davide Italianoc39c75d2015-10-06 16:20:00 +0000207 Config->EnableNewDtags = !Args.hasArg(OPT_disable_new_dtags);
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000208 Config->ExportDynamic = Args.hasArg(OPT_export_dynamic);
209 Config->NoInhibitExec = Args.hasArg(OPT_noinhibit_exec);
George Rimar57e40de2015-10-01 20:14:45 +0000210 Config->NoUndefined = Args.hasArg(OPT_no_undefined);
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000211 Config->Shared = Args.hasArg(OPT_shared);
212
Rui Ueyama2cac5842015-10-07 19:34:51 +0000213 Config->DynamicLinker = getString(Args, OPT_dynamic_linker);
214 Config->Entry = getString(Args, OPT_entry);
215 Config->Fini = getString(Args, OPT_fini, "_fini");
216 Config->Init = getString(Args, OPT_init, "_init");
Rui Ueyama964ffb32015-10-09 00:33:44 +0000217 Config->OutputFile = getString(Args, OPT_o);
Rui Ueyama2cac5842015-10-07 19:34:51 +0000218 Config->SoName = getString(Args, OPT_soname);
219 Config->Sysroot = getString(Args, OPT_sysroot);
220
Rui Ueyama58d7d702015-10-07 18:22:46 +0000221 for (auto *Arg : Args.filtered(OPT_z))
George Rimar97aad172015-10-07 15:00:21 +0000222 if (Arg->getValue() == StringRef("now"))
223 Config->ZNow = true;
George Rimar97aad172015-10-07 15:00:21 +0000224
Igor Kudrind912ee92015-10-01 16:42:03 +0000225 for (auto *Arg : Args) {
226 switch (Arg->getOption().getID()) {
227 case OPT_l:
228 addFile(searchLibrary(Arg->getValue()));
229 break;
230 case OPT_INPUT:
231 addFile(Arg->getValue());
232 break;
233 case OPT_Bstatic:
234 Config->Static = true;
235 break;
236 case OPT_Bdynamic:
237 Config->Static = false;
238 break;
Igor Kudrin2696bbe2015-10-01 18:02:21 +0000239 case OPT_whole_archive:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000240 WholeArchive = true;
Igor Kudrin2696bbe2015-10-01 18:02:21 +0000241 break;
242 case OPT_no_whole_archive:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000243 WholeArchive = false;
Igor Kudrin2696bbe2015-10-01 18:02:21 +0000244 break;
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +0000245 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000246 }
247
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000248 if (Files.empty())
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +0000249 error("no input files.");
250
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000251 // Set machine type if -m is not given.
252 if (Config->ElfKind == ELFNoneKind) {
253 for (std::unique_ptr<InputFile> &File : Files) {
254 auto *F = dyn_cast<ELFFileBase>(File.get());
255 if (!F)
256 continue;
257 Config->ElfKind = F->getELFKind();
258 Config->EMachine = F->getEMachine();
259 break;
260 }
261 }
262
263 // Check if all files are for the same machine type.
264 for (std::unique_ptr<InputFile> &File : Files) {
265 auto *F = dyn_cast<ELFFileBase>(File.get());
266 if (!F)
267 continue;
268 if (F->getELFKind() == Config->ElfKind &&
269 F->getEMachine() == Config->EMachine)
270 continue;
271 StringRef A = F->getName();
272 StringRef B = Files[0]->getName();
273 if (auto *Arg = Args.getLastArg(OPT_m))
274 B = Arg->getValue();
275 error(A + " is incompatible with " + B);
276 }
277}
278
279template <class ELFT> void LinkerDriver::link(opt::InputArgList &Args) {
280 SymbolTable<ELFT> Symtab;
Rui Ueyamaff777682015-10-09 21:12:40 +0000281 Target.reset(createTarget());
282
283 if (!Config->Shared) {
284 // Add entry symbol.
285 Config->EntrySym = Symtab.addUndefined(
286 Config->Entry.empty() ? Target->getDefaultEntry() : Config->Entry);
287
288 // In the assembly for 32 bit x86 the _GLOBAL_OFFSET_TABLE_ symbol
289 // is magical and is used to produce a R_386_GOTPC relocation.
290 // The R_386_GOTPC relocation value doesn't actually depend on the
291 // symbol value, so it could use an index of STN_UNDEF which, according
292 // to the spec, means the symbol value is 0.
293 // Unfortunately both gas and MC keep the _GLOBAL_OFFSET_TABLE_ symbol in
294 // the object file.
295 // The situation is even stranger on x86_64 where the assembly doesn't
296 // need the magical symbol, but gas still puts _GLOBAL_OFFSET_TABLE_ as
297 // an undefined symbol in the .o files.
298 // Given that the symbol is effectively unused, we just create a dummy
299 // hidden one to avoid the undefined symbol error.
300 Symtab.addIgnoredSym("_GLOBAL_OFFSET_TABLE_");
301 }
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000302
303 for (std::unique_ptr<InputFile> &F : Files)
304 Symtab.addFile(std::move(F));
305
Denis Protivensky22220d52015-10-05 09:43:57 +0000306 for (auto *Arg : Args.filtered(OPT_undefined))
Rui Ueyamaff777682015-10-09 21:12:40 +0000307 Symtab.addUndefinedOpt(Arg->getValue());
Denis Protivensky22220d52015-10-05 09:43:57 +0000308
Rui Ueyamaee592822015-10-07 00:25:09 +0000309 if (Config->OutputFile.empty())
310 Config->OutputFile = "a.out";
311
Rui Ueyama75230392015-10-07 18:29:51 +0000312 // Write the result to the file.
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000313 writeResult<ELFT>(&Symtab);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000314}