blob: 692e24ba9ccde91d8707e7c1a303bfd109c0041d [file] [log] [blame]
Greg Claytone9f49822011-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"
19#include "lldb/Core/PluginManager.h"
20#include "lldb/Core/Section.h"
21#include "lldb/Core/StreamFile.h"
22#include "lldb/Core/StreamString.h"
23#include "lldb/Core/Timer.h"
24#include "lldb/Core/UUID.h"
25#include "lldb/Symbol/ObjectFile.h"
26
27static uint32_t COFFMachineToMachCPU(uint16_t machine);
28
29#define IMAGE_FILE_MACHINE_UNKNOWN 0x0000
30#define IMAGE_FILE_MACHINE_AM33 0x01d3 // Matsushita AM33
31#define IMAGE_FILE_MACHINE_AMD64 0x8664 // x64
32#define IMAGE_FILE_MACHINE_ARM 0x01c0 // ARM little endian
33#define IMAGE_FILE_MACHINE_EBC 0x0ebc // EFI byte code
34#define IMAGE_FILE_MACHINE_I386 0x014c // Intel 386 or later processors and compatible processors
35#define IMAGE_FILE_MACHINE_IA64 0x0200 // Intel Itanium processor family
36#define IMAGE_FILE_MACHINE_M32R 0x9041 // Mitsubishi M32R little endian
37#define IMAGE_FILE_MACHINE_MIPS16 0x0266 // MIPS16
38#define IMAGE_FILE_MACHINE_MIPSFPU 0x0366 // MIPS with FPU
39#define IMAGE_FILE_MACHINE_MIPSFPU16 0x0466 // MIPS16 with FPU
40#define IMAGE_FILE_MACHINE_POWERPC 0x01f0 // Power PC little endian
41#define IMAGE_FILE_MACHINE_POWERPCFP 0x01f1 // Power PC with floating point support
42#define IMAGE_FILE_MACHINE_R4000 0x0166 // MIPS little endian
43#define IMAGE_FILE_MACHINE_SH3 0x01a2 // Hitachi SH3
44#define IMAGE_FILE_MACHINE_SH3DSP 0x01a3 // Hitachi SH3 DSP
45#define IMAGE_FILE_MACHINE_SH4 0x01a6 // Hitachi SH4
46#define IMAGE_FILE_MACHINE_SH5 0x01a8 // Hitachi SH5
47#define IMAGE_FILE_MACHINE_THUMB 0x01c2 // Thumb
48#define IMAGE_FILE_MACHINE_WCEMIPSV2 0x0169 // MIPS little-endian WCE v2
49
50
51#define IMAGE_DOS_SIGNATURE 0x5A4D // MZ
52#define IMAGE_OS2_SIGNATURE 0x454E // NE
53#define IMAGE_OS2_SIGNATURE_LE 0x454C // LE
54#define IMAGE_NT_SIGNATURE 0x00004550 // PE00
55#define OPT_HEADER_MAGIC_PE32 0x010b
56#define OPT_HEADER_MAGIC_PE32_PLUS 0x020b
57
58#define IMAGE_FILE_RELOCS_STRIPPED 0x0001
59#define IMAGE_FILE_EXECUTABLE_IMAGE 0x0002
60#define IMAGE_FILE_LINE_NUMS_STRIPPED 0x0004
61#define IMAGE_FILE_LOCAL_SYMS_STRIPPED 0x0008
62#define IMAGE_FILE_AGGRESSIVE_WS_TRIM 0x0010
63#define IMAGE_FILE_LARGE_ADDRESS_AWARE 0x0020
64//#define 0x0040 // Reserved
65#define IMAGE_FILE_BYTES_REVERSED_LO 0x0080
66#define IMAGE_FILE_32BIT_MACHINE 0x0100
67#define IMAGE_FILE_DEBUG_STRIPPED 0x0200
68#define IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP 0x0400
69#define IMAGE_FILE_NET_RUN_FROM_SWAP 0x0800
70#define IMAGE_FILE_SYSTEM 0x1000
71#define IMAGE_FILE_DLL 0x2000
72#define IMAGE_FILE_UP_SYSTEM_ONLY 0x4000
73#define IMAGE_FILE_BYTES_REVERSED_HI 0x8000
74
Greg Clayton707e3402011-09-10 01:04:42 +000075
76// Section Flags
77// The section flags in the Characteristics field of the section header indicate
78// characteristics of the section.
79#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.
80#define IMAGE_SCN_CNT_CODE 0x00000020 // The section contains executable code.
81#define IMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040 // The section contains initialized data.
82#define IMAGE_SCN_CNT_UNINITIALIZED_DATA 0x00000080 // The section contains uninitialized data.
83#define IMAGE_SCN_LNK_OTHER 0x00000100 // Reserved for future use.
84#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.
85#define IMAGE_SCN_LNK_REMOVE 0x00000800 // The section will not become part of the image. This is valid only for object files.
86#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.
87#define IMAGE_SCN_GPREL 0x00008000 // The section contains data referenced through the global pointer (GP).
88#define IMAGE_SCN_MEM_PURGEABLE 0x00020000
89#define IMAGE_SCN_MEM_16BIT 0x00020000 // For ARM machine types, the section contains Thumb code. Reserved for future use with other machine types.
90#define IMAGE_SCN_MEM_LOCKED 0x00040000
91#define IMAGE_SCN_MEM_PRELOAD 0x00080000
92#define IMAGE_SCN_ALIGN_1BYTES 0x00100000 // Align data on a 1-byte boundary. Valid only for object files.
93#define IMAGE_SCN_ALIGN_2BYTES 0x00200000 // Align data on a 2-byte boundary. Valid only for object files.
94#define IMAGE_SCN_ALIGN_4BYTES 0x00300000 // Align data on a 4-byte boundary. Valid only for object files.
95#define IMAGE_SCN_ALIGN_8BYTES 0x00400000 // Align data on an 8-byte boundary. Valid only for object files.
96#define IMAGE_SCN_ALIGN_16BYTES 0x00500000 // Align data on a 16-byte boundary. Valid only for object files.
97#define IMAGE_SCN_ALIGN_32BYTES 0x00600000 // Align data on a 32-byte boundary. Valid only for object files.
98#define IMAGE_SCN_ALIGN_64BYTES 0x00700000 // Align data on a 64-byte boundary. Valid only for object files.
99#define IMAGE_SCN_ALIGN_128BYTES 0x00800000 // Align data on a 128-byte boundary. Valid only for object files.
100#define IMAGE_SCN_ALIGN_256BYTES 0x00900000 // Align data on a 256-byte boundary. Valid only for object files.
101#define IMAGE_SCN_ALIGN_512BYTES 0x00A00000 // Align data on a 512-byte boundary. Valid only for object files.
102#define IMAGE_SCN_ALIGN_1024BYTES 0x00B00000 // Align data on a 1024-byte boundary. Valid only for object files.
103#define IMAGE_SCN_ALIGN_2048BYTES 0x00C00000 // Align data on a 2048-byte boundary. Valid only for object files.
104#define IMAGE_SCN_ALIGN_4096BYTES 0x00D00000 // Align data on a 4096-byte boundary. Valid only for object files.
105#define IMAGE_SCN_ALIGN_8192BYTES 0x00E00000 // Align data on an 8192-byte boundary. Valid only for object files.
106#define IMAGE_SCN_LNK_NRELOC_OVFL 0x01000000 // The section contains extended relocations.
107#define IMAGE_SCN_MEM_DISCARDABLE 0x02000000 // The section can be discarded as needed.
108#define IMAGE_SCN_MEM_NOT_CACHED 0x04000000 // The section cannot be cached.
109#define IMAGE_SCN_MEM_NOT_PAGED 0x08000000 // The section is not pageable.
110#define IMAGE_SCN_MEM_SHARED 0x10000000 // The section can be shared in memory.
111#define IMAGE_SCN_MEM_EXECUTE 0x20000000 // The section can be executed as code.
112#define IMAGE_SCN_MEM_READ 0x40000000 // The section can be read.
113#define IMAGE_SCN_MEM_WRITE 0x80000000 // The section can be written to.
114
Greg Claytone9f49822011-09-09 20:33:05 +0000115using namespace lldb;
116using namespace lldb_private;
117
118void
119ObjectFilePECOFF::Initialize()
120{
121 PluginManager::RegisterPlugin (GetPluginNameStatic(),
122 GetPluginDescriptionStatic(),
Greg Claytonb5a8f142012-02-05 02:38:54 +0000123 CreateInstance,
124 CreateMemoryInstance);
Greg Claytone9f49822011-09-09 20:33:05 +0000125}
126
127void
128ObjectFilePECOFF::Terminate()
129{
130 PluginManager::UnregisterPlugin (CreateInstance);
131}
132
133
134const char *
135ObjectFilePECOFF::GetPluginNameStatic()
136{
137 return "object-file.pe-coff";
138}
139
140const char *
141ObjectFilePECOFF::GetPluginDescriptionStatic()
142{
143 return "Portable Executable and Common Object File Format object file reader (32 and 64 bit)";
144}
145
146
147ObjectFile *
Greg Clayton3508c382012-02-24 01:59:29 +0000148ObjectFilePECOFF::CreateInstance (const lldb::ModuleSP &module_sp, DataBufferSP& dataSP, const FileSpec* file, addr_t offset, addr_t length)
Greg Claytone9f49822011-09-09 20:33:05 +0000149{
150 if (ObjectFilePECOFF::MagicBytesMatch(dataSP))
151 {
Greg Clayton3508c382012-02-24 01:59:29 +0000152 std::auto_ptr<ObjectFile> objfile_ap(new ObjectFilePECOFF (module_sp, dataSP, file, offset, length));
Greg Claytone9f49822011-09-09 20:33:05 +0000153 if (objfile_ap.get() && objfile_ap->ParseHeader())
154 return objfile_ap.release();
155 }
156 return NULL;
157}
158
Greg Claytonb5a8f142012-02-05 02:38:54 +0000159ObjectFile *
Greg Clayton3508c382012-02-24 01:59:29 +0000160ObjectFilePECOFF::CreateMemoryInstance (const lldb::ModuleSP &module_sp,
Greg Claytonb5a8f142012-02-05 02:38:54 +0000161 lldb::DataBufferSP& data_sp,
162 const lldb::ProcessSP &process_sp,
163 lldb::addr_t header_addr)
164{
165 return NULL;
166}
167
Greg Claytone9f49822011-09-09 20:33:05 +0000168bool
169ObjectFilePECOFF::MagicBytesMatch (DataBufferSP& dataSP)
170{
171 DataExtractor data(dataSP, eByteOrderLittle, 4);
172 uint32_t offset = 0;
173 uint16_t magic = data.GetU16 (&offset);
174 return magic == IMAGE_DOS_SIGNATURE;
175}
176
177
Greg Clayton3508c382012-02-24 01:59:29 +0000178ObjectFilePECOFF::ObjectFilePECOFF (const lldb::ModuleSP &module_sp,
Greg Claytone9f49822011-09-09 20:33:05 +0000179 DataBufferSP& dataSP,
180 const FileSpec* file,
181 addr_t offset,
182 addr_t length) :
Greg Clayton3508c382012-02-24 01:59:29 +0000183 ObjectFile (module_sp, file, offset, length, dataSP),
Greg Claytone9f49822011-09-09 20:33:05 +0000184 m_mutex (Mutex::eMutexTypeRecursive),
185 m_dos_header (),
186 m_coff_header (),
187 m_coff_header_opt (),
188 m_sect_headers ()
189{
190 ::memset (&m_dos_header, 0, sizeof(m_dos_header));
191 ::memset (&m_coff_header, 0, sizeof(m_coff_header));
192 ::memset (&m_coff_header_opt, 0, sizeof(m_coff_header_opt));
193}
194
195
196ObjectFilePECOFF::~ObjectFilePECOFF()
197{
198}
199
200
201bool
202ObjectFilePECOFF::ParseHeader ()
203{
204 Mutex::Locker locker(m_mutex);
205 m_sect_headers.clear();
206 m_data.SetByteOrder (eByteOrderLittle);
207 uint32_t offset = 0;
208
209 if (ParseDOSHeader())
210 {
211 offset = m_dos_header.e_lfanew;
212 uint32_t pe_signature = m_data.GetU32 (&offset);
213 if (pe_signature != IMAGE_NT_SIGNATURE)
214 return false;
Greg Clayton707e3402011-09-10 01:04:42 +0000215 if (ParseCOFFHeader(&offset))
216 {
217 if (m_coff_header.hdrsize > 0)
218 ParseCOFFOptionalHeader(&offset);
219 ParseSectionHeaders (offset);
220 }
221 StreamFile s(stdout, false);// REMOVE THIS LINE!!!
222 Dump(&s);// REMOVE THIS LINE!!!
Greg Claytone9f49822011-09-09 20:33:05 +0000223 return true;
224 }
225 return false;
226}
227
228
229ByteOrder
230ObjectFilePECOFF::GetByteOrder () const
231{
232 return eByteOrderLittle;
233}
234
235bool
236ObjectFilePECOFF::IsExecutable() const
237{
238 return (m_coff_header.flags & IMAGE_FILE_DLL) == 0;
239}
240
241size_t
242ObjectFilePECOFF::GetAddressByteSize () const
243{
244 if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32_PLUS)
245 return 8;
246 else if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32)
247 return 4;
248 return 4;
249}
250
251//----------------------------------------------------------------------
252// NeedsEndianSwap
253//
254// Return true if an endian swap needs to occur when extracting data
255// from this file.
256//----------------------------------------------------------------------
257bool
258ObjectFilePECOFF::NeedsEndianSwap() const
259{
260#if defined(__LITTLE_ENDIAN__)
261 return false;
262#else
263 return true;
264#endif
265}
266//----------------------------------------------------------------------
267// ParseDOSHeader
268//----------------------------------------------------------------------
269bool
270ObjectFilePECOFF::ParseDOSHeader ()
271{
272 bool success = false;
273 uint32_t offset = 0;
274 success = m_data.ValidOffsetForDataOfSize(0, sizeof(m_dos_header));
275
276 if (success)
277 {
278 m_dos_header.e_magic = m_data.GetU16(&offset); // Magic number
279 success = m_dos_header.e_magic == IMAGE_DOS_SIGNATURE;
280
281 if (success)
282 {
283 m_dos_header.e_cblp = m_data.GetU16(&offset); // Bytes on last page of file
284 m_dos_header.e_cp = m_data.GetU16(&offset); // Pages in file
285 m_dos_header.e_crlc = m_data.GetU16(&offset); // Relocations
286 m_dos_header.e_cparhdr = m_data.GetU16(&offset); // Size of header in paragraphs
287 m_dos_header.e_minalloc = m_data.GetU16(&offset); // Minimum extra paragraphs needed
288 m_dos_header.e_maxalloc = m_data.GetU16(&offset); // Maximum extra paragraphs needed
289 m_dos_header.e_ss = m_data.GetU16(&offset); // Initial (relative) SS value
290 m_dos_header.e_sp = m_data.GetU16(&offset); // Initial SP value
291 m_dos_header.e_csum = m_data.GetU16(&offset); // Checksum
292 m_dos_header.e_ip = m_data.GetU16(&offset); // Initial IP value
293 m_dos_header.e_cs = m_data.GetU16(&offset); // Initial (relative) CS value
294 m_dos_header.e_lfarlc = m_data.GetU16(&offset); // File address of relocation table
295 m_dos_header.e_ovno = m_data.GetU16(&offset); // Overlay number
296
297 m_dos_header.e_res[0] = m_data.GetU16(&offset); // Reserved words
298 m_dos_header.e_res[1] = m_data.GetU16(&offset); // Reserved words
299 m_dos_header.e_res[2] = m_data.GetU16(&offset); // Reserved words
300 m_dos_header.e_res[3] = m_data.GetU16(&offset); // Reserved words
301
302 m_dos_header.e_oemid = m_data.GetU16(&offset); // OEM identifier (for e_oeminfo)
303 m_dos_header.e_oeminfo = m_data.GetU16(&offset); // OEM information; e_oemid specific
304 m_dos_header.e_res2[0] = m_data.GetU16(&offset); // Reserved words
305 m_dos_header.e_res2[1] = m_data.GetU16(&offset); // Reserved words
306 m_dos_header.e_res2[2] = m_data.GetU16(&offset); // Reserved words
307 m_dos_header.e_res2[3] = m_data.GetU16(&offset); // Reserved words
308 m_dos_header.e_res2[4] = m_data.GetU16(&offset); // Reserved words
309 m_dos_header.e_res2[5] = m_data.GetU16(&offset); // Reserved words
310 m_dos_header.e_res2[6] = m_data.GetU16(&offset); // Reserved words
311 m_dos_header.e_res2[7] = m_data.GetU16(&offset); // Reserved words
312 m_dos_header.e_res2[8] = m_data.GetU16(&offset); // Reserved words
313 m_dos_header.e_res2[9] = m_data.GetU16(&offset); // Reserved words
314
315 m_dos_header.e_lfanew = m_data.GetU32(&offset); // File address of new exe header
316 }
317 }
318 if (!success)
319 memset(&m_dos_header, 0, sizeof(m_dos_header));
320 return success;
321}
322
323
324//----------------------------------------------------------------------
325// ParserCOFFHeader
326//----------------------------------------------------------------------
327bool
328ObjectFilePECOFF::ParseCOFFHeader(uint32_t* offset_ptr)
329{
330 bool success = m_data.ValidOffsetForDataOfSize (*offset_ptr, sizeof(m_coff_header));
331 if (success)
332 {
333 m_coff_header.machine = m_data.GetU16(offset_ptr);
334 m_coff_header.nsects = m_data.GetU16(offset_ptr);
335 m_coff_header.modtime = m_data.GetU32(offset_ptr);
336 m_coff_header.symoff = m_data.GetU32(offset_ptr);
337 m_coff_header.nsyms = m_data.GetU32(offset_ptr);
338 m_coff_header.hdrsize = m_data.GetU16(offset_ptr);
339 m_coff_header.flags = m_data.GetU16(offset_ptr);
340 }
341 if (!success)
342 memset(&m_coff_header, 0, sizeof(m_coff_header));
343 return success;
344}
345
346bool
347ObjectFilePECOFF::ParseCOFFOptionalHeader(uint32_t* offset_ptr)
348{
349 bool success = false;
350 const uint32_t end_offset = *offset_ptr + m_coff_header.hdrsize;
351 if (*offset_ptr < end_offset)
352 {
353 success = true;
354 m_coff_header_opt.magic = m_data.GetU16(offset_ptr);
355 m_coff_header_opt.major_linker_version = m_data.GetU8 (offset_ptr);
356 m_coff_header_opt.minor_linker_version = m_data.GetU8 (offset_ptr);
357 m_coff_header_opt.code_size = m_data.GetU32(offset_ptr);
358 m_coff_header_opt.data_size = m_data.GetU32(offset_ptr);
359 m_coff_header_opt.bss_size = m_data.GetU32(offset_ptr);
360 m_coff_header_opt.entry = m_data.GetU32(offset_ptr);
361 m_coff_header_opt.code_offset = m_data.GetU32(offset_ptr);
362
363 const uint32_t addr_byte_size = GetAddressByteSize ();
364
365 if (*offset_ptr < end_offset)
366 {
367 if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32)
368 {
369 // PE32 only
370 m_coff_header_opt.data_offset = m_data.GetU32(offset_ptr);
371 }
372 else
373 m_coff_header_opt.data_offset = 0;
374
375 if (*offset_ptr < end_offset)
376 {
377 m_coff_header_opt.image_base = m_data.GetMaxU64 (offset_ptr, addr_byte_size);
378 m_coff_header_opt.sect_alignment = m_data.GetU32(offset_ptr);
379 m_coff_header_opt.file_alignment = m_data.GetU32(offset_ptr);
380 m_coff_header_opt.major_os_system_version = m_data.GetU16(offset_ptr);
381 m_coff_header_opt.minor_os_system_version = m_data.GetU16(offset_ptr);
382 m_coff_header_opt.major_image_version = m_data.GetU16(offset_ptr);
383 m_coff_header_opt.minor_image_version = m_data.GetU16(offset_ptr);
384 m_coff_header_opt.major_subsystem_version = m_data.GetU16(offset_ptr);
385 m_coff_header_opt.minor_subsystem_version = m_data.GetU16(offset_ptr);
386 m_coff_header_opt.reserved1 = m_data.GetU32(offset_ptr);
387 m_coff_header_opt.image_size = m_data.GetU32(offset_ptr);
388 m_coff_header_opt.header_size = m_data.GetU32(offset_ptr);
Greg Clayton707e3402011-09-10 01:04:42 +0000389 m_coff_header_opt.checksum = m_data.GetU32(offset_ptr);
Greg Claytone9f49822011-09-09 20:33:05 +0000390 m_coff_header_opt.subsystem = m_data.GetU16(offset_ptr);
391 m_coff_header_opt.dll_flags = m_data.GetU16(offset_ptr);
392 m_coff_header_opt.stack_reserve_size = m_data.GetMaxU64 (offset_ptr, addr_byte_size);
393 m_coff_header_opt.stack_commit_size = m_data.GetMaxU64 (offset_ptr, addr_byte_size);
394 m_coff_header_opt.heap_reserve_size = m_data.GetMaxU64 (offset_ptr, addr_byte_size);
395 m_coff_header_opt.heap_commit_size = m_data.GetMaxU64 (offset_ptr, addr_byte_size);
396 m_coff_header_opt.loader_flags = m_data.GetU32(offset_ptr);
397 uint32_t num_data_dir_entries = m_data.GetU32(offset_ptr);
398 m_coff_header_opt.data_dirs.clear();
399 m_coff_header_opt.data_dirs.resize(num_data_dir_entries);
400 uint32_t i;
401 for (i=0; i<num_data_dir_entries; i++)
402 {
403 m_coff_header_opt.data_dirs[i].vmaddr = m_data.GetU32(offset_ptr);
404 m_coff_header_opt.data_dirs[i].vmsize = m_data.GetU32(offset_ptr);
405 }
406 }
407 }
408 }
409 // Make sure we are on track for section data which follows
410 *offset_ptr = end_offset;
411 return success;
412}
413
414
415//----------------------------------------------------------------------
416// ParseSectionHeaders
417//----------------------------------------------------------------------
418bool
419ObjectFilePECOFF::ParseSectionHeaders (uint32_t section_header_data_offset)
420{
421 const uint32_t nsects = m_coff_header.nsects;
422 m_sect_headers.clear();
423
424 if (nsects > 0)
425 {
426 const uint32_t addr_byte_size = GetAddressByteSize ();
427 const size_t section_header_byte_size = nsects * sizeof(section_header_t);
428 DataBufferSP section_header_data_sp(m_file.ReadFileContents (section_header_data_offset, section_header_byte_size));
429 DataExtractor section_header_data (section_header_data_sp, GetByteOrder(), addr_byte_size);
430
431 uint32_t offset = 0;
432 if (section_header_data.ValidOffsetForDataOfSize (offset, section_header_byte_size))
433 {
434 m_sect_headers.resize(nsects);
435
436 for (uint32_t idx = 0; idx<nsects; ++idx)
437 {
438 const void *name_data = section_header_data.GetData(&offset, 8);
439 if (name_data)
440 {
441 memcpy(m_sect_headers[idx].name, name_data, 8);
442 m_sect_headers[idx].vmsize = section_header_data.GetU32(&offset);
443 m_sect_headers[idx].vmaddr = section_header_data.GetU32(&offset);
444 m_sect_headers[idx].size = section_header_data.GetU32(&offset);
445 m_sect_headers[idx].offset = section_header_data.GetU32(&offset);
446 m_sect_headers[idx].reloff = section_header_data.GetU32(&offset);
447 m_sect_headers[idx].lineoff = section_header_data.GetU32(&offset);
448 m_sect_headers[idx].nreloc = section_header_data.GetU16(&offset);
449 m_sect_headers[idx].nline = section_header_data.GetU16(&offset);
450 m_sect_headers[idx].flags = section_header_data.GetU32(&offset);
451 }
452 }
453 }
454 }
455
456 return m_sect_headers.empty() == false;
457}
458
459bool
460ObjectFilePECOFF::GetSectionName(std::string& sect_name, const section_header_t& sect)
461{
462 if (sect.name[0] == '/')
463 {
464 uint32_t stroff = strtoul(&sect.name[1], NULL, 10);
465 uint32_t string_file_offset = m_coff_header.symoff + (m_coff_header.nsyms * 18) + stroff;
466 const char *name = m_data.GetCStr (&string_file_offset);
467 if (name)
468 {
469 sect_name = name;
470 return true;
471 }
472
473 return false;
474 }
475 sect_name = sect.name;
476 return true;
477}
478
479//----------------------------------------------------------------------
480// GetNListSymtab
481//----------------------------------------------------------------------
482Symtab *
483ObjectFilePECOFF::GetSymtab()
484{
485 Mutex::Locker symfile_locker(m_mutex);
486 if (m_symtab_ap.get() == NULL)
487 {
488 SectionList *sect_list = GetSectionList();
489 m_symtab_ap.reset(new Symtab(this));
490 Mutex::Locker symtab_locker (m_symtab_ap->GetMutex());
Greg Clayton707e3402011-09-10 01:04:42 +0000491
492 const uint32_t num_syms = m_coff_header.nsyms;
493
494 if (num_syms > 0 && m_coff_header.symoff > 0)
Greg Claytone9f49822011-09-09 20:33:05 +0000495 {
Greg Clayton707e3402011-09-10 01:04:42 +0000496 const uint32_t symbol_size = sizeof(section_header_t);
497 const uint32_t addr_byte_size = GetAddressByteSize ();
498 const size_t symbol_data_size = num_syms * symbol_size;
499 // Include the 4 bytes string table size at the end of the symbols
500 DataBufferSP symtab_data_sp(m_file.ReadFileContents (m_coff_header.symoff, symbol_data_size + 4));
501 DataExtractor symtab_data (symtab_data_sp, GetByteOrder(), addr_byte_size);
502 uint32_t offset = symbol_data_size;
503 const uint32_t strtab_size = symtab_data.GetU32 (&offset);
504 DataBufferSP strtab_data_sp(m_file.ReadFileContents (m_coff_header.symoff + symbol_data_size + 4, strtab_size));
505 DataExtractor strtab_data (strtab_data_sp, GetByteOrder(), addr_byte_size);
506
507 offset = 0;
508 std::string symbol_name;
Greg Claytone9f49822011-09-09 20:33:05 +0000509 Symbol *symbols = m_symtab_ap->Resize (num_syms);
510 for (uint32_t i=0; i<num_syms; ++i)
511 {
512 coff_symbol_t symbol;
Greg Clayton707e3402011-09-10 01:04:42 +0000513 const uint32_t symbol_offset = offset;
514 const char *symbol_name_cstr = NULL;
515 // If the first 4 bytes of the symbol string are zero, then we
516 // it is followed by a 4 byte string table offset. Else these
517 // 8 bytes contain the symbol name
518 if (symtab_data.GetU32 (&offset) == 0)
519 {
520 // Long string that doesn't fit into the symbol table name,
521 // so now we must read the 4 byte string table offset
522 uint32_t strtab_offset = symtab_data.GetU32 (&offset);
523 symbol_name_cstr = strtab_data.PeekCStr (strtab_offset);
524 symbol_name.assign (symbol_name_cstr);
525 }
526 else
527 {
528 // Short string that fits into the symbol table name which is 8 bytes
529 offset += sizeof(symbol.name) - 4; // Skip remaining
530 symbol_name_cstr = symtab_data.PeekCStr (symbol_offset);
531 if (symbol_name_cstr == NULL)
532 break;
533 symbol_name.assign (symbol_name_cstr, sizeof(symbol.name));
534 }
535 symbol.value = symtab_data.GetU32 (&offset);
536 symbol.sect = symtab_data.GetU16 (&offset);
537 symbol.type = symtab_data.GetU16 (&offset);
538 symbol.storage = symtab_data.GetU8 (&offset);
539 symbol.naux = symtab_data.GetU8 (&offset);
Greg Clayton3508c382012-02-24 01:59:29 +0000540 Address symbol_addr(sect_list->GetSectionAtIndex(symbol.sect-1), symbol.value);
Greg Clayton707e3402011-09-10 01:04:42 +0000541 symbols[i].GetMangled ().SetValue (symbol_name.c_str(), symbol_name[0]=='_' && symbol_name[1] == 'Z');
Greg Claytone9f49822011-09-09 20:33:05 +0000542 symbols[i].SetValue(symbol_addr);
543
544 if (symbol.naux > 0)
545 i += symbol.naux;
546 }
547
548 }
549 }
550 return m_symtab_ap.get();
551
552}
553
554SectionList *
555ObjectFilePECOFF::GetSectionList()
556{
557 Mutex::Locker symfile_locker(m_mutex);
558 if (m_sections_ap.get() == NULL)
559 {
560 m_sections_ap.reset(new SectionList());
561 const uint32_t nsects = m_sect_headers.size();
Greg Clayton3508c382012-02-24 01:59:29 +0000562 ModuleSP module_sp (GetModule());
Greg Claytone9f49822011-09-09 20:33:05 +0000563 for (uint32_t idx = 0; idx<nsects; ++idx)
564 {
565 std::string sect_name;
566 GetSectionName (sect_name, m_sect_headers[idx]);
567 ConstString const_sect_name (sect_name.c_str());
Greg Clayton707e3402011-09-10 01:04:42 +0000568 static ConstString g_code_sect_name (".code");
569 static ConstString g_CODE_sect_name ("CODE");
570 static ConstString g_data_sect_name (".data");
571 static ConstString g_DATA_sect_name ("DATA");
572 static ConstString g_bss_sect_name (".bss");
573 static ConstString g_BSS_sect_name ("BSS");
574 static ConstString g_debug_sect_name (".debug");
575 static ConstString g_reloc_sect_name (".reloc");
576 static ConstString g_stab_sect_name (".stab");
577 static ConstString g_stabstr_sect_name (".stabstr");
578 SectionType section_type = eSectionTypeOther;
579 if (m_sect_headers[idx].flags & IMAGE_SCN_CNT_CODE &&
580 ((const_sect_name == g_code_sect_name) || (const_sect_name == g_CODE_sect_name)))
581 {
582 section_type = eSectionTypeCode;
583 }
584 else if (m_sect_headers[idx].flags & IMAGE_SCN_CNT_INITIALIZED_DATA &&
585 ((const_sect_name == g_data_sect_name) || (const_sect_name == g_DATA_sect_name)))
586 {
587 section_type = eSectionTypeData;
588 }
589 else if (m_sect_headers[idx].flags & IMAGE_SCN_CNT_UNINITIALIZED_DATA &&
590 ((const_sect_name == g_bss_sect_name) || (const_sect_name == g_BSS_sect_name)))
591 {
592 if (m_sect_headers[idx].size == 0)
593 section_type = eSectionTypeZeroFill;
594 else
595 section_type = eSectionTypeData;
596 }
597 else if (const_sect_name == g_debug_sect_name)
598 {
599 section_type = eSectionTypeDebug;
600 }
601 else if (const_sect_name == g_stabstr_sect_name)
602 {
603 section_type = eSectionTypeDataCString;
604 }
605 else if (const_sect_name == g_reloc_sect_name)
606 {
607 section_type = eSectionTypeOther;
608 }
609 else if (m_sect_headers[idx].flags & IMAGE_SCN_CNT_CODE)
610 {
611 section_type = eSectionTypeCode;
612 }
613 else if (m_sect_headers[idx].flags & IMAGE_SCN_CNT_INITIALIZED_DATA)
614 {
615 section_type = eSectionTypeData;
616 }
617 else if (m_sect_headers[idx].flags & IMAGE_SCN_CNT_UNINITIALIZED_DATA)
618 {
619 if (m_sect_headers[idx].size == 0)
620 section_type = eSectionTypeZeroFill;
621 else
622 section_type = eSectionTypeData;
623 }
624
Greg Claytone9f49822011-09-09 20:33:05 +0000625 // Use a segment ID of the segment index shifted left by 8 so they
626 // never conflict with any of the sections.
Greg Clayton3508c382012-02-24 01:59:29 +0000627 SectionSP section_sp (new Section (module_sp, // Module to which this section belongs
Greg Claytone9f49822011-09-09 20:33:05 +0000628 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
629 const_sect_name, // Name of this section
Greg Clayton707e3402011-09-10 01:04:42 +0000630 section_type, // This section is a container of other sections.
Greg Claytone9f49822011-09-09 20:33:05 +0000631 m_sect_headers[idx].vmaddr, // File VM address == addresses as they are found in the object file
632 m_sect_headers[idx].vmsize, // VM size in bytes of this section
633 m_sect_headers[idx].offset, // Offset to the data for this section in the file
634 m_sect_headers[idx].size, // Size in bytes of this section as found in the the file
635 m_sect_headers[idx].flags)); // Flags for this section
636
637 //section_sp->SetIsEncrypted (segment_is_encrypted);
638
639 m_sections_ap->AddSection(section_sp);
640 }
641 }
642 return m_sections_ap.get();
643}
644
645bool
646ObjectFilePECOFF::GetUUID (UUID* uuid)
647{
648 return false;
649}
650
651uint32_t
652ObjectFilePECOFF::GetDependentModules (FileSpecList& files)
653{
654 return 0;
655}
656
657
658//----------------------------------------------------------------------
659// Dump
660//
661// Dump the specifics of the runtime file container (such as any headers
662// segments, sections, etc).
663//----------------------------------------------------------------------
664void
665ObjectFilePECOFF::Dump(Stream *s)
666{
667 Mutex::Locker locker(m_mutex);
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000668 s->Printf("%p: ", this);
Greg Claytone9f49822011-09-09 20:33:05 +0000669 s->Indent();
670 s->PutCString("ObjectFilePECOFF");
671
672 ArchSpec header_arch;
673 GetArchitecture (header_arch);
674
675 *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
676
677 if (m_sections_ap.get())
678 m_sections_ap->Dump(s, NULL, true, UINT32_MAX);
679
680 if (m_symtab_ap.get())
681 m_symtab_ap->Dump(s, NULL, eSortOrderNone);
682
683 if (m_dos_header.e_magic)
684 DumpDOSHeader (s, m_dos_header);
685 if (m_coff_header.machine)
686 {
687 DumpCOFFHeader (s, m_coff_header);
688 if (m_coff_header.hdrsize)
689 DumpOptCOFFHeader (s, m_coff_header_opt);
690 }
691 s->EOL();
692 DumpSectionHeaders(s);
693 s->EOL();
694}
695
696//----------------------------------------------------------------------
697// DumpDOSHeader
698//
699// Dump the MS-DOS header to the specified output stream
700//----------------------------------------------------------------------
701void
702ObjectFilePECOFF::DumpDOSHeader(Stream *s, const dos_header_t& header)
703{
704 s->PutCString ("MSDOS Header\n");
705 s->Printf (" e_magic = 0x%4.4x\n", header.e_magic);
706 s->Printf (" e_cblp = 0x%4.4x\n", header.e_cblp);
707 s->Printf (" e_cp = 0x%4.4x\n", header.e_cp);
708 s->Printf (" e_crlc = 0x%4.4x\n", header.e_crlc);
709 s->Printf (" e_cparhdr = 0x%4.4x\n", header.e_cparhdr);
710 s->Printf (" e_minalloc = 0x%4.4x\n", header.e_minalloc);
711 s->Printf (" e_maxalloc = 0x%4.4x\n", header.e_maxalloc);
712 s->Printf (" e_ss = 0x%4.4x\n", header.e_ss);
713 s->Printf (" e_sp = 0x%4.4x\n", header.e_sp);
714 s->Printf (" e_csum = 0x%4.4x\n", header.e_csum);
715 s->Printf (" e_ip = 0x%4.4x\n", header.e_ip);
716 s->Printf (" e_cs = 0x%4.4x\n", header.e_cs);
717 s->Printf (" e_lfarlc = 0x%4.4x\n", header.e_lfarlc);
718 s->Printf (" e_ovno = 0x%4.4x\n", header.e_ovno);
719 s->Printf (" e_res[4] = { 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x }\n",
720 header.e_res[0],
721 header.e_res[1],
722 header.e_res[2],
723 header.e_res[3]);
724 s->Printf (" e_oemid = 0x%4.4x\n", header.e_oemid);
725 s->Printf (" e_oeminfo = 0x%4.4x\n", header.e_oeminfo);
726 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",
727 header.e_res2[0],
728 header.e_res2[1],
729 header.e_res2[2],
730 header.e_res2[3],
731 header.e_res2[4],
732 header.e_res2[5],
733 header.e_res2[6],
734 header.e_res2[7],
735 header.e_res2[8],
736 header.e_res2[9]);
737 s->Printf (" e_lfanew = 0x%8.8x\n", header.e_lfanew);
738}
739
740//----------------------------------------------------------------------
741// DumpCOFFHeader
742//
743// Dump the COFF header to the specified output stream
744//----------------------------------------------------------------------
745void
746ObjectFilePECOFF::DumpCOFFHeader(Stream *s, const coff_header_t& header)
747{
748 s->PutCString ("COFF Header\n");
749 s->Printf (" machine = 0x%4.4x\n", header.machine);
750 s->Printf (" nsects = 0x%4.4x\n", header.nsects);
751 s->Printf (" modtime = 0x%8.8x\n", header.modtime);
752 s->Printf (" symoff = 0x%8.8x\n", header.symoff);
753 s->Printf (" nsyms = 0x%8.8x\n", header.nsyms);
754 s->Printf (" hdrsize = 0x%4.4x\n", header.hdrsize);
755}
756
757//----------------------------------------------------------------------
758// DumpOptCOFFHeader
759//
760// Dump the optional COFF header to the specified output stream
761//----------------------------------------------------------------------
762void
763ObjectFilePECOFF::DumpOptCOFFHeader(Stream *s, const coff_opt_header_t& header)
764{
765 s->PutCString ("Optional COFF Header\n");
766 s->Printf (" magic = 0x%4.4x\n", header.magic);
767 s->Printf (" major_linker_version = 0x%2.2x\n", header.major_linker_version);
768 s->Printf (" minor_linker_version = 0x%2.2x\n", header.minor_linker_version);
769 s->Printf (" code_size = 0x%8.8x\n", header.code_size);
770 s->Printf (" data_size = 0x%8.8x\n", header.data_size);
771 s->Printf (" bss_size = 0x%8.8x\n", header.bss_size);
772 s->Printf (" entry = 0x%8.8x\n", header.entry);
773 s->Printf (" code_offset = 0x%8.8x\n", header.code_offset);
774 s->Printf (" data_offset = 0x%8.8x\n", header.data_offset);
775 s->Printf (" image_base = 0x%16.16llx\n", header.image_base);
776 s->Printf (" sect_alignment = 0x%8.8x\n", header.sect_alignment);
777 s->Printf (" file_alignment = 0x%8.8x\n", header.file_alignment);
778 s->Printf (" major_os_system_version = 0x%4.4x\n", header.major_os_system_version);
779 s->Printf (" minor_os_system_version = 0x%4.4x\n", header.minor_os_system_version);
780 s->Printf (" major_image_version = 0x%4.4x\n", header.major_image_version);
781 s->Printf (" minor_image_version = 0x%4.4x\n", header.minor_image_version);
782 s->Printf (" major_subsystem_version = 0x%4.4x\n", header.major_subsystem_version);
783 s->Printf (" minor_subsystem_version = 0x%4.4x\n", header.minor_subsystem_version);
784 s->Printf (" reserved1 = 0x%8.8x\n", header.reserved1);
785 s->Printf (" image_size = 0x%8.8x\n", header.image_size);
786 s->Printf (" header_size = 0x%8.8x\n", header.header_size);
Greg Clayton707e3402011-09-10 01:04:42 +0000787 s->Printf (" checksum = 0x%8.8x\n", header.checksum);
Greg Claytone9f49822011-09-09 20:33:05 +0000788 s->Printf (" subsystem = 0x%4.4x\n", header.subsystem);
789 s->Printf (" dll_flags = 0x%4.4x\n", header.dll_flags);
790 s->Printf (" stack_reserve_size = 0x%16.16llx\n", header.stack_reserve_size);
791 s->Printf (" stack_commit_size = 0x%16.16llx\n", header.stack_commit_size);
792 s->Printf (" heap_reserve_size = 0x%16.16llx\n", header.heap_reserve_size);
793 s->Printf (" heap_commit_size = 0x%16.16llx\n", header.heap_commit_size);
794 s->Printf (" loader_flags = 0x%8.8x\n", header.loader_flags);
795 s->Printf (" num_data_dir_entries = 0x%8.8zx\n", header.data_dirs.size());
796 uint32_t i;
797 for (i=0; i<header.data_dirs.size(); i++)
798 {
Greg Clayton707e3402011-09-10 01:04:42 +0000799 s->Printf (" data_dirs[%2u] vmaddr = 0x%8.8x, vmsize = 0x%8.8x\n",
Greg Claytone9f49822011-09-09 20:33:05 +0000800 i,
801 header.data_dirs[i].vmaddr,
802 header.data_dirs[i].vmsize);
803 }
804}
805//----------------------------------------------------------------------
806// DumpSectionHeader
807//
808// Dump a single ELF section header to the specified output stream
809//----------------------------------------------------------------------
810void
811ObjectFilePECOFF::DumpSectionHeader(Stream *s, const section_header_t& sh)
812{
813 std::string name;
814 GetSectionName(name, sh);
815 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",
816 name.c_str(),
Greg Claytone9f49822011-09-09 20:33:05 +0000817 sh.vmaddr,
Greg Clayton707e3402011-09-10 01:04:42 +0000818 sh.vmsize,
Greg Claytone9f49822011-09-09 20:33:05 +0000819 sh.offset,
Greg Clayton707e3402011-09-10 01:04:42 +0000820 sh.size,
Greg Claytone9f49822011-09-09 20:33:05 +0000821 sh.reloff,
822 sh.lineoff,
823 sh.nreloc,
824 sh.nline,
825 sh.flags);
826}
827
828
829//----------------------------------------------------------------------
830// DumpSectionHeaders
831//
832// Dump all of the ELF section header to the specified output stream
833//----------------------------------------------------------------------
834void
835ObjectFilePECOFF::DumpSectionHeaders(Stream *s)
836{
837
838 s->PutCString ("Section Headers\n");
Greg Clayton707e3402011-09-10 01:04:42 +0000839 s->PutCString ("IDX name vm addr vm size file off file size reloc off line off nreloc nline flags\n");
840 s->PutCString ("==== ---------------- ---------- ---------- ---------- ---------- ---------- ---------- ------ ------ ----------\n");
Greg Claytone9f49822011-09-09 20:33:05 +0000841
842 uint32_t idx = 0;
843 SectionHeaderCollIter pos, end = m_sect_headers.end();
844
845 for (pos = m_sect_headers.begin(); pos != end; ++pos, ++idx)
846 {
Greg Clayton707e3402011-09-10 01:04:42 +0000847 s->Printf ("[%2u] ", idx);
Greg Claytone9f49822011-09-09 20:33:05 +0000848 ObjectFilePECOFF::DumpSectionHeader(s, *pos);
849 }
850}
851
852static bool
853COFFMachineToMachCPU (uint16_t machine, ArchSpec &arch)
854{
855 switch (machine)
856 {
857 case IMAGE_FILE_MACHINE_AMD64:
858 case IMAGE_FILE_MACHINE_IA64:
859 arch.SetArchitecture (eArchTypeMachO,
860 llvm::MachO::CPUTypeX86_64,
861 llvm::MachO::CPUSubType_X86_64_ALL);
862 return true;
863
864 case IMAGE_FILE_MACHINE_I386:
865 arch.SetArchitecture (eArchTypeMachO,
866 llvm::MachO::CPUTypeI386,
867 llvm::MachO::CPUSubType_I386_ALL);
868 return true;
869
870 case IMAGE_FILE_MACHINE_POWERPC:
871 case IMAGE_FILE_MACHINE_POWERPCFP:
872 arch.SetArchitecture (eArchTypeMachO,
873 llvm::MachO::CPUTypePowerPC,
874 llvm::MachO::CPUSubType_POWERPC_ALL);
875 return true;
876 case IMAGE_FILE_MACHINE_ARM:
877 case IMAGE_FILE_MACHINE_THUMB:
878 arch.SetArchitecture (eArchTypeMachO,
879 llvm::MachO::CPUTypeARM,
880 llvm::MachO::CPUSubType_ARM_V7);
881 return true;
882 }
883 return false;
884}
885bool
886ObjectFilePECOFF::GetArchitecture (ArchSpec &arch)
887{
888 // For index zero return our cpu type
889 return COFFMachineToMachCPU (m_coff_header.machine, arch);
890}
891
892ObjectFile::Type
893ObjectFilePECOFF::CalculateType()
894{
895 if (m_coff_header.machine != 0)
896 {
897 if ((m_coff_header.flags & IMAGE_FILE_DLL) == 0)
898 return eTypeExecutable;
899 else
900 return eTypeSharedLibrary;
901 }
902 return eTypeExecutable;
903}
904
905ObjectFile::Strata
906ObjectFilePECOFF::CalculateStrata()
907{
908 return eStrataUser;
909}
910//------------------------------------------------------------------
911// PluginInterface protocol
912//------------------------------------------------------------------
913const char *
914ObjectFilePECOFF::GetPluginName()
915{
916 return "ObjectFilePECOFF";
917}
918
919const char *
920ObjectFilePECOFF::GetShortPluginName()
921{
922 return GetPluginNameStatic();
923}
924
925uint32_t
926ObjectFilePECOFF::GetPluginVersion()
927{
928 return 1;
929}
930