| 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 | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/STLExtras.h" | 
|  | 14 | #include "llvm/Object/COFF.h" | 
|  | 15 | #include "llvm/Support/COFF.h" | 
|  | 16 | #include "llvm/Support/Debug.h" | 
|  | 17 | #include "llvm/Support/Endian.h" | 
|  | 18 | #include "llvm/Support/raw_ostream.h" | 
| Rui Ueyama | 5e31d0b | 2015-06-20 07:25:45 +0000 | [diff] [blame] | 19 | #include <algorithm> | 
| Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 20 |  | 
| Rui Ueyama | c6ea057 | 2015-06-06 22:46:15 +0000 | [diff] [blame] | 21 | using namespace llvm; | 
| Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 22 | using namespace llvm::object; | 
|  | 23 | using namespace llvm::support::endian; | 
|  | 24 | using namespace llvm::COFF; | 
| Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 25 |  | 
|  | 26 | namespace lld { | 
|  | 27 | namespace coff { | 
|  | 28 |  | 
|  | 29 | SectionChunk::SectionChunk(ObjectFile *F, const coff_section *H, uint32_t SI) | 
|  | 30 | : File(F), Header(H), SectionIndex(SI) { | 
|  | 31 | // Initialize SectionName. | 
|  | 32 | File->getCOFFObj()->getSectionName(Header, SectionName); | 
| Rui Ueyama | 8b33f59 | 2015-06-10 04:21:47 +0000 | [diff] [blame] | 33 |  | 
| Rui Ueyama | 2bf6a12 | 2015-06-14 21:50:50 +0000 | [diff] [blame] | 34 | // Bit [20:24] contains section alignment. Both 0 and 1 mean alignment 1. | 
|  | 35 | unsigned Shift = (Header->Characteristics >> 20) & 0xF; | 
|  | 36 | if (Shift > 0) | 
|  | 37 | Align = uint32_t(1) << (Shift - 1); | 
| Rui Ueyama | 8b33f59 | 2015-06-10 04:21:47 +0000 | [diff] [blame] | 38 |  | 
|  | 39 | // When a new chunk is created, we don't if if it's going to make it | 
|  | 40 | // to the final output. Initially all sections are unmarked in terms | 
|  | 41 | // of garbage collection. The writer will call markLive() to mark | 
|  | 42 | // all reachable section chunks. | 
|  | 43 | Live = false; | 
|  | 44 |  | 
|  | 45 | // COMDAT sections are not GC root. Non-text sections are not | 
|  | 46 | // subject of garbage collection (thus they are root). | 
|  | 47 | if (!isCOMDAT() && !(Header->Characteristics & IMAGE_SCN_CNT_CODE)) | 
|  | 48 | Root = true; | 
| Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 49 | } | 
|  | 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 | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 55 | ArrayRef<uint8_t> Data; | 
|  | 56 | File->getCOFFObj()->getSectionContents(Header, Data); | 
| Rui Ueyama | d6fefba4 | 2015-05-28 19:45:43 +0000 | [diff] [blame] | 57 | memcpy(Buf + FileOff, Data.data(), Data.size()); | 
| Rui Ueyama | 743afa0 | 2015-06-06 04:07:39 +0000 | [diff] [blame] | 58 |  | 
|  | 59 | // Apply relocations. | 
|  | 60 | for (const auto &I : getSectionRef().relocations()) { | 
|  | 61 | const coff_relocation *Rel = File->getCOFFObj()->getCOFFRelocation(I); | 
|  | 62 | applyReloc(Buf, Rel); | 
|  | 63 | } | 
| Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 64 | } | 
|  | 65 |  | 
| Rui Ueyama | 8b33f59 | 2015-06-10 04:21:47 +0000 | [diff] [blame] | 66 | void SectionChunk::mark() { | 
| Rui Ueyama | 4108f3f | 2015-06-14 22:01:39 +0000 | [diff] [blame] | 67 | assert(!Live); | 
| Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 68 | Live = true; | 
|  | 69 |  | 
|  | 70 | // Mark all symbols listed in the relocation table for this section. | 
|  | 71 | for (const auto &I : getSectionRef().relocations()) { | 
|  | 72 | const coff_relocation *Rel = File->getCOFFObj()->getCOFFRelocation(I); | 
|  | 73 | SymbolBody *B = File->getSymbolBody(Rel->SymbolTableIndex); | 
|  | 74 | if (auto *Def = dyn_cast<Defined>(B)) | 
|  | 75 | Def->markLive(); | 
|  | 76 | } | 
|  | 77 |  | 
|  | 78 | // Mark associative sections if any. | 
|  | 79 | for (Chunk *C : AssocChildren) | 
|  | 80 | C->markLive(); | 
|  | 81 | } | 
|  | 82 |  | 
|  | 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 | a6cd6c0 | 2015-06-07 23:10:50 +0000 | [diff] [blame] | 90 | 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] | 91 | static void add32(uint8_t *P, int32_t V) { write32le(P, read32le(P) + V); } | 
|  | 92 | static void add64(uint8_t *P, int64_t V) { write64le(P, read64le(P) + V); } | 
|  | 93 |  | 
|  | 94 | // Implements x64 PE/COFF relocations. | 
|  | 95 | void SectionChunk::applyReloc(uint8_t *Buf, const coff_relocation *Rel) { | 
| Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 96 | uint8_t *Off = Buf + FileOff + Rel->VirtualAddress; | 
|  | 97 | SymbolBody *Body = File->getSymbolBody(Rel->SymbolTableIndex); | 
|  | 98 | uint64_t S = cast<Defined>(Body)->getRVA(); | 
|  | 99 | uint64_t P = RVA + Rel->VirtualAddress; | 
|  | 100 | switch (Rel->Type) { | 
|  | 101 | case IMAGE_REL_AMD64_ADDR32:   add32(Off, S + Config->ImageBase); break; | 
|  | 102 | case IMAGE_REL_AMD64_ADDR64:   add64(Off, S + Config->ImageBase); break; | 
|  | 103 | case IMAGE_REL_AMD64_ADDR32NB: add32(Off, S); break; | 
|  | 104 | case IMAGE_REL_AMD64_REL32:    add32(Off, S - P - 4); break; | 
|  | 105 | case IMAGE_REL_AMD64_REL32_1:  add32(Off, S - P - 5); break; | 
|  | 106 | case IMAGE_REL_AMD64_REL32_2:  add32(Off, S - P - 6); break; | 
|  | 107 | case IMAGE_REL_AMD64_REL32_3:  add32(Off, S - P - 7); break; | 
|  | 108 | case IMAGE_REL_AMD64_REL32_4:  add32(Off, S - P - 8); break; | 
|  | 109 | case IMAGE_REL_AMD64_REL32_5:  add32(Off, S - P - 9); break; | 
|  | 110 | case IMAGE_REL_AMD64_SECTION:  add16(Off, Out->getSectionIndex()); break; | 
|  | 111 | case IMAGE_REL_AMD64_SECREL:   add32(Off, S - Out->getRVA()); break; | 
|  | 112 | default: | 
|  | 113 | llvm::report_fatal_error("Unsupported relocation type"); | 
|  | 114 | } | 
|  | 115 | } | 
|  | 116 |  | 
| Rui Ueyama | 588e832 | 2015-06-15 01:23:58 +0000 | [diff] [blame] | 117 | // Windows-specific. | 
|  | 118 | // Collect all locations that contain absolute 64-bit addresses, | 
|  | 119 | // which need to be fixed by the loader if load-time relocation is needed. | 
|  | 120 | // Only called when base relocation is enabled. | 
|  | 121 | void SectionChunk::getBaserels(std::vector<uint32_t> *Res, Defined *ImageBase) { | 
|  | 122 | for (const auto &I : getSectionRef().relocations()) { | 
|  | 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. | 
|  | 126 | const coff_relocation *Rel = File->getCOFFObj()->getCOFFRelocation(I); | 
|  | 127 | if (Rel->Type != IMAGE_REL_AMD64_ADDR64) | 
|  | 128 | continue; | 
|  | 129 | SymbolBody *Body = File->getSymbolBody(Rel->SymbolTableIndex); | 
|  | 130 | if (Body == ImageBase) | 
|  | 131 | continue; | 
|  | 132 | Res->push_back(RVA + Rel->VirtualAddress); | 
|  | 133 | } | 
|  | 134 | } | 
|  | 135 |  | 
| Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 136 | bool SectionChunk::hasData() const { | 
|  | 137 | return !(Header->Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA); | 
|  | 138 | } | 
|  | 139 |  | 
|  | 140 | uint32_t SectionChunk::getPermissions() const { | 
|  | 141 | return Header->Characteristics & PermMask; | 
|  | 142 | } | 
|  | 143 |  | 
|  | 144 | bool SectionChunk::isCOMDAT() const { | 
|  | 145 | return Header->Characteristics & IMAGE_SCN_LNK_COMDAT; | 
|  | 146 | } | 
|  | 147 |  | 
| Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 148 | void SectionChunk::printDiscardedMessage() { | 
| Rui Ueyama | 6a60be7 | 2015-06-24 00:00:52 +0000 | [diff] [blame] | 149 | llvm::dbgs() << "Discarded " << Sym->getName() << "\n"; | 
| Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 150 | } | 
|  | 151 |  | 
|  | 152 | SectionRef SectionChunk::getSectionRef() { | 
|  | 153 | DataRefImpl Ref; | 
|  | 154 | Ref.p = uintptr_t(Header); | 
|  | 155 | return SectionRef(Ref, File->getCOFFObj()); | 
|  | 156 | } | 
|  | 157 |  | 
| Rui Ueyama | 6a60be7 | 2015-06-24 00:00:52 +0000 | [diff] [blame] | 158 | StringRef SectionChunk::getDebugName() { | 
|  | 159 | return Sym->getName(); | 
|  | 160 | } | 
|  | 161 |  | 
| Rui Ueyama | 9cf1abb | 2015-06-08 03:17:07 +0000 | [diff] [blame] | 162 | CommonChunk::CommonChunk(const COFFSymbolRef S) : Sym(S) { | 
| Rui Ueyama | 5e31d0b | 2015-06-20 07:25:45 +0000 | [diff] [blame] | 163 | // Common symbols are aligned on natural boundaries up to 32 bytes. | 
|  | 164 | // This is what MSVC link.exe does. | 
|  | 165 | Align = std::min(uint64_t(32), NextPowerOf2(Sym.getValue())); | 
| Rui Ueyama | 9cf1abb | 2015-06-08 03:17:07 +0000 | [diff] [blame] | 166 | } | 
|  | 167 |  | 
| Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 168 | uint32_t CommonChunk::getPermissions() const { | 
| Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 169 | return IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_READ | | 
|  | 170 | IMAGE_SCN_MEM_WRITE; | 
|  | 171 | } | 
|  | 172 |  | 
| Rui Ueyama | d6fefba4 | 2015-05-28 19:45:43 +0000 | [diff] [blame] | 173 | void StringChunk::writeTo(uint8_t *Buf) { | 
|  | 174 | memcpy(Buf + FileOff, Str.data(), Str.size()); | 
|  | 175 | } | 
|  | 176 |  | 
|  | 177 | void ImportThunkChunk::writeTo(uint8_t *Buf) { | 
|  | 178 | memcpy(Buf + FileOff, ImportThunkData, sizeof(ImportThunkData)); | 
| Rui Ueyama | 743afa0 | 2015-06-06 04:07:39 +0000 | [diff] [blame] | 179 | // The first two bytes is a JMP instruction. Fill its operand. | 
| Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 180 | uint32_t Operand = ImpSymbol->getRVA() - RVA - getSize(); | 
| Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 181 | write32le(Buf + FileOff + 2, Operand); | 
|  | 182 | } | 
|  | 183 |  | 
| Rui Ueyama | 588e832 | 2015-06-15 01:23:58 +0000 | [diff] [blame] | 184 | // Windows-specific. | 
|  | 185 | // This class represents a block in .reloc section. | 
|  | 186 | BaserelChunk::BaserelChunk(uint32_t Page, uint32_t *Begin, uint32_t *End) { | 
|  | 187 | // Block header consists of 4 byte page RVA and 4 byte block size. | 
|  | 188 | // Each entry is 2 byte. Last entry may be padding. | 
|  | 189 | Data.resize(RoundUpToAlignment((End - Begin) * 2 + 8, 4)); | 
|  | 190 | uint8_t *P = Data.data(); | 
|  | 191 | write32le(P, Page); | 
|  | 192 | write32le(P + 4, Data.size()); | 
|  | 193 | P += 8; | 
|  | 194 | for (uint32_t *I = Begin; I != End; ++I) { | 
|  | 195 | write16le(P, (IMAGE_REL_BASED_DIR64 << 12) | (*I - Page)); | 
|  | 196 | P += 2; | 
|  | 197 | } | 
|  | 198 | } | 
|  | 199 |  | 
|  | 200 | void BaserelChunk::writeTo(uint8_t *Buf) { | 
|  | 201 | memcpy(Buf + FileOff, Data.data(), Data.size()); | 
|  | 202 | } | 
|  | 203 |  | 
| Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 204 | } // namespace coff | 
|  | 205 | } // namespace lld |