blob: 3663cd9c9a65d0f54ad8aef76ea9c4c08436b652 [file] [log] [blame]
Michael J. Spencer8e90ada2011-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
Michael J. Spencerec29b122011-06-25 17:54:50 +000014#include "llvm/Object/COFF.h"
Michael J. Spencer9da9e692012-03-19 20:27:37 +000015#include "llvm/ADT/ArrayRef.h"
Michael J. Spencere5fd0042011-10-07 19:25:32 +000016#include "llvm/ADT/SmallString.h"
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000017#include "llvm/ADT/StringSwitch.h"
18#include "llvm/ADT/Triple.h"
Rui Ueyamac2bed422013-09-27 21:04:00 +000019#include "llvm/Support/Debug.h"
20#include "llvm/Support/raw_ostream.h"
Will Dietz981af002013-10-12 00:55:57 +000021#include <cctype>
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000022
23using namespace llvm;
24using namespace object;
25
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000026using support::ulittle8_t;
27using support::ulittle16_t;
28using support::ulittle32_t;
29using support::little16_t;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000030
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000031// Returns false if size is greater than the buffer size. And sets ec.
Rui Ueyama686738e2014-01-16 20:30:36 +000032static bool checkSize(const MemoryBuffer *M, error_code &EC, uint64_t Size) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +000033 if (M->getBufferSize() < Size) {
34 EC = object_error::unexpected_eof;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000035 return false;
36 }
37 return true;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000038}
39
Rui Ueyamaed64342b2013-07-19 23:23:29 +000040// Sets Obj unless any bytes in [addr, addr + size) fall outsize of m.
41// Returns unexpected_eof if error.
42template<typename T>
Rui Ueyama686738e2014-01-16 20:30:36 +000043static error_code getObject(const T *&Obj, const MemoryBuffer *M,
44 const uint8_t *Ptr, const size_t Size = sizeof(T)) {
Rui Ueyamaed64342b2013-07-19 23:23:29 +000045 uintptr_t Addr = uintptr_t(Ptr);
46 if (Addr + Size < Addr ||
47 Addr + Size < Size ||
48 Addr + Size > uintptr_t(M->getBufferEnd())) {
49 return object_error::unexpected_eof;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000050 }
Rui Ueyamaed64342b2013-07-19 23:23:29 +000051 Obj = reinterpret_cast<const T *>(Addr);
52 return object_error::success;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000053}
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000054
Rui Ueyama8ff24d22014-01-16 20:11:48 +000055const coff_symbol *COFFObjectFile::toSymb(DataRefImpl Ref) const {
56 const coff_symbol *Addr = reinterpret_cast<const coff_symbol*>(Ref.p);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000057
58# ifndef NDEBUG
59 // Verify that the symbol points to a valid entry in the symbol table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +000060 uintptr_t Offset = uintptr_t(Addr) - uintptr_t(base());
61 if (Offset < COFFHeader->PointerToSymbolTable
62 || Offset >= COFFHeader->PointerToSymbolTable
Rui Ueyama82ebd8e2013-06-12 19:10:33 +000063 + (COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000064 report_fatal_error("Symbol was outside of symbol table.");
65
Rui Ueyama8ff24d22014-01-16 20:11:48 +000066 assert((Offset - COFFHeader->PointerToSymbolTable) % sizeof(coff_symbol)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000067 == 0 && "Symbol did not point to the beginning of a symbol");
68# endif
69
Rui Ueyama8ff24d22014-01-16 20:11:48 +000070 return Addr;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000071}
72
Rui Ueyama8ff24d22014-01-16 20:11:48 +000073const coff_section *COFFObjectFile::toSec(DataRefImpl Ref) const {
74 const coff_section *Addr = reinterpret_cast<const coff_section*>(Ref.p);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000075
76# ifndef NDEBUG
77 // Verify that the section points to a valid entry in the section table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +000078 if (Addr < SectionTable
79 || Addr >= (SectionTable + COFFHeader->NumberOfSections))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000080 report_fatal_error("Section was outside of section table.");
81
Rui Ueyama8ff24d22014-01-16 20:11:48 +000082 uintptr_t Offset = uintptr_t(Addr) - uintptr_t(SectionTable);
83 assert(Offset % sizeof(coff_section) == 0 &&
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000084 "Section did not point to the beginning of a section");
85# endif
86
Rui Ueyama8ff24d22014-01-16 20:11:48 +000087 return Addr;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000088}
89
Rui Ueyama8ff24d22014-01-16 20:11:48 +000090error_code COFFObjectFile::getSymbolNext(DataRefImpl Ref,
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000091 SymbolRef &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +000092 const coff_symbol *Symb = toSymb(Ref);
93 Symb += 1 + Symb->NumberOfAuxSymbols;
94 Ref.p = reinterpret_cast<uintptr_t>(Symb);
95 Result = SymbolRef(Ref, this);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000096 return object_error::success;
97}
98
Rui Ueyama8ff24d22014-01-16 20:11:48 +000099error_code COFFObjectFile::getSymbolName(DataRefImpl Ref,
100 StringRef &Result) const {
101 const coff_symbol *Symb = toSymb(Ref);
102 return getSymbolName(Symb, Result);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000103}
104
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000105error_code COFFObjectFile::getSymbolFileOffset(DataRefImpl Ref,
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000106 uint64_t &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000107 const coff_symbol *Symb = toSymb(Ref);
Michael J. Spencer5ebaed22011-07-05 14:48:59 +0000108 const coff_section *Section = NULL;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000109 if (error_code EC = getSection(Symb->SectionNumber, Section))
110 return EC;
Rafael Espindolae62ab112013-11-02 18:07:48 +0000111
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000112 if (Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000113 Result = UnknownAddressOrSize;
114 else if (Section)
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000115 Result = Section->PointerToRawData + Symb->Value;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000116 else
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000117 Result = Symb->Value;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000118 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000119}
120
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000121error_code COFFObjectFile::getSymbolAddress(DataRefImpl Ref,
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000122 uint64_t &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000123 const coff_symbol *Symb = toSymb(Ref);
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000124 const coff_section *Section = NULL;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000125 if (error_code EC = getSection(Symb->SectionNumber, Section))
126 return EC;
Rafael Espindolae62ab112013-11-02 18:07:48 +0000127
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000128 if (Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000129 Result = UnknownAddressOrSize;
130 else if (Section)
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000131 Result = Section->VirtualAddress + Symb->Value;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000132 else
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000133 Result = Symb->Value;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000134 return object_error::success;
135}
136
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000137error_code COFFObjectFile::getSymbolType(DataRefImpl Ref,
Michael J. Spencerd3946672011-10-17 20:19:29 +0000138 SymbolRef::Type &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000139 const coff_symbol *Symb = toSymb(Ref);
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000140 Result = SymbolRef::ST_Other;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000141 if (Symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
142 Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) {
David Meyer7e4b9762012-02-29 02:11:55 +0000143 Result = SymbolRef::ST_Unknown;
Rui Ueyama5efa6652014-01-16 20:22:55 +0000144 } else if (Symb->getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION) {
145 Result = SymbolRef::ST_Function;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000146 } else {
Rui Ueyama5efa6652014-01-16 20:22:55 +0000147 uint32_t Characteristics = 0;
148 if (Symb->SectionNumber > 0) {
149 const coff_section *Section = NULL;
150 if (error_code EC = getSection(Symb->SectionNumber, Section))
151 return EC;
152 Characteristics = Section->Characteristics;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000153 }
Rui Ueyama5efa6652014-01-16 20:22:55 +0000154 if (Characteristics & COFF::IMAGE_SCN_MEM_READ &&
155 ~Characteristics & COFF::IMAGE_SCN_MEM_WRITE) // Read only.
156 Result = SymbolRef::ST_Data;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000157 }
158 return object_error::success;
159}
160
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000161error_code COFFObjectFile::getSymbolFlags(DataRefImpl Ref,
David Meyer1df4b842012-02-28 23:47:53 +0000162 uint32_t &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000163 const coff_symbol *Symb = toSymb(Ref);
David Meyer1df4b842012-02-28 23:47:53 +0000164 Result = SymbolRef::SF_None;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000165
David Meyer7e4b9762012-02-29 02:11:55 +0000166 // TODO: Correctly set SF_FormatSpecific, SF_ThreadLocal, SF_Common
167
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000168 if (Symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
169 Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
David Meyer7e4b9762012-02-29 02:11:55 +0000170 Result |= SymbolRef::SF_Undefined;
David Meyer1df4b842012-02-28 23:47:53 +0000171
172 // TODO: This are certainly too restrictive.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000173 if (Symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL)
David Meyer1df4b842012-02-28 23:47:53 +0000174 Result |= SymbolRef::SF_Global;
175
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000176 if (Symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL)
David Meyer1df4b842012-02-28 23:47:53 +0000177 Result |= SymbolRef::SF_Weak;
178
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000179 if (Symb->SectionNumber == COFF::IMAGE_SYM_ABSOLUTE)
David Meyer1df4b842012-02-28 23:47:53 +0000180 Result |= SymbolRef::SF_Absolute;
181
Michael J. Spencer01759752011-10-17 23:54:22 +0000182 return object_error::success;
183}
184
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000185error_code COFFObjectFile::getSymbolSize(DataRefImpl Ref,
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000186 uint64_t &Result) const {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000187 // 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.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000190 const coff_symbol *Symb = toSymb(Ref);
Michael J. Spencer5ebaed22011-07-05 14:48:59 +0000191 const coff_section *Section = NULL;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000192 if (error_code EC = getSection(Symb->SectionNumber, Section))
193 return EC;
Rafael Espindolae62ab112013-11-02 18:07:48 +0000194
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000195 if (Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000196 Result = UnknownAddressOrSize;
197 else if (Section)
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000198 Result = Section->SizeOfRawData - Symb->Value;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000199 else
200 Result = 0;
201 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000202}
203
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000204error_code COFFObjectFile::getSymbolSection(DataRefImpl Ref,
Michael J. Spencer3217315392011-10-17 23:54:46 +0000205 section_iterator &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000206 const coff_symbol *Symb = toSymb(Ref);
207 if (Symb->SectionNumber <= COFF::IMAGE_SYM_UNDEFINED)
Michael J. Spencer3217315392011-10-17 23:54:46 +0000208 Result = end_sections();
209 else {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000210 const coff_section *Sec = 0;
211 if (error_code EC = getSection(Symb->SectionNumber, Sec)) return EC;
212 DataRefImpl Ref;
213 Ref.p = reinterpret_cast<uintptr_t>(Sec);
214 Result = section_iterator(SectionRef(Ref, this));
Michael J. Spencer3217315392011-10-17 23:54:46 +0000215 }
216 return object_error::success;
217}
218
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000219error_code COFFObjectFile::getSymbolValue(DataRefImpl Ref,
Tim Northover4f223bf72012-10-29 10:47:00 +0000220 uint64_t &Val) const {
221 report_fatal_error("getSymbolValue unimplemented in COFFObjectFile");
222}
223
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000224error_code COFFObjectFile::getSectionNext(DataRefImpl Ref,
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000225 SectionRef &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000226 const coff_section *Sec = toSec(Ref);
227 Sec += 1;
228 Ref.p = reinterpret_cast<uintptr_t>(Sec);
229 Result = SectionRef(Ref, this);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000230 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000231}
232
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000233error_code COFFObjectFile::getSectionName(DataRefImpl Ref,
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000234 StringRef &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000235 const coff_section *Sec = toSec(Ref);
236 return getSectionName(Sec, Result);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000237}
238
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000239error_code COFFObjectFile::getSectionAddress(DataRefImpl Ref,
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000240 uint64_t &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000241 const coff_section *Sec = toSec(Ref);
242 Result = Sec->VirtualAddress;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000243 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000244}
245
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000246error_code COFFObjectFile::getSectionSize(DataRefImpl Ref,
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000247 uint64_t &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000248 const coff_section *Sec = toSec(Ref);
249 Result = Sec->SizeOfRawData;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000250 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000251}
252
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000253error_code COFFObjectFile::getSectionContents(DataRefImpl Ref,
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000254 StringRef &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000255 const coff_section *Sec = toSec(Ref);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000256 ArrayRef<uint8_t> Res;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000257 error_code EC = getSectionContents(Sec, Res);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000258 Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size());
259 return EC;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000260}
261
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000262error_code COFFObjectFile::getSectionAlignment(DataRefImpl Ref,
Michael J. Spencer79894602011-10-10 21:55:43 +0000263 uint64_t &Res) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000264 const coff_section *Sec = toSec(Ref);
265 if (!Sec)
Michael J. Spencer79894602011-10-10 21:55:43 +0000266 return object_error::parse_failed;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000267 Res = uint64_t(1) << (((Sec->Characteristics & 0x00F00000) >> 20) - 1);
Michael J. Spencer79894602011-10-10 21:55:43 +0000268 return object_error::success;
269}
270
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000271error_code COFFObjectFile::isSectionText(DataRefImpl Ref,
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000272 bool &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000273 const coff_section *Sec = toSec(Ref);
274 Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000275 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000276}
277
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000278error_code COFFObjectFile::isSectionData(DataRefImpl Ref,
Michael J. Spencer800619f2011-09-28 20:57:30 +0000279 bool &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000280 const coff_section *Sec = toSec(Ref);
281 Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000282 return object_error::success;
283}
284
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000285error_code COFFObjectFile::isSectionBSS(DataRefImpl Ref,
Michael J. Spencer800619f2011-09-28 20:57:30 +0000286 bool &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000287 const coff_section *Sec = toSec(Ref);
288 Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000289 return object_error::success;
290}
291
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000292error_code COFFObjectFile::isSectionRequiredForExecution(DataRefImpl Ref,
Preston Gurd2138ef62012-04-12 20:13:57 +0000293 bool &Result) const {
294 // FIXME: Unimplemented
295 Result = true;
296 return object_error::success;
297}
298
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000299error_code COFFObjectFile::isSectionVirtual(DataRefImpl Ref,
Preston Gurd2138ef62012-04-12 20:13:57 +0000300 bool &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000301 const coff_section *Sec = toSec(Ref);
302 Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
Preston Gurd2138ef62012-04-12 20:13:57 +0000303 return object_error::success;
304}
305
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000306error_code COFFObjectFile::isSectionZeroInit(DataRefImpl Ref,
Preston Gurd2138ef62012-04-12 20:13:57 +0000307 bool &Result) const {
Andrew Kaylorb96a3202012-10-10 01:45:52 +0000308 // FIXME: Unimplemented.
Preston Gurd2138ef62012-04-12 20:13:57 +0000309 Result = false;
310 return object_error::success;
311}
312
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000313error_code COFFObjectFile::isSectionReadOnlyData(DataRefImpl Ref,
Andrew Kaylor3f31fa02012-10-10 01:41:33 +0000314 bool &Result) const {
315 // FIXME: Unimplemented.
316 Result = false;
317 return object_error::success;
318}
319
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000320error_code COFFObjectFile::sectionContainsSymbol(DataRefImpl SecRef,
321 DataRefImpl SymbRef,
Benjamin Kramerf6f3e812011-07-15 18:39:21 +0000322 bool &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000323 const coff_section *Sec = toSec(SecRef);
324 const coff_symbol *Symb = toSymb(SymbRef);
325 const coff_section *SymbSec = 0;
326 if (error_code EC = getSection(Symb->SectionNumber, SymbSec)) return EC;
327 if (SymbSec == Sec)
Michael J. Spencer9a288512011-10-13 20:36:54 +0000328 Result = true;
329 else
330 Result = false;
Benjamin Kramerf6f3e812011-07-15 18:39:21 +0000331 return object_error::success;
332}
333
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000334relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const {
335 const coff_section *Sec = toSec(Ref);
336 DataRefImpl Ret;
337 if (Sec->NumberOfRelocations == 0)
338 Ret.p = 0;
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000339 else
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000340 Ret.p = reinterpret_cast<uintptr_t>(base() + Sec->PointerToRelocations);
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000341
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000342 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000343}
344
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000345relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const {
346 const coff_section *Sec = toSec(Ref);
347 DataRefImpl Ret;
348 if (Sec->NumberOfRelocations == 0)
349 Ret.p = 0;
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000350 else
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000351 Ret.p = reinterpret_cast<uintptr_t>(
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000352 reinterpret_cast<const coff_relocation*>(
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000353 base() + Sec->PointerToRelocations)
354 + Sec->NumberOfRelocations);
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000355
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000356 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000357}
358
Rui Ueyamac2bed422013-09-27 21:04:00 +0000359// Initialize the pointer to the symbol table.
360error_code COFFObjectFile::initSymbolTablePtr() {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000361 if (error_code EC = getObject(
Rui Ueyamac2bed422013-09-27 21:04:00 +0000362 SymbolTable, Data, base() + COFFHeader->PointerToSymbolTable,
363 COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000364 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000365
366 // Find string table. The first four byte of the string table contains the
367 // total size of the string table, including the size field itself. If the
368 // string table is empty, the value of the first four byte would be 4.
369 const uint8_t *StringTableAddr =
370 base() + COFFHeader->PointerToSymbolTable +
371 COFFHeader->NumberOfSymbols * sizeof(coff_symbol);
372 const ulittle32_t *StringTableSizePtr;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000373 if (error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr))
374 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000375 StringTableSize = *StringTableSizePtr;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000376 if (error_code EC =
Rui Ueyamac2bed422013-09-27 21:04:00 +0000377 getObject(StringTable, Data, StringTableAddr, StringTableSize))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000378 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000379
380 // Check that the string table is null terminated if has any in it.
381 if (StringTableSize < 4 ||
382 (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0))
383 return object_error::parse_failed;
384 return object_error::success;
385}
386
387// Returns the file offset for the given RVA.
388error_code COFFObjectFile::getRvaPtr(uint32_t Rva, uintptr_t &Res) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000389 error_code EC;
390 for (section_iterator I = begin_sections(), E = end_sections(); I != E;
391 I.increment(EC)) {
392 if (EC)
393 return EC;
394 const coff_section *Section = getCOFFSection(I);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000395 uint32_t SectionStart = Section->VirtualAddress;
396 uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize;
397 if (SectionStart <= Rva && Rva < SectionEnd) {
398 uint32_t Offset = Rva - SectionStart;
399 Res = uintptr_t(base()) + Section->PointerToRawData + Offset;
400 return object_error::success;
401 }
402 }
403 return object_error::parse_failed;
404}
405
406// Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
407// table entry.
408error_code COFFObjectFile::
409getHintName(uint32_t Rva, uint16_t &Hint, StringRef &Name) const {
410 uintptr_t IntPtr = 0;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000411 if (error_code EC = getRvaPtr(Rva, IntPtr))
412 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000413 const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr);
414 Hint = *reinterpret_cast<const ulittle16_t *>(Ptr);
415 Name = StringRef(reinterpret_cast<const char *>(Ptr + 2));
416 return object_error::success;
417}
418
419// Find the import table.
420error_code COFFObjectFile::initImportTablePtr() {
421 // First, we get the RVA of the import table. If the file lacks a pointer to
422 // the import table, do nothing.
423 const data_directory *DataEntry;
424 if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
425 return object_error::success;
426
427 // Do nothing if the pointer to import table is NULL.
428 if (DataEntry->RelativeVirtualAddress == 0)
429 return object_error::success;
430
431 uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
432 NumberOfImportDirectory = DataEntry->Size /
433 sizeof(import_directory_table_entry);
434
435 // Find the section that contains the RVA. This is needed because the RVA is
436 // the import table's memory address which is different from its file offset.
437 uintptr_t IntPtr = 0;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000438 if (error_code EC = getRvaPtr(ImportTableRva, IntPtr))
439 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000440 ImportDirectory = reinterpret_cast<
441 const import_directory_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000442 return object_error::success;
443}
Rui Ueyamac2bed422013-09-27 21:04:00 +0000444
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000445// Find the export table.
446error_code COFFObjectFile::initExportTablePtr() {
447 // First, we get the RVA of the export table. If the file lacks a pointer to
448 // the export table, do nothing.
449 const data_directory *DataEntry;
450 if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
451 return object_error::success;
452
453 // Do nothing if the pointer to export table is NULL.
454 if (DataEntry->RelativeVirtualAddress == 0)
455 return object_error::success;
456
457 uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress;
458 uintptr_t IntPtr = 0;
459 if (error_code EC = getRvaPtr(ExportTableRva, IntPtr))
460 return EC;
461 ExportDirectory = reinterpret_cast<const export_directory_table_entry *>(IntPtr);
462 return object_error::success;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000463}
464
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000465COFFObjectFile::COFFObjectFile(MemoryBuffer *Object, error_code &EC)
466 : ObjectFile(Binary::ID_COFF, Object), COFFHeader(0), PE32Header(0),
467 DataDirectory(0), SectionTable(0), SymbolTable(0), StringTable(0),
468 StringTableSize(0), ImportDirectory(0), NumberOfImportDirectory(0),
469 ExportDirectory(0) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000470 // Check that we at least have enough room for a header.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000471 if (!checkSize(Data, EC, sizeof(coff_file_header))) return;
Eric Christopheree066fc2011-04-03 22:53:19 +0000472
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000473 // The current location in the file where we are looking at.
474 uint64_t CurPtr = 0;
475
476 // PE header is optional and is present only in executables. If it exists,
477 // it is placed right after COFF header.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000478 bool HasPEHeader = false;
Eric Christopheree066fc2011-04-03 22:53:19 +0000479
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000480 // Check if this is a PE/COFF file.
Michael J. Spencerec29b122011-06-25 17:54:50 +0000481 if (base()[0] == 0x4d && base()[1] == 0x5a) {
Eric Christopheree066fc2011-04-03 22:53:19 +0000482 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
483 // PE signature to find 'normal' COFF header.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000484 if (!checkSize(Data, EC, 0x3c + 8)) return;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000485 CurPtr = *reinterpret_cast<const ulittle16_t *>(base() + 0x3c);
486 // Check the PE magic bytes. ("PE\0\0")
487 if (std::memcmp(base() + CurPtr, "PE\0\0", 4) != 0) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000488 EC = object_error::parse_failed;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000489 return;
490 }
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000491 CurPtr += 4; // Skip the PE magic bytes.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000492 HasPEHeader = true;
Eric Christopheree066fc2011-04-03 22:53:19 +0000493 }
494
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000495 if ((EC = getObject(COFFHeader, Data, base() + CurPtr)))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000496 return;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000497 CurPtr += sizeof(coff_file_header);
498
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000499 if (HasPEHeader) {
500 if ((EC = getObject(PE32Header, Data, base() + CurPtr)))
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000501 return;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000502 if (PE32Header->Magic != 0x10b) {
503 // We only support PE32. If this is PE32 (not PE32+), the magic byte
504 // should be 0x10b. If this is not PE32, continue as if there's no PE
505 // header in this file.
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000506 PE32Header = 0;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000507 } else if (PE32Header->NumberOfRvaAndSize > 0) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000508 const uint8_t *Addr = base() + CurPtr + sizeof(pe32_header);
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000509 uint64_t size = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000510 if ((EC = getObject(DataDirectory, Data, Addr, size)))
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000511 return;
512 }
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000513 CurPtr += COFFHeader->SizeOfOptionalHeader;
514 }
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000515
Rui Ueyama15ba1e22013-11-15 20:23:25 +0000516 if (!COFFHeader->isImportLibrary())
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000517 if ((EC = getObject(SectionTable, Data, base() + CurPtr,
Rui Ueyama15ba1e22013-11-15 20:23:25 +0000518 COFFHeader->NumberOfSections * sizeof(coff_section))))
519 return;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000520
Rui Ueyamac2bed422013-09-27 21:04:00 +0000521 // Initialize the pointer to the symbol table.
522 if (COFFHeader->PointerToSymbolTable != 0)
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000523 if ((EC = initSymbolTablePtr()))
Michael J. Spencerd5930ca2011-11-08 23:34:07 +0000524 return;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000525
Rui Ueyamac2bed422013-09-27 21:04:00 +0000526 // Initialize the pointer to the beginning of the import table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000527 if ((EC = initImportTablePtr()))
Rui Ueyamac2bed422013-09-27 21:04:00 +0000528 return;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000529
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000530 // Initialize the pointer to the export table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000531 if ((EC = initExportTablePtr()))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000532 return;
533
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000534 EC = object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000535}
536
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000537symbol_iterator COFFObjectFile::begin_symbols() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000538 DataRefImpl Ret;
539 Ret.p = reinterpret_cast<uintptr_t>(SymbolTable);
540 return symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000541}
542
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000543symbol_iterator COFFObjectFile::end_symbols() const {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000544 // The symbol table ends where the string table begins.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000545 DataRefImpl Ret;
546 Ret.p = reinterpret_cast<uintptr_t>(StringTable);
547 return symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000548}
549
Michael J. Spencer8c4729f2012-02-28 00:40:37 +0000550symbol_iterator COFFObjectFile::begin_dynamic_symbols() const {
551 // TODO: implement
552 report_fatal_error("Dynamic symbols unimplemented in COFFObjectFile");
553}
554
555symbol_iterator COFFObjectFile::end_dynamic_symbols() const {
556 // TODO: implement
557 report_fatal_error("Dynamic symbols unimplemented in COFFObjectFile");
558}
559
David Meyer2fc34c52012-03-01 01:36:50 +0000560library_iterator COFFObjectFile::begin_libraries_needed() const {
561 // TODO: implement
562 report_fatal_error("Libraries needed unimplemented in COFFObjectFile");
563}
564
565library_iterator COFFObjectFile::end_libraries_needed() const {
566 // TODO: implement
567 report_fatal_error("Libraries needed unimplemented in COFFObjectFile");
568}
569
David Meyerc429b802012-03-01 22:19:54 +0000570StringRef COFFObjectFile::getLoadName() const {
571 // COFF does not have this field.
572 return "";
573}
574
Rui Ueyamabc654b12013-09-27 21:47:05 +0000575import_directory_iterator COFFObjectFile::import_directory_begin() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000576 return import_directory_iterator(
577 ImportDirectoryEntryRef(ImportDirectory, 0, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000578}
579
Rui Ueyamabc654b12013-09-27 21:47:05 +0000580import_directory_iterator COFFObjectFile::import_directory_end() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000581 return import_directory_iterator(
582 ImportDirectoryEntryRef(ImportDirectory, NumberOfImportDirectory, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000583}
David Meyerc429b802012-03-01 22:19:54 +0000584
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000585export_directory_iterator COFFObjectFile::export_directory_begin() const {
586 return export_directory_iterator(
587 ExportDirectoryEntryRef(ExportDirectory, 0, this));
588}
589
590export_directory_iterator COFFObjectFile::export_directory_end() const {
591 if (ExportDirectory == 0)
592 return export_directory_iterator(ExportDirectoryEntryRef(0, 0, this));
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000593 ExportDirectoryEntryRef Ref(ExportDirectory,
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000594 ExportDirectory->AddressTableEntries, this);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000595 return export_directory_iterator(Ref);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000596}
597
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000598section_iterator COFFObjectFile::begin_sections() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000599 DataRefImpl Ret;
600 Ret.p = reinterpret_cast<uintptr_t>(SectionTable);
601 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000602}
603
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000604section_iterator COFFObjectFile::end_sections() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000605 DataRefImpl Ret;
606 int NumSections = COFFHeader->isImportLibrary()
Rui Ueyama15ba1e22013-11-15 20:23:25 +0000607 ? 0 : COFFHeader->NumberOfSections;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000608 Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections);
609 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000610}
611
612uint8_t COFFObjectFile::getBytesInAddress() const {
Michael J. Spencer0324b672011-01-21 02:27:02 +0000613 return getArch() == Triple::x86_64 ? 8 : 4;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000614}
615
616StringRef COFFObjectFile::getFileFormatName() const {
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000617 switch(COFFHeader->Machine) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000618 case COFF::IMAGE_FILE_MACHINE_I386:
619 return "COFF-i386";
620 case COFF::IMAGE_FILE_MACHINE_AMD64:
621 return "COFF-x86-64";
622 default:
623 return "COFF-<unknown arch>";
624 }
625}
626
627unsigned COFFObjectFile::getArch() const {
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000628 switch(COFFHeader->Machine) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000629 case COFF::IMAGE_FILE_MACHINE_I386:
630 return Triple::x86;
631 case COFF::IMAGE_FILE_MACHINE_AMD64:
632 return Triple::x86_64;
633 default:
634 return Triple::UnknownArch;
635 }
636}
637
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000638// This method is kept here because lld uses this. As soon as we make
639// lld to use getCOFFHeader, this method will be removed.
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000640error_code COFFObjectFile::getHeader(const coff_file_header *&Res) const {
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000641 return getCOFFHeader(Res);
642}
643
644error_code COFFObjectFile::getCOFFHeader(const coff_file_header *&Res) const {
645 Res = COFFHeader;
646 return object_error::success;
647}
648
649error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
650 Res = PE32Header;
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000651 return object_error::success;
652}
653
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000654error_code COFFObjectFile::getDataDirectory(uint32_t Index,
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000655 const data_directory *&Res) const {
656 // Error if if there's no data directory or the index is out of range.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000657 if (!DataDirectory || Index > PE32Header->NumberOfRvaAndSize)
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000658 return object_error::parse_failed;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000659 Res = &DataDirectory[Index];
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000660 return object_error::success;
661}
662
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000663error_code COFFObjectFile::getSection(int32_t Index,
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000664 const coff_section *&Result) const {
665 // Check for special index values.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000666 if (Index == COFF::IMAGE_SYM_UNDEFINED ||
667 Index == COFF::IMAGE_SYM_ABSOLUTE ||
668 Index == COFF::IMAGE_SYM_DEBUG)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000669 Result = NULL;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000670 else if (Index > 0 && Index <= COFFHeader->NumberOfSections)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000671 // We already verified the section table data, so no need to check again.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000672 Result = SectionTable + (Index - 1);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000673 else
674 return object_error::parse_failed;
675 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000676}
677
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000678error_code COFFObjectFile::getString(uint32_t Offset,
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000679 StringRef &Result) const {
680 if (StringTableSize <= 4)
681 // Tried to get a string from an empty string table.
682 return object_error::parse_failed;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000683 if (Offset >= StringTableSize)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000684 return object_error::unexpected_eof;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000685 Result = StringRef(StringTable + Offset);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000686 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000687}
688
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000689error_code COFFObjectFile::getSymbol(uint32_t Index,
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000690 const coff_symbol *&Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000691 if (Index < COFFHeader->NumberOfSymbols)
692 Result = SymbolTable + Index;
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000693 else
694 return object_error::parse_failed;
695 return object_error::success;
696}
697
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000698error_code COFFObjectFile::getSymbolName(const coff_symbol *Symbol,
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000699 StringRef &Res) const {
700 // Check for string table entry. First 4 bytes are 0.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000701 if (Symbol->Name.Offset.Zeroes == 0) {
702 uint32_t Offset = Symbol->Name.Offset.Offset;
703 if (error_code EC = getString(Offset, Res))
704 return EC;
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000705 return object_error::success;
706 }
707
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000708 if (Symbol->Name.ShortName[7] == 0)
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000709 // Null terminated, let ::strlen figure out the length.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000710 Res = StringRef(Symbol->Name.ShortName);
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000711 else
712 // Not null terminated, use all 8 bytes.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000713 Res = StringRef(Symbol->Name.ShortName, 8);
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000714 return object_error::success;
715}
716
Marshall Clow71757ef2012-06-15 01:08:25 +0000717ArrayRef<uint8_t> COFFObjectFile::getSymbolAuxData(
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000718 const coff_symbol *Symbol) const {
719 const uint8_t *Aux = NULL;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000720
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000721 if (Symbol->NumberOfAuxSymbols > 0) {
Marshall Clow71757ef2012-06-15 01:08:25 +0000722 // AUX data comes immediately after the symbol in COFF
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000723 Aux = reinterpret_cast<const uint8_t *>(Symbol + 1);
Marshall Clow71757ef2012-06-15 01:08:25 +0000724# ifndef NDEBUG
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000725 // Verify that the Aux symbol points to a valid entry in the symbol table.
726 uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base());
727 if (Offset < COFFHeader->PointerToSymbolTable
728 || Offset >= COFFHeader->PointerToSymbolTable
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000729 + (COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
Marshall Clow71757ef2012-06-15 01:08:25 +0000730 report_fatal_error("Aux Symbol data was outside of symbol table.");
731
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000732 assert((Offset - COFFHeader->PointerToSymbolTable) % sizeof(coff_symbol)
Marshall Clow71757ef2012-06-15 01:08:25 +0000733 == 0 && "Aux Symbol data did not point to the beginning of a symbol");
Marshall Clow71757ef2012-06-15 01:08:25 +0000734# endif
Marshall Clowbfb85e62012-06-15 01:15:47 +0000735 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000736 return ArrayRef<uint8_t>(Aux, Symbol->NumberOfAuxSymbols * sizeof(coff_symbol));
Marshall Clow71757ef2012-06-15 01:08:25 +0000737}
738
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000739error_code COFFObjectFile::getSectionName(const coff_section *Sec,
740 StringRef &Res) const {
741 StringRef Name;
742 if (Sec->Name[7] == 0)
743 // Null terminated, let ::strlen figure out the length.
744 Name = Sec->Name;
745 else
746 // Not null terminated, use all 8 bytes.
747 Name = StringRef(Sec->Name, 8);
748
749 // Check for string table entry. First byte is '/'.
750 if (Name[0] == '/') {
751 uint32_t Offset;
752 if (Name.substr(1).getAsInteger(10, Offset))
753 return object_error::parse_failed;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000754 if (error_code EC = getString(Offset, Name))
755 return EC;
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000756 }
757
758 Res = Name;
759 return object_error::success;
760}
761
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000762error_code COFFObjectFile::getSectionContents(const coff_section *Sec,
763 ArrayRef<uint8_t> &Res) const {
764 // The only thing that we need to verify is that the contents is contained
765 // within the file bounds. We don't need to make sure it doesn't cover other
766 // data, as there's nothing that says that is not allowed.
767 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
768 uintptr_t ConEnd = ConStart + Sec->SizeOfRawData;
769 if (ConEnd > uintptr_t(Data->getBufferEnd()))
770 return object_error::parse_failed;
771 Res = ArrayRef<uint8_t>(reinterpret_cast<const unsigned char*>(ConStart),
772 Sec->SizeOfRawData);
773 return object_error::success;
774}
775
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000776const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000777 return reinterpret_cast<const coff_relocation*>(Rel.p);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000778}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000779
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000780error_code COFFObjectFile::getRelocationNext(DataRefImpl Rel,
781 RelocationRef &Res) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000782 Rel.p = reinterpret_cast<uintptr_t>(
783 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000784 Res = RelocationRef(Rel, this);
785 return object_error::success;
786}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000787
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000788error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel,
789 uint64_t &Res) const {
Rafael Espindola1e483872013-04-25 12:28:45 +0000790 report_fatal_error("getRelocationAddress not implemented in COFFObjectFile");
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000791}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000792
Danil Malyshevcbe72fc2011-11-29 17:40:10 +0000793error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel,
794 uint64_t &Res) const {
795 Res = toRel(Rel)->VirtualAddress;
796 return object_error::success;
797}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000798
Rafael Espindola806f0062013-06-05 01:33:53 +0000799symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000800 const coff_relocation* R = toRel(Rel);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000801 DataRefImpl Ref;
802 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable + R->SymbolTableIndex);
803 return symbol_iterator(SymbolRef(Ref, this));
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000804}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000805
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000806error_code COFFObjectFile::getRelocationType(DataRefImpl Rel,
Owen Anderson7be76592011-10-26 17:08:49 +0000807 uint64_t &Res) const {
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000808 const coff_relocation* R = toRel(Rel);
809 Res = R->Type;
810 return object_error::success;
811}
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000812
Marshall Clow71757ef2012-06-15 01:08:25 +0000813const coff_section *COFFObjectFile::getCOFFSection(section_iterator &It) const {
814 return toSec(It->getRawDataRefImpl());
815}
816
817const coff_symbol *COFFObjectFile::getCOFFSymbol(symbol_iterator &It) const {
818 return toSymb(It->getRawDataRefImpl());
819}
820
Marshall Clowd3e2a762012-06-18 19:47:16 +0000821const coff_relocation *COFFObjectFile::getCOFFRelocation(
822 relocation_iterator &It) const {
823 return toRel(It->getRawDataRefImpl());
824}
825
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000826#define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(enum) \
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000827 case COFF::enum: Res = #enum; break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000828
829error_code COFFObjectFile::getRelocationTypeName(DataRefImpl Rel,
830 SmallVectorImpl<char> &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000831 const coff_relocation *Reloc = toRel(Rel);
832 StringRef Res;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000833 switch (COFFHeader->Machine) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000834 case COFF::IMAGE_FILE_MACHINE_AMD64:
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000835 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000836 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
837 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
838 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
839 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
840 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
841 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
842 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
843 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
844 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
845 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
846 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
847 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
848 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
849 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
850 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
851 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
852 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
853 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000854 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000855 }
856 break;
857 case COFF::IMAGE_FILE_MACHINE_I386:
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000858 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000859 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
860 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
861 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
862 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
863 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
864 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
865 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
866 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
867 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
868 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
869 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
870 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000871 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000872 }
873 break;
874 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000875 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000876 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000877 Result.append(Res.begin(), Res.end());
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000878 return object_error::success;
879}
880
881#undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
882
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000883error_code COFFObjectFile::getRelocationValueString(DataRefImpl Rel,
884 SmallVectorImpl<char> &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000885 const coff_relocation *Reloc = toRel(Rel);
886 const coff_symbol *Symb = 0;
887 if (error_code EC = getSymbol(Reloc->SymbolTableIndex, Symb)) return EC;
888 DataRefImpl Sym;
889 Sym.p = reinterpret_cast<uintptr_t>(Symb);
890 StringRef SymName;
891 if (error_code EC = getSymbolName(Sym, SymName)) return EC;
892 Result.append(SymName.begin(), SymName.end());
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000893 return object_error::success;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000894}
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000895
David Meyer2fc34c52012-03-01 01:36:50 +0000896error_code COFFObjectFile::getLibraryNext(DataRefImpl LibData,
897 LibraryRef &Result) const {
898 report_fatal_error("getLibraryNext not implemented in COFFObjectFile");
899}
900
901error_code COFFObjectFile::getLibraryPath(DataRefImpl LibData,
902 StringRef &Result) const {
903 report_fatal_error("getLibraryPath not implemented in COFFObjectFile");
904}
905
Rui Ueyamac2bed422013-09-27 21:04:00 +0000906bool ImportDirectoryEntryRef::
907operator==(const ImportDirectoryEntryRef &Other) const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000908 return ImportTable == Other.ImportTable && Index == Other.Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000909}
910
911error_code
912ImportDirectoryEntryRef::getNext(ImportDirectoryEntryRef &Result) const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000913 Result = ImportDirectoryEntryRef(ImportTable, Index + 1, OwningObject);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000914 return object_error::success;
915}
916
917error_code ImportDirectoryEntryRef::
918getImportTableEntry(const import_directory_table_entry *&Result) const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000919 Result = ImportTable;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000920 return object_error::success;
921}
922
923error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000924 uintptr_t IntPtr = 0;
Rui Ueyamaa045b732014-01-16 03:13:19 +0000925 if (error_code EC = OwningObject->getRvaPtr(ImportTable->NameRVA, IntPtr))
926 return EC;
927 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000928 return object_error::success;
929}
930
931error_code ImportDirectoryEntryRef::getImportLookupEntry(
932 const import_lookup_table_entry32 *&Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000933 uintptr_t IntPtr = 0;
Rui Ueyamaa045b732014-01-16 03:13:19 +0000934 if (error_code EC =
935 OwningObject->getRvaPtr(ImportTable->ImportLookupTableRVA, IntPtr))
936 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000937 Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr);
938 return object_error::success;
939}
940
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000941bool ExportDirectoryEntryRef::
942operator==(const ExportDirectoryEntryRef &Other) const {
943 return ExportTable == Other.ExportTable && Index == Other.Index;
944}
945
946error_code
947ExportDirectoryEntryRef::getNext(ExportDirectoryEntryRef &Result) const {
948 Result = ExportDirectoryEntryRef(ExportTable, Index + 1, OwningObject);
949 return object_error::success;
950}
951
952// Returns the export ordinal of the current export symbol.
953error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const {
954 Result = ExportTable->OrdinalBase + Index;
955 return object_error::success;
956}
957
958// Returns the address of the current export symbol.
959error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const {
960 uintptr_t IntPtr = 0;
961 if (error_code EC = OwningObject->getRvaPtr(
962 ExportTable->ExportAddressTableRVA, IntPtr))
963 return EC;
964 const export_address_table_entry *entry = reinterpret_cast<const export_address_table_entry *>(IntPtr);
965 Result = entry[Index].ExportRVA;
966 return object_error::success;
967}
968
969// Returns the name of the current export symbol. If the symbol is exported only
970// by ordinal, the empty string is set as a result.
971error_code ExportDirectoryEntryRef::getName(StringRef &Result) const {
972 uintptr_t IntPtr = 0;
973 if (error_code EC = OwningObject->getRvaPtr(
974 ExportTable->OrdinalTableRVA, IntPtr))
975 return EC;
976 const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr);
977
978 uint32_t NumEntries = ExportTable->NumberOfNamePointers;
979 int Offset = 0;
980 for (const ulittle16_t *I = Start, *E = Start + NumEntries;
981 I < E; ++I, ++Offset) {
982 if (*I != Index)
983 continue;
984 if (error_code EC = OwningObject->getRvaPtr(
985 ExportTable->NamePointerRVA, IntPtr))
986 return EC;
987 const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr);
988 if (error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr))
989 return EC;
990 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
991 return object_error::success;
992 }
993 Result = "";
994 return object_error::success;
995}
996
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000997namespace llvm {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000998ObjectFile *ObjectFile::createCOFFObjectFile(MemoryBuffer *Object) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000999 error_code EC;
1000 return new COFFObjectFile(Object, EC);
Rui Ueyamaa045b732014-01-16 03:13:19 +00001001}
Rui Ueyama686738e2014-01-16 20:30:36 +00001002}