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" |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 11 | #include "Error.h" |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 12 | #include "InputFiles.h" |
Rui Ueyama | 67fcd1a0 | 2015-08-05 19:51:28 +0000 | [diff] [blame] | 13 | #include "Symbols.h" |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 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 | cd3f99b | 2015-07-24 23:51:14 +0000 | [diff] [blame] | 25 | using llvm::support::ulittle32_t; |
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 | de88072 | 2015-09-25 16:20:24 +0000 | [diff] [blame] | 31 | : Chunk(SectionKind), Repl(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 | 4dbff20 | 2015-09-16 21:40:47 +0000 | [diff] [blame] | 41 | |
| 42 | // Only COMDAT sections are subject of dead-stripping. |
| 43 | Live = !isCOMDAT(); |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Rui Ueyama | 42aa00b | 2015-06-25 00:33:38 +0000 | [diff] [blame] | 46 | static void add16(uint8_t *P, int16_t V) { write16le(P, read16le(P) + V); } |
| 47 | static void add32(uint8_t *P, int32_t V) { write32le(P, read32le(P) + V); } |
| 48 | static void add64(uint8_t *P, int64_t V) { write64le(P, read64le(P) + V); } |
Rui Ueyama | 237fca1 | 2015-07-25 03:03:46 +0000 | [diff] [blame] | 49 | static void or16(uint8_t *P, uint16_t V) { write16le(P, read16le(P) | V); } |
Rui Ueyama | 42aa00b | 2015-06-25 00:33:38 +0000 | [diff] [blame] | 50 | |
Rui Ueyama | eb26e1d | 2015-07-29 16:30:45 +0000 | [diff] [blame] | 51 | void SectionChunk::applyRelX64(uint8_t *Off, uint16_t Type, Defined *Sym, |
Rui Ueyama | 63bbe84 | 2015-09-19 23:28:57 +0000 | [diff] [blame] | 52 | uint64_t P) const { |
Rui Ueyama | eb26e1d | 2015-07-29 16:30:45 +0000 | [diff] [blame] | 53 | uint64_t S = Sym->getRVA(); |
Rui Ueyama | 661a4e7a | 2015-07-07 22:49:21 +0000 | [diff] [blame] | 54 | switch (Type) { |
| 55 | case IMAGE_REL_AMD64_ADDR32: add32(Off, S + Config->ImageBase); break; |
| 56 | case IMAGE_REL_AMD64_ADDR64: add64(Off, S + Config->ImageBase); break; |
| 57 | case IMAGE_REL_AMD64_ADDR32NB: add32(Off, S); break; |
| 58 | case IMAGE_REL_AMD64_REL32: add32(Off, S - P - 4); break; |
| 59 | case IMAGE_REL_AMD64_REL32_1: add32(Off, S - P - 5); break; |
| 60 | case IMAGE_REL_AMD64_REL32_2: add32(Off, S - P - 6); break; |
| 61 | case IMAGE_REL_AMD64_REL32_3: add32(Off, S - P - 7); break; |
| 62 | case IMAGE_REL_AMD64_REL32_4: add32(Off, S - P - 8); break; |
| 63 | case IMAGE_REL_AMD64_REL32_5: add32(Off, S - P - 9); break; |
Rui Ueyama | eb26e1d | 2015-07-29 16:30:45 +0000 | [diff] [blame] | 64 | case IMAGE_REL_AMD64_SECTION: add16(Off, Sym->getSectionIndex()); break; |
| 65 | case IMAGE_REL_AMD64_SECREL: add32(Off, Sym->getSecrel()); break; |
Rui Ueyama | 661a4e7a | 2015-07-07 22:49:21 +0000 | [diff] [blame] | 66 | default: |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 67 | error("Unsupported relocation type"); |
Rui Ueyama | 661a4e7a | 2015-07-07 22:49:21 +0000 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
Rui Ueyama | eb26e1d | 2015-07-29 16:30:45 +0000 | [diff] [blame] | 71 | void SectionChunk::applyRelX86(uint8_t *Off, uint16_t Type, Defined *Sym, |
Rui Ueyama | 63bbe84 | 2015-09-19 23:28:57 +0000 | [diff] [blame] | 72 | uint64_t P) const { |
Rui Ueyama | eb26e1d | 2015-07-29 16:30:45 +0000 | [diff] [blame] | 73 | uint64_t S = Sym->getRVA(); |
Rui Ueyama | 11863b4a | 2015-07-08 01:45:29 +0000 | [diff] [blame] | 74 | switch (Type) { |
| 75 | case IMAGE_REL_I386_ABSOLUTE: break; |
| 76 | case IMAGE_REL_I386_DIR32: add32(Off, S + Config->ImageBase); break; |
| 77 | case IMAGE_REL_I386_DIR32NB: add32(Off, S); break; |
| 78 | case IMAGE_REL_I386_REL32: add32(Off, S - P - 4); break; |
Rui Ueyama | eb26e1d | 2015-07-29 16:30:45 +0000 | [diff] [blame] | 79 | case IMAGE_REL_I386_SECTION: add16(Off, Sym->getSectionIndex()); break; |
| 80 | case IMAGE_REL_I386_SECREL: add32(Off, Sym->getSecrel()); break; |
Rui Ueyama | 11863b4a | 2015-07-08 01:45:29 +0000 | [diff] [blame] | 81 | default: |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 82 | error("Unsupported relocation type"); |
Rui Ueyama | 11863b4a | 2015-07-08 01:45:29 +0000 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
Rui Ueyama | ba7c041 | 2015-08-05 19:40:07 +0000 | [diff] [blame] | 86 | static void applyMOV(uint8_t *Off, uint16_t V) { |
| 87 | or16(Off, ((V & 0x800) >> 1) | ((V >> 12) & 0xf)); |
| 88 | or16(Off + 2, ((V & 0x700) << 4) | (V & 0xff)); |
Rui Ueyama | 237fca1 | 2015-07-25 03:03:46 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Rui Ueyama | ba7c041 | 2015-08-05 19:40:07 +0000 | [diff] [blame] | 91 | static void applyMOV32T(uint8_t *Off, uint32_t V) { |
| 92 | applyMOV(Off, V); // set MOVW operand |
| 93 | applyMOV(Off + 4, V >> 16); // set MOVT operand |
| 94 | } |
| 95 | |
| 96 | static void applyBranch20T(uint8_t *Off, int32_t V) { |
| 97 | uint32_t S = V < 0 ? 1 : 0; |
| 98 | uint32_t J1 = (V >> 19) & 1; |
| 99 | uint32_t J2 = (V >> 18) & 1; |
| 100 | or16(Off, (S << 10) | ((V >> 12) & 0x3f)); |
| 101 | or16(Off + 2, (J1 << 13) | (J2 << 11) | ((V >> 1) & 0x7ff)); |
| 102 | } |
| 103 | |
| 104 | static void applyBranch24T(uint8_t *Off, int32_t V) { |
Rui Ueyama | 3d9c863 | 2015-07-25 03:19:34 +0000 | [diff] [blame] | 105 | uint32_t S = V < 0 ? 1 : 0; |
| 106 | uint32_t J1 = ((~V >> 23) & 1) ^ S; |
| 107 | uint32_t J2 = ((~V >> 22) & 1) ^ S; |
Rui Ueyama | ba7c041 | 2015-08-05 19:40:07 +0000 | [diff] [blame] | 108 | or16(Off, (S << 10) | ((V >> 12) & 0x3ff)); |
| 109 | or16(Off + 2, (J1 << 13) | (J2 << 11) | ((V >> 1) & 0x7ff)); |
Rui Ueyama | 3d9c863 | 2015-07-25 03:19:34 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Rui Ueyama | eb26e1d | 2015-07-29 16:30:45 +0000 | [diff] [blame] | 112 | void SectionChunk::applyRelARM(uint8_t *Off, uint16_t Type, Defined *Sym, |
Rui Ueyama | 63bbe84 | 2015-09-19 23:28:57 +0000 | [diff] [blame] | 113 | uint64_t P) const { |
Rui Ueyama | eb26e1d | 2015-07-29 16:30:45 +0000 | [diff] [blame] | 114 | uint64_t S = Sym->getRVA(); |
Rui Ueyama | 8bc43a1 | 2015-07-29 19:25:00 +0000 | [diff] [blame] | 115 | // Pointer to thumb code must have the LSB set. |
| 116 | if (Sym->isExecutable()) |
| 117 | S |= 1; |
Rui Ueyama | 237fca1 | 2015-07-25 03:03:46 +0000 | [diff] [blame] | 118 | switch (Type) { |
Rui Ueyama | 3d9c863 | 2015-07-25 03:19:34 +0000 | [diff] [blame] | 119 | case IMAGE_REL_ARM_ADDR32: add32(Off, S + Config->ImageBase); break; |
| 120 | case IMAGE_REL_ARM_ADDR32NB: add32(Off, S); break; |
| 121 | case IMAGE_REL_ARM_MOV32T: applyMOV32T(Off, S + Config->ImageBase); break; |
Rui Ueyama | ba7c041 | 2015-08-05 19:40:07 +0000 | [diff] [blame] | 122 | case IMAGE_REL_ARM_BRANCH20T: applyBranch20T(Off, S - P - 4); break; |
| 123 | case IMAGE_REL_ARM_BRANCH24T: applyBranch24T(Off, S - P - 4); break; |
| 124 | case IMAGE_REL_ARM_BLX23T: applyBranch24T(Off, S - P - 4); break; |
Rui Ueyama | 237fca1 | 2015-07-25 03:03:46 +0000 | [diff] [blame] | 125 | default: |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 126 | error("Unsupported relocation type"); |
Rui Ueyama | 237fca1 | 2015-07-25 03:03:46 +0000 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
Rui Ueyama | 63bbe84 | 2015-09-19 23:28:57 +0000 | [diff] [blame] | 130 | void SectionChunk::writeTo(uint8_t *Buf) const { |
Rui Ueyama | 9aefa0c | 2015-05-28 20:04:51 +0000 | [diff] [blame] | 131 | if (!hasData()) |
| 132 | return; |
Rui Ueyama | 743afa0 | 2015-06-06 04:07:39 +0000 | [diff] [blame] | 133 | // Copy section contents from source object file to output file. |
Rui Ueyama | f34c088 | 2015-06-25 17:56:36 +0000 | [diff] [blame] | 134 | ArrayRef<uint8_t> A = getContents(); |
Rafael Espindola | 5c546a1 | 2015-08-14 03:30:59 +0000 | [diff] [blame] | 135 | memcpy(Buf + OutputSectionOff, A.data(), A.size()); |
Rui Ueyama | 743afa0 | 2015-06-06 04:07:39 +0000 | [diff] [blame] | 136 | |
| 137 | // Apply relocations. |
Rui Ueyama | 42aa00b | 2015-06-25 00:33:38 +0000 | [diff] [blame] | 138 | for (const coff_relocation &Rel : Relocs) { |
Rafael Espindola | 5c546a1 | 2015-08-14 03:30:59 +0000 | [diff] [blame] | 139 | uint8_t *Off = Buf + OutputSectionOff + Rel.VirtualAddress; |
Rui Ueyama | 0744e87 | 2015-07-02 00:21:11 +0000 | [diff] [blame] | 140 | SymbolBody *Body = File->getSymbolBody(Rel.SymbolTableIndex)->repl(); |
Rui Ueyama | eb26e1d | 2015-07-29 16:30:45 +0000 | [diff] [blame] | 141 | Defined *Sym = cast<Defined>(Body); |
Rui Ueyama | 42aa00b | 2015-06-25 00:33:38 +0000 | [diff] [blame] | 142 | uint64_t P = RVA + Rel.VirtualAddress; |
Rui Ueyama | 5e706b3 | 2015-07-25 21:54:50 +0000 | [diff] [blame] | 143 | switch (Config->Machine) { |
| 144 | case AMD64: |
Rui Ueyama | eb26e1d | 2015-07-29 16:30:45 +0000 | [diff] [blame] | 145 | applyRelX64(Off, Rel.Type, Sym, P); |
Rui Ueyama | 11863b4a | 2015-07-08 01:45:29 +0000 | [diff] [blame] | 146 | break; |
Rui Ueyama | 5e706b3 | 2015-07-25 21:54:50 +0000 | [diff] [blame] | 147 | case I386: |
Rui Ueyama | eb26e1d | 2015-07-29 16:30:45 +0000 | [diff] [blame] | 148 | applyRelX86(Off, Rel.Type, Sym, P); |
Rui Ueyama | 11863b4a | 2015-07-08 01:45:29 +0000 | [diff] [blame] | 149 | break; |
Rui Ueyama | 5e706b3 | 2015-07-25 21:54:50 +0000 | [diff] [blame] | 150 | case ARMNT: |
Rui Ueyama | eb26e1d | 2015-07-29 16:30:45 +0000 | [diff] [blame] | 151 | applyRelARM(Off, Rel.Type, Sym, P); |
Rui Ueyama | 237fca1 | 2015-07-25 03:03:46 +0000 | [diff] [blame] | 152 | break; |
Rui Ueyama | 11863b4a | 2015-07-08 01:45:29 +0000 | [diff] [blame] | 153 | default: |
| 154 | llvm_unreachable("unknown machine type"); |
| 155 | } |
Rui Ueyama | 42aa00b | 2015-06-25 00:33:38 +0000 | [diff] [blame] | 156 | } |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 159 | void SectionChunk::addAssociative(SectionChunk *Child) { |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 160 | AssocChildren.push_back(Child); |
| 161 | } |
| 162 | |
Rui Ueyama | 3afd5bf | 2015-07-25 01:44:32 +0000 | [diff] [blame] | 163 | static uint8_t getBaserelType(const coff_relocation &Rel) { |
Rui Ueyama | 5e706b3 | 2015-07-25 21:54:50 +0000 | [diff] [blame] | 164 | switch (Config->Machine) { |
| 165 | case AMD64: |
Rui Ueyama | 3afd5bf | 2015-07-25 01:44:32 +0000 | [diff] [blame] | 166 | if (Rel.Type == IMAGE_REL_AMD64_ADDR64) |
| 167 | return IMAGE_REL_BASED_DIR64; |
| 168 | return IMAGE_REL_BASED_ABSOLUTE; |
Rui Ueyama | 5e706b3 | 2015-07-25 21:54:50 +0000 | [diff] [blame] | 169 | case I386: |
Rui Ueyama | 3afd5bf | 2015-07-25 01:44:32 +0000 | [diff] [blame] | 170 | if (Rel.Type == IMAGE_REL_I386_DIR32) |
| 171 | return IMAGE_REL_BASED_HIGHLOW; |
| 172 | return IMAGE_REL_BASED_ABSOLUTE; |
Rui Ueyama | 5e706b3 | 2015-07-25 21:54:50 +0000 | [diff] [blame] | 173 | case ARMNT: |
Rui Ueyama | 237fca1 | 2015-07-25 03:03:46 +0000 | [diff] [blame] | 174 | if (Rel.Type == IMAGE_REL_ARM_ADDR32) |
| 175 | return IMAGE_REL_BASED_HIGHLOW; |
| 176 | if (Rel.Type == IMAGE_REL_ARM_MOV32T) |
| 177 | return IMAGE_REL_BASED_ARM_MOV32T; |
| 178 | return IMAGE_REL_BASED_ABSOLUTE; |
Rui Ueyama | 93b4571 | 2015-07-09 20:36:59 +0000 | [diff] [blame] | 179 | default: |
| 180 | llvm_unreachable("unknown machine type"); |
| 181 | } |
| 182 | } |
| 183 | |
Rui Ueyama | 588e832 | 2015-06-15 01:23:58 +0000 | [diff] [blame] | 184 | // Windows-specific. |
Rui Ueyama | 93b4571 | 2015-07-09 20:36:59 +0000 | [diff] [blame] | 185 | // Collect all locations that contain absolute addresses, which need to be |
| 186 | // fixed by the loader if load-time relocation is needed. |
Rui Ueyama | 588e832 | 2015-06-15 01:23:58 +0000 | [diff] [blame] | 187 | // Only called when base relocation is enabled. |
Rui Ueyama | 3afd5bf | 2015-07-25 01:44:32 +0000 | [diff] [blame] | 188 | void SectionChunk::getBaserels(std::vector<Baserel> *Res) { |
Rui Ueyama | 42aa00b | 2015-06-25 00:33:38 +0000 | [diff] [blame] | 189 | for (const coff_relocation &Rel : Relocs) { |
Rui Ueyama | 3afd5bf | 2015-07-25 01:44:32 +0000 | [diff] [blame] | 190 | uint8_t Ty = getBaserelType(Rel); |
| 191 | if (Ty == IMAGE_REL_BASED_ABSOLUTE) |
Rui Ueyama | 588e832 | 2015-06-15 01:23:58 +0000 | [diff] [blame] | 192 | continue; |
Rui Ueyama | 0744e87 | 2015-07-02 00:21:11 +0000 | [diff] [blame] | 193 | SymbolBody *Body = File->getSymbolBody(Rel.SymbolTableIndex)->repl(); |
Rui Ueyama | 3cb895c | 2015-07-24 22:58:44 +0000 | [diff] [blame] | 194 | if (isa<DefinedAbsolute>(Body)) |
Rui Ueyama | 588e832 | 2015-06-15 01:23:58 +0000 | [diff] [blame] | 195 | continue; |
Rui Ueyama | 3afd5bf | 2015-07-25 01:44:32 +0000 | [diff] [blame] | 196 | Res->emplace_back(RVA + Rel.VirtualAddress, Ty); |
Rui Ueyama | 588e832 | 2015-06-15 01:23:58 +0000 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 200 | bool SectionChunk::hasData() const { |
| 201 | return !(Header->Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA); |
| 202 | } |
| 203 | |
| 204 | uint32_t SectionChunk::getPermissions() const { |
| 205 | return Header->Characteristics & PermMask; |
| 206 | } |
| 207 | |
| 208 | bool SectionChunk::isCOMDAT() const { |
| 209 | return Header->Characteristics & IMAGE_SCN_LNK_COMDAT; |
| 210 | } |
| 211 | |
Rui Ueyama | fc510f4 | 2015-06-25 19:10:58 +0000 | [diff] [blame] | 212 | void SectionChunk::printDiscardedMessage() const { |
Rui Ueyama | 4bce7bc | 2015-09-16 21:30:40 +0000 | [diff] [blame] | 213 | // Removed by dead-stripping. If it's removed by ICF, ICF already |
| 214 | // printed out the name, so don't repeat that here. |
Rui Ueyama | de88072 | 2015-09-25 16:20:24 +0000 | [diff] [blame] | 215 | if (Sym && this == Repl) |
Rui Ueyama | 4bce7bc | 2015-09-16 21:30:40 +0000 | [diff] [blame] | 216 | llvm::outs() << "Discarded " << Sym->getName() << "\n"; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Rui Ueyama | 6a60be7 | 2015-06-24 00:00:52 +0000 | [diff] [blame] | 219 | StringRef SectionChunk::getDebugName() { |
Rui Ueyama | 6a883b0 | 2015-08-21 07:01:10 +0000 | [diff] [blame] | 220 | if (Sym) |
| 221 | return Sym->getName(); |
| 222 | return ""; |
Rui Ueyama | 6a60be7 | 2015-06-24 00:00:52 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Rui Ueyama | f34c088 | 2015-06-25 17:56:36 +0000 | [diff] [blame] | 225 | ArrayRef<uint8_t> SectionChunk::getContents() const { |
| 226 | ArrayRef<uint8_t> A; |
| 227 | File->getCOFFObj()->getSectionContents(Header, A); |
| 228 | return A; |
| 229 | } |
| 230 | |
Rui Ueyama | 3cb1f5c | 2015-09-21 19:36:51 +0000 | [diff] [blame] | 231 | void SectionChunk::replace(SectionChunk *Other) { |
Rui Ueyama | de88072 | 2015-09-25 16:20:24 +0000 | [diff] [blame] | 232 | Other->Repl = Repl; |
Rui Ueyama | 3cb1f5c | 2015-09-21 19:36:51 +0000 | [diff] [blame] | 233 | Other->Live = false; |
Rui Ueyama | ddf71fc | 2015-06-24 04:36:52 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Rui Ueyama | 9cf1abb | 2015-06-08 03:17:07 +0000 | [diff] [blame] | 236 | CommonChunk::CommonChunk(const COFFSymbolRef S) : Sym(S) { |
Rui Ueyama | 5e31d0b | 2015-06-20 07:25:45 +0000 | [diff] [blame] | 237 | // Common symbols are aligned on natural boundaries up to 32 bytes. |
| 238 | // This is what MSVC link.exe does. |
| 239 | Align = std::min(uint64_t(32), NextPowerOf2(Sym.getValue())); |
Rui Ueyama | 9cf1abb | 2015-06-08 03:17:07 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 242 | uint32_t CommonChunk::getPermissions() const { |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 243 | return IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_READ | |
| 244 | IMAGE_SCN_MEM_WRITE; |
| 245 | } |
| 246 | |
Rui Ueyama | 63bbe84 | 2015-09-19 23:28:57 +0000 | [diff] [blame] | 247 | void StringChunk::writeTo(uint8_t *Buf) const { |
Rafael Espindola | 5c546a1 | 2015-08-14 03:30:59 +0000 | [diff] [blame] | 248 | memcpy(Buf + OutputSectionOff, Str.data(), Str.size()); |
Rui Ueyama | d6fefba4 | 2015-05-28 19:45:43 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Rui Ueyama | 28df042 | 2015-07-25 01:16:06 +0000 | [diff] [blame] | 251 | ImportThunkChunkX64::ImportThunkChunkX64(Defined *S) : ImpSymbol(S) { |
Rui Ueyama | 7383562 | 2015-06-26 18:28:56 +0000 | [diff] [blame] | 252 | // Intel Optimization Manual says that all branch targets |
| 253 | // should be 16-byte aligned. MSVC linker does this too. |
Rui Ueyama | 5e706b3 | 2015-07-25 21:54:50 +0000 | [diff] [blame] | 254 | Align = 16; |
Rui Ueyama | 7383562 | 2015-06-26 18:28:56 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Rui Ueyama | 63bbe84 | 2015-09-19 23:28:57 +0000 | [diff] [blame] | 257 | void ImportThunkChunkX64::writeTo(uint8_t *Buf) const { |
Rafael Espindola | 5c546a1 | 2015-08-14 03:30:59 +0000 | [diff] [blame] | 258 | memcpy(Buf + OutputSectionOff, ImportThunkX86, sizeof(ImportThunkX86)); |
Rui Ueyama | 28df042 | 2015-07-25 01:16:06 +0000 | [diff] [blame] | 259 | // The first two bytes is a JMP instruction. Fill its operand. |
Rafael Espindola | 5c546a1 | 2015-08-14 03:30:59 +0000 | [diff] [blame] | 260 | write32le(Buf + OutputSectionOff + 2, ImpSymbol->getRVA() - RVA - getSize()); |
Rui Ueyama | 33fb2cb | 2015-07-15 00:25:38 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Rui Ueyama | 3afd5bf | 2015-07-25 01:44:32 +0000 | [diff] [blame] | 263 | void ImportThunkChunkX86::getBaserels(std::vector<Baserel> *Res) { |
| 264 | Res->emplace_back(getRVA() + 2); |
Rui Ueyama | 28df042 | 2015-07-25 01:16:06 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Rui Ueyama | 63bbe84 | 2015-09-19 23:28:57 +0000 | [diff] [blame] | 267 | void ImportThunkChunkX86::writeTo(uint8_t *Buf) const { |
Rafael Espindola | 5c546a1 | 2015-08-14 03:30:59 +0000 | [diff] [blame] | 268 | memcpy(Buf + OutputSectionOff, ImportThunkX86, sizeof(ImportThunkX86)); |
Rui Ueyama | 743afa0 | 2015-06-06 04:07:39 +0000 | [diff] [blame] | 269 | // The first two bytes is a JMP instruction. Fill its operand. |
Rafael Espindola | 5c546a1 | 2015-08-14 03:30:59 +0000 | [diff] [blame] | 270 | write32le(Buf + OutputSectionOff + 2, |
| 271 | ImpSymbol->getRVA() + Config->ImageBase); |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Rui Ueyama | 3dd9372 | 2015-07-25 03:39:29 +0000 | [diff] [blame] | 274 | void ImportThunkChunkARM::getBaserels(std::vector<Baserel> *Res) { |
| 275 | Res->emplace_back(getRVA(), IMAGE_REL_BASED_ARM_MOV32T); |
| 276 | } |
| 277 | |
Rui Ueyama | 63bbe84 | 2015-09-19 23:28:57 +0000 | [diff] [blame] | 278 | void ImportThunkChunkARM::writeTo(uint8_t *Buf) const { |
Rafael Espindola | 5c546a1 | 2015-08-14 03:30:59 +0000 | [diff] [blame] | 279 | memcpy(Buf + OutputSectionOff, ImportThunkARM, sizeof(ImportThunkARM)); |
Rui Ueyama | 3dd9372 | 2015-07-25 03:39:29 +0000 | [diff] [blame] | 280 | // Fix mov.w and mov.t operands. |
Rafael Espindola | 5c546a1 | 2015-08-14 03:30:59 +0000 | [diff] [blame] | 281 | applyMOV32T(Buf + OutputSectionOff, ImpSymbol->getRVA() + Config->ImageBase); |
Rui Ueyama | 3dd9372 | 2015-07-25 03:39:29 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Rui Ueyama | 3afd5bf | 2015-07-25 01:44:32 +0000 | [diff] [blame] | 284 | void LocalImportChunk::getBaserels(std::vector<Baserel> *Res) { |
| 285 | Res->emplace_back(getRVA()); |
Rui Ueyama | 7a333c6 | 2015-07-02 20:33:50 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Rui Ueyama | d4b351f | 2015-07-09 21:15:58 +0000 | [diff] [blame] | 288 | size_t LocalImportChunk::getSize() const { |
| 289 | return Config->is64() ? 8 : 4; |
| 290 | } |
| 291 | |
Rui Ueyama | 63bbe84 | 2015-09-19 23:28:57 +0000 | [diff] [blame] | 292 | void LocalImportChunk::writeTo(uint8_t *Buf) const { |
Rui Ueyama | d4b351f | 2015-07-09 21:15:58 +0000 | [diff] [blame] | 293 | if (Config->is64()) { |
Rafael Espindola | 5c546a1 | 2015-08-14 03:30:59 +0000 | [diff] [blame] | 294 | write64le(Buf + OutputSectionOff, Sym->getRVA() + Config->ImageBase); |
Rui Ueyama | d4b351f | 2015-07-09 21:15:58 +0000 | [diff] [blame] | 295 | } else { |
Rafael Espindola | 5c546a1 | 2015-08-14 03:30:59 +0000 | [diff] [blame] | 296 | write32le(Buf + OutputSectionOff, Sym->getRVA() + Config->ImageBase); |
Rui Ueyama | d4b351f | 2015-07-09 21:15:58 +0000 | [diff] [blame] | 297 | } |
Rui Ueyama | 7a333c6 | 2015-07-02 20:33:50 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Rui Ueyama | 63bbe84 | 2015-09-19 23:28:57 +0000 | [diff] [blame] | 300 | void SEHTableChunk::writeTo(uint8_t *Buf) const { |
Rafael Espindola | 5c546a1 | 2015-08-14 03:30:59 +0000 | [diff] [blame] | 301 | ulittle32_t *Begin = reinterpret_cast<ulittle32_t *>(Buf + OutputSectionOff); |
Rui Ueyama | cd3f99b | 2015-07-24 23:51:14 +0000 | [diff] [blame] | 302 | size_t Cnt = 0; |
| 303 | for (Defined *D : Syms) |
| 304 | Begin[Cnt++] = D->getRVA(); |
| 305 | std::sort(Begin, Begin + Cnt); |
| 306 | } |
| 307 | |
Rui Ueyama | 588e832 | 2015-06-15 01:23:58 +0000 | [diff] [blame] | 308 | // Windows-specific. |
| 309 | // This class represents a block in .reloc section. |
Rui Ueyama | 3afd5bf | 2015-07-25 01:44:32 +0000 | [diff] [blame] | 310 | BaserelChunk::BaserelChunk(uint32_t Page, Baserel *Begin, Baserel *End) { |
Rui Ueyama | 588e832 | 2015-06-15 01:23:58 +0000 | [diff] [blame] | 311 | // Block header consists of 4 byte page RVA and 4 byte block size. |
| 312 | // Each entry is 2 byte. Last entry may be padding. |
| 313 | Data.resize(RoundUpToAlignment((End - Begin) * 2 + 8, 4)); |
| 314 | uint8_t *P = Data.data(); |
| 315 | write32le(P, Page); |
| 316 | write32le(P + 4, Data.size()); |
| 317 | P += 8; |
Rui Ueyama | 3afd5bf | 2015-07-25 01:44:32 +0000 | [diff] [blame] | 318 | for (Baserel *I = Begin; I != End; ++I) { |
| 319 | write16le(P, (I->Type << 12) | (I->RVA - Page)); |
Rui Ueyama | 588e832 | 2015-06-15 01:23:58 +0000 | [diff] [blame] | 320 | P += 2; |
| 321 | } |
| 322 | } |
| 323 | |
Rui Ueyama | 63bbe84 | 2015-09-19 23:28:57 +0000 | [diff] [blame] | 324 | void BaserelChunk::writeTo(uint8_t *Buf) const { |
Rafael Espindola | 5c546a1 | 2015-08-14 03:30:59 +0000 | [diff] [blame] | 325 | memcpy(Buf + OutputSectionOff, Data.data(), Data.size()); |
Rui Ueyama | 588e832 | 2015-06-15 01:23:58 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Rui Ueyama | 3afd5bf | 2015-07-25 01:44:32 +0000 | [diff] [blame] | 328 | uint8_t Baserel::getDefaultType() { |
Rui Ueyama | 5e706b3 | 2015-07-25 21:54:50 +0000 | [diff] [blame] | 329 | switch (Config->Machine) { |
| 330 | case AMD64: |
Rui Ueyama | 3afd5bf | 2015-07-25 01:44:32 +0000 | [diff] [blame] | 331 | return IMAGE_REL_BASED_DIR64; |
Rui Ueyama | 5e706b3 | 2015-07-25 21:54:50 +0000 | [diff] [blame] | 332 | case I386: |
Rui Ueyama | 3afd5bf | 2015-07-25 01:44:32 +0000 | [diff] [blame] | 333 | return IMAGE_REL_BASED_HIGHLOW; |
| 334 | default: |
| 335 | llvm_unreachable("unknown machine type"); |
| 336 | } |
| 337 | } |
| 338 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 339 | } // namespace coff |
| 340 | } // namespace lld |