blob: 37afed76c37008d3a4bcfe2d0605ac815d3cb9d5 [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"
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
75using namespace lldb;
76using namespace lldb_private;
77
78void
79ObjectFilePECOFF::Initialize()
80{
81 PluginManager::RegisterPlugin (GetPluginNameStatic(),
82 GetPluginDescriptionStatic(),
83 CreateInstance);
84}
85
86void
87ObjectFilePECOFF::Terminate()
88{
89 PluginManager::UnregisterPlugin (CreateInstance);
90}
91
92
93const char *
94ObjectFilePECOFF::GetPluginNameStatic()
95{
96 return "object-file.pe-coff";
97}
98
99const char *
100ObjectFilePECOFF::GetPluginDescriptionStatic()
101{
102 return "Portable Executable and Common Object File Format object file reader (32 and 64 bit)";
103}
104
105
106ObjectFile *
107ObjectFilePECOFF::CreateInstance (Module* module, DataBufferSP& dataSP, const FileSpec* file, addr_t offset, addr_t length)
108{
109 if (ObjectFilePECOFF::MagicBytesMatch(dataSP))
110 {
111 std::auto_ptr<ObjectFile> objfile_ap(new ObjectFilePECOFF (module, dataSP, file, offset, length));
112 if (objfile_ap.get() && objfile_ap->ParseHeader())
113 return objfile_ap.release();
114 }
115 return NULL;
116}
117
118bool
119ObjectFilePECOFF::MagicBytesMatch (DataBufferSP& dataSP)
120{
121 DataExtractor data(dataSP, eByteOrderLittle, 4);
122 uint32_t offset = 0;
123 uint16_t magic = data.GetU16 (&offset);
124 return magic == IMAGE_DOS_SIGNATURE;
125}
126
127
128ObjectFilePECOFF::ObjectFilePECOFF (Module* module,
129 DataBufferSP& dataSP,
130 const FileSpec* file,
131 addr_t offset,
132 addr_t length) :
133 ObjectFile (module, file, offset, length, dataSP),
134 m_mutex (Mutex::eMutexTypeRecursive),
135 m_dos_header (),
136 m_coff_header (),
137 m_coff_header_opt (),
138 m_sect_headers ()
139{
140 ::memset (&m_dos_header, 0, sizeof(m_dos_header));
141 ::memset (&m_coff_header, 0, sizeof(m_coff_header));
142 ::memset (&m_coff_header_opt, 0, sizeof(m_coff_header_opt));
143}
144
145
146ObjectFilePECOFF::~ObjectFilePECOFF()
147{
148}
149
150
151bool
152ObjectFilePECOFF::ParseHeader ()
153{
154 Mutex::Locker locker(m_mutex);
155 m_sect_headers.clear();
156 m_data.SetByteOrder (eByteOrderLittle);
157 uint32_t offset = 0;
158
159 if (ParseDOSHeader())
160 {
161 offset = m_dos_header.e_lfanew;
162 uint32_t pe_signature = m_data.GetU32 (&offset);
163 if (pe_signature != IMAGE_NT_SIGNATURE)
164 return false;
165 }
166 if (ParseCOFFHeader(&offset))
167 {
168 if (m_coff_header.hdrsize > 0)
169 ParseCOFFOptionalHeader(&offset);
170 ParseSectionHeaders (offset);
171 return true;
172 }
173 return false;
174}
175
176
177ByteOrder
178ObjectFilePECOFF::GetByteOrder () const
179{
180 return eByteOrderLittle;
181}
182
183bool
184ObjectFilePECOFF::IsExecutable() const
185{
186 return (m_coff_header.flags & IMAGE_FILE_DLL) == 0;
187}
188
189size_t
190ObjectFilePECOFF::GetAddressByteSize () const
191{
192 if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32_PLUS)
193 return 8;
194 else if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32)
195 return 4;
196 return 4;
197}
198
199//----------------------------------------------------------------------
200// NeedsEndianSwap
201//
202// Return true if an endian swap needs to occur when extracting data
203// from this file.
204//----------------------------------------------------------------------
205bool
206ObjectFilePECOFF::NeedsEndianSwap() const
207{
208#if defined(__LITTLE_ENDIAN__)
209 return false;
210#else
211 return true;
212#endif
213}
214//----------------------------------------------------------------------
215// ParseDOSHeader
216//----------------------------------------------------------------------
217bool
218ObjectFilePECOFF::ParseDOSHeader ()
219{
220 bool success = false;
221 uint32_t offset = 0;
222 success = m_data.ValidOffsetForDataOfSize(0, sizeof(m_dos_header));
223
224 if (success)
225 {
226 m_dos_header.e_magic = m_data.GetU16(&offset); // Magic number
227 success = m_dos_header.e_magic == IMAGE_DOS_SIGNATURE;
228
229 if (success)
230 {
231 m_dos_header.e_cblp = m_data.GetU16(&offset); // Bytes on last page of file
232 m_dos_header.e_cp = m_data.GetU16(&offset); // Pages in file
233 m_dos_header.e_crlc = m_data.GetU16(&offset); // Relocations
234 m_dos_header.e_cparhdr = m_data.GetU16(&offset); // Size of header in paragraphs
235 m_dos_header.e_minalloc = m_data.GetU16(&offset); // Minimum extra paragraphs needed
236 m_dos_header.e_maxalloc = m_data.GetU16(&offset); // Maximum extra paragraphs needed
237 m_dos_header.e_ss = m_data.GetU16(&offset); // Initial (relative) SS value
238 m_dos_header.e_sp = m_data.GetU16(&offset); // Initial SP value
239 m_dos_header.e_csum = m_data.GetU16(&offset); // Checksum
240 m_dos_header.e_ip = m_data.GetU16(&offset); // Initial IP value
241 m_dos_header.e_cs = m_data.GetU16(&offset); // Initial (relative) CS value
242 m_dos_header.e_lfarlc = m_data.GetU16(&offset); // File address of relocation table
243 m_dos_header.e_ovno = m_data.GetU16(&offset); // Overlay number
244
245 m_dos_header.e_res[0] = m_data.GetU16(&offset); // Reserved words
246 m_dos_header.e_res[1] = m_data.GetU16(&offset); // Reserved words
247 m_dos_header.e_res[2] = m_data.GetU16(&offset); // Reserved words
248 m_dos_header.e_res[3] = m_data.GetU16(&offset); // Reserved words
249
250 m_dos_header.e_oemid = m_data.GetU16(&offset); // OEM identifier (for e_oeminfo)
251 m_dos_header.e_oeminfo = m_data.GetU16(&offset); // OEM information; e_oemid specific
252 m_dos_header.e_res2[0] = m_data.GetU16(&offset); // Reserved words
253 m_dos_header.e_res2[1] = m_data.GetU16(&offset); // Reserved words
254 m_dos_header.e_res2[2] = m_data.GetU16(&offset); // Reserved words
255 m_dos_header.e_res2[3] = m_data.GetU16(&offset); // Reserved words
256 m_dos_header.e_res2[4] = m_data.GetU16(&offset); // Reserved words
257 m_dos_header.e_res2[5] = m_data.GetU16(&offset); // Reserved words
258 m_dos_header.e_res2[6] = m_data.GetU16(&offset); // Reserved words
259 m_dos_header.e_res2[7] = m_data.GetU16(&offset); // Reserved words
260 m_dos_header.e_res2[8] = m_data.GetU16(&offset); // Reserved words
261 m_dos_header.e_res2[9] = m_data.GetU16(&offset); // Reserved words
262
263 m_dos_header.e_lfanew = m_data.GetU32(&offset); // File address of new exe header
264 }
265 }
266 if (!success)
267 memset(&m_dos_header, 0, sizeof(m_dos_header));
268 return success;
269}
270
271
272//----------------------------------------------------------------------
273// ParserCOFFHeader
274//----------------------------------------------------------------------
275bool
276ObjectFilePECOFF::ParseCOFFHeader(uint32_t* offset_ptr)
277{
278 bool success = m_data.ValidOffsetForDataOfSize (*offset_ptr, sizeof(m_coff_header));
279 if (success)
280 {
281 m_coff_header.machine = m_data.GetU16(offset_ptr);
282 m_coff_header.nsects = m_data.GetU16(offset_ptr);
283 m_coff_header.modtime = m_data.GetU32(offset_ptr);
284 m_coff_header.symoff = m_data.GetU32(offset_ptr);
285 m_coff_header.nsyms = m_data.GetU32(offset_ptr);
286 m_coff_header.hdrsize = m_data.GetU16(offset_ptr);
287 m_coff_header.flags = m_data.GetU16(offset_ptr);
288 }
289 if (!success)
290 memset(&m_coff_header, 0, sizeof(m_coff_header));
291 return success;
292}
293
294bool
295ObjectFilePECOFF::ParseCOFFOptionalHeader(uint32_t* offset_ptr)
296{
297 bool success = false;
298 const uint32_t end_offset = *offset_ptr + m_coff_header.hdrsize;
299 if (*offset_ptr < end_offset)
300 {
301 success = true;
302 m_coff_header_opt.magic = m_data.GetU16(offset_ptr);
303 m_coff_header_opt.major_linker_version = m_data.GetU8 (offset_ptr);
304 m_coff_header_opt.minor_linker_version = m_data.GetU8 (offset_ptr);
305 m_coff_header_opt.code_size = m_data.GetU32(offset_ptr);
306 m_coff_header_opt.data_size = m_data.GetU32(offset_ptr);
307 m_coff_header_opt.bss_size = m_data.GetU32(offset_ptr);
308 m_coff_header_opt.entry = m_data.GetU32(offset_ptr);
309 m_coff_header_opt.code_offset = m_data.GetU32(offset_ptr);
310
311 const uint32_t addr_byte_size = GetAddressByteSize ();
312
313 if (*offset_ptr < end_offset)
314 {
315 if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32)
316 {
317 // PE32 only
318 m_coff_header_opt.data_offset = m_data.GetU32(offset_ptr);
319 }
320 else
321 m_coff_header_opt.data_offset = 0;
322
323 if (*offset_ptr < end_offset)
324 {
325 m_coff_header_opt.image_base = m_data.GetMaxU64 (offset_ptr, addr_byte_size);
326 m_coff_header_opt.sect_alignment = m_data.GetU32(offset_ptr);
327 m_coff_header_opt.file_alignment = m_data.GetU32(offset_ptr);
328 m_coff_header_opt.major_os_system_version = m_data.GetU16(offset_ptr);
329 m_coff_header_opt.minor_os_system_version = m_data.GetU16(offset_ptr);
330 m_coff_header_opt.major_image_version = m_data.GetU16(offset_ptr);
331 m_coff_header_opt.minor_image_version = m_data.GetU16(offset_ptr);
332 m_coff_header_opt.major_subsystem_version = m_data.GetU16(offset_ptr);
333 m_coff_header_opt.minor_subsystem_version = m_data.GetU16(offset_ptr);
334 m_coff_header_opt.reserved1 = m_data.GetU32(offset_ptr);
335 m_coff_header_opt.image_size = m_data.GetU32(offset_ptr);
336 m_coff_header_opt.header_size = m_data.GetU32(offset_ptr);
337 m_coff_header_opt.checksym = m_data.GetU32(offset_ptr);
338 m_coff_header_opt.subsystem = m_data.GetU16(offset_ptr);
339 m_coff_header_opt.dll_flags = m_data.GetU16(offset_ptr);
340 m_coff_header_opt.stack_reserve_size = m_data.GetMaxU64 (offset_ptr, addr_byte_size);
341 m_coff_header_opt.stack_commit_size = m_data.GetMaxU64 (offset_ptr, addr_byte_size);
342 m_coff_header_opt.heap_reserve_size = m_data.GetMaxU64 (offset_ptr, addr_byte_size);
343 m_coff_header_opt.heap_commit_size = m_data.GetMaxU64 (offset_ptr, addr_byte_size);
344 m_coff_header_opt.loader_flags = m_data.GetU32(offset_ptr);
345 uint32_t num_data_dir_entries = m_data.GetU32(offset_ptr);
346 m_coff_header_opt.data_dirs.clear();
347 m_coff_header_opt.data_dirs.resize(num_data_dir_entries);
348 uint32_t i;
349 for (i=0; i<num_data_dir_entries; i++)
350 {
351 m_coff_header_opt.data_dirs[i].vmaddr = m_data.GetU32(offset_ptr);
352 m_coff_header_opt.data_dirs[i].vmsize = m_data.GetU32(offset_ptr);
353 }
354 }
355 }
356 }
357 // Make sure we are on track for section data which follows
358 *offset_ptr = end_offset;
359 return success;
360}
361
362
363//----------------------------------------------------------------------
364// ParseSectionHeaders
365//----------------------------------------------------------------------
366bool
367ObjectFilePECOFF::ParseSectionHeaders (uint32_t section_header_data_offset)
368{
369 const uint32_t nsects = m_coff_header.nsects;
370 m_sect_headers.clear();
371
372 if (nsects > 0)
373 {
374 const uint32_t addr_byte_size = GetAddressByteSize ();
375 const size_t section_header_byte_size = nsects * sizeof(section_header_t);
376 DataBufferSP section_header_data_sp(m_file.ReadFileContents (section_header_data_offset, section_header_byte_size));
377 DataExtractor section_header_data (section_header_data_sp, GetByteOrder(), addr_byte_size);
378
379 uint32_t offset = 0;
380 if (section_header_data.ValidOffsetForDataOfSize (offset, section_header_byte_size))
381 {
382 m_sect_headers.resize(nsects);
383
384 for (uint32_t idx = 0; idx<nsects; ++idx)
385 {
386 const void *name_data = section_header_data.GetData(&offset, 8);
387 if (name_data)
388 {
389 memcpy(m_sect_headers[idx].name, name_data, 8);
390 m_sect_headers[idx].vmsize = section_header_data.GetU32(&offset);
391 m_sect_headers[idx].vmaddr = section_header_data.GetU32(&offset);
392 m_sect_headers[idx].size = section_header_data.GetU32(&offset);
393 m_sect_headers[idx].offset = section_header_data.GetU32(&offset);
394 m_sect_headers[idx].reloff = section_header_data.GetU32(&offset);
395 m_sect_headers[idx].lineoff = section_header_data.GetU32(&offset);
396 m_sect_headers[idx].nreloc = section_header_data.GetU16(&offset);
397 m_sect_headers[idx].nline = section_header_data.GetU16(&offset);
398 m_sect_headers[idx].flags = section_header_data.GetU32(&offset);
399 }
400 }
401 }
402 }
403
404 return m_sect_headers.empty() == false;
405}
406
407bool
408ObjectFilePECOFF::GetSectionName(std::string& sect_name, const section_header_t& sect)
409{
410 if (sect.name[0] == '/')
411 {
412 uint32_t stroff = strtoul(&sect.name[1], NULL, 10);
413 uint32_t string_file_offset = m_coff_header.symoff + (m_coff_header.nsyms * 18) + stroff;
414 const char *name = m_data.GetCStr (&string_file_offset);
415 if (name)
416 {
417 sect_name = name;
418 return true;
419 }
420
421 return false;
422 }
423 sect_name = sect.name;
424 return true;
425}
426
427//----------------------------------------------------------------------
428// GetNListSymtab
429//----------------------------------------------------------------------
430Symtab *
431ObjectFilePECOFF::GetSymtab()
432{
433 Mutex::Locker symfile_locker(m_mutex);
434 if (m_symtab_ap.get() == NULL)
435 {
436 SectionList *sect_list = GetSectionList();
437 m_symtab_ap.reset(new Symtab(this));
438 Mutex::Locker symtab_locker (m_symtab_ap->GetMutex());
439 DataExtractor stab_data;
440 DataExtractor stabstr_data;
441 static ConstString g_stab_sect_name(".stab");
442 static ConstString g_stabstr_sect_name(".stabstr");
443 const Section *stab_section = sect_list->FindSectionByName (g_stab_sect_name).get();
444 if (stab_section == NULL)
445 return m_symtab_ap.get();
446 const Section *stabstr_section = sect_list->FindSectionByName (g_stabstr_sect_name).get();
447 if (stabstr_section == NULL)
448 return m_symtab_ap.get();
449 uint32_t offset = 0;
450 if (stab_section->ReadSectionDataFromObjectFile (this, stab_data) &&
451 stabstr_section->ReadSectionDataFromObjectFile (this, stabstr_data))
452 {
453 const uint32_t num_syms = stab_data.GetByteSize() / sizeof (coff_symbol_t);
454 Symbol *symbols = m_symtab_ap->Resize (num_syms);
455 for (uint32_t i=0; i<num_syms; ++i)
456 {
457 coff_symbol_t symbol;
458 const void *name_data = stab_data.GetData(&offset, sizeof(symbol.name));
459
460 if (name_data == NULL)
461 break;
462 memcpy (symbol.name, name_data, sizeof(symbol.name));
463 symbol.value = stab_data.GetU32 (&offset);
464 symbol.sect = stab_data.GetU16 (&offset);
465 symbol.type = stab_data.GetU16 (&offset);
466 symbol.storage = stab_data.GetU8 (&offset);
467 symbol.naux = stab_data.GetU8 (&offset);
468
469 symbol.name[sizeof(symbol.name)-1] = '\0';
470 Address symbol_addr(sect_list->GetSectionAtIndex(symbol.sect-1).get(), symbol.value);
471 symbols[i].GetMangled ().SetValue(symbol.name, symbol.name[0]=='_' && symbol.name[1] == 'Z');
472 symbols[i].SetValue(symbol_addr);
473
474 if (symbol.naux > 0)
475 i += symbol.naux;
476 }
477
478 }
479 }
480 return m_symtab_ap.get();
481
482}
483
484SectionList *
485ObjectFilePECOFF::GetSectionList()
486{
487 Mutex::Locker symfile_locker(m_mutex);
488 if (m_sections_ap.get() == NULL)
489 {
490 m_sections_ap.reset(new SectionList());
491 const uint32_t nsects = m_sect_headers.size();
492 Module *module = GetModule();
493 for (uint32_t idx = 0; idx<nsects; ++idx)
494 {
495 std::string sect_name;
496 GetSectionName (sect_name, m_sect_headers[idx]);
497 ConstString const_sect_name (sect_name.c_str());
498
499 // Use a segment ID of the segment index shifted left by 8 so they
500 // never conflict with any of the sections.
501 SectionSP section_sp (new Section (NULL,
502 module, // Module to which this section belongs
503 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
504 const_sect_name, // Name of this section
505 eSectionTypeCode, // This section is a container of other sections.
506 m_sect_headers[idx].vmaddr, // File VM address == addresses as they are found in the object file
507 m_sect_headers[idx].vmsize, // VM size in bytes of this section
508 m_sect_headers[idx].offset, // Offset to the data for this section in the file
509 m_sect_headers[idx].size, // Size in bytes of this section as found in the the file
510 m_sect_headers[idx].flags)); // Flags for this section
511
512 //section_sp->SetIsEncrypted (segment_is_encrypted);
513
514 m_sections_ap->AddSection(section_sp);
515 }
516 }
517 return m_sections_ap.get();
518}
519
520bool
521ObjectFilePECOFF::GetUUID (UUID* uuid)
522{
523 return false;
524}
525
526uint32_t
527ObjectFilePECOFF::GetDependentModules (FileSpecList& files)
528{
529 return 0;
530}
531
532
533//----------------------------------------------------------------------
534// Dump
535//
536// Dump the specifics of the runtime file container (such as any headers
537// segments, sections, etc).
538//----------------------------------------------------------------------
539void
540ObjectFilePECOFF::Dump(Stream *s)
541{
542 Mutex::Locker locker(m_mutex);
543 s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
544 s->Indent();
545 s->PutCString("ObjectFilePECOFF");
546
547 ArchSpec header_arch;
548 GetArchitecture (header_arch);
549
550 *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
551
552 if (m_sections_ap.get())
553 m_sections_ap->Dump(s, NULL, true, UINT32_MAX);
554
555 if (m_symtab_ap.get())
556 m_symtab_ap->Dump(s, NULL, eSortOrderNone);
557
558 if (m_dos_header.e_magic)
559 DumpDOSHeader (s, m_dos_header);
560 if (m_coff_header.machine)
561 {
562 DumpCOFFHeader (s, m_coff_header);
563 if (m_coff_header.hdrsize)
564 DumpOptCOFFHeader (s, m_coff_header_opt);
565 }
566 s->EOL();
567 DumpSectionHeaders(s);
568 s->EOL();
569}
570
571//----------------------------------------------------------------------
572// DumpDOSHeader
573//
574// Dump the MS-DOS header to the specified output stream
575//----------------------------------------------------------------------
576void
577ObjectFilePECOFF::DumpDOSHeader(Stream *s, const dos_header_t& header)
578{
579 s->PutCString ("MSDOS Header\n");
580 s->Printf (" e_magic = 0x%4.4x\n", header.e_magic);
581 s->Printf (" e_cblp = 0x%4.4x\n", header.e_cblp);
582 s->Printf (" e_cp = 0x%4.4x\n", header.e_cp);
583 s->Printf (" e_crlc = 0x%4.4x\n", header.e_crlc);
584 s->Printf (" e_cparhdr = 0x%4.4x\n", header.e_cparhdr);
585 s->Printf (" e_minalloc = 0x%4.4x\n", header.e_minalloc);
586 s->Printf (" e_maxalloc = 0x%4.4x\n", header.e_maxalloc);
587 s->Printf (" e_ss = 0x%4.4x\n", header.e_ss);
588 s->Printf (" e_sp = 0x%4.4x\n", header.e_sp);
589 s->Printf (" e_csum = 0x%4.4x\n", header.e_csum);
590 s->Printf (" e_ip = 0x%4.4x\n", header.e_ip);
591 s->Printf (" e_cs = 0x%4.4x\n", header.e_cs);
592 s->Printf (" e_lfarlc = 0x%4.4x\n", header.e_lfarlc);
593 s->Printf (" e_ovno = 0x%4.4x\n", header.e_ovno);
594 s->Printf (" e_res[4] = { 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x }\n",
595 header.e_res[0],
596 header.e_res[1],
597 header.e_res[2],
598 header.e_res[3]);
599 s->Printf (" e_oemid = 0x%4.4x\n", header.e_oemid);
600 s->Printf (" e_oeminfo = 0x%4.4x\n", header.e_oeminfo);
601 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",
602 header.e_res2[0],
603 header.e_res2[1],
604 header.e_res2[2],
605 header.e_res2[3],
606 header.e_res2[4],
607 header.e_res2[5],
608 header.e_res2[6],
609 header.e_res2[7],
610 header.e_res2[8],
611 header.e_res2[9]);
612 s->Printf (" e_lfanew = 0x%8.8x\n", header.e_lfanew);
613}
614
615//----------------------------------------------------------------------
616// DumpCOFFHeader
617//
618// Dump the COFF header to the specified output stream
619//----------------------------------------------------------------------
620void
621ObjectFilePECOFF::DumpCOFFHeader(Stream *s, const coff_header_t& header)
622{
623 s->PutCString ("COFF Header\n");
624 s->Printf (" machine = 0x%4.4x\n", header.machine);
625 s->Printf (" nsects = 0x%4.4x\n", header.nsects);
626 s->Printf (" modtime = 0x%8.8x\n", header.modtime);
627 s->Printf (" symoff = 0x%8.8x\n", header.symoff);
628 s->Printf (" nsyms = 0x%8.8x\n", header.nsyms);
629 s->Printf (" hdrsize = 0x%4.4x\n", header.hdrsize);
630}
631
632//----------------------------------------------------------------------
633// DumpOptCOFFHeader
634//
635// Dump the optional COFF header to the specified output stream
636//----------------------------------------------------------------------
637void
638ObjectFilePECOFF::DumpOptCOFFHeader(Stream *s, const coff_opt_header_t& header)
639{
640 s->PutCString ("Optional COFF Header\n");
641 s->Printf (" magic = 0x%4.4x\n", header.magic);
642 s->Printf (" major_linker_version = 0x%2.2x\n", header.major_linker_version);
643 s->Printf (" minor_linker_version = 0x%2.2x\n", header.minor_linker_version);
644 s->Printf (" code_size = 0x%8.8x\n", header.code_size);
645 s->Printf (" data_size = 0x%8.8x\n", header.data_size);
646 s->Printf (" bss_size = 0x%8.8x\n", header.bss_size);
647 s->Printf (" entry = 0x%8.8x\n", header.entry);
648 s->Printf (" code_offset = 0x%8.8x\n", header.code_offset);
649 s->Printf (" data_offset = 0x%8.8x\n", header.data_offset);
650 s->Printf (" image_base = 0x%16.16llx\n", header.image_base);
651 s->Printf (" sect_alignment = 0x%8.8x\n", header.sect_alignment);
652 s->Printf (" file_alignment = 0x%8.8x\n", header.file_alignment);
653 s->Printf (" major_os_system_version = 0x%4.4x\n", header.major_os_system_version);
654 s->Printf (" minor_os_system_version = 0x%4.4x\n", header.minor_os_system_version);
655 s->Printf (" major_image_version = 0x%4.4x\n", header.major_image_version);
656 s->Printf (" minor_image_version = 0x%4.4x\n", header.minor_image_version);
657 s->Printf (" major_subsystem_version = 0x%4.4x\n", header.major_subsystem_version);
658 s->Printf (" minor_subsystem_version = 0x%4.4x\n", header.minor_subsystem_version);
659 s->Printf (" reserved1 = 0x%8.8x\n", header.reserved1);
660 s->Printf (" image_size = 0x%8.8x\n", header.image_size);
661 s->Printf (" header_size = 0x%8.8x\n", header.header_size);
662 s->Printf (" checksym = 0x%8.8x\n", header.checksym);
663 s->Printf (" subsystem = 0x%4.4x\n", header.subsystem);
664 s->Printf (" dll_flags = 0x%4.4x\n", header.dll_flags);
665 s->Printf (" stack_reserve_size = 0x%16.16llx\n", header.stack_reserve_size);
666 s->Printf (" stack_commit_size = 0x%16.16llx\n", header.stack_commit_size);
667 s->Printf (" heap_reserve_size = 0x%16.16llx\n", header.heap_reserve_size);
668 s->Printf (" heap_commit_size = 0x%16.16llx\n", header.heap_commit_size);
669 s->Printf (" loader_flags = 0x%8.8x\n", header.loader_flags);
670 s->Printf (" num_data_dir_entries = 0x%8.8zx\n", header.data_dirs.size());
671 uint32_t i;
672 for (i=0; i<header.data_dirs.size(); i++)
673 {
674 s->Printf (" data_dirs[%u] vmaddr = 0x%8.8x, vmsize = 0x%8.8x\n",
675 i,
676 header.data_dirs[i].vmaddr,
677 header.data_dirs[i].vmsize);
678 }
679}
680//----------------------------------------------------------------------
681// DumpSectionHeader
682//
683// Dump a single ELF section header to the specified output stream
684//----------------------------------------------------------------------
685void
686ObjectFilePECOFF::DumpSectionHeader(Stream *s, const section_header_t& sh)
687{
688 std::string name;
689 GetSectionName(name, sh);
690 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",
691 name.c_str(),
692 sh.vmsize,
693 sh.vmaddr,
694 sh.size,
695 sh.offset,
696 sh.reloff,
697 sh.lineoff,
698 sh.nreloc,
699 sh.nline,
700 sh.flags);
701}
702
703
704//----------------------------------------------------------------------
705// DumpSectionHeaders
706//
707// Dump all of the ELF section header to the specified output stream
708//----------------------------------------------------------------------
709void
710ObjectFilePECOFF::DumpSectionHeaders(Stream *s)
711{
712
713 s->PutCString ("Section Headers\n");
714 s->PutCString ("IDX name vm size vm addr size offset reloff lineoff nrel nlin flags\n");
715 s->PutCString ("==== ---------------- -------- -------- -------- -------- -------- -------- ---- ---- --------\n");
716
717 uint32_t idx = 0;
718 SectionHeaderCollIter pos, end = m_sect_headers.end();
719
720 for (pos = m_sect_headers.begin(); pos != end; ++pos, ++idx)
721 {
722 s->Printf ("[%2u]", idx);
723 ObjectFilePECOFF::DumpSectionHeader(s, *pos);
724 }
725}
726
727static bool
728COFFMachineToMachCPU (uint16_t machine, ArchSpec &arch)
729{
730 switch (machine)
731 {
732 case IMAGE_FILE_MACHINE_AMD64:
733 case IMAGE_FILE_MACHINE_IA64:
734 arch.SetArchitecture (eArchTypeMachO,
735 llvm::MachO::CPUTypeX86_64,
736 llvm::MachO::CPUSubType_X86_64_ALL);
737 return true;
738
739 case IMAGE_FILE_MACHINE_I386:
740 arch.SetArchitecture (eArchTypeMachO,
741 llvm::MachO::CPUTypeI386,
742 llvm::MachO::CPUSubType_I386_ALL);
743 return true;
744
745 case IMAGE_FILE_MACHINE_POWERPC:
746 case IMAGE_FILE_MACHINE_POWERPCFP:
747 arch.SetArchitecture (eArchTypeMachO,
748 llvm::MachO::CPUTypePowerPC,
749 llvm::MachO::CPUSubType_POWERPC_ALL);
750 return true;
751 case IMAGE_FILE_MACHINE_ARM:
752 case IMAGE_FILE_MACHINE_THUMB:
753 arch.SetArchitecture (eArchTypeMachO,
754 llvm::MachO::CPUTypeARM,
755 llvm::MachO::CPUSubType_ARM_V7);
756 return true;
757 }
758 return false;
759}
760bool
761ObjectFilePECOFF::GetArchitecture (ArchSpec &arch)
762{
763 // For index zero return our cpu type
764 return COFFMachineToMachCPU (m_coff_header.machine, arch);
765}
766
767ObjectFile::Type
768ObjectFilePECOFF::CalculateType()
769{
770 if (m_coff_header.machine != 0)
771 {
772 if ((m_coff_header.flags & IMAGE_FILE_DLL) == 0)
773 return eTypeExecutable;
774 else
775 return eTypeSharedLibrary;
776 }
777 return eTypeExecutable;
778}
779
780ObjectFile::Strata
781ObjectFilePECOFF::CalculateStrata()
782{
783 return eStrataUser;
784}
785//------------------------------------------------------------------
786// PluginInterface protocol
787//------------------------------------------------------------------
788const char *
789ObjectFilePECOFF::GetPluginName()
790{
791 return "ObjectFilePECOFF";
792}
793
794const char *
795ObjectFilePECOFF::GetShortPluginName()
796{
797 return GetPluginNameStatic();
798}
799
800uint32_t
801ObjectFilePECOFF::GetPluginVersion()
802{
803 return 1;
804}
805