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