blob: b25db97d498e5dad5e315979711f14dfd3921f99 [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 {
530 const uint32_t symbol_size = sizeof(section_header_t);
531 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);
538 DataBufferSP strtab_data_sp(m_file.ReadFileContents (m_coff_header.symoff + symbol_data_size + 4, strtab_size));
539 DataExtractor strtab_data (strtab_data_sp, GetByteOrder(), addr_byte_size);
540
541 offset = 0;
542 std::string symbol_name;
543 Symbol *symbols = m_symtab_ap->Resize (num_syms);
544 for (uint32_t i=0; i<num_syms; ++i)
545 {
546 coff_symbol_t symbol;
547 const uint32_t symbol_offset = offset;
548 const char *symbol_name_cstr = NULL;
549 // If the first 4 bytes of the symbol string are zero, then we
550 // it is followed by a 4 byte string table offset. Else these
551 // 8 bytes contain the symbol name
552 if (symtab_data.GetU32 (&offset) == 0)
553 {
554 // Long string that doesn't fit into the symbol table name,
555 // so now we must read the 4 byte string table offset
556 uint32_t strtab_offset = symtab_data.GetU32 (&offset);
557 symbol_name_cstr = strtab_data.PeekCStr (strtab_offset);
558 symbol_name.assign (symbol_name_cstr);
559 }
560 else
561 {
562 // Short string that fits into the symbol table name which is 8 bytes
563 offset += sizeof(symbol.name) - 4; // Skip remaining
564 symbol_name_cstr = symtab_data.PeekCStr (symbol_offset);
565 if (symbol_name_cstr == NULL)
566 break;
567 symbol_name.assign (symbol_name_cstr, sizeof(symbol.name));
568 }
569 symbol.value = symtab_data.GetU32 (&offset);
570 symbol.sect = symtab_data.GetU16 (&offset);
571 symbol.type = symtab_data.GetU16 (&offset);
572 symbol.storage = symtab_data.GetU8 (&offset);
573 symbol.naux = symtab_data.GetU8 (&offset);
574 Address symbol_addr(sect_list->GetSectionAtIndex(symbol.sect-1), symbol.value);
Greg Clayton037520e2012-07-18 23:18:10 +0000575 symbols[i].GetMangled ().SetValue (ConstString(symbol_name.c_str()));
Greg Claytona1743492012-03-13 23:14:29 +0000576 symbols[i].GetAddress() = symbol_addr;
577
578 if (symbol.naux > 0)
579 i += symbol.naux;
580 }
581
582 }
Greg Claytonf754f882011-09-09 20:33:05 +0000583 }
584 }
585 return m_symtab_ap.get();
586
587}
588
589SectionList *
590ObjectFilePECOFF::GetSectionList()
591{
Greg Claytona1743492012-03-13 23:14:29 +0000592 ModuleSP module_sp(GetModule());
593 if (module_sp)
Greg Claytonf754f882011-09-09 20:33:05 +0000594 {
Greg Claytona1743492012-03-13 23:14:29 +0000595 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
596 if (m_sections_ap.get() == NULL)
Greg Claytonf754f882011-09-09 20:33:05 +0000597 {
Greg Claytona1743492012-03-13 23:14:29 +0000598 m_sections_ap.reset(new SectionList());
599 const uint32_t nsects = m_sect_headers.size();
600 ModuleSP module_sp (GetModule());
601 for (uint32_t idx = 0; idx<nsects; ++idx)
Greg Clayton28469ca2011-09-10 01:04:42 +0000602 {
Greg Claytona1743492012-03-13 23:14:29 +0000603 std::string sect_name;
604 GetSectionName (sect_name, m_sect_headers[idx]);
605 ConstString const_sect_name (sect_name.c_str());
606 static ConstString g_code_sect_name (".code");
607 static ConstString g_CODE_sect_name ("CODE");
608 static ConstString g_data_sect_name (".data");
609 static ConstString g_DATA_sect_name ("DATA");
610 static ConstString g_bss_sect_name (".bss");
611 static ConstString g_BSS_sect_name ("BSS");
612 static ConstString g_debug_sect_name (".debug");
613 static ConstString g_reloc_sect_name (".reloc");
614 static ConstString g_stab_sect_name (".stab");
615 static ConstString g_stabstr_sect_name (".stabstr");
616 SectionType section_type = eSectionTypeOther;
617 if (m_sect_headers[idx].flags & IMAGE_SCN_CNT_CODE &&
618 ((const_sect_name == g_code_sect_name) || (const_sect_name == g_CODE_sect_name)))
619 {
620 section_type = eSectionTypeCode;
621 }
622 else if (m_sect_headers[idx].flags & IMAGE_SCN_CNT_INITIALIZED_DATA &&
623 ((const_sect_name == g_data_sect_name) || (const_sect_name == g_DATA_sect_name)))
624 {
Greg Clayton28469ca2011-09-10 01:04:42 +0000625 section_type = eSectionTypeData;
Greg Claytona1743492012-03-13 23:14:29 +0000626 }
627 else if (m_sect_headers[idx].flags & IMAGE_SCN_CNT_UNINITIALIZED_DATA &&
628 ((const_sect_name == g_bss_sect_name) || (const_sect_name == g_BSS_sect_name)))
629 {
630 if (m_sect_headers[idx].size == 0)
631 section_type = eSectionTypeZeroFill;
632 else
633 section_type = eSectionTypeData;
634 }
635 else if (const_sect_name == g_debug_sect_name)
636 {
637 section_type = eSectionTypeDebug;
638 }
639 else if (const_sect_name == g_stabstr_sect_name)
640 {
641 section_type = eSectionTypeDataCString;
642 }
643 else if (const_sect_name == g_reloc_sect_name)
644 {
645 section_type = eSectionTypeOther;
646 }
647 else if (m_sect_headers[idx].flags & IMAGE_SCN_CNT_CODE)
648 {
649 section_type = eSectionTypeCode;
650 }
651 else if (m_sect_headers[idx].flags & IMAGE_SCN_CNT_INITIALIZED_DATA)
652 {
Greg Clayton28469ca2011-09-10 01:04:42 +0000653 section_type = eSectionTypeData;
Greg Claytona1743492012-03-13 23:14:29 +0000654 }
655 else if (m_sect_headers[idx].flags & IMAGE_SCN_CNT_UNINITIALIZED_DATA)
656 {
657 if (m_sect_headers[idx].size == 0)
658 section_type = eSectionTypeZeroFill;
659 else
660 section_type = eSectionTypeData;
661 }
662
663 // Use a segment ID of the segment index shifted left by 8 so they
664 // never conflict with any of the sections.
665 SectionSP section_sp (new Section (module_sp, // Module to which this section belongs
666 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
667 const_sect_name, // Name of this section
668 section_type, // This section is a container of other sections.
669 m_sect_headers[idx].vmaddr, // File VM address == addresses as they are found in the object file
670 m_sect_headers[idx].vmsize, // VM size in bytes of this section
671 m_sect_headers[idx].offset, // Offset to the data for this section in the file
672 m_sect_headers[idx].size, // Size in bytes of this section as found in the the file
673 m_sect_headers[idx].flags)); // Flags for this section
674
675 //section_sp->SetIsEncrypted (segment_is_encrypted);
676
677 m_sections_ap->AddSection(section_sp);
Greg Clayton28469ca2011-09-10 01:04:42 +0000678 }
Sean Callanan56775362012-06-08 02:16:08 +0000679
680 m_sections_ap->Finalize(); // Now that we're done adding sections, finalize to build fast-lookup caches
Greg Claytonf754f882011-09-09 20:33:05 +0000681 }
682 }
683 return m_sections_ap.get();
684}
685
686bool
687ObjectFilePECOFF::GetUUID (UUID* uuid)
688{
689 return false;
690}
691
692uint32_t
693ObjectFilePECOFF::GetDependentModules (FileSpecList& files)
694{
695 return 0;
696}
697
698
699//----------------------------------------------------------------------
700// Dump
701//
702// Dump the specifics of the runtime file container (such as any headers
703// segments, sections, etc).
704//----------------------------------------------------------------------
705void
706ObjectFilePECOFF::Dump(Stream *s)
707{
Greg Claytona1743492012-03-13 23:14:29 +0000708 ModuleSP module_sp(GetModule());
709 if (module_sp)
Greg Claytonf754f882011-09-09 20:33:05 +0000710 {
Greg Claytona1743492012-03-13 23:14:29 +0000711 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
712 s->Printf("%p: ", this);
713 s->Indent();
714 s->PutCString("ObjectFilePECOFF");
715
716 ArchSpec header_arch;
717 GetArchitecture (header_arch);
718
719 *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
720
721 if (m_sections_ap.get())
722 m_sections_ap->Dump(s, NULL, true, UINT32_MAX);
723
724 if (m_symtab_ap.get())
725 m_symtab_ap->Dump(s, NULL, eSortOrderNone);
726
727 if (m_dos_header.e_magic)
728 DumpDOSHeader (s, m_dos_header);
729 if (m_coff_header.machine)
730 {
731 DumpCOFFHeader (s, m_coff_header);
732 if (m_coff_header.hdrsize)
733 DumpOptCOFFHeader (s, m_coff_header_opt);
734 }
735 s->EOL();
736 DumpSectionHeaders(s);
737 s->EOL();
Greg Claytonf754f882011-09-09 20:33:05 +0000738 }
Greg Claytonf754f882011-09-09 20:33:05 +0000739}
740
741//----------------------------------------------------------------------
742// DumpDOSHeader
743//
744// Dump the MS-DOS header to the specified output stream
745//----------------------------------------------------------------------
746void
747ObjectFilePECOFF::DumpDOSHeader(Stream *s, const dos_header_t& header)
748{
749 s->PutCString ("MSDOS Header\n");
750 s->Printf (" e_magic = 0x%4.4x\n", header.e_magic);
751 s->Printf (" e_cblp = 0x%4.4x\n", header.e_cblp);
752 s->Printf (" e_cp = 0x%4.4x\n", header.e_cp);
753 s->Printf (" e_crlc = 0x%4.4x\n", header.e_crlc);
754 s->Printf (" e_cparhdr = 0x%4.4x\n", header.e_cparhdr);
755 s->Printf (" e_minalloc = 0x%4.4x\n", header.e_minalloc);
756 s->Printf (" e_maxalloc = 0x%4.4x\n", header.e_maxalloc);
757 s->Printf (" e_ss = 0x%4.4x\n", header.e_ss);
758 s->Printf (" e_sp = 0x%4.4x\n", header.e_sp);
759 s->Printf (" e_csum = 0x%4.4x\n", header.e_csum);
760 s->Printf (" e_ip = 0x%4.4x\n", header.e_ip);
761 s->Printf (" e_cs = 0x%4.4x\n", header.e_cs);
762 s->Printf (" e_lfarlc = 0x%4.4x\n", header.e_lfarlc);
763 s->Printf (" e_ovno = 0x%4.4x\n", header.e_ovno);
764 s->Printf (" e_res[4] = { 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x }\n",
765 header.e_res[0],
766 header.e_res[1],
767 header.e_res[2],
768 header.e_res[3]);
769 s->Printf (" e_oemid = 0x%4.4x\n", header.e_oemid);
770 s->Printf (" e_oeminfo = 0x%4.4x\n", header.e_oeminfo);
771 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",
772 header.e_res2[0],
773 header.e_res2[1],
774 header.e_res2[2],
775 header.e_res2[3],
776 header.e_res2[4],
777 header.e_res2[5],
778 header.e_res2[6],
779 header.e_res2[7],
780 header.e_res2[8],
781 header.e_res2[9]);
782 s->Printf (" e_lfanew = 0x%8.8x\n", header.e_lfanew);
783}
784
785//----------------------------------------------------------------------
786// DumpCOFFHeader
787//
788// Dump the COFF header to the specified output stream
789//----------------------------------------------------------------------
790void
791ObjectFilePECOFF::DumpCOFFHeader(Stream *s, const coff_header_t& header)
792{
793 s->PutCString ("COFF Header\n");
794 s->Printf (" machine = 0x%4.4x\n", header.machine);
795 s->Printf (" nsects = 0x%4.4x\n", header.nsects);
796 s->Printf (" modtime = 0x%8.8x\n", header.modtime);
797 s->Printf (" symoff = 0x%8.8x\n", header.symoff);
798 s->Printf (" nsyms = 0x%8.8x\n", header.nsyms);
799 s->Printf (" hdrsize = 0x%4.4x\n", header.hdrsize);
800}
801
802//----------------------------------------------------------------------
803// DumpOptCOFFHeader
804//
805// Dump the optional COFF header to the specified output stream
806//----------------------------------------------------------------------
807void
808ObjectFilePECOFF::DumpOptCOFFHeader(Stream *s, const coff_opt_header_t& header)
809{
810 s->PutCString ("Optional COFF Header\n");
811 s->Printf (" magic = 0x%4.4x\n", header.magic);
812 s->Printf (" major_linker_version = 0x%2.2x\n", header.major_linker_version);
813 s->Printf (" minor_linker_version = 0x%2.2x\n", header.minor_linker_version);
814 s->Printf (" code_size = 0x%8.8x\n", header.code_size);
815 s->Printf (" data_size = 0x%8.8x\n", header.data_size);
816 s->Printf (" bss_size = 0x%8.8x\n", header.bss_size);
817 s->Printf (" entry = 0x%8.8x\n", header.entry);
818 s->Printf (" code_offset = 0x%8.8x\n", header.code_offset);
819 s->Printf (" data_offset = 0x%8.8x\n", header.data_offset);
Daniel Malead01b2952012-11-29 21:49:15 +0000820 s->Printf (" image_base = 0x%16.16" PRIx64 "\n", header.image_base);
Greg Claytonf754f882011-09-09 20:33:05 +0000821 s->Printf (" sect_alignment = 0x%8.8x\n", header.sect_alignment);
822 s->Printf (" file_alignment = 0x%8.8x\n", header.file_alignment);
823 s->Printf (" major_os_system_version = 0x%4.4x\n", header.major_os_system_version);
824 s->Printf (" minor_os_system_version = 0x%4.4x\n", header.minor_os_system_version);
825 s->Printf (" major_image_version = 0x%4.4x\n", header.major_image_version);
826 s->Printf (" minor_image_version = 0x%4.4x\n", header.minor_image_version);
827 s->Printf (" major_subsystem_version = 0x%4.4x\n", header.major_subsystem_version);
828 s->Printf (" minor_subsystem_version = 0x%4.4x\n", header.minor_subsystem_version);
829 s->Printf (" reserved1 = 0x%8.8x\n", header.reserved1);
830 s->Printf (" image_size = 0x%8.8x\n", header.image_size);
831 s->Printf (" header_size = 0x%8.8x\n", header.header_size);
Greg Clayton28469ca2011-09-10 01:04:42 +0000832 s->Printf (" checksum = 0x%8.8x\n", header.checksum);
Greg Claytonf754f882011-09-09 20:33:05 +0000833 s->Printf (" subsystem = 0x%4.4x\n", header.subsystem);
834 s->Printf (" dll_flags = 0x%4.4x\n", header.dll_flags);
Daniel Malead01b2952012-11-29 21:49:15 +0000835 s->Printf (" stack_reserve_size = 0x%16.16" PRIx64 "\n", header.stack_reserve_size);
836 s->Printf (" stack_commit_size = 0x%16.16" PRIx64 "\n", header.stack_commit_size);
837 s->Printf (" heap_reserve_size = 0x%16.16" PRIx64 "\n", header.heap_reserve_size);
838 s->Printf (" heap_commit_size = 0x%16.16" PRIx64 "\n", header.heap_commit_size);
Greg Claytonf754f882011-09-09 20:33:05 +0000839 s->Printf (" loader_flags = 0x%8.8x\n", header.loader_flags);
840 s->Printf (" num_data_dir_entries = 0x%8.8zx\n", header.data_dirs.size());
841 uint32_t i;
842 for (i=0; i<header.data_dirs.size(); i++)
843 {
Greg Clayton28469ca2011-09-10 01:04:42 +0000844 s->Printf (" data_dirs[%2u] vmaddr = 0x%8.8x, vmsize = 0x%8.8x\n",
Greg Claytonf754f882011-09-09 20:33:05 +0000845 i,
846 header.data_dirs[i].vmaddr,
847 header.data_dirs[i].vmsize);
848 }
849}
850//----------------------------------------------------------------------
851// DumpSectionHeader
852//
853// Dump a single ELF section header to the specified output stream
854//----------------------------------------------------------------------
855void
856ObjectFilePECOFF::DumpSectionHeader(Stream *s, const section_header_t& sh)
857{
858 std::string name;
859 GetSectionName(name, sh);
860 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",
861 name.c_str(),
Greg Claytonf754f882011-09-09 20:33:05 +0000862 sh.vmaddr,
Greg Clayton28469ca2011-09-10 01:04:42 +0000863 sh.vmsize,
Greg Claytonf754f882011-09-09 20:33:05 +0000864 sh.offset,
Greg Clayton28469ca2011-09-10 01:04:42 +0000865 sh.size,
Greg Claytonf754f882011-09-09 20:33:05 +0000866 sh.reloff,
867 sh.lineoff,
868 sh.nreloc,
869 sh.nline,
870 sh.flags);
871}
872
873
874//----------------------------------------------------------------------
875// DumpSectionHeaders
876//
877// Dump all of the ELF section header to the specified output stream
878//----------------------------------------------------------------------
879void
880ObjectFilePECOFF::DumpSectionHeaders(Stream *s)
881{
882
883 s->PutCString ("Section Headers\n");
Greg Clayton28469ca2011-09-10 01:04:42 +0000884 s->PutCString ("IDX name vm addr vm size file off file size reloc off line off nreloc nline flags\n");
885 s->PutCString ("==== ---------------- ---------- ---------- ---------- ---------- ---------- ---------- ------ ------ ----------\n");
Greg Claytonf754f882011-09-09 20:33:05 +0000886
887 uint32_t idx = 0;
888 SectionHeaderCollIter pos, end = m_sect_headers.end();
889
890 for (pos = m_sect_headers.begin(); pos != end; ++pos, ++idx)
891 {
Greg Clayton28469ca2011-09-10 01:04:42 +0000892 s->Printf ("[%2u] ", idx);
Greg Claytonf754f882011-09-09 20:33:05 +0000893 ObjectFilePECOFF::DumpSectionHeader(s, *pos);
894 }
895}
896
897static bool
898COFFMachineToMachCPU (uint16_t machine, ArchSpec &arch)
899{
900 switch (machine)
901 {
902 case IMAGE_FILE_MACHINE_AMD64:
903 case IMAGE_FILE_MACHINE_IA64:
904 arch.SetArchitecture (eArchTypeMachO,
905 llvm::MachO::CPUTypeX86_64,
906 llvm::MachO::CPUSubType_X86_64_ALL);
907 return true;
908
909 case IMAGE_FILE_MACHINE_I386:
910 arch.SetArchitecture (eArchTypeMachO,
911 llvm::MachO::CPUTypeI386,
912 llvm::MachO::CPUSubType_I386_ALL);
913 return true;
914
915 case IMAGE_FILE_MACHINE_POWERPC:
916 case IMAGE_FILE_MACHINE_POWERPCFP:
917 arch.SetArchitecture (eArchTypeMachO,
918 llvm::MachO::CPUTypePowerPC,
919 llvm::MachO::CPUSubType_POWERPC_ALL);
920 return true;
921 case IMAGE_FILE_MACHINE_ARM:
922 case IMAGE_FILE_MACHINE_THUMB:
923 arch.SetArchitecture (eArchTypeMachO,
924 llvm::MachO::CPUTypeARM,
925 llvm::MachO::CPUSubType_ARM_V7);
926 return true;
927 }
928 return false;
929}
930bool
931ObjectFilePECOFF::GetArchitecture (ArchSpec &arch)
932{
933 // For index zero return our cpu type
934 return COFFMachineToMachCPU (m_coff_header.machine, arch);
935}
936
937ObjectFile::Type
938ObjectFilePECOFF::CalculateType()
939{
940 if (m_coff_header.machine != 0)
941 {
942 if ((m_coff_header.flags & IMAGE_FILE_DLL) == 0)
943 return eTypeExecutable;
944 else
945 return eTypeSharedLibrary;
946 }
947 return eTypeExecutable;
948}
949
950ObjectFile::Strata
951ObjectFilePECOFF::CalculateStrata()
952{
953 return eStrataUser;
954}
955//------------------------------------------------------------------
956// PluginInterface protocol
957//------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +0000958ConstString
Greg Claytonf754f882011-09-09 20:33:05 +0000959ObjectFilePECOFF::GetPluginName()
960{
Greg Claytonf754f882011-09-09 20:33:05 +0000961 return GetPluginNameStatic();
962}
963
964uint32_t
965ObjectFilePECOFF::GetPluginVersion()
966{
967 return 1;
968}
969