Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 1 | //===- Writer.cpp ---------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Linker |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Michael J. Spencer | f832541 | 2015-09-04 22:48:30 +0000 | [diff] [blame] | 10 | #include "Writer.h" |
Rui Ueyama | cb8474ed | 2015-08-05 23:51:50 +0000 | [diff] [blame] | 11 | #include "Config.h" |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 12 | #include "LinkerScript.h" |
Rui Ueyama | 9381eb1 | 2016-12-18 14:06:06 +0000 | [diff] [blame] | 13 | #include "Memory.h" |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 14 | #include "OutputSections.h" |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 15 | #include "Relocations.h" |
Rui Ueyama | fbbde54 | 2016-06-29 09:08:02 +0000 | [diff] [blame] | 16 | #include "Strings.h" |
Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 17 | #include "SymbolTable.h" |
Rui Ueyama | 6dc7fcb | 2016-11-01 20:28:21 +0000 | [diff] [blame] | 18 | #include "SyntheticSections.h" |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 19 | #include "Target.h" |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/StringMap.h" |
Hal Finkel | 3bae2d8 | 2015-10-12 20:51:48 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/StringSwitch.h" |
Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 22 | #include "llvm/Support/FileOutputBuffer.h" |
Rui Ueyama | 6d12eae | 2016-12-05 17:40:37 +0000 | [diff] [blame] | 23 | #include "llvm/Support/FileSystem.h" |
Rafael Espindola | d0078b2 | 2016-02-06 00:06:26 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
Rui Ueyama | e8b2df4 | 2016-09-29 01:45:22 +0000 | [diff] [blame] | 25 | #include <climits> |
Rui Ueyama | 6d12eae | 2016-12-05 17:40:37 +0000 | [diff] [blame] | 26 | #include <thread> |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace llvm; |
| 29 | using namespace llvm::ELF; |
| 30 | using namespace llvm::object; |
Peter Smith | 0a259f3 | 2016-10-10 09:39:26 +0000 | [diff] [blame] | 31 | using namespace llvm::support; |
| 32 | using namespace llvm::support::endian; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 33 | |
| 34 | using namespace lld; |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 35 | using namespace lld::elf; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 36 | |
Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 37 | namespace { |
| 38 | // The writer writes a SymbolTable result to a file. |
| 39 | template <class ELFT> class Writer { |
| 40 | public: |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 41 | typedef typename ELFT::uint uintX_t; |
| 42 | typedef typename ELFT::Shdr Elf_Shdr; |
| 43 | typedef typename ELFT::Ehdr Elf_Ehdr; |
| 44 | typedef typename ELFT::Phdr Elf_Phdr; |
| 45 | typedef typename ELFT::Sym Elf_Sym; |
| 46 | typedef typename ELFT::SymRange Elf_Sym_Range; |
| 47 | typedef typename ELFT::Rela Elf_Rela; |
Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 48 | void run(); |
| 49 | |
| 50 | private: |
Rui Ueyama | f83aca4 | 2016-11-01 23:17:45 +0000 | [diff] [blame] | 51 | void createSyntheticSections(); |
Rui Ueyama | 5a9640b | 2015-10-08 23:49:30 +0000 | [diff] [blame] | 52 | void copyLocalSymbols(); |
Rui Ueyama | f18fe7b | 2015-12-26 07:50:39 +0000 | [diff] [blame] | 53 | void addReservedSymbols(); |
Eugene Leviant | 282251a | 2016-11-01 09:49:24 +0000 | [diff] [blame] | 54 | void addInputSec(InputSectionBase<ELFT> *S); |
George Rimar | 9e69450 | 2016-07-29 16:18:47 +0000 | [diff] [blame] | 55 | void createSections(); |
Rafael Espindola | 9f0c4bb | 2016-11-10 14:53:24 +0000 | [diff] [blame] | 56 | void forEachRelSec(std::function<void(InputSectionBase<ELFT> &)> Fn); |
Rafael Espindola | 24c073d | 2016-09-21 22:36:19 +0000 | [diff] [blame] | 57 | void sortSections(); |
Eugene Leviant | e63d81b | 2016-07-20 14:43:20 +0000 | [diff] [blame] | 58 | void finalizeSections(); |
Rui Ueyama | 84417f8 | 2015-12-26 07:50:41 +0000 | [diff] [blame] | 59 | void addPredefinedSections(); |
Rui Ueyama | 2df0fd8 | 2015-12-25 07:38:58 +0000 | [diff] [blame] | 60 | |
Rafael Espindola | 17cb7c0 | 2016-12-19 17:01:01 +0000 | [diff] [blame] | 61 | std::vector<PhdrEntry> createPhdrs(); |
Rafael Espindola | 074ba93 | 2016-12-06 13:43:34 +0000 | [diff] [blame] | 62 | void removeEmptyPTLoad(); |
Rafael Espindola | 17cb7c0 | 2016-12-19 17:01:01 +0000 | [diff] [blame] | 63 | void addPtArmExid(std::vector<PhdrEntry> &Phdrs); |
Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 64 | void assignAddresses(); |
Rui Ueyama | e044e9c | 2016-04-01 17:07:17 +0000 | [diff] [blame] | 65 | void assignFileOffsets(); |
George Rimar | 86ce267 | 2016-08-25 09:05:47 +0000 | [diff] [blame] | 66 | void assignFileOffsetsBinary(); |
Rui Ueyama | e044e9c | 2016-04-01 17:07:17 +0000 | [diff] [blame] | 67 | void setPhdrs(); |
George Rimar | 7ca0627 | 2016-04-06 07:20:45 +0000 | [diff] [blame] | 68 | void fixHeaders(); |
Rui Ueyama | 4709190 | 2016-03-30 19:41:51 +0000 | [diff] [blame] | 69 | void fixSectionAlignments(); |
Rui Ueyama | 1a311f1 | 2015-12-26 10:52:26 +0000 | [diff] [blame] | 70 | void fixAbsoluteSymbols(); |
Rui Ueyama | f7f52ef | 2016-04-01 17:24:19 +0000 | [diff] [blame] | 71 | void openFile(); |
Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 72 | void writeHeader(); |
| 73 | void writeSections(); |
George Rimar | 86ce267 | 2016-08-25 09:05:47 +0000 | [diff] [blame] | 74 | void writeSectionsBinary(); |
Rui Ueyama | 634ddf0 | 2016-03-11 20:51:53 +0000 | [diff] [blame] | 75 | void writeBuildId(); |
Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 76 | |
Rui Ueyama | 818bb2f | 2016-07-16 18:55:47 +0000 | [diff] [blame] | 77 | std::unique_ptr<FileOutputBuffer> Buffer; |
Michael J. Spencer | 2f00824 | 2015-09-17 19:58:07 +0000 | [diff] [blame] | 78 | |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 79 | std::vector<OutputSectionBase *> OutputSections; |
Eugene Leviant | e63d81b | 2016-07-20 14:43:20 +0000 | [diff] [blame] | 80 | OutputSectionFactory<ELFT> Factory; |
Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 81 | |
Rui Ueyama | 0168722 | 2015-12-26 09:47:57 +0000 | [diff] [blame] | 82 | void addRelIpltSymbols(); |
Rui Ueyama | a5d79d1 | 2015-12-26 09:48:00 +0000 | [diff] [blame] | 83 | void addStartEndSymbols(); |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 84 | void addStartStopSymbols(OutputSectionBase *Sec); |
Rui Ueyama | 2eda6d1 | 2016-11-23 22:41:00 +0000 | [diff] [blame] | 85 | uintX_t getEntryAddr(); |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 86 | OutputSectionBase *findSection(StringRef Name); |
Rui Ueyama | 2f1b79f | 2015-10-10 22:34:30 +0000 | [diff] [blame] | 87 | |
Rafael Espindola | 17cb7c0 | 2016-12-19 17:01:01 +0000 | [diff] [blame] | 88 | std::vector<PhdrEntry> Phdrs; |
Michael J. Spencer | 2f00824 | 2015-09-17 19:58:07 +0000 | [diff] [blame] | 89 | |
Rafael Espindola | 98f6bd0 | 2015-08-11 23:14:13 +0000 | [diff] [blame] | 90 | uintX_t FileSize; |
Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 91 | uintX_t SectionHeaderOff; |
Rafael Espindola | 5967c97 | 2016-12-19 21:21:07 +0000 | [diff] [blame] | 92 | bool AllocateHeader = true; |
Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 93 | }; |
| 94 | } // anonymous namespace |
| 95 | |
Rui Ueyama | 55518e7 | 2016-10-28 20:57:25 +0000 | [diff] [blame] | 96 | StringRef elf::getOutputSectionName(StringRef Name) { |
Eugene Leviant | a8d12ef | 2016-10-05 10:10:45 +0000 | [diff] [blame] | 97 | if (Config->Relocatable) |
| 98 | return Name; |
| 99 | |
Rafael Espindola | 7d38273 | 2016-09-19 19:59:21 +0000 | [diff] [blame] | 100 | for (StringRef V : |
| 101 | {".text.", ".rodata.", ".data.rel.ro.", ".data.", ".bss.", |
| 102 | ".init_array.", ".fini_array.", ".ctors.", ".dtors.", ".tbss.", |
| 103 | ".gcc_except_table.", ".tdata.", ".ARM.exidx."}) { |
| 104 | StringRef Prefix = V.drop_back(); |
| 105 | if (Name.startswith(V) || Name == Prefix) |
| 106 | return Prefix; |
| 107 | } |
Rui Ueyama | 0538408 | 2016-10-12 22:36:31 +0000 | [diff] [blame] | 108 | |
Rui Ueyama | e8a6102 | 2016-11-05 23:05:47 +0000 | [diff] [blame] | 109 | // CommonSection is identified as "COMMON" in linker scripts. |
| 110 | // By default, it should go to .bss section. |
| 111 | if (Name == "COMMON") |
| 112 | return ".bss"; |
| 113 | |
Rui Ueyama | 0538408 | 2016-10-12 22:36:31 +0000 | [diff] [blame] | 114 | // ".zdebug_" is a prefix for ZLIB-compressed sections. |
| 115 | // Because we decompressed input sections, we want to remove 'z'. |
| 116 | if (Name.startswith(".zdebug_")) |
Rui Ueyama | 55518e7 | 2016-10-28 20:57:25 +0000 | [diff] [blame] | 117 | return Saver.save(Twine(".") + Name.substr(2)); |
George Rimar | 5d53d1f | 2016-07-12 08:50:42 +0000 | [diff] [blame] | 118 | return Name; |
| 119 | } |
| 120 | |
Rui Ueyama | 96bdd5b | 2016-07-25 22:26:28 +0000 | [diff] [blame] | 121 | template <class ELFT> void elf::reportDiscarded(InputSectionBase<ELFT> *IS) { |
Rafael Espindola | 8f9026b | 2016-11-08 18:23:02 +0000 | [diff] [blame] | 122 | if (!Config->PrintGcSections) |
George Rimar | 5d53d1f | 2016-07-12 08:50:42 +0000 | [diff] [blame] | 123 | return; |
Rafael Espindola | 042a3f2 | 2016-09-08 14:06:08 +0000 | [diff] [blame] | 124 | errs() << "removing unused section from '" << IS->Name << "' in file '" |
| 125 | << IS->getFile()->getName() << "'\n"; |
George Rimar | 5d53d1f | 2016-07-12 08:50:42 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Rui Ueyama | fd03cfd | 2016-07-21 11:01:23 +0000 | [diff] [blame] | 128 | template <class ELFT> static bool needsInterpSection() { |
| 129 | return !Symtab<ELFT>::X->getSharedFiles().empty() && |
Eugene Leviant | f9bc3bd | 2016-08-16 06:40:58 +0000 | [diff] [blame] | 130 | !Config->DynamicLinker.empty() && |
| 131 | !Script<ELFT>::X->ignoreInterpSection(); |
Rui Ueyama | fd03cfd | 2016-07-21 11:01:23 +0000 | [diff] [blame] | 132 | } |
| 133 | |
George Rimar | d8b2776 | 2016-11-14 10:14:18 +0000 | [diff] [blame] | 134 | template <class ELFT> void elf::writeResult() { Writer<ELFT>().run(); } |
Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 135 | |
Rafael Espindola | 074ba93 | 2016-12-06 13:43:34 +0000 | [diff] [blame] | 136 | template <class ELFT> void Writer<ELFT>::removeEmptyPTLoad() { |
Rafael Espindola | 17cb7c0 | 2016-12-19 17:01:01 +0000 | [diff] [blame] | 137 | auto I = std::remove_if(Phdrs.begin(), Phdrs.end(), [&](const PhdrEntry &P) { |
| 138 | if (P.p_type != PT_LOAD) |
Rafael Espindola | 074ba93 | 2016-12-06 13:43:34 +0000 | [diff] [blame] | 139 | return false; |
Rafael Espindola | 4121761 | 2016-12-08 03:17:05 +0000 | [diff] [blame] | 140 | if (!P.First) |
| 141 | return true; |
Rafael Espindola | 074ba93 | 2016-12-06 13:43:34 +0000 | [diff] [blame] | 142 | uintX_t Size = P.Last->Addr + P.Last->Size - P.First->Addr; |
| 143 | return Size == 0; |
| 144 | }); |
| 145 | Phdrs.erase(I, Phdrs.end()); |
| 146 | } |
| 147 | |
Rui Ueyama | f83aca4 | 2016-11-01 23:17:45 +0000 | [diff] [blame] | 148 | // The main function of the writer. |
| 149 | template <class ELFT> void Writer<ELFT>::run() { |
Rui Ueyama | 0b1b695 | 2016-11-21 02:11:05 +0000 | [diff] [blame] | 150 | // Create linker-synthesized sections such as .got or .plt. |
| 151 | // Such sections are of type input section. |
Rui Ueyama | f83aca4 | 2016-11-01 23:17:45 +0000 | [diff] [blame] | 152 | createSyntheticSections(); |
George Rimar | 7beff42 | 2016-11-15 08:19:02 +0000 | [diff] [blame] | 153 | |
Rui Ueyama | 0b1b695 | 2016-11-21 02:11:05 +0000 | [diff] [blame] | 154 | // We need to create some reserved symbols such as _end. Create them. |
George Rimar | 7beff42 | 2016-11-15 08:19:02 +0000 | [diff] [blame] | 155 | if (!Config->Relocatable) |
| 156 | addReservedSymbols(); |
Rui Ueyama | f83aca4 | 2016-11-01 23:17:45 +0000 | [diff] [blame] | 157 | |
Rui Ueyama | 0b1b695 | 2016-11-21 02:11:05 +0000 | [diff] [blame] | 158 | // Some architectures use small displacements for jump instructions. |
| 159 | // It is linker's responsibility to create thunks containing long |
| 160 | // jump instructions if jump targets are too far. Create thunks. |
Rui Ueyama | f83aca4 | 2016-11-01 23:17:45 +0000 | [diff] [blame] | 161 | if (Target->NeedsThunks) |
| 162 | forEachRelSec(createThunks<ELFT>); |
| 163 | |
Rui Ueyama | 0b1b695 | 2016-11-21 02:11:05 +0000 | [diff] [blame] | 164 | // Create output sections. |
Rui Ueyama | f83aca4 | 2016-11-01 23:17:45 +0000 | [diff] [blame] | 165 | Script<ELFT>::X->OutputSections = &OutputSections; |
| 166 | if (ScriptConfig->HasSections) { |
Rui Ueyama | 0b1b695 | 2016-11-21 02:11:05 +0000 | [diff] [blame] | 167 | // If linker script contains SECTIONS commands, let it create sections. |
| 168 | Script<ELFT>::X->processCommands(Factory); |
| 169 | |
| 170 | // Linker scripts may have left some input sections unassigned. |
| 171 | // Assign such sections using the default rule. |
| 172 | Script<ELFT>::X->addOrphanSections(Factory); |
Rui Ueyama | f83aca4 | 2016-11-01 23:17:45 +0000 | [diff] [blame] | 173 | } else { |
Rui Ueyama | 0b1b695 | 2016-11-21 02:11:05 +0000 | [diff] [blame] | 174 | // If linker script does not contain SECTIONS commands, create |
| 175 | // output sections by default rules. We still need to give the |
| 176 | // linker script a chance to run, because it might contain |
| 177 | // non-SECTIONS commands such as ASSERT. |
Rui Ueyama | f83aca4 | 2016-11-01 23:17:45 +0000 | [diff] [blame] | 178 | createSections(); |
| 179 | Script<ELFT>::X->processCommands(Factory); |
| 180 | } |
| 181 | |
| 182 | if (Config->Discard != DiscardPolicy::All) |
| 183 | copyLocalSymbols(); |
| 184 | |
Rui Ueyama | 0b1b695 | 2016-11-21 02:11:05 +0000 | [diff] [blame] | 185 | // Now that we have a complete set of output sections. This function |
| 186 | // completes section contents. For example, we need to add strings |
| 187 | // to the string table, and add entries to .got and .plt. |
| 188 | // finalizeSections does that. |
Rui Ueyama | f83aca4 | 2016-11-01 23:17:45 +0000 | [diff] [blame] | 189 | finalizeSections(); |
Rui Ueyama | f373dd7 | 2016-11-24 01:43:21 +0000 | [diff] [blame] | 190 | if (ErrorCount) |
Rui Ueyama | f83aca4 | 2016-11-01 23:17:45 +0000 | [diff] [blame] | 191 | return; |
| 192 | |
| 193 | if (Config->Relocatable) { |
| 194 | assignFileOffsets(); |
| 195 | } else { |
Rui Ueyama | f83aca4 | 2016-11-01 23:17:45 +0000 | [diff] [blame] | 196 | if (ScriptConfig->HasSections) { |
| 197 | Script<ELFT>::X->assignAddresses(Phdrs); |
| 198 | } else { |
| 199 | fixSectionAlignments(); |
| 200 | assignAddresses(); |
| 201 | } |
| 202 | |
Rafael Espindola | 074ba93 | 2016-12-06 13:43:34 +0000 | [diff] [blame] | 203 | // Remove empty PT_LOAD to avoid causing the dynamic linker to try to mmap a |
| 204 | // 0 sized region. This has to be done late since only after assignAddresses |
| 205 | // we know the size of the sections. |
| 206 | removeEmptyPTLoad(); |
| 207 | |
Rui Ueyama | f83aca4 | 2016-11-01 23:17:45 +0000 | [diff] [blame] | 208 | if (!Config->OFormatBinary) |
| 209 | assignFileOffsets(); |
| 210 | else |
| 211 | assignFileOffsetsBinary(); |
| 212 | |
| 213 | setPhdrs(); |
| 214 | fixAbsoluteSymbols(); |
| 215 | } |
| 216 | |
Rui Ueyama | 0b1b695 | 2016-11-21 02:11:05 +0000 | [diff] [blame] | 217 | // Write the result down to a file. |
Rui Ueyama | f83aca4 | 2016-11-01 23:17:45 +0000 | [diff] [blame] | 218 | openFile(); |
Rui Ueyama | f373dd7 | 2016-11-24 01:43:21 +0000 | [diff] [blame] | 219 | if (ErrorCount) |
Rui Ueyama | f83aca4 | 2016-11-01 23:17:45 +0000 | [diff] [blame] | 220 | return; |
| 221 | if (!Config->OFormatBinary) { |
| 222 | writeHeader(); |
| 223 | writeSections(); |
| 224 | } else { |
| 225 | writeSectionsBinary(); |
| 226 | } |
Rui Ueyama | 0b1b695 | 2016-11-21 02:11:05 +0000 | [diff] [blame] | 227 | |
| 228 | // Backfill .note.gnu.build-id section content. This is done at last |
| 229 | // because the content is usually a hash value of the entire output file. |
Rui Ueyama | f83aca4 | 2016-11-01 23:17:45 +0000 | [diff] [blame] | 230 | writeBuildId(); |
Rui Ueyama | f373dd7 | 2016-11-24 01:43:21 +0000 | [diff] [blame] | 231 | if (ErrorCount) |
Rui Ueyama | f83aca4 | 2016-11-01 23:17:45 +0000 | [diff] [blame] | 232 | return; |
Rui Ueyama | 0b1b695 | 2016-11-21 02:11:05 +0000 | [diff] [blame] | 233 | |
Rui Ueyama | f83aca4 | 2016-11-01 23:17:45 +0000 | [diff] [blame] | 234 | if (auto EC = Buffer->commit()) |
| 235 | error(EC, "failed to write to the output file"); |
Rui Ueyama | 0b1b695 | 2016-11-21 02:11:05 +0000 | [diff] [blame] | 236 | |
| 237 | // Flush the output streams and exit immediately. A full shutdown |
| 238 | // is a good test that we are keeping track of all allocated memory, |
| 239 | // but actually freeing it is a waste of time in a regular linker run. |
| 240 | if (Config->ExitEarly) |
Rui Ueyama | f83aca4 | 2016-11-01 23:17:45 +0000 | [diff] [blame] | 241 | exitLld(0); |
Rui Ueyama | f83aca4 | 2016-11-01 23:17:45 +0000 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | // Initialize Out<ELFT> members. |
| 245 | template <class ELFT> void Writer<ELFT>::createSyntheticSections() { |
| 246 | // Initialize all pointers with NULL. This is needed because |
| 247 | // you can call lld::elf::main more than once as a library. |
Rui Ueyama | cfadbd9 | 2016-11-01 23:12:51 +0000 | [diff] [blame] | 248 | memset(&Out<ELFT>::First, 0, sizeof(Out<ELFT>)); |
| 249 | |
Rui Ueyama | 4197a6a | 2016-02-05 18:41:40 +0000 | [diff] [blame] | 250 | // Create singleton output sections. |
Rui Ueyama | cfadbd9 | 2016-11-01 23:12:51 +0000 | [diff] [blame] | 251 | Out<ELFT>::Bss = |
| 252 | make<OutputSection<ELFT>>(".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE); |
Peter Collingbourne | feb6629 | 2017-01-10 01:21:50 +0000 | [diff] [blame^] | 253 | Out<ELFT>::BssRelRo = make<OutputSection<ELFT>>(".bss.rel.ro", SHT_NOBITS, |
| 254 | SHF_ALLOC | SHF_WRITE); |
Eugene Leviant | 22eb026 | 2016-11-14 09:16:00 +0000 | [diff] [blame] | 255 | In<ELFT>::DynStrTab = make<StringTableSection<ELFT>>(".dynstr", true); |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 256 | In<ELFT>::Dynamic = make<DynamicSection<ELFT>>(); |
Rui Ueyama | cfadbd9 | 2016-11-01 23:12:51 +0000 | [diff] [blame] | 257 | Out<ELFT>::EhFrame = make<EhOutputSection<ELFT>>(); |
Eugene Leviant | a96d902 | 2016-11-16 10:02:27 +0000 | [diff] [blame] | 258 | In<ELFT>::RelaDyn = make<RelocationSection<ELFT>>( |
Rui Ueyama | cfadbd9 | 2016-11-01 23:12:51 +0000 | [diff] [blame] | 259 | Config->Rela ? ".rela.dyn" : ".rel.dyn", Config->ZCombreloc); |
Eugene Leviant | 22eb026 | 2016-11-14 09:16:00 +0000 | [diff] [blame] | 260 | In<ELFT>::ShStrTab = make<StringTableSection<ELFT>>(".shstrtab", false); |
Rui Ueyama | 4197a6a | 2016-02-05 18:41:40 +0000 | [diff] [blame] | 261 | |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 262 | Out<ELFT>::ElfHeader = make<OutputSectionBase>("", 0, SHF_ALLOC); |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 263 | Out<ELFT>::ElfHeader->Size = sizeof(Elf_Ehdr); |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 264 | Out<ELFT>::ProgramHeaders = make<OutputSectionBase>("", 0, SHF_ALLOC); |
Rui Ueyama | cfadbd9 | 2016-11-01 23:12:51 +0000 | [diff] [blame] | 265 | Out<ELFT>::ProgramHeaders->updateAlignment(sizeof(uintX_t)); |
Rui Ueyama | 4197a6a | 2016-02-05 18:41:40 +0000 | [diff] [blame] | 266 | |
Rui Ueyama | e8a6102 | 2016-11-05 23:05:47 +0000 | [diff] [blame] | 267 | if (needsInterpSection<ELFT>()) { |
Rafael Espindola | c0e47fb | 2016-11-08 14:56:27 +0000 | [diff] [blame] | 268 | In<ELFT>::Interp = createInterpSection<ELFT>(); |
Rui Ueyama | e8a6102 | 2016-11-05 23:05:47 +0000 | [diff] [blame] | 269 | Symtab<ELFT>::X->Sections.push_back(In<ELFT>::Interp); |
| 270 | } else { |
| 271 | In<ELFT>::Interp = nullptr; |
| 272 | } |
Rui Ueyama | 3a41be2 | 2016-04-07 22:49:21 +0000 | [diff] [blame] | 273 | |
George Rimar | 3fb5a6d | 2016-11-29 16:05:27 +0000 | [diff] [blame] | 274 | if (!Config->Relocatable) |
| 275 | Symtab<ELFT>::X->Sections.push_back(createCommentSection<ELFT>()); |
Rui Ueyama | 3b31e67 | 2016-05-23 16:24:16 +0000 | [diff] [blame] | 276 | |
George Rimar | f21aade | 2016-08-31 08:38:11 +0000 | [diff] [blame] | 277 | if (Config->Strip != StripPolicy::All) { |
Eugene Leviant | 22eb026 | 2016-11-14 09:16:00 +0000 | [diff] [blame] | 278 | In<ELFT>::StrTab = make<StringTableSection<ELFT>>(".strtab", false); |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 279 | In<ELFT>::SymTab = make<SymbolTableSection<ELFT>>(*In<ELFT>::StrTab); |
Rui Ueyama | 4197a6a | 2016-02-05 18:41:40 +0000 | [diff] [blame] | 280 | } |
Rui Ueyama | cfadbd9 | 2016-11-01 23:12:51 +0000 | [diff] [blame] | 281 | |
Rui Ueyama | c4030a1 | 2016-11-22 00:54:15 +0000 | [diff] [blame] | 282 | if (Config->BuildId != BuildIdKind::None) { |
| 283 | In<ELFT>::BuildId = make<BuildIdSection<ELFT>>(); |
Rui Ueyama | e8a6102 | 2016-11-05 23:05:47 +0000 | [diff] [blame] | 284 | Symtab<ELFT>::X->Sections.push_back(In<ELFT>::BuildId); |
Rui Ueyama | c4030a1 | 2016-11-22 00:54:15 +0000 | [diff] [blame] | 285 | } |
Rui Ueyama | e8a6102 | 2016-11-05 23:05:47 +0000 | [diff] [blame] | 286 | |
Rafael Espindola | 682a5bc | 2016-11-08 14:42:34 +0000 | [diff] [blame] | 287 | InputSection<ELFT> *Common = createCommonSection<ELFT>(); |
Rui Ueyama | e8a6102 | 2016-11-05 23:05:47 +0000 | [diff] [blame] | 288 | if (!Common->Data.empty()) { |
| 289 | In<ELFT>::Common = Common; |
| 290 | Symtab<ELFT>::X->Sections.push_back(Common); |
| 291 | } |
Simon Atanasyan | ce02cf0 | 2016-11-09 21:36:56 +0000 | [diff] [blame] | 292 | |
Rui Ueyama | 1d75de0 | 2016-11-22 04:28:39 +0000 | [diff] [blame] | 293 | // Add MIPS-specific sections. |
George Rimar | 11992c86 | 2016-11-25 08:05:41 +0000 | [diff] [blame] | 294 | bool HasDynSymTab = !Symtab<ELFT>::X->getSharedFiles().empty() || Config->Pic; |
Simon Atanasyan | ce02cf0 | 2016-11-09 21:36:56 +0000 | [diff] [blame] | 295 | if (Config->EMachine == EM_MIPS) { |
George Rimar | 11992c86 | 2016-11-25 08:05:41 +0000 | [diff] [blame] | 296 | if (!Config->Shared && HasDynSymTab) { |
Rui Ueyama | bdfa155 | 2016-11-22 19:24:52 +0000 | [diff] [blame] | 297 | In<ELFT>::MipsRldMap = make<MipsRldMapSection<ELFT>>(); |
Eugene Leviant | 17b7a57 | 2016-11-22 17:49:14 +0000 | [diff] [blame] | 298 | Symtab<ELFT>::X->Sections.push_back(In<ELFT>::MipsRldMap); |
| 299 | } |
Rui Ueyama | 1d75de0 | 2016-11-22 04:28:39 +0000 | [diff] [blame] | 300 | if (auto *Sec = MipsAbiFlagsSection<ELFT>::create()) |
Rui Ueyama | 12f2da8 | 2016-11-22 03:57:06 +0000 | [diff] [blame] | 301 | Symtab<ELFT>::X->Sections.push_back(Sec); |
Rui Ueyama | 1d75de0 | 2016-11-22 04:28:39 +0000 | [diff] [blame] | 302 | if (auto *Sec = MipsOptionsSection<ELFT>::create()) |
Rui Ueyama | 9cfac8a | 2016-11-22 04:13:09 +0000 | [diff] [blame] | 303 | Symtab<ELFT>::X->Sections.push_back(Sec); |
Rui Ueyama | 1d75de0 | 2016-11-22 04:28:39 +0000 | [diff] [blame] | 304 | if (auto *Sec = MipsReginfoSection<ELFT>::create()) |
Rui Ueyama | b71cae9 | 2016-11-22 03:57:08 +0000 | [diff] [blame] | 305 | Symtab<ELFT>::X->Sections.push_back(Sec); |
Simon Atanasyan | ce02cf0 | 2016-11-09 21:36:56 +0000 | [diff] [blame] | 306 | } |
Eugene Leviant | 41ca327 | 2016-11-10 09:48:29 +0000 | [diff] [blame] | 307 | |
George Rimar | 11992c86 | 2016-11-25 08:05:41 +0000 | [diff] [blame] | 308 | if (HasDynSymTab) { |
| 309 | In<ELFT>::DynSymTab = make<SymbolTableSection<ELFT>>(*In<ELFT>::DynStrTab); |
| 310 | Symtab<ELFT>::X->Sections.push_back(In<ELFT>::DynSymTab); |
| 311 | |
| 312 | In<ELFT>::VerSym = make<VersionTableSection<ELFT>>(); |
| 313 | Symtab<ELFT>::X->Sections.push_back(In<ELFT>::VerSym); |
| 314 | |
| 315 | if (!Config->VersionDefinitions.empty()) { |
| 316 | In<ELFT>::VerDef = make<VersionDefinitionSection<ELFT>>(); |
| 317 | Symtab<ELFT>::X->Sections.push_back(In<ELFT>::VerDef); |
| 318 | } |
| 319 | |
| 320 | In<ELFT>::VerNeed = make<VersionNeedSection<ELFT>>(); |
| 321 | Symtab<ELFT>::X->Sections.push_back(In<ELFT>::VerNeed); |
| 322 | |
| 323 | if (Config->GnuHash) { |
| 324 | In<ELFT>::GnuHashTab = make<GnuHashTableSection<ELFT>>(); |
| 325 | Symtab<ELFT>::X->Sections.push_back(In<ELFT>::GnuHashTab); |
| 326 | } |
| 327 | |
| 328 | if (Config->SysvHash) { |
| 329 | In<ELFT>::HashTab = make<HashTableSection<ELFT>>(); |
| 330 | Symtab<ELFT>::X->Sections.push_back(In<ELFT>::HashTab); |
| 331 | } |
| 332 | |
| 333 | Symtab<ELFT>::X->Sections.push_back(In<ELFT>::Dynamic); |
| 334 | Symtab<ELFT>::X->Sections.push_back(In<ELFT>::DynStrTab); |
| 335 | Symtab<ELFT>::X->Sections.push_back(In<ELFT>::RelaDyn); |
| 336 | } |
| 337 | |
Rui Ueyama | 1d75de0 | 2016-11-22 04:28:39 +0000 | [diff] [blame] | 338 | // Add .got. MIPS' .got is so different from the other archs, |
| 339 | // it has its own class. |
George Rimar | 11992c86 | 2016-11-25 08:05:41 +0000 | [diff] [blame] | 340 | if (Config->EMachine == EM_MIPS) { |
Simon Atanasyan | 725dc14 | 2016-11-16 21:01:02 +0000 | [diff] [blame] | 341 | In<ELFT>::MipsGot = make<MipsGotSection<ELFT>>(); |
George Rimar | 11992c86 | 2016-11-25 08:05:41 +0000 | [diff] [blame] | 342 | Symtab<ELFT>::X->Sections.push_back(In<ELFT>::MipsGot); |
| 343 | } else { |
Simon Atanasyan | 725dc14 | 2016-11-16 21:01:02 +0000 | [diff] [blame] | 344 | In<ELFT>::Got = make<GotSection<ELFT>>(); |
George Rimar | 11992c86 | 2016-11-25 08:05:41 +0000 | [diff] [blame] | 345 | Symtab<ELFT>::X->Sections.push_back(In<ELFT>::Got); |
| 346 | } |
Simon Atanasyan | 725dc14 | 2016-11-16 21:01:02 +0000 | [diff] [blame] | 347 | |
Eugene Leviant | 41ca327 | 2016-11-10 09:48:29 +0000 | [diff] [blame] | 348 | In<ELFT>::GotPlt = make<GotPltSection<ELFT>>(); |
George Rimar | 11992c86 | 2016-11-25 08:05:41 +0000 | [diff] [blame] | 349 | Symtab<ELFT>::X->Sections.push_back(In<ELFT>::GotPlt); |
Peter Smith | baffdb8 | 2016-12-08 12:58:55 +0000 | [diff] [blame] | 350 | In<ELFT>::IgotPlt = make<IgotPltSection<ELFT>>(); |
| 351 | Symtab<ELFT>::X->Sections.push_back(In<ELFT>::IgotPlt); |
George Rimar | 3fb5a6d | 2016-11-29 16:05:27 +0000 | [diff] [blame] | 352 | |
| 353 | if (Config->GdbIndex) { |
| 354 | In<ELFT>::GdbIndex = make<GdbIndexSection<ELFT>>(); |
| 355 | Symtab<ELFT>::X->Sections.push_back(In<ELFT>::GdbIndex); |
| 356 | } |
| 357 | |
| 358 | // We always need to add rel[a].plt to output if it has entries. |
| 359 | // Even for static linking it can contain R_[*]_IRELATIVE relocations. |
| 360 | In<ELFT>::RelaPlt = make<RelocationSection<ELFT>>( |
| 361 | Config->Rela ? ".rela.plt" : ".rel.plt", false /*Sort*/); |
| 362 | Symtab<ELFT>::X->Sections.push_back(In<ELFT>::RelaPlt); |
| 363 | |
Peter Smith | baffdb8 | 2016-12-08 12:58:55 +0000 | [diff] [blame] | 364 | // The RelaIplt immediately follows .rel.plt (.rel.dyn for ARM) to ensure |
| 365 | // that the IRelative relocations are processed last by the dynamic loader |
| 366 | In<ELFT>::RelaIplt = make<RelocationSection<ELFT>>( |
| 367 | (Config->EMachine == EM_ARM) ? ".rel.dyn" : In<ELFT>::RelaPlt->Name, |
| 368 | false /*Sort*/); |
| 369 | Symtab<ELFT>::X->Sections.push_back(In<ELFT>::RelaIplt); |
| 370 | |
George Rimar | 3fb5a6d | 2016-11-29 16:05:27 +0000 | [diff] [blame] | 371 | In<ELFT>::Plt = make<PltSection<ELFT>>(); |
| 372 | Symtab<ELFT>::X->Sections.push_back(In<ELFT>::Plt); |
Peter Smith | baffdb8 | 2016-12-08 12:58:55 +0000 | [diff] [blame] | 373 | In<ELFT>::Iplt = make<IpltSection<ELFT>>(); |
| 374 | Symtab<ELFT>::X->Sections.push_back(In<ELFT>::Iplt); |
George Rimar | 3fb5a6d | 2016-11-29 16:05:27 +0000 | [diff] [blame] | 375 | |
| 376 | if (Config->EhFrameHdr) { |
| 377 | In<ELFT>::EhFrameHdr = make<EhFrameHeader<ELFT>>(); |
| 378 | Symtab<ELFT>::X->Sections.push_back(In<ELFT>::EhFrameHdr); |
| 379 | } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Rafael Espindola | 10d71ff | 2016-01-27 18:04:26 +0000 | [diff] [blame] | 382 | template <class ELFT> |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 383 | static bool shouldKeepInSymtab(InputSectionBase<ELFT> *Sec, StringRef SymName, |
| 384 | const SymbolBody &B) { |
| 385 | if (B.isFile()) |
Rafael Espindola | 10d71ff | 2016-01-27 18:04:26 +0000 | [diff] [blame] | 386 | return false; |
| 387 | |
George Rimar | 4cfe572 | 2016-03-03 07:49:35 +0000 | [diff] [blame] | 388 | // We keep sections in symtab for relocatable output. |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 389 | if (B.isSection()) |
George Rimar | 4cfe572 | 2016-03-03 07:49:35 +0000 | [diff] [blame] | 390 | return Config->Relocatable; |
| 391 | |
Rafael Espindola | 10d71ff | 2016-01-27 18:04:26 +0000 | [diff] [blame] | 392 | // If sym references a section in a discarded group, don't keep it. |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 393 | if (Sec == &InputSection<ELFT>::Discarded) |
Rafael Espindola | 10d71ff | 2016-01-27 18:04:26 +0000 | [diff] [blame] | 394 | return false; |
| 395 | |
George Rimar | 9503f6d | 2016-08-31 08:46:30 +0000 | [diff] [blame] | 396 | if (Config->Discard == DiscardPolicy::None) |
Rafael Espindola | 10d71ff | 2016-01-27 18:04:26 +0000 | [diff] [blame] | 397 | return true; |
| 398 | |
| 399 | // In ELF assembly .L symbols are normally discarded by the assembler. |
| 400 | // If the assembler fails to do so, the linker discards them if |
| 401 | // * --discard-locals is used. |
| 402 | // * The symbol is in a SHF_MERGE section, which is normally the reason for |
| 403 | // the assembler keeping the .L symbol. |
| 404 | if (!SymName.startswith(".L") && !SymName.empty()) |
| 405 | return true; |
| 406 | |
George Rimar | 9503f6d | 2016-08-31 08:46:30 +0000 | [diff] [blame] | 407 | if (Config->Discard == DiscardPolicy::Locals) |
Rafael Espindola | 10d71ff | 2016-01-27 18:04:26 +0000 | [diff] [blame] | 408 | return false; |
| 409 | |
Rafael Espindola | 1854a8e | 2016-10-26 12:36:56 +0000 | [diff] [blame] | 410 | return !Sec || !(Sec->Flags & SHF_MERGE); |
Rafael Espindola | 10d71ff | 2016-01-27 18:04:26 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Rafael Espindola | 474eb01 | 2016-05-05 16:40:28 +0000 | [diff] [blame] | 413 | template <class ELFT> static bool includeInSymtab(const SymbolBody &B) { |
| 414 | if (!B.isLocal() && !B.symbol()->IsUsedInRegularObj) |
| 415 | return false; |
| 416 | |
George Rimar | 2bb88ab | 2016-12-19 18:00:52 +0000 | [diff] [blame] | 417 | // If --retain-symbols-file is given, we'll keep only symbols listed in that |
| 418 | // file. |
| 419 | if (Config->Discard == DiscardPolicy::RetainFile && |
| 420 | !Config->RetainSymbolsFile.count(B.getName())) |
| 421 | return false; |
| 422 | |
Rafael Espindola | 474eb01 | 2016-05-05 16:40:28 +0000 | [diff] [blame] | 423 | if (auto *D = dyn_cast<DefinedRegular<ELFT>>(&B)) { |
| 424 | // Always include absolute symbols. |
| 425 | if (!D->Section) |
| 426 | return true; |
| 427 | // Exclude symbols pointing to garbage-collected sections. |
| 428 | if (!D->Section->Live) |
| 429 | return false; |
| 430 | if (auto *S = dyn_cast<MergeInputSection<ELFT>>(D->Section)) |
Rui Ueyama | 90fa372 | 2016-05-22 00:41:38 +0000 | [diff] [blame] | 431 | if (!S->getSectionPiece(D->Value)->Live) |
Rafael Espindola | 474eb01 | 2016-05-05 16:40:28 +0000 | [diff] [blame] | 432 | return false; |
| 433 | } |
| 434 | return true; |
| 435 | } |
Rafael Espindola | 462220d | 2016-05-05 16:38:46 +0000 | [diff] [blame] | 436 | |
Rui Ueyama | 5a9640b | 2015-10-08 23:49:30 +0000 | [diff] [blame] | 437 | // Local symbols are not in the linker's symbol table. This function scans |
| 438 | // each object file's symbol table to copy local symbols to the output. |
| 439 | template <class ELFT> void Writer<ELFT>::copyLocalSymbols() { |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 440 | if (!In<ELFT>::SymTab) |
Rui Ueyama | 90f76fb | 2016-01-21 03:07:38 +0000 | [diff] [blame] | 441 | return; |
Rui Ueyama | 38dbd3e | 2016-09-14 00:05:51 +0000 | [diff] [blame] | 442 | for (elf::ObjectFile<ELFT> *F : Symtab<ELFT>::X->getObjectFiles()) { |
Rafael Espindola | 67d72c0 | 2016-03-11 12:06:30 +0000 | [diff] [blame] | 443 | for (SymbolBody *B : F->getLocalSymbols()) { |
George Rimar | 78fe56e | 2016-10-11 09:07:14 +0000 | [diff] [blame] | 444 | if (!B->IsLocal) |
Rui Ueyama | 3fc0f7e | 2016-11-23 18:07:33 +0000 | [diff] [blame] | 445 | fatal(toString(F) + |
George Rimar | 78fe56e | 2016-10-11 09:07:14 +0000 | [diff] [blame] | 446 | ": broken object: getLocalSymbols returns a non-local symbol"); |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 447 | auto *DR = dyn_cast<DefinedRegular<ELFT>>(B); |
Rui Ueyama | 3fc0f7e | 2016-11-23 18:07:33 +0000 | [diff] [blame] | 448 | |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 449 | // No reason to keep local undefined symbol in symtab. |
| 450 | if (!DR) |
Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 451 | continue; |
Rafael Espindola | 462220d | 2016-05-05 16:38:46 +0000 | [diff] [blame] | 452 | if (!includeInSymtab<ELFT>(*B)) |
| 453 | continue; |
Rui Ueyama | 3fc0f7e | 2016-11-23 18:07:33 +0000 | [diff] [blame] | 454 | |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 455 | InputSectionBase<ELFT> *Sec = DR->Section; |
Rui Ueyama | c72ba3a | 2016-11-23 04:57:25 +0000 | [diff] [blame] | 456 | if (!shouldKeepInSymtab<ELFT>(Sec, B->getName(), *B)) |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 457 | continue; |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 458 | ++In<ELFT>::SymTab->NumLocals; |
George Rimar | 4cfe572 | 2016-03-03 07:49:35 +0000 | [diff] [blame] | 459 | if (Config->Relocatable) |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 460 | B->DynsymIndex = In<ELFT>::SymTab->NumLocals; |
Rui Ueyama | c72ba3a | 2016-11-23 04:57:25 +0000 | [diff] [blame] | 461 | F->KeptLocalSyms.push_back(std::make_pair( |
| 462 | DR, In<ELFT>::SymTab->StrTabSec.addString(B->getName()))); |
Rui Ueyama | 5a9640b | 2015-10-08 23:49:30 +0000 | [diff] [blame] | 463 | } |
| 464 | } |
| 465 | } |
| 466 | |
Hal Finkel | 3bae2d8 | 2015-10-12 20:51:48 +0000 | [diff] [blame] | 467 | // PPC64 has a number of special SHT_PROGBITS+SHF_ALLOC+SHF_WRITE sections that |
| 468 | // we would like to make sure appear is a specific order to maximize their |
| 469 | // coverage by a single signed 16-bit offset from the TOC base pointer. |
| 470 | // Conversely, the special .tocbss section should be first among all SHT_NOBITS |
| 471 | // sections. This will put it next to the loaded special PPC64 sections (and, |
| 472 | // thus, within reach of the TOC base pointer). |
| 473 | static int getPPC64SectionRank(StringRef SectionName) { |
| 474 | return StringSwitch<int>(SectionName) |
George Rimar | ee741cf | 2016-04-14 13:23:02 +0000 | [diff] [blame] | 475 | .Case(".tocbss", 0) |
| 476 | .Case(".branch_lt", 2) |
| 477 | .Case(".toc", 3) |
| 478 | .Case(".toc1", 4) |
| 479 | .Case(".opd", 5) |
| 480 | .Default(1); |
Hal Finkel | 3bae2d8 | 2015-10-12 20:51:48 +0000 | [diff] [blame] | 481 | } |
| 482 | |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 483 | template <class ELFT> bool elf::isRelroSection(const OutputSectionBase *Sec) { |
Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 484 | if (!Config->ZRelro) |
| 485 | return false; |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 486 | uint64_t Flags = Sec->Flags; |
George Rimar | e3336c0 | 2015-11-24 10:15:50 +0000 | [diff] [blame] | 487 | if (!(Flags & SHF_ALLOC) || !(Flags & SHF_WRITE)) |
| 488 | return false; |
Rui Ueyama | ccfc326 | 2015-12-10 19:13:08 +0000 | [diff] [blame] | 489 | if (Flags & SHF_TLS) |
| 490 | return true; |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 491 | uint32_t Type = Sec->Type; |
Rui Ueyama | ccfc326 | 2015-12-10 19:13:08 +0000 | [diff] [blame] | 492 | if (Type == SHT_INIT_ARRAY || Type == SHT_FINI_ARRAY || |
| 493 | Type == SHT_PREINIT_ARRAY) |
George Rimar | e3336c0 | 2015-11-24 10:15:50 +0000 | [diff] [blame] | 494 | return true; |
Eugene Leviant | 41ca327 | 2016-11-10 09:48:29 +0000 | [diff] [blame] | 495 | if (Sec == In<ELFT>::GotPlt->OutSec) |
George Rimar | e3336c0 | 2015-11-24 10:15:50 +0000 | [diff] [blame] | 496 | return Config->ZNow; |
Simon Atanasyan | 725dc14 | 2016-11-16 21:01:02 +0000 | [diff] [blame] | 497 | if (Sec == In<ELFT>::Dynamic->OutSec) |
| 498 | return true; |
| 499 | if (In<ELFT>::Got && Sec == In<ELFT>::Got->OutSec) |
| 500 | return true; |
| 501 | if (In<ELFT>::MipsGot && Sec == In<ELFT>::MipsGot->OutSec) |
George Rimar | e3336c0 | 2015-11-24 10:15:50 +0000 | [diff] [blame] | 502 | return true; |
Peter Collingbourne | feb6629 | 2017-01-10 01:21:50 +0000 | [diff] [blame^] | 503 | if (Sec == Out<ELFT>::BssRelRo) |
| 504 | return true; |
Rui Ueyama | 01faef0 | 2015-12-10 19:19:04 +0000 | [diff] [blame] | 505 | StringRef S = Sec->getName(); |
| 506 | return S == ".data.rel.ro" || S == ".ctors" || S == ".dtors" || S == ".jcr" || |
George Rimar | d003c7f | 2016-12-20 11:28:54 +0000 | [diff] [blame] | 507 | S == ".eh_frame" || S == ".openbsd.randomdata"; |
George Rimar | e3336c0 | 2015-11-24 10:15:50 +0000 | [diff] [blame] | 508 | } |
| 509 | |
Rui Ueyama | 5a9640b | 2015-10-08 23:49:30 +0000 | [diff] [blame] | 510 | template <class ELFT> |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 511 | static bool compareSectionsNonScript(const OutputSectionBase *A, |
| 512 | const OutputSectionBase *B) { |
Rui Ueyama | e288eef | 2016-11-02 18:58:44 +0000 | [diff] [blame] | 513 | // Put .interp first because some loaders want to see that section |
| 514 | // on the first page of the executable file when loaded into memory. |
| 515 | bool AIsInterp = A->getName() == ".interp"; |
| 516 | bool BIsInterp = B->getName() == ".interp"; |
| 517 | if (AIsInterp != BIsInterp) |
| 518 | return AIsInterp; |
| 519 | |
Rui Ueyama | 5a9640b | 2015-10-08 23:49:30 +0000 | [diff] [blame] | 520 | // Allocatable sections go first to reduce the total PT_LOAD size and |
| 521 | // so debug info doesn't change addresses in actual code. |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 522 | bool AIsAlloc = A->Flags & SHF_ALLOC; |
| 523 | bool BIsAlloc = B->Flags & SHF_ALLOC; |
Rui Ueyama | 5a9640b | 2015-10-08 23:49:30 +0000 | [diff] [blame] | 524 | if (AIsAlloc != BIsAlloc) |
| 525 | return AIsAlloc; |
| 526 | |
Rafael Espindola | b6b8f6c | 2016-09-20 22:43:15 +0000 | [diff] [blame] | 527 | // We don't have any special requirements for the relative order of two non |
| 528 | // allocatable sections. |
Rui Ueyama | 5a9640b | 2015-10-08 23:49:30 +0000 | [diff] [blame] | 529 | if (!AIsAlloc) |
Rafael Espindola | 24c073d | 2016-09-21 22:36:19 +0000 | [diff] [blame] | 530 | return false; |
Rui Ueyama | 5a9640b | 2015-10-08 23:49:30 +0000 | [diff] [blame] | 531 | |
Rafael Espindola | 5967c97 | 2016-12-19 21:21:07 +0000 | [diff] [blame] | 532 | // We want to put section specified by -T option first, so we |
| 533 | // can start assigning VA starting from them later. |
| 534 | auto AAddrSetI = Config->SectionStartMap.find(A->getName()); |
| 535 | auto BAddrSetI = Config->SectionStartMap.find(B->getName()); |
| 536 | bool AHasAddrSet = AAddrSetI != Config->SectionStartMap.end(); |
| 537 | bool BHasAddrSet = BAddrSetI != Config->SectionStartMap.end(); |
| 538 | if (AHasAddrSet != BHasAddrSet) |
| 539 | return AHasAddrSet; |
| 540 | if (AHasAddrSet) |
| 541 | return AAddrSetI->second < BAddrSetI->second; |
| 542 | |
Rui Ueyama | 5a9640b | 2015-10-08 23:49:30 +0000 | [diff] [blame] | 543 | // We want the read only sections first so that they go in the PT_LOAD |
| 544 | // covering the program headers at the start of the file. |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 545 | bool AIsWritable = A->Flags & SHF_WRITE; |
| 546 | bool BIsWritable = B->Flags & SHF_WRITE; |
Rui Ueyama | 5a9640b | 2015-10-08 23:49:30 +0000 | [diff] [blame] | 547 | if (AIsWritable != BIsWritable) |
| 548 | return BIsWritable; |
| 549 | |
George Rimar | 1642c5d | 2016-11-28 10:26:21 +0000 | [diff] [blame] | 550 | if (!Config->SingleRoRx) { |
Rafael Espindola | e979fd1 | 2016-09-29 22:48:55 +0000 | [diff] [blame] | 551 | // For a corresponding reason, put non exec sections first (the program |
| 552 | // header PT_LOAD is not executable). |
| 553 | // We only do that if we are not using linker scripts, since with linker |
| 554 | // scripts ro and rx sections are in the same PT_LOAD, so their relative |
George Rimar | 1642c5d | 2016-11-28 10:26:21 +0000 | [diff] [blame] | 555 | // order is not important. The same applies for -no-rosegment. |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 556 | bool AIsExec = A->Flags & SHF_EXECINSTR; |
| 557 | bool BIsExec = B->Flags & SHF_EXECINSTR; |
Rafael Espindola | e979fd1 | 2016-09-29 22:48:55 +0000 | [diff] [blame] | 558 | if (AIsExec != BIsExec) |
| 559 | return BIsExec; |
| 560 | } |
Rui Ueyama | 5a9640b | 2015-10-08 23:49:30 +0000 | [diff] [blame] | 561 | |
Hal Finkel | 0d7e83b | 2015-10-13 17:57:46 +0000 | [diff] [blame] | 562 | // If we got here we know that both A and B are in the same PT_LOAD. |
Michael J. Spencer | 1bf7300 | 2015-10-16 23:11:07 +0000 | [diff] [blame] | 563 | |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 564 | bool AIsTls = A->Flags & SHF_TLS; |
| 565 | bool BIsTls = B->Flags & SHF_TLS; |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 566 | bool AIsNoBits = A->Type == SHT_NOBITS; |
| 567 | bool BIsNoBits = B->Type == SHT_NOBITS; |
Hal Finkel | 3bae2d8 | 2015-10-12 20:51:48 +0000 | [diff] [blame] | 568 | |
Peter Collingbourne | 628ec9f | 2017-01-10 01:21:30 +0000 | [diff] [blame] | 569 | // The first requirement we have is to put (non-TLS) nobits sections last. The |
| 570 | // reason is that the only thing the dynamic linker will see about them is a |
| 571 | // p_memsz that is larger than p_filesz. Seeing that it zeros the end of the |
| 572 | // PT_LOAD, so that has to correspond to the nobits sections. |
| 573 | bool AIsNonTlsNoBits = AIsNoBits && !AIsTls; |
| 574 | bool BIsNonTlsNoBits = BIsNoBits && !BIsTls; |
| 575 | if (AIsNonTlsNoBits != BIsNonTlsNoBits) |
| 576 | return BIsNonTlsNoBits; |
| 577 | |
| 578 | // We place nobits RelRo sections before plain r/w ones, and non-nobits RelRo |
| 579 | // sections after r/w ones, so that the RelRo sections are contiguous. |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 580 | bool AIsRelRo = isRelroSection<ELFT>(A); |
| 581 | bool BIsRelRo = isRelroSection<ELFT>(B); |
George Rimar | e3336c0 | 2015-11-24 10:15:50 +0000 | [diff] [blame] | 582 | if (AIsRelRo != BIsRelRo) |
Peter Collingbourne | 628ec9f | 2017-01-10 01:21:30 +0000 | [diff] [blame] | 583 | return AIsNonTlsNoBits ? AIsRelRo : BIsRelRo; |
| 584 | |
| 585 | // The TLS initialization block needs to be a single contiguous block in a R/W |
| 586 | // PT_LOAD, so stick TLS sections directly before the other RelRo R/W |
| 587 | // sections. The TLS NOBITS sections are placed here as they don't take up |
| 588 | // virtual address space in the PT_LOAD. |
| 589 | if (AIsTls != BIsTls) |
| 590 | return AIsTls; |
| 591 | |
| 592 | // Within the TLS initialization block, the non-nobits sections need to appear |
| 593 | // first. |
| 594 | if (AIsNoBits != BIsNoBits) |
| 595 | return BIsNoBits; |
George Rimar | e3336c0 | 2015-11-24 10:15:50 +0000 | [diff] [blame] | 596 | |
Hal Finkel | 9abc2a5 | 2015-10-13 19:07:29 +0000 | [diff] [blame] | 597 | // Some architectures have additional ordering restrictions for sections |
| 598 | // within the same PT_LOAD. |
| 599 | if (Config->EMachine == EM_PPC64) |
| 600 | return getPPC64SectionRank(A->getName()) < |
| 601 | getPPC64SectionRank(B->getName()); |
| 602 | |
Rafael Espindola | 24c073d | 2016-09-21 22:36:19 +0000 | [diff] [blame] | 603 | return false; |
| 604 | } |
| 605 | |
| 606 | // Output section ordering is determined by this function. |
| 607 | template <class ELFT> |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 608 | static bool compareSections(const OutputSectionBase *A, |
| 609 | const OutputSectionBase *B) { |
Rafael Espindola | 24c073d | 2016-09-21 22:36:19 +0000 | [diff] [blame] | 610 | // For now, put sections mentioned in a linker script first. |
| 611 | int AIndex = Script<ELFT>::X->getSectionIndex(A->getName()); |
| 612 | int BIndex = Script<ELFT>::X->getSectionIndex(B->getName()); |
| 613 | bool AInScript = AIndex != INT_MAX; |
| 614 | bool BInScript = BIndex != INT_MAX; |
| 615 | if (AInScript != BInScript) |
| 616 | return AInScript; |
| 617 | // If both are in the script, use that order. |
| 618 | if (AInScript) |
| 619 | return AIndex < BIndex; |
| 620 | |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 621 | return compareSectionsNonScript<ELFT>(A, B); |
Rui Ueyama | 5a9640b | 2015-10-08 23:49:30 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 624 | // Program header entry |
Rafael Espindola | 17cb7c0 | 2016-12-19 17:01:01 +0000 | [diff] [blame] | 625 | PhdrEntry::PhdrEntry(unsigned Type, unsigned Flags) { |
| 626 | p_type = Type; |
| 627 | p_flags = Flags; |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 628 | } |
| 629 | |
Rafael Espindola | 17cb7c0 | 2016-12-19 17:01:01 +0000 | [diff] [blame] | 630 | void PhdrEntry::add(OutputSectionBase *Sec) { |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 631 | Last = Sec; |
| 632 | if (!First) |
| 633 | First = Sec; |
Rafael Espindola | 17cb7c0 | 2016-12-19 17:01:01 +0000 | [diff] [blame] | 634 | p_align = std::max(p_align, Sec->Addralign); |
| 635 | if (p_type == PT_LOAD) |
Eugene Leviant | 3d9abec | 2016-09-29 09:20:33 +0000 | [diff] [blame] | 636 | Sec->FirstInPtLoad = First; |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 637 | } |
| 638 | |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 639 | template <class ELFT> |
Peter Collingbourne | 578af61 | 2017-01-04 02:43:01 +0000 | [diff] [blame] | 640 | static void addOptionalSynthetic(StringRef Name, OutputSectionBase *Sec, |
| 641 | typename ELFT::uint Val, |
| 642 | uint8_t StOther = STV_HIDDEN) { |
| 643 | if (SymbolBody *S = Symtab<ELFT>::X->find(Name)) |
| 644 | if (S->isUndefined() || S->isShared()) |
| 645 | Symtab<ELFT>::X->addSynthetic(Name, Sec, Val, StOther); |
Rafael Espindola | 6f92e14 | 2016-04-12 13:26:51 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 648 | template <class ELFT> |
Rui Ueyama | 0cbf749 | 2016-11-23 06:59:47 +0000 | [diff] [blame] | 649 | static Symbol *addRegular(StringRef Name, InputSectionBase<ELFT> *Sec, |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 650 | typename ELFT::uint Value) { |
Rafael Espindola | dab02d4 | 2016-11-17 21:20:16 +0000 | [diff] [blame] | 651 | // The linker generated symbols are added as STB_WEAK to allow user defined |
| 652 | // ones to override them. |
Rui Ueyama | 0cbf749 | 2016-11-23 06:59:47 +0000 | [diff] [blame] | 653 | return Symtab<ELFT>::X->addRegular(Name, STV_HIDDEN, STT_NOTYPE, Value, |
| 654 | /*Size=*/0, STB_WEAK, Sec, |
| 655 | /*File=*/nullptr); |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | template <class ELFT> |
| 659 | static Symbol *addOptionalRegular(StringRef Name, InputSectionBase<ELFT> *IS, |
| 660 | typename ELFT::uint Value) { |
| 661 | SymbolBody *S = Symtab<ELFT>::X->find(Name); |
| 662 | if (!S) |
| 663 | return nullptr; |
| 664 | if (!S->isUndefined() && !S->isShared()) |
| 665 | return S->symbol(); |
| 666 | return addRegular(Name, IS, Value); |
| 667 | } |
| 668 | |
Rui Ueyama | 0168722 | 2015-12-26 09:47:57 +0000 | [diff] [blame] | 669 | // The beginning and the ending of .rel[a].plt section are marked |
| 670 | // with __rel[a]_iplt_{start,end} symbols if it is a statically linked |
| 671 | // executable. The runtime needs these symbols in order to resolve |
| 672 | // all IRELATIVE relocs on startup. For dynamic executables, we don't |
| 673 | // need these symbols, since IRELATIVE relocs are resolved through GOT |
| 674 | // and PLT. For details, see http://www.airs.com/blog/archives/403. |
George Rimar | ee741cf | 2016-04-14 13:23:02 +0000 | [diff] [blame] | 675 | template <class ELFT> void Writer<ELFT>::addRelIpltSymbols() { |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 676 | if (In<ELFT>::DynSymTab) |
George Rimar | a07ff66 | 2015-12-21 10:12:06 +0000 | [diff] [blame] | 677 | return; |
Rui Ueyama | 6c5638b | 2016-03-13 20:10:20 +0000 | [diff] [blame] | 678 | StringRef S = Config->Rela ? "__rela_iplt_start" : "__rel_iplt_start"; |
Peter Smith | baffdb8 | 2016-12-08 12:58:55 +0000 | [diff] [blame] | 679 | addOptionalRegular<ELFT>(S, In<ELFT>::RelaIplt, 0); |
Rui Ueyama | 0168722 | 2015-12-26 09:47:57 +0000 | [diff] [blame] | 680 | |
Rui Ueyama | 6c5638b | 2016-03-13 20:10:20 +0000 | [diff] [blame] | 681 | S = Config->Rela ? "__rela_iplt_end" : "__rel_iplt_end"; |
Peter Smith | baffdb8 | 2016-12-08 12:58:55 +0000 | [diff] [blame] | 682 | addOptionalRegular<ELFT>(S, In<ELFT>::RelaIplt, -1); |
George Rimar | a07ff66 | 2015-12-21 10:12:06 +0000 | [diff] [blame] | 683 | } |
| 684 | |
Rui Ueyama | f18fe7b | 2015-12-26 07:50:39 +0000 | [diff] [blame] | 685 | // The linker is expected to define some symbols depending on |
| 686 | // the linking result. This function defines such symbols. |
| 687 | template <class ELFT> void Writer<ELFT>::addReservedSymbols() { |
George Rimar | 7beff42 | 2016-11-15 08:19:02 +0000 | [diff] [blame] | 688 | if (Config->EMachine == EM_MIPS) { |
Rafael Espindola | 9b3f99e | 2016-04-12 02:24:43 +0000 | [diff] [blame] | 689 | // Define _gp for MIPS. st_value of _gp symbol will be updated by Writer |
Simon Atanasyan | 6a4eb75 | 2016-12-08 06:19:47 +0000 | [diff] [blame] | 690 | // so that it points to an absolute address which by default is relative |
| 691 | // to GOT. Default offset is 0x7ff0. |
Rafael Espindola | 9b3f99e | 2016-04-12 02:24:43 +0000 | [diff] [blame] | 692 | // See "Global Data Symbols" in Chapter 6 in the following document: |
| 693 | // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf |
Simon Atanasyan | 6a4eb75 | 2016-12-08 06:19:47 +0000 | [diff] [blame] | 694 | ElfSym<ELFT>::MipsGp = |
| 695 | Symtab<ELFT>::X->addAbsolute("_gp", STV_HIDDEN, STB_LOCAL); |
Rafael Espindola | 9b3f99e | 2016-04-12 02:24:43 +0000 | [diff] [blame] | 696 | |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 697 | // On MIPS O32 ABI, _gp_disp is a magic symbol designates offset between |
Simon Atanasyan | 6a4eb75 | 2016-12-08 06:19:47 +0000 | [diff] [blame] | 698 | // start of function and 'gp' pointer into GOT. To simplify relocation |
| 699 | // calculation we assign _gp value to it and calculate corresponding |
| 700 | // relocations as relative to this value. |
| 701 | if (Symtab<ELFT>::X->find("_gp_disp")) |
| 702 | ElfSym<ELFT>::MipsGpDisp = |
| 703 | Symtab<ELFT>::X->addAbsolute("_gp_disp", STV_HIDDEN, STB_LOCAL); |
Peter Collingbourne | 6f535b7 | 2016-05-03 18:03:45 +0000 | [diff] [blame] | 704 | |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 705 | // The __gnu_local_gp is a magic symbol equal to the current value of 'gp' |
| 706 | // pointer. This symbol is used in the code generated by .cpload pseudo-op |
| 707 | // in case of using -mno-shared option. |
| 708 | // https://sourceware.org/ml/binutils/2004-12/msg00094.html |
Simon Atanasyan | 6a4eb75 | 2016-12-08 06:19:47 +0000 | [diff] [blame] | 709 | if (Symtab<ELFT>::X->find("__gnu_local_gp")) |
| 710 | ElfSym<ELFT>::MipsLocalGp = |
| 711 | Symtab<ELFT>::X->addAbsolute("__gnu_local_gp", STV_HIDDEN, STB_LOCAL); |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | // In the assembly for 32 bit x86 the _GLOBAL_OFFSET_TABLE_ symbol |
| 715 | // is magical and is used to produce a R_386_GOTPC relocation. |
| 716 | // The R_386_GOTPC relocation value doesn't actually depend on the |
| 717 | // symbol value, so it could use an index of STN_UNDEF which, according |
| 718 | // to the spec, means the symbol value is 0. |
| 719 | // Unfortunately both gas and MC keep the _GLOBAL_OFFSET_TABLE_ symbol in |
| 720 | // the object file. |
| 721 | // The situation is even stranger on x86_64 where the assembly doesn't |
| 722 | // need the magical symbol, but gas still puts _GLOBAL_OFFSET_TABLE_ as |
| 723 | // an undefined symbol in the .o files. |
| 724 | // Given that the symbol is effectively unused, we just create a dummy |
| 725 | // hidden one to avoid the undefined symbol error. |
George Rimar | 7beff42 | 2016-11-15 08:19:02 +0000 | [diff] [blame] | 726 | Symtab<ELFT>::X->addIgnored("_GLOBAL_OFFSET_TABLE_"); |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 727 | |
Rui Ueyama | f18fe7b | 2015-12-26 07:50:39 +0000 | [diff] [blame] | 728 | // __tls_get_addr is defined by the dynamic linker for dynamic ELFs. For |
| 729 | // static linking the linker is required to optimize away any references to |
| 730 | // __tls_get_addr, so it's not defined anywhere. Create a hidden definition |
Peter Smith | 17cd375 | 2016-10-27 10:28:53 +0000 | [diff] [blame] | 731 | // to avoid the undefined symbol error. As usual special cases are ARM and |
| 732 | // MIPS - the libc for these targets defines __tls_get_addr itself because |
| 733 | // there are no TLS optimizations for these targets. |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 734 | if (!In<ELFT>::DynSymTab && |
Peter Smith | 17cd375 | 2016-10-27 10:28:53 +0000 | [diff] [blame] | 735 | (Config->EMachine != EM_MIPS && Config->EMachine != EM_ARM)) |
Rui Ueyama | 84907c5 | 2016-08-09 03:38:23 +0000 | [diff] [blame] | 736 | Symtab<ELFT>::X->addIgnored("__tls_get_addr"); |
Rui Ueyama | f18fe7b | 2015-12-26 07:50:39 +0000 | [diff] [blame] | 737 | |
George Rimar | 28ac19c | 2016-08-08 08:42:48 +0000 | [diff] [blame] | 738 | // If linker script do layout we do not need to create any standart symbols. |
Eugene Leviant | e05336ff | 2016-09-14 08:32:36 +0000 | [diff] [blame] | 739 | if (ScriptConfig->HasSections) |
George Rimar | 28ac19c | 2016-08-08 08:42:48 +0000 | [diff] [blame] | 740 | return; |
| 741 | |
Petr Hosek | 4f7a5c3 | 2016-08-22 18:44:04 +0000 | [diff] [blame] | 742 | ElfSym<ELFT>::EhdrStart = Symtab<ELFT>::X->addIgnored("__ehdr_start"); |
| 743 | |
Rui Ueyama | 467dbdd | 2016-04-21 20:50:15 +0000 | [diff] [blame] | 744 | auto Define = [this](StringRef S, DefinedRegular<ELFT> *&Sym1, |
| 745 | DefinedRegular<ELFT> *&Sym2) { |
Rui Ueyama | 84907c5 | 2016-08-09 03:38:23 +0000 | [diff] [blame] | 746 | Sym1 = Symtab<ELFT>::X->addIgnored(S, STV_DEFAULT); |
Rui Ueyama | 4d169bd | 2016-02-26 16:38:39 +0000 | [diff] [blame] | 747 | |
| 748 | // The name without the underscore is not a reserved name, |
| 749 | // so it is defined only when there is a reference against it. |
Rui Ueyama | 68e1555 | 2016-02-26 16:49:54 +0000 | [diff] [blame] | 750 | assert(S.startswith("_")); |
Rui Ueyama | 4d169bd | 2016-02-26 16:38:39 +0000 | [diff] [blame] | 751 | S = S.substr(1); |
Rui Ueyama | 84907c5 | 2016-08-09 03:38:23 +0000 | [diff] [blame] | 752 | if (SymbolBody *B = Symtab<ELFT>::X->find(S)) |
George Rimar | 9e85939 | 2016-02-26 14:36:36 +0000 | [diff] [blame] | 753 | if (B->isUndefined()) |
Rui Ueyama | 84907c5 | 2016-08-09 03:38:23 +0000 | [diff] [blame] | 754 | Sym2 = Symtab<ELFT>::X->addAbsolute(S, STV_DEFAULT); |
George Rimar | 9e85939 | 2016-02-26 14:36:36 +0000 | [diff] [blame] | 755 | }; |
| 756 | |
Rui Ueyama | 467dbdd | 2016-04-21 20:50:15 +0000 | [diff] [blame] | 757 | Define("_end", ElfSym<ELFT>::End, ElfSym<ELFT>::End2); |
| 758 | Define("_etext", ElfSym<ELFT>::Etext, ElfSym<ELFT>::Etext2); |
| 759 | Define("_edata", ElfSym<ELFT>::Edata, ElfSym<ELFT>::Edata2); |
Rui Ueyama | f18fe7b | 2015-12-26 07:50:39 +0000 | [diff] [blame] | 760 | } |
| 761 | |
Rui Ueyama | c418570 | 2016-02-10 23:20:42 +0000 | [diff] [blame] | 762 | // Sort input sections by section name suffixes for |
| 763 | // __attribute__((init_priority(N))). |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 764 | template <class ELFT> static void sortInitFini(OutputSectionBase *S) { |
Rui Ueyama | c418570 | 2016-02-10 23:20:42 +0000 | [diff] [blame] | 765 | if (S) |
Rui Ueyama | 5af8368 | 2016-02-11 23:41:38 +0000 | [diff] [blame] | 766 | reinterpret_cast<OutputSection<ELFT> *>(S)->sortInitFini(); |
| 767 | } |
| 768 | |
| 769 | // Sort input sections by the special rule for .ctors and .dtors. |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 770 | template <class ELFT> static void sortCtorsDtors(OutputSectionBase *S) { |
Rui Ueyama | 5af8368 | 2016-02-11 23:41:38 +0000 | [diff] [blame] | 771 | if (S) |
| 772 | reinterpret_cast<OutputSection<ELFT> *>(S)->sortCtorsDtors(); |
Rui Ueyama | c418570 | 2016-02-10 23:20:42 +0000 | [diff] [blame] | 773 | } |
| 774 | |
George Rimar | 1a33c0f | 2016-11-10 09:05:20 +0000 | [diff] [blame] | 775 | // Sort input sections using the list provided by --symbol-ordering-file. |
| 776 | template <class ELFT> |
Rui Ueyama | 3127031 | 2016-12-20 01:51:08 +0000 | [diff] [blame] | 777 | static void sortBySymbolsOrder(ArrayRef<OutputSectionBase *> OutputSections) { |
George Rimar | 1a33c0f | 2016-11-10 09:05:20 +0000 | [diff] [blame] | 778 | if (Config->SymbolOrderingFile.empty()) |
| 779 | return; |
| 780 | |
Rui Ueyama | 3127031 | 2016-12-20 01:51:08 +0000 | [diff] [blame] | 781 | // Build a map from symbols to their priorities. Symbols that didn't |
| 782 | // appear in the symbol ordering file have the lowest priority 0. |
| 783 | // All explicitly mentioned symbols have negative (higher) priorities. |
| 784 | DenseMap<StringRef, int> SymbolOrder; |
| 785 | int Priority = -Config->SymbolOrderingFile.size(); |
| 786 | for (StringRef S : Config->SymbolOrderingFile) |
| 787 | SymbolOrder.insert({S, Priority++}); |
| 788 | |
| 789 | // Build a map from sections to their priorities. |
| 790 | DenseMap<InputSectionBase<ELFT> *, int> SectionOrder; |
George Rimar | 1a33c0f | 2016-11-10 09:05:20 +0000 | [diff] [blame] | 791 | for (elf::ObjectFile<ELFT> *File : Symtab<ELFT>::X->getObjectFiles()) { |
| 792 | for (SymbolBody *Body : File->getSymbols()) { |
| 793 | auto *D = dyn_cast<DefinedRegular<ELFT>>(Body); |
| 794 | if (!D || !D->Section) |
| 795 | continue; |
Rui Ueyama | 3127031 | 2016-12-20 01:51:08 +0000 | [diff] [blame] | 796 | int &Priority = SectionOrder[D->Section]; |
| 797 | Priority = std::min(Priority, SymbolOrder.lookup(D->getName())); |
George Rimar | 1a33c0f | 2016-11-10 09:05:20 +0000 | [diff] [blame] | 798 | } |
| 799 | } |
| 800 | |
Rui Ueyama | 3127031 | 2016-12-20 01:51:08 +0000 | [diff] [blame] | 801 | // Sort sections by priority. |
| 802 | for (OutputSectionBase *Base : OutputSections) |
Rui Ueyama | 8f687f7 | 2016-12-19 03:14:16 +0000 | [diff] [blame] | 803 | if (auto *Sec = dyn_cast<OutputSection<ELFT>>(Base)) |
Rui Ueyama | 3127031 | 2016-12-20 01:51:08 +0000 | [diff] [blame] | 804 | Sec->sort([&](InputSection<ELFT> *S) { return SectionOrder.lookup(S); }); |
George Rimar | 1a33c0f | 2016-11-10 09:05:20 +0000 | [diff] [blame] | 805 | } |
| 806 | |
Eugene Leviant | e63d81b | 2016-07-20 14:43:20 +0000 | [diff] [blame] | 807 | template <class ELFT> |
Rafael Espindola | 0f7ceda | 2016-07-20 17:58:07 +0000 | [diff] [blame] | 808 | void Writer<ELFT>::forEachRelSec( |
Rafael Espindola | 9f0c4bb | 2016-11-10 14:53:24 +0000 | [diff] [blame] | 809 | std::function<void(InputSectionBase<ELFT> &)> Fn) { |
Rui Ueyama | 8c6a5aa | 2016-11-05 22:37:59 +0000 | [diff] [blame] | 810 | for (InputSectionBase<ELFT> *IS : Symtab<ELFT>::X->Sections) { |
Rafael Espindola | 8f9026b | 2016-11-08 18:23:02 +0000 | [diff] [blame] | 811 | if (!IS->Live) |
Rui Ueyama | 8c6a5aa | 2016-11-05 22:37:59 +0000 | [diff] [blame] | 812 | continue; |
| 813 | // Scan all relocations. Each relocation goes through a series |
| 814 | // of tests to determine if it needs special treatment, such as |
| 815 | // creating GOT, PLT, copy relocations, etc. |
| 816 | // Note that relocations for non-alloc sections are directly |
| 817 | // processed by InputSection::relocateNonAlloc. |
| 818 | if (!(IS->Flags & SHF_ALLOC)) |
| 819 | continue; |
Rafael Espindola | 9f0c4bb | 2016-11-10 14:53:24 +0000 | [diff] [blame] | 820 | if (isa<InputSection<ELFT>>(IS) || isa<EhInputSection<ELFT>>(IS)) |
| 821 | Fn(*IS); |
Rafael Espindola | 0f7ceda | 2016-07-20 17:58:07 +0000 | [diff] [blame] | 822 | } |
| 823 | } |
| 824 | |
Eugene Leviant | 282251a | 2016-11-01 09:49:24 +0000 | [diff] [blame] | 825 | template <class ELFT> |
| 826 | void Writer<ELFT>::addInputSec(InputSectionBase<ELFT> *IS) { |
Eugene Leviant | 22eb026 | 2016-11-14 09:16:00 +0000 | [diff] [blame] | 827 | if (!IS) |
| 828 | return; |
| 829 | |
Rafael Espindola | 8f9026b | 2016-11-08 18:23:02 +0000 | [diff] [blame] | 830 | if (!IS->Live) { |
Eugene Leviant | 282251a | 2016-11-01 09:49:24 +0000 | [diff] [blame] | 831 | reportDiscarded(IS); |
| 832 | return; |
| 833 | } |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 834 | OutputSectionBase *Sec; |
Eugene Leviant | 282251a | 2016-11-01 09:49:24 +0000 | [diff] [blame] | 835 | bool IsNew; |
| 836 | StringRef OutsecName = getOutputSectionName(IS->Name); |
| 837 | std::tie(Sec, IsNew) = Factory.create(IS, OutsecName); |
| 838 | if (IsNew) |
| 839 | OutputSections.push_back(Sec); |
| 840 | Sec->addSection(IS); |
| 841 | } |
Rafael Espindola | 093abab | 2016-10-27 17:45:40 +0000 | [diff] [blame] | 842 | |
Eugene Leviant | 282251a | 2016-11-01 09:49:24 +0000 | [diff] [blame] | 843 | template <class ELFT> void Writer<ELFT>::createSections() { |
Rui Ueyama | 8c6a5aa | 2016-11-05 22:37:59 +0000 | [diff] [blame] | 844 | for (InputSectionBase<ELFT> *IS : Symtab<ELFT>::X->Sections) |
| 845 | addInputSec(IS); |
Eugene Leviant | ceabe80 | 2016-08-11 07:56:43 +0000 | [diff] [blame] | 846 | |
George Rimar | 1a33c0f | 2016-11-10 09:05:20 +0000 | [diff] [blame] | 847 | sortBySymbolsOrder<ELFT>(OutputSections); |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 848 | sortInitFini<ELFT>(findSection(".init_array")); |
| 849 | sortInitFini<ELFT>(findSection(".fini_array")); |
| 850 | sortCtorsDtors<ELFT>(findSection(".ctors")); |
| 851 | sortCtorsDtors<ELFT>(findSection(".dtors")); |
Eugene Leviant | ceabe80 | 2016-08-11 07:56:43 +0000 | [diff] [blame] | 852 | |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 853 | for (OutputSectionBase *Sec : OutputSections) |
Eugene Leviant | ceabe80 | 2016-08-11 07:56:43 +0000 | [diff] [blame] | 854 | Sec->assignOffsets(); |
Eugene Leviant | e63d81b | 2016-07-20 14:43:20 +0000 | [diff] [blame] | 855 | } |
| 856 | |
Eugene Leviant | bae1c65 | 2016-11-08 10:44:48 +0000 | [diff] [blame] | 857 | template <class ELFT> |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 858 | static bool canSharePtLoad(const OutputSectionBase &S1, |
| 859 | const OutputSectionBase &S2) { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 860 | if (!(S1.Flags & SHF_ALLOC) || !(S2.Flags & SHF_ALLOC)) |
Eugene Leviant | bae1c65 | 2016-11-08 10:44:48 +0000 | [diff] [blame] | 861 | return false; |
| 862 | |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 863 | bool S1IsWrite = S1.Flags & SHF_WRITE; |
| 864 | bool S2IsWrite = S2.Flags & SHF_WRITE; |
Eugene Leviant | bae1c65 | 2016-11-08 10:44:48 +0000 | [diff] [blame] | 865 | if (S1IsWrite != S2IsWrite) |
| 866 | return false; |
| 867 | |
| 868 | if (!S1IsWrite) |
| 869 | return true; // RO and RX share a PT_LOAD with linker scripts. |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 870 | return (S1.Flags & SHF_EXECINSTR) == (S2.Flags & SHF_EXECINSTR); |
Eugene Leviant | bae1c65 | 2016-11-08 10:44:48 +0000 | [diff] [blame] | 871 | } |
| 872 | |
Rafael Espindola | 24c073d | 2016-09-21 22:36:19 +0000 | [diff] [blame] | 873 | template <class ELFT> void Writer<ELFT>::sortSections() { |
Rafael Espindola | 1960bcd | 2016-11-11 22:43:27 +0000 | [diff] [blame] | 874 | // Don't sort if using -r. It is not necessary and we want to preserve the |
| 875 | // relative order for SHF_LINK_ORDER sections. |
| 876 | if (Config->Relocatable) |
| 877 | return; |
Rafael Espindola | 24c073d | 2016-09-21 22:36:19 +0000 | [diff] [blame] | 878 | if (!ScriptConfig->HasSections) { |
| 879 | std::stable_sort(OutputSections.begin(), OutputSections.end(), |
| 880 | compareSectionsNonScript<ELFT>); |
| 881 | return; |
| 882 | } |
Rafael Espindola | 9546fff | 2016-09-22 14:40:50 +0000 | [diff] [blame] | 883 | Script<ELFT>::X->adjustSectionsBeforeSorting(); |
Rafael Espindola | 24c073d | 2016-09-21 22:36:19 +0000 | [diff] [blame] | 884 | |
| 885 | // The order of the sections in the script is arbitrary and may not agree with |
| 886 | // compareSectionsNonScript. This means that we cannot easily define a |
| 887 | // strict weak ordering. To see why, consider a comparison of a section in the |
| 888 | // script and one not in the script. We have a two simple options: |
| 889 | // * Make them equivalent (a is not less than b, and b is not less than a). |
| 890 | // The problem is then that equivalence has to be transitive and we can |
| 891 | // have sections a, b and c with only b in a script and a less than c |
| 892 | // which breaks this property. |
| 893 | // * Use compareSectionsNonScript. Given that the script order doesn't have |
| 894 | // to match, we can end up with sections a, b, c, d where b and c are in the |
| 895 | // script and c is compareSectionsNonScript less than b. In which case d |
| 896 | // can be equivalent to c, a to b and d < a. As a concrete example: |
| 897 | // .a (rx) # not in script |
| 898 | // .b (rx) # in script |
| 899 | // .c (ro) # in script |
| 900 | // .d (ro) # not in script |
| 901 | // |
| 902 | // The way we define an order then is: |
| 903 | // * First put script sections at the start and sort the script and |
| 904 | // non-script sections independently. |
Eugene Leviant | bae1c65 | 2016-11-08 10:44:48 +0000 | [diff] [blame] | 905 | // * Move each non-script section to its preferred position. We try |
| 906 | // to put each section in the last position where it it can share |
| 907 | // a PT_LOAD. |
Rafael Espindola | 24c073d | 2016-09-21 22:36:19 +0000 | [diff] [blame] | 908 | |
| 909 | std::stable_sort(OutputSections.begin(), OutputSections.end(), |
| 910 | compareSections<ELFT>); |
| 911 | |
| 912 | auto I = OutputSections.begin(); |
| 913 | auto E = OutputSections.end(); |
Eugene Leviant | bae1c65 | 2016-11-08 10:44:48 +0000 | [diff] [blame] | 914 | auto NonScriptI = |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 915 | std::find_if(OutputSections.begin(), E, [](OutputSectionBase *S) { |
Eugene Leviant | bae1c65 | 2016-11-08 10:44:48 +0000 | [diff] [blame] | 916 | return Script<ELFT>::X->getSectionIndex(S->getName()) == INT_MAX; |
| 917 | }); |
Rafael Espindola | 24c073d | 2016-09-21 22:36:19 +0000 | [diff] [blame] | 918 | while (NonScriptI != E) { |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 919 | auto BestPos = std::max_element( |
| 920 | I, NonScriptI, [&](OutputSectionBase *&A, OutputSectionBase *&B) { |
| 921 | bool ACanSharePtLoad = canSharePtLoad<ELFT>(**NonScriptI, *A); |
| 922 | bool BCanSharePtLoad = canSharePtLoad<ELFT>(**NonScriptI, *B); |
Eugene Leviant | bae1c65 | 2016-11-08 10:44:48 +0000 | [diff] [blame] | 923 | if (ACanSharePtLoad != BCanSharePtLoad) |
| 924 | return BCanSharePtLoad; |
| 925 | |
| 926 | bool ACmp = compareSectionsNonScript<ELFT>(*NonScriptI, A); |
| 927 | bool BCmp = compareSectionsNonScript<ELFT>(*NonScriptI, B); |
| 928 | if (ACmp != BCmp) |
| 929 | return BCmp; // FIXME: missing test |
| 930 | |
| 931 | size_t PosA = &A - &OutputSections[0]; |
| 932 | size_t PosB = &B - &OutputSections[0]; |
| 933 | return ACmp ? PosA > PosB : PosA < PosB; |
Rafael Espindola | 24c073d | 2016-09-21 22:36:19 +0000 | [diff] [blame] | 934 | }); |
Eugene Leviant | bae1c65 | 2016-11-08 10:44:48 +0000 | [diff] [blame] | 935 | |
| 936 | // max_element only returns NonScriptI if the range is empty. If the range |
| 937 | // is not empty we should consider moving the the element forward one |
| 938 | // position. |
| 939 | if (BestPos != NonScriptI && |
| 940 | !compareSectionsNonScript<ELFT>(*NonScriptI, *BestPos)) |
| 941 | ++BestPos; |
| 942 | std::rotate(BestPos, NonScriptI, NonScriptI + 1); |
Rafael Espindola | 24c073d | 2016-09-21 22:36:19 +0000 | [diff] [blame] | 943 | ++NonScriptI; |
Rafael Espindola | 24c073d | 2016-09-21 22:36:19 +0000 | [diff] [blame] | 944 | } |
Rafael Espindola | f7a1744 | 2016-11-14 15:39:38 +0000 | [diff] [blame] | 945 | |
| 946 | Script<ELFT>::X->adjustSectionsAfterSorting(); |
Rafael Espindola | 24c073d | 2016-09-21 22:36:19 +0000 | [diff] [blame] | 947 | } |
| 948 | |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 949 | template <class ELFT> |
| 950 | static void |
| 951 | finalizeSynthetic(const std::vector<SyntheticSection<ELFT> *> &Sections) { |
| 952 | for (SyntheticSection<ELFT> *SS : Sections) |
George Rimar | 3fb5a6d | 2016-11-29 16:05:27 +0000 | [diff] [blame] | 953 | if (SS && SS->OutSec && !SS->empty()) { |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 954 | SS->finalize(); |
| 955 | SS->OutSec->Size = 0; |
| 956 | SS->OutSec->assignOffsets(); |
| 957 | } |
| 958 | } |
| 959 | |
George Rimar | 11992c86 | 2016-11-25 08:05:41 +0000 | [diff] [blame] | 960 | // We need to add input synthetic sections early in createSyntheticSections() |
Rui Ueyama | c38860b | 2016-12-05 21:39:35 +0000 | [diff] [blame] | 961 | // to make them visible from linkescript side. But not all sections are always |
| 962 | // required to be in output. For example we don't need dynamic section content |
| 963 | // sometimes. This function filters out such unused sections from output. |
| 964 | template <class ELFT> |
| 965 | static void removeUnusedSyntheticSections(std::vector<OutputSectionBase *> &V) { |
George Rimar | 11992c86 | 2016-11-25 08:05:41 +0000 | [diff] [blame] | 966 | // Input synthetic sections are placed after all regular ones. We iterate over |
| 967 | // them all and exit at first non-synthetic. |
Rui Ueyama | c38860b | 2016-12-05 21:39:35 +0000 | [diff] [blame] | 968 | for (InputSectionBase<ELFT> *S : llvm::reverse(Symtab<ELFT>::X->Sections)) { |
| 969 | SyntheticSection<ELFT> *SS = dyn_cast<SyntheticSection<ELFT>>(S); |
| 970 | if (!SS) |
George Rimar | 11992c86 | 2016-11-25 08:05:41 +0000 | [diff] [blame] | 971 | return; |
Rui Ueyama | c38860b | 2016-12-05 21:39:35 +0000 | [diff] [blame] | 972 | if (!SS->empty() || !SS->OutSec) |
George Rimar | 11992c86 | 2016-11-25 08:05:41 +0000 | [diff] [blame] | 973 | continue; |
| 974 | |
Rui Ueyama | c38860b | 2016-12-05 21:39:35 +0000 | [diff] [blame] | 975 | OutputSection<ELFT> *OutSec = cast<OutputSection<ELFT>>(SS->OutSec); |
| 976 | OutSec->Sections.erase( |
| 977 | std::find(OutSec->Sections.begin(), OutSec->Sections.end(), SS)); |
| 978 | // If there is no other sections in output section, remove it from output. |
| 979 | if (OutSec->Sections.empty()) |
| 980 | V.erase(std::find(V.begin(), V.end(), OutSec)); |
George Rimar | 11992c86 | 2016-11-25 08:05:41 +0000 | [diff] [blame] | 981 | } |
| 982 | } |
| 983 | |
Eugene Leviant | e63d81b | 2016-07-20 14:43:20 +0000 | [diff] [blame] | 984 | // Create output section objects and add them to OutputSections. |
| 985 | template <class ELFT> void Writer<ELFT>::finalizeSections() { |
George Rimar | 58fa524 | 2016-10-20 09:19:48 +0000 | [diff] [blame] | 986 | Out<ELFT>::DebugInfo = findSection(".debug_info"); |
Rui Ueyama | a8f6fea | 2016-08-09 04:25:20 +0000 | [diff] [blame] | 987 | Out<ELFT>::PreinitArray = findSection(".preinit_array"); |
| 988 | Out<ELFT>::InitArray = findSection(".init_array"); |
| 989 | Out<ELFT>::FiniArray = findSection(".fini_array"); |
Rafael Espindola | 7757224 | 2015-10-02 19:37:55 +0000 | [diff] [blame] | 990 | |
Rui Ueyama | a5d79d1 | 2015-12-26 09:48:00 +0000 | [diff] [blame] | 991 | // The linker needs to define SECNAME_start, SECNAME_end and SECNAME_stop |
| 992 | // symbols for sections, so that the runtime can get the start and end |
| 993 | // addresses of each section by section name. Add such symbols. |
George Rimar | c1034a8 | 2016-03-01 19:12:35 +0000 | [diff] [blame] | 994 | if (!Config->Relocatable) { |
| 995 | addStartEndSymbols(); |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 996 | for (OutputSectionBase *Sec : OutputSections) |
George Rimar | c1034a8 | 2016-03-01 19:12:35 +0000 | [diff] [blame] | 997 | addStartStopSymbols(Sec); |
| 998 | } |
Rui Ueyama | d4530c6 | 2016-03-04 18:34:14 +0000 | [diff] [blame] | 999 | |
| 1000 | // Add _DYNAMIC symbol. Unlike GNU gold, our _DYNAMIC symbol has no type. |
| 1001 | // It should be okay as no one seems to care about the type. |
| 1002 | // Even the author of gold doesn't remember why gold behaves that way. |
| 1003 | // https://sourceware.org/ml/binutils/2002-03/msg00360.html |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1004 | if (In<ELFT>::DynSymTab) |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 1005 | addRegular("_DYNAMIC", In<ELFT>::Dynamic, 0); |
Rafael Espindola | 334c3e1 | 2015-10-19 15:21:42 +0000 | [diff] [blame] | 1006 | |
Rafael Espindola | de9857e | 2016-02-04 21:33:05 +0000 | [diff] [blame] | 1007 | // Define __rel[a]_iplt_{start,end} symbols if needed. |
| 1008 | addRelIpltSymbols(); |
| 1009 | |
Rui Ueyama | 3b31e67 | 2016-05-23 16:24:16 +0000 | [diff] [blame] | 1010 | if (!Out<ELFT>::EhFrame->empty()) { |
| 1011 | OutputSections.push_back(Out<ELFT>::EhFrame); |
| 1012 | Out<ELFT>::EhFrame->finalize(); |
| 1013 | } |
Rafael Espindola | 56004c5 | 2016-04-07 14:22:09 +0000 | [diff] [blame] | 1014 | |
Rafael Espindola | 0f7ceda | 2016-07-20 17:58:07 +0000 | [diff] [blame] | 1015 | // Scan relocations. This must be done after every symbol is declared so that |
| 1016 | // we can correctly decide if a dynamic relocation is needed. |
| 1017 | forEachRelSec(scanRelocations<ELFT>); |
| 1018 | |
Rui Ueyama | 1b2a8bf | 2015-12-26 10:22:16 +0000 | [diff] [blame] | 1019 | // Now that we have defined all possible symbols including linker- |
| 1020 | // synthesized ones. Visit all symbols to give the finishing touches. |
Rui Ueyama | 84907c5 | 2016-08-09 03:38:23 +0000 | [diff] [blame] | 1021 | for (Symbol *S : Symtab<ELFT>::X->getSymbols()) { |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 1022 | SymbolBody *Body = S->body(); |
Rafael Espindola | 0baa73f | 2016-04-26 13:56:26 +0000 | [diff] [blame] | 1023 | |
Rafael Espindola | 4f674ed | 2015-10-05 15:24:04 +0000 | [diff] [blame] | 1024 | if (!includeInSymtab<ELFT>(*Body)) |
Rafael Espindola | 05a3dd2 | 2015-09-22 23:38:23 +0000 | [diff] [blame] | 1025 | continue; |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1026 | if (In<ELFT>::SymTab) |
| 1027 | In<ELFT>::SymTab->addSymbol(Body); |
Rafael Espindola | 05a3dd2 | 2015-09-22 23:38:23 +0000 | [diff] [blame] | 1028 | |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1029 | if (In<ELFT>::DynSymTab && S->includeInDynsym()) { |
| 1030 | In<ELFT>::DynSymTab->addSymbol(Body); |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 1031 | if (auto *SS = dyn_cast<SharedSymbol<ELFT>>(Body)) |
Rui Ueyama | 434b561 | 2016-07-17 03:11:46 +0000 | [diff] [blame] | 1032 | if (SS->file()->isNeeded()) |
Eugene Leviant | e9bab5d | 2016-11-21 16:59:33 +0000 | [diff] [blame] | 1033 | In<ELFT>::VerNeed->addSymbol(SS); |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 1034 | } |
Rafael Espindola | 05a3dd2 | 2015-09-22 23:38:23 +0000 | [diff] [blame] | 1035 | } |
Rui Ueyama | c2a0d7e | 2016-01-28 22:56:29 +0000 | [diff] [blame] | 1036 | |
| 1037 | // Do not proceed if there was an undefined symbol. |
Rui Ueyama | f373dd7 | 2016-11-24 01:43:21 +0000 | [diff] [blame] | 1038 | if (ErrorCount) |
Rui Ueyama | f7f52ef | 2016-04-01 17:24:19 +0000 | [diff] [blame] | 1039 | return; |
Rui Ueyama | c2a0d7e | 2016-01-28 22:56:29 +0000 | [diff] [blame] | 1040 | |
Rui Ueyama | 84417f8 | 2015-12-26 07:50:41 +0000 | [diff] [blame] | 1041 | // So far we have added sections from input object files. |
| 1042 | // This function adds linker-created Out<ELFT>::* sections. |
| 1043 | addPredefinedSections(); |
Rui Ueyama | c38860b | 2016-12-05 21:39:35 +0000 | [diff] [blame] | 1044 | removeUnusedSyntheticSections<ELFT>(OutputSections); |
Rui Ueyama | 84417f8 | 2015-12-26 07:50:41 +0000 | [diff] [blame] | 1045 | |
Rafael Espindola | 24c073d | 2016-09-21 22:36:19 +0000 | [diff] [blame] | 1046 | sortSections(); |
Rui Ueyama | 84417f8 | 2015-12-26 07:50:41 +0000 | [diff] [blame] | 1047 | |
George Rimar | 7ca0627 | 2016-04-06 07:20:45 +0000 | [diff] [blame] | 1048 | unsigned I = 1; |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 1049 | for (OutputSectionBase *Sec : OutputSections) { |
George Rimar | 7ca0627 | 2016-04-06 07:20:45 +0000 | [diff] [blame] | 1050 | Sec->SectionIndex = I++; |
Eugene Leviant | 22eb026 | 2016-11-14 09:16:00 +0000 | [diff] [blame] | 1051 | Sec->ShName = In<ELFT>::ShStrTab->addString(Sec->getName()); |
George Rimar | 7ca0627 | 2016-04-06 07:20:45 +0000 | [diff] [blame] | 1052 | } |
Rui Ueyama | 84417f8 | 2015-12-26 07:50:41 +0000 | [diff] [blame] | 1053 | |
Rafael Espindola | 5967c97 | 2016-12-19 21:21:07 +0000 | [diff] [blame] | 1054 | // Binary and relocatable output does not have PHDRS. |
| 1055 | // The headers have to be created before finalize as that can influence the |
| 1056 | // image base and the dynamic section on mips includes the image base. |
| 1057 | if (!Config->Relocatable && !Config->OFormatBinary) { |
| 1058 | Phdrs = Script<ELFT>::X->hasPhdrsCommands() ? Script<ELFT>::X->createPhdrs() |
| 1059 | : createPhdrs(); |
| 1060 | addPtArmExid(Phdrs); |
| 1061 | fixHeaders(); |
| 1062 | } |
| 1063 | |
Simon Atanasyan | 40d25f3 | 2016-02-02 09:07:47 +0000 | [diff] [blame] | 1064 | // Fill other section headers. The dynamic table is finalized |
| 1065 | // at the end because some tags like RELSZ depend on result |
Rui Ueyama | a959393 | 2016-11-02 02:18:01 +0000 | [diff] [blame] | 1066 | // of finalizing other sections. |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 1067 | for (OutputSectionBase *Sec : OutputSections) |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 1068 | Sec->finalize(); |
Simon Atanasyan | 40d25f3 | 2016-02-02 09:07:47 +0000 | [diff] [blame] | 1069 | |
Eugene Leviant | be809a7 | 2016-11-18 06:44:18 +0000 | [diff] [blame] | 1070 | // Dynamic section must be the last one in this list and dynamic |
| 1071 | // symbol table section (DynSymTab) must be the first one. |
Eugene Leviant | e9bab5d | 2016-11-21 16:59:33 +0000 | [diff] [blame] | 1072 | finalizeSynthetic<ELFT>( |
| 1073 | {In<ELFT>::DynSymTab, In<ELFT>::GnuHashTab, In<ELFT>::HashTab, |
Peter Smith | baffdb8 | 2016-12-08 12:58:55 +0000 | [diff] [blame] | 1074 | In<ELFT>::SymTab, In<ELFT>::ShStrTab, In<ELFT>::StrTab, |
| 1075 | In<ELFT>::VerDef, In<ELFT>::DynStrTab, In<ELFT>::GdbIndex, |
| 1076 | In<ELFT>::Got, In<ELFT>::MipsGot, In<ELFT>::IgotPlt, |
| 1077 | In<ELFT>::GotPlt, In<ELFT>::RelaDyn, In<ELFT>::RelaIplt, |
| 1078 | In<ELFT>::RelaPlt, In<ELFT>::Plt, In<ELFT>::Iplt, |
| 1079 | In<ELFT>::Plt, In<ELFT>::EhFrameHdr, In<ELFT>::VerSym, |
| 1080 | In<ELFT>::VerNeed, In<ELFT>::Dynamic}); |
Rui Ueyama | 84417f8 | 2015-12-26 07:50:41 +0000 | [diff] [blame] | 1081 | } |
| 1082 | |
Rui Ueyama | 84417f8 | 2015-12-26 07:50:41 +0000 | [diff] [blame] | 1083 | template <class ELFT> void Writer<ELFT>::addPredefinedSections() { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1084 | if (Out<ELFT>::Bss->Size > 0) |
George Rimar | 3fb5a6d | 2016-11-29 16:05:27 +0000 | [diff] [blame] | 1085 | OutputSections.push_back(Out<ELFT>::Bss); |
Peter Collingbourne | feb6629 | 2017-01-10 01:21:50 +0000 | [diff] [blame^] | 1086 | if (Out<ELFT>::BssRelRo->Size > 0) |
| 1087 | OutputSections.push_back(Out<ELFT>::BssRelRo); |
Peter Smith | 719eb8e | 2016-11-24 11:43:55 +0000 | [diff] [blame] | 1088 | |
| 1089 | auto OS = dyn_cast_or_null<OutputSection<ELFT>>(findSection(".ARM.exidx")); |
| 1090 | if (OS && !OS->Sections.empty() && !Config->Relocatable) |
| 1091 | OS->addSection(make<ARMExidxSentinelSection<ELFT>>()); |
George Rimar | 3fb5a6d | 2016-11-29 16:05:27 +0000 | [diff] [blame] | 1092 | |
| 1093 | addInputSec(In<ELFT>::SymTab); |
| 1094 | addInputSec(In<ELFT>::ShStrTab); |
| 1095 | addInputSec(In<ELFT>::StrTab); |
Rafael Espindola | abad618 | 2015-08-13 15:23:46 +0000 | [diff] [blame] | 1096 | } |
| 1097 | |
Rui Ueyama | a5d79d1 | 2015-12-26 09:48:00 +0000 | [diff] [blame] | 1098 | // The linker is expected to define SECNAME_start and SECNAME_end |
| 1099 | // symbols for a few sections. This function defines them. |
| 1100 | template <class ELFT> void Writer<ELFT>::addStartEndSymbols() { |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 1101 | auto Define = [&](StringRef Start, StringRef End, OutputSectionBase *OS) { |
Peter Collingbourne | 0fab40b | 2016-10-24 20:46:21 +0000 | [diff] [blame] | 1102 | // These symbols resolve to the image base if the section does not exist. |
Rui Ueyama | 4f2f50d | 2016-12-21 08:40:09 +0000 | [diff] [blame] | 1103 | // A special value -1 indicates end of the section. |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 1104 | addOptionalSynthetic<ELFT>(Start, OS, 0); |
Rui Ueyama | 4f2f50d | 2016-12-21 08:40:09 +0000 | [diff] [blame] | 1105 | addOptionalSynthetic<ELFT>(End, OS, OS ? -1 : 0); |
Rui Ueyama | a5d79d1 | 2015-12-26 09:48:00 +0000 | [diff] [blame] | 1106 | }; |
| 1107 | |
| 1108 | Define("__preinit_array_start", "__preinit_array_end", |
Rui Ueyama | a8f6fea | 2016-08-09 04:25:20 +0000 | [diff] [blame] | 1109 | Out<ELFT>::PreinitArray); |
| 1110 | Define("__init_array_start", "__init_array_end", Out<ELFT>::InitArray); |
| 1111 | Define("__fini_array_start", "__fini_array_end", Out<ELFT>::FiniArray); |
Peter Smith | 17cd375 | 2016-10-27 10:28:53 +0000 | [diff] [blame] | 1112 | |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 1113 | if (OutputSectionBase *Sec = findSection(".ARM.exidx")) |
Peter Smith | 17cd375 | 2016-10-27 10:28:53 +0000 | [diff] [blame] | 1114 | Define("__exidx_start", "__exidx_end", Sec); |
Rui Ueyama | a5d79d1 | 2015-12-26 09:48:00 +0000 | [diff] [blame] | 1115 | } |
| 1116 | |
Rui Ueyama | d9189ce | 2015-10-15 17:11:03 +0000 | [diff] [blame] | 1117 | // If a section name is valid as a C identifier (which is rare because of |
| 1118 | // the leading '.'), linkers are expected to define __start_<secname> and |
| 1119 | // __stop_<secname> symbols. They are at beginning and end of the section, |
| 1120 | // respectively. This is not requested by the ELF standard, but GNU ld and |
| 1121 | // gold provide the feature, and used by many programs. |
| 1122 | template <class ELFT> |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 1123 | void Writer<ELFT>::addStartStopSymbols(OutputSectionBase *Sec) { |
Rui Ueyama | d9189ce | 2015-10-15 17:11:03 +0000 | [diff] [blame] | 1124 | StringRef S = Sec->getName(); |
| 1125 | if (!isValidCIdentifier(S)) |
| 1126 | return; |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 1127 | addOptionalSynthetic<ELFT>(Saver.save("__start_" + S), Sec, 0, STV_DEFAULT); |
Rui Ueyama | 4f2f50d | 2016-12-21 08:40:09 +0000 | [diff] [blame] | 1128 | addOptionalSynthetic<ELFT>(Saver.save("__stop_" + S), Sec, -1, STV_DEFAULT); |
Rui Ueyama | d9189ce | 2015-10-15 17:11:03 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
Rui Ueyama | 30634d9 | 2016-08-09 01:35:38 +0000 | [diff] [blame] | 1131 | template <class ELFT> |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 1132 | OutputSectionBase *Writer<ELFT>::findSection(StringRef Name) { |
| 1133 | for (OutputSectionBase *Sec : OutputSections) |
Rui Ueyama | 30634d9 | 2016-08-09 01:35:38 +0000 | [diff] [blame] | 1134 | if (Sec->getName() == Name) |
| 1135 | return Sec; |
| 1136 | return nullptr; |
| 1137 | } |
| 1138 | |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 1139 | template <class ELFT> static bool needsPtLoad(OutputSectionBase *Sec) { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1140 | if (!(Sec->Flags & SHF_ALLOC)) |
Rafael Espindola | ef762f2 | 2016-02-10 23:29:38 +0000 | [diff] [blame] | 1141 | return false; |
| 1142 | |
| 1143 | // Don't allocate VA space for TLS NOBITS sections. The PT_TLS PHDR is |
| 1144 | // responsible for allocating space for them, not the PT_LOAD that |
| 1145 | // contains the TLS initialization image. |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1146 | if (Sec->Flags & SHF_TLS && Sec->Type == SHT_NOBITS) |
Rafael Espindola | ef762f2 | 2016-02-10 23:29:38 +0000 | [diff] [blame] | 1147 | return false; |
| 1148 | return true; |
Michael J. Spencer | 1d299a8 | 2015-09-09 20:48:09 +0000 | [diff] [blame] | 1149 | } |
| 1150 | |
Rafael Espindola | b45fd70 | 2016-09-20 15:22:27 +0000 | [diff] [blame] | 1151 | // Linker scripts are responsible for aligning addresses. Unfortunately, most |
| 1152 | // linker scripts are designed for creating two PT_LOADs only, one RX and one |
| 1153 | // RW. This means that there is no alignment in the RO to RX transition and we |
| 1154 | // cannot create a PT_LOAD there. |
| 1155 | template <class ELFT> |
| 1156 | static typename ELFT::uint computeFlags(typename ELFT::uint F) { |
George Rimar | 595a763 | 2016-11-29 09:43:51 +0000 | [diff] [blame] | 1157 | if (Config->OMagic) |
| 1158 | return PF_R | PF_W | PF_X; |
George Rimar | 63bf011 | 2016-11-28 10:05:20 +0000 | [diff] [blame] | 1159 | if (Config->SingleRoRx && !(F & PF_W)) |
Rafael Espindola | b45fd70 | 2016-09-20 15:22:27 +0000 | [diff] [blame] | 1160 | return F | PF_X; |
| 1161 | return F; |
| 1162 | } |
| 1163 | |
Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 1164 | // Decide which program headers to create and which sections to include in each |
| 1165 | // one. |
Rafael Espindola | 17cb7c0 | 2016-12-19 17:01:01 +0000 | [diff] [blame] | 1166 | template <class ELFT> std::vector<PhdrEntry> Writer<ELFT>::createPhdrs() { |
| 1167 | std::vector<PhdrEntry> Ret; |
| 1168 | auto AddHdr = [&](unsigned Type, unsigned Flags) -> PhdrEntry * { |
Rui Ueyama | 703296a | 2016-07-20 19:36:39 +0000 | [diff] [blame] | 1169 | Ret.emplace_back(Type, Flags); |
| 1170 | return &Ret.back(); |
Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 1171 | }; |
George Rimar | e3336c0 | 2015-11-24 10:15:50 +0000 | [diff] [blame] | 1172 | |
Rui Ueyama | 803195e | 2015-10-23 21:45:59 +0000 | [diff] [blame] | 1173 | // The first phdr entry is PT_PHDR which describes the program header itself. |
Rafael Espindola | 17cb7c0 | 2016-12-19 17:01:01 +0000 | [diff] [blame] | 1174 | PhdrEntry &Hdr = *AddHdr(PT_PHDR, PF_R); |
Rui Ueyama | 18f084f | 2016-07-20 19:36:41 +0000 | [diff] [blame] | 1175 | Hdr.add(Out<ELFT>::ProgramHeaders); |
Rui Ueyama | 953c2c4 | 2015-10-10 23:59:57 +0000 | [diff] [blame] | 1176 | |
Rui Ueyama | 803195e | 2015-10-23 21:45:59 +0000 | [diff] [blame] | 1177 | // PT_INTERP must be the second entry if exists. |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 1178 | if (OutputSectionBase *Sec = findSection(".interp")) { |
Rafael Espindola | 17cb7c0 | 2016-12-19 17:01:01 +0000 | [diff] [blame] | 1179 | PhdrEntry &Hdr = *AddHdr(PT_INTERP, Sec->getPhdrFlags()); |
Rui Ueyama | e288eef | 2016-11-02 18:58:44 +0000 | [diff] [blame] | 1180 | Hdr.add(Sec); |
Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 1181 | } |
Rafael Espindola | 7010776 | 2015-09-11 18:49:42 +0000 | [diff] [blame] | 1182 | |
Rui Ueyama | 803195e | 2015-10-23 21:45:59 +0000 | [diff] [blame] | 1183 | // Add the first PT_LOAD segment for regular output sections. |
Rafael Espindola | b45fd70 | 2016-09-20 15:22:27 +0000 | [diff] [blame] | 1184 | uintX_t Flags = computeFlags<ELFT>(PF_R); |
Rafael Espindola | 17cb7c0 | 2016-12-19 17:01:01 +0000 | [diff] [blame] | 1185 | PhdrEntry *Load = AddHdr(PT_LOAD, Flags); |
Rafael Espindola | 0a2e211 | 2015-09-10 15:41:34 +0000 | [diff] [blame] | 1186 | |
Rafael Espindola | 17cb7c0 | 2016-12-19 17:01:01 +0000 | [diff] [blame] | 1187 | PhdrEntry TlsHdr(PT_TLS, PF_R); |
| 1188 | PhdrEntry RelRo(PT_GNU_RELRO, PF_R); |
| 1189 | PhdrEntry Note(PT_NOTE, PF_R); |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 1190 | for (OutputSectionBase *Sec : OutputSections) { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1191 | if (!(Sec->Flags & SHF_ALLOC)) |
Rafael Espindola | aab6d5c | 2016-09-16 21:29:07 +0000 | [diff] [blame] | 1192 | break; |
Rafael Espindola | 97cbe3e3 | 2015-12-23 15:20:38 +0000 | [diff] [blame] | 1193 | |
Rafael Espindola | ef762f2 | 2016-02-10 23:29:38 +0000 | [diff] [blame] | 1194 | // If we meet TLS section then we create TLS header |
Davide Italiano | 8f41f33 | 2016-09-20 16:57:02 +0000 | [diff] [blame] | 1195 | // and put all TLS sections inside for further use when |
Rafael Espindola | ef762f2 | 2016-02-10 23:29:38 +0000 | [diff] [blame] | 1196 | // assign addresses. |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1197 | if (Sec->Flags & SHF_TLS) |
Rui Ueyama | 18f084f | 2016-07-20 19:36:41 +0000 | [diff] [blame] | 1198 | TlsHdr.add(Sec); |
Rafael Espindola | ef762f2 | 2016-02-10 23:29:38 +0000 | [diff] [blame] | 1199 | |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 1200 | if (!needsPtLoad<ELFT>(Sec)) |
Rafael Espindola | ef762f2 | 2016-02-10 23:29:38 +0000 | [diff] [blame] | 1201 | continue; |
| 1202 | |
George Rimar | 8ceadb3 | 2016-08-17 07:44:19 +0000 | [diff] [blame] | 1203 | // Segments are contiguous memory regions that has the same attributes |
| 1204 | // (e.g. executable or writable). There is one phdr for each segment. |
| 1205 | // Therefore, we need to create a new phdr when the next section has |
| 1206 | // different flags or is loaded at a discontiguous address using AT linker |
| 1207 | // script command. |
Rafael Espindola | b45fd70 | 2016-09-20 15:22:27 +0000 | [diff] [blame] | 1208 | uintX_t NewFlags = computeFlags<ELFT>(Sec->getPhdrFlags()); |
Eugene Leviant | b71d6f7 | 2016-10-06 09:39:28 +0000 | [diff] [blame] | 1209 | if (Script<ELFT>::X->hasLMA(Sec->getName()) || Flags != NewFlags) { |
Rafael Espindola | e090fb2 | 2016-03-09 21:37:22 +0000 | [diff] [blame] | 1210 | Load = AddHdr(PT_LOAD, NewFlags); |
Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 1211 | Flags = NewFlags; |
| 1212 | } |
Michael J. Spencer | 78aa1de | 2015-11-03 00:34:39 +0000 | [diff] [blame] | 1213 | |
Rui Ueyama | 18f084f | 2016-07-20 19:36:41 +0000 | [diff] [blame] | 1214 | Load->add(Sec); |
Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 1215 | |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 1216 | if (isRelroSection<ELFT>(Sec)) |
Rui Ueyama | 18f084f | 2016-07-20 19:36:41 +0000 | [diff] [blame] | 1217 | RelRo.add(Sec); |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1218 | if (Sec->Type == SHT_NOTE) |
Rui Ueyama | 18f084f | 2016-07-20 19:36:41 +0000 | [diff] [blame] | 1219 | Note.add(Sec); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 1220 | } |
Rafael Espindola | 6b83b90 | 2015-08-12 00:00:24 +0000 | [diff] [blame] | 1221 | |
Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 1222 | // Add the TLS segment unless it's empty. |
| 1223 | if (TlsHdr.First) |
Rui Ueyama | 703296a | 2016-07-20 19:36:39 +0000 | [diff] [blame] | 1224 | Ret.push_back(std::move(TlsHdr)); |
Michael J. Spencer | 78aa1de | 2015-11-03 00:34:39 +0000 | [diff] [blame] | 1225 | |
Rui Ueyama | 803195e | 2015-10-23 21:45:59 +0000 | [diff] [blame] | 1226 | // Add an entry for .dynamic. |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1227 | if (In<ELFT>::DynSymTab) { |
Rafael Espindola | 17cb7c0 | 2016-12-19 17:01:01 +0000 | [diff] [blame] | 1228 | PhdrEntry &H = |
| 1229 | *AddHdr(PT_DYNAMIC, In<ELFT>::Dynamic->OutSec->getPhdrFlags()); |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 1230 | H.add(In<ELFT>::Dynamic->OutSec); |
Rui Ueyama | 2f1b79f | 2015-10-10 22:34:30 +0000 | [diff] [blame] | 1231 | } |
Rafael Espindola | 91009b3 | 2015-08-12 01:45:28 +0000 | [diff] [blame] | 1232 | |
Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 1233 | // PT_GNU_RELRO includes all sections that should be marked as |
| 1234 | // read-only by dynamic linker after proccessing relocations. |
| 1235 | if (RelRo.First) |
Rui Ueyama | 703296a | 2016-07-20 19:36:39 +0000 | [diff] [blame] | 1236 | Ret.push_back(std::move(RelRo)); |
George Rimar | e3336c0 | 2015-11-24 10:15:50 +0000 | [diff] [blame] | 1237 | |
Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 1238 | // PT_GNU_EH_FRAME is a special section pointing on .eh_frame_hdr. |
Eugene Leviant | 952eb4d | 2016-11-21 15:52:10 +0000 | [diff] [blame] | 1239 | if (!Out<ELFT>::EhFrame->empty() && In<ELFT>::EhFrameHdr) { |
Rafael Espindola | 17cb7c0 | 2016-12-19 17:01:01 +0000 | [diff] [blame] | 1240 | PhdrEntry &Hdr = |
Eugene Leviant | 952eb4d | 2016-11-21 15:52:10 +0000 | [diff] [blame] | 1241 | *AddHdr(PT_GNU_EH_FRAME, In<ELFT>::EhFrameHdr->OutSec->getPhdrFlags()); |
| 1242 | Hdr.add(In<ELFT>::EhFrameHdr->OutSec); |
George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 1243 | } |
| 1244 | |
George Rimar | 270173f | 2016-10-14 13:02:22 +0000 | [diff] [blame] | 1245 | // PT_OPENBSD_RANDOMIZE specifies the location and size of a part of the |
| 1246 | // memory image of the program that must be filled with random data before any |
| 1247 | // code in the object is executed. |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 1248 | if (OutputSectionBase *Sec = findSection(".openbsd.randomdata")) { |
Rafael Espindola | 17cb7c0 | 2016-12-19 17:01:01 +0000 | [diff] [blame] | 1249 | PhdrEntry &Hdr = *AddHdr(PT_OPENBSD_RANDOMIZE, Sec->getPhdrFlags()); |
George Rimar | 270173f | 2016-10-14 13:02:22 +0000 | [diff] [blame] | 1250 | Hdr.add(Sec); |
| 1251 | } |
| 1252 | |
Rui Ueyama | e79b09a | 2015-11-21 22:19:32 +0000 | [diff] [blame] | 1253 | // PT_GNU_STACK is a special section to tell the loader to make the |
| 1254 | // pages for the stack non-executable. |
Rui Ueyama | 110dda7 | 2016-10-11 17:46:48 +0000 | [diff] [blame] | 1255 | if (!Config->ZExecstack) { |
Rafael Espindola | 17cb7c0 | 2016-12-19 17:01:01 +0000 | [diff] [blame] | 1256 | PhdrEntry &Hdr = *AddHdr(PT_GNU_STACK, PF_R | PF_W); |
Petr Hosek | 1fb2ecc | 2016-08-17 23:59:58 +0000 | [diff] [blame] | 1257 | if (Config->ZStackSize != uint64_t(-1)) |
Rafael Espindola | 17cb7c0 | 2016-12-19 17:01:01 +0000 | [diff] [blame] | 1258 | Hdr.p_memsz = Config->ZStackSize; |
Petr Hosek | 1fb2ecc | 2016-08-17 23:59:58 +0000 | [diff] [blame] | 1259 | } |
Rafael Espindola | 9907eb0 | 2016-03-01 13:23:29 +0000 | [diff] [blame] | 1260 | |
George Rimar | cc6e567 | 2016-10-14 10:34:36 +0000 | [diff] [blame] | 1261 | // PT_OPENBSD_WXNEEDED is a OpenBSD-specific header to mark the executable |
| 1262 | // is expected to perform W^X violations, such as calling mprotect(2) or |
| 1263 | // mmap(2) with PROT_WRITE | PROT_EXEC, which is prohibited by default on |
| 1264 | // OpenBSD. |
| 1265 | if (Config->ZWxneeded) |
| 1266 | AddHdr(PT_OPENBSD_WXNEEDED, PF_X); |
| 1267 | |
Rafael Espindola | 9907eb0 | 2016-03-01 13:23:29 +0000 | [diff] [blame] | 1268 | if (Note.First) |
Rui Ueyama | 703296a | 2016-07-20 19:36:39 +0000 | [diff] [blame] | 1269 | Ret.push_back(std::move(Note)); |
| 1270 | return Ret; |
Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 1271 | } |
| 1272 | |
Rafael Espindola | 8e67000 | 2016-11-28 00:40:21 +0000 | [diff] [blame] | 1273 | template <class ELFT> |
Rafael Espindola | 17cb7c0 | 2016-12-19 17:01:01 +0000 | [diff] [blame] | 1274 | void Writer<ELFT>::addPtArmExid(std::vector<PhdrEntry> &Phdrs) { |
Rafael Espindola | 8e67000 | 2016-11-28 00:40:21 +0000 | [diff] [blame] | 1275 | if (Config->EMachine != EM_ARM) |
| 1276 | return; |
| 1277 | auto I = std::find_if( |
| 1278 | OutputSections.begin(), OutputSections.end(), |
| 1279 | [](OutputSectionBase *Sec) { return Sec->Type == SHT_ARM_EXIDX; }); |
| 1280 | if (I == OutputSections.end()) |
| 1281 | return; |
| 1282 | |
| 1283 | // PT_ARM_EXIDX is the ARM EHABI equivalent of PT_GNU_EH_FRAME |
Rafael Espindola | 17cb7c0 | 2016-12-19 17:01:01 +0000 | [diff] [blame] | 1284 | PhdrEntry ARMExidx(PT_ARM_EXIDX, PF_R); |
Rafael Espindola | 8e67000 | 2016-11-28 00:40:21 +0000 | [diff] [blame] | 1285 | ARMExidx.add(*I); |
| 1286 | Phdrs.push_back(ARMExidx); |
| 1287 | } |
| 1288 | |
Peter Collingbourne | 628ec9f | 2017-01-10 01:21:30 +0000 | [diff] [blame] | 1289 | // The first section of each PT_LOAD, the first section in PT_GNU_RELRO and the |
| 1290 | // first section after PT_GNU_RELRO have to be page aligned so that the dynamic |
| 1291 | // linker can set the permissions. |
Rui Ueyama | 4709190 | 2016-03-30 19:41:51 +0000 | [diff] [blame] | 1292 | template <class ELFT> void Writer<ELFT>::fixSectionAlignments() { |
Rafael Espindola | 17cb7c0 | 2016-12-19 17:01:01 +0000 | [diff] [blame] | 1293 | for (const PhdrEntry &P : Phdrs) |
Rafael Espindola | 5967c97 | 2016-12-19 21:21:07 +0000 | [diff] [blame] | 1294 | if (P.p_type == PT_LOAD && P.First) |
Rui Ueyama | 4709190 | 2016-03-30 19:41:51 +0000 | [diff] [blame] | 1295 | P.First->PageAlign = true; |
| 1296 | |
Rafael Espindola | 17cb7c0 | 2016-12-19 17:01:01 +0000 | [diff] [blame] | 1297 | for (const PhdrEntry &P : Phdrs) { |
| 1298 | if (P.p_type != PT_GNU_RELRO) |
Rui Ueyama | 4709190 | 2016-03-30 19:41:51 +0000 | [diff] [blame] | 1299 | continue; |
Peter Collingbourne | 628ec9f | 2017-01-10 01:21:30 +0000 | [diff] [blame] | 1300 | if (P.First) |
| 1301 | P.First->PageAlign = true; |
Rui Ueyama | 4709190 | 2016-03-30 19:41:51 +0000 | [diff] [blame] | 1302 | // Find the first section after PT_GNU_RELRO. If it is in a PT_LOAD we |
| 1303 | // have to align it to a page. |
| 1304 | auto End = OutputSections.end(); |
| 1305 | auto I = std::find(OutputSections.begin(), End, P.Last); |
| 1306 | if (I == End || (I + 1) == End) |
| 1307 | continue; |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 1308 | OutputSectionBase *Sec = *(I + 1); |
| 1309 | if (needsPtLoad<ELFT>(Sec)) |
Rui Ueyama | 4709190 | 2016-03-30 19:41:51 +0000 | [diff] [blame] | 1310 | Sec->PageAlign = true; |
| 1311 | } |
| 1312 | } |
| 1313 | |
Rafael Espindola | 5967c97 | 2016-12-19 21:21:07 +0000 | [diff] [blame] | 1314 | template <class ELFT> |
| 1315 | void elf::allocateHeaders(MutableArrayRef<PhdrEntry> Phdrs, |
| 1316 | ArrayRef<OutputSectionBase *> OutputSections) { |
| 1317 | auto FirstPTLoad = |
| 1318 | std::find_if(Phdrs.begin(), Phdrs.end(), |
| 1319 | [](const PhdrEntry &E) { return E.p_type == PT_LOAD; }); |
| 1320 | if (FirstPTLoad == Phdrs.end()) |
| 1321 | return; |
| 1322 | if (FirstPTLoad->First) |
| 1323 | for (OutputSectionBase *Sec : OutputSections) |
| 1324 | if (Sec->FirstInPtLoad == FirstPTLoad->First) |
| 1325 | Sec->FirstInPtLoad = Out<ELFT>::ElfHeader; |
| 1326 | FirstPTLoad->First = Out<ELFT>::ElfHeader; |
| 1327 | if (!FirstPTLoad->Last) |
| 1328 | FirstPTLoad->Last = Out<ELFT>::ProgramHeaders; |
| 1329 | } |
| 1330 | |
George Rimar | 7ca0627 | 2016-04-06 07:20:45 +0000 | [diff] [blame] | 1331 | // We should set file offsets and VAs for elf header and program headers |
| 1332 | // sections. These are special, we do not include them into output sections |
| 1333 | // list, but have them to simplify the code. |
| 1334 | template <class ELFT> void Writer<ELFT>::fixHeaders() { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1335 | Out<ELFT>::ProgramHeaders->Size = sizeof(Elf_Phdr) * Phdrs.size(); |
Rafael Espindola | 1c57007 | 2016-11-21 19:59:33 +0000 | [diff] [blame] | 1336 | // If the script has SECTIONS, assignAddresses will compute the values. |
| 1337 | if (ScriptConfig->HasSections) |
| 1338 | return; |
Rafael Espindola | 5967c97 | 2016-12-19 21:21:07 +0000 | [diff] [blame] | 1339 | |
| 1340 | uintX_t HeaderSize = getHeaderSize<ELFT>(); |
| 1341 | // When -T<section> option is specified, lower the base to make room for those |
| 1342 | // sections. |
| 1343 | if (!Config->SectionStartMap.empty()) { |
| 1344 | uint64_t Min = -1; |
| 1345 | for (const auto &P : Config->SectionStartMap) |
| 1346 | Min = std::min(Min, P.second); |
| 1347 | if (HeaderSize < Min) |
| 1348 | Min -= HeaderSize; |
| 1349 | else |
| 1350 | AllocateHeader = false; |
| 1351 | if (Min < Config->ImageBase) |
| 1352 | Config->ImageBase = alignDown(Min, Config->MaxPageSize); |
| 1353 | } |
| 1354 | |
| 1355 | if (AllocateHeader) |
| 1356 | allocateHeaders<ELFT>(Phdrs, OutputSections); |
| 1357 | |
Rafael Espindola | 1c57007 | 2016-11-21 19:59:33 +0000 | [diff] [blame] | 1358 | uintX_t BaseVA = Config->ImageBase; |
| 1359 | Out<ELFT>::ElfHeader->Addr = BaseVA; |
| 1360 | Out<ELFT>::ProgramHeaders->Addr = BaseVA + Out<ELFT>::ElfHeader->Size; |
George Rimar | 7ca0627 | 2016-04-06 07:20:45 +0000 | [diff] [blame] | 1361 | } |
| 1362 | |
Rui Ueyama | e044e9c | 2016-04-01 17:07:17 +0000 | [diff] [blame] | 1363 | // Assign VAs (addresses at run-time) to output sections. |
Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 1364 | template <class ELFT> void Writer<ELFT>::assignAddresses() { |
Rafael Espindola | 5967c97 | 2016-12-19 21:21:07 +0000 | [diff] [blame] | 1365 | uintX_t VA = Config->ImageBase; |
| 1366 | if (AllocateHeader) |
| 1367 | VA += getHeaderSize<ELFT>(); |
George Rimar | 7ca0627 | 2016-04-06 07:20:45 +0000 | [diff] [blame] | 1368 | uintX_t ThreadBssOffset = 0; |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 1369 | for (OutputSectionBase *Sec : OutputSections) { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1370 | uintX_t Alignment = Sec->Addralign; |
Rui Ueyama | 4709190 | 2016-03-30 19:41:51 +0000 | [diff] [blame] | 1371 | if (Sec->PageAlign) |
Petr Hosek | 997f883 | 2016-09-28 15:20:47 +0000 | [diff] [blame] | 1372 | Alignment = std::max<uintX_t>(Alignment, Config->MaxPageSize); |
Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 1373 | |
George Rimar | d73ef17 | 2016-09-14 13:07:13 +0000 | [diff] [blame] | 1374 | auto I = Config->SectionStartMap.find(Sec->getName()); |
| 1375 | if (I != Config->SectionStartMap.end()) |
| 1376 | VA = I->second; |
| 1377 | |
Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 1378 | // We only assign VAs to allocated sections. |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 1379 | if (needsPtLoad<ELFT>(Sec)) { |
Rui Ueyama | 424b408 | 2016-06-17 01:18:46 +0000 | [diff] [blame] | 1380 | VA = alignTo(VA, Alignment); |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1381 | Sec->Addr = VA; |
| 1382 | VA += Sec->Size; |
| 1383 | } else if (Sec->Flags & SHF_TLS && Sec->Type == SHT_NOBITS) { |
Rafael Espindola | ef762f2 | 2016-02-10 23:29:38 +0000 | [diff] [blame] | 1384 | uintX_t TVA = VA + ThreadBssOffset; |
Rui Ueyama | 424b408 | 2016-06-17 01:18:46 +0000 | [diff] [blame] | 1385 | TVA = alignTo(TVA, Alignment); |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1386 | Sec->Addr = TVA; |
| 1387 | ThreadBssOffset = TVA - VA + Sec->Size; |
Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 1388 | } |
Rui Ueyama | 69960ba | 2015-10-10 23:25:39 +0000 | [diff] [blame] | 1389 | } |
George Rimar | 900a260 | 2016-04-01 10:49:14 +0000 | [diff] [blame] | 1390 | } |
| 1391 | |
George Rimar | 5f85732 | 2016-04-27 09:16:28 +0000 | [diff] [blame] | 1392 | // Adjusts the file alignment for a given output section and returns |
| 1393 | // its new file offset. The file offset must be the same with its |
| 1394 | // virtual address (modulo the page size) so that the loader can load |
| 1395 | // executables without any address adjustment. |
| 1396 | template <class ELFT, class uintX_t> |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 1397 | static uintX_t getFileAlignment(uintX_t Off, OutputSectionBase *Sec) { |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 1398 | OutputSectionBase *First = Sec->FirstInPtLoad; |
Rafael Espindola | 8b8f74f | 2016-12-07 20:20:39 +0000 | [diff] [blame] | 1399 | // If the section is not in a PT_LOAD, we just have to align it. |
Rafael Espindola | c94678b | 2016-09-29 16:29:55 +0000 | [diff] [blame] | 1400 | if (!First) |
Rafael Espindola | 8b8f74f | 2016-12-07 20:20:39 +0000 | [diff] [blame] | 1401 | return alignTo(Off, Sec->Addralign); |
Eugene Leviant | 3d9abec | 2016-09-29 09:20:33 +0000 | [diff] [blame] | 1402 | |
Rafael Espindola | 8b8f74f | 2016-12-07 20:20:39 +0000 | [diff] [blame] | 1403 | // The first section in a PT_LOAD has to have congruent offset and address |
| 1404 | // module the page size. |
Rafael Espindola | c94678b | 2016-09-29 16:29:55 +0000 | [diff] [blame] | 1405 | if (Sec == First) |
Rafael Espindola | a86a9c6 | 2016-12-07 20:10:43 +0000 | [diff] [blame] | 1406 | return alignTo(Off, Config->MaxPageSize, Sec->Addr); |
Rafael Espindola | 8b8f74f | 2016-12-07 20:20:39 +0000 | [diff] [blame] | 1407 | |
| 1408 | // If two sections share the same PT_LOAD the file offset is calculated |
| 1409 | // using this formula: Off2 = Off1 + (VA2 - VA1). |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1410 | return First->Offset + Sec->Addr - First->Addr; |
George Rimar | 5f85732 | 2016-04-27 09:16:28 +0000 | [diff] [blame] | 1411 | } |
| 1412 | |
George Rimar | 86ce267 | 2016-08-25 09:05:47 +0000 | [diff] [blame] | 1413 | template <class ELFT, class uintX_t> |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 1414 | void setOffset(OutputSectionBase *Sec, uintX_t &Off) { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1415 | if (Sec->Type == SHT_NOBITS) { |
| 1416 | Sec->Offset = Off; |
George Rimar | 86ce267 | 2016-08-25 09:05:47 +0000 | [diff] [blame] | 1417 | return; |
| 1418 | } |
| 1419 | |
| 1420 | Off = getFileAlignment<ELFT>(Off, Sec); |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1421 | Sec->Offset = Off; |
| 1422 | Off += Sec->Size; |
George Rimar | 86ce267 | 2016-08-25 09:05:47 +0000 | [diff] [blame] | 1423 | } |
| 1424 | |
| 1425 | template <class ELFT> void Writer<ELFT>::assignFileOffsetsBinary() { |
| 1426 | uintX_t Off = 0; |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 1427 | for (OutputSectionBase *Sec : OutputSections) |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1428 | if (Sec->Flags & SHF_ALLOC) |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 1429 | setOffset<ELFT>(Sec, Off); |
George Rimar | 86ce267 | 2016-08-25 09:05:47 +0000 | [diff] [blame] | 1430 | FileSize = alignTo(Off, sizeof(uintX_t)); |
| 1431 | } |
| 1432 | |
Rui Ueyama | e044e9c | 2016-04-01 17:07:17 +0000 | [diff] [blame] | 1433 | // Assign file offsets to output sections. |
| 1434 | template <class ELFT> void Writer<ELFT>::assignFileOffsets() { |
Eugene Leviant | 467c4d5 | 2016-07-01 10:27:36 +0000 | [diff] [blame] | 1435 | uintX_t Off = 0; |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 1436 | setOffset<ELFT>(Out<ELFT>::ElfHeader, Off); |
| 1437 | setOffset<ELFT>(Out<ELFT>::ProgramHeaders, Off); |
George Rimar | 7ca0627 | 2016-04-06 07:20:45 +0000 | [diff] [blame] | 1438 | |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 1439 | for (OutputSectionBase *Sec : OutputSections) |
| 1440 | setOffset<ELFT>(Sec, Off); |
Eugene Leviant | 467c4d5 | 2016-07-01 10:27:36 +0000 | [diff] [blame] | 1441 | |
Rui Ueyama | e044e9c | 2016-04-01 17:07:17 +0000 | [diff] [blame] | 1442 | SectionHeaderOff = alignTo(Off, sizeof(uintX_t)); |
George Rimar | 7ca0627 | 2016-04-06 07:20:45 +0000 | [diff] [blame] | 1443 | FileSize = SectionHeaderOff + (OutputSections.size() + 1) * sizeof(Elf_Shdr); |
Rui Ueyama | e044e9c | 2016-04-01 17:07:17 +0000 | [diff] [blame] | 1444 | } |
| 1445 | |
| 1446 | // Finalize the program headers. We call this function after we assign |
| 1447 | // file offsets and VAs to all sections. |
| 1448 | template <class ELFT> void Writer<ELFT>::setPhdrs() { |
Rafael Espindola | 17cb7c0 | 2016-12-19 17:01:01 +0000 | [diff] [blame] | 1449 | for (PhdrEntry &P : Phdrs) { |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 1450 | OutputSectionBase *First = P.First; |
| 1451 | OutputSectionBase *Last = P.Last; |
Rui Ueyama | e8a45e4 | 2016-04-01 22:42:04 +0000 | [diff] [blame] | 1452 | if (First) { |
Rafael Espindola | 17cb7c0 | 2016-12-19 17:01:01 +0000 | [diff] [blame] | 1453 | P.p_filesz = Last->Offset - First->Offset; |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1454 | if (Last->Type != SHT_NOBITS) |
Rafael Espindola | 17cb7c0 | 2016-12-19 17:01:01 +0000 | [diff] [blame] | 1455 | P.p_filesz += Last->Size; |
| 1456 | P.p_memsz = Last->Addr + Last->Size - First->Addr; |
| 1457 | P.p_offset = First->Offset; |
| 1458 | P.p_vaddr = First->Addr; |
Eugene Leviant | b71d6f7 | 2016-10-06 09:39:28 +0000 | [diff] [blame] | 1459 | if (!P.HasLMA) |
Rafael Espindola | 17cb7c0 | 2016-12-19 17:01:01 +0000 | [diff] [blame] | 1460 | P.p_paddr = First->getLMA(); |
Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 1461 | } |
Rafael Espindola | 17cb7c0 | 2016-12-19 17:01:01 +0000 | [diff] [blame] | 1462 | if (P.p_type == PT_LOAD) |
| 1463 | P.p_align = Config->MaxPageSize; |
Peter Collingbourne | 7b5088b | 2017-01-04 18:56:15 +0000 | [diff] [blame] | 1464 | else if (P.p_type == PT_GNU_RELRO) { |
Rafael Espindola | 17cb7c0 | 2016-12-19 17:01:01 +0000 | [diff] [blame] | 1465 | P.p_align = 1; |
Peter Collingbourne | 7b5088b | 2017-01-04 18:56:15 +0000 | [diff] [blame] | 1466 | // The glibc dynamic loader rounds the size down, so we need to round up |
| 1467 | // to protect the last page. This is a no-op on FreeBSD which always |
| 1468 | // rounds up. |
| 1469 | P.p_memsz = alignTo(P.p_memsz, Config->MaxPageSize); |
| 1470 | } |
George Rimar | 8ceadb3 | 2016-08-17 07:44:19 +0000 | [diff] [blame] | 1471 | |
Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 1472 | // The TLS pointer goes after PT_TLS. At least glibc will align it, |
| 1473 | // so round up the size to make sure the offsets are correct. |
Rafael Espindola | 17cb7c0 | 2016-12-19 17:01:01 +0000 | [diff] [blame] | 1474 | if (P.p_type == PT_TLS) { |
| 1475 | Out<ELFT>::TlsPhdr = &P; |
| 1476 | if (P.p_memsz) |
| 1477 | P.p_memsz = alignTo(P.p_memsz, P.p_align); |
Rui Ueyama | 803195e | 2015-10-23 21:45:59 +0000 | [diff] [blame] | 1478 | } |
| 1479 | } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 1480 | } |
| 1481 | |
Rui Ueyama | 2eda6d1 | 2016-11-23 22:41:00 +0000 | [diff] [blame] | 1482 | // The entry point address is chosen in the following ways. |
| 1483 | // |
| 1484 | // 1. the '-e' entry command-line option; |
| 1485 | // 2. the ENTRY(symbol) command in a linker control script; |
| 1486 | // 3. the value of the symbol start, if present; |
| 1487 | // 4. the address of the first byte of the .text section, if present; |
| 1488 | // 5. the address 0. |
| 1489 | template <class ELFT> typename ELFT::uint Writer<ELFT>::getEntryAddr() { |
Rui Ueyama | a1407c4 | 2016-12-07 03:23:06 +0000 | [diff] [blame] | 1490 | // Case 1, 2 or 3. As a special case, if the symbol is actually |
| 1491 | // a number, we'll use that number as an address. |
Rui Ueyama | 8da7aa0 | 2016-10-20 00:07:36 +0000 | [diff] [blame] | 1492 | if (SymbolBody *B = Symtab<ELFT>::X->find(Config->Entry)) |
| 1493 | return B->getVA<ELFT>(); |
Rui Ueyama | a1407c4 | 2016-12-07 03:23:06 +0000 | [diff] [blame] | 1494 | uint64_t Addr; |
| 1495 | if (!Config->Entry.getAsInteger(0, Addr)) |
| 1496 | return Addr; |
Rui Ueyama | 2eda6d1 | 2016-11-23 22:41:00 +0000 | [diff] [blame] | 1497 | |
| 1498 | // Case 4 |
| 1499 | if (OutputSectionBase *Sec = findSection(".text")) { |
Rui Ueyama | 9e5f5ef | 2016-12-07 04:06:21 +0000 | [diff] [blame] | 1500 | if (Config->WarnMissingEntry) |
Petr Hosek | 2f50fef | 2016-12-07 02:26:16 +0000 | [diff] [blame] | 1501 | warn("cannot find entry symbol " + Config->Entry + "; defaulting to 0x" + |
| 1502 | utohexstr(Sec->Addr)); |
Rui Ueyama | 2eda6d1 | 2016-11-23 22:41:00 +0000 | [diff] [blame] | 1503 | return Sec->Addr; |
| 1504 | } |
| 1505 | |
| 1506 | // Case 5 |
Rui Ueyama | 9e5f5ef | 2016-12-07 04:06:21 +0000 | [diff] [blame] | 1507 | if (Config->WarnMissingEntry) |
Petr Hosek | 2f50fef | 2016-12-07 02:26:16 +0000 | [diff] [blame] | 1508 | warn("cannot find entry symbol " + Config->Entry + |
| 1509 | "; not setting start address"); |
Rui Ueyama | 8da7aa0 | 2016-10-20 00:07:36 +0000 | [diff] [blame] | 1510 | return 0; |
Rui Ueyama | 3bfaba9 | 2015-12-24 08:37:34 +0000 | [diff] [blame] | 1511 | } |
| 1512 | |
Rui Ueyama | 4cea4e8 | 2016-02-25 19:28:37 +0000 | [diff] [blame] | 1513 | template <class ELFT> static uint8_t getELFEncoding() { |
| 1514 | if (ELFT::TargetEndianness == llvm::support::little) |
| 1515 | return ELFDATA2LSB; |
| 1516 | return ELFDATA2MSB; |
| 1517 | } |
| 1518 | |
| 1519 | static uint16_t getELFType() { |
George Rimar | 786e866 | 2016-03-17 05:57:33 +0000 | [diff] [blame] | 1520 | if (Config->Pic) |
Rui Ueyama | 4cea4e8 | 2016-02-25 19:28:37 +0000 | [diff] [blame] | 1521 | return ET_DYN; |
| 1522 | if (Config->Relocatable) |
| 1523 | return ET_REL; |
| 1524 | return ET_EXEC; |
| 1525 | } |
| 1526 | |
Rui Ueyama | 1a311f1 | 2015-12-26 10:52:26 +0000 | [diff] [blame] | 1527 | // This function is called after we have assigned address and size |
| 1528 | // to each section. This function fixes some predefined absolute |
| 1529 | // symbol values that depend on section address and size. |
| 1530 | template <class ELFT> void Writer<ELFT>::fixAbsoluteSymbols() { |
Petr Hosek | 4f7a5c3 | 2016-08-22 18:44:04 +0000 | [diff] [blame] | 1531 | // __ehdr_start is the location of program headers. |
| 1532 | if (ElfSym<ELFT>::EhdrStart) |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1533 | ElfSym<ELFT>::EhdrStart->Value = Out<ELFT>::ProgramHeaders->Addr; |
Petr Hosek | 4f7a5c3 | 2016-08-22 18:44:04 +0000 | [diff] [blame] | 1534 | |
George Rimar | d5d5447 | 2016-06-20 13:30:37 +0000 | [diff] [blame] | 1535 | auto Set = [](DefinedRegular<ELFT> *S1, DefinedRegular<ELFT> *S2, uintX_t V) { |
Rui Ueyama | 467dbdd | 2016-04-21 20:50:15 +0000 | [diff] [blame] | 1536 | if (S1) |
| 1537 | S1->Value = V; |
| 1538 | if (S2) |
| 1539 | S2->Value = V; |
| 1540 | }; |
| 1541 | |
George Rimar | 2abc587 | 2016-03-01 19:18:07 +0000 | [diff] [blame] | 1542 | // _etext is the first location after the last read-only loadable segment. |
| 1543 | // _edata is the first location after the last read-write loadable segment. |
George Rimar | efded31 | 2016-04-01 10:23:32 +0000 | [diff] [blame] | 1544 | // _end is the first location after the uninitialized data region. |
Rafael Espindola | 17cb7c0 | 2016-12-19 17:01:01 +0000 | [diff] [blame] | 1545 | for (PhdrEntry &P : Phdrs) { |
| 1546 | if (P.p_type != PT_LOAD) |
George Rimar | 9e85939 | 2016-02-26 14:36:36 +0000 | [diff] [blame] | 1547 | continue; |
Rafael Espindola | 17cb7c0 | 2016-12-19 17:01:01 +0000 | [diff] [blame] | 1548 | Set(ElfSym<ELFT>::End, ElfSym<ELFT>::End2, P.p_vaddr + P.p_memsz); |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 1549 | |
Rafael Espindola | 17cb7c0 | 2016-12-19 17:01:01 +0000 | [diff] [blame] | 1550 | uintX_t Val = P.p_vaddr + P.p_filesz; |
| 1551 | if (P.p_flags & PF_W) |
Rui Ueyama | 467dbdd | 2016-04-21 20:50:15 +0000 | [diff] [blame] | 1552 | Set(ElfSym<ELFT>::Edata, ElfSym<ELFT>::Edata2, Val); |
George Rimar | 8bbff7e | 2016-04-14 14:37:59 +0000 | [diff] [blame] | 1553 | else |
Rui Ueyama | 467dbdd | 2016-04-21 20:50:15 +0000 | [diff] [blame] | 1554 | Set(ElfSym<ELFT>::Etext, ElfSym<ELFT>::Etext2, Val); |
George Rimar | 9e85939 | 2016-02-26 14:36:36 +0000 | [diff] [blame] | 1555 | } |
Simon Atanasyan | 8469b88 | 2016-11-23 22:22:16 +0000 | [diff] [blame] | 1556 | |
| 1557 | // Setup MIPS _gp_disp/__gnu_local_gp symbols which should |
| 1558 | // be equal to the _gp symbol's value. |
| 1559 | if (Config->EMachine == EM_MIPS) { |
Simon Atanasyan | 2903a8a | 2016-12-12 21:34:11 +0000 | [diff] [blame] | 1560 | if (!ElfSym<ELFT>::MipsGp->Value) { |
| 1561 | // Find GP-relative section with the lowest address |
| 1562 | // and use this address to calculate default _gp value. |
| 1563 | uintX_t Gp = -1; |
| 1564 | for (const OutputSectionBase * OS : OutputSections) |
| 1565 | if ((OS->Flags & SHF_MIPS_GPREL) && OS->Addr < Gp) |
| 1566 | Gp = OS->Addr; |
| 1567 | if (Gp != (uintX_t)-1) |
| 1568 | ElfSym<ELFT>::MipsGp->Value = Gp + 0x7ff0; |
| 1569 | } |
Simon Atanasyan | 8469b88 | 2016-11-23 22:22:16 +0000 | [diff] [blame] | 1570 | if (ElfSym<ELFT>::MipsGpDisp) |
Simon Atanasyan | 6a4eb75 | 2016-12-08 06:19:47 +0000 | [diff] [blame] | 1571 | ElfSym<ELFT>::MipsGpDisp->Value = ElfSym<ELFT>::MipsGp->Value; |
Simon Atanasyan | 8469b88 | 2016-11-23 22:22:16 +0000 | [diff] [blame] | 1572 | if (ElfSym<ELFT>::MipsLocalGp) |
Simon Atanasyan | 6a4eb75 | 2016-12-08 06:19:47 +0000 | [diff] [blame] | 1573 | ElfSym<ELFT>::MipsLocalGp->Value = ElfSym<ELFT>::MipsGp->Value; |
Simon Atanasyan | 8469b88 | 2016-11-23 22:22:16 +0000 | [diff] [blame] | 1574 | } |
Rui Ueyama | 1a311f1 | 2015-12-26 10:52:26 +0000 | [diff] [blame] | 1575 | } |
| 1576 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 1577 | template <class ELFT> void Writer<ELFT>::writeHeader() { |
| 1578 | uint8_t *Buf = Buffer->getBufferStart(); |
Rui Ueyama | e08cd67 | 2015-10-23 22:44:39 +0000 | [diff] [blame] | 1579 | memcpy(Buf, "\177ELF", 4); |
| 1580 | |
Rui Ueyama | 6621d8e | 2015-10-24 17:57:40 +0000 | [diff] [blame] | 1581 | // Write the ELF header. |
Rafael Espindola | 18608a0 | 2015-09-08 21:57:31 +0000 | [diff] [blame] | 1582 | auto *EHdr = reinterpret_cast<Elf_Ehdr *>(Buf); |
Rafael Espindola | 4b7c2fc | 2015-08-05 15:08:40 +0000 | [diff] [blame] | 1583 | EHdr->e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32; |
Rui Ueyama | 4cea4e8 | 2016-02-25 19:28:37 +0000 | [diff] [blame] | 1584 | EHdr->e_ident[EI_DATA] = getELFEncoding<ELFT>(); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 1585 | EHdr->e_ident[EI_VERSION] = EV_CURRENT; |
Rafael Espindola | 7cc713a | 2016-10-27 14:00:51 +0000 | [diff] [blame] | 1586 | EHdr->e_ident[EI_OSABI] = Config->OSABI; |
Rui Ueyama | 4cea4e8 | 2016-02-25 19:28:37 +0000 | [diff] [blame] | 1587 | EHdr->e_type = getELFType(); |
Rafael Espindola | 7cc713a | 2016-10-27 14:00:51 +0000 | [diff] [blame] | 1588 | EHdr->e_machine = Config->EMachine; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 1589 | EHdr->e_version = EV_CURRENT; |
Rui Ueyama | 2eda6d1 | 2016-11-23 22:41:00 +0000 | [diff] [blame] | 1590 | EHdr->e_entry = getEntryAddr(); |
Michael J. Spencer | 8039dae2 | 2015-07-29 00:30:10 +0000 | [diff] [blame] | 1591 | EHdr->e_shoff = SectionHeaderOff; |
Rafael Espindola | 18608a0 | 2015-09-08 21:57:31 +0000 | [diff] [blame] | 1592 | EHdr->e_ehsize = sizeof(Elf_Ehdr); |
Rui Ueyama | 2f1b79f | 2015-10-10 22:34:30 +0000 | [diff] [blame] | 1593 | EHdr->e_phnum = Phdrs.size(); |
Rafael Espindola | 18608a0 | 2015-09-08 21:57:31 +0000 | [diff] [blame] | 1594 | EHdr->e_shentsize = sizeof(Elf_Shdr); |
George Rimar | 7ca0627 | 2016-04-06 07:20:45 +0000 | [diff] [blame] | 1595 | EHdr->e_shnum = OutputSections.size() + 1; |
Eugene Leviant | 22eb026 | 2016-11-14 09:16:00 +0000 | [diff] [blame] | 1596 | EHdr->e_shstrndx = In<ELFT>::ShStrTab->OutSec->SectionIndex; |
Rui Ueyama | 6621d8e | 2015-10-24 17:57:40 +0000 | [diff] [blame] | 1597 | |
Rui Ueyama | 1e2e8ea | 2016-09-13 20:51:32 +0000 | [diff] [blame] | 1598 | if (Config->EMachine == EM_ARM) |
| 1599 | // We don't currently use any features incompatible with EF_ARM_EABI_VER5, |
| 1600 | // but we don't have any firm guarantees of conformance. Linux AArch64 |
| 1601 | // kernels (as of 2016) require an EABI version to be set. |
| 1602 | EHdr->e_flags = EF_ARM_EABI_VER5; |
| 1603 | else if (Config->EMachine == EM_MIPS) |
Simon Atanasyan | 4f90c2f | 2016-07-20 20:30:41 +0000 | [diff] [blame] | 1604 | EHdr->e_flags = getMipsEFlags<ELFT>(); |
Rui Ueyama | 22b5d1f | 2016-03-13 19:29:17 +0000 | [diff] [blame] | 1605 | |
George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 1606 | if (!Config->Relocatable) { |
| 1607 | EHdr->e_phoff = sizeof(Elf_Ehdr); |
| 1608 | EHdr->e_phentsize = sizeof(Elf_Phdr); |
| 1609 | } |
| 1610 | |
Rui Ueyama | 6621d8e | 2015-10-24 17:57:40 +0000 | [diff] [blame] | 1611 | // Write the program header table. |
Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 1612 | auto *HBuf = reinterpret_cast<Elf_Phdr *>(Buf + EHdr->e_phoff); |
Rafael Espindola | 17cb7c0 | 2016-12-19 17:01:01 +0000 | [diff] [blame] | 1613 | for (PhdrEntry &P : Phdrs) { |
| 1614 | HBuf->p_type = P.p_type; |
| 1615 | HBuf->p_flags = P.p_flags; |
| 1616 | HBuf->p_offset = P.p_offset; |
| 1617 | HBuf->p_vaddr = P.p_vaddr; |
| 1618 | HBuf->p_paddr = P.p_paddr; |
| 1619 | HBuf->p_filesz = P.p_filesz; |
| 1620 | HBuf->p_memsz = P.p_memsz; |
| 1621 | HBuf->p_align = P.p_align; |
| 1622 | ++HBuf; |
| 1623 | } |
Rafael Espindola | e438e07 | 2015-09-08 22:55:28 +0000 | [diff] [blame] | 1624 | |
Rui Ueyama | 6621d8e | 2015-10-24 17:57:40 +0000 | [diff] [blame] | 1625 | // Write the section header table. Note that the first table entry is null. |
Rui Ueyama | ad59b65 | 2016-02-25 23:58:21 +0000 | [diff] [blame] | 1626 | auto *SHdrs = reinterpret_cast<Elf_Shdr *>(Buf + EHdr->e_shoff); |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 1627 | for (OutputSectionBase *Sec : OutputSections) |
| 1628 | Sec->writeHeaderTo<ELFT>(++SHdrs); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 1629 | } |
| 1630 | |
Rui Ueyama | 6d12eae | 2016-12-05 17:40:37 +0000 | [diff] [blame] | 1631 | // Removes a given file asynchronously. This is a performance hack, |
| 1632 | // so remove this when operating systems are improved. |
| 1633 | // |
| 1634 | // On Linux (and probably on other Unix-like systems), unlink(2) is a |
| 1635 | // noticeably slow system call. As of 2016, unlink takes 250 |
| 1636 | // milliseconds to remove a 1 GB file on ext4 filesystem on my machine. |
| 1637 | // |
| 1638 | // To create a new result file, we first remove existing file. So, if |
| 1639 | // you repeatedly link a 1 GB program in a regular compile-link-debug |
| 1640 | // cycle, every cycle wastes 250 milliseconds only to remove a file. |
| 1641 | // Since LLD can link a 1 GB binary in about 5 seconds, that waste |
| 1642 | // actually counts. |
| 1643 | // |
| 1644 | // This function spawns a background thread to call unlink. |
| 1645 | // The calling thread returns almost immediately. |
| 1646 | static void unlinkAsync(StringRef Path) { |
| 1647 | if (!Config->Threads || !sys::fs::exists(Config->OutputFile)) |
| 1648 | return; |
| 1649 | |
George Rimar | 31a46b4 | 2016-12-25 06:49:17 +0000 | [diff] [blame] | 1650 | // First, rename Path to avoid race condition. We cannot remove |
Rui Ueyama | 6d12eae | 2016-12-05 17:40:37 +0000 | [diff] [blame] | 1651 | // Path from a different thread because we are now going to create |
| 1652 | // Path as a new file. If we do that in a different thread, the new |
| 1653 | // thread can remove the new file. |
| 1654 | SmallString<128> TempPath; |
Rui Ueyama | 817c9d6 | 2017-01-09 23:07:05 +0000 | [diff] [blame] | 1655 | if (sys::fs::createUniqueFile(Path + "tmp%%%%%%%%", TempPath)) |
| 1656 | return; |
| 1657 | if (sys::fs::rename(Path, TempPath)) { |
| 1658 | sys::fs::remove(TempPath); |
| 1659 | return; |
| 1660 | } |
Rui Ueyama | 6d12eae | 2016-12-05 17:40:37 +0000 | [diff] [blame] | 1661 | |
| 1662 | // Remove TempPath in background. |
| 1663 | std::thread([=] { ::remove(TempPath.str().str().c_str()); }).detach(); |
| 1664 | } |
| 1665 | |
| 1666 | // Open a result file. |
Rui Ueyama | f7f52ef | 2016-04-01 17:24:19 +0000 | [diff] [blame] | 1667 | template <class ELFT> void Writer<ELFT>::openFile() { |
Rui Ueyama | 6d12eae | 2016-12-05 17:40:37 +0000 | [diff] [blame] | 1668 | unlinkAsync(Config->OutputFile); |
Rafael Espindola | bdc8f2f | 2015-08-13 00:31:46 +0000 | [diff] [blame] | 1669 | ErrorOr<std::unique_ptr<FileOutputBuffer>> BufferOrErr = |
Rui Ueyama | cbe3926 | 2016-02-02 22:48:04 +0000 | [diff] [blame] | 1670 | FileOutputBuffer::create(Config->OutputFile, FileSize, |
| 1671 | FileOutputBuffer::F_executable); |
Rui Ueyama | 6d12eae | 2016-12-05 17:40:37 +0000 | [diff] [blame] | 1672 | |
Rui Ueyama | aa2db88 | 2016-07-15 01:38:54 +0000 | [diff] [blame] | 1673 | if (auto EC = BufferOrErr.getError()) |
| 1674 | error(EC, "failed to open " + Config->OutputFile); |
Rui Ueyama | f7f52ef | 2016-04-01 17:24:19 +0000 | [diff] [blame] | 1675 | else |
Rui Ueyama | aa2db88 | 2016-07-15 01:38:54 +0000 | [diff] [blame] | 1676 | Buffer = std::move(*BufferOrErr); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 1677 | } |
| 1678 | |
George Rimar | 86ce267 | 2016-08-25 09:05:47 +0000 | [diff] [blame] | 1679 | template <class ELFT> void Writer<ELFT>::writeSectionsBinary() { |
| 1680 | uint8_t *Buf = Buffer->getBufferStart(); |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 1681 | for (OutputSectionBase *Sec : OutputSections) |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1682 | if (Sec->Flags & SHF_ALLOC) |
| 1683 | Sec->writeTo(Buf + Sec->Offset); |
George Rimar | 86ce267 | 2016-08-25 09:05:47 +0000 | [diff] [blame] | 1684 | } |
| 1685 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 1686 | // Write section contents to a mmap'ed file. |
| 1687 | template <class ELFT> void Writer<ELFT>::writeSections() { |
| 1688 | uint8_t *Buf = Buffer->getBufferStart(); |
Hal Finkel | daedc12 | 2015-10-12 23:16:53 +0000 | [diff] [blame] | 1689 | |
Rui Ueyama | 7511825 | 2016-08-09 01:35:37 +0000 | [diff] [blame] | 1690 | // PPC64 needs to process relocations in the .opd section |
| 1691 | // before processing relocations in code-containing sections. |
Rui Ueyama | 30634d9 | 2016-08-09 01:35:38 +0000 | [diff] [blame] | 1692 | Out<ELFT>::Opd = findSection(".opd"); |
Rui Ueyama | 7511825 | 2016-08-09 01:35:37 +0000 | [diff] [blame] | 1693 | if (Out<ELFT>::Opd) { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1694 | Out<ELFT>::OpdBuf = Buf + Out<ELFT>::Opd->Offset; |
| 1695 | Out<ELFT>::Opd->writeTo(Buf + Out<ELFT>::Opd->Offset); |
Rafael Espindola | 7a51305 | 2015-10-13 14:45:51 +0000 | [diff] [blame] | 1696 | } |
Hal Finkel | daedc12 | 2015-10-12 23:16:53 +0000 | [diff] [blame] | 1697 | |
Eugene Leviant | 952eb4d | 2016-11-21 15:52:10 +0000 | [diff] [blame] | 1698 | OutputSectionBase *EhFrameHdr = |
| 1699 | In<ELFT>::EhFrameHdr ? In<ELFT>::EhFrameHdr->OutSec : nullptr; |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 1700 | for (OutputSectionBase *Sec : OutputSections) |
Eugene Leviant | 952eb4d | 2016-11-21 15:52:10 +0000 | [diff] [blame] | 1701 | if (Sec != Out<ELFT>::Opd && Sec != EhFrameHdr) |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1702 | Sec->writeTo(Buf + Sec->Offset); |
Eugene Leviant | e4f590f | 2016-08-31 07:43:50 +0000 | [diff] [blame] | 1703 | |
| 1704 | // The .eh_frame_hdr depends on .eh_frame section contents, therefore |
| 1705 | // it should be written after .eh_frame is written. |
Eugene Leviant | 952eb4d | 2016-11-21 15:52:10 +0000 | [diff] [blame] | 1706 | if (!Out<ELFT>::EhFrame->empty() && EhFrameHdr) |
| 1707 | EhFrameHdr->writeTo(Buf + EhFrameHdr->Offset); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 1708 | } |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 1709 | |
Rui Ueyama | 634ddf0 | 2016-03-11 20:51:53 +0000 | [diff] [blame] | 1710 | template <class ELFT> void Writer<ELFT>::writeBuildId() { |
Eugene Leviant | 282251a | 2016-11-01 09:49:24 +0000 | [diff] [blame] | 1711 | if (!In<ELFT>::BuildId || !In<ELFT>::BuildId->OutSec) |
Rui Ueyama | 634ddf0 | 2016-03-11 20:51:53 +0000 | [diff] [blame] | 1712 | return; |
| 1713 | |
Petr Hosek | fdfcb79 | 2016-09-01 22:43:03 +0000 | [diff] [blame] | 1714 | // Compute a hash of all sections of the output file. |
Rui Ueyama | 634ddf0 | 2016-03-11 20:51:53 +0000 | [diff] [blame] | 1715 | uint8_t *Start = Buffer->getBufferStart(); |
Petr Hosek | fdfcb79 | 2016-09-01 22:43:03 +0000 | [diff] [blame] | 1716 | uint8_t *End = Start + FileSize; |
Eugene Leviant | 282251a | 2016-11-01 09:49:24 +0000 | [diff] [blame] | 1717 | In<ELFT>::BuildId->writeBuildId({Start, End}); |
Rui Ueyama | 634ddf0 | 2016-03-11 20:51:53 +0000 | [diff] [blame] | 1718 | } |
| 1719 | |
Rui Ueyama | 84907c5 | 2016-08-09 03:38:23 +0000 | [diff] [blame] | 1720 | template void elf::writeResult<ELF32LE>(); |
| 1721 | template void elf::writeResult<ELF32BE>(); |
| 1722 | template void elf::writeResult<ELF64LE>(); |
| 1723 | template void elf::writeResult<ELF64BE>(); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 1724 | |
Rafael Espindola | 5967c97 | 2016-12-19 21:21:07 +0000 | [diff] [blame] | 1725 | template void elf::allocateHeaders<ELF32LE>(MutableArrayRef<PhdrEntry>, |
| 1726 | ArrayRef<OutputSectionBase *>); |
| 1727 | template void elf::allocateHeaders<ELF32BE>(MutableArrayRef<PhdrEntry>, |
| 1728 | ArrayRef<OutputSectionBase *>); |
| 1729 | template void elf::allocateHeaders<ELF64LE>(MutableArrayRef<PhdrEntry>, |
| 1730 | ArrayRef<OutputSectionBase *>); |
| 1731 | template void elf::allocateHeaders<ELF64BE>(MutableArrayRef<PhdrEntry>, |
| 1732 | ArrayRef<OutputSectionBase *>); |
| 1733 | |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 1734 | template bool elf::isRelroSection<ELF32LE>(const OutputSectionBase *); |
| 1735 | template bool elf::isRelroSection<ELF32BE>(const OutputSectionBase *); |
| 1736 | template bool elf::isRelroSection<ELF64LE>(const OutputSectionBase *); |
| 1737 | template bool elf::isRelroSection<ELF64BE>(const OutputSectionBase *); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 1738 | |
Rui Ueyama | 96bdd5b | 2016-07-25 22:26:28 +0000 | [diff] [blame] | 1739 | template void elf::reportDiscarded<ELF32LE>(InputSectionBase<ELF32LE> *); |
| 1740 | template void elf::reportDiscarded<ELF32BE>(InputSectionBase<ELF32BE> *); |
| 1741 | template void elf::reportDiscarded<ELF64LE>(InputSectionBase<ELF64LE> *); |
| 1742 | template void elf::reportDiscarded<ELF64BE>(InputSectionBase<ELF64BE> *); |