blob: e592a739d1ef8db1569bc52495ec1206198f850e [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 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
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};
Rui Ueyamacacf9672015-10-11 02:22:31 +000053 error("Unknown emulation: " + S);
Denis Protivensky1ef7b3f2015-10-07 09:13:03 +000054}
55
Rui Ueyama983ed2b2015-10-01 15:23:09 +000056// Opens and parses a file. Path has to be resolved already.
57// Newly created memory buffers are owned by this driver.
58void LinkerDriver::addFile(StringRef Path) {
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +000059 using namespace llvm::sys::fs;
Rui Ueyamaa4672402015-10-11 02:03:03 +000060 if (Config->Verbose)
61 llvm::outs() << Path << "\n";
Rui Ueyama983ed2b2015-10-01 15:23:09 +000062 auto MBOrErr = MemoryBuffer::getFile(Path);
Rui Ueyama1c42afc2015-10-12 15:49:06 +000063 error(MBOrErr, "cannot open " + Path);
Rui Ueyama983ed2b2015-10-01 15:23:09 +000064 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
65 MemoryBufferRef MBRef = MB->getMemBufferRef();
66 OwningMBs.push_back(std::move(MB)); // take MB ownership
67
68 switch (identify_magic(MBRef.getBuffer())) {
69 case file_magic::unknown:
Rui Ueyamaa47ee682015-10-11 01:53:04 +000070 readLinkerScript(&Alloc, MBRef);
Rui Ueyama983ed2b2015-10-01 15:23:09 +000071 return;
72 case file_magic::archive:
Rui Ueyama3ce825e2015-10-09 21:07:25 +000073 if (WholeArchive) {
74 auto File = make_unique<ArchiveFile>(MBRef);
75 for (MemoryBufferRef &MB : File->getMembers())
76 Files.push_back(createELFFile<ObjectFile>(MB));
77 OwningArchives.emplace_back(std::move(File));
78 return;
79 }
80 Files.push_back(make_unique<ArchiveFile>(MBRef));
Rui Ueyama983ed2b2015-10-01 15:23:09 +000081 return;
Rafael Espindolaaf707642015-10-12 01:55:32 +000082 case file_magic::elf_shared_object:
83 Files.push_back(createELFFile<SharedFile>(MBRef));
Rui Ueyama983ed2b2015-10-01 15:23:09 +000084 return;
85 default:
Rui Ueyama3ce825e2015-10-09 21:07:25 +000086 Files.push_back(createELFFile<ObjectFile>(MBRef));
Rui Ueyama983ed2b2015-10-01 15:23:09 +000087 }
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +000088}
89
Rui Ueyama2cac5842015-10-07 19:34:51 +000090static StringRef
91getString(opt::InputArgList &Args, unsigned Key, StringRef Default = "") {
92 if (auto *Arg = Args.getLastArg(Key))
93 return Arg->getValue();
94 return Default;
95}
96
Rui Ueyama1a8fffa2015-11-12 19:00:37 +000097static bool hasZOption(opt::InputArgList &Args, StringRef Key) {
98 for (auto *Arg : Args.filtered(OPT_z))
99 if (Key == Arg->getValue())
100 return true;
101 return false;
102}
103
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000104void LinkerDriver::main(ArrayRef<const char *> ArgsArr) {
Rui Ueyama9ea49c72015-10-07 23:46:11 +0000105 initSymbols();
106
Rui Ueyamac6e1b972015-10-11 18:19:01 +0000107 opt::InputArgList Args = parseArgs(&Alloc, ArgsArr);
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000108 createFiles(Args);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000109
Rui Ueyamaf5bcf2a2015-11-12 18:54:15 +0000110 // Traditional linkers can generate re-linkable object files instead
111 // of executables or DSOs. We don't support that since the feature
112 // does not seem to provide more value than the static archiver.
113 if (Args.hasArg(OPT_relocatable))
114 error("-r option is not supported. Use 'ar' command instead.");
115
Rui Ueyamae717a712015-10-13 16:20:50 +0000116 switch (Config->EKind) {
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000117 case ELF32LEKind:
118 link<ELF32LE>(Args);
119 return;
120 case ELF32BEKind:
121 link<ELF32BE>(Args);
122 return;
123 case ELF64LEKind:
124 link<ELF64LE>(Args);
125 return;
126 case ELF64BEKind:
127 link<ELF64BE>(Args);
128 return;
129 default:
130 error("-m or at least a .o file required");
131 }
132}
133
134void LinkerDriver::createFiles(opt::InputArgList &Args) {
Rui Ueyama2cac5842015-10-07 19:34:51 +0000135 for (auto *Arg : Args.filtered(OPT_L))
Rui Ueyama52a15092015-10-11 03:28:42 +0000136 Config->SearchPaths.push_back(Arg->getValue());
Igor Kudrin1309fc02015-09-28 15:01:59 +0000137
Rafael Espindola2e9eac12015-09-11 21:18:56 +0000138 std::vector<StringRef> RPaths;
139 for (auto *Arg : Args.filtered(OPT_rpath))
140 RPaths.push_back(Arg->getValue());
141 if (!RPaths.empty())
142 Config->RPath = llvm::join(RPaths.begin(), RPaths.end(), ":");
143
Rui Ueyamacacf9672015-10-11 02:22:31 +0000144 if (auto *Arg = Args.getLastArg(OPT_m)) {
Rui Ueyama305a7d32015-10-27 19:29:27 +0000145 StringRef S = Arg->getValue();
146 std::pair<ELFKind, uint16_t> P = parseEmulation(S);
Rui Ueyamae717a712015-10-13 16:20:50 +0000147 Config->EKind = P.first;
Rui Ueyamacacf9672015-10-11 02:22:31 +0000148 Config->EMachine = P.second;
Rui Ueyama305a7d32015-10-27 19:29:27 +0000149 Config->Emulation = S;
Rui Ueyamacacf9672015-10-11 02:22:31 +0000150 }
Denis Protivensky1ef7b3f2015-10-07 09:13:03 +0000151
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000152 Config->AllowMultipleDefinition = Args.hasArg(OPT_allow_multiple_definition);
Davide Italianocebb4492015-10-13 21:02:34 +0000153 Config->Bsymbolic = Args.hasArg(OPT_Bsymbolic);
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000154 Config->DiscardAll = Args.hasArg(OPT_discard_all);
155 Config->DiscardLocals = Args.hasArg(OPT_discard_locals);
156 Config->DiscardNone = Args.hasArg(OPT_discard_none);
Davide Italianoc39c75d2015-10-06 16:20:00 +0000157 Config->EnableNewDtags = !Args.hasArg(OPT_disable_new_dtags);
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000158 Config->ExportDynamic = Args.hasArg(OPT_export_dynamic);
Rui Ueyamac4aaed92015-10-22 18:49:53 +0000159 Config->GcSections = Args.hasArg(OPT_gc_sections);
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000160 Config->NoInhibitExec = Args.hasArg(OPT_noinhibit_exec);
George Rimar57e40de2015-10-01 20:14:45 +0000161 Config->NoUndefined = Args.hasArg(OPT_no_undefined);
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000162 Config->Shared = Args.hasArg(OPT_shared);
George Rimar5dad7c12015-10-24 08:52:46 +0000163 Config->StripAll = Args.hasArg(OPT_strip_all);
Rui Ueyamaa4672402015-10-11 02:03:03 +0000164 Config->Verbose = Args.hasArg(OPT_verbose);
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000165
Rui Ueyama2cac5842015-10-07 19:34:51 +0000166 Config->DynamicLinker = getString(Args, OPT_dynamic_linker);
167 Config->Entry = getString(Args, OPT_entry);
168 Config->Fini = getString(Args, OPT_fini, "_fini");
169 Config->Init = getString(Args, OPT_init, "_init");
Rui Ueyama964ffb32015-10-09 00:33:44 +0000170 Config->OutputFile = getString(Args, OPT_o);
Rui Ueyama2cac5842015-10-07 19:34:51 +0000171 Config->SoName = getString(Args, OPT_soname);
172 Config->Sysroot = getString(Args, OPT_sysroot);
173
Rui Ueyama1a8fffa2015-11-12 19:00:37 +0000174 Config->ZNodelete = hasZOption(Args, "nodelete");
175 Config->ZNow = hasZOption(Args, "now");
176 Config->ZOrigin = hasZOption(Args, "origin");
177
Rafael Espindola2c1217d2015-10-23 19:02:19 +0000178 if (auto *Arg = Args.getLastArg(OPT_O)) {
179 StringRef Val = Arg->getValue();
180 if (Val.getAsInteger(10, Config->Optimize))
181 error("Invalid optimization level");
182 }
183
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000184 if (auto *Arg = Args.getLastArg(OPT_hash_style)) {
185 StringRef S = Arg->getValue();
186 if (S == "gnu") {
187 Config->GnuHash = true;
188 Config->SysvHash = false;
189 } else if (S == "both") {
190 Config->GnuHash = true;
191 } else if (S != "sysv")
192 error("Unknown hash style: " + S);
193 }
194
George Rimar83f406c2015-10-19 17:35:12 +0000195 for (auto *Arg : Args.filtered(OPT_undefined))
196 Config->Undefined.push_back(Arg->getValue());
197
Igor Kudrind912ee92015-10-01 16:42:03 +0000198 for (auto *Arg : Args) {
199 switch (Arg->getOption().getID()) {
200 case OPT_l:
201 addFile(searchLibrary(Arg->getValue()));
202 break;
203 case OPT_INPUT:
Davide Italiano4e47d582015-10-11 03:53:36 +0000204 case OPT_script:
Igor Kudrind912ee92015-10-01 16:42:03 +0000205 addFile(Arg->getValue());
206 break;
Rui Ueyama35da9b62015-10-11 20:59:12 +0000207 case OPT_as_needed:
208 Config->AsNeeded = true;
209 break;
210 case OPT_no_as_needed:
211 Config->AsNeeded = false;
212 break;
Igor Kudrind912ee92015-10-01 16:42:03 +0000213 case OPT_Bstatic:
214 Config->Static = true;
215 break;
216 case OPT_Bdynamic:
217 Config->Static = false;
218 break;
Igor Kudrin2696bbe2015-10-01 18:02:21 +0000219 case OPT_whole_archive:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000220 WholeArchive = true;
Igor Kudrin2696bbe2015-10-01 18:02:21 +0000221 break;
222 case OPT_no_whole_archive:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000223 WholeArchive = false;
Igor Kudrin2696bbe2015-10-01 18:02:21 +0000224 break;
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +0000225 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000226 }
227
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000228 if (Files.empty())
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +0000229 error("no input files.");
Igor Kudrinf4cdfe882015-11-12 04:08:12 +0000230
231 if (Config->GnuHash && Config->EMachine == EM_MIPS)
232 error("The .gnu.hash section is not compatible with the MIPS target.");
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000233}
234
235template <class ELFT> void LinkerDriver::link(opt::InputArgList &Args) {
236 SymbolTable<ELFT> Symtab;
Rui Ueyamaff777682015-10-09 21:12:40 +0000237 Target.reset(createTarget());
238
239 if (!Config->Shared) {
240 // Add entry symbol.
Rui Ueyama0cef6892015-10-12 15:23:54 +0000241 if (Config->Entry.empty())
242 Config->Entry = (Config->EMachine == EM_MIPS) ? "__start" : "_start";
Rui Ueyama2b675072015-10-14 22:20:57 +0000243
244 // Set either EntryAddr (if S is a number) or EntrySym (otherwise).
245 StringRef S = Config->Entry;
246 if (S.getAsInteger(0, Config->EntryAddr))
247 Config->EntrySym = Symtab.addUndefined(S);
Rui Ueyamaff777682015-10-09 21:12:40 +0000248
249 // In the assembly for 32 bit x86 the _GLOBAL_OFFSET_TABLE_ symbol
250 // is magical and is used to produce a R_386_GOTPC relocation.
251 // The R_386_GOTPC relocation value doesn't actually depend on the
252 // symbol value, so it could use an index of STN_UNDEF which, according
253 // to the spec, means the symbol value is 0.
254 // Unfortunately both gas and MC keep the _GLOBAL_OFFSET_TABLE_ symbol in
255 // the object file.
256 // The situation is even stranger on x86_64 where the assembly doesn't
257 // need the magical symbol, but gas still puts _GLOBAL_OFFSET_TABLE_ as
258 // an undefined symbol in the .o files.
259 // Given that the symbol is effectively unused, we just create a dummy
260 // hidden one to avoid the undefined symbol error.
261 Symtab.addIgnoredSym("_GLOBAL_OFFSET_TABLE_");
262 }
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000263
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000264 // Define _gp for MIPS. st_value of _gp symbol will be updated by Writer
265 // so that it points to an absolute address which is relative to GOT.
266 // See "Global Data Symbols" in Chapter 6 in the following document:
267 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
268 if (Config->EMachine == EM_MIPS)
269 Symtab.addAbsoluteSym("_gp", DefinedAbsolute<ELFT>::MipsGp);
270
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000271 for (std::unique_ptr<InputFile> &F : Files)
272 Symtab.addFile(std::move(F));
273
Rui Ueyamaf215dac2015-10-19 20:55:28 +0000274 for (StringRef S : Config->Undefined)
275 Symtab.addUndefinedOpt(S);
Denis Protivensky22220d52015-10-05 09:43:57 +0000276
Rui Ueyamaee592822015-10-07 00:25:09 +0000277 if (Config->OutputFile.empty())
278 Config->OutputFile = "a.out";
279
Rui Ueyama75230392015-10-07 18:29:51 +0000280 // Write the result to the file.
Rui Ueyama93bfee52015-10-13 18:10:33 +0000281 Symtab.scanShlibUndefined();
Rui Ueyamac4aaed92015-10-22 18:49:53 +0000282 if (Config->GcSections)
283 markLive<ELFT>(&Symtab);
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000284 writeResult<ELFT>(&Symtab);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000285}