blob: bf902adab4cc37b832957c3fecba1a97fb792441 [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 Ueyama83cd6e02016-01-06 20:11:55 +000029Configuration *elf2::Config;
30LinkerDriver *elf2::Driver;
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +000031
Rui Ueyama83cd6e02016-01-06 20:11:55 +000032void 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) {
Rui Ueyamaa561e782015-10-11 19:46:00 +000041 if (S == "elf32btsmip")
42 return {ELF32BEKind, EM_MIPS};
43 if (S == "elf32ltsmip")
44 return {ELF32LEKind, EM_MIPS};
45 if (S == "elf32ppc")
46 return {ELF32BEKind, EM_PPC};
47 if (S == "elf64ppc")
48 return {ELF64BEKind, EM_PPC64};
49 if (S == "elf_i386")
50 return {ELF32LEKind, EM_386};
51 if (S == "elf_x86_64")
52 return {ELF64LEKind, EM_X86_64};
Igor Kudrin2f610d52015-11-20 02:48:53 +000053 if (S == "aarch64linux")
54 return {ELF64LEKind, EM_AARCH64};
Rui Ueyama9aa56862015-11-24 18:55:36 +000055 if (S == "i386pe" || S == "i386pep" || S == "thumb2pe")
56 error("Windows targets are not supported on the ELF frontend: " + S);
Rui Ueyamacacf9672015-10-11 02:22:31 +000057 error("Unknown emulation: " + S);
Denis Protivensky1ef7b3f2015-10-07 09:13:03 +000058}
59
Rui Ueyama9b093692016-01-06 00:51:35 +000060// Returns slices of MB by parsing MB as an archive file.
61// Each slice consists of a member file in the archive.
62static std::vector<MemoryBufferRef> getArchiveMembers(MemoryBufferRef MB) {
63 ErrorOr<std::unique_ptr<Archive>> FileOrErr = Archive::create(MB);
64 error(FileOrErr, "Failed to parse archive");
65 std::unique_ptr<Archive> File = std::move(*FileOrErr);
66
67 std::vector<MemoryBufferRef> V;
68 for (const ErrorOr<Archive::Child> &C : File->children()) {
69 error(C, "Could not get the child of the archive " + File->getFileName());
70 ErrorOr<MemoryBufferRef> MbOrErr = C->getMemoryBufferRef();
71 error(MbOrErr, "Could not get the buffer for a child of the archive " +
72 File->getFileName());
73 V.push_back(*MbOrErr);
74 }
75 return V;
76}
77
Rui Ueyama983ed2b2015-10-01 15:23:09 +000078// Opens and parses a file. Path has to be resolved already.
79// Newly created memory buffers are owned by this driver.
80void LinkerDriver::addFile(StringRef Path) {
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +000081 using namespace llvm::sys::fs;
Rui Ueyamaa4672402015-10-11 02:03:03 +000082 if (Config->Verbose)
83 llvm::outs() << Path << "\n";
Rui Ueyama983ed2b2015-10-01 15:23:09 +000084 auto MBOrErr = MemoryBuffer::getFile(Path);
Rui Ueyama1c42afc2015-10-12 15:49:06 +000085 error(MBOrErr, "cannot open " + Path);
Rui Ueyama983ed2b2015-10-01 15:23:09 +000086 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
87 MemoryBufferRef MBRef = MB->getMemBufferRef();
88 OwningMBs.push_back(std::move(MB)); // take MB ownership
89
90 switch (identify_magic(MBRef.getBuffer())) {
91 case file_magic::unknown:
Rui Ueyamaa47ee682015-10-11 01:53:04 +000092 readLinkerScript(&Alloc, MBRef);
Rui Ueyama983ed2b2015-10-01 15:23:09 +000093 return;
94 case file_magic::archive:
Rui Ueyama3ce825e2015-10-09 21:07:25 +000095 if (WholeArchive) {
Rui Ueyama9b093692016-01-06 00:51:35 +000096 for (MemoryBufferRef MB : getArchiveMembers(MBRef))
Rui Ueyama533c0302016-01-06 00:09:43 +000097 Files.push_back(createObjectFile(MB));
Rui Ueyama3ce825e2015-10-09 21:07:25 +000098 return;
99 }
100 Files.push_back(make_unique<ArchiveFile>(MBRef));
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000101 return;
Rafael Espindolaaf707642015-10-12 01:55:32 +0000102 case file_magic::elf_shared_object:
Rui Ueyama533c0302016-01-06 00:09:43 +0000103 Files.push_back(createSharedFile(MBRef));
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000104 return;
105 default:
Rui Ueyama533c0302016-01-06 00:09:43 +0000106 Files.push_back(createObjectFile(MBRef));
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000107 }
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +0000108}
109
Rui Ueyamad32c63d2016-01-07 17:33:25 +0000110// Some command line options or some combinations of them are not allowed.
111// This function checks for such errors.
112static void checkOptions(opt::InputArgList &Args) {
113 // Traditional linkers can generate re-linkable object files instead
114 // of executables or DSOs. We don't support that since the feature
115 // does not seem to provide more value than the static archiver.
116 if (Args.hasArg(OPT_relocatable))
117 error("-r option is not supported. Use 'ar' command instead.");
118
119 // The MIPS ABI as of 2016 does not support the GNU-style symbol lookup
120 // table which is a relatively new feature.
121 if (Config->EMachine == EM_MIPS && Config->GnuHash)
122 error("The .gnu.hash section is not compatible with the MIPS target.");
123
124 if (Config->EMachine == EM_AMDGPU && !Config->Entry.empty())
125 error("-e option is not valid for AMDGPU.");
126}
127
Rui Ueyama2cac5842015-10-07 19:34:51 +0000128static StringRef
129getString(opt::InputArgList &Args, unsigned Key, StringRef Default = "") {
130 if (auto *Arg = Args.getLastArg(Key))
131 return Arg->getValue();
132 return Default;
133}
134
Rui Ueyama1a8fffa2015-11-12 19:00:37 +0000135static bool hasZOption(opt::InputArgList &Args, StringRef Key) {
136 for (auto *Arg : Args.filtered(OPT_z))
137 if (Key == Arg->getValue())
138 return true;
139 return false;
140}
141
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000142void LinkerDriver::main(ArrayRef<const char *> ArgsArr) {
Rui Ueyama9aa56862015-11-24 18:55:36 +0000143 initSymbols();
Rui Ueyama9ea49c72015-10-07 23:46:11 +0000144
Rui Ueyamac6e1b972015-10-11 18:19:01 +0000145 opt::InputArgList Args = parseArgs(&Alloc, ArgsArr);
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000146 createFiles(Args);
Rui Ueyamad32c63d2016-01-07 17:33:25 +0000147 checkOptions(Args);
Rui Ueyamaf5bcf2a2015-11-12 18:54:15 +0000148
Rui Ueyamae717a712015-10-13 16:20:50 +0000149 switch (Config->EKind) {
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000150 case ELF32LEKind:
151 link<ELF32LE>(Args);
152 return;
153 case ELF32BEKind:
154 link<ELF32BE>(Args);
155 return;
156 case ELF64LEKind:
157 link<ELF64LE>(Args);
158 return;
159 case ELF64BEKind:
160 link<ELF64BE>(Args);
161 return;
162 default:
163 error("-m or at least a .o file required");
164 }
165}
166
167void LinkerDriver::createFiles(opt::InputArgList &Args) {
Rui Ueyama2cac5842015-10-07 19:34:51 +0000168 for (auto *Arg : Args.filtered(OPT_L))
Rui Ueyama52a15092015-10-11 03:28:42 +0000169 Config->SearchPaths.push_back(Arg->getValue());
Igor Kudrin1309fc02015-09-28 15:01:59 +0000170
Rafael Espindola2e9eac12015-09-11 21:18:56 +0000171 std::vector<StringRef> RPaths;
172 for (auto *Arg : Args.filtered(OPT_rpath))
173 RPaths.push_back(Arg->getValue());
174 if (!RPaths.empty())
175 Config->RPath = llvm::join(RPaths.begin(), RPaths.end(), ":");
176
Rui Ueyama9aa56862015-11-24 18:55:36 +0000177 if (auto *Arg = Args.getLastArg(OPT_m)) {
178 StringRef S = Arg->getValue();
179 std::pair<ELFKind, uint16_t> P = parseEmulation(S);
180 Config->EKind = P.first;
181 Config->EMachine = P.second;
182 Config->Emulation = S;
183 }
184
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000185 Config->AllowMultipleDefinition = Args.hasArg(OPT_allow_multiple_definition);
Davide Italianocebb4492015-10-13 21:02:34 +0000186 Config->Bsymbolic = Args.hasArg(OPT_Bsymbolic);
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000187 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);
Rui Ueyamac4aaed92015-10-22 18:49:53 +0000192 Config->GcSections = Args.hasArg(OPT_gc_sections);
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000193 Config->NoInhibitExec = Args.hasArg(OPT_noinhibit_exec);
George Rimar57e40de2015-10-01 20:14:45 +0000194 Config->NoUndefined = Args.hasArg(OPT_no_undefined);
George Rimara5fbebc2015-12-10 09:12:18 +0000195 Config->PrintGcSections = Args.hasArg(OPT_print_gc_sections);
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000196 Config->Shared = Args.hasArg(OPT_shared);
George Rimar5dad7c12015-10-24 08:52:46 +0000197 Config->StripAll = Args.hasArg(OPT_strip_all);
Rui Ueyamaa4672402015-10-11 02:03:03 +0000198 Config->Verbose = Args.hasArg(OPT_verbose);
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000199
Rui Ueyama2cac5842015-10-07 19:34:51 +0000200 Config->DynamicLinker = getString(Args, OPT_dynamic_linker);
201 Config->Entry = getString(Args, OPT_entry);
202 Config->Fini = getString(Args, OPT_fini, "_fini");
203 Config->Init = getString(Args, OPT_init, "_init");
Rui Ueyama964ffb32015-10-09 00:33:44 +0000204 Config->OutputFile = getString(Args, OPT_o);
Rui Ueyama2cac5842015-10-07 19:34:51 +0000205 Config->SoName = getString(Args, OPT_soname);
206 Config->Sysroot = getString(Args, OPT_sysroot);
207
Rui Ueyama7b19c342015-11-24 18:48:16 +0000208 Config->ZExecStack = hasZOption(Args, "execstack");
Rui Ueyama1a8fffa2015-11-12 19:00:37 +0000209 Config->ZNodelete = hasZOption(Args, "nodelete");
210 Config->ZNow = hasZOption(Args, "now");
211 Config->ZOrigin = hasZOption(Args, "origin");
George Rimare3336c02015-11-24 10:15:50 +0000212 Config->ZRelro = !hasZOption(Args, "norelro");
Rui Ueyama1a8fffa2015-11-12 19:00:37 +0000213
Rafael Espindola2c1217d2015-10-23 19:02:19 +0000214 if (auto *Arg = Args.getLastArg(OPT_O)) {
215 StringRef Val = Arg->getValue();
216 if (Val.getAsInteger(10, Config->Optimize))
217 error("Invalid optimization level");
218 }
219
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000220 if (auto *Arg = Args.getLastArg(OPT_hash_style)) {
221 StringRef S = Arg->getValue();
222 if (S == "gnu") {
223 Config->GnuHash = true;
224 Config->SysvHash = false;
225 } else if (S == "both") {
226 Config->GnuHash = true;
227 } else if (S != "sysv")
228 error("Unknown hash style: " + S);
229 }
230
George Rimar83f406c2015-10-19 17:35:12 +0000231 for (auto *Arg : Args.filtered(OPT_undefined))
232 Config->Undefined.push_back(Arg->getValue());
233
Igor Kudrind912ee92015-10-01 16:42:03 +0000234 for (auto *Arg : Args) {
235 switch (Arg->getOption().getID()) {
236 case OPT_l:
237 addFile(searchLibrary(Arg->getValue()));
238 break;
239 case OPT_INPUT:
Davide Italiano4e47d582015-10-11 03:53:36 +0000240 case OPT_script:
Igor Kudrind912ee92015-10-01 16:42:03 +0000241 addFile(Arg->getValue());
242 break;
Rui Ueyama35da9b62015-10-11 20:59:12 +0000243 case OPT_as_needed:
244 Config->AsNeeded = true;
245 break;
246 case OPT_no_as_needed:
247 Config->AsNeeded = false;
248 break;
Igor Kudrind912ee92015-10-01 16:42:03 +0000249 case OPT_Bstatic:
250 Config->Static = true;
251 break;
252 case OPT_Bdynamic:
253 Config->Static = false;
254 break;
Igor Kudrin2696bbe2015-10-01 18:02:21 +0000255 case OPT_whole_archive:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000256 WholeArchive = true;
Igor Kudrin2696bbe2015-10-01 18:02:21 +0000257 break;
258 case OPT_no_whole_archive:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000259 WholeArchive = false;
Igor Kudrin2696bbe2015-10-01 18:02:21 +0000260 break;
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +0000261 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000262 }
263
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000264 if (Files.empty())
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +0000265 error("no input files.");
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000266}
267
268template <class ELFT> void LinkerDriver::link(opt::InputArgList &Args) {
269 SymbolTable<ELFT> Symtab;
Rui Ueyamaff777682015-10-09 21:12:40 +0000270 Target.reset(createTarget());
271
272 if (!Config->Shared) {
273 // Add entry symbol.
Tom Stellard80efb162016-01-07 03:59:08 +0000274 //
275 // There is no entry symbol for AMDGPU binaries, so skip adding one to avoid
276 // having and undefined symbol.
277 if (Config->Entry.empty() && Config->EMachine != EM_AMDGPU)
Rui Ueyama0cef6892015-10-12 15:23:54 +0000278 Config->Entry = (Config->EMachine == EM_MIPS) ? "__start" : "_start";
Rui Ueyama2b675072015-10-14 22:20:57 +0000279
Rui Ueyamaff777682015-10-09 21:12:40 +0000280 // In the assembly for 32 bit x86 the _GLOBAL_OFFSET_TABLE_ symbol
281 // is magical and is used to produce a R_386_GOTPC relocation.
282 // The R_386_GOTPC relocation value doesn't actually depend on the
283 // symbol value, so it could use an index of STN_UNDEF which, according
284 // to the spec, means the symbol value is 0.
285 // Unfortunately both gas and MC keep the _GLOBAL_OFFSET_TABLE_ symbol in
286 // the object file.
287 // The situation is even stranger on x86_64 where the assembly doesn't
288 // need the magical symbol, but gas still puts _GLOBAL_OFFSET_TABLE_ as
289 // an undefined symbol in the .o files.
290 // Given that the symbol is effectively unused, we just create a dummy
291 // hidden one to avoid the undefined symbol error.
Rui Ueyamadd7d9982015-12-16 22:31:14 +0000292 Symtab.addIgnored("_GLOBAL_OFFSET_TABLE_");
Rui Ueyamaff777682015-10-09 21:12:40 +0000293 }
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000294
Ed Mastef2ac6882015-12-11 17:46:46 +0000295 if (!Config->Entry.empty()) {
296 // Set either EntryAddr (if S is a number) or EntrySym (otherwise).
297 StringRef S = Config->Entry;
298 if (S.getAsInteger(0, Config->EntryAddr))
299 Config->EntrySym = Symtab.addUndefined(S);
300 }
301
Rui Ueyama27161302015-12-16 21:35:39 +0000302 if (Config->EMachine == EM_MIPS) {
303 // On MIPS O32 ABI, _gp_disp is a magic symbol designates offset between
304 // start of function and gp pointer into GOT.
Rui Ueyamadd7d9982015-12-16 22:31:14 +0000305 Config->MipsGpDisp = Symtab.addIgnored("_gp_disp");
Rui Ueyama27161302015-12-16 21:35:39 +0000306
307 // Define _gp for MIPS. st_value of _gp symbol will be updated by Writer
308 // so that it points to an absolute address which is relative to GOT.
309 // See "Global Data Symbols" in Chapter 6 in the following document:
310 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000311 Symtab.addAbsolute("_gp", ElfSym<ELFT>::MipsGp);
Rui Ueyama27161302015-12-16 21:35:39 +0000312 }
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000313
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000314 for (std::unique_ptr<InputFile> &F : Files)
315 Symtab.addFile(std::move(F));
316
Rui Ueyamaf215dac2015-10-19 20:55:28 +0000317 for (StringRef S : Config->Undefined)
318 Symtab.addUndefinedOpt(S);
Denis Protivensky22220d52015-10-05 09:43:57 +0000319
Rui Ueyamadeb15402016-01-07 17:20:07 +0000320 for (auto *Arg : Args.filtered(OPT_wrap))
321 Symtab.wrap(Arg->getValue());
322
Rui Ueyamaee592822015-10-07 00:25:09 +0000323 if (Config->OutputFile.empty())
324 Config->OutputFile = "a.out";
325
Rui Ueyama75230392015-10-07 18:29:51 +0000326 // Write the result to the file.
Rui Ueyama93bfee52015-10-13 18:10:33 +0000327 Symtab.scanShlibUndefined();
Rui Ueyamac4aaed92015-10-22 18:49:53 +0000328 if (Config->GcSections)
329 markLive<ELFT>(&Symtab);
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000330 writeResult<ELFT>(&Symtab);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000331}