| Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 1 | //===- Chunks.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 |  | 
|  | 10 | #include "Chunks.h" | 
|  | 11 | #include "InputFiles.h" | 
|  | 12 | #include "Writer.h" | 
| Rui Ueyama | ddf71fc | 2015-06-24 04:36:52 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/Hashing.h" | 
| Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/STLExtras.h" | 
|  | 15 | #include "llvm/Object/COFF.h" | 
|  | 16 | #include "llvm/Support/COFF.h" | 
|  | 17 | #include "llvm/Support/Debug.h" | 
|  | 18 | #include "llvm/Support/Endian.h" | 
|  | 19 | #include "llvm/Support/raw_ostream.h" | 
| Rui Ueyama | 5e31d0b | 2015-06-20 07:25:45 +0000 | [diff] [blame] | 20 | #include <algorithm> | 
| Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 21 |  | 
| Rui Ueyama | c6ea057 | 2015-06-06 22:46:15 +0000 | [diff] [blame] | 22 | using namespace llvm; | 
| Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 23 | using namespace llvm::object; | 
|  | 24 | using namespace llvm::support::endian; | 
|  | 25 | using namespace llvm::COFF; | 
| Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 26 |  | 
|  | 27 | namespace lld { | 
|  | 28 | namespace coff { | 
|  | 29 |  | 
| Peter Collingbourne | bd3a29d | 2015-06-24 00:12:36 +0000 | [diff] [blame] | 30 | SectionChunk::SectionChunk(ObjectFile *F, const coff_section *H) | 
| Rui Ueyama | 9b921e5 | 2015-06-25 22:00:42 +0000 | [diff] [blame] | 31 | : Chunk(SectionKind), Ptr(this), File(F), Header(H), | 
| Rui Ueyama | 02c3027 | 2015-06-25 17:43:37 +0000 | [diff] [blame] | 32 | Relocs(File->getCOFFObj()->getRelocations(Header)), | 
|  | 33 | NumRelocs(std::distance(Relocs.begin(), Relocs.end())) { | 
| Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 34 | // Initialize SectionName. | 
|  | 35 | File->getCOFFObj()->getSectionName(Header, SectionName); | 
| Rui Ueyama | 8b33f59 | 2015-06-10 04:21:47 +0000 | [diff] [blame] | 36 |  | 
| Rui Ueyama | 2bf6a12 | 2015-06-14 21:50:50 +0000 | [diff] [blame] | 37 | // Bit [20:24] contains section alignment. Both 0 and 1 mean alignment 1. | 
|  | 38 | unsigned Shift = (Header->Characteristics >> 20) & 0xF; | 
|  | 39 | if (Shift > 0) | 
|  | 40 | Align = uint32_t(1) << (Shift - 1); | 
| Rui Ueyama | 8b33f59 | 2015-06-10 04:21:47 +0000 | [diff] [blame] | 41 |  | 
| Rui Ueyama | 8b33f59 | 2015-06-10 04:21:47 +0000 | [diff] [blame] | 42 | // COMDAT sections are not GC root. Non-text sections are not | 
|  | 43 | // subject of garbage collection (thus they are root). | 
| Rui Ueyama | fc510f4 | 2015-06-25 19:10:58 +0000 | [diff] [blame] | 44 | Root = !isCOMDAT() && !(Header->Characteristics & IMAGE_SCN_CNT_CODE); | 
| Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 45 | } | 
|  | 46 |  | 
| Rui Ueyama | 42aa00b | 2015-06-25 00:33:38 +0000 | [diff] [blame] | 47 | static void add16(uint8_t *P, int16_t V) { write16le(P, read16le(P) + V); } | 
|  | 48 | static void add32(uint8_t *P, int32_t V) { write32le(P, read32le(P) + V); } | 
|  | 49 | static void add64(uint8_t *P, int64_t V) { write64le(P, read64le(P) + V); } | 
|  | 50 |  | 
| Rui Ueyama | d6fefba4 | 2015-05-28 19:45:43 +0000 | [diff] [blame] | 51 | void SectionChunk::writeTo(uint8_t *Buf) { | 
| Rui Ueyama | 9aefa0c | 2015-05-28 20:04:51 +0000 | [diff] [blame] | 52 | if (!hasData()) | 
|  | 53 | return; | 
| Rui Ueyama | 743afa0 | 2015-06-06 04:07:39 +0000 | [diff] [blame] | 54 | // Copy section contents from source object file to output file. | 
| Rui Ueyama | f34c088 | 2015-06-25 17:56:36 +0000 | [diff] [blame] | 55 | ArrayRef<uint8_t> A = getContents(); | 
|  | 56 | memcpy(Buf + FileOff, A.data(), A.size()); | 
| Rui Ueyama | 743afa0 | 2015-06-06 04:07:39 +0000 | [diff] [blame] | 57 |  | 
|  | 58 | // Apply relocations. | 
| Rui Ueyama | 42aa00b | 2015-06-25 00:33:38 +0000 | [diff] [blame] | 59 | for (const coff_relocation &Rel : Relocs) { | 
|  | 60 | uint8_t *Off = Buf + FileOff + Rel.VirtualAddress; | 
| Chandler Carruth | be6e80b | 2015-06-29 18:50:11 +0000 | [diff] [blame] | 61 | SymbolBody *Body = | 
|  | 62 | File->getSymbolBody(Rel.SymbolTableIndex)->getReplacement(); | 
| Rui Ueyama | 42aa00b | 2015-06-25 00:33:38 +0000 | [diff] [blame] | 63 | uint64_t S = cast<Defined>(Body)->getRVA(); | 
|  | 64 | uint64_t P = RVA + Rel.VirtualAddress; | 
|  | 65 | switch (Rel.Type) { | 
|  | 66 | case IMAGE_REL_AMD64_ADDR32:   add32(Off, S + Config->ImageBase); break; | 
|  | 67 | case IMAGE_REL_AMD64_ADDR64:   add64(Off, S + Config->ImageBase); break; | 
|  | 68 | case IMAGE_REL_AMD64_ADDR32NB: add32(Off, S); break; | 
|  | 69 | case IMAGE_REL_AMD64_REL32:    add32(Off, S - P - 4); break; | 
|  | 70 | case IMAGE_REL_AMD64_REL32_1:  add32(Off, S - P - 5); break; | 
|  | 71 | case IMAGE_REL_AMD64_REL32_2:  add32(Off, S - P - 6); break; | 
|  | 72 | case IMAGE_REL_AMD64_REL32_3:  add32(Off, S - P - 7); break; | 
|  | 73 | case IMAGE_REL_AMD64_REL32_4:  add32(Off, S - P - 8); break; | 
|  | 74 | case IMAGE_REL_AMD64_REL32_5:  add32(Off, S - P - 9); break; | 
|  | 75 | case IMAGE_REL_AMD64_SECTION:  add16(Off, Out->getSectionIndex()); break; | 
|  | 76 | case IMAGE_REL_AMD64_SECREL:   add32(Off, S - Out->getRVA()); break; | 
|  | 77 | default: | 
|  | 78 | llvm::report_fatal_error("Unsupported relocation type"); | 
|  | 79 | } | 
|  | 80 | } | 
| Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 81 | } | 
|  | 82 |  | 
| Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 83 | void SectionChunk::addAssociative(SectionChunk *Child) { | 
| Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 84 | AssocChildren.push_back(Child); | 
| Rui Ueyama | 8b33f59 | 2015-06-10 04:21:47 +0000 | [diff] [blame] | 85 | // Associative sections are live if their parent COMDATs are live, | 
|  | 86 | // and vice versa, so they are not considered live by themselves. | 
|  | 87 | Child->Root = false; | 
| Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 88 | } | 
|  | 89 |  | 
| Rui Ueyama | 588e832 | 2015-06-15 01:23:58 +0000 | [diff] [blame] | 90 | // Windows-specific. | 
|  | 91 | // Collect all locations that contain absolute 64-bit addresses, | 
|  | 92 | // which need to be fixed by the loader if load-time relocation is needed. | 
|  | 93 | // Only called when base relocation is enabled. | 
|  | 94 | void SectionChunk::getBaserels(std::vector<uint32_t> *Res, Defined *ImageBase) { | 
| Rui Ueyama | 42aa00b | 2015-06-25 00:33:38 +0000 | [diff] [blame] | 95 | for (const coff_relocation &Rel : Relocs) { | 
| Rui Ueyama | 588e832 | 2015-06-15 01:23:58 +0000 | [diff] [blame] | 96 | // ADDR64 relocations contain absolute addresses. | 
|  | 97 | // Symbol __ImageBase is special -- it's an absolute symbol, but its | 
|  | 98 | // address never changes even if image is relocated. | 
| Rui Ueyama | 42aa00b | 2015-06-25 00:33:38 +0000 | [diff] [blame] | 99 | if (Rel.Type != IMAGE_REL_AMD64_ADDR64) | 
| Rui Ueyama | 588e832 | 2015-06-15 01:23:58 +0000 | [diff] [blame] | 100 | continue; | 
| Chandler Carruth | be6e80b | 2015-06-29 18:50:11 +0000 | [diff] [blame] | 101 | SymbolBody *Body = | 
|  | 102 | File->getSymbolBody(Rel.SymbolTableIndex)->getReplacement(); | 
| Rui Ueyama | 588e832 | 2015-06-15 01:23:58 +0000 | [diff] [blame] | 103 | if (Body == ImageBase) | 
|  | 104 | continue; | 
| Rui Ueyama | 42aa00b | 2015-06-25 00:33:38 +0000 | [diff] [blame] | 105 | Res->push_back(RVA + Rel.VirtualAddress); | 
| Rui Ueyama | 588e832 | 2015-06-15 01:23:58 +0000 | [diff] [blame] | 106 | } | 
|  | 107 | } | 
|  | 108 |  | 
| Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 109 | bool SectionChunk::hasData() const { | 
|  | 110 | return !(Header->Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA); | 
|  | 111 | } | 
|  | 112 |  | 
|  | 113 | uint32_t SectionChunk::getPermissions() const { | 
|  | 114 | return Header->Characteristics & PermMask; | 
|  | 115 | } | 
|  | 116 |  | 
|  | 117 | bool SectionChunk::isCOMDAT() const { | 
|  | 118 | return Header->Characteristics & IMAGE_SCN_LNK_COMDAT; | 
|  | 119 | } | 
|  | 120 |  | 
| Rui Ueyama | fc510f4 | 2015-06-25 19:10:58 +0000 | [diff] [blame] | 121 | void SectionChunk::printDiscardedMessage() const { | 
| Rui Ueyama | ddf71fc | 2015-06-24 04:36:52 +0000 | [diff] [blame] | 122 | if (this == Ptr) { | 
|  | 123 | // Removed by dead-stripping. | 
|  | 124 | llvm::dbgs() << "Discarded " << Sym->getName() << "\n"; | 
|  | 125 | } else { | 
|  | 126 | // Removed by ICF. | 
|  | 127 | llvm::dbgs() << "Replaced " << Sym->getName() << "\n"; | 
|  | 128 | } | 
| Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 129 | } | 
|  | 130 |  | 
| Rui Ueyama | 6a60be7 | 2015-06-24 00:00:52 +0000 | [diff] [blame] | 131 | StringRef SectionChunk::getDebugName() { | 
|  | 132 | return Sym->getName(); | 
|  | 133 | } | 
|  | 134 |  | 
| Rui Ueyama | ddf71fc | 2015-06-24 04:36:52 +0000 | [diff] [blame] | 135 | uint64_t SectionChunk::getHash() const { | 
| Rui Ueyama | f34c088 | 2015-06-25 17:56:36 +0000 | [diff] [blame] | 136 | ArrayRef<uint8_t> A = getContents(); | 
| Rui Ueyama | 02c3027 | 2015-06-25 17:43:37 +0000 | [diff] [blame] | 137 | return hash_combine(getPermissions(), | 
|  | 138 | llvm::hash_value(SectionName), | 
|  | 139 | NumRelocs, | 
| Rui Ueyama | ddf71fc | 2015-06-24 04:36:52 +0000 | [diff] [blame] | 140 | uint32_t(Header->SizeOfRawData), | 
| Rui Ueyama | 02c3027 | 2015-06-25 17:43:37 +0000 | [diff] [blame] | 141 | std::distance(Relocs.end(), Relocs.begin()), | 
| Rui Ueyama | ddf71fc | 2015-06-24 04:36:52 +0000 | [diff] [blame] | 142 | hash_combine_range(A.data(), A.data() + A.size())); | 
|  | 143 | } | 
|  | 144 |  | 
|  | 145 | // Returns true if this and a given chunk are identical COMDAT sections. | 
|  | 146 | bool SectionChunk::equals(const SectionChunk *X) const { | 
|  | 147 | // Compare headers | 
|  | 148 | if (getPermissions() != X->getPermissions()) | 
|  | 149 | return false; | 
|  | 150 | if (SectionName != X->SectionName) | 
|  | 151 | return false; | 
|  | 152 | if (Header->SizeOfRawData != X->Header->SizeOfRawData) | 
|  | 153 | return false; | 
| Rui Ueyama | 02c3027 | 2015-06-25 17:43:37 +0000 | [diff] [blame] | 154 | if (NumRelocs != X->NumRelocs) | 
| Rui Ueyama | ddf71fc | 2015-06-24 04:36:52 +0000 | [diff] [blame] | 155 | return false; | 
|  | 156 |  | 
|  | 157 | // Compare data | 
| Rui Ueyama | f34c088 | 2015-06-25 17:56:36 +0000 | [diff] [blame] | 158 | if (getContents() != X->getContents()) | 
| Rui Ueyama | ddf71fc | 2015-06-24 04:36:52 +0000 | [diff] [blame] | 159 | return false; | 
|  | 160 |  | 
| Rui Ueyama | 871847e | 2015-06-28 01:30:54 +0000 | [diff] [blame] | 161 | // Compare associative sections | 
|  | 162 | if (AssocChildren.size() != X->AssocChildren.size()) | 
|  | 163 | return false; | 
|  | 164 | for (size_t I = 0, E = AssocChildren.size(); I != E; ++I) | 
|  | 165 | if (AssocChildren[I]->Ptr != X->AssocChildren[I]->Ptr) | 
|  | 166 | return false; | 
|  | 167 |  | 
| Rui Ueyama | ddf71fc | 2015-06-24 04:36:52 +0000 | [diff] [blame] | 168 | // Compare relocations | 
| Rui Ueyama | c6fcfbc | 2015-06-25 17:51:07 +0000 | [diff] [blame] | 169 | auto Eq = [&](const coff_relocation &R1, const coff_relocation &R2) { | 
|  | 170 | if (R1.Type != R2.Type) | 
| Rui Ueyama | ddf71fc | 2015-06-24 04:36:52 +0000 | [diff] [blame] | 171 | return false; | 
| Rui Ueyama | c6fcfbc | 2015-06-25 17:51:07 +0000 | [diff] [blame] | 172 | if (R1.VirtualAddress != R2.VirtualAddress) | 
| Rui Ueyama | ddf71fc | 2015-06-24 04:36:52 +0000 | [diff] [blame] | 173 | return false; | 
| Chandler Carruth | be6e80b | 2015-06-29 18:50:11 +0000 | [diff] [blame] | 174 | SymbolBody *B1 = File->getSymbolBody(R1.SymbolTableIndex)->getReplacement(); | 
|  | 175 | SymbolBody *B2 = | 
|  | 176 | X->File->getSymbolBody(R2.SymbolTableIndex)->getReplacement(); | 
| Rui Ueyama | 871847e | 2015-06-28 01:30:54 +0000 | [diff] [blame] | 177 | if (B1 == B2) | 
|  | 178 | return true; | 
| Rui Ueyama | 9b921e5 | 2015-06-25 22:00:42 +0000 | [diff] [blame] | 179 | auto *D1 = dyn_cast<DefinedRegular>(B1); | 
|  | 180 | auto *D2 = dyn_cast<DefinedRegular>(B2); | 
| Rui Ueyama | 871847e | 2015-06-28 01:30:54 +0000 | [diff] [blame] | 181 | return (D1 && D2 && | 
|  | 182 | D1->getValue() == D2->getValue() && | 
|  | 183 | D1->getChunk() == D2->getChunk()); | 
| Rui Ueyama | c6fcfbc | 2015-06-25 17:51:07 +0000 | [diff] [blame] | 184 | }; | 
|  | 185 | return std::equal(Relocs.begin(), Relocs.end(), X->Relocs.begin(), Eq); | 
| Rui Ueyama | ddf71fc | 2015-06-24 04:36:52 +0000 | [diff] [blame] | 186 | } | 
|  | 187 |  | 
| Rui Ueyama | f34c088 | 2015-06-25 17:56:36 +0000 | [diff] [blame] | 188 | ArrayRef<uint8_t> SectionChunk::getContents() const { | 
|  | 189 | ArrayRef<uint8_t> A; | 
|  | 190 | File->getCOFFObj()->getSectionContents(Header, A); | 
|  | 191 | return A; | 
|  | 192 | } | 
|  | 193 |  | 
| Rui Ueyama | ddf71fc | 2015-06-24 04:36:52 +0000 | [diff] [blame] | 194 | void SectionChunk::replaceWith(SectionChunk *Other) { | 
| Rui Ueyama | 9b921e5 | 2015-06-25 22:00:42 +0000 | [diff] [blame] | 195 | Ptr = Other->Ptr; | 
| Rui Ueyama | ddf71fc | 2015-06-24 04:36:52 +0000 | [diff] [blame] | 196 | Live = false; | 
|  | 197 | } | 
|  | 198 |  | 
| Rui Ueyama | 9cf1abb | 2015-06-08 03:17:07 +0000 | [diff] [blame] | 199 | CommonChunk::CommonChunk(const COFFSymbolRef S) : Sym(S) { | 
| Rui Ueyama | 5e31d0b | 2015-06-20 07:25:45 +0000 | [diff] [blame] | 200 | // Common symbols are aligned on natural boundaries up to 32 bytes. | 
|  | 201 | // This is what MSVC link.exe does. | 
|  | 202 | Align = std::min(uint64_t(32), NextPowerOf2(Sym.getValue())); | 
| Rui Ueyama | 9cf1abb | 2015-06-08 03:17:07 +0000 | [diff] [blame] | 203 | } | 
|  | 204 |  | 
| Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 205 | uint32_t CommonChunk::getPermissions() const { | 
| Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 206 | return IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_READ | | 
|  | 207 | IMAGE_SCN_MEM_WRITE; | 
|  | 208 | } | 
|  | 209 |  | 
| Rui Ueyama | d6fefba4 | 2015-05-28 19:45:43 +0000 | [diff] [blame] | 210 | void StringChunk::writeTo(uint8_t *Buf) { | 
|  | 211 | memcpy(Buf + FileOff, Str.data(), Str.size()); | 
|  | 212 | } | 
|  | 213 |  | 
| Rui Ueyama | 7383562 | 2015-06-26 18:28:56 +0000 | [diff] [blame] | 214 | ImportThunkChunk::ImportThunkChunk(Defined *S) : ImpSymbol(S) { | 
|  | 215 | // Intel Optimization Manual says that all branch targets | 
|  | 216 | // should be 16-byte aligned. MSVC linker does this too. | 
|  | 217 | Align = 16; | 
|  | 218 | } | 
|  | 219 |  | 
| Rui Ueyama | d6fefba4 | 2015-05-28 19:45:43 +0000 | [diff] [blame] | 220 | void ImportThunkChunk::writeTo(uint8_t *Buf) { | 
|  | 221 | memcpy(Buf + FileOff, ImportThunkData, sizeof(ImportThunkData)); | 
| Rui Ueyama | 743afa0 | 2015-06-06 04:07:39 +0000 | [diff] [blame] | 222 | // The first two bytes is a JMP instruction. Fill its operand. | 
| Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 223 | uint32_t Operand = ImpSymbol->getRVA() - RVA - getSize(); | 
| Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 224 | write32le(Buf + FileOff + 2, Operand); | 
|  | 225 | } | 
|  | 226 |  | 
| Rui Ueyama | 588e832 | 2015-06-15 01:23:58 +0000 | [diff] [blame] | 227 | // Windows-specific. | 
|  | 228 | // This class represents a block in .reloc section. | 
|  | 229 | BaserelChunk::BaserelChunk(uint32_t Page, uint32_t *Begin, uint32_t *End) { | 
|  | 230 | // Block header consists of 4 byte page RVA and 4 byte block size. | 
|  | 231 | // Each entry is 2 byte. Last entry may be padding. | 
|  | 232 | Data.resize(RoundUpToAlignment((End - Begin) * 2 + 8, 4)); | 
|  | 233 | uint8_t *P = Data.data(); | 
|  | 234 | write32le(P, Page); | 
|  | 235 | write32le(P + 4, Data.size()); | 
|  | 236 | P += 8; | 
|  | 237 | for (uint32_t *I = Begin; I != End; ++I) { | 
|  | 238 | write16le(P, (IMAGE_REL_BASED_DIR64 << 12) | (*I - Page)); | 
|  | 239 | P += 2; | 
|  | 240 | } | 
|  | 241 | } | 
|  | 242 |  | 
| Rui Ueyama | 88e0f92 | 2015-06-25 03:31:47 +0000 | [diff] [blame] | 243 | void LocalImportChunk::writeTo(uint8_t *Buf) { | 
|  | 244 | write32le(Buf + FileOff, Sym->getRVA()); | 
|  | 245 | } | 
|  | 246 |  | 
| Rui Ueyama | 588e832 | 2015-06-15 01:23:58 +0000 | [diff] [blame] | 247 | void BaserelChunk::writeTo(uint8_t *Buf) { | 
|  | 248 | memcpy(Buf + FileOff, Data.data(), Data.size()); | 
|  | 249 | } | 
|  | 250 |  | 
| Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 251 | } // namespace coff | 
|  | 252 | } // namespace lld |