blob: dc49530242dfe01949f8fb72c746c01d939b5126 [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"
Rafael Espindolaab14c882016-04-13 19:07:40 +000012#include "DynamicList.h"
Rafael Espindola192e1fa2015-08-06 15:08:23 +000013#include "Error.h"
Rui Ueyama0b289522016-02-25 18:43:51 +000014#include "ICF.h"
Rui Ueyamaafff74e22015-08-05 23:24:46 +000015#include "InputFiles.h"
Rui Ueyama717677a2016-02-11 21:17:59 +000016#include "LinkerScript.h"
Rui Ueyamaafff74e22015-08-05 23:24:46 +000017#include "SymbolTable.h"
Rui Ueyamaff777682015-10-09 21:12:40 +000018#include "Target.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000019#include "Writer.h"
Rui Ueyamaa453c0a2016-03-02 19:08:05 +000020#include "lld/Driver/Driver.h"
Rafael Espindola2e9eac12015-09-11 21:18:56 +000021#include "llvm/ADT/StringExtras.h"
Rafael Espindola9f77ef02016-02-12 20:54:57 +000022#include "llvm/Support/TargetSelect.h"
Rui Ueyamaa4672402015-10-11 02:03:03 +000023#include "llvm/Support/raw_ostream.h"
Rui Ueyamacacf9672015-10-11 02:22:31 +000024#include <utility>
Michael J. Spencer84487f12015-07-24 21:03:07 +000025
26using namespace llvm;
Denis Protivensky1ef7b3f2015-10-07 09:13:03 +000027using namespace llvm::ELF;
Rui Ueyama3ce825e2015-10-09 21:07:25 +000028using namespace llvm::object;
Michael J. Spencer84487f12015-07-24 21:03:07 +000029
30using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:54 +000031using namespace lld::elf;
Michael J. Spencer84487f12015-07-24 21:03:07 +000032
Rafael Espindolae0df00b2016-02-28 00:25:54 +000033Configuration *elf::Config;
34LinkerDriver *elf::Driver;
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +000035
Rafael Espindolae0df00b2016-02-28 00:25:54 +000036bool elf::link(ArrayRef<const char *> Args, raw_ostream &Error) {
Rui Ueyama64cfffd2016-01-28 18:40:06 +000037 HasError = false;
Rui Ueyamab6940112016-02-02 22:49:32 +000038 ErrorOS = &Error;
Rui Ueyama07320e42016-04-20 20:13:41 +000039
Rui Ueyama570752c2015-08-18 09:13:25 +000040 Configuration C;
Rui Ueyama983ed2b2015-10-01 15:23:09 +000041 LinkerDriver D;
Rui Ueyama07320e42016-04-20 20:13:41 +000042 ScriptConfiguration SC;
Rui Ueyama570752c2015-08-18 09:13:25 +000043 Config = &C;
Rui Ueyama983ed2b2015-10-01 15:23:09 +000044 Driver = &D;
Rui Ueyama07320e42016-04-20 20:13:41 +000045 ScriptConfig = &SC;
46
Rui Ueyama1eb9f442016-02-28 03:18:09 +000047 Driver->main(Args);
Rui Ueyama64cfffd2016-01-28 18:40:06 +000048 return !HasError;
Michael J. Spencer84487f12015-07-24 21:03:07 +000049}
50
Rui Ueyamacacf9672015-10-11 02:22:31 +000051static std::pair<ELFKind, uint16_t> parseEmulation(StringRef S) {
Ed Mastee2b76772016-03-31 20:26:30 +000052 if (S.endswith("_fbsd"))
53 S = S.drop_back(5);
Rui Ueyamaa561e782015-10-11 19:46:00 +000054 if (S == "elf32btsmip")
55 return {ELF32BEKind, EM_MIPS};
56 if (S == "elf32ltsmip")
57 return {ELF32LEKind, EM_MIPS};
Ed Mastee2b76772016-03-31 20:26:30 +000058 if (S == "elf32ppc")
Rui Ueyamaa561e782015-10-11 19:46:00 +000059 return {ELF32BEKind, EM_PPC};
Ed Mastee2b76772016-03-31 20:26:30 +000060 if (S == "elf64ppc")
Rui Ueyamaa561e782015-10-11 19:46:00 +000061 return {ELF64BEKind, EM_PPC64};
62 if (S == "elf_i386")
63 return {ELF32LEKind, EM_386};
64 if (S == "elf_x86_64")
65 return {ELF64LEKind, EM_X86_64};
Igor Kudrin2f610d52015-11-20 02:48:53 +000066 if (S == "aarch64linux")
67 return {ELF64LEKind, EM_AARCH64};
Rui Ueyama9aa56862015-11-24 18:55:36 +000068 if (S == "i386pe" || S == "i386pep" || S == "thumb2pe")
Rui Ueyama7f4a4922016-03-13 03:17:42 +000069 error("Windows targets are not supported on the ELF frontend: " + S);
Rui Ueyama21eecb42016-02-02 21:13:09 +000070 else
George Rimar777f9632016-03-12 08:31:34 +000071 error("unknown emulation: " + S);
Rui Ueyama6c8b0c12016-04-07 23:54:33 +000072 return {ELFNoneKind, EM_NONE};
Denis Protivensky1ef7b3f2015-10-07 09:13:03 +000073}
74
Rui Ueyama9b093692016-01-06 00:51:35 +000075// Returns slices of MB by parsing MB as an archive file.
76// Each slice consists of a member file in the archive.
Peter Collingbourned418b1d2016-03-31 23:12:18 +000077std::vector<MemoryBufferRef>
78LinkerDriver::getArchiveMembers(MemoryBufferRef MB) {
Rafael Espindola1130935c2016-03-03 16:21:44 +000079 std::unique_ptr<Archive> File =
Rui Ueyama64bd8df2016-03-14 21:31:07 +000080 check(Archive::create(MB), "failed to parse archive");
Rui Ueyama9b093692016-01-06 00:51:35 +000081
82 std::vector<MemoryBufferRef> V;
Rafael Espindola1130935c2016-03-03 16:21:44 +000083 for (const ErrorOr<Archive::Child> &COrErr : File->children()) {
Rui Ueyama64bd8df2016-03-14 21:31:07 +000084 Archive::Child C = check(COrErr, "could not get the child of the archive " +
Rafael Espindola1130935c2016-03-03 16:21:44 +000085 File->getFileName());
Rui Ueyama7e71c612016-04-02 19:09:07 +000086 MemoryBufferRef MBRef =
Rafael Espindola75714f62016-03-03 22:24:39 +000087 check(C.getMemoryBufferRef(),
Rui Ueyama64bd8df2016-03-14 21:31:07 +000088 "could not get the buffer for a child of the archive " +
Rafael Espindola1130935c2016-03-03 16:21:44 +000089 File->getFileName());
Rui Ueyama7e71c612016-04-02 19:09:07 +000090 V.push_back(MBRef);
Rui Ueyama9b093692016-01-06 00:51:35 +000091 }
Peter Collingbourned418b1d2016-03-31 23:12:18 +000092
93 // Take ownership of memory buffers created for members of thin archives.
94 for (std::unique_ptr<MemoryBuffer> &MB : File->takeThinBuffers())
95 OwningMBs.push_back(std::move(MB));
96
Rui Ueyama9b093692016-01-06 00:51:35 +000097 return V;
98}
99
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000100// Opens and parses a file. Path has to be resolved already.
101// Newly created memory buffers are owned by this driver.
102void LinkerDriver::addFile(StringRef Path) {
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +0000103 using namespace llvm::sys::fs;
George Rimar2a78fce2016-04-13 18:07:57 +0000104 if (Config->Verbose)
George Rimar1e4b39f2016-03-29 08:45:40 +0000105 llvm::outs() << Path << "\n";
Adhemerval Zanella9df07202016-04-13 18:51:11 +0000106 Optional<MemoryBufferRef> Buffer = readFile(Path);
107 if (!Buffer.hasValue())
Rui Ueyama21eecb42016-02-02 21:13:09 +0000108 return;
Adhemerval Zanella9df07202016-04-13 18:51:11 +0000109 MemoryBufferRef MBRef = *Buffer;
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000110
111 switch (identify_magic(MBRef.getBuffer())) {
112 case file_magic::unknown:
Rui Ueyama07320e42016-04-20 20:13:41 +0000113 readLinkerScript(MBRef);
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000114 return;
115 case file_magic::archive:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000116 if (WholeArchive) {
Rui Ueyama9b093692016-01-06 00:51:35 +0000117 for (MemoryBufferRef MB : getArchiveMembers(MBRef))
Rui Ueyama2b4df612016-03-09 21:15:17 +0000118 Files.push_back(createObjectFile(MB, Path));
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000119 return;
120 }
121 Files.push_back(make_unique<ArchiveFile>(MBRef));
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000122 return;
Rafael Espindolaaf707642015-10-12 01:55:32 +0000123 case file_magic::elf_shared_object:
George Rimar58941ee2016-02-25 08:23:37 +0000124 if (Config->Relocatable) {
George Rimar777f9632016-03-12 08:31:34 +0000125 error("attempted static link of dynamic object " + Path);
George Rimar58941ee2016-02-25 08:23:37 +0000126 return;
127 }
Rui Ueyama533c0302016-01-06 00:09:43 +0000128 Files.push_back(createSharedFile(MBRef));
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000129 return;
130 default:
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000131 if (InLib)
132 Files.push_back(make_unique<LazyObjectFile>(MBRef));
133 else
134 Files.push_back(createObjectFile(MBRef));
Rui Ueyama983ed2b2015-10-01 15:23:09 +0000135 }
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +0000136}
137
Adhemerval Zanella9df07202016-04-13 18:51:11 +0000138Optional<MemoryBufferRef> LinkerDriver::readFile(StringRef Path) {
139 auto MBOrErr = MemoryBuffer::getFile(Path);
Rafael Espindola3fa73522016-04-13 19:09:48 +0000140 error(MBOrErr, "cannot open " + Path);
141 if (HasError)
Adhemerval Zanella9df07202016-04-13 18:51:11 +0000142 return None;
Adhemerval Zanella9df07202016-04-13 18:51:11 +0000143 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
144 MemoryBufferRef MBRef = MB->getMemBufferRef();
145 OwningMBs.push_back(std::move(MB)); // take MB ownership
146 return MBRef;
147}
148
149void LinkerDriver::readDynamicList(StringRef Path) {
150 if (Optional<MemoryBufferRef> Buffer = readFile(Path))
151 parseDynamicList(*Buffer);
152}
153
Rui Ueyama21eecb42016-02-02 21:13:09 +0000154// Add a given library by searching it from input search paths.
155void LinkerDriver::addLibrary(StringRef Name) {
156 std::string Path = searchLibrary(Name);
157 if (Path.empty())
George Rimar777f9632016-03-12 08:31:34 +0000158 error("unable to find library -l" + Name);
Rui Ueyama21eecb42016-02-02 21:13:09 +0000159 else
160 addFile(Path);
161}
162
Rui Ueyama1fc5d482016-04-02 18:18:44 +0000163// This function is called on startup. We need this for LTO since
164// LTO calls LLVM functions to compile bitcode files to native code.
165// Technically this can be delayed until we read bitcode files, but
166// we don't bother to do lazily because the initialization is fast.
167static void initLLVM(opt::InputArgList &Args) {
168 InitializeAllTargets();
169 InitializeAllTargetMCs();
170 InitializeAllAsmPrinters();
171 InitializeAllAsmParsers();
172
173 // Parse and evaluate -mllvm options.
174 std::vector<const char *> V;
175 V.push_back("lld (LLVM option parsing)");
176 for (auto *Arg : Args.filtered(OPT_mllvm))
177 V.push_back(Arg->getValue());
178 cl::ParseCommandLineOptions(V.size(), V.data());
179}
180
Rui Ueyamad32c63d2016-01-07 17:33:25 +0000181// Some command line options or some combinations of them are not allowed.
182// This function checks for such errors.
183static void checkOptions(opt::InputArgList &Args) {
Rui Ueyamad32c63d2016-01-07 17:33:25 +0000184 // The MIPS ABI as of 2016 does not support the GNU-style symbol lookup
185 // table which is a relatively new feature.
186 if (Config->EMachine == EM_MIPS && Config->GnuHash)
George Rimar777f9632016-03-12 08:31:34 +0000187 error("the .gnu.hash section is not compatible with the MIPS target.");
Rui Ueyamad32c63d2016-01-07 17:33:25 +0000188
189 if (Config->EMachine == EM_AMDGPU && !Config->Entry.empty())
Rui Ueyama21eecb42016-02-02 21:13:09 +0000190 error("-e option is not valid for AMDGPU.");
George Rimar58941ee2016-02-25 08:23:37 +0000191
George Rimar786e8662016-03-17 05:57:33 +0000192 if (Config->Pie && Config->Shared)
193 error("-shared and -pie may not be used together");
194
Rui Ueyamad1aa97b2016-04-02 18:52:23 +0000195 if (Config->Relocatable) {
196 if (Config->Shared)
197 error("-r and -shared may not be used together");
198 if (Config->GcSections)
199 error("-r and --gc-sections may not be used together");
200 if (Config->ICF)
201 error("-r and --icf may not be used together");
202 if (Config->Pie)
203 error("-r and -pie may not be used together");
204 }
Rui Ueyamad32c63d2016-01-07 17:33:25 +0000205}
206
Rui Ueyama2cac5842015-10-07 19:34:51 +0000207static StringRef
208getString(opt::InputArgList &Args, unsigned Key, StringRef Default = "") {
209 if (auto *Arg = Args.getLastArg(Key))
210 return Arg->getValue();
211 return Default;
212}
213
Rui Ueyamab339b6d2016-03-31 21:15:31 +0000214static int getInteger(opt::InputArgList &Args, unsigned Key, int Default) {
215 int V = Default;
216 if (auto *Arg = Args.getLastArg(Key)) {
217 StringRef S = Arg->getValue();
218 if (S.getAsInteger(10, V))
219 error(Arg->getSpelling() + ": number expected, but got " + S);
220 }
221 return V;
222}
223
Rui Ueyama1a8fffa2015-11-12 19:00:37 +0000224static bool hasZOption(opt::InputArgList &Args, StringRef Key) {
225 for (auto *Arg : Args.filtered(OPT_z))
226 if (Key == Arg->getValue())
227 return true;
228 return false;
229}
230
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000231void LinkerDriver::main(ArrayRef<const char *> ArgsArr) {
Rui Ueyama15fa0352016-03-15 18:20:50 +0000232 ELFOptTable Parser;
233 opt::InputArgList Args = Parser.parse(ArgsArr.slice(1));
Rui Ueyama1eb9f442016-02-28 03:18:09 +0000234 if (Args.hasArg(OPT_help)) {
235 printHelp(ArgsArr[0]);
236 return;
237 }
Rui Ueyama1abcf372016-02-28 03:18:07 +0000238 if (Args.hasArg(OPT_version)) {
239 printVersion();
240 return;
241 }
242
Rui Ueyama1fc5d482016-04-02 18:18:44 +0000243 initLLVM(Args);
Rui Ueyama0dd684c2016-01-07 17:54:19 +0000244 readConfigs(Args);
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000245 createFiles(Args);
Rui Ueyamad32c63d2016-01-07 17:33:25 +0000246 checkOptions(Args);
Rui Ueyama21eecb42016-02-02 21:13:09 +0000247 if (HasError)
248 return;
Rui Ueyamaf5bcf2a2015-11-12 18:54:15 +0000249
Rui Ueyamae717a712015-10-13 16:20:50 +0000250 switch (Config->EKind) {
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000251 case ELF32LEKind:
252 link<ELF32LE>(Args);
253 return;
254 case ELF32BEKind:
255 link<ELF32BE>(Args);
256 return;
257 case ELF64LEKind:
258 link<ELF64LE>(Args);
259 return;
260 case ELF64BEKind:
261 link<ELF64BE>(Args);
262 return;
263 default:
Rui Ueyama21eecb42016-02-02 21:13:09 +0000264 error("-m or at least a .o file required");
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000265 }
266}
267
Rui Ueyama0dd684c2016-01-07 17:54:19 +0000268// Initializes Config members by the command line options.
269void LinkerDriver::readConfigs(opt::InputArgList &Args) {
Rafael Espindola06501922016-03-08 17:13:12 +0000270 for (auto *Arg : Args.filtered(OPT_L))
271 Config->SearchPaths.push_back(Arg->getValue());
272
Rafael Espindola2e9eac12015-09-11 21:18:56 +0000273 std::vector<StringRef> RPaths;
274 for (auto *Arg : Args.filtered(OPT_rpath))
275 RPaths.push_back(Arg->getValue());
276 if (!RPaths.empty())
277 Config->RPath = llvm::join(RPaths.begin(), RPaths.end(), ":");
278
Rui Ueyama9aa56862015-11-24 18:55:36 +0000279 if (auto *Arg = Args.getLastArg(OPT_m)) {
Rui Ueyama94b08e32016-01-12 01:33:23 +0000280 // Parse ELF{32,64}{LE,BE} and CPU type.
Rui Ueyama9aa56862015-11-24 18:55:36 +0000281 StringRef S = Arg->getValue();
Rui Ueyama94b08e32016-01-12 01:33:23 +0000282 std::tie(Config->EKind, Config->EMachine) = parseEmulation(S);
Rui Ueyama9aa56862015-11-24 18:55:36 +0000283 Config->Emulation = S;
284 }
285
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000286 Config->AllowMultipleDefinition = Args.hasArg(OPT_allow_multiple_definition);
Davide Italianocebb4492015-10-13 21:02:34 +0000287 Config->Bsymbolic = Args.hasArg(OPT_Bsymbolic);
George Rimar5c36e592016-02-02 09:28:53 +0000288 Config->BsymbolicFunctions = Args.hasArg(OPT_Bsymbolic_functions);
Rui Ueyamaa4a628f2016-01-13 18:55:39 +0000289 Config->Demangle = !Args.hasArg(OPT_no_demangle);
Davide Italiano842fa532016-04-03 03:39:09 +0000290 Config->DisableVerify = Args.hasArg(OPT_disable_verify);
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000291 Config->DiscardAll = Args.hasArg(OPT_discard_all);
Davide Italiano3c0410d2016-04-21 04:46:22 +0000292 Config->DiscardValueNames = Args.hasArg(OPT_lto_discard_value_names);
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000293 Config->DiscardLocals = Args.hasArg(OPT_discard_locals);
294 Config->DiscardNone = Args.hasArg(OPT_discard_none);
George Rimarf6bc65a2016-01-15 13:34:52 +0000295 Config->EhFrameHdr = Args.hasArg(OPT_eh_frame_hdr);
Davide Italianoc39c75d2015-10-06 16:20:00 +0000296 Config->EnableNewDtags = !Args.hasArg(OPT_disable_new_dtags);
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000297 Config->ExportDynamic = Args.hasArg(OPT_export_dynamic);
Rui Ueyamac4aaed92015-10-22 18:49:53 +0000298 Config->GcSections = Args.hasArg(OPT_gc_sections);
Rui Ueyama0b289522016-02-25 18:43:51 +0000299 Config->ICF = Args.hasArg(OPT_icf);
Rui Ueyama8c764872016-04-07 20:41:41 +0000300 Config->NoGnuUnique = Args.hasArg(OPT_no_gnu_unique);
George Rimar57e40de2015-10-01 20:14:45 +0000301 Config->NoUndefined = Args.hasArg(OPT_no_undefined);
Rui Ueyama3f9f0922016-03-08 04:06:29 +0000302 Config->NoinhibitExec = Args.hasArg(OPT_noinhibit_exec);
George Rimar786e8662016-03-17 05:57:33 +0000303 Config->Pie = Args.hasArg(OPT_pie);
George Rimara5fbebc2015-12-10 09:12:18 +0000304 Config->PrintGcSections = Args.hasArg(OPT_print_gc_sections);
George Rimar58941ee2016-02-25 08:23:37 +0000305 Config->Relocatable = Args.hasArg(OPT_relocatable);
Sean Silva35ef3d92016-03-09 20:01:08 +0000306 Config->SaveTemps = Args.hasArg(OPT_save_temps);
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000307 Config->Shared = Args.hasArg(OPT_shared);
George Rimar5dad7c12015-10-24 08:52:46 +0000308 Config->StripAll = Args.hasArg(OPT_strip_all);
Rui Ueyamafc6a4b02016-04-07 21:04:51 +0000309 Config->StripDebug = Args.hasArg(OPT_strip_debug);
Rui Ueyamae9809502016-03-11 04:23:12 +0000310 Config->Threads = Args.hasArg(OPT_threads);
George Rimar1e4b39f2016-03-29 08:45:40 +0000311 Config->Trace = Args.hasArg(OPT_trace);
Rui Ueyamaa4672402015-10-11 02:03:03 +0000312 Config->Verbose = Args.hasArg(OPT_verbose);
George Rimar34358002016-03-14 09:19:30 +0000313 Config->WarnCommon = Args.hasArg(OPT_warn_common);
Rui Ueyamad7c417c2015-09-29 22:33:18 +0000314
Rui Ueyama2cac5842015-10-07 19:34:51 +0000315 Config->DynamicLinker = getString(Args, OPT_dynamic_linker);
316 Config->Entry = getString(Args, OPT_entry);
317 Config->Fini = getString(Args, OPT_fini, "_fini");
318 Config->Init = getString(Args, OPT_init, "_init");
Rui Ueyama964ffb32015-10-09 00:33:44 +0000319 Config->OutputFile = getString(Args, OPT_o);
Rui Ueyama2cac5842015-10-07 19:34:51 +0000320 Config->SoName = getString(Args, OPT_soname);
321 Config->Sysroot = getString(Args, OPT_sysroot);
322
Rui Ueyamab339b6d2016-03-31 21:15:31 +0000323 Config->Optimize = getInteger(Args, OPT_O, 0);
324 Config->LtoO = getInteger(Args, OPT_lto_O, 2);
Davide Italiano8848d442016-04-03 02:41:15 +0000325 if (Config->LtoO > 3)
326 error("invalid optimization level for LTO: " + getString(Args, OPT_lto_O));
Davide Italianobc176632016-04-15 22:38:10 +0000327 Config->LtoJobs = getInteger(Args, OPT_lto_jobs, 1);
328 if (Config->LtoJobs == 0)
329 error("number of threads must be > 0");
Rui Ueyamab339b6d2016-03-31 21:15:31 +0000330
Rui Ueyama7b19c342015-11-24 18:48:16 +0000331 Config->ZExecStack = hasZOption(Args, "execstack");
Rui Ueyama1a8fffa2015-11-12 19:00:37 +0000332 Config->ZNodelete = hasZOption(Args, "nodelete");
333 Config->ZNow = hasZOption(Args, "now");
334 Config->ZOrigin = hasZOption(Args, "origin");
George Rimare3336c02015-11-24 10:15:50 +0000335 Config->ZRelro = !hasZOption(Args, "norelro");
Rui Ueyama1a8fffa2015-11-12 19:00:37 +0000336
George Rimar58941ee2016-02-25 08:23:37 +0000337 if (Config->Relocatable)
338 Config->StripAll = false;
339
Rui Ueyamafc6a4b02016-04-07 21:04:51 +0000340 // --strip-all implies --strip-debug.
341 if (Config->StripAll)
342 Config->StripDebug = true;
343
344 // Config->Pic is true if we are generating position-independent code.
345 Config->Pic = Config->Pie || Config->Shared;
346
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000347 if (auto *Arg = Args.getLastArg(OPT_hash_style)) {
348 StringRef S = Arg->getValue();
349 if (S == "gnu") {
350 Config->GnuHash = true;
351 Config->SysvHash = false;
352 } else if (S == "both") {
353 Config->GnuHash = true;
354 } else if (S != "sysv")
George Rimar777f9632016-03-12 08:31:34 +0000355 error("unknown hash style: " + S);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000356 }
357
Rui Ueyama3a41be22016-04-07 22:49:21 +0000358 // Parse --build-id or --build-id=<style>.
359 if (Args.hasArg(OPT_build_id))
360 Config->BuildId = BuildIdKind::Fnv1;
361 if (auto *Arg = Args.getLastArg(OPT_build_id_eq)) {
362 StringRef S = Arg->getValue();
363 if (S == "md5") {
364 Config->BuildId = BuildIdKind::Md5;
Rui Ueyamad86ec302016-04-07 23:51:56 +0000365 } else if (S == "sha1") {
366 Config->BuildId = BuildIdKind::Sha1;
Rui Ueyama3a41be22016-04-07 22:49:21 +0000367 } else
368 error("unknown --build-id style: " + S);
369 }
370
George Rimar83f406c2015-10-19 17:35:12 +0000371 for (auto *Arg : Args.filtered(OPT_undefined))
372 Config->Undefined.push_back(Arg->getValue());
Adhemerval Zanella9df07202016-04-13 18:51:11 +0000373
374 if (Args.hasArg(OPT_dynamic_list))
375 readDynamicList(getString(Args, OPT_dynamic_list));
Rui Ueyama0dd684c2016-01-07 17:54:19 +0000376}
George Rimar83f406c2015-10-19 17:35:12 +0000377
Rui Ueyama0dd684c2016-01-07 17:54:19 +0000378void LinkerDriver::createFiles(opt::InputArgList &Args) {
Igor Kudrind912ee92015-10-01 16:42:03 +0000379 for (auto *Arg : Args) {
380 switch (Arg->getOption().getID()) {
381 case OPT_l:
Rui Ueyama21eecb42016-02-02 21:13:09 +0000382 addLibrary(Arg->getValue());
Igor Kudrind912ee92015-10-01 16:42:03 +0000383 break;
384 case OPT_INPUT:
Davide Italiano4e47d582015-10-11 03:53:36 +0000385 case OPT_script:
Igor Kudrind912ee92015-10-01 16:42:03 +0000386 addFile(Arg->getValue());
387 break;
Rui Ueyama35da9b62015-10-11 20:59:12 +0000388 case OPT_as_needed:
389 Config->AsNeeded = true;
390 break;
391 case OPT_no_as_needed:
392 Config->AsNeeded = false;
393 break;
Igor Kudrind912ee92015-10-01 16:42:03 +0000394 case OPT_Bstatic:
395 Config->Static = true;
396 break;
397 case OPT_Bdynamic:
398 Config->Static = false;
399 break;
Igor Kudrin2696bbe2015-10-01 18:02:21 +0000400 case OPT_whole_archive:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000401 WholeArchive = true;
Igor Kudrin2696bbe2015-10-01 18:02:21 +0000402 break;
403 case OPT_no_whole_archive:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000404 WholeArchive = false;
Igor Kudrin2696bbe2015-10-01 18:02:21 +0000405 break;
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000406 case OPT_start_lib:
407 InLib = true;
408 break;
409 case OPT_end_lib:
410 InLib = false;
411 break;
Rui Ueyamaf5c4aca2015-09-30 17:06:09 +0000412 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000413 }
414
Rui Ueyama21eecb42016-02-02 21:13:09 +0000415 if (Files.empty() && !HasError)
George Rimar57610422016-03-11 14:43:02 +0000416 error("no input files.");
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000417}
418
419template <class ELFT> void LinkerDriver::link(opt::InputArgList &Args) {
420 SymbolTable<ELFT> Symtab;
Rui Ueyama07320e42016-04-20 20:13:41 +0000421
Rui Ueyamac1c282a2016-02-11 21:18:01 +0000422 std::unique_ptr<TargetInfo> TI(createTarget());
423 Target = TI.get();
Rui Ueyama07320e42016-04-20 20:13:41 +0000424 LinkerScript<ELFT> LS;
425 Script<ELFT>::X = &LS;
Rui Ueyamaff777682015-10-09 21:12:40 +0000426
Rui Ueyama6c5638b2016-03-13 20:10:20 +0000427 Config->Rela = ELFT::Is64Bits;
428
George Rimar177f7bc2016-03-17 11:00:27 +0000429 // Add entry symbol.
430 // There is no entry symbol for AMDGPU binaries, so skip adding one to avoid
431 // having and undefined symbol.
Davide Italiano00a7cf92016-03-15 22:24:58 +0000432 if (Config->Entry.empty() && !Config->Shared && !Config->Relocatable &&
George Rimar177f7bc2016-03-17 11:00:27 +0000433 Config->EMachine != EM_AMDGPU)
Davide Italiano00a7cf92016-03-15 22:24:58 +0000434 Config->Entry = Config->EMachine == EM_MIPS ? "__start" : "_start";
Rui Ueyama2b675072015-10-14 22:20:57 +0000435
Ed Mastef2ac6882015-12-11 17:46:46 +0000436 if (!Config->Entry.empty()) {
437 // Set either EntryAddr (if S is a number) or EntrySym (otherwise).
438 StringRef S = Config->Entry;
439 if (S.getAsInteger(0, Config->EntryAddr))
Rafael Espindola38c67a22016-04-15 14:41:56 +0000440 Config->EntrySym = Symtab.addUndefined(S)->getSymbol();
Ed Mastef2ac6882015-12-11 17:46:46 +0000441 }
442
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000443 for (std::unique_ptr<InputFile> &F : Files)
444 Symtab.addFile(std::move(F));
Rui Ueyama16ba6692016-01-29 19:41:13 +0000445 if (HasError)
446 return; // There were duplicate symbols or incompatible files
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000447
Rui Ueyamaf215dac2015-10-19 20:55:28 +0000448 for (StringRef S : Config->Undefined)
449 Symtab.addUndefinedOpt(S);
Denis Protivensky22220d52015-10-05 09:43:57 +0000450
Davide Italiano887d76c2016-04-03 02:16:56 +0000451 // -save-temps creates a file based on the output file name so we want
452 // to set it right before LTO. This code can't be moved to option parsing
453 // because linker scripts can override the output filename using the
454 // OUTPUT() directive.
455 if (Config->OutputFile.empty())
456 Config->OutputFile = "a.out";
457
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000458 Symtab.addCombinedLtoObject();
459
Rui Ueyamadeb15402016-01-07 17:20:07 +0000460 for (auto *Arg : Args.filtered(OPT_wrap))
461 Symtab.wrap(Arg->getValue());
462
Rui Ueyama75230392015-10-07 18:29:51 +0000463 // Write the result to the file.
Rui Ueyama93bfee52015-10-13 18:10:33 +0000464 Symtab.scanShlibUndefined();
Adhemerval Zanella9df07202016-04-13 18:51:11 +0000465 Symtab.scanDynamicList();
Rui Ueyamac4aaed92015-10-22 18:49:53 +0000466 if (Config->GcSections)
467 markLive<ELFT>(&Symtab);
Rui Ueyama0b289522016-02-25 18:43:51 +0000468 if (Config->ICF)
469 doIcf<ELFT>(&Symtab);
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000470 writeResult<ELFT>(&Symtab);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000471}