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" |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 11 | #include "Chunks.h" |
Rui Ueyama | cb8474ed | 2015-08-05 23:51:50 +0000 | [diff] [blame] | 12 | #include "Config.h" |
Rafael Espindola | 192e1fa | 2015-08-06 15:08:23 +0000 | [diff] [blame] | 13 | #include "Error.h" |
Michael J. Spencer | 67bc8d6 | 2015-08-27 23:15:56 +0000 | [diff] [blame] | 14 | #include "Symbols.h" |
Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 15 | #include "SymbolTable.h" |
Rafael Espindola | 6b83b90 | 2015-08-12 00:00:24 +0000 | [diff] [blame] | 16 | |
Rui Ueyama | e44524d | 2015-07-28 00:17:25 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/DenseMap.h" |
Rafael Espindola | 871765c | 2015-08-28 02:46:41 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/STLExtras.h" |
Rafael Espindola | ee1364f | 2015-09-01 21:47:21 +0000 | [diff] [blame] | 19 | #include "llvm/MC/StringTableBuilder.h" |
Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 20 | #include "llvm/Support/FileOutputBuffer.h" |
Michael J. Spencer | 67bc8d6 | 2015-08-27 23:15:56 +0000 | [diff] [blame] | 21 | #include "llvm/Support/raw_ostream.h" |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace llvm; |
| 24 | using namespace llvm::ELF; |
| 25 | using namespace llvm::object; |
| 26 | |
| 27 | using namespace lld; |
| 28 | using namespace lld::elf2; |
| 29 | |
| 30 | static const int PageSize = 4096; |
| 31 | |
Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 32 | namespace { |
Rafael Espindola | 52a0f1e | 2015-08-11 23:22:24 +0000 | [diff] [blame] | 33 | // OutputSection represents a section in an output file. It's a |
| 34 | // container of chunks. OutputSection and Chunk are 1:N relationship. |
| 35 | // Chunks cannot belong to more than one OutputSections. The writer |
| 36 | // creates multiple OutputSections and assign them unique, |
| 37 | // non-overlapping file offsets and VAs. |
Rafael Espindola | ebd2108 | 2015-08-13 22:14:37 +0000 | [diff] [blame] | 38 | template <bool Is64Bits> class OutputSectionBase { |
Rafael Espindola | 52a0f1e | 2015-08-11 23:22:24 +0000 | [diff] [blame] | 39 | public: |
Rafael Espindola | a175eb6 | 2015-08-13 18:37:23 +0000 | [diff] [blame] | 40 | typedef typename std::conditional<Is64Bits, uint64_t, uint32_t>::type uintX_t; |
| 41 | typedef |
| 42 | typename std::conditional<Is64Bits, Elf64_Shdr, Elf32_Shdr>::type HeaderT; |
Rafael Espindola | 0218340 | 2015-08-11 23:34:29 +0000 | [diff] [blame] | 43 | |
Rafael Espindola | ebd2108 | 2015-08-13 22:14:37 +0000 | [diff] [blame] | 44 | OutputSectionBase(StringRef Name, uint32_t sh_type, uintX_t sh_flags) |
Rafael Espindola | 375a508 | 2015-08-13 17:32:30 +0000 | [diff] [blame] | 45 | : Name(Name) { |
Rafael Espindola | a175eb6 | 2015-08-13 18:37:23 +0000 | [diff] [blame] | 46 | memset(&Header, 0, sizeof(HeaderT)); |
Rafael Espindola | 375a508 | 2015-08-13 17:32:30 +0000 | [diff] [blame] | 47 | Header.sh_type = sh_type; |
| 48 | Header.sh_flags = sh_flags; |
Rafael Espindola | 372889a | 2015-08-12 23:25:42 +0000 | [diff] [blame] | 49 | } |
Rafael Espindola | 184d94e | 2015-08-13 18:25:47 +0000 | [diff] [blame] | 50 | void setVA(uintX_t VA) { Header.sh_addr = VA; } |
Rafael Espindola | f383707 | 2015-08-25 15:53:17 +0000 | [diff] [blame] | 51 | uintX_t getVA() { return Header.sh_addr; } |
Rafael Espindola | 184d94e | 2015-08-13 18:25:47 +0000 | [diff] [blame] | 52 | void setFileOffset(uintX_t Off) { Header.sh_offset = Off; } |
Rafael Espindola | a175eb6 | 2015-08-13 18:37:23 +0000 | [diff] [blame] | 53 | template <endianness E> |
| 54 | void writeHeaderTo(typename ELFFile<ELFType<E, Is64Bits>>::Elf_Shdr *SHdr); |
Rafael Espindola | 6b83b90 | 2015-08-12 00:00:24 +0000 | [diff] [blame] | 55 | StringRef getName() { return Name; } |
| 56 | void setNameOffset(uintX_t Offset) { Header.sh_name = Offset; } |
Rafael Espindola | 52a0f1e | 2015-08-11 23:22:24 +0000 | [diff] [blame] | 57 | |
Rafael Espindola | 832b93f | 2015-08-24 20:06:32 +0000 | [diff] [blame] | 58 | unsigned getSectionIndex() const { return SectionIndex; } |
| 59 | void setSectionIndex(unsigned I) { SectionIndex = I; } |
| 60 | |
Rafael Espindola | 52a0f1e | 2015-08-11 23:22:24 +0000 | [diff] [blame] | 61 | // Returns the size of the section in the output file. |
Rafael Espindola | 0218340 | 2015-08-11 23:34:29 +0000 | [diff] [blame] | 62 | uintX_t getSize() { return Header.sh_size; } |
Rafael Espindola | 0518574 | 2015-08-31 22:07:18 +0000 | [diff] [blame] | 63 | void setSize(uintX_t Val) { Header.sh_size = Val; } |
Rafael Espindola | abad618 | 2015-08-13 15:23:46 +0000 | [diff] [blame] | 64 | uintX_t getFlags() { return Header.sh_flags; } |
Rui Ueyama | 8050d32 | 2015-08-14 05:17:30 +0000 | [diff] [blame] | 65 | uintX_t getFileOff() { return Header.sh_offset; } |
Michael J. Spencer | baae538 | 2015-09-05 00:25:33 +0000 | [diff] [blame] | 66 | uintX_t getAlign() { |
| 67 | // The ELF spec states that a value of 0 means the section has no alignment |
| 68 | // constraits. |
| 69 | return std::max<uintX_t>(Header.sh_addralign, 1); |
| 70 | } |
Rafael Espindola | 058f343 | 2015-08-31 20:23:57 +0000 | [diff] [blame] | 71 | uint32_t getType() { return Header.sh_type; } |
Rafael Espindola | abad618 | 2015-08-13 15:23:46 +0000 | [diff] [blame] | 72 | |
Rafael Espindola | ebd2108 | 2015-08-13 22:14:37 +0000 | [diff] [blame] | 73 | virtual void finalize() {} |
| 74 | virtual void writeTo(uint8_t *Buf) = 0; |
| 75 | |
| 76 | protected: |
Rafael Espindola | 52a0f1e | 2015-08-11 23:22:24 +0000 | [diff] [blame] | 77 | StringRef Name; |
Rafael Espindola | a175eb6 | 2015-08-13 18:37:23 +0000 | [diff] [blame] | 78 | HeaderT Header; |
Rafael Espindola | 832b93f | 2015-08-24 20:06:32 +0000 | [diff] [blame] | 79 | unsigned SectionIndex; |
Rafael Espindola | ebd2108 | 2015-08-13 22:14:37 +0000 | [diff] [blame] | 80 | ~OutputSectionBase() = default; |
| 81 | }; |
Rafael Espindola | 832b93f | 2015-08-24 20:06:32 +0000 | [diff] [blame] | 82 | } |
Rafael Espindola | ebd2108 | 2015-08-13 22:14:37 +0000 | [diff] [blame] | 83 | |
Rafael Espindola | 83b0dc6 | 2015-08-13 22:21:37 +0000 | [diff] [blame] | 84 | template <class ELFT> |
Rafael Espindola | 832b93f | 2015-08-24 20:06:32 +0000 | [diff] [blame] | 85 | class lld::elf2::OutputSection final |
| 86 | : public OutputSectionBase<ELFT::Is64Bits> { |
Rafael Espindola | ebd2108 | 2015-08-13 22:14:37 +0000 | [diff] [blame] | 87 | public: |
Rafael Espindola | 83b0dc6 | 2015-08-13 22:21:37 +0000 | [diff] [blame] | 88 | typedef typename OutputSectionBase<ELFT::Is64Bits>::uintX_t uintX_t; |
Michael J. Spencer | 67bc8d6 | 2015-08-27 23:15:56 +0000 | [diff] [blame] | 89 | typedef typename ELFFile<ELFT>::Elf_Shdr Elf_Shdr; |
| 90 | typedef typename ELFFile<ELFT>::Elf_Rela Elf_Rela; |
Rafael Espindola | ebd2108 | 2015-08-13 22:14:37 +0000 | [diff] [blame] | 91 | OutputSection(StringRef Name, uint32_t sh_type, uintX_t sh_flags) |
Rafael Espindola | 83b0dc6 | 2015-08-13 22:21:37 +0000 | [diff] [blame] | 92 | : OutputSectionBase<ELFT::Is64Bits>(Name, sh_type, sh_flags) {} |
Rafael Espindola | ebd2108 | 2015-08-13 22:14:37 +0000 | [diff] [blame] | 93 | |
Rafael Espindola | 83b0dc6 | 2015-08-13 22:21:37 +0000 | [diff] [blame] | 94 | void addChunk(SectionChunk<ELFT> *C); |
Rafael Espindola | ebd2108 | 2015-08-13 22:14:37 +0000 | [diff] [blame] | 95 | void writeTo(uint8_t *Buf) override; |
| 96 | |
| 97 | private: |
Rafael Espindola | 83b0dc6 | 2015-08-13 22:21:37 +0000 | [diff] [blame] | 98 | std::vector<SectionChunk<ELFT> *> Chunks; |
Rafael Espindola | 52a0f1e | 2015-08-11 23:22:24 +0000 | [diff] [blame] | 99 | }; |
| 100 | |
Rafael Espindola | 832b93f | 2015-08-24 20:06:32 +0000 | [diff] [blame] | 101 | namespace { |
Rafael Espindola | ebd2108 | 2015-08-13 22:14:37 +0000 | [diff] [blame] | 102 | template <bool Is64Bits> |
| 103 | class StringTableSection final : public OutputSectionBase<Is64Bits> { |
Rafael Espindola | ebd2108 | 2015-08-13 22:14:37 +0000 | [diff] [blame] | 104 | public: |
Rafael Espindola | ee1364f | 2015-09-01 21:47:21 +0000 | [diff] [blame] | 105 | llvm::StringTableBuilder StrTabBuilder; |
| 106 | |
Rafael Espindola | ebd2108 | 2015-08-13 22:14:37 +0000 | [diff] [blame] | 107 | typedef typename OutputSectionBase<Is64Bits>::uintX_t uintX_t; |
Rafael Espindola | ee1364f | 2015-09-01 21:47:21 +0000 | [diff] [blame] | 108 | StringTableSection() : OutputSectionBase<Is64Bits>(".strtab", SHT_STRTAB, 0) { |
Rafael Espindola | ebd2108 | 2015-08-13 22:14:37 +0000 | [diff] [blame] | 109 | this->Header.sh_addralign = 1; |
| 110 | } |
| 111 | |
| 112 | void add(StringRef S) { StrTabBuilder.add(S); } |
Rui Ueyama | 8050d32 | 2015-08-14 05:17:30 +0000 | [diff] [blame] | 113 | size_t getFileOff(StringRef S) { return StrTabBuilder.getOffset(S); } |
Rafael Espindola | ebd2108 | 2015-08-13 22:14:37 +0000 | [diff] [blame] | 114 | void writeTo(uint8_t *Buf) override; |
| 115 | |
| 116 | void finalize() override { |
| 117 | StrTabBuilder.finalize(StringTableBuilder::ELF); |
| 118 | this->Header.sh_size = StrTabBuilder.data().size(); |
| 119 | } |
| 120 | }; |
| 121 | |
Rafael Espindola | f763ca3 | 2015-08-14 02:42:20 +0000 | [diff] [blame] | 122 | template <class ELFT> |
| 123 | class SymbolTableSection final : public OutputSectionBase<ELFT::Is64Bits> { |
| 124 | public: |
Rafael Espindola | 62b81b8 | 2015-08-14 13:07:05 +0000 | [diff] [blame] | 125 | typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym; |
Rafael Espindola | f383707 | 2015-08-25 15:53:17 +0000 | [diff] [blame] | 126 | typedef typename OutputSectionBase<ELFT::Is64Bits>::uintX_t uintX_t; |
Rafael Espindola | 57b2592 | 2015-09-08 19:23:30 +0000 | [diff] [blame] | 127 | SymbolTableSection(SymbolTable &Table, |
| 128 | StringTableSection<ELFT::Is64Bits> &StrTabSec) |
Rafael Espindola | 62b81b8 | 2015-08-14 13:07:05 +0000 | [diff] [blame] | 129 | : OutputSectionBase<ELFT::Is64Bits>(".symtab", SHT_SYMTAB, 0), |
Rafael Espindola | 57b2592 | 2015-09-08 19:23:30 +0000 | [diff] [blame] | 130 | Table(Table), Builder(StrTabSec.StrTabBuilder), StrTabSec(StrTabSec) { |
Rafael Espindola | f763ca3 | 2015-08-14 02:42:20 +0000 | [diff] [blame] | 131 | typedef OutputSectionBase<ELFT::Is64Bits> Base; |
| 132 | typename Base::HeaderT &Header = this->Header; |
| 133 | |
| 134 | // For now the only local symbol is going to be the one at index 0 |
| 135 | Header.sh_info = 1; |
| 136 | |
Rafael Espindola | 62b81b8 | 2015-08-14 13:07:05 +0000 | [diff] [blame] | 137 | Header.sh_entsize = sizeof(Elf_Sym); |
Rafael Espindola | f763ca3 | 2015-08-14 02:42:20 +0000 | [diff] [blame] | 138 | Header.sh_addralign = ELFT::Is64Bits ? 8 : 4; |
| 139 | } |
Rafael Espindola | 5b3942f | 2015-09-01 20:36:51 +0000 | [diff] [blame] | 140 | |
| 141 | void finalize() override { |
| 142 | this->Header.sh_size = (NumVisible + 1) * sizeof(Elf_Sym); |
Rafael Espindola | 57b2592 | 2015-09-08 19:23:30 +0000 | [diff] [blame] | 143 | this->Header.sh_link = StrTabSec.getSectionIndex(); |
Rafael Espindola | 5b3942f | 2015-09-01 20:36:51 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Rafael Espindola | f763ca3 | 2015-08-14 02:42:20 +0000 | [diff] [blame] | 146 | void writeTo(uint8_t *Buf) override; |
Rafael Espindola | 62b81b8 | 2015-08-14 13:07:05 +0000 | [diff] [blame] | 147 | |
Rafael Espindola | e6f5210 | 2015-08-24 14:48:18 +0000 | [diff] [blame] | 148 | const SymbolTable &getSymTable() { return Table; } |
| 149 | |
Rafael Espindola | 8b09d68 | 2015-08-31 22:33:21 +0000 | [diff] [blame] | 150 | OutputSection<ELFT> *BSSSec = nullptr; |
Rafael Espindola | 5b3942f | 2015-09-01 20:36:51 +0000 | [diff] [blame] | 151 | unsigned NumVisible = 0; |
Rafael Espindola | 8b09d68 | 2015-08-31 22:33:21 +0000 | [diff] [blame] | 152 | |
Rafael Espindola | 62b81b8 | 2015-08-14 13:07:05 +0000 | [diff] [blame] | 153 | private: |
| 154 | SymbolTable &Table; |
Rafael Espindola | ee1364f | 2015-09-01 21:47:21 +0000 | [diff] [blame] | 155 | llvm::StringTableBuilder &Builder; |
Rafael Espindola | 57b2592 | 2015-09-08 19:23:30 +0000 | [diff] [blame] | 156 | const StringTableSection<ELFT::Is64Bits> &StrTabSec; |
Rafael Espindola | f763ca3 | 2015-08-14 02:42:20 +0000 | [diff] [blame] | 157 | }; |
| 158 | |
Rafael Espindola | 740fafe | 2015-09-08 19:43:27 +0000 | [diff] [blame] | 159 | template <bool Is64Bits> |
| 160 | class DynamicSection final : public OutputSectionBase<Is64Bits> { |
| 161 | public: |
| 162 | DynamicSection(const StringTableSection<Is64Bits> &StrTabSec) |
| 163 | : OutputSectionBase<Is64Bits>(".dynamic", SHT_DYNAMIC, |
| 164 | SHF_ALLOC | SHF_WRITE), |
| 165 | StrTabSec(StrTabSec) { |
| 166 | typedef OutputSectionBase<Is64Bits> Base; |
| 167 | typename Base::HeaderT &Header = this->Header; |
| 168 | Header.sh_addralign = Is64Bits ? 8 : 4; |
| 169 | Header.sh_entsize = Is64Bits ? 16 : 8; |
| 170 | } |
| 171 | |
| 172 | void finalize() override { |
| 173 | this->Header.sh_link = StrTabSec.getSectionIndex(); |
| 174 | } |
| 175 | |
| 176 | void writeTo(uint8_t *Buf) override {} |
| 177 | |
| 178 | private: |
| 179 | const StringTableSection<Is64Bits> &StrTabSec; |
| 180 | }; |
| 181 | |
Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 182 | // The writer writes a SymbolTable result to a file. |
| 183 | template <class ELFT> class Writer { |
| 184 | public: |
| 185 | typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t; |
Rafael Espindola | a747179 | 2015-08-13 17:04:50 +0000 | [diff] [blame] | 186 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr; |
Rafael Espindola | 0518574 | 2015-08-31 22:07:18 +0000 | [diff] [blame] | 187 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym; |
Rafael Espindola | 740fafe | 2015-09-08 19:43:27 +0000 | [diff] [blame] | 188 | Writer(SymbolTable *T) : SymTable(*T, StringTable), DynamicSec(StringTable) {} |
Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 189 | void run(); |
| 190 | |
| 191 | private: |
| 192 | void createSections(); |
| 193 | void assignAddresses(); |
| 194 | void openFile(StringRef OutputPath); |
| 195 | void writeHeader(); |
| 196 | void writeSections(); |
| 197 | |
Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 198 | std::unique_ptr<llvm::FileOutputBuffer> Buffer; |
Rafael Espindola | 83b0dc6 | 2015-08-13 22:21:37 +0000 | [diff] [blame] | 199 | llvm::SpecificBumpPtrAllocator<OutputSection<ELFT>> CAlloc; |
Rafael Espindola | ebd2108 | 2015-08-13 22:14:37 +0000 | [diff] [blame] | 200 | std::vector<OutputSectionBase<ELFT::Is64Bits> *> OutputSections; |
Rafael Espindola | 5f55387 | 2015-09-08 17:39:39 +0000 | [diff] [blame] | 201 | unsigned getNumSections() const { return OutputSections.size() + 1; } |
Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 202 | |
Rafael Espindola | 98f6bd0 | 2015-08-11 23:14:13 +0000 | [diff] [blame] | 203 | uintX_t FileSize; |
| 204 | uintX_t SizeOfHeaders; |
Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 205 | uintX_t SectionHeaderOff; |
Rafael Espindola | ebd2108 | 2015-08-13 22:14:37 +0000 | [diff] [blame] | 206 | |
Rafael Espindola | ebd2108 | 2015-08-13 22:14:37 +0000 | [diff] [blame] | 207 | StringTableSection<ELFT::Is64Bits> StringTable; |
| 208 | |
Rafael Espindola | ee1364f | 2015-09-01 21:47:21 +0000 | [diff] [blame] | 209 | SymbolTableSection<ELFT> SymTable; |
Rafael Espindola | 740fafe | 2015-09-08 19:43:27 +0000 | [diff] [blame] | 210 | |
| 211 | DynamicSection<ELFT::Is64Bits> DynamicSec; |
Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 212 | }; |
| 213 | } // anonymous namespace |
| 214 | |
| 215 | namespace lld { |
| 216 | namespace elf2 { |
| 217 | |
Rafael Espindola | 2ffdd4d | 2015-08-04 14:29:01 +0000 | [diff] [blame] | 218 | template <class ELFT> |
Rui Ueyama | cb8474ed | 2015-08-05 23:51:50 +0000 | [diff] [blame] | 219 | void writeResult(SymbolTable *Symtab) { Writer<ELFT>(Symtab).run(); } |
Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 220 | |
Rui Ueyama | cb8474ed | 2015-08-05 23:51:50 +0000 | [diff] [blame] | 221 | template void writeResult<ELF32LE>(SymbolTable *); |
| 222 | template void writeResult<ELF32BE>(SymbolTable *); |
| 223 | template void writeResult<ELF64LE>(SymbolTable *); |
| 224 | template void writeResult<ELF64BE>(SymbolTable *); |
Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 225 | |
| 226 | } // namespace elf2 |
| 227 | } // namespace lld |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 228 | |
| 229 | // The main function of the writer. |
Rui Ueyama | afff74e2 | 2015-08-05 23:24:46 +0000 | [diff] [blame] | 230 | template <class ELFT> void Writer<ELFT>::run() { |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 231 | createSections(); |
| 232 | assignAddresses(); |
Rui Ueyama | cb8474ed | 2015-08-05 23:51:50 +0000 | [diff] [blame] | 233 | openFile(Config->OutputFile); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 234 | writeHeader(); |
| 235 | writeSections(); |
| 236 | error(Buffer->commit()); |
| 237 | } |
| 238 | |
Rafael Espindola | 83b0dc6 | 2015-08-13 22:21:37 +0000 | [diff] [blame] | 239 | template <class ELFT> |
| 240 | void OutputSection<ELFT>::addChunk(SectionChunk<ELFT> *C) { |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 241 | Chunks.push_back(C); |
Rafael Espindola | 832b93f | 2015-08-24 20:06:32 +0000 | [diff] [blame] | 242 | C->setOutputSection(this); |
Rafael Espindola | 0160a28 | 2015-08-13 20:13:39 +0000 | [diff] [blame] | 243 | uint32_t Align = C->getAlign(); |
Rafael Espindola | ebd2108 | 2015-08-13 22:14:37 +0000 | [diff] [blame] | 244 | if (Align > this->Header.sh_addralign) |
| 245 | this->Header.sh_addralign = Align; |
Rafael Espindola | 0160a28 | 2015-08-13 20:13:39 +0000 | [diff] [blame] | 246 | |
Rafael Espindola | ebd2108 | 2015-08-13 22:14:37 +0000 | [diff] [blame] | 247 | uintX_t Off = this->Header.sh_size; |
Rafael Espindola | 0160a28 | 2015-08-13 20:13:39 +0000 | [diff] [blame] | 248 | Off = RoundUpToAlignment(Off, Align); |
Rafael Espindola | 674b5d5 | 2015-08-13 15:54:36 +0000 | [diff] [blame] | 249 | C->setOutputSectionOff(Off); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 250 | Off += C->getSize(); |
Rafael Espindola | ebd2108 | 2015-08-13 22:14:37 +0000 | [diff] [blame] | 251 | this->Header.sh_size = Off; |
| 252 | } |
| 253 | |
Michael J. Spencer | 67bc8d6 | 2015-08-27 23:15:56 +0000 | [diff] [blame] | 254 | template <class ELFT> |
| 255 | static typename llvm::object::ELFFile<ELFT>::uintX_t |
| 256 | getSymVA(DefinedRegular<ELFT> *DR) { |
| 257 | const SectionChunk<ELFT> *SC = &DR->Section; |
| 258 | OutputSection<ELFT> *OS = SC->getOutputSection(); |
| 259 | return OS->getVA() + SC->getOutputSectionOff() + DR->Sym.st_value; |
| 260 | } |
| 261 | |
Rafael Espindola | 83b0dc6 | 2015-08-13 22:21:37 +0000 | [diff] [blame] | 262 | template <class ELFT> void OutputSection<ELFT>::writeTo(uint8_t *Buf) { |
Michael J. Spencer | 67bc8d6 | 2015-08-27 23:15:56 +0000 | [diff] [blame] | 263 | for (SectionChunk<ELFT> *C : Chunks) { |
Rafael Espindola | ebd2108 | 2015-08-13 22:14:37 +0000 | [diff] [blame] | 264 | C->writeTo(Buf); |
Michael J. Spencer | 67bc8d6 | 2015-08-27 23:15:56 +0000 | [diff] [blame] | 265 | ObjectFile<ELFT> *File = C->getFile(); |
| 266 | ELFFile<ELFT> *EObj = File->getObj(); |
| 267 | uint8_t *Base = Buf + C->getOutputSectionOff(); |
| 268 | |
| 269 | // Iterate over all relocation sections that apply to this section. |
| 270 | for (const Elf_Shdr *RelSec : C->RelocSections) { |
| 271 | // Only support RELA for now. |
| 272 | if (RelSec->sh_type != SHT_RELA) |
| 273 | continue; |
| 274 | for (const Elf_Rela &RI : EObj->relas(RelSec)) { |
| 275 | uint32_t SymIndex = RI.getSymbol(EObj->isMips64EL()); |
| 276 | SymbolBody *Body = File->getSymbolBody(SymIndex); |
| 277 | if (!Body) |
| 278 | continue; |
Rafael Espindola | 30e1797 | 2015-08-30 23:17:30 +0000 | [diff] [blame] | 279 | // Skip unsupported for now. |
Michael J. Spencer | 67bc8d6 | 2015-08-27 23:15:56 +0000 | [diff] [blame] | 280 | if (!isa<DefinedRegular<ELFT>>(Body)) |
Rafael Espindola | 30e1797 | 2015-08-30 23:17:30 +0000 | [diff] [blame] | 281 | continue; |
Michael J. Spencer | 67bc8d6 | 2015-08-27 23:15:56 +0000 | [diff] [blame] | 282 | uintX_t Offset = RI.r_offset; |
| 283 | uint32_t Type = RI.getType(EObj->isMips64EL()); |
| 284 | uintX_t P = this->getVA() + C->getOutputSectionOff(); |
| 285 | uintX_t SymVA = getSymVA<ELFT>(cast<DefinedRegular<ELFT>>(Body)); |
Davide Italiano | 94f183a | 2015-08-29 13:15:42 +0000 | [diff] [blame] | 286 | uint8_t *Location = Base + Offset; |
Michael J. Spencer | 67bc8d6 | 2015-08-27 23:15:56 +0000 | [diff] [blame] | 287 | switch (Type) { |
| 288 | case llvm::ELF::R_X86_64_PC32: |
Davide Italiano | 94f183a | 2015-08-29 13:15:42 +0000 | [diff] [blame] | 289 | support::endian::write32le(Location, |
Michael J. Spencer | 67bc8d6 | 2015-08-27 23:15:56 +0000 | [diff] [blame] | 290 | SymVA + (RI.r_addend - (P + Offset))); |
| 291 | break; |
Davide Italiano | 94f183a | 2015-08-29 13:15:42 +0000 | [diff] [blame] | 292 | case llvm::ELF::R_X86_64_32: |
| 293 | support::endian::write32le(Location, SymVA + RI.r_addend); |
| 294 | break; |
Michael J. Spencer | 67bc8d6 | 2015-08-27 23:15:56 +0000 | [diff] [blame] | 295 | default: |
| 296 | llvm::errs() << Twine("unrecognized reloc ") + Twine(Type) << '\n'; |
| 297 | break; |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | } |
Rafael Espindola | ebd2108 | 2015-08-13 22:14:37 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | template <bool Is64Bits> |
| 305 | void StringTableSection<Is64Bits>::writeTo(uint8_t *Buf) { |
| 306 | StringRef Data = StrTabBuilder.data(); |
| 307 | memcpy(Buf, Data.data(), Data.size()); |
Michael J. Spencer | 8039dae2 | 2015-07-29 00:30:10 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Rafael Espindola | 871765c | 2015-08-28 02:46:41 +0000 | [diff] [blame] | 310 | template <class ELFT> |
| 311 | static int compareSym(const typename ELFFile<ELFT>::Elf_Sym *A, |
| 312 | const typename ELFFile<ELFT>::Elf_Sym *B) { |
| 313 | uint32_t AN = A->st_name; |
| 314 | uint32_t BN = B->st_name; |
| 315 | assert(AN != BN); |
| 316 | return AN - BN; |
| 317 | } |
| 318 | |
Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 319 | static bool includeInSymtab(const SymbolBody &B) { |
| 320 | if (B.isLazy()) |
| 321 | return false; |
| 322 | if (!B.isUsedInRegularObj()) |
| 323 | return false; |
| 324 | uint8_t V = B.getMostConstrainingVisibility(); |
| 325 | if (V != STV_DEFAULT && V != STV_PROTECTED) |
| 326 | return false; |
| 327 | return true; |
| 328 | } |
| 329 | |
Rafael Espindola | 62b81b8 | 2015-08-14 13:07:05 +0000 | [diff] [blame] | 330 | template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) { |
Rafael Espindola | 871765c | 2015-08-28 02:46:41 +0000 | [diff] [blame] | 331 | uint8_t *BufStart = Buf; |
| 332 | |
Rafael Espindola | 62b81b8 | 2015-08-14 13:07:05 +0000 | [diff] [blame] | 333 | Buf += sizeof(Elf_Sym); |
Rafael Espindola | 62b81b8 | 2015-08-14 13:07:05 +0000 | [diff] [blame] | 334 | for (auto &P : Table.getSymbols()) { |
| 335 | StringRef Name = P.first; |
Rafael Espindola | 383c323 | 2015-08-14 13:52:36 +0000 | [diff] [blame] | 336 | Symbol *Sym = P.second; |
Rafael Espindola | 7f37775 | 2015-09-01 20:30:52 +0000 | [diff] [blame] | 337 | SymbolBody *Body = Sym->Body; |
Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 338 | if (!includeInSymtab(*Body)) |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 339 | continue; |
Rafael Espindola | 7f37775 | 2015-09-01 20:30:52 +0000 | [diff] [blame] | 340 | const Elf_Sym &InputSym = cast<ELFSymbolBody<ELFT>>(Body)->Sym; |
Rafael Espindola | 383c323 | 2015-08-14 13:52:36 +0000 | [diff] [blame] | 341 | |
| 342 | auto *ESym = reinterpret_cast<Elf_Sym *>(Buf); |
| 343 | ESym->st_name = Builder.getOffset(Name); |
Rafael Espindola | 832b93f | 2015-08-24 20:06:32 +0000 | [diff] [blame] | 344 | |
| 345 | const SectionChunk<ELFT> *Section = nullptr; |
Rafael Espindola | 8b09d68 | 2015-08-31 22:33:21 +0000 | [diff] [blame] | 346 | OutputSection<ELFT> *Out = nullptr; |
Rafael Espindola | 832b93f | 2015-08-24 20:06:32 +0000 | [diff] [blame] | 347 | |
Rafael Espindola | c44d17a | 2015-08-14 15:10:49 +0000 | [diff] [blame] | 348 | switch (Body->kind()) { |
Rafael Espindola | 7f37775 | 2015-09-01 20:30:52 +0000 | [diff] [blame] | 349 | case SymbolBody::DefinedRegularKind: |
| 350 | Section = &cast<DefinedRegular<ELFT>>(Body)->Section; |
Rafael Espindola | 383c323 | 2015-08-14 13:52:36 +0000 | [diff] [blame] | 351 | break; |
Rafael Espindola | 8b09d68 | 2015-08-31 22:33:21 +0000 | [diff] [blame] | 352 | case SymbolBody::DefinedCommonKind: |
Rafael Espindola | 8b09d68 | 2015-08-31 22:33:21 +0000 | [diff] [blame] | 353 | Out = BSSSec; |
| 354 | break; |
Rafael Espindola | 3a63f3f | 2015-08-28 20:19:34 +0000 | [diff] [blame] | 355 | case SymbolBody::UndefinedKind: |
Rafael Espindola | 4f624b9 | 2015-09-08 14:32:29 +0000 | [diff] [blame] | 356 | if (!Body->isWeak()) |
| 357 | error(Twine("undefined symbol: ") + Name); |
Rafael Espindola | 0e0c190 | 2015-08-27 12:40:06 +0000 | [diff] [blame] | 358 | case SymbolBody::DefinedAbsoluteKind: |
Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 359 | case SymbolBody::SharedKind: |
Rafael Espindola | 383c323 | 2015-08-14 13:52:36 +0000 | [diff] [blame] | 360 | break; |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 361 | case SymbolBody::LazyKind: |
| 362 | llvm_unreachable("Lazy symbol got to output symbol table!"); |
Rafael Espindola | 383c323 | 2015-08-14 13:52:36 +0000 | [diff] [blame] | 363 | } |
Rafael Espindola | f383707 | 2015-08-25 15:53:17 +0000 | [diff] [blame] | 364 | |
Davide Italiano | ad6c81c | 2015-09-04 19:42:14 +0000 | [diff] [blame] | 365 | ESym->setBindingAndType(InputSym.getBinding(), InputSym.getType()); |
Rafael Espindola | 7f37775 | 2015-09-01 20:30:52 +0000 | [diff] [blame] | 366 | ESym->st_size = InputSym.st_size; |
Davide Italiano | ad6c81c | 2015-09-04 19:42:14 +0000 | [diff] [blame] | 367 | ESym->setVisibility(Body->getMostConstrainingVisibility()); |
Rafael Espindola | 7f37775 | 2015-09-01 20:30:52 +0000 | [diff] [blame] | 368 | if (InputSym.isAbsolute()) { |
| 369 | ESym->st_shndx = SHN_ABS; |
| 370 | ESym->st_value = InputSym.st_value; |
Rafael Espindola | f383707 | 2015-08-25 15:53:17 +0000 | [diff] [blame] | 371 | } |
Rafael Espindola | 383c323 | 2015-08-14 13:52:36 +0000 | [diff] [blame] | 372 | |
Rafael Espindola | 8b09d68 | 2015-08-31 22:33:21 +0000 | [diff] [blame] | 373 | if (Section) |
| 374 | Out = Section->getOutputSection(); |
| 375 | |
| 376 | if (Out) { |
Rafael Espindola | 832b93f | 2015-08-24 20:06:32 +0000 | [diff] [blame] | 377 | ESym->st_shndx = Out->getSectionIndex(); |
Rafael Espindola | f383707 | 2015-08-25 15:53:17 +0000 | [diff] [blame] | 378 | uintX_t VA = Out->getVA(); |
Rafael Espindola | 8b09d68 | 2015-08-31 22:33:21 +0000 | [diff] [blame] | 379 | if (Section) |
| 380 | VA += Section->getOutputSectionOff(); |
Rafael Espindola | ce8c9c0 | 2015-08-31 22:55:21 +0000 | [diff] [blame] | 381 | if (auto *C = dyn_cast<DefinedCommon<ELFT>>(Body)) |
| 382 | VA += C->OffsetInBSS; |
| 383 | else |
Rafael Espindola | 7f37775 | 2015-09-01 20:30:52 +0000 | [diff] [blame] | 384 | VA += InputSym.st_value; |
Rafael Espindola | f383707 | 2015-08-25 15:53:17 +0000 | [diff] [blame] | 385 | ESym->st_value = VA; |
Rafael Espindola | 832b93f | 2015-08-24 20:06:32 +0000 | [diff] [blame] | 386 | } |
| 387 | |
Rafael Espindola | 62b81b8 | 2015-08-14 13:07:05 +0000 | [diff] [blame] | 388 | Buf += sizeof(Elf_Sym); |
| 389 | } |
Rafael Espindola | 871765c | 2015-08-28 02:46:41 +0000 | [diff] [blame] | 390 | |
| 391 | // The order the global symbols are in is not defined. We can use an arbitrary |
| 392 | // order, but it has to be reproducible. That is true even when cross linking. |
| 393 | // The default hashing of StringRef produces different results on 32 and 64 |
| 394 | // bit systems so we sort by st_name. That is arbitrary but deterministic. |
| 395 | // FIXME: Experiment with passing in a custom hashing instead. |
| 396 | auto *Syms = reinterpret_cast<Elf_Sym *>(BufStart); |
| 397 | ++Syms; |
Rafael Espindola | 5b3942f | 2015-09-01 20:36:51 +0000 | [diff] [blame] | 398 | array_pod_sort(Syms, Syms + NumVisible, compareSym<ELFT>); |
Rafael Espindola | 62b81b8 | 2015-08-14 13:07:05 +0000 | [diff] [blame] | 399 | } |
Rafael Espindola | f763ca3 | 2015-08-14 02:42:20 +0000 | [diff] [blame] | 400 | |
Rafael Espindola | a175eb6 | 2015-08-13 18:37:23 +0000 | [diff] [blame] | 401 | template <bool Is64Bits> |
| 402 | template <endianness E> |
Rafael Espindola | ebd2108 | 2015-08-13 22:14:37 +0000 | [diff] [blame] | 403 | void OutputSectionBase<Is64Bits>::writeHeaderTo( |
Rafael Espindola | a175eb6 | 2015-08-13 18:37:23 +0000 | [diff] [blame] | 404 | typename ELFFile<ELFType<E, Is64Bits>>::Elf_Shdr *SHdr) { |
| 405 | SHdr->sh_name = Header.sh_name; |
| 406 | SHdr->sh_type = Header.sh_type; |
| 407 | SHdr->sh_flags = Header.sh_flags; |
| 408 | SHdr->sh_addr = Header.sh_addr; |
| 409 | SHdr->sh_offset = Header.sh_offset; |
| 410 | SHdr->sh_size = Header.sh_size; |
| 411 | SHdr->sh_link = Header.sh_link; |
| 412 | SHdr->sh_info = Header.sh_info; |
| 413 | SHdr->sh_addralign = Header.sh_addralign; |
| 414 | SHdr->sh_entsize = Header.sh_entsize; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Rafael Espindola | a747179 | 2015-08-13 17:04:50 +0000 | [diff] [blame] | 417 | namespace { |
| 418 | template <bool Is64Bits> struct SectionKey { |
| 419 | typedef typename std::conditional<Is64Bits, uint64_t, uint32_t>::type uintX_t; |
| 420 | StringRef Name; |
| 421 | uint32_t sh_type; |
| 422 | uintX_t sh_flags; |
| 423 | }; |
| 424 | } |
| 425 | namespace llvm { |
| 426 | template <bool Is64Bits> struct DenseMapInfo<SectionKey<Is64Bits>> { |
| 427 | static SectionKey<Is64Bits> getEmptyKey() { |
| 428 | return SectionKey<Is64Bits>{DenseMapInfo<StringRef>::getEmptyKey(), 0, 0}; |
| 429 | } |
| 430 | static SectionKey<Is64Bits> getTombstoneKey() { |
| 431 | return SectionKey<Is64Bits>{DenseMapInfo<StringRef>::getTombstoneKey(), 0, |
| 432 | 0}; |
| 433 | } |
| 434 | static unsigned getHashValue(const SectionKey<Is64Bits> &Val) { |
| 435 | return hash_combine(Val.Name, Val.sh_type, Val.sh_flags); |
| 436 | } |
| 437 | static bool isEqual(const SectionKey<Is64Bits> &LHS, |
| 438 | const SectionKey<Is64Bits> &RHS) { |
| 439 | return DenseMapInfo<StringRef>::isEqual(LHS.Name, RHS.Name) && |
| 440 | LHS.sh_type == RHS.sh_type && LHS.sh_flags == RHS.sh_flags; |
| 441 | } |
| 442 | }; |
| 443 | } |
| 444 | |
Rafael Espindola | b56cb94 | 2015-09-01 00:16:38 +0000 | [diff] [blame] | 445 | template <class ELFT> |
| 446 | static bool cmpAlign(const DefinedCommon<ELFT> *A, |
| 447 | const DefinedCommon<ELFT> *B) { |
Rafael Espindola | f31f961 | 2015-09-01 01:19:12 +0000 | [diff] [blame] | 448 | return A->MaxAlignment > B->MaxAlignment; |
Rafael Espindola | b56cb94 | 2015-09-01 00:16:38 +0000 | [diff] [blame] | 449 | } |
| 450 | |
Rafael Espindola | b01b574 | 2015-09-08 18:08:57 +0000 | [diff] [blame] | 451 | template <bool Is64Bits> |
| 452 | static bool compSec(OutputSectionBase<Is64Bits> *A, |
| 453 | OutputSectionBase<Is64Bits> *B) { |
| 454 | // Place SHF_ALLOC sections first. |
| 455 | return (A->getFlags() & SHF_ALLOC) && !(B->getFlags() & SHF_ALLOC); |
| 456 | } |
| 457 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 458 | // Create output section objects and add them to OutputSections. |
| 459 | template <class ELFT> void Writer<ELFT>::createSections() { |
Rafael Espindola | 83b0dc6 | 2015-08-13 22:21:37 +0000 | [diff] [blame] | 460 | SmallDenseMap<SectionKey<ELFT::Is64Bits>, OutputSection<ELFT> *> Map; |
Rafael Espindola | 0518574 | 2015-08-31 22:07:18 +0000 | [diff] [blame] | 461 | auto getSection = [&](StringRef Name, uint32_t sh_type, |
| 462 | uintX_t sh_flags) -> OutputSection<ELFT> * { |
| 463 | SectionKey<ELFT::Is64Bits> Key{Name, sh_type, sh_flags}; |
| 464 | OutputSection<ELFT> *&Sec = Map[Key]; |
| 465 | if (!Sec) { |
| 466 | Sec = new (CAlloc.Allocate()) |
| 467 | OutputSection<ELFT>(Key.Name, Key.sh_type, Key.sh_flags); |
Rafael Espindola | b01b574 | 2015-09-08 18:08:57 +0000 | [diff] [blame] | 468 | OutputSections.push_back(Sec); |
Rafael Espindola | 0518574 | 2015-08-31 22:07:18 +0000 | [diff] [blame] | 469 | } |
| 470 | return Sec; |
| 471 | }; |
| 472 | |
Rafael Espindola | e6f5210 | 2015-08-24 14:48:18 +0000 | [diff] [blame] | 473 | const SymbolTable &Symtab = SymTable.getSymTable(); |
Rafael Espindola | 222edc6 | 2015-09-03 18:56:20 +0000 | [diff] [blame] | 474 | for (const std::unique_ptr<ObjectFileBase> &FileB : Symtab.getObjectFiles()) { |
Rafael Espindola | e7a00e3 | 2015-08-05 13:55:34 +0000 | [diff] [blame] | 475 | auto &File = cast<ObjectFile<ELFT>>(*FileB); |
| 476 | for (SectionChunk<ELFT> *C : File.getChunks()) { |
Rafael Espindola | 832b93f | 2015-08-24 20:06:32 +0000 | [diff] [blame] | 477 | if (!C) |
| 478 | continue; |
Rafael Espindola | a747179 | 2015-08-13 17:04:50 +0000 | [diff] [blame] | 479 | const Elf_Shdr *H = C->getSectionHdr(); |
Rafael Espindola | 0518574 | 2015-08-31 22:07:18 +0000 | [diff] [blame] | 480 | OutputSection<ELFT> *Sec = |
| 481 | getSection(C->getSectionName(), H->sh_type, H->sh_flags); |
Rafael Espindola | 29e8d34 | 2015-08-13 17:35:13 +0000 | [diff] [blame] | 482 | Sec->addChunk(C); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 483 | } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 484 | } |
Rafael Espindola | 0518574 | 2015-08-31 22:07:18 +0000 | [diff] [blame] | 485 | |
Rafael Espindola | 8b09d68 | 2015-08-31 22:33:21 +0000 | [diff] [blame] | 486 | SymTable.BSSSec = getSection(".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE); |
| 487 | OutputSection<ELFT> *BSSSec = SymTable.BSSSec; |
Rafael Espindola | 0518574 | 2015-08-31 22:07:18 +0000 | [diff] [blame] | 488 | // FIXME: Try to avoid the extra walk over all global symbols. |
Rafael Espindola | 5b3942f | 2015-09-01 20:36:51 +0000 | [diff] [blame] | 489 | unsigned &NumVisible = SymTable.NumVisible; |
Rafael Espindola | ee1364f | 2015-09-01 21:47:21 +0000 | [diff] [blame] | 490 | llvm::StringTableBuilder &Builder = StringTable.StrTabBuilder; |
Rafael Espindola | b56cb94 | 2015-09-01 00:16:38 +0000 | [diff] [blame] | 491 | std::vector<DefinedCommon<ELFT> *> CommonSymbols; |
Rafael Espindola | 0518574 | 2015-08-31 22:07:18 +0000 | [diff] [blame] | 492 | for (auto &P : Symtab.getSymbols()) { |
Rafael Espindola | ee1364f | 2015-09-01 21:47:21 +0000 | [diff] [blame] | 493 | StringRef Name = P.first; |
Rafael Espindola | 0518574 | 2015-08-31 22:07:18 +0000 | [diff] [blame] | 494 | SymbolBody *Body = P.second->Body; |
Rafael Espindola | b56cb94 | 2015-09-01 00:16:38 +0000 | [diff] [blame] | 495 | if (auto *C = dyn_cast<DefinedCommon<ELFT>>(Body)) |
| 496 | CommonSymbols.push_back(C); |
Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 497 | if (!includeInSymtab(*Body)) |
Rafael Espindola | ee1364f | 2015-09-01 21:47:21 +0000 | [diff] [blame] | 498 | continue; |
| 499 | NumVisible++; |
| 500 | Builder.add(Name); |
Rafael Espindola | b56cb94 | 2015-09-01 00:16:38 +0000 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | // Sort the common symbols by alignment as an heuristic to pack them better. |
| 504 | std::stable_sort(CommonSymbols.begin(), CommonSymbols.end(), cmpAlign<ELFT>); |
| 505 | uintX_t Off = BSSSec->getSize(); |
| 506 | for (DefinedCommon<ELFT> *C : CommonSymbols) { |
Rafael Espindola | 0518574 | 2015-08-31 22:07:18 +0000 | [diff] [blame] | 507 | const Elf_Sym &Sym = C->Sym; |
Rafael Espindola | f31f961 | 2015-09-01 01:19:12 +0000 | [diff] [blame] | 508 | uintX_t Align = C->MaxAlignment; |
Rafael Espindola | 0518574 | 2015-08-31 22:07:18 +0000 | [diff] [blame] | 509 | Off = RoundUpToAlignment(Off, Align); |
Rafael Espindola | ce8c9c0 | 2015-08-31 22:55:21 +0000 | [diff] [blame] | 510 | C->OffsetInBSS = Off; |
Rafael Espindola | 0518574 | 2015-08-31 22:07:18 +0000 | [diff] [blame] | 511 | Off += Sym.st_size; |
| 512 | } |
Rafael Espindola | b56cb94 | 2015-09-01 00:16:38 +0000 | [diff] [blame] | 513 | |
Rafael Espindola | 0518574 | 2015-08-31 22:07:18 +0000 | [diff] [blame] | 514 | BSSSec->setSize(Off); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 515 | |
Rafael Espindola | b01b574 | 2015-09-08 18:08:57 +0000 | [diff] [blame] | 516 | OutputSections.push_back(&SymTable); |
| 517 | OutputSections.push_back(&StringTable); |
| 518 | |
Rafael Espindola | 740fafe | 2015-09-08 19:43:27 +0000 | [diff] [blame] | 519 | const std::vector<std::unique_ptr<SharedFileBase>> &SharedFiles = |
| 520 | Symtab.getSharedFiles(); |
| 521 | if (!SharedFiles.empty()) |
| 522 | OutputSections.push_back(&DynamicSec); |
| 523 | |
Rafael Espindola | b01b574 | 2015-09-08 18:08:57 +0000 | [diff] [blame] | 524 | std::stable_sort(OutputSections.begin(), OutputSections.end(), |
| 525 | compSec<ELFT::Is64Bits>); |
| 526 | for (unsigned I = 0, N = OutputSections.size(); I < N; ++I) |
| 527 | OutputSections[I]->setSectionIndex(I + 1); |
Rafael Espindola | abad618 | 2015-08-13 15:23:46 +0000 | [diff] [blame] | 528 | } |
| 529 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 530 | // Visits all sections to assign incremental, non-overlapping RVAs and |
| 531 | // file offsets. |
| 532 | template <class ELFT> void Writer<ELFT>::assignAddresses() { |
Michael J. Spencer | 8039dae2 | 2015-07-29 00:30:10 +0000 | [diff] [blame] | 533 | SizeOfHeaders = RoundUpToAlignment(sizeof(Elf_Ehdr_Impl<ELFT>), PageSize); |
Rafael Espindola | 98f6bd0 | 2015-08-11 23:14:13 +0000 | [diff] [blame] | 534 | uintX_t VA = 0x1000; // The first page is kept unmapped. |
| 535 | uintX_t FileOff = SizeOfHeaders; |
Rafael Espindola | abad618 | 2015-08-13 15:23:46 +0000 | [diff] [blame] | 536 | |
Rafael Espindola | ebd2108 | 2015-08-13 22:14:37 +0000 | [diff] [blame] | 537 | for (OutputSectionBase<ELFT::Is64Bits> *Sec : OutputSections) { |
| 538 | StringTable.add(Sec->getName()); |
| 539 | Sec->finalize(); |
| 540 | |
Rafael Espindola | 2db634d | 2015-08-13 20:24:18 +0000 | [diff] [blame] | 541 | uintX_t Align = Sec->getAlign(); |
| 542 | uintX_t Size = Sec->getSize(); |
Rafael Espindola | ef1ac01 | 2015-08-13 15:31:17 +0000 | [diff] [blame] | 543 | if (Sec->getFlags() & SHF_ALLOC) { |
| 544 | Sec->setVA(VA); |
Rafael Espindola | 2db634d | 2015-08-13 20:24:18 +0000 | [diff] [blame] | 545 | VA += RoundUpToAlignment(Size, Align); |
Rafael Espindola | ef1ac01 | 2015-08-13 15:31:17 +0000 | [diff] [blame] | 546 | } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 547 | Sec->setFileOffset(FileOff); |
Rafael Espindola | 058f343 | 2015-08-31 20:23:57 +0000 | [diff] [blame] | 548 | if (Sec->getType() != SHT_NOBITS) |
| 549 | FileOff += RoundUpToAlignment(Size, Align); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 550 | } |
Rafael Espindola | 6b83b90 | 2015-08-12 00:00:24 +0000 | [diff] [blame] | 551 | |
Rafael Espindola | 91009b3 | 2015-08-12 01:45:28 +0000 | [diff] [blame] | 552 | FileOff += OffsetToAlignment(FileOff, ELFT::Is64Bits ? 8 : 4); |
| 553 | |
Michael J. Spencer | 8039dae2 | 2015-07-29 00:30:10 +0000 | [diff] [blame] | 554 | // Add space for section headers. |
| 555 | SectionHeaderOff = FileOff; |
Rafael Espindola | 5f55387 | 2015-09-08 17:39:39 +0000 | [diff] [blame] | 556 | FileOff += getNumSections() * sizeof(Elf_Shdr_Impl<ELFT>); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 557 | FileSize = SizeOfHeaders + RoundUpToAlignment(FileOff - SizeOfHeaders, 8); |
| 558 | } |
| 559 | |
| 560 | template <class ELFT> void Writer<ELFT>::writeHeader() { |
| 561 | uint8_t *Buf = Buffer->getBufferStart(); |
| 562 | auto *EHdr = reinterpret_cast<Elf_Ehdr_Impl<ELFT> *>(Buf); |
| 563 | EHdr->e_ident[EI_MAG0] = 0x7F; |
| 564 | EHdr->e_ident[EI_MAG1] = 0x45; |
| 565 | EHdr->e_ident[EI_MAG2] = 0x4C; |
| 566 | EHdr->e_ident[EI_MAG3] = 0x46; |
Rafael Espindola | 4b7c2fc | 2015-08-05 15:08:40 +0000 | [diff] [blame] | 567 | EHdr->e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32; |
| 568 | EHdr->e_ident[EI_DATA] = ELFT::TargetEndianness == llvm::support::little |
| 569 | ? ELFDATA2LSB |
| 570 | : ELFDATA2MSB; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 571 | EHdr->e_ident[EI_VERSION] = EV_CURRENT; |
Rafael Espindola | 87ee8dc | 2015-08-05 11:55:52 +0000 | [diff] [blame] | 572 | EHdr->e_ident[EI_OSABI] = ELFOSABI_NONE; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 573 | |
| 574 | EHdr->e_type = ET_EXEC; |
Rafael Espindola | e6f5210 | 2015-08-24 14:48:18 +0000 | [diff] [blame] | 575 | const SymbolTable &Symtab = SymTable.getSymTable(); |
Rafael Espindola | f98d6d8 | 2015-09-03 20:03:54 +0000 | [diff] [blame] | 576 | auto &FirstObj = cast<ObjectFile<ELFT>>(*Symtab.getFirstELF()); |
| 577 | EHdr->e_machine = FirstObj.getEMachine(); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 578 | EHdr->e_version = EV_CURRENT; |
Rafael Espindola | b9fe03d | 2015-09-08 21:32:44 +0000 | [diff] [blame^] | 579 | EHdr->e_entry = 0x401000; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 580 | EHdr->e_phoff = sizeof(Elf_Ehdr_Impl<ELFT>); |
Michael J. Spencer | 8039dae2 | 2015-07-29 00:30:10 +0000 | [diff] [blame] | 581 | EHdr->e_shoff = SectionHeaderOff; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 582 | EHdr->e_ehsize = sizeof(Elf_Ehdr_Impl<ELFT>); |
| 583 | EHdr->e_phentsize = sizeof(Elf_Phdr_Impl<ELFT>); |
| 584 | EHdr->e_phnum = 1; |
| 585 | EHdr->e_shentsize = sizeof(Elf_Shdr_Impl<ELFT>); |
Rafael Espindola | 5f55387 | 2015-09-08 17:39:39 +0000 | [diff] [blame] | 586 | EHdr->e_shnum = getNumSections(); |
Rafael Espindola | 57b2592 | 2015-09-08 19:23:30 +0000 | [diff] [blame] | 587 | EHdr->e_shstrndx = StringTable.getSectionIndex(); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 588 | |
| 589 | auto PHdrs = reinterpret_cast<Elf_Phdr_Impl<ELFT> *>(Buf + EHdr->e_phoff); |
| 590 | PHdrs->p_type = PT_LOAD; |
| 591 | PHdrs->p_flags = PF_R | PF_X; |
| 592 | PHdrs->p_offset = 0x0000; |
| 593 | PHdrs->p_vaddr = 0x400000; |
| 594 | PHdrs->p_paddr = PHdrs->p_vaddr; |
| 595 | PHdrs->p_filesz = FileSize; |
| 596 | PHdrs->p_memsz = FileSize; |
| 597 | PHdrs->p_align = 0x4000; |
Michael J. Spencer | 8039dae2 | 2015-07-29 00:30:10 +0000 | [diff] [blame] | 598 | |
| 599 | auto SHdrs = reinterpret_cast<Elf_Shdr_Impl<ELFT> *>(Buf + EHdr->e_shoff); |
| 600 | // First entry is null. |
| 601 | ++SHdrs; |
Rafael Espindola | ebd2108 | 2015-08-13 22:14:37 +0000 | [diff] [blame] | 602 | for (OutputSectionBase<ELFT::Is64Bits> *Sec : OutputSections) { |
Rui Ueyama | 8050d32 | 2015-08-14 05:17:30 +0000 | [diff] [blame] | 603 | Sec->setNameOffset(StringTable.getFileOff(Sec->getName())); |
Rafael Espindola | a175eb6 | 2015-08-13 18:37:23 +0000 | [diff] [blame] | 604 | Sec->template writeHeaderTo<ELFT::TargetEndianness>(SHdrs++); |
Rafael Espindola | 6b83b90 | 2015-08-12 00:00:24 +0000 | [diff] [blame] | 605 | } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | template <class ELFT> void Writer<ELFT>::openFile(StringRef Path) { |
Rafael Espindola | bdc8f2f | 2015-08-13 00:31:46 +0000 | [diff] [blame] | 609 | ErrorOr<std::unique_ptr<FileOutputBuffer>> BufferOrErr = |
| 610 | FileOutputBuffer::create(Path, FileSize, FileOutputBuffer::F_executable); |
| 611 | error(BufferOrErr, Twine("failed to open ") + Path); |
| 612 | Buffer = std::move(*BufferOrErr); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | // Write section contents to a mmap'ed file. |
| 616 | template <class ELFT> void Writer<ELFT>::writeSections() { |
| 617 | uint8_t *Buf = Buffer->getBufferStart(); |
Rafael Espindola | ebd2108 | 2015-08-13 22:14:37 +0000 | [diff] [blame] | 618 | for (OutputSectionBase<ELFT::Is64Bits> *Sec : OutputSections) |
Rui Ueyama | 8050d32 | 2015-08-14 05:17:30 +0000 | [diff] [blame] | 619 | Sec->writeTo(Buf + Sec->getFileOff()); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 620 | } |