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" |
| 13 | #include "lld/Core/Error.h" |
| 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" |
| 20 | |
| 21 | using namespace llvm::object; |
| 22 | using namespace llvm::support::endian; |
| 23 | using namespace llvm::COFF; |
| 24 | using llvm::RoundUpToAlignment; |
| 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); |
| 33 | // Bit [20:24] contains section alignment. |
| 34 | unsigned Shift = ((Header->Characteristics & 0xF00000) >> 20) - 1; |
| 35 | Align = uint32_t(1) << Shift; |
| 36 | } |
| 37 | |
Rui Ueyama | d6fefba4 | 2015-05-28 19:45:43 +0000 | [diff] [blame] | 38 | void SectionChunk::writeTo(uint8_t *Buf) { |
Rui Ueyama | 9aefa0c | 2015-05-28 20:04:51 +0000 | [diff] [blame^] | 39 | if (!hasData()) |
| 40 | return; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 41 | ArrayRef<uint8_t> Data; |
| 42 | File->getCOFFObj()->getSectionContents(Header, Data); |
Rui Ueyama | d6fefba4 | 2015-05-28 19:45:43 +0000 | [diff] [blame] | 43 | memcpy(Buf + FileOff, Data.data(), Data.size()); |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | // Returns true if this chunk should be considered as a GC root. |
| 47 | bool SectionChunk::isRoot() { |
| 48 | // COMDAT sections are live only when they are referenced by something else. |
| 49 | if (isCOMDAT()) |
| 50 | return false; |
| 51 | |
| 52 | // Associative sections are live if their parent COMDATs are live, |
| 53 | // and vice versa, so they are not considered live by themselves. |
| 54 | if (IsAssocChild) |
| 55 | return false; |
| 56 | |
| 57 | // Only code is subject of dead-stripping. |
| 58 | return !(Header->Characteristics & IMAGE_SCN_CNT_CODE); |
| 59 | } |
| 60 | |
| 61 | void SectionChunk::markLive() { |
| 62 | if (Live) |
| 63 | return; |
| 64 | Live = true; |
| 65 | |
| 66 | // Mark all symbols listed in the relocation table for this section. |
| 67 | for (const auto &I : getSectionRef().relocations()) { |
| 68 | const coff_relocation *Rel = File->getCOFFObj()->getCOFFRelocation(I); |
| 69 | SymbolBody *B = File->getSymbolBody(Rel->SymbolTableIndex); |
| 70 | if (auto *Def = dyn_cast<Defined>(B)) |
| 71 | Def->markLive(); |
| 72 | } |
| 73 | |
| 74 | // Mark associative sections if any. |
| 75 | for (Chunk *C : AssocChildren) |
| 76 | C->markLive(); |
| 77 | } |
| 78 | |
| 79 | void SectionChunk::addAssociative(SectionChunk *Child) { |
| 80 | Child->IsAssocChild = true; |
| 81 | AssocChildren.push_back(Child); |
| 82 | } |
| 83 | |
| 84 | void SectionChunk::applyRelocations(uint8_t *Buf) { |
| 85 | for (const auto &I : getSectionRef().relocations()) { |
| 86 | const coff_relocation *Rel = File->getCOFFObj()->getCOFFRelocation(I); |
| 87 | applyReloc(Buf, Rel); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | static void add16(uint8_t *P, int32_t V) { write16le(P, read16le(P) + V); } |
| 92 | static void add32(uint8_t *P, int32_t V) { write32le(P, read32le(P) + V); } |
| 93 | static void add64(uint8_t *P, int64_t V) { write64le(P, read64le(P) + V); } |
| 94 | |
| 95 | // Implements x64 PE/COFF relocations. |
| 96 | void SectionChunk::applyReloc(uint8_t *Buf, const coff_relocation *Rel) { |
| 97 | using namespace llvm::COFF; |
| 98 | uint8_t *Off = Buf + FileOff + Rel->VirtualAddress; |
| 99 | SymbolBody *Body = File->getSymbolBody(Rel->SymbolTableIndex); |
| 100 | uint64_t S = cast<Defined>(Body)->getRVA(); |
| 101 | uint64_t P = RVA + Rel->VirtualAddress; |
| 102 | switch (Rel->Type) { |
| 103 | case IMAGE_REL_AMD64_ADDR32: add32(Off, S + Config->ImageBase); break; |
| 104 | case IMAGE_REL_AMD64_ADDR64: add64(Off, S + Config->ImageBase); break; |
| 105 | case IMAGE_REL_AMD64_ADDR32NB: add32(Off, S); break; |
| 106 | case IMAGE_REL_AMD64_REL32: add32(Off, S - P - 4); break; |
| 107 | case IMAGE_REL_AMD64_REL32_1: add32(Off, S - P - 5); break; |
| 108 | case IMAGE_REL_AMD64_REL32_2: add32(Off, S - P - 6); break; |
| 109 | case IMAGE_REL_AMD64_REL32_3: add32(Off, S - P - 7); break; |
| 110 | case IMAGE_REL_AMD64_REL32_4: add32(Off, S - P - 8); break; |
| 111 | case IMAGE_REL_AMD64_REL32_5: add32(Off, S - P - 9); break; |
| 112 | case IMAGE_REL_AMD64_SECTION: add16(Off, Out->getSectionIndex()); break; |
| 113 | case IMAGE_REL_AMD64_SECREL: add32(Off, S - Out->getRVA()); break; |
| 114 | default: |
| 115 | llvm::report_fatal_error("Unsupported relocation type"); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | bool SectionChunk::hasData() const { |
| 120 | return !(Header->Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA); |
| 121 | } |
| 122 | |
| 123 | uint32_t SectionChunk::getPermissions() const { |
| 124 | return Header->Characteristics & PermMask; |
| 125 | } |
| 126 | |
| 127 | bool SectionChunk::isCOMDAT() const { |
| 128 | return Header->Characteristics & IMAGE_SCN_LNK_COMDAT; |
| 129 | } |
| 130 | |
| 131 | // Prints "Discarded <symbol>" for all external function symbols. |
| 132 | void SectionChunk::printDiscardedMessage() { |
| 133 | uint32_t E = File->getCOFFObj()->getNumberOfSymbols(); |
| 134 | for (uint32_t I = 0; I < E; ++I) { |
| 135 | auto SrefOrErr = File->getCOFFObj()->getSymbol(I); |
| 136 | COFFSymbolRef Sym = SrefOrErr.get(); |
| 137 | if (uint32_t(Sym.getSectionNumber()) != SectionIndex) |
| 138 | continue; |
| 139 | if (!Sym.isFunctionDefinition()) |
| 140 | continue; |
| 141 | StringRef SymbolName; |
| 142 | File->getCOFFObj()->getSymbolName(Sym, SymbolName); |
| 143 | llvm::dbgs() << "Discarded " << SymbolName << " from " |
| 144 | << File->getShortName() << "\n"; |
| 145 | I += Sym.getNumberOfAuxSymbols(); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | SectionRef SectionChunk::getSectionRef() { |
| 150 | DataRefImpl Ref; |
| 151 | Ref.p = uintptr_t(Header); |
| 152 | return SectionRef(Ref, File->getCOFFObj()); |
| 153 | } |
| 154 | |
| 155 | uint32_t CommonChunk::getPermissions() const { |
| 156 | using namespace llvm::COFF; |
| 157 | return IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_READ | |
| 158 | IMAGE_SCN_MEM_WRITE; |
| 159 | } |
| 160 | |
Rui Ueyama | d6fefba4 | 2015-05-28 19:45:43 +0000 | [diff] [blame] | 161 | void StringChunk::writeTo(uint8_t *Buf) { |
| 162 | memcpy(Buf + FileOff, Str.data(), Str.size()); |
| 163 | } |
| 164 | |
| 165 | void ImportThunkChunk::writeTo(uint8_t *Buf) { |
| 166 | memcpy(Buf + FileOff, ImportThunkData, sizeof(ImportThunkData)); |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | void ImportThunkChunk::applyRelocations(uint8_t *Buf) { |
| 170 | uint32_t Operand = ImpSymbol->getRVA() - RVA - getSize(); |
| 171 | // The first two bytes are a JMP instruction. Fill its operand. |
| 172 | write32le(Buf + FileOff + 2, Operand); |
| 173 | } |
| 174 | |
Rui Ueyama | d6fefba4 | 2015-05-28 19:45:43 +0000 | [diff] [blame] | 175 | HintNameChunk::HintNameChunk(StringRef N) |
| 176 | : Name(N), Size(RoundUpToAlignment(Name.size() + 4, 2)) {} |
| 177 | |
| 178 | void HintNameChunk::writeTo(uint8_t *Buf) { |
| 179 | // The first two bytes is Hint/Name field. |
| 180 | memcpy(Buf + FileOff + 2, Name.data(), Name.size()); |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | void LookupChunk::applyRelocations(uint8_t *Buf) { |
| 184 | write32le(Buf + FileOff, HintName->getRVA()); |
| 185 | } |
| 186 | |
| 187 | void DirectoryChunk::applyRelocations(uint8_t *Buf) { |
| 188 | auto *E = (coff_import_directory_table_entry *)(Buf + FileOff); |
| 189 | E->ImportLookupTableRVA = LookupTab->getRVA(); |
| 190 | E->NameRVA = DLLName->getRVA(); |
| 191 | E->ImportAddressTableRVA = AddressTab->getRVA(); |
| 192 | } |
| 193 | |
| 194 | ImportTable::ImportTable(StringRef N, |
| 195 | std::vector<DefinedImportData *> &Symbols) { |
| 196 | DLLName = new StringChunk(N); |
| 197 | DirTab = new DirectoryChunk(DLLName); |
| 198 | for (DefinedImportData *S : Symbols) |
| 199 | HintNameTables.push_back(new HintNameChunk(S->getExportName())); |
| 200 | |
| 201 | for (HintNameChunk *H : HintNameTables) { |
| 202 | LookupTables.push_back(new LookupChunk(H)); |
| 203 | AddressTables.push_back(new LookupChunk(H)); |
| 204 | } |
| 205 | |
| 206 | for (int I = 0, E = Symbols.size(); I < E; ++I) |
| 207 | Symbols[I]->setLocation(AddressTables[I]); |
| 208 | |
| 209 | DirTab->LookupTab = LookupTables[0]; |
| 210 | DirTab->AddressTab = AddressTables[0]; |
| 211 | } |
| 212 | |
| 213 | } // namespace coff |
| 214 | } // namespace lld |