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