blob: 3d1e62e1e5576f2eee3103909fc8d1c13efc0326 [file] [log] [blame]
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +00001//===- COFFObjectFile.cpp - COFF object file implementation -----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file declares the COFFObjectFile class.
11//
12//===----------------------------------------------------------------------===//
13
Michael J. Spencer001c9202011-06-25 17:54:50 +000014#include "llvm/Object/COFF.h"
Michael J. Spencer1f6e3f92012-03-19 20:27:37 +000015#include "llvm/ADT/ArrayRef.h"
Michael J. Spencer4344b1e2011-10-07 19:25:32 +000016#include "llvm/ADT/SmallString.h"
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +000017#include "llvm/ADT/StringSwitch.h"
18#include "llvm/ADT/Triple.h"
Rui Ueyamaa6610ee2013-09-27 21:04:00 +000019#include "llvm/Support/Debug.h"
20#include "llvm/Support/raw_ostream.h"
Will Dietze3ba15c2013-10-12 00:55:57 +000021#include <cctype>
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +000022
23using namespace llvm;
24using namespace object;
25
26namespace {
27using support::ulittle8_t;
28using support::ulittle16_t;
29using support::ulittle32_t;
30using support::little16_t;
31}
32
Michael J. Spencer25b15772011-06-25 17:55:23 +000033namespace {
34// Returns false if size is greater than the buffer size. And sets ec.
35bool checkSize(const MemoryBuffer *m, error_code &ec, uint64_t size) {
36 if (m->getBufferSize() < size) {
37 ec = object_error::unexpected_eof;
38 return false;
39 }
40 return true;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +000041}
42
Rui Ueyama2f6c0482013-07-19 23:23:29 +000043// Sets Obj unless any bytes in [addr, addr + size) fall outsize of m.
44// Returns unexpected_eof if error.
45template<typename T>
46error_code getObject(const T *&Obj, const MemoryBuffer *M, const uint8_t *Ptr,
47 const size_t Size = sizeof(T)) {
48 uintptr_t Addr = uintptr_t(Ptr);
49 if (Addr + Size < Addr ||
50 Addr + Size < Size ||
51 Addr + Size > uintptr_t(M->getBufferEnd())) {
52 return object_error::unexpected_eof;
Michael J. Spencer25b15772011-06-25 17:55:23 +000053 }
Rui Ueyama2f6c0482013-07-19 23:23:29 +000054 Obj = reinterpret_cast<const T *>(Addr);
55 return object_error::success;
Michael J. Spencer25b15772011-06-25 17:55:23 +000056}
57}
58
59const coff_symbol *COFFObjectFile::toSymb(DataRefImpl Symb) const {
60 const coff_symbol *addr = reinterpret_cast<const coff_symbol*>(Symb.p);
61
62# ifndef NDEBUG
63 // Verify that the symbol points to a valid entry in the symbol table.
64 uintptr_t offset = uintptr_t(addr) - uintptr_t(base());
Rui Ueyama4bf771b2013-06-12 19:10:33 +000065 if (offset < COFFHeader->PointerToSymbolTable
66 || offset >= COFFHeader->PointerToSymbolTable
67 + (COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
Michael J. Spencer25b15772011-06-25 17:55:23 +000068 report_fatal_error("Symbol was outside of symbol table.");
69
Rui Ueyama4bf771b2013-06-12 19:10:33 +000070 assert((offset - COFFHeader->PointerToSymbolTable) % sizeof(coff_symbol)
Michael J. Spencer25b15772011-06-25 17:55:23 +000071 == 0 && "Symbol did not point to the beginning of a symbol");
72# endif
73
74 return addr;
75}
76
77const coff_section *COFFObjectFile::toSec(DataRefImpl Sec) const {
78 const coff_section *addr = reinterpret_cast<const coff_section*>(Sec.p);
79
80# ifndef NDEBUG
81 // Verify that the section points to a valid entry in the section table.
82 if (addr < SectionTable
Rui Ueyama4bf771b2013-06-12 19:10:33 +000083 || addr >= (SectionTable + COFFHeader->NumberOfSections))
Michael J. Spencer25b15772011-06-25 17:55:23 +000084 report_fatal_error("Section was outside of section table.");
85
86 uintptr_t offset = uintptr_t(addr) - uintptr_t(SectionTable);
87 assert(offset % sizeof(coff_section) == 0 &&
88 "Section did not point to the beginning of a section");
89# endif
90
91 return addr;
92}
93
94error_code COFFObjectFile::getSymbolNext(DataRefImpl Symb,
95 SymbolRef &Result) const {
96 const coff_symbol *symb = toSymb(Symb);
97 symb += 1 + symb->NumberOfAuxSymbols;
98 Symb.p = reinterpret_cast<uintptr_t>(symb);
99 Result = SymbolRef(Symb, this);
100 return object_error::success;
101}
102
103 error_code COFFObjectFile::getSymbolName(DataRefImpl Symb,
104 StringRef &Result) const {
105 const coff_symbol *symb = toSymb(Symb);
Michael J. Spencer0e752cb2011-10-17 23:53:56 +0000106 return getSymbolName(symb, Result);
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000107}
108
Danil Malyshevb0436a72011-11-29 17:40:10 +0000109error_code COFFObjectFile::getSymbolFileOffset(DataRefImpl Symb,
Michael J. Spencer25b15772011-06-25 17:55:23 +0000110 uint64_t &Result) const {
111 const coff_symbol *symb = toSymb(Symb);
Michael J. Spencer64388ce2011-07-05 14:48:59 +0000112 const coff_section *Section = NULL;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000113 if (error_code ec = getSection(symb->SectionNumber, Section))
114 return ec;
115 char Type;
116 if (error_code ec = getSymbolNMTypeChar(Symb, Type))
117 return ec;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000118 if (Type == 'U' || Type == 'w')
Michael J. Spencer25b15772011-06-25 17:55:23 +0000119 Result = UnknownAddressOrSize;
120 else if (Section)
Danil Malyshevb0436a72011-11-29 17:40:10 +0000121 Result = Section->PointerToRawData + symb->Value;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000122 else
123 Result = symb->Value;
124 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000125}
126
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000127error_code COFFObjectFile::getSymbolAddress(DataRefImpl Symb,
128 uint64_t &Result) const {
129 const coff_symbol *symb = toSymb(Symb);
130 const coff_section *Section = NULL;
131 if (error_code ec = getSection(symb->SectionNumber, Section))
132 return ec;
133 char Type;
134 if (error_code ec = getSymbolNMTypeChar(Symb, Type))
135 return ec;
136 if (Type == 'U' || Type == 'w')
137 Result = UnknownAddressOrSize;
138 else if (Section)
Danil Malyshevb0436a72011-11-29 17:40:10 +0000139 Result = Section->VirtualAddress + symb->Value;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000140 else
Danil Malyshevb0436a72011-11-29 17:40:10 +0000141 Result = symb->Value;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000142 return object_error::success;
143}
144
145error_code COFFObjectFile::getSymbolType(DataRefImpl Symb,
Michael J. Spencer1130a792011-10-17 20:19:29 +0000146 SymbolRef::Type &Result) const {
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000147 const coff_symbol *symb = toSymb(Symb);
148 Result = SymbolRef::ST_Other;
149 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
150 symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) {
David Meyer2c677272012-02-29 02:11:55 +0000151 Result = SymbolRef::ST_Unknown;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000152 } else {
Michael J. Spencer5e3a0822011-10-18 19:31:59 +0000153 if (symb->getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION) {
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000154 Result = SymbolRef::ST_Function;
155 } else {
156 char Type;
157 if (error_code ec = getSymbolNMTypeChar(Symb, Type))
158 return ec;
159 if (Type == 'r' || Type == 'R') {
160 Result = SymbolRef::ST_Data;
161 }
162 }
163 }
164 return object_error::success;
165}
166
David Meyerc46255a2012-02-28 23:47:53 +0000167error_code COFFObjectFile::getSymbolFlags(DataRefImpl Symb,
168 uint32_t &Result) const {
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000169 const coff_symbol *symb = toSymb(Symb);
David Meyerc46255a2012-02-28 23:47:53 +0000170 Result = SymbolRef::SF_None;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000171
David Meyer2c677272012-02-29 02:11:55 +0000172 // TODO: Correctly set SF_FormatSpecific, SF_ThreadLocal, SF_Common
173
174 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
175 symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
176 Result |= SymbolRef::SF_Undefined;
David Meyerc46255a2012-02-28 23:47:53 +0000177
178 // TODO: This are certainly too restrictive.
179 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL)
180 Result |= SymbolRef::SF_Global;
181
182 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL)
183 Result |= SymbolRef::SF_Weak;
184
185 if (symb->SectionNumber == COFF::IMAGE_SYM_ABSOLUTE)
186 Result |= SymbolRef::SF_Absolute;
187
Michael J. Spencerc38c36a2011-10-17 23:54:22 +0000188 return object_error::success;
189}
190
Michael J. Spencer25b15772011-06-25 17:55:23 +0000191error_code COFFObjectFile::getSymbolSize(DataRefImpl Symb,
192 uint64_t &Result) const {
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000193 // FIXME: Return the correct size. This requires looking at all the symbols
194 // in the same section as this symbol, and looking for either the next
195 // symbol, or the end of the section.
Michael J. Spencer25b15772011-06-25 17:55:23 +0000196 const coff_symbol *symb = toSymb(Symb);
Michael J. Spencer64388ce2011-07-05 14:48:59 +0000197 const coff_section *Section = NULL;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000198 if (error_code ec = getSection(symb->SectionNumber, Section))
199 return ec;
200 char Type;
201 if (error_code ec = getSymbolNMTypeChar(Symb, Type))
202 return ec;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000203 if (Type == 'U' || Type == 'w')
Michael J. Spencer25b15772011-06-25 17:55:23 +0000204 Result = UnknownAddressOrSize;
205 else if (Section)
206 Result = Section->SizeOfRawData - symb->Value;
207 else
208 Result = 0;
209 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000210}
211
Michael J. Spencer25b15772011-06-25 17:55:23 +0000212error_code COFFObjectFile::getSymbolNMTypeChar(DataRefImpl Symb,
213 char &Result) const {
214 const coff_symbol *symb = toSymb(Symb);
215 StringRef name;
216 if (error_code ec = getSymbolName(Symb, name))
217 return ec;
218 char ret = StringSwitch<char>(name)
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000219 .StartsWith(".debug", 'N')
220 .StartsWith(".sxdata", 'N')
221 .Default('?');
222
Michael J. Spencer25b15772011-06-25 17:55:23 +0000223 if (ret != '?') {
224 Result = ret;
225 return object_error::success;
226 }
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000227
228 uint32_t Characteristics = 0;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000229 if (symb->SectionNumber > 0) {
Michael J. Spencer64388ce2011-07-05 14:48:59 +0000230 const coff_section *Section = NULL;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000231 if (error_code ec = getSection(symb->SectionNumber, Section))
232 return ec;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000233 Characteristics = Section->Characteristics;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000234 }
235
236 switch (symb->SectionNumber) {
237 case COFF::IMAGE_SYM_UNDEFINED:
238 // Check storage classes.
Michael J. Spencer25b15772011-06-25 17:55:23 +0000239 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL) {
240 Result = 'w';
241 return object_error::success; // Don't do ::toupper.
Michael J. Spencer11ba26d2011-11-16 23:36:12 +0000242 } else if (symb->Value != 0) // Check for common symbols.
243 ret = 'c';
244 else
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000245 ret = 'u';
246 break;
247 case COFF::IMAGE_SYM_ABSOLUTE:
248 ret = 'a';
249 break;
250 case COFF::IMAGE_SYM_DEBUG:
251 ret = 'n';
252 break;
253 default:
254 // Check section type.
255 if (Characteristics & COFF::IMAGE_SCN_CNT_CODE)
256 ret = 't';
257 else if ( Characteristics & COFF::IMAGE_SCN_MEM_READ
258 && ~Characteristics & COFF::IMAGE_SCN_MEM_WRITE) // Read only.
259 ret = 'r';
260 else if (Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
261 ret = 'd';
262 else if (Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
263 ret = 'b';
264 else if (Characteristics & COFF::IMAGE_SCN_LNK_INFO)
265 ret = 'i';
266
267 // Check for section symbol.
268 else if ( symb->StorageClass == COFF::IMAGE_SYM_CLASS_STATIC
269 && symb->Value == 0)
270 ret = 's';
271 }
272
273 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL)
Guy Benyei87d0b9e2013-02-12 21:21:59 +0000274 ret = ::toupper(static_cast<unsigned char>(ret));
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000275
Michael J. Spencer25b15772011-06-25 17:55:23 +0000276 Result = ret;
277 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000278}
279
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000280error_code COFFObjectFile::getSymbolSection(DataRefImpl Symb,
281 section_iterator &Result) const {
282 const coff_symbol *symb = toSymb(Symb);
283 if (symb->SectionNumber <= COFF::IMAGE_SYM_UNDEFINED)
284 Result = end_sections();
285 else {
Daniel Dunbara483fc82011-11-28 22:19:32 +0000286 const coff_section *sec = 0;
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000287 if (error_code ec = getSection(symb->SectionNumber, sec)) return ec;
288 DataRefImpl Sec;
289 Sec.p = reinterpret_cast<uintptr_t>(sec);
290 Result = section_iterator(SectionRef(Sec, this));
291 }
292 return object_error::success;
293}
294
Tim Northovera41dce32012-10-29 10:47:00 +0000295error_code COFFObjectFile::getSymbolValue(DataRefImpl Symb,
296 uint64_t &Val) const {
297 report_fatal_error("getSymbolValue unimplemented in COFFObjectFile");
298}
299
Michael J. Spencer25b15772011-06-25 17:55:23 +0000300error_code COFFObjectFile::getSectionNext(DataRefImpl Sec,
301 SectionRef &Result) const {
302 const coff_section *sec = toSec(Sec);
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000303 sec += 1;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000304 Sec.p = reinterpret_cast<uintptr_t>(sec);
305 Result = SectionRef(Sec, this);
306 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000307}
308
Michael J. Spencer25b15772011-06-25 17:55:23 +0000309error_code COFFObjectFile::getSectionName(DataRefImpl Sec,
310 StringRef &Result) const {
311 const coff_section *sec = toSec(Sec);
Michael J. Spencerb35a8962012-03-19 20:27:15 +0000312 return getSectionName(sec, Result);
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000313}
314
Michael J. Spencer25b15772011-06-25 17:55:23 +0000315error_code COFFObjectFile::getSectionAddress(DataRefImpl Sec,
316 uint64_t &Result) const {
317 const coff_section *sec = toSec(Sec);
318 Result = sec->VirtualAddress;
319 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000320}
321
Michael J. Spencer25b15772011-06-25 17:55:23 +0000322error_code COFFObjectFile::getSectionSize(DataRefImpl Sec,
323 uint64_t &Result) const {
324 const coff_section *sec = toSec(Sec);
325 Result = sec->SizeOfRawData;
326 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000327}
328
Michael J. Spencer25b15772011-06-25 17:55:23 +0000329error_code COFFObjectFile::getSectionContents(DataRefImpl Sec,
330 StringRef &Result) const {
331 const coff_section *sec = toSec(Sec);
Michael J. Spencer1f6e3f92012-03-19 20:27:37 +0000332 ArrayRef<uint8_t> Res;
333 error_code EC = getSectionContents(sec, Res);
334 Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size());
335 return EC;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000336}
337
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000338error_code COFFObjectFile::getSectionAlignment(DataRefImpl Sec,
339 uint64_t &Res) const {
340 const coff_section *sec = toSec(Sec);
341 if (!sec)
342 return object_error::parse_failed;
343 Res = uint64_t(1) << (((sec->Characteristics & 0x00F00000) >> 20) - 1);
344 return object_error::success;
345}
346
Michael J. Spencer25b15772011-06-25 17:55:23 +0000347error_code COFFObjectFile::isSectionText(DataRefImpl Sec,
348 bool &Result) const {
349 const coff_section *sec = toSec(Sec);
350 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
351 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000352}
353
Michael J. Spencer13afc5e2011-09-28 20:57:30 +0000354error_code COFFObjectFile::isSectionData(DataRefImpl Sec,
355 bool &Result) const {
356 const coff_section *sec = toSec(Sec);
357 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
358 return object_error::success;
359}
360
361error_code COFFObjectFile::isSectionBSS(DataRefImpl Sec,
362 bool &Result) const {
363 const coff_section *sec = toSec(Sec);
364 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
365 return object_error::success;
366}
367
Preston Gurdc68dda82012-04-12 20:13:57 +0000368error_code COFFObjectFile::isSectionRequiredForExecution(DataRefImpl Sec,
369 bool &Result) const {
370 // FIXME: Unimplemented
371 Result = true;
372 return object_error::success;
373}
374
375error_code COFFObjectFile::isSectionVirtual(DataRefImpl Sec,
376 bool &Result) const {
377 const coff_section *sec = toSec(Sec);
378 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
379 return object_error::success;
380}
381
382error_code COFFObjectFile::isSectionZeroInit(DataRefImpl Sec,
383 bool &Result) const {
Andrew Kaylor30b20eb2012-10-10 01:45:52 +0000384 // FIXME: Unimplemented.
Preston Gurdc68dda82012-04-12 20:13:57 +0000385 Result = false;
386 return object_error::success;
387}
388
Andrew Kaylor3a129c82012-10-10 01:41:33 +0000389error_code COFFObjectFile::isSectionReadOnlyData(DataRefImpl Sec,
390 bool &Result) const {
391 // FIXME: Unimplemented.
392 Result = false;
393 return object_error::success;
394}
395
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000396error_code COFFObjectFile::sectionContainsSymbol(DataRefImpl Sec,
397 DataRefImpl Symb,
398 bool &Result) const {
Michael J. Spencerbff6f862011-10-13 20:36:54 +0000399 const coff_section *sec = toSec(Sec);
400 const coff_symbol *symb = toSymb(Symb);
Daniel Dunbara483fc82011-11-28 22:19:32 +0000401 const coff_section *symb_sec = 0;
Michael J. Spencerbff6f862011-10-13 20:36:54 +0000402 if (error_code ec = getSection(symb->SectionNumber, symb_sec)) return ec;
403 if (symb_sec == sec)
404 Result = true;
405 else
406 Result = false;
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000407 return object_error::success;
408}
409
Rui Ueyama29552222013-09-27 21:47:05 +0000410relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Sec) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000411 const coff_section *sec = toSec(Sec);
412 DataRefImpl ret;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000413 if (sec->NumberOfRelocations == 0)
414 ret.p = 0;
415 else
416 ret.p = reinterpret_cast<uintptr_t>(base() + sec->PointerToRelocations);
417
418 return relocation_iterator(RelocationRef(ret, this));
419}
420
Rui Ueyama29552222013-09-27 21:47:05 +0000421relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Sec) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000422 const coff_section *sec = toSec(Sec);
423 DataRefImpl ret;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000424 if (sec->NumberOfRelocations == 0)
425 ret.p = 0;
426 else
427 ret.p = reinterpret_cast<uintptr_t>(
428 reinterpret_cast<const coff_relocation*>(
429 base() + sec->PointerToRelocations)
430 + sec->NumberOfRelocations);
431
432 return relocation_iterator(RelocationRef(ret, this));
433}
434
Rui Ueyamaa6610ee2013-09-27 21:04:00 +0000435// Initialize the pointer to the symbol table.
436error_code COFFObjectFile::initSymbolTablePtr() {
437 if (error_code ec = getObject(
438 SymbolTable, Data, base() + COFFHeader->PointerToSymbolTable,
439 COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
440 return ec;
441
442 // Find string table. The first four byte of the string table contains the
443 // total size of the string table, including the size field itself. If the
444 // string table is empty, the value of the first four byte would be 4.
445 const uint8_t *StringTableAddr =
446 base() + COFFHeader->PointerToSymbolTable +
447 COFFHeader->NumberOfSymbols * sizeof(coff_symbol);
448 const ulittle32_t *StringTableSizePtr;
449 if (error_code ec = getObject(StringTableSizePtr, Data, StringTableAddr))
450 return ec;
451 StringTableSize = *StringTableSizePtr;
452 if (error_code ec =
453 getObject(StringTable, Data, StringTableAddr, StringTableSize))
454 return ec;
455
456 // Check that the string table is null terminated if has any in it.
457 if (StringTableSize < 4 ||
458 (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0))
459 return object_error::parse_failed;
460 return object_error::success;
461}
462
463// Returns the file offset for the given RVA.
464error_code COFFObjectFile::getRvaPtr(uint32_t Rva, uintptr_t &Res) const {
465 error_code ec;
466 for (section_iterator i = begin_sections(), e = end_sections(); i != e;
467 i.increment(ec)) {
468 if (ec)
469 return ec;
470 const coff_section *Section = getCOFFSection(i);
471 uint32_t SectionStart = Section->VirtualAddress;
472 uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize;
473 if (SectionStart <= Rva && Rva < SectionEnd) {
474 uint32_t Offset = Rva - SectionStart;
475 Res = uintptr_t(base()) + Section->PointerToRawData + Offset;
476 return object_error::success;
477 }
478 }
479 return object_error::parse_failed;
480}
481
482// Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
483// table entry.
484error_code COFFObjectFile::
485getHintName(uint32_t Rva, uint16_t &Hint, StringRef &Name) const {
486 uintptr_t IntPtr = 0;
487 if (error_code ec = getRvaPtr(Rva, IntPtr))
488 return ec;
489 const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr);
490 Hint = *reinterpret_cast<const ulittle16_t *>(Ptr);
491 Name = StringRef(reinterpret_cast<const char *>(Ptr + 2));
492 return object_error::success;
493}
494
495// Find the import table.
496error_code COFFObjectFile::initImportTablePtr() {
497 // First, we get the RVA of the import table. If the file lacks a pointer to
498 // the import table, do nothing.
499 const data_directory *DataEntry;
500 if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
501 return object_error::success;
502
503 // Do nothing if the pointer to import table is NULL.
504 if (DataEntry->RelativeVirtualAddress == 0)
505 return object_error::success;
506
507 uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
508 NumberOfImportDirectory = DataEntry->Size /
509 sizeof(import_directory_table_entry);
510
511 // Find the section that contains the RVA. This is needed because the RVA is
512 // the import table's memory address which is different from its file offset.
513 uintptr_t IntPtr = 0;
514 if (error_code ec = getRvaPtr(ImportTableRva, IntPtr))
515 return ec;
516 ImportDirectory = reinterpret_cast<
517 const import_directory_table_entry *>(IntPtr);
518
519 // It's an error if there's no section containing the Import Table RVA.
520 return object_error::parse_failed;
521}
522
Michael J. Spencer001c9202011-06-25 17:54:50 +0000523COFFObjectFile::COFFObjectFile(MemoryBuffer *Object, error_code &ec)
Rafael Espindola2c6f9972013-04-07 16:40:00 +0000524 : ObjectFile(Binary::ID_COFF, Object)
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000525 , COFFHeader(0)
526 , PE32Header(0)
Rui Ueyama2f6c0482013-07-19 23:23:29 +0000527 , DataDirectory(0)
Michael J. Spencer7151ddd2011-11-08 23:34:07 +0000528 , SectionTable(0)
529 , SymbolTable(0)
530 , StringTable(0)
Rui Ueyamaa6610ee2013-09-27 21:04:00 +0000531 , StringTableSize(0)
532 , ImportDirectory(0)
533 , NumberOfImportDirectory(0) {
Michael J. Spencer25b15772011-06-25 17:55:23 +0000534 // Check that we at least have enough room for a header.
535 if (!checkSize(Data, ec, sizeof(coff_file_header))) return;
Eric Christopher539d8d82011-04-03 22:53:19 +0000536
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000537 // The current location in the file where we are looking at.
538 uint64_t CurPtr = 0;
539
540 // PE header is optional and is present only in executables. If it exists,
541 // it is placed right after COFF header.
542 bool hasPEHeader = false;
Eric Christopher539d8d82011-04-03 22:53:19 +0000543
Michael J. Spencer25b15772011-06-25 17:55:23 +0000544 // Check if this is a PE/COFF file.
Michael J. Spencer001c9202011-06-25 17:54:50 +0000545 if (base()[0] == 0x4d && base()[1] == 0x5a) {
Eric Christopher539d8d82011-04-03 22:53:19 +0000546 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
547 // PE signature to find 'normal' COFF header.
Michael J. Spencer25b15772011-06-25 17:55:23 +0000548 if (!checkSize(Data, ec, 0x3c + 8)) return;
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000549 CurPtr = *reinterpret_cast<const ulittle16_t *>(base() + 0x3c);
550 // Check the PE magic bytes. ("PE\0\0")
551 if (std::memcmp(base() + CurPtr, "PE\0\0", 4) != 0) {
Michael J. Spencer25b15772011-06-25 17:55:23 +0000552 ec = object_error::parse_failed;
553 return;
554 }
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000555 CurPtr += 4; // Skip the PE magic bytes.
556 hasPEHeader = true;
Eric Christopher539d8d82011-04-03 22:53:19 +0000557 }
558
Rui Ueyama2f6c0482013-07-19 23:23:29 +0000559 if ((ec = getObject(COFFHeader, Data, base() + CurPtr)))
Michael J. Spencer25b15772011-06-25 17:55:23 +0000560 return;
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000561 CurPtr += sizeof(coff_file_header);
562
563 if (hasPEHeader) {
Rui Ueyama2f6c0482013-07-19 23:23:29 +0000564 if ((ec = getObject(PE32Header, Data, base() + CurPtr)))
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000565 return;
Rui Ueyama2f6c0482013-07-19 23:23:29 +0000566 if (PE32Header->Magic != 0x10b) {
567 // We only support PE32. If this is PE32 (not PE32+), the magic byte
568 // should be 0x10b. If this is not PE32, continue as if there's no PE
569 // header in this file.
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000570 PE32Header = 0;
Rui Ueyama2f6c0482013-07-19 23:23:29 +0000571 } else if (PE32Header->NumberOfRvaAndSize > 0) {
572 const uint8_t *addr = base() + CurPtr + sizeof(pe32_header);
573 uint64_t size = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
574 if ((ec = getObject(DataDirectory, Data, addr, size)))
575 return;
576 }
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000577 CurPtr += COFFHeader->SizeOfOptionalHeader;
578 }
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000579
Rui Ueyama2f6c0482013-07-19 23:23:29 +0000580 if ((ec = getObject(SectionTable, Data, base() + CurPtr,
581 COFFHeader->NumberOfSections * sizeof(coff_section))))
Michael J. Spencer25b15772011-06-25 17:55:23 +0000582 return;
583
Rui Ueyamaa6610ee2013-09-27 21:04:00 +0000584 // Initialize the pointer to the symbol table.
585 if (COFFHeader->PointerToSymbolTable != 0)
586 if ((ec = initSymbolTablePtr()))
Michael J. Spencer7151ddd2011-11-08 23:34:07 +0000587 return;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000588
Rui Ueyamaa6610ee2013-09-27 21:04:00 +0000589 // Initialize the pointer to the beginning of the import table.
590 if ((ec = initImportTablePtr()))
591 return;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000592
Michael J. Spencer25b15772011-06-25 17:55:23 +0000593 ec = object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000594}
595
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000596symbol_iterator COFFObjectFile::begin_symbols() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000597 DataRefImpl ret;
Rui Ueyamaa6610ee2013-09-27 21:04:00 +0000598 ret.p = reinterpret_cast<uintptr_t>(SymbolTable);
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000599 return symbol_iterator(SymbolRef(ret, this));
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000600}
601
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000602symbol_iterator COFFObjectFile::end_symbols() const {
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000603 // The symbol table ends where the string table begins.
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000604 DataRefImpl ret;
Rui Ueyamaa6610ee2013-09-27 21:04:00 +0000605 ret.p = reinterpret_cast<uintptr_t>(StringTable);
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000606 return symbol_iterator(SymbolRef(ret, this));
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000607}
608
Michael J. Spencerdfa18962012-02-28 00:40:37 +0000609symbol_iterator COFFObjectFile::begin_dynamic_symbols() const {
610 // TODO: implement
611 report_fatal_error("Dynamic symbols unimplemented in COFFObjectFile");
612}
613
614symbol_iterator COFFObjectFile::end_dynamic_symbols() const {
615 // TODO: implement
616 report_fatal_error("Dynamic symbols unimplemented in COFFObjectFile");
617}
618
David Meyer5c2b4ea2012-03-01 01:36:50 +0000619library_iterator COFFObjectFile::begin_libraries_needed() const {
620 // TODO: implement
621 report_fatal_error("Libraries needed unimplemented in COFFObjectFile");
622}
623
624library_iterator COFFObjectFile::end_libraries_needed() const {
625 // TODO: implement
626 report_fatal_error("Libraries needed unimplemented in COFFObjectFile");
627}
628
David Meyer97f77872012-03-01 22:19:54 +0000629StringRef COFFObjectFile::getLoadName() const {
630 // COFF does not have this field.
631 return "";
632}
633
Rui Ueyama29552222013-09-27 21:47:05 +0000634import_directory_iterator COFFObjectFile::import_directory_begin() const {
Rui Ueyamaa6610ee2013-09-27 21:04:00 +0000635 DataRefImpl Imp;
636 Imp.p = reinterpret_cast<uintptr_t>(ImportDirectory);
637 return import_directory_iterator(ImportDirectoryEntryRef(Imp, this));
638}
639
Rui Ueyama29552222013-09-27 21:47:05 +0000640import_directory_iterator COFFObjectFile::import_directory_end() const {
Rui Ueyamaa6610ee2013-09-27 21:04:00 +0000641 DataRefImpl Imp;
642 if (ImportDirectory) {
643 Imp.p = reinterpret_cast<uintptr_t>(
644 ImportDirectory + (NumberOfImportDirectory - 1));
645 } else {
646 Imp.p = 0;
647 }
648 return import_directory_iterator(ImportDirectoryEntryRef(Imp, this));
649}
David Meyer97f77872012-03-01 22:19:54 +0000650
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000651section_iterator COFFObjectFile::begin_sections() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000652 DataRefImpl ret;
Rui Ueyamaa6610ee2013-09-27 21:04:00 +0000653 ret.p = reinterpret_cast<uintptr_t>(SectionTable);
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000654 return section_iterator(SectionRef(ret, this));
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000655}
656
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000657section_iterator COFFObjectFile::end_sections() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000658 DataRefImpl ret;
Rui Ueyamaa6610ee2013-09-27 21:04:00 +0000659 ret.p = reinterpret_cast<uintptr_t>(SectionTable + COFFHeader->NumberOfSections);
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000660 return section_iterator(SectionRef(ret, this));
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000661}
662
663uint8_t COFFObjectFile::getBytesInAddress() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000664 return getArch() == Triple::x86_64 ? 8 : 4;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000665}
666
667StringRef COFFObjectFile::getFileFormatName() const {
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000668 switch(COFFHeader->Machine) {
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000669 case COFF::IMAGE_FILE_MACHINE_I386:
670 return "COFF-i386";
671 case COFF::IMAGE_FILE_MACHINE_AMD64:
672 return "COFF-x86-64";
673 default:
674 return "COFF-<unknown arch>";
675 }
676}
677
678unsigned COFFObjectFile::getArch() const {
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000679 switch(COFFHeader->Machine) {
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000680 case COFF::IMAGE_FILE_MACHINE_I386:
681 return Triple::x86;
682 case COFF::IMAGE_FILE_MACHINE_AMD64:
683 return Triple::x86_64;
684 default:
685 return Triple::UnknownArch;
686 }
687}
688
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000689// This method is kept here because lld uses this. As soon as we make
690// lld to use getCOFFHeader, this method will be removed.
Michael J. Spencer0e752cb2011-10-17 23:53:56 +0000691error_code COFFObjectFile::getHeader(const coff_file_header *&Res) const {
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000692 return getCOFFHeader(Res);
693}
694
695error_code COFFObjectFile::getCOFFHeader(const coff_file_header *&Res) const {
696 Res = COFFHeader;
697 return object_error::success;
698}
699
700error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
701 Res = PE32Header;
Michael J. Spencer0e752cb2011-10-17 23:53:56 +0000702 return object_error::success;
703}
704
Rui Ueyama2f6c0482013-07-19 23:23:29 +0000705error_code COFFObjectFile::getDataDirectory(uint32_t index,
706 const data_directory *&Res) const {
707 // Error if if there's no data directory or the index is out of range.
708 if (!DataDirectory || index > PE32Header->NumberOfRvaAndSize)
709 return object_error::parse_failed;
710 Res = &DataDirectory[index];
711 return object_error::success;
712}
713
Michael J. Spencer25b15772011-06-25 17:55:23 +0000714error_code COFFObjectFile::getSection(int32_t index,
715 const coff_section *&Result) const {
716 // Check for special index values.
717 if (index == COFF::IMAGE_SYM_UNDEFINED ||
718 index == COFF::IMAGE_SYM_ABSOLUTE ||
719 index == COFF::IMAGE_SYM_DEBUG)
720 Result = NULL;
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000721 else if (index > 0 && index <= COFFHeader->NumberOfSections)
Michael J. Spencer25b15772011-06-25 17:55:23 +0000722 // We already verified the section table data, so no need to check again.
723 Result = SectionTable + (index - 1);
724 else
725 return object_error::parse_failed;
726 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000727}
728
Michael J. Spencer25b15772011-06-25 17:55:23 +0000729error_code COFFObjectFile::getString(uint32_t offset,
730 StringRef &Result) const {
731 if (StringTableSize <= 4)
732 // Tried to get a string from an empty string table.
733 return object_error::parse_failed;
734 if (offset >= StringTableSize)
735 return object_error::unexpected_eof;
736 Result = StringRef(StringTable + offset);
737 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000738}
739
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000740error_code COFFObjectFile::getSymbol(uint32_t index,
741 const coff_symbol *&Result) const {
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000742 if (index < COFFHeader->NumberOfSymbols)
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000743 Result = SymbolTable + index;
744 else
745 return object_error::parse_failed;
746 return object_error::success;
747}
748
Michael J. Spencer0e752cb2011-10-17 23:53:56 +0000749error_code COFFObjectFile::getSymbolName(const coff_symbol *symbol,
750 StringRef &Res) const {
751 // Check for string table entry. First 4 bytes are 0.
752 if (symbol->Name.Offset.Zeroes == 0) {
753 uint32_t Offset = symbol->Name.Offset.Offset;
754 if (error_code ec = getString(Offset, Res))
755 return ec;
756 return object_error::success;
757 }
758
759 if (symbol->Name.ShortName[7] == 0)
760 // Null terminated, let ::strlen figure out the length.
761 Res = StringRef(symbol->Name.ShortName);
762 else
763 // Not null terminated, use all 8 bytes.
764 Res = StringRef(symbol->Name.ShortName, 8);
765 return object_error::success;
766}
767
Marshall Clowd4d03e02012-06-15 01:08:25 +0000768ArrayRef<uint8_t> COFFObjectFile::getSymbolAuxData(
769 const coff_symbol *symbol) const {
770 const uint8_t *aux = NULL;
Rui Ueyamaa6610ee2013-09-27 21:04:00 +0000771
Marshall Clowd4d03e02012-06-15 01:08:25 +0000772 if ( symbol->NumberOfAuxSymbols > 0 ) {
773 // AUX data comes immediately after the symbol in COFF
774 aux = reinterpret_cast<const uint8_t *>(symbol + 1);
775# ifndef NDEBUG
776 // Verify that the aux symbol points to a valid entry in the symbol table.
777 uintptr_t offset = uintptr_t(aux) - uintptr_t(base());
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000778 if (offset < COFFHeader->PointerToSymbolTable
779 || offset >= COFFHeader->PointerToSymbolTable
780 + (COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
Marshall Clowd4d03e02012-06-15 01:08:25 +0000781 report_fatal_error("Aux Symbol data was outside of symbol table.");
782
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000783 assert((offset - COFFHeader->PointerToSymbolTable) % sizeof(coff_symbol)
Marshall Clowd4d03e02012-06-15 01:08:25 +0000784 == 0 && "Aux Symbol data did not point to the beginning of a symbol");
Marshall Clowd4d03e02012-06-15 01:08:25 +0000785# endif
Marshall Clow45aad162012-06-15 01:15:47 +0000786 }
Marshall Clowd4d03e02012-06-15 01:08:25 +0000787 return ArrayRef<uint8_t>(aux, symbol->NumberOfAuxSymbols * sizeof(coff_symbol));
788}
789
Michael J. Spencerb35a8962012-03-19 20:27:15 +0000790error_code COFFObjectFile::getSectionName(const coff_section *Sec,
791 StringRef &Res) const {
792 StringRef Name;
793 if (Sec->Name[7] == 0)
794 // Null terminated, let ::strlen figure out the length.
795 Name = Sec->Name;
796 else
797 // Not null terminated, use all 8 bytes.
798 Name = StringRef(Sec->Name, 8);
799
800 // Check for string table entry. First byte is '/'.
801 if (Name[0] == '/') {
802 uint32_t Offset;
803 if (Name.substr(1).getAsInteger(10, Offset))
804 return object_error::parse_failed;
805 if (error_code ec = getString(Offset, Name))
806 return ec;
807 }
808
809 Res = Name;
810 return object_error::success;
811}
812
Michael J. Spencer1f6e3f92012-03-19 20:27:37 +0000813error_code COFFObjectFile::getSectionContents(const coff_section *Sec,
814 ArrayRef<uint8_t> &Res) const {
815 // The only thing that we need to verify is that the contents is contained
816 // within the file bounds. We don't need to make sure it doesn't cover other
817 // data, as there's nothing that says that is not allowed.
818 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
819 uintptr_t ConEnd = ConStart + Sec->SizeOfRawData;
820 if (ConEnd > uintptr_t(Data->getBufferEnd()))
821 return object_error::parse_failed;
822 Res = ArrayRef<uint8_t>(reinterpret_cast<const unsigned char*>(ConStart),
823 Sec->SizeOfRawData);
824 return object_error::success;
825}
826
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000827const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000828 return reinterpret_cast<const coff_relocation*>(Rel.p);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000829}
830error_code COFFObjectFile::getRelocationNext(DataRefImpl Rel,
831 RelocationRef &Res) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000832 Rel.p = reinterpret_cast<uintptr_t>(
833 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000834 Res = RelocationRef(Rel, this);
835 return object_error::success;
836}
837error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel,
838 uint64_t &Res) const {
Rafael Espindola956ca722013-04-25 12:28:45 +0000839 report_fatal_error("getRelocationAddress not implemented in COFFObjectFile");
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000840}
Danil Malyshevb0436a72011-11-29 17:40:10 +0000841error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel,
842 uint64_t &Res) const {
843 Res = toRel(Rel)->VirtualAddress;
844 return object_error::success;
845}
Rafael Espindola6c1202c2013-06-05 01:33:53 +0000846symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000847 const coff_relocation* R = toRel(Rel);
848 DataRefImpl Symb;
849 Symb.p = reinterpret_cast<uintptr_t>(SymbolTable + R->SymbolTableIndex);
Rafael Espindola6c1202c2013-06-05 01:33:53 +0000850 return symbol_iterator(SymbolRef(Symb, this));
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000851}
852error_code COFFObjectFile::getRelocationType(DataRefImpl Rel,
Owen Anderson9472b8d2011-10-26 17:08:49 +0000853 uint64_t &Res) const {
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000854 const coff_relocation* R = toRel(Rel);
855 Res = R->Type;
856 return object_error::success;
857}
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000858
Marshall Clowd4d03e02012-06-15 01:08:25 +0000859const coff_section *COFFObjectFile::getCOFFSection(section_iterator &It) const {
860 return toSec(It->getRawDataRefImpl());
861}
862
863const coff_symbol *COFFObjectFile::getCOFFSymbol(symbol_iterator &It) const {
864 return toSymb(It->getRawDataRefImpl());
865}
866
Marshall Clow9ac0f1d2012-06-18 19:47:16 +0000867const coff_relocation *COFFObjectFile::getCOFFRelocation(
868 relocation_iterator &It) const {
869 return toRel(It->getRawDataRefImpl());
870}
871
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000872#define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(enum) \
873 case COFF::enum: res = #enum; break;
874
875error_code COFFObjectFile::getRelocationTypeName(DataRefImpl Rel,
876 SmallVectorImpl<char> &Result) const {
877 const coff_relocation *reloc = toRel(Rel);
878 StringRef res;
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000879 switch (COFFHeader->Machine) {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000880 case COFF::IMAGE_FILE_MACHINE_AMD64:
881 switch (reloc->Type) {
882 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
883 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
884 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
885 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
886 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
887 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
888 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
889 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
890 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
891 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
892 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
893 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
894 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
895 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
896 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
897 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
898 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
899 default:
900 res = "Unknown";
901 }
902 break;
903 case COFF::IMAGE_FILE_MACHINE_I386:
904 switch (reloc->Type) {
905 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
906 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
907 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
908 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
909 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
910 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
911 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
912 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
913 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
914 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
915 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
916 default:
917 res = "Unknown";
918 }
919 break;
920 default:
921 res = "Unknown";
922 }
923 Result.append(res.begin(), res.end());
924 return object_error::success;
925}
926
927#undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
928
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000929error_code COFFObjectFile::getRelocationValueString(DataRefImpl Rel,
930 SmallVectorImpl<char> &Result) const {
931 const coff_relocation *reloc = toRel(Rel);
NAKAMURA Takumi48f248a2011-10-08 11:22:53 +0000932 const coff_symbol *symb = 0;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000933 if (error_code ec = getSymbol(reloc->SymbolTableIndex, symb)) return ec;
934 DataRefImpl sym;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000935 sym.p = reinterpret_cast<uintptr_t>(symb);
936 StringRef symname;
937 if (error_code ec = getSymbolName(sym, symname)) return ec;
938 Result.append(symname.begin(), symname.end());
939 return object_error::success;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000940}
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000941
David Meyer5c2b4ea2012-03-01 01:36:50 +0000942error_code COFFObjectFile::getLibraryNext(DataRefImpl LibData,
943 LibraryRef &Result) const {
944 report_fatal_error("getLibraryNext not implemented in COFFObjectFile");
945}
946
947error_code COFFObjectFile::getLibraryPath(DataRefImpl LibData,
948 StringRef &Result) const {
949 report_fatal_error("getLibraryPath not implemented in COFFObjectFile");
950}
951
Rui Ueyamaa6610ee2013-09-27 21:04:00 +0000952bool ImportDirectoryEntryRef::
953operator==(const ImportDirectoryEntryRef &Other) const {
954 return ImportDirectoryPimpl == Other.ImportDirectoryPimpl;
955}
956
957static const import_directory_table_entry *toImportEntry(DataRefImpl Imp) {
958 return reinterpret_cast<const import_directory_table_entry *>(Imp.p);
959}
960
961error_code
962ImportDirectoryEntryRef::getNext(ImportDirectoryEntryRef &Result) const {
963 const import_directory_table_entry *Dir = toImportEntry(ImportDirectoryPimpl);
964 Dir += 1;
965 DataRefImpl Next;
966 Next.p = reinterpret_cast<uintptr_t>(Dir);
967 Result = ImportDirectoryEntryRef(Next, OwningObject);
968 return object_error::success;
969}
970
971error_code ImportDirectoryEntryRef::
972getImportTableEntry(const import_directory_table_entry *&Result) const {
973 Result = toImportEntry(ImportDirectoryPimpl);
974 return object_error::success;
975}
976
977error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
978 const import_directory_table_entry *Dir = toImportEntry(ImportDirectoryPimpl);
979 uintptr_t IntPtr = 0;
980 if (error_code ec = OwningObject->getRvaPtr(Dir->NameRVA, IntPtr))
981 return ec;
982 const char *Ptr = reinterpret_cast<const char *>(IntPtr);
983 Result = StringRef(Ptr);
984 return object_error::success;
985}
986
987error_code ImportDirectoryEntryRef::getImportLookupEntry(
988 const import_lookup_table_entry32 *&Result) const {
989 const import_directory_table_entry *Dir = toImportEntry(ImportDirectoryPimpl);
990 uintptr_t IntPtr = 0;
991 if (error_code ec = OwningObject->getRvaPtr(
992 Dir->ImportLookupTableRVA, IntPtr))
993 return ec;
994 Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr);
995 return object_error::success;
996}
997
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000998namespace llvm {
999
1000 ObjectFile *ObjectFile::createCOFFObjectFile(MemoryBuffer *Object) {
Michael J. Spencer001c9202011-06-25 17:54:50 +00001001 error_code ec;
1002 return new COFFObjectFile(Object, ec);
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +00001003 }
1004
1005} // end namespace llvm