]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/llvm/lib/Object/COFFObjectFile.cpp
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / contrib / llvm / lib / Object / COFFObjectFile.cpp
1 //===- 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
14 #include "llvm/Object/COFF.h"
15 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/ADT/StringSwitch.h"
18 #include "llvm/ADT/Triple.h"
19
20 using namespace llvm;
21 using namespace object;
22
23 namespace {
24 using support::ulittle8_t;
25 using support::ulittle16_t;
26 using support::ulittle32_t;
27 using support::little16_t;
28 }
29
30 namespace {
31 // Returns false if size is greater than the buffer size. And sets ec.
32 bool checkSize(const MemoryBuffer *m, error_code &ec, uint64_t size) {
33   if (m->getBufferSize() < size) {
34     ec = object_error::unexpected_eof;
35     return false;
36   }
37   return true;
38 }
39
40 // Returns false if any bytes in [addr, addr + size) fall outsize of m.
41 bool checkAddr(const MemoryBuffer *m,
42                error_code &ec,
43                uintptr_t addr,
44                uint64_t size) {
45   if (addr + size < addr ||
46       addr + size < size ||
47       addr + size > uintptr_t(m->getBufferEnd())) {
48     ec = object_error::unexpected_eof;
49     return false;
50   }
51   return true;
52 }
53 }
54
55 const coff_symbol *COFFObjectFile::toSymb(DataRefImpl Symb) const {
56   const coff_symbol *addr = reinterpret_cast<const coff_symbol*>(Symb.p);
57
58 # ifndef NDEBUG
59   // Verify that the symbol points to a valid entry in the symbol table.
60   uintptr_t offset = uintptr_t(addr) - uintptr_t(base());
61   if (offset < Header->PointerToSymbolTable
62       || offset >= Header->PointerToSymbolTable
63          + (Header->NumberOfSymbols * sizeof(coff_symbol)))
64     report_fatal_error("Symbol was outside of symbol table.");
65
66   assert((offset - Header->PointerToSymbolTable) % sizeof(coff_symbol)
67          == 0 && "Symbol did not point to the beginning of a symbol");
68 # endif
69
70   return addr;
71 }
72
73 const coff_section *COFFObjectFile::toSec(DataRefImpl Sec) const {
74   const coff_section *addr = reinterpret_cast<const coff_section*>(Sec.p);
75
76 # ifndef NDEBUG
77   // Verify that the section points to a valid entry in the section table.
78   if (addr < SectionTable
79       || addr >= (SectionTable + Header->NumberOfSections))
80     report_fatal_error("Section was outside of section table.");
81
82   uintptr_t offset = uintptr_t(addr) - uintptr_t(SectionTable);
83   assert(offset % sizeof(coff_section) == 0 &&
84          "Section did not point to the beginning of a section");
85 # endif
86
87   return addr;
88 }
89
90 error_code COFFObjectFile::getSymbolNext(DataRefImpl Symb,
91                                          SymbolRef &Result) const {
92   const coff_symbol *symb = toSymb(Symb);
93   symb += 1 + symb->NumberOfAuxSymbols;
94   Symb.p = reinterpret_cast<uintptr_t>(symb);
95   Result = SymbolRef(Symb, this);
96   return object_error::success;
97 }
98
99  error_code COFFObjectFile::getSymbolName(DataRefImpl Symb,
100                                           StringRef &Result) const {
101   const coff_symbol *symb = toSymb(Symb);
102   return getSymbolName(symb, Result);
103 }
104
105 error_code COFFObjectFile::getSymbolFileOffset(DataRefImpl Symb,
106                                             uint64_t &Result) const {
107   const coff_symbol *symb = toSymb(Symb);
108   const coff_section *Section = NULL;
109   if (error_code ec = getSection(symb->SectionNumber, Section))
110     return ec;
111   char Type;
112   if (error_code ec = getSymbolNMTypeChar(Symb, Type))
113     return ec;
114   if (Type == 'U' || Type == 'w')
115     Result = UnknownAddressOrSize;
116   else if (Section)
117     Result = Section->PointerToRawData + symb->Value;
118   else
119     Result = symb->Value;
120   return object_error::success;
121 }
122
123 error_code COFFObjectFile::getSymbolAddress(DataRefImpl Symb,
124                                             uint64_t &Result) const {
125   const coff_symbol *symb = toSymb(Symb);
126   const coff_section *Section = NULL;
127   if (error_code ec = getSection(symb->SectionNumber, Section))
128     return ec;
129   char Type;
130   if (error_code ec = getSymbolNMTypeChar(Symb, Type))
131     return ec;
132   if (Type == 'U' || Type == 'w')
133     Result = UnknownAddressOrSize;
134   else if (Section)
135     Result = Section->VirtualAddress + symb->Value;
136   else
137     Result = symb->Value;
138   return object_error::success;
139 }
140
141 error_code COFFObjectFile::getSymbolType(DataRefImpl Symb,
142                                          SymbolRef::Type &Result) const {
143   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) {
147     Result = SymbolRef::ST_Unknown;
148   } else {
149     if (symb->getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION) {
150       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
163 error_code COFFObjectFile::getSymbolFlags(DataRefImpl Symb,
164                                           uint32_t &Result) const {
165   const coff_symbol *symb = toSymb(Symb);
166   Result = SymbolRef::SF_None;
167
168   // 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;
173
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
184   return object_error::success;
185 }
186
187 error_code COFFObjectFile::getSymbolSize(DataRefImpl Symb,
188                                          uint64_t &Result) const {
189   // 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.
192   const coff_symbol *symb = toSymb(Symb);
193   const coff_section *Section = NULL;
194   if (error_code ec = getSection(symb->SectionNumber, Section))
195     return ec;
196   char Type;
197   if (error_code ec = getSymbolNMTypeChar(Symb, Type))
198     return ec;
199   if (Type == 'U' || Type == 'w')
200     Result = UnknownAddressOrSize;
201   else if (Section)
202     Result = Section->SizeOfRawData - symb->Value;
203   else
204     Result = 0;
205   return object_error::success;
206 }
207
208 error_code COFFObjectFile::getSymbolNMTypeChar(DataRefImpl Symb,
209                                                char &Result) const {
210   const coff_symbol *symb = toSymb(Symb);
211   StringRef name;
212   if (error_code ec = getSymbolName(Symb, name))
213     return ec;
214   char ret = StringSwitch<char>(name)
215     .StartsWith(".debug", 'N')
216     .StartsWith(".sxdata", 'N')
217     .Default('?');
218
219   if (ret != '?') {
220     Result = ret;
221     return object_error::success;
222   }
223
224   uint32_t Characteristics = 0;
225   if (symb->SectionNumber > 0) {
226     const coff_section *Section = NULL;
227     if (error_code ec = getSection(symb->SectionNumber, Section))
228       return ec;
229     Characteristics = Section->Characteristics;
230   }
231
232   switch (symb->SectionNumber) {
233   case COFF::IMAGE_SYM_UNDEFINED:
234     // Check storage classes.
235     if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL) {
236       Result = 'w';
237       return object_error::success; // Don't do ::toupper.
238     } else if (symb->Value != 0) // Check for common symbols.
239       ret = 'c';
240     else
241       ret = 'u';
242     break;
243   case COFF::IMAGE_SYM_ABSOLUTE:
244     ret = 'a';
245     break;
246   case COFF::IMAGE_SYM_DEBUG:
247     ret = 'n';
248     break;
249   default:
250     // Check section type.
251     if (Characteristics & COFF::IMAGE_SCN_CNT_CODE)
252       ret = 't';
253     else if (  Characteristics & COFF::IMAGE_SCN_MEM_READ
254             && ~Characteristics & COFF::IMAGE_SCN_MEM_WRITE) // Read only.
255       ret = 'r';
256     else if (Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
257       ret = 'd';
258     else if (Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
259       ret = 'b';
260     else if (Characteristics & COFF::IMAGE_SCN_LNK_INFO)
261       ret = 'i';
262
263     // Check for section symbol.
264     else if (  symb->StorageClass == COFF::IMAGE_SYM_CLASS_STATIC
265             && symb->Value == 0)
266        ret = 's';
267   }
268
269   if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL)
270     ret = ::toupper(static_cast<unsigned char>(ret));
271
272   Result = ret;
273   return object_error::success;
274 }
275
276 error_code COFFObjectFile::getSymbolSection(DataRefImpl Symb,
277                                             section_iterator &Result) const {
278   const coff_symbol *symb = toSymb(Symb);
279   if (symb->SectionNumber <= COFF::IMAGE_SYM_UNDEFINED)
280     Result = end_sections();
281   else {
282     const coff_section *sec = 0;
283     if (error_code ec = getSection(symb->SectionNumber, sec)) return ec;
284     DataRefImpl Sec;
285     Sec.p = reinterpret_cast<uintptr_t>(sec);
286     Result = section_iterator(SectionRef(Sec, this));
287   }
288   return object_error::success;
289 }
290
291 error_code COFFObjectFile::getSymbolValue(DataRefImpl Symb,
292                                           uint64_t &Val) const {
293   report_fatal_error("getSymbolValue unimplemented in COFFObjectFile");
294 }
295
296 error_code COFFObjectFile::getSectionNext(DataRefImpl Sec,
297                                           SectionRef &Result) const {
298   const coff_section *sec = toSec(Sec);
299   sec += 1;
300   Sec.p = reinterpret_cast<uintptr_t>(sec);
301   Result = SectionRef(Sec, this);
302   return object_error::success;
303 }
304
305 error_code COFFObjectFile::getSectionName(DataRefImpl Sec,
306                                           StringRef &Result) const {
307   const coff_section *sec = toSec(Sec);
308   return getSectionName(sec, Result);
309 }
310
311 error_code COFFObjectFile::getSectionAddress(DataRefImpl Sec,
312                                              uint64_t &Result) const {
313   const coff_section *sec = toSec(Sec);
314   Result = sec->VirtualAddress;
315   return object_error::success;
316 }
317
318 error_code COFFObjectFile::getSectionSize(DataRefImpl Sec,
319                                           uint64_t &Result) const {
320   const coff_section *sec = toSec(Sec);
321   Result = sec->SizeOfRawData;
322   return object_error::success;
323 }
324
325 error_code COFFObjectFile::getSectionContents(DataRefImpl Sec,
326                                               StringRef &Result) const {
327   const coff_section *sec = toSec(Sec);
328   ArrayRef<uint8_t> Res;
329   error_code EC = getSectionContents(sec, Res);
330   Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size());
331   return EC;
332 }
333
334 error_code COFFObjectFile::getSectionAlignment(DataRefImpl Sec,
335                                                uint64_t &Res) const {
336   const coff_section *sec = toSec(Sec);
337   if (!sec)
338     return object_error::parse_failed;
339   Res = uint64_t(1) << (((sec->Characteristics & 0x00F00000) >> 20) - 1);
340   return object_error::success;
341 }
342
343 error_code COFFObjectFile::isSectionText(DataRefImpl Sec,
344                                          bool &Result) const {
345   const coff_section *sec = toSec(Sec);
346   Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
347   return object_error::success;
348 }
349
350 error_code COFFObjectFile::isSectionData(DataRefImpl Sec,
351                                          bool &Result) const {
352   const coff_section *sec = toSec(Sec);
353   Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
354   return object_error::success;
355 }
356
357 error_code COFFObjectFile::isSectionBSS(DataRefImpl Sec,
358                                         bool &Result) const {
359   const coff_section *sec = toSec(Sec);
360   Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
361   return object_error::success;
362 }
363
364 error_code COFFObjectFile::isSectionRequiredForExecution(DataRefImpl Sec,
365                                                          bool &Result) const {
366   // FIXME: Unimplemented
367   Result = true;
368   return object_error::success;
369 }
370
371 error_code COFFObjectFile::isSectionVirtual(DataRefImpl Sec,
372                                            bool &Result) const {
373   const coff_section *sec = toSec(Sec);
374   Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
375   return object_error::success;
376 }
377
378 error_code COFFObjectFile::isSectionZeroInit(DataRefImpl Sec,
379                                              bool &Result) const {
380   // FIXME: Unimplemented.
381   Result = false;
382   return object_error::success;
383 }
384
385 error_code COFFObjectFile::isSectionReadOnlyData(DataRefImpl Sec,
386                                                 bool &Result) const {
387   // FIXME: Unimplemented.
388   Result = false;
389   return object_error::success;
390 }
391
392 error_code COFFObjectFile::sectionContainsSymbol(DataRefImpl Sec,
393                                                  DataRefImpl Symb,
394                                                  bool &Result) const {
395   const coff_section *sec = toSec(Sec);
396   const coff_symbol *symb = toSymb(Symb);
397   const coff_section *symb_sec = 0;
398   if (error_code ec = getSection(symb->SectionNumber, symb_sec)) return ec;
399   if (symb_sec == sec)
400     Result = true;
401   else
402     Result = false;
403   return object_error::success;
404 }
405
406 relocation_iterator COFFObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
407   const coff_section *sec = toSec(Sec);
408   DataRefImpl ret;
409   if (sec->NumberOfRelocations == 0)
410     ret.p = 0;
411   else
412     ret.p = reinterpret_cast<uintptr_t>(base() + sec->PointerToRelocations);
413
414   return relocation_iterator(RelocationRef(ret, this));
415 }
416
417 relocation_iterator COFFObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
418   const coff_section *sec = toSec(Sec);
419   DataRefImpl ret;
420   if (sec->NumberOfRelocations == 0)
421     ret.p = 0;
422   else
423     ret.p = reinterpret_cast<uintptr_t>(
424               reinterpret_cast<const coff_relocation*>(
425                 base() + sec->PointerToRelocations)
426               + sec->NumberOfRelocations);
427
428   return relocation_iterator(RelocationRef(ret, this));
429 }
430
431 COFFObjectFile::COFFObjectFile(MemoryBuffer *Object, error_code &ec)
432   : ObjectFile(Binary::ID_COFF, Object)
433   , Header(0)
434   , SectionTable(0)
435   , SymbolTable(0)
436   , StringTable(0)
437   , StringTableSize(0) {
438   // Check that we at least have enough room for a header.
439   if (!checkSize(Data, ec, sizeof(coff_file_header))) return;
440
441   // The actual starting location of the COFF header in the file. This can be
442   // non-zero in PE/COFF files.
443   uint64_t HeaderStart = 0;
444
445   // Check if this is a PE/COFF file.
446   if (base()[0] == 0x4d && base()[1] == 0x5a) {
447     // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
448     // PE signature to find 'normal' COFF header.
449     if (!checkSize(Data, ec, 0x3c + 8)) return;
450     HeaderStart = *reinterpret_cast<const ulittle16_t *>(base() + 0x3c);
451     // Check the PE header. ("PE\0\0")
452     if (std::memcmp(base() + HeaderStart, "PE\0\0", 4) != 0) {
453       ec = object_error::parse_failed;
454       return;
455     }
456     HeaderStart += 4; // Skip the PE Header.
457   }
458
459   Header = reinterpret_cast<const coff_file_header *>(base() + HeaderStart);
460   if (!checkAddr(Data, ec, uintptr_t(Header), sizeof(coff_file_header)))
461     return;
462
463   SectionTable =
464     reinterpret_cast<const coff_section *>( base()
465                                           + HeaderStart
466                                           + sizeof(coff_file_header)
467                                           + Header->SizeOfOptionalHeader);
468   if (!checkAddr(Data, ec, uintptr_t(SectionTable),
469                  Header->NumberOfSections * sizeof(coff_section)))
470     return;
471
472   if (Header->PointerToSymbolTable != 0) {
473     SymbolTable =
474       reinterpret_cast<const coff_symbol *>(base()
475                                             + Header->PointerToSymbolTable);
476     if (!checkAddr(Data, ec, uintptr_t(SymbolTable),
477                    Header->NumberOfSymbols * sizeof(coff_symbol)))
478       return;
479
480     // Find string table.
481     StringTable = reinterpret_cast<const char *>(base())
482                   + Header->PointerToSymbolTable
483                   + Header->NumberOfSymbols * sizeof(coff_symbol);
484     if (!checkAddr(Data, ec, uintptr_t(StringTable), sizeof(ulittle32_t)))
485       return;
486
487     StringTableSize = *reinterpret_cast<const ulittle32_t *>(StringTable);
488     if (!checkAddr(Data, ec, uintptr_t(StringTable), StringTableSize))
489       return;
490     // Check that the string table is null terminated if has any in it.
491     if (StringTableSize < 4
492         || (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)) {
493       ec = object_error::parse_failed;
494       return;
495     }
496   }
497
498   ec = object_error::success;
499 }
500
501 symbol_iterator COFFObjectFile::begin_symbols() const {
502   DataRefImpl ret;
503   ret.p = reinterpret_cast<intptr_t>(SymbolTable);
504   return symbol_iterator(SymbolRef(ret, this));
505 }
506
507 symbol_iterator COFFObjectFile::end_symbols() const {
508   // The symbol table ends where the string table begins.
509   DataRefImpl ret;
510   ret.p = reinterpret_cast<intptr_t>(StringTable);
511   return symbol_iterator(SymbolRef(ret, this));
512 }
513
514 symbol_iterator COFFObjectFile::begin_dynamic_symbols() const {
515   // TODO: implement
516   report_fatal_error("Dynamic symbols unimplemented in COFFObjectFile");
517 }
518
519 symbol_iterator COFFObjectFile::end_dynamic_symbols() const {
520   // TODO: implement
521   report_fatal_error("Dynamic symbols unimplemented in COFFObjectFile");
522 }
523
524 library_iterator COFFObjectFile::begin_libraries_needed() const {
525   // TODO: implement
526   report_fatal_error("Libraries needed unimplemented in COFFObjectFile");
527 }
528
529 library_iterator COFFObjectFile::end_libraries_needed() const {
530   // TODO: implement
531   report_fatal_error("Libraries needed unimplemented in COFFObjectFile");
532 }
533
534 StringRef COFFObjectFile::getLoadName() const {
535   // COFF does not have this field.
536   return "";
537 }
538
539
540 section_iterator COFFObjectFile::begin_sections() const {
541   DataRefImpl ret;
542   ret.p = reinterpret_cast<intptr_t>(SectionTable);
543   return section_iterator(SectionRef(ret, this));
544 }
545
546 section_iterator COFFObjectFile::end_sections() const {
547   DataRefImpl ret;
548   ret.p = reinterpret_cast<intptr_t>(SectionTable + Header->NumberOfSections);
549   return section_iterator(SectionRef(ret, this));
550 }
551
552 uint8_t COFFObjectFile::getBytesInAddress() const {
553   return getArch() == Triple::x86_64 ? 8 : 4;
554 }
555
556 StringRef COFFObjectFile::getFileFormatName() const {
557   switch(Header->Machine) {
558   case COFF::IMAGE_FILE_MACHINE_I386:
559     return "COFF-i386";
560   case COFF::IMAGE_FILE_MACHINE_AMD64:
561     return "COFF-x86-64";
562   default:
563     return "COFF-<unknown arch>";
564   }
565 }
566
567 unsigned COFFObjectFile::getArch() const {
568   switch(Header->Machine) {
569   case COFF::IMAGE_FILE_MACHINE_I386:
570     return Triple::x86;
571   case COFF::IMAGE_FILE_MACHINE_AMD64:
572     return Triple::x86_64;
573   default:
574     return Triple::UnknownArch;
575   }
576 }
577
578 error_code COFFObjectFile::getHeader(const coff_file_header *&Res) const {
579   Res = Header;
580   return object_error::success;
581 }
582
583 error_code COFFObjectFile::getSection(int32_t index,
584                                       const coff_section *&Result) const {
585   // Check for special index values.
586   if (index == COFF::IMAGE_SYM_UNDEFINED ||
587       index == COFF::IMAGE_SYM_ABSOLUTE ||
588       index == COFF::IMAGE_SYM_DEBUG)
589     Result = NULL;
590   else if (index > 0 && index <= Header->NumberOfSections)
591     // We already verified the section table data, so no need to check again.
592     Result = SectionTable + (index - 1);
593   else
594     return object_error::parse_failed;
595   return object_error::success;
596 }
597
598 error_code COFFObjectFile::getString(uint32_t offset,
599                                      StringRef &Result) const {
600   if (StringTableSize <= 4)
601     // Tried to get a string from an empty string table.
602     return object_error::parse_failed;
603   if (offset >= StringTableSize)
604     return object_error::unexpected_eof;
605   Result = StringRef(StringTable + offset);
606   return object_error::success;
607 }
608
609 error_code COFFObjectFile::getSymbol(uint32_t index,
610                                      const coff_symbol *&Result) const {
611   if (index < Header->NumberOfSymbols)
612     Result = SymbolTable + index;
613   else
614     return object_error::parse_failed;
615   return object_error::success;
616 }
617
618 error_code COFFObjectFile::getSymbolName(const coff_symbol *symbol,
619                                          StringRef &Res) const {
620   // Check for string table entry. First 4 bytes are 0.
621   if (symbol->Name.Offset.Zeroes == 0) {
622     uint32_t Offset = symbol->Name.Offset.Offset;
623     if (error_code ec = getString(Offset, Res))
624       return ec;
625     return object_error::success;
626   }
627
628   if (symbol->Name.ShortName[7] == 0)
629     // Null terminated, let ::strlen figure out the length.
630     Res = StringRef(symbol->Name.ShortName);
631   else
632     // Not null terminated, use all 8 bytes.
633     Res = StringRef(symbol->Name.ShortName, 8);
634   return object_error::success;
635 }
636
637 ArrayRef<uint8_t> COFFObjectFile::getSymbolAuxData(
638                                   const coff_symbol *symbol) const {
639   const uint8_t *aux = NULL;
640   
641   if ( symbol->NumberOfAuxSymbols > 0 ) {
642   // AUX data comes immediately after the symbol in COFF
643     aux = reinterpret_cast<const uint8_t *>(symbol + 1);
644 # ifndef NDEBUG
645     // Verify that the aux symbol points to a valid entry in the symbol table.
646     uintptr_t offset = uintptr_t(aux) - uintptr_t(base());
647     if (offset < Header->PointerToSymbolTable
648         || offset >= Header->PointerToSymbolTable
649            + (Header->NumberOfSymbols * sizeof(coff_symbol)))
650       report_fatal_error("Aux Symbol data was outside of symbol table.");
651
652     assert((offset - Header->PointerToSymbolTable) % sizeof(coff_symbol)
653          == 0 && "Aux Symbol data did not point to the beginning of a symbol");
654 # endif
655   }
656   return ArrayRef<uint8_t>(aux, symbol->NumberOfAuxSymbols * sizeof(coff_symbol));
657 }
658
659 error_code COFFObjectFile::getSectionName(const coff_section *Sec,
660                                           StringRef &Res) const {
661   StringRef Name;
662   if (Sec->Name[7] == 0)
663     // Null terminated, let ::strlen figure out the length.
664     Name = Sec->Name;
665   else
666     // Not null terminated, use all 8 bytes.
667     Name = StringRef(Sec->Name, 8);
668
669   // Check for string table entry. First byte is '/'.
670   if (Name[0] == '/') {
671     uint32_t Offset;
672     if (Name.substr(1).getAsInteger(10, Offset))
673       return object_error::parse_failed;
674     if (error_code ec = getString(Offset, Name))
675       return ec;
676   }
677
678   Res = Name;
679   return object_error::success;
680 }
681
682 error_code COFFObjectFile::getSectionContents(const coff_section *Sec,
683                                               ArrayRef<uint8_t> &Res) const {
684   // The only thing that we need to verify is that the contents is contained
685   // within the file bounds. We don't need to make sure it doesn't cover other
686   // data, as there's nothing that says that is not allowed.
687   uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
688   uintptr_t ConEnd = ConStart + Sec->SizeOfRawData;
689   if (ConEnd > uintptr_t(Data->getBufferEnd()))
690     return object_error::parse_failed;
691   Res = ArrayRef<uint8_t>(reinterpret_cast<const unsigned char*>(ConStart),
692                           Sec->SizeOfRawData);
693   return object_error::success;
694 }
695
696 const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
697   return reinterpret_cast<const coff_relocation*>(Rel.p);
698 }
699 error_code COFFObjectFile::getRelocationNext(DataRefImpl Rel,
700                                              RelocationRef &Res) const {
701   Rel.p = reinterpret_cast<uintptr_t>(
702             reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
703   Res = RelocationRef(Rel, this);
704   return object_error::success;
705 }
706 error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel,
707                                                 uint64_t &Res) const {
708   report_fatal_error("getRelocationAddress not implemented in COFFObjectFile");
709 }
710 error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel,
711                                                uint64_t &Res) const {
712   Res = toRel(Rel)->VirtualAddress;
713   return object_error::success;
714 }
715 error_code COFFObjectFile::getRelocationSymbol(DataRefImpl Rel,
716                                                SymbolRef &Res) const {
717   const coff_relocation* R = toRel(Rel);
718   DataRefImpl Symb;
719   Symb.p = reinterpret_cast<uintptr_t>(SymbolTable + R->SymbolTableIndex);
720   Res = SymbolRef(Symb, this);
721   return object_error::success;
722 }
723 error_code COFFObjectFile::getRelocationType(DataRefImpl Rel,
724                                              uint64_t &Res) const {
725   const coff_relocation* R = toRel(Rel);
726   Res = R->Type;
727   return object_error::success;
728 }
729
730 const coff_section *COFFObjectFile::getCOFFSection(section_iterator &It) const {
731   return toSec(It->getRawDataRefImpl());
732 }
733
734 const coff_symbol *COFFObjectFile::getCOFFSymbol(symbol_iterator &It) const {
735   return toSymb(It->getRawDataRefImpl());
736 }
737
738 const coff_relocation *COFFObjectFile::getCOFFRelocation(
739                                              relocation_iterator &It) const {
740   return toRel(It->getRawDataRefImpl());
741 }
742
743
744 #define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(enum) \
745   case COFF::enum: res = #enum; break;
746
747 error_code COFFObjectFile::getRelocationTypeName(DataRefImpl Rel,
748                                           SmallVectorImpl<char> &Result) const {
749   const coff_relocation *reloc = toRel(Rel);
750   StringRef res;
751   switch (Header->Machine) {
752   case COFF::IMAGE_FILE_MACHINE_AMD64:
753     switch (reloc->Type) {
754     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
755     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
756     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
757     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
758     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
759     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
760     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
761     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
762     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
763     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
764     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
765     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
766     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
767     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
768     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
769     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
770     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
771     default:
772       res = "Unknown";
773     }
774     break;
775   case COFF::IMAGE_FILE_MACHINE_I386:
776     switch (reloc->Type) {
777     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
778     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
779     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
780     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
781     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
782     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
783     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
784     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
785     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
786     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
787     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
788     default:
789       res = "Unknown";
790     }
791     break;
792   default:
793     res = "Unknown";
794   }
795   Result.append(res.begin(), res.end());
796   return object_error::success;
797 }
798
799 #undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
800
801 error_code COFFObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel,
802                                                        int64_t &Res) const {
803   Res = 0;
804   return object_error::success;
805 }
806 error_code COFFObjectFile::getRelocationValueString(DataRefImpl Rel,
807                                           SmallVectorImpl<char> &Result) const {
808   const coff_relocation *reloc = toRel(Rel);
809   const coff_symbol *symb = 0;
810   if (error_code ec = getSymbol(reloc->SymbolTableIndex, symb)) return ec;
811   DataRefImpl sym;
812   sym.p = reinterpret_cast<uintptr_t>(symb);
813   StringRef symname;
814   if (error_code ec = getSymbolName(sym, symname)) return ec;
815   Result.append(symname.begin(), symname.end());
816   return object_error::success;
817 }
818
819 error_code COFFObjectFile::getLibraryNext(DataRefImpl LibData,
820                                           LibraryRef &Result) const {
821   report_fatal_error("getLibraryNext not implemented in COFFObjectFile");
822 }
823
824 error_code COFFObjectFile::getLibraryPath(DataRefImpl LibData,
825                                           StringRef &Result) const {
826   report_fatal_error("getLibraryPath not implemented in COFFObjectFile");
827 }
828
829 namespace llvm {
830
831   ObjectFile *ObjectFile::createCOFFObjectFile(MemoryBuffer *Object) {
832     error_code ec;
833     return new COFFObjectFile(Object, ec);
834   }
835
836 } // end namespace llvm