blob: 3bb905370262d845f909349576ce08de25f53899 [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 Ueyamab6940112016-02-02 22:49:32 +000032bool elf2::link(ArrayRef<const char *> Args, raw_ostream &Error) {
Rui Ueyama64cfffd2016-01-28 18:40:06 +000033 HasError = false;
Rui Ueyamab6940112016-02-02 22:49:32 +000034 ErrorOS = &Error;
Rui Ueyama570752c2015-08-18 09:13:25 +000035 Configuration C;
Rui Ueyama983ed2b2015-10-01 15:23:09 +000036 LinkerDriver D;
Rui Ueyama570752c2015-08-18 09:13:25 +000037 Config = &C;
Rui Ueyama983ed2b2015-10-01 15:23:09 +000038 Driver = &D;
Rui Ueyama3ce825e2015-10-09 21:07:25 +000039 Driver->main(Args.slice(1));
Rui Ueyama64cfffd2016-01-28 18:40:06 +000040 return !HasError;
Michael J. Spencer84487f12015-07-24 21:03:07 +000041}
42
Rui Ueyamacacf9672015-10-11 02:22:31 +000043static std::pair<ELFKind, uint16_t> parseEmulation(StringRef S) {
Rui Ueyamaa561e782015-10-11 19:46:00 +000044 if (S == "elf32btsmip")
45 return {ELF32BEKind, EM_MIPS};
46 if (S == "elf32ltsmip")
47 return {ELF32LEKind, EM_MIPS};
Davide Italiano15414cd2016-01-12 02:58:59 +000048 if (S == "elf32ppc" || S == "elf32ppc_fbsd")
Rui Ueyamaa561e782015-10-11 19:46:00 +000049 return {ELF32BEKind, EM_PPC};
Davide Italiano15414cd2016-01-12 02:58:59 +000050 if (S == "elf64ppc" || S == "elf64ppc_fbsd")
Rui Ueyamaa561e782015-10-11 19:46:00 +000051 return {ELF64BEKind, EM_PPC64};
52 if (S == "elf_i386")
53 return {ELF32LEKind, EM_386};
54 if (S == "elf_x86_64")
55 return {ELF64LEKind, EM_X86_64};
Igor Kudrin2f610d52015-11-20 02:48:53 +000056 if (S == "aarch64linux")
57 return {ELF64LEKind, EM_AARCH64};
Rui Ueyama9aa56862015-11-24 18:55:36 +000058 if (S == "i386pe" || S == "i386pep" || S == "thumb2pe")
Rui Ueyama21eecb42016-02-02 21:13:09 +000059 error("Windows targets are not supported on the ELF frontend: " + S);
60 else
61 error("Unknown emulation: " + S);
62 return {ELFNoneKind, 0};
Denis Protivensky1ef7b3f2015-10-07 09:13:03 +000063}
64
Rui Ueyama9b093692016-01-06 00:51:35 +000065// Returns slices of MB by parsing MB as an archive file.
66// Each slice consists of a member file in the archive.
67static std::vector<MemoryBufferRef> getArchiveMembers(MemoryBufferRef MB) {
68 ErrorOr<std::unique_ptr<Archive>> FileOrErr = Archive::create(MB);
Rui Ueyama64cfffd2016-01-28 18:40:06 +000069 fatal(FileOrErr, "Failed to parse archive");
Rui Ueyama9b093692016-01-06 00:51:35 +000070 std::unique_ptr<Archive> File = std::move(*FileOrErr);
71
72 std::vector<MemoryBufferRef> V;
73 for (const ErrorOr<Archive::Child> &C : File->children()) {
Rui Ueyama64cfffd2016-01-28 18:40:06 +000074 fatal(C, "Could not get the child of the archive " + File->getFileName());
Rui Ueyama9b093692016-01-06 00:51:35 +000075 ErrorOr<MemoryBufferRef> MbOrErr = C->getMemoryBufferRef();
Rui Ueyama64cfffd2016-01-28 18:40:06 +000076 fatal(MbOrErr, "Could not get the buffer for a child of the archive " +
Rui Ueyama9b093692016-01-06 00:51:35 +000077 File->getFileName());
78 V.push_back(*MbOrErr);
79 }
80 return V;
81}
82
Rui Ueyama983ed2b2015-10-01 15:23:09 +000083// Opens and parses a file. Path has to be resolved already.
84// Newly created memory buffers are owned by this driver.
85void LinkerDriver::addFile(StringRef Path) {
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +000086 using namespace llvm::sys::fs;
Rui Ueyamaa4672402015-10-11 02:03:03 +000087 if (Config->Verbose)
88 llvm::outs() << Path << "\n";
Rui Ueyama983ed2b2015-10-01 15:23:09 +000089 auto MBOrErr = MemoryBuffer::getFile(Path);
Rui Ueyama21eecb42016-02-02 21:13:09 +000090 if (error(MBOrErr, "cannot open " + Path))
91 return;
Rui Ueyama983ed2b2015-10-01 15:23:09 +000092 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
93 MemoryBufferRef MBRef = MB->getMemBufferRef();
94 OwningMBs.push_back(std::move(MB)); // take MB ownership
95
96 switch (identify_magic(MBRef.getBuffer())) {
97 case file_magic::unknown:
Rui Ueyamaa47ee682015-10-11 01:53:04 +000098 readLinkerScript(&Alloc, MBRef);
Rui Ueyama983ed2b2015-10-01 15:23:09 +000099 return;
100 case file_magic::archive:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000101 if (WholeArchive) {
Rui Ueyama71c066d2016-02-02 08:22:41 +0000102 StringRef S = MBRef.getBufferIdentifier();
Rui Ueyama9b093692016-01-06 00:51:35 +0000103 for (MemoryBufferRef MB : getArchiveMembers(MBRef))
Rui Ueyama71c066d2016-02-02 08:22:41 +0000104 Files.push_back(createObjectFile(MB, S));
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000105 return;
106 }
107 Files.push_back(make_unique<ArchiveFile>(MBRef));
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000108 return;
Rafael Espindolaaf707642015-10-12 01:55:32 +0000109 case file_magic::elf_shared_object:
Rui Ueyama533c0302016-01-06 00:09:43 +0000110 Files.push_back(createSharedFile(MBRef));
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000111 return;
112 default:
Rui Ueyama533c0302016-01-06 00:09:43 +0000113 Files.push_back(createObjectFile(MBRef));
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000114 }
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +0000115}
116
Rui Ueyama21eecb42016-02-02 21:13:09 +0000117// Add a given library by searching it from input search paths.
118void LinkerDriver::addLibrary(StringRef Name) {
119 std::string Path = searchLibrary(Name);
120 if (Path.empty())
121 error("Unable to find library -l" + Name);
122 else
123 addFile(Path);
124}
125
Rui Ueyamad32c63d2016-01-07 17:33:25 +0000126// Some command line options or some combinations of them are not allowed.
127// This function checks for such errors.
128static void checkOptions(opt::InputArgList &Args) {
129 // Traditional linkers can generate re-linkable object files instead
130 // of executables or DSOs. We don't support that since the feature
131 // does not seem to provide more value than the static archiver.
132 if (Args.hasArg(OPT_relocatable))
Rui Ueyama21eecb42016-02-02 21:13:09 +0000133 error("-r option is not supported. Use 'ar' command instead.");
Rui Ueyamad32c63d2016-01-07 17:33:25 +0000134
135 // The MIPS ABI as of 2016 does not support the GNU-style symbol lookup
136 // table which is a relatively new feature.
137 if (Config->EMachine == EM_MIPS && Config->GnuHash)
Rui Ueyama21eecb42016-02-02 21:13:09 +0000138 error("The .gnu.hash section is not compatible with the MIPS target.");
Rui Ueyamad32c63d2016-01-07 17:33:25 +0000139
140 if (Config->EMachine == EM_AMDGPU && !Config->Entry.empty())
Rui Ueyama21eecb42016-02-02 21:13:09 +0000141 error("-e option is not valid for AMDGPU.");
Rui Ueyamad32c63d2016-01-07 17:33:25 +0000142}
143
Rui Ueyama2cac5842015-10-07 19:34:51 +0000144static StringRef
145getString(opt::InputArgList &Args, unsigned Key, StringRef Default = "") {
146 if (auto *Arg = Args.getLastArg(Key))
147 return Arg->getValue();
148 return Default;
149}
150
Rui Ueyama1a8fffa2015-11-12 19:00:37 +0000151static bool hasZOption(opt::InputArgList &Args, StringRef Key) {
152 for (auto *Arg : Args.filtered(OPT_z))
153 if (Key == Arg->getValue())
154 return true;
155 return false;
156}
157
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000158void LinkerDriver::main(ArrayRef<const char *> ArgsArr) {
Rui Ueyama9aa56862015-11-24 18:55:36 +0000159 initSymbols();
Rui Ueyama9ea49c72015-10-07 23:46:11 +0000160
Rui Ueyamac6e1b972015-10-11 18:19:01 +0000161 opt::InputArgList Args = parseArgs(&Alloc, ArgsArr);
Rui Ueyama0dd684c2016-01-07 17:54:19 +0000162 readConfigs(Args);
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000163 createFiles(Args);
Rui Ueyamad32c63d2016-01-07 17:33:25 +0000164 checkOptions(Args);
Rui Ueyama21eecb42016-02-02 21:13:09 +0000165 if (HasError)
166 return;
Rui Ueyamaf5bcf2a2015-11-12 18:54:15 +0000167
Rui Ueyamae717a712015-10-13 16:20:50 +0000168 switch (Config->EKind) {
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000169 case ELF32LEKind:
170 link<ELF32LE>(Args);
171 return;
172 case ELF32BEKind:
173 link<ELF32BE>(Args);
174 return;
175 case ELF64LEKind:
176 link<ELF64LE>(Args);
177 return;
178 case ELF64BEKind:
179 link<ELF64BE>(Args);
180 return;
181 default:
Rui Ueyama21eecb42016-02-02 21:13:09 +0000182 error("-m or at least a .o file required");
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000183 }
184}
185
Rui Ueyama0dd684c2016-01-07 17:54:19 +0000186// Initializes Config members by the command line options.
187void LinkerDriver::readConfigs(opt::InputArgList &Args) {
Rui Ueyama2cac5842015-10-07 19:34:51 +0000188 for (auto *Arg : Args.filtered(OPT_L))
Rui Ueyama52a15092015-10-11 03:28:42 +0000189 Config->SearchPaths.push_back(Arg->getValue());
Igor Kudrin1309fc02015-09-28 15:01:59 +0000190
Rafael Espindola2e9eac12015-09-11 21:18:56 +0000191 std::vector<StringRef> RPaths;
192 for (auto *Arg : Args.filtered(OPT_rpath))
193 RPaths.push_back(Arg->getValue());
194 if (!RPaths.empty())
195 Config->RPath = llvm::join(RPaths.begin(), RPaths.end(), ":");
196
Rui Ueyama9aa56862015-11-24 18:55:36 +0000197 if (auto *Arg = Args.getLastArg(OPT_m)) {
Rui Ueyama94b08e32016-01-12 01:33:23 +0000198 // Parse ELF{32,64}{LE,BE} and CPU type.
Rui Ueyama9aa56862015-11-24 18:55:36 +0000199 StringRef S = Arg->getValue();
Rui Ueyama94b08e32016-01-12 01:33:23 +0000200 std::tie(Config->EKind, Config->EMachine) = parseEmulation(S);
Rui Ueyama9aa56862015-11-24 18:55:36 +0000201 Config->Emulation = S;
202 }
203
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000204 Config->AllowMultipleDefinition = Args.hasArg(OPT_allow_multiple_definition);
Davide Italianocebb4492015-10-13 21:02:34 +0000205 Config->Bsymbolic = Args.hasArg(OPT_Bsymbolic);
George Rimar5c36e592016-02-02 09:28:53 +0000206 Config->BsymbolicFunctions = Args.hasArg(OPT_Bsymbolic_functions);
Rui Ueyamaa4a628f2016-01-13 18:55:39 +0000207 Config->Demangle = !Args.hasArg(OPT_no_demangle);
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000208 Config->DiscardAll = Args.hasArg(OPT_discard_all);
209 Config->DiscardLocals = Args.hasArg(OPT_discard_locals);
210 Config->DiscardNone = Args.hasArg(OPT_discard_none);
George Rimarf6bc65a2016-01-15 13:34:52 +0000211 Config->EhFrameHdr = Args.hasArg(OPT_eh_frame_hdr);
Davide Italianoc39c75d2015-10-06 16:20:00 +0000212 Config->EnableNewDtags = !Args.hasArg(OPT_disable_new_dtags);
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000213 Config->ExportDynamic = Args.hasArg(OPT_export_dynamic);
Rui Ueyamac4aaed92015-10-22 18:49:53 +0000214 Config->GcSections = Args.hasArg(OPT_gc_sections);
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000215 Config->NoInhibitExec = Args.hasArg(OPT_noinhibit_exec);
George Rimar57e40de2015-10-01 20:14:45 +0000216 Config->NoUndefined = Args.hasArg(OPT_no_undefined);
George Rimara5fbebc2015-12-10 09:12:18 +0000217 Config->PrintGcSections = Args.hasArg(OPT_print_gc_sections);
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000218 Config->Shared = Args.hasArg(OPT_shared);
George Rimar5dad7c12015-10-24 08:52:46 +0000219 Config->StripAll = Args.hasArg(OPT_strip_all);
Rui Ueyamaa4672402015-10-11 02:03:03 +0000220 Config->Verbose = Args.hasArg(OPT_verbose);
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000221
Rui Ueyama2cac5842015-10-07 19:34:51 +0000222 Config->DynamicLinker = getString(Args, OPT_dynamic_linker);
223 Config->Entry = getString(Args, OPT_entry);
224 Config->Fini = getString(Args, OPT_fini, "_fini");
225 Config->Init = getString(Args, OPT_init, "_init");
Rui Ueyama964ffb32015-10-09 00:33:44 +0000226 Config->OutputFile = getString(Args, OPT_o);
Rui Ueyama2cac5842015-10-07 19:34:51 +0000227 Config->SoName = getString(Args, OPT_soname);
228 Config->Sysroot = getString(Args, OPT_sysroot);
229
Rui Ueyama7b19c342015-11-24 18:48:16 +0000230 Config->ZExecStack = hasZOption(Args, "execstack");
Rui Ueyama1a8fffa2015-11-12 19:00:37 +0000231 Config->ZNodelete = hasZOption(Args, "nodelete");
232 Config->ZNow = hasZOption(Args, "now");
233 Config->ZOrigin = hasZOption(Args, "origin");
George Rimare3336c02015-11-24 10:15:50 +0000234 Config->ZRelro = !hasZOption(Args, "norelro");
Rui Ueyama1a8fffa2015-11-12 19:00:37 +0000235
Rafael Espindola2c1217d2015-10-23 19:02:19 +0000236 if (auto *Arg = Args.getLastArg(OPT_O)) {
237 StringRef Val = Arg->getValue();
238 if (Val.getAsInteger(10, Config->Optimize))
Rui Ueyama21eecb42016-02-02 21:13:09 +0000239 error("Invalid optimization level");
Rafael Espindola2c1217d2015-10-23 19:02:19 +0000240 }
241
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000242 if (auto *Arg = Args.getLastArg(OPT_hash_style)) {
243 StringRef S = Arg->getValue();
244 if (S == "gnu") {
245 Config->GnuHash = true;
246 Config->SysvHash = false;
247 } else if (S == "both") {
248 Config->GnuHash = true;
249 } else if (S != "sysv")
Rui Ueyama21eecb42016-02-02 21:13:09 +0000250 error("Unknown hash style: " + S);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000251 }
252
George Rimar83f406c2015-10-19 17:35:12 +0000253 for (auto *Arg : Args.filtered(OPT_undefined))
254 Config->Undefined.push_back(Arg->getValue());
Rui Ueyama0dd684c2016-01-07 17:54:19 +0000255}
George Rimar83f406c2015-10-19 17:35:12 +0000256
Rui Ueyama0dd684c2016-01-07 17:54:19 +0000257void LinkerDriver::createFiles(opt::InputArgList &Args) {
Igor Kudrind912ee92015-10-01 16:42:03 +0000258 for (auto *Arg : Args) {
259 switch (Arg->getOption().getID()) {
260 case OPT_l:
Rui Ueyama21eecb42016-02-02 21:13:09 +0000261 addLibrary(Arg->getValue());
Igor Kudrind912ee92015-10-01 16:42:03 +0000262 break;
263 case OPT_INPUT:
Davide Italiano4e47d582015-10-11 03:53:36 +0000264 case OPT_script:
Igor Kudrind912ee92015-10-01 16:42:03 +0000265 addFile(Arg->getValue());
266 break;
Rui Ueyama35da9b62015-10-11 20:59:12 +0000267 case OPT_as_needed:
268 Config->AsNeeded = true;
269 break;
270 case OPT_no_as_needed:
271 Config->AsNeeded = false;
272 break;
Igor Kudrind912ee92015-10-01 16:42:03 +0000273 case OPT_Bstatic:
274 Config->Static = true;
275 break;
276 case OPT_Bdynamic:
277 Config->Static = false;
278 break;
Igor Kudrin2696bbe2015-10-01 18:02:21 +0000279 case OPT_whole_archive:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000280 WholeArchive = true;
Igor Kudrin2696bbe2015-10-01 18:02:21 +0000281 break;
282 case OPT_no_whole_archive:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000283 WholeArchive = false;
Igor Kudrin2696bbe2015-10-01 18:02:21 +0000284 break;
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +0000285 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000286 }
287
Rui Ueyama21eecb42016-02-02 21:13:09 +0000288 if (Files.empty() && !HasError)
289 error("no input files.");
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000290}
291
292template <class ELFT> void LinkerDriver::link(opt::InputArgList &Args) {
293 SymbolTable<ELFT> Symtab;
Rui Ueyamaff777682015-10-09 21:12:40 +0000294 Target.reset(createTarget());
295
296 if (!Config->Shared) {
297 // Add entry symbol.
Tom Stellard80efb162016-01-07 03:59:08 +0000298 //
299 // There is no entry symbol for AMDGPU binaries, so skip adding one to avoid
300 // having and undefined symbol.
301 if (Config->Entry.empty() && Config->EMachine != EM_AMDGPU)
Rui Ueyama0cef6892015-10-12 15:23:54 +0000302 Config->Entry = (Config->EMachine == EM_MIPS) ? "__start" : "_start";
Rui Ueyama2b675072015-10-14 22:20:57 +0000303
Rui Ueyamaff777682015-10-09 21:12:40 +0000304 // In the assembly for 32 bit x86 the _GLOBAL_OFFSET_TABLE_ symbol
305 // is magical and is used to produce a R_386_GOTPC relocation.
306 // The R_386_GOTPC relocation value doesn't actually depend on the
307 // symbol value, so it could use an index of STN_UNDEF which, according
308 // to the spec, means the symbol value is 0.
309 // Unfortunately both gas and MC keep the _GLOBAL_OFFSET_TABLE_ symbol in
310 // the object file.
311 // The situation is even stranger on x86_64 where the assembly doesn't
312 // need the magical symbol, but gas still puts _GLOBAL_OFFSET_TABLE_ as
313 // an undefined symbol in the .o files.
314 // Given that the symbol is effectively unused, we just create a dummy
315 // hidden one to avoid the undefined symbol error.
Rui Ueyamadd7d9982015-12-16 22:31:14 +0000316 Symtab.addIgnored("_GLOBAL_OFFSET_TABLE_");
Rui Ueyamaff777682015-10-09 21:12:40 +0000317 }
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000318
Ed Mastef2ac6882015-12-11 17:46:46 +0000319 if (!Config->Entry.empty()) {
320 // Set either EntryAddr (if S is a number) or EntrySym (otherwise).
321 StringRef S = Config->Entry;
322 if (S.getAsInteger(0, Config->EntryAddr))
323 Config->EntrySym = Symtab.addUndefined(S);
324 }
325
Rui Ueyama27161302015-12-16 21:35:39 +0000326 if (Config->EMachine == EM_MIPS) {
327 // On MIPS O32 ABI, _gp_disp is a magic symbol designates offset between
Simon Atanasyan188558e2016-01-12 06:23:57 +0000328 // start of function and gp pointer into GOT. Use 'strong' variant of
329 // the addIgnored to prevent '_gp_disp' substitution.
Rafael Espindola3a6a0a02016-01-19 00:05:54 +0000330 Config->MipsGpDisp = Symtab.addIgnored("_gp_disp");
Simon Atanasyan597df212016-02-04 12:09:49 +0000331 Config->MipsLocalGp = Symtab.addIgnored("__gnu_local_gp");
Rui Ueyama27161302015-12-16 21:35:39 +0000332
333 // Define _gp for MIPS. st_value of _gp symbol will be updated by Writer
334 // so that it points to an absolute address which is relative to GOT.
335 // See "Global Data Symbols" in Chapter 6 in the following document:
336 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000337 Symtab.addAbsolute("_gp", ElfSym<ELFT>::MipsGp);
Rui Ueyama27161302015-12-16 21:35:39 +0000338 }
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000339
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000340 for (std::unique_ptr<InputFile> &F : Files)
341 Symtab.addFile(std::move(F));
Rui Ueyama16ba6692016-01-29 19:41:13 +0000342 if (HasError)
343 return; // There were duplicate symbols or incompatible files
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000344
Rui Ueyamaf215dac2015-10-19 20:55:28 +0000345 for (StringRef S : Config->Undefined)
346 Symtab.addUndefinedOpt(S);
Denis Protivensky22220d52015-10-05 09:43:57 +0000347
Rui Ueyamadeb15402016-01-07 17:20:07 +0000348 for (auto *Arg : Args.filtered(OPT_wrap))
349 Symtab.wrap(Arg->getValue());
350
Rui Ueyamaee592822015-10-07 00:25:09 +0000351 if (Config->OutputFile.empty())
352 Config->OutputFile = "a.out";
353
Rui Ueyama75230392015-10-07 18:29:51 +0000354 // Write the result to the file.
Rui Ueyama93bfee52015-10-13 18:10:33 +0000355 Symtab.scanShlibUndefined();
Rui Ueyamac4aaed92015-10-22 18:49:53 +0000356 if (Config->GcSections)
357 markLive<ELFT>(&Symtab);
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000358 writeResult<ELFT>(&Symtab);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000359}