Change relocation API to be per section.
[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   // Check for string table entry. First 4 bytes are 0.
102   if (symb->Name.Offset.Zeroes == 0) {
103     uint32_t Offset = symb->Name.Offset.Offset;
104     if (error_code ec = getString(Offset, Result))
105       return ec;
106     return object_error::success;
107   }
108
109   if (symb->Name.ShortName[7] == 0)
110     // Null terminated, let ::strlen figure out the length.
111     Result = StringRef(symb->Name.ShortName);
112   else
113     // Not null terminated, use all 8 bytes.
114     Result = StringRef(symb->Name.ShortName, 8);
115   return object_error::success;
116 }
117
118 error_code COFFObjectFile::getSymbolOffset(DataRefImpl Symb,
119                                             uint64_t &Result) const {
120   const coff_symbol *symb = toSymb(Symb);
121   const coff_section *Section = NULL;
122   if (error_code ec = getSection(symb->SectionNumber, Section))
123     return ec;
124   char Type;
125   if (error_code ec = getSymbolNMTypeChar(Symb, Type))
126     return ec;
127   if (Type == 'U' || Type == 'w')
128     Result = UnknownAddressOrSize;
129   else if (Section)
130     Result = Section->VirtualAddress + symb->Value;
131   else
132     Result = symb->Value;
133   return object_error::success;
134 }
135
136 error_code COFFObjectFile::getSymbolAddress(DataRefImpl Symb,
137                                             uint64_t &Result) const {
138   const coff_symbol *symb = toSymb(Symb);
139   const coff_section *Section = NULL;
140   if (error_code ec = getSection(symb->SectionNumber, Section))
141     return ec;
142   char Type;
143   if (error_code ec = getSymbolNMTypeChar(Symb, Type))
144     return ec;
145   if (Type == 'U' || Type == 'w')
146     Result = UnknownAddressOrSize;
147   else if (Section)
148     Result = reinterpret_cast<uintptr_t>(base() +
149                                          Section->PointerToRawData +
150                                          symb->Value);
151   else
152     Result = reinterpret_cast<uintptr_t>(base() + symb->Value);
153   return object_error::success;
154 }
155
156 error_code COFFObjectFile::getSymbolType(DataRefImpl Symb,
157                                          SymbolRef::SymbolType &Result) const {
158   const coff_symbol *symb = toSymb(Symb);
159   Result = SymbolRef::ST_Other;
160   if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
161       symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) {
162     Result = SymbolRef::ST_External;
163   } else {
164     if (symb->Type.ComplexType == COFF::IMAGE_SYM_DTYPE_FUNCTION) {
165       Result = SymbolRef::ST_Function;
166     } else {
167       char Type;
168       if (error_code ec = getSymbolNMTypeChar(Symb, Type))
169         return ec;
170       if (Type == 'r' || Type == 'R') {
171         Result = SymbolRef::ST_Data;
172       }
173     }
174   }
175   return object_error::success;
176 }
177
178 error_code COFFObjectFile::isSymbolGlobal(DataRefImpl Symb,
179                                           bool &Result) const {
180   const coff_symbol *symb = toSymb(Symb);
181   Result = (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL);
182   return object_error::success;
183 }
184
185 error_code COFFObjectFile::getSymbolSize(DataRefImpl Symb,
186                                          uint64_t &Result) const {
187   // FIXME: Return the correct size. This requires looking at all the symbols
188   //        in the same section as this symbol, and looking for either the next
189   //        symbol, or the end of the section.
190   const coff_symbol *symb = toSymb(Symb);
191   const coff_section *Section = NULL;
192   if (error_code ec = getSection(symb->SectionNumber, Section))
193     return ec;
194   char Type;
195   if (error_code ec = getSymbolNMTypeChar(Symb, Type))
196     return ec;
197   if (Type == 'U' || Type == 'w')
198     Result = UnknownAddressOrSize;
199   else if (Section)
200     Result = Section->SizeOfRawData - symb->Value;
201   else
202     Result = 0;
203   return object_error::success;
204 }
205
206 error_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)
213     .StartsWith(".debug", 'N')
214     .StartsWith(".sxdata", 'N')
215     .Default('?');
216
217   if (ret != '?') {
218     Result = ret;
219     return object_error::success;
220   }
221
222   uint32_t Characteristics = 0;
223   if (symb->SectionNumber > 0) {
224     const coff_section *Section = NULL;
225     if (error_code ec = getSection(symb->SectionNumber, Section))
226       return ec;
227     Characteristics = Section->Characteristics;
228   }
229
230   switch (symb->SectionNumber) {
231   case COFF::IMAGE_SYM_UNDEFINED:
232     // Check storage classes.
233     if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL) {
234       Result = 'w';
235       return object_error::success; // Don't do ::toupper.
236     } else
237       ret = 'u';
238     break;
239   case COFF::IMAGE_SYM_ABSOLUTE:
240     ret = 'a';
241     break;
242   case COFF::IMAGE_SYM_DEBUG:
243     ret = 'n';
244     break;
245   default:
246     // Check section type.
247     if (Characteristics & COFF::IMAGE_SCN_CNT_CODE)
248       ret = 't';
249     else if (  Characteristics & COFF::IMAGE_SCN_MEM_READ
250             && ~Characteristics & COFF::IMAGE_SCN_MEM_WRITE) // Read only.
251       ret = 'r';
252     else if (Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
253       ret = 'd';
254     else if (Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
255       ret = 'b';
256     else if (Characteristics & COFF::IMAGE_SCN_LNK_INFO)
257       ret = 'i';
258
259     // Check for section symbol.
260     else if (  symb->StorageClass == COFF::IMAGE_SYM_CLASS_STATIC
261             && symb->Value == 0)
262        ret = 's';
263   }
264
265   if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL)
266     ret = ::toupper(ret);
267
268   Result = ret;
269   return object_error::success;
270 }
271
272 error_code COFFObjectFile::isSymbolInternal(DataRefImpl Symb,
273                                             bool &Result) const {
274   Result = false;
275   return object_error::success;
276 }
277
278 error_code COFFObjectFile::getSectionNext(DataRefImpl Sec,
279                                           SectionRef &Result) const {
280   const coff_section *sec = toSec(Sec);
281   sec += 1;
282   Sec.p = reinterpret_cast<uintptr_t>(sec);
283   Result = SectionRef(Sec, this);
284   return object_error::success;
285 }
286
287 error_code COFFObjectFile::getSectionName(DataRefImpl Sec,
288                                           StringRef &Result) const {
289   const coff_section *sec = toSec(Sec);
290   StringRef name;
291   if (sec->Name[7] == 0)
292     // Null terminated, let ::strlen figure out the length.
293     name = sec->Name;
294   else
295     // Not null terminated, use all 8 bytes.
296     name = StringRef(sec->Name, 8);
297
298   // Check for string table entry. First byte is '/'.
299   if (name[0] == '/') {
300     uint32_t Offset;
301     name.substr(1).getAsInteger(10, Offset);
302     if (error_code ec = getString(Offset, name))
303       return ec;
304   }
305
306   Result = name;
307   return object_error::success;
308 }
309
310 error_code COFFObjectFile::getSectionAddress(DataRefImpl Sec,
311                                              uint64_t &Result) const {
312   const coff_section *sec = toSec(Sec);
313   Result = sec->VirtualAddress;
314   return object_error::success;
315 }
316
317 error_code COFFObjectFile::getSectionSize(DataRefImpl Sec,
318                                           uint64_t &Result) const {
319   const coff_section *sec = toSec(Sec);
320   Result = sec->SizeOfRawData;
321   return object_error::success;
322 }
323
324 error_code COFFObjectFile::getSectionContents(DataRefImpl Sec,
325                                               StringRef &Result) const {
326   const coff_section *sec = toSec(Sec);
327   // The only thing that we need to verify is that the contents is contained
328   // within the file bounds. We don't need to make sure it doesn't cover other
329   // data, as there's nothing that says that is not allowed.
330   uintptr_t con_start = uintptr_t(base()) + sec->PointerToRawData;
331   uintptr_t con_end = con_start + sec->SizeOfRawData;
332   if (con_end >= uintptr_t(Data->getBufferEnd()))
333     return object_error::parse_failed;
334   Result = StringRef(reinterpret_cast<const char*>(con_start),
335                      sec->SizeOfRawData);
336   return object_error::success;
337 }
338
339 error_code COFFObjectFile::isSectionText(DataRefImpl Sec,
340                                          bool &Result) const {
341   const coff_section *sec = toSec(Sec);
342   Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
343   return object_error::success;
344 }
345
346 error_code COFFObjectFile::isSectionData(DataRefImpl Sec,
347                                          bool &Result) const {
348   const coff_section *sec = toSec(Sec);
349   Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
350   return object_error::success;
351 }
352
353 error_code COFFObjectFile::isSectionBSS(DataRefImpl Sec,
354                                         bool &Result) const {
355   const coff_section *sec = toSec(Sec);
356   Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
357   return object_error::success;
358 }
359
360 error_code COFFObjectFile::sectionContainsSymbol(DataRefImpl Sec,
361                                                  DataRefImpl Symb,
362                                                  bool &Result) const {
363   // FIXME: Unimplemented.
364   Result = false;
365   return object_error::success;
366 }
367
368 relocation_iterator COFFObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
369   const coff_section *sec = toSec(Sec);
370   DataRefImpl ret;
371   std::memset(&ret, 0, sizeof(ret));
372   if (sec->NumberOfRelocations == 0)
373     ret.p = 0;
374   else
375     ret.p = reinterpret_cast<uintptr_t>(base() + sec->PointerToRelocations);
376
377   return relocation_iterator(RelocationRef(ret, this));
378 }
379
380 relocation_iterator COFFObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
381   const coff_section *sec = toSec(Sec);
382   DataRefImpl ret;
383   std::memset(&ret, 0, sizeof(ret));
384   if (sec->NumberOfRelocations == 0)
385     ret.p = 0;
386   else
387     ret.p = reinterpret_cast<uintptr_t>(
388               reinterpret_cast<const coff_relocation*>(
389                 base() + sec->PointerToRelocations)
390               + sec->NumberOfRelocations);
391
392   return relocation_iterator(RelocationRef(ret, this));
393 }
394
395 COFFObjectFile::COFFObjectFile(MemoryBuffer *Object, error_code &ec)
396   : ObjectFile(Binary::isCOFF, Object, ec) {
397   // Check that we at least have enough room for a header.
398   if (!checkSize(Data, ec, sizeof(coff_file_header))) return;
399
400   // The actual starting location of the COFF header in the file. This can be
401   // non-zero in PE/COFF files.
402   uint64_t HeaderStart = 0;
403
404   // Check if this is a PE/COFF file.
405   if (base()[0] == 0x4d && base()[1] == 0x5a) {
406     // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
407     // PE signature to find 'normal' COFF header.
408     if (!checkSize(Data, ec, 0x3c + 8)) return;
409     HeaderStart += *reinterpret_cast<const ulittle32_t *>(base() + 0x3c);
410     // Check the PE header. ("PE\0\0")
411     if (std::memcmp(base() + HeaderStart, "PE\0\0", 4) != 0) {
412       ec = object_error::parse_failed;
413       return;
414     }
415     HeaderStart += 4; // Skip the PE Header.
416   }
417
418   Header = reinterpret_cast<const coff_file_header *>(base() + HeaderStart);
419   if (!checkAddr(Data, ec, uintptr_t(Header), sizeof(coff_file_header)))
420     return;
421
422   SectionTable =
423     reinterpret_cast<const coff_section *>( base()
424                                           + HeaderStart
425                                           + sizeof(coff_file_header)
426                                           + Header->SizeOfOptionalHeader);
427   if (!checkAddr(Data, ec, uintptr_t(SectionTable),
428                  Header->NumberOfSections * sizeof(coff_section)))
429     return;
430
431   SymbolTable =
432     reinterpret_cast<const coff_symbol *>(base()
433                                           + Header->PointerToSymbolTable);
434   if (!checkAddr(Data, ec, uintptr_t(SymbolTable),
435                  Header->NumberOfSymbols * sizeof(coff_symbol)))
436     return;
437
438   // Find string table.
439   StringTable = reinterpret_cast<const char *>(base())
440                 + Header->PointerToSymbolTable
441                 + Header->NumberOfSymbols * sizeof(coff_symbol);
442   if (!checkAddr(Data, ec, uintptr_t(StringTable), sizeof(ulittle32_t)))
443     return;
444
445   StringTableSize = *reinterpret_cast<const ulittle32_t *>(StringTable);
446   if (!checkAddr(Data, ec, uintptr_t(StringTable), StringTableSize))
447     return;
448   // Check that the string table is null terminated if has any in it.
449   if (StringTableSize < 4
450       || (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)) {
451     ec = object_error::parse_failed;
452     return;
453   }
454
455   ec = object_error::success;
456 }
457
458 symbol_iterator COFFObjectFile::begin_symbols() const {
459   DataRefImpl ret;
460   std::memset(&ret, 0, sizeof(DataRefImpl));
461   ret.p = reinterpret_cast<intptr_t>(SymbolTable);
462   return symbol_iterator(SymbolRef(ret, this));
463 }
464
465 symbol_iterator COFFObjectFile::end_symbols() const {
466   // The symbol table ends where the string table begins.
467   DataRefImpl ret;
468   std::memset(&ret, 0, sizeof(DataRefImpl));
469   ret.p = reinterpret_cast<intptr_t>(StringTable);
470   return symbol_iterator(SymbolRef(ret, this));
471 }
472
473 section_iterator COFFObjectFile::begin_sections() const {
474   DataRefImpl ret;
475   std::memset(&ret, 0, sizeof(DataRefImpl));
476   ret.p = reinterpret_cast<intptr_t>(SectionTable);
477   return section_iterator(SectionRef(ret, this));
478 }
479
480 section_iterator COFFObjectFile::end_sections() const {
481   DataRefImpl ret;
482   std::memset(&ret, 0, sizeof(DataRefImpl));
483   ret.p = reinterpret_cast<intptr_t>(SectionTable + Header->NumberOfSections);
484   return section_iterator(SectionRef(ret, this));
485 }
486
487 uint8_t COFFObjectFile::getBytesInAddress() const {
488   return getArch() == Triple::x86_64 ? 8 : 4;
489 }
490
491 StringRef COFFObjectFile::getFileFormatName() const {
492   switch(Header->Machine) {
493   case COFF::IMAGE_FILE_MACHINE_I386:
494     return "COFF-i386";
495   case COFF::IMAGE_FILE_MACHINE_AMD64:
496     return "COFF-x86-64";
497   default:
498     return "COFF-<unknown arch>";
499   }
500 }
501
502 unsigned COFFObjectFile::getArch() const {
503   switch(Header->Machine) {
504   case COFF::IMAGE_FILE_MACHINE_I386:
505     return Triple::x86;
506   case COFF::IMAGE_FILE_MACHINE_AMD64:
507     return Triple::x86_64;
508   default:
509     return Triple::UnknownArch;
510   }
511 }
512
513 error_code COFFObjectFile::getSection(int32_t index,
514                                       const coff_section *&Result) const {
515   // Check for special index values.
516   if (index == COFF::IMAGE_SYM_UNDEFINED ||
517       index == COFF::IMAGE_SYM_ABSOLUTE ||
518       index == COFF::IMAGE_SYM_DEBUG)
519     Result = NULL;
520   else if (index > 0 && index <= Header->NumberOfSections)
521     // We already verified the section table data, so no need to check again.
522     Result = SectionTable + (index - 1);
523   else
524     return object_error::parse_failed;
525   return object_error::success;
526 }
527
528 error_code COFFObjectFile::getString(uint32_t offset,
529                                      StringRef &Result) const {
530   if (StringTableSize <= 4)
531     // Tried to get a string from an empty string table.
532     return object_error::parse_failed;
533   if (offset >= StringTableSize)
534     return object_error::unexpected_eof;
535   Result = StringRef(StringTable + offset);
536   return object_error::success;
537 }
538
539 error_code COFFObjectFile::getSymbol(uint32_t index,
540                                      const coff_symbol *&Result) const {
541   if (index > 0 && index < Header->NumberOfSymbols)
542     Result = SymbolTable + index;
543   else
544     return object_error::parse_failed;
545   return object_error::success;
546 }
547
548 const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
549   return reinterpret_cast<const coff_relocation*>(Rel.p);
550 }
551 error_code COFFObjectFile::getRelocationNext(DataRefImpl Rel,
552                                              RelocationRef &Res) const {
553   ++*reinterpret_cast<const coff_relocation**>(&Rel.p);
554   Res = RelocationRef(Rel, this);
555   return object_error::success;
556 }
557 error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel,
558                                                 uint64_t &Res) const {
559   Res = toRel(Rel)->VirtualAddress;
560   return object_error::success;
561 }
562 error_code COFFObjectFile::getRelocationSymbol(DataRefImpl Rel,
563                                                SymbolRef &Res) const {
564   const coff_relocation* R = toRel(Rel);
565   DataRefImpl Symb;
566   Symb.p = reinterpret_cast<uintptr_t>(SymbolTable + R->SymbolTableIndex);
567   Res = SymbolRef(Symb, this);
568   return object_error::success;
569 }
570 error_code COFFObjectFile::getRelocationType(DataRefImpl Rel,
571                                              uint32_t &Res) const {
572   const coff_relocation* R = toRel(Rel);
573   Res = R->Type;
574   return object_error::success;
575 }
576
577 #define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(enum) \
578   case COFF::enum: res = #enum; break;
579
580 error_code COFFObjectFile::getRelocationTypeName(DataRefImpl Rel,
581                                           SmallVectorImpl<char> &Result) const {
582   const coff_relocation *reloc = toRel(Rel);
583   StringRef res;
584   switch (Header->Machine) {
585   case COFF::IMAGE_FILE_MACHINE_AMD64:
586     switch (reloc->Type) {
587     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
588     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
589     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
590     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
591     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
592     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
593     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
594     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
595     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
596     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
597     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
598     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
599     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
600     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
601     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
602     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
603     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
604     default:
605       res = "Unknown";
606     }
607     break;
608   case COFF::IMAGE_FILE_MACHINE_I386:
609     switch (reloc->Type) {
610     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
611     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
612     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
613     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
614     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
615     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
616     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
617     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
618     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
619     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
620     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
621     default:
622       res = "Unknown";
623     }
624     break;
625   default:
626     res = "Unknown";
627   }
628   Result.append(res.begin(), res.end());
629   return object_error::success;
630 }
631
632 #undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
633
634 error_code COFFObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel,
635                                                        int64_t &Res) const {
636   Res = 0;
637   return object_error::success;
638 }
639 error_code COFFObjectFile::getRelocationValueString(DataRefImpl Rel,
640                                           SmallVectorImpl<char> &Result) const {
641   const coff_relocation *reloc = toRel(Rel);
642   const coff_symbol *symb;
643   if (error_code ec = getSymbol(reloc->SymbolTableIndex, symb)) return ec;
644   DataRefImpl sym;
645   ::memset(&sym, 0, sizeof(sym));
646   sym.p = reinterpret_cast<uintptr_t>(symb);
647   StringRef symname;
648   if (error_code ec = getSymbolName(sym, symname)) return ec;
649   Result.append(symname.begin(), symname.end());
650   return object_error::success;
651 }
652
653 namespace llvm {
654
655   ObjectFile *ObjectFile::createCOFFObjectFile(MemoryBuffer *Object) {
656     error_code ec;
657     return new COFFObjectFile(Object, ec);
658   }
659
660 } // end namespace llvm