blob: 4da254ca9a07ee1f18746d0b8c6e4989af464719 [file] [log] [blame]
Greg Claytonf754f882011-09-09 20:33:05 +00001//===-- ObjectFilePECOFF.cpp ------------------------------------*- 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#include "ObjectFilePECOFF.h"
11
12#include "llvm/Support/MachO.h"
13
14#include "lldb/Core/ArchSpec.h"
15#include "lldb/Core/DataBuffer.h"
16#include "lldb/Host/FileSpec.h"
17#include "lldb/Core/FileSpecList.h"
18#include "lldb/Core/Module.h"
Greg Claytonf4d6de62013-04-24 22:29:28 +000019#include "lldb/Core/ModuleSpec.h"
Greg Claytonf754f882011-09-09 20:33:05 +000020#include "lldb/Core/PluginManager.h"
21#include "lldb/Core/Section.h"
22#include "lldb/Core/StreamFile.h"
23#include "lldb/Core/StreamString.h"
24#include "lldb/Core/Timer.h"
25#include "lldb/Core/UUID.h"
26#include "lldb/Symbol/ObjectFile.h"
27
28static uint32_t COFFMachineToMachCPU(uint16_t machine);
29
30#define IMAGE_FILE_MACHINE_UNKNOWN 0x0000
31#define IMAGE_FILE_MACHINE_AM33 0x01d3 // Matsushita AM33
32#define IMAGE_FILE_MACHINE_AMD64 0x8664 // x64
33#define IMAGE_FILE_MACHINE_ARM 0x01c0 // ARM little endian
34#define IMAGE_FILE_MACHINE_EBC 0x0ebc // EFI byte code
35#define IMAGE_FILE_MACHINE_I386 0x014c // Intel 386 or later processors and compatible processors
36#define IMAGE_FILE_MACHINE_IA64 0x0200 // Intel Itanium processor family
37#define IMAGE_FILE_MACHINE_M32R 0x9041 // Mitsubishi M32R little endian
38#define IMAGE_FILE_MACHINE_MIPS16 0x0266 // MIPS16
39#define IMAGE_FILE_MACHINE_MIPSFPU 0x0366 // MIPS with FPU
40#define IMAGE_FILE_MACHINE_MIPSFPU16 0x0466 // MIPS16 with FPU
41#define IMAGE_FILE_MACHINE_POWERPC 0x01f0 // Power PC little endian
42#define IMAGE_FILE_MACHINE_POWERPCFP 0x01f1 // Power PC with floating point support
43#define IMAGE_FILE_MACHINE_R4000 0x0166 // MIPS little endian
44#define IMAGE_FILE_MACHINE_SH3 0x01a2 // Hitachi SH3
45#define IMAGE_FILE_MACHINE_SH3DSP 0x01a3 // Hitachi SH3 DSP
46#define IMAGE_FILE_MACHINE_SH4 0x01a6 // Hitachi SH4
47#define IMAGE_FILE_MACHINE_SH5 0x01a8 // Hitachi SH5
48#define IMAGE_FILE_MACHINE_THUMB 0x01c2 // Thumb
49#define IMAGE_FILE_MACHINE_WCEMIPSV2 0x0169 // MIPS little-endian WCE v2
50
51
52#define IMAGE_DOS_SIGNATURE 0x5A4D // MZ
53#define IMAGE_OS2_SIGNATURE 0x454E // NE
54#define IMAGE_OS2_SIGNATURE_LE 0x454C // LE
55#define IMAGE_NT_SIGNATURE 0x00004550 // PE00
56#define OPT_HEADER_MAGIC_PE32 0x010b
57#define OPT_HEADER_MAGIC_PE32_PLUS 0x020b
58
59#define IMAGE_FILE_RELOCS_STRIPPED 0x0001
60#define IMAGE_FILE_EXECUTABLE_IMAGE 0x0002
61#define IMAGE_FILE_LINE_NUMS_STRIPPED 0x0004
62#define IMAGE_FILE_LOCAL_SYMS_STRIPPED 0x0008
63#define IMAGE_FILE_AGGRESSIVE_WS_TRIM 0x0010
64#define IMAGE_FILE_LARGE_ADDRESS_AWARE 0x0020
65//#define 0x0040 // Reserved
66#define IMAGE_FILE_BYTES_REVERSED_LO 0x0080
67#define IMAGE_FILE_32BIT_MACHINE 0x0100
68#define IMAGE_FILE_DEBUG_STRIPPED 0x0200
69#define IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP 0x0400
70#define IMAGE_FILE_NET_RUN_FROM_SWAP 0x0800
71#define IMAGE_FILE_SYSTEM 0x1000
72#define IMAGE_FILE_DLL 0x2000
73#define IMAGE_FILE_UP_SYSTEM_ONLY 0x4000
74#define IMAGE_FILE_BYTES_REVERSED_HI 0x8000
75
Greg Clayton28469ca2011-09-10 01:04:42 +000076
77// Section Flags
78// The section flags in the Characteristics field of the section header indicate
79// characteristics of the section.
80#define IMAGE_SCN_TYPE_NO_PAD 0x00000008 // The section should not be padded to the next boundary. This flag is obsolete and is replaced by IMAGE_SCN_ALIGN_1BYTES. This is valid only for object files.
81#define IMAGE_SCN_CNT_CODE 0x00000020 // The section contains executable code.
82#define IMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040 // The section contains initialized data.
83#define IMAGE_SCN_CNT_UNINITIALIZED_DATA 0x00000080 // The section contains uninitialized data.
84#define IMAGE_SCN_LNK_OTHER 0x00000100 // Reserved for future use.
85#define IMAGE_SCN_LNK_INFO 0x00000200 // The section contains comments or other information. The .drectve section has this type. This is valid for object files only.
86#define IMAGE_SCN_LNK_REMOVE 0x00000800 // The section will not become part of the image. This is valid only for object files.
87#define IMAGE_SCN_LNK_COMDAT 0x00001000 // The section contains COMDAT data. For more information, see section 5.5.6, “COMDAT Sections (Object Only).” This is valid only for object files.
88#define IMAGE_SCN_GPREL 0x00008000 // The section contains data referenced through the global pointer (GP).
89#define IMAGE_SCN_MEM_PURGEABLE 0x00020000
90#define IMAGE_SCN_MEM_16BIT 0x00020000 // For ARM machine types, the section contains Thumb code. Reserved for future use with other machine types.
91#define IMAGE_SCN_MEM_LOCKED 0x00040000
92#define IMAGE_SCN_MEM_PRELOAD 0x00080000
93#define IMAGE_SCN_ALIGN_1BYTES 0x00100000 // Align data on a 1-byte boundary. Valid only for object files.
94#define IMAGE_SCN_ALIGN_2BYTES 0x00200000 // Align data on a 2-byte boundary. Valid only for object files.
95#define IMAGE_SCN_ALIGN_4BYTES 0x00300000 // Align data on a 4-byte boundary. Valid only for object files.
96#define IMAGE_SCN_ALIGN_8BYTES 0x00400000 // Align data on an 8-byte boundary. Valid only for object files.
97#define IMAGE_SCN_ALIGN_16BYTES 0x00500000 // Align data on a 16-byte boundary. Valid only for object files.
98#define IMAGE_SCN_ALIGN_32BYTES 0x00600000 // Align data on a 32-byte boundary. Valid only for object files.
99#define IMAGE_SCN_ALIGN_64BYTES 0x00700000 // Align data on a 64-byte boundary. Valid only for object files.
100#define IMAGE_SCN_ALIGN_128BYTES 0x00800000 // Align data on a 128-byte boundary. Valid only for object files.
101#define IMAGE_SCN_ALIGN_256BYTES 0x00900000 // Align data on a 256-byte boundary. Valid only for object files.
102#define IMAGE_SCN_ALIGN_512BYTES 0x00A00000 // Align data on a 512-byte boundary. Valid only for object files.
103#define IMAGE_SCN_ALIGN_1024BYTES 0x00B00000 // Align data on a 1024-byte boundary. Valid only for object files.
104#define IMAGE_SCN_ALIGN_2048BYTES 0x00C00000 // Align data on a 2048-byte boundary. Valid only for object files.
105#define IMAGE_SCN_ALIGN_4096BYTES 0x00D00000 // Align data on a 4096-byte boundary. Valid only for object files.
106#define IMAGE_SCN_ALIGN_8192BYTES 0x00E00000 // Align data on an 8192-byte boundary. Valid only for object files.
107#define IMAGE_SCN_LNK_NRELOC_OVFL 0x01000000 // The section contains extended relocations.
108#define IMAGE_SCN_MEM_DISCARDABLE 0x02000000 // The section can be discarded as needed.
109#define IMAGE_SCN_MEM_NOT_CACHED 0x04000000 // The section cannot be cached.
110#define IMAGE_SCN_MEM_NOT_PAGED 0x08000000 // The section is not pageable.
111#define IMAGE_SCN_MEM_SHARED 0x10000000 // The section can be shared in memory.
112#define IMAGE_SCN_MEM_EXECUTE 0x20000000 // The section can be executed as code.
113#define IMAGE_SCN_MEM_READ 0x40000000 // The section can be read.
114#define IMAGE_SCN_MEM_WRITE 0x80000000 // The section can be written to.
115
Greg Claytonf754f882011-09-09 20:33:05 +0000116using namespace lldb;
117using namespace lldb_private;
118
119void
120ObjectFilePECOFF::Initialize()
121{
122 PluginManager::RegisterPlugin (GetPluginNameStatic(),
123 GetPluginDescriptionStatic(),
Greg Claytonc9660542012-02-05 02:38:54 +0000124 CreateInstance,
Greg Claytonf4d6de62013-04-24 22:29:28 +0000125 CreateMemoryInstance,
126 GetModuleSpecifications);
Greg Claytonf754f882011-09-09 20:33:05 +0000127}
128
129void
130ObjectFilePECOFF::Terminate()
131{
132 PluginManager::UnregisterPlugin (CreateInstance);
133}
134
135
Greg Clayton57abc5d2013-05-10 21:47:16 +0000136lldb_private::ConstString
Greg Claytonf754f882011-09-09 20:33:05 +0000137ObjectFilePECOFF::GetPluginNameStatic()
138{
Greg Clayton57abc5d2013-05-10 21:47:16 +0000139 static ConstString g_name("pe-coff");
140 return g_name;
Greg Claytonf754f882011-09-09 20:33:05 +0000141}
142
143const char *
144ObjectFilePECOFF::GetPluginDescriptionStatic()
145{
146 return "Portable Executable and Common Object File Format object file reader (32 and 64 bit)";
147}
148
149
150ObjectFile *
Greg Clayton5ce9c562013-02-06 17:22:03 +0000151ObjectFilePECOFF::CreateInstance (const lldb::ModuleSP &module_sp,
152 DataBufferSP& data_sp,
153 lldb::offset_t data_offset,
154 const lldb_private::FileSpec* file,
155 lldb::offset_t file_offset,
156 lldb::offset_t length)
Greg Claytonf754f882011-09-09 20:33:05 +0000157{
Greg Clayton5ce9c562013-02-06 17:22:03 +0000158 if (!data_sp)
Greg Claytonf754f882011-09-09 20:33:05 +0000159 {
Greg Clayton5ce9c562013-02-06 17:22:03 +0000160 data_sp = file->MemoryMapFileContents(file_offset, length);
161 data_offset = 0;
162 }
163
164 if (ObjectFilePECOFF::MagicBytesMatch(data_sp))
165 {
166 // Update the data to contain the entire file if it doesn't already
167 if (data_sp->GetByteSize() < length)
168 data_sp = file->MemoryMapFileContents(file_offset, length);
Greg Clayton7b0992d2013-04-18 22:45:39 +0000169 std::unique_ptr<ObjectFile> objfile_ap(new ObjectFilePECOFF (module_sp, data_sp, data_offset, file, file_offset, length));
Greg Claytonf754f882011-09-09 20:33:05 +0000170 if (objfile_ap.get() && objfile_ap->ParseHeader())
171 return objfile_ap.release();
172 }
173 return NULL;
174}
175
Greg Claytonc9660542012-02-05 02:38:54 +0000176ObjectFile *
Greg Claytone72dfb32012-02-24 01:59:29 +0000177ObjectFilePECOFF::CreateMemoryInstance (const lldb::ModuleSP &module_sp,
Greg Claytonc9660542012-02-05 02:38:54 +0000178 lldb::DataBufferSP& data_sp,
179 const lldb::ProcessSP &process_sp,
180 lldb::addr_t header_addr)
181{
182 return NULL;
183}
184
Greg Claytonf4d6de62013-04-24 22:29:28 +0000185size_t
186ObjectFilePECOFF::GetModuleSpecifications (const lldb_private::FileSpec& file,
187 lldb::DataBufferSP& data_sp,
188 lldb::offset_t data_offset,
189 lldb::offset_t file_offset,
190 lldb::offset_t length,
191 lldb_private::ModuleSpecList &specs)
192{
193 return 0;
194}
195
196
Greg Claytonf754f882011-09-09 20:33:05 +0000197bool
Greg Clayton5ce9c562013-02-06 17:22:03 +0000198ObjectFilePECOFF::MagicBytesMatch (DataBufferSP& data_sp)
Greg Claytonf754f882011-09-09 20:33:05 +0000199{
Greg Clayton5ce9c562013-02-06 17:22:03 +0000200 DataExtractor data(data_sp, eByteOrderLittle, 4);
Greg Claytonc7bece562013-01-25 18:06:21 +0000201 lldb::offset_t offset = 0;
Greg Claytonf754f882011-09-09 20:33:05 +0000202 uint16_t magic = data.GetU16 (&offset);
203 return magic == IMAGE_DOS_SIGNATURE;
204}
205
206
Greg Claytone72dfb32012-02-24 01:59:29 +0000207ObjectFilePECOFF::ObjectFilePECOFF (const lldb::ModuleSP &module_sp,
Greg Clayton5ce9c562013-02-06 17:22:03 +0000208 DataBufferSP& data_sp,
209 lldb::offset_t data_offset,
Greg Claytonf754f882011-09-09 20:33:05 +0000210 const FileSpec* file,
Greg Clayton5ce9c562013-02-06 17:22:03 +0000211 lldb::offset_t file_offset,
212 lldb::offset_t length) :
213 ObjectFile (module_sp, file, file_offset, length, data_sp, data_offset),
Greg Claytonf754f882011-09-09 20:33:05 +0000214 m_dos_header (),
215 m_coff_header (),
216 m_coff_header_opt (),
217 m_sect_headers ()
218{
219 ::memset (&m_dos_header, 0, sizeof(m_dos_header));
220 ::memset (&m_coff_header, 0, sizeof(m_coff_header));
221 ::memset (&m_coff_header_opt, 0, sizeof(m_coff_header_opt));
222}
223
224
225ObjectFilePECOFF::~ObjectFilePECOFF()
226{
227}
228
229
230bool
231ObjectFilePECOFF::ParseHeader ()
232{
Greg Claytona1743492012-03-13 23:14:29 +0000233 ModuleSP module_sp(GetModule());
234 if (module_sp)
Greg Claytonf754f882011-09-09 20:33:05 +0000235 {
Greg Claytona1743492012-03-13 23:14:29 +0000236 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
237 m_sect_headers.clear();
238 m_data.SetByteOrder (eByteOrderLittle);
Greg Claytonc7bece562013-01-25 18:06:21 +0000239 lldb::offset_t offset = 0;
Greg Claytona1743492012-03-13 23:14:29 +0000240
241 if (ParseDOSHeader())
Greg Clayton28469ca2011-09-10 01:04:42 +0000242 {
Greg Claytona1743492012-03-13 23:14:29 +0000243 offset = m_dos_header.e_lfanew;
244 uint32_t pe_signature = m_data.GetU32 (&offset);
245 if (pe_signature != IMAGE_NT_SIGNATURE)
246 return false;
247 if (ParseCOFFHeader(&offset))
248 {
249 if (m_coff_header.hdrsize > 0)
250 ParseCOFFOptionalHeader(&offset);
251 ParseSectionHeaders (offset);
252 }
Greg Claytona1743492012-03-13 23:14:29 +0000253 return true;
Greg Clayton28469ca2011-09-10 01:04:42 +0000254 }
Greg Claytonf754f882011-09-09 20:33:05 +0000255 }
256 return false;
257}
258
259
260ByteOrder
261ObjectFilePECOFF::GetByteOrder () const
262{
263 return eByteOrderLittle;
264}
265
266bool
267ObjectFilePECOFF::IsExecutable() const
268{
269 return (m_coff_header.flags & IMAGE_FILE_DLL) == 0;
270}
271
Greg Claytonc7bece562013-01-25 18:06:21 +0000272uint32_t
Greg Claytonf754f882011-09-09 20:33:05 +0000273ObjectFilePECOFF::GetAddressByteSize () const
274{
275 if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32_PLUS)
276 return 8;
277 else if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32)
278 return 4;
279 return 4;
280}
281
282//----------------------------------------------------------------------
283// NeedsEndianSwap
284//
285// Return true if an endian swap needs to occur when extracting data
286// from this file.
287//----------------------------------------------------------------------
288bool
289ObjectFilePECOFF::NeedsEndianSwap() const
290{
291#if defined(__LITTLE_ENDIAN__)
292 return false;
293#else
294 return true;
295#endif
296}
297//----------------------------------------------------------------------
298// ParseDOSHeader
299//----------------------------------------------------------------------
300bool
301ObjectFilePECOFF::ParseDOSHeader ()
302{
303 bool success = false;
Greg Claytonc7bece562013-01-25 18:06:21 +0000304 lldb::offset_t offset = 0;
Greg Claytonf754f882011-09-09 20:33:05 +0000305 success = m_data.ValidOffsetForDataOfSize(0, sizeof(m_dos_header));
306
307 if (success)
308 {
309 m_dos_header.e_magic = m_data.GetU16(&offset); // Magic number
310 success = m_dos_header.e_magic == IMAGE_DOS_SIGNATURE;
311
312 if (success)
313 {
314 m_dos_header.e_cblp = m_data.GetU16(&offset); // Bytes on last page of file
315 m_dos_header.e_cp = m_data.GetU16(&offset); // Pages in file
316 m_dos_header.e_crlc = m_data.GetU16(&offset); // Relocations
317 m_dos_header.e_cparhdr = m_data.GetU16(&offset); // Size of header in paragraphs
318 m_dos_header.e_minalloc = m_data.GetU16(&offset); // Minimum extra paragraphs needed
319 m_dos_header.e_maxalloc = m_data.GetU16(&offset); // Maximum extra paragraphs needed
320 m_dos_header.e_ss = m_data.GetU16(&offset); // Initial (relative) SS value
321 m_dos_header.e_sp = m_data.GetU16(&offset); // Initial SP value
322 m_dos_header.e_csum = m_data.GetU16(&offset); // Checksum
323 m_dos_header.e_ip = m_data.GetU16(&offset); // Initial IP value
324 m_dos_header.e_cs = m_data.GetU16(&offset); // Initial (relative) CS value
325 m_dos_header.e_lfarlc = m_data.GetU16(&offset); // File address of relocation table
326 m_dos_header.e_ovno = m_data.GetU16(&offset); // Overlay number
327
328 m_dos_header.e_res[0] = m_data.GetU16(&offset); // Reserved words
329 m_dos_header.e_res[1] = m_data.GetU16(&offset); // Reserved words
330 m_dos_header.e_res[2] = m_data.GetU16(&offset); // Reserved words
331 m_dos_header.e_res[3] = m_data.GetU16(&offset); // Reserved words
332
333 m_dos_header.e_oemid = m_data.GetU16(&offset); // OEM identifier (for e_oeminfo)
334 m_dos_header.e_oeminfo = m_data.GetU16(&offset); // OEM information; e_oemid specific
335 m_dos_header.e_res2[0] = m_data.GetU16(&offset); // Reserved words
336 m_dos_header.e_res2[1] = m_data.GetU16(&offset); // Reserved words
337 m_dos_header.e_res2[2] = m_data.GetU16(&offset); // Reserved words
338 m_dos_header.e_res2[3] = m_data.GetU16(&offset); // Reserved words
339 m_dos_header.e_res2[4] = m_data.GetU16(&offset); // Reserved words
340 m_dos_header.e_res2[5] = m_data.GetU16(&offset); // Reserved words
341 m_dos_header.e_res2[6] = m_data.GetU16(&offset); // Reserved words
342 m_dos_header.e_res2[7] = m_data.GetU16(&offset); // Reserved words
343 m_dos_header.e_res2[8] = m_data.GetU16(&offset); // Reserved words
344 m_dos_header.e_res2[9] = m_data.GetU16(&offset); // Reserved words
345
346 m_dos_header.e_lfanew = m_data.GetU32(&offset); // File address of new exe header
347 }
348 }
349 if (!success)
350 memset(&m_dos_header, 0, sizeof(m_dos_header));
351 return success;
352}
353
354
355//----------------------------------------------------------------------
356// ParserCOFFHeader
357//----------------------------------------------------------------------
358bool
Greg Claytonc7bece562013-01-25 18:06:21 +0000359ObjectFilePECOFF::ParseCOFFHeader(lldb::offset_t *offset_ptr)
Greg Claytonf754f882011-09-09 20:33:05 +0000360{
361 bool success = m_data.ValidOffsetForDataOfSize (*offset_ptr, sizeof(m_coff_header));
362 if (success)
363 {
364 m_coff_header.machine = m_data.GetU16(offset_ptr);
365 m_coff_header.nsects = m_data.GetU16(offset_ptr);
366 m_coff_header.modtime = m_data.GetU32(offset_ptr);
367 m_coff_header.symoff = m_data.GetU32(offset_ptr);
368 m_coff_header.nsyms = m_data.GetU32(offset_ptr);
369 m_coff_header.hdrsize = m_data.GetU16(offset_ptr);
370 m_coff_header.flags = m_data.GetU16(offset_ptr);
371 }
372 if (!success)
373 memset(&m_coff_header, 0, sizeof(m_coff_header));
374 return success;
375}
376
377bool
Greg Claytonc7bece562013-01-25 18:06:21 +0000378ObjectFilePECOFF::ParseCOFFOptionalHeader(lldb::offset_t *offset_ptr)
Greg Claytonf754f882011-09-09 20:33:05 +0000379{
380 bool success = false;
Greg Claytonc7bece562013-01-25 18:06:21 +0000381 const lldb::offset_t end_offset = *offset_ptr + m_coff_header.hdrsize;
Greg Claytonf754f882011-09-09 20:33:05 +0000382 if (*offset_ptr < end_offset)
383 {
384 success = true;
385 m_coff_header_opt.magic = m_data.GetU16(offset_ptr);
386 m_coff_header_opt.major_linker_version = m_data.GetU8 (offset_ptr);
387 m_coff_header_opt.minor_linker_version = m_data.GetU8 (offset_ptr);
388 m_coff_header_opt.code_size = m_data.GetU32(offset_ptr);
389 m_coff_header_opt.data_size = m_data.GetU32(offset_ptr);
390 m_coff_header_opt.bss_size = m_data.GetU32(offset_ptr);
391 m_coff_header_opt.entry = m_data.GetU32(offset_ptr);
392 m_coff_header_opt.code_offset = m_data.GetU32(offset_ptr);
393
394 const uint32_t addr_byte_size = GetAddressByteSize ();
395
396 if (*offset_ptr < end_offset)
397 {
398 if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32)
399 {
400 // PE32 only
401 m_coff_header_opt.data_offset = m_data.GetU32(offset_ptr);
402 }
403 else
404 m_coff_header_opt.data_offset = 0;
405
406 if (*offset_ptr < end_offset)
407 {
408 m_coff_header_opt.image_base = m_data.GetMaxU64 (offset_ptr, addr_byte_size);
409 m_coff_header_opt.sect_alignment = m_data.GetU32(offset_ptr);
410 m_coff_header_opt.file_alignment = m_data.GetU32(offset_ptr);
411 m_coff_header_opt.major_os_system_version = m_data.GetU16(offset_ptr);
412 m_coff_header_opt.minor_os_system_version = m_data.GetU16(offset_ptr);
413 m_coff_header_opt.major_image_version = m_data.GetU16(offset_ptr);
414 m_coff_header_opt.minor_image_version = m_data.GetU16(offset_ptr);
415 m_coff_header_opt.major_subsystem_version = m_data.GetU16(offset_ptr);
416 m_coff_header_opt.minor_subsystem_version = m_data.GetU16(offset_ptr);
417 m_coff_header_opt.reserved1 = m_data.GetU32(offset_ptr);
418 m_coff_header_opt.image_size = m_data.GetU32(offset_ptr);
419 m_coff_header_opt.header_size = m_data.GetU32(offset_ptr);
Greg Clayton28469ca2011-09-10 01:04:42 +0000420 m_coff_header_opt.checksum = m_data.GetU32(offset_ptr);
Greg Claytonf754f882011-09-09 20:33:05 +0000421 m_coff_header_opt.subsystem = m_data.GetU16(offset_ptr);
422 m_coff_header_opt.dll_flags = m_data.GetU16(offset_ptr);
423 m_coff_header_opt.stack_reserve_size = m_data.GetMaxU64 (offset_ptr, addr_byte_size);
424 m_coff_header_opt.stack_commit_size = m_data.GetMaxU64 (offset_ptr, addr_byte_size);
425 m_coff_header_opt.heap_reserve_size = m_data.GetMaxU64 (offset_ptr, addr_byte_size);
426 m_coff_header_opt.heap_commit_size = m_data.GetMaxU64 (offset_ptr, addr_byte_size);
427 m_coff_header_opt.loader_flags = m_data.GetU32(offset_ptr);
428 uint32_t num_data_dir_entries = m_data.GetU32(offset_ptr);
429 m_coff_header_opt.data_dirs.clear();
430 m_coff_header_opt.data_dirs.resize(num_data_dir_entries);
431 uint32_t i;
432 for (i=0; i<num_data_dir_entries; i++)
433 {
434 m_coff_header_opt.data_dirs[i].vmaddr = m_data.GetU32(offset_ptr);
435 m_coff_header_opt.data_dirs[i].vmsize = m_data.GetU32(offset_ptr);
436 }
437 }
438 }
439 }
440 // Make sure we are on track for section data which follows
441 *offset_ptr = end_offset;
442 return success;
443}
444
445
446//----------------------------------------------------------------------
447// ParseSectionHeaders
448//----------------------------------------------------------------------
449bool
450ObjectFilePECOFF::ParseSectionHeaders (uint32_t section_header_data_offset)
451{
452 const uint32_t nsects = m_coff_header.nsects;
453 m_sect_headers.clear();
454
455 if (nsects > 0)
456 {
457 const uint32_t addr_byte_size = GetAddressByteSize ();
458 const size_t section_header_byte_size = nsects * sizeof(section_header_t);
459 DataBufferSP section_header_data_sp(m_file.ReadFileContents (section_header_data_offset, section_header_byte_size));
460 DataExtractor section_header_data (section_header_data_sp, GetByteOrder(), addr_byte_size);
461
Greg Claytonc7bece562013-01-25 18:06:21 +0000462 lldb::offset_t offset = 0;
Greg Claytonf754f882011-09-09 20:33:05 +0000463 if (section_header_data.ValidOffsetForDataOfSize (offset, section_header_byte_size))
464 {
465 m_sect_headers.resize(nsects);
466
467 for (uint32_t idx = 0; idx<nsects; ++idx)
468 {
469 const void *name_data = section_header_data.GetData(&offset, 8);
470 if (name_data)
471 {
472 memcpy(m_sect_headers[idx].name, name_data, 8);
473 m_sect_headers[idx].vmsize = section_header_data.GetU32(&offset);
474 m_sect_headers[idx].vmaddr = section_header_data.GetU32(&offset);
475 m_sect_headers[idx].size = section_header_data.GetU32(&offset);
476 m_sect_headers[idx].offset = section_header_data.GetU32(&offset);
477 m_sect_headers[idx].reloff = section_header_data.GetU32(&offset);
478 m_sect_headers[idx].lineoff = section_header_data.GetU32(&offset);
479 m_sect_headers[idx].nreloc = section_header_data.GetU16(&offset);
480 m_sect_headers[idx].nline = section_header_data.GetU16(&offset);
481 m_sect_headers[idx].flags = section_header_data.GetU32(&offset);
482 }
483 }
484 }
485 }
486
487 return m_sect_headers.empty() == false;
488}
489
490bool
491ObjectFilePECOFF::GetSectionName(std::string& sect_name, const section_header_t& sect)
492{
493 if (sect.name[0] == '/')
494 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000495 lldb::offset_t stroff = strtoul(&sect.name[1], NULL, 10);
496 lldb::offset_t string_file_offset = m_coff_header.symoff + (m_coff_header.nsyms * 18) + stroff;
Greg Claytonf754f882011-09-09 20:33:05 +0000497 const char *name = m_data.GetCStr (&string_file_offset);
498 if (name)
499 {
500 sect_name = name;
501 return true;
502 }
503
504 return false;
505 }
506 sect_name = sect.name;
507 return true;
508}
509
510//----------------------------------------------------------------------
511// GetNListSymtab
512//----------------------------------------------------------------------
513Symtab *
514ObjectFilePECOFF::GetSymtab()
515{
Greg Claytona1743492012-03-13 23:14:29 +0000516 ModuleSP module_sp(GetModule());
517 if (module_sp)
Greg Claytonf754f882011-09-09 20:33:05 +0000518 {
Greg Claytona1743492012-03-13 23:14:29 +0000519 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
520 if (m_symtab_ap.get() == NULL)
Greg Claytonf754f882011-09-09 20:33:05 +0000521 {
Greg Claytona1743492012-03-13 23:14:29 +0000522 SectionList *sect_list = GetSectionList();
523 m_symtab_ap.reset(new Symtab(this));
524 Mutex::Locker symtab_locker (m_symtab_ap->GetMutex());
Greg Claytonf754f882011-09-09 20:33:05 +0000525
Greg Claytona1743492012-03-13 23:14:29 +0000526 const uint32_t num_syms = m_coff_header.nsyms;
527
528 if (num_syms > 0 && m_coff_header.symoff > 0)
529 {
Greg Clayton0076e712013-06-18 00:08:58 +0000530 const uint32_t symbol_size = 18;
Greg Claytona1743492012-03-13 23:14:29 +0000531 const uint32_t addr_byte_size = GetAddressByteSize ();
532 const size_t symbol_data_size = num_syms * symbol_size;
533 // Include the 4 bytes string table size at the end of the symbols
534 DataBufferSP symtab_data_sp(m_file.ReadFileContents (m_coff_header.symoff, symbol_data_size + 4));
535 DataExtractor symtab_data (symtab_data_sp, GetByteOrder(), addr_byte_size);
Greg Claytonc7bece562013-01-25 18:06:21 +0000536 lldb::offset_t offset = symbol_data_size;
Greg Claytona1743492012-03-13 23:14:29 +0000537 const uint32_t strtab_size = symtab_data.GetU32 (&offset);
Greg Clayton0076e712013-06-18 00:08:58 +0000538 DataBufferSP strtab_data_sp(m_file.ReadFileContents (m_coff_header.symoff + symbol_data_size, strtab_size));
Greg Claytona1743492012-03-13 23:14:29 +0000539 DataExtractor strtab_data (strtab_data_sp, GetByteOrder(), addr_byte_size);
540
Greg Clayton0076e712013-06-18 00:08:58 +0000541 // First 4 bytes should be zeroed after strtab_size has been read,
542 // because it is used as offset 0 to encode a NULL string.
543 uint32_t* strtab_data_start = (uint32_t*)strtab_data_sp->GetBytes();
544 strtab_data_start[0] = 0;
545
Greg Claytona1743492012-03-13 23:14:29 +0000546 offset = 0;
547 std::string symbol_name;
548 Symbol *symbols = m_symtab_ap->Resize (num_syms);
549 for (uint32_t i=0; i<num_syms; ++i)
550 {
551 coff_symbol_t symbol;
552 const uint32_t symbol_offset = offset;
553 const char *symbol_name_cstr = NULL;
554 // If the first 4 bytes of the symbol string are zero, then we
555 // it is followed by a 4 byte string table offset. Else these
556 // 8 bytes contain the symbol name
557 if (symtab_data.GetU32 (&offset) == 0)
558 {
559 // Long string that doesn't fit into the symbol table name,
560 // so now we must read the 4 byte string table offset
561 uint32_t strtab_offset = symtab_data.GetU32 (&offset);
562 symbol_name_cstr = strtab_data.PeekCStr (strtab_offset);
563 symbol_name.assign (symbol_name_cstr);
564 }
565 else
566 {
567 // Short string that fits into the symbol table name which is 8 bytes
568 offset += sizeof(symbol.name) - 4; // Skip remaining
569 symbol_name_cstr = symtab_data.PeekCStr (symbol_offset);
570 if (symbol_name_cstr == NULL)
571 break;
572 symbol_name.assign (symbol_name_cstr, sizeof(symbol.name));
573 }
574 symbol.value = symtab_data.GetU32 (&offset);
575 symbol.sect = symtab_data.GetU16 (&offset);
576 symbol.type = symtab_data.GetU16 (&offset);
577 symbol.storage = symtab_data.GetU8 (&offset);
578 symbol.naux = symtab_data.GetU8 (&offset);
Greg Clayton037520e2012-07-18 23:18:10 +0000579 symbols[i].GetMangled ().SetValue (ConstString(symbol_name.c_str()));
Greg Clayton0076e712013-06-18 00:08:58 +0000580 if ((int16_t)symbol.sect >= 1)
581 {
582 Address symbol_addr(sect_list->GetSectionAtIndex(symbol.sect-1), symbol.value);
583 symbols[i].GetAddress() = symbol_addr;
584 }
Greg Claytona1743492012-03-13 23:14:29 +0000585
586 if (symbol.naux > 0)
Greg Clayton0076e712013-06-18 00:08:58 +0000587 {
Greg Claytona1743492012-03-13 23:14:29 +0000588 i += symbol.naux;
Greg Clayton0076e712013-06-18 00:08:58 +0000589 offset += symbol_size;
590 }
Greg Claytona1743492012-03-13 23:14:29 +0000591 }
592
593 }
Greg Claytonf754f882011-09-09 20:33:05 +0000594 }
595 }
596 return m_symtab_ap.get();
597
598}
599
600SectionList *
601ObjectFilePECOFF::GetSectionList()
602{
Greg Claytona1743492012-03-13 23:14:29 +0000603 ModuleSP module_sp(GetModule());
604 if (module_sp)
Greg Claytonf754f882011-09-09 20:33:05 +0000605 {
Greg Claytona1743492012-03-13 23:14:29 +0000606 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
607 if (m_sections_ap.get() == NULL)
Greg Claytonf754f882011-09-09 20:33:05 +0000608 {
Greg Claytona1743492012-03-13 23:14:29 +0000609 m_sections_ap.reset(new SectionList());
610 const uint32_t nsects = m_sect_headers.size();
611 ModuleSP module_sp (GetModule());
612 for (uint32_t idx = 0; idx<nsects; ++idx)
Greg Clayton28469ca2011-09-10 01:04:42 +0000613 {
Greg Claytona1743492012-03-13 23:14:29 +0000614 std::string sect_name;
615 GetSectionName (sect_name, m_sect_headers[idx]);
616 ConstString const_sect_name (sect_name.c_str());
617 static ConstString g_code_sect_name (".code");
618 static ConstString g_CODE_sect_name ("CODE");
619 static ConstString g_data_sect_name (".data");
620 static ConstString g_DATA_sect_name ("DATA");
621 static ConstString g_bss_sect_name (".bss");
622 static ConstString g_BSS_sect_name ("BSS");
623 static ConstString g_debug_sect_name (".debug");
624 static ConstString g_reloc_sect_name (".reloc");
625 static ConstString g_stab_sect_name (".stab");
626 static ConstString g_stabstr_sect_name (".stabstr");
Greg Clayton0076e712013-06-18 00:08:58 +0000627 static ConstString g_sect_name_dwarf_debug_abbrev (".debug_abbrev");
628 static ConstString g_sect_name_dwarf_debug_aranges (".debug_aranges");
629 static ConstString g_sect_name_dwarf_debug_frame (".debug_frame");
630 static ConstString g_sect_name_dwarf_debug_info (".debug_info");
631 static ConstString g_sect_name_dwarf_debug_line (".debug_line");
632 static ConstString g_sect_name_dwarf_debug_loc (".debug_loc");
633 static ConstString g_sect_name_dwarf_debug_macinfo (".debug_macinfo");
634 static ConstString g_sect_name_dwarf_debug_pubnames (".debug_pubnames");
635 static ConstString g_sect_name_dwarf_debug_pubtypes (".debug_pubtypes");
636 static ConstString g_sect_name_dwarf_debug_ranges (".debug_ranges");
637 static ConstString g_sect_name_dwarf_debug_str (".debug_str");
638 static ConstString g_sect_name_eh_frame (".eh_frame");
Greg Claytona1743492012-03-13 23:14:29 +0000639 SectionType section_type = eSectionTypeOther;
640 if (m_sect_headers[idx].flags & IMAGE_SCN_CNT_CODE &&
641 ((const_sect_name == g_code_sect_name) || (const_sect_name == g_CODE_sect_name)))
642 {
643 section_type = eSectionTypeCode;
644 }
645 else if (m_sect_headers[idx].flags & IMAGE_SCN_CNT_INITIALIZED_DATA &&
646 ((const_sect_name == g_data_sect_name) || (const_sect_name == g_DATA_sect_name)))
647 {
Greg Clayton28469ca2011-09-10 01:04:42 +0000648 section_type = eSectionTypeData;
Greg Claytona1743492012-03-13 23:14:29 +0000649 }
650 else if (m_sect_headers[idx].flags & IMAGE_SCN_CNT_UNINITIALIZED_DATA &&
651 ((const_sect_name == g_bss_sect_name) || (const_sect_name == g_BSS_sect_name)))
652 {
653 if (m_sect_headers[idx].size == 0)
654 section_type = eSectionTypeZeroFill;
655 else
656 section_type = eSectionTypeData;
657 }
658 else if (const_sect_name == g_debug_sect_name)
659 {
660 section_type = eSectionTypeDebug;
661 }
662 else if (const_sect_name == g_stabstr_sect_name)
663 {
664 section_type = eSectionTypeDataCString;
665 }
666 else if (const_sect_name == g_reloc_sect_name)
667 {
668 section_type = eSectionTypeOther;
669 }
Greg Clayton0076e712013-06-18 00:08:58 +0000670 else if (const_sect_name == g_sect_name_dwarf_debug_abbrev) section_type = eSectionTypeDWARFDebugAbbrev;
671 else if (const_sect_name == g_sect_name_dwarf_debug_aranges) section_type = eSectionTypeDWARFDebugAranges;
672 else if (const_sect_name == g_sect_name_dwarf_debug_frame) section_type = eSectionTypeDWARFDebugFrame;
673 else if (const_sect_name == g_sect_name_dwarf_debug_info) section_type = eSectionTypeDWARFDebugInfo;
674 else if (const_sect_name == g_sect_name_dwarf_debug_line) section_type = eSectionTypeDWARFDebugLine;
675 else if (const_sect_name == g_sect_name_dwarf_debug_loc) section_type = eSectionTypeDWARFDebugLoc;
676 else if (const_sect_name == g_sect_name_dwarf_debug_macinfo) section_type = eSectionTypeDWARFDebugMacInfo;
677 else if (const_sect_name == g_sect_name_dwarf_debug_pubnames) section_type = eSectionTypeDWARFDebugPubNames;
678 else if (const_sect_name == g_sect_name_dwarf_debug_pubtypes) section_type = eSectionTypeDWARFDebugPubTypes;
679 else if (const_sect_name == g_sect_name_dwarf_debug_ranges) section_type = eSectionTypeDWARFDebugRanges;
680 else if (const_sect_name == g_sect_name_dwarf_debug_str) section_type = eSectionTypeDWARFDebugStr;
681 else if (const_sect_name == g_sect_name_eh_frame) section_type = eSectionTypeEHFrame;
Greg Claytona1743492012-03-13 23:14:29 +0000682 else if (m_sect_headers[idx].flags & IMAGE_SCN_CNT_CODE)
683 {
684 section_type = eSectionTypeCode;
685 }
686 else if (m_sect_headers[idx].flags & IMAGE_SCN_CNT_INITIALIZED_DATA)
687 {
Greg Clayton28469ca2011-09-10 01:04:42 +0000688 section_type = eSectionTypeData;
Greg Claytona1743492012-03-13 23:14:29 +0000689 }
690 else if (m_sect_headers[idx].flags & IMAGE_SCN_CNT_UNINITIALIZED_DATA)
691 {
692 if (m_sect_headers[idx].size == 0)
693 section_type = eSectionTypeZeroFill;
694 else
695 section_type = eSectionTypeData;
696 }
697
698 // Use a segment ID of the segment index shifted left by 8 so they
699 // never conflict with any of the sections.
700 SectionSP section_sp (new Section (module_sp, // Module to which this section belongs
701 idx + 1, // Section ID is the 1 based segment index shifted right by 8 bits as not to collide with any of the 256 section IDs that are possible
702 const_sect_name, // Name of this section
703 section_type, // This section is a container of other sections.
Greg Clayton0076e712013-06-18 00:08:58 +0000704 m_coff_header_opt.image_base + m_sect_headers[idx].vmaddr, // File VM address == addresses as they are found in the object file
Greg Claytona1743492012-03-13 23:14:29 +0000705 m_sect_headers[idx].vmsize, // VM size in bytes of this section
706 m_sect_headers[idx].offset, // Offset to the data for this section in the file
707 m_sect_headers[idx].size, // Size in bytes of this section as found in the the file
708 m_sect_headers[idx].flags)); // Flags for this section
709
710 //section_sp->SetIsEncrypted (segment_is_encrypted);
711
712 m_sections_ap->AddSection(section_sp);
Greg Clayton28469ca2011-09-10 01:04:42 +0000713 }
Sean Callanan56775362012-06-08 02:16:08 +0000714
715 m_sections_ap->Finalize(); // Now that we're done adding sections, finalize to build fast-lookup caches
Greg Claytonf754f882011-09-09 20:33:05 +0000716 }
717 }
718 return m_sections_ap.get();
719}
720
721bool
722ObjectFilePECOFF::GetUUID (UUID* uuid)
723{
724 return false;
725}
726
727uint32_t
728ObjectFilePECOFF::GetDependentModules (FileSpecList& files)
729{
730 return 0;
731}
732
733
734//----------------------------------------------------------------------
735// Dump
736//
737// Dump the specifics of the runtime file container (such as any headers
738// segments, sections, etc).
739//----------------------------------------------------------------------
740void
741ObjectFilePECOFF::Dump(Stream *s)
742{
Greg Claytona1743492012-03-13 23:14:29 +0000743 ModuleSP module_sp(GetModule());
744 if (module_sp)
Greg Claytonf754f882011-09-09 20:33:05 +0000745 {
Greg Claytona1743492012-03-13 23:14:29 +0000746 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
747 s->Printf("%p: ", this);
748 s->Indent();
749 s->PutCString("ObjectFilePECOFF");
750
751 ArchSpec header_arch;
752 GetArchitecture (header_arch);
753
754 *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
755
756 if (m_sections_ap.get())
757 m_sections_ap->Dump(s, NULL, true, UINT32_MAX);
758
759 if (m_symtab_ap.get())
760 m_symtab_ap->Dump(s, NULL, eSortOrderNone);
761
762 if (m_dos_header.e_magic)
763 DumpDOSHeader (s, m_dos_header);
764 if (m_coff_header.machine)
765 {
766 DumpCOFFHeader (s, m_coff_header);
767 if (m_coff_header.hdrsize)
768 DumpOptCOFFHeader (s, m_coff_header_opt);
769 }
770 s->EOL();
771 DumpSectionHeaders(s);
772 s->EOL();
Greg Claytonf754f882011-09-09 20:33:05 +0000773 }
Greg Claytonf754f882011-09-09 20:33:05 +0000774}
775
776//----------------------------------------------------------------------
777// DumpDOSHeader
778//
779// Dump the MS-DOS header to the specified output stream
780//----------------------------------------------------------------------
781void
782ObjectFilePECOFF::DumpDOSHeader(Stream *s, const dos_header_t& header)
783{
784 s->PutCString ("MSDOS Header\n");
785 s->Printf (" e_magic = 0x%4.4x\n", header.e_magic);
786 s->Printf (" e_cblp = 0x%4.4x\n", header.e_cblp);
787 s->Printf (" e_cp = 0x%4.4x\n", header.e_cp);
788 s->Printf (" e_crlc = 0x%4.4x\n", header.e_crlc);
789 s->Printf (" e_cparhdr = 0x%4.4x\n", header.e_cparhdr);
790 s->Printf (" e_minalloc = 0x%4.4x\n", header.e_minalloc);
791 s->Printf (" e_maxalloc = 0x%4.4x\n", header.e_maxalloc);
792 s->Printf (" e_ss = 0x%4.4x\n", header.e_ss);
793 s->Printf (" e_sp = 0x%4.4x\n", header.e_sp);
794 s->Printf (" e_csum = 0x%4.4x\n", header.e_csum);
795 s->Printf (" e_ip = 0x%4.4x\n", header.e_ip);
796 s->Printf (" e_cs = 0x%4.4x\n", header.e_cs);
797 s->Printf (" e_lfarlc = 0x%4.4x\n", header.e_lfarlc);
798 s->Printf (" e_ovno = 0x%4.4x\n", header.e_ovno);
799 s->Printf (" e_res[4] = { 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x }\n",
800 header.e_res[0],
801 header.e_res[1],
802 header.e_res[2],
803 header.e_res[3]);
804 s->Printf (" e_oemid = 0x%4.4x\n", header.e_oemid);
805 s->Printf (" e_oeminfo = 0x%4.4x\n", header.e_oeminfo);
806 s->Printf (" e_res2[10] = { 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x }\n",
807 header.e_res2[0],
808 header.e_res2[1],
809 header.e_res2[2],
810 header.e_res2[3],
811 header.e_res2[4],
812 header.e_res2[5],
813 header.e_res2[6],
814 header.e_res2[7],
815 header.e_res2[8],
816 header.e_res2[9]);
817 s->Printf (" e_lfanew = 0x%8.8x\n", header.e_lfanew);
818}
819
820//----------------------------------------------------------------------
821// DumpCOFFHeader
822//
823// Dump the COFF header to the specified output stream
824//----------------------------------------------------------------------
825void
826ObjectFilePECOFF::DumpCOFFHeader(Stream *s, const coff_header_t& header)
827{
828 s->PutCString ("COFF Header\n");
829 s->Printf (" machine = 0x%4.4x\n", header.machine);
830 s->Printf (" nsects = 0x%4.4x\n", header.nsects);
831 s->Printf (" modtime = 0x%8.8x\n", header.modtime);
832 s->Printf (" symoff = 0x%8.8x\n", header.symoff);
833 s->Printf (" nsyms = 0x%8.8x\n", header.nsyms);
834 s->Printf (" hdrsize = 0x%4.4x\n", header.hdrsize);
835}
836
837//----------------------------------------------------------------------
838// DumpOptCOFFHeader
839//
840// Dump the optional COFF header to the specified output stream
841//----------------------------------------------------------------------
842void
843ObjectFilePECOFF::DumpOptCOFFHeader(Stream *s, const coff_opt_header_t& header)
844{
845 s->PutCString ("Optional COFF Header\n");
846 s->Printf (" magic = 0x%4.4x\n", header.magic);
847 s->Printf (" major_linker_version = 0x%2.2x\n", header.major_linker_version);
848 s->Printf (" minor_linker_version = 0x%2.2x\n", header.minor_linker_version);
849 s->Printf (" code_size = 0x%8.8x\n", header.code_size);
850 s->Printf (" data_size = 0x%8.8x\n", header.data_size);
851 s->Printf (" bss_size = 0x%8.8x\n", header.bss_size);
852 s->Printf (" entry = 0x%8.8x\n", header.entry);
853 s->Printf (" code_offset = 0x%8.8x\n", header.code_offset);
854 s->Printf (" data_offset = 0x%8.8x\n", header.data_offset);
Daniel Malead01b2952012-11-29 21:49:15 +0000855 s->Printf (" image_base = 0x%16.16" PRIx64 "\n", header.image_base);
Greg Claytonf754f882011-09-09 20:33:05 +0000856 s->Printf (" sect_alignment = 0x%8.8x\n", header.sect_alignment);
857 s->Printf (" file_alignment = 0x%8.8x\n", header.file_alignment);
858 s->Printf (" major_os_system_version = 0x%4.4x\n", header.major_os_system_version);
859 s->Printf (" minor_os_system_version = 0x%4.4x\n", header.minor_os_system_version);
860 s->Printf (" major_image_version = 0x%4.4x\n", header.major_image_version);
861 s->Printf (" minor_image_version = 0x%4.4x\n", header.minor_image_version);
862 s->Printf (" major_subsystem_version = 0x%4.4x\n", header.major_subsystem_version);
863 s->Printf (" minor_subsystem_version = 0x%4.4x\n", header.minor_subsystem_version);
864 s->Printf (" reserved1 = 0x%8.8x\n", header.reserved1);
865 s->Printf (" image_size = 0x%8.8x\n", header.image_size);
866 s->Printf (" header_size = 0x%8.8x\n", header.header_size);
Greg Clayton28469ca2011-09-10 01:04:42 +0000867 s->Printf (" checksum = 0x%8.8x\n", header.checksum);
Greg Claytonf754f882011-09-09 20:33:05 +0000868 s->Printf (" subsystem = 0x%4.4x\n", header.subsystem);
869 s->Printf (" dll_flags = 0x%4.4x\n", header.dll_flags);
Daniel Malead01b2952012-11-29 21:49:15 +0000870 s->Printf (" stack_reserve_size = 0x%16.16" PRIx64 "\n", header.stack_reserve_size);
871 s->Printf (" stack_commit_size = 0x%16.16" PRIx64 "\n", header.stack_commit_size);
872 s->Printf (" heap_reserve_size = 0x%16.16" PRIx64 "\n", header.heap_reserve_size);
873 s->Printf (" heap_commit_size = 0x%16.16" PRIx64 "\n", header.heap_commit_size);
Greg Claytonf754f882011-09-09 20:33:05 +0000874 s->Printf (" loader_flags = 0x%8.8x\n", header.loader_flags);
875 s->Printf (" num_data_dir_entries = 0x%8.8zx\n", header.data_dirs.size());
876 uint32_t i;
877 for (i=0; i<header.data_dirs.size(); i++)
878 {
Greg Clayton28469ca2011-09-10 01:04:42 +0000879 s->Printf (" data_dirs[%2u] vmaddr = 0x%8.8x, vmsize = 0x%8.8x\n",
Greg Claytonf754f882011-09-09 20:33:05 +0000880 i,
881 header.data_dirs[i].vmaddr,
882 header.data_dirs[i].vmsize);
883 }
884}
885//----------------------------------------------------------------------
886// DumpSectionHeader
887//
888// Dump a single ELF section header to the specified output stream
889//----------------------------------------------------------------------
890void
891ObjectFilePECOFF::DumpSectionHeader(Stream *s, const section_header_t& sh)
892{
893 std::string name;
894 GetSectionName(name, sh);
895 s->Printf ("%-16s 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x 0x%4.4x 0x%4.4x 0x%8.8x\n",
896 name.c_str(),
Greg Claytonf754f882011-09-09 20:33:05 +0000897 sh.vmaddr,
Greg Clayton28469ca2011-09-10 01:04:42 +0000898 sh.vmsize,
Greg Claytonf754f882011-09-09 20:33:05 +0000899 sh.offset,
Greg Clayton28469ca2011-09-10 01:04:42 +0000900 sh.size,
Greg Claytonf754f882011-09-09 20:33:05 +0000901 sh.reloff,
902 sh.lineoff,
903 sh.nreloc,
904 sh.nline,
905 sh.flags);
906}
907
908
909//----------------------------------------------------------------------
910// DumpSectionHeaders
911//
912// Dump all of the ELF section header to the specified output stream
913//----------------------------------------------------------------------
914void
915ObjectFilePECOFF::DumpSectionHeaders(Stream *s)
916{
917
918 s->PutCString ("Section Headers\n");
Greg Clayton28469ca2011-09-10 01:04:42 +0000919 s->PutCString ("IDX name vm addr vm size file off file size reloc off line off nreloc nline flags\n");
920 s->PutCString ("==== ---------------- ---------- ---------- ---------- ---------- ---------- ---------- ------ ------ ----------\n");
Greg Claytonf754f882011-09-09 20:33:05 +0000921
922 uint32_t idx = 0;
923 SectionHeaderCollIter pos, end = m_sect_headers.end();
924
925 for (pos = m_sect_headers.begin(); pos != end; ++pos, ++idx)
926 {
Greg Clayton28469ca2011-09-10 01:04:42 +0000927 s->Printf ("[%2u] ", idx);
Greg Claytonf754f882011-09-09 20:33:05 +0000928 ObjectFilePECOFF::DumpSectionHeader(s, *pos);
929 }
930}
931
932static bool
933COFFMachineToMachCPU (uint16_t machine, ArchSpec &arch)
934{
935 switch (machine)
936 {
937 case IMAGE_FILE_MACHINE_AMD64:
938 case IMAGE_FILE_MACHINE_IA64:
939 arch.SetArchitecture (eArchTypeMachO,
940 llvm::MachO::CPUTypeX86_64,
941 llvm::MachO::CPUSubType_X86_64_ALL);
942 return true;
943
944 case IMAGE_FILE_MACHINE_I386:
945 arch.SetArchitecture (eArchTypeMachO,
946 llvm::MachO::CPUTypeI386,
947 llvm::MachO::CPUSubType_I386_ALL);
948 return true;
949
950 case IMAGE_FILE_MACHINE_POWERPC:
951 case IMAGE_FILE_MACHINE_POWERPCFP:
952 arch.SetArchitecture (eArchTypeMachO,
953 llvm::MachO::CPUTypePowerPC,
954 llvm::MachO::CPUSubType_POWERPC_ALL);
955 return true;
956 case IMAGE_FILE_MACHINE_ARM:
957 case IMAGE_FILE_MACHINE_THUMB:
958 arch.SetArchitecture (eArchTypeMachO,
959 llvm::MachO::CPUTypeARM,
960 llvm::MachO::CPUSubType_ARM_V7);
961 return true;
962 }
963 return false;
964}
965bool
966ObjectFilePECOFF::GetArchitecture (ArchSpec &arch)
967{
968 // For index zero return our cpu type
969 return COFFMachineToMachCPU (m_coff_header.machine, arch);
970}
971
972ObjectFile::Type
973ObjectFilePECOFF::CalculateType()
974{
975 if (m_coff_header.machine != 0)
976 {
977 if ((m_coff_header.flags & IMAGE_FILE_DLL) == 0)
978 return eTypeExecutable;
979 else
980 return eTypeSharedLibrary;
981 }
982 return eTypeExecutable;
983}
984
985ObjectFile::Strata
986ObjectFilePECOFF::CalculateStrata()
987{
988 return eStrataUser;
989}
990//------------------------------------------------------------------
991// PluginInterface protocol
992//------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +0000993ConstString
Greg Claytonf754f882011-09-09 20:33:05 +0000994ObjectFilePECOFF::GetPluginName()
995{
Greg Claytonf754f882011-09-09 20:33:05 +0000996 return GetPluginNameStatic();
997}
998
999uint32_t
1000ObjectFilePECOFF::GetPluginVersion()
1001{
1002 return 1;
1003}
1004