blob: 320f9cbef0fa09f1b4bec1a185fa9637b6491d5a [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 Ueyama0b289522016-02-25 18:43:51 +000013#include "ICF.h"
Rui Ueyamaafff74e22015-08-05 23:24:46 +000014#include "InputFiles.h"
Rui Ueyama717677a2016-02-11 21:17:59 +000015#include "LinkerScript.h"
Rui Ueyamaafff74e22015-08-05 23:24:46 +000016#include "SymbolTable.h"
Rui Ueyamaff777682015-10-09 21:12:40 +000017#include "Target.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000018#include "Writer.h"
Rui Ueyamaa453c0a2016-03-02 19:08:05 +000019#include "lld/Driver/Driver.h"
Rafael Espindola2e9eac12015-09-11 21:18:56 +000020#include "llvm/ADT/StringExtras.h"
Rafael Espindola9f77ef02016-02-12 20:54:57 +000021#include "llvm/Support/TargetSelect.h"
Rui Ueyamaa4672402015-10-11 02:03:03 +000022#include "llvm/Support/raw_ostream.h"
Rui Ueyamacacf9672015-10-11 02:22:31 +000023#include <utility>
Michael J. Spencer84487f12015-07-24 21:03:07 +000024
25using namespace llvm;
Denis Protivensky1ef7b3f2015-10-07 09:13:03 +000026using namespace llvm::ELF;
Rui Ueyama3ce825e2015-10-09 21:07:25 +000027using namespace llvm::object;
Michael J. Spencer84487f12015-07-24 21:03:07 +000028
29using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:54 +000030using namespace lld::elf;
Michael J. Spencer84487f12015-07-24 21:03:07 +000031
Rafael Espindolae0df00b2016-02-28 00:25:54 +000032Configuration *elf::Config;
33LinkerDriver *elf::Driver;
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +000034
Rafael Espindolae0df00b2016-02-28 00:25:54 +000035bool elf::link(ArrayRef<const char *> Args, raw_ostream &Error) {
Rui Ueyama64cfffd2016-01-28 18:40:06 +000036 HasError = false;
Rui Ueyamab6940112016-02-02 22:49:32 +000037 ErrorOS = &Error;
Rui Ueyama570752c2015-08-18 09:13:25 +000038 Configuration C;
Rui Ueyama983ed2b2015-10-01 15:23:09 +000039 LinkerDriver D;
Rui Ueyama717677a2016-02-11 21:17:59 +000040 LinkerScript LS;
Rui Ueyama570752c2015-08-18 09:13:25 +000041 Config = &C;
Rui Ueyama983ed2b2015-10-01 15:23:09 +000042 Driver = &D;
Rui Ueyama717677a2016-02-11 21:17:59 +000043 Script = &LS;
Rui Ueyama1eb9f442016-02-28 03:18:09 +000044 Driver->main(Args);
Rui Ueyama64cfffd2016-01-28 18:40:06 +000045 return !HasError;
Michael J. Spencer84487f12015-07-24 21:03:07 +000046}
47
Rui Ueyamacacf9672015-10-11 02:22:31 +000048static std::pair<ELFKind, uint16_t> parseEmulation(StringRef S) {
Ed Mastee2b76772016-03-31 20:26:30 +000049 if (S.endswith("_fbsd"))
50 S = S.drop_back(5);
Rui Ueyamaa561e782015-10-11 19:46:00 +000051 if (S == "elf32btsmip")
52 return {ELF32BEKind, EM_MIPS};
53 if (S == "elf32ltsmip")
54 return {ELF32LEKind, EM_MIPS};
Ed Mastee2b76772016-03-31 20:26:30 +000055 if (S == "elf32ppc")
Rui Ueyamaa561e782015-10-11 19:46:00 +000056 return {ELF32BEKind, EM_PPC};
Ed Mastee2b76772016-03-31 20:26:30 +000057 if (S == "elf64ppc")
Rui Ueyamaa561e782015-10-11 19:46:00 +000058 return {ELF64BEKind, EM_PPC64};
59 if (S == "elf_i386")
60 return {ELF32LEKind, EM_386};
61 if (S == "elf_x86_64")
62 return {ELF64LEKind, EM_X86_64};
Igor Kudrin2f610d52015-11-20 02:48:53 +000063 if (S == "aarch64linux")
64 return {ELF64LEKind, EM_AARCH64};
Rui Ueyama9aa56862015-11-24 18:55:36 +000065 if (S == "i386pe" || S == "i386pep" || S == "thumb2pe")
Rui Ueyama7f4a4922016-03-13 03:17:42 +000066 error("Windows targets are not supported on the ELF frontend: " + S);
Rui Ueyama21eecb42016-02-02 21:13:09 +000067 else
George Rimar777f9632016-03-12 08:31:34 +000068 error("unknown emulation: " + S);
Rui Ueyama21eecb42016-02-02 21:13:09 +000069 return {ELFNoneKind, 0};
Denis Protivensky1ef7b3f2015-10-07 09:13:03 +000070}
71
Rui Ueyama9b093692016-01-06 00:51:35 +000072// Returns slices of MB by parsing MB as an archive file.
73// Each slice consists of a member file in the archive.
Peter Collingbourned418b1d2016-03-31 23:12:18 +000074std::vector<MemoryBufferRef>
75LinkerDriver::getArchiveMembers(MemoryBufferRef MB) {
Rafael Espindola1130935c2016-03-03 16:21:44 +000076 std::unique_ptr<Archive> File =
Rui Ueyama64bd8df2016-03-14 21:31:07 +000077 check(Archive::create(MB), "failed to parse archive");
Rui Ueyama9b093692016-01-06 00:51:35 +000078
79 std::vector<MemoryBufferRef> V;
Rafael Espindola1130935c2016-03-03 16:21:44 +000080 for (const ErrorOr<Archive::Child> &COrErr : File->children()) {
Rui Ueyama64bd8df2016-03-14 21:31:07 +000081 Archive::Child C = check(COrErr, "could not get the child of the archive " +
Rafael Espindola1130935c2016-03-03 16:21:44 +000082 File->getFileName());
83 MemoryBufferRef Mb =
Rafael Espindola75714f62016-03-03 22:24:39 +000084 check(C.getMemoryBufferRef(),
Rui Ueyama64bd8df2016-03-14 21:31:07 +000085 "could not get the buffer for a child of the archive " +
Rafael Espindola1130935c2016-03-03 16:21:44 +000086 File->getFileName());
87 V.push_back(Mb);
Rui Ueyama9b093692016-01-06 00:51:35 +000088 }
Peter Collingbourned418b1d2016-03-31 23:12:18 +000089
90 // Take ownership of memory buffers created for members of thin archives.
91 for (std::unique_ptr<MemoryBuffer> &MB : File->takeThinBuffers())
92 OwningMBs.push_back(std::move(MB));
93
Rui Ueyama9b093692016-01-06 00:51:35 +000094 return V;
95}
96
Rui Ueyama983ed2b2015-10-01 15:23:09 +000097// Opens and parses a file. Path has to be resolved already.
98// Newly created memory buffers are owned by this driver.
99void LinkerDriver::addFile(StringRef Path) {
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +0000100 using namespace llvm::sys::fs;
George Rimar1e4b39f2016-03-29 08:45:40 +0000101 if (Config->Verbose || Config->Trace)
102 llvm::outs() << Path << "\n";
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000103 auto MBOrErr = MemoryBuffer::getFile(Path);
Rui Ueyama6eafa7f2016-03-13 04:25:41 +0000104 if (!MBOrErr) {
105 error(MBOrErr, "cannot open " + Path);
Rui Ueyama21eecb42016-02-02 21:13:09 +0000106 return;
Rui Ueyama6eafa7f2016-03-13 04:25:41 +0000107 }
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000108 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
109 MemoryBufferRef MBRef = MB->getMemBufferRef();
110 OwningMBs.push_back(std::move(MB)); // take MB ownership
111
112 switch (identify_magic(MBRef.getBuffer())) {
113 case file_magic::unknown:
Rui Ueyamaf9de0d62016-02-11 21:38:55 +0000114 Script->read(MBRef);
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000115 return;
116 case file_magic::archive:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000117 if (WholeArchive) {
Rui Ueyama9b093692016-01-06 00:51:35 +0000118 for (MemoryBufferRef MB : getArchiveMembers(MBRef))
Rui Ueyama2b4df612016-03-09 21:15:17 +0000119 Files.push_back(createObjectFile(MB, Path));
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000120 return;
121 }
122 Files.push_back(make_unique<ArchiveFile>(MBRef));
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000123 return;
Rafael Espindolaaf707642015-10-12 01:55:32 +0000124 case file_magic::elf_shared_object:
George Rimar58941ee2016-02-25 08:23:37 +0000125 if (Config->Relocatable) {
George Rimar777f9632016-03-12 08:31:34 +0000126 error("attempted static link of dynamic object " + Path);
George Rimar58941ee2016-02-25 08:23:37 +0000127 return;
128 }
Rui Ueyama533c0302016-01-06 00:09:43 +0000129 Files.push_back(createSharedFile(MBRef));
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000130 return;
131 default:
Rui Ueyama533c0302016-01-06 00:09:43 +0000132 Files.push_back(createObjectFile(MBRef));
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000133 }
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +0000134}
135
Rui Ueyama21eecb42016-02-02 21:13:09 +0000136// Add a given library by searching it from input search paths.
137void LinkerDriver::addLibrary(StringRef Name) {
138 std::string Path = searchLibrary(Name);
139 if (Path.empty())
George Rimar777f9632016-03-12 08:31:34 +0000140 error("unable to find library -l" + Name);
Rui Ueyama21eecb42016-02-02 21:13:09 +0000141 else
142 addFile(Path);
143}
144
Rui Ueyamad32c63d2016-01-07 17:33:25 +0000145// Some command line options or some combinations of them are not allowed.
146// This function checks for such errors.
147static void checkOptions(opt::InputArgList &Args) {
Rui Ueyamad32c63d2016-01-07 17:33:25 +0000148 // The MIPS ABI as of 2016 does not support the GNU-style symbol lookup
149 // table which is a relatively new feature.
150 if (Config->EMachine == EM_MIPS && Config->GnuHash)
George Rimar777f9632016-03-12 08:31:34 +0000151 error("the .gnu.hash section is not compatible with the MIPS target.");
Rui Ueyamad32c63d2016-01-07 17:33:25 +0000152
153 if (Config->EMachine == EM_AMDGPU && !Config->Entry.empty())
Rui Ueyama21eecb42016-02-02 21:13:09 +0000154 error("-e option is not valid for AMDGPU.");
George Rimar58941ee2016-02-25 08:23:37 +0000155
George Rimar786e8662016-03-17 05:57:33 +0000156 if (Config->Pie && Config->Shared)
157 error("-shared and -pie may not be used together");
158
George Rimar262b9272016-03-01 19:38:51 +0000159 if (!Config->Relocatable)
160 return;
161
162 if (Config->Shared)
George Rimar58941ee2016-02-25 08:23:37 +0000163 error("-r and -shared may not be used together");
George Rimar262b9272016-03-01 19:38:51 +0000164 if (Config->GcSections)
165 error("-r and --gc-sections may not be used together");
166 if (Config->ICF)
167 error("-r and --icf may not be used together");
George Rimar786e8662016-03-17 05:57:33 +0000168 if (Config->Pie)
169 error("-r and -pie may not be used together");
Rui Ueyamad32c63d2016-01-07 17:33:25 +0000170}
171
Rui Ueyama2cac5842015-10-07 19:34:51 +0000172static StringRef
173getString(opt::InputArgList &Args, unsigned Key, StringRef Default = "") {
174 if (auto *Arg = Args.getLastArg(Key))
175 return Arg->getValue();
176 return Default;
177}
178
Rui Ueyamab339b6d2016-03-31 21:15:31 +0000179static int getInteger(opt::InputArgList &Args, unsigned Key, int Default) {
180 int V = Default;
181 if (auto *Arg = Args.getLastArg(Key)) {
182 StringRef S = Arg->getValue();
183 if (S.getAsInteger(10, V))
184 error(Arg->getSpelling() + ": number expected, but got " + S);
185 }
186 return V;
187}
188
Rui Ueyama1a8fffa2015-11-12 19:00:37 +0000189static bool hasZOption(opt::InputArgList &Args, StringRef Key) {
190 for (auto *Arg : Args.filtered(OPT_z))
191 if (Key == Arg->getValue())
192 return true;
193 return false;
194}
195
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000196void LinkerDriver::main(ArrayRef<const char *> ArgsArr) {
Rui Ueyama15fa0352016-03-15 18:20:50 +0000197 ELFOptTable Parser;
198 opt::InputArgList Args = Parser.parse(ArgsArr.slice(1));
Rui Ueyama1eb9f442016-02-28 03:18:09 +0000199 if (Args.hasArg(OPT_help)) {
200 printHelp(ArgsArr[0]);
201 return;
202 }
Rui Ueyama1abcf372016-02-28 03:18:07 +0000203 if (Args.hasArg(OPT_version)) {
204 printVersion();
205 return;
206 }
207
Rui Ueyama0dd684c2016-01-07 17:54:19 +0000208 readConfigs(Args);
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000209 createFiles(Args);
Rui Ueyamad32c63d2016-01-07 17:33:25 +0000210 checkOptions(Args);
Rui Ueyama21eecb42016-02-02 21:13:09 +0000211 if (HasError)
212 return;
Rui Ueyamaf5bcf2a2015-11-12 18:54:15 +0000213
Rui Ueyamae717a712015-10-13 16:20:50 +0000214 switch (Config->EKind) {
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000215 case ELF32LEKind:
216 link<ELF32LE>(Args);
217 return;
218 case ELF32BEKind:
219 link<ELF32BE>(Args);
220 return;
221 case ELF64LEKind:
222 link<ELF64LE>(Args);
223 return;
224 case ELF64BEKind:
225 link<ELF64BE>(Args);
226 return;
227 default:
Rui Ueyama21eecb42016-02-02 21:13:09 +0000228 error("-m or at least a .o file required");
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000229 }
230}
231
Rui Ueyama0dd684c2016-01-07 17:54:19 +0000232// Initializes Config members by the command line options.
233void LinkerDriver::readConfigs(opt::InputArgList &Args) {
Rafael Espindola06501922016-03-08 17:13:12 +0000234 for (auto *Arg : Args.filtered(OPT_L))
235 Config->SearchPaths.push_back(Arg->getValue());
236
Rafael Espindola2e9eac12015-09-11 21:18:56 +0000237 std::vector<StringRef> RPaths;
238 for (auto *Arg : Args.filtered(OPT_rpath))
239 RPaths.push_back(Arg->getValue());
240 if (!RPaths.empty())
241 Config->RPath = llvm::join(RPaths.begin(), RPaths.end(), ":");
242
Rui Ueyama9aa56862015-11-24 18:55:36 +0000243 if (auto *Arg = Args.getLastArg(OPT_m)) {
Rui Ueyama94b08e32016-01-12 01:33:23 +0000244 // Parse ELF{32,64}{LE,BE} and CPU type.
Rui Ueyama9aa56862015-11-24 18:55:36 +0000245 StringRef S = Arg->getValue();
Rui Ueyama94b08e32016-01-12 01:33:23 +0000246 std::tie(Config->EKind, Config->EMachine) = parseEmulation(S);
Rui Ueyama9aa56862015-11-24 18:55:36 +0000247 Config->Emulation = S;
248 }
249
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000250 Config->AllowMultipleDefinition = Args.hasArg(OPT_allow_multiple_definition);
Davide Italianocebb4492015-10-13 21:02:34 +0000251 Config->Bsymbolic = Args.hasArg(OPT_Bsymbolic);
George Rimar5c36e592016-02-02 09:28:53 +0000252 Config->BsymbolicFunctions = Args.hasArg(OPT_Bsymbolic_functions);
Rui Ueyama634ddf02016-03-11 20:51:53 +0000253 Config->BuildId = Args.hasArg(OPT_build_id);
Rui Ueyamaa4a628f2016-01-13 18:55:39 +0000254 Config->Demangle = !Args.hasArg(OPT_no_demangle);
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000255 Config->DiscardAll = Args.hasArg(OPT_discard_all);
256 Config->DiscardLocals = Args.hasArg(OPT_discard_locals);
257 Config->DiscardNone = Args.hasArg(OPT_discard_none);
George Rimarf6bc65a2016-01-15 13:34:52 +0000258 Config->EhFrameHdr = Args.hasArg(OPT_eh_frame_hdr);
Davide Italianoc39c75d2015-10-06 16:20:00 +0000259 Config->EnableNewDtags = !Args.hasArg(OPT_disable_new_dtags);
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000260 Config->ExportDynamic = Args.hasArg(OPT_export_dynamic);
Rui Ueyamac4aaed92015-10-22 18:49:53 +0000261 Config->GcSections = Args.hasArg(OPT_gc_sections);
Rui Ueyama0b289522016-02-25 18:43:51 +0000262 Config->ICF = Args.hasArg(OPT_icf);
George Rimar57e40de2015-10-01 20:14:45 +0000263 Config->NoUndefined = Args.hasArg(OPT_no_undefined);
Rui Ueyama3f9f0922016-03-08 04:06:29 +0000264 Config->NoinhibitExec = Args.hasArg(OPT_noinhibit_exec);
George Rimar786e8662016-03-17 05:57:33 +0000265 Config->Pie = Args.hasArg(OPT_pie);
George Rimara5fbebc2015-12-10 09:12:18 +0000266 Config->PrintGcSections = Args.hasArg(OPT_print_gc_sections);
George Rimar58941ee2016-02-25 08:23:37 +0000267 Config->Relocatable = Args.hasArg(OPT_relocatable);
Sean Silva35ef3d92016-03-09 20:01:08 +0000268 Config->SaveTemps = Args.hasArg(OPT_save_temps);
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000269 Config->Shared = Args.hasArg(OPT_shared);
George Rimar5dad7c12015-10-24 08:52:46 +0000270 Config->StripAll = Args.hasArg(OPT_strip_all);
Rui Ueyamae9809502016-03-11 04:23:12 +0000271 Config->Threads = Args.hasArg(OPT_threads);
George Rimar1e4b39f2016-03-29 08:45:40 +0000272 Config->Trace = Args.hasArg(OPT_trace);
Rui Ueyamaa4672402015-10-11 02:03:03 +0000273 Config->Verbose = Args.hasArg(OPT_verbose);
George Rimar34358002016-03-14 09:19:30 +0000274 Config->WarnCommon = Args.hasArg(OPT_warn_common);
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000275
Rui Ueyama2cac5842015-10-07 19:34:51 +0000276 Config->DynamicLinker = getString(Args, OPT_dynamic_linker);
277 Config->Entry = getString(Args, OPT_entry);
278 Config->Fini = getString(Args, OPT_fini, "_fini");
279 Config->Init = getString(Args, OPT_init, "_init");
Rui Ueyama964ffb32015-10-09 00:33:44 +0000280 Config->OutputFile = getString(Args, OPT_o);
Rui Ueyama2cac5842015-10-07 19:34:51 +0000281 Config->SoName = getString(Args, OPT_soname);
282 Config->Sysroot = getString(Args, OPT_sysroot);
283
Rui Ueyamab339b6d2016-03-31 21:15:31 +0000284 Config->Optimize = getInteger(Args, OPT_O, 0);
285 Config->LtoO = getInteger(Args, OPT_lto_O, 2);
286
Rui Ueyama7b19c342015-11-24 18:48:16 +0000287 Config->ZExecStack = hasZOption(Args, "execstack");
Rui Ueyama1a8fffa2015-11-12 19:00:37 +0000288 Config->ZNodelete = hasZOption(Args, "nodelete");
289 Config->ZNow = hasZOption(Args, "now");
290 Config->ZOrigin = hasZOption(Args, "origin");
George Rimare3336c02015-11-24 10:15:50 +0000291 Config->ZRelro = !hasZOption(Args, "norelro");
Rui Ueyama1a8fffa2015-11-12 19:00:37 +0000292
George Rimar786e8662016-03-17 05:57:33 +0000293 Config->Pic = Config->Pie || Config->Shared;
294
George Rimar58941ee2016-02-25 08:23:37 +0000295 if (Config->Relocatable)
296 Config->StripAll = false;
297
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000298 if (auto *Arg = Args.getLastArg(OPT_hash_style)) {
299 StringRef S = Arg->getValue();
300 if (S == "gnu") {
301 Config->GnuHash = true;
302 Config->SysvHash = false;
303 } else if (S == "both") {
304 Config->GnuHash = true;
305 } else if (S != "sysv")
George Rimar777f9632016-03-12 08:31:34 +0000306 error("unknown hash style: " + S);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000307 }
308
George Rimar83f406c2015-10-19 17:35:12 +0000309 for (auto *Arg : Args.filtered(OPT_undefined))
310 Config->Undefined.push_back(Arg->getValue());
Rui Ueyama0dd684c2016-01-07 17:54:19 +0000311}
George Rimar83f406c2015-10-19 17:35:12 +0000312
Rui Ueyama0dd684c2016-01-07 17:54:19 +0000313void LinkerDriver::createFiles(opt::InputArgList &Args) {
Igor Kudrind912ee92015-10-01 16:42:03 +0000314 for (auto *Arg : Args) {
315 switch (Arg->getOption().getID()) {
316 case OPT_l:
Rui Ueyama21eecb42016-02-02 21:13:09 +0000317 addLibrary(Arg->getValue());
Igor Kudrind912ee92015-10-01 16:42:03 +0000318 break;
319 case OPT_INPUT:
Davide Italiano4e47d582015-10-11 03:53:36 +0000320 case OPT_script:
Igor Kudrind912ee92015-10-01 16:42:03 +0000321 addFile(Arg->getValue());
322 break;
Rui Ueyama35da9b62015-10-11 20:59:12 +0000323 case OPT_as_needed:
324 Config->AsNeeded = true;
325 break;
326 case OPT_no_as_needed:
327 Config->AsNeeded = false;
328 break;
Igor Kudrind912ee92015-10-01 16:42:03 +0000329 case OPT_Bstatic:
330 Config->Static = true;
331 break;
332 case OPT_Bdynamic:
333 Config->Static = false;
334 break;
Igor Kudrin2696bbe2015-10-01 18:02:21 +0000335 case OPT_whole_archive:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000336 WholeArchive = true;
Igor Kudrin2696bbe2015-10-01 18:02:21 +0000337 break;
338 case OPT_no_whole_archive:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000339 WholeArchive = false;
Igor Kudrin2696bbe2015-10-01 18:02:21 +0000340 break;
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +0000341 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000342 }
343
Rui Ueyama21eecb42016-02-02 21:13:09 +0000344 if (Files.empty() && !HasError)
George Rimar57610422016-03-11 14:43:02 +0000345 error("no input files.");
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000346}
347
George Rimar56e0d532016-03-11 10:07:18 +0000348template <class ELFT> static void initSymbols() {
349 ElfSym<ELFT>::Etext.setBinding(STB_GLOBAL);
350 ElfSym<ELFT>::Edata.setBinding(STB_GLOBAL);
351 ElfSym<ELFT>::End.setBinding(STB_GLOBAL);
352 ElfSym<ELFT>::Ignored.setBinding(STB_WEAK);
353 ElfSym<ELFT>::Ignored.setVisibility(STV_HIDDEN);
354}
355
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000356template <class ELFT> void LinkerDriver::link(opt::InputArgList &Args) {
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000357 // For LTO
358 InitializeAllTargets();
359 InitializeAllTargetMCs();
360 InitializeAllAsmPrinters();
Sean Silva03e41ee2016-03-10 04:58:52 +0000361 InitializeAllAsmParsers();
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000362
Rui Ueyama6c5638b2016-03-13 20:10:20 +0000363 initSymbols<ELFT>();
364
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000365 SymbolTable<ELFT> Symtab;
Rui Ueyamac1c282a2016-02-11 21:18:01 +0000366 std::unique_ptr<TargetInfo> TI(createTarget());
367 Target = TI.get();
Rui Ueyamaff777682015-10-09 21:12:40 +0000368
Rui Ueyama6c5638b2016-03-13 20:10:20 +0000369 Config->Rela = ELFT::Is64Bits;
370
George Rimar177f7bc2016-03-17 11:00:27 +0000371 // Add entry symbol.
372 // There is no entry symbol for AMDGPU binaries, so skip adding one to avoid
373 // having and undefined symbol.
Davide Italiano00a7cf92016-03-15 22:24:58 +0000374 if (Config->Entry.empty() && !Config->Shared && !Config->Relocatable &&
George Rimar177f7bc2016-03-17 11:00:27 +0000375 Config->EMachine != EM_AMDGPU)
Davide Italiano00a7cf92016-03-15 22:24:58 +0000376 Config->Entry = Config->EMachine == EM_MIPS ? "__start" : "_start";
Rui Ueyama2b675072015-10-14 22:20:57 +0000377
George Rimar177f7bc2016-03-17 11:00:27 +0000378 // In the assembly for 32 bit x86 the _GLOBAL_OFFSET_TABLE_ symbol
379 // is magical and is used to produce a R_386_GOTPC relocation.
380 // The R_386_GOTPC relocation value doesn't actually depend on the
381 // symbol value, so it could use an index of STN_UNDEF which, according
382 // to the spec, means the symbol value is 0.
383 // Unfortunately both gas and MC keep the _GLOBAL_OFFSET_TABLE_ symbol in
384 // the object file.
385 // The situation is even stranger on x86_64 where the assembly doesn't
386 // need the magical symbol, but gas still puts _GLOBAL_OFFSET_TABLE_ as
387 // an undefined symbol in the .o files.
388 // Given that the symbol is effectively unused, we just create a dummy
389 // hidden one to avoid the undefined symbol error.
390 if (!Config->Relocatable)
Rui Ueyamadd7d9982015-12-16 22:31:14 +0000391 Symtab.addIgnored("_GLOBAL_OFFSET_TABLE_");
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000392
Ed Mastef2ac6882015-12-11 17:46:46 +0000393 if (!Config->Entry.empty()) {
394 // Set either EntryAddr (if S is a number) or EntrySym (otherwise).
395 StringRef S = Config->Entry;
396 if (S.getAsInteger(0, Config->EntryAddr))
397 Config->EntrySym = Symtab.addUndefined(S);
398 }
399
Rui Ueyama27161302015-12-16 21:35:39 +0000400 if (Config->EMachine == EM_MIPS) {
401 // On MIPS O32 ABI, _gp_disp is a magic symbol designates offset between
Simon Atanasyanb76108a2016-02-07 12:09:40 +0000402 // start of function and 'gp' pointer into GOT.
Rafael Espindola3a6a0a02016-01-19 00:05:54 +0000403 Config->MipsGpDisp = Symtab.addIgnored("_gp_disp");
Simon Atanasyanb76108a2016-02-07 12:09:40 +0000404 // The __gnu_local_gp is a magic symbol equal to the current value of 'gp'
405 // pointer. This symbol is used in the code generated by .cpload pseudo-op
406 // in case of using -mno-shared option.
407 // https://sourceware.org/ml/binutils/2004-12/msg00094.html
Simon Atanasyan597df212016-02-04 12:09:49 +0000408 Config->MipsLocalGp = Symtab.addIgnored("__gnu_local_gp");
Rui Ueyama27161302015-12-16 21:35:39 +0000409
410 // Define _gp for MIPS. st_value of _gp symbol will be updated by Writer
411 // so that it points to an absolute address which is relative to GOT.
412 // See "Global Data Symbols" in Chapter 6 in the following document:
413 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000414 Symtab.addAbsolute("_gp", ElfSym<ELFT>::MipsGp);
Rui Ueyama27161302015-12-16 21:35:39 +0000415 }
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000416
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000417 for (std::unique_ptr<InputFile> &F : Files)
418 Symtab.addFile(std::move(F));
Rui Ueyama16ba6692016-01-29 19:41:13 +0000419 if (HasError)
420 return; // There were duplicate symbols or incompatible files
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000421
Rui Ueyamaf215dac2015-10-19 20:55:28 +0000422 for (StringRef S : Config->Undefined)
423 Symtab.addUndefinedOpt(S);
Denis Protivensky22220d52015-10-05 09:43:57 +0000424
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000425 Symtab.addCombinedLtoObject();
426
Rui Ueyamadeb15402016-01-07 17:20:07 +0000427 for (auto *Arg : Args.filtered(OPT_wrap))
428 Symtab.wrap(Arg->getValue());
429
Rui Ueyamaee592822015-10-07 00:25:09 +0000430 if (Config->OutputFile.empty())
431 Config->OutputFile = "a.out";
432
Rui Ueyama75230392015-10-07 18:29:51 +0000433 // Write the result to the file.
Rui Ueyama93bfee52015-10-13 18:10:33 +0000434 Symtab.scanShlibUndefined();
Rui Ueyamac4aaed92015-10-22 18:49:53 +0000435 if (Config->GcSections)
436 markLive<ELFT>(&Symtab);
Rui Ueyama0b289522016-02-25 18:43:51 +0000437 if (Config->ICF)
438 doIcf<ELFT>(&Symtab);
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000439 writeResult<ELFT>(&Symtab);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000440}