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 | ddf71fc | 2015-06-24 04:36:52 +0000 | [diff] [blame] | 31 | : File(F), Ptr(this), Header(H) { |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 32 | // Initialize SectionName. |
| 33 | File->getCOFFObj()->getSectionName(Header, SectionName); |
Rui Ueyama | 8b33f59 | 2015-06-10 04:21:47 +0000 | [diff] [blame] | 34 | |
Rui Ueyama | 2bf6a12 | 2015-06-14 21:50:50 +0000 | [diff] [blame] | 35 | // Bit [20:24] contains section alignment. Both 0 and 1 mean alignment 1. |
| 36 | unsigned Shift = (Header->Characteristics >> 20) & 0xF; |
| 37 | if (Shift > 0) |
| 38 | Align = uint32_t(1) << (Shift - 1); |
Rui Ueyama | 8b33f59 | 2015-06-10 04:21:47 +0000 | [diff] [blame] | 39 | |
| 40 | // When a new chunk is created, we don't if if it's going to make it |
| 41 | // to the final output. Initially all sections are unmarked in terms |
| 42 | // of garbage collection. The writer will call markLive() to mark |
| 43 | // all reachable section chunks. |
| 44 | Live = false; |
| 45 | |
| 46 | // COMDAT sections are not GC root. Non-text sections are not |
| 47 | // subject of garbage collection (thus they are root). |
| 48 | if (!isCOMDAT() && !(Header->Characteristics & IMAGE_SCN_CNT_CODE)) |
| 49 | Root = true; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Rui Ueyama | d6fefba4 | 2015-05-28 19:45:43 +0000 | [diff] [blame] | 52 | void SectionChunk::writeTo(uint8_t *Buf) { |
Rui Ueyama | 9aefa0c | 2015-05-28 20:04:51 +0000 | [diff] [blame] | 53 | if (!hasData()) |
| 54 | return; |
Rui Ueyama | 743afa0 | 2015-06-06 04:07:39 +0000 | [diff] [blame] | 55 | // Copy section contents from source object file to output file. |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 56 | ArrayRef<uint8_t> Data; |
| 57 | File->getCOFFObj()->getSectionContents(Header, Data); |
Rui Ueyama | d6fefba4 | 2015-05-28 19:45:43 +0000 | [diff] [blame] | 58 | memcpy(Buf + FileOff, Data.data(), Data.size()); |
Rui Ueyama | 743afa0 | 2015-06-06 04:07:39 +0000 | [diff] [blame] | 59 | |
| 60 | // Apply relocations. |
Rui Ueyama | cde9242 | 2015-06-24 23:03:17 +0000 | [diff] [blame^] | 61 | for (const coff_relocation *Rel = relBegin(), *E = relEnd(); Rel != E; ++Rel) |
Rui Ueyama | 743afa0 | 2015-06-06 04:07:39 +0000 | [diff] [blame] | 62 | applyReloc(Buf, Rel); |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Rui Ueyama | 8b33f59 | 2015-06-10 04:21:47 +0000 | [diff] [blame] | 65 | void SectionChunk::mark() { |
Rui Ueyama | 4108f3f | 2015-06-14 22:01:39 +0000 | [diff] [blame] | 66 | assert(!Live); |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 67 | Live = true; |
| 68 | |
| 69 | // Mark all symbols listed in the relocation table for this section. |
Rui Ueyama | cde9242 | 2015-06-24 23:03:17 +0000 | [diff] [blame^] | 70 | for (const coff_relocation *Rel = relBegin(), *E = relEnd(); Rel != E; |
| 71 | ++Rel) { |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 72 | SymbolBody *B = File->getSymbolBody(Rel->SymbolTableIndex); |
| 73 | if (auto *Def = dyn_cast<Defined>(B)) |
| 74 | Def->markLive(); |
| 75 | } |
| 76 | |
| 77 | // Mark associative sections if any. |
| 78 | for (Chunk *C : AssocChildren) |
| 79 | C->markLive(); |
| 80 | } |
| 81 | |
| 82 | void SectionChunk::addAssociative(SectionChunk *Child) { |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 83 | AssocChildren.push_back(Child); |
Rui Ueyama | 8b33f59 | 2015-06-10 04:21:47 +0000 | [diff] [blame] | 84 | // Associative sections are live if their parent COMDATs are live, |
| 85 | // and vice versa, so they are not considered live by themselves. |
| 86 | Child->Root = false; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Rui Ueyama | a6cd6c0 | 2015-06-07 23:10:50 +0000 | [diff] [blame] | 89 | static void add16(uint8_t *P, int16_t V) { write16le(P, read16le(P) + V); } |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 90 | static void add32(uint8_t *P, int32_t V) { write32le(P, read32le(P) + V); } |
| 91 | static void add64(uint8_t *P, int64_t V) { write64le(P, read64le(P) + V); } |
| 92 | |
| 93 | // Implements x64 PE/COFF relocations. |
| 94 | void SectionChunk::applyReloc(uint8_t *Buf, const coff_relocation *Rel) { |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 95 | uint8_t *Off = Buf + FileOff + Rel->VirtualAddress; |
| 96 | SymbolBody *Body = File->getSymbolBody(Rel->SymbolTableIndex); |
| 97 | uint64_t S = cast<Defined>(Body)->getRVA(); |
| 98 | uint64_t P = RVA + Rel->VirtualAddress; |
| 99 | switch (Rel->Type) { |
| 100 | case IMAGE_REL_AMD64_ADDR32: add32(Off, S + Config->ImageBase); break; |
| 101 | case IMAGE_REL_AMD64_ADDR64: add64(Off, S + Config->ImageBase); break; |
| 102 | case IMAGE_REL_AMD64_ADDR32NB: add32(Off, S); break; |
| 103 | case IMAGE_REL_AMD64_REL32: add32(Off, S - P - 4); break; |
| 104 | case IMAGE_REL_AMD64_REL32_1: add32(Off, S - P - 5); break; |
| 105 | case IMAGE_REL_AMD64_REL32_2: add32(Off, S - P - 6); break; |
| 106 | case IMAGE_REL_AMD64_REL32_3: add32(Off, S - P - 7); break; |
| 107 | case IMAGE_REL_AMD64_REL32_4: add32(Off, S - P - 8); break; |
| 108 | case IMAGE_REL_AMD64_REL32_5: add32(Off, S - P - 9); break; |
| 109 | case IMAGE_REL_AMD64_SECTION: add16(Off, Out->getSectionIndex()); break; |
| 110 | case IMAGE_REL_AMD64_SECREL: add32(Off, S - Out->getRVA()); break; |
| 111 | default: |
| 112 | llvm::report_fatal_error("Unsupported relocation type"); |
| 113 | } |
| 114 | } |
| 115 | |
Rui Ueyama | 588e832 | 2015-06-15 01:23:58 +0000 | [diff] [blame] | 116 | // Windows-specific. |
| 117 | // Collect all locations that contain absolute 64-bit addresses, |
| 118 | // which need to be fixed by the loader if load-time relocation is needed. |
| 119 | // Only called when base relocation is enabled. |
| 120 | void SectionChunk::getBaserels(std::vector<uint32_t> *Res, Defined *ImageBase) { |
Rui Ueyama | cde9242 | 2015-06-24 23:03:17 +0000 | [diff] [blame^] | 121 | for (const coff_relocation *Rel = relBegin(), *E = relEnd(); Rel != E; |
| 122 | ++Rel) { |
Rui Ueyama | 588e832 | 2015-06-15 01:23:58 +0000 | [diff] [blame] | 123 | // ADDR64 relocations contain absolute addresses. |
| 124 | // Symbol __ImageBase is special -- it's an absolute symbol, but its |
| 125 | // address never changes even if image is relocated. |
Rui Ueyama | 588e832 | 2015-06-15 01:23:58 +0000 | [diff] [blame] | 126 | if (Rel->Type != IMAGE_REL_AMD64_ADDR64) |
| 127 | continue; |
| 128 | SymbolBody *Body = File->getSymbolBody(Rel->SymbolTableIndex); |
| 129 | if (Body == ImageBase) |
| 130 | continue; |
| 131 | Res->push_back(RVA + Rel->VirtualAddress); |
| 132 | } |
| 133 | } |
| 134 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 135 | bool SectionChunk::hasData() const { |
| 136 | return !(Header->Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA); |
| 137 | } |
| 138 | |
| 139 | uint32_t SectionChunk::getPermissions() const { |
| 140 | return Header->Characteristics & PermMask; |
| 141 | } |
| 142 | |
| 143 | bool SectionChunk::isCOMDAT() const { |
| 144 | return Header->Characteristics & IMAGE_SCN_LNK_COMDAT; |
| 145 | } |
| 146 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 147 | void SectionChunk::printDiscardedMessage() { |
Rui Ueyama | ddf71fc | 2015-06-24 04:36:52 +0000 | [diff] [blame] | 148 | if (this == Ptr) { |
| 149 | // Removed by dead-stripping. |
| 150 | llvm::dbgs() << "Discarded " << Sym->getName() << "\n"; |
| 151 | } else { |
| 152 | // Removed by ICF. |
| 153 | llvm::dbgs() << "Replaced " << Sym->getName() << "\n"; |
| 154 | } |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Rui Ueyama | 6a60be7 | 2015-06-24 00:00:52 +0000 | [diff] [blame] | 157 | StringRef SectionChunk::getDebugName() { |
| 158 | return Sym->getName(); |
| 159 | } |
| 160 | |
Rui Ueyama | ddf71fc | 2015-06-24 04:36:52 +0000 | [diff] [blame] | 161 | uint64_t SectionChunk::getHash() const { |
| 162 | ArrayRef<uint8_t> A; |
| 163 | File->getCOFFObj()->getSectionContents(Header, A); |
| 164 | return hash_combine(getPermissions(), llvm::hash_value(SectionName), |
| 165 | uint32_t(Header->SizeOfRawData), |
| 166 | uint32_t(Header->NumberOfRelocations), |
| 167 | hash_combine_range(A.data(), A.data() + A.size())); |
| 168 | } |
| 169 | |
| 170 | // Returns true if this and a given chunk are identical COMDAT sections. |
| 171 | bool SectionChunk::equals(const SectionChunk *X) const { |
| 172 | // Compare headers |
| 173 | if (getPermissions() != X->getPermissions()) |
| 174 | return false; |
| 175 | if (SectionName != X->SectionName) |
| 176 | return false; |
| 177 | if (Header->SizeOfRawData != X->Header->SizeOfRawData) |
| 178 | return false; |
| 179 | if (Header->NumberOfRelocations != X->Header->NumberOfRelocations) |
| 180 | return false; |
| 181 | |
| 182 | // Compare data |
| 183 | ArrayRef<uint8_t> A, B; |
| 184 | File->getCOFFObj()->getSectionContents(Header, A); |
| 185 | X->File->getCOFFObj()->getSectionContents(X->Header, B); |
| 186 | assert(A.size() == B.size()); |
| 187 | if (memcmp(A.data(), B.data(), A.size())) |
| 188 | return false; |
| 189 | |
| 190 | // Compare relocations |
Rui Ueyama | cde9242 | 2015-06-24 23:03:17 +0000 | [diff] [blame^] | 191 | const coff_relocation *Rel1 = relBegin(); |
| 192 | const coff_relocation *End = relEnd(); |
| 193 | const coff_relocation *Rel2 = X->relBegin(); |
| 194 | for (; Rel1 != End; ++Rel1, ++Rel2) { |
Rui Ueyama | ddf71fc | 2015-06-24 04:36:52 +0000 | [diff] [blame] | 195 | if (Rel1->Type != Rel2->Type) |
| 196 | return false; |
| 197 | if (Rel1->VirtualAddress != Rel2->VirtualAddress) |
| 198 | return false; |
| 199 | SymbolBody *B1 = File->getSymbolBody(Rel1->SymbolTableIndex); |
| 200 | SymbolBody *B2 = X->File->getSymbolBody(Rel2->SymbolTableIndex); |
| 201 | if (auto *C1 = dyn_cast<DefinedCOMDAT>(B1)) |
| 202 | if (auto *C2 = dyn_cast<DefinedCOMDAT>(B2)) |
| 203 | if (C1->getChunk() == C2->getChunk()) |
| 204 | continue; |
| 205 | if (B1 != B2) |
| 206 | return false; |
| 207 | } |
| 208 | return true; |
| 209 | } |
| 210 | |
| 211 | // Returns a pointer to this chunk or its replacement. |
| 212 | SectionChunk *SectionChunk::repl() { |
| 213 | while (Ptr != Ptr->Ptr) |
| 214 | Ptr = Ptr->Ptr; |
| 215 | return Ptr; |
| 216 | } |
| 217 | |
| 218 | void SectionChunk::replaceWith(SectionChunk *Other) { |
| 219 | Ptr = Other; |
| 220 | Live = false; |
| 221 | } |
| 222 | |
Rui Ueyama | cde9242 | 2015-06-24 23:03:17 +0000 | [diff] [blame^] | 223 | const coff_relocation *SectionChunk::relBegin() const { |
| 224 | if (!Reloc) { |
| 225 | DataRefImpl Ref; |
| 226 | Ref.p = uintptr_t(Header); |
| 227 | SectionRef Sref(Ref, File->getCOFFObj()); |
| 228 | relocation_iterator It = Sref.relocation_begin(); |
| 229 | Reloc = File->getCOFFObj()->getCOFFRelocation(*It); |
| 230 | } |
| 231 | return Reloc; |
| 232 | } |
| 233 | |
| 234 | const coff_relocation *SectionChunk::relEnd() const { |
| 235 | const coff_relocation *I = relBegin(); |
| 236 | return I + Header->NumberOfRelocations; |
| 237 | } |
| 238 | |
Rui Ueyama | 9cf1abb | 2015-06-08 03:17:07 +0000 | [diff] [blame] | 239 | CommonChunk::CommonChunk(const COFFSymbolRef S) : Sym(S) { |
Rui Ueyama | 5e31d0b | 2015-06-20 07:25:45 +0000 | [diff] [blame] | 240 | // Common symbols are aligned on natural boundaries up to 32 bytes. |
| 241 | // This is what MSVC link.exe does. |
| 242 | Align = std::min(uint64_t(32), NextPowerOf2(Sym.getValue())); |
Rui Ueyama | 9cf1abb | 2015-06-08 03:17:07 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 245 | uint32_t CommonChunk::getPermissions() const { |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 246 | return IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_READ | |
| 247 | IMAGE_SCN_MEM_WRITE; |
| 248 | } |
| 249 | |
Rui Ueyama | d6fefba4 | 2015-05-28 19:45:43 +0000 | [diff] [blame] | 250 | void StringChunk::writeTo(uint8_t *Buf) { |
| 251 | memcpy(Buf + FileOff, Str.data(), Str.size()); |
| 252 | } |
| 253 | |
| 254 | void ImportThunkChunk::writeTo(uint8_t *Buf) { |
| 255 | memcpy(Buf + FileOff, ImportThunkData, sizeof(ImportThunkData)); |
Rui Ueyama | 743afa0 | 2015-06-06 04:07:39 +0000 | [diff] [blame] | 256 | // The first two bytes is a JMP instruction. Fill its operand. |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 257 | uint32_t Operand = ImpSymbol->getRVA() - RVA - getSize(); |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 258 | write32le(Buf + FileOff + 2, Operand); |
| 259 | } |
| 260 | |
Rui Ueyama | 588e832 | 2015-06-15 01:23:58 +0000 | [diff] [blame] | 261 | // Windows-specific. |
| 262 | // This class represents a block in .reloc section. |
| 263 | BaserelChunk::BaserelChunk(uint32_t Page, uint32_t *Begin, uint32_t *End) { |
| 264 | // Block header consists of 4 byte page RVA and 4 byte block size. |
| 265 | // Each entry is 2 byte. Last entry may be padding. |
| 266 | Data.resize(RoundUpToAlignment((End - Begin) * 2 + 8, 4)); |
| 267 | uint8_t *P = Data.data(); |
| 268 | write32le(P, Page); |
| 269 | write32le(P + 4, Data.size()); |
| 270 | P += 8; |
| 271 | for (uint32_t *I = Begin; I != End; ++I) { |
| 272 | write16le(P, (IMAGE_REL_BASED_DIR64 << 12) | (*I - Page)); |
| 273 | P += 2; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | void BaserelChunk::writeTo(uint8_t *Buf) { |
| 278 | memcpy(Buf + FileOff, Data.data(), Data.size()); |
| 279 | } |
| 280 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 281 | } // namespace coff |
| 282 | } // namespace lld |