blob: d6a659d6126378b805473247af093194a5b9531f [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"
Rafael Espindola2e9eac12015-09-11 21:18:56 +000019#include "llvm/ADT/StringExtras.h"
Rafael Espindola9f77ef02016-02-12 20:54:57 +000020#include "llvm/Support/TargetSelect.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;
Rafael Espindolae0df00b2016-02-28 00:25:54 +000029using namespace lld::elf;
Michael J. Spencer84487f12015-07-24 21:03:07 +000030
Rafael Espindolae0df00b2016-02-28 00:25:54 +000031Configuration *elf::Config;
32LinkerDriver *elf::Driver;
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +000033
Rafael Espindolae0df00b2016-02-28 00:25:54 +000034bool elf::link(ArrayRef<const char *> Args, raw_ostream &Error) {
Rui Ueyama64cfffd2016-01-28 18:40:06 +000035 HasError = false;
Rui Ueyamab6940112016-02-02 22:49:32 +000036 ErrorOS = &Error;
Rui Ueyama570752c2015-08-18 09:13:25 +000037 Configuration C;
Rui Ueyama983ed2b2015-10-01 15:23:09 +000038 LinkerDriver D;
Rui Ueyama717677a2016-02-11 21:17:59 +000039 LinkerScript LS;
Rui Ueyama570752c2015-08-18 09:13:25 +000040 Config = &C;
Rui Ueyama983ed2b2015-10-01 15:23:09 +000041 Driver = &D;
Rui Ueyama717677a2016-02-11 21:17:59 +000042 Script = &LS;
Rui Ueyama1eb9f442016-02-28 03:18:09 +000043 Driver->main(Args);
Rui Ueyama64cfffd2016-01-28 18:40:06 +000044 return !HasError;
Michael J. Spencer84487f12015-07-24 21:03:07 +000045}
46
Rui Ueyamacacf9672015-10-11 02:22:31 +000047static std::pair<ELFKind, uint16_t> parseEmulation(StringRef S) {
Rui Ueyamaa561e782015-10-11 19:46:00 +000048 if (S == "elf32btsmip")
49 return {ELF32BEKind, EM_MIPS};
50 if (S == "elf32ltsmip")
51 return {ELF32LEKind, EM_MIPS};
Davide Italiano15414cd2016-01-12 02:58:59 +000052 if (S == "elf32ppc" || S == "elf32ppc_fbsd")
Rui Ueyamaa561e782015-10-11 19:46:00 +000053 return {ELF32BEKind, EM_PPC};
Davide Italiano15414cd2016-01-12 02:58:59 +000054 if (S == "elf64ppc" || S == "elf64ppc_fbsd")
Rui Ueyamaa561e782015-10-11 19:46:00 +000055 return {ELF64BEKind, EM_PPC64};
56 if (S == "elf_i386")
57 return {ELF32LEKind, EM_386};
58 if (S == "elf_x86_64")
59 return {ELF64LEKind, EM_X86_64};
Igor Kudrin2f610d52015-11-20 02:48:53 +000060 if (S == "aarch64linux")
61 return {ELF64LEKind, EM_AARCH64};
Rui Ueyama9aa56862015-11-24 18:55:36 +000062 if (S == "i386pe" || S == "i386pep" || S == "thumb2pe")
Rui Ueyama21eecb42016-02-02 21:13:09 +000063 error("Windows targets are not supported on the ELF frontend: " + S);
64 else
65 error("Unknown emulation: " + S);
66 return {ELFNoneKind, 0};
Denis Protivensky1ef7b3f2015-10-07 09:13:03 +000067}
68
Rui Ueyama9b093692016-01-06 00:51:35 +000069// Returns slices of MB by parsing MB as an archive file.
70// Each slice consists of a member file in the archive.
71static std::vector<MemoryBufferRef> getArchiveMembers(MemoryBufferRef MB) {
72 ErrorOr<std::unique_ptr<Archive>> FileOrErr = Archive::create(MB);
Rui Ueyama64cfffd2016-01-28 18:40:06 +000073 fatal(FileOrErr, "Failed to parse archive");
Rui Ueyama9b093692016-01-06 00:51:35 +000074 std::unique_ptr<Archive> File = std::move(*FileOrErr);
75
76 std::vector<MemoryBufferRef> V;
77 for (const ErrorOr<Archive::Child> &C : File->children()) {
Rui Ueyama64cfffd2016-01-28 18:40:06 +000078 fatal(C, "Could not get the child of the archive " + File->getFileName());
Rui Ueyama9b093692016-01-06 00:51:35 +000079 ErrorOr<MemoryBufferRef> MbOrErr = C->getMemoryBufferRef();
Rui Ueyama64cfffd2016-01-28 18:40:06 +000080 fatal(MbOrErr, "Could not get the buffer for a child of the archive " +
Rui Ueyama9b093692016-01-06 00:51:35 +000081 File->getFileName());
82 V.push_back(*MbOrErr);
83 }
84 return V;
85}
86
Rui Ueyama983ed2b2015-10-01 15:23:09 +000087// Opens and parses a file. Path has to be resolved already.
88// Newly created memory buffers are owned by this driver.
89void LinkerDriver::addFile(StringRef Path) {
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +000090 using namespace llvm::sys::fs;
Rui Ueyama10bd2832016-02-25 18:56:01 +000091 log(Path);
Rui Ueyama983ed2b2015-10-01 15:23:09 +000092 auto MBOrErr = MemoryBuffer::getFile(Path);
Rui Ueyama21eecb42016-02-02 21:13:09 +000093 if (error(MBOrErr, "cannot open " + Path))
94 return;
Rui Ueyama983ed2b2015-10-01 15:23:09 +000095 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
96 MemoryBufferRef MBRef = MB->getMemBufferRef();
97 OwningMBs.push_back(std::move(MB)); // take MB ownership
98
99 switch (identify_magic(MBRef.getBuffer())) {
100 case file_magic::unknown:
Rui Ueyamaf9de0d62016-02-11 21:38:55 +0000101 Script->read(MBRef);
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000102 return;
103 case file_magic::archive:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000104 if (WholeArchive) {
Rui Ueyama71c066d2016-02-02 08:22:41 +0000105 StringRef S = MBRef.getBufferIdentifier();
Rui Ueyama9b093692016-01-06 00:51:35 +0000106 for (MemoryBufferRef MB : getArchiveMembers(MBRef))
Rui Ueyama71c066d2016-02-02 08:22:41 +0000107 Files.push_back(createObjectFile(MB, S));
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000108 return;
109 }
110 Files.push_back(make_unique<ArchiveFile>(MBRef));
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000111 return;
Rafael Espindolaaf707642015-10-12 01:55:32 +0000112 case file_magic::elf_shared_object:
George Rimar58941ee2016-02-25 08:23:37 +0000113 if (Config->Relocatable) {
114 error("Attempted static link of dynamic object " + Path);
115 return;
116 }
Rui Ueyama533c0302016-01-06 00:09:43 +0000117 Files.push_back(createSharedFile(MBRef));
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000118 return;
119 default:
Rui Ueyama533c0302016-01-06 00:09:43 +0000120 Files.push_back(createObjectFile(MBRef));
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000121 }
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +0000122}
123
Rui Ueyama21eecb42016-02-02 21:13:09 +0000124// Add a given library by searching it from input search paths.
125void LinkerDriver::addLibrary(StringRef Name) {
126 std::string Path = searchLibrary(Name);
127 if (Path.empty())
128 error("Unable to find library -l" + Name);
129 else
130 addFile(Path);
131}
132
Rui Ueyamad32c63d2016-01-07 17:33:25 +0000133// Some command line options or some combinations of them are not allowed.
134// This function checks for such errors.
135static void checkOptions(opt::InputArgList &Args) {
Rui Ueyamad32c63d2016-01-07 17:33:25 +0000136 // The MIPS ABI as of 2016 does not support the GNU-style symbol lookup
137 // table which is a relatively new feature.
138 if (Config->EMachine == EM_MIPS && Config->GnuHash)
Rui Ueyama21eecb42016-02-02 21:13:09 +0000139 error("The .gnu.hash section is not compatible with the MIPS target.");
Rui Ueyamad32c63d2016-01-07 17:33:25 +0000140
141 if (Config->EMachine == EM_AMDGPU && !Config->Entry.empty())
Rui Ueyama21eecb42016-02-02 21:13:09 +0000142 error("-e option is not valid for AMDGPU.");
George Rimar58941ee2016-02-25 08:23:37 +0000143
144 if (Config->Relocatable && Config->Shared)
145 error("-r and -shared may not be used together");
Rui Ueyamad32c63d2016-01-07 17:33:25 +0000146}
147
Rui Ueyama2cac5842015-10-07 19:34:51 +0000148static StringRef
149getString(opt::InputArgList &Args, unsigned Key, StringRef Default = "") {
150 if (auto *Arg = Args.getLastArg(Key))
151 return Arg->getValue();
152 return Default;
153}
154
Rui Ueyama1a8fffa2015-11-12 19:00:37 +0000155static bool hasZOption(opt::InputArgList &Args, StringRef Key) {
156 for (auto *Arg : Args.filtered(OPT_z))
157 if (Key == Arg->getValue())
158 return true;
159 return false;
160}
161
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000162void LinkerDriver::main(ArrayRef<const char *> ArgsArr) {
Rui Ueyama9aa56862015-11-24 18:55:36 +0000163 initSymbols();
Rui Ueyama9ea49c72015-10-07 23:46:11 +0000164
Rui Ueyama1eb9f442016-02-28 03:18:09 +0000165 opt::InputArgList Args = parseArgs(&Alloc, ArgsArr.slice(1));
166 if (Args.hasArg(OPT_help)) {
167 printHelp(ArgsArr[0]);
168 return;
169 }
Rui Ueyama1abcf372016-02-28 03:18:07 +0000170 if (Args.hasArg(OPT_version)) {
171 printVersion();
172 return;
173 }
174
Rui Ueyama0dd684c2016-01-07 17:54:19 +0000175 readConfigs(Args);
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000176 createFiles(Args);
Rui Ueyamad32c63d2016-01-07 17:33:25 +0000177 checkOptions(Args);
Rui Ueyama21eecb42016-02-02 21:13:09 +0000178 if (HasError)
179 return;
Rui Ueyamaf5bcf2a2015-11-12 18:54:15 +0000180
Rui Ueyamae717a712015-10-13 16:20:50 +0000181 switch (Config->EKind) {
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000182 case ELF32LEKind:
183 link<ELF32LE>(Args);
184 return;
185 case ELF32BEKind:
186 link<ELF32BE>(Args);
187 return;
188 case ELF64LEKind:
189 link<ELF64LE>(Args);
190 return;
191 case ELF64BEKind:
192 link<ELF64BE>(Args);
193 return;
194 default:
Rui Ueyama21eecb42016-02-02 21:13:09 +0000195 error("-m or at least a .o file required");
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000196 }
197}
198
Rui Ueyama0dd684c2016-01-07 17:54:19 +0000199// Initializes Config members by the command line options.
200void LinkerDriver::readConfigs(opt::InputArgList &Args) {
Rui Ueyama2cac5842015-10-07 19:34:51 +0000201 for (auto *Arg : Args.filtered(OPT_L))
Rui Ueyama52a15092015-10-11 03:28:42 +0000202 Config->SearchPaths.push_back(Arg->getValue());
Igor Kudrin1309fc02015-09-28 15:01:59 +0000203
Rafael Espindola2e9eac12015-09-11 21:18:56 +0000204 std::vector<StringRef> RPaths;
205 for (auto *Arg : Args.filtered(OPT_rpath))
206 RPaths.push_back(Arg->getValue());
207 if (!RPaths.empty())
208 Config->RPath = llvm::join(RPaths.begin(), RPaths.end(), ":");
209
Rui Ueyama9aa56862015-11-24 18:55:36 +0000210 if (auto *Arg = Args.getLastArg(OPT_m)) {
Rui Ueyama94b08e32016-01-12 01:33:23 +0000211 // Parse ELF{32,64}{LE,BE} and CPU type.
Rui Ueyama9aa56862015-11-24 18:55:36 +0000212 StringRef S = Arg->getValue();
Rui Ueyama94b08e32016-01-12 01:33:23 +0000213 std::tie(Config->EKind, Config->EMachine) = parseEmulation(S);
Rui Ueyama9aa56862015-11-24 18:55:36 +0000214 Config->Emulation = S;
215 }
216
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000217 Config->AllowMultipleDefinition = Args.hasArg(OPT_allow_multiple_definition);
Davide Italianocebb4492015-10-13 21:02:34 +0000218 Config->Bsymbolic = Args.hasArg(OPT_Bsymbolic);
George Rimar5c36e592016-02-02 09:28:53 +0000219 Config->BsymbolicFunctions = Args.hasArg(OPT_Bsymbolic_functions);
Rui Ueyamaa4a628f2016-01-13 18:55:39 +0000220 Config->Demangle = !Args.hasArg(OPT_no_demangle);
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000221 Config->DiscardAll = Args.hasArg(OPT_discard_all);
222 Config->DiscardLocals = Args.hasArg(OPT_discard_locals);
223 Config->DiscardNone = Args.hasArg(OPT_discard_none);
George Rimarf6bc65a2016-01-15 13:34:52 +0000224 Config->EhFrameHdr = Args.hasArg(OPT_eh_frame_hdr);
Davide Italianoc39c75d2015-10-06 16:20:00 +0000225 Config->EnableNewDtags = !Args.hasArg(OPT_disable_new_dtags);
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000226 Config->ExportDynamic = Args.hasArg(OPT_export_dynamic);
Rui Ueyamac4aaed92015-10-22 18:49:53 +0000227 Config->GcSections = Args.hasArg(OPT_gc_sections);
Rui Ueyama0b289522016-02-25 18:43:51 +0000228 Config->ICF = Args.hasArg(OPT_icf);
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000229 Config->NoInhibitExec = Args.hasArg(OPT_noinhibit_exec);
George Rimar57e40de2015-10-01 20:14:45 +0000230 Config->NoUndefined = Args.hasArg(OPT_no_undefined);
George Rimara5fbebc2015-12-10 09:12:18 +0000231 Config->PrintGcSections = Args.hasArg(OPT_print_gc_sections);
George Rimar58941ee2016-02-25 08:23:37 +0000232 Config->Relocatable = Args.hasArg(OPT_relocatable);
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000233 Config->Shared = Args.hasArg(OPT_shared);
George Rimar5dad7c12015-10-24 08:52:46 +0000234 Config->StripAll = Args.hasArg(OPT_strip_all);
Rui Ueyamaa4672402015-10-11 02:03:03 +0000235 Config->Verbose = Args.hasArg(OPT_verbose);
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000236
Rui Ueyama2cac5842015-10-07 19:34:51 +0000237 Config->DynamicLinker = getString(Args, OPT_dynamic_linker);
238 Config->Entry = getString(Args, OPT_entry);
239 Config->Fini = getString(Args, OPT_fini, "_fini");
240 Config->Init = getString(Args, OPT_init, "_init");
Rui Ueyama964ffb32015-10-09 00:33:44 +0000241 Config->OutputFile = getString(Args, OPT_o);
Rui Ueyama2cac5842015-10-07 19:34:51 +0000242 Config->SoName = getString(Args, OPT_soname);
243 Config->Sysroot = getString(Args, OPT_sysroot);
244
Rui Ueyama7b19c342015-11-24 18:48:16 +0000245 Config->ZExecStack = hasZOption(Args, "execstack");
Rui Ueyama1a8fffa2015-11-12 19:00:37 +0000246 Config->ZNodelete = hasZOption(Args, "nodelete");
247 Config->ZNow = hasZOption(Args, "now");
248 Config->ZOrigin = hasZOption(Args, "origin");
George Rimare3336c02015-11-24 10:15:50 +0000249 Config->ZRelro = !hasZOption(Args, "norelro");
Rui Ueyama1a8fffa2015-11-12 19:00:37 +0000250
George Rimar58941ee2016-02-25 08:23:37 +0000251 if (Config->Relocatable)
252 Config->StripAll = false;
253
Rafael Espindola2c1217d2015-10-23 19:02:19 +0000254 if (auto *Arg = Args.getLastArg(OPT_O)) {
255 StringRef Val = Arg->getValue();
256 if (Val.getAsInteger(10, Config->Optimize))
Rui Ueyama21eecb42016-02-02 21:13:09 +0000257 error("Invalid optimization level");
Rafael Espindola2c1217d2015-10-23 19:02:19 +0000258 }
259
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000260 if (auto *Arg = Args.getLastArg(OPT_hash_style)) {
261 StringRef S = Arg->getValue();
262 if (S == "gnu") {
263 Config->GnuHash = true;
264 Config->SysvHash = false;
265 } else if (S == "both") {
266 Config->GnuHash = true;
267 } else if (S != "sysv")
Rui Ueyama21eecb42016-02-02 21:13:09 +0000268 error("Unknown hash style: " + S);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000269 }
270
George Rimar83f406c2015-10-19 17:35:12 +0000271 for (auto *Arg : Args.filtered(OPT_undefined))
272 Config->Undefined.push_back(Arg->getValue());
Rui Ueyama0dd684c2016-01-07 17:54:19 +0000273}
George Rimar83f406c2015-10-19 17:35:12 +0000274
Rui Ueyama0dd684c2016-01-07 17:54:19 +0000275void LinkerDriver::createFiles(opt::InputArgList &Args) {
Igor Kudrind912ee92015-10-01 16:42:03 +0000276 for (auto *Arg : Args) {
277 switch (Arg->getOption().getID()) {
278 case OPT_l:
Rui Ueyama21eecb42016-02-02 21:13:09 +0000279 addLibrary(Arg->getValue());
Igor Kudrind912ee92015-10-01 16:42:03 +0000280 break;
281 case OPT_INPUT:
Davide Italiano4e47d582015-10-11 03:53:36 +0000282 case OPT_script:
Igor Kudrind912ee92015-10-01 16:42:03 +0000283 addFile(Arg->getValue());
284 break;
Rui Ueyama35da9b62015-10-11 20:59:12 +0000285 case OPT_as_needed:
286 Config->AsNeeded = true;
287 break;
288 case OPT_no_as_needed:
289 Config->AsNeeded = false;
290 break;
Igor Kudrind912ee92015-10-01 16:42:03 +0000291 case OPT_Bstatic:
292 Config->Static = true;
293 break;
294 case OPT_Bdynamic:
295 Config->Static = false;
296 break;
Igor Kudrin2696bbe2015-10-01 18:02:21 +0000297 case OPT_whole_archive:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000298 WholeArchive = true;
Igor Kudrin2696bbe2015-10-01 18:02:21 +0000299 break;
300 case OPT_no_whole_archive:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000301 WholeArchive = false;
Igor Kudrin2696bbe2015-10-01 18:02:21 +0000302 break;
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +0000303 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000304 }
305
Rui Ueyama21eecb42016-02-02 21:13:09 +0000306 if (Files.empty() && !HasError)
307 error("no input files.");
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000308}
309
310template <class ELFT> void LinkerDriver::link(opt::InputArgList &Args) {
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000311 // For LTO
312 InitializeAllTargets();
313 InitializeAllTargetMCs();
314 InitializeAllAsmPrinters();
315
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000316 SymbolTable<ELFT> Symtab;
Rui Ueyamac1c282a2016-02-11 21:18:01 +0000317 std::unique_ptr<TargetInfo> TI(createTarget());
318 Target = TI.get();
Rui Ueyamaff777682015-10-09 21:12:40 +0000319
George Rimar58941ee2016-02-25 08:23:37 +0000320 if (!Config->Shared && !Config->Relocatable) {
Rui Ueyamaff777682015-10-09 21:12:40 +0000321 // Add entry symbol.
Tom Stellard80efb162016-01-07 03:59:08 +0000322 //
323 // There is no entry symbol for AMDGPU binaries, so skip adding one to avoid
324 // having and undefined symbol.
325 if (Config->Entry.empty() && Config->EMachine != EM_AMDGPU)
Rui Ueyama0cef6892015-10-12 15:23:54 +0000326 Config->Entry = (Config->EMachine == EM_MIPS) ? "__start" : "_start";
Rui Ueyama2b675072015-10-14 22:20:57 +0000327
Rui Ueyamaff777682015-10-09 21:12:40 +0000328 // In the assembly for 32 bit x86 the _GLOBAL_OFFSET_TABLE_ symbol
329 // is magical and is used to produce a R_386_GOTPC relocation.
330 // The R_386_GOTPC relocation value doesn't actually depend on the
331 // symbol value, so it could use an index of STN_UNDEF which, according
332 // to the spec, means the symbol value is 0.
333 // Unfortunately both gas and MC keep the _GLOBAL_OFFSET_TABLE_ symbol in
334 // the object file.
335 // The situation is even stranger on x86_64 where the assembly doesn't
336 // need the magical symbol, but gas still puts _GLOBAL_OFFSET_TABLE_ as
337 // an undefined symbol in the .o files.
338 // Given that the symbol is effectively unused, we just create a dummy
339 // hidden one to avoid the undefined symbol error.
Rui Ueyamadd7d9982015-12-16 22:31:14 +0000340 Symtab.addIgnored("_GLOBAL_OFFSET_TABLE_");
Rui Ueyamaff777682015-10-09 21:12:40 +0000341 }
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000342
Ed Mastef2ac6882015-12-11 17:46:46 +0000343 if (!Config->Entry.empty()) {
344 // Set either EntryAddr (if S is a number) or EntrySym (otherwise).
345 StringRef S = Config->Entry;
346 if (S.getAsInteger(0, Config->EntryAddr))
347 Config->EntrySym = Symtab.addUndefined(S);
348 }
349
Rui Ueyama27161302015-12-16 21:35:39 +0000350 if (Config->EMachine == EM_MIPS) {
351 // On MIPS O32 ABI, _gp_disp is a magic symbol designates offset between
Simon Atanasyanb76108a2016-02-07 12:09:40 +0000352 // start of function and 'gp' pointer into GOT.
Rafael Espindola3a6a0a02016-01-19 00:05:54 +0000353 Config->MipsGpDisp = Symtab.addIgnored("_gp_disp");
Simon Atanasyanb76108a2016-02-07 12:09:40 +0000354 // The __gnu_local_gp is a magic symbol equal to the current value of 'gp'
355 // pointer. This symbol is used in the code generated by .cpload pseudo-op
356 // in case of using -mno-shared option.
357 // https://sourceware.org/ml/binutils/2004-12/msg00094.html
Simon Atanasyan597df212016-02-04 12:09:49 +0000358 Config->MipsLocalGp = Symtab.addIgnored("__gnu_local_gp");
Rui Ueyama27161302015-12-16 21:35:39 +0000359
360 // Define _gp for MIPS. st_value of _gp symbol will be updated by Writer
361 // so that it points to an absolute address which is relative to GOT.
362 // See "Global Data Symbols" in Chapter 6 in the following document:
363 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000364 Symtab.addAbsolute("_gp", ElfSym<ELFT>::MipsGp);
Rui Ueyama27161302015-12-16 21:35:39 +0000365 }
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000366
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000367 for (std::unique_ptr<InputFile> &F : Files)
368 Symtab.addFile(std::move(F));
Rui Ueyama16ba6692016-01-29 19:41:13 +0000369 if (HasError)
370 return; // There were duplicate symbols or incompatible files
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000371
Rui Ueyamaf215dac2015-10-19 20:55:28 +0000372 for (StringRef S : Config->Undefined)
373 Symtab.addUndefinedOpt(S);
Denis Protivensky22220d52015-10-05 09:43:57 +0000374
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000375 Symtab.addCombinedLtoObject();
376
Rui Ueyamadeb15402016-01-07 17:20:07 +0000377 for (auto *Arg : Args.filtered(OPT_wrap))
378 Symtab.wrap(Arg->getValue());
379
Rui Ueyamaee592822015-10-07 00:25:09 +0000380 if (Config->OutputFile.empty())
381 Config->OutputFile = "a.out";
382
Rui Ueyama75230392015-10-07 18:29:51 +0000383 // Write the result to the file.
Rui Ueyama93bfee52015-10-13 18:10:33 +0000384 Symtab.scanShlibUndefined();
Rui Ueyamac4aaed92015-10-22 18:49:53 +0000385 if (Config->GcSections)
386 markLive<ELFT>(&Symtab);
Rui Ueyama0b289522016-02-25 18:43:51 +0000387 if (Config->ICF)
388 doIcf<ELFT>(&Symtab);
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000389 writeResult<ELFT>(&Symtab);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000390}