Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 1 | //===- 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 Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 10 | #include "Driver.h" |
Rafael Espindola | 192e1fa | 2015-08-06 15:08:23 +0000 | [diff] [blame] | 11 | #include "Config.h" |
| 12 | #include "Error.h" |
Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 13 | #include "InputFiles.h" |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 14 | #include "LinkerScript.h" |
Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 15 | #include "SymbolTable.h" |
Rui Ueyama | ff77768 | 2015-10-09 21:12:40 +0000 | [diff] [blame] | 16 | #include "Target.h" |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 17 | #include "Writer.h" |
| 18 | #include "llvm/ADT/STLExtras.h" |
Rafael Espindola | 2e9eac1 | 2015-09-11 21:18:56 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/StringExtras.h" |
Rui Ueyama | a467240 | 2015-10-11 02:03:03 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
Rui Ueyama | cacf967 | 2015-10-11 02:22:31 +0000 | [diff] [blame] | 21 | #include <utility> |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace llvm; |
Denis Protivensky | 1ef7b3f | 2015-10-07 09:13:03 +0000 | [diff] [blame] | 24 | using namespace llvm::ELF; |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 25 | using namespace llvm::object; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 26 | |
| 27 | using namespace lld; |
| 28 | using namespace lld::elf2; |
| 29 | |
Rui Ueyama | 83cd6e0 | 2016-01-06 20:11:55 +0000 | [diff] [blame] | 30 | Configuration *elf2::Config; |
| 31 | LinkerDriver *elf2::Driver; |
Rui Ueyama | f5c4aca | 2015-09-30 17:06:09 +0000 | [diff] [blame] | 32 | |
Rui Ueyama | b694011 | 2016-02-02 22:49:32 +0000 | [diff] [blame] | 33 | bool elf2::link(ArrayRef<const char *> Args, raw_ostream &Error) { |
Rui Ueyama | 64cfffd | 2016-01-28 18:40:06 +0000 | [diff] [blame] | 34 | HasError = false; |
Rui Ueyama | b694011 | 2016-02-02 22:49:32 +0000 | [diff] [blame] | 35 | ErrorOS = &Error; |
Rui Ueyama | 570752c | 2015-08-18 09:13:25 +0000 | [diff] [blame] | 36 | Configuration C; |
Rui Ueyama | 983ed2b | 2015-10-01 15:23:09 +0000 | [diff] [blame] | 37 | LinkerDriver D; |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 38 | LinkerScript LS; |
Rui Ueyama | 570752c | 2015-08-18 09:13:25 +0000 | [diff] [blame] | 39 | Config = &C; |
Rui Ueyama | 983ed2b | 2015-10-01 15:23:09 +0000 | [diff] [blame] | 40 | Driver = &D; |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 41 | Script = &LS; |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 42 | Driver->main(Args.slice(1)); |
Rui Ueyama | 64cfffd | 2016-01-28 18:40:06 +0000 | [diff] [blame] | 43 | return !HasError; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Rui Ueyama | cacf967 | 2015-10-11 02:22:31 +0000 | [diff] [blame] | 46 | static std::pair<ELFKind, uint16_t> parseEmulation(StringRef S) { |
Rui Ueyama | a561e78 | 2015-10-11 19:46:00 +0000 | [diff] [blame] | 47 | if (S == "elf32btsmip") |
| 48 | return {ELF32BEKind, EM_MIPS}; |
| 49 | if (S == "elf32ltsmip") |
| 50 | return {ELF32LEKind, EM_MIPS}; |
Davide Italiano | 15414cd | 2016-01-12 02:58:59 +0000 | [diff] [blame] | 51 | if (S == "elf32ppc" || S == "elf32ppc_fbsd") |
Rui Ueyama | a561e78 | 2015-10-11 19:46:00 +0000 | [diff] [blame] | 52 | return {ELF32BEKind, EM_PPC}; |
Davide Italiano | 15414cd | 2016-01-12 02:58:59 +0000 | [diff] [blame] | 53 | if (S == "elf64ppc" || S == "elf64ppc_fbsd") |
Rui Ueyama | a561e78 | 2015-10-11 19:46:00 +0000 | [diff] [blame] | 54 | return {ELF64BEKind, EM_PPC64}; |
| 55 | if (S == "elf_i386") |
| 56 | return {ELF32LEKind, EM_386}; |
| 57 | if (S == "elf_x86_64") |
| 58 | return {ELF64LEKind, EM_X86_64}; |
Igor Kudrin | 2f610d5 | 2015-11-20 02:48:53 +0000 | [diff] [blame] | 59 | if (S == "aarch64linux") |
| 60 | return {ELF64LEKind, EM_AARCH64}; |
Rui Ueyama | 9aa5686 | 2015-11-24 18:55:36 +0000 | [diff] [blame] | 61 | if (S == "i386pe" || S == "i386pep" || S == "thumb2pe") |
Rui Ueyama | 21eecb4 | 2016-02-02 21:13:09 +0000 | [diff] [blame] | 62 | error("Windows targets are not supported on the ELF frontend: " + S); |
| 63 | else |
| 64 | error("Unknown emulation: " + S); |
| 65 | return {ELFNoneKind, 0}; |
Denis Protivensky | 1ef7b3f | 2015-10-07 09:13:03 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Rui Ueyama | 9b09369 | 2016-01-06 00:51:35 +0000 | [diff] [blame] | 68 | // Returns slices of MB by parsing MB as an archive file. |
| 69 | // Each slice consists of a member file in the archive. |
| 70 | static std::vector<MemoryBufferRef> getArchiveMembers(MemoryBufferRef MB) { |
| 71 | ErrorOr<std::unique_ptr<Archive>> FileOrErr = Archive::create(MB); |
Rui Ueyama | 64cfffd | 2016-01-28 18:40:06 +0000 | [diff] [blame] | 72 | fatal(FileOrErr, "Failed to parse archive"); |
Rui Ueyama | 9b09369 | 2016-01-06 00:51:35 +0000 | [diff] [blame] | 73 | std::unique_ptr<Archive> File = std::move(*FileOrErr); |
| 74 | |
| 75 | std::vector<MemoryBufferRef> V; |
| 76 | for (const ErrorOr<Archive::Child> &C : File->children()) { |
Rui Ueyama | 64cfffd | 2016-01-28 18:40:06 +0000 | [diff] [blame] | 77 | fatal(C, "Could not get the child of the archive " + File->getFileName()); |
Rui Ueyama | 9b09369 | 2016-01-06 00:51:35 +0000 | [diff] [blame] | 78 | ErrorOr<MemoryBufferRef> MbOrErr = C->getMemoryBufferRef(); |
Rui Ueyama | 64cfffd | 2016-01-28 18:40:06 +0000 | [diff] [blame] | 79 | fatal(MbOrErr, "Could not get the buffer for a child of the archive " + |
Rui Ueyama | 9b09369 | 2016-01-06 00:51:35 +0000 | [diff] [blame] | 80 | File->getFileName()); |
| 81 | V.push_back(*MbOrErr); |
| 82 | } |
| 83 | return V; |
| 84 | } |
| 85 | |
Rui Ueyama | 983ed2b | 2015-10-01 15:23:09 +0000 | [diff] [blame] | 86 | // Opens and parses a file. Path has to be resolved already. |
| 87 | // Newly created memory buffers are owned by this driver. |
| 88 | void LinkerDriver::addFile(StringRef Path) { |
Rui Ueyama | f5c4aca | 2015-09-30 17:06:09 +0000 | [diff] [blame] | 89 | using namespace llvm::sys::fs; |
Rui Ueyama | a467240 | 2015-10-11 02:03:03 +0000 | [diff] [blame] | 90 | if (Config->Verbose) |
| 91 | llvm::outs() << Path << "\n"; |
Rui Ueyama | 983ed2b | 2015-10-01 15:23:09 +0000 | [diff] [blame] | 92 | auto MBOrErr = MemoryBuffer::getFile(Path); |
Rui Ueyama | 21eecb4 | 2016-02-02 21:13:09 +0000 | [diff] [blame] | 93 | if (error(MBOrErr, "cannot open " + Path)) |
| 94 | return; |
Rui Ueyama | 983ed2b | 2015-10-01 15:23:09 +0000 | [diff] [blame] | 95 | 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 Ueyama | f9de0d6 | 2016-02-11 21:38:55 +0000 | [diff] [blame^] | 101 | Script->read(MBRef); |
Rui Ueyama | 983ed2b | 2015-10-01 15:23:09 +0000 | [diff] [blame] | 102 | return; |
| 103 | case file_magic::archive: |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 104 | if (WholeArchive) { |
Rui Ueyama | 71c066d | 2016-02-02 08:22:41 +0000 | [diff] [blame] | 105 | StringRef S = MBRef.getBufferIdentifier(); |
Rui Ueyama | 9b09369 | 2016-01-06 00:51:35 +0000 | [diff] [blame] | 106 | for (MemoryBufferRef MB : getArchiveMembers(MBRef)) |
Rui Ueyama | 71c066d | 2016-02-02 08:22:41 +0000 | [diff] [blame] | 107 | Files.push_back(createObjectFile(MB, S)); |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 108 | return; |
| 109 | } |
| 110 | Files.push_back(make_unique<ArchiveFile>(MBRef)); |
Rui Ueyama | 983ed2b | 2015-10-01 15:23:09 +0000 | [diff] [blame] | 111 | return; |
Rafael Espindola | af70764 | 2015-10-12 01:55:32 +0000 | [diff] [blame] | 112 | case file_magic::elf_shared_object: |
Rui Ueyama | 533c030 | 2016-01-06 00:09:43 +0000 | [diff] [blame] | 113 | Files.push_back(createSharedFile(MBRef)); |
Rui Ueyama | 983ed2b | 2015-10-01 15:23:09 +0000 | [diff] [blame] | 114 | return; |
| 115 | default: |
Rui Ueyama | 533c030 | 2016-01-06 00:09:43 +0000 | [diff] [blame] | 116 | Files.push_back(createObjectFile(MBRef)); |
Rui Ueyama | 983ed2b | 2015-10-01 15:23:09 +0000 | [diff] [blame] | 117 | } |
Rui Ueyama | f5c4aca | 2015-09-30 17:06:09 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Rui Ueyama | 21eecb4 | 2016-02-02 21:13:09 +0000 | [diff] [blame] | 120 | // Add a given library by searching it from input search paths. |
| 121 | void LinkerDriver::addLibrary(StringRef Name) { |
| 122 | std::string Path = searchLibrary(Name); |
| 123 | if (Path.empty()) |
| 124 | error("Unable to find library -l" + Name); |
| 125 | else |
| 126 | addFile(Path); |
| 127 | } |
| 128 | |
Rui Ueyama | d32c63d | 2016-01-07 17:33:25 +0000 | [diff] [blame] | 129 | // Some command line options or some combinations of them are not allowed. |
| 130 | // This function checks for such errors. |
| 131 | static void checkOptions(opt::InputArgList &Args) { |
| 132 | // Traditional linkers can generate re-linkable object files instead |
| 133 | // of executables or DSOs. We don't support that since the feature |
| 134 | // does not seem to provide more value than the static archiver. |
| 135 | if (Args.hasArg(OPT_relocatable)) |
Rui Ueyama | 21eecb4 | 2016-02-02 21:13:09 +0000 | [diff] [blame] | 136 | error("-r option is not supported. Use 'ar' command instead."); |
Rui Ueyama | d32c63d | 2016-01-07 17:33:25 +0000 | [diff] [blame] | 137 | |
| 138 | // The MIPS ABI as of 2016 does not support the GNU-style symbol lookup |
| 139 | // table which is a relatively new feature. |
| 140 | if (Config->EMachine == EM_MIPS && Config->GnuHash) |
Rui Ueyama | 21eecb4 | 2016-02-02 21:13:09 +0000 | [diff] [blame] | 141 | error("The .gnu.hash section is not compatible with the MIPS target."); |
Rui Ueyama | d32c63d | 2016-01-07 17:33:25 +0000 | [diff] [blame] | 142 | |
| 143 | if (Config->EMachine == EM_AMDGPU && !Config->Entry.empty()) |
Rui Ueyama | 21eecb4 | 2016-02-02 21:13:09 +0000 | [diff] [blame] | 144 | error("-e option is not valid for AMDGPU."); |
Rui Ueyama | d32c63d | 2016-01-07 17:33:25 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Rui Ueyama | 2cac584 | 2015-10-07 19:34:51 +0000 | [diff] [blame] | 147 | static StringRef |
| 148 | getString(opt::InputArgList &Args, unsigned Key, StringRef Default = "") { |
| 149 | if (auto *Arg = Args.getLastArg(Key)) |
| 150 | return Arg->getValue(); |
| 151 | return Default; |
| 152 | } |
| 153 | |
Rui Ueyama | 1a8fffa | 2015-11-12 19:00:37 +0000 | [diff] [blame] | 154 | static bool hasZOption(opt::InputArgList &Args, StringRef Key) { |
| 155 | for (auto *Arg : Args.filtered(OPT_z)) |
| 156 | if (Key == Arg->getValue()) |
| 157 | return true; |
| 158 | return false; |
| 159 | } |
| 160 | |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 161 | void LinkerDriver::main(ArrayRef<const char *> ArgsArr) { |
Rui Ueyama | 9aa5686 | 2015-11-24 18:55:36 +0000 | [diff] [blame] | 162 | initSymbols(); |
Rui Ueyama | 9ea49c7 | 2015-10-07 23:46:11 +0000 | [diff] [blame] | 163 | |
Rui Ueyama | c6e1b97 | 2015-10-11 18:19:01 +0000 | [diff] [blame] | 164 | opt::InputArgList Args = parseArgs(&Alloc, ArgsArr); |
Rui Ueyama | 0dd684c | 2016-01-07 17:54:19 +0000 | [diff] [blame] | 165 | readConfigs(Args); |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 166 | createFiles(Args); |
Rui Ueyama | d32c63d | 2016-01-07 17:33:25 +0000 | [diff] [blame] | 167 | checkOptions(Args); |
Rui Ueyama | 21eecb4 | 2016-02-02 21:13:09 +0000 | [diff] [blame] | 168 | if (HasError) |
| 169 | return; |
Rui Ueyama | f5bcf2a | 2015-11-12 18:54:15 +0000 | [diff] [blame] | 170 | |
Rui Ueyama | e717a71 | 2015-10-13 16:20:50 +0000 | [diff] [blame] | 171 | switch (Config->EKind) { |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 172 | case ELF32LEKind: |
| 173 | link<ELF32LE>(Args); |
| 174 | return; |
| 175 | case ELF32BEKind: |
| 176 | link<ELF32BE>(Args); |
| 177 | return; |
| 178 | case ELF64LEKind: |
| 179 | link<ELF64LE>(Args); |
| 180 | return; |
| 181 | case ELF64BEKind: |
| 182 | link<ELF64BE>(Args); |
| 183 | return; |
| 184 | default: |
Rui Ueyama | 21eecb4 | 2016-02-02 21:13:09 +0000 | [diff] [blame] | 185 | error("-m or at least a .o file required"); |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
Rui Ueyama | 0dd684c | 2016-01-07 17:54:19 +0000 | [diff] [blame] | 189 | // Initializes Config members by the command line options. |
| 190 | void LinkerDriver::readConfigs(opt::InputArgList &Args) { |
Rui Ueyama | 2cac584 | 2015-10-07 19:34:51 +0000 | [diff] [blame] | 191 | for (auto *Arg : Args.filtered(OPT_L)) |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 192 | Config->SearchPaths.push_back(Arg->getValue()); |
Igor Kudrin | 1309fc0 | 2015-09-28 15:01:59 +0000 | [diff] [blame] | 193 | |
Rafael Espindola | 2e9eac1 | 2015-09-11 21:18:56 +0000 | [diff] [blame] | 194 | std::vector<StringRef> RPaths; |
| 195 | for (auto *Arg : Args.filtered(OPT_rpath)) |
| 196 | RPaths.push_back(Arg->getValue()); |
| 197 | if (!RPaths.empty()) |
| 198 | Config->RPath = llvm::join(RPaths.begin(), RPaths.end(), ":"); |
| 199 | |
Rui Ueyama | 9aa5686 | 2015-11-24 18:55:36 +0000 | [diff] [blame] | 200 | if (auto *Arg = Args.getLastArg(OPT_m)) { |
Rui Ueyama | 94b08e3 | 2016-01-12 01:33:23 +0000 | [diff] [blame] | 201 | // Parse ELF{32,64}{LE,BE} and CPU type. |
Rui Ueyama | 9aa5686 | 2015-11-24 18:55:36 +0000 | [diff] [blame] | 202 | StringRef S = Arg->getValue(); |
Rui Ueyama | 94b08e3 | 2016-01-12 01:33:23 +0000 | [diff] [blame] | 203 | std::tie(Config->EKind, Config->EMachine) = parseEmulation(S); |
Rui Ueyama | 9aa5686 | 2015-11-24 18:55:36 +0000 | [diff] [blame] | 204 | Config->Emulation = S; |
| 205 | } |
| 206 | |
Rui Ueyama | d7c417c | 2015-09-29 22:33:18 +0000 | [diff] [blame] | 207 | Config->AllowMultipleDefinition = Args.hasArg(OPT_allow_multiple_definition); |
Davide Italiano | cebb449 | 2015-10-13 21:02:34 +0000 | [diff] [blame] | 208 | Config->Bsymbolic = Args.hasArg(OPT_Bsymbolic); |
George Rimar | 5c36e59 | 2016-02-02 09:28:53 +0000 | [diff] [blame] | 209 | Config->BsymbolicFunctions = Args.hasArg(OPT_Bsymbolic_functions); |
Rui Ueyama | a4a628f | 2016-01-13 18:55:39 +0000 | [diff] [blame] | 210 | Config->Demangle = !Args.hasArg(OPT_no_demangle); |
Rui Ueyama | d7c417c | 2015-09-29 22:33:18 +0000 | [diff] [blame] | 211 | Config->DiscardAll = Args.hasArg(OPT_discard_all); |
| 212 | Config->DiscardLocals = Args.hasArg(OPT_discard_locals); |
| 213 | Config->DiscardNone = Args.hasArg(OPT_discard_none); |
George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 214 | Config->EhFrameHdr = Args.hasArg(OPT_eh_frame_hdr); |
Davide Italiano | c39c75d | 2015-10-06 16:20:00 +0000 | [diff] [blame] | 215 | Config->EnableNewDtags = !Args.hasArg(OPT_disable_new_dtags); |
Rui Ueyama | d7c417c | 2015-09-29 22:33:18 +0000 | [diff] [blame] | 216 | Config->ExportDynamic = Args.hasArg(OPT_export_dynamic); |
Rui Ueyama | c4aaed9 | 2015-10-22 18:49:53 +0000 | [diff] [blame] | 217 | Config->GcSections = Args.hasArg(OPT_gc_sections); |
Rui Ueyama | d7c417c | 2015-09-29 22:33:18 +0000 | [diff] [blame] | 218 | Config->NoInhibitExec = Args.hasArg(OPT_noinhibit_exec); |
George Rimar | 57e40de | 2015-10-01 20:14:45 +0000 | [diff] [blame] | 219 | Config->NoUndefined = Args.hasArg(OPT_no_undefined); |
George Rimar | a5fbebc | 2015-12-10 09:12:18 +0000 | [diff] [blame] | 220 | Config->PrintGcSections = Args.hasArg(OPT_print_gc_sections); |
Rui Ueyama | d7c417c | 2015-09-29 22:33:18 +0000 | [diff] [blame] | 221 | Config->Shared = Args.hasArg(OPT_shared); |
George Rimar | 5dad7c1 | 2015-10-24 08:52:46 +0000 | [diff] [blame] | 222 | Config->StripAll = Args.hasArg(OPT_strip_all); |
Rui Ueyama | a467240 | 2015-10-11 02:03:03 +0000 | [diff] [blame] | 223 | Config->Verbose = Args.hasArg(OPT_verbose); |
Rui Ueyama | d7c417c | 2015-09-29 22:33:18 +0000 | [diff] [blame] | 224 | |
Rui Ueyama | 2cac584 | 2015-10-07 19:34:51 +0000 | [diff] [blame] | 225 | Config->DynamicLinker = getString(Args, OPT_dynamic_linker); |
| 226 | Config->Entry = getString(Args, OPT_entry); |
| 227 | Config->Fini = getString(Args, OPT_fini, "_fini"); |
| 228 | Config->Init = getString(Args, OPT_init, "_init"); |
Rui Ueyama | 964ffb3 | 2015-10-09 00:33:44 +0000 | [diff] [blame] | 229 | Config->OutputFile = getString(Args, OPT_o); |
Rui Ueyama | 2cac584 | 2015-10-07 19:34:51 +0000 | [diff] [blame] | 230 | Config->SoName = getString(Args, OPT_soname); |
| 231 | Config->Sysroot = getString(Args, OPT_sysroot); |
| 232 | |
Rui Ueyama | 7b19c34 | 2015-11-24 18:48:16 +0000 | [diff] [blame] | 233 | Config->ZExecStack = hasZOption(Args, "execstack"); |
Rui Ueyama | 1a8fffa | 2015-11-12 19:00:37 +0000 | [diff] [blame] | 234 | Config->ZNodelete = hasZOption(Args, "nodelete"); |
| 235 | Config->ZNow = hasZOption(Args, "now"); |
| 236 | Config->ZOrigin = hasZOption(Args, "origin"); |
George Rimar | e3336c0 | 2015-11-24 10:15:50 +0000 | [diff] [blame] | 237 | Config->ZRelro = !hasZOption(Args, "norelro"); |
Rui Ueyama | 1a8fffa | 2015-11-12 19:00:37 +0000 | [diff] [blame] | 238 | |
Rafael Espindola | 2c1217d | 2015-10-23 19:02:19 +0000 | [diff] [blame] | 239 | if (auto *Arg = Args.getLastArg(OPT_O)) { |
| 240 | StringRef Val = Arg->getValue(); |
| 241 | if (Val.getAsInteger(10, Config->Optimize)) |
Rui Ueyama | 21eecb4 | 2016-02-02 21:13:09 +0000 | [diff] [blame] | 242 | error("Invalid optimization level"); |
Rafael Espindola | 2c1217d | 2015-10-23 19:02:19 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 245 | if (auto *Arg = Args.getLastArg(OPT_hash_style)) { |
| 246 | StringRef S = Arg->getValue(); |
| 247 | if (S == "gnu") { |
| 248 | Config->GnuHash = true; |
| 249 | Config->SysvHash = false; |
| 250 | } else if (S == "both") { |
| 251 | Config->GnuHash = true; |
| 252 | } else if (S != "sysv") |
Rui Ueyama | 21eecb4 | 2016-02-02 21:13:09 +0000 | [diff] [blame] | 253 | error("Unknown hash style: " + S); |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 254 | } |
| 255 | |
George Rimar | 83f406c | 2015-10-19 17:35:12 +0000 | [diff] [blame] | 256 | for (auto *Arg : Args.filtered(OPT_undefined)) |
| 257 | Config->Undefined.push_back(Arg->getValue()); |
Rui Ueyama | 0dd684c | 2016-01-07 17:54:19 +0000 | [diff] [blame] | 258 | } |
George Rimar | 83f406c | 2015-10-19 17:35:12 +0000 | [diff] [blame] | 259 | |
Rui Ueyama | 0dd684c | 2016-01-07 17:54:19 +0000 | [diff] [blame] | 260 | void LinkerDriver::createFiles(opt::InputArgList &Args) { |
Igor Kudrin | d912ee9 | 2015-10-01 16:42:03 +0000 | [diff] [blame] | 261 | for (auto *Arg : Args) { |
| 262 | switch (Arg->getOption().getID()) { |
| 263 | case OPT_l: |
Rui Ueyama | 21eecb4 | 2016-02-02 21:13:09 +0000 | [diff] [blame] | 264 | addLibrary(Arg->getValue()); |
Igor Kudrin | d912ee9 | 2015-10-01 16:42:03 +0000 | [diff] [blame] | 265 | break; |
| 266 | case OPT_INPUT: |
Davide Italiano | 4e47d58 | 2015-10-11 03:53:36 +0000 | [diff] [blame] | 267 | case OPT_script: |
Igor Kudrin | d912ee9 | 2015-10-01 16:42:03 +0000 | [diff] [blame] | 268 | addFile(Arg->getValue()); |
| 269 | break; |
Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 +0000 | [diff] [blame] | 270 | case OPT_as_needed: |
| 271 | Config->AsNeeded = true; |
| 272 | break; |
| 273 | case OPT_no_as_needed: |
| 274 | Config->AsNeeded = false; |
| 275 | break; |
Igor Kudrin | d912ee9 | 2015-10-01 16:42:03 +0000 | [diff] [blame] | 276 | case OPT_Bstatic: |
| 277 | Config->Static = true; |
| 278 | break; |
| 279 | case OPT_Bdynamic: |
| 280 | Config->Static = false; |
| 281 | break; |
Igor Kudrin | 2696bbe | 2015-10-01 18:02:21 +0000 | [diff] [blame] | 282 | case OPT_whole_archive: |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 283 | WholeArchive = true; |
Igor Kudrin | 2696bbe | 2015-10-01 18:02:21 +0000 | [diff] [blame] | 284 | break; |
| 285 | case OPT_no_whole_archive: |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 286 | WholeArchive = false; |
Igor Kudrin | 2696bbe | 2015-10-01 18:02:21 +0000 | [diff] [blame] | 287 | break; |
Rui Ueyama | f5c4aca | 2015-09-30 17:06:09 +0000 | [diff] [blame] | 288 | } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Rui Ueyama | 21eecb4 | 2016-02-02 21:13:09 +0000 | [diff] [blame] | 291 | if (Files.empty() && !HasError) |
| 292 | error("no input files."); |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | template <class ELFT> void LinkerDriver::link(opt::InputArgList &Args) { |
| 296 | SymbolTable<ELFT> Symtab; |
Rui Ueyama | c1c282a | 2016-02-11 21:18:01 +0000 | [diff] [blame] | 297 | std::unique_ptr<TargetInfo> TI(createTarget()); |
| 298 | Target = TI.get(); |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 299 | Script->finalize(); |
Rui Ueyama | ff77768 | 2015-10-09 21:12:40 +0000 | [diff] [blame] | 300 | |
| 301 | if (!Config->Shared) { |
| 302 | // Add entry symbol. |
Tom Stellard | 80efb16 | 2016-01-07 03:59:08 +0000 | [diff] [blame] | 303 | // |
| 304 | // There is no entry symbol for AMDGPU binaries, so skip adding one to avoid |
| 305 | // having and undefined symbol. |
| 306 | if (Config->Entry.empty() && Config->EMachine != EM_AMDGPU) |
Rui Ueyama | 0cef689 | 2015-10-12 15:23:54 +0000 | [diff] [blame] | 307 | Config->Entry = (Config->EMachine == EM_MIPS) ? "__start" : "_start"; |
Rui Ueyama | 2b67507 | 2015-10-14 22:20:57 +0000 | [diff] [blame] | 308 | |
Rui Ueyama | ff77768 | 2015-10-09 21:12:40 +0000 | [diff] [blame] | 309 | // In the assembly for 32 bit x86 the _GLOBAL_OFFSET_TABLE_ symbol |
| 310 | // is magical and is used to produce a R_386_GOTPC relocation. |
| 311 | // The R_386_GOTPC relocation value doesn't actually depend on the |
| 312 | // symbol value, so it could use an index of STN_UNDEF which, according |
| 313 | // to the spec, means the symbol value is 0. |
| 314 | // Unfortunately both gas and MC keep the _GLOBAL_OFFSET_TABLE_ symbol in |
| 315 | // the object file. |
| 316 | // The situation is even stranger on x86_64 where the assembly doesn't |
| 317 | // need the magical symbol, but gas still puts _GLOBAL_OFFSET_TABLE_ as |
| 318 | // an undefined symbol in the .o files. |
| 319 | // Given that the symbol is effectively unused, we just create a dummy |
| 320 | // hidden one to avoid the undefined symbol error. |
Rui Ueyama | dd7d998 | 2015-12-16 22:31:14 +0000 | [diff] [blame] | 321 | Symtab.addIgnored("_GLOBAL_OFFSET_TABLE_"); |
Rui Ueyama | ff77768 | 2015-10-09 21:12:40 +0000 | [diff] [blame] | 322 | } |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 323 | |
Ed Maste | f2ac688 | 2015-12-11 17:46:46 +0000 | [diff] [blame] | 324 | if (!Config->Entry.empty()) { |
| 325 | // Set either EntryAddr (if S is a number) or EntrySym (otherwise). |
| 326 | StringRef S = Config->Entry; |
| 327 | if (S.getAsInteger(0, Config->EntryAddr)) |
| 328 | Config->EntrySym = Symtab.addUndefined(S); |
| 329 | } |
| 330 | |
Rui Ueyama | 2716130 | 2015-12-16 21:35:39 +0000 | [diff] [blame] | 331 | if (Config->EMachine == EM_MIPS) { |
| 332 | // On MIPS O32 ABI, _gp_disp is a magic symbol designates offset between |
Simon Atanasyan | b76108a | 2016-02-07 12:09:40 +0000 | [diff] [blame] | 333 | // start of function and 'gp' pointer into GOT. |
Rafael Espindola | 3a6a0a0 | 2016-01-19 00:05:54 +0000 | [diff] [blame] | 334 | Config->MipsGpDisp = Symtab.addIgnored("_gp_disp"); |
Simon Atanasyan | b76108a | 2016-02-07 12:09:40 +0000 | [diff] [blame] | 335 | // The __gnu_local_gp is a magic symbol equal to the current value of 'gp' |
| 336 | // pointer. This symbol is used in the code generated by .cpload pseudo-op |
| 337 | // in case of using -mno-shared option. |
| 338 | // https://sourceware.org/ml/binutils/2004-12/msg00094.html |
Simon Atanasyan | 597df21 | 2016-02-04 12:09:49 +0000 | [diff] [blame] | 339 | Config->MipsLocalGp = Symtab.addIgnored("__gnu_local_gp"); |
Rui Ueyama | 2716130 | 2015-12-16 21:35:39 +0000 | [diff] [blame] | 340 | |
| 341 | // Define _gp for MIPS. st_value of _gp symbol will be updated by Writer |
| 342 | // so that it points to an absolute address which is relative to GOT. |
| 343 | // See "Global Data Symbols" in Chapter 6 in the following document: |
| 344 | // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf |
Rui Ueyama | a246e094 | 2015-12-25 06:12:18 +0000 | [diff] [blame] | 345 | Symtab.addAbsolute("_gp", ElfSym<ELFT>::MipsGp); |
Rui Ueyama | 2716130 | 2015-12-16 21:35:39 +0000 | [diff] [blame] | 346 | } |
Igor Kudrin | 15cd9ff | 2015-11-06 07:43:03 +0000 | [diff] [blame] | 347 | |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 348 | for (std::unique_ptr<InputFile> &F : Files) |
| 349 | Symtab.addFile(std::move(F)); |
Rui Ueyama | 16ba669 | 2016-01-29 19:41:13 +0000 | [diff] [blame] | 350 | if (HasError) |
| 351 | return; // There were duplicate symbols or incompatible files |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 352 | |
Rui Ueyama | f215dac | 2015-10-19 20:55:28 +0000 | [diff] [blame] | 353 | for (StringRef S : Config->Undefined) |
| 354 | Symtab.addUndefinedOpt(S); |
Denis Protivensky | 22220d5 | 2015-10-05 09:43:57 +0000 | [diff] [blame] | 355 | |
Rui Ueyama | deb1540 | 2016-01-07 17:20:07 +0000 | [diff] [blame] | 356 | for (auto *Arg : Args.filtered(OPT_wrap)) |
| 357 | Symtab.wrap(Arg->getValue()); |
| 358 | |
Rui Ueyama | ee59282 | 2015-10-07 00:25:09 +0000 | [diff] [blame] | 359 | if (Config->OutputFile.empty()) |
| 360 | Config->OutputFile = "a.out"; |
| 361 | |
Rui Ueyama | 7523039 | 2015-10-07 18:29:51 +0000 | [diff] [blame] | 362 | // Write the result to the file. |
Rui Ueyama | 93bfee5 | 2015-10-13 18:10:33 +0000 | [diff] [blame] | 363 | Symtab.scanShlibUndefined(); |
Rui Ueyama | c4aaed9 | 2015-10-22 18:49:53 +0000 | [diff] [blame] | 364 | if (Config->GcSections) |
| 365 | markLive<ELFT>(&Symtab); |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 366 | writeResult<ELFT>(&Symtab); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 367 | } |