blob: 86bf44baaeb6e6681a6462b4f27e5377b8f438c2 [file] [log] [blame]
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +00001//===- COFFObjectFile.cpp - COFF object file implementation -----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file declares the COFFObjectFile class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/ADT/StringSwitch.h"
15#include "llvm/ADT/Triple.h"
Michael J. Spencer5e45dc42011-06-13 11:53:31 +000016#include "llvm/Object/ObjectFile.h"
17#include "llvm/Support/COFF.h"
18#include "llvm/Support/Endian.h"
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +000019
20using namespace llvm;
21using namespace object;
22
23namespace {
24using support::ulittle8_t;
25using support::ulittle16_t;
26using support::ulittle32_t;
27using support::little16_t;
28}
29
Michael J. Spencer5e45dc42011-06-13 11:53:31 +000030namespace {
31struct 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
42extern char coff_file_header_layout_static_assert
43 [sizeof(coff_file_header) == 20 ? 1 : -1];
44
45namespace {
46struct 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
70extern char coff_coff_symbol_layout_static_assert
71 [sizeof(coff_symbol) == 18 ? 1 : -1];
72
73namespace {
74struct 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
88extern char coff_coff_section_layout_static_assert
89 [sizeof(coff_section) == 40 ? 1 : -1];
90
91namespace {
92class COFFObjectFile : public ObjectFile {
93private:
94 uint64_t HeaderOff;
95 const coff_file_header *Header;
96 const coff_section *SectionTable;
97 const coff_symbol *SymbolTable;
98 const char *StringTable;
99
100 const coff_section *getSection(std::size_t index) const;
101 const char *getString(std::size_t offset) const;
102
103protected:
104 virtual SymbolRef getSymbolNext(DataRefImpl Symb) const;
105 virtual StringRef getSymbolName(DataRefImpl Symb) const;
106 virtual uint64_t getSymbolAddress(DataRefImpl Symb) const;
107 virtual uint64_t getSymbolSize(DataRefImpl Symb) const;
108 virtual char getSymbolNMTypeChar(DataRefImpl Symb) const;
109 virtual bool isSymbolInternal(DataRefImpl Symb) const;
110
111 virtual SectionRef getSectionNext(DataRefImpl Sec) const;
112 virtual StringRef getSectionName(DataRefImpl Sec) const;
113 virtual uint64_t getSectionAddress(DataRefImpl Sec) const;
114 virtual uint64_t getSectionSize(DataRefImpl Sec) const;
115 virtual StringRef getSectionContents(DataRefImpl Sec) const;
116 virtual bool isSectionText(DataRefImpl Sec) const;
117
118public:
119 COFFObjectFile(MemoryBuffer *Object);
120 virtual symbol_iterator begin_symbols() const;
121 virtual symbol_iterator end_symbols() const;
122 virtual section_iterator begin_sections() const;
123 virtual section_iterator end_sections() const;
124
125 virtual uint8_t getBytesInAddress() const;
126 virtual StringRef getFileFormatName() const;
127 virtual unsigned getArch() const;
128};
129} // end namespace
130
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000131SymbolRef COFFObjectFile::getSymbolNext(DataRefImpl Symb) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000132 const coff_symbol *symb = reinterpret_cast<const coff_symbol*>(Symb.p);
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000133 symb += 1 + symb->NumberOfAuxSymbols;
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000134 Symb.p = reinterpret_cast<intptr_t>(symb);
135 return SymbolRef(Symb, this);
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000136}
137
138StringRef COFFObjectFile::getSymbolName(DataRefImpl Symb) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000139 const coff_symbol *symb = reinterpret_cast<const coff_symbol*>(Symb.p);
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000140 // Check for string table entry. First 4 bytes are 0.
141 if (symb->Name.Offset.Zeroes == 0) {
142 uint32_t Offset = symb->Name.Offset.Offset;
143 return StringRef(getString(Offset));
144 }
145
146 if (symb->Name.ShortName[7] == 0)
147 // Null terminated, let ::strlen figure out the length.
148 return StringRef(symb->Name.ShortName);
149 // Not null terminated, use all 8 bytes.
150 return StringRef(symb->Name.ShortName, 8);
151}
152
153uint64_t COFFObjectFile::getSymbolAddress(DataRefImpl Symb) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000154 const coff_symbol *symb = reinterpret_cast<const coff_symbol*>(Symb.p);
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000155 const coff_section *Section = getSection(symb->SectionNumber);
156 char Type = getSymbolNMTypeChar(Symb);
157 if (Type == 'U' || Type == 'w')
158 return UnknownAddressOrSize;
159 if (Section)
160 return Section->VirtualAddress + symb->Value;
161 return symb->Value;
162}
163
164uint64_t COFFObjectFile::getSymbolSize(DataRefImpl Symb) const {
165 // FIXME: Return the correct size. This requires looking at all the symbols
166 // in the same section as this symbol, and looking for either the next
167 // symbol, or the end of the section.
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000168 const coff_symbol *symb = reinterpret_cast<const coff_symbol*>(Symb.p);
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000169 const coff_section *Section = getSection(symb->SectionNumber);
170 char Type = getSymbolNMTypeChar(Symb);
171 if (Type == 'U' || Type == 'w')
172 return UnknownAddressOrSize;
173 if (Section)
174 return Section->SizeOfRawData - symb->Value;
175 return 0;
176}
177
178char COFFObjectFile::getSymbolNMTypeChar(DataRefImpl Symb) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000179 const coff_symbol *symb = reinterpret_cast<const coff_symbol*>(Symb.p);
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000180 char ret = StringSwitch<char>(getSymbolName(Symb))
181 .StartsWith(".debug", 'N')
182 .StartsWith(".sxdata", 'N')
183 .Default('?');
184
185 if (ret != '?')
186 return ret;
187
188 uint32_t Characteristics = 0;
Nick Lewycky71f234b2011-05-02 05:05:29 +0000189 if (const coff_section *Section = getSection(symb->SectionNumber)) {
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000190 Characteristics = Section->Characteristics;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000191 }
192
193 switch (symb->SectionNumber) {
194 case COFF::IMAGE_SYM_UNDEFINED:
195 // Check storage classes.
196 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL)
197 return 'w'; // Don't do ::toupper.
198 else
199 ret = 'u';
200 break;
201 case COFF::IMAGE_SYM_ABSOLUTE:
202 ret = 'a';
203 break;
204 case COFF::IMAGE_SYM_DEBUG:
205 ret = 'n';
206 break;
207 default:
208 // Check section type.
209 if (Characteristics & COFF::IMAGE_SCN_CNT_CODE)
210 ret = 't';
211 else if ( Characteristics & COFF::IMAGE_SCN_MEM_READ
212 && ~Characteristics & COFF::IMAGE_SCN_MEM_WRITE) // Read only.
213 ret = 'r';
214 else if (Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
215 ret = 'd';
216 else if (Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
217 ret = 'b';
218 else if (Characteristics & COFF::IMAGE_SCN_LNK_INFO)
219 ret = 'i';
220
221 // Check for section symbol.
222 else if ( symb->StorageClass == COFF::IMAGE_SYM_CLASS_STATIC
223 && symb->Value == 0)
224 ret = 's';
225 }
226
227 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL)
228 ret = ::toupper(ret);
229
230 return ret;
231}
232
233bool COFFObjectFile::isSymbolInternal(DataRefImpl Symb) const {
234 return false;
235}
236
237SectionRef COFFObjectFile::getSectionNext(DataRefImpl Sec) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000238 const coff_section *sec = reinterpret_cast<const coff_section*>(Sec.p);
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000239 sec += 1;
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000240 Sec.p = reinterpret_cast<intptr_t>(sec);
241 return SectionRef(Sec, this);
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000242}
243
244StringRef COFFObjectFile::getSectionName(DataRefImpl Sec) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000245 const coff_section *sec = reinterpret_cast<const coff_section*>(Sec.p);
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000246 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;
Eric Christopher539d8d82011-04-03 22:53:19 +0000257 name.substr(1).getAsInteger(10, Offset);
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000258 return StringRef(getString(Offset));
259 }
260
261 // It's just a normal name.
262 return name;
263}
264
265uint64_t COFFObjectFile::getSectionAddress(DataRefImpl Sec) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000266 const coff_section *sec = reinterpret_cast<const coff_section*>(Sec.p);
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000267 return sec->VirtualAddress;
268}
269
270uint64_t COFFObjectFile::getSectionSize(DataRefImpl Sec) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000271 const coff_section *sec = reinterpret_cast<const coff_section*>(Sec.p);
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000272 return sec->SizeOfRawData;
273}
274
275StringRef COFFObjectFile::getSectionContents(DataRefImpl Sec) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000276 const coff_section *sec = reinterpret_cast<const coff_section*>(Sec.p);
Michael J. Spencer5e45dc42011-06-13 11:53:31 +0000277 return StringRef(reinterpret_cast<const char *>(base + sec->PointerToRawData),
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000278 sec->SizeOfRawData);
279}
280
281bool COFFObjectFile::isSectionText(DataRefImpl Sec) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000282 const coff_section *sec = reinterpret_cast<const coff_section*>(Sec.p);
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000283 return sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
284}
285
Michael J. Spencer5e45dc42011-06-13 11:53:31 +0000286COFFObjectFile::COFFObjectFile(MemoryBuffer *Object)
287 : ObjectFile(Object) {
Eric Christopher539d8d82011-04-03 22:53:19 +0000288
289 HeaderOff = 0;
290
Michael J. Spencer5e45dc42011-06-13 11:53:31 +0000291 if (base[0] == 0x4d && base[1] == 0x5a) {
Eric Christopher539d8d82011-04-03 22:53:19 +0000292 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
293 // PE signature to find 'normal' COFF header.
Michael J. Spencer5e45dc42011-06-13 11:53:31 +0000294 HeaderOff += *reinterpret_cast<const ulittle32_t *>(base + 0x3c);
Eric Christopher539d8d82011-04-03 22:53:19 +0000295 HeaderOff += 4;
296 }
297
Michael J. Spencer5e45dc42011-06-13 11:53:31 +0000298 Header = reinterpret_cast<const coff_file_header *>(base + HeaderOff);
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000299 SectionTable =
Michael J. Spencer5e45dc42011-06-13 11:53:31 +0000300 reinterpret_cast<const coff_section *>( base
Eric Christopher539d8d82011-04-03 22:53:19 +0000301 + HeaderOff
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000302 + sizeof(coff_file_header)
303 + Header->SizeOfOptionalHeader);
304 SymbolTable =
Michael J. Spencer5e45dc42011-06-13 11:53:31 +0000305 reinterpret_cast<const coff_symbol *>(base + Header->PointerToSymbolTable);
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000306
307 // Find string table.
Michael J. Spencer5e45dc42011-06-13 11:53:31 +0000308 StringTable = reinterpret_cast<const char *>(base)
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000309 + Header->PointerToSymbolTable
310 + Header->NumberOfSymbols * 18;
311}
312
313ObjectFile::symbol_iterator COFFObjectFile::begin_symbols() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000314 DataRefImpl ret;
Eric Christopher539d8d82011-04-03 22:53:19 +0000315 memset(&ret, 0, sizeof(DataRefImpl));
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000316 ret.p = reinterpret_cast<intptr_t>(SymbolTable);
317 return symbol_iterator(SymbolRef(ret, this));
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000318}
319
320ObjectFile::symbol_iterator COFFObjectFile::end_symbols() const {
321 // The symbol table ends where the string table begins.
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000322 DataRefImpl ret;
Eric Christopher539d8d82011-04-03 22:53:19 +0000323 memset(&ret, 0, sizeof(DataRefImpl));
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000324 ret.p = reinterpret_cast<intptr_t>(StringTable);
325 return symbol_iterator(SymbolRef(ret, this));
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000326}
327
328ObjectFile::section_iterator COFFObjectFile::begin_sections() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000329 DataRefImpl ret;
Eric Christopher539d8d82011-04-03 22:53:19 +0000330 memset(&ret, 0, sizeof(DataRefImpl));
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000331 ret.p = reinterpret_cast<intptr_t>(SectionTable);
332 return section_iterator(SectionRef(ret, this));
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000333}
334
335ObjectFile::section_iterator COFFObjectFile::end_sections() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000336 DataRefImpl ret;
Eric Christopher539d8d82011-04-03 22:53:19 +0000337 memset(&ret, 0, sizeof(DataRefImpl));
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000338 ret.p = reinterpret_cast<intptr_t>(SectionTable + Header->NumberOfSections);
339 return section_iterator(SectionRef(ret, this));
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000340}
341
342uint8_t COFFObjectFile::getBytesInAddress() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000343 return getArch() == Triple::x86_64 ? 8 : 4;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000344}
345
346StringRef COFFObjectFile::getFileFormatName() const {
347 switch(Header->Machine) {
348 case COFF::IMAGE_FILE_MACHINE_I386:
349 return "COFF-i386";
350 case COFF::IMAGE_FILE_MACHINE_AMD64:
351 return "COFF-x86-64";
352 default:
353 return "COFF-<unknown arch>";
354 }
355}
356
357unsigned COFFObjectFile::getArch() const {
358 switch(Header->Machine) {
359 case COFF::IMAGE_FILE_MACHINE_I386:
360 return Triple::x86;
361 case COFF::IMAGE_FILE_MACHINE_AMD64:
362 return Triple::x86_64;
363 default:
364 return Triple::UnknownArch;
365 }
366}
367
368const coff_section *COFFObjectFile::getSection(std::size_t index) const {
369 if (index > 0 && index <= Header->NumberOfSections)
370 return SectionTable + (index - 1);
371 return 0;
372}
373
374const char *COFFObjectFile::getString(std::size_t offset) const {
375 const ulittle32_t *StringTableSize =
376 reinterpret_cast<const ulittle32_t *>(StringTable);
377 if (offset < *StringTableSize)
378 return StringTable + offset;
379 return 0;
380}
381
382namespace llvm {
383
384 ObjectFile *ObjectFile::createCOFFObjectFile(MemoryBuffer *Object) {
Michael J. Spencer5e45dc42011-06-13 11:53:31 +0000385 return new COFFObjectFile(Object);
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000386 }
387
388} // end namespace llvm