blob: f908ec0a3c8cdfa5b575d78375454b25e22da482 [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_dos_header (),
185 m_coff_header (),
186 m_coff_header_opt (),
187 m_sect_headers ()
188{
189 ::memset (&m_dos_header, 0, sizeof(m_dos_header));
190 ::memset (&m_coff_header, 0, sizeof(m_coff_header));
191 ::memset (&m_coff_header_opt, 0, sizeof(m_coff_header_opt));
192}
193
194
195ObjectFilePECOFF::~ObjectFilePECOFF()
196{
197}
198
199
200bool
201ObjectFilePECOFF::ParseHeader ()
202{
Greg Clayton9482f052012-03-13 23:14:29 +0000203 ModuleSP module_sp(GetModule());
204 if (module_sp)
Greg Claytone9f49822011-09-09 20:33:05 +0000205 {
Greg Clayton9482f052012-03-13 23:14:29 +0000206 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
207 m_sect_headers.clear();
208 m_data.SetByteOrder (eByteOrderLittle);
209 uint32_t offset = 0;
210
211 if (ParseDOSHeader())
Greg Clayton707e3402011-09-10 01:04:42 +0000212 {
Greg Clayton9482f052012-03-13 23:14:29 +0000213 offset = m_dos_header.e_lfanew;
214 uint32_t pe_signature = m_data.GetU32 (&offset);
215 if (pe_signature != IMAGE_NT_SIGNATURE)
216 return false;
217 if (ParseCOFFHeader(&offset))
218 {
219 if (m_coff_header.hdrsize > 0)
220 ParseCOFFOptionalHeader(&offset);
221 ParseSectionHeaders (offset);
222 }
223 StreamFile s(stdout, false);// REMOVE THIS LINE!!!
224 Dump(&s);// REMOVE THIS LINE!!!
225 return true;
Greg Clayton707e3402011-09-10 01:04:42 +0000226 }
Greg Claytone9f49822011-09-09 20:33:05 +0000227 }
228 return false;
229}
230
231
232ByteOrder
233ObjectFilePECOFF::GetByteOrder () const
234{
235 return eByteOrderLittle;
236}
237
238bool
239ObjectFilePECOFF::IsExecutable() const
240{
241 return (m_coff_header.flags & IMAGE_FILE_DLL) == 0;
242}
243
244size_t
245ObjectFilePECOFF::GetAddressByteSize () const
246{
247 if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32_PLUS)
248 return 8;
249 else if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32)
250 return 4;
251 return 4;
252}
253
254//----------------------------------------------------------------------
255// NeedsEndianSwap
256//
257// Return true if an endian swap needs to occur when extracting data
258// from this file.
259//----------------------------------------------------------------------
260bool
261ObjectFilePECOFF::NeedsEndianSwap() const
262{
263#if defined(__LITTLE_ENDIAN__)
264 return false;
265#else
266 return true;
267#endif
268}
269//----------------------------------------------------------------------
270// ParseDOSHeader
271//----------------------------------------------------------------------
272bool
273ObjectFilePECOFF::ParseDOSHeader ()
274{
275 bool success = false;
276 uint32_t offset = 0;
277 success = m_data.ValidOffsetForDataOfSize(0, sizeof(m_dos_header));
278
279 if (success)
280 {
281 m_dos_header.e_magic = m_data.GetU16(&offset); // Magic number
282 success = m_dos_header.e_magic == IMAGE_DOS_SIGNATURE;
283
284 if (success)
285 {
286 m_dos_header.e_cblp = m_data.GetU16(&offset); // Bytes on last page of file
287 m_dos_header.e_cp = m_data.GetU16(&offset); // Pages in file
288 m_dos_header.e_crlc = m_data.GetU16(&offset); // Relocations
289 m_dos_header.e_cparhdr = m_data.GetU16(&offset); // Size of header in paragraphs
290 m_dos_header.e_minalloc = m_data.GetU16(&offset); // Minimum extra paragraphs needed
291 m_dos_header.e_maxalloc = m_data.GetU16(&offset); // Maximum extra paragraphs needed
292 m_dos_header.e_ss = m_data.GetU16(&offset); // Initial (relative) SS value
293 m_dos_header.e_sp = m_data.GetU16(&offset); // Initial SP value
294 m_dos_header.e_csum = m_data.GetU16(&offset); // Checksum
295 m_dos_header.e_ip = m_data.GetU16(&offset); // Initial IP value
296 m_dos_header.e_cs = m_data.GetU16(&offset); // Initial (relative) CS value
297 m_dos_header.e_lfarlc = m_data.GetU16(&offset); // File address of relocation table
298 m_dos_header.e_ovno = m_data.GetU16(&offset); // Overlay number
299
300 m_dos_header.e_res[0] = m_data.GetU16(&offset); // Reserved words
301 m_dos_header.e_res[1] = m_data.GetU16(&offset); // Reserved words
302 m_dos_header.e_res[2] = m_data.GetU16(&offset); // Reserved words
303 m_dos_header.e_res[3] = m_data.GetU16(&offset); // Reserved words
304
305 m_dos_header.e_oemid = m_data.GetU16(&offset); // OEM identifier (for e_oeminfo)
306 m_dos_header.e_oeminfo = m_data.GetU16(&offset); // OEM information; e_oemid specific
307 m_dos_header.e_res2[0] = m_data.GetU16(&offset); // Reserved words
308 m_dos_header.e_res2[1] = m_data.GetU16(&offset); // Reserved words
309 m_dos_header.e_res2[2] = m_data.GetU16(&offset); // Reserved words
310 m_dos_header.e_res2[3] = m_data.GetU16(&offset); // Reserved words
311 m_dos_header.e_res2[4] = m_data.GetU16(&offset); // Reserved words
312 m_dos_header.e_res2[5] = m_data.GetU16(&offset); // Reserved words
313 m_dos_header.e_res2[6] = m_data.GetU16(&offset); // Reserved words
314 m_dos_header.e_res2[7] = m_data.GetU16(&offset); // Reserved words
315 m_dos_header.e_res2[8] = m_data.GetU16(&offset); // Reserved words
316 m_dos_header.e_res2[9] = m_data.GetU16(&offset); // Reserved words
317
318 m_dos_header.e_lfanew = m_data.GetU32(&offset); // File address of new exe header
319 }
320 }
321 if (!success)
322 memset(&m_dos_header, 0, sizeof(m_dos_header));
323 return success;
324}
325
326
327//----------------------------------------------------------------------
328// ParserCOFFHeader
329//----------------------------------------------------------------------
330bool
331ObjectFilePECOFF::ParseCOFFHeader(uint32_t* offset_ptr)
332{
333 bool success = m_data.ValidOffsetForDataOfSize (*offset_ptr, sizeof(m_coff_header));
334 if (success)
335 {
336 m_coff_header.machine = m_data.GetU16(offset_ptr);
337 m_coff_header.nsects = m_data.GetU16(offset_ptr);
338 m_coff_header.modtime = m_data.GetU32(offset_ptr);
339 m_coff_header.symoff = m_data.GetU32(offset_ptr);
340 m_coff_header.nsyms = m_data.GetU32(offset_ptr);
341 m_coff_header.hdrsize = m_data.GetU16(offset_ptr);
342 m_coff_header.flags = m_data.GetU16(offset_ptr);
343 }
344 if (!success)
345 memset(&m_coff_header, 0, sizeof(m_coff_header));
346 return success;
347}
348
349bool
350ObjectFilePECOFF::ParseCOFFOptionalHeader(uint32_t* offset_ptr)
351{
352 bool success = false;
353 const uint32_t end_offset = *offset_ptr + m_coff_header.hdrsize;
354 if (*offset_ptr < end_offset)
355 {
356 success = true;
357 m_coff_header_opt.magic = m_data.GetU16(offset_ptr);
358 m_coff_header_opt.major_linker_version = m_data.GetU8 (offset_ptr);
359 m_coff_header_opt.minor_linker_version = m_data.GetU8 (offset_ptr);
360 m_coff_header_opt.code_size = m_data.GetU32(offset_ptr);
361 m_coff_header_opt.data_size = m_data.GetU32(offset_ptr);
362 m_coff_header_opt.bss_size = m_data.GetU32(offset_ptr);
363 m_coff_header_opt.entry = m_data.GetU32(offset_ptr);
364 m_coff_header_opt.code_offset = m_data.GetU32(offset_ptr);
365
366 const uint32_t addr_byte_size = GetAddressByteSize ();
367
368 if (*offset_ptr < end_offset)
369 {
370 if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32)
371 {
372 // PE32 only
373 m_coff_header_opt.data_offset = m_data.GetU32(offset_ptr);
374 }
375 else
376 m_coff_header_opt.data_offset = 0;
377
378 if (*offset_ptr < end_offset)
379 {
380 m_coff_header_opt.image_base = m_data.GetMaxU64 (offset_ptr, addr_byte_size);
381 m_coff_header_opt.sect_alignment = m_data.GetU32(offset_ptr);
382 m_coff_header_opt.file_alignment = m_data.GetU32(offset_ptr);
383 m_coff_header_opt.major_os_system_version = m_data.GetU16(offset_ptr);
384 m_coff_header_opt.minor_os_system_version = m_data.GetU16(offset_ptr);
385 m_coff_header_opt.major_image_version = m_data.GetU16(offset_ptr);
386 m_coff_header_opt.minor_image_version = m_data.GetU16(offset_ptr);
387 m_coff_header_opt.major_subsystem_version = m_data.GetU16(offset_ptr);
388 m_coff_header_opt.minor_subsystem_version = m_data.GetU16(offset_ptr);
389 m_coff_header_opt.reserved1 = m_data.GetU32(offset_ptr);
390 m_coff_header_opt.image_size = m_data.GetU32(offset_ptr);
391 m_coff_header_opt.header_size = m_data.GetU32(offset_ptr);
Greg Clayton707e3402011-09-10 01:04:42 +0000392 m_coff_header_opt.checksum = m_data.GetU32(offset_ptr);
Greg Claytone9f49822011-09-09 20:33:05 +0000393 m_coff_header_opt.subsystem = m_data.GetU16(offset_ptr);
394 m_coff_header_opt.dll_flags = m_data.GetU16(offset_ptr);
395 m_coff_header_opt.stack_reserve_size = m_data.GetMaxU64 (offset_ptr, addr_byte_size);
396 m_coff_header_opt.stack_commit_size = m_data.GetMaxU64 (offset_ptr, addr_byte_size);
397 m_coff_header_opt.heap_reserve_size = m_data.GetMaxU64 (offset_ptr, addr_byte_size);
398 m_coff_header_opt.heap_commit_size = m_data.GetMaxU64 (offset_ptr, addr_byte_size);
399 m_coff_header_opt.loader_flags = m_data.GetU32(offset_ptr);
400 uint32_t num_data_dir_entries = m_data.GetU32(offset_ptr);
401 m_coff_header_opt.data_dirs.clear();
402 m_coff_header_opt.data_dirs.resize(num_data_dir_entries);
403 uint32_t i;
404 for (i=0; i<num_data_dir_entries; i++)
405 {
406 m_coff_header_opt.data_dirs[i].vmaddr = m_data.GetU32(offset_ptr);
407 m_coff_header_opt.data_dirs[i].vmsize = m_data.GetU32(offset_ptr);
408 }
409 }
410 }
411 }
412 // Make sure we are on track for section data which follows
413 *offset_ptr = end_offset;
414 return success;
415}
416
417
418//----------------------------------------------------------------------
419// ParseSectionHeaders
420//----------------------------------------------------------------------
421bool
422ObjectFilePECOFF::ParseSectionHeaders (uint32_t section_header_data_offset)
423{
424 const uint32_t nsects = m_coff_header.nsects;
425 m_sect_headers.clear();
426
427 if (nsects > 0)
428 {
429 const uint32_t addr_byte_size = GetAddressByteSize ();
430 const size_t section_header_byte_size = nsects * sizeof(section_header_t);
431 DataBufferSP section_header_data_sp(m_file.ReadFileContents (section_header_data_offset, section_header_byte_size));
432 DataExtractor section_header_data (section_header_data_sp, GetByteOrder(), addr_byte_size);
433
434 uint32_t offset = 0;
435 if (section_header_data.ValidOffsetForDataOfSize (offset, section_header_byte_size))
436 {
437 m_sect_headers.resize(nsects);
438
439 for (uint32_t idx = 0; idx<nsects; ++idx)
440 {
441 const void *name_data = section_header_data.GetData(&offset, 8);
442 if (name_data)
443 {
444 memcpy(m_sect_headers[idx].name, name_data, 8);
445 m_sect_headers[idx].vmsize = section_header_data.GetU32(&offset);
446 m_sect_headers[idx].vmaddr = section_header_data.GetU32(&offset);
447 m_sect_headers[idx].size = section_header_data.GetU32(&offset);
448 m_sect_headers[idx].offset = section_header_data.GetU32(&offset);
449 m_sect_headers[idx].reloff = section_header_data.GetU32(&offset);
450 m_sect_headers[idx].lineoff = section_header_data.GetU32(&offset);
451 m_sect_headers[idx].nreloc = section_header_data.GetU16(&offset);
452 m_sect_headers[idx].nline = section_header_data.GetU16(&offset);
453 m_sect_headers[idx].flags = section_header_data.GetU32(&offset);
454 }
455 }
456 }
457 }
458
459 return m_sect_headers.empty() == false;
460}
461
462bool
463ObjectFilePECOFF::GetSectionName(std::string& sect_name, const section_header_t& sect)
464{
465 if (sect.name[0] == '/')
466 {
467 uint32_t stroff = strtoul(&sect.name[1], NULL, 10);
468 uint32_t string_file_offset = m_coff_header.symoff + (m_coff_header.nsyms * 18) + stroff;
469 const char *name = m_data.GetCStr (&string_file_offset);
470 if (name)
471 {
472 sect_name = name;
473 return true;
474 }
475
476 return false;
477 }
478 sect_name = sect.name;
479 return true;
480}
481
482//----------------------------------------------------------------------
483// GetNListSymtab
484//----------------------------------------------------------------------
485Symtab *
486ObjectFilePECOFF::GetSymtab()
487{
Greg Clayton9482f052012-03-13 23:14:29 +0000488 ModuleSP module_sp(GetModule());
489 if (module_sp)
Greg Claytone9f49822011-09-09 20:33:05 +0000490 {
Greg Clayton9482f052012-03-13 23:14:29 +0000491 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
492 if (m_symtab_ap.get() == NULL)
Greg Claytone9f49822011-09-09 20:33:05 +0000493 {
Greg Clayton9482f052012-03-13 23:14:29 +0000494 SectionList *sect_list = GetSectionList();
495 m_symtab_ap.reset(new Symtab(this));
496 Mutex::Locker symtab_locker (m_symtab_ap->GetMutex());
Greg Claytone9f49822011-09-09 20:33:05 +0000497
Greg Clayton9482f052012-03-13 23:14:29 +0000498 const uint32_t num_syms = m_coff_header.nsyms;
499
500 if (num_syms > 0 && m_coff_header.symoff > 0)
501 {
502 const uint32_t symbol_size = sizeof(section_header_t);
503 const uint32_t addr_byte_size = GetAddressByteSize ();
504 const size_t symbol_data_size = num_syms * symbol_size;
505 // Include the 4 bytes string table size at the end of the symbols
506 DataBufferSP symtab_data_sp(m_file.ReadFileContents (m_coff_header.symoff, symbol_data_size + 4));
507 DataExtractor symtab_data (symtab_data_sp, GetByteOrder(), addr_byte_size);
508 uint32_t offset = symbol_data_size;
509 const uint32_t strtab_size = symtab_data.GetU32 (&offset);
510 DataBufferSP strtab_data_sp(m_file.ReadFileContents (m_coff_header.symoff + symbol_data_size + 4, strtab_size));
511 DataExtractor strtab_data (strtab_data_sp, GetByteOrder(), addr_byte_size);
512
513 offset = 0;
514 std::string symbol_name;
515 Symbol *symbols = m_symtab_ap->Resize (num_syms);
516 for (uint32_t i=0; i<num_syms; ++i)
517 {
518 coff_symbol_t symbol;
519 const uint32_t symbol_offset = offset;
520 const char *symbol_name_cstr = NULL;
521 // If the first 4 bytes of the symbol string are zero, then we
522 // it is followed by a 4 byte string table offset. Else these
523 // 8 bytes contain the symbol name
524 if (symtab_data.GetU32 (&offset) == 0)
525 {
526 // Long string that doesn't fit into the symbol table name,
527 // so now we must read the 4 byte string table offset
528 uint32_t strtab_offset = symtab_data.GetU32 (&offset);
529 symbol_name_cstr = strtab_data.PeekCStr (strtab_offset);
530 symbol_name.assign (symbol_name_cstr);
531 }
532 else
533 {
534 // Short string that fits into the symbol table name which is 8 bytes
535 offset += sizeof(symbol.name) - 4; // Skip remaining
536 symbol_name_cstr = symtab_data.PeekCStr (symbol_offset);
537 if (symbol_name_cstr == NULL)
538 break;
539 symbol_name.assign (symbol_name_cstr, sizeof(symbol.name));
540 }
541 symbol.value = symtab_data.GetU32 (&offset);
542 symbol.sect = symtab_data.GetU16 (&offset);
543 symbol.type = symtab_data.GetU16 (&offset);
544 symbol.storage = symtab_data.GetU8 (&offset);
545 symbol.naux = symtab_data.GetU8 (&offset);
546 Address symbol_addr(sect_list->GetSectionAtIndex(symbol.sect-1), symbol.value);
547 symbols[i].GetMangled ().SetValue (symbol_name.c_str(), symbol_name[0]=='_' && symbol_name[1] == 'Z');
548 symbols[i].GetAddress() = symbol_addr;
549
550 if (symbol.naux > 0)
551 i += symbol.naux;
552 }
553
554 }
Greg Claytone9f49822011-09-09 20:33:05 +0000555 }
556 }
557 return m_symtab_ap.get();
558
559}
560
561SectionList *
562ObjectFilePECOFF::GetSectionList()
563{
Greg Clayton9482f052012-03-13 23:14:29 +0000564 ModuleSP module_sp(GetModule());
565 if (module_sp)
Greg Claytone9f49822011-09-09 20:33:05 +0000566 {
Greg Clayton9482f052012-03-13 23:14:29 +0000567 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
568 if (m_sections_ap.get() == NULL)
Greg Claytone9f49822011-09-09 20:33:05 +0000569 {
Greg Clayton9482f052012-03-13 23:14:29 +0000570 m_sections_ap.reset(new SectionList());
571 const uint32_t nsects = m_sect_headers.size();
572 ModuleSP module_sp (GetModule());
573 for (uint32_t idx = 0; idx<nsects; ++idx)
Greg Clayton707e3402011-09-10 01:04:42 +0000574 {
Greg Clayton9482f052012-03-13 23:14:29 +0000575 std::string sect_name;
576 GetSectionName (sect_name, m_sect_headers[idx]);
577 ConstString const_sect_name (sect_name.c_str());
578 static ConstString g_code_sect_name (".code");
579 static ConstString g_CODE_sect_name ("CODE");
580 static ConstString g_data_sect_name (".data");
581 static ConstString g_DATA_sect_name ("DATA");
582 static ConstString g_bss_sect_name (".bss");
583 static ConstString g_BSS_sect_name ("BSS");
584 static ConstString g_debug_sect_name (".debug");
585 static ConstString g_reloc_sect_name (".reloc");
586 static ConstString g_stab_sect_name (".stab");
587 static ConstString g_stabstr_sect_name (".stabstr");
588 SectionType section_type = eSectionTypeOther;
589 if (m_sect_headers[idx].flags & IMAGE_SCN_CNT_CODE &&
590 ((const_sect_name == g_code_sect_name) || (const_sect_name == g_CODE_sect_name)))
591 {
592 section_type = eSectionTypeCode;
593 }
594 else if (m_sect_headers[idx].flags & IMAGE_SCN_CNT_INITIALIZED_DATA &&
595 ((const_sect_name == g_data_sect_name) || (const_sect_name == g_DATA_sect_name)))
596 {
Greg Clayton707e3402011-09-10 01:04:42 +0000597 section_type = eSectionTypeData;
Greg Clayton9482f052012-03-13 23:14:29 +0000598 }
599 else if (m_sect_headers[idx].flags & IMAGE_SCN_CNT_UNINITIALIZED_DATA &&
600 ((const_sect_name == g_bss_sect_name) || (const_sect_name == g_BSS_sect_name)))
601 {
602 if (m_sect_headers[idx].size == 0)
603 section_type = eSectionTypeZeroFill;
604 else
605 section_type = eSectionTypeData;
606 }
607 else if (const_sect_name == g_debug_sect_name)
608 {
609 section_type = eSectionTypeDebug;
610 }
611 else if (const_sect_name == g_stabstr_sect_name)
612 {
613 section_type = eSectionTypeDataCString;
614 }
615 else if (const_sect_name == g_reloc_sect_name)
616 {
617 section_type = eSectionTypeOther;
618 }
619 else if (m_sect_headers[idx].flags & IMAGE_SCN_CNT_CODE)
620 {
621 section_type = eSectionTypeCode;
622 }
623 else if (m_sect_headers[idx].flags & IMAGE_SCN_CNT_INITIALIZED_DATA)
624 {
Greg Clayton707e3402011-09-10 01:04:42 +0000625 section_type = eSectionTypeData;
Greg Clayton9482f052012-03-13 23:14:29 +0000626 }
627 else if (m_sect_headers[idx].flags & IMAGE_SCN_CNT_UNINITIALIZED_DATA)
628 {
629 if (m_sect_headers[idx].size == 0)
630 section_type = eSectionTypeZeroFill;
631 else
632 section_type = eSectionTypeData;
633 }
634
635 // Use a segment ID of the segment index shifted left by 8 so they
636 // never conflict with any of the sections.
637 SectionSP section_sp (new Section (module_sp, // Module to which this section belongs
638 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
639 const_sect_name, // Name of this section
640 section_type, // This section is a container of other sections.
641 m_sect_headers[idx].vmaddr, // File VM address == addresses as they are found in the object file
642 m_sect_headers[idx].vmsize, // VM size in bytes of this section
643 m_sect_headers[idx].offset, // Offset to the data for this section in the file
644 m_sect_headers[idx].size, // Size in bytes of this section as found in the the file
645 m_sect_headers[idx].flags)); // Flags for this section
646
647 //section_sp->SetIsEncrypted (segment_is_encrypted);
648
649 m_sections_ap->AddSection(section_sp);
Greg Clayton707e3402011-09-10 01:04:42 +0000650 }
Greg Claytone9f49822011-09-09 20:33:05 +0000651 }
652 }
653 return m_sections_ap.get();
654}
655
656bool
657ObjectFilePECOFF::GetUUID (UUID* uuid)
658{
659 return false;
660}
661
662uint32_t
663ObjectFilePECOFF::GetDependentModules (FileSpecList& files)
664{
665 return 0;
666}
667
668
669//----------------------------------------------------------------------
670// Dump
671//
672// Dump the specifics of the runtime file container (such as any headers
673// segments, sections, etc).
674//----------------------------------------------------------------------
675void
676ObjectFilePECOFF::Dump(Stream *s)
677{
Greg Clayton9482f052012-03-13 23:14:29 +0000678 ModuleSP module_sp(GetModule());
679 if (module_sp)
Greg Claytone9f49822011-09-09 20:33:05 +0000680 {
Greg Clayton9482f052012-03-13 23:14:29 +0000681 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
682 s->Printf("%p: ", this);
683 s->Indent();
684 s->PutCString("ObjectFilePECOFF");
685
686 ArchSpec header_arch;
687 GetArchitecture (header_arch);
688
689 *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
690
691 if (m_sections_ap.get())
692 m_sections_ap->Dump(s, NULL, true, UINT32_MAX);
693
694 if (m_symtab_ap.get())
695 m_symtab_ap->Dump(s, NULL, eSortOrderNone);
696
697 if (m_dos_header.e_magic)
698 DumpDOSHeader (s, m_dos_header);
699 if (m_coff_header.machine)
700 {
701 DumpCOFFHeader (s, m_coff_header);
702 if (m_coff_header.hdrsize)
703 DumpOptCOFFHeader (s, m_coff_header_opt);
704 }
705 s->EOL();
706 DumpSectionHeaders(s);
707 s->EOL();
Greg Claytone9f49822011-09-09 20:33:05 +0000708 }
Greg Claytone9f49822011-09-09 20:33:05 +0000709}
710
711//----------------------------------------------------------------------
712// DumpDOSHeader
713//
714// Dump the MS-DOS header to the specified output stream
715//----------------------------------------------------------------------
716void
717ObjectFilePECOFF::DumpDOSHeader(Stream *s, const dos_header_t& header)
718{
719 s->PutCString ("MSDOS Header\n");
720 s->Printf (" e_magic = 0x%4.4x\n", header.e_magic);
721 s->Printf (" e_cblp = 0x%4.4x\n", header.e_cblp);
722 s->Printf (" e_cp = 0x%4.4x\n", header.e_cp);
723 s->Printf (" e_crlc = 0x%4.4x\n", header.e_crlc);
724 s->Printf (" e_cparhdr = 0x%4.4x\n", header.e_cparhdr);
725 s->Printf (" e_minalloc = 0x%4.4x\n", header.e_minalloc);
726 s->Printf (" e_maxalloc = 0x%4.4x\n", header.e_maxalloc);
727 s->Printf (" e_ss = 0x%4.4x\n", header.e_ss);
728 s->Printf (" e_sp = 0x%4.4x\n", header.e_sp);
729 s->Printf (" e_csum = 0x%4.4x\n", header.e_csum);
730 s->Printf (" e_ip = 0x%4.4x\n", header.e_ip);
731 s->Printf (" e_cs = 0x%4.4x\n", header.e_cs);
732 s->Printf (" e_lfarlc = 0x%4.4x\n", header.e_lfarlc);
733 s->Printf (" e_ovno = 0x%4.4x\n", header.e_ovno);
734 s->Printf (" e_res[4] = { 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x }\n",
735 header.e_res[0],
736 header.e_res[1],
737 header.e_res[2],
738 header.e_res[3]);
739 s->Printf (" e_oemid = 0x%4.4x\n", header.e_oemid);
740 s->Printf (" e_oeminfo = 0x%4.4x\n", header.e_oeminfo);
741 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",
742 header.e_res2[0],
743 header.e_res2[1],
744 header.e_res2[2],
745 header.e_res2[3],
746 header.e_res2[4],
747 header.e_res2[5],
748 header.e_res2[6],
749 header.e_res2[7],
750 header.e_res2[8],
751 header.e_res2[9]);
752 s->Printf (" e_lfanew = 0x%8.8x\n", header.e_lfanew);
753}
754
755//----------------------------------------------------------------------
756// DumpCOFFHeader
757//
758// Dump the COFF header to the specified output stream
759//----------------------------------------------------------------------
760void
761ObjectFilePECOFF::DumpCOFFHeader(Stream *s, const coff_header_t& header)
762{
763 s->PutCString ("COFF Header\n");
764 s->Printf (" machine = 0x%4.4x\n", header.machine);
765 s->Printf (" nsects = 0x%4.4x\n", header.nsects);
766 s->Printf (" modtime = 0x%8.8x\n", header.modtime);
767 s->Printf (" symoff = 0x%8.8x\n", header.symoff);
768 s->Printf (" nsyms = 0x%8.8x\n", header.nsyms);
769 s->Printf (" hdrsize = 0x%4.4x\n", header.hdrsize);
770}
771
772//----------------------------------------------------------------------
773// DumpOptCOFFHeader
774//
775// Dump the optional COFF header to the specified output stream
776//----------------------------------------------------------------------
777void
778ObjectFilePECOFF::DumpOptCOFFHeader(Stream *s, const coff_opt_header_t& header)
779{
780 s->PutCString ("Optional COFF Header\n");
781 s->Printf (" magic = 0x%4.4x\n", header.magic);
782 s->Printf (" major_linker_version = 0x%2.2x\n", header.major_linker_version);
783 s->Printf (" minor_linker_version = 0x%2.2x\n", header.minor_linker_version);
784 s->Printf (" code_size = 0x%8.8x\n", header.code_size);
785 s->Printf (" data_size = 0x%8.8x\n", header.data_size);
786 s->Printf (" bss_size = 0x%8.8x\n", header.bss_size);
787 s->Printf (" entry = 0x%8.8x\n", header.entry);
788 s->Printf (" code_offset = 0x%8.8x\n", header.code_offset);
789 s->Printf (" data_offset = 0x%8.8x\n", header.data_offset);
790 s->Printf (" image_base = 0x%16.16llx\n", header.image_base);
791 s->Printf (" sect_alignment = 0x%8.8x\n", header.sect_alignment);
792 s->Printf (" file_alignment = 0x%8.8x\n", header.file_alignment);
793 s->Printf (" major_os_system_version = 0x%4.4x\n", header.major_os_system_version);
794 s->Printf (" minor_os_system_version = 0x%4.4x\n", header.minor_os_system_version);
795 s->Printf (" major_image_version = 0x%4.4x\n", header.major_image_version);
796 s->Printf (" minor_image_version = 0x%4.4x\n", header.minor_image_version);
797 s->Printf (" major_subsystem_version = 0x%4.4x\n", header.major_subsystem_version);
798 s->Printf (" minor_subsystem_version = 0x%4.4x\n", header.minor_subsystem_version);
799 s->Printf (" reserved1 = 0x%8.8x\n", header.reserved1);
800 s->Printf (" image_size = 0x%8.8x\n", header.image_size);
801 s->Printf (" header_size = 0x%8.8x\n", header.header_size);
Greg Clayton707e3402011-09-10 01:04:42 +0000802 s->Printf (" checksum = 0x%8.8x\n", header.checksum);
Greg Claytone9f49822011-09-09 20:33:05 +0000803 s->Printf (" subsystem = 0x%4.4x\n", header.subsystem);
804 s->Printf (" dll_flags = 0x%4.4x\n", header.dll_flags);
805 s->Printf (" stack_reserve_size = 0x%16.16llx\n", header.stack_reserve_size);
806 s->Printf (" stack_commit_size = 0x%16.16llx\n", header.stack_commit_size);
807 s->Printf (" heap_reserve_size = 0x%16.16llx\n", header.heap_reserve_size);
808 s->Printf (" heap_commit_size = 0x%16.16llx\n", header.heap_commit_size);
809 s->Printf (" loader_flags = 0x%8.8x\n", header.loader_flags);
810 s->Printf (" num_data_dir_entries = 0x%8.8zx\n", header.data_dirs.size());
811 uint32_t i;
812 for (i=0; i<header.data_dirs.size(); i++)
813 {
Greg Clayton707e3402011-09-10 01:04:42 +0000814 s->Printf (" data_dirs[%2u] vmaddr = 0x%8.8x, vmsize = 0x%8.8x\n",
Greg Claytone9f49822011-09-09 20:33:05 +0000815 i,
816 header.data_dirs[i].vmaddr,
817 header.data_dirs[i].vmsize);
818 }
819}
820//----------------------------------------------------------------------
821// DumpSectionHeader
822//
823// Dump a single ELF section header to the specified output stream
824//----------------------------------------------------------------------
825void
826ObjectFilePECOFF::DumpSectionHeader(Stream *s, const section_header_t& sh)
827{
828 std::string name;
829 GetSectionName(name, sh);
830 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",
831 name.c_str(),
Greg Claytone9f49822011-09-09 20:33:05 +0000832 sh.vmaddr,
Greg Clayton707e3402011-09-10 01:04:42 +0000833 sh.vmsize,
Greg Claytone9f49822011-09-09 20:33:05 +0000834 sh.offset,
Greg Clayton707e3402011-09-10 01:04:42 +0000835 sh.size,
Greg Claytone9f49822011-09-09 20:33:05 +0000836 sh.reloff,
837 sh.lineoff,
838 sh.nreloc,
839 sh.nline,
840 sh.flags);
841}
842
843
844//----------------------------------------------------------------------
845// DumpSectionHeaders
846//
847// Dump all of the ELF section header to the specified output stream
848//----------------------------------------------------------------------
849void
850ObjectFilePECOFF::DumpSectionHeaders(Stream *s)
851{
852
853 s->PutCString ("Section Headers\n");
Greg Clayton707e3402011-09-10 01:04:42 +0000854 s->PutCString ("IDX name vm addr vm size file off file size reloc off line off nreloc nline flags\n");
855 s->PutCString ("==== ---------------- ---------- ---------- ---------- ---------- ---------- ---------- ------ ------ ----------\n");
Greg Claytone9f49822011-09-09 20:33:05 +0000856
857 uint32_t idx = 0;
858 SectionHeaderCollIter pos, end = m_sect_headers.end();
859
860 for (pos = m_sect_headers.begin(); pos != end; ++pos, ++idx)
861 {
Greg Clayton707e3402011-09-10 01:04:42 +0000862 s->Printf ("[%2u] ", idx);
Greg Claytone9f49822011-09-09 20:33:05 +0000863 ObjectFilePECOFF::DumpSectionHeader(s, *pos);
864 }
865}
866
867static bool
868COFFMachineToMachCPU (uint16_t machine, ArchSpec &arch)
869{
870 switch (machine)
871 {
872 case IMAGE_FILE_MACHINE_AMD64:
873 case IMAGE_FILE_MACHINE_IA64:
874 arch.SetArchitecture (eArchTypeMachO,
875 llvm::MachO::CPUTypeX86_64,
876 llvm::MachO::CPUSubType_X86_64_ALL);
877 return true;
878
879 case IMAGE_FILE_MACHINE_I386:
880 arch.SetArchitecture (eArchTypeMachO,
881 llvm::MachO::CPUTypeI386,
882 llvm::MachO::CPUSubType_I386_ALL);
883 return true;
884
885 case IMAGE_FILE_MACHINE_POWERPC:
886 case IMAGE_FILE_MACHINE_POWERPCFP:
887 arch.SetArchitecture (eArchTypeMachO,
888 llvm::MachO::CPUTypePowerPC,
889 llvm::MachO::CPUSubType_POWERPC_ALL);
890 return true;
891 case IMAGE_FILE_MACHINE_ARM:
892 case IMAGE_FILE_MACHINE_THUMB:
893 arch.SetArchitecture (eArchTypeMachO,
894 llvm::MachO::CPUTypeARM,
895 llvm::MachO::CPUSubType_ARM_V7);
896 return true;
897 }
898 return false;
899}
900bool
901ObjectFilePECOFF::GetArchitecture (ArchSpec &arch)
902{
903 // For index zero return our cpu type
904 return COFFMachineToMachCPU (m_coff_header.machine, arch);
905}
906
907ObjectFile::Type
908ObjectFilePECOFF::CalculateType()
909{
910 if (m_coff_header.machine != 0)
911 {
912 if ((m_coff_header.flags & IMAGE_FILE_DLL) == 0)
913 return eTypeExecutable;
914 else
915 return eTypeSharedLibrary;
916 }
917 return eTypeExecutable;
918}
919
920ObjectFile::Strata
921ObjectFilePECOFF::CalculateStrata()
922{
923 return eStrataUser;
924}
925//------------------------------------------------------------------
926// PluginInterface protocol
927//------------------------------------------------------------------
928const char *
929ObjectFilePECOFF::GetPluginName()
930{
931 return "ObjectFilePECOFF";
932}
933
934const char *
935ObjectFilePECOFF::GetShortPluginName()
936{
937 return GetPluginNameStatic();
938}
939
940uint32_t
941ObjectFilePECOFF::GetPluginVersion()
942{
943 return 1;
944}
945