blob: 4221ce1fab8b6f9f20f97bf8bf5aacf867e03824 [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"
Rui Ueyamacacf9672015-10-11 02:22:31 +000022#include <utility>
Michael J. Spencer84487f12015-07-24 21:03:07 +000023
24using namespace llvm;
Denis Protivensky1ef7b3f2015-10-07 09:13:03 +000025using namespace llvm::ELF;
Rui Ueyama3ce825e2015-10-09 21:07:25 +000026using namespace llvm::object;
Michael J. Spencer84487f12015-07-24 21:03:07 +000027
28using namespace lld;
29using namespace lld::elf2;
30
Rui Ueyama983ed2b2015-10-01 15:23:09 +000031Configuration *lld::elf2::Config;
32LinkerDriver *lld::elf2::Driver;
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +000033
Rui Ueyama983ed2b2015-10-01 15:23:09 +000034void lld::elf2::link(ArrayRef<const char *> Args) {
Rui Ueyama570752c2015-08-18 09:13:25 +000035 Configuration C;
Rui Ueyama983ed2b2015-10-01 15:23:09 +000036 LinkerDriver D;
Rui Ueyama570752c2015-08-18 09:13:25 +000037 Config = &C;
Rui Ueyama983ed2b2015-10-01 15:23:09 +000038 Driver = &D;
Rui Ueyama3ce825e2015-10-09 21:07:25 +000039 Driver->main(Args.slice(1));
Michael J. Spencer84487f12015-07-24 21:03:07 +000040}
41
Rui Ueyamacacf9672015-10-11 02:22:31 +000042static std::pair<ELFKind, uint16_t> parseEmulation(StringRef S) {
43 if (S == "elf32btsmip") return {ELF32BEKind, EM_MIPS};
44 if (S == "elf32ltsmip") return {ELF32LEKind, EM_MIPS};
45 if (S == "elf32ppc") return {ELF32BEKind, EM_PPC};
46 if (S == "elf64ppc") return {ELF64BEKind, EM_PPC64};
47 if (S == "elf_i386") return {ELF32LEKind, EM_386};
48 if (S == "elf_x86_64") return {ELF64LEKind, EM_X86_64};
49 error("Unknown emulation: " + S);
Denis Protivensky1ef7b3f2015-10-07 09:13:03 +000050}
51
Rui Ueyamaff777682015-10-09 21:12:40 +000052static TargetInfo *createTarget() {
53 switch (Config->EMachine) {
54 case EM_386:
55 return new X86TargetInfo();
56 case EM_AARCH64:
57 return new AArch64TargetInfo();
58 case EM_ARM:
59 return new ARMTargetInfo();
60 case EM_MIPS:
61 return new MipsTargetInfo();
62 case EM_PPC:
63 return new PPCTargetInfo();
64 case EM_PPC64:
65 return new PPC64TargetInfo();
66 case EM_X86_64:
67 return new X86_64TargetInfo();
68 }
69 error("Unknown target machine");
70}
71
Igor Kudrin1309fc02015-09-28 15:01:59 +000072// Makes a path by concatenating Dir and File.
Igor Kudrinf03d2b42015-09-30 10:39:37 +000073// If Dir starts with '=' the result will be preceded by Sysroot,
Igor Kudrin1309fc02015-09-28 15:01:59 +000074// which can be set with --sysroot command line switch.
75static std::string buildSysrootedPath(StringRef Dir, StringRef File) {
76 SmallString<128> Path;
Igor Kudrinf03d2b42015-09-30 10:39:37 +000077 if (Dir.startswith("="))
78 sys::path::append(Path, Config->Sysroot, Dir.substr(1), File);
79 else
80 sys::path::append(Path, Dir, File);
Igor Kudrin1309fc02015-09-28 15:01:59 +000081 return Path.str().str();
82}
83
Rafael Espindolaabb7b282015-09-28 12:52:21 +000084// Searches a given library from input search paths, which are filled
85// from -L command line switches. Returns a path to an existent library file.
86static std::string searchLibrary(StringRef Path) {
87 std::vector<std::string> Names;
88 if (Path[0] == ':') {
89 Names.push_back(Path.drop_front().str());
90 } else {
Igor Kudrind912ee92015-10-01 16:42:03 +000091 if (!Config->Static)
92 Names.push_back((Twine("lib") + Path + ".so").str());
Rafael Espindolaabb7b282015-09-28 12:52:21 +000093 Names.push_back((Twine("lib") + Path + ".a").str());
94 }
Rafael Espindolaabb7b282015-09-28 12:52:21 +000095 for (StringRef Dir : Config->InputSearchPaths) {
96 for (const std::string &Name : Names) {
Igor Kudrin1309fc02015-09-28 15:01:59 +000097 std::string FullPath = buildSysrootedPath(Dir, Name);
98 if (sys::fs::exists(FullPath))
99 return FullPath;
Rafael Espindolaabb7b282015-09-28 12:52:21 +0000100 }
101 }
102 error(Twine("Unable to find library -l") + Path);
103}
104
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000105// Opens and parses a file. Path has to be resolved already.
106// Newly created memory buffers are owned by this driver.
107void LinkerDriver::addFile(StringRef Path) {
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +0000108 using namespace llvm::sys::fs;
Rui Ueyamaa4672402015-10-11 02:03:03 +0000109 if (Config->Verbose)
110 llvm::outs() << Path << "\n";
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000111 auto MBOrErr = MemoryBuffer::getFile(Path);
112 error(MBOrErr, Twine("cannot open ") + Path);
113 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
114 MemoryBufferRef MBRef = MB->getMemBufferRef();
115 OwningMBs.push_back(std::move(MB)); // take MB ownership
116
117 switch (identify_magic(MBRef.getBuffer())) {
118 case file_magic::unknown:
Rui Ueyamaa47ee682015-10-11 01:53:04 +0000119 readLinkerScript(&Alloc, MBRef);
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000120 return;
121 case file_magic::archive:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000122 if (WholeArchive) {
123 auto File = make_unique<ArchiveFile>(MBRef);
124 for (MemoryBufferRef &MB : File->getMembers())
125 Files.push_back(createELFFile<ObjectFile>(MB));
126 OwningArchives.emplace_back(std::move(File));
127 return;
128 }
129 Files.push_back(make_unique<ArchiveFile>(MBRef));
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000130 return;
131 case file_magic::elf_shared_object:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000132 Files.push_back(createELFFile<SharedFile>(MBRef));
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000133 return;
134 default:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000135 Files.push_back(createELFFile<ObjectFile>(MBRef));
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000136 }
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +0000137}
138
Rui Ueyama2cac5842015-10-07 19:34:51 +0000139static StringRef
140getString(opt::InputArgList &Args, unsigned Key, StringRef Default = "") {
141 if (auto *Arg = Args.getLastArg(Key))
142 return Arg->getValue();
143 return Default;
144}
145
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000146void LinkerDriver::main(ArrayRef<const char *> ArgsArr) {
Rui Ueyama9ea49c72015-10-07 23:46:11 +0000147 initSymbols();
148
Rui Ueyamab9732562015-10-11 01:53:07 +0000149 opt::InputArgList Args = ArgParser(&Alloc).parse(ArgsArr);
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000150 createFiles(Args);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000151
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000152 switch (Config->ElfKind) {
153 case ELF32LEKind:
154 link<ELF32LE>(Args);
155 return;
156 case ELF32BEKind:
157 link<ELF32BE>(Args);
158 return;
159 case ELF64LEKind:
160 link<ELF64LE>(Args);
161 return;
162 case ELF64BEKind:
163 link<ELF64BE>(Args);
164 return;
165 default:
166 error("-m or at least a .o file required");
167 }
168}
169
170void LinkerDriver::createFiles(opt::InputArgList &Args) {
Rui Ueyama2cac5842015-10-07 19:34:51 +0000171 for (auto *Arg : Args.filtered(OPT_L))
172 Config->InputSearchPaths.push_back(Arg->getValue());
Igor Kudrin1309fc02015-09-28 15:01:59 +0000173
Rafael Espindola2e9eac12015-09-11 21:18:56 +0000174 std::vector<StringRef> RPaths;
175 for (auto *Arg : Args.filtered(OPT_rpath))
176 RPaths.push_back(Arg->getValue());
177 if (!RPaths.empty())
178 Config->RPath = llvm::join(RPaths.begin(), RPaths.end(), ":");
179
Rui Ueyamacacf9672015-10-11 02:22:31 +0000180 if (auto *Arg = Args.getLastArg(OPT_m)) {
181 std::pair<ELFKind, uint16_t> P = parseEmulation(Arg->getValue());
182 Config->ElfKind = P.first;
183 Config->EMachine = P.second;
184 }
Denis Protivensky1ef7b3f2015-10-07 09:13:03 +0000185
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000186 Config->AllowMultipleDefinition = Args.hasArg(OPT_allow_multiple_definition);
187 Config->DiscardAll = Args.hasArg(OPT_discard_all);
188 Config->DiscardLocals = Args.hasArg(OPT_discard_locals);
189 Config->DiscardNone = Args.hasArg(OPT_discard_none);
Davide Italianoc39c75d2015-10-06 16:20:00 +0000190 Config->EnableNewDtags = !Args.hasArg(OPT_disable_new_dtags);
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000191 Config->ExportDynamic = Args.hasArg(OPT_export_dynamic);
192 Config->NoInhibitExec = Args.hasArg(OPT_noinhibit_exec);
George Rimar57e40de2015-10-01 20:14:45 +0000193 Config->NoUndefined = Args.hasArg(OPT_no_undefined);
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000194 Config->Shared = Args.hasArg(OPT_shared);
Rui Ueyamaa4672402015-10-11 02:03:03 +0000195 Config->Verbose = Args.hasArg(OPT_verbose);
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000196
Rui Ueyama2cac5842015-10-07 19:34:51 +0000197 Config->DynamicLinker = getString(Args, OPT_dynamic_linker);
198 Config->Entry = getString(Args, OPT_entry);
199 Config->Fini = getString(Args, OPT_fini, "_fini");
200 Config->Init = getString(Args, OPT_init, "_init");
Rui Ueyama964ffb32015-10-09 00:33:44 +0000201 Config->OutputFile = getString(Args, OPT_o);
Rui Ueyama2cac5842015-10-07 19:34:51 +0000202 Config->SoName = getString(Args, OPT_soname);
203 Config->Sysroot = getString(Args, OPT_sysroot);
204
Rui Ueyama58d7d702015-10-07 18:22:46 +0000205 for (auto *Arg : Args.filtered(OPT_z))
George Rimar97aad172015-10-07 15:00:21 +0000206 if (Arg->getValue() == StringRef("now"))
207 Config->ZNow = true;
George Rimar97aad172015-10-07 15:00:21 +0000208
Igor Kudrind912ee92015-10-01 16:42:03 +0000209 for (auto *Arg : Args) {
210 switch (Arg->getOption().getID()) {
211 case OPT_l:
212 addFile(searchLibrary(Arg->getValue()));
213 break;
214 case OPT_INPUT:
215 addFile(Arg->getValue());
216 break;
217 case OPT_Bstatic:
218 Config->Static = true;
219 break;
220 case OPT_Bdynamic:
221 Config->Static = false;
222 break;
Igor Kudrin2696bbe2015-10-01 18:02:21 +0000223 case OPT_whole_archive:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000224 WholeArchive = true;
Igor Kudrin2696bbe2015-10-01 18:02:21 +0000225 break;
226 case OPT_no_whole_archive:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000227 WholeArchive = false;
Igor Kudrin2696bbe2015-10-01 18:02:21 +0000228 break;
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +0000229 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000230 }
231
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000232 if (Files.empty())
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +0000233 error("no input files.");
234
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000235 // Set machine type if -m is not given.
236 if (Config->ElfKind == ELFNoneKind) {
237 for (std::unique_ptr<InputFile> &File : Files) {
238 auto *F = dyn_cast<ELFFileBase>(File.get());
239 if (!F)
240 continue;
241 Config->ElfKind = F->getELFKind();
242 Config->EMachine = F->getEMachine();
243 break;
244 }
245 }
246
247 // Check if all files are for the same machine type.
248 for (std::unique_ptr<InputFile> &File : Files) {
249 auto *F = dyn_cast<ELFFileBase>(File.get());
250 if (!F)
251 continue;
252 if (F->getELFKind() == Config->ElfKind &&
253 F->getEMachine() == Config->EMachine)
254 continue;
255 StringRef A = F->getName();
256 StringRef B = Files[0]->getName();
257 if (auto *Arg = Args.getLastArg(OPT_m))
258 B = Arg->getValue();
259 error(A + " is incompatible with " + B);
260 }
261}
262
263template <class ELFT> void LinkerDriver::link(opt::InputArgList &Args) {
264 SymbolTable<ELFT> Symtab;
Rui Ueyamaff777682015-10-09 21:12:40 +0000265 Target.reset(createTarget());
266
267 if (!Config->Shared) {
268 // Add entry symbol.
269 Config->EntrySym = Symtab.addUndefined(
270 Config->Entry.empty() ? Target->getDefaultEntry() : Config->Entry);
271
272 // In the assembly for 32 bit x86 the _GLOBAL_OFFSET_TABLE_ symbol
273 // is magical and is used to produce a R_386_GOTPC relocation.
274 // The R_386_GOTPC relocation value doesn't actually depend on the
275 // symbol value, so it could use an index of STN_UNDEF which, according
276 // to the spec, means the symbol value is 0.
277 // Unfortunately both gas and MC keep the _GLOBAL_OFFSET_TABLE_ symbol in
278 // the object file.
279 // The situation is even stranger on x86_64 where the assembly doesn't
280 // need the magical symbol, but gas still puts _GLOBAL_OFFSET_TABLE_ as
281 // an undefined symbol in the .o files.
282 // Given that the symbol is effectively unused, we just create a dummy
283 // hidden one to avoid the undefined symbol error.
284 Symtab.addIgnoredSym("_GLOBAL_OFFSET_TABLE_");
285 }
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000286
287 for (std::unique_ptr<InputFile> &F : Files)
288 Symtab.addFile(std::move(F));
289
Denis Protivensky22220d52015-10-05 09:43:57 +0000290 for (auto *Arg : Args.filtered(OPT_undefined))
Rui Ueyamaff777682015-10-09 21:12:40 +0000291 Symtab.addUndefinedOpt(Arg->getValue());
Denis Protivensky22220d52015-10-05 09:43:57 +0000292
Rui Ueyamaee592822015-10-07 00:25:09 +0000293 if (Config->OutputFile.empty())
294 Config->OutputFile = "a.out";
295
Rui Ueyama75230392015-10-07 18:29:51 +0000296 // Write the result to the file.
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000297 writeResult<ELFT>(&Symtab);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000298}