Object: Add COFF Support.
[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/ADT/StringSwitch.h"
15 #include "llvm/ADT/Triple.h"
16 #include "llvm/Object/ObjectFile.h"
17 #include "llvm/Support/COFF.h"
18 #include "llvm/Support/Endian.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 struct coff_file_header {
32   ulittle16_t Machine;
33   ulittle16_t NumberOfSections;
34   ulittle32_t TimeDateStamp;
35   ulittle32_t PointerToSymbolTable;
36   ulittle32_t NumberOfSymbols;
37   ulittle16_t SizeOfOptionalHeader;
38   ulittle16_t Characteristics;
39 };
40 }
41
42 extern char coff_file_header_layout_static_assert
43             [sizeof(coff_file_header) == 20 ? 1 : -1];
44
45 namespace {
46 struct coff_symbol {
47   struct StringTableOffset {
48     ulittle32_t Zeroes;
49     ulittle32_t Offset;
50   };
51
52   union {
53     char ShortName[8];
54     StringTableOffset Offset;
55   } Name;
56
57   ulittle32_t Value;
58   little16_t SectionNumber;
59
60   struct {
61     ulittle8_t BaseType;
62     ulittle8_t ComplexType;
63   } Type;
64
65   ulittle8_t  StorageClass;
66   ulittle8_t  NumberOfAuxSymbols;
67 };
68 }
69
70 extern char coff_coff_symbol_layout_static_assert
71             [sizeof(coff_symbol) == 18 ? 1 : -1];
72
73 namespace {
74 struct coff_section {
75   char Name[8];
76   ulittle32_t VirtualSize;
77   ulittle32_t VirtualAddress;
78   ulittle32_t SizeOfRawData;
79   ulittle32_t PointerToRawData;
80   ulittle32_t PointerToRelocations;
81   ulittle32_t PointerToLinenumbers;
82   ulittle16_t NumberOfRelocations;
83   ulittle16_t NumberOfLinenumbers;
84   ulittle32_t Characteristics;
85 };
86 }
87
88 extern char coff_coff_section_layout_static_assert
89             [sizeof(coff_section) == 40 ? 1 : -1];
90
91 namespace {
92 class COFFObjectFile : public ObjectFile {
93 private:
94   const coff_file_header *Header;
95   const coff_section     *SectionTable;
96   const coff_symbol      *SymbolTable;
97   const char             *StringTable;
98
99   const coff_section     *getSection(std::size_t index) const;
100   const char             *getString(std::size_t offset) const;
101
102 protected:
103   virtual SymbolRef getSymbolNext(DataRefImpl Symb) const;
104   virtual StringRef getSymbolName(DataRefImpl Symb) const;
105   virtual uint64_t  getSymbolAddress(DataRefImpl Symb) const;
106   virtual uint64_t  getSymbolSize(DataRefImpl Symb) const;
107   virtual char      getSymbolNMTypeChar(DataRefImpl Symb) const;
108   virtual bool      isSymbolInternal(DataRefImpl Symb) const;
109
110   virtual SectionRef getSectionNext(DataRefImpl Sec) const;
111   virtual StringRef  getSectionName(DataRefImpl Sec) const;
112   virtual uint64_t   getSectionAddress(DataRefImpl Sec) const;
113   virtual uint64_t   getSectionSize(DataRefImpl Sec) const;
114   virtual StringRef  getSectionContents(DataRefImpl Sec) const;
115   virtual bool       isSectionText(DataRefImpl Sec) const;
116
117 public:
118   COFFObjectFile(MemoryBuffer *Object);
119   virtual symbol_iterator begin_symbols() const;
120   virtual symbol_iterator end_symbols() const;
121   virtual section_iterator begin_sections() const;
122   virtual section_iterator end_sections() const;
123
124   virtual uint8_t getBytesInAddress() const;
125   virtual StringRef getFileFormatName() const;
126   virtual unsigned getArch() const;
127 };
128 } // end namespace
129
130 SymbolRef COFFObjectFile::getSymbolNext(DataRefImpl Symb) const {
131   const coff_symbol *symb = *reinterpret_cast<const coff_symbol**>(&Symb);
132   symb += 1 + symb->NumberOfAuxSymbols;
133   return SymbolRef(DataRefImpl(symb), this);
134 }
135
136 StringRef COFFObjectFile::getSymbolName(DataRefImpl Symb) const {
137   const coff_symbol *symb = *reinterpret_cast<const coff_symbol**>(&Symb);
138   // Check for string table entry. First 4 bytes are 0.
139   if (symb->Name.Offset.Zeroes == 0) {
140     uint32_t Offset = symb->Name.Offset.Offset;
141     return StringRef(getString(Offset));
142   }
143
144   if (symb->Name.ShortName[7] == 0)
145     // Null terminated, let ::strlen figure out the length.
146     return StringRef(symb->Name.ShortName);
147   // Not null terminated, use all 8 bytes.
148   return StringRef(symb->Name.ShortName, 8);
149 }
150
151 uint64_t COFFObjectFile::getSymbolAddress(DataRefImpl Symb) const {
152   const coff_symbol *symb = *reinterpret_cast<const coff_symbol**>(&Symb);
153   const coff_section *Section = getSection(symb->SectionNumber);
154   char Type = getSymbolNMTypeChar(Symb);
155   if (Type == 'U' || Type == 'w')
156     return UnknownAddressOrSize;
157   if (Section)
158     return Section->VirtualAddress + symb->Value;
159   return symb->Value;
160 }
161
162 uint64_t COFFObjectFile::getSymbolSize(DataRefImpl Symb) const {
163   // FIXME: Return the correct size. This requires looking at all the symbols
164   //        in the same section as this symbol, and looking for either the next
165   //        symbol, or the end of the section.
166   const coff_symbol *symb = *reinterpret_cast<const coff_symbol**>(&Symb);
167   const coff_section *Section = getSection(symb->SectionNumber);
168   char Type = getSymbolNMTypeChar(Symb);
169   if (Type == 'U' || Type == 'w')
170     return UnknownAddressOrSize;
171   if (Section)
172     return Section->SizeOfRawData - symb->Value;
173   return 0;
174 }
175
176 char COFFObjectFile::getSymbolNMTypeChar(DataRefImpl Symb) const {
177   const coff_symbol *symb = *reinterpret_cast<const coff_symbol**>(&Symb);
178   char ret = StringSwitch<char>(getSymbolName(Symb))
179     .StartsWith(".debug", 'N')
180     .StartsWith(".sxdata", 'N')
181     .Default('?');
182
183   if (ret != '?')
184     return ret;
185
186   uint32_t Characteristics = 0;
187   uint32_t PointerToRawData = 0;
188   const coff_section *Section = getSection(symb->SectionNumber);
189   if (Section) {
190     Characteristics = Section->Characteristics;
191     PointerToRawData = Section->PointerToRawData;
192   }
193
194   switch (symb->SectionNumber) {
195   case COFF::IMAGE_SYM_UNDEFINED:
196     // Check storage classes.
197     if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL)
198       return 'w'; // Don't do ::toupper.
199     else
200       ret = 'u';
201     break;
202   case COFF::IMAGE_SYM_ABSOLUTE:
203     ret = 'a';
204     break;
205   case COFF::IMAGE_SYM_DEBUG:
206     ret = 'n';
207     break;
208   default:
209     // Check section type.
210     if (Characteristics & COFF::IMAGE_SCN_CNT_CODE)
211       ret = 't';
212     else if (  Characteristics & COFF::IMAGE_SCN_MEM_READ
213             && ~Characteristics & COFF::IMAGE_SCN_MEM_WRITE) // Read only.
214       ret = 'r';
215     else if (Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
216       ret = 'd';
217     else if (Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
218       ret = 'b';
219     else if (Characteristics & COFF::IMAGE_SCN_LNK_INFO)
220       ret = 'i';
221
222     // Check for section symbol.
223     else if (  symb->StorageClass == COFF::IMAGE_SYM_CLASS_STATIC
224             && symb->Value == 0)
225        ret = 's';
226   }
227
228   if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL)
229     ret = ::toupper(ret);
230
231   return ret;
232 }
233
234 bool COFFObjectFile::isSymbolInternal(DataRefImpl Symb) const {
235   return false;
236 }
237
238 SectionRef COFFObjectFile::getSectionNext(DataRefImpl Sec) const {
239   const coff_section *sec = *reinterpret_cast<const coff_section**>(&Sec);
240   sec += 1;
241   return SectionRef(DataRefImpl(sec), this);
242 }
243
244 StringRef COFFObjectFile::getSectionName(DataRefImpl Sec) const {
245   const coff_section *sec = *reinterpret_cast<const coff_section**>(&Sec);
246   StringRef name;
247   if (sec->Name[7] == 0)
248     // Null terminated, let ::strlen figure out the length.
249     name = sec->Name;
250   else
251     // Not null terminated, use all 8 bytes.
252     name = StringRef(sec->Name, 8);
253
254   // Check for string table entry. First byte is '/'.
255   if (name[0] == '/') {
256     uint32_t Offset;
257     name.getAsInteger(10, Offset);
258     return StringRef(getString(Offset));
259   }
260
261   // It's just a normal name.
262   return name;
263 }
264
265 uint64_t COFFObjectFile::getSectionAddress(DataRefImpl Sec) const {
266   const coff_section *sec = *reinterpret_cast<const coff_section**>(&Sec);
267   return sec->VirtualAddress;
268 }
269
270 uint64_t COFFObjectFile::getSectionSize(DataRefImpl Sec) const {
271   const coff_section *sec = *reinterpret_cast<const coff_section**>(&Sec);
272   return sec->SizeOfRawData;
273 }
274
275 StringRef COFFObjectFile::getSectionContents(DataRefImpl Sec) const {
276   const coff_section *sec = *reinterpret_cast<const coff_section**>(&Sec);
277   return StringRef(reinterpret_cast<const char *>(base + sec->PointerToRawData),
278                    sec->SizeOfRawData);
279 }
280
281 bool COFFObjectFile::isSectionText(DataRefImpl Sec) const {
282   const coff_section *sec = *reinterpret_cast<const coff_section**>(&Sec);
283   return sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
284 }
285
286 COFFObjectFile::COFFObjectFile(MemoryBuffer *Object)
287   : ObjectFile(Object) {
288   Header = reinterpret_cast<const coff_file_header *>(base);
289   SectionTable =
290     reinterpret_cast<const coff_section *>( base
291                                           + sizeof(coff_file_header)
292                                           + Header->SizeOfOptionalHeader);
293   SymbolTable =
294     reinterpret_cast<const coff_symbol *>(base + Header->PointerToSymbolTable);
295
296   // Find string table.
297   StringTable = reinterpret_cast<const char *>(base)
298               + Header->PointerToSymbolTable
299               + Header->NumberOfSymbols * 18;
300 }
301
302 ObjectFile::symbol_iterator COFFObjectFile::begin_symbols() const {
303   return symbol_iterator(
304     SymbolRef(DataRefImpl(SymbolTable), this));
305 }
306
307 ObjectFile::symbol_iterator COFFObjectFile::end_symbols() const {
308   // The symbol table ends where the string table begins.
309   return symbol_iterator(
310     SymbolRef(DataRefImpl(StringTable), this));
311 }
312
313 ObjectFile::section_iterator COFFObjectFile::begin_sections() const {
314   return section_iterator(
315     SectionRef(DataRefImpl(SectionTable), this));
316 }
317
318 ObjectFile::section_iterator COFFObjectFile::end_sections() const {
319   return section_iterator(
320     SectionRef(
321       DataRefImpl((void *)(SectionTable + Header->NumberOfSections)), this));
322 }
323
324 uint8_t COFFObjectFile::getBytesInAddress() const {
325   return 4;
326 }
327
328 StringRef COFFObjectFile::getFileFormatName() const {
329   switch(Header->Machine) {
330   case COFF::IMAGE_FILE_MACHINE_I386:
331     return "COFF-i386";
332   case COFF::IMAGE_FILE_MACHINE_AMD64:
333     return "COFF-x86-64";
334   default:
335     return "COFF-<unknown arch>";
336   }
337 }
338
339 unsigned COFFObjectFile::getArch() const {
340   switch(Header->Machine) {
341   case COFF::IMAGE_FILE_MACHINE_I386:
342     return Triple::x86;
343   case COFF::IMAGE_FILE_MACHINE_AMD64:
344     return Triple::x86_64;
345   default:
346     return Triple::UnknownArch;
347   }
348 }
349
350 const coff_section *COFFObjectFile::getSection(std::size_t index) const {
351   if (index > 0 && index <= Header->NumberOfSections)
352     return SectionTable + (index - 1);
353   return 0;
354 }
355
356 const char *COFFObjectFile::getString(std::size_t offset) const {
357   const ulittle32_t *StringTableSize =
358     reinterpret_cast<const ulittle32_t *>(StringTable);
359   if (offset < *StringTableSize)
360     return StringTable + offset;
361   return 0;
362 }
363
364 namespace llvm {
365
366   ObjectFile *ObjectFile::createCOFFObjectFile(MemoryBuffer *Object) {
367     return new COFFObjectFile(Object);
368   }
369
370 } // end namespace llvm