1 //===- COFFObjectFile.cpp - COFF object file implementation -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the COFFObjectFile class.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Object/COFF.h"
15 #include "llvm/ADT/StringSwitch.h"
16 #include "llvm/ADT/Triple.h"
19 using namespace object;
22 using support::ulittle8_t;
23 using support::ulittle16_t;
24 using support::ulittle32_t;
25 using support::little16_t;
28 SymbolRef COFFObjectFile::getSymbolNext(DataRefImpl Symb) const {
29 const coff_symbol *symb = reinterpret_cast<const coff_symbol*>(Symb.p);
30 symb += 1 + symb->NumberOfAuxSymbols;
31 Symb.p = reinterpret_cast<intptr_t>(symb);
32 return SymbolRef(Symb, this);
35 StringRef COFFObjectFile::getSymbolName(DataRefImpl Symb) const {
36 const coff_symbol *symb = reinterpret_cast<const coff_symbol*>(Symb.p);
37 // Check for string table entry. First 4 bytes are 0.
38 if (symb->Name.Offset.Zeroes == 0) {
39 uint32_t Offset = symb->Name.Offset.Offset;
40 return StringRef(getString(Offset));
43 if (symb->Name.ShortName[7] == 0)
44 // Null terminated, let ::strlen figure out the length.
45 return StringRef(symb->Name.ShortName);
46 // Not null terminated, use all 8 bytes.
47 return StringRef(symb->Name.ShortName, 8);
50 uint64_t COFFObjectFile::getSymbolAddress(DataRefImpl Symb) const {
51 const coff_symbol *symb = reinterpret_cast<const coff_symbol*>(Symb.p);
52 const coff_section *Section = getSection(symb->SectionNumber);
53 char Type = getSymbolNMTypeChar(Symb);
54 if (Type == 'U' || Type == 'w')
55 return UnknownAddressOrSize;
57 return Section->VirtualAddress + symb->Value;
61 uint64_t COFFObjectFile::getSymbolSize(DataRefImpl Symb) const {
62 // FIXME: Return the correct size. This requires looking at all the symbols
63 // in the same section as this symbol, and looking for either the next
64 // symbol, or the end of the section.
65 const coff_symbol *symb = reinterpret_cast<const coff_symbol*>(Symb.p);
66 const coff_section *Section = getSection(symb->SectionNumber);
67 char Type = getSymbolNMTypeChar(Symb);
68 if (Type == 'U' || Type == 'w')
69 return UnknownAddressOrSize;
71 return Section->SizeOfRawData - symb->Value;
75 char COFFObjectFile::getSymbolNMTypeChar(DataRefImpl Symb) const {
76 const coff_symbol *symb = reinterpret_cast<const coff_symbol*>(Symb.p);
77 char ret = StringSwitch<char>(getSymbolName(Symb))
78 .StartsWith(".debug", 'N')
79 .StartsWith(".sxdata", 'N')
85 uint32_t Characteristics = 0;
86 if (const coff_section *Section = getSection(symb->SectionNumber)) {
87 Characteristics = Section->Characteristics;
90 switch (symb->SectionNumber) {
91 case COFF::IMAGE_SYM_UNDEFINED:
92 // Check storage classes.
93 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL)
94 return 'w'; // Don't do ::toupper.
98 case COFF::IMAGE_SYM_ABSOLUTE:
101 case COFF::IMAGE_SYM_DEBUG:
105 // Check section type.
106 if (Characteristics & COFF::IMAGE_SCN_CNT_CODE)
108 else if ( Characteristics & COFF::IMAGE_SCN_MEM_READ
109 && ~Characteristics & COFF::IMAGE_SCN_MEM_WRITE) // Read only.
111 else if (Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
113 else if (Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
115 else if (Characteristics & COFF::IMAGE_SCN_LNK_INFO)
118 // Check for section symbol.
119 else if ( symb->StorageClass == COFF::IMAGE_SYM_CLASS_STATIC
124 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL)
125 ret = ::toupper(ret);
130 bool COFFObjectFile::isSymbolInternal(DataRefImpl Symb) const {
134 SectionRef COFFObjectFile::getSectionNext(DataRefImpl Sec) const {
135 const coff_section *sec = reinterpret_cast<const coff_section*>(Sec.p);
137 Sec.p = reinterpret_cast<intptr_t>(sec);
138 return SectionRef(Sec, this);
141 StringRef COFFObjectFile::getSectionName(DataRefImpl Sec) const {
142 const coff_section *sec = reinterpret_cast<const coff_section*>(Sec.p);
144 if (sec->Name[7] == 0)
145 // Null terminated, let ::strlen figure out the length.
148 // Not null terminated, use all 8 bytes.
149 name = StringRef(sec->Name, 8);
151 // Check for string table entry. First byte is '/'.
152 if (name[0] == '/') {
154 name.substr(1).getAsInteger(10, Offset);
155 return StringRef(getString(Offset));
158 // It's just a normal name.
162 uint64_t COFFObjectFile::getSectionAddress(DataRefImpl Sec) const {
163 const coff_section *sec = reinterpret_cast<const coff_section*>(Sec.p);
164 return sec->VirtualAddress;
167 uint64_t COFFObjectFile::getSectionSize(DataRefImpl Sec) const {
168 const coff_section *sec = reinterpret_cast<const coff_section*>(Sec.p);
169 return sec->SizeOfRawData;
172 StringRef COFFObjectFile::getSectionContents(DataRefImpl Sec) const {
173 const coff_section *sec = reinterpret_cast<const coff_section*>(Sec.p);
174 return StringRef(reinterpret_cast<const char *>(base()
175 + sec->PointerToRawData),
179 bool COFFObjectFile::isSectionText(DataRefImpl Sec) const {
180 const coff_section *sec = reinterpret_cast<const coff_section*>(Sec.p);
181 return sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
184 COFFObjectFile::COFFObjectFile(MemoryBuffer *Object, error_code &ec)
185 : ObjectFile(Binary::isCOFF, Object, ec) {
189 if (base()[0] == 0x4d && base()[1] == 0x5a) {
190 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
191 // PE signature to find 'normal' COFF header.
192 HeaderOff += *reinterpret_cast<const ulittle32_t *>(base() + 0x3c);
196 Header = reinterpret_cast<const coff_file_header *>(base() + HeaderOff);
198 reinterpret_cast<const coff_section *>( base()
200 + sizeof(coff_file_header)
201 + Header->SizeOfOptionalHeader);
203 reinterpret_cast<const coff_symbol *>(base()
204 + Header->PointerToSymbolTable);
206 // Find string table.
207 StringTable = reinterpret_cast<const char *>(base())
208 + Header->PointerToSymbolTable
209 + Header->NumberOfSymbols * 18;
212 ObjectFile::symbol_iterator COFFObjectFile::begin_symbols() const {
214 memset(&ret, 0, sizeof(DataRefImpl));
215 ret.p = reinterpret_cast<intptr_t>(SymbolTable);
216 return symbol_iterator(SymbolRef(ret, this));
219 ObjectFile::symbol_iterator COFFObjectFile::end_symbols() const {
220 // The symbol table ends where the string table begins.
222 memset(&ret, 0, sizeof(DataRefImpl));
223 ret.p = reinterpret_cast<intptr_t>(StringTable);
224 return symbol_iterator(SymbolRef(ret, this));
227 ObjectFile::section_iterator COFFObjectFile::begin_sections() const {
229 memset(&ret, 0, sizeof(DataRefImpl));
230 ret.p = reinterpret_cast<intptr_t>(SectionTable);
231 return section_iterator(SectionRef(ret, this));
234 ObjectFile::section_iterator COFFObjectFile::end_sections() const {
236 memset(&ret, 0, sizeof(DataRefImpl));
237 ret.p = reinterpret_cast<intptr_t>(SectionTable + Header->NumberOfSections);
238 return section_iterator(SectionRef(ret, this));
241 uint8_t COFFObjectFile::getBytesInAddress() const {
242 return getArch() == Triple::x86_64 ? 8 : 4;
245 StringRef COFFObjectFile::getFileFormatName() const {
246 switch(Header->Machine) {
247 case COFF::IMAGE_FILE_MACHINE_I386:
249 case COFF::IMAGE_FILE_MACHINE_AMD64:
250 return "COFF-x86-64";
252 return "COFF-<unknown arch>";
256 unsigned COFFObjectFile::getArch() const {
257 switch(Header->Machine) {
258 case COFF::IMAGE_FILE_MACHINE_I386:
260 case COFF::IMAGE_FILE_MACHINE_AMD64:
261 return Triple::x86_64;
263 return Triple::UnknownArch;
267 const coff_section *COFFObjectFile::getSection(std::size_t index) const {
268 if (index > 0 && index <= Header->NumberOfSections)
269 return SectionTable + (index - 1);
273 const char *COFFObjectFile::getString(std::size_t offset) const {
274 const ulittle32_t *StringTableSize =
275 reinterpret_cast<const ulittle32_t *>(StringTable);
276 if (offset < *StringTableSize)
277 return StringTable + offset;
283 ObjectFile *ObjectFile::createCOFFObjectFile(MemoryBuffer *Object) {
285 return new COFFObjectFile(Object, ec);
288 } // end namespace llvm