blob: a1bbc56d2be55c1ccf5250e80dafeb1c9507d9dc [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
Rafael Espindola5e812af2014-01-30 02:49:50 +000090void COFFObjectFile::moveSymbolNext(DataRefImpl &Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +000091 const coff_symbol *Symb = toSymb(Ref);
92 Symb += 1 + Symb->NumberOfAuxSymbols;
93 Ref.p = reinterpret_cast<uintptr_t>(Symb);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000094}
95
Rui Ueyama8ff24d22014-01-16 20:11:48 +000096error_code COFFObjectFile::getSymbolName(DataRefImpl Ref,
97 StringRef &Result) const {
98 const coff_symbol *Symb = toSymb(Ref);
99 return getSymbolName(Symb, Result);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000100}
101
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000102error_code COFFObjectFile::getSymbolFileOffset(DataRefImpl Ref,
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000103 uint64_t &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000104 const coff_symbol *Symb = toSymb(Ref);
Michael J. Spencer5ebaed22011-07-05 14:48:59 +0000105 const coff_section *Section = NULL;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000106 if (error_code EC = getSection(Symb->SectionNumber, Section))
107 return EC;
Rafael Espindolae62ab112013-11-02 18:07:48 +0000108
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000109 if (Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000110 Result = UnknownAddressOrSize;
111 else if (Section)
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000112 Result = Section->PointerToRawData + Symb->Value;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000113 else
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000114 Result = Symb->Value;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000115 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000116}
117
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000118error_code COFFObjectFile::getSymbolAddress(DataRefImpl Ref,
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000119 uint64_t &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000120 const coff_symbol *Symb = toSymb(Ref);
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000121 const coff_section *Section = NULL;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000122 if (error_code EC = getSection(Symb->SectionNumber, Section))
123 return EC;
Rafael Espindolae62ab112013-11-02 18:07:48 +0000124
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000125 if (Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000126 Result = UnknownAddressOrSize;
127 else if (Section)
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000128 Result = Section->VirtualAddress + Symb->Value;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000129 else
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000130 Result = Symb->Value;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000131 return object_error::success;
132}
133
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000134error_code COFFObjectFile::getSymbolType(DataRefImpl Ref,
Michael J. Spencerd3946672011-10-17 20:19:29 +0000135 SymbolRef::Type &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000136 const coff_symbol *Symb = toSymb(Ref);
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000137 Result = SymbolRef::ST_Other;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000138 if (Symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
139 Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) {
David Meyer7e4b9762012-02-29 02:11:55 +0000140 Result = SymbolRef::ST_Unknown;
Rui Ueyama5efa6652014-01-16 20:22:55 +0000141 } else if (Symb->getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION) {
142 Result = SymbolRef::ST_Function;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000143 } else {
Rui Ueyama5efa6652014-01-16 20:22:55 +0000144 uint32_t Characteristics = 0;
145 if (Symb->SectionNumber > 0) {
146 const coff_section *Section = NULL;
147 if (error_code EC = getSection(Symb->SectionNumber, Section))
148 return EC;
149 Characteristics = Section->Characteristics;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000150 }
Rui Ueyama5efa6652014-01-16 20:22:55 +0000151 if (Characteristics & COFF::IMAGE_SCN_MEM_READ &&
152 ~Characteristics & COFF::IMAGE_SCN_MEM_WRITE) // Read only.
153 Result = SymbolRef::ST_Data;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000154 }
155 return object_error::success;
156}
157
Rafael Espindola20122a42014-01-31 20:57:12 +0000158uint32_t COFFObjectFile::getSymbolFlags(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000159 const coff_symbol *Symb = toSymb(Ref);
Rafael Espindola20122a42014-01-31 20:57:12 +0000160 uint32_t Result = SymbolRef::SF_None;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000161
Rafael Espindola975e1152014-02-04 22:50:47 +0000162 // TODO: Correctly set SF_FormatSpecific, SF_Common
David Meyer7e4b9762012-02-29 02:11:55 +0000163
Rafael Espindola22fe9c12014-02-05 05:19:19 +0000164 if (Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) {
165 if (Symb->Value == 0)
166 Result |= SymbolRef::SF_Undefined;
167 else
168 Result |= SymbolRef::SF_Common;
169 }
170
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
Rafael Espindola20122a42014-01-31 20:57:12 +0000182 return Result;
Michael J. Spencer01759752011-10-17 23:54:22 +0000183}
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)
Rafael Espindolab5155a52014-02-10 20:24:04 +0000208 Result = section_end();
Michael J. Spencer3217315392011-10-17 23:54:46 +0000209 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
Rafael Espindola5e812af2014-01-30 02:49:50 +0000224void COFFObjectFile::moveSectionNext(DataRefImpl &Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000225 const coff_section *Sec = toSec(Ref);
226 Sec += 1;
227 Ref.p = reinterpret_cast<uintptr_t>(Sec);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000228}
229
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000230error_code COFFObjectFile::getSectionName(DataRefImpl Ref,
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000231 StringRef &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000232 const coff_section *Sec = toSec(Ref);
233 return getSectionName(Sec, Result);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000234}
235
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000236error_code COFFObjectFile::getSectionAddress(DataRefImpl Ref,
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000237 uint64_t &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000238 const coff_section *Sec = toSec(Ref);
239 Result = Sec->VirtualAddress;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000240 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000241}
242
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000243error_code COFFObjectFile::getSectionSize(DataRefImpl Ref,
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000244 uint64_t &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000245 const coff_section *Sec = toSec(Ref);
246 Result = Sec->SizeOfRawData;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000247 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000248}
249
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000250error_code COFFObjectFile::getSectionContents(DataRefImpl Ref,
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000251 StringRef &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000252 const coff_section *Sec = toSec(Ref);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000253 ArrayRef<uint8_t> Res;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000254 error_code EC = getSectionContents(Sec, Res);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000255 Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size());
256 return EC;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000257}
258
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000259error_code COFFObjectFile::getSectionAlignment(DataRefImpl Ref,
Michael J. Spencer79894602011-10-10 21:55:43 +0000260 uint64_t &Res) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000261 const coff_section *Sec = toSec(Ref);
262 if (!Sec)
Michael J. Spencer79894602011-10-10 21:55:43 +0000263 return object_error::parse_failed;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000264 Res = uint64_t(1) << (((Sec->Characteristics & 0x00F00000) >> 20) - 1);
Michael J. Spencer79894602011-10-10 21:55:43 +0000265 return object_error::success;
266}
267
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000268error_code COFFObjectFile::isSectionText(DataRefImpl Ref,
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000269 bool &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000270 const coff_section *Sec = toSec(Ref);
271 Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000272 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000273}
274
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000275error_code COFFObjectFile::isSectionData(DataRefImpl Ref,
Michael J. Spencer800619f2011-09-28 20:57:30 +0000276 bool &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000277 const coff_section *Sec = toSec(Ref);
278 Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000279 return object_error::success;
280}
281
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000282error_code COFFObjectFile::isSectionBSS(DataRefImpl Ref,
Michael J. Spencer800619f2011-09-28 20:57:30 +0000283 bool &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000284 const coff_section *Sec = toSec(Ref);
285 Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000286 return object_error::success;
287}
288
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000289error_code COFFObjectFile::isSectionRequiredForExecution(DataRefImpl Ref,
Preston Gurd2138ef62012-04-12 20:13:57 +0000290 bool &Result) const {
291 // FIXME: Unimplemented
292 Result = true;
293 return object_error::success;
294}
295
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000296error_code COFFObjectFile::isSectionVirtual(DataRefImpl Ref,
Preston Gurd2138ef62012-04-12 20:13:57 +0000297 bool &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000298 const coff_section *Sec = toSec(Ref);
299 Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
Preston Gurd2138ef62012-04-12 20:13:57 +0000300 return object_error::success;
301}
302
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000303error_code COFFObjectFile::isSectionZeroInit(DataRefImpl Ref,
Preston Gurd2138ef62012-04-12 20:13:57 +0000304 bool &Result) const {
Andrew Kaylorb96a3202012-10-10 01:45:52 +0000305 // FIXME: Unimplemented.
Preston Gurd2138ef62012-04-12 20:13:57 +0000306 Result = false;
307 return object_error::success;
308}
309
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000310error_code COFFObjectFile::isSectionReadOnlyData(DataRefImpl Ref,
Andrew Kaylor3f31fa02012-10-10 01:41:33 +0000311 bool &Result) const {
312 // FIXME: Unimplemented.
313 Result = false;
314 return object_error::success;
315}
316
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000317error_code COFFObjectFile::sectionContainsSymbol(DataRefImpl SecRef,
318 DataRefImpl SymbRef,
Benjamin Kramerf6f3e812011-07-15 18:39:21 +0000319 bool &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000320 const coff_section *Sec = toSec(SecRef);
321 const coff_symbol *Symb = toSymb(SymbRef);
322 const coff_section *SymbSec = 0;
323 if (error_code EC = getSection(Symb->SectionNumber, SymbSec)) return EC;
324 if (SymbSec == Sec)
Michael J. Spencer9a288512011-10-13 20:36:54 +0000325 Result = true;
326 else
327 Result = false;
Benjamin Kramerf6f3e812011-07-15 18:39:21 +0000328 return object_error::success;
329}
330
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000331relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const {
332 const coff_section *Sec = toSec(Ref);
333 DataRefImpl Ret;
334 if (Sec->NumberOfRelocations == 0)
335 Ret.p = 0;
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000336 else
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000337 Ret.p = reinterpret_cast<uintptr_t>(base() + Sec->PointerToRelocations);
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000338
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000339 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000340}
341
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000342relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const {
343 const coff_section *Sec = toSec(Ref);
344 DataRefImpl Ret;
345 if (Sec->NumberOfRelocations == 0)
346 Ret.p = 0;
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000347 else
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000348 Ret.p = reinterpret_cast<uintptr_t>(
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000349 reinterpret_cast<const coff_relocation*>(
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000350 base() + Sec->PointerToRelocations)
351 + Sec->NumberOfRelocations);
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000352
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000353 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000354}
355
Rui Ueyamac2bed422013-09-27 21:04:00 +0000356// Initialize the pointer to the symbol table.
357error_code COFFObjectFile::initSymbolTablePtr() {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000358 if (error_code EC = getObject(
Rui Ueyamac2bed422013-09-27 21:04:00 +0000359 SymbolTable, Data, base() + COFFHeader->PointerToSymbolTable,
360 COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000361 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000362
363 // Find string table. The first four byte of the string table contains the
364 // total size of the string table, including the size field itself. If the
365 // string table is empty, the value of the first four byte would be 4.
366 const uint8_t *StringTableAddr =
367 base() + COFFHeader->PointerToSymbolTable +
368 COFFHeader->NumberOfSymbols * sizeof(coff_symbol);
369 const ulittle32_t *StringTableSizePtr;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000370 if (error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr))
371 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000372 StringTableSize = *StringTableSizePtr;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000373 if (error_code EC =
Rui Ueyamac2bed422013-09-27 21:04:00 +0000374 getObject(StringTable, Data, StringTableAddr, StringTableSize))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000375 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000376
377 // Check that the string table is null terminated if has any in it.
378 if (StringTableSize < 4 ||
379 (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0))
380 return object_error::parse_failed;
381 return object_error::success;
382}
383
384// Returns the file offset for the given RVA.
385error_code COFFObjectFile::getRvaPtr(uint32_t Rva, uintptr_t &Res) const {
Rafael Espindolab5155a52014-02-10 20:24:04 +0000386 for (section_iterator I = section_begin(), E = section_end(); I != E;
Rafael Espindola5e812af2014-01-30 02:49:50 +0000387 ++I) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000388 const coff_section *Section = getCOFFSection(I);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000389 uint32_t SectionStart = Section->VirtualAddress;
390 uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize;
391 if (SectionStart <= Rva && Rva < SectionEnd) {
392 uint32_t Offset = Rva - SectionStart;
393 Res = uintptr_t(base()) + Section->PointerToRawData + Offset;
394 return object_error::success;
395 }
396 }
397 return object_error::parse_failed;
398}
399
400// Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
401// table entry.
402error_code COFFObjectFile::
403getHintName(uint32_t Rva, uint16_t &Hint, StringRef &Name) const {
404 uintptr_t IntPtr = 0;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000405 if (error_code EC = getRvaPtr(Rva, IntPtr))
406 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000407 const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr);
408 Hint = *reinterpret_cast<const ulittle16_t *>(Ptr);
409 Name = StringRef(reinterpret_cast<const char *>(Ptr + 2));
410 return object_error::success;
411}
412
413// Find the import table.
414error_code COFFObjectFile::initImportTablePtr() {
415 // First, we get the RVA of the import table. If the file lacks a pointer to
416 // the import table, do nothing.
417 const data_directory *DataEntry;
418 if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
419 return object_error::success;
420
421 // Do nothing if the pointer to import table is NULL.
422 if (DataEntry->RelativeVirtualAddress == 0)
423 return object_error::success;
424
425 uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
426 NumberOfImportDirectory = DataEntry->Size /
427 sizeof(import_directory_table_entry);
428
429 // Find the section that contains the RVA. This is needed because the RVA is
430 // the import table's memory address which is different from its file offset.
431 uintptr_t IntPtr = 0;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000432 if (error_code EC = getRvaPtr(ImportTableRva, IntPtr))
433 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000434 ImportDirectory = reinterpret_cast<
435 const import_directory_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000436 return object_error::success;
437}
Rui Ueyamac2bed422013-09-27 21:04:00 +0000438
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000439// Find the export table.
440error_code COFFObjectFile::initExportTablePtr() {
441 // First, we get the RVA of the export table. If the file lacks a pointer to
442 // the export table, do nothing.
443 const data_directory *DataEntry;
444 if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
445 return object_error::success;
446
447 // Do nothing if the pointer to export table is NULL.
448 if (DataEntry->RelativeVirtualAddress == 0)
449 return object_error::success;
450
451 uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress;
452 uintptr_t IntPtr = 0;
453 if (error_code EC = getRvaPtr(ExportTableRva, IntPtr))
454 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +0000455 ExportDirectory =
456 reinterpret_cast<const export_directory_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000457 return object_error::success;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000458}
459
Rafael Espindolaafcc3df2014-01-24 21:32:21 +0000460COFFObjectFile::COFFObjectFile(MemoryBuffer *Object, error_code &EC,
461 bool BufferOwned)
462 : ObjectFile(Binary::ID_COFF, Object, BufferOwned), COFFHeader(0),
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000463 PE32Header(0), PE32PlusHeader(0), DataDirectory(0), SectionTable(0),
464 SymbolTable(0), StringTable(0), StringTableSize(0), ImportDirectory(0),
Rafael Espindolaafcc3df2014-01-24 21:32:21 +0000465 NumberOfImportDirectory(0), ExportDirectory(0) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000466 // Check that we at least have enough room for a header.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000467 if (!checkSize(Data, EC, sizeof(coff_file_header))) return;
Eric Christopheree066fc2011-04-03 22:53:19 +0000468
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000469 // The current location in the file where we are looking at.
470 uint64_t CurPtr = 0;
471
472 // PE header is optional and is present only in executables. If it exists,
473 // it is placed right after COFF header.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000474 bool HasPEHeader = false;
Eric Christopheree066fc2011-04-03 22:53:19 +0000475
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000476 // Check if this is a PE/COFF file.
Michael J. Spencerec29b122011-06-25 17:54:50 +0000477 if (base()[0] == 0x4d && base()[1] == 0x5a) {
Eric Christopheree066fc2011-04-03 22:53:19 +0000478 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
479 // PE signature to find 'normal' COFF header.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000480 if (!checkSize(Data, EC, 0x3c + 8)) return;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000481 CurPtr = *reinterpret_cast<const ulittle16_t *>(base() + 0x3c);
482 // Check the PE magic bytes. ("PE\0\0")
483 if (std::memcmp(base() + CurPtr, "PE\0\0", 4) != 0) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000484 EC = object_error::parse_failed;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000485 return;
486 }
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000487 CurPtr += 4; // Skip the PE magic bytes.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000488 HasPEHeader = true;
Eric Christopheree066fc2011-04-03 22:53:19 +0000489 }
490
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000491 if ((EC = getObject(COFFHeader, Data, base() + CurPtr)))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000492 return;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000493 CurPtr += sizeof(coff_file_header);
494
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000495 if (HasPEHeader) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000496 const pe32_header *Header;
497 if ((EC = getObject(Header, Data, base() + CurPtr)))
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000498 return;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000499
500 const uint8_t *DataDirAddr;
501 uint64_t DataDirSize;
502 if (Header->Magic == 0x10b) {
503 PE32Header = Header;
504 DataDirAddr = base() + CurPtr + sizeof(pe32_header);
505 DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
506 } else if (Header->Magic == 0x20b) {
507 PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header);
508 DataDirAddr = base() + CurPtr + sizeof(pe32plus_header);
509 DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize;
510 } else {
511 // It's neither PE32 nor PE32+.
512 EC = object_error::parse_failed;
513 return;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000514 }
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000515 if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize)))
516 return;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000517 CurPtr += COFFHeader->SizeOfOptionalHeader;
518 }
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000519
Rafael Espindola692410e2014-01-21 23:06:54 +0000520 if (COFFHeader->isImportLibrary())
521 return;
522
523 if ((EC = getObject(SectionTable, Data, base() + CurPtr,
524 COFFHeader->NumberOfSections * sizeof(coff_section))))
525 return;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000526
Rui Ueyamac2bed422013-09-27 21:04:00 +0000527 // Initialize the pointer to the symbol table.
528 if (COFFHeader->PointerToSymbolTable != 0)
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000529 if ((EC = initSymbolTablePtr()))
Michael J. Spencerd5930ca2011-11-08 23:34:07 +0000530 return;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000531
Rui Ueyamac2bed422013-09-27 21:04:00 +0000532 // Initialize the pointer to the beginning of the import table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000533 if ((EC = initImportTablePtr()))
Rui Ueyamac2bed422013-09-27 21:04:00 +0000534 return;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000535
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000536 // Initialize the pointer to the export table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000537 if ((EC = initExportTablePtr()))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000538 return;
539
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000540 EC = object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000541}
542
Rafael Espindolab5155a52014-02-10 20:24:04 +0000543symbol_iterator COFFObjectFile::symbol_begin() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000544 DataRefImpl Ret;
545 Ret.p = reinterpret_cast<uintptr_t>(SymbolTable);
546 return symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000547}
548
Rafael Espindolab5155a52014-02-10 20:24:04 +0000549symbol_iterator COFFObjectFile::symbol_end() const {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000550 // The symbol table ends where the string table begins.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000551 DataRefImpl Ret;
552 Ret.p = reinterpret_cast<uintptr_t>(StringTable);
553 return symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000554}
555
Rafael Espindolab5155a52014-02-10 20:24:04 +0000556library_iterator COFFObjectFile::needed_library_begin() const {
David Meyer2fc34c52012-03-01 01:36:50 +0000557 // TODO: implement
558 report_fatal_error("Libraries needed unimplemented in COFFObjectFile");
559}
560
Rafael Espindolab5155a52014-02-10 20:24:04 +0000561library_iterator COFFObjectFile::needed_library_end() const {
David Meyer2fc34c52012-03-01 01:36:50 +0000562 // TODO: implement
563 report_fatal_error("Libraries needed unimplemented in COFFObjectFile");
564}
565
David Meyerc429b802012-03-01 22:19:54 +0000566StringRef COFFObjectFile::getLoadName() const {
567 // COFF does not have this field.
568 return "";
569}
570
Rui Ueyamabc654b12013-09-27 21:47:05 +0000571import_directory_iterator COFFObjectFile::import_directory_begin() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000572 return import_directory_iterator(
573 ImportDirectoryEntryRef(ImportDirectory, 0, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000574}
575
Rui Ueyamabc654b12013-09-27 21:47:05 +0000576import_directory_iterator COFFObjectFile::import_directory_end() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000577 return import_directory_iterator(
578 ImportDirectoryEntryRef(ImportDirectory, NumberOfImportDirectory, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000579}
David Meyerc429b802012-03-01 22:19:54 +0000580
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000581export_directory_iterator COFFObjectFile::export_directory_begin() const {
582 return export_directory_iterator(
583 ExportDirectoryEntryRef(ExportDirectory, 0, this));
584}
585
586export_directory_iterator COFFObjectFile::export_directory_end() const {
587 if (ExportDirectory == 0)
588 return export_directory_iterator(ExportDirectoryEntryRef(0, 0, this));
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000589 ExportDirectoryEntryRef Ref(ExportDirectory,
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000590 ExportDirectory->AddressTableEntries, this);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000591 return export_directory_iterator(Ref);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000592}
593
Rafael Espindolab5155a52014-02-10 20:24:04 +0000594section_iterator COFFObjectFile::section_begin() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000595 DataRefImpl Ret;
596 Ret.p = reinterpret_cast<uintptr_t>(SectionTable);
597 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000598}
599
Rafael Espindolab5155a52014-02-10 20:24:04 +0000600section_iterator COFFObjectFile::section_end() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000601 DataRefImpl Ret;
602 int NumSections = COFFHeader->isImportLibrary()
Rui Ueyama15ba1e22013-11-15 20:23:25 +0000603 ? 0 : COFFHeader->NumberOfSections;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000604 Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections);
605 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000606}
607
608uint8_t COFFObjectFile::getBytesInAddress() const {
Michael J. Spencer0324b672011-01-21 02:27:02 +0000609 return getArch() == Triple::x86_64 ? 8 : 4;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000610}
611
612StringRef COFFObjectFile::getFileFormatName() const {
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000613 switch(COFFHeader->Machine) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000614 case COFF::IMAGE_FILE_MACHINE_I386:
615 return "COFF-i386";
616 case COFF::IMAGE_FILE_MACHINE_AMD64:
617 return "COFF-x86-64";
618 default:
619 return "COFF-<unknown arch>";
620 }
621}
622
623unsigned COFFObjectFile::getArch() const {
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000624 switch(COFFHeader->Machine) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000625 case COFF::IMAGE_FILE_MACHINE_I386:
626 return Triple::x86;
627 case COFF::IMAGE_FILE_MACHINE_AMD64:
628 return Triple::x86_64;
629 default:
630 return Triple::UnknownArch;
631 }
632}
633
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000634// This method is kept here because lld uses this. As soon as we make
635// lld to use getCOFFHeader, this method will be removed.
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000636error_code COFFObjectFile::getHeader(const coff_file_header *&Res) const {
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000637 return getCOFFHeader(Res);
638}
639
640error_code COFFObjectFile::getCOFFHeader(const coff_file_header *&Res) const {
641 Res = COFFHeader;
642 return object_error::success;
643}
644
645error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
646 Res = PE32Header;
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000647 return object_error::success;
648}
649
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000650error_code
651COFFObjectFile::getPE32PlusHeader(const pe32plus_header *&Res) const {
652 Res = PE32PlusHeader;
653 return object_error::success;
654}
655
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000656error_code COFFObjectFile::getDataDirectory(uint32_t Index,
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000657 const data_directory *&Res) const {
658 // Error if if there's no data directory or the index is out of range.
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000659 if (!DataDirectory)
660 return object_error::parse_failed;
661 assert(PE32Header || PE32PlusHeader);
662 uint32_t NumEnt = PE32Header ? PE32Header->NumberOfRvaAndSize
663 : PE32PlusHeader->NumberOfRvaAndSize;
664 if (Index > NumEnt)
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000665 return object_error::parse_failed;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000666 Res = &DataDirectory[Index];
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000667 return object_error::success;
668}
669
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000670error_code COFFObjectFile::getSection(int32_t Index,
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000671 const coff_section *&Result) const {
672 // Check for special index values.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000673 if (Index == COFF::IMAGE_SYM_UNDEFINED ||
674 Index == COFF::IMAGE_SYM_ABSOLUTE ||
675 Index == COFF::IMAGE_SYM_DEBUG)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000676 Result = NULL;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000677 else if (Index > 0 && Index <= COFFHeader->NumberOfSections)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000678 // We already verified the section table data, so no need to check again.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000679 Result = SectionTable + (Index - 1);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000680 else
681 return object_error::parse_failed;
682 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000683}
684
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000685error_code COFFObjectFile::getString(uint32_t Offset,
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000686 StringRef &Result) const {
687 if (StringTableSize <= 4)
688 // Tried to get a string from an empty string table.
689 return object_error::parse_failed;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000690 if (Offset >= StringTableSize)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000691 return object_error::unexpected_eof;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000692 Result = StringRef(StringTable + Offset);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000693 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000694}
695
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000696error_code COFFObjectFile::getSymbol(uint32_t Index,
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000697 const coff_symbol *&Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000698 if (Index < COFFHeader->NumberOfSymbols)
699 Result = SymbolTable + Index;
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000700 else
701 return object_error::parse_failed;
702 return object_error::success;
703}
704
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000705error_code COFFObjectFile::getSymbolName(const coff_symbol *Symbol,
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000706 StringRef &Res) const {
707 // Check for string table entry. First 4 bytes are 0.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000708 if (Symbol->Name.Offset.Zeroes == 0) {
709 uint32_t Offset = Symbol->Name.Offset.Offset;
710 if (error_code EC = getString(Offset, Res))
711 return EC;
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000712 return object_error::success;
713 }
714
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000715 if (Symbol->Name.ShortName[7] == 0)
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000716 // Null terminated, let ::strlen figure out the length.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000717 Res = StringRef(Symbol->Name.ShortName);
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000718 else
719 // Not null terminated, use all 8 bytes.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000720 Res = StringRef(Symbol->Name.ShortName, 8);
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000721 return object_error::success;
722}
723
Marshall Clow71757ef2012-06-15 01:08:25 +0000724ArrayRef<uint8_t> COFFObjectFile::getSymbolAuxData(
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000725 const coff_symbol *Symbol) const {
726 const uint8_t *Aux = NULL;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000727
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000728 if (Symbol->NumberOfAuxSymbols > 0) {
Marshall Clow71757ef2012-06-15 01:08:25 +0000729 // AUX data comes immediately after the symbol in COFF
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000730 Aux = reinterpret_cast<const uint8_t *>(Symbol + 1);
Marshall Clow71757ef2012-06-15 01:08:25 +0000731# ifndef NDEBUG
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000732 // Verify that the Aux symbol points to a valid entry in the symbol table.
733 uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base());
734 if (Offset < COFFHeader->PointerToSymbolTable
735 || Offset >= COFFHeader->PointerToSymbolTable
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000736 + (COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
Marshall Clow71757ef2012-06-15 01:08:25 +0000737 report_fatal_error("Aux Symbol data was outside of symbol table.");
738
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000739 assert((Offset - COFFHeader->PointerToSymbolTable) % sizeof(coff_symbol)
Marshall Clow71757ef2012-06-15 01:08:25 +0000740 == 0 && "Aux Symbol data did not point to the beginning of a symbol");
Marshall Clow71757ef2012-06-15 01:08:25 +0000741# endif
Marshall Clowbfb85e62012-06-15 01:15:47 +0000742 }
Rui Ueyama24fc2d62014-01-17 22:11:27 +0000743 return ArrayRef<uint8_t>(Aux,
744 Symbol->NumberOfAuxSymbols * sizeof(coff_symbol));
Marshall Clow71757ef2012-06-15 01:08:25 +0000745}
746
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000747error_code COFFObjectFile::getSectionName(const coff_section *Sec,
748 StringRef &Res) const {
749 StringRef Name;
750 if (Sec->Name[7] == 0)
751 // Null terminated, let ::strlen figure out the length.
752 Name = Sec->Name;
753 else
754 // Not null terminated, use all 8 bytes.
755 Name = StringRef(Sec->Name, 8);
756
757 // Check for string table entry. First byte is '/'.
758 if (Name[0] == '/') {
759 uint32_t Offset;
760 if (Name.substr(1).getAsInteger(10, Offset))
761 return object_error::parse_failed;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000762 if (error_code EC = getString(Offset, Name))
763 return EC;
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000764 }
765
766 Res = Name;
767 return object_error::success;
768}
769
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000770error_code COFFObjectFile::getSectionContents(const coff_section *Sec,
771 ArrayRef<uint8_t> &Res) const {
772 // The only thing that we need to verify is that the contents is contained
773 // within the file bounds. We don't need to make sure it doesn't cover other
774 // data, as there's nothing that says that is not allowed.
775 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
776 uintptr_t ConEnd = ConStart + Sec->SizeOfRawData;
777 if (ConEnd > uintptr_t(Data->getBufferEnd()))
778 return object_error::parse_failed;
779 Res = ArrayRef<uint8_t>(reinterpret_cast<const unsigned char*>(ConStart),
780 Sec->SizeOfRawData);
781 return object_error::success;
782}
783
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000784const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000785 return reinterpret_cast<const coff_relocation*>(Rel.p);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000786}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000787
Rafael Espindola5e812af2014-01-30 02:49:50 +0000788void COFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000789 Rel.p = reinterpret_cast<uintptr_t>(
790 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000791}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000792
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000793error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel,
794 uint64_t &Res) const {
Rafael Espindola1e483872013-04-25 12:28:45 +0000795 report_fatal_error("getRelocationAddress not implemented in COFFObjectFile");
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000796}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000797
Danil Malyshevcbe72fc2011-11-29 17:40:10 +0000798error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel,
799 uint64_t &Res) const {
800 Res = toRel(Rel)->VirtualAddress;
801 return object_error::success;
802}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000803
Rafael Espindola806f0062013-06-05 01:33:53 +0000804symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000805 const coff_relocation* R = toRel(Rel);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000806 DataRefImpl Ref;
807 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable + R->SymbolTableIndex);
808 return symbol_iterator(SymbolRef(Ref, this));
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000809}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000810
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000811error_code COFFObjectFile::getRelocationType(DataRefImpl Rel,
Owen Anderson7be76592011-10-26 17:08:49 +0000812 uint64_t &Res) const {
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000813 const coff_relocation* R = toRel(Rel);
814 Res = R->Type;
815 return object_error::success;
816}
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000817
Marshall Clow71757ef2012-06-15 01:08:25 +0000818const coff_section *COFFObjectFile::getCOFFSection(section_iterator &It) const {
819 return toSec(It->getRawDataRefImpl());
820}
821
822const coff_symbol *COFFObjectFile::getCOFFSymbol(symbol_iterator &It) const {
823 return toSymb(It->getRawDataRefImpl());
824}
825
Marshall Clowd3e2a762012-06-18 19:47:16 +0000826const coff_relocation *COFFObjectFile::getCOFFRelocation(
827 relocation_iterator &It) const {
828 return toRel(It->getRawDataRefImpl());
829}
830
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000831#define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(enum) \
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000832 case COFF::enum: Res = #enum; break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000833
834error_code COFFObjectFile::getRelocationTypeName(DataRefImpl Rel,
835 SmallVectorImpl<char> &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000836 const coff_relocation *Reloc = toRel(Rel);
837 StringRef Res;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000838 switch (COFFHeader->Machine) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000839 case COFF::IMAGE_FILE_MACHINE_AMD64:
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000840 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000841 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
842 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
843 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
844 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
845 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
846 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
847 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
848 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
849 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
850 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
851 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
852 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
853 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
854 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
855 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
856 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
857 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
858 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000859 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000860 }
861 break;
862 case COFF::IMAGE_FILE_MACHINE_I386:
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000863 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000864 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
865 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
866 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
867 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
868 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
869 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
870 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
871 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
872 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
873 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
874 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
875 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000876 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000877 }
878 break;
879 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000880 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000881 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000882 Result.append(Res.begin(), Res.end());
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000883 return object_error::success;
884}
885
886#undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
887
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000888error_code COFFObjectFile::getRelocationValueString(DataRefImpl Rel,
889 SmallVectorImpl<char> &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000890 const coff_relocation *Reloc = toRel(Rel);
891 const coff_symbol *Symb = 0;
892 if (error_code EC = getSymbol(Reloc->SymbolTableIndex, Symb)) return EC;
893 DataRefImpl Sym;
894 Sym.p = reinterpret_cast<uintptr_t>(Symb);
895 StringRef SymName;
896 if (error_code EC = getSymbolName(Sym, SymName)) return EC;
897 Result.append(SymName.begin(), SymName.end());
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000898 return object_error::success;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000899}
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000900
David Meyer2fc34c52012-03-01 01:36:50 +0000901error_code COFFObjectFile::getLibraryNext(DataRefImpl LibData,
902 LibraryRef &Result) const {
903 report_fatal_error("getLibraryNext not implemented in COFFObjectFile");
904}
905
906error_code COFFObjectFile::getLibraryPath(DataRefImpl LibData,
907 StringRef &Result) const {
908 report_fatal_error("getLibraryPath not implemented in COFFObjectFile");
909}
910
Rui Ueyamac2bed422013-09-27 21:04:00 +0000911bool ImportDirectoryEntryRef::
912operator==(const ImportDirectoryEntryRef &Other) const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000913 return ImportTable == Other.ImportTable && Index == Other.Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000914}
915
Rafael Espindola5e812af2014-01-30 02:49:50 +0000916void ImportDirectoryEntryRef::moveNext() {
917 ++Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000918}
919
920error_code ImportDirectoryEntryRef::
921getImportTableEntry(const import_directory_table_entry *&Result) const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000922 Result = ImportTable;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000923 return object_error::success;
924}
925
926error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000927 uintptr_t IntPtr = 0;
Rui Ueyamaa045b732014-01-16 03:13:19 +0000928 if (error_code EC = OwningObject->getRvaPtr(ImportTable->NameRVA, IntPtr))
929 return EC;
930 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000931 return object_error::success;
932}
933
934error_code ImportDirectoryEntryRef::getImportLookupEntry(
935 const import_lookup_table_entry32 *&Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000936 uintptr_t IntPtr = 0;
Rui Ueyamaa045b732014-01-16 03:13:19 +0000937 if (error_code EC =
938 OwningObject->getRvaPtr(ImportTable->ImportLookupTableRVA, IntPtr))
939 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000940 Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr);
941 return object_error::success;
942}
943
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000944bool ExportDirectoryEntryRef::
945operator==(const ExportDirectoryEntryRef &Other) const {
946 return ExportTable == Other.ExportTable && Index == Other.Index;
947}
948
Rafael Espindola5e812af2014-01-30 02:49:50 +0000949void ExportDirectoryEntryRef::moveNext() {
950 ++Index;
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000951}
952
Rui Ueyamada49d0d2014-01-16 20:50:34 +0000953// Returns the name of the current export symbol. If the symbol is exported only
954// by ordinal, the empty string is set as a result.
955error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const {
956 uintptr_t IntPtr = 0;
957 if (error_code EC = OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr))
958 return EC;
959 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
960 return object_error::success;
961}
962
Rui Ueyamae5df6092014-01-17 22:02:24 +0000963// Returns the starting ordinal number.
964error_code ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const {
965 Result = ExportTable->OrdinalBase;
966 return object_error::success;
967}
968
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000969// Returns the export ordinal of the current export symbol.
970error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const {
971 Result = ExportTable->OrdinalBase + Index;
972 return object_error::success;
973}
974
975// Returns the address of the current export symbol.
976error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const {
977 uintptr_t IntPtr = 0;
978 if (error_code EC = OwningObject->getRvaPtr(
979 ExportTable->ExportAddressTableRVA, IntPtr))
980 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +0000981 const export_address_table_entry *entry =
982 reinterpret_cast<const export_address_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000983 Result = entry[Index].ExportRVA;
984 return object_error::success;
985}
986
987// Returns the name of the current export symbol. If the symbol is exported only
988// by ordinal, the empty string is set as a result.
Rui Ueyamada49d0d2014-01-16 20:50:34 +0000989error_code ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000990 uintptr_t IntPtr = 0;
991 if (error_code EC = OwningObject->getRvaPtr(
992 ExportTable->OrdinalTableRVA, IntPtr))
993 return EC;
994 const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr);
995
996 uint32_t NumEntries = ExportTable->NumberOfNamePointers;
997 int Offset = 0;
998 for (const ulittle16_t *I = Start, *E = Start + NumEntries;
999 I < E; ++I, ++Offset) {
1000 if (*I != Index)
1001 continue;
1002 if (error_code EC = OwningObject->getRvaPtr(
1003 ExportTable->NamePointerRVA, IntPtr))
1004 return EC;
1005 const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr);
1006 if (error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr))
1007 return EC;
1008 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1009 return object_error::success;
1010 }
1011 Result = "";
1012 return object_error::success;
1013}
1014
Rafael Espindolaafcc3df2014-01-24 21:32:21 +00001015ErrorOr<ObjectFile *> ObjectFile::createCOFFObjectFile(MemoryBuffer *Object,
1016 bool BufferOwned) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001017 error_code EC;
Rafael Espindolaafcc3df2014-01-24 21:32:21 +00001018 OwningPtr<COFFObjectFile> Ret(new COFFObjectFile(Object, EC, BufferOwned));
Rafael Espindola692410e2014-01-21 23:06:54 +00001019 if (EC)
1020 return EC;
1021 return Ret.take();
Rui Ueyama686738e2014-01-16 20:30:36 +00001022}