File: | include/llvm/Object/COFF.h |
Warning: | line 317, column 38 Called C++ object pointer is null |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===- COFFObjectFile.cpp - COFF object file implementation ---------------===// | |||
2 | // | |||
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | |||
4 | // See https://llvm.org/LICENSE.txt for license information. | |||
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | |||
6 | // | |||
7 | //===----------------------------------------------------------------------===// | |||
8 | // | |||
9 | // This file declares the COFFObjectFile class. | |||
10 | // | |||
11 | //===----------------------------------------------------------------------===// | |||
12 | ||||
13 | #include "llvm/ADT/ArrayRef.h" | |||
14 | #include "llvm/ADT/StringRef.h" | |||
15 | #include "llvm/ADT/Triple.h" | |||
16 | #include "llvm/ADT/iterator_range.h" | |||
17 | #include "llvm/BinaryFormat/COFF.h" | |||
18 | #include "llvm/Object/Binary.h" | |||
19 | #include "llvm/Object/COFF.h" | |||
20 | #include "llvm/Object/Error.h" | |||
21 | #include "llvm/Object/ObjectFile.h" | |||
22 | #include "llvm/Support/BinaryStreamReader.h" | |||
23 | #include "llvm/Support/Endian.h" | |||
24 | #include "llvm/Support/Error.h" | |||
25 | #include "llvm/Support/ErrorHandling.h" | |||
26 | #include "llvm/Support/MathExtras.h" | |||
27 | #include "llvm/Support/MemoryBuffer.h" | |||
28 | #include <algorithm> | |||
29 | #include <cassert> | |||
30 | #include <cstddef> | |||
31 | #include <cstdint> | |||
32 | #include <cstring> | |||
33 | #include <limits> | |||
34 | #include <memory> | |||
35 | #include <system_error> | |||
36 | ||||
37 | using namespace llvm; | |||
38 | using namespace object; | |||
39 | ||||
40 | using support::ulittle16_t; | |||
41 | using support::ulittle32_t; | |||
42 | using support::ulittle64_t; | |||
43 | using support::little16_t; | |||
44 | ||||
45 | // Returns false if size is greater than the buffer size. And sets ec. | |||
46 | static bool checkSize(MemoryBufferRef M, std::error_code &EC, uint64_t Size) { | |||
47 | if (M.getBufferSize() < Size) { | |||
48 | EC = object_error::unexpected_eof; | |||
49 | return false; | |||
50 | } | |||
51 | return true; | |||
52 | } | |||
53 | ||||
54 | // Sets Obj unless any bytes in [addr, addr + size) fall outsize of m. | |||
55 | // Returns unexpected_eof if error. | |||
56 | template <typename T> | |||
57 | static std::error_code getObject(const T *&Obj, MemoryBufferRef M, | |||
58 | const void *Ptr, | |||
59 | const uint64_t Size = sizeof(T)) { | |||
60 | uintptr_t Addr = uintptr_t(Ptr); | |||
61 | if (std::error_code EC = Binary::checkOffset(M, Addr, Size)) | |||
62 | return EC; | |||
63 | Obj = reinterpret_cast<const T *>(Addr); | |||
64 | return std::error_code(); | |||
65 | } | |||
66 | ||||
67 | // Decode a string table entry in base 64 (//AAAAAA). Expects \arg Str without | |||
68 | // prefixed slashes. | |||
69 | static bool decodeBase64StringEntry(StringRef Str, uint32_t &Result) { | |||
70 | assert(Str.size() <= 6 && "String too long, possible overflow.")((Str.size() <= 6 && "String too long, possible overflow." ) ? static_cast<void> (0) : __assert_fail ("Str.size() <= 6 && \"String too long, possible overflow.\"" , "/build/llvm-toolchain-snapshot-10~svn372306/lib/Object/COFFObjectFile.cpp" , 70, __PRETTY_FUNCTION__)); | |||
71 | if (Str.size() > 6) | |||
72 | return true; | |||
73 | ||||
74 | uint64_t Value = 0; | |||
75 | while (!Str.empty()) { | |||
76 | unsigned CharVal; | |||
77 | if (Str[0] >= 'A' && Str[0] <= 'Z') // 0..25 | |||
78 | CharVal = Str[0] - 'A'; | |||
79 | else if (Str[0] >= 'a' && Str[0] <= 'z') // 26..51 | |||
80 | CharVal = Str[0] - 'a' + 26; | |||
81 | else if (Str[0] >= '0' && Str[0] <= '9') // 52..61 | |||
82 | CharVal = Str[0] - '0' + 52; | |||
83 | else if (Str[0] == '+') // 62 | |||
84 | CharVal = 62; | |||
85 | else if (Str[0] == '/') // 63 | |||
86 | CharVal = 63; | |||
87 | else | |||
88 | return true; | |||
89 | ||||
90 | Value = (Value * 64) + CharVal; | |||
91 | Str = Str.substr(1); | |||
92 | } | |||
93 | ||||
94 | if (Value > std::numeric_limits<uint32_t>::max()) | |||
95 | return true; | |||
96 | ||||
97 | Result = static_cast<uint32_t>(Value); | |||
98 | return false; | |||
99 | } | |||
100 | ||||
101 | template <typename coff_symbol_type> | |||
102 | const coff_symbol_type *COFFObjectFile::toSymb(DataRefImpl Ref) const { | |||
103 | const coff_symbol_type *Addr = | |||
104 | reinterpret_cast<const coff_symbol_type *>(Ref.p); | |||
105 | ||||
106 | assert(!checkOffset(Data, uintptr_t(Addr), sizeof(*Addr)))((!checkOffset(Data, uintptr_t(Addr), sizeof(*Addr))) ? static_cast <void> (0) : __assert_fail ("!checkOffset(Data, uintptr_t(Addr), sizeof(*Addr))" , "/build/llvm-toolchain-snapshot-10~svn372306/lib/Object/COFFObjectFile.cpp" , 106, __PRETTY_FUNCTION__)); | |||
107 | #ifndef NDEBUG | |||
108 | // Verify that the symbol points to a valid entry in the symbol table. | |||
109 | uintptr_t Offset = uintptr_t(Addr) - uintptr_t(base()); | |||
110 | ||||
111 | assert((Offset - getPointerToSymbolTable()) % sizeof(coff_symbol_type) == 0 &&(((Offset - getPointerToSymbolTable()) % sizeof(coff_symbol_type ) == 0 && "Symbol did not point to the beginning of a symbol" ) ? static_cast<void> (0) : __assert_fail ("(Offset - getPointerToSymbolTable()) % sizeof(coff_symbol_type) == 0 && \"Symbol did not point to the beginning of a symbol\"" , "/build/llvm-toolchain-snapshot-10~svn372306/lib/Object/COFFObjectFile.cpp" , 112, __PRETTY_FUNCTION__)) | |||
112 | "Symbol did not point to the beginning of a symbol")(((Offset - getPointerToSymbolTable()) % sizeof(coff_symbol_type ) == 0 && "Symbol did not point to the beginning of a symbol" ) ? static_cast<void> (0) : __assert_fail ("(Offset - getPointerToSymbolTable()) % sizeof(coff_symbol_type) == 0 && \"Symbol did not point to the beginning of a symbol\"" , "/build/llvm-toolchain-snapshot-10~svn372306/lib/Object/COFFObjectFile.cpp" , 112, __PRETTY_FUNCTION__)); | |||
113 | #endif | |||
114 | ||||
115 | return Addr; | |||
116 | } | |||
117 | ||||
118 | const coff_section *COFFObjectFile::toSec(DataRefImpl Ref) const { | |||
119 | const coff_section *Addr = reinterpret_cast<const coff_section*>(Ref.p); | |||
120 | ||||
121 | #ifndef NDEBUG | |||
122 | // Verify that the section points to a valid entry in the section table. | |||
123 | if (Addr < SectionTable || Addr >= (SectionTable + getNumberOfSections())) | |||
124 | report_fatal_error("Section was outside of section table."); | |||
125 | ||||
126 | uintptr_t Offset = uintptr_t(Addr) - uintptr_t(SectionTable); | |||
127 | assert(Offset % sizeof(coff_section) == 0 &&((Offset % sizeof(coff_section) == 0 && "Section did not point to the beginning of a section" ) ? static_cast<void> (0) : __assert_fail ("Offset % sizeof(coff_section) == 0 && \"Section did not point to the beginning of a section\"" , "/build/llvm-toolchain-snapshot-10~svn372306/lib/Object/COFFObjectFile.cpp" , 128, __PRETTY_FUNCTION__)) | |||
128 | "Section did not point to the beginning of a section")((Offset % sizeof(coff_section) == 0 && "Section did not point to the beginning of a section" ) ? static_cast<void> (0) : __assert_fail ("Offset % sizeof(coff_section) == 0 && \"Section did not point to the beginning of a section\"" , "/build/llvm-toolchain-snapshot-10~svn372306/lib/Object/COFFObjectFile.cpp" , 128, __PRETTY_FUNCTION__)); | |||
129 | #endif | |||
130 | ||||
131 | return Addr; | |||
132 | } | |||
133 | ||||
134 | void COFFObjectFile::moveSymbolNext(DataRefImpl &Ref) const { | |||
135 | auto End = reinterpret_cast<uintptr_t>(StringTable); | |||
136 | if (SymbolTable16) { | |||
137 | const coff_symbol16 *Symb = toSymb<coff_symbol16>(Ref); | |||
138 | Symb += 1 + Symb->NumberOfAuxSymbols; | |||
139 | Ref.p = std::min(reinterpret_cast<uintptr_t>(Symb), End); | |||
140 | } else if (SymbolTable32) { | |||
141 | const coff_symbol32 *Symb = toSymb<coff_symbol32>(Ref); | |||
142 | Symb += 1 + Symb->NumberOfAuxSymbols; | |||
143 | Ref.p = std::min(reinterpret_cast<uintptr_t>(Symb), End); | |||
144 | } else { | |||
145 | llvm_unreachable("no symbol table pointer!")::llvm::llvm_unreachable_internal("no symbol table pointer!", "/build/llvm-toolchain-snapshot-10~svn372306/lib/Object/COFFObjectFile.cpp" , 145); | |||
146 | } | |||
147 | } | |||
148 | ||||
149 | Expected<StringRef> COFFObjectFile::getSymbolName(DataRefImpl Ref) const { | |||
150 | COFFSymbolRef Symb = getCOFFSymbol(Ref); | |||
151 | StringRef Result; | |||
152 | if (std::error_code EC = getSymbolName(Symb, Result)) | |||
153 | return errorCodeToError(EC); | |||
154 | return Result; | |||
155 | } | |||
156 | ||||
157 | uint64_t COFFObjectFile::getSymbolValueImpl(DataRefImpl Ref) const { | |||
158 | return getCOFFSymbol(Ref).getValue(); | |||
159 | } | |||
160 | ||||
161 | uint32_t COFFObjectFile::getSymbolAlignment(DataRefImpl Ref) const { | |||
162 | // MSVC/link.exe seems to align symbols to the next-power-of-2 | |||
163 | // up to 32 bytes. | |||
164 | COFFSymbolRef Symb = getCOFFSymbol(Ref); | |||
165 | return std::min(uint64_t(32), PowerOf2Ceil(Symb.getValue())); | |||
166 | } | |||
167 | ||||
168 | Expected<uint64_t> COFFObjectFile::getSymbolAddress(DataRefImpl Ref) const { | |||
169 | uint64_t Result = getSymbolValue(Ref); | |||
170 | COFFSymbolRef Symb = getCOFFSymbol(Ref); | |||
171 | int32_t SectionNumber = Symb.getSectionNumber(); | |||
172 | ||||
173 | if (Symb.isAnyUndefined() || Symb.isCommon() || | |||
174 | COFF::isReservedSectionNumber(SectionNumber)) | |||
175 | return Result; | |||
176 | ||||
177 | const coff_section *Section = nullptr; | |||
178 | if (std::error_code EC = getSection(SectionNumber, Section)) | |||
179 | return errorCodeToError(EC); | |||
180 | Result += Section->VirtualAddress; | |||
181 | ||||
182 | // The section VirtualAddress does not include ImageBase, and we want to | |||
183 | // return virtual addresses. | |||
184 | Result += getImageBase(); | |||
185 | ||||
186 | return Result; | |||
187 | } | |||
188 | ||||
189 | Expected<SymbolRef::Type> COFFObjectFile::getSymbolType(DataRefImpl Ref) const { | |||
190 | COFFSymbolRef Symb = getCOFFSymbol(Ref); | |||
191 | int32_t SectionNumber = Symb.getSectionNumber(); | |||
192 | ||||
193 | if (Symb.getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION) | |||
194 | return SymbolRef::ST_Function; | |||
195 | if (Symb.isAnyUndefined()) | |||
196 | return SymbolRef::ST_Unknown; | |||
197 | if (Symb.isCommon()) | |||
198 | return SymbolRef::ST_Data; | |||
199 | if (Symb.isFileRecord()) | |||
200 | return SymbolRef::ST_File; | |||
201 | ||||
202 | // TODO: perhaps we need a new symbol type ST_Section. | |||
203 | if (SectionNumber == COFF::IMAGE_SYM_DEBUG || Symb.isSectionDefinition()) | |||
204 | return SymbolRef::ST_Debug; | |||
205 | ||||
206 | if (!COFF::isReservedSectionNumber(SectionNumber)) | |||
207 | return SymbolRef::ST_Data; | |||
208 | ||||
209 | return SymbolRef::ST_Other; | |||
210 | } | |||
211 | ||||
212 | uint32_t COFFObjectFile::getSymbolFlags(DataRefImpl Ref) const { | |||
213 | COFFSymbolRef Symb = getCOFFSymbol(Ref); | |||
214 | uint32_t Result = SymbolRef::SF_None; | |||
215 | ||||
216 | if (Symb.isExternal() || Symb.isWeakExternal()) | |||
217 | Result |= SymbolRef::SF_Global; | |||
218 | ||||
219 | if (const coff_aux_weak_external *AWE = Symb.getWeakExternal()) { | |||
220 | Result |= SymbolRef::SF_Weak; | |||
221 | if (AWE->Characteristics != COFF::IMAGE_WEAK_EXTERN_SEARCH_ALIAS) | |||
222 | Result |= SymbolRef::SF_Undefined; | |||
223 | } | |||
224 | ||||
225 | if (Symb.getSectionNumber() == COFF::IMAGE_SYM_ABSOLUTE) | |||
226 | Result |= SymbolRef::SF_Absolute; | |||
227 | ||||
228 | if (Symb.isFileRecord()) | |||
229 | Result |= SymbolRef::SF_FormatSpecific; | |||
230 | ||||
231 | if (Symb.isSectionDefinition()) | |||
232 | Result |= SymbolRef::SF_FormatSpecific; | |||
233 | ||||
234 | if (Symb.isCommon()) | |||
235 | Result |= SymbolRef::SF_Common; | |||
236 | ||||
237 | if (Symb.isUndefined()) | |||
238 | Result |= SymbolRef::SF_Undefined; | |||
239 | ||||
240 | return Result; | |||
241 | } | |||
242 | ||||
243 | uint64_t COFFObjectFile::getCommonSymbolSizeImpl(DataRefImpl Ref) const { | |||
244 | COFFSymbolRef Symb = getCOFFSymbol(Ref); | |||
245 | return Symb.getValue(); | |||
| ||||
246 | } | |||
247 | ||||
248 | Expected<section_iterator> | |||
249 | COFFObjectFile::getSymbolSection(DataRefImpl Ref) const { | |||
250 | COFFSymbolRef Symb = getCOFFSymbol(Ref); | |||
251 | if (COFF::isReservedSectionNumber(Symb.getSectionNumber())) | |||
252 | return section_end(); | |||
253 | const coff_section *Sec = nullptr; | |||
254 | if (std::error_code EC = getSection(Symb.getSectionNumber(), Sec)) | |||
255 | return errorCodeToError(EC); | |||
256 | DataRefImpl Ret; | |||
257 | Ret.p = reinterpret_cast<uintptr_t>(Sec); | |||
258 | return section_iterator(SectionRef(Ret, this)); | |||
259 | } | |||
260 | ||||
261 | unsigned COFFObjectFile::getSymbolSectionID(SymbolRef Sym) const { | |||
262 | COFFSymbolRef Symb = getCOFFSymbol(Sym.getRawDataRefImpl()); | |||
263 | return Symb.getSectionNumber(); | |||
264 | } | |||
265 | ||||
266 | void COFFObjectFile::moveSectionNext(DataRefImpl &Ref) const { | |||
267 | const coff_section *Sec = toSec(Ref); | |||
268 | Sec += 1; | |||
269 | Ref.p = reinterpret_cast<uintptr_t>(Sec); | |||
270 | } | |||
271 | ||||
272 | Expected<StringRef> COFFObjectFile::getSectionName(DataRefImpl Ref) const { | |||
273 | const coff_section *Sec = toSec(Ref); | |||
274 | return getSectionName(Sec); | |||
275 | } | |||
276 | ||||
277 | uint64_t COFFObjectFile::getSectionAddress(DataRefImpl Ref) const { | |||
278 | const coff_section *Sec = toSec(Ref); | |||
279 | uint64_t Result = Sec->VirtualAddress; | |||
280 | ||||
281 | // The section VirtualAddress does not include ImageBase, and we want to | |||
282 | // return virtual addresses. | |||
283 | Result += getImageBase(); | |||
284 | return Result; | |||
285 | } | |||
286 | ||||
287 | uint64_t COFFObjectFile::getSectionIndex(DataRefImpl Sec) const { | |||
288 | return toSec(Sec) - SectionTable; | |||
289 | } | |||
290 | ||||
291 | uint64_t COFFObjectFile::getSectionSize(DataRefImpl Ref) const { | |||
292 | return getSectionSize(toSec(Ref)); | |||
293 | } | |||
294 | ||||
295 | Expected<ArrayRef<uint8_t>> | |||
296 | COFFObjectFile::getSectionContents(DataRefImpl Ref) const { | |||
297 | const coff_section *Sec = toSec(Ref); | |||
298 | ArrayRef<uint8_t> Res; | |||
299 | if (Error E = getSectionContents(Sec, Res)) | |||
300 | return std::move(E); | |||
301 | return Res; | |||
302 | } | |||
303 | ||||
304 | uint64_t COFFObjectFile::getSectionAlignment(DataRefImpl Ref) const { | |||
305 | const coff_section *Sec = toSec(Ref); | |||
306 | return Sec->getAlignment(); | |||
307 | } | |||
308 | ||||
309 | bool COFFObjectFile::isSectionCompressed(DataRefImpl Sec) const { | |||
310 | return false; | |||
311 | } | |||
312 | ||||
313 | bool COFFObjectFile::isSectionText(DataRefImpl Ref) const { | |||
314 | const coff_section *Sec = toSec(Ref); | |||
315 | return Sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE; | |||
316 | } | |||
317 | ||||
318 | bool COFFObjectFile::isSectionData(DataRefImpl Ref) const { | |||
319 | const coff_section *Sec = toSec(Ref); | |||
320 | return Sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA; | |||
321 | } | |||
322 | ||||
323 | bool COFFObjectFile::isSectionBSS(DataRefImpl Ref) const { | |||
324 | const coff_section *Sec = toSec(Ref); | |||
325 | const uint32_t BssFlags = COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA | | |||
326 | COFF::IMAGE_SCN_MEM_READ | | |||
327 | COFF::IMAGE_SCN_MEM_WRITE; | |||
328 | return (Sec->Characteristics & BssFlags) == BssFlags; | |||
329 | } | |||
330 | ||||
331 | unsigned COFFObjectFile::getSectionID(SectionRef Sec) const { | |||
332 | uintptr_t Offset = | |||
333 | uintptr_t(Sec.getRawDataRefImpl().p) - uintptr_t(SectionTable); | |||
334 | assert((Offset % sizeof(coff_section)) == 0)(((Offset % sizeof(coff_section)) == 0) ? static_cast<void > (0) : __assert_fail ("(Offset % sizeof(coff_section)) == 0" , "/build/llvm-toolchain-snapshot-10~svn372306/lib/Object/COFFObjectFile.cpp" , 334, __PRETTY_FUNCTION__)); | |||
335 | return (Offset / sizeof(coff_section)) + 1; | |||
336 | } | |||
337 | ||||
338 | bool COFFObjectFile::isSectionVirtual(DataRefImpl Ref) const { | |||
339 | const coff_section *Sec = toSec(Ref); | |||
340 | // In COFF, a virtual section won't have any in-file | |||
341 | // content, so the file pointer to the content will be zero. | |||
342 | return Sec->PointerToRawData == 0; | |||
343 | } | |||
344 | ||||
345 | static uint32_t getNumberOfRelocations(const coff_section *Sec, | |||
346 | MemoryBufferRef M, const uint8_t *base) { | |||
347 | // The field for the number of relocations in COFF section table is only | |||
348 | // 16-bit wide. If a section has more than 65535 relocations, 0xFFFF is set to | |||
349 | // NumberOfRelocations field, and the actual relocation count is stored in the | |||
350 | // VirtualAddress field in the first relocation entry. | |||
351 | if (Sec->hasExtendedRelocations()) { | |||
352 | const coff_relocation *FirstReloc; | |||
353 | if (getObject(FirstReloc, M, reinterpret_cast<const coff_relocation*>( | |||
354 | base + Sec->PointerToRelocations))) | |||
355 | return 0; | |||
356 | // -1 to exclude this first relocation entry. | |||
357 | return FirstReloc->VirtualAddress - 1; | |||
358 | } | |||
359 | return Sec->NumberOfRelocations; | |||
360 | } | |||
361 | ||||
362 | static const coff_relocation * | |||
363 | getFirstReloc(const coff_section *Sec, MemoryBufferRef M, const uint8_t *Base) { | |||
364 | uint64_t NumRelocs = getNumberOfRelocations(Sec, M, Base); | |||
365 | if (!NumRelocs) | |||
366 | return nullptr; | |||
367 | auto begin = reinterpret_cast<const coff_relocation *>( | |||
368 | Base + Sec->PointerToRelocations); | |||
369 | if (Sec->hasExtendedRelocations()) { | |||
370 | // Skip the first relocation entry repurposed to store the number of | |||
371 | // relocations. | |||
372 | begin++; | |||
373 | } | |||
374 | if (Binary::checkOffset(M, uintptr_t(begin), | |||
375 | sizeof(coff_relocation) * NumRelocs)) | |||
376 | return nullptr; | |||
377 | return begin; | |||
378 | } | |||
379 | ||||
380 | relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const { | |||
381 | const coff_section *Sec = toSec(Ref); | |||
382 | const coff_relocation *begin = getFirstReloc(Sec, Data, base()); | |||
383 | if (begin && Sec->VirtualAddress != 0) | |||
384 | report_fatal_error("Sections with relocations should have an address of 0"); | |||
385 | DataRefImpl Ret; | |||
386 | Ret.p = reinterpret_cast<uintptr_t>(begin); | |||
387 | return relocation_iterator(RelocationRef(Ret, this)); | |||
388 | } | |||
389 | ||||
390 | relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const { | |||
391 | const coff_section *Sec = toSec(Ref); | |||
392 | const coff_relocation *I = getFirstReloc(Sec, Data, base()); | |||
393 | if (I) | |||
394 | I += getNumberOfRelocations(Sec, Data, base()); | |||
395 | DataRefImpl Ret; | |||
396 | Ret.p = reinterpret_cast<uintptr_t>(I); | |||
397 | return relocation_iterator(RelocationRef(Ret, this)); | |||
398 | } | |||
399 | ||||
400 | // Initialize the pointer to the symbol table. | |||
401 | std::error_code COFFObjectFile::initSymbolTablePtr() { | |||
402 | if (COFFHeader) | |||
403 | if (std::error_code EC = getObject( | |||
404 | SymbolTable16, Data, base() + getPointerToSymbolTable(), | |||
405 | (uint64_t)getNumberOfSymbols() * getSymbolTableEntrySize())) | |||
406 | return EC; | |||
407 | ||||
408 | if (COFFBigObjHeader) | |||
409 | if (std::error_code EC = getObject( | |||
410 | SymbolTable32, Data, base() + getPointerToSymbolTable(), | |||
411 | (uint64_t)getNumberOfSymbols() * getSymbolTableEntrySize())) | |||
412 | return EC; | |||
413 | ||||
414 | // Find string table. The first four byte of the string table contains the | |||
415 | // total size of the string table, including the size field itself. If the | |||
416 | // string table is empty, the value of the first four byte would be 4. | |||
417 | uint32_t StringTableOffset = getPointerToSymbolTable() + | |||
418 | getNumberOfSymbols() * getSymbolTableEntrySize(); | |||
419 | const uint8_t *StringTableAddr = base() + StringTableOffset; | |||
420 | const ulittle32_t *StringTableSizePtr; | |||
421 | if (std::error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr)) | |||
422 | return EC; | |||
423 | StringTableSize = *StringTableSizePtr; | |||
424 | if (std::error_code EC = | |||
425 | getObject(StringTable, Data, StringTableAddr, StringTableSize)) | |||
426 | return EC; | |||
427 | ||||
428 | // Treat table sizes < 4 as empty because contrary to the PECOFF spec, some | |||
429 | // tools like cvtres write a size of 0 for an empty table instead of 4. | |||
430 | if (StringTableSize < 4) | |||
431 | StringTableSize = 4; | |||
432 | ||||
433 | // Check that the string table is null terminated if has any in it. | |||
434 | if (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0) | |||
435 | return object_error::parse_failed; | |||
436 | return std::error_code(); | |||
437 | } | |||
438 | ||||
439 | uint64_t COFFObjectFile::getImageBase() const { | |||
440 | if (PE32Header) | |||
441 | return PE32Header->ImageBase; | |||
442 | else if (PE32PlusHeader) | |||
443 | return PE32PlusHeader->ImageBase; | |||
444 | // This actually comes up in practice. | |||
445 | return 0; | |||
446 | } | |||
447 | ||||
448 | // Returns the file offset for the given VA. | |||
449 | std::error_code COFFObjectFile::getVaPtr(uint64_t Addr, uintptr_t &Res) const { | |||
450 | uint64_t ImageBase = getImageBase(); | |||
451 | uint64_t Rva = Addr - ImageBase; | |||
452 | assert(Rva <= UINT32_MAX)((Rva <= (4294967295U)) ? static_cast<void> (0) : __assert_fail ("Rva <= UINT32_MAX", "/build/llvm-toolchain-snapshot-10~svn372306/lib/Object/COFFObjectFile.cpp" , 452, __PRETTY_FUNCTION__)); | |||
453 | return getRvaPtr((uint32_t)Rva, Res); | |||
454 | } | |||
455 | ||||
456 | // Returns the file offset for the given RVA. | |||
457 | std::error_code COFFObjectFile::getRvaPtr(uint32_t Addr, uintptr_t &Res) const { | |||
458 | for (const SectionRef &S : sections()) { | |||
459 | const coff_section *Section = getCOFFSection(S); | |||
460 | uint32_t SectionStart = Section->VirtualAddress; | |||
461 | uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize; | |||
462 | if (SectionStart <= Addr && Addr < SectionEnd) { | |||
463 | uint32_t Offset = Addr - SectionStart; | |||
464 | Res = uintptr_t(base()) + Section->PointerToRawData + Offset; | |||
465 | return std::error_code(); | |||
466 | } | |||
467 | } | |||
468 | return object_error::parse_failed; | |||
469 | } | |||
470 | ||||
471 | std::error_code | |||
472 | COFFObjectFile::getRvaAndSizeAsBytes(uint32_t RVA, uint32_t Size, | |||
473 | ArrayRef<uint8_t> &Contents) const { | |||
474 | for (const SectionRef &S : sections()) { | |||
475 | const coff_section *Section = getCOFFSection(S); | |||
476 | uint32_t SectionStart = Section->VirtualAddress; | |||
477 | // Check if this RVA is within the section bounds. Be careful about integer | |||
478 | // overflow. | |||
479 | uint32_t OffsetIntoSection = RVA - SectionStart; | |||
480 | if (SectionStart <= RVA && OffsetIntoSection < Section->VirtualSize && | |||
481 | Size <= Section->VirtualSize - OffsetIntoSection) { | |||
482 | uintptr_t Begin = | |||
483 | uintptr_t(base()) + Section->PointerToRawData + OffsetIntoSection; | |||
484 | Contents = | |||
485 | ArrayRef<uint8_t>(reinterpret_cast<const uint8_t *>(Begin), Size); | |||
486 | return std::error_code(); | |||
487 | } | |||
488 | } | |||
489 | return object_error::parse_failed; | |||
490 | } | |||
491 | ||||
492 | // Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name | |||
493 | // table entry. | |||
494 | std::error_code COFFObjectFile::getHintName(uint32_t Rva, uint16_t &Hint, | |||
495 | StringRef &Name) const { | |||
496 | uintptr_t IntPtr = 0; | |||
497 | if (std::error_code EC = getRvaPtr(Rva, IntPtr)) | |||
498 | return EC; | |||
499 | const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr); | |||
500 | Hint = *reinterpret_cast<const ulittle16_t *>(Ptr); | |||
501 | Name = StringRef(reinterpret_cast<const char *>(Ptr + 2)); | |||
502 | return std::error_code(); | |||
503 | } | |||
504 | ||||
505 | std::error_code | |||
506 | COFFObjectFile::getDebugPDBInfo(const debug_directory *DebugDir, | |||
507 | const codeview::DebugInfo *&PDBInfo, | |||
508 | StringRef &PDBFileName) const { | |||
509 | ArrayRef<uint8_t> InfoBytes; | |||
510 | if (std::error_code EC = getRvaAndSizeAsBytes( | |||
511 | DebugDir->AddressOfRawData, DebugDir->SizeOfData, InfoBytes)) | |||
512 | return EC; | |||
513 | if (InfoBytes.size() < sizeof(*PDBInfo) + 1) | |||
514 | return object_error::parse_failed; | |||
515 | PDBInfo = reinterpret_cast<const codeview::DebugInfo *>(InfoBytes.data()); | |||
516 | InfoBytes = InfoBytes.drop_front(sizeof(*PDBInfo)); | |||
517 | PDBFileName = StringRef(reinterpret_cast<const char *>(InfoBytes.data()), | |||
518 | InfoBytes.size()); | |||
519 | // Truncate the name at the first null byte. Ignore any padding. | |||
520 | PDBFileName = PDBFileName.split('\0').first; | |||
521 | return std::error_code(); | |||
522 | } | |||
523 | ||||
524 | std::error_code | |||
525 | COFFObjectFile::getDebugPDBInfo(const codeview::DebugInfo *&PDBInfo, | |||
526 | StringRef &PDBFileName) const { | |||
527 | for (const debug_directory &D : debug_directories()) | |||
528 | if (D.Type == COFF::IMAGE_DEBUG_TYPE_CODEVIEW) | |||
529 | return getDebugPDBInfo(&D, PDBInfo, PDBFileName); | |||
530 | // If we get here, there is no PDB info to return. | |||
531 | PDBInfo = nullptr; | |||
532 | PDBFileName = StringRef(); | |||
533 | return std::error_code(); | |||
534 | } | |||
535 | ||||
536 | // Find the import table. | |||
537 | std::error_code COFFObjectFile::initImportTablePtr() { | |||
538 | // First, we get the RVA of the import table. If the file lacks a pointer to | |||
539 | // the import table, do nothing. | |||
540 | const data_directory *DataEntry; | |||
541 | if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry)) | |||
542 | return std::error_code(); | |||
543 | ||||
544 | // Do nothing if the pointer to import table is NULL. | |||
545 | if (DataEntry->RelativeVirtualAddress == 0) | |||
546 | return std::error_code(); | |||
547 | ||||
548 | uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress; | |||
549 | ||||
550 | // Find the section that contains the RVA. This is needed because the RVA is | |||
551 | // the import table's memory address which is different from its file offset. | |||
552 | uintptr_t IntPtr = 0; | |||
553 | if (std::error_code EC = getRvaPtr(ImportTableRva, IntPtr)) | |||
554 | return EC; | |||
555 | if (std::error_code EC = checkOffset(Data, IntPtr, DataEntry->Size)) | |||
556 | return EC; | |||
557 | ImportDirectory = reinterpret_cast< | |||
558 | const coff_import_directory_table_entry *>(IntPtr); | |||
559 | return std::error_code(); | |||
560 | } | |||
561 | ||||
562 | // Initializes DelayImportDirectory and NumberOfDelayImportDirectory. | |||
563 | std::error_code COFFObjectFile::initDelayImportTablePtr() { | |||
564 | const data_directory *DataEntry; | |||
565 | if (getDataDirectory(COFF::DELAY_IMPORT_DESCRIPTOR, DataEntry)) | |||
566 | return std::error_code(); | |||
567 | if (DataEntry->RelativeVirtualAddress == 0) | |||
568 | return std::error_code(); | |||
569 | ||||
570 | uint32_t RVA = DataEntry->RelativeVirtualAddress; | |||
571 | NumberOfDelayImportDirectory = DataEntry->Size / | |||
572 | sizeof(delay_import_directory_table_entry) - 1; | |||
573 | ||||
574 | uintptr_t IntPtr = 0; | |||
575 | if (std::error_code EC = getRvaPtr(RVA, IntPtr)) | |||
576 | return EC; | |||
577 | DelayImportDirectory = reinterpret_cast< | |||
578 | const delay_import_directory_table_entry *>(IntPtr); | |||
579 | return std::error_code(); | |||
580 | } | |||
581 | ||||
582 | // Find the export table. | |||
583 | std::error_code COFFObjectFile::initExportTablePtr() { | |||
584 | // First, we get the RVA of the export table. If the file lacks a pointer to | |||
585 | // the export table, do nothing. | |||
586 | const data_directory *DataEntry; | |||
587 | if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry)) | |||
588 | return std::error_code(); | |||
589 | ||||
590 | // Do nothing if the pointer to export table is NULL. | |||
591 | if (DataEntry->RelativeVirtualAddress == 0) | |||
592 | return std::error_code(); | |||
593 | ||||
594 | uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress; | |||
595 | uintptr_t IntPtr = 0; | |||
596 | if (std::error_code EC = getRvaPtr(ExportTableRva, IntPtr)) | |||
597 | return EC; | |||
598 | ExportDirectory = | |||
599 | reinterpret_cast<const export_directory_table_entry *>(IntPtr); | |||
600 | return std::error_code(); | |||
601 | } | |||
602 | ||||
603 | std::error_code COFFObjectFile::initBaseRelocPtr() { | |||
604 | const data_directory *DataEntry; | |||
605 | if (getDataDirectory(COFF::BASE_RELOCATION_TABLE, DataEntry)) | |||
606 | return std::error_code(); | |||
607 | if (DataEntry->RelativeVirtualAddress == 0) | |||
608 | return std::error_code(); | |||
609 | ||||
610 | uintptr_t IntPtr = 0; | |||
611 | if (std::error_code EC = getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr)) | |||
612 | return EC; | |||
613 | BaseRelocHeader = reinterpret_cast<const coff_base_reloc_block_header *>( | |||
614 | IntPtr); | |||
615 | BaseRelocEnd = reinterpret_cast<coff_base_reloc_block_header *>( | |||
616 | IntPtr + DataEntry->Size); | |||
617 | // FIXME: Verify the section containing BaseRelocHeader has at least | |||
618 | // DataEntry->Size bytes after DataEntry->RelativeVirtualAddress. | |||
619 | return std::error_code(); | |||
620 | } | |||
621 | ||||
622 | std::error_code COFFObjectFile::initDebugDirectoryPtr() { | |||
623 | // Get the RVA of the debug directory. Do nothing if it does not exist. | |||
624 | const data_directory *DataEntry; | |||
625 | if (getDataDirectory(COFF::DEBUG_DIRECTORY, DataEntry)) | |||
626 | return std::error_code(); | |||
627 | ||||
628 | // Do nothing if the RVA is NULL. | |||
629 | if (DataEntry->RelativeVirtualAddress == 0) | |||
630 | return std::error_code(); | |||
631 | ||||
632 | // Check that the size is a multiple of the entry size. | |||
633 | if (DataEntry->Size % sizeof(debug_directory) != 0) | |||
634 | return object_error::parse_failed; | |||
635 | ||||
636 | uintptr_t IntPtr = 0; | |||
637 | if (std::error_code EC = getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr)) | |||
638 | return EC; | |||
639 | DebugDirectoryBegin = reinterpret_cast<const debug_directory *>(IntPtr); | |||
640 | DebugDirectoryEnd = reinterpret_cast<const debug_directory *>( | |||
641 | IntPtr + DataEntry->Size); | |||
642 | // FIXME: Verify the section containing DebugDirectoryBegin has at least | |||
643 | // DataEntry->Size bytes after DataEntry->RelativeVirtualAddress. | |||
644 | return std::error_code(); | |||
645 | } | |||
646 | ||||
647 | std::error_code COFFObjectFile::initLoadConfigPtr() { | |||
648 | // Get the RVA of the debug directory. Do nothing if it does not exist. | |||
649 | const data_directory *DataEntry; | |||
650 | if (getDataDirectory(COFF::LOAD_CONFIG_TABLE, DataEntry)) | |||
651 | return std::error_code(); | |||
652 | ||||
653 | // Do nothing if the RVA is NULL. | |||
654 | if (DataEntry->RelativeVirtualAddress == 0) | |||
655 | return std::error_code(); | |||
656 | uintptr_t IntPtr = 0; | |||
657 | if (std::error_code EC = getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr)) | |||
658 | return EC; | |||
659 | ||||
660 | LoadConfig = (const void *)IntPtr; | |||
661 | return std::error_code(); | |||
662 | } | |||
663 | ||||
664 | COFFObjectFile::COFFObjectFile(MemoryBufferRef Object, std::error_code &EC) | |||
665 | : ObjectFile(Binary::ID_COFF, Object), COFFHeader(nullptr), | |||
666 | COFFBigObjHeader(nullptr), PE32Header(nullptr), PE32PlusHeader(nullptr), | |||
667 | DataDirectory(nullptr), SectionTable(nullptr), SymbolTable16(nullptr), | |||
668 | SymbolTable32(nullptr), StringTable(nullptr), StringTableSize(0), | |||
669 | ImportDirectory(nullptr), | |||
670 | DelayImportDirectory(nullptr), NumberOfDelayImportDirectory(0), | |||
671 | ExportDirectory(nullptr), BaseRelocHeader(nullptr), BaseRelocEnd(nullptr), | |||
672 | DebugDirectoryBegin(nullptr), DebugDirectoryEnd(nullptr) { | |||
673 | // Check that we at least have enough room for a header. | |||
674 | if (!checkSize(Data, EC, sizeof(coff_file_header))) | |||
675 | return; | |||
676 | ||||
677 | // The current location in the file where we are looking at. | |||
678 | uint64_t CurPtr = 0; | |||
679 | ||||
680 | // PE header is optional and is present only in executables. If it exists, | |||
681 | // it is placed right after COFF header. | |||
682 | bool HasPEHeader = false; | |||
683 | ||||
684 | // Check if this is a PE/COFF file. | |||
685 | if (checkSize(Data, EC, sizeof(dos_header) + sizeof(COFF::PEMagic))) { | |||
686 | // PE/COFF, seek through MS-DOS compatibility stub and 4-byte | |||
687 | // PE signature to find 'normal' COFF header. | |||
688 | const auto *DH = reinterpret_cast<const dos_header *>(base()); | |||
689 | if (DH->Magic[0] == 'M' && DH->Magic[1] == 'Z') { | |||
690 | CurPtr = DH->AddressOfNewExeHeader; | |||
691 | // Check the PE magic bytes. ("PE\0\0") | |||
692 | if (memcmp(base() + CurPtr, COFF::PEMagic, sizeof(COFF::PEMagic)) != 0) { | |||
693 | EC = object_error::parse_failed; | |||
694 | return; | |||
695 | } | |||
696 | CurPtr += sizeof(COFF::PEMagic); // Skip the PE magic bytes. | |||
697 | HasPEHeader = true; | |||
698 | } | |||
699 | } | |||
700 | ||||
701 | if ((EC = getObject(COFFHeader, Data, base() + CurPtr))) | |||
702 | return; | |||
703 | ||||
704 | // It might be a bigobj file, let's check. Note that COFF bigobj and COFF | |||
705 | // import libraries share a common prefix but bigobj is more restrictive. | |||
706 | if (!HasPEHeader && COFFHeader->Machine == COFF::IMAGE_FILE_MACHINE_UNKNOWN && | |||
707 | COFFHeader->NumberOfSections == uint16_t(0xffff) && | |||
708 | checkSize(Data, EC, sizeof(coff_bigobj_file_header))) { | |||
709 | if ((EC = getObject(COFFBigObjHeader, Data, base() + CurPtr))) | |||
710 | return; | |||
711 | ||||
712 | // Verify that we are dealing with bigobj. | |||
713 | if (COFFBigObjHeader->Version >= COFF::BigObjHeader::MinBigObjectVersion && | |||
714 | std::memcmp(COFFBigObjHeader->UUID, COFF::BigObjMagic, | |||
715 | sizeof(COFF::BigObjMagic)) == 0) { | |||
716 | COFFHeader = nullptr; | |||
717 | CurPtr += sizeof(coff_bigobj_file_header); | |||
718 | } else { | |||
719 | // It's not a bigobj. | |||
720 | COFFBigObjHeader = nullptr; | |||
721 | } | |||
722 | } | |||
723 | if (COFFHeader) { | |||
724 | // The prior checkSize call may have failed. This isn't a hard error | |||
725 | // because we were just trying to sniff out bigobj. | |||
726 | EC = std::error_code(); | |||
727 | CurPtr += sizeof(coff_file_header); | |||
728 | ||||
729 | if (COFFHeader->isImportLibrary()) | |||
730 | return; | |||
731 | } | |||
732 | ||||
733 | if (HasPEHeader) { | |||
734 | const pe32_header *Header; | |||
735 | if ((EC = getObject(Header, Data, base() + CurPtr))) | |||
736 | return; | |||
737 | ||||
738 | const uint8_t *DataDirAddr; | |||
739 | uint64_t DataDirSize; | |||
740 | if (Header->Magic == COFF::PE32Header::PE32) { | |||
741 | PE32Header = Header; | |||
742 | DataDirAddr = base() + CurPtr + sizeof(pe32_header); | |||
743 | DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize; | |||
744 | } else if (Header->Magic == COFF::PE32Header::PE32_PLUS) { | |||
745 | PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header); | |||
746 | DataDirAddr = base() + CurPtr + sizeof(pe32plus_header); | |||
747 | DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize; | |||
748 | } else { | |||
749 | // It's neither PE32 nor PE32+. | |||
750 | EC = object_error::parse_failed; | |||
751 | return; | |||
752 | } | |||
753 | if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize))) | |||
754 | return; | |||
755 | } | |||
756 | ||||
757 | if (COFFHeader) | |||
758 | CurPtr += COFFHeader->SizeOfOptionalHeader; | |||
759 | ||||
760 | if ((EC = getObject(SectionTable, Data, base() + CurPtr, | |||
761 | (uint64_t)getNumberOfSections() * sizeof(coff_section)))) | |||
762 | return; | |||
763 | ||||
764 | // Initialize the pointer to the symbol table. | |||
765 | if (getPointerToSymbolTable() != 0) { | |||
766 | if ((EC = initSymbolTablePtr())) { | |||
767 | SymbolTable16 = nullptr; | |||
768 | SymbolTable32 = nullptr; | |||
769 | StringTable = nullptr; | |||
770 | StringTableSize = 0; | |||
771 | } | |||
772 | } else { | |||
773 | // We had better not have any symbols if we don't have a symbol table. | |||
774 | if (getNumberOfSymbols() != 0) { | |||
775 | EC = object_error::parse_failed; | |||
776 | return; | |||
777 | } | |||
778 | } | |||
779 | ||||
780 | // Initialize the pointer to the beginning of the import table. | |||
781 | if ((EC = initImportTablePtr())) | |||
782 | return; | |||
783 | if ((EC = initDelayImportTablePtr())) | |||
784 | return; | |||
785 | ||||
786 | // Initialize the pointer to the export table. | |||
787 | if ((EC = initExportTablePtr())) | |||
788 | return; | |||
789 | ||||
790 | // Initialize the pointer to the base relocation table. | |||
791 | if ((EC = initBaseRelocPtr())) | |||
792 | return; | |||
793 | ||||
794 | // Initialize the pointer to the export table. | |||
795 | if ((EC = initDebugDirectoryPtr())) | |||
796 | return; | |||
797 | ||||
798 | if ((EC = initLoadConfigPtr())) | |||
799 | return; | |||
800 | ||||
801 | EC = std::error_code(); | |||
802 | } | |||
803 | ||||
804 | basic_symbol_iterator COFFObjectFile::symbol_begin() const { | |||
805 | DataRefImpl Ret; | |||
806 | Ret.p = getSymbolTable(); | |||
807 | return basic_symbol_iterator(SymbolRef(Ret, this)); | |||
808 | } | |||
809 | ||||
810 | basic_symbol_iterator COFFObjectFile::symbol_end() const { | |||
811 | // The symbol table ends where the string table begins. | |||
812 | DataRefImpl Ret; | |||
813 | Ret.p = reinterpret_cast<uintptr_t>(StringTable); | |||
814 | return basic_symbol_iterator(SymbolRef(Ret, this)); | |||
815 | } | |||
816 | ||||
817 | import_directory_iterator COFFObjectFile::import_directory_begin() const { | |||
818 | if (!ImportDirectory) | |||
819 | return import_directory_end(); | |||
820 | if (ImportDirectory->isNull()) | |||
821 | return import_directory_end(); | |||
822 | return import_directory_iterator( | |||
823 | ImportDirectoryEntryRef(ImportDirectory, 0, this)); | |||
824 | } | |||
825 | ||||
826 | import_directory_iterator COFFObjectFile::import_directory_end() const { | |||
827 | return import_directory_iterator( | |||
828 | ImportDirectoryEntryRef(nullptr, -1, this)); | |||
829 | } | |||
830 | ||||
831 | delay_import_directory_iterator | |||
832 | COFFObjectFile::delay_import_directory_begin() const { | |||
833 | return delay_import_directory_iterator( | |||
834 | DelayImportDirectoryEntryRef(DelayImportDirectory, 0, this)); | |||
835 | } | |||
836 | ||||
837 | delay_import_directory_iterator | |||
838 | COFFObjectFile::delay_import_directory_end() const { | |||
839 | return delay_import_directory_iterator( | |||
840 | DelayImportDirectoryEntryRef( | |||
841 | DelayImportDirectory, NumberOfDelayImportDirectory, this)); | |||
842 | } | |||
843 | ||||
844 | export_directory_iterator COFFObjectFile::export_directory_begin() const { | |||
845 | return export_directory_iterator( | |||
846 | ExportDirectoryEntryRef(ExportDirectory, 0, this)); | |||
847 | } | |||
848 | ||||
849 | export_directory_iterator COFFObjectFile::export_directory_end() const { | |||
850 | if (!ExportDirectory) | |||
851 | return export_directory_iterator(ExportDirectoryEntryRef(nullptr, 0, this)); | |||
852 | ExportDirectoryEntryRef Ref(ExportDirectory, | |||
853 | ExportDirectory->AddressTableEntries, this); | |||
854 | return export_directory_iterator(Ref); | |||
855 | } | |||
856 | ||||
857 | section_iterator COFFObjectFile::section_begin() const { | |||
858 | DataRefImpl Ret; | |||
859 | Ret.p = reinterpret_cast<uintptr_t>(SectionTable); | |||
860 | return section_iterator(SectionRef(Ret, this)); | |||
861 | } | |||
862 | ||||
863 | section_iterator COFFObjectFile::section_end() const { | |||
864 | DataRefImpl Ret; | |||
865 | int NumSections = | |||
866 | COFFHeader && COFFHeader->isImportLibrary() ? 0 : getNumberOfSections(); | |||
867 | Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections); | |||
868 | return section_iterator(SectionRef(Ret, this)); | |||
869 | } | |||
870 | ||||
871 | base_reloc_iterator COFFObjectFile::base_reloc_begin() const { | |||
872 | return base_reloc_iterator(BaseRelocRef(BaseRelocHeader, this)); | |||
873 | } | |||
874 | ||||
875 | base_reloc_iterator COFFObjectFile::base_reloc_end() const { | |||
876 | return base_reloc_iterator(BaseRelocRef(BaseRelocEnd, this)); | |||
877 | } | |||
878 | ||||
879 | uint8_t COFFObjectFile::getBytesInAddress() const { | |||
880 | return getArch() == Triple::x86_64 || getArch() == Triple::aarch64 ? 8 : 4; | |||
881 | } | |||
882 | ||||
883 | StringRef COFFObjectFile::getFileFormatName() const { | |||
884 | switch(getMachine()) { | |||
885 | case COFF::IMAGE_FILE_MACHINE_I386: | |||
886 | return "COFF-i386"; | |||
887 | case COFF::IMAGE_FILE_MACHINE_AMD64: | |||
888 | return "COFF-x86-64"; | |||
889 | case COFF::IMAGE_FILE_MACHINE_ARMNT: | |||
890 | return "COFF-ARM"; | |||
891 | case COFF::IMAGE_FILE_MACHINE_ARM64: | |||
892 | return "COFF-ARM64"; | |||
893 | default: | |||
894 | return "COFF-<unknown arch>"; | |||
895 | } | |||
896 | } | |||
897 | ||||
898 | Triple::ArchType COFFObjectFile::getArch() const { | |||
899 | switch (getMachine()) { | |||
900 | case COFF::IMAGE_FILE_MACHINE_I386: | |||
901 | return Triple::x86; | |||
902 | case COFF::IMAGE_FILE_MACHINE_AMD64: | |||
903 | return Triple::x86_64; | |||
904 | case COFF::IMAGE_FILE_MACHINE_ARMNT: | |||
905 | return Triple::thumb; | |||
906 | case COFF::IMAGE_FILE_MACHINE_ARM64: | |||
907 | return Triple::aarch64; | |||
908 | default: | |||
909 | return Triple::UnknownArch; | |||
910 | } | |||
911 | } | |||
912 | ||||
913 | Expected<uint64_t> COFFObjectFile::getStartAddress() const { | |||
914 | if (PE32Header) | |||
915 | return PE32Header->AddressOfEntryPoint; | |||
916 | return 0; | |||
917 | } | |||
918 | ||||
919 | iterator_range<import_directory_iterator> | |||
920 | COFFObjectFile::import_directories() const { | |||
921 | return make_range(import_directory_begin(), import_directory_end()); | |||
922 | } | |||
923 | ||||
924 | iterator_range<delay_import_directory_iterator> | |||
925 | COFFObjectFile::delay_import_directories() const { | |||
926 | return make_range(delay_import_directory_begin(), | |||
927 | delay_import_directory_end()); | |||
928 | } | |||
929 | ||||
930 | iterator_range<export_directory_iterator> | |||
931 | COFFObjectFile::export_directories() const { | |||
932 | return make_range(export_directory_begin(), export_directory_end()); | |||
933 | } | |||
934 | ||||
935 | iterator_range<base_reloc_iterator> COFFObjectFile::base_relocs() const { | |||
936 | return make_range(base_reloc_begin(), base_reloc_end()); | |||
937 | } | |||
938 | ||||
939 | std::error_code | |||
940 | COFFObjectFile::getDataDirectory(uint32_t Index, | |||
941 | const data_directory *&Res) const { | |||
942 | // Error if there's no data directory or the index is out of range. | |||
943 | if (!DataDirectory) { | |||
944 | Res = nullptr; | |||
945 | return object_error::parse_failed; | |||
946 | } | |||
947 | assert(PE32Header || PE32PlusHeader)((PE32Header || PE32PlusHeader) ? static_cast<void> (0) : __assert_fail ("PE32Header || PE32PlusHeader", "/build/llvm-toolchain-snapshot-10~svn372306/lib/Object/COFFObjectFile.cpp" , 947, __PRETTY_FUNCTION__)); | |||
948 | uint32_t NumEnt = PE32Header ? PE32Header->NumberOfRvaAndSize | |||
949 | : PE32PlusHeader->NumberOfRvaAndSize; | |||
950 | if (Index >= NumEnt) { | |||
951 | Res = nullptr; | |||
952 | return object_error::parse_failed; | |||
953 | } | |||
954 | Res = &DataDirectory[Index]; | |||
955 | return std::error_code(); | |||
956 | } | |||
957 | ||||
958 | std::error_code COFFObjectFile::getSection(int32_t Index, | |||
959 | const coff_section *&Result) const { | |||
960 | Result = nullptr; | |||
961 | if (COFF::isReservedSectionNumber(Index)) | |||
962 | return std::error_code(); | |||
963 | if (static_cast<uint32_t>(Index) <= getNumberOfSections()) { | |||
964 | // We already verified the section table data, so no need to check again. | |||
965 | Result = SectionTable + (Index - 1); | |||
966 | return std::error_code(); | |||
967 | } | |||
968 | return object_error::parse_failed; | |||
969 | } | |||
970 | ||||
971 | std::error_code COFFObjectFile::getSection(StringRef SectionName, | |||
972 | const coff_section *&Result) const { | |||
973 | Result = nullptr; | |||
974 | for (const SectionRef &Section : sections()) { | |||
975 | auto NameOrErr = Section.getName(); | |||
976 | if (!NameOrErr) | |||
977 | return errorToErrorCode(NameOrErr.takeError()); | |||
978 | ||||
979 | if (*NameOrErr == SectionName) { | |||
980 | Result = getCOFFSection(Section); | |||
981 | return std::error_code(); | |||
982 | } | |||
983 | } | |||
984 | return object_error::parse_failed; | |||
985 | } | |||
986 | ||||
987 | std::error_code COFFObjectFile::getString(uint32_t Offset, | |||
988 | StringRef &Result) const { | |||
989 | if (StringTableSize <= 4) | |||
990 | // Tried to get a string from an empty string table. | |||
991 | return object_error::parse_failed; | |||
992 | if (Offset >= StringTableSize) | |||
993 | return object_error::unexpected_eof; | |||
994 | Result = StringRef(StringTable + Offset); | |||
995 | return std::error_code(); | |||
996 | } | |||
997 | ||||
998 | std::error_code COFFObjectFile::getSymbolName(COFFSymbolRef Symbol, | |||
999 | StringRef &Res) const { | |||
1000 | return getSymbolName(Symbol.getGeneric(), Res); | |||
1001 | } | |||
1002 | ||||
1003 | std::error_code COFFObjectFile::getSymbolName(const coff_symbol_generic *Symbol, | |||
1004 | StringRef &Res) const { | |||
1005 | // Check for string table entry. First 4 bytes are 0. | |||
1006 | if (Symbol->Name.Offset.Zeroes == 0) { | |||
1007 | if (std::error_code EC = getString(Symbol->Name.Offset.Offset, Res)) | |||
1008 | return EC; | |||
1009 | return std::error_code(); | |||
1010 | } | |||
1011 | ||||
1012 | if (Symbol->Name.ShortName[COFF::NameSize - 1] == 0) | |||
1013 | // Null terminated, let ::strlen figure out the length. | |||
1014 | Res = StringRef(Symbol->Name.ShortName); | |||
1015 | else | |||
1016 | // Not null terminated, use all 8 bytes. | |||
1017 | Res = StringRef(Symbol->Name.ShortName, COFF::NameSize); | |||
1018 | return std::error_code(); | |||
1019 | } | |||
1020 | ||||
1021 | ArrayRef<uint8_t> | |||
1022 | COFFObjectFile::getSymbolAuxData(COFFSymbolRef Symbol) const { | |||
1023 | const uint8_t *Aux = nullptr; | |||
1024 | ||||
1025 | size_t SymbolSize = getSymbolTableEntrySize(); | |||
1026 | if (Symbol.getNumberOfAuxSymbols() > 0) { | |||
1027 | // AUX data comes immediately after the symbol in COFF | |||
1028 | Aux = reinterpret_cast<const uint8_t *>(Symbol.getRawPtr()) + SymbolSize; | |||
1029 | #ifndef NDEBUG | |||
1030 | // Verify that the Aux symbol points to a valid entry in the symbol table. | |||
1031 | uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base()); | |||
1032 | if (Offset < getPointerToSymbolTable() || | |||
1033 | Offset >= | |||
1034 | getPointerToSymbolTable() + (getNumberOfSymbols() * SymbolSize)) | |||
1035 | report_fatal_error("Aux Symbol data was outside of symbol table."); | |||
1036 | ||||
1037 | assert((Offset - getPointerToSymbolTable()) % SymbolSize == 0 &&(((Offset - getPointerToSymbolTable()) % SymbolSize == 0 && "Aux Symbol data did not point to the beginning of a symbol" ) ? static_cast<void> (0) : __assert_fail ("(Offset - getPointerToSymbolTable()) % SymbolSize == 0 && \"Aux Symbol data did not point to the beginning of a symbol\"" , "/build/llvm-toolchain-snapshot-10~svn372306/lib/Object/COFFObjectFile.cpp" , 1038, __PRETTY_FUNCTION__)) | |||
1038 | "Aux Symbol data did not point to the beginning of a symbol")(((Offset - getPointerToSymbolTable()) % SymbolSize == 0 && "Aux Symbol data did not point to the beginning of a symbol" ) ? static_cast<void> (0) : __assert_fail ("(Offset - getPointerToSymbolTable()) % SymbolSize == 0 && \"Aux Symbol data did not point to the beginning of a symbol\"" , "/build/llvm-toolchain-snapshot-10~svn372306/lib/Object/COFFObjectFile.cpp" , 1038, __PRETTY_FUNCTION__)); | |||
1039 | #endif | |||
1040 | } | |||
1041 | return makeArrayRef(Aux, Symbol.getNumberOfAuxSymbols() * SymbolSize); | |||
1042 | } | |||
1043 | ||||
1044 | uint32_t COFFObjectFile::getSymbolIndex(COFFSymbolRef Symbol) const { | |||
1045 | uintptr_t Offset = | |||
1046 | reinterpret_cast<uintptr_t>(Symbol.getRawPtr()) - getSymbolTable(); | |||
1047 | assert(Offset % getSymbolTableEntrySize() == 0 &&((Offset % getSymbolTableEntrySize() == 0 && "Symbol did not point to the beginning of a symbol" ) ? static_cast<void> (0) : __assert_fail ("Offset % getSymbolTableEntrySize() == 0 && \"Symbol did not point to the beginning of a symbol\"" , "/build/llvm-toolchain-snapshot-10~svn372306/lib/Object/COFFObjectFile.cpp" , 1048, __PRETTY_FUNCTION__)) | |||
1048 | "Symbol did not point to the beginning of a symbol")((Offset % getSymbolTableEntrySize() == 0 && "Symbol did not point to the beginning of a symbol" ) ? static_cast<void> (0) : __assert_fail ("Offset % getSymbolTableEntrySize() == 0 && \"Symbol did not point to the beginning of a symbol\"" , "/build/llvm-toolchain-snapshot-10~svn372306/lib/Object/COFFObjectFile.cpp" , 1048, __PRETTY_FUNCTION__)); | |||
1049 | size_t Index = Offset / getSymbolTableEntrySize(); | |||
1050 | assert(Index < getNumberOfSymbols())((Index < getNumberOfSymbols()) ? static_cast<void> ( 0) : __assert_fail ("Index < getNumberOfSymbols()", "/build/llvm-toolchain-snapshot-10~svn372306/lib/Object/COFFObjectFile.cpp" , 1050, __PRETTY_FUNCTION__)); | |||
1051 | return Index; | |||
1052 | } | |||
1053 | ||||
1054 | Expected<StringRef> | |||
1055 | COFFObjectFile::getSectionName(const coff_section *Sec) const { | |||
1056 | StringRef Name; | |||
1057 | if (Sec->Name[COFF::NameSize - 1] == 0) | |||
1058 | // Null terminated, let ::strlen figure out the length. | |||
1059 | Name = Sec->Name; | |||
1060 | else | |||
1061 | // Not null terminated, use all 8 bytes. | |||
1062 | Name = StringRef(Sec->Name, COFF::NameSize); | |||
1063 | ||||
1064 | // Check for string table entry. First byte is '/'. | |||
1065 | if (Name.startswith("/")) { | |||
1066 | uint32_t Offset; | |||
1067 | if (Name.startswith("//")) { | |||
1068 | if (decodeBase64StringEntry(Name.substr(2), Offset)) | |||
1069 | return createStringError(object_error::parse_failed, | |||
1070 | "inalid section name"); | |||
1071 | } else { | |||
1072 | if (Name.substr(1).getAsInteger(10, Offset)) | |||
1073 | return createStringError(object_error::parse_failed, | |||
1074 | "invalid section name"); | |||
1075 | } | |||
1076 | if (std::error_code EC = getString(Offset, Name)) | |||
1077 | return errorCodeToError(EC); | |||
1078 | } | |||
1079 | ||||
1080 | return Name; | |||
1081 | } | |||
1082 | ||||
1083 | uint64_t COFFObjectFile::getSectionSize(const coff_section *Sec) const { | |||
1084 | // SizeOfRawData and VirtualSize change what they represent depending on | |||
1085 | // whether or not we have an executable image. | |||
1086 | // | |||
1087 | // For object files, SizeOfRawData contains the size of section's data; | |||
1088 | // VirtualSize should be zero but isn't due to buggy COFF writers. | |||
1089 | // | |||
1090 | // For executables, SizeOfRawData *must* be a multiple of FileAlignment; the | |||
1091 | // actual section size is in VirtualSize. It is possible for VirtualSize to | |||
1092 | // be greater than SizeOfRawData; the contents past that point should be | |||
1093 | // considered to be zero. | |||
1094 | if (getDOSHeader()) | |||
1095 | return std::min(Sec->VirtualSize, Sec->SizeOfRawData); | |||
1096 | return Sec->SizeOfRawData; | |||
1097 | } | |||
1098 | ||||
1099 | Error COFFObjectFile::getSectionContents(const coff_section *Sec, | |||
1100 | ArrayRef<uint8_t> &Res) const { | |||
1101 | // In COFF, a virtual section won't have any in-file | |||
1102 | // content, so the file pointer to the content will be zero. | |||
1103 | if (Sec->PointerToRawData == 0) | |||
1104 | return Error::success(); | |||
1105 | // The only thing that we need to verify is that the contents is contained | |||
1106 | // within the file bounds. We don't need to make sure it doesn't cover other | |||
1107 | // data, as there's nothing that says that is not allowed. | |||
1108 | uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData; | |||
1109 | uint32_t SectionSize = getSectionSize(Sec); | |||
1110 | if (checkOffset(Data, ConStart, SectionSize)) | |||
1111 | return make_error<BinaryError>(); | |||
1112 | Res = makeArrayRef(reinterpret_cast<const uint8_t *>(ConStart), SectionSize); | |||
1113 | return Error::success(); | |||
1114 | } | |||
1115 | ||||
1116 | const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const { | |||
1117 | return reinterpret_cast<const coff_relocation*>(Rel.p); | |||
1118 | } | |||
1119 | ||||
1120 | void COFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const { | |||
1121 | Rel.p = reinterpret_cast<uintptr_t>( | |||
1122 | reinterpret_cast<const coff_relocation*>(Rel.p) + 1); | |||
1123 | } | |||
1124 | ||||
1125 | uint64_t COFFObjectFile::getRelocationOffset(DataRefImpl Rel) const { | |||
1126 | const coff_relocation *R = toRel(Rel); | |||
1127 | return R->VirtualAddress; | |||
1128 | } | |||
1129 | ||||
1130 | symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const { | |||
1131 | const coff_relocation *R = toRel(Rel); | |||
1132 | DataRefImpl Ref; | |||
1133 | if (R->SymbolTableIndex >= getNumberOfSymbols()) | |||
1134 | return symbol_end(); | |||
1135 | if (SymbolTable16) | |||
1136 | Ref.p = reinterpret_cast<uintptr_t>(SymbolTable16 + R->SymbolTableIndex); | |||
1137 | else if (SymbolTable32) | |||
1138 | Ref.p = reinterpret_cast<uintptr_t>(SymbolTable32 + R->SymbolTableIndex); | |||
1139 | else | |||
1140 | llvm_unreachable("no symbol table pointer!")::llvm::llvm_unreachable_internal("no symbol table pointer!", "/build/llvm-toolchain-snapshot-10~svn372306/lib/Object/COFFObjectFile.cpp" , 1140); | |||
1141 | return symbol_iterator(SymbolRef(Ref, this)); | |||
1142 | } | |||
1143 | ||||
1144 | uint64_t COFFObjectFile::getRelocationType(DataRefImpl Rel) const { | |||
1145 | const coff_relocation* R = toRel(Rel); | |||
1146 | return R->Type; | |||
1147 | } | |||
1148 | ||||
1149 | const coff_section * | |||
1150 | COFFObjectFile::getCOFFSection(const SectionRef &Section) const { | |||
1151 | return toSec(Section.getRawDataRefImpl()); | |||
1152 | } | |||
1153 | ||||
1154 | COFFSymbolRef COFFObjectFile::getCOFFSymbol(const DataRefImpl &Ref) const { | |||
1155 | if (SymbolTable16) | |||
1156 | return toSymb<coff_symbol16>(Ref); | |||
1157 | if (SymbolTable32) | |||
1158 | return toSymb<coff_symbol32>(Ref); | |||
1159 | llvm_unreachable("no symbol table pointer!")::llvm::llvm_unreachable_internal("no symbol table pointer!", "/build/llvm-toolchain-snapshot-10~svn372306/lib/Object/COFFObjectFile.cpp" , 1159); | |||
1160 | } | |||
1161 | ||||
1162 | COFFSymbolRef COFFObjectFile::getCOFFSymbol(const SymbolRef &Symbol) const { | |||
1163 | return getCOFFSymbol(Symbol.getRawDataRefImpl()); | |||
1164 | } | |||
1165 | ||||
1166 | const coff_relocation * | |||
1167 | COFFObjectFile::getCOFFRelocation(const RelocationRef &Reloc) const { | |||
1168 | return toRel(Reloc.getRawDataRefImpl()); | |||
1169 | } | |||
1170 | ||||
1171 | ArrayRef<coff_relocation> | |||
1172 | COFFObjectFile::getRelocations(const coff_section *Sec) const { | |||
1173 | return {getFirstReloc(Sec, Data, base()), | |||
1174 | getNumberOfRelocations(Sec, Data, base())}; | |||
1175 | } | |||
1176 | ||||
1177 | #define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(reloc_type) \ | |||
1178 | case COFF::reloc_type: \ | |||
1179 | return #reloc_type; | |||
1180 | ||||
1181 | StringRef COFFObjectFile::getRelocationTypeName(uint16_t Type) const { | |||
1182 | switch (getMachine()) { | |||
1183 | case COFF::IMAGE_FILE_MACHINE_AMD64: | |||
1184 | switch (Type) { | |||
1185 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE); | |||
1186 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64); | |||
1187 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32); | |||
1188 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB); | |||
1189 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32); | |||
1190 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1); | |||
1191 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2); | |||
1192 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3); | |||
1193 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4); | |||
1194 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5); | |||
1195 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION); | |||
1196 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL); | |||
1197 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7); | |||
1198 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN); | |||
1199 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32); | |||
1200 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR); | |||
1201 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32); | |||
1202 | default: | |||
1203 | return "Unknown"; | |||
1204 | } | |||
1205 | break; | |||
1206 | case COFF::IMAGE_FILE_MACHINE_ARMNT: | |||
1207 | switch (Type) { | |||
1208 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ABSOLUTE); | |||
1209 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32); | |||
1210 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32NB); | |||
1211 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24); | |||
1212 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH11); | |||
1213 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_TOKEN); | |||
1214 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX24); | |||
1215 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX11); | |||
1216 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_REL32); | |||
1217 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECTION); | |||
1218 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECREL); | |||
1219 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32A); | |||
1220 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32T); | |||
1221 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH20T); | |||
1222 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24T); | |||
1223 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX23T); | |||
1224 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_PAIR); | |||
1225 | default: | |||
1226 | return "Unknown"; | |||
1227 | } | |||
1228 | break; | |||
1229 | case COFF::IMAGE_FILE_MACHINE_ARM64: | |||
1230 | switch (Type) { | |||
1231 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_ABSOLUTE); | |||
1232 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_ADDR32); | |||
1233 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_ADDR32NB); | |||
1234 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_BRANCH26); | |||
1235 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_PAGEBASE_REL21); | |||
1236 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_REL21); | |||
1237 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_PAGEOFFSET_12A); | |||
1238 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_PAGEOFFSET_12L); | |||
1239 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_SECREL); | |||
1240 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_SECREL_LOW12A); | |||
1241 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_SECREL_HIGH12A); | |||
1242 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_SECREL_LOW12L); | |||
1243 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_TOKEN); | |||
1244 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_SECTION); | |||
1245 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_ADDR64); | |||
1246 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_BRANCH19); | |||
1247 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_BRANCH14); | |||
1248 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_REL32); | |||
1249 | default: | |||
1250 | return "Unknown"; | |||
1251 | } | |||
1252 | break; | |||
1253 | case COFF::IMAGE_FILE_MACHINE_I386: | |||
1254 | switch (Type) { | |||
1255 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE); | |||
1256 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16); | |||
1257 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16); | |||
1258 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32); | |||
1259 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB); | |||
1260 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12); | |||
1261 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION); | |||
1262 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL); | |||
1263 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN); | |||
1264 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7); | |||
1265 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32); | |||
1266 | default: | |||
1267 | return "Unknown"; | |||
1268 | } | |||
1269 | break; | |||
1270 | default: | |||
1271 | return "Unknown"; | |||
1272 | } | |||
1273 | } | |||
1274 | ||||
1275 | #undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME | |||
1276 | ||||
1277 | void COFFObjectFile::getRelocationTypeName( | |||
1278 | DataRefImpl Rel, SmallVectorImpl<char> &Result) const { | |||
1279 | const coff_relocation *Reloc = toRel(Rel); | |||
1280 | StringRef Res = getRelocationTypeName(Reloc->Type); | |||
1281 | Result.append(Res.begin(), Res.end()); | |||
1282 | } | |||
1283 | ||||
1284 | bool COFFObjectFile::isRelocatableObject() const { | |||
1285 | return !DataDirectory; | |||
1286 | } | |||
1287 | ||||
1288 | StringRef COFFObjectFile::mapDebugSectionName(StringRef Name) const { | |||
1289 | return StringSwitch<StringRef>(Name) | |||
1290 | .Case("eh_fram", "eh_frame") | |||
1291 | .Default(Name); | |||
1292 | } | |||
1293 | ||||
1294 | bool ImportDirectoryEntryRef:: | |||
1295 | operator==(const ImportDirectoryEntryRef &Other) const { | |||
1296 | return ImportTable == Other.ImportTable && Index == Other.Index; | |||
1297 | } | |||
1298 | ||||
1299 | void ImportDirectoryEntryRef::moveNext() { | |||
1300 | ++Index; | |||
1301 | if (ImportTable[Index].isNull()) { | |||
1302 | Index = -1; | |||
1303 | ImportTable = nullptr; | |||
1304 | } | |||
1305 | } | |||
1306 | ||||
1307 | std::error_code ImportDirectoryEntryRef::getImportTableEntry( | |||
1308 | const coff_import_directory_table_entry *&Result) const { | |||
1309 | return getObject(Result, OwningObject->Data, ImportTable + Index); | |||
1310 | } | |||
1311 | ||||
1312 | static imported_symbol_iterator | |||
1313 | makeImportedSymbolIterator(const COFFObjectFile *Object, | |||
1314 | uintptr_t Ptr, int Index) { | |||
1315 | if (Object->getBytesInAddress() == 4) { | |||
1316 | auto *P = reinterpret_cast<const import_lookup_table_entry32 *>(Ptr); | |||
1317 | return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object)); | |||
1318 | } | |||
1319 | auto *P = reinterpret_cast<const import_lookup_table_entry64 *>(Ptr); | |||
1320 | return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object)); | |||
1321 | } | |||
1322 | ||||
1323 | static imported_symbol_iterator | |||
1324 | importedSymbolBegin(uint32_t RVA, const COFFObjectFile *Object) { | |||
1325 | uintptr_t IntPtr = 0; | |||
1326 | Object->getRvaPtr(RVA, IntPtr); | |||
1327 | return makeImportedSymbolIterator(Object, IntPtr, 0); | |||
1328 | } | |||
1329 | ||||
1330 | static imported_symbol_iterator | |||
1331 | importedSymbolEnd(uint32_t RVA, const COFFObjectFile *Object) { | |||
1332 | uintptr_t IntPtr = 0; | |||
1333 | Object->getRvaPtr(RVA, IntPtr); | |||
1334 | // Forward the pointer to the last entry which is null. | |||
1335 | int Index = 0; | |||
1336 | if (Object->getBytesInAddress() == 4) { | |||
1337 | auto *Entry = reinterpret_cast<ulittle32_t *>(IntPtr); | |||
1338 | while (*Entry++) | |||
1339 | ++Index; | |||
1340 | } else { | |||
1341 | auto *Entry = reinterpret_cast<ulittle64_t *>(IntPtr); | |||
1342 | while (*Entry++) | |||
1343 | ++Index; | |||
1344 | } | |||
1345 | return makeImportedSymbolIterator(Object, IntPtr, Index); | |||
1346 | } | |||
1347 | ||||
1348 | imported_symbol_iterator | |||
1349 | ImportDirectoryEntryRef::imported_symbol_begin() const { | |||
1350 | return importedSymbolBegin(ImportTable[Index].ImportAddressTableRVA, | |||
1351 | OwningObject); | |||
1352 | } | |||
1353 | ||||
1354 | imported_symbol_iterator | |||
1355 | ImportDirectoryEntryRef::imported_symbol_end() const { | |||
1356 | return importedSymbolEnd(ImportTable[Index].ImportAddressTableRVA, | |||
1357 | OwningObject); | |||
1358 | } | |||
1359 | ||||
1360 | iterator_range<imported_symbol_iterator> | |||
1361 | ImportDirectoryEntryRef::imported_symbols() const { | |||
1362 | return make_range(imported_symbol_begin(), imported_symbol_end()); | |||
1363 | } | |||
1364 | ||||
1365 | imported_symbol_iterator ImportDirectoryEntryRef::lookup_table_begin() const { | |||
1366 | return importedSymbolBegin(ImportTable[Index].ImportLookupTableRVA, | |||
1367 | OwningObject); | |||
1368 | } | |||
1369 | ||||
1370 | imported_symbol_iterator ImportDirectoryEntryRef::lookup_table_end() const { | |||
1371 | return importedSymbolEnd(ImportTable[Index].ImportLookupTableRVA, | |||
1372 | OwningObject); | |||
1373 | } | |||
1374 | ||||
1375 | iterator_range<imported_symbol_iterator> | |||
1376 | ImportDirectoryEntryRef::lookup_table_symbols() const { | |||
1377 | return make_range(lookup_table_begin(), lookup_table_end()); | |||
1378 | } | |||
1379 | ||||
1380 | std::error_code ImportDirectoryEntryRef::getName(StringRef &Result) const { | |||
1381 | uintptr_t IntPtr = 0; | |||
1382 | if (std::error_code EC = | |||
1383 | OwningObject->getRvaPtr(ImportTable[Index].NameRVA, IntPtr)) | |||
1384 | return EC; | |||
1385 | Result = StringRef(reinterpret_cast<const char *>(IntPtr)); | |||
1386 | return std::error_code(); | |||
1387 | } | |||
1388 | ||||
1389 | std::error_code | |||
1390 | ImportDirectoryEntryRef::getImportLookupTableRVA(uint32_t &Result) const { | |||
1391 | Result = ImportTable[Index].ImportLookupTableRVA; | |||
1392 | return std::error_code(); | |||
1393 | } | |||
1394 | ||||
1395 | std::error_code | |||
1396 | ImportDirectoryEntryRef::getImportAddressTableRVA(uint32_t &Result) const { | |||
1397 | Result = ImportTable[Index].ImportAddressTableRVA; | |||
1398 | return std::error_code(); | |||
1399 | } | |||
1400 | ||||
1401 | bool DelayImportDirectoryEntryRef:: | |||
1402 | operator==(const DelayImportDirectoryEntryRef &Other) const { | |||
1403 | return Table == Other.Table && Index == Other.Index; | |||
1404 | } | |||
1405 | ||||
1406 | void DelayImportDirectoryEntryRef::moveNext() { | |||
1407 | ++Index; | |||
1408 | } | |||
1409 | ||||
1410 | imported_symbol_iterator | |||
1411 | DelayImportDirectoryEntryRef::imported_symbol_begin() const { | |||
1412 | return importedSymbolBegin(Table[Index].DelayImportNameTable, | |||
1413 | OwningObject); | |||
1414 | } | |||
1415 | ||||
1416 | imported_symbol_iterator | |||
1417 | DelayImportDirectoryEntryRef::imported_symbol_end() const { | |||
1418 | return importedSymbolEnd(Table[Index].DelayImportNameTable, | |||
1419 | OwningObject); | |||
1420 | } | |||
1421 | ||||
1422 | iterator_range<imported_symbol_iterator> | |||
1423 | DelayImportDirectoryEntryRef::imported_symbols() const { | |||
1424 | return make_range(imported_symbol_begin(), imported_symbol_end()); | |||
1425 | } | |||
1426 | ||||
1427 | std::error_code DelayImportDirectoryEntryRef::getName(StringRef &Result) const { | |||
1428 | uintptr_t IntPtr = 0; | |||
1429 | if (std::error_code EC = OwningObject->getRvaPtr(Table[Index].Name, IntPtr)) | |||
1430 | return EC; | |||
1431 | Result = StringRef(reinterpret_cast<const char *>(IntPtr)); | |||
1432 | return std::error_code(); | |||
1433 | } | |||
1434 | ||||
1435 | std::error_code DelayImportDirectoryEntryRef:: | |||
1436 | getDelayImportTable(const delay_import_directory_table_entry *&Result) const { | |||
1437 | Result = &Table[Index]; | |||
1438 | return std::error_code(); | |||
1439 | } | |||
1440 | ||||
1441 | std::error_code DelayImportDirectoryEntryRef:: | |||
1442 | getImportAddress(int AddrIndex, uint64_t &Result) const { | |||
1443 | uint32_t RVA = Table[Index].DelayImportAddressTable + | |||
1444 | AddrIndex * (OwningObject->is64() ? 8 : 4); | |||
1445 | uintptr_t IntPtr = 0; | |||
1446 | if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr)) | |||
1447 | return EC; | |||
1448 | if (OwningObject->is64()) | |||
1449 | Result = *reinterpret_cast<const ulittle64_t *>(IntPtr); | |||
1450 | else | |||
1451 | Result = *reinterpret_cast<const ulittle32_t *>(IntPtr); | |||
1452 | return std::error_code(); | |||
1453 | } | |||
1454 | ||||
1455 | bool ExportDirectoryEntryRef:: | |||
1456 | operator==(const ExportDirectoryEntryRef &Other) const { | |||
1457 | return ExportTable == Other.ExportTable && Index == Other.Index; | |||
1458 | } | |||
1459 | ||||
1460 | void ExportDirectoryEntryRef::moveNext() { | |||
1461 | ++Index; | |||
1462 | } | |||
1463 | ||||
1464 | // Returns the name of the current export symbol. If the symbol is exported only | |||
1465 | // by ordinal, the empty string is set as a result. | |||
1466 | std::error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const { | |||
1467 | uintptr_t IntPtr = 0; | |||
1468 | if (std::error_code EC = | |||
1469 | OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr)) | |||
1470 | return EC; | |||
1471 | Result = StringRef(reinterpret_cast<const char *>(IntPtr)); | |||
1472 | return std::error_code(); | |||
1473 | } | |||
1474 | ||||
1475 | // Returns the starting ordinal number. | |||
1476 | std::error_code | |||
1477 | ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const { | |||
1478 | Result = ExportTable->OrdinalBase; | |||
1479 | return std::error_code(); | |||
1480 | } | |||
1481 | ||||
1482 | // Returns the export ordinal of the current export symbol. | |||
1483 | std::error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const { | |||
1484 | Result = ExportTable->OrdinalBase + Index; | |||
1485 | return std::error_code(); | |||
1486 | } | |||
1487 | ||||
1488 | // Returns the address of the current export symbol. | |||
1489 | std::error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const { | |||
1490 | uintptr_t IntPtr = 0; | |||
1491 | if (std::error_code EC = | |||
1492 | OwningObject->getRvaPtr(ExportTable->ExportAddressTableRVA, IntPtr)) | |||
1493 | return EC; | |||
1494 | const export_address_table_entry *entry = | |||
1495 | reinterpret_cast<const export_address_table_entry *>(IntPtr); | |||
1496 | Result = entry[Index].ExportRVA; | |||
1497 | return std::error_code(); | |||
1498 | } | |||
1499 | ||||
1500 | // Returns the name of the current export symbol. If the symbol is exported only | |||
1501 | // by ordinal, the empty string is set as a result. | |||
1502 | std::error_code | |||
1503 | ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const { | |||
1504 | uintptr_t IntPtr = 0; | |||
1505 | if (std::error_code EC = | |||
1506 | OwningObject->getRvaPtr(ExportTable->OrdinalTableRVA, IntPtr)) | |||
1507 | return EC; | |||
1508 | const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr); | |||
1509 | ||||
1510 | uint32_t NumEntries = ExportTable->NumberOfNamePointers; | |||
1511 | int Offset = 0; | |||
1512 | for (const ulittle16_t *I = Start, *E = Start + NumEntries; | |||
1513 | I < E; ++I, ++Offset) { | |||
1514 | if (*I != Index) | |||
1515 | continue; | |||
1516 | if (std::error_code EC = | |||
1517 | OwningObject->getRvaPtr(ExportTable->NamePointerRVA, IntPtr)) | |||
1518 | return EC; | |||
1519 | const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr); | |||
1520 | if (std::error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr)) | |||
1521 | return EC; | |||
1522 | Result = StringRef(reinterpret_cast<const char *>(IntPtr)); | |||
1523 | return std::error_code(); | |||
1524 | } | |||
1525 | Result = ""; | |||
1526 | return std::error_code(); | |||
1527 | } | |||
1528 | ||||
1529 | std::error_code ExportDirectoryEntryRef::isForwarder(bool &Result) const { | |||
1530 | const data_directory *DataEntry; | |||
1531 | if (auto EC = OwningObject->getDataDirectory(COFF::EXPORT_TABLE, DataEntry)) | |||
1532 | return EC; | |||
1533 | uint32_t RVA; | |||
1534 | if (auto EC = getExportRVA(RVA)) | |||
1535 | return EC; | |||
1536 | uint32_t Begin = DataEntry->RelativeVirtualAddress; | |||
1537 | uint32_t End = DataEntry->RelativeVirtualAddress + DataEntry->Size; | |||
1538 | Result = (Begin <= RVA && RVA < End); | |||
1539 | return std::error_code(); | |||
1540 | } | |||
1541 | ||||
1542 | std::error_code ExportDirectoryEntryRef::getForwardTo(StringRef &Result) const { | |||
1543 | uint32_t RVA; | |||
1544 | if (auto EC = getExportRVA(RVA)) | |||
1545 | return EC; | |||
1546 | uintptr_t IntPtr = 0; | |||
1547 | if (auto EC = OwningObject->getRvaPtr(RVA, IntPtr)) | |||
1548 | return EC; | |||
1549 | Result = StringRef(reinterpret_cast<const char *>(IntPtr)); | |||
1550 | return std::error_code(); | |||
1551 | } | |||
1552 | ||||
1553 | bool ImportedSymbolRef:: | |||
1554 | operator==(const ImportedSymbolRef &Other) const { | |||
1555 | return Entry32 == Other.Entry32 && Entry64 == Other.Entry64 | |||
1556 | && Index == Other.Index; | |||
1557 | } | |||
1558 | ||||
1559 | void ImportedSymbolRef::moveNext() { | |||
1560 | ++Index; | |||
1561 | } | |||
1562 | ||||
1563 | std::error_code | |||
1564 | ImportedSymbolRef::getSymbolName(StringRef &Result) const { | |||
1565 | uint32_t RVA; | |||
1566 | if (Entry32) { | |||
1567 | // If a symbol is imported only by ordinal, it has no name. | |||
1568 | if (Entry32[Index].isOrdinal()) | |||
1569 | return std::error_code(); | |||
1570 | RVA = Entry32[Index].getHintNameRVA(); | |||
1571 | } else { | |||
1572 | if (Entry64[Index].isOrdinal()) | |||
1573 | return std::error_code(); | |||
1574 | RVA = Entry64[Index].getHintNameRVA(); | |||
1575 | } | |||
1576 | uintptr_t IntPtr = 0; | |||
1577 | if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr)) | |||
1578 | return EC; | |||
1579 | // +2 because the first two bytes is hint. | |||
1580 | Result = StringRef(reinterpret_cast<const char *>(IntPtr + 2)); | |||
1581 | return std::error_code(); | |||
1582 | } | |||
1583 | ||||
1584 | std::error_code ImportedSymbolRef::isOrdinal(bool &Result) const { | |||
1585 | if (Entry32) | |||
1586 | Result = Entry32[Index].isOrdinal(); | |||
1587 | else | |||
1588 | Result = Entry64[Index].isOrdinal(); | |||
1589 | return std::error_code(); | |||
1590 | } | |||
1591 | ||||
1592 | std::error_code ImportedSymbolRef::getHintNameRVA(uint32_t &Result) const { | |||
1593 | if (Entry32) | |||
1594 | Result = Entry32[Index].getHintNameRVA(); | |||
1595 | else | |||
1596 | Result = Entry64[Index].getHintNameRVA(); | |||
1597 | return std::error_code(); | |||
1598 | } | |||
1599 | ||||
1600 | std::error_code ImportedSymbolRef::getOrdinal(uint16_t &Result) const { | |||
1601 | uint32_t RVA; | |||
1602 | if (Entry32) { | |||
1603 | if (Entry32[Index].isOrdinal()) { | |||
1604 | Result = Entry32[Index].getOrdinal(); | |||
1605 | return std::error_code(); | |||
1606 | } | |||
1607 | RVA = Entry32[Index].getHintNameRVA(); | |||
1608 | } else { | |||
1609 | if (Entry64[Index].isOrdinal()) { | |||
1610 | Result = Entry64[Index].getOrdinal(); | |||
1611 | return std::error_code(); | |||
1612 | } | |||
1613 | RVA = Entry64[Index].getHintNameRVA(); | |||
1614 | } | |||
1615 | uintptr_t IntPtr = 0; | |||
1616 | if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr)) | |||
1617 | return EC; | |||
1618 | Result = *reinterpret_cast<const ulittle16_t *>(IntPtr); | |||
1619 | return std::error_code(); | |||
1620 | } | |||
1621 | ||||
1622 | Expected<std::unique_ptr<COFFObjectFile>> | |||
1623 | ObjectFile::createCOFFObjectFile(MemoryBufferRef Object) { | |||
1624 | std::error_code EC; | |||
1625 | std::unique_ptr<COFFObjectFile> Ret(new COFFObjectFile(Object, EC)); | |||
1626 | if (EC) | |||
1627 | return errorCodeToError(EC); | |||
1628 | return std::move(Ret); | |||
1629 | } | |||
1630 | ||||
1631 | bool BaseRelocRef::operator==(const BaseRelocRef &Other) const { | |||
1632 | return Header == Other.Header && Index == Other.Index; | |||
1633 | } | |||
1634 | ||||
1635 | void BaseRelocRef::moveNext() { | |||
1636 | // Header->BlockSize is the size of the current block, including the | |||
1637 | // size of the header itself. | |||
1638 | uint32_t Size = sizeof(*Header) + | |||
1639 | sizeof(coff_base_reloc_block_entry) * (Index + 1); | |||
1640 | if (Size == Header->BlockSize) { | |||
1641 | // .reloc contains a list of base relocation blocks. Each block | |||
1642 | // consists of the header followed by entries. The header contains | |||
1643 | // how many entories will follow. When we reach the end of the | |||
1644 | // current block, proceed to the next block. | |||
1645 | Header = reinterpret_cast<const coff_base_reloc_block_header *>( | |||
1646 | reinterpret_cast<const uint8_t *>(Header) + Size); | |||
1647 | Index = 0; | |||
1648 | } else { | |||
1649 | ++Index; | |||
1650 | } | |||
1651 | } | |||
1652 | ||||
1653 | std::error_code BaseRelocRef::getType(uint8_t &Type) const { | |||
1654 | auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1); | |||
1655 | Type = Entry[Index].getType(); | |||
1656 | return std::error_code(); | |||
1657 | } | |||
1658 | ||||
1659 | std::error_code BaseRelocRef::getRVA(uint32_t &Result) const { | |||
1660 | auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1); | |||
1661 | Result = Header->PageRVA + Entry[Index].getOffset(); | |||
1662 | return std::error_code(); | |||
1663 | } | |||
1664 | ||||
1665 | #define RETURN_IF_ERROR(Expr)do { Error E = (Expr); if (E) return std::move(E); } while (0 ) \ | |||
1666 | do { \ | |||
1667 | Error E = (Expr); \ | |||
1668 | if (E) \ | |||
1669 | return std::move(E); \ | |||
1670 | } while (0) | |||
1671 | ||||
1672 | Expected<ArrayRef<UTF16>> | |||
1673 | ResourceSectionRef::getDirStringAtOffset(uint32_t Offset) { | |||
1674 | BinaryStreamReader Reader = BinaryStreamReader(BBS); | |||
1675 | Reader.setOffset(Offset); | |||
1676 | uint16_t Length; | |||
1677 | RETURN_IF_ERROR(Reader.readInteger(Length))do { Error E = (Reader.readInteger(Length)); if (E) return std ::move(E); } while (0); | |||
1678 | ArrayRef<UTF16> RawDirString; | |||
1679 | RETURN_IF_ERROR(Reader.readArray(RawDirString, Length))do { Error E = (Reader.readArray(RawDirString, Length)); if ( E) return std::move(E); } while (0); | |||
1680 | return RawDirString; | |||
1681 | } | |||
1682 | ||||
1683 | Expected<ArrayRef<UTF16>> | |||
1684 | ResourceSectionRef::getEntryNameString(const coff_resource_dir_entry &Entry) { | |||
1685 | return getDirStringAtOffset(Entry.Identifier.getNameOffset()); | |||
1686 | } | |||
1687 | ||||
1688 | Expected<const coff_resource_dir_table &> | |||
1689 | ResourceSectionRef::getTableAtOffset(uint32_t Offset) { | |||
1690 | const coff_resource_dir_table *Table = nullptr; | |||
1691 | ||||
1692 | BinaryStreamReader Reader(BBS); | |||
1693 | Reader.setOffset(Offset); | |||
1694 | RETURN_IF_ERROR(Reader.readObject(Table))do { Error E = (Reader.readObject(Table)); if (E) return std:: move(E); } while (0); | |||
1695 | assert(Table != nullptr)((Table != nullptr) ? static_cast<void> (0) : __assert_fail ("Table != nullptr", "/build/llvm-toolchain-snapshot-10~svn372306/lib/Object/COFFObjectFile.cpp" , 1695, __PRETTY_FUNCTION__)); | |||
1696 | return *Table; | |||
1697 | } | |||
1698 | ||||
1699 | Expected<const coff_resource_dir_entry &> | |||
1700 | ResourceSectionRef::getTableEntryAtOffset(uint32_t Offset) { | |||
1701 | const coff_resource_dir_entry *Entry = nullptr; | |||
1702 | ||||
1703 | BinaryStreamReader Reader(BBS); | |||
1704 | Reader.setOffset(Offset); | |||
1705 | RETURN_IF_ERROR(Reader.readObject(Entry))do { Error E = (Reader.readObject(Entry)); if (E) return std:: move(E); } while (0); | |||
1706 | assert(Entry != nullptr)((Entry != nullptr) ? static_cast<void> (0) : __assert_fail ("Entry != nullptr", "/build/llvm-toolchain-snapshot-10~svn372306/lib/Object/COFFObjectFile.cpp" , 1706, __PRETTY_FUNCTION__)); | |||
1707 | return *Entry; | |||
1708 | } | |||
1709 | ||||
1710 | Expected<const coff_resource_data_entry &> | |||
1711 | ResourceSectionRef::getDataEntryAtOffset(uint32_t Offset) { | |||
1712 | const coff_resource_data_entry *Entry = nullptr; | |||
1713 | ||||
1714 | BinaryStreamReader Reader(BBS); | |||
1715 | Reader.setOffset(Offset); | |||
1716 | RETURN_IF_ERROR(Reader.readObject(Entry))do { Error E = (Reader.readObject(Entry)); if (E) return std:: move(E); } while (0); | |||
1717 | assert(Entry != nullptr)((Entry != nullptr) ? static_cast<void> (0) : __assert_fail ("Entry != nullptr", "/build/llvm-toolchain-snapshot-10~svn372306/lib/Object/COFFObjectFile.cpp" , 1717, __PRETTY_FUNCTION__)); | |||
1718 | return *Entry; | |||
1719 | } | |||
1720 | ||||
1721 | Expected<const coff_resource_dir_table &> | |||
1722 | ResourceSectionRef::getEntrySubDir(const coff_resource_dir_entry &Entry) { | |||
1723 | assert(Entry.Offset.isSubDir())((Entry.Offset.isSubDir()) ? static_cast<void> (0) : __assert_fail ("Entry.Offset.isSubDir()", "/build/llvm-toolchain-snapshot-10~svn372306/lib/Object/COFFObjectFile.cpp" , 1723, __PRETTY_FUNCTION__)); | |||
1724 | return getTableAtOffset(Entry.Offset.value()); | |||
1725 | } | |||
1726 | ||||
1727 | Expected<const coff_resource_data_entry &> | |||
1728 | ResourceSectionRef::getEntryData(const coff_resource_dir_entry &Entry) { | |||
1729 | assert(!Entry.Offset.isSubDir())((!Entry.Offset.isSubDir()) ? static_cast<void> (0) : __assert_fail ("!Entry.Offset.isSubDir()", "/build/llvm-toolchain-snapshot-10~svn372306/lib/Object/COFFObjectFile.cpp" , 1729, __PRETTY_FUNCTION__)); | |||
1730 | return getDataEntryAtOffset(Entry.Offset.value()); | |||
1731 | } | |||
1732 | ||||
1733 | Expected<const coff_resource_dir_table &> ResourceSectionRef::getBaseTable() { | |||
1734 | return getTableAtOffset(0); | |||
1735 | } | |||
1736 | ||||
1737 | Expected<const coff_resource_dir_entry &> | |||
1738 | ResourceSectionRef::getTableEntry(const coff_resource_dir_table &Table, | |||
1739 | uint32_t Index) { | |||
1740 | if (Index >= (uint32_t)(Table.NumberOfNameEntries + Table.NumberOfIDEntries)) | |||
1741 | return createStringError(object_error::parse_failed, "index out of range"); | |||
1742 | const uint8_t *TablePtr = reinterpret_cast<const uint8_t *>(&Table); | |||
1743 | ptrdiff_t TableOffset = TablePtr - BBS.data().data(); | |||
1744 | return getTableEntryAtOffset(TableOffset + sizeof(Table) + | |||
1745 | Index * sizeof(coff_resource_dir_entry)); | |||
1746 | } | |||
1747 | ||||
1748 | Error ResourceSectionRef::load(const COFFObjectFile *O) { | |||
1749 | for (const SectionRef &S : O->sections()) { | |||
1750 | Expected<StringRef> Name = S.getName(); | |||
1751 | if (!Name) | |||
1752 | return Name.takeError(); | |||
1753 | ||||
1754 | if (*Name == ".rsrc" || *Name == ".rsrc$01") | |||
1755 | return load(O, S); | |||
1756 | } | |||
1757 | return createStringError(object_error::parse_failed, | |||
1758 | "no resource section found"); | |||
1759 | } | |||
1760 | ||||
1761 | Error ResourceSectionRef::load(const COFFObjectFile *O, const SectionRef &S) { | |||
1762 | Obj = O; | |||
1763 | Section = S; | |||
1764 | Expected<StringRef> Contents = Section.getContents(); | |||
1765 | if (!Contents) | |||
1766 | return Contents.takeError(); | |||
1767 | BBS = BinaryByteStream(*Contents, support::little); | |||
1768 | const coff_section *COFFSect = Obj->getCOFFSection(Section); | |||
1769 | ArrayRef<coff_relocation> OrigRelocs = Obj->getRelocations(COFFSect); | |||
1770 | Relocs.reserve(OrigRelocs.size()); | |||
1771 | for (const coff_relocation &R : OrigRelocs) | |||
1772 | Relocs.push_back(&R); | |||
1773 | std::sort(Relocs.begin(), Relocs.end(), | |||
1774 | [](const coff_relocation *A, const coff_relocation *B) { | |||
1775 | return A->VirtualAddress < B->VirtualAddress; | |||
1776 | }); | |||
1777 | return Error::success(); | |||
1778 | } | |||
1779 | ||||
1780 | Expected<StringRef> | |||
1781 | ResourceSectionRef::getContents(const coff_resource_data_entry &Entry) { | |||
1782 | if (!Obj) | |||
1783 | return createStringError(object_error::parse_failed, "no object provided"); | |||
1784 | ||||
1785 | // Find a potential relocation at the DataRVA field (first member of | |||
1786 | // the coff_resource_data_entry struct). | |||
1787 | const uint8_t *EntryPtr = reinterpret_cast<const uint8_t *>(&Entry); | |||
1788 | ptrdiff_t EntryOffset = EntryPtr - BBS.data().data(); | |||
1789 | coff_relocation RelocTarget{ulittle32_t(EntryOffset), ulittle32_t(0), | |||
1790 | ulittle16_t(0)}; | |||
1791 | auto RelocsForOffset = | |||
1792 | std::equal_range(Relocs.begin(), Relocs.end(), &RelocTarget, | |||
1793 | [](const coff_relocation *A, const coff_relocation *B) { | |||
1794 | return A->VirtualAddress < B->VirtualAddress; | |||
1795 | }); | |||
1796 | ||||
1797 | if (RelocsForOffset.first != RelocsForOffset.second) { | |||
1798 | // We found a relocation with the right offset. Check that it does have | |||
1799 | // the expected type. | |||
1800 | const coff_relocation &R = **RelocsForOffset.first; | |||
1801 | uint16_t RVAReloc; | |||
1802 | switch (Obj->getMachine()) { | |||
1803 | case COFF::IMAGE_FILE_MACHINE_I386: | |||
1804 | RVAReloc = COFF::IMAGE_REL_I386_DIR32NB; | |||
1805 | break; | |||
1806 | case COFF::IMAGE_FILE_MACHINE_AMD64: | |||
1807 | RVAReloc = COFF::IMAGE_REL_AMD64_ADDR32NB; | |||
1808 | break; | |||
1809 | case COFF::IMAGE_FILE_MACHINE_ARMNT: | |||
1810 | RVAReloc = COFF::IMAGE_REL_ARM_ADDR32NB; | |||
1811 | break; | |||
1812 | case COFF::IMAGE_FILE_MACHINE_ARM64: | |||
1813 | RVAReloc = COFF::IMAGE_REL_ARM64_ADDR32NB; | |||
1814 | break; | |||
1815 | default: | |||
1816 | return createStringError(object_error::parse_failed, | |||
1817 | "unsupported architecture"); | |||
1818 | } | |||
1819 | if (R.Type != RVAReloc) | |||
1820 | return createStringError(object_error::parse_failed, | |||
1821 | "unexpected relocation type"); | |||
1822 | // Get the relocation's symbol | |||
1823 | Expected<COFFSymbolRef> Sym = Obj->getSymbol(R.SymbolTableIndex); | |||
1824 | if (!Sym) | |||
1825 | return Sym.takeError(); | |||
1826 | const coff_section *Section = nullptr; | |||
1827 | // And the symbol's section | |||
1828 | if (std::error_code EC = Obj->getSection(Sym->getSectionNumber(), Section)) | |||
1829 | return errorCodeToError(EC); | |||
1830 | // Add the initial value of DataRVA to the symbol's offset to find the | |||
1831 | // data it points at. | |||
1832 | uint64_t Offset = Entry.DataRVA + Sym->getValue(); | |||
1833 | ArrayRef<uint8_t> Contents; | |||
1834 | if (Error E = Obj->getSectionContents(Section, Contents)) | |||
1835 | return std::move(E); | |||
1836 | if (Offset + Entry.DataSize > Contents.size()) | |||
1837 | return createStringError(object_error::parse_failed, | |||
1838 | "data outside of section"); | |||
1839 | // Return a reference to the data inside the section. | |||
1840 | return StringRef(reinterpret_cast<const char *>(Contents.data()) + Offset, | |||
1841 | Entry.DataSize); | |||
1842 | } else { | |||
1843 | // Relocatable objects need a relocation for the DataRVA field. | |||
1844 | if (Obj->isRelocatableObject()) | |||
1845 | return createStringError(object_error::parse_failed, | |||
1846 | "no relocation found for DataRVA"); | |||
1847 | ||||
1848 | // Locate the section that contains the address that DataRVA points at. | |||
1849 | uint64_t VA = Entry.DataRVA + Obj->getImageBase(); | |||
1850 | for (const SectionRef &S : Obj->sections()) { | |||
1851 | if (VA >= S.getAddress() && | |||
1852 | VA + Entry.DataSize <= S.getAddress() + S.getSize()) { | |||
1853 | uint64_t Offset = VA - S.getAddress(); | |||
1854 | Expected<StringRef> Contents = S.getContents(); | |||
1855 | if (!Contents) | |||
1856 | return Contents.takeError(); | |||
1857 | return Contents->slice(Offset, Offset + Entry.DataSize); | |||
1858 | } | |||
1859 | } | |||
1860 | return createStringError(object_error::parse_failed, | |||
1861 | "address not found in image"); | |||
1862 | } | |||
1863 | } |
1 | //===- COFF.h - COFF object file implementation -----------------*- C++ -*-===// | |||
2 | // | |||
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | |||
4 | // See https://llvm.org/LICENSE.txt for license information. | |||
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | |||
6 | // | |||
7 | //===----------------------------------------------------------------------===// | |||
8 | // | |||
9 | // This file declares the COFFObjectFile class. | |||
10 | // | |||
11 | //===----------------------------------------------------------------------===// | |||
12 | ||||
13 | #ifndef LLVM_OBJECT_COFF_H | |||
14 | #define LLVM_OBJECT_COFF_H | |||
15 | ||||
16 | #include "llvm/ADT/iterator_range.h" | |||
17 | #include "llvm/BinaryFormat/COFF.h" | |||
18 | #include "llvm/MC/SubtargetFeature.h" | |||
19 | #include "llvm/Object/Binary.h" | |||
20 | #include "llvm/Object/CVDebugRecord.h" | |||
21 | #include "llvm/Object/Error.h" | |||
22 | #include "llvm/Object/ObjectFile.h" | |||
23 | #include "llvm/Support/BinaryByteStream.h" | |||
24 | #include "llvm/Support/ConvertUTF.h" | |||
25 | #include "llvm/Support/Endian.h" | |||
26 | #include "llvm/Support/ErrorHandling.h" | |||
27 | #include <cassert> | |||
28 | #include <cstddef> | |||
29 | #include <cstdint> | |||
30 | #include <system_error> | |||
31 | ||||
32 | namespace llvm { | |||
33 | ||||
34 | template <typename T> class ArrayRef; | |||
35 | ||||
36 | namespace object { | |||
37 | ||||
38 | class BaseRelocRef; | |||
39 | class DelayImportDirectoryEntryRef; | |||
40 | class ExportDirectoryEntryRef; | |||
41 | class ImportDirectoryEntryRef; | |||
42 | class ImportedSymbolRef; | |||
43 | class ResourceSectionRef; | |||
44 | ||||
45 | using import_directory_iterator = content_iterator<ImportDirectoryEntryRef>; | |||
46 | using delay_import_directory_iterator = | |||
47 | content_iterator<DelayImportDirectoryEntryRef>; | |||
48 | using export_directory_iterator = content_iterator<ExportDirectoryEntryRef>; | |||
49 | using imported_symbol_iterator = content_iterator<ImportedSymbolRef>; | |||
50 | using base_reloc_iterator = content_iterator<BaseRelocRef>; | |||
51 | ||||
52 | /// The DOS compatible header at the front of all PE/COFF executables. | |||
53 | struct dos_header { | |||
54 | char Magic[2]; | |||
55 | support::ulittle16_t UsedBytesInTheLastPage; | |||
56 | support::ulittle16_t FileSizeInPages; | |||
57 | support::ulittle16_t NumberOfRelocationItems; | |||
58 | support::ulittle16_t HeaderSizeInParagraphs; | |||
59 | support::ulittle16_t MinimumExtraParagraphs; | |||
60 | support::ulittle16_t MaximumExtraParagraphs; | |||
61 | support::ulittle16_t InitialRelativeSS; | |||
62 | support::ulittle16_t InitialSP; | |||
63 | support::ulittle16_t Checksum; | |||
64 | support::ulittle16_t InitialIP; | |||
65 | support::ulittle16_t InitialRelativeCS; | |||
66 | support::ulittle16_t AddressOfRelocationTable; | |||
67 | support::ulittle16_t OverlayNumber; | |||
68 | support::ulittle16_t Reserved[4]; | |||
69 | support::ulittle16_t OEMid; | |||
70 | support::ulittle16_t OEMinfo; | |||
71 | support::ulittle16_t Reserved2[10]; | |||
72 | support::ulittle32_t AddressOfNewExeHeader; | |||
73 | }; | |||
74 | ||||
75 | struct coff_file_header { | |||
76 | support::ulittle16_t Machine; | |||
77 | support::ulittle16_t NumberOfSections; | |||
78 | support::ulittle32_t TimeDateStamp; | |||
79 | support::ulittle32_t PointerToSymbolTable; | |||
80 | support::ulittle32_t NumberOfSymbols; | |||
81 | support::ulittle16_t SizeOfOptionalHeader; | |||
82 | support::ulittle16_t Characteristics; | |||
83 | ||||
84 | bool isImportLibrary() const { return NumberOfSections == 0xffff; } | |||
85 | }; | |||
86 | ||||
87 | struct coff_bigobj_file_header { | |||
88 | support::ulittle16_t Sig1; | |||
89 | support::ulittle16_t Sig2; | |||
90 | support::ulittle16_t Version; | |||
91 | support::ulittle16_t Machine; | |||
92 | support::ulittle32_t TimeDateStamp; | |||
93 | uint8_t UUID[16]; | |||
94 | support::ulittle32_t unused1; | |||
95 | support::ulittle32_t unused2; | |||
96 | support::ulittle32_t unused3; | |||
97 | support::ulittle32_t unused4; | |||
98 | support::ulittle32_t NumberOfSections; | |||
99 | support::ulittle32_t PointerToSymbolTable; | |||
100 | support::ulittle32_t NumberOfSymbols; | |||
101 | }; | |||
102 | ||||
103 | /// The 32-bit PE header that follows the COFF header. | |||
104 | struct pe32_header { | |||
105 | support::ulittle16_t Magic; | |||
106 | uint8_t MajorLinkerVersion; | |||
107 | uint8_t MinorLinkerVersion; | |||
108 | support::ulittle32_t SizeOfCode; | |||
109 | support::ulittle32_t SizeOfInitializedData; | |||
110 | support::ulittle32_t SizeOfUninitializedData; | |||
111 | support::ulittle32_t AddressOfEntryPoint; | |||
112 | support::ulittle32_t BaseOfCode; | |||
113 | support::ulittle32_t BaseOfData; | |||
114 | support::ulittle32_t ImageBase; | |||
115 | support::ulittle32_t SectionAlignment; | |||
116 | support::ulittle32_t FileAlignment; | |||
117 | support::ulittle16_t MajorOperatingSystemVersion; | |||
118 | support::ulittle16_t MinorOperatingSystemVersion; | |||
119 | support::ulittle16_t MajorImageVersion; | |||
120 | support::ulittle16_t MinorImageVersion; | |||
121 | support::ulittle16_t MajorSubsystemVersion; | |||
122 | support::ulittle16_t MinorSubsystemVersion; | |||
123 | support::ulittle32_t Win32VersionValue; | |||
124 | support::ulittle32_t SizeOfImage; | |||
125 | support::ulittle32_t SizeOfHeaders; | |||
126 | support::ulittle32_t CheckSum; | |||
127 | support::ulittle16_t Subsystem; | |||
128 | // FIXME: This should be DllCharacteristics. | |||
129 | support::ulittle16_t DLLCharacteristics; | |||
130 | support::ulittle32_t SizeOfStackReserve; | |||
131 | support::ulittle32_t SizeOfStackCommit; | |||
132 | support::ulittle32_t SizeOfHeapReserve; | |||
133 | support::ulittle32_t SizeOfHeapCommit; | |||
134 | support::ulittle32_t LoaderFlags; | |||
135 | // FIXME: This should be NumberOfRvaAndSizes. | |||
136 | support::ulittle32_t NumberOfRvaAndSize; | |||
137 | }; | |||
138 | ||||
139 | /// The 64-bit PE header that follows the COFF header. | |||
140 | struct pe32plus_header { | |||
141 | support::ulittle16_t Magic; | |||
142 | uint8_t MajorLinkerVersion; | |||
143 | uint8_t MinorLinkerVersion; | |||
144 | support::ulittle32_t SizeOfCode; | |||
145 | support::ulittle32_t SizeOfInitializedData; | |||
146 | support::ulittle32_t SizeOfUninitializedData; | |||
147 | support::ulittle32_t AddressOfEntryPoint; | |||
148 | support::ulittle32_t BaseOfCode; | |||
149 | support::ulittle64_t ImageBase; | |||
150 | support::ulittle32_t SectionAlignment; | |||
151 | support::ulittle32_t FileAlignment; | |||
152 | support::ulittle16_t MajorOperatingSystemVersion; | |||
153 | support::ulittle16_t MinorOperatingSystemVersion; | |||
154 | support::ulittle16_t MajorImageVersion; | |||
155 | support::ulittle16_t MinorImageVersion; | |||
156 | support::ulittle16_t MajorSubsystemVersion; | |||
157 | support::ulittle16_t MinorSubsystemVersion; | |||
158 | support::ulittle32_t Win32VersionValue; | |||
159 | support::ulittle32_t SizeOfImage; | |||
160 | support::ulittle32_t SizeOfHeaders; | |||
161 | support::ulittle32_t CheckSum; | |||
162 | support::ulittle16_t Subsystem; | |||
163 | support::ulittle16_t DLLCharacteristics; | |||
164 | support::ulittle64_t SizeOfStackReserve; | |||
165 | support::ulittle64_t SizeOfStackCommit; | |||
166 | support::ulittle64_t SizeOfHeapReserve; | |||
167 | support::ulittle64_t SizeOfHeapCommit; | |||
168 | support::ulittle32_t LoaderFlags; | |||
169 | support::ulittle32_t NumberOfRvaAndSize; | |||
170 | }; | |||
171 | ||||
172 | struct data_directory { | |||
173 | support::ulittle32_t RelativeVirtualAddress; | |||
174 | support::ulittle32_t Size; | |||
175 | }; | |||
176 | ||||
177 | struct debug_directory { | |||
178 | support::ulittle32_t Characteristics; | |||
179 | support::ulittle32_t TimeDateStamp; | |||
180 | support::ulittle16_t MajorVersion; | |||
181 | support::ulittle16_t MinorVersion; | |||
182 | support::ulittle32_t Type; | |||
183 | support::ulittle32_t SizeOfData; | |||
184 | support::ulittle32_t AddressOfRawData; | |||
185 | support::ulittle32_t PointerToRawData; | |||
186 | }; | |||
187 | ||||
188 | template <typename IntTy> | |||
189 | struct import_lookup_table_entry { | |||
190 | IntTy Data; | |||
191 | ||||
192 | bool isOrdinal() const { return Data < 0; } | |||
193 | ||||
194 | uint16_t getOrdinal() const { | |||
195 | assert(isOrdinal() && "ILT entry is not an ordinal!")((isOrdinal() && "ILT entry is not an ordinal!") ? static_cast <void> (0) : __assert_fail ("isOrdinal() && \"ILT entry is not an ordinal!\"" , "/build/llvm-toolchain-snapshot-10~svn372306/include/llvm/Object/COFF.h" , 195, __PRETTY_FUNCTION__)); | |||
196 | return Data & 0xFFFF; | |||
197 | } | |||
198 | ||||
199 | uint32_t getHintNameRVA() const { | |||
200 | assert(!isOrdinal() && "ILT entry is not a Hint/Name RVA!")((!isOrdinal() && "ILT entry is not a Hint/Name RVA!" ) ? static_cast<void> (0) : __assert_fail ("!isOrdinal() && \"ILT entry is not a Hint/Name RVA!\"" , "/build/llvm-toolchain-snapshot-10~svn372306/include/llvm/Object/COFF.h" , 200, __PRETTY_FUNCTION__)); | |||
201 | return Data & 0xFFFFFFFF; | |||
202 | } | |||
203 | }; | |||
204 | ||||
205 | using import_lookup_table_entry32 = | |||
206 | import_lookup_table_entry<support::little32_t>; | |||
207 | using import_lookup_table_entry64 = | |||
208 | import_lookup_table_entry<support::little64_t>; | |||
209 | ||||
210 | struct delay_import_directory_table_entry { | |||
211 | // dumpbin reports this field as "Characteristics" instead of "Attributes". | |||
212 | support::ulittle32_t Attributes; | |||
213 | support::ulittle32_t Name; | |||
214 | support::ulittle32_t ModuleHandle; | |||
215 | support::ulittle32_t DelayImportAddressTable; | |||
216 | support::ulittle32_t DelayImportNameTable; | |||
217 | support::ulittle32_t BoundDelayImportTable; | |||
218 | support::ulittle32_t UnloadDelayImportTable; | |||
219 | support::ulittle32_t TimeStamp; | |||
220 | }; | |||
221 | ||||
222 | struct export_directory_table_entry { | |||
223 | support::ulittle32_t ExportFlags; | |||
224 | support::ulittle32_t TimeDateStamp; | |||
225 | support::ulittle16_t MajorVersion; | |||
226 | support::ulittle16_t MinorVersion; | |||
227 | support::ulittle32_t NameRVA; | |||
228 | support::ulittle32_t OrdinalBase; | |||
229 | support::ulittle32_t AddressTableEntries; | |||
230 | support::ulittle32_t NumberOfNamePointers; | |||
231 | support::ulittle32_t ExportAddressTableRVA; | |||
232 | support::ulittle32_t NamePointerRVA; | |||
233 | support::ulittle32_t OrdinalTableRVA; | |||
234 | }; | |||
235 | ||||
236 | union export_address_table_entry { | |||
237 | support::ulittle32_t ExportRVA; | |||
238 | support::ulittle32_t ForwarderRVA; | |||
239 | }; | |||
240 | ||||
241 | using export_name_pointer_table_entry = support::ulittle32_t; | |||
242 | using export_ordinal_table_entry = support::ulittle16_t; | |||
243 | ||||
244 | struct StringTableOffset { | |||
245 | support::ulittle32_t Zeroes; | |||
246 | support::ulittle32_t Offset; | |||
247 | }; | |||
248 | ||||
249 | template <typename SectionNumberType> | |||
250 | struct coff_symbol { | |||
251 | union { | |||
252 | char ShortName[COFF::NameSize]; | |||
253 | StringTableOffset Offset; | |||
254 | } Name; | |||
255 | ||||
256 | support::ulittle32_t Value; | |||
257 | SectionNumberType SectionNumber; | |||
258 | ||||
259 | support::ulittle16_t Type; | |||
260 | ||||
261 | uint8_t StorageClass; | |||
262 | uint8_t NumberOfAuxSymbols; | |||
263 | }; | |||
264 | ||||
265 | using coff_symbol16 = coff_symbol<support::ulittle16_t>; | |||
266 | using coff_symbol32 = coff_symbol<support::ulittle32_t>; | |||
267 | ||||
268 | // Contains only common parts of coff_symbol16 and coff_symbol32. | |||
269 | struct coff_symbol_generic { | |||
270 | union { | |||
271 | char ShortName[COFF::NameSize]; | |||
272 | StringTableOffset Offset; | |||
273 | } Name; | |||
274 | support::ulittle32_t Value; | |||
275 | }; | |||
276 | ||||
277 | struct coff_aux_section_definition; | |||
278 | struct coff_aux_weak_external; | |||
279 | ||||
280 | class COFFSymbolRef { | |||
281 | public: | |||
282 | COFFSymbolRef() = default; | |||
283 | COFFSymbolRef(const coff_symbol16 *CS) : CS16(CS) {} | |||
284 | COFFSymbolRef(const coff_symbol32 *CS) : CS32(CS) {} | |||
285 | ||||
286 | const void *getRawPtr() const { | |||
287 | return CS16 ? static_cast<const void *>(CS16) : CS32; | |||
288 | } | |||
289 | ||||
290 | const coff_symbol_generic *getGeneric() const { | |||
291 | if (CS16) | |||
292 | return reinterpret_cast<const coff_symbol_generic *>(CS16); | |||
293 | return reinterpret_cast<const coff_symbol_generic *>(CS32); | |||
294 | } | |||
295 | ||||
296 | friend bool operator<(COFFSymbolRef A, COFFSymbolRef B) { | |||
297 | return A.getRawPtr() < B.getRawPtr(); | |||
298 | } | |||
299 | ||||
300 | bool isBigObj() const { | |||
301 | if (CS16) | |||
302 | return false; | |||
303 | if (CS32) | |||
304 | return true; | |||
305 | llvm_unreachable("COFFSymbolRef points to nothing!")::llvm::llvm_unreachable_internal("COFFSymbolRef points to nothing!" , "/build/llvm-toolchain-snapshot-10~svn372306/include/llvm/Object/COFF.h" , 305); | |||
306 | } | |||
307 | ||||
308 | const char *getShortName() const { | |||
309 | return CS16 ? CS16->Name.ShortName : CS32->Name.ShortName; | |||
310 | } | |||
311 | ||||
312 | const StringTableOffset &getStringTableOffset() const { | |||
313 | assert(isSet() && "COFFSymbolRef points to nothing!")((isSet() && "COFFSymbolRef points to nothing!") ? static_cast <void> (0) : __assert_fail ("isSet() && \"COFFSymbolRef points to nothing!\"" , "/build/llvm-toolchain-snapshot-10~svn372306/include/llvm/Object/COFF.h" , 313, __PRETTY_FUNCTION__)); | |||
314 | return CS16 ? CS16->Name.Offset : CS32->Name.Offset; | |||
315 | } | |||
316 | ||||
317 | uint32_t getValue() const { return CS16 ? CS16->Value : CS32->Value; } | |||
| ||||
318 | ||||
319 | int32_t getSectionNumber() const { | |||
320 | assert(isSet() && "COFFSymbolRef points to nothing!")((isSet() && "COFFSymbolRef points to nothing!") ? static_cast <void> (0) : __assert_fail ("isSet() && \"COFFSymbolRef points to nothing!\"" , "/build/llvm-toolchain-snapshot-10~svn372306/include/llvm/Object/COFF.h" , 320, __PRETTY_FUNCTION__)); | |||
321 | if (CS16) { | |||
322 | // Reserved sections are returned as negative numbers. | |||
323 | if (CS16->SectionNumber <= COFF::MaxNumberOfSections16) | |||
324 | return CS16->SectionNumber; | |||
325 | return static_cast<int16_t>(CS16->SectionNumber); | |||
326 | } | |||
327 | return static_cast<int32_t>(CS32->SectionNumber); | |||
328 | } | |||
329 | ||||
330 | uint16_t getType() const { | |||
331 | assert(isSet() && "COFFSymbolRef points to nothing!")((isSet() && "COFFSymbolRef points to nothing!") ? static_cast <void> (0) : __assert_fail ("isSet() && \"COFFSymbolRef points to nothing!\"" , "/build/llvm-toolchain-snapshot-10~svn372306/include/llvm/Object/COFF.h" , 331, __PRETTY_FUNCTION__)); | |||
332 | return CS16 ? CS16->Type : CS32->Type; | |||
333 | } | |||
334 | ||||
335 | uint8_t getStorageClass() const { | |||
336 | assert(isSet() && "COFFSymbolRef points to nothing!")((isSet() && "COFFSymbolRef points to nothing!") ? static_cast <void> (0) : __assert_fail ("isSet() && \"COFFSymbolRef points to nothing!\"" , "/build/llvm-toolchain-snapshot-10~svn372306/include/llvm/Object/COFF.h" , 336, __PRETTY_FUNCTION__)); | |||
337 | return CS16 ? CS16->StorageClass : CS32->StorageClass; | |||
338 | } | |||
339 | ||||
340 | uint8_t getNumberOfAuxSymbols() const { | |||
341 | assert(isSet() && "COFFSymbolRef points to nothing!")((isSet() && "COFFSymbolRef points to nothing!") ? static_cast <void> (0) : __assert_fail ("isSet() && \"COFFSymbolRef points to nothing!\"" , "/build/llvm-toolchain-snapshot-10~svn372306/include/llvm/Object/COFF.h" , 341, __PRETTY_FUNCTION__)); | |||
342 | return CS16 ? CS16->NumberOfAuxSymbols : CS32->NumberOfAuxSymbols; | |||
343 | } | |||
344 | ||||
345 | uint8_t getBaseType() const { return getType() & 0x0F; } | |||
346 | ||||
347 | uint8_t getComplexType() const { | |||
348 | return (getType() & 0xF0) >> COFF::SCT_COMPLEX_TYPE_SHIFT; | |||
349 | } | |||
350 | ||||
351 | template <typename T> const T *getAux() const { | |||
352 | return CS16 ? reinterpret_cast<const T *>(CS16 + 1) | |||
353 | : reinterpret_cast<const T *>(CS32 + 1); | |||
354 | } | |||
355 | ||||
356 | const coff_aux_section_definition *getSectionDefinition() const { | |||
357 | if (!getNumberOfAuxSymbols() || | |||
358 | getStorageClass() != COFF::IMAGE_SYM_CLASS_STATIC) | |||
359 | return nullptr; | |||
360 | return getAux<coff_aux_section_definition>(); | |||
361 | } | |||
362 | ||||
363 | const coff_aux_weak_external *getWeakExternal() const { | |||
364 | if (!getNumberOfAuxSymbols() || | |||
365 | getStorageClass() != COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL) | |||
366 | return nullptr; | |||
367 | return getAux<coff_aux_weak_external>(); | |||
368 | } | |||
369 | ||||
370 | bool isAbsolute() const { | |||
371 | return getSectionNumber() == -1; | |||
372 | } | |||
373 | ||||
374 | bool isExternal() const { | |||
375 | return getStorageClass() == COFF::IMAGE_SYM_CLASS_EXTERNAL; | |||
376 | } | |||
377 | ||||
378 | bool isCommon() const { | |||
379 | return isExternal() && getSectionNumber() == COFF::IMAGE_SYM_UNDEFINED && | |||
380 | getValue() != 0; | |||
381 | } | |||
382 | ||||
383 | bool isUndefined() const { | |||
384 | return isExternal() && getSectionNumber() == COFF::IMAGE_SYM_UNDEFINED && | |||
385 | getValue() == 0; | |||
386 | } | |||
387 | ||||
388 | bool isWeakExternal() const { | |||
389 | return getStorageClass() == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL; | |||
390 | } | |||
391 | ||||
392 | bool isFunctionDefinition() const { | |||
393 | return isExternal() && getBaseType() == COFF::IMAGE_SYM_TYPE_NULL && | |||
394 | getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION && | |||
395 | !COFF::isReservedSectionNumber(getSectionNumber()); | |||
396 | } | |||
397 | ||||
398 | bool isFunctionLineInfo() const { | |||
399 | return getStorageClass() == COFF::IMAGE_SYM_CLASS_FUNCTION; | |||
400 | } | |||
401 | ||||
402 | bool isAnyUndefined() const { | |||
403 | return isUndefined() || isWeakExternal(); | |||
404 | } | |||
405 | ||||
406 | bool isFileRecord() const { | |||
407 | return getStorageClass() == COFF::IMAGE_SYM_CLASS_FILE; | |||
408 | } | |||
409 | ||||
410 | bool isSection() const { | |||
411 | return getStorageClass() == COFF::IMAGE_SYM_CLASS_SECTION; | |||
412 | } | |||
413 | ||||
414 | bool isSectionDefinition() const { | |||
415 | // C++/CLI creates external ABS symbols for non-const appdomain globals. | |||
416 | // These are also followed by an auxiliary section definition. | |||
417 | bool isAppdomainGlobal = | |||
418 | getStorageClass() == COFF::IMAGE_SYM_CLASS_EXTERNAL && | |||
419 | getSectionNumber() == COFF::IMAGE_SYM_ABSOLUTE; | |||
420 | bool isOrdinarySection = getStorageClass() == COFF::IMAGE_SYM_CLASS_STATIC; | |||
421 | if (!getNumberOfAuxSymbols()) | |||
422 | return false; | |||
423 | return isAppdomainGlobal || isOrdinarySection; | |||
424 | } | |||
425 | ||||
426 | bool isCLRToken() const { | |||
427 | return getStorageClass() == COFF::IMAGE_SYM_CLASS_CLR_TOKEN; | |||
428 | } | |||
429 | ||||
430 | private: | |||
431 | bool isSet() const { return CS16 || CS32; } | |||
432 | ||||
433 | const coff_symbol16 *CS16 = nullptr; | |||
434 | const coff_symbol32 *CS32 = nullptr; | |||
435 | }; | |||
436 | ||||
437 | struct coff_section { | |||
438 | char Name[COFF::NameSize]; | |||
439 | support::ulittle32_t VirtualSize; | |||
440 | support::ulittle32_t VirtualAddress; | |||
441 | support::ulittle32_t SizeOfRawData; | |||
442 | support::ulittle32_t PointerToRawData; | |||
443 | support::ulittle32_t PointerToRelocations; | |||
444 | support::ulittle32_t PointerToLinenumbers; | |||
445 | support::ulittle16_t NumberOfRelocations; | |||
446 | support::ulittle16_t NumberOfLinenumbers; | |||
447 | support::ulittle32_t Characteristics; | |||
448 | ||||
449 | // Returns true if the actual number of relocations is stored in | |||
450 | // VirtualAddress field of the first relocation table entry. | |||
451 | bool hasExtendedRelocations() const { | |||
452 | return (Characteristics & COFF::IMAGE_SCN_LNK_NRELOC_OVFL) && | |||
453 | NumberOfRelocations == UINT16_MAX(65535); | |||
454 | } | |||
455 | ||||
456 | uint32_t getAlignment() const { | |||
457 | // The IMAGE_SCN_TYPE_NO_PAD bit is a legacy way of getting to | |||
458 | // IMAGE_SCN_ALIGN_1BYTES. | |||
459 | if (Characteristics & COFF::IMAGE_SCN_TYPE_NO_PAD) | |||
460 | return 1; | |||
461 | ||||
462 | // Bit [20:24] contains section alignment. 0 means use a default alignment | |||
463 | // of 16. | |||
464 | uint32_t Shift = (Characteristics >> 20) & 0xF; | |||
465 | if (Shift > 0) | |||
466 | return 1U << (Shift - 1); | |||
467 | return 16; | |||
468 | } | |||
469 | }; | |||
470 | ||||
471 | struct coff_relocation { | |||
472 | support::ulittle32_t VirtualAddress; | |||
473 | support::ulittle32_t SymbolTableIndex; | |||
474 | support::ulittle16_t Type; | |||
475 | }; | |||
476 | ||||
477 | struct coff_aux_function_definition { | |||
478 | support::ulittle32_t TagIndex; | |||
479 | support::ulittle32_t TotalSize; | |||
480 | support::ulittle32_t PointerToLinenumber; | |||
481 | support::ulittle32_t PointerToNextFunction; | |||
482 | char Unused1[2]; | |||
483 | }; | |||
484 | ||||
485 | static_assert(sizeof(coff_aux_function_definition) == 18, | |||
486 | "auxiliary entry must be 18 bytes"); | |||
487 | ||||
488 | struct coff_aux_bf_and_ef_symbol { | |||
489 | char Unused1[4]; | |||
490 | support::ulittle16_t Linenumber; | |||
491 | char Unused2[6]; | |||
492 | support::ulittle32_t PointerToNextFunction; | |||
493 | char Unused3[2]; | |||
494 | }; | |||
495 | ||||
496 | static_assert(sizeof(coff_aux_bf_and_ef_symbol) == 18, | |||
497 | "auxiliary entry must be 18 bytes"); | |||
498 | ||||
499 | struct coff_aux_weak_external { | |||
500 | support::ulittle32_t TagIndex; | |||
501 | support::ulittle32_t Characteristics; | |||
502 | char Unused1[10]; | |||
503 | }; | |||
504 | ||||
505 | static_assert(sizeof(coff_aux_weak_external) == 18, | |||
506 | "auxiliary entry must be 18 bytes"); | |||
507 | ||||
508 | struct coff_aux_section_definition { | |||
509 | support::ulittle32_t Length; | |||
510 | support::ulittle16_t NumberOfRelocations; | |||
511 | support::ulittle16_t NumberOfLinenumbers; | |||
512 | support::ulittle32_t CheckSum; | |||
513 | support::ulittle16_t NumberLowPart; | |||
514 | uint8_t Selection; | |||
515 | uint8_t Unused; | |||
516 | support::ulittle16_t NumberHighPart; | |||
517 | int32_t getNumber(bool IsBigObj) const { | |||
518 | uint32_t Number = static_cast<uint32_t>(NumberLowPart); | |||
519 | if (IsBigObj) | |||
520 | Number |= static_cast<uint32_t>(NumberHighPart) << 16; | |||
521 | return static_cast<int32_t>(Number); | |||
522 | } | |||
523 | }; | |||
524 | ||||
525 | static_assert(sizeof(coff_aux_section_definition) == 18, | |||
526 | "auxiliary entry must be 18 bytes"); | |||
527 | ||||
528 | struct coff_aux_clr_token { | |||
529 | uint8_t AuxType; | |||
530 | uint8_t Reserved; | |||
531 | support::ulittle32_t SymbolTableIndex; | |||
532 | char MBZ[12]; | |||
533 | }; | |||
534 | ||||
535 | static_assert(sizeof(coff_aux_clr_token) == 18, | |||
536 | "auxiliary entry must be 18 bytes"); | |||
537 | ||||
538 | struct coff_import_header { | |||
539 | support::ulittle16_t Sig1; | |||
540 | support::ulittle16_t Sig2; | |||
541 | support::ulittle16_t Version; | |||
542 | support::ulittle16_t Machine; | |||
543 | support::ulittle32_t TimeDateStamp; | |||
544 | support::ulittle32_t SizeOfData; | |||
545 | support::ulittle16_t OrdinalHint; | |||
546 | support::ulittle16_t TypeInfo; | |||
547 | ||||
548 | int getType() const { return TypeInfo & 0x3; } | |||
549 | int getNameType() const { return (TypeInfo >> 2) & 0x7; } | |||
550 | }; | |||
551 | ||||
552 | struct coff_import_directory_table_entry { | |||
553 | support::ulittle32_t ImportLookupTableRVA; | |||
554 | support::ulittle32_t TimeDateStamp; | |||
555 | support::ulittle32_t ForwarderChain; | |||
556 | support::ulittle32_t NameRVA; | |||
557 | support::ulittle32_t ImportAddressTableRVA; | |||
558 | ||||
559 | bool isNull() const { | |||
560 | return ImportLookupTableRVA == 0 && TimeDateStamp == 0 && | |||
561 | ForwarderChain == 0 && NameRVA == 0 && ImportAddressTableRVA == 0; | |||
562 | } | |||
563 | }; | |||
564 | ||||
565 | template <typename IntTy> | |||
566 | struct coff_tls_directory { | |||
567 | IntTy StartAddressOfRawData; | |||
568 | IntTy EndAddressOfRawData; | |||
569 | IntTy AddressOfIndex; | |||
570 | IntTy AddressOfCallBacks; | |||
571 | support::ulittle32_t SizeOfZeroFill; | |||
572 | support::ulittle32_t Characteristics; | |||
573 | ||||
574 | uint32_t getAlignment() const { | |||
575 | // Bit [20:24] contains section alignment. | |||
576 | uint32_t Shift = (Characteristics & 0x00F00000) >> 20; | |||
577 | if (Shift > 0) | |||
578 | return 1U << (Shift - 1); | |||
579 | return 0; | |||
580 | } | |||
581 | }; | |||
582 | ||||
583 | using coff_tls_directory32 = coff_tls_directory<support::little32_t>; | |||
584 | using coff_tls_directory64 = coff_tls_directory<support::little64_t>; | |||
585 | ||||
586 | /// Bits in control flow guard flags as we understand them. | |||
587 | enum class coff_guard_flags : uint32_t { | |||
588 | CFInstrumented = 0x00000100, | |||
589 | HasFidTable = 0x00000400, | |||
590 | ProtectDelayLoadIAT = 0x00001000, | |||
591 | DelayLoadIATSection = 0x00002000, // Delay load in separate section | |||
592 | HasLongJmpTable = 0x00010000, | |||
593 | FidTableHasFlags = 0x10000000, // Indicates that fid tables are 5 bytes | |||
594 | }; | |||
595 | ||||
596 | enum class frame_type : uint16_t { Fpo = 0, Trap = 1, Tss = 2, NonFpo = 3 }; | |||
597 | ||||
598 | struct coff_load_config_code_integrity { | |||
599 | support::ulittle16_t Flags; | |||
600 | support::ulittle16_t Catalog; | |||
601 | support::ulittle32_t CatalogOffset; | |||
602 | support::ulittle32_t Reserved; | |||
603 | }; | |||
604 | ||||
605 | /// 32-bit load config (IMAGE_LOAD_CONFIG_DIRECTORY32) | |||
606 | struct coff_load_configuration32 { | |||
607 | support::ulittle32_t Size; | |||
608 | support::ulittle32_t TimeDateStamp; | |||
609 | support::ulittle16_t MajorVersion; | |||
610 | support::ulittle16_t MinorVersion; | |||
611 | support::ulittle32_t GlobalFlagsClear; | |||
612 | support::ulittle32_t GlobalFlagsSet; | |||
613 | support::ulittle32_t CriticalSectionDefaultTimeout; | |||
614 | support::ulittle32_t DeCommitFreeBlockThreshold; | |||
615 | support::ulittle32_t DeCommitTotalFreeThreshold; | |||
616 | support::ulittle32_t LockPrefixTable; | |||
617 | support::ulittle32_t MaximumAllocationSize; | |||
618 | support::ulittle32_t VirtualMemoryThreshold; | |||
619 | support::ulittle32_t ProcessAffinityMask; | |||
620 | support::ulittle32_t ProcessHeapFlags; | |||
621 | support::ulittle16_t CSDVersion; | |||
622 | support::ulittle16_t DependentLoadFlags; | |||
623 | support::ulittle32_t EditList; | |||
624 | support::ulittle32_t SecurityCookie; | |||
625 | support::ulittle32_t SEHandlerTable; | |||
626 | support::ulittle32_t SEHandlerCount; | |||
627 | ||||
628 | // Added in MSVC 2015 for /guard:cf. | |||
629 | support::ulittle32_t GuardCFCheckFunction; | |||
630 | support::ulittle32_t GuardCFCheckDispatch; | |||
631 | support::ulittle32_t GuardCFFunctionTable; | |||
632 | support::ulittle32_t GuardCFFunctionCount; | |||
633 | support::ulittle32_t GuardFlags; // coff_guard_flags | |||
634 | ||||
635 | // Added in MSVC 2017 | |||
636 | coff_load_config_code_integrity CodeIntegrity; | |||
637 | support::ulittle32_t GuardAddressTakenIatEntryTable; | |||
638 | support::ulittle32_t GuardAddressTakenIatEntryCount; | |||
639 | support::ulittle32_t GuardLongJumpTargetTable; | |||
640 | support::ulittle32_t GuardLongJumpTargetCount; | |||
641 | support::ulittle32_t DynamicValueRelocTable; | |||
642 | support::ulittle32_t CHPEMetadataPointer; | |||
643 | support::ulittle32_t GuardRFFailureRoutine; | |||
644 | support::ulittle32_t GuardRFFailureRoutineFunctionPointer; | |||
645 | support::ulittle32_t DynamicValueRelocTableOffset; | |||
646 | support::ulittle16_t DynamicValueRelocTableSection; | |||
647 | support::ulittle16_t Reserved2; | |||
648 | support::ulittle32_t GuardRFVerifyStackPointerFunctionPointer; | |||
649 | support::ulittle32_t HotPatchTableOffset; | |||
650 | }; | |||
651 | ||||
652 | /// 64-bit load config (IMAGE_LOAD_CONFIG_DIRECTORY64) | |||
653 | struct coff_load_configuration64 { | |||
654 | support::ulittle32_t Size; | |||
655 | support::ulittle32_t TimeDateStamp; | |||
656 | support::ulittle16_t MajorVersion; | |||
657 | support::ulittle16_t MinorVersion; | |||
658 | support::ulittle32_t GlobalFlagsClear; | |||
659 | support::ulittle32_t GlobalFlagsSet; | |||
660 | support::ulittle32_t CriticalSectionDefaultTimeout; | |||
661 | support::ulittle64_t DeCommitFreeBlockThreshold; | |||
662 | support::ulittle64_t DeCommitTotalFreeThreshold; | |||
663 | support::ulittle64_t LockPrefixTable; | |||
664 | support::ulittle64_t MaximumAllocationSize; | |||
665 | support::ulittle64_t VirtualMemoryThreshold; | |||
666 | support::ulittle64_t ProcessAffinityMask; | |||
667 | support::ulittle32_t ProcessHeapFlags; | |||
668 | support::ulittle16_t CSDVersion; | |||
669 | support::ulittle16_t DependentLoadFlags; | |||
670 | support::ulittle64_t EditList; | |||
671 | support::ulittle64_t SecurityCookie; | |||
672 | support::ulittle64_t SEHandlerTable; | |||
673 | support::ulittle64_t SEHandlerCount; | |||
674 | ||||
675 | // Added in MSVC 2015 for /guard:cf. | |||
676 | support::ulittle64_t GuardCFCheckFunction; | |||
677 | support::ulittle64_t GuardCFCheckDispatch; | |||
678 | support::ulittle64_t GuardCFFunctionTable; | |||
679 | support::ulittle64_t GuardCFFunctionCount; | |||
680 | support::ulittle32_t GuardFlags; | |||
681 | ||||
682 | // Added in MSVC 2017 | |||
683 | coff_load_config_code_integrity CodeIntegrity; | |||
684 | support::ulittle64_t GuardAddressTakenIatEntryTable; | |||
685 | support::ulittle64_t GuardAddressTakenIatEntryCount; | |||
686 | support::ulittle64_t GuardLongJumpTargetTable; | |||
687 | support::ulittle64_t GuardLongJumpTargetCount; | |||
688 | support::ulittle64_t DynamicValueRelocTable; | |||
689 | support::ulittle64_t CHPEMetadataPointer; | |||
690 | support::ulittle64_t GuardRFFailureRoutine; | |||
691 | support::ulittle64_t GuardRFFailureRoutineFunctionPointer; | |||
692 | support::ulittle32_t DynamicValueRelocTableOffset; | |||
693 | support::ulittle16_t DynamicValueRelocTableSection; | |||
694 | support::ulittle16_t Reserved2; | |||
695 | support::ulittle64_t GuardRFVerifyStackPointerFunctionPointer; | |||
696 | support::ulittle32_t HotPatchTableOffset; | |||
697 | }; | |||
698 | ||||
699 | struct coff_runtime_function_x64 { | |||
700 | support::ulittle32_t BeginAddress; | |||
701 | support::ulittle32_t EndAddress; | |||
702 | support::ulittle32_t UnwindInformation; | |||
703 | }; | |||
704 | ||||
705 | struct coff_base_reloc_block_header { | |||
706 | support::ulittle32_t PageRVA; | |||
707 | support::ulittle32_t BlockSize; | |||
708 | }; | |||
709 | ||||
710 | struct coff_base_reloc_block_entry { | |||
711 | support::ulittle16_t Data; | |||
712 | ||||
713 | int getType() const { return Data >> 12; } | |||
714 | int getOffset() const { return Data & ((1 << 12) - 1); } | |||
715 | }; | |||
716 | ||||
717 | struct coff_resource_dir_entry { | |||
718 | union { | |||
719 | support::ulittle32_t NameOffset; | |||
720 | support::ulittle32_t ID; | |||
721 | uint32_t getNameOffset() const { | |||
722 | return maskTrailingOnes<uint32_t>(31) & NameOffset; | |||
723 | } | |||
724 | // Even though the PE/COFF spec doesn't mention this, the high bit of a name | |||
725 | // offset is set. | |||
726 | void setNameOffset(uint32_t Offset) { NameOffset = Offset | (1 << 31); } | |||
727 | } Identifier; | |||
728 | union { | |||
729 | support::ulittle32_t DataEntryOffset; | |||
730 | support::ulittle32_t SubdirOffset; | |||
731 | ||||
732 | bool isSubDir() const { return SubdirOffset >> 31; } | |||
733 | uint32_t value() const { | |||
734 | return maskTrailingOnes<uint32_t>(31) & SubdirOffset; | |||
735 | } | |||
736 | ||||
737 | } Offset; | |||
738 | }; | |||
739 | ||||
740 | struct coff_resource_data_entry { | |||
741 | support::ulittle32_t DataRVA; | |||
742 | support::ulittle32_t DataSize; | |||
743 | support::ulittle32_t Codepage; | |||
744 | support::ulittle32_t Reserved; | |||
745 | }; | |||
746 | ||||
747 | struct coff_resource_dir_table { | |||
748 | support::ulittle32_t Characteristics; | |||
749 | support::ulittle32_t TimeDateStamp; | |||
750 | support::ulittle16_t MajorVersion; | |||
751 | support::ulittle16_t MinorVersion; | |||
752 | support::ulittle16_t NumberOfNameEntries; | |||
753 | support::ulittle16_t NumberOfIDEntries; | |||
754 | }; | |||
755 | ||||
756 | struct debug_h_header { | |||
757 | support::ulittle32_t Magic; | |||
758 | support::ulittle16_t Version; | |||
759 | support::ulittle16_t HashAlgorithm; | |||
760 | }; | |||
761 | ||||
762 | class COFFObjectFile : public ObjectFile { | |||
763 | private: | |||
764 | friend class ImportDirectoryEntryRef; | |||
765 | friend class ExportDirectoryEntryRef; | |||
766 | const coff_file_header *COFFHeader; | |||
767 | const coff_bigobj_file_header *COFFBigObjHeader; | |||
768 | const pe32_header *PE32Header; | |||
769 | const pe32plus_header *PE32PlusHeader; | |||
770 | const data_directory *DataDirectory; | |||
771 | const coff_section *SectionTable; | |||
772 | const coff_symbol16 *SymbolTable16; | |||
773 | const coff_symbol32 *SymbolTable32; | |||
774 | const char *StringTable; | |||
775 | uint32_t StringTableSize; | |||
776 | const coff_import_directory_table_entry *ImportDirectory; | |||
777 | const delay_import_directory_table_entry *DelayImportDirectory; | |||
778 | uint32_t NumberOfDelayImportDirectory; | |||
779 | const export_directory_table_entry *ExportDirectory; | |||
780 | const coff_base_reloc_block_header *BaseRelocHeader; | |||
781 | const coff_base_reloc_block_header *BaseRelocEnd; | |||
782 | const debug_directory *DebugDirectoryBegin; | |||
783 | const debug_directory *DebugDirectoryEnd; | |||
784 | // Either coff_load_configuration32 or coff_load_configuration64. | |||
785 | const void *LoadConfig = nullptr; | |||
786 | ||||
787 | std::error_code getString(uint32_t offset, StringRef &Res) const; | |||
788 | ||||
789 | template <typename coff_symbol_type> | |||
790 | const coff_symbol_type *toSymb(DataRefImpl Symb) const; | |||
791 | const coff_section *toSec(DataRefImpl Sec) const; | |||
792 | const coff_relocation *toRel(DataRefImpl Rel) const; | |||
793 | ||||
794 | std::error_code initSymbolTablePtr(); | |||
795 | std::error_code initImportTablePtr(); | |||
796 | std::error_code initDelayImportTablePtr(); | |||
797 | std::error_code initExportTablePtr(); | |||
798 | std::error_code initBaseRelocPtr(); | |||
799 | std::error_code initDebugDirectoryPtr(); | |||
800 | std::error_code initLoadConfigPtr(); | |||
801 | ||||
802 | public: | |||
803 | uintptr_t getSymbolTable() const { | |||
804 | if (SymbolTable16) | |||
805 | return reinterpret_cast<uintptr_t>(SymbolTable16); | |||
806 | if (SymbolTable32) | |||
807 | return reinterpret_cast<uintptr_t>(SymbolTable32); | |||
808 | return uintptr_t(0); | |||
809 | } | |||
810 | ||||
811 | uint16_t getMachine() const { | |||
812 | if (COFFHeader) | |||
813 | return COFFHeader->Machine; | |||
814 | if (COFFBigObjHeader) | |||
815 | return COFFBigObjHeader->Machine; | |||
816 | llvm_unreachable("no COFF header!")::llvm::llvm_unreachable_internal("no COFF header!", "/build/llvm-toolchain-snapshot-10~svn372306/include/llvm/Object/COFF.h" , 816); | |||
817 | } | |||
818 | ||||
819 | uint16_t getSizeOfOptionalHeader() const { | |||
820 | if (COFFHeader) | |||
821 | return COFFHeader->isImportLibrary() ? 0 | |||
822 | : COFFHeader->SizeOfOptionalHeader; | |||
823 | // bigobj doesn't have this field. | |||
824 | if (COFFBigObjHeader) | |||
825 | return 0; | |||
826 | llvm_unreachable("no COFF header!")::llvm::llvm_unreachable_internal("no COFF header!", "/build/llvm-toolchain-snapshot-10~svn372306/include/llvm/Object/COFF.h" , 826); | |||
827 | } | |||
828 | ||||
829 | uint16_t getCharacteristics() const { | |||
830 | if (COFFHeader) | |||
831 | return COFFHeader->isImportLibrary() ? 0 : COFFHeader->Characteristics; | |||
832 | // bigobj doesn't have characteristics to speak of, | |||
833 | // editbin will silently lie to you if you attempt to set any. | |||
834 | if (COFFBigObjHeader) | |||
835 | return 0; | |||
836 | llvm_unreachable("no COFF header!")::llvm::llvm_unreachable_internal("no COFF header!", "/build/llvm-toolchain-snapshot-10~svn372306/include/llvm/Object/COFF.h" , 836); | |||
837 | } | |||
838 | ||||
839 | uint32_t getTimeDateStamp() const { | |||
840 | if (COFFHeader) | |||
841 | return COFFHeader->TimeDateStamp; | |||
842 | if (COFFBigObjHeader) | |||
843 | return COFFBigObjHeader->TimeDateStamp; | |||
844 | llvm_unreachable("no COFF header!")::llvm::llvm_unreachable_internal("no COFF header!", "/build/llvm-toolchain-snapshot-10~svn372306/include/llvm/Object/COFF.h" , 844); | |||
845 | } | |||
846 | ||||
847 | uint32_t getNumberOfSections() const { | |||
848 | if (COFFHeader) | |||
849 | return COFFHeader->isImportLibrary() ? 0 : COFFHeader->NumberOfSections; | |||
850 | if (COFFBigObjHeader) | |||
851 | return COFFBigObjHeader->NumberOfSections; | |||
852 | llvm_unreachable("no COFF header!")::llvm::llvm_unreachable_internal("no COFF header!", "/build/llvm-toolchain-snapshot-10~svn372306/include/llvm/Object/COFF.h" , 852); | |||
853 | } | |||
854 | ||||
855 | uint32_t getPointerToSymbolTable() const { | |||
856 | if (COFFHeader) | |||
857 | return COFFHeader->isImportLibrary() ? 0 | |||
858 | : COFFHeader->PointerToSymbolTable; | |||
859 | if (COFFBigObjHeader) | |||
860 | return COFFBigObjHeader->PointerToSymbolTable; | |||
861 | llvm_unreachable("no COFF header!")::llvm::llvm_unreachable_internal("no COFF header!", "/build/llvm-toolchain-snapshot-10~svn372306/include/llvm/Object/COFF.h" , 861); | |||
862 | } | |||
863 | ||||
864 | uint32_t getRawNumberOfSymbols() const { | |||
865 | if (COFFHeader) | |||
866 | return COFFHeader->isImportLibrary() ? 0 : COFFHeader->NumberOfSymbols; | |||
867 | if (COFFBigObjHeader) | |||
868 | return COFFBigObjHeader->NumberOfSymbols; | |||
869 | llvm_unreachable("no COFF header!")::llvm::llvm_unreachable_internal("no COFF header!", "/build/llvm-toolchain-snapshot-10~svn372306/include/llvm/Object/COFF.h" , 869); | |||
870 | } | |||
871 | ||||
872 | uint32_t getNumberOfSymbols() const { | |||
873 | if (!SymbolTable16 && !SymbolTable32) | |||
874 | return 0; | |||
875 | return getRawNumberOfSymbols(); | |||
876 | } | |||
877 | ||||
878 | const coff_load_configuration32 *getLoadConfig32() const { | |||
879 | assert(!is64())((!is64()) ? static_cast<void> (0) : __assert_fail ("!is64()" , "/build/llvm-toolchain-snapshot-10~svn372306/include/llvm/Object/COFF.h" , 879, __PRETTY_FUNCTION__)); | |||
880 | return reinterpret_cast<const coff_load_configuration32 *>(LoadConfig); | |||
881 | } | |||
882 | ||||
883 | const coff_load_configuration64 *getLoadConfig64() const { | |||
884 | assert(is64())((is64()) ? static_cast<void> (0) : __assert_fail ("is64()" , "/build/llvm-toolchain-snapshot-10~svn372306/include/llvm/Object/COFF.h" , 884, __PRETTY_FUNCTION__)); | |||
885 | return reinterpret_cast<const coff_load_configuration64 *>(LoadConfig); | |||
886 | } | |||
887 | StringRef getRelocationTypeName(uint16_t Type) const; | |||
888 | ||||
889 | protected: | |||
890 | void moveSymbolNext(DataRefImpl &Symb) const override; | |||
891 | Expected<StringRef> getSymbolName(DataRefImpl Symb) const override; | |||
892 | Expected<uint64_t> getSymbolAddress(DataRefImpl Symb) const override; | |||
893 | uint32_t getSymbolAlignment(DataRefImpl Symb) const override; | |||
894 | uint64_t getSymbolValueImpl(DataRefImpl Symb) const override; | |||
895 | uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override; | |||
896 | uint32_t getSymbolFlags(DataRefImpl Symb) const override; | |||
897 | Expected<SymbolRef::Type> getSymbolType(DataRefImpl Symb) const override; | |||
898 | Expected<section_iterator> getSymbolSection(DataRefImpl Symb) const override; | |||
899 | void moveSectionNext(DataRefImpl &Sec) const override; | |||
900 | Expected<StringRef> getSectionName(DataRefImpl Sec) const override; | |||
901 | uint64_t getSectionAddress(DataRefImpl Sec) const override; | |||
902 | uint64_t getSectionIndex(DataRefImpl Sec) const override; | |||
903 | uint64_t getSectionSize(DataRefImpl Sec) const override; | |||
904 | Expected<ArrayRef<uint8_t>> | |||
905 | getSectionContents(DataRefImpl Sec) const override; | |||
906 | uint64_t getSectionAlignment(DataRefImpl Sec) const override; | |||
907 | bool isSectionCompressed(DataRefImpl Sec) const override; | |||
908 | bool isSectionText(DataRefImpl Sec) const override; | |||
909 | bool isSectionData(DataRefImpl Sec) const override; | |||
910 | bool isSectionBSS(DataRefImpl Sec) const override; | |||
911 | bool isSectionVirtual(DataRefImpl Sec) const override; | |||
912 | relocation_iterator section_rel_begin(DataRefImpl Sec) const override; | |||
913 | relocation_iterator section_rel_end(DataRefImpl Sec) const override; | |||
914 | ||||
915 | void moveRelocationNext(DataRefImpl &Rel) const override; | |||
916 | uint64_t getRelocationOffset(DataRefImpl Rel) const override; | |||
917 | symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override; | |||
918 | uint64_t getRelocationType(DataRefImpl Rel) const override; | |||
919 | void getRelocationTypeName(DataRefImpl Rel, | |||
920 | SmallVectorImpl<char> &Result) const override; | |||
921 | ||||
922 | public: | |||
923 | COFFObjectFile(MemoryBufferRef Object, std::error_code &EC); | |||
924 | ||||
925 | basic_symbol_iterator symbol_begin() const override; | |||
926 | basic_symbol_iterator symbol_end() const override; | |||
927 | section_iterator section_begin() const override; | |||
928 | section_iterator section_end() const override; | |||
929 | ||||
930 | const coff_section *getCOFFSection(const SectionRef &Section) const; | |||
931 | COFFSymbolRef getCOFFSymbol(const DataRefImpl &Ref) const; | |||
932 | COFFSymbolRef getCOFFSymbol(const SymbolRef &Symbol) const; | |||
933 | const coff_relocation *getCOFFRelocation(const RelocationRef &Reloc) const; | |||
934 | unsigned getSectionID(SectionRef Sec) const; | |||
935 | unsigned getSymbolSectionID(SymbolRef Sym) const; | |||
936 | ||||
937 | uint8_t getBytesInAddress() const override; | |||
938 | StringRef getFileFormatName() const override; | |||
939 | Triple::ArchType getArch() const override; | |||
940 | Expected<uint64_t> getStartAddress() const override; | |||
941 | SubtargetFeatures getFeatures() const override { return SubtargetFeatures(); } | |||
942 | ||||
943 | import_directory_iterator import_directory_begin() const; | |||
944 | import_directory_iterator import_directory_end() const; | |||
945 | delay_import_directory_iterator delay_import_directory_begin() const; | |||
946 | delay_import_directory_iterator delay_import_directory_end() const; | |||
947 | export_directory_iterator export_directory_begin() const; | |||
948 | export_directory_iterator export_directory_end() const; | |||
949 | base_reloc_iterator base_reloc_begin() const; | |||
950 | base_reloc_iterator base_reloc_end() const; | |||
951 | const debug_directory *debug_directory_begin() const { | |||
952 | return DebugDirectoryBegin; | |||
953 | } | |||
954 | const debug_directory *debug_directory_end() const { | |||
955 | return DebugDirectoryEnd; | |||
956 | } | |||
957 | ||||
958 | iterator_range<import_directory_iterator> import_directories() const; | |||
959 | iterator_range<delay_import_directory_iterator> | |||
960 | delay_import_directories() const; | |||
961 | iterator_range<export_directory_iterator> export_directories() const; | |||
962 | iterator_range<base_reloc_iterator> base_relocs() const; | |||
963 | iterator_range<const debug_directory *> debug_directories() const { | |||
964 | return make_range(debug_directory_begin(), debug_directory_end()); | |||
965 | } | |||
966 | ||||
967 | const dos_header *getDOSHeader() const { | |||
968 | if (!PE32Header && !PE32PlusHeader) | |||
969 | return nullptr; | |||
970 | return reinterpret_cast<const dos_header *>(base()); | |||
971 | } | |||
972 | ||||
973 | const coff_file_header *getCOFFHeader() const { return COFFHeader; } | |||
974 | const coff_bigobj_file_header *getCOFFBigObjHeader() const { | |||
975 | return COFFBigObjHeader; | |||
976 | } | |||
977 | const pe32_header *getPE32Header() const { return PE32Header; } | |||
978 | const pe32plus_header *getPE32PlusHeader() const { return PE32PlusHeader; } | |||
979 | ||||
980 | std::error_code getDataDirectory(uint32_t index, | |||
981 | const data_directory *&Res) const; | |||
982 | std::error_code getSection(int32_t index, const coff_section *&Res) const; | |||
983 | std::error_code getSection(StringRef SectionName, | |||
984 | const coff_section *&Res) const; | |||
985 | ||||
986 | template <typename coff_symbol_type> | |||
987 | std::error_code getSymbol(uint32_t Index, | |||
988 | const coff_symbol_type *&Res) const { | |||
989 | if (Index >= getNumberOfSymbols()) | |||
990 | return object_error::parse_failed; | |||
991 | ||||
992 | Res = reinterpret_cast<coff_symbol_type *>(getSymbolTable()) + Index; | |||
993 | return std::error_code(); | |||
994 | } | |||
995 | Expected<COFFSymbolRef> getSymbol(uint32_t index) const { | |||
996 | if (SymbolTable16) { | |||
997 | const coff_symbol16 *Symb = nullptr; | |||
998 | if (std::error_code EC = getSymbol(index, Symb)) | |||
999 | return errorCodeToError(EC); | |||
1000 | return COFFSymbolRef(Symb); | |||
1001 | } | |||
1002 | if (SymbolTable32) { | |||
1003 | const coff_symbol32 *Symb = nullptr; | |||
1004 | if (std::error_code EC = getSymbol(index, Symb)) | |||
1005 | return errorCodeToError(EC); | |||
1006 | return COFFSymbolRef(Symb); | |||
1007 | } | |||
1008 | return errorCodeToError(object_error::parse_failed); | |||
1009 | } | |||
1010 | ||||
1011 | template <typename T> | |||
1012 | std::error_code getAuxSymbol(uint32_t index, const T *&Res) const { | |||
1013 | Expected<COFFSymbolRef> S = getSymbol(index); | |||
1014 | if (Error E = S.takeError()) | |||
1015 | return errorToErrorCode(std::move(E)); | |||
1016 | Res = reinterpret_cast<const T *>(S->getRawPtr()); | |||
1017 | return std::error_code(); | |||
1018 | } | |||
1019 | ||||
1020 | std::error_code getSymbolName(COFFSymbolRef Symbol, StringRef &Res) const; | |||
1021 | std::error_code getSymbolName(const coff_symbol_generic *Symbol, | |||
1022 | StringRef &Res) const; | |||
1023 | ||||
1024 | ArrayRef<uint8_t> getSymbolAuxData(COFFSymbolRef Symbol) const; | |||
1025 | ||||
1026 | uint32_t getSymbolIndex(COFFSymbolRef Symbol) const; | |||
1027 | ||||
1028 | size_t getSymbolTableEntrySize() const { | |||
1029 | if (COFFHeader) | |||
1030 | return sizeof(coff_symbol16); | |||
1031 | if (COFFBigObjHeader) | |||
1032 | return sizeof(coff_symbol32); | |||
1033 | llvm_unreachable("null symbol table pointer!")::llvm::llvm_unreachable_internal("null symbol table pointer!" , "/build/llvm-toolchain-snapshot-10~svn372306/include/llvm/Object/COFF.h" , 1033); | |||
1034 | } | |||
1035 | ||||
1036 | ArrayRef<coff_relocation> getRelocations(const coff_section *Sec) const; | |||
1037 | ||||
1038 | Expected<StringRef> getSectionName(const coff_section *Sec) const; | |||
1039 | uint64_t getSectionSize(const coff_section *Sec) const; | |||
1040 | Error getSectionContents(const coff_section *Sec, | |||
1041 | ArrayRef<uint8_t> &Res) const; | |||
1042 | ||||
1043 | uint64_t getImageBase() const; | |||
1044 | std::error_code getVaPtr(uint64_t VA, uintptr_t &Res) const; | |||
1045 | std::error_code getRvaPtr(uint32_t Rva, uintptr_t &Res) const; | |||
1046 | ||||
1047 | /// Given an RVA base and size, returns a valid array of bytes or an error | |||
1048 | /// code if the RVA and size is not contained completely within a valid | |||
1049 | /// section. | |||
1050 | std::error_code getRvaAndSizeAsBytes(uint32_t RVA, uint32_t Size, | |||
1051 | ArrayRef<uint8_t> &Contents) const; | |||
1052 | ||||
1053 | std::error_code getHintName(uint32_t Rva, uint16_t &Hint, | |||
1054 | StringRef &Name) const; | |||
1055 | ||||
1056 | /// Get PDB information out of a codeview debug directory entry. | |||
1057 | std::error_code getDebugPDBInfo(const debug_directory *DebugDir, | |||
1058 | const codeview::DebugInfo *&Info, | |||
1059 | StringRef &PDBFileName) const; | |||
1060 | ||||
1061 | /// Get PDB information from an executable. If the information is not present, | |||
1062 | /// Info will be set to nullptr and PDBFileName will be empty. An error is | |||
1063 | /// returned only on corrupt object files. Convenience accessor that can be | |||
1064 | /// used if the debug directory is not already handy. | |||
1065 | std::error_code getDebugPDBInfo(const codeview::DebugInfo *&Info, | |||
1066 | StringRef &PDBFileName) const; | |||
1067 | ||||
1068 | bool isRelocatableObject() const override; | |||
1069 | bool is64() const { return PE32PlusHeader; } | |||
1070 | ||||
1071 | StringRef mapDebugSectionName(StringRef Name) const override; | |||
1072 | ||||
1073 | static bool classof(const Binary *v) { return v->isCOFF(); } | |||
1074 | }; | |||
1075 | ||||
1076 | // The iterator for the import directory table. | |||
1077 | class ImportDirectoryEntryRef { | |||
1078 | public: | |||
1079 | ImportDirectoryEntryRef() = default; | |||
1080 | ImportDirectoryEntryRef(const coff_import_directory_table_entry *Table, | |||
1081 | uint32_t I, const COFFObjectFile *Owner) | |||
1082 | : ImportTable(Table), Index(I), OwningObject(Owner) {} | |||
1083 | ||||
1084 | bool operator==(const ImportDirectoryEntryRef &Other) const; | |||
1085 | void moveNext(); | |||
1086 | ||||
1087 | imported_symbol_iterator imported_symbol_begin() const; | |||
1088 | imported_symbol_iterator imported_symbol_end() const; | |||
1089 | iterator_range<imported_symbol_iterator> imported_symbols() const; | |||
1090 | ||||
1091 | imported_symbol_iterator lookup_table_begin() const; | |||
1092 | imported_symbol_iterator lookup_table_end() const; | |||
1093 | iterator_range<imported_symbol_iterator> lookup_table_symbols() const; | |||
1094 | ||||
1095 | std::error_code getName(StringRef &Result) const; | |||
1096 | std::error_code getImportLookupTableRVA(uint32_t &Result) const; | |||
1097 | std::error_code getImportAddressTableRVA(uint32_t &Result) const; | |||
1098 | ||||
1099 | std::error_code | |||
1100 | getImportTableEntry(const coff_import_directory_table_entry *&Result) const; | |||
1101 | ||||
1102 | private: | |||
1103 | const coff_import_directory_table_entry *ImportTable; | |||
1104 | uint32_t Index; | |||
1105 | const COFFObjectFile *OwningObject = nullptr; | |||
1106 | }; | |||
1107 | ||||
1108 | class DelayImportDirectoryEntryRef { | |||
1109 | public: | |||
1110 | DelayImportDirectoryEntryRef() = default; | |||
1111 | DelayImportDirectoryEntryRef(const delay_import_directory_table_entry *T, | |||
1112 | uint32_t I, const COFFObjectFile *Owner) | |||
1113 | : Table(T), Index(I), OwningObject(Owner) {} | |||
1114 | ||||
1115 | bool operator==(const DelayImportDirectoryEntryRef &Other) const; | |||
1116 | void moveNext(); | |||
1117 | ||||
1118 | imported_symbol_iterator imported_symbol_begin() const; | |||
1119 | imported_symbol_iterator imported_symbol_end() const; | |||
1120 | iterator_range<imported_symbol_iterator> imported_symbols() const; | |||
1121 | ||||
1122 | std::error_code getName(StringRef &Result) const; | |||
1123 | std::error_code getDelayImportTable( | |||
1124 | const delay_import_directory_table_entry *&Result) const; | |||
1125 | std::error_code getImportAddress(int AddrIndex, uint64_t &Result) const; | |||
1126 | ||||
1127 | private: | |||
1128 | const delay_import_directory_table_entry *Table; | |||
1129 | uint32_t Index; | |||
1130 | const COFFObjectFile *OwningObject = nullptr; | |||
1131 | }; | |||
1132 | ||||
1133 | // The iterator for the export directory table entry. | |||
1134 | class ExportDirectoryEntryRef { | |||
1135 | public: | |||
1136 | ExportDirectoryEntryRef() = default; | |||
1137 | ExportDirectoryEntryRef(const export_directory_table_entry *Table, uint32_t I, | |||
1138 | const COFFObjectFile *Owner) | |||
1139 | : ExportTable(Table), Index(I), OwningObject(Owner) {} | |||
1140 | ||||
1141 | bool operator==(const ExportDirectoryEntryRef &Other) const; | |||
1142 | void moveNext(); | |||
1143 | ||||
1144 | std::error_code getDllName(StringRef &Result) const; | |||
1145 | std::error_code getOrdinalBase(uint32_t &Result) const; | |||
1146 | std::error_code getOrdinal(uint32_t &Result) const; | |||
1147 | std::error_code getExportRVA(uint32_t &Result) const; | |||
1148 | std::error_code getSymbolName(StringRef &Result) const; | |||
1149 | ||||
1150 | std::error_code isForwarder(bool &Result) const; | |||
1151 | std::error_code getForwardTo(StringRef &Result) const; | |||
1152 | ||||
1153 | private: | |||
1154 | const export_directory_table_entry *ExportTable; | |||
1155 | uint32_t Index; | |||
1156 | const COFFObjectFile *OwningObject = nullptr; | |||
1157 | }; | |||
1158 | ||||
1159 | class ImportedSymbolRef { | |||
1160 | public: | |||
1161 | ImportedSymbolRef() = default; | |||
1162 | ImportedSymbolRef(const import_lookup_table_entry32 *Entry, uint32_t I, | |||
1163 | const COFFObjectFile *Owner) | |||
1164 | : Entry32(Entry), Entry64(nullptr), Index(I), OwningObject(Owner) {} | |||
1165 | ImportedSymbolRef(const import_lookup_table_entry64 *Entry, uint32_t I, | |||
1166 | const COFFObjectFile *Owner) | |||
1167 | : Entry32(nullptr), Entry64(Entry), Index(I), OwningObject(Owner) {} | |||
1168 | ||||
1169 | bool operator==(const ImportedSymbolRef &Other) const; | |||
1170 | void moveNext(); | |||
1171 | ||||
1172 | std::error_code getSymbolName(StringRef &Result) const; | |||
1173 | std::error_code isOrdinal(bool &Result) const; | |||
1174 | std::error_code getOrdinal(uint16_t &Result) const; | |||
1175 | std::error_code getHintNameRVA(uint32_t &Result) const; | |||
1176 | ||||
1177 | private: | |||
1178 | const import_lookup_table_entry32 *Entry32; | |||
1179 | const import_lookup_table_entry64 *Entry64; | |||
1180 | uint32_t Index; | |||
1181 | const COFFObjectFile *OwningObject = nullptr; | |||
1182 | }; | |||
1183 | ||||
1184 | class BaseRelocRef { | |||
1185 | public: | |||
1186 | BaseRelocRef() = default; | |||
1187 | BaseRelocRef(const coff_base_reloc_block_header *Header, | |||
1188 | const COFFObjectFile *Owner) | |||
1189 | : Header(Header), Index(0) {} | |||
1190 | ||||
1191 | bool operator==(const BaseRelocRef &Other) const; | |||
1192 | void moveNext(); | |||
1193 | ||||
1194 | std::error_code getType(uint8_t &Type) const; | |||
1195 | std::error_code getRVA(uint32_t &Result) const; | |||
1196 | ||||
1197 | private: | |||
1198 | const coff_base_reloc_block_header *Header; | |||
1199 | uint32_t Index; | |||
1200 | }; | |||
1201 | ||||
1202 | class ResourceSectionRef { | |||
1203 | public: | |||
1204 | ResourceSectionRef() = default; | |||
1205 | explicit ResourceSectionRef(StringRef Ref) : BBS(Ref, support::little) {} | |||
1206 | ||||
1207 | Error load(const COFFObjectFile *O); | |||
1208 | Error load(const COFFObjectFile *O, const SectionRef &S); | |||
1209 | ||||
1210 | Expected<ArrayRef<UTF16>> | |||
1211 | getEntryNameString(const coff_resource_dir_entry &Entry); | |||
1212 | Expected<const coff_resource_dir_table &> | |||
1213 | getEntrySubDir(const coff_resource_dir_entry &Entry); | |||
1214 | Expected<const coff_resource_data_entry &> | |||
1215 | getEntryData(const coff_resource_dir_entry &Entry); | |||
1216 | Expected<const coff_resource_dir_table &> getBaseTable(); | |||
1217 | Expected<const coff_resource_dir_entry &> | |||
1218 | getTableEntry(const coff_resource_dir_table &Table, uint32_t Index); | |||
1219 | ||||
1220 | Expected<StringRef> getContents(const coff_resource_data_entry &Entry); | |||
1221 | ||||
1222 | private: | |||
1223 | BinaryByteStream BBS; | |||
1224 | ||||
1225 | SectionRef Section; | |||
1226 | const COFFObjectFile *Obj; | |||
1227 | ||||
1228 | std::vector<const coff_relocation *> Relocs; | |||
1229 | ||||
1230 | Expected<const coff_resource_dir_table &> getTableAtOffset(uint32_t Offset); | |||
1231 | Expected<const coff_resource_dir_entry &> | |||
1232 | getTableEntryAtOffset(uint32_t Offset); | |||
1233 | Expected<const coff_resource_data_entry &> | |||
1234 | getDataEntryAtOffset(uint32_t Offset); | |||
1235 | Expected<ArrayRef<UTF16>> getDirStringAtOffset(uint32_t Offset); | |||
1236 | }; | |||
1237 | ||||
1238 | // Corresponds to `_FPO_DATA` structure in the PE/COFF spec. | |||
1239 | struct FpoData { | |||
1240 | support::ulittle32_t Offset; // ulOffStart: Offset 1st byte of function code | |||
1241 | support::ulittle32_t Size; // cbProcSize: # bytes in function | |||
1242 | support::ulittle32_t NumLocals; // cdwLocals: # bytes in locals/4 | |||
1243 | support::ulittle16_t NumParams; // cdwParams: # bytes in params/4 | |||
1244 | support::ulittle16_t Attributes; | |||
1245 | ||||
1246 | // cbProlog: # bytes in prolog | |||
1247 | int getPrologSize() const { return Attributes & 0xF; } | |||
1248 | ||||
1249 | // cbRegs: # regs saved | |||
1250 | int getNumSavedRegs() const { return (Attributes >> 8) & 0x7; } | |||
1251 | ||||
1252 | // fHasSEH: true if seh is func | |||
1253 | bool hasSEH() const { return (Attributes >> 9) & 1; } | |||
1254 | ||||
1255 | // fUseBP: true if EBP has been allocated | |||
1256 | bool useBP() const { return (Attributes >> 10) & 1; } | |||
1257 | ||||
1258 | // cbFrame: frame pointer | |||
1259 | frame_type getFP() const { return static_cast<frame_type>(Attributes >> 14); } | |||
1260 | }; | |||
1261 | ||||
1262 | } // end namespace object | |||
1263 | ||||
1264 | } // end namespace llvm | |||
1265 | ||||
1266 | #endif // LLVM_OBJECT_COFF_H |