blob: 860d8123a7b47a03fe11d8aa63e1ef13490d532a [file] [log] [blame]
Michael J. Spencer8e90ada2011-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. Spencerec29b122011-06-25 17:54:50 +000014#include "llvm/Object/COFF.h"
Michael J. Spencer9da9e692012-03-19 20:27:37 +000015#include "llvm/ADT/ArrayRef.h"
Michael J. Spencere5fd0042011-10-07 19:25:32 +000016#include "llvm/ADT/SmallString.h"
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000017#include "llvm/ADT/StringSwitch.h"
18#include "llvm/ADT/Triple.h"
Rui Ueyamac2bed422013-09-27 21:04:00 +000019#include "llvm/Support/Debug.h"
20#include "llvm/Support/raw_ostream.h"
Will Dietz981af002013-10-12 00:55:57 +000021#include <cctype>
Michael J. Spencer8e90ada2011-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. Spencer1d6167f2011-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. Spencer8e90ada2011-01-20 06:38:34 +000041}
42
Rui Ueyamaed64342b2013-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. Spencer1d6167f2011-06-25 17:55:23 +000053 }
Rui Ueyamaed64342b2013-07-19 23:23:29 +000054 Obj = reinterpret_cast<const T *>(Addr);
55 return object_error::success;
Michael J. Spencer1d6167f2011-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 Ueyama82ebd8e2013-06-12 19:10:33 +000065 if (offset < COFFHeader->PointerToSymbolTable
66 || offset >= COFFHeader->PointerToSymbolTable
67 + (COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000068 report_fatal_error("Symbol was outside of symbol table.");
69
Rui Ueyama82ebd8e2013-06-12 19:10:33 +000070 assert((offset - COFFHeader->PointerToSymbolTable) % sizeof(coff_symbol)
Michael J. Spencer1d6167f2011-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 Ueyama82ebd8e2013-06-12 19:10:33 +000083 || addr >= (SectionTable + COFFHeader->NumberOfSections))
Michael J. Spencer1d6167f2011-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. Spencer89a7a5e2011-10-17 23:53:56 +0000106 return getSymbolName(symb, Result);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000107}
108
Danil Malyshevcbe72fc2011-11-29 17:40:10 +0000109error_code COFFObjectFile::getSymbolFileOffset(DataRefImpl Symb,
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000110 uint64_t &Result) const {
111 const coff_symbol *symb = toSymb(Symb);
Michael J. Spencer5ebaed22011-07-05 14:48:59 +0000112 const coff_section *Section = NULL;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000113 if (error_code ec = getSection(symb->SectionNumber, Section))
114 return ec;
Rafael Espindolae62ab112013-11-02 18:07:48 +0000115
116 if (symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000117 Result = UnknownAddressOrSize;
118 else if (Section)
Danil Malyshevcbe72fc2011-11-29 17:40:10 +0000119 Result = Section->PointerToRawData + symb->Value;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000120 else
121 Result = symb->Value;
122 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000123}
124
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000125error_code COFFObjectFile::getSymbolAddress(DataRefImpl Symb,
126 uint64_t &Result) const {
127 const coff_symbol *symb = toSymb(Symb);
128 const coff_section *Section = NULL;
129 if (error_code ec = getSection(symb->SectionNumber, Section))
130 return ec;
Rafael Espindolae62ab112013-11-02 18:07:48 +0000131
132 if (symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000133 Result = UnknownAddressOrSize;
134 else if (Section)
Danil Malyshevcbe72fc2011-11-29 17:40:10 +0000135 Result = Section->VirtualAddress + symb->Value;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000136 else
Danil Malyshevcbe72fc2011-11-29 17:40:10 +0000137 Result = symb->Value;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000138 return object_error::success;
139}
140
141error_code COFFObjectFile::getSymbolType(DataRefImpl Symb,
Michael J. Spencerd3946672011-10-17 20:19:29 +0000142 SymbolRef::Type &Result) const {
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000143 const coff_symbol *symb = toSymb(Symb);
144 Result = SymbolRef::ST_Other;
145 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
146 symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) {
David Meyer7e4b9762012-02-29 02:11:55 +0000147 Result = SymbolRef::ST_Unknown;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000148 } else {
Michael J. Spencer097be9f2011-10-18 19:31:59 +0000149 if (symb->getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION) {
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000150 Result = SymbolRef::ST_Function;
151 } else {
152 char Type;
153 if (error_code ec = getSymbolNMTypeChar(Symb, Type))
154 return ec;
155 if (Type == 'r' || Type == 'R') {
156 Result = SymbolRef::ST_Data;
157 }
158 }
159 }
160 return object_error::success;
161}
162
David Meyer1df4b842012-02-28 23:47:53 +0000163error_code COFFObjectFile::getSymbolFlags(DataRefImpl Symb,
164 uint32_t &Result) const {
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000165 const coff_symbol *symb = toSymb(Symb);
David Meyer1df4b842012-02-28 23:47:53 +0000166 Result = SymbolRef::SF_None;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000167
David Meyer7e4b9762012-02-29 02:11:55 +0000168 // TODO: Correctly set SF_FormatSpecific, SF_ThreadLocal, SF_Common
169
170 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
171 symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
172 Result |= SymbolRef::SF_Undefined;
David Meyer1df4b842012-02-28 23:47:53 +0000173
174 // TODO: This are certainly too restrictive.
175 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL)
176 Result |= SymbolRef::SF_Global;
177
178 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL)
179 Result |= SymbolRef::SF_Weak;
180
181 if (symb->SectionNumber == COFF::IMAGE_SYM_ABSOLUTE)
182 Result |= SymbolRef::SF_Absolute;
183
Michael J. Spencer01759752011-10-17 23:54:22 +0000184 return object_error::success;
185}
186
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000187error_code COFFObjectFile::getSymbolSize(DataRefImpl Symb,
188 uint64_t &Result) const {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000189 // FIXME: Return the correct size. This requires looking at all the symbols
190 // in the same section as this symbol, and looking for either the next
191 // symbol, or the end of the section.
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000192 const coff_symbol *symb = toSymb(Symb);
Michael J. Spencer5ebaed22011-07-05 14:48:59 +0000193 const coff_section *Section = NULL;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000194 if (error_code ec = getSection(symb->SectionNumber, Section))
195 return ec;
Rafael Espindolae62ab112013-11-02 18:07:48 +0000196
197 if (symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000198 Result = UnknownAddressOrSize;
199 else if (Section)
200 Result = Section->SizeOfRawData - symb->Value;
201 else
202 Result = 0;
203 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000204}
205
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000206error_code COFFObjectFile::getSymbolNMTypeChar(DataRefImpl Symb,
207 char &Result) const {
208 const coff_symbol *symb = toSymb(Symb);
209 StringRef name;
210 if (error_code ec = getSymbolName(Symb, name))
211 return ec;
212 char ret = StringSwitch<char>(name)
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000213 .StartsWith(".debug", 'N')
214 .StartsWith(".sxdata", 'N')
215 .Default('?');
216
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000217 if (ret != '?') {
218 Result = ret;
219 return object_error::success;
220 }
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000221
222 uint32_t Characteristics = 0;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000223 if (symb->SectionNumber > 0) {
Michael J. Spencer5ebaed22011-07-05 14:48:59 +0000224 const coff_section *Section = NULL;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000225 if (error_code ec = getSection(symb->SectionNumber, Section))
226 return ec;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000227 Characteristics = Section->Characteristics;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000228 }
229
230 switch (symb->SectionNumber) {
231 case COFF::IMAGE_SYM_UNDEFINED:
232 // Check storage classes.
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000233 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL) {
234 Result = 'w';
235 return object_error::success; // Don't do ::toupper.
Michael J. Spencerd27d51f2011-11-16 23:36:12 +0000236 } else if (symb->Value != 0) // Check for common symbols.
237 ret = 'c';
238 else
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000239 ret = 'u';
240 break;
241 case COFF::IMAGE_SYM_ABSOLUTE:
242 ret = 'a';
243 break;
244 case COFF::IMAGE_SYM_DEBUG:
245 ret = 'n';
246 break;
247 default:
248 // Check section type.
249 if (Characteristics & COFF::IMAGE_SCN_CNT_CODE)
250 ret = 't';
251 else if ( Characteristics & COFF::IMAGE_SCN_MEM_READ
252 && ~Characteristics & COFF::IMAGE_SCN_MEM_WRITE) // Read only.
253 ret = 'r';
254 else if (Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
255 ret = 'd';
256 else if (Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
257 ret = 'b';
258 else if (Characteristics & COFF::IMAGE_SCN_LNK_INFO)
259 ret = 'i';
260
261 // Check for section symbol.
262 else if ( symb->StorageClass == COFF::IMAGE_SYM_CLASS_STATIC
263 && symb->Value == 0)
264 ret = 's';
265 }
266
267 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL)
Guy Benyei83c74e92013-02-12 21:21:59 +0000268 ret = ::toupper(static_cast<unsigned char>(ret));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000269
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000270 Result = ret;
271 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000272}
273
Michael J. Spencer3217315392011-10-17 23:54:46 +0000274error_code COFFObjectFile::getSymbolSection(DataRefImpl Symb,
275 section_iterator &Result) const {
276 const coff_symbol *symb = toSymb(Symb);
277 if (symb->SectionNumber <= COFF::IMAGE_SYM_UNDEFINED)
278 Result = end_sections();
279 else {
Daniel Dunbar9339d452011-11-28 22:19:32 +0000280 const coff_section *sec = 0;
Michael J. Spencer3217315392011-10-17 23:54:46 +0000281 if (error_code ec = getSection(symb->SectionNumber, sec)) return ec;
282 DataRefImpl Sec;
283 Sec.p = reinterpret_cast<uintptr_t>(sec);
284 Result = section_iterator(SectionRef(Sec, this));
285 }
286 return object_error::success;
287}
288
Tim Northover4f223bf72012-10-29 10:47:00 +0000289error_code COFFObjectFile::getSymbolValue(DataRefImpl Symb,
290 uint64_t &Val) const {
291 report_fatal_error("getSymbolValue unimplemented in COFFObjectFile");
292}
293
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000294error_code COFFObjectFile::getSectionNext(DataRefImpl Sec,
295 SectionRef &Result) const {
296 const coff_section *sec = toSec(Sec);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000297 sec += 1;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000298 Sec.p = reinterpret_cast<uintptr_t>(sec);
299 Result = SectionRef(Sec, this);
300 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000301}
302
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000303error_code COFFObjectFile::getSectionName(DataRefImpl Sec,
304 StringRef &Result) const {
305 const coff_section *sec = toSec(Sec);
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000306 return getSectionName(sec, Result);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000307}
308
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000309error_code COFFObjectFile::getSectionAddress(DataRefImpl Sec,
310 uint64_t &Result) const {
311 const coff_section *sec = toSec(Sec);
312 Result = sec->VirtualAddress;
313 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000314}
315
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000316error_code COFFObjectFile::getSectionSize(DataRefImpl Sec,
317 uint64_t &Result) const {
318 const coff_section *sec = toSec(Sec);
319 Result = sec->SizeOfRawData;
320 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000321}
322
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000323error_code COFFObjectFile::getSectionContents(DataRefImpl Sec,
324 StringRef &Result) const {
325 const coff_section *sec = toSec(Sec);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000326 ArrayRef<uint8_t> Res;
327 error_code EC = getSectionContents(sec, Res);
328 Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size());
329 return EC;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000330}
331
Michael J. Spencer79894602011-10-10 21:55:43 +0000332error_code COFFObjectFile::getSectionAlignment(DataRefImpl Sec,
333 uint64_t &Res) const {
334 const coff_section *sec = toSec(Sec);
335 if (!sec)
336 return object_error::parse_failed;
337 Res = uint64_t(1) << (((sec->Characteristics & 0x00F00000) >> 20) - 1);
338 return object_error::success;
339}
340
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000341error_code COFFObjectFile::isSectionText(DataRefImpl Sec,
342 bool &Result) const {
343 const coff_section *sec = toSec(Sec);
344 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
345 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000346}
347
Michael J. Spencer800619f2011-09-28 20:57:30 +0000348error_code COFFObjectFile::isSectionData(DataRefImpl Sec,
349 bool &Result) const {
350 const coff_section *sec = toSec(Sec);
351 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
352 return object_error::success;
353}
354
355error_code COFFObjectFile::isSectionBSS(DataRefImpl Sec,
356 bool &Result) const {
357 const coff_section *sec = toSec(Sec);
358 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
359 return object_error::success;
360}
361
Preston Gurd2138ef62012-04-12 20:13:57 +0000362error_code COFFObjectFile::isSectionRequiredForExecution(DataRefImpl Sec,
363 bool &Result) const {
364 // FIXME: Unimplemented
365 Result = true;
366 return object_error::success;
367}
368
369error_code COFFObjectFile::isSectionVirtual(DataRefImpl Sec,
370 bool &Result) const {
371 const coff_section *sec = toSec(Sec);
372 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
373 return object_error::success;
374}
375
376error_code COFFObjectFile::isSectionZeroInit(DataRefImpl Sec,
377 bool &Result) const {
Andrew Kaylorb96a3202012-10-10 01:45:52 +0000378 // FIXME: Unimplemented.
Preston Gurd2138ef62012-04-12 20:13:57 +0000379 Result = false;
380 return object_error::success;
381}
382
Andrew Kaylor3f31fa02012-10-10 01:41:33 +0000383error_code COFFObjectFile::isSectionReadOnlyData(DataRefImpl Sec,
384 bool &Result) const {
385 // FIXME: Unimplemented.
386 Result = false;
387 return object_error::success;
388}
389
Benjamin Kramerf6f3e812011-07-15 18:39:21 +0000390error_code COFFObjectFile::sectionContainsSymbol(DataRefImpl Sec,
391 DataRefImpl Symb,
392 bool &Result) const {
Michael J. Spencer9a288512011-10-13 20:36:54 +0000393 const coff_section *sec = toSec(Sec);
394 const coff_symbol *symb = toSymb(Symb);
Daniel Dunbar9339d452011-11-28 22:19:32 +0000395 const coff_section *symb_sec = 0;
Michael J. Spencer9a288512011-10-13 20:36:54 +0000396 if (error_code ec = getSection(symb->SectionNumber, symb_sec)) return ec;
397 if (symb_sec == sec)
398 Result = true;
399 else
400 Result = false;
Benjamin Kramerf6f3e812011-07-15 18:39:21 +0000401 return object_error::success;
402}
403
Rui Ueyamabc654b12013-09-27 21:47:05 +0000404relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Sec) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000405 const coff_section *sec = toSec(Sec);
406 DataRefImpl ret;
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000407 if (sec->NumberOfRelocations == 0)
408 ret.p = 0;
409 else
410 ret.p = reinterpret_cast<uintptr_t>(base() + sec->PointerToRelocations);
411
412 return relocation_iterator(RelocationRef(ret, this));
413}
414
Rui Ueyamabc654b12013-09-27 21:47:05 +0000415relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Sec) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000416 const coff_section *sec = toSec(Sec);
417 DataRefImpl ret;
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000418 if (sec->NumberOfRelocations == 0)
419 ret.p = 0;
420 else
421 ret.p = reinterpret_cast<uintptr_t>(
422 reinterpret_cast<const coff_relocation*>(
423 base() + sec->PointerToRelocations)
424 + sec->NumberOfRelocations);
425
426 return relocation_iterator(RelocationRef(ret, this));
427}
428
Rui Ueyamac2bed422013-09-27 21:04:00 +0000429// Initialize the pointer to the symbol table.
430error_code COFFObjectFile::initSymbolTablePtr() {
431 if (error_code ec = getObject(
432 SymbolTable, Data, base() + COFFHeader->PointerToSymbolTable,
433 COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
434 return ec;
435
436 // Find string table. The first four byte of the string table contains the
437 // total size of the string table, including the size field itself. If the
438 // string table is empty, the value of the first four byte would be 4.
439 const uint8_t *StringTableAddr =
440 base() + COFFHeader->PointerToSymbolTable +
441 COFFHeader->NumberOfSymbols * sizeof(coff_symbol);
442 const ulittle32_t *StringTableSizePtr;
443 if (error_code ec = getObject(StringTableSizePtr, Data, StringTableAddr))
444 return ec;
445 StringTableSize = *StringTableSizePtr;
446 if (error_code ec =
447 getObject(StringTable, Data, StringTableAddr, StringTableSize))
448 return ec;
449
450 // Check that the string table is null terminated if has any in it.
451 if (StringTableSize < 4 ||
452 (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0))
453 return object_error::parse_failed;
454 return object_error::success;
455}
456
457// Returns the file offset for the given RVA.
458error_code COFFObjectFile::getRvaPtr(uint32_t Rva, uintptr_t &Res) const {
459 error_code ec;
460 for (section_iterator i = begin_sections(), e = end_sections(); i != e;
461 i.increment(ec)) {
462 if (ec)
463 return ec;
464 const coff_section *Section = getCOFFSection(i);
465 uint32_t SectionStart = Section->VirtualAddress;
466 uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize;
467 if (SectionStart <= Rva && Rva < SectionEnd) {
468 uint32_t Offset = Rva - SectionStart;
469 Res = uintptr_t(base()) + Section->PointerToRawData + Offset;
470 return object_error::success;
471 }
472 }
473 return object_error::parse_failed;
474}
475
476// Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
477// table entry.
478error_code COFFObjectFile::
479getHintName(uint32_t Rva, uint16_t &Hint, StringRef &Name) const {
480 uintptr_t IntPtr = 0;
481 if (error_code ec = getRvaPtr(Rva, IntPtr))
482 return ec;
483 const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr);
484 Hint = *reinterpret_cast<const ulittle16_t *>(Ptr);
485 Name = StringRef(reinterpret_cast<const char *>(Ptr + 2));
486 return object_error::success;
487}
488
489// Find the import table.
490error_code COFFObjectFile::initImportTablePtr() {
491 // First, we get the RVA of the import table. If the file lacks a pointer to
492 // the import table, do nothing.
493 const data_directory *DataEntry;
494 if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
495 return object_error::success;
496
497 // Do nothing if the pointer to import table is NULL.
498 if (DataEntry->RelativeVirtualAddress == 0)
499 return object_error::success;
500
501 uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
502 NumberOfImportDirectory = DataEntry->Size /
503 sizeof(import_directory_table_entry);
504
505 // Find the section that contains the RVA. This is needed because the RVA is
506 // the import table's memory address which is different from its file offset.
507 uintptr_t IntPtr = 0;
508 if (error_code ec = getRvaPtr(ImportTableRva, IntPtr))
509 return ec;
510 ImportDirectory = reinterpret_cast<
511 const import_directory_table_entry *>(IntPtr);
512
513 // It's an error if there's no section containing the Import Table RVA.
514 return object_error::parse_failed;
515}
516
Michael J. Spencerec29b122011-06-25 17:54:50 +0000517COFFObjectFile::COFFObjectFile(MemoryBuffer *Object, error_code &ec)
Rafael Espindola717c4d42013-04-07 16:40:00 +0000518 : ObjectFile(Binary::ID_COFF, Object)
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000519 , COFFHeader(0)
520 , PE32Header(0)
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000521 , DataDirectory(0)
Michael J. Spencerd5930ca2011-11-08 23:34:07 +0000522 , SectionTable(0)
523 , SymbolTable(0)
524 , StringTable(0)
Rui Ueyamac2bed422013-09-27 21:04:00 +0000525 , StringTableSize(0)
526 , ImportDirectory(0)
527 , NumberOfImportDirectory(0) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000528 // Check that we at least have enough room for a header.
529 if (!checkSize(Data, ec, sizeof(coff_file_header))) return;
Eric Christopheree066fc2011-04-03 22:53:19 +0000530
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000531 // The current location in the file where we are looking at.
532 uint64_t CurPtr = 0;
533
534 // PE header is optional and is present only in executables. If it exists,
535 // it is placed right after COFF header.
536 bool hasPEHeader = false;
Eric Christopheree066fc2011-04-03 22:53:19 +0000537
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000538 // Check if this is a PE/COFF file.
Michael J. Spencerec29b122011-06-25 17:54:50 +0000539 if (base()[0] == 0x4d && base()[1] == 0x5a) {
Eric Christopheree066fc2011-04-03 22:53:19 +0000540 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
541 // PE signature to find 'normal' COFF header.
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000542 if (!checkSize(Data, ec, 0x3c + 8)) return;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000543 CurPtr = *reinterpret_cast<const ulittle16_t *>(base() + 0x3c);
544 // Check the PE magic bytes. ("PE\0\0")
545 if (std::memcmp(base() + CurPtr, "PE\0\0", 4) != 0) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000546 ec = object_error::parse_failed;
547 return;
548 }
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000549 CurPtr += 4; // Skip the PE magic bytes.
550 hasPEHeader = true;
Eric Christopheree066fc2011-04-03 22:53:19 +0000551 }
552
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000553 if ((ec = getObject(COFFHeader, Data, base() + CurPtr)))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000554 return;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000555 CurPtr += sizeof(coff_file_header);
556
557 if (hasPEHeader) {
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000558 if ((ec = getObject(PE32Header, Data, base() + CurPtr)))
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000559 return;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000560 if (PE32Header->Magic != 0x10b) {
561 // We only support PE32. If this is PE32 (not PE32+), the magic byte
562 // should be 0x10b. If this is not PE32, continue as if there's no PE
563 // header in this file.
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000564 PE32Header = 0;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000565 } else if (PE32Header->NumberOfRvaAndSize > 0) {
566 const uint8_t *addr = base() + CurPtr + sizeof(pe32_header);
567 uint64_t size = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
568 if ((ec = getObject(DataDirectory, Data, addr, size)))
569 return;
570 }
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000571 CurPtr += COFFHeader->SizeOfOptionalHeader;
572 }
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000573
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000574 if ((ec = getObject(SectionTable, Data, base() + CurPtr,
575 COFFHeader->NumberOfSections * sizeof(coff_section))))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000576 return;
577
Rui Ueyamac2bed422013-09-27 21:04:00 +0000578 // Initialize the pointer to the symbol table.
579 if (COFFHeader->PointerToSymbolTable != 0)
580 if ((ec = initSymbolTablePtr()))
Michael J. Spencerd5930ca2011-11-08 23:34:07 +0000581 return;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000582
Rui Ueyamac2bed422013-09-27 21:04:00 +0000583 // Initialize the pointer to the beginning of the import table.
584 if ((ec = initImportTablePtr()))
585 return;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000586
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000587 ec = object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000588}
589
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000590symbol_iterator COFFObjectFile::begin_symbols() const {
Michael J. Spencer0324b672011-01-21 02:27:02 +0000591 DataRefImpl ret;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000592 ret.p = reinterpret_cast<uintptr_t>(SymbolTable);
Michael J. Spencer0324b672011-01-21 02:27:02 +0000593 return symbol_iterator(SymbolRef(ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000594}
595
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000596symbol_iterator COFFObjectFile::end_symbols() const {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000597 // The symbol table ends where the string table begins.
Michael J. Spencer0324b672011-01-21 02:27:02 +0000598 DataRefImpl ret;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000599 ret.p = reinterpret_cast<uintptr_t>(StringTable);
Michael J. Spencer0324b672011-01-21 02:27:02 +0000600 return symbol_iterator(SymbolRef(ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000601}
602
Michael J. Spencer8c4729f2012-02-28 00:40:37 +0000603symbol_iterator COFFObjectFile::begin_dynamic_symbols() const {
604 // TODO: implement
605 report_fatal_error("Dynamic symbols unimplemented in COFFObjectFile");
606}
607
608symbol_iterator COFFObjectFile::end_dynamic_symbols() const {
609 // TODO: implement
610 report_fatal_error("Dynamic symbols unimplemented in COFFObjectFile");
611}
612
David Meyer2fc34c52012-03-01 01:36:50 +0000613library_iterator COFFObjectFile::begin_libraries_needed() const {
614 // TODO: implement
615 report_fatal_error("Libraries needed unimplemented in COFFObjectFile");
616}
617
618library_iterator COFFObjectFile::end_libraries_needed() const {
619 // TODO: implement
620 report_fatal_error("Libraries needed unimplemented in COFFObjectFile");
621}
622
David Meyerc429b802012-03-01 22:19:54 +0000623StringRef COFFObjectFile::getLoadName() const {
624 // COFF does not have this field.
625 return "";
626}
627
Rui Ueyamabc654b12013-09-27 21:47:05 +0000628import_directory_iterator COFFObjectFile::import_directory_begin() const {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000629 DataRefImpl Imp;
630 Imp.p = reinterpret_cast<uintptr_t>(ImportDirectory);
631 return import_directory_iterator(ImportDirectoryEntryRef(Imp, this));
632}
633
Rui Ueyamabc654b12013-09-27 21:47:05 +0000634import_directory_iterator COFFObjectFile::import_directory_end() const {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000635 DataRefImpl Imp;
636 if (ImportDirectory) {
637 Imp.p = reinterpret_cast<uintptr_t>(
638 ImportDirectory + (NumberOfImportDirectory - 1));
639 } else {
640 Imp.p = 0;
641 }
642 return import_directory_iterator(ImportDirectoryEntryRef(Imp, this));
643}
David Meyerc429b802012-03-01 22:19:54 +0000644
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000645section_iterator COFFObjectFile::begin_sections() const {
Michael J. Spencer0324b672011-01-21 02:27:02 +0000646 DataRefImpl ret;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000647 ret.p = reinterpret_cast<uintptr_t>(SectionTable);
Michael J. Spencer0324b672011-01-21 02:27:02 +0000648 return section_iterator(SectionRef(ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000649}
650
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000651section_iterator COFFObjectFile::end_sections() const {
Michael J. Spencer0324b672011-01-21 02:27:02 +0000652 DataRefImpl ret;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000653 ret.p = reinterpret_cast<uintptr_t>(SectionTable + COFFHeader->NumberOfSections);
Michael J. Spencer0324b672011-01-21 02:27:02 +0000654 return section_iterator(SectionRef(ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000655}
656
657uint8_t COFFObjectFile::getBytesInAddress() const {
Michael J. Spencer0324b672011-01-21 02:27:02 +0000658 return getArch() == Triple::x86_64 ? 8 : 4;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000659}
660
661StringRef COFFObjectFile::getFileFormatName() const {
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000662 switch(COFFHeader->Machine) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000663 case COFF::IMAGE_FILE_MACHINE_I386:
664 return "COFF-i386";
665 case COFF::IMAGE_FILE_MACHINE_AMD64:
666 return "COFF-x86-64";
667 default:
668 return "COFF-<unknown arch>";
669 }
670}
671
672unsigned COFFObjectFile::getArch() const {
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000673 switch(COFFHeader->Machine) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000674 case COFF::IMAGE_FILE_MACHINE_I386:
675 return Triple::x86;
676 case COFF::IMAGE_FILE_MACHINE_AMD64:
677 return Triple::x86_64;
678 default:
679 return Triple::UnknownArch;
680 }
681}
682
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000683// This method is kept here because lld uses this. As soon as we make
684// lld to use getCOFFHeader, this method will be removed.
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000685error_code COFFObjectFile::getHeader(const coff_file_header *&Res) const {
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000686 return getCOFFHeader(Res);
687}
688
689error_code COFFObjectFile::getCOFFHeader(const coff_file_header *&Res) const {
690 Res = COFFHeader;
691 return object_error::success;
692}
693
694error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
695 Res = PE32Header;
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000696 return object_error::success;
697}
698
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000699error_code COFFObjectFile::getDataDirectory(uint32_t index,
700 const data_directory *&Res) const {
701 // Error if if there's no data directory or the index is out of range.
702 if (!DataDirectory || index > PE32Header->NumberOfRvaAndSize)
703 return object_error::parse_failed;
704 Res = &DataDirectory[index];
705 return object_error::success;
706}
707
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000708error_code COFFObjectFile::getSection(int32_t index,
709 const coff_section *&Result) const {
710 // Check for special index values.
711 if (index == COFF::IMAGE_SYM_UNDEFINED ||
712 index == COFF::IMAGE_SYM_ABSOLUTE ||
713 index == COFF::IMAGE_SYM_DEBUG)
714 Result = NULL;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000715 else if (index > 0 && index <= COFFHeader->NumberOfSections)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000716 // We already verified the section table data, so no need to check again.
717 Result = SectionTable + (index - 1);
718 else
719 return object_error::parse_failed;
720 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000721}
722
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000723error_code COFFObjectFile::getString(uint32_t offset,
724 StringRef &Result) const {
725 if (StringTableSize <= 4)
726 // Tried to get a string from an empty string table.
727 return object_error::parse_failed;
728 if (offset >= StringTableSize)
729 return object_error::unexpected_eof;
730 Result = StringRef(StringTable + offset);
731 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000732}
733
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000734error_code COFFObjectFile::getSymbol(uint32_t index,
735 const coff_symbol *&Result) const {
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000736 if (index < COFFHeader->NumberOfSymbols)
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000737 Result = SymbolTable + index;
738 else
739 return object_error::parse_failed;
740 return object_error::success;
741}
742
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000743error_code COFFObjectFile::getSymbolName(const coff_symbol *symbol,
744 StringRef &Res) const {
745 // Check for string table entry. First 4 bytes are 0.
746 if (symbol->Name.Offset.Zeroes == 0) {
747 uint32_t Offset = symbol->Name.Offset.Offset;
748 if (error_code ec = getString(Offset, Res))
749 return ec;
750 return object_error::success;
751 }
752
753 if (symbol->Name.ShortName[7] == 0)
754 // Null terminated, let ::strlen figure out the length.
755 Res = StringRef(symbol->Name.ShortName);
756 else
757 // Not null terminated, use all 8 bytes.
758 Res = StringRef(symbol->Name.ShortName, 8);
759 return object_error::success;
760}
761
Marshall Clow71757ef2012-06-15 01:08:25 +0000762ArrayRef<uint8_t> COFFObjectFile::getSymbolAuxData(
763 const coff_symbol *symbol) const {
764 const uint8_t *aux = NULL;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000765
Marshall Clow71757ef2012-06-15 01:08:25 +0000766 if ( symbol->NumberOfAuxSymbols > 0 ) {
767 // AUX data comes immediately after the symbol in COFF
768 aux = reinterpret_cast<const uint8_t *>(symbol + 1);
769# ifndef NDEBUG
770 // Verify that the aux symbol points to a valid entry in the symbol table.
771 uintptr_t offset = uintptr_t(aux) - uintptr_t(base());
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000772 if (offset < COFFHeader->PointerToSymbolTable
773 || offset >= COFFHeader->PointerToSymbolTable
774 + (COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
Marshall Clow71757ef2012-06-15 01:08:25 +0000775 report_fatal_error("Aux Symbol data was outside of symbol table.");
776
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000777 assert((offset - COFFHeader->PointerToSymbolTable) % sizeof(coff_symbol)
Marshall Clow71757ef2012-06-15 01:08:25 +0000778 == 0 && "Aux Symbol data did not point to the beginning of a symbol");
Marshall Clow71757ef2012-06-15 01:08:25 +0000779# endif
Marshall Clowbfb85e62012-06-15 01:15:47 +0000780 }
Marshall Clow71757ef2012-06-15 01:08:25 +0000781 return ArrayRef<uint8_t>(aux, symbol->NumberOfAuxSymbols * sizeof(coff_symbol));
782}
783
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000784error_code COFFObjectFile::getSectionName(const coff_section *Sec,
785 StringRef &Res) const {
786 StringRef Name;
787 if (Sec->Name[7] == 0)
788 // Null terminated, let ::strlen figure out the length.
789 Name = Sec->Name;
790 else
791 // Not null terminated, use all 8 bytes.
792 Name = StringRef(Sec->Name, 8);
793
794 // Check for string table entry. First byte is '/'.
795 if (Name[0] == '/') {
796 uint32_t Offset;
797 if (Name.substr(1).getAsInteger(10, Offset))
798 return object_error::parse_failed;
799 if (error_code ec = getString(Offset, Name))
800 return ec;
801 }
802
803 Res = Name;
804 return object_error::success;
805}
806
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000807error_code COFFObjectFile::getSectionContents(const coff_section *Sec,
808 ArrayRef<uint8_t> &Res) const {
809 // The only thing that we need to verify is that the contents is contained
810 // within the file bounds. We don't need to make sure it doesn't cover other
811 // data, as there's nothing that says that is not allowed.
812 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
813 uintptr_t ConEnd = ConStart + Sec->SizeOfRawData;
814 if (ConEnd > uintptr_t(Data->getBufferEnd()))
815 return object_error::parse_failed;
816 Res = ArrayRef<uint8_t>(reinterpret_cast<const unsigned char*>(ConStart),
817 Sec->SizeOfRawData);
818 return object_error::success;
819}
820
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000821const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000822 return reinterpret_cast<const coff_relocation*>(Rel.p);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000823}
824error_code COFFObjectFile::getRelocationNext(DataRefImpl Rel,
825 RelocationRef &Res) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000826 Rel.p = reinterpret_cast<uintptr_t>(
827 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000828 Res = RelocationRef(Rel, this);
829 return object_error::success;
830}
831error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel,
832 uint64_t &Res) const {
Rafael Espindola1e483872013-04-25 12:28:45 +0000833 report_fatal_error("getRelocationAddress not implemented in COFFObjectFile");
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000834}
Danil Malyshevcbe72fc2011-11-29 17:40:10 +0000835error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel,
836 uint64_t &Res) const {
837 Res = toRel(Rel)->VirtualAddress;
838 return object_error::success;
839}
Rafael Espindola806f0062013-06-05 01:33:53 +0000840symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000841 const coff_relocation* R = toRel(Rel);
842 DataRefImpl Symb;
843 Symb.p = reinterpret_cast<uintptr_t>(SymbolTable + R->SymbolTableIndex);
Rafael Espindola806f0062013-06-05 01:33:53 +0000844 return symbol_iterator(SymbolRef(Symb, this));
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000845}
846error_code COFFObjectFile::getRelocationType(DataRefImpl Rel,
Owen Anderson7be76592011-10-26 17:08:49 +0000847 uint64_t &Res) const {
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000848 const coff_relocation* R = toRel(Rel);
849 Res = R->Type;
850 return object_error::success;
851}
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000852
Marshall Clow71757ef2012-06-15 01:08:25 +0000853const coff_section *COFFObjectFile::getCOFFSection(section_iterator &It) const {
854 return toSec(It->getRawDataRefImpl());
855}
856
857const coff_symbol *COFFObjectFile::getCOFFSymbol(symbol_iterator &It) const {
858 return toSymb(It->getRawDataRefImpl());
859}
860
Marshall Clowd3e2a762012-06-18 19:47:16 +0000861const coff_relocation *COFFObjectFile::getCOFFRelocation(
862 relocation_iterator &It) const {
863 return toRel(It->getRawDataRefImpl());
864}
865
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000866#define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(enum) \
867 case COFF::enum: res = #enum; break;
868
869error_code COFFObjectFile::getRelocationTypeName(DataRefImpl Rel,
870 SmallVectorImpl<char> &Result) const {
871 const coff_relocation *reloc = toRel(Rel);
872 StringRef res;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000873 switch (COFFHeader->Machine) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000874 case COFF::IMAGE_FILE_MACHINE_AMD64:
875 switch (reloc->Type) {
876 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
877 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
878 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
879 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
880 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
881 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
882 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
883 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
884 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
885 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
886 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
887 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
888 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
889 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
890 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
891 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
892 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
893 default:
894 res = "Unknown";
895 }
896 break;
897 case COFF::IMAGE_FILE_MACHINE_I386:
898 switch (reloc->Type) {
899 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
900 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
901 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
902 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
903 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
904 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
905 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
906 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
907 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
908 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
909 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
910 default:
911 res = "Unknown";
912 }
913 break;
914 default:
915 res = "Unknown";
916 }
917 Result.append(res.begin(), res.end());
918 return object_error::success;
919}
920
921#undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
922
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000923error_code COFFObjectFile::getRelocationValueString(DataRefImpl Rel,
924 SmallVectorImpl<char> &Result) const {
925 const coff_relocation *reloc = toRel(Rel);
NAKAMURA Takumi648b2fa2011-10-08 11:22:53 +0000926 const coff_symbol *symb = 0;
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000927 if (error_code ec = getSymbol(reloc->SymbolTableIndex, symb)) return ec;
928 DataRefImpl sym;
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000929 sym.p = reinterpret_cast<uintptr_t>(symb);
930 StringRef symname;
931 if (error_code ec = getSymbolName(sym, symname)) return ec;
932 Result.append(symname.begin(), symname.end());
933 return object_error::success;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000934}
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000935
David Meyer2fc34c52012-03-01 01:36:50 +0000936error_code COFFObjectFile::getLibraryNext(DataRefImpl LibData,
937 LibraryRef &Result) const {
938 report_fatal_error("getLibraryNext not implemented in COFFObjectFile");
939}
940
941error_code COFFObjectFile::getLibraryPath(DataRefImpl LibData,
942 StringRef &Result) const {
943 report_fatal_error("getLibraryPath not implemented in COFFObjectFile");
944}
945
Rui Ueyamac2bed422013-09-27 21:04:00 +0000946bool ImportDirectoryEntryRef::
947operator==(const ImportDirectoryEntryRef &Other) const {
948 return ImportDirectoryPimpl == Other.ImportDirectoryPimpl;
949}
950
951static const import_directory_table_entry *toImportEntry(DataRefImpl Imp) {
952 return reinterpret_cast<const import_directory_table_entry *>(Imp.p);
953}
954
955error_code
956ImportDirectoryEntryRef::getNext(ImportDirectoryEntryRef &Result) const {
957 const import_directory_table_entry *Dir = toImportEntry(ImportDirectoryPimpl);
958 Dir += 1;
959 DataRefImpl Next;
960 Next.p = reinterpret_cast<uintptr_t>(Dir);
961 Result = ImportDirectoryEntryRef(Next, OwningObject);
962 return object_error::success;
963}
964
965error_code ImportDirectoryEntryRef::
966getImportTableEntry(const import_directory_table_entry *&Result) const {
967 Result = toImportEntry(ImportDirectoryPimpl);
968 return object_error::success;
969}
970
971error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
972 const import_directory_table_entry *Dir = toImportEntry(ImportDirectoryPimpl);
973 uintptr_t IntPtr = 0;
974 if (error_code ec = OwningObject->getRvaPtr(Dir->NameRVA, IntPtr))
975 return ec;
976 const char *Ptr = reinterpret_cast<const char *>(IntPtr);
977 Result = StringRef(Ptr);
978 return object_error::success;
979}
980
981error_code ImportDirectoryEntryRef::getImportLookupEntry(
982 const import_lookup_table_entry32 *&Result) const {
983 const import_directory_table_entry *Dir = toImportEntry(ImportDirectoryPimpl);
984 uintptr_t IntPtr = 0;
985 if (error_code ec = OwningObject->getRvaPtr(
986 Dir->ImportLookupTableRVA, IntPtr))
987 return ec;
988 Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr);
989 return object_error::success;
990}
991
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000992namespace llvm {
993
994 ObjectFile *ObjectFile::createCOFFObjectFile(MemoryBuffer *Object) {
Michael J. Spencerec29b122011-06-25 17:54:50 +0000995 error_code ec;
996 return new COFFObjectFile(Object, ec);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000997 }
998
999} // end namespace llvm