blob: 8e12dee6f65ed56a237bbe63df9a765992af5d88 [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"
Michael J. Spencer84487f12015-07-24 21:03:07 +000015#include "Writer.h"
16#include "llvm/ADT/STLExtras.h"
Rafael Espindola2e9eac12015-09-11 21:18:56 +000017#include "llvm/ADT/StringExtras.h"
Rafael Espindolaf98d6d82015-09-03 20:03:54 +000018#include "llvm/Support/FileSystem.h"
Rafael Espindolaabb7b282015-09-28 12:52:21 +000019#include "llvm/Support/Path.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000020
21using namespace llvm;
Denis Protivensky1ef7b3f2015-10-07 09:13:03 +000022using namespace llvm::ELF;
Rui Ueyama3ce825e2015-10-09 21:07:25 +000023using namespace llvm::object;
Michael J. Spencer84487f12015-07-24 21:03:07 +000024
25using namespace lld;
26using namespace lld::elf2;
27
Rui Ueyama983ed2b2015-10-01 15:23:09 +000028Configuration *lld::elf2::Config;
29LinkerDriver *lld::elf2::Driver;
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +000030
Rui Ueyama983ed2b2015-10-01 15:23:09 +000031void lld::elf2::link(ArrayRef<const char *> Args) {
Rui Ueyama570752c2015-08-18 09:13:25 +000032 Configuration C;
Rui Ueyama983ed2b2015-10-01 15:23:09 +000033 LinkerDriver D;
Rui Ueyama570752c2015-08-18 09:13:25 +000034 Config = &C;
Rui Ueyama983ed2b2015-10-01 15:23:09 +000035 Driver = &D;
Rui Ueyama3ce825e2015-10-09 21:07:25 +000036 Driver->main(Args.slice(1));
Michael J. Spencer84487f12015-07-24 21:03:07 +000037}
38
Denis Protivensky1ef7b3f2015-10-07 09:13:03 +000039static void setELFType(StringRef Emul) {
40 if (Emul == "elf_i386") {
41 Config->ElfKind = ELF32LEKind;
42 Config->EMachine = EM_386;
43 return;
44 }
45 if (Emul == "elf_x86_64") {
46 Config->ElfKind = ELF64LEKind;
47 Config->EMachine = EM_X86_64;
48 return;
49 }
Simon Atanasyan456bd052015-10-08 12:13:38 +000050 if (Emul == "elf32ltsmip") {
51 Config->ElfKind = ELF32LEKind;
52 Config->EMachine = EM_MIPS;
53 return;
54 }
55 if (Emul == "elf32btsmip") {
56 Config->ElfKind = ELF32BEKind;
57 Config->EMachine = EM_MIPS;
58 return;
59 }
Denis Protivensky1ef7b3f2015-10-07 09:13:03 +000060 if (Emul == "elf32ppc") {
61 Config->ElfKind = ELF32BEKind;
62 Config->EMachine = EM_PPC;
63 return;
64 }
65 if (Emul == "elf64ppc") {
66 Config->ElfKind = ELF64BEKind;
67 Config->EMachine = EM_PPC64;
68 return;
69 }
70 error(Twine("Unknown emulation: ") + Emul);
71}
72
Igor Kudrin1309fc02015-09-28 15:01:59 +000073// Makes a path by concatenating Dir and File.
Igor Kudrinf03d2b42015-09-30 10:39:37 +000074// If Dir starts with '=' the result will be preceded by Sysroot,
Igor Kudrin1309fc02015-09-28 15:01:59 +000075// which can be set with --sysroot command line switch.
76static std::string buildSysrootedPath(StringRef Dir, StringRef File) {
77 SmallString<128> Path;
Igor Kudrinf03d2b42015-09-30 10:39:37 +000078 if (Dir.startswith("="))
79 sys::path::append(Path, Config->Sysroot, Dir.substr(1), File);
80 else
81 sys::path::append(Path, Dir, File);
Igor Kudrin1309fc02015-09-28 15:01:59 +000082 return Path.str().str();
83}
84
Rafael Espindolaabb7b282015-09-28 12:52:21 +000085// Searches a given library from input search paths, which are filled
86// from -L command line switches. Returns a path to an existent library file.
87static std::string searchLibrary(StringRef Path) {
88 std::vector<std::string> Names;
89 if (Path[0] == ':') {
90 Names.push_back(Path.drop_front().str());
91 } else {
Igor Kudrind912ee92015-10-01 16:42:03 +000092 if (!Config->Static)
93 Names.push_back((Twine("lib") + Path + ".so").str());
Rafael Espindolaabb7b282015-09-28 12:52:21 +000094 Names.push_back((Twine("lib") + Path + ".a").str());
95 }
Rafael Espindolaabb7b282015-09-28 12:52:21 +000096 for (StringRef Dir : Config->InputSearchPaths) {
97 for (const std::string &Name : Names) {
Igor Kudrin1309fc02015-09-28 15:01:59 +000098 std::string FullPath = buildSysrootedPath(Dir, Name);
99 if (sys::fs::exists(FullPath))
100 return FullPath;
Rafael Espindolaabb7b282015-09-28 12:52:21 +0000101 }
102 }
103 error(Twine("Unable to find library -l") + Path);
104}
105
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000106// Opens and parses a file. Path has to be resolved already.
107// Newly created memory buffers are owned by this driver.
108void LinkerDriver::addFile(StringRef Path) {
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +0000109 using namespace llvm::sys::fs;
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000110 auto MBOrErr = MemoryBuffer::getFile(Path);
111 error(MBOrErr, Twine("cannot open ") + Path);
112 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
113 MemoryBufferRef MBRef = MB->getMemBufferRef();
114 OwningMBs.push_back(std::move(MB)); // take MB ownership
115
116 switch (identify_magic(MBRef.getBuffer())) {
117 case file_magic::unknown:
118 readLinkerScript(MBRef);
119 return;
120 case file_magic::archive:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000121 if (WholeArchive) {
122 auto File = make_unique<ArchiveFile>(MBRef);
123 for (MemoryBufferRef &MB : File->getMembers())
124 Files.push_back(createELFFile<ObjectFile>(MB));
125 OwningArchives.emplace_back(std::move(File));
126 return;
127 }
128 Files.push_back(make_unique<ArchiveFile>(MBRef));
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000129 return;
130 case file_magic::elf_shared_object:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000131 Files.push_back(createELFFile<SharedFile>(MBRef));
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000132 return;
133 default:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000134 Files.push_back(createELFFile<ObjectFile>(MBRef));
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000135 }
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +0000136}
137
Rui Ueyama2cac5842015-10-07 19:34:51 +0000138static StringRef
139getString(opt::InputArgList &Args, unsigned Key, StringRef Default = "") {
140 if (auto *Arg = Args.getLastArg(Key))
141 return Arg->getValue();
142 return Default;
143}
144
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000145void LinkerDriver::main(ArrayRef<const char *> ArgsArr) {
Rui Ueyama9ea49c72015-10-07 23:46:11 +0000146 initSymbols();
147
Michael J. Spencer84487f12015-07-24 21:03:07 +0000148 opt::InputArgList Args = Parser.parse(ArgsArr);
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000149 createFiles(Args);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000150
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000151 switch (Config->ElfKind) {
152 case ELF32LEKind:
153 link<ELF32LE>(Args);
154 return;
155 case ELF32BEKind:
156 link<ELF32BE>(Args);
157 return;
158 case ELF64LEKind:
159 link<ELF64LE>(Args);
160 return;
161 case ELF64BEKind:
162 link<ELF64BE>(Args);
163 return;
164 default:
165 error("-m or at least a .o file required");
166 }
167}
168
169void LinkerDriver::createFiles(opt::InputArgList &Args) {
Rui Ueyama2cac5842015-10-07 19:34:51 +0000170 for (auto *Arg : Args.filtered(OPT_L))
171 Config->InputSearchPaths.push_back(Arg->getValue());
Igor Kudrin1309fc02015-09-28 15:01:59 +0000172
Rafael Espindola2e9eac12015-09-11 21:18:56 +0000173 std::vector<StringRef> RPaths;
174 for (auto *Arg : Args.filtered(OPT_rpath))
175 RPaths.push_back(Arg->getValue());
176 if (!RPaths.empty())
177 Config->RPath = llvm::join(RPaths.begin(), RPaths.end(), ":");
178
Denis Protivensky1ef7b3f2015-10-07 09:13:03 +0000179 if (auto *Arg = Args.getLastArg(OPT_m))
180 setELFType(Arg->getValue());
181
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000182 Config->AllowMultipleDefinition = Args.hasArg(OPT_allow_multiple_definition);
183 Config->DiscardAll = Args.hasArg(OPT_discard_all);
184 Config->DiscardLocals = Args.hasArg(OPT_discard_locals);
185 Config->DiscardNone = Args.hasArg(OPT_discard_none);
Davide Italianoc39c75d2015-10-06 16:20:00 +0000186 Config->EnableNewDtags = !Args.hasArg(OPT_disable_new_dtags);
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000187 Config->ExportDynamic = Args.hasArg(OPT_export_dynamic);
188 Config->NoInhibitExec = Args.hasArg(OPT_noinhibit_exec);
George Rimar57e40de2015-10-01 20:14:45 +0000189 Config->NoUndefined = Args.hasArg(OPT_no_undefined);
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000190 Config->Shared = Args.hasArg(OPT_shared);
191
Rui Ueyama2cac5842015-10-07 19:34:51 +0000192 Config->DynamicLinker = getString(Args, OPT_dynamic_linker);
193 Config->Entry = getString(Args, OPT_entry);
194 Config->Fini = getString(Args, OPT_fini, "_fini");
195 Config->Init = getString(Args, OPT_init, "_init");
Rui Ueyama964ffb32015-10-09 00:33:44 +0000196 Config->OutputFile = getString(Args, OPT_o);
Rui Ueyama2cac5842015-10-07 19:34:51 +0000197 Config->SoName = getString(Args, OPT_soname);
198 Config->Sysroot = getString(Args, OPT_sysroot);
199
Rui Ueyama58d7d702015-10-07 18:22:46 +0000200 for (auto *Arg : Args.filtered(OPT_z))
George Rimar97aad172015-10-07 15:00:21 +0000201 if (Arg->getValue() == StringRef("now"))
202 Config->ZNow = true;
George Rimar97aad172015-10-07 15:00:21 +0000203
Igor Kudrind912ee92015-10-01 16:42:03 +0000204 for (auto *Arg : Args) {
205 switch (Arg->getOption().getID()) {
206 case OPT_l:
207 addFile(searchLibrary(Arg->getValue()));
208 break;
209 case OPT_INPUT:
210 addFile(Arg->getValue());
211 break;
212 case OPT_Bstatic:
213 Config->Static = true;
214 break;
215 case OPT_Bdynamic:
216 Config->Static = false;
217 break;
Igor Kudrin2696bbe2015-10-01 18:02:21 +0000218 case OPT_whole_archive:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000219 WholeArchive = true;
Igor Kudrin2696bbe2015-10-01 18:02:21 +0000220 break;
221 case OPT_no_whole_archive:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000222 WholeArchive = false;
Igor Kudrin2696bbe2015-10-01 18:02:21 +0000223 break;
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +0000224 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000225 }
226
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000227 if (Files.empty())
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +0000228 error("no input files.");
229
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000230 // Set machine type if -m is not given.
231 if (Config->ElfKind == ELFNoneKind) {
232 for (std::unique_ptr<InputFile> &File : Files) {
233 auto *F = dyn_cast<ELFFileBase>(File.get());
234 if (!F)
235 continue;
236 Config->ElfKind = F->getELFKind();
237 Config->EMachine = F->getEMachine();
238 break;
239 }
240 }
241
242 // Check if all files are for the same machine type.
243 for (std::unique_ptr<InputFile> &File : Files) {
244 auto *F = dyn_cast<ELFFileBase>(File.get());
245 if (!F)
246 continue;
247 if (F->getELFKind() == Config->ElfKind &&
248 F->getEMachine() == Config->EMachine)
249 continue;
250 StringRef A = F->getName();
251 StringRef B = Files[0]->getName();
252 if (auto *Arg = Args.getLastArg(OPT_m))
253 B = Arg->getValue();
254 error(A + " is incompatible with " + B);
255 }
256}
257
258template <class ELFT> void LinkerDriver::link(opt::InputArgList &Args) {
259 SymbolTable<ELFT> Symtab;
260
261 for (std::unique_ptr<InputFile> &F : Files)
262 Symtab.addFile(std::move(F));
263
Denis Protivensky22220d52015-10-05 09:43:57 +0000264 for (auto *Arg : Args.filtered(OPT_undefined))
265 Symtab.addUndefinedSym(Arg->getValue());
266
Rui Ueyamaee592822015-10-07 00:25:09 +0000267 if (Config->OutputFile.empty())
268 Config->OutputFile = "a.out";
269
Rui Ueyama75230392015-10-07 18:29:51 +0000270 // Write the result to the file.
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000271 writeResult<ELFT>(&Symtab);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000272}