blob: 1a360f5bed0129481331dbd8285cfb2dab9c6ffd [file] [log] [blame]
Eric Christopher9cad53c2013-04-03 18:31:38 +00001//===-- COFFDumper.cpp - COFF-specific dumper -------------------*- 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/// \file
11/// \brief This file implements the COFF-specific dumper for llvm-readobj.
12///
13//===----------------------------------------------------------------------===//
14
15#include "llvm-readobj.h"
Eric Christopher9cad53c2013-04-03 18:31:38 +000016#include "Error.h"
Chandler Carruth07baed52014-01-13 08:04:33 +000017#include "ObjDumper.h"
Eric Christopher9cad53c2013-04-03 18:31:38 +000018#include "StreamWriter.h"
Eric Christopher9cad53c2013-04-03 18:31:38 +000019#include "llvm/ADT/DenseMap.h"
20#include "llvm/ADT/SmallString.h"
21#include "llvm/Object/COFF.h"
22#include "llvm/Object/ObjectFile.h"
Chandler Carruth07baed52014-01-13 08:04:33 +000023#include "llvm/Support/COFF.h"
Eric Christopher9cad53c2013-04-03 18:31:38 +000024#include "llvm/Support/Casting.h"
25#include "llvm/Support/Compiler.h"
Timur Iskhodzhanov48703be2013-12-19 11:37:14 +000026#include "llvm/Support/DataExtractor.h"
Eric Christopher9cad53c2013-04-03 18:31:38 +000027#include "llvm/Support/Format.h"
28#include "llvm/Support/SourceMgr.h"
29#include "llvm/Support/Win64EH.h"
30#include "llvm/Support/raw_ostream.h"
31#include "llvm/Support/system_error.h"
Eric Christopher9cad53c2013-04-03 18:31:38 +000032#include <algorithm>
33#include <cstring>
34#include <time.h>
35
36using namespace llvm;
37using namespace llvm::object;
38using namespace llvm::Win64EH;
39
40namespace {
41
42class COFFDumper : public ObjDumper {
43public:
44 COFFDumper(const llvm::object::COFFObjectFile *Obj, StreamWriter& Writer)
45 : ObjDumper(Writer)
46 , Obj(Obj) {
47 cacheRelocations();
48 }
49
Craig Topper73156022014-03-02 09:09:27 +000050 virtual void printFileHeaders() override;
51 virtual void printSections() override;
52 virtual void printRelocations() override;
53 virtual void printSymbols() override;
54 virtual void printDynamicSymbols() override;
55 virtual void printUnwindInfo() override;
Eric Christopher9cad53c2013-04-03 18:31:38 +000056
57private:
Alexey Samsonov27dc8392014-03-18 06:53:02 +000058 void printSymbol(const SymbolRef &Sym);
59 void printRelocation(const SectionRef &Section, const RelocationRef &Reloc);
Rui Ueyamaed64342b2013-07-19 23:23:29 +000060 void printDataDirectory(uint32_t Index, const std::string &FieldName);
Eric Christopher9cad53c2013-04-03 18:31:38 +000061 void printX64UnwindInfo();
62
Rui Ueyama10ed9dd2014-01-26 04:15:52 +000063 template <class PEHeader> void printPEHeader(const PEHeader *Hdr);
64 void printBaseOfDataField(const pe32_header *Hdr);
65 void printBaseOfDataField(const pe32plus_header *Hdr);
66
Saleem Abdulrasool5dd27f42014-05-25 20:26:37 +000067 void printRuntimeFunction(const RuntimeFunction& RTF,
68 const coff_section *Section,
69 uint64_t SectionOffset);
Eric Christopher9cad53c2013-04-03 18:31:38 +000070
Saleem Abdulrasool5dd27f42014-05-25 20:26:37 +000071 void printUnwindInfo(const Win64EH::UnwindInfo& UI,
72 const coff_section *Section, uint64_t SectionOffset);
Eric Christopher9cad53c2013-04-03 18:31:38 +000073
Alexey Samsonov27dc8392014-03-18 06:53:02 +000074 void printUnwindCode(const Win64EH::UnwindInfo &UI, ArrayRef<UnwindCode> UCs);
Eric Christopher9cad53c2013-04-03 18:31:38 +000075
Alexey Samsonov27dc8392014-03-18 06:53:02 +000076 void printCodeViewLineTables(const SectionRef &Section);
Timur Iskhodzhanov48703be2013-12-19 11:37:14 +000077
Eric Christopher9cad53c2013-04-03 18:31:38 +000078 void cacheRelocations();
79
Saleem Abdulrasool5dd27f42014-05-25 20:26:37 +000080 error_code resolveRelocation(const coff_section *Section, uint64_t Offset,
81 const coff_section *&ReesolvedSection,
82 uint64_t &ResolvedAddress);
83
84 error_code resolveSymbol(const coff_section *Section, uint64_t Offset,
85 SymbolRef &Sym);
86 error_code resolveSymbolName(const coff_section *Section, uint64_t Offset,
87 StringRef &Name);
88 std::string formatSymbol(const coff_section *Section, uint64_t Offset,
89 uint32_t Disp);
Eric Christopher9cad53c2013-04-03 18:31:38 +000090
91 typedef DenseMap<const coff_section*, std::vector<RelocationRef> > RelocMapTy;
92
93 const llvm::object::COFFObjectFile *Obj;
94 RelocMapTy RelocMap;
Eric Christopher9cad53c2013-04-03 18:31:38 +000095};
96
97} // namespace
98
99
100namespace llvm {
101
Ahmed Charles56440fd2014-03-06 05:51:42 +0000102error_code createCOFFDumper(const object::ObjectFile *Obj, StreamWriter &Writer,
103 std::unique_ptr<ObjDumper> &Result) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000104 const COFFObjectFile *COFFObj = dyn_cast<COFFObjectFile>(Obj);
105 if (!COFFObj)
106 return readobj_error::unsupported_obj_file_format;
107
108 Result.reset(new COFFDumper(COFFObj, Writer));
109 return readobj_error::success;
110}
111
112} // namespace llvm
113
114
115// Returns the name of the unwind code.
116static StringRef getUnwindCodeTypeName(uint8_t Code) {
117 switch(Code) {
118 default: llvm_unreachable("Invalid unwind code");
119 case UOP_PushNonVol: return "PUSH_NONVOL";
120 case UOP_AllocLarge: return "ALLOC_LARGE";
121 case UOP_AllocSmall: return "ALLOC_SMALL";
122 case UOP_SetFPReg: return "SET_FPREG";
123 case UOP_SaveNonVol: return "SAVE_NONVOL";
124 case UOP_SaveNonVolBig: return "SAVE_NONVOL_FAR";
125 case UOP_SaveXMM128: return "SAVE_XMM128";
126 case UOP_SaveXMM128Big: return "SAVE_XMM128_FAR";
127 case UOP_PushMachFrame: return "PUSH_MACHFRAME";
128 }
129}
130
131// Returns the name of a referenced register.
132static StringRef getUnwindRegisterName(uint8_t Reg) {
133 switch(Reg) {
134 default: llvm_unreachable("Invalid register");
135 case 0: return "RAX";
136 case 1: return "RCX";
137 case 2: return "RDX";
138 case 3: return "RBX";
139 case 4: return "RSP";
140 case 5: return "RBP";
141 case 6: return "RSI";
142 case 7: return "RDI";
143 case 8: return "R8";
144 case 9: return "R9";
145 case 10: return "R10";
146 case 11: return "R11";
147 case 12: return "R12";
148 case 13: return "R13";
149 case 14: return "R14";
150 case 15: return "R15";
151 }
152}
153
154// Calculates the number of array slots required for the unwind code.
155static unsigned getNumUsedSlots(const UnwindCode &UnwindCode) {
156 switch (UnwindCode.getUnwindOp()) {
157 default: llvm_unreachable("Invalid unwind code");
158 case UOP_PushNonVol:
159 case UOP_AllocSmall:
160 case UOP_SetFPReg:
161 case UOP_PushMachFrame:
162 return 1;
163 case UOP_SaveNonVol:
164 case UOP_SaveXMM128:
165 return 2;
166 case UOP_SaveNonVolBig:
167 case UOP_SaveXMM128Big:
168 return 3;
169 case UOP_AllocLarge:
170 return (UnwindCode.getOpInfo() == 0) ? 2 : 3;
171 }
172}
173
Saleem Abdulrasool5dd27f42014-05-25 20:26:37 +0000174// Given a a section and an offset into this section the function returns the
175// symbol used for the relocation at the offset.
176error_code COFFDumper::resolveSymbol(const coff_section *Section,
177 uint64_t Offset, SymbolRef &Sym) {
178 const auto &Relocations = RelocMap[Section];
179 for (const auto &Relocation : Relocations) {
180 uint64_t RelocationOffset;
181 if (error_code EC = Relocation.getOffset(RelocationOffset))
Eric Christopher9cad53c2013-04-03 18:31:38 +0000182 return EC;
183
Saleem Abdulrasool5dd27f42014-05-25 20:26:37 +0000184 if (RelocationOffset == Offset) {
Saleem Abdulrasool4a6f5832014-05-20 05:18:06 +0000185 Sym = *Relocation.getSymbol();
Eric Christopher9cad53c2013-04-03 18:31:38 +0000186 return readobj_error::success;
187 }
188 }
Eric Christopher9cad53c2013-04-03 18:31:38 +0000189 return readobj_error::unknown_symbol;
190}
191
Saleem Abdulrasool5dd27f42014-05-25 20:26:37 +0000192// Given a section and an offset into this section the function returns the name
193// of the symbol used for the relocation at the offset.
194error_code COFFDumper::resolveSymbolName(const coff_section *Section,
195 uint64_t Offset, StringRef &Name) {
196 SymbolRef Symbol;
197 if (error_code EC = resolveSymbol(Section, Offset, Symbol))
198 return EC;
199 if (error_code EC = Symbol.getName(Name))
200 return EC;
Eric Christopher9cad53c2013-04-03 18:31:38 +0000201 return object_error::success;
202}
203
204static const EnumEntry<COFF::MachineTypes> ImageFileMachineType[] = {
205 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_UNKNOWN ),
206 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_AM33 ),
207 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_AMD64 ),
208 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_ARM ),
Saleem Abdulrasool5e1780e2014-03-11 03:08:37 +0000209 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_ARMNT ),
Eric Christopher9cad53c2013-04-03 18:31:38 +0000210 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_EBC ),
211 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_I386 ),
212 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_IA64 ),
213 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_M32R ),
214 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_MIPS16 ),
215 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_MIPSFPU ),
216 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_MIPSFPU16),
217 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_POWERPC ),
218 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_POWERPCFP),
219 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_R4000 ),
220 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_SH3 ),
221 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_SH3DSP ),
222 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_SH4 ),
223 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_SH5 ),
224 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_THUMB ),
225 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_MACHINE_WCEMIPSV2)
226};
227
228static const EnumEntry<COFF::Characteristics> ImageFileCharacteristics[] = {
229 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_RELOCS_STRIPPED ),
230 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_EXECUTABLE_IMAGE ),
231 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_LINE_NUMS_STRIPPED ),
232 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_LOCAL_SYMS_STRIPPED ),
233 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_AGGRESSIVE_WS_TRIM ),
234 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_LARGE_ADDRESS_AWARE ),
235 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_BYTES_REVERSED_LO ),
236 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_32BIT_MACHINE ),
237 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_DEBUG_STRIPPED ),
238 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP),
239 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_NET_RUN_FROM_SWAP ),
240 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_SYSTEM ),
241 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_DLL ),
242 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_UP_SYSTEM_ONLY ),
243 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_FILE_BYTES_REVERSED_HI )
244};
245
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000246static const EnumEntry<COFF::WindowsSubsystem> PEWindowsSubsystem[] = {
247 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_UNKNOWN ),
248 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_NATIVE ),
249 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_WINDOWS_GUI ),
250 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_WINDOWS_CUI ),
251 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_POSIX_CUI ),
252 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_WINDOWS_CE_GUI ),
253 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_EFI_APPLICATION ),
254 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER),
255 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER ),
256 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_EFI_ROM ),
257 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SUBSYSTEM_XBOX ),
258};
259
260static const EnumEntry<COFF::DLLCharacteristics> PEDLLCharacteristics[] = {
Rui Ueyama06dc5e72014-01-27 04:22:24 +0000261 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA ),
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000262 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE ),
263 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY ),
264 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_NX_COMPAT ),
265 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_NO_ISOLATION ),
266 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_NO_SEH ),
267 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_NO_BIND ),
268 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_WDM_DRIVER ),
269 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_DLL_CHARACTERISTICS_TERMINAL_SERVER_AWARE),
270};
271
Eric Christopher9cad53c2013-04-03 18:31:38 +0000272static const EnumEntry<COFF::SectionCharacteristics>
273ImageSectionCharacteristics[] = {
274 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_TYPE_NO_PAD ),
275 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_CNT_CODE ),
276 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_CNT_INITIALIZED_DATA ),
277 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_CNT_UNINITIALIZED_DATA),
278 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_OTHER ),
279 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_INFO ),
280 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_REMOVE ),
281 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_COMDAT ),
282 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_GPREL ),
283 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_PURGEABLE ),
284 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_16BIT ),
285 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_LOCKED ),
286 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_PRELOAD ),
287 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_1BYTES ),
288 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_2BYTES ),
289 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_4BYTES ),
290 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_8BYTES ),
291 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_16BYTES ),
292 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_32BYTES ),
293 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_64BYTES ),
294 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_128BYTES ),
295 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_256BYTES ),
296 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_512BYTES ),
297 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_1024BYTES ),
298 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_2048BYTES ),
299 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_4096BYTES ),
300 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_ALIGN_8192BYTES ),
301 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_LNK_NRELOC_OVFL ),
302 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_DISCARDABLE ),
303 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_NOT_CACHED ),
304 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_NOT_PAGED ),
305 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_SHARED ),
306 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_EXECUTE ),
307 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_READ ),
308 LLVM_READOBJ_ENUM_ENT(COFF, IMAGE_SCN_MEM_WRITE )
309};
310
311static const EnumEntry<COFF::SymbolBaseType> ImageSymType[] = {
312 { "Null" , COFF::IMAGE_SYM_TYPE_NULL },
313 { "Void" , COFF::IMAGE_SYM_TYPE_VOID },
314 { "Char" , COFF::IMAGE_SYM_TYPE_CHAR },
315 { "Short" , COFF::IMAGE_SYM_TYPE_SHORT },
316 { "Int" , COFF::IMAGE_SYM_TYPE_INT },
317 { "Long" , COFF::IMAGE_SYM_TYPE_LONG },
318 { "Float" , COFF::IMAGE_SYM_TYPE_FLOAT },
319 { "Double", COFF::IMAGE_SYM_TYPE_DOUBLE },
320 { "Struct", COFF::IMAGE_SYM_TYPE_STRUCT },
321 { "Union" , COFF::IMAGE_SYM_TYPE_UNION },
322 { "Enum" , COFF::IMAGE_SYM_TYPE_ENUM },
323 { "MOE" , COFF::IMAGE_SYM_TYPE_MOE },
324 { "Byte" , COFF::IMAGE_SYM_TYPE_BYTE },
325 { "Word" , COFF::IMAGE_SYM_TYPE_WORD },
326 { "UInt" , COFF::IMAGE_SYM_TYPE_UINT },
327 { "DWord" , COFF::IMAGE_SYM_TYPE_DWORD }
328};
329
330static const EnumEntry<COFF::SymbolComplexType> ImageSymDType[] = {
331 { "Null" , COFF::IMAGE_SYM_DTYPE_NULL },
332 { "Pointer" , COFF::IMAGE_SYM_DTYPE_POINTER },
333 { "Function", COFF::IMAGE_SYM_DTYPE_FUNCTION },
334 { "Array" , COFF::IMAGE_SYM_DTYPE_ARRAY }
335};
336
337static const EnumEntry<COFF::SymbolStorageClass> ImageSymClass[] = {
338 { "EndOfFunction" , COFF::IMAGE_SYM_CLASS_END_OF_FUNCTION },
339 { "Null" , COFF::IMAGE_SYM_CLASS_NULL },
340 { "Automatic" , COFF::IMAGE_SYM_CLASS_AUTOMATIC },
341 { "External" , COFF::IMAGE_SYM_CLASS_EXTERNAL },
342 { "Static" , COFF::IMAGE_SYM_CLASS_STATIC },
343 { "Register" , COFF::IMAGE_SYM_CLASS_REGISTER },
344 { "ExternalDef" , COFF::IMAGE_SYM_CLASS_EXTERNAL_DEF },
345 { "Label" , COFF::IMAGE_SYM_CLASS_LABEL },
346 { "UndefinedLabel" , COFF::IMAGE_SYM_CLASS_UNDEFINED_LABEL },
347 { "MemberOfStruct" , COFF::IMAGE_SYM_CLASS_MEMBER_OF_STRUCT },
348 { "Argument" , COFF::IMAGE_SYM_CLASS_ARGUMENT },
349 { "StructTag" , COFF::IMAGE_SYM_CLASS_STRUCT_TAG },
350 { "MemberOfUnion" , COFF::IMAGE_SYM_CLASS_MEMBER_OF_UNION },
351 { "UnionTag" , COFF::IMAGE_SYM_CLASS_UNION_TAG },
352 { "TypeDefinition" , COFF::IMAGE_SYM_CLASS_TYPE_DEFINITION },
353 { "UndefinedStatic", COFF::IMAGE_SYM_CLASS_UNDEFINED_STATIC },
354 { "EnumTag" , COFF::IMAGE_SYM_CLASS_ENUM_TAG },
355 { "MemberOfEnum" , COFF::IMAGE_SYM_CLASS_MEMBER_OF_ENUM },
356 { "RegisterParam" , COFF::IMAGE_SYM_CLASS_REGISTER_PARAM },
357 { "BitField" , COFF::IMAGE_SYM_CLASS_BIT_FIELD },
358 { "Block" , COFF::IMAGE_SYM_CLASS_BLOCK },
359 { "Function" , COFF::IMAGE_SYM_CLASS_FUNCTION },
360 { "EndOfStruct" , COFF::IMAGE_SYM_CLASS_END_OF_STRUCT },
361 { "File" , COFF::IMAGE_SYM_CLASS_FILE },
362 { "Section" , COFF::IMAGE_SYM_CLASS_SECTION },
363 { "WeakExternal" , COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL },
364 { "CLRToken" , COFF::IMAGE_SYM_CLASS_CLR_TOKEN }
365};
366
367static const EnumEntry<COFF::COMDATType> ImageCOMDATSelect[] = {
368 { "NoDuplicates", COFF::IMAGE_COMDAT_SELECT_NODUPLICATES },
369 { "Any" , COFF::IMAGE_COMDAT_SELECT_ANY },
370 { "SameSize" , COFF::IMAGE_COMDAT_SELECT_SAME_SIZE },
371 { "ExactMatch" , COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH },
372 { "Associative" , COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE },
373 { "Largest" , COFF::IMAGE_COMDAT_SELECT_LARGEST },
374 { "Newest" , COFF::IMAGE_COMDAT_SELECT_NEWEST }
375};
376
377static const EnumEntry<COFF::WeakExternalCharacteristics>
378WeakExternalCharacteristics[] = {
379 { "NoLibrary", COFF::IMAGE_WEAK_EXTERN_SEARCH_NOLIBRARY },
380 { "Library" , COFF::IMAGE_WEAK_EXTERN_SEARCH_LIBRARY },
381 { "Alias" , COFF::IMAGE_WEAK_EXTERN_SEARCH_ALIAS }
382};
383
384static const EnumEntry<unsigned> UnwindFlags[] = {
385 { "ExceptionHandler", Win64EH::UNW_ExceptionHandler },
386 { "TerminateHandler", Win64EH::UNW_TerminateHandler },
387 { "ChainInfo" , Win64EH::UNW_ChainInfo }
388};
389
390static const EnumEntry<unsigned> UnwindOpInfo[] = {
391 { "RAX", 0 },
392 { "RCX", 1 },
393 { "RDX", 2 },
394 { "RBX", 3 },
395 { "RSP", 4 },
396 { "RBP", 5 },
397 { "RSI", 6 },
398 { "RDI", 7 },
399 { "R8", 8 },
400 { "R9", 9 },
401 { "R10", 10 },
402 { "R11", 11 },
403 { "R12", 12 },
404 { "R13", 13 },
405 { "R14", 14 },
406 { "R15", 15 }
407};
408
Eric Christopher9cad53c2013-04-03 18:31:38 +0000409static uint64_t getOffsetOfLSDA(const Win64EH::UnwindInfo& UI) {
410 return static_cast<const char*>(UI.getLanguageSpecificData())
411 - reinterpret_cast<const char*>(&UI);
412}
413
414static uint32_t getLargeSlotValue(ArrayRef<UnwindCode> UCs) {
415 if (UCs.size() < 3)
416 return 0;
417
418 return UCs[1].FrameOffset + (static_cast<uint32_t>(UCs[2].FrameOffset) << 16);
419}
420
421template<typename T>
422static error_code getSymbolAuxData(const COFFObjectFile *Obj,
423 const coff_symbol *Symbol, const T* &Aux) {
424 ArrayRef<uint8_t> AuxData = Obj->getSymbolAuxData(Symbol);
425 Aux = reinterpret_cast<const T*>(AuxData.data());
426 return readobj_error::success;
427}
428
Saleem Abdulrasool5dd27f42014-05-25 20:26:37 +0000429std::string COFFDumper::formatSymbol(const coff_section *Section,
430 uint64_t Offset, uint32_t Disp) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000431 std::string Buffer;
432 raw_string_ostream Str(Buffer);
433
434 StringRef Sym;
Saleem Abdulrasool5dd27f42014-05-25 20:26:37 +0000435 if (resolveSymbolName(Section, Offset, Sym)) {
Benjamin Kramer48b6a882013-07-06 20:01:46 +0000436 Str << format(" (0x%" PRIX64 ")", Offset);
Eric Christopher9cad53c2013-04-03 18:31:38 +0000437 return Str.str();
438 }
439
440 Str << Sym;
441 if (Disp > 0) {
Benjamin Kramer48b6a882013-07-06 20:01:46 +0000442 Str << format(" +0x%X (0x%" PRIX64 ")", Disp, Offset);
Eric Christopher9cad53c2013-04-03 18:31:38 +0000443 } else {
Benjamin Kramer48b6a882013-07-06 20:01:46 +0000444 Str << format(" (0x%" PRIX64 ")", Offset);
Eric Christopher9cad53c2013-04-03 18:31:38 +0000445 }
446
447 return Str.str();
448}
449
Saleem Abdulrasool5dd27f42014-05-25 20:26:37 +0000450error_code COFFDumper::resolveRelocation(const coff_section *Section,
451 uint64_t Offset,
452 const coff_section *&ResolvedSection,
453 uint64_t &ResolvedAddress) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000454 SymbolRef Sym;
Saleem Abdulrasool5dd27f42014-05-25 20:26:37 +0000455 if (error_code EC = resolveSymbol(Section, Offset, Sym))
Eric Christopher9cad53c2013-04-03 18:31:38 +0000456 return EC;
457
Saleem Abdulrasool4e08f622014-05-25 20:26:40 +0000458 if (error_code EC = Sym.getAddress(ResolvedAddr))
Eric Christopher9cad53c2013-04-03 18:31:38 +0000459 return EC;
460
Saleem Abdulrasool4e08f622014-05-25 20:26:40 +0000461 section_iterator SI(Obj->section_begin());
462 if (error_code EC = Sym.getSection(SI))
463 return EC;
464
465 ResolvedSection = Obj->getCOFFSection(*SI);
Eric Christopher9cad53c2013-04-03 18:31:38 +0000466 return object_error::success;
467}
468
469void COFFDumper::cacheRelocations() {
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000470 for (const SectionRef &S : Obj->sections()) {
471 const coff_section *Section = Obj->getCOFFSection(S);
Eric Christopher9cad53c2013-04-03 18:31:38 +0000472
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000473 for (const RelocationRef &Reloc : S.relocations())
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000474 RelocMap[Section].push_back(Reloc);
Eric Christopher9cad53c2013-04-03 18:31:38 +0000475
476 // Sort relocations by address.
477 std::sort(RelocMap[Section].begin(), RelocMap[Section].end(),
478 relocAddressLess);
479 }
480}
481
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000482void COFFDumper::printDataDirectory(uint32_t Index, const std::string &FieldName) {
483 const data_directory *Data;
484 if (Obj->getDataDirectory(Index, Data))
485 return;
486 W.printHex(FieldName + "RVA", Data->RelativeVirtualAddress);
487 W.printHex(FieldName + "Size", Data->Size);
488}
489
Eric Christopher9cad53c2013-04-03 18:31:38 +0000490void COFFDumper::printFileHeaders() {
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000491 // Print COFF header
Craig Toppere6cb63e2014-04-25 04:24:47 +0000492 const coff_file_header *COFFHeader = nullptr;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000493 if (error(Obj->getCOFFHeader(COFFHeader)))
Eric Christopher9cad53c2013-04-03 18:31:38 +0000494 return;
495
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000496 time_t TDS = COFFHeader->TimeDateStamp;
Eric Christopher9cad53c2013-04-03 18:31:38 +0000497 char FormattedTime[20] = { };
498 strftime(FormattedTime, 20, "%Y-%m-%d %H:%M:%S", gmtime(&TDS));
499
500 {
501 DictScope D(W, "ImageFileHeader");
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000502 W.printEnum ("Machine", COFFHeader->Machine,
Eric Christopher9cad53c2013-04-03 18:31:38 +0000503 makeArrayRef(ImageFileMachineType));
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000504 W.printNumber("SectionCount", COFFHeader->NumberOfSections);
505 W.printHex ("TimeDateStamp", FormattedTime, COFFHeader->TimeDateStamp);
506 W.printHex ("PointerToSymbolTable", COFFHeader->PointerToSymbolTable);
507 W.printNumber("SymbolCount", COFFHeader->NumberOfSymbols);
508 W.printNumber("OptionalHeaderSize", COFFHeader->SizeOfOptionalHeader);
509 W.printFlags ("Characteristics", COFFHeader->Characteristics,
Eric Christopher9cad53c2013-04-03 18:31:38 +0000510 makeArrayRef(ImageFileCharacteristics));
511 }
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000512
513 // Print PE header. This header does not exist if this is an object file and
514 // not an executable.
Craig Toppere6cb63e2014-04-25 04:24:47 +0000515 const pe32_header *PEHeader = nullptr;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000516 if (error(Obj->getPE32Header(PEHeader)))
517 return;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000518 if (PEHeader)
519 printPEHeader<pe32_header>(PEHeader);
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000520
Craig Toppere6cb63e2014-04-25 04:24:47 +0000521 const pe32plus_header *PEPlusHeader = nullptr;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000522 if (error(Obj->getPE32PlusHeader(PEPlusHeader)))
523 return;
524 if (PEPlusHeader)
525 printPEHeader<pe32plus_header>(PEPlusHeader);
526}
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000527
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000528template <class PEHeader>
529void COFFDumper::printPEHeader(const PEHeader *Hdr) {
530 DictScope D(W, "ImageOptionalHeader");
531 W.printNumber("MajorLinkerVersion", Hdr->MajorLinkerVersion);
532 W.printNumber("MinorLinkerVersion", Hdr->MinorLinkerVersion);
533 W.printNumber("SizeOfCode", Hdr->SizeOfCode);
534 W.printNumber("SizeOfInitializedData", Hdr->SizeOfInitializedData);
535 W.printNumber("SizeOfUninitializedData", Hdr->SizeOfUninitializedData);
536 W.printHex ("AddressOfEntryPoint", Hdr->AddressOfEntryPoint);
537 W.printHex ("BaseOfCode", Hdr->BaseOfCode);
538 printBaseOfDataField(Hdr);
539 W.printHex ("ImageBase", Hdr->ImageBase);
540 W.printNumber("SectionAlignment", Hdr->SectionAlignment);
541 W.printNumber("FileAlignment", Hdr->FileAlignment);
542 W.printNumber("MajorOperatingSystemVersion",
543 Hdr->MajorOperatingSystemVersion);
544 W.printNumber("MinorOperatingSystemVersion",
545 Hdr->MinorOperatingSystemVersion);
546 W.printNumber("MajorImageVersion", Hdr->MajorImageVersion);
547 W.printNumber("MinorImageVersion", Hdr->MinorImageVersion);
548 W.printNumber("MajorSubsystemVersion", Hdr->MajorSubsystemVersion);
549 W.printNumber("MinorSubsystemVersion", Hdr->MinorSubsystemVersion);
550 W.printNumber("SizeOfImage", Hdr->SizeOfImage);
551 W.printNumber("SizeOfHeaders", Hdr->SizeOfHeaders);
552 W.printEnum ("Subsystem", Hdr->Subsystem, makeArrayRef(PEWindowsSubsystem));
553 W.printFlags ("Subsystem", Hdr->DLLCharacteristics,
554 makeArrayRef(PEDLLCharacteristics));
555 W.printNumber("SizeOfStackReserve", Hdr->SizeOfStackReserve);
556 W.printNumber("SizeOfStackCommit", Hdr->SizeOfStackCommit);
557 W.printNumber("SizeOfHeapReserve", Hdr->SizeOfHeapReserve);
558 W.printNumber("SizeOfHeapCommit", Hdr->SizeOfHeapCommit);
559 W.printNumber("NumberOfRvaAndSize", Hdr->NumberOfRvaAndSize);
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000560
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000561 if (Hdr->NumberOfRvaAndSize > 0) {
562 DictScope D(W, "DataDirectory");
563 static const char * const directory[] = {
564 "ExportTable", "ImportTable", "ResourceTable", "ExceptionTable",
565 "CertificateTable", "BaseRelocationTable", "Debug", "Architecture",
566 "GlobalPtr", "TLSTable", "LoadConfigTable", "BoundImport", "IAT",
567 "DelayImportDescriptor", "CLRRuntimeHeader", "Reserved"
568 };
569
570 for (uint32_t i = 0; i < Hdr->NumberOfRvaAndSize; ++i) {
571 printDataDirectory(i, directory[i]);
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000572 }
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000573 }
Eric Christopher9cad53c2013-04-03 18:31:38 +0000574}
575
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000576void COFFDumper::printBaseOfDataField(const pe32_header *Hdr) {
577 W.printHex("BaseOfData", Hdr->BaseOfData);
578}
579
580void COFFDumper::printBaseOfDataField(const pe32plus_header *) {}
581
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000582void COFFDumper::printCodeViewLineTables(const SectionRef &Section) {
Timur Iskhodzhanov48703be2013-12-19 11:37:14 +0000583 StringRef Data;
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000584 if (error(Section.getContents(Data)))
585 return;
Timur Iskhodzhanov48703be2013-12-19 11:37:14 +0000586
587 SmallVector<StringRef, 10> FunctionNames;
588 StringMap<StringRef> FunctionLineTables;
589 StringRef FileIndexToStringOffsetTable;
590 StringRef StringTable;
591
592 ListScope D(W, "CodeViewLineTables");
593 {
594 DataExtractor DE(Data, true, 4);
595 uint32_t Offset = 0,
596 Magic = DE.getU32(&Offset);
597 W.printHex("Magic", Magic);
598 if (Magic != COFF::DEBUG_SECTION_MAGIC) {
599 error(object_error::parse_failed);
600 return;
601 }
602
603 bool Finished = false;
604 while (DE.isValidOffset(Offset) && !Finished) {
605 // The section consists of a number of subsection in the following format:
606 // |Type|PayloadSize|Payload...|
607 uint32_t SubSectionType = DE.getU32(&Offset),
608 PayloadSize = DE.getU32(&Offset);
609 ListScope S(W, "Subsection");
610 W.printHex("Type", SubSectionType);
611 W.printHex("PayloadSize", PayloadSize);
612 if (PayloadSize > Data.size() - Offset) {
613 error(object_error::parse_failed);
614 return;
615 }
616
617 // Print the raw contents to simplify debugging if anything goes wrong
618 // afterwards.
619 StringRef Contents = Data.substr(Offset, PayloadSize);
620 W.printBinaryBlock("Contents", Contents);
621
622 switch (SubSectionType) {
623 case COFF::DEBUG_LINE_TABLE_SUBSECTION: {
624 // Holds a PC to file:line table. Some data to parse this subsection is
625 // stored in the other subsections, so just check sanity and store the
626 // pointers for deferred processing.
627
628 if (PayloadSize < 12) {
629 // There should be at least three words to store two function
630 // relocations and size of the code.
631 error(object_error::parse_failed);
632 return;
633 }
634
635 StringRef FunctionName;
Saleem Abdulrasool5dd27f42014-05-25 20:26:37 +0000636 if (error(resolveSymbolName(Obj->getCOFFSection(Section), Offset,
637 FunctionName)))
Timur Iskhodzhanov48703be2013-12-19 11:37:14 +0000638 return;
639 W.printString("FunctionName", FunctionName);
640 if (FunctionLineTables.count(FunctionName) != 0) {
641 // Saw debug info for this function already?
642 error(object_error::parse_failed);
643 return;
644 }
645
646 FunctionLineTables[FunctionName] = Contents;
647 FunctionNames.push_back(FunctionName);
648 break;
649 }
650 case COFF::DEBUG_STRING_TABLE_SUBSECTION:
Craig Toppere6cb63e2014-04-25 04:24:47 +0000651 if (PayloadSize == 0 || StringTable.data() != nullptr ||
Timur Iskhodzhanov48703be2013-12-19 11:37:14 +0000652 Contents.back() != '\0') {
653 // Empty or duplicate or non-null-terminated subsection.
654 error(object_error::parse_failed);
655 return;
656 }
657 StringTable = Contents;
658 break;
659 case COFF::DEBUG_INDEX_SUBSECTION:
660 // Holds the translation table from file indices
661 // to offsets in the string table.
662
Craig Toppere6cb63e2014-04-25 04:24:47 +0000663 if (PayloadSize == 0 ||
664 FileIndexToStringOffsetTable.data() != nullptr) {
Timur Iskhodzhanov48703be2013-12-19 11:37:14 +0000665 // Empty or duplicate subsection.
666 error(object_error::parse_failed);
667 return;
668 }
669 FileIndexToStringOffsetTable = Contents;
670 break;
671 }
672 Offset += PayloadSize;
673
674 // Align the reading pointer by 4.
675 Offset += (-Offset) % 4;
676 }
677 }
678
679 // Dump the line tables now that we've read all the subsections and know all
680 // the required information.
681 for (unsigned I = 0, E = FunctionNames.size(); I != E; ++I) {
682 StringRef Name = FunctionNames[I];
683 ListScope S(W, "FunctionLineTable");
684 W.printString("FunctionName", Name);
685
686 DataExtractor DE(FunctionLineTables[Name], true, 4);
687 uint32_t Offset = 8; // Skip relocations.
688 uint32_t FunctionSize = DE.getU32(&Offset);
689 W.printHex("CodeSize", FunctionSize);
690 while (DE.isValidOffset(Offset)) {
691 // For each range of lines with the same filename, we have a segment
692 // in the line table. The filename string is accessed using double
693 // indirection to the string table subsection using the index subsection.
694 uint32_t OffsetInIndex = DE.getU32(&Offset),
695 SegmentLength = DE.getU32(&Offset),
696 FullSegmentSize = DE.getU32(&Offset);
697 if (FullSegmentSize != 12 + 8 * SegmentLength) {
698 error(object_error::parse_failed);
699 return;
700 }
701
702 uint32_t FilenameOffset;
703 {
704 DataExtractor SDE(FileIndexToStringOffsetTable, true, 4);
705 uint32_t OffsetInSDE = OffsetInIndex;
706 if (!SDE.isValidOffset(OffsetInSDE)) {
707 error(object_error::parse_failed);
708 return;
709 }
710 FilenameOffset = SDE.getU32(&OffsetInSDE);
711 }
712
713 if (FilenameOffset == 0 || FilenameOffset + 1 >= StringTable.size() ||
714 StringTable.data()[FilenameOffset - 1] != '\0') {
715 // Each string in an F3 subsection should be preceded by a null
716 // character.
717 error(object_error::parse_failed);
718 return;
719 }
720
721 StringRef Filename(StringTable.data() + FilenameOffset);
722 ListScope S(W, "FilenameSegment");
723 W.printString("Filename", Filename);
724 for (unsigned J = 0; J != SegmentLength && DE.isValidOffset(Offset);
725 ++J) {
726 // Then go the (PC, LineNumber) pairs. The line number is stored in the
727 // least significant 31 bits of the respective word in the table.
728 uint32_t PC = DE.getU32(&Offset),
729 LineNumber = DE.getU32(&Offset) & 0x7fffffff;
730 if (PC >= FunctionSize) {
731 error(object_error::parse_failed);
732 return;
733 }
734 char Buffer[32];
735 format("+0x%X", PC).snprint(Buffer, 32);
736 W.printNumber(Buffer, LineNumber);
737 }
738 }
739 }
740}
741
Eric Christopher9cad53c2013-04-03 18:31:38 +0000742void COFFDumper::printSections() {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000743 ListScope SectionsD(W, "Sections");
744 int SectionNumber = 0;
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000745 for (const SectionRef &Sec : Obj->sections()) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000746 ++SectionNumber;
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000747 const coff_section *Section = Obj->getCOFFSection(Sec);
Eric Christopher9cad53c2013-04-03 18:31:38 +0000748
749 StringRef Name;
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000750 if (error(Sec.getName(Name)))
751 Name = "";
Eric Christopher9cad53c2013-04-03 18:31:38 +0000752
753 DictScope D(W, "Section");
754 W.printNumber("Number", SectionNumber);
755 W.printBinary("Name", Name, Section->Name);
756 W.printHex ("VirtualSize", Section->VirtualSize);
757 W.printHex ("VirtualAddress", Section->VirtualAddress);
758 W.printNumber("RawDataSize", Section->SizeOfRawData);
759 W.printHex ("PointerToRawData", Section->PointerToRawData);
760 W.printHex ("PointerToRelocations", Section->PointerToRelocations);
761 W.printHex ("PointerToLineNumbers", Section->PointerToLinenumbers);
762 W.printNumber("RelocationCount", Section->NumberOfRelocations);
763 W.printNumber("LineNumberCount", Section->NumberOfLinenumbers);
764 W.printFlags ("Characteristics", Section->Characteristics,
765 makeArrayRef(ImageSectionCharacteristics),
766 COFF::SectionCharacteristics(0x00F00000));
767
768 if (opts::SectionRelocations) {
769 ListScope D(W, "Relocations");
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000770 for (const RelocationRef &Reloc : Sec.relocations())
771 printRelocation(Sec, Reloc);
Eric Christopher9cad53c2013-04-03 18:31:38 +0000772 }
773
774 if (opts::SectionSymbols) {
775 ListScope D(W, "Symbols");
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000776 for (const SymbolRef &Symbol : Obj->symbols()) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000777 bool Contained = false;
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000778 if (Sec.containsSymbol(Symbol, Contained) || !Contained)
Eric Christopher9cad53c2013-04-03 18:31:38 +0000779 continue;
780
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000781 printSymbol(Symbol);
Eric Christopher9cad53c2013-04-03 18:31:38 +0000782 }
783 }
784
Timur Iskhodzhanov48703be2013-12-19 11:37:14 +0000785 if (Name == ".debug$S" && opts::CodeViewLineTables)
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000786 printCodeViewLineTables(Sec);
Timur Iskhodzhanov48703be2013-12-19 11:37:14 +0000787
Eric Christopher9cad53c2013-04-03 18:31:38 +0000788 if (opts::SectionData) {
789 StringRef Data;
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000790 if (error(Sec.getContents(Data)))
791 break;
Eric Christopher9cad53c2013-04-03 18:31:38 +0000792
793 W.printBinaryBlock("SectionData", Data);
794 }
795 }
796}
797
798void COFFDumper::printRelocations() {
799 ListScope D(W, "Relocations");
800
Eric Christopher9cad53c2013-04-03 18:31:38 +0000801 int SectionNumber = 0;
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000802 for (const SectionRef &Section : Obj->sections()) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000803 ++SectionNumber;
Eric Christopher9cad53c2013-04-03 18:31:38 +0000804 StringRef Name;
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000805 if (error(Section.getName(Name)))
Eric Christopher9cad53c2013-04-03 18:31:38 +0000806 continue;
807
808 bool PrintedGroup = false;
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000809 for (const RelocationRef &Reloc : Section.relocations()) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000810 if (!PrintedGroup) {
811 W.startLine() << "Section (" << SectionNumber << ") " << Name << " {\n";
812 W.indent();
813 PrintedGroup = true;
814 }
815
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000816 printRelocation(Section, Reloc);
Eric Christopher9cad53c2013-04-03 18:31:38 +0000817 }
818
819 if (PrintedGroup) {
820 W.unindent();
821 W.startLine() << "}\n";
822 }
823 }
824}
825
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000826void COFFDumper::printRelocation(const SectionRef &Section,
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000827 const RelocationRef &Reloc) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000828 uint64_t Offset;
829 uint64_t RelocType;
830 SmallString<32> RelocName;
Eric Christopher9cad53c2013-04-03 18:31:38 +0000831 StringRef SymbolName;
832 StringRef Contents;
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000833 if (error(Reloc.getOffset(Offset)))
834 return;
835 if (error(Reloc.getType(RelocType)))
836 return;
837 if (error(Reloc.getTypeName(RelocName)))
838 return;
839 symbol_iterator Symbol = Reloc.getSymbol();
840 if (error(Symbol->getName(SymbolName)))
841 return;
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000842 if (error(Section.getContents(Contents)))
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000843 return;
Eric Christopher9cad53c2013-04-03 18:31:38 +0000844
Nico Rieckf3f0b792013-04-12 04:01:52 +0000845 if (opts::ExpandRelocs) {
846 DictScope Group(W, "Relocation");
847 W.printHex("Offset", Offset);
848 W.printNumber("Type", RelocName, RelocType);
849 W.printString("Symbol", SymbolName.size() > 0 ? SymbolName : "-");
850 } else {
851 raw_ostream& OS = W.startLine();
852 OS << W.hex(Offset)
853 << " " << RelocName
854 << " " << (SymbolName.size() > 0 ? SymbolName : "-")
855 << "\n";
856 }
Eric Christopher9cad53c2013-04-03 18:31:38 +0000857}
858
859void COFFDumper::printSymbols() {
860 ListScope Group(W, "Symbols");
861
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000862 for (const SymbolRef &Symbol : Obj->symbols())
863 printSymbol(Symbol);
Eric Christopher9cad53c2013-04-03 18:31:38 +0000864}
865
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000866void COFFDumper::printDynamicSymbols() { ListScope Group(W, "DynamicSymbols"); }
Eric Christopher9cad53c2013-04-03 18:31:38 +0000867
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000868void COFFDumper::printSymbol(const SymbolRef &Sym) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000869 DictScope D(W, "Symbol");
870
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000871 const coff_symbol *Symbol = Obj->getCOFFSymbol(Sym);
Eric Christopher9cad53c2013-04-03 18:31:38 +0000872 const coff_section *Section;
873 if (error_code EC = Obj->getSection(Symbol->SectionNumber, Section)) {
874 W.startLine() << "Invalid section number: " << EC.message() << "\n";
875 W.flush();
876 return;
877 }
878
879 StringRef SymbolName;
880 if (Obj->getSymbolName(Symbol, SymbolName))
881 SymbolName = "";
882
Nico Riecka8de6532013-04-22 08:34:46 +0000883 StringRef SectionName = "";
884 if (Section)
885 Obj->getSectionName(Section, SectionName);
Eric Christopher9cad53c2013-04-03 18:31:38 +0000886
887 W.printString("Name", SymbolName);
888 W.printNumber("Value", Symbol->Value);
889 W.printNumber("Section", SectionName, Symbol->SectionNumber);
890 W.printEnum ("BaseType", Symbol->getBaseType(), makeArrayRef(ImageSymType));
891 W.printEnum ("ComplexType", Symbol->getComplexType(),
892 makeArrayRef(ImageSymDType));
893 W.printEnum ("StorageClass", Symbol->StorageClass,
894 makeArrayRef(ImageSymClass));
895 W.printNumber("AuxSymbolCount", Symbol->NumberOfAuxSymbols);
896
897 for (unsigned I = 0; I < Symbol->NumberOfAuxSymbols; ++I) {
David Majnemerddf28f22014-03-19 04:47:47 +0000898 if (Symbol->isFunctionDefinition()) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000899 const coff_aux_function_definition *Aux;
900 if (error(getSymbolAuxData(Obj, Symbol + I, Aux)))
901 break;
902
903 DictScope AS(W, "AuxFunctionDef");
904 W.printNumber("TagIndex", Aux->TagIndex);
905 W.printNumber("TotalSize", Aux->TotalSize);
David Majnemerf3a2af52014-03-19 04:33:27 +0000906 W.printHex("PointerToLineNumber", Aux->PointerToLinenumber);
Eric Christopher9cad53c2013-04-03 18:31:38 +0000907 W.printHex("PointerToNextFunction", Aux->PointerToNextFunction);
908 W.printBinary("Unused", makeArrayRef(Aux->Unused));
909
David Majnemerddf28f22014-03-19 04:47:47 +0000910 } else if (Symbol->isWeakExternal()) {
David Majnemerf3a2af52014-03-19 04:33:27 +0000911 const coff_aux_weak_external *Aux;
Eric Christopher9cad53c2013-04-03 18:31:38 +0000912 if (error(getSymbolAuxData(Obj, Symbol + I, Aux)))
913 break;
914
915 const coff_symbol *Linked;
916 StringRef LinkedName;
917 error_code EC;
918 if ((EC = Obj->getSymbol(Aux->TagIndex, Linked)) ||
919 (EC = Obj->getSymbolName(Linked, LinkedName))) {
920 LinkedName = "";
921 error(EC);
922 }
923
924 DictScope AS(W, "AuxWeakExternal");
925 W.printNumber("Linked", LinkedName, Aux->TagIndex);
926 W.printEnum ("Search", Aux->Characteristics,
927 makeArrayRef(WeakExternalCharacteristics));
David Majnemerf3a2af52014-03-19 04:33:27 +0000928 W.printBinary("Unused", makeArrayRef(Aux->Unused));
Eric Christopher9cad53c2013-04-03 18:31:38 +0000929
David Majnemerddf28f22014-03-19 04:47:47 +0000930 } else if (Symbol->isFileRecord()) {
Saleem Abdulrasool38103702014-04-13 22:54:15 +0000931 const coff_aux_file *Aux;
Eric Christopher9cad53c2013-04-03 18:31:38 +0000932 if (error(getSymbolAuxData(Obj, Symbol + I, Aux)))
933 break;
934
Nico Rieck0ab8e602013-04-22 08:35:11 +0000935 DictScope AS(W, "AuxFileRecord");
Saleem Abdulrasoold38c6b12014-04-14 02:37:23 +0000936
937 StringRef Name(Aux->FileName,
938 Symbol->NumberOfAuxSymbols * COFF::SymbolSize);
939 W.printString("FileName", Name.rtrim(StringRef("\0", 1)));
Saleem Abdulrasool3b5e0012014-04-16 04:15:29 +0000940 break;
David Majnemerddf28f22014-03-19 04:47:47 +0000941 } else if (Symbol->isSectionDefinition()) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000942 const coff_aux_section_definition *Aux;
943 if (error(getSymbolAuxData(Obj, Symbol + I, Aux)))
944 break;
945
946 DictScope AS(W, "AuxSectionDef");
947 W.printNumber("Length", Aux->Length);
948 W.printNumber("RelocationCount", Aux->NumberOfRelocations);
949 W.printNumber("LineNumberCount", Aux->NumberOfLinenumbers);
950 W.printHex("Checksum", Aux->CheckSum);
951 W.printNumber("Number", Aux->Number);
952 W.printEnum("Selection", Aux->Selection, makeArrayRef(ImageCOMDATSelect));
953 W.printBinary("Unused", makeArrayRef(Aux->Unused));
954
Nico Riecka711dee2013-04-22 08:34:59 +0000955 if (Section && Section->Characteristics & COFF::IMAGE_SCN_LNK_COMDAT
Eric Christopher9cad53c2013-04-03 18:31:38 +0000956 && Aux->Selection == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) {
957 const coff_section *Assoc;
958 StringRef AssocName;
959 error_code EC;
960 if ((EC = Obj->getSection(Aux->Number, Assoc)) ||
961 (EC = Obj->getSectionName(Assoc, AssocName))) {
962 AssocName = "";
963 error(EC);
964 }
965
966 W.printNumber("AssocSection", AssocName, Aux->Number);
967 }
David Majnemerddf28f22014-03-19 04:47:47 +0000968 } else if (Symbol->isCLRToken()) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000969 const coff_aux_clr_token *Aux;
970 if (error(getSymbolAuxData(Obj, Symbol + I, Aux)))
971 break;
972
Nico Rieck8678acd2014-03-17 01:46:52 +0000973 const coff_symbol *ReferredSym;
974 StringRef ReferredName;
975 error_code EC;
976 if ((EC = Obj->getSymbol(Aux->SymbolTableIndex, ReferredSym)) ||
977 (EC = Obj->getSymbolName(ReferredSym, ReferredName))) {
978 ReferredName = "";
979 error(EC);
980 }
981
Eric Christopher9cad53c2013-04-03 18:31:38 +0000982 DictScope AS(W, "AuxCLRToken");
983 W.printNumber("AuxType", Aux->AuxType);
984 W.printNumber("Reserved", Aux->Reserved);
Nico Rieck8678acd2014-03-17 01:46:52 +0000985 W.printNumber("SymbolTableIndex", ReferredName, Aux->SymbolTableIndex);
David Majnemerf3a2af52014-03-19 04:33:27 +0000986 W.printBinary("Unused", makeArrayRef(Aux->Unused));
Eric Christopher9cad53c2013-04-03 18:31:38 +0000987
988 } else {
989 W.startLine() << "<unhandled auxiliary record>\n";
990 }
991 }
992}
993
994void COFFDumper::printUnwindInfo() {
995 const coff_file_header *Header;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000996 if (error(Obj->getCOFFHeader(Header)))
Eric Christopher9cad53c2013-04-03 18:31:38 +0000997 return;
998
999 ListScope D(W, "UnwindInformation");
1000 if (Header->Machine != COFF::IMAGE_FILE_MACHINE_AMD64) {
1001 W.startLine() << "Unsupported image machine type "
1002 "(currently only AMD64 is supported).\n";
1003 return;
1004 }
1005
1006 printX64UnwindInfo();
1007}
1008
1009void COFFDumper::printX64UnwindInfo() {
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001010 for (const SectionRef &Section : Obj->sections()) {
Eric Christopher9cad53c2013-04-03 18:31:38 +00001011 StringRef Name;
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001012 if (error(Section.getName(Name)))
Eric Christopher9cad53c2013-04-03 18:31:38 +00001013 continue;
1014 if (Name != ".pdata" && !Name.startswith(".pdata$"))
1015 continue;
1016
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001017 const coff_section *PData = Obj->getCOFFSection(Section);
Eric Christopher9cad53c2013-04-03 18:31:38 +00001018
1019 ArrayRef<uint8_t> Contents;
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001020 if (error(Obj->getSectionContents(PData, Contents)) || Contents.empty())
Eric Christopher9cad53c2013-04-03 18:31:38 +00001021 continue;
1022
1023 ArrayRef<RuntimeFunction> RFs(
1024 reinterpret_cast<const RuntimeFunction *>(Contents.data()),
1025 Contents.size() / sizeof(RuntimeFunction));
1026
1027 for (const RuntimeFunction *I = RFs.begin(), *E = RFs.end(); I < E; ++I) {
1028 const uint64_t OffsetInSection = std::distance(RFs.begin(), I)
1029 * sizeof(RuntimeFunction);
1030
Saleem Abdulrasool5dd27f42014-05-25 20:26:37 +00001031 printRuntimeFunction(*I, PData, OffsetInSection);
Eric Christopher9cad53c2013-04-03 18:31:38 +00001032 }
1033 }
1034}
1035
Saleem Abdulrasool5dd27f42014-05-25 20:26:37 +00001036void COFFDumper::printRuntimeFunction(const RuntimeFunction& RTF,
1037 const coff_section *Section,
1038 uint64_t SectionOffset) {
Eric Christopher9cad53c2013-04-03 18:31:38 +00001039
1040 DictScope D(W, "RuntimeFunction");
1041 W.printString("StartAddress",
Saleem Abdulrasool5dd27f42014-05-25 20:26:37 +00001042 formatSymbol(Section, SectionOffset + 0, RTF.StartAddress));
Eric Christopher9cad53c2013-04-03 18:31:38 +00001043 W.printString("EndAddress",
Saleem Abdulrasool5dd27f42014-05-25 20:26:37 +00001044 formatSymbol(Section, SectionOffset + 4, RTF.EndAddress));
Eric Christopher9cad53c2013-04-03 18:31:38 +00001045 W.printString("UnwindInfoAddress",
Saleem Abdulrasool5dd27f42014-05-25 20:26:37 +00001046 formatSymbol(Section, SectionOffset + 8, RTF.UnwindInfoOffset));
Eric Christopher9cad53c2013-04-03 18:31:38 +00001047
Craig Toppere6cb63e2014-04-25 04:24:47 +00001048 const coff_section* XData = nullptr;
Eric Christopher9cad53c2013-04-03 18:31:38 +00001049 uint64_t UnwindInfoOffset = 0;
Saleem Abdulrasool5dd27f42014-05-25 20:26:37 +00001050 if (error(getSectionFromRelocation(Section, SectionOffset + 8,
1051 XData, UnwindInfoOffset)))
Eric Christopher9cad53c2013-04-03 18:31:38 +00001052 return;
1053
1054 ArrayRef<uint8_t> XContents;
1055 if (error(Obj->getSectionContents(XData, XContents)) || XContents.empty())
1056 return;
1057
1058 UnwindInfoOffset += RTF.UnwindInfoOffset;
1059 if (UnwindInfoOffset > XContents.size())
1060 return;
1061
1062 const Win64EH::UnwindInfo *UI =
1063 reinterpret_cast<const Win64EH::UnwindInfo *>(
1064 XContents.data() + UnwindInfoOffset);
1065
Saleem Abdulrasool5dd27f42014-05-25 20:26:37 +00001066 printUnwindInfo(*UI, XData, UnwindInfoOffset);
Eric Christopher9cad53c2013-04-03 18:31:38 +00001067}
1068
Saleem Abdulrasool5dd27f42014-05-25 20:26:37 +00001069void COFFDumper::printUnwindInfo(const Win64EH::UnwindInfo& UI,
1070 const coff_section *Section,
1071 uint64_t SectionOffset) {
Eric Christopher9cad53c2013-04-03 18:31:38 +00001072 DictScope D(W, "UnwindInfo");
1073 W.printNumber("Version", UI.getVersion());
1074 W.printFlags("Flags", UI.getFlags(), makeArrayRef(UnwindFlags));
1075 W.printNumber("PrologSize", UI.PrologSize);
1076 if (UI.getFrameRegister() != 0) {
1077 W.printEnum("FrameRegister", UI.getFrameRegister(),
1078 makeArrayRef(UnwindOpInfo));
1079 W.printHex("FrameOffset", UI.getFrameOffset());
1080 } else {
1081 W.printString("FrameRegister", StringRef("-"));
1082 W.printString("FrameOffset", StringRef("-"));
1083 }
1084
1085 W.printNumber("UnwindCodeCount", UI.NumCodes);
1086 {
1087 ListScope CodesD(W, "UnwindCodes");
1088 ArrayRef<UnwindCode> UCs(&UI.UnwindCodes[0], UI.NumCodes);
1089 for (const UnwindCode *I = UCs.begin(), *E = UCs.end(); I < E; ++I) {
1090 unsigned UsedSlots = getNumUsedSlots(*I);
1091 if (UsedSlots > UCs.size()) {
1092 errs() << "Corrupt unwind data";
1093 return;
1094 }
1095 printUnwindCode(UI, ArrayRef<UnwindCode>(I, E));
1096 I += UsedSlots - 1;
1097 }
1098 }
1099
Saleem Abdulrasool5dd27f42014-05-25 20:26:37 +00001100 uint64_t LSDAOffset = SectionOffset + getOffsetOfLSDA(UI);
Eric Christopher9cad53c2013-04-03 18:31:38 +00001101 if (UI.getFlags() & (UNW_ExceptionHandler | UNW_TerminateHandler)) {
Saleem Abdulrasool5dd27f42014-05-25 20:26:37 +00001102 W.printString("Handler",
1103 formatSymbol(Section, LSDAOffset,
1104 UI.getLanguageSpecificHandlerOffset()));
Eric Christopher9cad53c2013-04-03 18:31:38 +00001105 } else if (UI.getFlags() & UNW_ChainInfo) {
1106 const RuntimeFunction *Chained = UI.getChainedFunctionEntry();
1107 if (Chained) {
1108 DictScope D(W, "Chained");
Saleem Abdulrasool5dd27f42014-05-25 20:26:37 +00001109 W.printString("StartAddress", formatSymbol(Section, LSDAOffset + 0,
1110 Chained->StartAddress));
1111 W.printString("EndAddress", formatSymbol(Section, LSDAOffset + 4,
1112 Chained->EndAddress));
1113 W.printString("UnwindInfoAddress",
1114 formatSymbol(Section, LSDAOffset + 8,
1115 Chained->UnwindInfoOffset));
Eric Christopher9cad53c2013-04-03 18:31:38 +00001116 }
1117 }
1118}
1119
1120// Prints one unwind code. Because an unwind code can occupy up to 3 slots in
1121// the unwind codes array, this function requires that the correct number of
1122// slots is provided.
1123void COFFDumper::printUnwindCode(const Win64EH::UnwindInfo& UI,
1124 ArrayRef<UnwindCode> UCs) {
1125 assert(UCs.size() >= getNumUsedSlots(UCs[0]));
1126
1127 W.startLine() << format("0x%02X: ", unsigned(UCs[0].u.CodeOffset))
1128 << getUnwindCodeTypeName(UCs[0].getUnwindOp());
1129
1130 uint32_t AllocSize = 0;
1131
1132 switch (UCs[0].getUnwindOp()) {
1133 case UOP_PushNonVol:
1134 outs() << " reg=" << getUnwindRegisterName(UCs[0].getOpInfo());
1135 break;
1136
1137 case UOP_AllocLarge:
1138 if (UCs[0].getOpInfo() == 0) {
1139 AllocSize = UCs[1].FrameOffset * 8;
1140 } else {
1141 AllocSize = getLargeSlotValue(UCs);
1142 }
1143 outs() << " size=" << AllocSize;
1144 break;
1145 case UOP_AllocSmall:
1146 outs() << " size=" << ((UCs[0].getOpInfo() + 1) * 8);
1147 break;
1148 case UOP_SetFPReg:
1149 if (UI.getFrameRegister() == 0) {
1150 outs() << " reg=<invalid>";
1151 } else {
1152 outs() << " reg=" << getUnwindRegisterName(UI.getFrameRegister())
1153 << format(", offset=0x%X", UI.getFrameOffset() * 16);
1154 }
1155 break;
1156 case UOP_SaveNonVol:
1157 outs() << " reg=" << getUnwindRegisterName(UCs[0].getOpInfo())
1158 << format(", offset=0x%X", UCs[1].FrameOffset * 8);
1159 break;
1160 case UOP_SaveNonVolBig:
1161 outs() << " reg=" << getUnwindRegisterName(UCs[0].getOpInfo())
1162 << format(", offset=0x%X", getLargeSlotValue(UCs));
1163 break;
1164 case UOP_SaveXMM128:
1165 outs() << " reg=XMM" << static_cast<uint32_t>(UCs[0].getOpInfo())
1166 << format(", offset=0x%X", UCs[1].FrameOffset * 16);
1167 break;
1168 case UOP_SaveXMM128Big:
1169 outs() << " reg=XMM" << static_cast<uint32_t>(UCs[0].getOpInfo())
1170 << format(", offset=0x%X", getLargeSlotValue(UCs));
1171 break;
1172 case UOP_PushMachFrame:
1173 outs() << " errcode=" << (UCs[0].getOpInfo() == 0 ? "no" : "yes");
1174 break;
1175 }
1176
1177 outs() << "\n";
1178}