Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 | [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 | //===----------------------------------------------------------------------===// |
Rui Ueyama | ec29bee | 2017-01-26 01:52:05 | [diff] [blame] | 9 | // |
| 10 | // The driver drives the entire linking process. It is responsible for |
| 11 | // parsing command line options and doing whatever it is instructed to do. |
| 12 | // |
| 13 | // One notable thing in the LLD's driver when compared to other linkers is |
| 14 | // that the LLD's driver is agnostic on the host operating system. |
| 15 | // Other linkers usually have implicit default values (such as a dynamic |
| 16 | // linker path or library paths) for each host OS. |
| 17 | // |
| 18 | // I don't think implicit default values are useful because they are |
| 19 | // usually explicitly specified by the compiler driver. They can even |
| 20 | // be harmful when you are doing cross-linking. Therefore, in LLD, we |
Rui Ueyama | 81cb710 | 2017-03-24 00:15:57 | [diff] [blame] | 21 | // simply trust the compiler driver to pass all required options and |
| 22 | // don't try to make effort on our side. |
Rui Ueyama | ec29bee | 2017-01-26 01:52:05 | [diff] [blame] | 23 | // |
| 24 | //===----------------------------------------------------------------------===// |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 | [diff] [blame] | 25 | |
Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 | [diff] [blame] | 26 | #include "Driver.h" |
Rafael Espindola | 192e1fa | 2015-08-06 15:08:23 | [diff] [blame] | 27 | #include "Config.h" |
Rui Ueyama | 9b55e92 | 2017-03-24 00:15:16 | [diff] [blame] | 28 | #include "Filesystem.h" |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 | [diff] [blame] | 29 | #include "ICF.h" |
Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 | [diff] [blame] | 30 | #include "InputFiles.h" |
Rui Ueyama | b91bf1a | 2016-05-23 16:55:43 | [diff] [blame] | 31 | #include "InputSection.h" |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 | [diff] [blame] | 32 | #include "LinkerScript.h" |
Sam Clegg | f187c4d | 2018-02-20 22:09:59 | [diff] [blame] | 33 | #include "MarkLive.h" |
George Rimar | a8dba48 | 2017-03-20 10:09:58 | [diff] [blame] | 34 | #include "OutputSections.h" |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 35 | #include "ScriptParser.h" |
Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 | [diff] [blame] | 36 | #include "SymbolTable.h" |
Rafael Espindola | d26b52f | 2017-12-09 16:56:18 | [diff] [blame] | 37 | #include "Symbols.h" |
Peter Collingbourne | dc7936e | 2017-06-12 00:00:51 | [diff] [blame] | 38 | #include "SyntheticSections.h" |
Rui Ueyama | ff77768 | 2015-10-09 21:12:40 | [diff] [blame] | 39 | #include "Target.h" |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 | [diff] [blame] | 40 | #include "Writer.h" |
Rui Ueyama | 3e03944 | 2017-11-28 19:58:45 | [diff] [blame] | 41 | #include "lld/Common/Args.h" |
Rui Ueyama | 3f85170 | 2017-10-02 21:00:41 | [diff] [blame] | 42 | #include "lld/Common/Driver.h" |
Bob Haarman | b8a59c8 | 2017-10-25 22:28:38 | [diff] [blame] | 43 | #include "lld/Common/ErrorHandler.h" |
Rui Ueyama | 2017d52 | 2017-11-28 20:39:17 | [diff] [blame] | 44 | #include "lld/Common/Memory.h" |
Rui Ueyama | ee17371 | 2018-02-28 17:38:19 | [diff] [blame] | 45 | #include "lld/Common/Strings.h" |
George Rimar | 9dc740d | 2018-02-06 12:20:05 | [diff] [blame] | 46 | #include "lld/Common/TargetOptionsCommandFlags.h" |
Bob Haarman | 4f5c8c2 | 2017-10-13 18:22:55 | [diff] [blame] | 47 | #include "lld/Common/Threads.h" |
Rui Ueyama | 3f85170 | 2017-10-02 21:00:41 | [diff] [blame] | 48 | #include "lld/Common/Version.h" |
James Henderson | de300e6 | 2018-02-14 13:36:22 | [diff] [blame] | 49 | #include "llvm/ADT/SetVector.h" |
Rafael Espindola | 2e9eac1 | 2015-09-11 21:18:56 | [diff] [blame] | 50 | #include "llvm/ADT/StringExtras.h" |
Rui Ueyama | 7b14a62 | 2016-06-07 17:55:05 | [diff] [blame] | 51 | #include "llvm/ADT/StringSwitch.h" |
Peter Collingbourne | cd513a4 | 2016-11-11 19:50:24 | [diff] [blame] | 52 | #include "llvm/Support/CommandLine.h" |
George Rimar | dbf9339 | 2017-04-17 08:58:12 | [diff] [blame] | 53 | #include "llvm/Support/Compression.h" |
Peter Collingbourne | a327a4c | 2018-07-18 22:49:31 | [diff] [blame] | 54 | #include "llvm/Support/LEB128.h" |
Rui Ueyama | 7f1f912 | 2017-01-06 02:33:53 | [diff] [blame] | 55 | #include "llvm/Support/Path.h" |
Rui Ueyama | ec1c75e | 2017-01-09 01:42:02 | [diff] [blame] | 56 | #include "llvm/Support/TarWriter.h" |
Rafael Espindola | 9f77ef0 | 2016-02-12 20:54:57 | [diff] [blame] | 57 | #include "llvm/Support/TargetSelect.h" |
Rui Ueyama | a467240 | 2015-10-11 02:03:03 | [diff] [blame] | 58 | #include "llvm/Support/raw_ostream.h" |
Davide Italiano | e160f0d | 2016-06-24 18:02:50 | [diff] [blame] | 59 | #include <cstdlib> |
Rui Ueyama | cacf967 | 2015-10-11 02:22:31 | [diff] [blame] | 60 | #include <utility> |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 | [diff] [blame] | 61 | |
| 62 | using namespace llvm; |
Denis Protivensky | 1ef7b3f | 2015-10-07 09:13:03 | [diff] [blame] | 63 | using namespace llvm::ELF; |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 | [diff] [blame] | 64 | using namespace llvm::object; |
Rui Ueyama | 673b609 | 2016-04-26 20:36:46 | [diff] [blame] | 65 | using namespace llvm::sys; |
Rui Ueyama | d19bc04 | 2018-09-20 21:29:14 | [diff] [blame] | 66 | using namespace llvm::support; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 | [diff] [blame] | 67 | |
| 68 | using namespace lld; |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 | [diff] [blame] | 69 | using namespace lld::elf; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 | [diff] [blame] | 70 | |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 | [diff] [blame] | 71 | Configuration *elf::Config; |
| 72 | LinkerDriver *elf::Driver; |
Rui Ueyama | f5c4aca | 2015-09-30 17:06:09 | [diff] [blame] | 73 | |
Rafael Espindola | 7a7a81d | 2018-02-05 20:55:46 | [diff] [blame] | 74 | static void setConfigs(opt::InputArgList &Args); |
Rui Ueyama | d57e74b7 | 2017-03-17 23:29:01 | [diff] [blame] | 75 | |
Rafael Espindola | cb09daa | 2016-10-26 18:59:00 | [diff] [blame] | 76 | bool elf::link(ArrayRef<const char *> Args, bool CanExitEarly, |
| 77 | raw_ostream &Error) { |
Rui Ueyama | 4183120 | 2018-08-27 06:18:10 | [diff] [blame] | 78 | errorHandler().LogName = args::getFilenameWithoutExe(Args[0]); |
Bob Haarman | b8a59c8 | 2017-10-25 22:28:38 | [diff] [blame] | 79 | errorHandler().ErrorLimitExceededMsg = |
| 80 | "too many errors emitted, stopping now (use " |
| 81 | "-error-limit=0 to see all errors)"; |
| 82 | errorHandler().ErrorOS = &Error; |
Rui Ueyama | 7c9ad29 | 2018-02-16 23:41:48 | [diff] [blame] | 83 | errorHandler().ExitEarly = CanExitEarly; |
Bob Haarman | b8a59c8 | 2017-10-25 22:28:38 | [diff] [blame] | 84 | errorHandler().ColorDiagnostics = Error.has_colors(); |
Rui Ueyama | 7c9ad29 | 2018-02-16 23:41:48 | [diff] [blame] | 85 | |
Rui Ueyama | 536a267 | 2017-02-27 02:32:08 | [diff] [blame] | 86 | InputSections.clear(); |
Evgeny Mankov | 31fef4d | 2017-09-25 14:42:15 | [diff] [blame] | 87 | OutputSections.clear(); |
Evgeny Mankov | 31fef4d | 2017-09-25 14:42:15 | [diff] [blame] | 88 | BinaryFiles.clear(); |
| 89 | BitcodeFiles.clear(); |
| 90 | ObjectFiles.clear(); |
| 91 | SharedFiles.clear(); |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 | [diff] [blame] | 92 | |
Rui Ueyama | 6f6d46d | 2016-12-08 17:48:52 | [diff] [blame] | 93 | Config = make<Configuration>(); |
| 94 | Driver = make<LinkerDriver>(); |
Rui Ueyama | a34da93 | 2017-03-21 23:03:09 | [diff] [blame] | 95 | Script = make<LinkerScript>(); |
Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 | [diff] [blame] | 96 | Symtab = make<SymbolTable>(); |
Rui Ueyama | 4e24752 | 2018-09-25 19:26:58 | [diff] [blame] | 97 | |
| 98 | Tar = nullptr; |
| 99 | memset(&In, 0, sizeof(In)); |
| 100 | |
Rafael Espindola | 64626b3 | 2018-02-06 22:37:05 | [diff] [blame] | 101 | Config->ProgName = Args[0]; |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 | [diff] [blame] | 102 | |
Rui Ueyama | 7c9ad29 | 2018-02-16 23:41:48 | [diff] [blame] | 103 | Driver->main(Args); |
Rui Ueyama | 7d1f5fd | 2017-10-04 00:50:11 | [diff] [blame] | 104 | |
| 105 | // Exit immediately if we don't need to return to the caller. |
| 106 | // This saves time because the overhead of calling destructors |
| 107 | // for all globally-allocated objects is not negligible. |
Rui Ueyama | 7c9ad29 | 2018-02-16 23:41:48 | [diff] [blame] | 108 | if (CanExitEarly) |
Bob Haarman | b8a59c8 | 2017-10-25 22:28:38 | [diff] [blame] | 109 | exitLld(errorCount() ? 1 : 0); |
Rui Ueyama | 7d1f5fd | 2017-10-04 00:50:11 | [diff] [blame] | 110 | |
Rui Ueyama | 55518e7 | 2016-10-28 20:57:25 | [diff] [blame] | 111 | freeArena(); |
Bob Haarman | b8a59c8 | 2017-10-25 22:28:38 | [diff] [blame] | 112 | return !errorCount(); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 | [diff] [blame] | 113 | } |
| 114 | |
Rui Ueyama | 7b14a62 | 2016-06-07 17:55:05 | [diff] [blame] | 115 | // Parses a linker -m option. |
Rui Ueyama | 58026af | 2016-11-09 22:32:43 | [diff] [blame] | 116 | static std::tuple<ELFKind, uint16_t, uint8_t> parseEmulation(StringRef Emul) { |
Rafael Espindola | 7cc713a | 2016-10-27 14:00:51 | [diff] [blame] | 117 | uint8_t OSABI = 0; |
Ed Maste | 2b68ada | 2016-09-08 19:36:22 | [diff] [blame] | 118 | StringRef S = Emul; |
Rafael Espindola | 7cc713a | 2016-10-27 14:00:51 | [diff] [blame] | 119 | if (S.endswith("_fbsd")) { |
Ed Maste | e2b7677 | 2016-03-31 20:26:30 | [diff] [blame] | 120 | S = S.drop_back(5); |
Rafael Espindola | 7cc713a | 2016-10-27 14:00:51 | [diff] [blame] | 121 | OSABI = ELFOSABI_FREEBSD; |
| 122 | } |
Rui Ueyama | 7b14a62 | 2016-06-07 17:55:05 | [diff] [blame] | 123 | |
| 124 | std::pair<ELFKind, uint16_t> Ret = |
| 125 | StringSwitch<std::pair<ELFKind, uint16_t>>(S) |
Peter Smith | 2689d9f | 2018-05-04 14:28:29 | [diff] [blame] | 126 | .Cases("aarch64elf", "aarch64linux", "aarch64_elf64_le_vec", |
| 127 | {ELF64LEKind, EM_AARCH64}) |
Eugene Leviant | ad270ec | 2017-06-14 08:25:38 | [diff] [blame] | 128 | .Cases("armelf", "armelf_linux_eabi", {ELF32LEKind, EM_ARM}) |
Rui Ueyama | 1e52f25 | 2016-07-12 23:28:33 | [diff] [blame] | 129 | .Case("elf32_x86_64", {ELF32LEKind, EM_X86_64}) |
Rui Ueyama | 8e0c326 | 2017-01-30 01:50:16 | [diff] [blame] | 130 | .Cases("elf32btsmip", "elf32btsmipn32", {ELF32BEKind, EM_MIPS}) |
| 131 | .Cases("elf32ltsmip", "elf32ltsmipn32", {ELF32LEKind, EM_MIPS}) |
Rui Ueyama | 5cd9c6b | 2018-08-09 17:59:56 | [diff] [blame] | 132 | .Case("elf32lriscv", {ELF32LEKind, EM_RISCV}) |
Rui Ueyama | 7b14a62 | 2016-06-07 17:55:05 | [diff] [blame] | 133 | .Case("elf32ppc", {ELF32BEKind, EM_PPC}) |
| 134 | .Case("elf64btsmip", {ELF64BEKind, EM_MIPS}) |
| 135 | .Case("elf64ltsmip", {ELF64LEKind, EM_MIPS}) |
Rui Ueyama | 5cd9c6b | 2018-08-09 17:59:56 | [diff] [blame] | 136 | .Case("elf64lriscv", {ELF64LEKind, EM_RISCV}) |
Rui Ueyama | 7b14a62 | 2016-06-07 17:55:05 | [diff] [blame] | 137 | .Case("elf64ppc", {ELF64BEKind, EM_PPC64}) |
Fangrui Song | 17208f1 | 2018-03-09 22:11:46 | [diff] [blame] | 138 | .Case("elf64lppc", {ELF64LEKind, EM_PPC64}) |
Rui Ueyama | c79ecdd | 2016-09-30 22:01:25 | [diff] [blame] | 139 | .Cases("elf_amd64", "elf_x86_64", {ELF64LEKind, EM_X86_64}) |
Rui Ueyama | 7b14a62 | 2016-06-07 17:55:05 | [diff] [blame] | 140 | .Case("elf_i386", {ELF32LEKind, EM_386}) |
Rui Ueyama | 6c50990 | 2016-08-03 20:15:56 | [diff] [blame] | 141 | .Case("elf_iamcu", {ELF32LEKind, EM_IAMCU}) |
Rui Ueyama | 7b14a62 | 2016-06-07 17:55:05 | [diff] [blame] | 142 | .Default({ELFNoneKind, EM_NONE}); |
| 143 | |
Martell Malone | 894dbbe | 2017-09-11 17:02:59 | [diff] [blame] | 144 | if (Ret.first == ELFNoneKind) |
| 145 | error("unknown emulation: " + Emul); |
Rui Ueyama | 58026af | 2016-11-09 22:32:43 | [diff] [blame] | 146 | return std::make_tuple(Ret.first, Ret.second, OSABI); |
Denis Protivensky | 1ef7b3f | 2015-10-07 09:13:03 | [diff] [blame] | 147 | } |
| 148 | |
Rui Ueyama | 9b09369 | 2016-01-06 00:51:35 | [diff] [blame] | 149 | // Returns slices of MB by parsing MB as an archive file. |
| 150 | // Each slice consists of a member file in the archive. |
Rafael Espindola | 0b1413a | 2017-05-05 15:08:06 | [diff] [blame] | 151 | std::vector<std::pair<MemoryBufferRef, uint64_t>> static getArchiveMembers( |
| 152 | MemoryBufferRef MB) { |
Rafael Espindola | 1130935c | 2016-03-03 16:21:44 | [diff] [blame] | 153 | std::unique_ptr<Archive> File = |
Rui Ueyama | bdc5150 | 2017-12-06 22:08:17 | [diff] [blame] | 154 | CHECK(Archive::create(MB), |
Eugene Leviant | 7d7ff80 | 2016-11-21 09:28:07 | [diff] [blame] | 155 | MB.getBufferIdentifier() + ": failed to parse archive"); |
Rui Ueyama | 9b09369 | 2016-01-06 00:51:35 | [diff] [blame] | 156 | |
Rafael Espindola | 0b1413a | 2017-05-05 15:08:06 | [diff] [blame] | 157 | std::vector<std::pair<MemoryBufferRef, uint64_t>> V; |
Mehdi Amini | 41af430 | 2016-11-11 04:28:40 | [diff] [blame] | 158 | Error Err = Error::success(); |
Rafael Espindola | 9e50291 | 2017-09-20 22:59:50 | [diff] [blame] | 159 | bool AddToTar = File->isThin() && Tar; |
Lang Hames | 622ef17 | 2016-07-14 02:35:18 | [diff] [blame] | 160 | for (const ErrorOr<Archive::Child> &COrErr : File->children(Err)) { |
Eugene Leviant | 7d7ff80 | 2016-11-21 09:28:07 | [diff] [blame] | 161 | Archive::Child C = |
Rui Ueyama | bdc5150 | 2017-12-06 22:08:17 | [diff] [blame] | 162 | CHECK(COrErr, MB.getBufferIdentifier() + |
Eugene Leviant | 7d7ff80 | 2016-11-21 09:28:07 | [diff] [blame] | 163 | ": could not get the child of the archive"); |
Rui Ueyama | 7e71c61 | 2016-04-02 19:09:07 | [diff] [blame] | 164 | MemoryBufferRef MBRef = |
Rui Ueyama | bdc5150 | 2017-12-06 22:08:17 | [diff] [blame] | 165 | CHECK(C.getMemoryBufferRef(), |
Eugene Leviant | 7d7ff80 | 2016-11-21 09:28:07 | [diff] [blame] | 166 | MB.getBufferIdentifier() + |
| 167 | ": could not get the buffer for a child of the archive"); |
Rafael Espindola | 9e50291 | 2017-09-20 22:59:50 | [diff] [blame] | 168 | if (AddToTar) |
| 169 | Tar->append(relativeToRoot(check(C.getFullName())), MBRef.getBuffer()); |
Rafael Espindola | 0b1413a | 2017-05-05 15:08:06 | [diff] [blame] | 170 | V.push_back(std::make_pair(MBRef, C.getChildOffset())); |
Rui Ueyama | 9b09369 | 2016-01-06 00:51:35 | [diff] [blame] | 171 | } |
Rui Ueyama | f8292e9 | 2016-07-15 02:01:03 | [diff] [blame] | 172 | if (Err) |
Eugene Leviant | 7d7ff80 | 2016-11-21 09:28:07 | [diff] [blame] | 173 | fatal(MB.getBufferIdentifier() + ": Archive::children failed: " + |
| 174 | toString(std::move(Err))); |
Peter Collingbourne | d418b1d | 2016-03-31 23:12:18 | [diff] [blame] | 175 | |
| 176 | // Take ownership of memory buffers created for members of thin archives. |
| 177 | for (std::unique_ptr<MemoryBuffer> &MB : File->takeThinBuffers()) |
Rui Ueyama | 58841b4 | 2016-12-23 03:19:09 | [diff] [blame] | 178 | make<std::unique_ptr<MemoryBuffer>>(std::move(MB)); |
Peter Collingbourne | d418b1d | 2016-03-31 23:12:18 | [diff] [blame] | 179 | |
Rui Ueyama | 9b09369 | 2016-01-06 00:51:35 | [diff] [blame] | 180 | return V; |
| 181 | } |
| 182 | |
Rui Ueyama | b201a20 | 2017-05-03 23:10:33 | [diff] [blame] | 183 | // Opens a file and create a file object. Path has to be resolved already. |
Evgeniy Stepanov | a76349b | 2017-04-12 00:13:48 | [diff] [blame] | 184 | void LinkerDriver::addFile(StringRef Path, bool WithLOption) { |
Rui Ueyama | 5002a67 | 2016-05-02 19:59:56 | [diff] [blame] | 185 | using namespace sys::fs; |
Davide Italiano | 034f58a9 | 2016-04-26 00:22:24 | [diff] [blame] | 186 | |
Adhemerval Zanella | 9df0720 | 2016-04-13 18:51:11 | [diff] [blame] | 187 | Optional<MemoryBufferRef> Buffer = readFile(Path); |
| 188 | if (!Buffer.hasValue()) |
Rui Ueyama | 21eecb4 | 2016-02-02 21:13:09 | [diff] [blame] | 189 | return; |
Adhemerval Zanella | 9df0720 | 2016-04-13 18:51:11 | [diff] [blame] | 190 | MemoryBufferRef MBRef = *Buffer; |
Rui Ueyama | 983ed2b | 2015-10-01 15:23:09 | [diff] [blame] | 191 | |
Rui Ueyama | e262bb1 | 2018-08-06 21:29:41 | [diff] [blame] | 192 | if (Config->FormatBinary) { |
Rui Ueyama | d52adb3 | 2016-11-01 22:53:18 | [diff] [blame] | 193 | Files.push_back(make<BinaryFile>(MBRef)); |
Michael J. Spencer | a9424f3 | 2016-09-09 22:08:04 | [diff] [blame] | 194 | return; |
| 195 | } |
| 196 | |
Rui Ueyama | 983ed2b | 2015-10-01 15:23:09 | [diff] [blame] | 197 | switch (identify_magic(MBRef.getBuffer())) { |
| 198 | case file_magic::unknown: |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 | [diff] [blame] | 199 | readLinkerScript(MBRef); |
Rui Ueyama | 983ed2b | 2015-10-01 15:23:09 | [diff] [blame] | 200 | return; |
Rui Ueyama | fd7deda | 2017-05-03 21:03:08 | [diff] [blame] | 201 | case file_magic::archive: { |
| 202 | // Handle -whole-archive. |
Rui Ueyama | c773c9f | 2016-10-26 04:01:07 | [diff] [blame] | 203 | if (InWholeArchive) { |
Rafael Espindola | 0b1413a | 2017-05-05 15:08:06 | [diff] [blame] | 204 | for (const auto &P : getArchiveMembers(MBRef)) |
| 205 | Files.push_back(createObjectFile(P.first, Path, P.second)); |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 | [diff] [blame] | 206 | return; |
| 207 | } |
Rui Ueyama | fd7deda | 2017-05-03 21:03:08 | [diff] [blame] | 208 | |
| 209 | std::unique_ptr<Archive> File = |
Rui Ueyama | bdc5150 | 2017-12-06 22:08:17 | [diff] [blame] | 210 | CHECK(Archive::create(MBRef), Path + ": failed to parse archive"); |
Rui Ueyama | fd7deda | 2017-05-03 21:03:08 | [diff] [blame] | 211 | |
| 212 | // If an archive file has no symbol table, it is likely that a user |
| 213 | // is attempting LTO and using a default ar command that doesn't |
| 214 | // understand the LLVM bitcode file. It is a pretty common error, so |
| 215 | // we'll handle it as if it had a symbol table. |
George Rimar | 1840901a | 2017-06-09 12:26:57 | [diff] [blame] | 216 | if (!File->isEmpty() && !File->hasSymbolTable()) { |
Rafael Espindola | 0b1413a | 2017-05-05 15:08:06 | [diff] [blame] | 217 | for (const auto &P : getArchiveMembers(MBRef)) |
Rui Ueyama | 709fb2bb1 | 2017-07-26 22:13:32 | [diff] [blame] | 218 | Files.push_back(make<LazyObjFile>(P.first, Path, P.second)); |
Rui Ueyama | fd7deda | 2017-05-03 21:03:08 | [diff] [blame] | 219 | return; |
| 220 | } |
| 221 | |
| 222 | // Handle the regular case. |
| 223 | Files.push_back(make<ArchiveFile>(std::move(File))); |
Rui Ueyama | 983ed2b | 2015-10-01 15:23:09 | [diff] [blame] | 224 | return; |
Rui Ueyama | fd7deda | 2017-05-03 21:03:08 | [diff] [blame] | 225 | } |
Rafael Espindola | af70764 | 2015-10-12 01:55:32 | [diff] [blame] | 226 | case file_magic::elf_shared_object: |
Rui Ueyama | a01246b | 2018-12-18 22:30:23 | [diff] [blame] | 227 | if (Config->Static || Config->Relocatable) { |
George Rimar | 777f963 | 2016-03-12 08:31:34 | [diff] [blame] | 228 | error("attempted static link of dynamic object " + Path); |
George Rimar | 58941ee | 2016-02-25 08:23:37 | [diff] [blame] | 229 | return; |
| 230 | } |
Rui Ueyama | d2ccdbfa | 2017-06-21 03:05:08 | [diff] [blame] | 231 | |
Rui Ueyama | 3233d3e | 2017-04-13 00:23:32 | [diff] [blame] | 232 | // DSOs usually have DT_SONAME tags in their ELF headers, and the |
| 233 | // sonames are used to identify DSOs. But if they are missing, |
| 234 | // they are identified by filenames. We don't know whether the new |
| 235 | // file has a DT_SONAME or not because we haven't parsed it yet. |
| 236 | // Here, we set the default soname for the file because we might |
| 237 | // need it later. |
| 238 | // |
| 239 | // If a file was specified by -lfoo, the directory part is not |
| 240 | // significant, as a user did not specify it. This behavior is |
| 241 | // compatible with GNU. |
Rui Ueyama | d2ccdbfa | 2017-06-21 03:05:08 | [diff] [blame] | 242 | Files.push_back( |
| 243 | createSharedFile(MBRef, WithLOption ? path::filename(Path) : Path)); |
Rui Ueyama | 983ed2b | 2015-10-01 15:23:09 | [diff] [blame] | 244 | return; |
Rui Ueyama | d0de239 | 2018-02-02 00:27:49 | [diff] [blame] | 245 | case file_magic::bitcode: |
| 246 | case file_magic::elf_relocatable: |
Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 | [diff] [blame] | 247 | if (InLib) |
Rui Ueyama | 709fb2bb1 | 2017-07-26 22:13:32 | [diff] [blame] | 248 | Files.push_back(make<LazyObjFile>(MBRef, "", 0)); |
Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 | [diff] [blame] | 249 | else |
Rui Ueyama | 55518e7 | 2016-10-28 20:57:25 | [diff] [blame] | 250 | Files.push_back(createObjectFile(MBRef)); |
Rui Ueyama | d0de239 | 2018-02-02 00:27:49 | [diff] [blame] | 251 | break; |
| 252 | default: |
| 253 | error(Path + ": unknown file type"); |
Rui Ueyama | 983ed2b | 2015-10-01 15:23:09 | [diff] [blame] | 254 | } |
Rui Ueyama | f5c4aca | 2015-09-30 17:06:09 | [diff] [blame] | 255 | } |
| 256 | |
Rui Ueyama | 21eecb4 | 2016-02-02 21:13:09 | [diff] [blame] | 257 | // Add a given library by searching it from input search paths. |
| 258 | void LinkerDriver::addLibrary(StringRef Name) { |
Rui Ueyama | 061f928 | 2016-11-19 19:23:58 | [diff] [blame] | 259 | if (Optional<std::string> Path = searchLibrary(Name)) |
Evgeniy Stepanov | a76349b | 2017-04-12 00:13:48 | [diff] [blame] | 260 | addFile(*Path, /*WithLOption=*/true); |
Davide Italiano | bfccefd | 2016-04-24 18:23:21 | [diff] [blame] | 261 | else |
Rui Ueyama | 061f928 | 2016-11-19 19:23:58 | [diff] [blame] | 262 | error("unable to find library -l" + Name); |
Rui Ueyama | 21eecb4 | 2016-02-02 21:13:09 | [diff] [blame] | 263 | } |
| 264 | |
Rui Ueyama | 1fc5d48 | 2016-04-02 18:18:44 | [diff] [blame] | 265 | // This function is called on startup. We need this for LTO since |
| 266 | // LTO calls LLVM functions to compile bitcode files to native code. |
| 267 | // Technically this can be delayed until we read bitcode files, but |
| 268 | // we don't bother to do lazily because the initialization is fast. |
George Rimar | 9dc740d | 2018-02-06 12:20:05 | [diff] [blame] | 269 | static void initLLVM() { |
Rui Ueyama | 1fc5d48 | 2016-04-02 18:18:44 | [diff] [blame] | 270 | InitializeAllTargets(); |
| 271 | InitializeAllTargetMCs(); |
| 272 | InitializeAllAsmPrinters(); |
| 273 | InitializeAllAsmParsers(); |
Rui Ueyama | 1fc5d48 | 2016-04-02 18:18:44 | [diff] [blame] | 274 | } |
| 275 | |
Rui Ueyama | d32c63d | 2016-01-07 17:33:25 | [diff] [blame] | 276 | // Some command line options or some combinations of them are not allowed. |
| 277 | // This function checks for such errors. |
Sam Clegg | d5f6506 | 2018-11-15 18:09:41 | [diff] [blame] | 278 | static void checkOptions() { |
Rui Ueyama | d32c63d | 2016-01-07 17:33:25 | [diff] [blame] | 279 | // The MIPS ABI as of 2016 does not support the GNU-style symbol lookup |
| 280 | // table which is a relatively new feature. |
| 281 | if (Config->EMachine == EM_MIPS && Config->GnuHash) |
Rui Ueyama | 9db0642 | 2018-10-25 18:07:55 | [diff] [blame] | 282 | error("the .gnu.hash section is not compatible with the MIPS target"); |
Rui Ueyama | d32c63d | 2016-01-07 17:33:25 | [diff] [blame] | 283 | |
Peter Smith | 732cd8c | 2017-12-05 15:59:05 | [diff] [blame] | 284 | if (Config->FixCortexA53Errata843419 && Config->EMachine != EM_AARCH64) |
Rui Ueyama | 9db0642 | 2018-10-25 18:07:55 | [diff] [blame] | 285 | error("--fix-cortex-a53-843419 is only supported on AArch64 targets"); |
Peter Smith | 732cd8c | 2017-12-05 15:59:05 | [diff] [blame] | 286 | |
Sean Fertile | 7f3f05e | 2018-09-20 00:26:44 | [diff] [blame] | 287 | if (Config->TocOptimize && Config->EMachine != EM_PPC64) |
Rui Ueyama | 9db0642 | 2018-10-25 18:07:55 | [diff] [blame] | 288 | error("--toc-optimize is only supported on the PowerPC64 target"); |
Sean Fertile | 7f3f05e | 2018-09-20 00:26:44 | [diff] [blame] | 289 | |
George Rimar | 786e866 | 2016-03-17 05:57:33 | [diff] [blame] | 290 | if (Config->Pie && Config->Shared) |
| 291 | error("-shared and -pie may not be used together"); |
| 292 | |
George Rimar | f525c92 | 2017-07-17 09:43:18 | [diff] [blame] | 293 | if (!Config->Shared && !Config->FilterList.empty()) |
| 294 | error("-F may not be used without -shared"); |
| 295 | |
Rui Ueyama | cf8247e | 2017-04-26 21:27:33 | [diff] [blame] | 296 | if (!Config->Shared && !Config->AuxiliaryList.empty()) |
| 297 | error("-f may not be used without -shared"); |
| 298 | |
Rafael Espindola | 477ff12 | 2017-11-30 20:46:33 | [diff] [blame] | 299 | if (!Config->Relocatable && !Config->DefineCommon) |
| 300 | error("-no-define-common not supported in non relocatable output"); |
| 301 | |
Rui Ueyama | d1aa97b | 2016-04-02 18:52:23 | [diff] [blame] | 302 | if (Config->Relocatable) { |
| 303 | if (Config->Shared) |
| 304 | error("-r and -shared may not be used together"); |
| 305 | if (Config->GcSections) |
| 306 | error("-r and --gc-sections may not be used together"); |
Fangrui Song | 4a29482 | 2018-07-18 22:02:48 | [diff] [blame] | 307 | if (Config->GdbIndex) |
| 308 | error("-r and --gdb-index may not be used together"); |
Peter Collingbourne | a327a4c | 2018-07-18 22:49:31 | [diff] [blame] | 309 | if (Config->ICF != ICFLevel::None) |
Rui Ueyama | d1aa97b | 2016-04-02 18:52:23 | [diff] [blame] | 310 | error("-r and --icf may not be used together"); |
| 311 | if (Config->Pie) |
| 312 | error("-r and -pie may not be used together"); |
| 313 | } |
David Bolvansky | a932cd4 | 2018-07-30 17:02:46 | [diff] [blame] | 314 | |
| 315 | if (Config->ExecuteOnly) { |
| 316 | if (Config->EMachine != EM_AARCH64) |
| 317 | error("-execute-only is only supported on AArch64 targets"); |
| 318 | |
| 319 | if (Config->SingleRoRx && !Script->HasSectionsCommand) |
| 320 | error("-execute-only and -no-rosegment cannot be used together"); |
| 321 | } |
Rui Ueyama | d32c63d | 2016-01-07 17:33:25 | [diff] [blame] | 322 | } |
| 323 | |
Davide Italiano | e160f0d | 2016-06-24 18:02:50 | [diff] [blame] | 324 | static const char *getReproduceOption(opt::InputArgList &Args) { |
Rui Ueyama | b1421a1 | 2016-06-25 04:37:56 | [diff] [blame] | 325 | if (auto *Arg = Args.getLastArg(OPT_reproduce)) |
| 326 | return Arg->getValue(); |
Davide Italiano | e160f0d | 2016-06-24 18:02:50 | [diff] [blame] | 327 | return getenv("LLD_REPRODUCE"); |
| 328 | } |
| 329 | |
Rui Ueyama | 1a8fffa | 2015-11-12 19:00:37 | [diff] [blame] | 330 | static bool hasZOption(opt::InputArgList &Args, StringRef Key) { |
| 331 | for (auto *Arg : Args.filtered(OPT_z)) |
| 332 | if (Key == Arg->getValue()) |
| 333 | return true; |
| 334 | return false; |
| 335 | } |
| 336 | |
Rui Ueyama | 88fe5c9 | 2018-04-20 21:24:08 | [diff] [blame] | 337 | static bool getZFlag(opt::InputArgList &Args, StringRef K1, StringRef K2, |
| 338 | bool Default) { |
| 339 | for (auto *Arg : Args.filtered_reverse(OPT_z)) { |
| 340 | if (K1 == Arg->getValue()) |
| 341 | return true; |
| 342 | if (K2 == Arg->getValue()) |
| 343 | return false; |
| 344 | } |
| 345 | return Default; |
| 346 | } |
| 347 | |
Rui Ueyama | a071669 | 2018-09-20 21:40:38 | [diff] [blame] | 348 | static bool isKnownZFlag(StringRef S) { |
Rui Ueyama | 2fa0604 | 2018-06-27 07:22:27 | [diff] [blame] | 349 | return S == "combreloc" || S == "copyreloc" || S == "defs" || |
George Rimar | 27bbe7d | 2018-08-28 08:24:34 | [diff] [blame] | 350 | S == "execstack" || S == "global" || S == "hazardplt" || |
Ed Maste | c0b474f | 2018-09-14 14:25:37 | [diff] [blame] | 351 | S == "initfirst" || S == "interpose" || |
| 352 | S == "keep-text-section-prefix" || S == "lazy" || S == "muldefs" || |
George Rimar | a1b3ddb | 2018-11-27 09:48:17 | [diff] [blame] | 353 | S == "nocombreloc" || S == "nocopyreloc" || S == "nodefaultlib" || |
| 354 | S == "nodelete" || S == "nodlopen" || S == "noexecstack" || |
Rui Ueyama | 53f6bfb | 2018-06-27 07:56:23 | [diff] [blame] | 355 | S == "nokeep-text-section-prefix" || S == "norelro" || S == "notext" || |
| 356 | S == "now" || S == "origin" || S == "relro" || S == "retpolineplt" || |
| 357 | S == "rodynamic" || S == "text" || S == "wxneeded" || |
| 358 | S.startswith("max-page-size=") || S.startswith("stack-size="); |
Rui Ueyama | 2fa0604 | 2018-06-27 07:22:27 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | // Report an error for an unknown -z option. |
| 362 | static void checkZOptions(opt::InputArgList &Args) { |
| 363 | for (auto *Arg : Args.filtered(OPT_z)) |
Rui Ueyama | a071669 | 2018-09-20 21:40:38 | [diff] [blame] | 364 | if (!isKnownZFlag(Arg->getValue())) |
Rui Ueyama | 2fa0604 | 2018-06-27 07:22:27 | [diff] [blame] | 365 | error("unknown -z value: " + StringRef(Arg->getValue())); |
| 366 | } |
| 367 | |
Rui Ueyama | 7c9ad29 | 2018-02-16 23:41:48 | [diff] [blame] | 368 | void LinkerDriver::main(ArrayRef<const char *> ArgsArr) { |
Rui Ueyama | 15fa035 | 2016-03-15 18:20:50 | [diff] [blame] | 369 | ELFOptTable Parser; |
| 370 | opt::InputArgList Args = Parser.parse(ArgsArr.slice(1)); |
Rui Ueyama | 72b1ee2 | 2016-11-26 15:10:01 | [diff] [blame] | 371 | |
Rui Ueyama | 398f55f | 2016-12-20 02:17:24 | [diff] [blame] | 372 | // Interpret this flag early because error() depends on them. |
Rui Ueyama | 3e03944 | 2017-11-28 19:58:45 | [diff] [blame] | 373 | errorHandler().ErrorLimit = args::getInteger(Args, OPT_error_limit, 20); |
Rui Ueyama | 72b1ee2 | 2016-11-26 15:10:01 | [diff] [blame] | 374 | |
| 375 | // Handle -help |
Rui Ueyama | 1eb9f44 | 2016-02-28 03:18:09 | [diff] [blame] | 376 | if (Args.hasArg(OPT_help)) { |
Rafael Espindola | 64626b3 | 2018-02-06 22:37:05 | [diff] [blame] | 377 | printHelp(); |
Rui Ueyama | 1eb9f44 | 2016-02-28 03:18:09 | [diff] [blame] | 378 | return; |
| 379 | } |
George Rimar | 0a94bff | 2016-11-19 18:14:24 | [diff] [blame] | 380 | |
Rui Ueyama | bdc8bc0 | 2017-03-22 18:04:57 | [diff] [blame] | 381 | // Handle -v or -version. |
| 382 | // |
| 383 | // A note about "compatible with GNU linkers" message: this is a hack for |
| 384 | // scripts generated by GNU Libtool 2.4.6 (released in February 2014 and |
| 385 | // still the newest version in March 2017) or earlier to recognize LLD as |
| 386 | // a GNU compatible linker. As long as an output for the -v option |
| 387 | // contains "GNU" or "with BFD", they recognize us as GNU-compatible. |
| 388 | // |
| 389 | // This is somewhat ugly hack, but in reality, we had no choice other |
| 390 | // than doing this. Considering the very long release cycle of Libtool, |
| 391 | // it is not easy to improve it to recognize LLD as a GNU compatible |
| 392 | // linker in a timely manner. Even if we can make it, there are still a |
| 393 | // lot of "configure" scripts out there that are generated by old version |
| 394 | // of Libtool. We cannot convince every software developer to migrate to |
| 395 | // the latest version and re-generate scripts. So we have this hack. |
| 396 | if (Args.hasArg(OPT_v) || Args.hasArg(OPT_version)) |
| 397 | message(getLLDVersion() + " (compatible with GNU linkers)"); |
| 398 | |
Davide Italiano | e160f0d | 2016-06-24 18:02:50 | [diff] [blame] | 399 | if (const char *Path = getReproduceOption(Args)) { |
Rui Ueyama | fe65877 | 2016-05-15 17:10:23 | [diff] [blame] | 400 | // Note that --reproduce is a debug option so you can ignore it |
| 401 | // if you are trying to understand the whole picture of the code. |
Rui Ueyama | 7f1f912 | 2017-01-06 02:33:53 | [diff] [blame] | 402 | Expected<std::unique_ptr<TarWriter>> ErrOrWriter = |
| 403 | TarWriter::create(Path, path::stem(Path)); |
| 404 | if (ErrOrWriter) { |
Rui Ueyama | 2d7d128 | 2018-12-18 23:50:37 | [diff] [blame] | 405 | Tar = std::move(*ErrOrWriter); |
Rui Ueyama | 7f1f912 | 2017-01-06 02:33:53 | [diff] [blame] | 406 | Tar->append("response.txt", createResponseFile(Args)); |
| 407 | Tar->append("version.txt", getLLDVersion() + "\n"); |
Rui Ueyama | 7f1f912 | 2017-01-06 02:33:53 | [diff] [blame] | 408 | } else { |
Rui Ueyama | 5961809 | 2018-12-18 23:33:10 | [diff] [blame] | 409 | error("--reproduce: " + toString(ErrOrWriter.takeError())); |
Rui Ueyama | 7f1f912 | 2017-01-06 02:33:53 | [diff] [blame] | 410 | } |
Rafael Espindola | 1dd2b3d | 2016-05-03 17:30:44 | [diff] [blame] | 411 | } |
Davide Italiano | 034f58a9 | 2016-04-26 00:22:24 | [diff] [blame] | 412 | |
Rafael Espindola | 698dba7 | 2016-06-05 13:19:39 | [diff] [blame] | 413 | readConfigs(Args); |
Rui Ueyama | 2fa0604 | 2018-06-27 07:22:27 | [diff] [blame] | 414 | checkZOptions(Args); |
George Rimar | ee981860 | 2018-10-15 14:21:43 | [diff] [blame] | 415 | |
| 416 | // The behavior of -v or --version is a bit strange, but this is |
| 417 | // needed for compatibility with GNU linkers. |
| 418 | if (Args.hasArg(OPT_v) && !Args.hasArg(OPT_INPUT)) |
| 419 | return; |
| 420 | if (Args.hasArg(OPT_version)) |
| 421 | return; |
| 422 | |
George Rimar | 9dc740d | 2018-02-06 12:20:05 | [diff] [blame] | 423 | initLLVM(); |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 | [diff] [blame] | 424 | createFiles(Args); |
Rui Ueyama | 66f28f7 | 2018-06-05 16:13:40 | [diff] [blame] | 425 | if (errorCount()) |
| 426 | return; |
| 427 | |
Rui Ueyama | c185c01 | 2016-10-20 04:47:47 | [diff] [blame] | 428 | inferMachineType(); |
Rafael Espindola | 7a7a81d | 2018-02-05 20:55:46 | [diff] [blame] | 429 | setConfigs(Args); |
Sam Clegg | d5f6506 | 2018-11-15 18:09:41 | [diff] [blame] | 430 | checkOptions(); |
Bob Haarman | b8a59c8 | 2017-10-25 22:28:38 | [diff] [blame] | 431 | if (errorCount()) |
Rui Ueyama | 21eecb4 | 2016-02-02 21:13:09 | [diff] [blame] | 432 | return; |
Rui Ueyama | f5bcf2a | 2015-11-12 18:54:15 | [diff] [blame] | 433 | |
Rui Ueyama | e717a71 | 2015-10-13 16:20:50 | [diff] [blame] | 434 | switch (Config->EKind) { |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 | [diff] [blame] | 435 | case ELF32LEKind: |
| 436 | link<ELF32LE>(Args); |
| 437 | return; |
| 438 | case ELF32BEKind: |
| 439 | link<ELF32BE>(Args); |
| 440 | return; |
| 441 | case ELF64LEKind: |
| 442 | link<ELF64LE>(Args); |
| 443 | return; |
| 444 | case ELF64BEKind: |
| 445 | link<ELF64BE>(Args); |
| 446 | return; |
| 447 | default: |
Rui Ueyama | c185c01 | 2016-10-20 04:47:47 | [diff] [blame] | 448 | llvm_unreachable("unknown Config->EKind"); |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | |
Rui Ueyama | bd27849 | 2017-04-29 23:06:43 | [diff] [blame] | 452 | static std::string getRpath(opt::InputArgList &Args) { |
Rui Ueyama | 3e03944 | 2017-11-28 19:58:45 | [diff] [blame] | 453 | std::vector<StringRef> V = args::getStrings(Args, OPT_rpath); |
Rui Ueyama | 7edde29 | 2017-02-25 01:51:44 | [diff] [blame] | 454 | return llvm::join(V.begin(), V.end(), ":"); |
| 455 | } |
| 456 | |
Rui Ueyama | dc0464c | 2017-01-29 01:59:11 | [diff] [blame] | 457 | // Determines what we should do if there are remaining unresolved |
| 458 | // symbols after the name resolution. |
| 459 | static UnresolvedPolicy getUnresolvedSymbolPolicy(opt::InputArgList &Args) { |
Rui Ueyama | ac7aaeb | 2017-10-24 20:59:55 | [diff] [blame] | 460 | UnresolvedPolicy ErrorOrWarn = Args.hasFlag(OPT_error_unresolved_symbols, |
| 461 | OPT_warn_unresolved_symbols, true) |
Rui Ueyama | 0cbf1fd | 2017-03-23 18:16:42 | [diff] [blame] | 462 | ? UnresolvedPolicy::ReportError |
| 463 | : UnresolvedPolicy::Warn; |
Rui Ueyama | dc0464c | 2017-01-29 01:59:11 | [diff] [blame] | 464 | |
| 465 | // Process the last of -unresolved-symbols, -no-undefined or -z defs. |
| 466 | for (auto *Arg : llvm::reverse(Args)) { |
| 467 | switch (Arg->getOption().getID()) { |
| 468 | case OPT_unresolved_symbols: { |
| 469 | StringRef S = Arg->getValue(); |
| 470 | if (S == "ignore-all" || S == "ignore-in-object-files") |
| 471 | return UnresolvedPolicy::Ignore; |
| 472 | if (S == "ignore-in-shared-libs" || S == "report-all") |
| 473 | return ErrorOrWarn; |
| 474 | error("unknown --unresolved-symbols value: " + S); |
| 475 | continue; |
| 476 | } |
| 477 | case OPT_no_undefined: |
| 478 | return ErrorOrWarn; |
| 479 | case OPT_z: |
| 480 | if (StringRef(Arg->getValue()) == "defs") |
| 481 | return ErrorOrWarn; |
| 482 | continue; |
| 483 | } |
Davide Italiano | f8ff8fc | 2017-01-26 02:19:20 | [diff] [blame] | 484 | } |
| 485 | |
Rui Ueyama | dc0464c | 2017-01-29 01:59:11 | [diff] [blame] | 486 | // -shared implies -unresolved-symbols=ignore-all because missing |
| 487 | // symbols are likely to be resolved at runtime using other DSOs. |
| 488 | if (Config->Shared) |
| 489 | return UnresolvedPolicy::Ignore; |
| 490 | return ErrorOrWarn; |
George Rimar | e86dcd0 | 2016-06-29 12:35:04 | [diff] [blame] | 491 | } |
| 492 | |
Rui Ueyama | 10571cd | 2017-02-25 02:23:28 | [diff] [blame] | 493 | static Target2Policy getTarget2(opt::InputArgList &Args) { |
Yuka Takahashi | 1f1378d | 2017-06-14 08:01:26 | [diff] [blame] | 494 | StringRef S = Args.getLastArgValue(OPT_target2, "got-rel"); |
Rui Ueyama | 85a5e69 | 2017-04-29 22:56:38 | [diff] [blame] | 495 | if (S == "rel") |
| 496 | return Target2Policy::Rel; |
| 497 | if (S == "abs") |
| 498 | return Target2Policy::Abs; |
| 499 | if (S == "got-rel") |
| 500 | return Target2Policy::GotRel; |
| 501 | error("unknown --target2 option: " + S); |
Peter Smith | 9bbd4e2 | 2016-10-17 18:12:24 | [diff] [blame] | 502 | return Target2Policy::GotRel; |
| 503 | } |
| 504 | |
George Rimar | 86ce267 | 2016-08-25 09:05:47 | [diff] [blame] | 505 | static bool isOutputFormatBinary(opt::InputArgList &Args) { |
Rui Ueyama | 5530f23 | 2018-08-01 22:31:31 | [diff] [blame] | 506 | StringRef S = Args.getLastArgValue(OPT_oformat, "elf"); |
| 507 | if (S == "binary") |
| 508 | return true; |
| 509 | if (!S.startswith("elf")) |
George Rimar | 86ce267 | 2016-08-25 09:05:47 | [diff] [blame] | 510 | error("unknown --oformat value: " + S); |
George Rimar | 86ce267 | 2016-08-25 09:05:47 | [diff] [blame] | 511 | return false; |
| 512 | } |
| 513 | |
Rui Ueyama | 10571cd | 2017-02-25 02:23:28 | [diff] [blame] | 514 | static DiscardPolicy getDiscard(opt::InputArgList &Args) { |
Rui Ueyama | a4a643c | 2017-02-25 01:51:25 | [diff] [blame] | 515 | if (Args.hasArg(OPT_relocatable)) |
Rafael Espindola | 61d052d | 2016-12-04 08:34:17 | [diff] [blame] | 516 | return DiscardPolicy::None; |
Rui Ueyama | 524d44c | 2017-02-25 02:12:37 | [diff] [blame] | 517 | |
George Rimar | 9503f6d | 2016-08-31 08:46:30 | [diff] [blame] | 518 | auto *Arg = |
| 519 | Args.getLastArg(OPT_discard_all, OPT_discard_locals, OPT_discard_none); |
| 520 | if (!Arg) |
| 521 | return DiscardPolicy::Default; |
Rui Ueyama | 6af4016 | 2016-09-02 19:49:27 | [diff] [blame] | 522 | if (Arg->getOption().getID() == OPT_discard_all) |
George Rimar | 9503f6d | 2016-08-31 08:46:30 | [diff] [blame] | 523 | return DiscardPolicy::All; |
Rui Ueyama | 6af4016 | 2016-09-02 19:49:27 | [diff] [blame] | 524 | if (Arg->getOption().getID() == OPT_discard_locals) |
George Rimar | 9503f6d | 2016-08-31 08:46:30 | [diff] [blame] | 525 | return DiscardPolicy::Locals; |
Rui Ueyama | 6af4016 | 2016-09-02 19:49:27 | [diff] [blame] | 526 | return DiscardPolicy::None; |
George Rimar | 9503f6d | 2016-08-31 08:46:30 | [diff] [blame] | 527 | } |
| 528 | |
Rui Ueyama | 10571cd | 2017-02-25 02:23:28 | [diff] [blame] | 529 | static StringRef getDynamicLinker(opt::InputArgList &Args) { |
George Rimar | 87b0d68 | 2017-02-24 08:26:18 | [diff] [blame] | 530 | auto *Arg = Args.getLastArg(OPT_dynamic_linker, OPT_no_dynamic_linker); |
| 531 | if (!Arg || Arg->getOption().getID() == OPT_no_dynamic_linker) |
| 532 | return ""; |
| 533 | return Arg->getValue(); |
| 534 | } |
| 535 | |
Peter Collingbourne | a327a4c | 2018-07-18 22:49:31 | [diff] [blame] | 536 | static ICFLevel getICF(opt::InputArgList &Args) { |
| 537 | auto *Arg = Args.getLastArg(OPT_icf_none, OPT_icf_safe, OPT_icf_all); |
| 538 | if (!Arg || Arg->getOption().getID() == OPT_icf_none) |
| 539 | return ICFLevel::None; |
| 540 | if (Arg->getOption().getID() == OPT_icf_safe) |
| 541 | return ICFLevel::Safe; |
| 542 | return ICFLevel::All; |
| 543 | } |
| 544 | |
Rui Ueyama | 10571cd | 2017-02-25 02:23:28 | [diff] [blame] | 545 | static StripPolicy getStrip(opt::InputArgList &Args) { |
Rui Ueyama | 524d44c | 2017-02-25 02:12:37 | [diff] [blame] | 546 | if (Args.hasArg(OPT_relocatable)) |
| 547 | return StripPolicy::None; |
| 548 | |
| 549 | auto *Arg = Args.getLastArg(OPT_strip_all, OPT_strip_debug); |
| 550 | if (!Arg) |
| 551 | return StripPolicy::None; |
| 552 | if (Arg->getOption().getID() == OPT_strip_all) |
| 553 | return StripPolicy::All; |
| 554 | return StripPolicy::Debug; |
George Rimar | f21aade | 2016-08-31 08:38:11 | [diff] [blame] | 555 | } |
| 556 | |
Sam Clegg | 7e75663 | 2017-12-05 16:50:46 | [diff] [blame] | 557 | static uint64_t parseSectionAddress(StringRef S, const opt::Arg &Arg) { |
George Rimar | d73ef17 | 2016-09-14 13:07:13 | [diff] [blame] | 558 | uint64_t VA = 0; |
| 559 | if (S.startswith("0x")) |
| 560 | S = S.drop_front(2); |
George Rimar | ab94768 | 2017-05-16 08:19:25 | [diff] [blame] | 561 | if (!to_integer(S, VA, 16)) |
Rui Ueyama | b4c63ca | 2017-01-06 10:04:35 | [diff] [blame] | 562 | error("invalid argument: " + toString(Arg)); |
George Rimar | d73ef17 | 2016-09-14 13:07:13 | [diff] [blame] | 563 | return VA; |
| 564 | } |
| 565 | |
| 566 | static StringMap<uint64_t> getSectionStartMap(opt::InputArgList &Args) { |
| 567 | StringMap<uint64_t> Ret; |
| 568 | for (auto *Arg : Args.filtered(OPT_section_start)) { |
| 569 | StringRef Name; |
| 570 | StringRef Addr; |
| 571 | std::tie(Name, Addr) = StringRef(Arg->getValue()).split('='); |
Sam Clegg | 7e75663 | 2017-12-05 16:50:46 | [diff] [blame] | 572 | Ret[Name] = parseSectionAddress(Addr, *Arg); |
George Rimar | d73ef17 | 2016-09-14 13:07:13 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | if (auto *Arg = Args.getLastArg(OPT_Ttext)) |
Sam Clegg | 7e75663 | 2017-12-05 16:50:46 | [diff] [blame] | 576 | Ret[".text"] = parseSectionAddress(Arg->getValue(), *Arg); |
George Rimar | d73ef17 | 2016-09-14 13:07:13 | [diff] [blame] | 577 | if (auto *Arg = Args.getLastArg(OPT_Tdata)) |
Sam Clegg | 7e75663 | 2017-12-05 16:50:46 | [diff] [blame] | 578 | Ret[".data"] = parseSectionAddress(Arg->getValue(), *Arg); |
George Rimar | d73ef17 | 2016-09-14 13:07:13 | [diff] [blame] | 579 | if (auto *Arg = Args.getLastArg(OPT_Tbss)) |
Sam Clegg | 7e75663 | 2017-12-05 16:50:46 | [diff] [blame] | 580 | Ret[".bss"] = parseSectionAddress(Arg->getValue(), *Arg); |
George Rimar | d73ef17 | 2016-09-14 13:07:13 | [diff] [blame] | 581 | return Ret; |
| 582 | } |
| 583 | |
Rui Ueyama | 10571cd | 2017-02-25 02:23:28 | [diff] [blame] | 584 | static SortSectionPolicy getSortSection(opt::InputArgList &Args) { |
Yuka Takahashi | 1f1378d | 2017-06-14 08:01:26 | [diff] [blame] | 585 | StringRef S = Args.getLastArgValue(OPT_sort_section); |
George Rimar | be394db | 2016-09-16 20:21:55 | [diff] [blame] | 586 | if (S == "alignment") |
| 587 | return SortSectionPolicy::Alignment; |
| 588 | if (S == "name") |
| 589 | return SortSectionPolicy::Name; |
| 590 | if (!S.empty()) |
| 591 | error("unknown --sort-section rule: " + S); |
Rui Ueyama | b2a0abd | 2016-09-16 21:14:55 | [diff] [blame] | 592 | return SortSectionPolicy::Default; |
George Rimar | be394db | 2016-09-16 20:21:55 | [diff] [blame] | 593 | } |
| 594 | |
George Rimar | 9814d15 | 2017-10-25 15:20:30 | [diff] [blame] | 595 | static OrphanHandlingPolicy getOrphanHandling(opt::InputArgList &Args) { |
| 596 | StringRef S = Args.getLastArgValue(OPT_orphan_handling, "place"); |
| 597 | if (S == "warn") |
| 598 | return OrphanHandlingPolicy::Warn; |
| 599 | if (S == "error") |
| 600 | return OrphanHandlingPolicy::Error; |
| 601 | if (S != "place") |
| 602 | error("unknown --orphan-handling mode: " + S); |
| 603 | return OrphanHandlingPolicy::Place; |
| 604 | } |
| 605 | |
Rui Ueyama | 08af54c | 2017-04-26 21:23:11 | [diff] [blame] | 606 | // Parse --build-id or --build-id=<style>. We handle "tree" as a |
| 607 | // synonym for "sha1" because all our hash functions including |
| 608 | // -build-id=sha1 are actually tree hashes for performance reasons. |
| 609 | static std::pair<BuildIdKind, std::vector<uint8_t>> |
| 610 | getBuildId(opt::InputArgList &Args) { |
Peter Collingbourne | 968fe93 | 2017-05-23 21:16:48 | [diff] [blame] | 611 | auto *Arg = Args.getLastArg(OPT_build_id, OPT_build_id_eq); |
| 612 | if (!Arg) |
| 613 | return {BuildIdKind::None, {}}; |
| 614 | |
| 615 | if (Arg->getOption().getID() == OPT_build_id) |
Rui Ueyama | 08af54c | 2017-04-26 21:23:11 | [diff] [blame] | 616 | return {BuildIdKind::Fast, {}}; |
| 617 | |
Peter Collingbourne | 968fe93 | 2017-05-23 21:16:48 | [diff] [blame] | 618 | StringRef S = Arg->getValue(); |
Rui Ueyama | fa9f699 | 2018-02-07 19:22:42 | [diff] [blame] | 619 | if (S == "fast") |
| 620 | return {BuildIdKind::Fast, {}}; |
Rui Ueyama | 08af54c | 2017-04-26 21:23:11 | [diff] [blame] | 621 | if (S == "md5") |
| 622 | return {BuildIdKind::Md5, {}}; |
| 623 | if (S == "sha1" || S == "tree") |
| 624 | return {BuildIdKind::Sha1, {}}; |
| 625 | if (S == "uuid") |
| 626 | return {BuildIdKind::Uuid, {}}; |
| 627 | if (S.startswith("0x")) |
| 628 | return {BuildIdKind::Hexstring, parseHex(S.substr(2))}; |
| 629 | |
| 630 | if (S != "none") |
| 631 | error("unknown --build-id style: " + S); |
| 632 | return {BuildIdKind::None, {}}; |
| 633 | } |
| 634 | |
Rui Ueyama | 45192b3 | 2018-07-09 20:22:28 | [diff] [blame] | 635 | static std::pair<bool, bool> getPackDynRelocs(opt::InputArgList &Args) { |
| 636 | StringRef S = Args.getLastArgValue(OPT_pack_dyn_relocs, "none"); |
| 637 | if (S == "android") |
| 638 | return {true, false}; |
| 639 | if (S == "relr") |
| 640 | return {false, true}; |
| 641 | if (S == "android+relr") |
| 642 | return {true, true}; |
| 643 | |
| 644 | if (S != "none") |
| 645 | error("unknown -pack-dyn-relocs format: " + S); |
| 646 | return {false, false}; |
| 647 | } |
| 648 | |
Michael J. Spencer | b842725 | 2018-04-17 23:30:05 | [diff] [blame] | 649 | static void readCallGraph(MemoryBufferRef MB) { |
| 650 | // Build a map from symbol name to section |
Rui Ueyama | 2b39ea4 | 2018-10-26 15:07:02 | [diff] [blame] | 651 | DenseMap<StringRef, Symbol *> Map; |
Michael J. Spencer | b842725 | 2018-04-17 23:30:05 | [diff] [blame] | 652 | for (InputFile *File : ObjectFiles) |
| 653 | for (Symbol *Sym : File->getSymbols()) |
Rui Ueyama | 2b39ea4 | 2018-10-26 15:07:02 | [diff] [blame] | 654 | Map[Sym->getName()] = Sym; |
Michael J. Spencer | b842725 | 2018-04-17 23:30:05 | [diff] [blame] | 655 | |
Rui Ueyama | 2b39ea4 | 2018-10-26 15:07:02 | [diff] [blame] | 656 | auto FindSection = [&](StringRef Name) -> InputSectionBase * { |
| 657 | Symbol *Sym = Map.lookup(Name); |
| 658 | if (!Sym) { |
| 659 | if (Config->WarnSymbolOrdering) |
| 660 | warn(MB.getBufferIdentifier() + ": no such symbol: " + Name); |
| 661 | return nullptr; |
| 662 | } |
Rui Ueyama | 4c06a6c | 2018-10-26 15:07:12 | [diff] [blame] | 663 | maybeWarnUnorderableSymbol(Sym); |
George Rimar | 03b4d0c | 2018-08-04 07:31:19 | [diff] [blame] | 664 | |
Rui Ueyama | 2b39ea4 | 2018-10-26 15:07:02 | [diff] [blame] | 665 | if (Defined *DR = dyn_cast_or_null<Defined>(Sym)) |
George Rimar | 03b4d0c | 2018-08-04 07:31:19 | [diff] [blame] | 666 | return dyn_cast_or_null<InputSectionBase>(DR->Section); |
| 667 | return nullptr; |
| 668 | }; |
| 669 | |
Rui Ueyama | 2b39ea4 | 2018-10-26 15:07:02 | [diff] [blame] | 670 | for (StringRef Line : args::getLines(MB)) { |
Michael J. Spencer | b842725 | 2018-04-17 23:30:05 | [diff] [blame] | 671 | SmallVector<StringRef, 3> Fields; |
Rui Ueyama | 2b39ea4 | 2018-10-26 15:07:02 | [diff] [blame] | 672 | Line.split(Fields, ' '); |
Michael J. Spencer | b842725 | 2018-04-17 23:30:05 | [diff] [blame] | 673 | uint64_t Count; |
George Rimar | 03b4d0c | 2018-08-04 07:31:19 | [diff] [blame] | 674 | |
Rui Ueyama | 2b39ea4 | 2018-10-26 15:07:02 | [diff] [blame] | 675 | if (Fields.size() != 3 || !to_integer(Fields[2], Count)) { |
| 676 | error(MB.getBufferIdentifier() + ": parse error"); |
| 677 | return; |
| 678 | } |
| 679 | |
| 680 | if (InputSectionBase *From = FindSection(Fields[0])) |
| 681 | if (InputSectionBase *To = FindSection(Fields[1])) |
| 682 | Config->CallGraphProfile[std::make_pair(From, To)] += Count; |
Michael J. Spencer | b842725 | 2018-04-17 23:30:05 | [diff] [blame] | 683 | } |
| 684 | } |
| 685 | |
Michael J. Spencer | 8222f90 | 2018-10-02 00:17:15 | [diff] [blame] | 686 | template <class ELFT> static void readCallGraphsFromObjectFiles() { |
Michael J. Spencer | 8222f90 | 2018-10-02 00:17:15 | [diff] [blame] | 687 | for (auto File : ObjectFiles) { |
| 688 | auto *Obj = cast<ObjFile<ELFT>>(File); |
Rui Ueyama | 2b39ea4 | 2018-10-26 15:07:02 | [diff] [blame] | 689 | |
Michael J. Spencer | 8222f90 | 2018-10-02 00:17:15 | [diff] [blame] | 690 | for (const Elf_CGProfile_Impl<ELFT> &CGPE : Obj->CGProfile) { |
Fangrui Song | efc0fe5 | 2018-10-22 23:43:53 | [diff] [blame] | 691 | auto *FromSym = dyn_cast<Defined>(&Obj->getSymbol(CGPE.cgp_from)); |
| 692 | auto *ToSym = dyn_cast<Defined>(&Obj->getSymbol(CGPE.cgp_to)); |
| 693 | if (!FromSym || !ToSym) |
Michael J. Spencer | 8222f90 | 2018-10-02 00:17:15 | [diff] [blame] | 694 | continue; |
Rui Ueyama | 2b39ea4 | 2018-10-26 15:07:02 | [diff] [blame] | 695 | |
| 696 | auto *From = dyn_cast_or_null<InputSectionBase>(FromSym->Section); |
| 697 | auto *To = dyn_cast_or_null<InputSectionBase>(ToSym->Section); |
| 698 | if (From && To) |
| 699 | Config->CallGraphProfile[{From, To}] += CGPE.cgp_weight; |
Michael J. Spencer | 8222f90 | 2018-10-02 00:17:15 | [diff] [blame] | 700 | } |
| 701 | } |
| 702 | } |
| 703 | |
George Rimar | dbf9339 | 2017-04-17 08:58:12 | [diff] [blame] | 704 | static bool getCompressDebugSections(opt::InputArgList &Args) { |
Yuka Takahashi | 1f1378d | 2017-06-14 08:01:26 | [diff] [blame] | 705 | StringRef S = Args.getLastArgValue(OPT_compress_debug_sections, "none"); |
Rui Ueyama | 543161a | 2017-04-29 22:56:24 | [diff] [blame] | 706 | if (S == "none") |
| 707 | return false; |
| 708 | if (S != "zlib") |
| 709 | error("unknown --compress-debug-sections value: " + S); |
| 710 | if (!zlib::isAvailable()) |
| 711 | error("--compress-debug-sections: zlib is not available"); |
| 712 | return true; |
George Rimar | dbf9339 | 2017-04-17 08:58:12 | [diff] [blame] | 713 | } |
| 714 | |
Rui Ueyama | 0dd56dc | 2018-05-22 02:53:11 | [diff] [blame] | 715 | static std::pair<StringRef, StringRef> getOldNewOptions(opt::InputArgList &Args, |
| 716 | unsigned Id) { |
| 717 | auto *Arg = Args.getLastArg(Id); |
| 718 | if (!Arg) |
| 719 | return {"", ""}; |
| 720 | |
| 721 | StringRef S = Arg->getValue(); |
| 722 | std::pair<StringRef, StringRef> Ret = S.split(';'); |
| 723 | if (Ret.second.empty()) |
| 724 | error(Arg->getSpelling() + " expects 'old;new' format, but got " + S); |
| 725 | return Ret; |
George Rimar | cb38bf5 | 2017-08-14 10:17:30 | [diff] [blame] | 726 | } |
| 727 | |
James Henderson | de300e6 | 2018-02-14 13:36:22 | [diff] [blame] | 728 | // Parse the symbol ordering file and warn for any duplicate entries. |
| 729 | static std::vector<StringRef> getSymbolOrderingFile(MemoryBufferRef MB) { |
| 730 | SetVector<StringRef> Names; |
| 731 | for (StringRef S : args::getLines(MB)) |
| 732 | if (!Names.insert(S) && Config->WarnSymbolOrdering) |
| 733 | warn(MB.getBufferIdentifier() + ": duplicate ordered symbol: " + S); |
| 734 | |
| 735 | return Names.takeVector(); |
| 736 | } |
| 737 | |
Rui Ueyama | 5aab635 | 2018-03-30 17:22:44 | [diff] [blame] | 738 | static void parseClangOption(StringRef Opt, const Twine &Msg) { |
| 739 | std::string Err; |
| 740 | raw_string_ostream OS(Err); |
| 741 | |
| 742 | const char *Argv[] = {Config->ProgName.data(), Opt.data()}; |
| 743 | if (cl::ParseCommandLineOptions(2, Argv, "", &OS)) |
| 744 | return; |
| 745 | OS.flush(); |
| 746 | error(Msg + ": " + StringRef(Err).trim()); |
| 747 | } |
| 748 | |
Rui Ueyama | 0dd684c | 2016-01-07 17:54:19 | [diff] [blame] | 749 | // Initializes Config members by the command line options. |
| 750 | void LinkerDriver::readConfigs(opt::InputArgList &Args) { |
Rui Ueyama | 206dc22 | 2018-02-09 01:43:59 | [diff] [blame] | 751 | errorHandler().Verbose = Args.hasArg(OPT_verbose); |
Rui Ueyama | d42b1c0 | 2018-02-08 23:52:09 | [diff] [blame] | 752 | errorHandler().FatalWarnings = |
| 753 | Args.hasFlag(OPT_fatal_warnings, OPT_no_fatal_warnings, false); |
Rui Ueyama | f1894fe | 2018-05-22 16:19:38 | [diff] [blame] | 754 | ThreadsEnabled = Args.hasFlag(OPT_threads, OPT_no_threads, true); |
Rui Ueyama | d42b1c0 | 2018-02-08 23:52:09 | [diff] [blame] | 755 | |
Rui Ueyama | be7e001 | 2017-08-11 20:49:48 | [diff] [blame] | 756 | Config->AllowMultipleDefinition = |
Rui Ueyama | bb8d15e | 2018-02-06 00:45:15 | [diff] [blame] | 757 | Args.hasFlag(OPT_allow_multiple_definition, |
| 758 | OPT_no_allow_multiple_definition, false) || |
| 759 | hasZOption(Args, "muldefs"); |
Rui Ueyama | 3e03944 | 2017-11-28 19:58:45 | [diff] [blame] | 760 | Config->AuxiliaryList = args::getStrings(Args, OPT_auxiliary); |
Rui Ueyama | a4a643c | 2017-02-25 01:51:25 | [diff] [blame] | 761 | Config->Bsymbolic = Args.hasArg(OPT_Bsymbolic); |
| 762 | Config->BsymbolicFunctions = Args.hasArg(OPT_Bsymbolic_functions); |
Rui Ueyama | 6a8e79b | 2018-02-02 22:24:06 | [diff] [blame] | 763 | Config->CheckSections = |
| 764 | Args.hasFlag(OPT_check_sections, OPT_no_check_sections, true); |
Rui Ueyama | 875ae82 | 2017-07-20 18:17:55 | [diff] [blame] | 765 | Config->Chroot = Args.getLastArgValue(OPT_chroot); |
George Rimar | dbf9339 | 2017-04-17 08:58:12 | [diff] [blame] | 766 | Config->CompressDebugSections = getCompressDebugSections(Args); |
Rui Ueyama | db46a62 | 2018-03-14 20:29:45 | [diff] [blame] | 767 | Config->Cref = Args.hasFlag(OPT_cref, OPT_no_cref, false); |
Rui Ueyama | ac7aaeb | 2017-10-24 20:59:55 | [diff] [blame] | 768 | Config->DefineCommon = Args.hasFlag(OPT_define_common, OPT_no_define_common, |
| 769 | !Args.hasArg(OPT_relocatable)); |
| 770 | Config->Demangle = Args.hasFlag(OPT_demangle, OPT_no_demangle, true); |
Rui Ueyama | a4a643c | 2017-02-25 01:51:25 | [diff] [blame] | 771 | Config->DisableVerify = Args.hasArg(OPT_disable_verify); |
Rui Ueyama | 10571cd | 2017-02-25 02:23:28 | [diff] [blame] | 772 | Config->Discard = getDiscard(Args); |
Yunlian Jiang | 496fb3e | 2018-07-16 17:55:48 | [diff] [blame] | 773 | Config->DwoDir = Args.getLastArgValue(OPT_plugin_opt_dwo_dir_eq); |
Rui Ueyama | 10571cd | 2017-02-25 02:23:28 | [diff] [blame] | 774 | Config->DynamicLinker = getDynamicLinker(Args); |
Rui Ueyama | 38eab87 | 2017-08-29 16:53:24 | [diff] [blame] | 775 | Config->EhFrameHdr = |
Rui Ueyama | ac7aaeb | 2017-10-24 20:59:55 | [diff] [blame] | 776 | Args.hasFlag(OPT_eh_frame_hdr, OPT_no_eh_frame_hdr, false); |
Rui Ueyama | 9f49990 | 2018-12-14 21:58:49 | [diff] [blame] | 777 | Config->EmitLLVM = Args.hasArg(OPT_plugin_opt_emit_llvm, false); |
Rui Ueyama | a4a643c | 2017-02-25 01:51:25 | [diff] [blame] | 778 | Config->EmitRelocs = Args.hasArg(OPT_emit_relocs); |
Fangrui Song | cc18f8a | 2018-10-25 23:15:23 | [diff] [blame] | 779 | Config->CallGraphProfileSort = Args.hasFlag( |
| 780 | OPT_call_graph_profile_sort, OPT_no_call_graph_profile_sort, true); |
Rafael Espindola | 2e4bcb0 | 2018-03-01 22:23:51 | [diff] [blame] | 781 | Config->EnableNewDtags = |
| 782 | Args.hasFlag(OPT_enable_new_dtags, OPT_disable_new_dtags, true); |
Yuka Takahashi | 1f1378d | 2017-06-14 08:01:26 | [diff] [blame] | 783 | Config->Entry = Args.getLastArgValue(OPT_entry); |
David Bolvansky | a932cd4 | 2018-07-30 17:02:46 | [diff] [blame] | 784 | Config->ExecuteOnly = |
| 785 | Args.hasFlag(OPT_execute_only, OPT_no_execute_only, false); |
Rui Ueyama | a4a643c | 2017-02-25 01:51:25 | [diff] [blame] | 786 | Config->ExportDynamic = |
Rui Ueyama | ac7aaeb | 2017-10-24 20:59:55 | [diff] [blame] | 787 | Args.hasFlag(OPT_export_dynamic, OPT_no_export_dynamic, false); |
Rui Ueyama | 3e03944 | 2017-11-28 19:58:45 | [diff] [blame] | 788 | Config->FilterList = args::getStrings(Args, OPT_filter); |
Yuka Takahashi | 1f1378d | 2017-06-14 08:01:26 | [diff] [blame] | 789 | Config->Fini = Args.getLastArgValue(OPT_fini, "_fini"); |
Peter Smith | 732cd8c | 2017-12-05 15:59:05 | [diff] [blame] | 790 | Config->FixCortexA53Errata843419 = Args.hasArg(OPT_fix_cortex_a53_843419); |
Rui Ueyama | ac7aaeb | 2017-10-24 20:59:55 | [diff] [blame] | 791 | Config->GcSections = Args.hasFlag(OPT_gc_sections, OPT_no_gc_sections, false); |
Rui Ueyama | aad2e32 | 2018-02-02 21:44:06 | [diff] [blame] | 792 | Config->GnuUnique = Args.hasFlag(OPT_gnu_unique, OPT_no_gnu_unique, true); |
Rui Ueyama | ac7aaeb | 2017-10-24 20:59:55 | [diff] [blame] | 793 | Config->GdbIndex = Args.hasFlag(OPT_gdb_index, OPT_no_gdb_index, false); |
Peter Collingbourne | a327a4c | 2018-07-18 22:49:31 | [diff] [blame] | 794 | Config->ICF = getICF(Args); |
Rafael Espindola | b5506e6 | 2018-01-10 01:37:36 | [diff] [blame] | 795 | Config->IgnoreDataAddressEquality = |
| 796 | Args.hasArg(OPT_ignore_data_address_equality); |
| 797 | Config->IgnoreFunctionAddressEquality = |
| 798 | Args.hasArg(OPT_ignore_function_address_equality); |
Yuka Takahashi | 1f1378d | 2017-06-14 08:01:26 | [diff] [blame] | 799 | Config->Init = Args.getLastArgValue(OPT_init, "_init"); |
| 800 | Config->LTOAAPipeline = Args.getLastArgValue(OPT_lto_aa_pipeline); |
Rumeet Dhindsa | 682a417 | 2018-04-09 17:56:07 | [diff] [blame] | 801 | Config->LTODebugPassManager = Args.hasArg(OPT_lto_debug_pass_manager); |
| 802 | Config->LTONewPassManager = Args.hasArg(OPT_lto_new_pass_manager); |
Yuka Takahashi | 1f1378d | 2017-06-14 08:01:26 | [diff] [blame] | 803 | Config->LTONewPmPasses = Args.getLastArgValue(OPT_lto_newpm_passes); |
Rui Ueyama | 3e03944 | 2017-11-28 19:58:45 | [diff] [blame] | 804 | Config->LTOO = args::getInteger(Args, OPT_lto_O, 2); |
Rui Ueyama | 0dd56dc | 2018-05-22 02:53:11 | [diff] [blame] | 805 | Config->LTOObjPath = Args.getLastArgValue(OPT_plugin_opt_obj_path_eq); |
Rui Ueyama | 3e03944 | 2017-11-28 19:58:45 | [diff] [blame] | 806 | Config->LTOPartitions = args::getInteger(Args, OPT_lto_partitions, 1); |
Rumeet Dhindsa | 682a417 | 2018-04-09 17:56:07 | [diff] [blame] | 807 | Config->LTOSampleProfile = Args.getLastArgValue(OPT_lto_sample_profile); |
Yuka Takahashi | 1f1378d | 2017-06-14 08:01:26 | [diff] [blame] | 808 | Config->MapFile = Args.getLastArgValue(OPT_Map); |
Simon Atanasyan | ed9ee69 | 2018-06-11 07:24:31 | [diff] [blame] | 809 | Config->MipsGotSize = args::getInteger(Args, OPT_mips_got_size, 0xfff0); |
Peter Smith | 96ca4f5 | 2017-12-15 11:09:41 | [diff] [blame] | 810 | Config->MergeArmExidx = |
| 811 | Args.hasFlag(OPT_merge_exidx_entries, OPT_no_merge_exidx_entries, true); |
George Rimar | 84941ef | 2017-07-26 09:46:59 | [diff] [blame] | 812 | Config->NoinhibitExec = Args.hasArg(OPT_noinhibit_exec); |
Rui Ueyama | a4a643c | 2017-02-25 01:51:25 | [diff] [blame] | 813 | Config->Nostdlib = Args.hasArg(OPT_nostdlib); |
| 814 | Config->OFormatBinary = isOutputFormatBinary(Args); |
Rui Ueyama | 700b1f8 | 2017-11-01 02:04:43 | [diff] [blame] | 815 | Config->Omagic = Args.hasFlag(OPT_omagic, OPT_no_omagic, false); |
Yuka Takahashi | 1f1378d | 2017-06-14 08:01:26 | [diff] [blame] | 816 | Config->OptRemarksFilename = Args.getLastArgValue(OPT_opt_remarks_filename); |
Rui Ueyama | a4a643c | 2017-02-25 01:51:25 | [diff] [blame] | 817 | Config->OptRemarksWithHotness = Args.hasArg(OPT_opt_remarks_with_hotness); |
Rui Ueyama | 3e03944 | 2017-11-28 19:58:45 | [diff] [blame] | 818 | Config->Optimize = args::getInteger(Args, OPT_O, 1); |
George Rimar | 9814d15 | 2017-10-25 15:20:30 | [diff] [blame] | 819 | Config->OrphanHandling = getOrphanHandling(Args); |
Yuka Takahashi | 1f1378d | 2017-06-14 08:01:26 | [diff] [blame] | 820 | Config->OutputFile = Args.getLastArgValue(OPT_o); |
Rui Ueyama | 5d87b69 | 2018-02-02 00:31:05 | [diff] [blame] | 821 | Config->Pie = Args.hasFlag(OPT_pie, OPT_no_pie, false); |
James Henderson | 9c6e2fd | 2018-02-01 16:00:46 | [diff] [blame] | 822 | Config->PrintIcfSections = |
| 823 | Args.hasFlag(OPT_print_icf_sections, OPT_no_print_icf_sections, false); |
Rui Ueyama | 700b1f8 | 2017-11-01 02:04:43 | [diff] [blame] | 824 | Config->PrintGcSections = |
| 825 | Args.hasFlag(OPT_print_gc_sections, OPT_no_print_gc_sections, false); |
Rui Ueyama | bd27849 | 2017-04-29 23:06:43 | [diff] [blame] | 826 | Config->Rpath = getRpath(Args); |
Rui Ueyama | a4a643c | 2017-02-25 01:51:25 | [diff] [blame] | 827 | Config->Relocatable = Args.hasArg(OPT_relocatable); |
| 828 | Config->SaveTemps = Args.hasArg(OPT_save_temps); |
Rui Ueyama | 3e03944 | 2017-11-28 19:58:45 | [diff] [blame] | 829 | Config->SearchPaths = args::getStrings(Args, OPT_library_path); |
Rui Ueyama | a4a643c | 2017-02-25 01:51:25 | [diff] [blame] | 830 | Config->SectionStartMap = getSectionStartMap(Args); |
| 831 | Config->Shared = Args.hasArg(OPT_shared); |
| 832 | Config->SingleRoRx = Args.hasArg(OPT_no_rosegment); |
Yuka Takahashi | 1f1378d | 2017-06-14 08:01:26 | [diff] [blame] | 833 | Config->SoName = Args.getLastArgValue(OPT_soname); |
Rui Ueyama | 10571cd | 2017-02-25 02:23:28 | [diff] [blame] | 834 | Config->SortSection = getSortSection(Args); |
Sean Fertile | 4b5ec7f | 2018-10-16 17:13:01 | [diff] [blame] | 835 | Config->SplitStackAdjustSize = args::getInteger(Args, OPT_split_stack_adjust_size, 16384); |
Rui Ueyama | 10571cd | 2017-02-25 02:23:28 | [diff] [blame] | 836 | Config->Strip = getStrip(Args); |
Yuka Takahashi | 1f1378d | 2017-06-14 08:01:26 | [diff] [blame] | 837 | Config->Sysroot = Args.getLastArgValue(OPT_sysroot); |
Rui Ueyama | ac7aaeb | 2017-10-24 20:59:55 | [diff] [blame] | 838 | Config->Target1Rel = Args.hasFlag(OPT_target1_rel, OPT_target1_abs, false); |
Rui Ueyama | 10571cd | 2017-02-25 02:23:28 | [diff] [blame] | 839 | Config->Target2 = getTarget2(Args); |
Yuka Takahashi | 1f1378d | 2017-06-14 08:01:26 | [diff] [blame] | 840 | Config->ThinLTOCacheDir = Args.getLastArgValue(OPT_thinlto_cache_dir); |
Rui Ueyama | bdc5150 | 2017-12-06 22:08:17 | [diff] [blame] | 841 | Config->ThinLTOCachePolicy = CHECK( |
Yuka Takahashi | 1f1378d | 2017-06-14 08:01:26 | [diff] [blame] | 842 | parseCachePruningPolicy(Args.getLastArgValue(OPT_thinlto_cache_policy)), |
| 843 | "--thinlto-cache-policy: invalid cache policy"); |
Rui Ueyama | 0dd56dc | 2018-05-22 02:53:11 | [diff] [blame] | 844 | Config->ThinLTOEmitImportsFiles = |
| 845 | Args.hasArg(OPT_plugin_opt_thinlto_emit_imports_files); |
| 846 | Config->ThinLTOIndexOnly = Args.hasArg(OPT_plugin_opt_thinlto_index_only) || |
| 847 | Args.hasArg(OPT_plugin_opt_thinlto_index_only_eq); |
| 848 | Config->ThinLTOIndexOnlyArg = |
| 849 | Args.getLastArgValue(OPT_plugin_opt_thinlto_index_only_eq); |
Rui Ueyama | 3e03944 | 2017-11-28 19:58:45 | [diff] [blame] | 850 | Config->ThinLTOJobs = args::getInteger(Args, OPT_thinlto_jobs, -1u); |
Rui Ueyama | 47055bb | 2018-05-22 16:16:09 | [diff] [blame] | 851 | Config->ThinLTOObjectSuffixReplace = |
| 852 | getOldNewOptions(Args, OPT_plugin_opt_thinlto_object_suffix_replace_eq); |
| 853 | Config->ThinLTOPrefixReplace = |
| 854 | getOldNewOptions(Args, OPT_plugin_opt_thinlto_prefix_replace_eq); |
Rui Ueyama | a4a643c | 2017-02-25 01:51:25 | [diff] [blame] | 855 | Config->Trace = Args.hasArg(OPT_trace); |
Rui Ueyama | 3e03944 | 2017-11-28 19:58:45 | [diff] [blame] | 856 | Config->Undefined = args::getStrings(Args, OPT_undefined); |
Rui Ueyama | aad2e32 | 2018-02-02 21:44:06 | [diff] [blame] | 857 | Config->UndefinedVersion = |
| 858 | Args.hasFlag(OPT_undefined_version, OPT_no_undefined_version, true); |
Rui Ueyama | 11479da | 2018-07-09 20:08:55 | [diff] [blame] | 859 | Config->UseAndroidRelrTags = Args.hasFlag( |
| 860 | OPT_use_android_relr_tags, OPT_no_use_android_relr_tags, false); |
Rui Ueyama | a4a643c | 2017-02-25 01:51:25 | [diff] [blame] | 861 | Config->UnresolvedSymbols = getUnresolvedSymbolPolicy(Args); |
Rui Ueyama | 1d92aa7 | 2018-04-09 23:05:48 | [diff] [blame] | 862 | Config->WarnBackrefs = |
| 863 | Args.hasFlag(OPT_warn_backrefs, OPT_no_warn_backrefs, false); |
Rui Ueyama | bb8d15e | 2018-02-06 00:45:15 | [diff] [blame] | 864 | Config->WarnCommon = Args.hasFlag(OPT_warn_common, OPT_no_warn_common, false); |
Ali Tamur | 63830b2 | 2018-10-02 20:30:22 | [diff] [blame] | 865 | Config->WarnIfuncTextrel = |
| 866 | Args.hasFlag(OPT_warn_ifunc_textrel, OPT_no_warn_ifunc_textrel, false); |
James Henderson | de300e6 | 2018-02-14 13:36:22 | [diff] [blame] | 867 | Config->WarnSymbolOrdering = |
| 868 | Args.hasFlag(OPT_warn_symbol_ordering, OPT_no_warn_symbol_ordering, true); |
Rui Ueyama | 88fe5c9 | 2018-04-20 21:24:08 | [diff] [blame] | 869 | Config->ZCombreloc = getZFlag(Args, "combreloc", "nocombreloc", true); |
| 870 | Config->ZCopyreloc = getZFlag(Args, "copyreloc", "nocopyreloc", true); |
| 871 | Config->ZExecstack = getZFlag(Args, "execstack", "noexecstack", false); |
George Rimar | 27bbe7d | 2018-08-28 08:24:34 | [diff] [blame] | 872 | Config->ZGlobal = hasZOption(Args, "global"); |
Simon Dardis | cd87582 | 2018-02-20 23:49:17 | [diff] [blame] | 873 | Config->ZHazardplt = hasZOption(Args, "hazardplt"); |
Fangrui Song | bd3684f | 2018-06-20 02:06:01 | [diff] [blame] | 874 | Config->ZInitfirst = hasZOption(Args, "initfirst"); |
Ed Maste | c0b474f | 2018-09-14 14:25:37 | [diff] [blame] | 875 | Config->ZInterpose = hasZOption(Args, "interpose"); |
Sriraman Tallam | be01d2e | 2018-05-08 23:19:50 | [diff] [blame] | 876 | Config->ZKeepTextSectionPrefix = getZFlag( |
| 877 | Args, "keep-text-section-prefix", "nokeep-text-section-prefix", false); |
George Rimar | a1b3ddb | 2018-11-27 09:48:17 | [diff] [blame] | 878 | Config->ZNodefaultlib = hasZOption(Args, "nodefaultlib"); |
Rui Ueyama | a4a643c | 2017-02-25 01:51:25 | [diff] [blame] | 879 | Config->ZNodelete = hasZOption(Args, "nodelete"); |
Davide Italiano | 7690721 | 2017-03-23 00:54:16 | [diff] [blame] | 880 | Config->ZNodlopen = hasZOption(Args, "nodlopen"); |
Rui Ueyama | 88fe5c9 | 2018-04-20 21:24:08 | [diff] [blame] | 881 | Config->ZNow = getZFlag(Args, "now", "lazy", false); |
Rui Ueyama | a4a643c | 2017-02-25 01:51:25 | [diff] [blame] | 882 | Config->ZOrigin = hasZOption(Args, "origin"); |
Rui Ueyama | 88fe5c9 | 2018-04-20 21:24:08 | [diff] [blame] | 883 | Config->ZRelro = getZFlag(Args, "relro", "norelro", true); |
Chandler Carruth | c58f216 | 2018-01-22 22:05:25 | [diff] [blame] | 884 | Config->ZRetpolineplt = hasZOption(Args, "retpolineplt"); |
Petr Hosek | ffa786f | 2017-05-26 19:12:38 | [diff] [blame] | 885 | Config->ZRodynamic = hasZOption(Args, "rodynamic"); |
Rui Ueyama | 3e03944 | 2017-11-28 19:58:45 | [diff] [blame] | 886 | Config->ZStackSize = args::getZOptionValue(Args, OPT_z, "stack-size", 0); |
Rui Ueyama | 88fe5c9 | 2018-04-20 21:24:08 | [diff] [blame] | 887 | Config->ZText = getZFlag(Args, "text", "notext", true); |
Rui Ueyama | a4a643c | 2017-02-25 01:51:25 | [diff] [blame] | 888 | Config->ZWxneeded = hasZOption(Args, "wxneeded"); |
| 889 | |
Rui Ueyama | 47055bb | 2018-05-22 16:16:09 | [diff] [blame] | 890 | // Parse LTO options. |
Rui Ueyama | 0dd56dc | 2018-05-22 02:53:11 | [diff] [blame] | 891 | if (auto *Arg = Args.getLastArg(OPT_plugin_opt_mcpu_eq)) |
| 892 | parseClangOption(Saver.save("-mcpu=" + StringRef(Arg->getValue())), |
| 893 | Arg->getSpelling()); |
| 894 | |
| 895 | for (auto *Arg : Args.filtered(OPT_plugin_opt)) |
| 896 | parseClangOption(Arg->getValue(), Arg->getSpelling()); |
Rui Ueyama | 5aab635 | 2018-03-30 17:22:44 | [diff] [blame] | 897 | |
| 898 | // Parse -mllvm options. |
George Rimar | 9dc740d | 2018-02-06 12:20:05 | [diff] [blame] | 899 | for (auto *Arg : Args.filtered(OPT_mllvm)) |
Rui Ueyama | 5aab635 | 2018-03-30 17:22:44 | [diff] [blame] | 900 | parseClangOption(Arg->getValue(), Arg->getSpelling()); |
Rui Ueyama | a138972 | 2017-08-14 17:48:30 | [diff] [blame] | 901 | |
Rui Ueyama | a4a643c | 2017-02-25 01:51:25 | [diff] [blame] | 902 | if (Config->LTOO > 3) |
George Rimar | cb38bf5 | 2017-08-14 10:17:30 | [diff] [blame] | 903 | error("invalid optimization level for LTO: " + Twine(Config->LTOO)); |
Rui Ueyama | a4a643c | 2017-02-25 01:51:25 | [diff] [blame] | 904 | if (Config->LTOPartitions == 0) |
| 905 | error("--lto-partitions: number of threads must be > 0"); |
| 906 | if (Config->ThinLTOJobs == 0) |
| 907 | error("--thinlto-jobs: number of threads must be > 0"); |
| 908 | |
Sean Fertile | 4b5ec7f | 2018-10-16 17:13:01 | [diff] [blame] | 909 | if (Config->SplitStackAdjustSize < 0) |
| 910 | error("--split-stack-adjust-size: size must be >= 0"); |
| 911 | |
Rui Ueyama | a138972 | 2017-08-14 17:48:30 | [diff] [blame] | 912 | // Parse ELF{32,64}{LE,BE} and CPU type. |
Rui Ueyama | 9aa5686 | 2015-11-24 18:55:36 | [diff] [blame] | 913 | if (auto *Arg = Args.getLastArg(OPT_m)) { |
| 914 | StringRef S = Arg->getValue(); |
Rui Ueyama | 58026af | 2016-11-09 22:32:43 | [diff] [blame] | 915 | std::tie(Config->EKind, Config->EMachine, Config->OSABI) = |
| 916 | parseEmulation(S); |
| 917 | Config->MipsN32Abi = (S == "elf32btsmipn32" || S == "elf32ltsmipn32"); |
Rui Ueyama | 9aa5686 | 2015-11-24 18:55:36 | [diff] [blame] | 918 | Config->Emulation = S; |
| 919 | } |
| 920 | |
George Rimar | d46753e | 2017-10-06 09:37:44 | [diff] [blame] | 921 | // Parse -hash-style={sysv,gnu,both}. |
| 922 | if (auto *Arg = Args.getLastArg(OPT_hash_style)) { |
| 923 | StringRef S = Arg->getValue(); |
| 924 | if (S == "sysv") |
| 925 | Config->SysvHash = true; |
| 926 | else if (S == "gnu") |
| 927 | Config->GnuHash = true; |
| 928 | else if (S == "both") |
| 929 | Config->SysvHash = Config->GnuHash = true; |
| 930 | else |
| 931 | error("unknown -hash-style: " + S); |
| 932 | } |
| 933 | |
Rui Ueyama | 1705f99 | 2017-01-15 02:52:34 | [diff] [blame] | 934 | if (Args.hasArg(OPT_print_map)) |
| 935 | Config->MapFile = "-"; |
| 936 | |
George Rimar | d250618 | 2016-12-03 07:09:28 | [diff] [blame] | 937 | // --omagic is an option to create old-fashioned executables in which |
| 938 | // .text segments are writable. Today, the option is still in use to |
| 939 | // create special-purpose programs such as boot loaders. It doesn't |
| 940 | // make sense to create PT_GNU_RELRO for such executables. |
Rui Ueyama | a37ace8d | 2017-02-25 01:52:03 | [diff] [blame] | 941 | if (Config->Omagic) |
George Rimar | d250618 | 2016-12-03 07:09:28 | [diff] [blame] | 942 | Config->ZRelro = false; |
| 943 | |
Rui Ueyama | 08af54c | 2017-04-26 21:23:11 | [diff] [blame] | 944 | std::tie(Config->BuildId, Config->BuildIdVector) = getBuildId(Args); |
Rui Ueyama | 3a41be2 | 2016-04-07 22:49:21 | [diff] [blame] | 945 | |
Rui Ueyama | 45192b3 | 2018-07-09 20:22:28 | [diff] [blame] | 946 | std::tie(Config->AndroidPackDynRelocs, Config->RelrPackDynRelocs) = |
| 947 | getPackDynRelocs(Args); |
Peter Collingbourne | 5c54f15 | 2017-10-27 17:49:40 | [diff] [blame] | 948 | |
George Rimar | 1a33c0f | 2016-11-10 09:05:20 | [diff] [blame] | 949 | if (auto *Arg = Args.getLastArg(OPT_symbol_ordering_file)) |
| 950 | if (Optional<MemoryBufferRef> Buffer = readFile(Arg->getValue())) |
James Henderson | de300e6 | 2018-02-14 13:36:22 | [diff] [blame] | 951 | Config->SymbolOrderingFile = getSymbolOrderingFile(*Buffer); |
George Rimar | 1a33c0f | 2016-11-10 09:05:20 | [diff] [blame] | 952 | |
Rui Ueyama | bfe0264 | 2017-01-25 21:49:23 | [diff] [blame] | 953 | // If --retain-symbol-file is used, we'll keep only the symbols listed in |
George Rimar | 2bb88ab | 2016-12-19 18:00:52 | [diff] [blame] | 954 | // the file and discard all others. |
| 955 | if (auto *Arg = Args.getLastArg(OPT_retain_symbols_file)) { |
Rafael Espindola | c0fc253 | 2017-01-25 21:23:06 | [diff] [blame] | 956 | Config->DefaultSymbolVersion = VER_NDX_LOCAL; |
George Rimar | 2bb88ab | 2016-12-19 18:00:52 | [diff] [blame] | 957 | if (Optional<MemoryBufferRef> Buffer = readFile(Arg->getValue())) |
Rui Ueyama | 3e03944 | 2017-11-28 19:58:45 | [diff] [blame] | 958 | for (StringRef S : args::getLines(*Buffer)) |
Rafael Espindola | c0fc253 | 2017-01-25 21:23:06 | [diff] [blame] | 959 | Config->VersionScriptGlobals.push_back( |
| 960 | {S, /*IsExternCpp*/ false, /*HasWildcard*/ false}); |
George Rimar | 2bb88ab | 2016-12-19 18:00:52 | [diff] [blame] | 961 | } |
| 962 | |
Rui Ueyama | 8d817f2 | 2017-04-25 17:40:12 | [diff] [blame] | 963 | bool HasExportDynamic = |
Rui Ueyama | ac7aaeb | 2017-10-24 20:59:55 | [diff] [blame] | 964 | Args.hasFlag(OPT_export_dynamic, OPT_no_export_dynamic, false); |
Rafael Espindola | d0ebd84 | 2016-12-08 17:54:26 | [diff] [blame] | 965 | |
Rui Ueyama | 8d817f2 | 2017-04-25 17:40:12 | [diff] [blame] | 966 | // Parses -dynamic-list and -export-dynamic-symbol. They make some |
| 967 | // symbols private. Note that -export-dynamic takes precedence over them |
| 968 | // as it says all symbols should be exported. |
| 969 | if (!HasExportDynamic) { |
| 970 | for (auto *Arg : Args.filtered(OPT_dynamic_list)) |
| 971 | if (Optional<MemoryBufferRef> Buffer = readFile(Arg->getValue())) |
| 972 | readDynamicList(*Buffer); |
| 973 | |
Rui Ueyama | 5e52022 | 2018-02-14 18:38:33 | [diff] [blame] | 974 | for (auto *Arg : Args.filtered(OPT_export_dynamic_symbol)) |
Peter Collingbourne | d6924d7 | 2017-10-06 22:09:03 | [diff] [blame] | 975 | Config->DynamicList.push_back( |
Rui Ueyama | 8d817f2 | 2017-04-25 17:40:12 | [diff] [blame] | 976 | {Arg->getValue(), /*IsExternCpp*/ false, /*HasWildcard*/ false}); |
Rafael Espindola | d0ebd84 | 2016-12-08 17:54:26 | [diff] [blame] | 977 | } |
Peter Collingbourne | 66ac1d6 | 2016-04-22 20:21:26 | [diff] [blame] | 978 | |
Rui Ueyama | 5e52022 | 2018-02-14 18:38:33 | [diff] [blame] | 979 | // If --export-dynamic-symbol=foo is given and symbol foo is defined in |
| 980 | // an object file in an archive file, that object file should be pulled |
| 981 | // out and linked. (It doesn't have to behave like that from technical |
| 982 | // point of view, but this is needed for compatibility with GNU.) |
| 983 | for (auto *Arg : Args.filtered(OPT_export_dynamic_symbol)) |
| 984 | Config->Undefined.push_back(Arg->getValue()); |
| 985 | |
Igor Kudrin | 892b146 | 2017-12-07 03:25:39 | [diff] [blame] | 986 | for (auto *Arg : Args.filtered(OPT_version_script)) |
Fangrui Song | c60f85d | 2018-07-25 21:53:18 | [diff] [blame] | 987 | if (Optional<std::string> Path = searchScript(Arg->getValue())) { |
| 988 | if (Optional<MemoryBufferRef> Buffer = readFile(*Path)) |
| 989 | readVersionScript(*Buffer); |
| 990 | } else { |
| 991 | error(Twine("cannot find version script ") + Arg->getValue()); |
| 992 | } |
Rui Ueyama | 0dd684c | 2016-01-07 17:54:19 | [diff] [blame] | 993 | } |
George Rimar | 83f406c | 2015-10-19 17:35:12 | [diff] [blame] | 994 | |
Rui Ueyama | d57e74b7 | 2017-03-17 23:29:01 | [diff] [blame] | 995 | // Some Config members do not directly correspond to any particular |
| 996 | // command line options, but computed based on other Config values. |
| 997 | // This function initialize such members. See Config.h for the details |
| 998 | // of these values. |
Rafael Espindola | 7a7a81d | 2018-02-05 20:55:46 | [diff] [blame] | 999 | static void setConfigs(opt::InputArgList &Args) { |
Rui Ueyama | d19bc04 | 2018-09-20 21:29:14 | [diff] [blame] | 1000 | ELFKind K = Config->EKind; |
| 1001 | uint16_t M = Config->EMachine; |
Rui Ueyama | d57e74b7 | 2017-03-17 23:29:01 | [diff] [blame] | 1002 | |
Rui Ueyama | d57e74b7 | 2017-03-17 23:29:01 | [diff] [blame] | 1003 | Config->CopyRelocs = (Config->Relocatable || Config->EmitRelocs); |
Rui Ueyama | d19bc04 | 2018-09-20 21:29:14 | [diff] [blame] | 1004 | Config->Is64 = (K == ELF64LEKind || K == ELF64BEKind); |
| 1005 | Config->IsLE = (K == ELF32LEKind || K == ELF64LEKind); |
| 1006 | Config->Endianness = Config->IsLE ? endianness::little : endianness::big; |
| 1007 | Config->IsMips64EL = (K == ELF64LEKind && M == EM_MIPS); |
Rui Ueyama | d57e74b7 | 2017-03-17 23:29:01 | [diff] [blame] | 1008 | Config->Pic = Config->Pie || Config->Shared; |
Rui Ueyama | 7ab38c3 | 2017-03-22 00:01:11 | [diff] [blame] | 1009 | Config->Wordsize = Config->Is64 ? 8 : 4; |
Rui Ueyama | e53890f | 2018-06-08 00:18:32 | [diff] [blame] | 1010 | |
Rui Ueyama | e53890f | 2018-06-08 00:18:32 | [diff] [blame] | 1011 | // ELF defines two different ways to store relocation addends as shown below: |
| 1012 | // |
| 1013 | // Rel: Addends are stored to the location where relocations are applied. |
| 1014 | // Rela: Addends are stored as part of relocation entry. |
| 1015 | // |
| 1016 | // In other words, Rela makes it easy to read addends at the price of extra |
| 1017 | // 4 or 8 byte for each relocation entry. We don't know why ELF defined two |
| 1018 | // different mechanisms in the first place, but this is how the spec is |
| 1019 | // defined. |
| 1020 | // |
| 1021 | // You cannot choose which one, Rel or Rela, you want to use. Instead each |
| 1022 | // ABI defines which one you need to use. The following expression expresses |
| 1023 | // that. |
Sid Manning | fc50e63 | 2018-09-28 14:09:16 | [diff] [blame] | 1024 | Config->IsRela = M == EM_AARCH64 || M == EM_AMDGPU || M == EM_HEXAGON || |
| 1025 | M == EM_PPC || M == EM_PPC64 || M == EM_RISCV || |
| 1026 | M == EM_X86_64; |
Rui Ueyama | e53890f | 2018-06-08 00:18:32 | [diff] [blame] | 1027 | |
Alexander Richardson | cfb6093 | 2018-02-16 10:01:17 | [diff] [blame] | 1028 | // If the output uses REL relocations we must store the dynamic relocation |
| 1029 | // addends to the output sections. We also store addends for RELA relocations |
| 1030 | // if --apply-dynamic-relocs is used. |
| 1031 | // We default to not writing the addends when using RELA relocations since |
| 1032 | // any standard conforming tool can find it in r_addend. |
Rafael Espindola | 7a7a81d | 2018-02-05 20:55:46 | [diff] [blame] | 1033 | Config->WriteAddends = Args.hasFlag(OPT_apply_dynamic_relocs, |
| 1034 | OPT_no_apply_dynamic_relocs, false) || |
| 1035 | !Config->IsRela; |
Sean Fertile | 7f3f05e | 2018-09-20 00:26:44 | [diff] [blame] | 1036 | |
| 1037 | Config->TocOptimize = |
Rui Ueyama | d19bc04 | 2018-09-20 21:29:14 | [diff] [blame] | 1038 | Args.hasFlag(OPT_toc_optimize, OPT_no_toc_optimize, M == EM_PPC64); |
Rui Ueyama | d57e74b7 | 2017-03-17 23:29:01 | [diff] [blame] | 1039 | } |
| 1040 | |
Rui Ueyama | 0aeb119 | 2016-10-20 04:36:36 | [diff] [blame] | 1041 | // Returns a value of "-format" option. |
Rui Ueyama | e262bb1 | 2018-08-06 21:29:41 | [diff] [blame] | 1042 | static bool isFormatBinary(StringRef S) { |
Rui Ueyama | 0aeb119 | 2016-10-20 04:36:36 | [diff] [blame] | 1043 | if (S == "binary") |
| 1044 | return true; |
| 1045 | if (S == "elf" || S == "default") |
| 1046 | return false; |
Rui Ueyama | d7c4454 | 2016-10-20 04:47:45 | [diff] [blame] | 1047 | error("unknown -format value: " + S + |
Rui Ueyama | 0aeb119 | 2016-10-20 04:36:36 | [diff] [blame] | 1048 | " (supported formats: elf, default, binary)"); |
| 1049 | return false; |
| 1050 | } |
| 1051 | |
Rui Ueyama | 0dd684c | 2016-01-07 17:54:19 | [diff] [blame] | 1052 | void LinkerDriver::createFiles(opt::InputArgList &Args) { |
Rui Ueyama | f75ea0b | 2018-05-31 13:00:25 | [diff] [blame] | 1053 | // For --{push,pop}-state. |
| 1054 | std::vector<std::tuple<bool, bool, bool>> Stack; |
| 1055 | |
| 1056 | // Iterate over argv to process input files and positional arguments. |
Igor Kudrin | d912ee9 | 2015-10-01 16:42:03 | [diff] [blame] | 1057 | for (auto *Arg : Args) { |
George Rimar | 4f98e0b | 2018-04-04 08:13:28 | [diff] [blame] | 1058 | switch (Arg->getOption().getUnaliasedOption().getID()) { |
George Rimar | 4f37d57 | 2017-07-21 16:27:26 | [diff] [blame] | 1059 | case OPT_library: |
Rui Ueyama | 21eecb4 | 2016-02-02 21:13:09 | [diff] [blame] | 1060 | addLibrary(Arg->getValue()); |
Igor Kudrin | d912ee9 | 2015-10-01 16:42:03 | [diff] [blame] | 1061 | break; |
| 1062 | case OPT_INPUT: |
Evgeniy Stepanov | a76349b | 2017-04-12 00:13:48 | [diff] [blame] | 1063 | addFile(Arg->getValue(), /*WithLOption=*/false); |
Igor Kudrin | d912ee9 | 2015-10-01 16:42:03 | [diff] [blame] | 1064 | break; |
George Rimar | 0b89c55 | 2018-01-17 10:24:49 | [diff] [blame] | 1065 | case OPT_defsym: { |
| 1066 | StringRef From; |
| 1067 | StringRef To; |
| 1068 | std::tie(From, To) = StringRef(Arg->getValue()).split('='); |
George Rimar | 00d6f4b | 2018-08-10 06:32:39 | [diff] [blame] | 1069 | if (From.empty() || To.empty()) |
| 1070 | error("-defsym: syntax error: " + StringRef(Arg->getValue())); |
| 1071 | else |
| 1072 | readDefsym(From, MemoryBufferRef(To, "-defsym")); |
George Rimar | 0b89c55 | 2018-01-17 10:24:49 | [diff] [blame] | 1073 | break; |
| 1074 | } |
Michael J. Spencer | a9424f3 | 2016-09-09 22:08:04 | [diff] [blame] | 1075 | case OPT_script: |
Fangrui Song | c60f85d | 2018-07-25 21:53:18 | [diff] [blame] | 1076 | if (Optional<std::string> Path = searchScript(Arg->getValue())) { |
Alexander Richardson | 1de7847 | 2017-11-20 15:43:20 | [diff] [blame] | 1077 | if (Optional<MemoryBufferRef> MB = readFile(*Path)) |
| 1078 | readLinkerScript(*MB); |
| 1079 | break; |
| 1080 | } |
| 1081 | error(Twine("cannot find linker script ") + Arg->getValue()); |
Michael J. Spencer | a9424f3 | 2016-09-09 22:08:04 | [diff] [blame] | 1082 | break; |
Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 | [diff] [blame] | 1083 | case OPT_as_needed: |
George Rimar | 4f98e0b | 2018-04-04 08:13:28 | [diff] [blame] | 1084 | Config->AsNeeded = true; |
Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 | [diff] [blame] | 1085 | break; |
Rui Ueyama | 0aeb119 | 2016-10-20 04:36:36 | [diff] [blame] | 1086 | case OPT_format: |
Rui Ueyama | e262bb1 | 2018-08-06 21:29:41 | [diff] [blame] | 1087 | Config->FormatBinary = isFormatBinary(Arg->getValue()); |
Michael J. Spencer | a9424f3 | 2016-09-09 22:08:04 | [diff] [blame] | 1088 | break; |
George Rimar | 4f98e0b | 2018-04-04 08:13:28 | [diff] [blame] | 1089 | case OPT_no_as_needed: |
| 1090 | Config->AsNeeded = false; |
| 1091 | break; |
Igor Kudrin | d912ee9 | 2015-10-01 16:42:03 | [diff] [blame] | 1092 | case OPT_Bstatic: |
George Rimar | 4f98e0b | 2018-04-04 08:13:28 | [diff] [blame] | 1093 | Config->Static = true; |
| 1094 | break; |
Igor Kudrin | d912ee9 | 2015-10-01 16:42:03 | [diff] [blame] | 1095 | case OPT_Bdynamic: |
George Rimar | 4f98e0b | 2018-04-04 08:13:28 | [diff] [blame] | 1096 | Config->Static = false; |
Igor Kudrin | d912ee9 | 2015-10-01 16:42:03 | [diff] [blame] | 1097 | break; |
Igor Kudrin | 2696bbe | 2015-10-01 18:02:21 | [diff] [blame] | 1098 | case OPT_whole_archive: |
George Rimar | 4f98e0b | 2018-04-04 08:13:28 | [diff] [blame] | 1099 | InWholeArchive = true; |
| 1100 | break; |
Igor Kudrin | 2696bbe | 2015-10-01 18:02:21 | [diff] [blame] | 1101 | case OPT_no_whole_archive: |
George Rimar | 4f98e0b | 2018-04-04 08:13:28 | [diff] [blame] | 1102 | InWholeArchive = false; |
Igor Kudrin | 2696bbe | 2015-10-01 18:02:21 | [diff] [blame] | 1103 | break; |
Rui Ueyama | 5a67a6e | 2018-03-30 01:15:36 | [diff] [blame] | 1104 | case OPT_just_symbols: |
| 1105 | if (Optional<MemoryBufferRef> MB = readFile(Arg->getValue())) { |
| 1106 | Files.push_back(createObjectFile(*MB)); |
| 1107 | Files.back()->JustSymbols = true; |
| 1108 | } |
| 1109 | break; |
Rui Ueyama | 1d92aa7 | 2018-04-09 23:05:48 | [diff] [blame] | 1110 | case OPT_start_group: |
| 1111 | if (InputFile::IsInGroup) |
| 1112 | error("nested --start-group"); |
| 1113 | InputFile::IsInGroup = true; |
| 1114 | break; |
| 1115 | case OPT_end_group: |
| 1116 | if (!InputFile::IsInGroup) |
| 1117 | error("stray --end-group"); |
| 1118 | InputFile::IsInGroup = false; |
Fangrui Song | b72daf0 | 2018-04-19 23:23:23 | [diff] [blame] | 1119 | ++InputFile::NextGroupId; |
Rui Ueyama | 1d92aa7 | 2018-04-09 23:05:48 | [diff] [blame] | 1120 | break; |
Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 | [diff] [blame] | 1121 | case OPT_start_lib: |
Fangrui Song | 2416d7f | 2018-04-20 16:33:01 | [diff] [blame] | 1122 | if (InLib) |
| 1123 | error("nested --start-lib"); |
| 1124 | if (InputFile::IsInGroup) |
| 1125 | error("may not nest --start-lib in --start-group"); |
George Rimar | 4f98e0b | 2018-04-04 08:13:28 | [diff] [blame] | 1126 | InLib = true; |
Fangrui Song | 2416d7f | 2018-04-20 16:33:01 | [diff] [blame] | 1127 | InputFile::IsInGroup = true; |
George Rimar | 4f98e0b | 2018-04-04 08:13:28 | [diff] [blame] | 1128 | break; |
Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 | [diff] [blame] | 1129 | case OPT_end_lib: |
Fangrui Song | 2416d7f | 2018-04-20 16:33:01 | [diff] [blame] | 1130 | if (!InLib) |
| 1131 | error("stray --end-lib"); |
George Rimar | 4f98e0b | 2018-04-04 08:13:28 | [diff] [blame] | 1132 | InLib = false; |
Fangrui Song | 2416d7f | 2018-04-20 16:33:01 | [diff] [blame] | 1133 | InputFile::IsInGroup = false; |
| 1134 | ++InputFile::NextGroupId; |
Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 | [diff] [blame] | 1135 | break; |
Rui Ueyama | f75ea0b | 2018-05-31 13:00:25 | [diff] [blame] | 1136 | case OPT_push_state: |
Rui Ueyama | e37a5ce | 2018-05-31 13:24:01 | [diff] [blame] | 1137 | Stack.emplace_back(Config->AsNeeded, Config->Static, InWholeArchive); |
Rui Ueyama | f75ea0b | 2018-05-31 13:00:25 | [diff] [blame] | 1138 | break; |
| 1139 | case OPT_pop_state: |
| 1140 | if (Stack.empty()) { |
| 1141 | error("unbalanced --push-state/--pop-state"); |
| 1142 | break; |
| 1143 | } |
| 1144 | std::tie(Config->AsNeeded, Config->Static, InWholeArchive) = Stack.back(); |
| 1145 | Stack.pop_back(); |
| 1146 | break; |
Rui Ueyama | f5c4aca | 2015-09-30 17:06:09 | [diff] [blame] | 1147 | } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 | [diff] [blame] | 1148 | } |
| 1149 | |
Bob Haarman | b8a59c8 | 2017-10-25 22:28:38 | [diff] [blame] | 1150 | if (Files.empty() && errorCount() == 0) |
Rui Ueyama | 8088ebe | 2016-10-19 18:09:52 | [diff] [blame] | 1151 | error("no input files"); |
Rui Ueyama | c185c01 | 2016-10-20 04:47:47 | [diff] [blame] | 1152 | } |
Rui Ueyama | 5e64d3f | 2016-06-29 01:30:50 | [diff] [blame] | 1153 | |
Rui Ueyama | c185c01 | 2016-10-20 04:47:47 | [diff] [blame] | 1154 | // If -m <machine_type> was not given, infer it from object files. |
| 1155 | void LinkerDriver::inferMachineType() { |
| 1156 | if (Config->EKind != ELFNoneKind) |
| 1157 | return; |
| 1158 | |
| 1159 | for (InputFile *F : Files) { |
| 1160 | if (F->EKind == ELFNoneKind) |
| 1161 | continue; |
| 1162 | Config->EKind = F->EKind; |
| 1163 | Config->EMachine = F->EMachine; |
Rafael Espindola | 7cc713a | 2016-10-27 14:00:51 | [diff] [blame] | 1164 | Config->OSABI = F->OSABI; |
Simon Atanasyan | 9e0297b | 2016-11-05 22:58:01 | [diff] [blame] | 1165 | Config->MipsN32Abi = Config->EMachine == EM_MIPS && isMipsN32Abi(F); |
Rui Ueyama | c185c01 | 2016-10-20 04:47:47 | [diff] [blame] | 1166 | return; |
Rui Ueyama | 5e64d3f | 2016-06-29 01:30:50 | [diff] [blame] | 1167 | } |
Rui Ueyama | c185c01 | 2016-10-20 04:47:47 | [diff] [blame] | 1168 | error("target emulation unknown: -m or at least one .o file required"); |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 | [diff] [blame] | 1169 | } |
| 1170 | |
Rui Ueyama | e4eadb6 | 2016-12-08 17:44:37 | [diff] [blame] | 1171 | // Parse -z max-page-size=<value>. The default value is defined by |
| 1172 | // each target. |
| 1173 | static uint64_t getMaxPageSize(opt::InputArgList &Args) { |
Rui Ueyama | 3e03944 | 2017-11-28 19:58:45 | [diff] [blame] | 1174 | uint64_t Val = args::getZOptionValue(Args, OPT_z, "max-page-size", |
| 1175 | Target->DefaultMaxPageSize); |
Rui Ueyama | e4eadb6 | 2016-12-08 17:44:37 | [diff] [blame] | 1176 | if (!isPowerOf2_64(Val)) |
| 1177 | error("max-page-size: value isn't a power of 2"); |
| 1178 | return Val; |
| 1179 | } |
| 1180 | |
Rui Ueyama | c5dd543 | 2016-10-26 04:34:16 | [diff] [blame] | 1181 | // Parses -image-base option. |
James Henderson | b5ca92e | 2017-10-10 10:09:35 | [diff] [blame] | 1182 | static Optional<uint64_t> getImageBase(opt::InputArgList &Args) { |
| 1183 | // Because we are using "Config->MaxPageSize" here, this function has to be |
| 1184 | // called after the variable is initialized. |
Rui Ueyama | c5dd543 | 2016-10-26 04:34:16 | [diff] [blame] | 1185 | auto *Arg = Args.getLastArg(OPT_image_base); |
| 1186 | if (!Arg) |
James Henderson | b5ca92e | 2017-10-10 10:09:35 | [diff] [blame] | 1187 | return None; |
Rui Ueyama | c5dd543 | 2016-10-26 04:34:16 | [diff] [blame] | 1188 | |
| 1189 | StringRef S = Arg->getValue(); |
| 1190 | uint64_t V; |
George Rimar | ab94768 | 2017-05-16 08:19:25 | [diff] [blame] | 1191 | if (!to_integer(S, V)) { |
Rui Ueyama | c5dd543 | 2016-10-26 04:34:16 | [diff] [blame] | 1192 | error("-image-base: number expected, but got " + S); |
| 1193 | return 0; |
| 1194 | } |
Rafael Espindola | 476d207 | 2016-12-07 20:29:46 | [diff] [blame] | 1195 | if ((V % Config->MaxPageSize) != 0) |
Rui Ueyama | c5dd543 | 2016-10-26 04:34:16 | [diff] [blame] | 1196 | warn("-image-base: address isn't multiple of page size: " + S); |
| 1197 | return V; |
| 1198 | } |
| 1199 | |
Rui Ueyama | d1f8b81 | 2017-06-21 15:36:24 | [diff] [blame] | 1200 | // Parses `--exclude-libs=lib,lib,...`. |
| 1201 | // The library names may be delimited by commas or colons. |
| 1202 | static DenseSet<StringRef> getExcludeLibs(opt::InputArgList &Args) { |
| 1203 | DenseSet<StringRef> Ret; |
| 1204 | for (auto *Arg : Args.filtered(OPT_exclude_libs)) { |
| 1205 | StringRef S = Arg->getValue(); |
| 1206 | for (;;) { |
| 1207 | size_t Pos = S.find_first_of(",:"); |
| 1208 | if (Pos == StringRef::npos) |
| 1209 | break; |
| 1210 | Ret.insert(S.substr(0, Pos)); |
| 1211 | S = S.substr(Pos + 1); |
| 1212 | } |
| 1213 | Ret.insert(S); |
| 1214 | } |
| 1215 | return Ret; |
| 1216 | } |
| 1217 | |
| 1218 | // Handles the -exclude-libs option. If a static library file is specified |
| 1219 | // by the -exclude-libs option, all public symbols from the archive become |
| 1220 | // private unless otherwise specified by version scripts or something. |
| 1221 | // A special library name "ALL" means all archive files. |
| 1222 | // |
| 1223 | // This is not a popular option, but some programs such as bionic libc use it. |
Oleg Ranevskyy | 0cf24ed | 2017-10-31 13:51:06 | [diff] [blame] | 1224 | template <class ELFT> |
Peter Collingbourne | 09e04af | 2018-02-16 20:23:54 | [diff] [blame] | 1225 | static void excludeLibs(opt::InputArgList &Args) { |
Rui Ueyama | d1f8b81 | 2017-06-21 15:36:24 | [diff] [blame] | 1226 | DenseSet<StringRef> Libs = getExcludeLibs(Args); |
| 1227 | bool All = Libs.count("ALL"); |
| 1228 | |
Yi Kong | a2125b1 | 2018-07-11 17:45:28 | [diff] [blame] | 1229 | auto Visit = [&](InputFile *File) { |
Peter Collingbourne | 09e04af | 2018-02-16 20:23:54 | [diff] [blame] | 1230 | if (!File->ArchiveName.empty()) |
| 1231 | if (All || Libs.count(path::filename(File->ArchiveName))) |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 | [diff] [blame] | 1232 | for (Symbol *Sym : File->getSymbols()) |
Peter Collingbourne | 09e04af | 2018-02-16 20:23:54 | [diff] [blame] | 1233 | if (!Sym->isLocal() && Sym->File == File) |
Rui Ueyama | f1f0084 | 2017-10-31 16:07:41 | [diff] [blame] | 1234 | Sym->VersionId = VER_NDX_LOCAL; |
Yi Kong | a2125b1 | 2018-07-11 17:45:28 | [diff] [blame] | 1235 | }; |
| 1236 | |
| 1237 | for (InputFile *File : ObjectFiles) |
| 1238 | Visit(File); |
| 1239 | |
| 1240 | for (BitcodeFile *File : BitcodeFiles) |
| 1241 | Visit(File); |
Rui Ueyama | d1f8b81 | 2017-06-21 15:36:24 | [diff] [blame] | 1242 | } |
| 1243 | |
Rui Ueyama | cc013f6 | 2018-04-03 18:01:18 | [diff] [blame] | 1244 | // Force Sym to be entered in the output. Used for -u or equivalent. |
| 1245 | template <class ELFT> static void handleUndefined(StringRef Name) { |
| 1246 | Symbol *Sym = Symtab->find(Name); |
| 1247 | if (!Sym) |
| 1248 | return; |
| 1249 | |
| 1250 | // Since symbol S may not be used inside the program, LTO may |
| 1251 | // eliminate it. Mark the symbol as "used" to prevent it. |
| 1252 | Sym->IsUsedInRegularObj = true; |
| 1253 | |
| 1254 | if (Sym->isLazy()) |
| 1255 | Symtab->fetchLazy<ELFT>(Sym); |
| 1256 | } |
| 1257 | |
Peter Collingbourne | 9893011 | 2018-08-08 23:48:12 | [diff] [blame] | 1258 | template <class ELFT> static void handleLibcall(StringRef Name) { |
| 1259 | Symbol *Sym = Symtab->find(Name); |
| 1260 | if (!Sym || !Sym->isLazy()) |
| 1261 | return; |
| 1262 | |
| 1263 | MemoryBufferRef MB; |
| 1264 | if (auto *LO = dyn_cast<LazyObject>(Sym)) |
| 1265 | MB = LO->File->MB; |
| 1266 | else |
| 1267 | MB = cast<LazyArchive>(Sym)->getMemberBuffer(); |
| 1268 | |
| 1269 | if (isBitcode(MB)) |
| 1270 | Symtab->fetchLazy<ELFT>(Sym); |
| 1271 | } |
| 1272 | |
Chih-Hung Hsieh | 73e0484 | 2018-09-11 23:00:36 | [diff] [blame] | 1273 | // If all references to a DSO happen to be weak, the DSO is not added |
| 1274 | // to DT_NEEDED. If that happens, we need to eliminate shared symbols |
| 1275 | // created from the DSO. Otherwise, they become dangling references |
| 1276 | // that point to a non-existent DSO. |
| 1277 | template <class ELFT> static void demoteSharedSymbols() { |
Rafael Espindola | 61376d9 | 2018-04-25 00:29:13 | [diff] [blame] | 1278 | for (Symbol *Sym : Symtab->getSymbols()) { |
Chih-Hung Hsieh | 73e0484 | 2018-09-11 23:00:36 | [diff] [blame] | 1279 | if (auto *S = dyn_cast<SharedSymbol>(Sym)) { |
| 1280 | if (!S->getFile<ELFT>().IsNeeded) { |
| 1281 | bool Used = S->Used; |
| 1282 | replaceSymbol<Undefined>(S, nullptr, S->getName(), STB_WEAK, S->StOther, |
| 1283 | S->Type); |
| 1284 | S->Used = Used; |
| 1285 | } |
Rafael Espindola | 61376d9 | 2018-04-25 00:29:13 | [diff] [blame] | 1286 | } |
| 1287 | } |
| 1288 | } |
| 1289 | |
Peter Collingbourne | a052206 | 2018-07-21 02:14:59 | [diff] [blame] | 1290 | // The section referred to by S is considered address-significant. Set the |
| 1291 | // KeepUnique flag on the section if appropriate. |
| 1292 | static void markAddrsig(Symbol *S) { |
| 1293 | if (auto *D = dyn_cast_or_null<Defined>(S)) |
| 1294 | if (D->Section) |
| 1295 | // We don't need to keep text sections unique under --icf=all even if they |
| 1296 | // are address-significant. |
| 1297 | if (Config->ICF == ICFLevel::Safe || !(D->Section->Flags & SHF_EXECINSTR)) |
| 1298 | D->Section->KeepUnique = true; |
Peter Collingbourne | a327a4c | 2018-07-18 22:49:31 | [diff] [blame] | 1299 | } |
| 1300 | |
Peter Smith | dbef8cc | 2018-05-15 08:57:21 | [diff] [blame] | 1301 | // Record sections that define symbols mentioned in --keep-unique <symbol> |
Peter Collingbourne | a327a4c | 2018-07-18 22:49:31 | [diff] [blame] | 1302 | // and symbols referred to by address-significance tables. These sections are |
| 1303 | // ineligible for ICF. |
| 1304 | template <class ELFT> |
Peter Smith | dbef8cc | 2018-05-15 08:57:21 | [diff] [blame] | 1305 | static void findKeepUniqueSections(opt::InputArgList &Args) { |
| 1306 | for (auto *Arg : Args.filtered(OPT_keep_unique)) { |
| 1307 | StringRef Name = Arg->getValue(); |
Peter Collingbourne | a052206 | 2018-07-21 02:14:59 | [diff] [blame] | 1308 | auto *D = dyn_cast_or_null<Defined>(Symtab->find(Name)); |
| 1309 | if (!D || !D->Section) { |
Peter Smith | dbef8cc | 2018-05-15 08:57:21 | [diff] [blame] | 1310 | warn("could not find symbol " + Name + " to keep unique"); |
Peter Collingbourne | a052206 | 2018-07-21 02:14:59 | [diff] [blame] | 1311 | continue; |
| 1312 | } |
| 1313 | D->Section->KeepUnique = true; |
Peter Smith | dbef8cc | 2018-05-15 08:57:21 | [diff] [blame] | 1314 | } |
Peter Collingbourne | a327a4c | 2018-07-18 22:49:31 | [diff] [blame] | 1315 | |
Peter Collingbourne | a052206 | 2018-07-21 02:14:59 | [diff] [blame] | 1316 | // --icf=all --ignore-data-address-equality means that we can ignore |
| 1317 | // the dynsym and address-significance tables entirely. |
| 1318 | if (Config->ICF == ICFLevel::All && Config->IgnoreDataAddressEquality) |
| 1319 | return; |
Peter Collingbourne | a327a4c | 2018-07-18 22:49:31 | [diff] [blame] | 1320 | |
Peter Collingbourne | a052206 | 2018-07-21 02:14:59 | [diff] [blame] | 1321 | // Symbols in the dynsym could be address-significant in other executables |
| 1322 | // or DSOs, so we conservatively mark them as address-significant. |
| 1323 | for (Symbol *S : Symtab->getSymbols()) |
| 1324 | if (S->includeInDynsym()) |
| 1325 | markAddrsig(S); |
| 1326 | |
| 1327 | // Visit the address-significance table in each object file and mark each |
| 1328 | // referenced symbol as address-significant. |
| 1329 | for (InputFile *F : ObjectFiles) { |
| 1330 | auto *Obj = cast<ObjFile<ELFT>>(F); |
| 1331 | ArrayRef<Symbol *> Syms = Obj->getSymbols(); |
| 1332 | if (Obj->AddrsigSec) { |
| 1333 | ArrayRef<uint8_t> Contents = |
| 1334 | check(Obj->getObj().getSectionContents(Obj->AddrsigSec)); |
| 1335 | const uint8_t *Cur = Contents.begin(); |
| 1336 | while (Cur != Contents.end()) { |
| 1337 | unsigned Size; |
| 1338 | const char *Err; |
| 1339 | uint64_t SymIndex = decodeULEB128(Cur, &Size, Contents.end(), &Err); |
| 1340 | if (Err) |
| 1341 | fatal(toString(F) + ": could not decode addrsig section: " + Err); |
| 1342 | markAddrsig(Syms[SymIndex]); |
| 1343 | Cur += Size; |
Peter Collingbourne | a327a4c | 2018-07-18 22:49:31 | [diff] [blame] | 1344 | } |
Peter Collingbourne | a052206 | 2018-07-21 02:14:59 | [diff] [blame] | 1345 | } else { |
| 1346 | // If an object file does not have an address-significance table, |
| 1347 | // conservatively mark all of its symbols as address-significant. |
| 1348 | for (Symbol *S : Syms) |
| 1349 | markAddrsig(S); |
Peter Collingbourne | a327a4c | 2018-07-18 22:49:31 | [diff] [blame] | 1350 | } |
| 1351 | } |
Peter Smith | dbef8cc | 2018-05-15 08:57:21 | [diff] [blame] | 1352 | } |
| 1353 | |
Rui Ueyama | c7497d3 | 2018-10-11 20:34:29 | [diff] [blame] | 1354 | template <class ELFT> static Symbol *addUndefined(StringRef Name) { |
| 1355 | return Symtab->addUndefined<ELFT>(Name, STB_GLOBAL, STV_DEFAULT, 0, false, |
| 1356 | nullptr); |
| 1357 | } |
| 1358 | |
Rui Ueyama | 07b4536 | 2018-08-22 07:02:26 | [diff] [blame] | 1359 | // The --wrap option is a feature to rename symbols so that you can write |
| 1360 | // wrappers for existing functions. If you pass `-wrap=foo`, all |
| 1361 | // occurrences of symbol `foo` are resolved to `wrap_foo` (so, you are |
| 1362 | // expected to write `wrap_foo` function as a wrapper). The original |
| 1363 | // symbol becomes accessible as `real_foo`, so you can call that from your |
| 1364 | // wrapper. |
| 1365 | // |
| 1366 | // This data structure is instantiated for each -wrap option. |
| 1367 | struct WrappedSymbol { |
| 1368 | Symbol *Sym; |
| 1369 | Symbol *Real; |
| 1370 | Symbol *Wrap; |
| 1371 | }; |
| 1372 | |
| 1373 | // Handles -wrap option. |
| 1374 | // |
| 1375 | // This function instantiates wrapper symbols. At this point, they seem |
| 1376 | // like they are not being used at all, so we explicitly set some flags so |
| 1377 | // that LTO won't eliminate them. |
| 1378 | template <class ELFT> |
| 1379 | static std::vector<WrappedSymbol> addWrappedSymbols(opt::InputArgList &Args) { |
| 1380 | std::vector<WrappedSymbol> V; |
| 1381 | DenseSet<StringRef> Seen; |
| 1382 | |
| 1383 | for (auto *Arg : Args.filtered(OPT_wrap)) { |
| 1384 | StringRef Name = Arg->getValue(); |
| 1385 | if (!Seen.insert(Name).second) |
| 1386 | continue; |
| 1387 | |
| 1388 | Symbol *Sym = Symtab->find(Name); |
| 1389 | if (!Sym) |
| 1390 | continue; |
| 1391 | |
Rui Ueyama | c7497d3 | 2018-10-11 20:34:29 | [diff] [blame] | 1392 | Symbol *Real = addUndefined<ELFT>(Saver.save("__real_" + Name)); |
| 1393 | Symbol *Wrap = addUndefined<ELFT>(Saver.save("__wrap_" + Name)); |
Rui Ueyama | 07b4536 | 2018-08-22 07:02:26 | [diff] [blame] | 1394 | V.push_back({Sym, Real, Wrap}); |
| 1395 | |
| 1396 | // We want to tell LTO not to inline symbols to be overwritten |
| 1397 | // because LTO doesn't know the final symbol contents after renaming. |
| 1398 | Real->CanInline = false; |
| 1399 | Sym->CanInline = false; |
| 1400 | |
| 1401 | // Tell LTO not to eliminate these symbols. |
| 1402 | Sym->IsUsedInRegularObj = true; |
| 1403 | Wrap->IsUsedInRegularObj = true; |
| 1404 | } |
| 1405 | return V; |
| 1406 | } |
| 1407 | |
| 1408 | // Do renaming for -wrap by updating pointers to symbols. |
| 1409 | // |
| 1410 | // When this function is executed, only InputFiles and symbol table |
| 1411 | // contain pointers to symbol objects. We visit them to replace pointers, |
| 1412 | // so that wrapped symbols are swapped as instructed by the command line. |
| 1413 | template <class ELFT> static void wrapSymbols(ArrayRef<WrappedSymbol> Wrapped) { |
| 1414 | DenseMap<Symbol *, Symbol *> Map; |
| 1415 | for (const WrappedSymbol &W : Wrapped) { |
| 1416 | Map[W.Sym] = W.Wrap; |
| 1417 | Map[W.Real] = W.Sym; |
| 1418 | } |
| 1419 | |
| 1420 | // Update pointers in input files. |
| 1421 | parallelForEach(ObjectFiles, [&](InputFile *File) { |
| 1422 | std::vector<Symbol *> &Syms = File->getMutableSymbols(); |
| 1423 | for (size_t I = 0, E = Syms.size(); I != E; ++I) |
| 1424 | if (Symbol *S = Map.lookup(Syms[I])) |
| 1425 | Syms[I] = S; |
| 1426 | }); |
| 1427 | |
| 1428 | // Update pointers in the symbol table. |
| 1429 | for (const WrappedSymbol &W : Wrapped) |
| 1430 | Symtab->wrap(W.Sym, W.Real, W.Wrap); |
| 1431 | } |
| 1432 | |
Peter Collingbourne | 2da4e52 | 2018-07-31 20:36:17 | [diff] [blame] | 1433 | static const char *LibcallRoutineNames[] = { |
| 1434 | #define HANDLE_LIBCALL(code, name) name, |
| 1435 | #include "llvm/IR/RuntimeLibcalls.def" |
| 1436 | #undef HANDLE_LIBCALL |
| 1437 | }; |
| 1438 | |
Rui Ueyama | 630a382 | 2016-04-22 19:58:47 | [diff] [blame] | 1439 | // Do actual linking. Note that when this function is called, |
| 1440 | // all linker scripts have already been parsed. |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 | [diff] [blame] | 1441 | template <class ELFT> void LinkerDriver::link(opt::InputArgList &Args) { |
Rui Ueyama | e145bc2 | 2017-06-16 20:15:03 | [diff] [blame] | 1442 | Target = getTarget(); |
Rui Ueyama | 4e24752 | 2018-09-25 19:26:58 | [diff] [blame] | 1443 | InX<ELFT>::VerSym = nullptr; |
| 1444 | InX<ELFT>::VerNeed = nullptr; |
Rui Ueyama | ff77768 | 2015-10-09 21:12:40 | [diff] [blame] | 1445 | |
Rui Ueyama | e4eadb6 | 2016-12-08 17:44:37 | [diff] [blame] | 1446 | Config->MaxPageSize = getMaxPageSize(Args); |
Rui Ueyama | c5dd543 | 2016-10-26 04:34:16 | [diff] [blame] | 1447 | Config->ImageBase = getImageBase(Args); |
Rui Ueyama | 6c5638b | 2016-03-13 20:10:20 | [diff] [blame] | 1448 | |
George Rimar | d46753e | 2017-10-06 09:37:44 | [diff] [blame] | 1449 | // If a -hash-style option was not given, set to a default value, |
| 1450 | // which varies depending on the target. |
| 1451 | if (!Args.hasArg(OPT_hash_style)) { |
| 1452 | if (Config->EMachine == EM_MIPS) |
| 1453 | Config->SysvHash = true; |
| 1454 | else |
| 1455 | Config->SysvHash = Config->GnuHash = true; |
| 1456 | } |
| 1457 | |
Rui Ueyama | 630a382 | 2016-04-22 19:58:47 | [diff] [blame] | 1458 | // Default output filename is "a.out" by the Unix tradition. |
| 1459 | if (Config->OutputFile.empty()) |
| 1460 | Config->OutputFile = "a.out"; |
| 1461 | |
James Henderson | b7a90ef | 2017-04-04 09:42:24 | [diff] [blame] | 1462 | // Fail early if the output file or map file is not writable. If a user has a |
| 1463 | // long link, e.g. due to a large LTO link, they do not wish to run it and |
| 1464 | // find that it failed because there was a mistake in their command-line. |
Rui Ueyama | 7163027 | 2017-04-26 16:14:46 | [diff] [blame] | 1465 | if (auto E = tryCreateFile(Config->OutputFile)) |
| 1466 | error("cannot open output file " + Config->OutputFile + ": " + E.message()); |
| 1467 | if (auto E = tryCreateFile(Config->MapFile)) |
| 1468 | error("cannot open map file " + Config->MapFile + ": " + E.message()); |
Bob Haarman | b8a59c8 | 2017-10-25 22:28:38 | [diff] [blame] | 1469 | if (errorCount()) |
Rui Ueyama | f327999 | 2017-03-13 23:23:40 | [diff] [blame] | 1470 | return; |
| 1471 | |
Rui Ueyama | 34bf867 | 2016-12-08 17:32:58 | [diff] [blame] | 1472 | // Use default entry point name if no name was given via the command |
| 1473 | // line nor linker scripts. For some reason, MIPS entry point name is |
Rui Ueyama | 4de746f | 2016-12-07 04:45:34 | [diff] [blame] | 1474 | // different from others. |
Simon Atanasyan | 8f7d281 | 2016-12-20 22:24:45 | [diff] [blame] | 1475 | Config->WarnMissingEntry = |
| 1476 | (!Config->Entry.empty() || (!Config->Shared && !Config->Relocatable)); |
Rui Ueyama | 34bf867 | 2016-12-08 17:32:58 | [diff] [blame] | 1477 | if (Config->Entry.empty() && !Config->Relocatable) |
Rui Ueyama | 4de746f | 2016-12-07 04:45:34 | [diff] [blame] | 1478 | Config->Entry = (Config->EMachine == EM_MIPS) ? "__start" : "_start"; |
| 1479 | |
Rui Ueyama | 69c778c | 2016-07-17 17:50:09 | [diff] [blame] | 1480 | // Handle --trace-symbol. |
| 1481 | for (auto *Arg : Args.filtered(OPT_trace_symbol)) |
Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 | [diff] [blame] | 1482 | Symtab->trace(Arg->getValue()); |
Rui Ueyama | 69c778c | 2016-07-17 17:50:09 | [diff] [blame] | 1483 | |
Rui Ueyama | 4de746f | 2016-12-07 04:45:34 | [diff] [blame] | 1484 | // Add all files to the symbol table. This will add almost all |
| 1485 | // symbols that we need to the symbol table. |
Rui Ueyama | 38dbd3e | 2016-09-14 00:05:51 | [diff] [blame] | 1486 | for (InputFile *F : Files) |
Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 | [diff] [blame] | 1487 | Symtab->addFile<ELFT>(F); |
Eugene Leviant | 3967ea0 | 2016-09-08 08:57:51 | [diff] [blame] | 1488 | |
Rafael Espindola | e05e2f8 | 2017-09-15 18:05:02 | [diff] [blame] | 1489 | // Now that we have every file, we can decide if we will need a |
| 1490 | // dynamic symbol table. |
| 1491 | // We need one if we were asked to export dynamic symbols or if we are |
| 1492 | // producing a shared library. |
| 1493 | // We also need one if any shared libraries are used and for pie executables |
| 1494 | // (probably because the dynamic linker needs it). |
George Rimar | 696a7f9 | 2017-09-19 09:20:54 | [diff] [blame] | 1495 | Config->HasDynSymTab = |
| 1496 | !SharedFiles.empty() || Config->Pic || Config->ExportDynamic; |
Rafael Espindola | e05e2f8 | 2017-09-15 18:05:02 | [diff] [blame] | 1497 | |
George Rimar | 51d193f | 2017-08-23 08:37:22 | [diff] [blame] | 1498 | // Some symbols (such as __ehdr_start) are defined lazily only when there |
| 1499 | // are undefined symbols for them, so we add these to trigger that logic. |
Rui Ueyama | c7497d3 | 2018-10-11 20:34:29 | [diff] [blame] | 1500 | for (StringRef Name : Script->ReferencedSymbols) |
| 1501 | addUndefined<ELFT>(Name); |
George Rimar | 51d193f | 2017-08-23 08:37:22 | [diff] [blame] | 1502 | |
Rui Ueyama | a215e7c | 2017-10-03 20:45:09 | [diff] [blame] | 1503 | // Handle the `--undefined <sym>` options. |
| 1504 | for (StringRef S : Config->Undefined) |
Rui Ueyama | cc013f6 | 2018-04-03 18:01:18 | [diff] [blame] | 1505 | handleUndefined<ELFT>(S); |
Rui Ueyama | a215e7c | 2017-10-03 20:45:09 | [diff] [blame] | 1506 | |
Peter Collingbourne | 2da4e52 | 2018-07-31 20:36:17 | [diff] [blame] | 1507 | // If an entry symbol is in a static archive, pull out that file now. |
Rui Ueyama | cc013f6 | 2018-04-03 18:01:18 | [diff] [blame] | 1508 | handleUndefined<ELFT>(Config->Entry); |
Eugene Leviant | 3967ea0 | 2016-09-08 08:57:51 | [diff] [blame] | 1509 | |
Peter Collingbourne | 2da4e52 | 2018-07-31 20:36:17 | [diff] [blame] | 1510 | // If any of our inputs are bitcode files, the LTO code generator may create |
| 1511 | // references to certain library functions that might not be explicit in the |
| 1512 | // bitcode file's symbol table. If any of those library functions are defined |
| 1513 | // in a bitcode file in an archive member, we need to arrange to use LTO to |
| 1514 | // compile those archive members by adding them to the link beforehand. |
| 1515 | // |
Peter Collingbourne | 9893011 | 2018-08-08 23:48:12 | [diff] [blame] | 1516 | // However, adding all libcall symbols to the link can have undesired |
| 1517 | // consequences. For example, the libgcc implementation of |
| 1518 | // __sync_val_compare_and_swap_8 on 32-bit ARM pulls in an .init_array entry |
| 1519 | // that aborts the program if the Linux kernel does not support 64-bit |
| 1520 | // atomics, which would prevent the program from running even if it does not |
| 1521 | // use 64-bit atomics. |
| 1522 | // |
| 1523 | // Therefore, we only add libcall symbols to the link before LTO if we have |
| 1524 | // to, i.e. if the symbol's definition is in bitcode. Any other required |
| 1525 | // libcall symbols will be added to the link after LTO when we add the LTO |
| 1526 | // object file to the link. |
Peter Collingbourne | 2da4e52 | 2018-07-31 20:36:17 | [diff] [blame] | 1527 | if (!BitcodeFiles.empty()) |
| 1528 | for (const char *S : LibcallRoutineNames) |
Peter Collingbourne | 9893011 | 2018-08-08 23:48:12 | [diff] [blame] | 1529 | handleLibcall<ELFT>(S); |
Peter Collingbourne | 2da4e52 | 2018-07-31 20:36:17 | [diff] [blame] | 1530 | |
Rui Ueyama | 4de746f | 2016-12-07 04:45:34 | [diff] [blame] | 1531 | // Return if there were name resolution errors. |
Bob Haarman | b8a59c8 | 2017-10-25 22:28:38 | [diff] [blame] | 1532 | if (errorCount()) |
Rui Ueyama | 4de746f | 2016-12-07 04:45:34 | [diff] [blame] | 1533 | return; |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 | [diff] [blame] | 1534 | |
George Rimar | 9e2c8a9 | 2018-03-08 14:54:38 | [diff] [blame] | 1535 | // Now when we read all script files, we want to finalize order of linker |
| 1536 | // script commands, which can be not yet final because of INSERT commands. |
| 1537 | Script->processInsertCommands(); |
| 1538 | |
Igor Kudrin | 3345c9a | 2018-02-27 07:18:07 | [diff] [blame] | 1539 | // We want to declare linker script's symbols early, |
| 1540 | // so that we can version them. |
| 1541 | // They also might be exported if referenced by DSOs. |
| 1542 | Script->declareSymbols(); |
| 1543 | |
Rui Ueyama | d1f8b81 | 2017-06-21 15:36:24 | [diff] [blame] | 1544 | // Handle the -exclude-libs option. |
| 1545 | if (Args.hasArg(OPT_exclude_libs)) |
Peter Collingbourne | 09e04af | 2018-02-16 20:23:54 | [diff] [blame] | 1546 | excludeLibs<ELFT>(Args); |
Rui Ueyama | d1f8b81 | 2017-06-21 15:36:24 | [diff] [blame] | 1547 | |
Rafael Espindola | 63fcc5c | 2017-12-11 17:23:28 | [diff] [blame] | 1548 | // Create ElfHeader early. We need a dummy section in |
| 1549 | // addReservedSymbols to mark the created symbols as not absolute. |
| 1550 | Out::ElfHeader = make<OutputSection>("", 0, SHF_ALLOC); |
| 1551 | Out::ElfHeader->Size = sizeof(typename ELFT::Ehdr); |
| 1552 | |
Thomas Anderson | 4a401e9 | 2019-01-02 19:28:00 | [diff] [blame] | 1553 | // Create wrapped symbols for -wrap option. |
| 1554 | std::vector<WrappedSymbol> Wrapped = addWrappedSymbols<ELFT>(Args); |
| 1555 | |
Rafael Espindola | 63fcc5c | 2017-12-11 17:23:28 | [diff] [blame] | 1556 | // We need to create some reserved symbols such as _end. Create them. |
| 1557 | if (!Config->Relocatable) |
Rafael Espindola | 9a84f6b | 2017-12-23 17:21:39 | [diff] [blame] | 1558 | addReservedSymbols(); |
Rafael Espindola | 63fcc5c | 2017-12-11 17:23:28 | [diff] [blame] | 1559 | |
Rui Ueyama | d1f8b81 | 2017-06-21 15:36:24 | [diff] [blame] | 1560 | // Apply version scripts. |
Rui Ueyama | 44a8471 | 2018-02-15 02:40:58 | [diff] [blame] | 1561 | // |
| 1562 | // For a relocatable output, version scripts don't make sense, and |
| 1563 | // parsing a symbol version string (e.g. dropping "@ver1" from a symbol |
| 1564 | // name "foo@ver1") rather do harm, so we don't call this if -r is given. |
| 1565 | if (!Config->Relocatable) |
| 1566 | Symtab->scanVersionScript(); |
Peter Collingbourne | 3a35c45 | 2016-04-22 18:47:52 | [diff] [blame] | 1567 | |
Rui Ueyama | 554adb2 | 2018-05-07 22:11:34 | [diff] [blame] | 1568 | // Do link-time optimization if given files are LLVM bitcode files. |
| 1569 | // This compiles bitcode files into real object files. |
Peter Collingbourne | 9893011 | 2018-08-08 23:48:12 | [diff] [blame] | 1570 | // |
| 1571 | // With this the symbol table should be complete. After this, no new names |
| 1572 | // except a few linker-synthesized ones will be added to the symbol table. |
Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 | [diff] [blame] | 1573 | Symtab->addCombinedLTOObject<ELFT>(); |
Bob Haarman | b8a59c8 | 2017-10-25 22:28:38 | [diff] [blame] | 1574 | if (errorCount()) |
Davide Italiano | d26c4a1 | 2016-05-15 19:29:38 | [diff] [blame] | 1575 | return; |
Rafael Espindola | 9f77ef0 | 2016-02-12 20:54:57 | [diff] [blame] | 1576 | |
Rui Ueyama | 554adb2 | 2018-05-07 22:11:34 | [diff] [blame] | 1577 | // If -thinlto-index-only is given, we should create only "index |
| 1578 | // files" and not object files. Index file creation is already done |
| 1579 | // in addCombinedLTOObject, so we are done if that's the case. |
| 1580 | if (Config->ThinLTOIndexOnly) |
| 1581 | return; |
| 1582 | |
Rui Ueyama | 9f49990 | 2018-12-14 21:58:49 | [diff] [blame] | 1583 | // Likewise, --plugin-opt=emit-llvm is an option to make LTO create |
| 1584 | // an output file in bitcode and exit, so that you can just get a |
| 1585 | // combined bitcode file. |
| 1586 | if (Config->EmitLLVM) |
| 1587 | return; |
| 1588 | |
Rui Ueyama | dc0b0b0 | 2017-11-04 23:09:43 | [diff] [blame] | 1589 | // Apply symbol renames for -wrap. |
Rui Ueyama | 07b4536 | 2018-08-22 07:02:26 | [diff] [blame] | 1590 | if (!Wrapped.empty()) |
| 1591 | wrapSymbols<ELFT>(Wrapped); |
George Rimar | 9703ad2 | 2017-04-26 10:40:02 | [diff] [blame] | 1592 | |
Rui Ueyama | 8c6a5aa | 2016-11-05 22:37:59 | [diff] [blame] | 1593 | // Now that we have a complete list of input files. |
| 1594 | // Beyond this point, no new files are added. |
| 1595 | // Aggregate all input sections into one place. |
George Rimar | 696a7f9 | 2017-09-19 09:20:54 | [diff] [blame] | 1596 | for (InputFile *F : ObjectFiles) |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 | [diff] [blame] | 1597 | for (InputSectionBase *S : F->getSections()) |
Rafael Espindola | 774ea7d | 2017-02-23 16:49:07 | [diff] [blame] | 1598 | if (S && S != &InputSection::Discarded) |
Rui Ueyama | 536a267 | 2017-02-27 02:32:08 | [diff] [blame] | 1599 | InputSections.push_back(S); |
George Rimar | 696a7f9 | 2017-09-19 09:20:54 | [diff] [blame] | 1600 | for (BinaryFile *F : BinaryFiles) |
Rafael Espindola | c404d50 | 2017-02-23 02:32:18 | [diff] [blame] | 1601 | for (InputSectionBase *S : F->getSections()) |
Rui Ueyama | 536a267 | 2017-02-27 02:32:08 | [diff] [blame] | 1602 | InputSections.push_back(cast<InputSection>(S)); |
Rui Ueyama | 8c6a5aa | 2016-11-05 22:37:59 | [diff] [blame] | 1603 | |
George Rimar | ddd2424 | 2017-11-04 08:20:30 | [diff] [blame] | 1604 | // We do not want to emit debug sections if --strip-all |
| 1605 | // or -strip-debug are given. |
| 1606 | if (Config->Strip != StripPolicy::None) |
| 1607 | llvm::erase_if(InputSections, [](InputSectionBase *S) { |
| 1608 | return S->Name.startswith(".debug") || S->Name.startswith(".zdebug"); |
| 1609 | }); |
| 1610 | |
Konstantin Zhuravlyov | e7f1734 | 2017-10-24 17:01:40 | [diff] [blame] | 1611 | Config->EFlags = Target->calcEFlags(); |
Simon Atanasyan | 649e4d3 | 2017-10-02 14:56:41 | [diff] [blame] | 1612 | |
Peter Smith | 57eb046 | 2017-11-28 13:51:48 | [diff] [blame] | 1613 | if (Config->EMachine == EM_ARM) { |
| 1614 | // FIXME: These warnings can be removed when lld only uses these features |
| 1615 | // when the input objects have been compiled with an architecture that |
| 1616 | // supports them. |
| 1617 | if (Config->ARMHasBlx == false) |
| 1618 | warn("lld uses blx instruction, no object with architecture supporting " |
Rui Ueyama | 9db0642 | 2018-10-25 18:07:55 | [diff] [blame] | 1619 | "feature detected"); |
Peter Smith | 57eb046 | 2017-11-28 13:51:48 | [diff] [blame] | 1620 | } |
| 1621 | |
Peter Collingbourne | dc7936e | 2017-06-12 00:00:51 | [diff] [blame] | 1622 | // This adds a .comment section containing a version string. We have to add it |
Rui Ueyama | e28c146 | 2018-10-08 16:58:59 | [diff] [blame] | 1623 | // before mergeSections because the .comment section is a mergeable section. |
Peter Collingbourne | dc7936e | 2017-06-12 00:00:51 | [diff] [blame] | 1624 | if (!Config->Relocatable) |
Rafael Espindola | 5c73c49 | 2017-12-21 01:21:59 | [diff] [blame] | 1625 | InputSections.push_back(createCommentSection()); |
Peter Collingbourne | dc7936e | 2017-06-12 00:00:51 | [diff] [blame] | 1626 | |
| 1627 | // Do size optimizations: garbage collection, merging of SHF_MERGE sections |
| 1628 | // and identical code folding. |
Rafael Espindola | f1652d4 | 2018-04-27 18:17:36 | [diff] [blame] | 1629 | splitSections<ELFT>(); |
Rui Ueyama | a1b79df | 2017-10-10 22:59:32 | [diff] [blame] | 1630 | markLive<ELFT>(); |
Chih-Hung Hsieh | 73e0484 | 2018-09-11 23:00:36 | [diff] [blame] | 1631 | demoteSharedSymbols<ELFT>(); |
Rui Ueyama | 2b714b5 | 2017-10-11 03:12:53 | [diff] [blame] | 1632 | mergeSections(); |
Peter Collingbourne | a327a4c | 2018-07-18 22:49:31 | [diff] [blame] | 1633 | if (Config->ICF != ICFLevel::None) { |
| 1634 | findKeepUniqueSections<ELFT>(Args); |
Rui Ueyama | 4f8d21f | 2016-05-02 19:30:42 | [diff] [blame] | 1635 | doIcf<ELFT>(); |
Peter Smith | dbef8cc | 2018-05-15 08:57:21 | [diff] [blame] | 1636 | } |
Rui Ueyama | b91bf1a | 2016-05-23 16:55:43 | [diff] [blame] | 1637 | |
Michael J. Spencer | b842725 | 2018-04-17 23:30:05 | [diff] [blame] | 1638 | // Read the callgraph now that we know what was gced or icfed |
Fangrui Song | cc18f8a | 2018-10-25 23:15:23 | [diff] [blame] | 1639 | if (Config->CallGraphProfileSort) { |
| 1640 | if (auto *Arg = Args.getLastArg(OPT_call_graph_ordering_file)) |
| 1641 | if (Optional<MemoryBufferRef> Buffer = readFile(Arg->getValue())) |
| 1642 | readCallGraph(*Buffer); |
| 1643 | readCallGraphsFromObjectFiles<ELFT>(); |
| 1644 | } |
Michael J. Spencer | b842725 | 2018-04-17 23:30:05 | [diff] [blame] | 1645 | |
Rui Ueyama | a75b7a4 | 2016-09-13 19:56:25 | [diff] [blame] | 1646 | // Write the result to the file. |
Rui Ueyama | 84907c5 | 2016-08-09 03:38:23 | [diff] [blame] | 1647 | writeResult<ELFT>(); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 | [diff] [blame] | 1648 | } |