blob: 4f9ed2e815860c38053948772e7190d6f8037326 [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
Charles Davis237ad972013-08-27 05:04:33 +000012#include "llvm/Support/COFF.h"
Greg Claytonf754f882011-09-09 20:33:05 +000013
14#include "lldb/Core/ArchSpec.h"
15#include "lldb/Core/DataBuffer.h"
16#include "lldb/Host/FileSpec.h"
17#include "lldb/Core/FileSpecList.h"
18#include "lldb/Core/Module.h"
Greg Claytonf4d6de62013-04-24 22:29:28 +000019#include "lldb/Core/ModuleSpec.h"
Greg Claytonf754f882011-09-09 20:33:05 +000020#include "lldb/Core/PluginManager.h"
21#include "lldb/Core/Section.h"
22#include "lldb/Core/StreamFile.h"
23#include "lldb/Core/StreamString.h"
24#include "lldb/Core/Timer.h"
25#include "lldb/Core/UUID.h"
26#include "lldb/Symbol/ObjectFile.h"
Virgile Bello2756adf2014-03-08 17:17:20 +000027#include "lldb/Target/SectionLoadList.h"
28#include "lldb/Target/Target.h"
Greg Claytonf754f882011-09-09 20:33:05 +000029
Greg Claytonf754f882011-09-09 20:33:05 +000030#define IMAGE_DOS_SIGNATURE 0x5A4D // MZ
Greg Claytonf754f882011-09-09 20:33:05 +000031#define IMAGE_NT_SIGNATURE 0x00004550 // PE00
32#define OPT_HEADER_MAGIC_PE32 0x010b
33#define OPT_HEADER_MAGIC_PE32_PLUS 0x020b
34
Greg Claytonf754f882011-09-09 20:33:05 +000035using namespace lldb;
36using namespace lldb_private;
37
38void
39ObjectFilePECOFF::Initialize()
40{
41 PluginManager::RegisterPlugin (GetPluginNameStatic(),
42 GetPluginDescriptionStatic(),
Greg Claytonc9660542012-02-05 02:38:54 +000043 CreateInstance,
Greg Claytonf4d6de62013-04-24 22:29:28 +000044 CreateMemoryInstance,
45 GetModuleSpecifications);
Greg Claytonf754f882011-09-09 20:33:05 +000046}
47
48void
49ObjectFilePECOFF::Terminate()
50{
51 PluginManager::UnregisterPlugin (CreateInstance);
52}
53
54
Greg Clayton57abc5d2013-05-10 21:47:16 +000055lldb_private::ConstString
Greg Claytonf754f882011-09-09 20:33:05 +000056ObjectFilePECOFF::GetPluginNameStatic()
57{
Greg Clayton57abc5d2013-05-10 21:47:16 +000058 static ConstString g_name("pe-coff");
59 return g_name;
Greg Claytonf754f882011-09-09 20:33:05 +000060}
61
62const char *
63ObjectFilePECOFF::GetPluginDescriptionStatic()
64{
65 return "Portable Executable and Common Object File Format object file reader (32 and 64 bit)";
66}
67
68
69ObjectFile *
Greg Clayton5ce9c562013-02-06 17:22:03 +000070ObjectFilePECOFF::CreateInstance (const lldb::ModuleSP &module_sp,
71 DataBufferSP& data_sp,
72 lldb::offset_t data_offset,
73 const lldb_private::FileSpec* file,
74 lldb::offset_t file_offset,
75 lldb::offset_t length)
Greg Claytonf754f882011-09-09 20:33:05 +000076{
Greg Clayton5ce9c562013-02-06 17:22:03 +000077 if (!data_sp)
Greg Claytonf754f882011-09-09 20:33:05 +000078 {
Greg Clayton5ce9c562013-02-06 17:22:03 +000079 data_sp = file->MemoryMapFileContents(file_offset, length);
80 data_offset = 0;
81 }
82
83 if (ObjectFilePECOFF::MagicBytesMatch(data_sp))
84 {
85 // Update the data to contain the entire file if it doesn't already
86 if (data_sp->GetByteSize() < length)
87 data_sp = file->MemoryMapFileContents(file_offset, length);
Greg Clayton7b0992d2013-04-18 22:45:39 +000088 std::unique_ptr<ObjectFile> objfile_ap(new ObjectFilePECOFF (module_sp, data_sp, data_offset, file, file_offset, length));
Greg Claytonf754f882011-09-09 20:33:05 +000089 if (objfile_ap.get() && objfile_ap->ParseHeader())
90 return objfile_ap.release();
91 }
92 return NULL;
93}
94
Greg Claytonc9660542012-02-05 02:38:54 +000095ObjectFile *
Greg Claytone72dfb32012-02-24 01:59:29 +000096ObjectFilePECOFF::CreateMemoryInstance (const lldb::ModuleSP &module_sp,
Greg Claytonc9660542012-02-05 02:38:54 +000097 lldb::DataBufferSP& data_sp,
98 const lldb::ProcessSP &process_sp,
99 lldb::addr_t header_addr)
100{
101 return NULL;
102}
103
Greg Claytonf4d6de62013-04-24 22:29:28 +0000104size_t
105ObjectFilePECOFF::GetModuleSpecifications (const lldb_private::FileSpec& file,
106 lldb::DataBufferSP& data_sp,
107 lldb::offset_t data_offset,
108 lldb::offset_t file_offset,
109 lldb::offset_t length,
110 lldb_private::ModuleSpecList &specs)
111{
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000112 const size_t initial_count = specs.GetSize();
113
114 if (ObjectFilePECOFF::MagicBytesMatch(data_sp))
115 {
116 DataExtractor data;
117 data.SetData(data_sp, data_offset, length);
118 data.SetByteOrder(eByteOrderLittle);
119
120 dos_header_t dos_header;
121 coff_header_t coff_header;
122
123 if (ParseDOSHeader(data, dos_header))
124 {
125 lldb::offset_t offset = dos_header.e_lfanew;
126 uint32_t pe_signature = data.GetU32(&offset);
127 if (pe_signature != IMAGE_NT_SIGNATURE)
128 return false;
129 if (ParseCOFFHeader(data, &offset, coff_header))
130 {
131 ArchSpec spec;
Zachary Turnerad587ae42014-07-28 16:44:49 +0000132 if (coff_header.machine == MachineAmd64)
133 spec.SetTriple("x86_64-pc-windows");
134 else if (coff_header.machine == MachineX86)
135 spec.SetTriple("i386-pc-windows");
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000136 specs.Append(ModuleSpec(file, spec));
137 }
138 }
139 }
140
141 return specs.GetSize() - initial_count;
Greg Claytonf4d6de62013-04-24 22:29:28 +0000142}
143
144
Greg Claytonf754f882011-09-09 20:33:05 +0000145bool
Greg Clayton5ce9c562013-02-06 17:22:03 +0000146ObjectFilePECOFF::MagicBytesMatch (DataBufferSP& data_sp)
Greg Claytonf754f882011-09-09 20:33:05 +0000147{
Greg Clayton5ce9c562013-02-06 17:22:03 +0000148 DataExtractor data(data_sp, eByteOrderLittle, 4);
Greg Claytonc7bece562013-01-25 18:06:21 +0000149 lldb::offset_t offset = 0;
Greg Claytonf754f882011-09-09 20:33:05 +0000150 uint16_t magic = data.GetU16 (&offset);
151 return magic == IMAGE_DOS_SIGNATURE;
152}
153
154
Greg Claytone72dfb32012-02-24 01:59:29 +0000155ObjectFilePECOFF::ObjectFilePECOFF (const lldb::ModuleSP &module_sp,
Greg Clayton5ce9c562013-02-06 17:22:03 +0000156 DataBufferSP& data_sp,
157 lldb::offset_t data_offset,
Greg Claytonf754f882011-09-09 20:33:05 +0000158 const FileSpec* file,
Greg Clayton5ce9c562013-02-06 17:22:03 +0000159 lldb::offset_t file_offset,
160 lldb::offset_t length) :
161 ObjectFile (module_sp, file, file_offset, length, data_sp, data_offset),
Greg Claytonf754f882011-09-09 20:33:05 +0000162 m_dos_header (),
163 m_coff_header (),
164 m_coff_header_opt (),
165 m_sect_headers ()
166{
167 ::memset (&m_dos_header, 0, sizeof(m_dos_header));
168 ::memset (&m_coff_header, 0, sizeof(m_coff_header));
169 ::memset (&m_coff_header_opt, 0, sizeof(m_coff_header_opt));
170}
171
172
173ObjectFilePECOFF::~ObjectFilePECOFF()
174{
175}
176
177
178bool
179ObjectFilePECOFF::ParseHeader ()
180{
Greg Claytona1743492012-03-13 23:14:29 +0000181 ModuleSP module_sp(GetModule());
182 if (module_sp)
Greg Claytonf754f882011-09-09 20:33:05 +0000183 {
Greg Claytona1743492012-03-13 23:14:29 +0000184 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
185 m_sect_headers.clear();
186 m_data.SetByteOrder (eByteOrderLittle);
Greg Claytonc7bece562013-01-25 18:06:21 +0000187 lldb::offset_t offset = 0;
Greg Claytona1743492012-03-13 23:14:29 +0000188
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000189 if (ParseDOSHeader(m_data, m_dos_header))
Greg Clayton28469ca2011-09-10 01:04:42 +0000190 {
Greg Claytona1743492012-03-13 23:14:29 +0000191 offset = m_dos_header.e_lfanew;
192 uint32_t pe_signature = m_data.GetU32 (&offset);
193 if (pe_signature != IMAGE_NT_SIGNATURE)
194 return false;
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000195 if (ParseCOFFHeader(m_data, &offset, m_coff_header))
Greg Claytona1743492012-03-13 23:14:29 +0000196 {
197 if (m_coff_header.hdrsize > 0)
198 ParseCOFFOptionalHeader(&offset);
199 ParseSectionHeaders (offset);
200 }
Greg Claytona1743492012-03-13 23:14:29 +0000201 return true;
Greg Clayton28469ca2011-09-10 01:04:42 +0000202 }
Greg Claytonf754f882011-09-09 20:33:05 +0000203 }
204 return false;
205}
206
Virgile Bello2756adf2014-03-08 17:17:20 +0000207bool
208ObjectFilePECOFF::SetLoadAddress(Target &target, addr_t value, bool value_is_offset)
209{
210 bool changed = false;
211 ModuleSP module_sp = GetModule();
212 if (module_sp)
213 {
214 size_t num_loaded_sections = 0;
215 SectionList *section_list = GetSectionList ();
216 if (section_list)
217 {
218 if (!value_is_offset)
219 {
220 value -= m_image_base;
221 }
222
223 const size_t num_sections = section_list->GetSize();
224 size_t sect_idx = 0;
225
226 for (sect_idx = 0; sect_idx < num_sections; ++sect_idx)
227 {
228 // Iterate through the object file sections to find all
229 // of the sections that have SHF_ALLOC in their flag bits.
230 SectionSP section_sp (section_list->GetSectionAtIndex (sect_idx));
231 if (section_sp && !section_sp->IsThreadSpecific())
232 {
233 if (target.GetSectionLoadList().SetSectionLoadAddress (section_sp, section_sp->GetFileAddress() + value))
234 ++num_loaded_sections;
235 }
236 }
237 changed = num_loaded_sections > 0;
Virgile Bello2756adf2014-03-08 17:17:20 +0000238 }
239 }
240 return changed;
241}
242
Greg Claytonf754f882011-09-09 20:33:05 +0000243
244ByteOrder
245ObjectFilePECOFF::GetByteOrder () const
246{
247 return eByteOrderLittle;
248}
249
250bool
251ObjectFilePECOFF::IsExecutable() const
252{
Charles Davis237ad972013-08-27 05:04:33 +0000253 return (m_coff_header.flags & llvm::COFF::IMAGE_FILE_DLL) == 0;
Greg Claytonf754f882011-09-09 20:33:05 +0000254}
255
Greg Claytonc7bece562013-01-25 18:06:21 +0000256uint32_t
Greg Claytonf754f882011-09-09 20:33:05 +0000257ObjectFilePECOFF::GetAddressByteSize () const
258{
259 if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32_PLUS)
260 return 8;
261 else if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32)
262 return 4;
263 return 4;
264}
265
266//----------------------------------------------------------------------
267// NeedsEndianSwap
268//
269// Return true if an endian swap needs to occur when extracting data
270// from this file.
271//----------------------------------------------------------------------
272bool
273ObjectFilePECOFF::NeedsEndianSwap() const
274{
275#if defined(__LITTLE_ENDIAN__)
276 return false;
277#else
278 return true;
279#endif
280}
281//----------------------------------------------------------------------
282// ParseDOSHeader
283//----------------------------------------------------------------------
284bool
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000285ObjectFilePECOFF::ParseDOSHeader (DataExtractor &data, dos_header_t &dos_header)
Greg Claytonf754f882011-09-09 20:33:05 +0000286{
287 bool success = false;
Greg Claytonc7bece562013-01-25 18:06:21 +0000288 lldb::offset_t offset = 0;
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000289 success = data.ValidOffsetForDataOfSize(0, sizeof(dos_header));
Greg Claytonf754f882011-09-09 20:33:05 +0000290
291 if (success)
292 {
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000293 dos_header.e_magic = data.GetU16(&offset); // Magic number
294 success = dos_header.e_magic == IMAGE_DOS_SIGNATURE;
Greg Claytonf754f882011-09-09 20:33:05 +0000295
296 if (success)
297 {
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000298 dos_header.e_cblp = data.GetU16(&offset); // Bytes on last page of file
299 dos_header.e_cp = data.GetU16(&offset); // Pages in file
300 dos_header.e_crlc = data.GetU16(&offset); // Relocations
301 dos_header.e_cparhdr = data.GetU16(&offset); // Size of header in paragraphs
302 dos_header.e_minalloc = data.GetU16(&offset); // Minimum extra paragraphs needed
303 dos_header.e_maxalloc = data.GetU16(&offset); // Maximum extra paragraphs needed
304 dos_header.e_ss = data.GetU16(&offset); // Initial (relative) SS value
305 dos_header.e_sp = data.GetU16(&offset); // Initial SP value
306 dos_header.e_csum = data.GetU16(&offset); // Checksum
307 dos_header.e_ip = data.GetU16(&offset); // Initial IP value
308 dos_header.e_cs = data.GetU16(&offset); // Initial (relative) CS value
309 dos_header.e_lfarlc = data.GetU16(&offset); // File address of relocation table
310 dos_header.e_ovno = data.GetU16(&offset); // Overlay number
Greg Claytonf754f882011-09-09 20:33:05 +0000311
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000312 dos_header.e_res[0] = data.GetU16(&offset); // Reserved words
313 dos_header.e_res[1] = data.GetU16(&offset); // Reserved words
314 dos_header.e_res[2] = data.GetU16(&offset); // Reserved words
315 dos_header.e_res[3] = data.GetU16(&offset); // Reserved words
Greg Claytonf754f882011-09-09 20:33:05 +0000316
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000317 dos_header.e_oemid = data.GetU16(&offset); // OEM identifier (for e_oeminfo)
318 dos_header.e_oeminfo = data.GetU16(&offset); // OEM information; e_oemid specific
319 dos_header.e_res2[0] = data.GetU16(&offset); // Reserved words
320 dos_header.e_res2[1] = data.GetU16(&offset); // Reserved words
321 dos_header.e_res2[2] = data.GetU16(&offset); // Reserved words
322 dos_header.e_res2[3] = data.GetU16(&offset); // Reserved words
323 dos_header.e_res2[4] = data.GetU16(&offset); // Reserved words
324 dos_header.e_res2[5] = data.GetU16(&offset); // Reserved words
325 dos_header.e_res2[6] = data.GetU16(&offset); // Reserved words
326 dos_header.e_res2[7] = data.GetU16(&offset); // Reserved words
327 dos_header.e_res2[8] = data.GetU16(&offset); // Reserved words
328 dos_header.e_res2[9] = data.GetU16(&offset); // Reserved words
Greg Claytonf754f882011-09-09 20:33:05 +0000329
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000330 dos_header.e_lfanew = data.GetU32(&offset); // File address of new exe header
Greg Claytonf754f882011-09-09 20:33:05 +0000331 }
332 }
333 if (!success)
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000334 memset(&dos_header, 0, sizeof(dos_header));
Greg Claytonf754f882011-09-09 20:33:05 +0000335 return success;
336}
337
338
339//----------------------------------------------------------------------
340// ParserCOFFHeader
341//----------------------------------------------------------------------
342bool
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000343ObjectFilePECOFF::ParseCOFFHeader(DataExtractor &data, lldb::offset_t *offset_ptr, coff_header_t &coff_header)
Greg Claytonf754f882011-09-09 20:33:05 +0000344{
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000345 bool success = data.ValidOffsetForDataOfSize (*offset_ptr, sizeof(coff_header));
Greg Claytonf754f882011-09-09 20:33:05 +0000346 if (success)
347 {
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000348 coff_header.machine = data.GetU16(offset_ptr);
349 coff_header.nsects = data.GetU16(offset_ptr);
350 coff_header.modtime = data.GetU32(offset_ptr);
351 coff_header.symoff = data.GetU32(offset_ptr);
352 coff_header.nsyms = data.GetU32(offset_ptr);
353 coff_header.hdrsize = data.GetU16(offset_ptr);
354 coff_header.flags = data.GetU16(offset_ptr);
Greg Claytonf754f882011-09-09 20:33:05 +0000355 }
356 if (!success)
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000357 memset(&coff_header, 0, sizeof(coff_header));
Greg Claytonf754f882011-09-09 20:33:05 +0000358 return success;
359}
360
361bool
Greg Claytonc7bece562013-01-25 18:06:21 +0000362ObjectFilePECOFF::ParseCOFFOptionalHeader(lldb::offset_t *offset_ptr)
Greg Claytonf754f882011-09-09 20:33:05 +0000363{
364 bool success = false;
Greg Claytonc7bece562013-01-25 18:06:21 +0000365 const lldb::offset_t end_offset = *offset_ptr + m_coff_header.hdrsize;
Greg Claytonf754f882011-09-09 20:33:05 +0000366 if (*offset_ptr < end_offset)
367 {
368 success = true;
369 m_coff_header_opt.magic = m_data.GetU16(offset_ptr);
370 m_coff_header_opt.major_linker_version = m_data.GetU8 (offset_ptr);
371 m_coff_header_opt.minor_linker_version = m_data.GetU8 (offset_ptr);
372 m_coff_header_opt.code_size = m_data.GetU32(offset_ptr);
373 m_coff_header_opt.data_size = m_data.GetU32(offset_ptr);
374 m_coff_header_opt.bss_size = m_data.GetU32(offset_ptr);
375 m_coff_header_opt.entry = m_data.GetU32(offset_ptr);
376 m_coff_header_opt.code_offset = m_data.GetU32(offset_ptr);
377
378 const uint32_t addr_byte_size = GetAddressByteSize ();
379
380 if (*offset_ptr < end_offset)
381 {
382 if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32)
383 {
384 // PE32 only
385 m_coff_header_opt.data_offset = m_data.GetU32(offset_ptr);
386 }
387 else
388 m_coff_header_opt.data_offset = 0;
389
390 if (*offset_ptr < end_offset)
391 {
392 m_coff_header_opt.image_base = m_data.GetMaxU64 (offset_ptr, addr_byte_size);
393 m_coff_header_opt.sect_alignment = m_data.GetU32(offset_ptr);
394 m_coff_header_opt.file_alignment = m_data.GetU32(offset_ptr);
395 m_coff_header_opt.major_os_system_version = m_data.GetU16(offset_ptr);
396 m_coff_header_opt.minor_os_system_version = m_data.GetU16(offset_ptr);
397 m_coff_header_opt.major_image_version = m_data.GetU16(offset_ptr);
398 m_coff_header_opt.minor_image_version = m_data.GetU16(offset_ptr);
399 m_coff_header_opt.major_subsystem_version = m_data.GetU16(offset_ptr);
400 m_coff_header_opt.minor_subsystem_version = m_data.GetU16(offset_ptr);
401 m_coff_header_opt.reserved1 = m_data.GetU32(offset_ptr);
402 m_coff_header_opt.image_size = m_data.GetU32(offset_ptr);
403 m_coff_header_opt.header_size = m_data.GetU32(offset_ptr);
Greg Clayton28469ca2011-09-10 01:04:42 +0000404 m_coff_header_opt.checksum = m_data.GetU32(offset_ptr);
Greg Claytonf754f882011-09-09 20:33:05 +0000405 m_coff_header_opt.subsystem = m_data.GetU16(offset_ptr);
406 m_coff_header_opt.dll_flags = m_data.GetU16(offset_ptr);
407 m_coff_header_opt.stack_reserve_size = m_data.GetMaxU64 (offset_ptr, addr_byte_size);
408 m_coff_header_opt.stack_commit_size = m_data.GetMaxU64 (offset_ptr, addr_byte_size);
409 m_coff_header_opt.heap_reserve_size = m_data.GetMaxU64 (offset_ptr, addr_byte_size);
410 m_coff_header_opt.heap_commit_size = m_data.GetMaxU64 (offset_ptr, addr_byte_size);
411 m_coff_header_opt.loader_flags = m_data.GetU32(offset_ptr);
412 uint32_t num_data_dir_entries = m_data.GetU32(offset_ptr);
413 m_coff_header_opt.data_dirs.clear();
414 m_coff_header_opt.data_dirs.resize(num_data_dir_entries);
415 uint32_t i;
416 for (i=0; i<num_data_dir_entries; i++)
417 {
418 m_coff_header_opt.data_dirs[i].vmaddr = m_data.GetU32(offset_ptr);
419 m_coff_header_opt.data_dirs[i].vmsize = m_data.GetU32(offset_ptr);
420 }
Virgile Bello2756adf2014-03-08 17:17:20 +0000421
422 m_file_offset = m_coff_header_opt.image_base;
423 m_image_base = m_coff_header_opt.image_base;
Greg Claytonf754f882011-09-09 20:33:05 +0000424 }
425 }
426 }
427 // Make sure we are on track for section data which follows
428 *offset_ptr = end_offset;
429 return success;
430}
431
432
433//----------------------------------------------------------------------
434// ParseSectionHeaders
435//----------------------------------------------------------------------
436bool
437ObjectFilePECOFF::ParseSectionHeaders (uint32_t section_header_data_offset)
438{
439 const uint32_t nsects = m_coff_header.nsects;
440 m_sect_headers.clear();
441
442 if (nsects > 0)
443 {
444 const uint32_t addr_byte_size = GetAddressByteSize ();
445 const size_t section_header_byte_size = nsects * sizeof(section_header_t);
446 DataBufferSP section_header_data_sp(m_file.ReadFileContents (section_header_data_offset, section_header_byte_size));
447 DataExtractor section_header_data (section_header_data_sp, GetByteOrder(), addr_byte_size);
448
Greg Claytonc7bece562013-01-25 18:06:21 +0000449 lldb::offset_t offset = 0;
Greg Claytonf754f882011-09-09 20:33:05 +0000450 if (section_header_data.ValidOffsetForDataOfSize (offset, section_header_byte_size))
451 {
452 m_sect_headers.resize(nsects);
453
454 for (uint32_t idx = 0; idx<nsects; ++idx)
455 {
456 const void *name_data = section_header_data.GetData(&offset, 8);
457 if (name_data)
458 {
459 memcpy(m_sect_headers[idx].name, name_data, 8);
460 m_sect_headers[idx].vmsize = section_header_data.GetU32(&offset);
461 m_sect_headers[idx].vmaddr = section_header_data.GetU32(&offset);
462 m_sect_headers[idx].size = section_header_data.GetU32(&offset);
463 m_sect_headers[idx].offset = section_header_data.GetU32(&offset);
464 m_sect_headers[idx].reloff = section_header_data.GetU32(&offset);
465 m_sect_headers[idx].lineoff = section_header_data.GetU32(&offset);
466 m_sect_headers[idx].nreloc = section_header_data.GetU16(&offset);
467 m_sect_headers[idx].nline = section_header_data.GetU16(&offset);
468 m_sect_headers[idx].flags = section_header_data.GetU32(&offset);
469 }
470 }
471 }
472 }
473
474 return m_sect_headers.empty() == false;
475}
476
477bool
478ObjectFilePECOFF::GetSectionName(std::string& sect_name, const section_header_t& sect)
479{
480 if (sect.name[0] == '/')
481 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000482 lldb::offset_t stroff = strtoul(&sect.name[1], NULL, 10);
483 lldb::offset_t string_file_offset = m_coff_header.symoff + (m_coff_header.nsyms * 18) + stroff;
Greg Claytonf754f882011-09-09 20:33:05 +0000484 const char *name = m_data.GetCStr (&string_file_offset);
485 if (name)
486 {
487 sect_name = name;
488 return true;
489 }
490
491 return false;
492 }
493 sect_name = sect.name;
494 return true;
495}
496
497//----------------------------------------------------------------------
498// GetNListSymtab
499//----------------------------------------------------------------------
500Symtab *
Greg Clayton3046e662013-07-10 01:23:25 +0000501ObjectFilePECOFF::GetSymtab()
Greg Claytonf754f882011-09-09 20:33:05 +0000502{
Greg Claytona1743492012-03-13 23:14:29 +0000503 ModuleSP module_sp(GetModule());
504 if (module_sp)
Greg Claytonf754f882011-09-09 20:33:05 +0000505 {
Greg Claytona1743492012-03-13 23:14:29 +0000506 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
507 if (m_symtab_ap.get() == NULL)
Greg Claytonf754f882011-09-09 20:33:05 +0000508 {
Greg Claytona1743492012-03-13 23:14:29 +0000509 SectionList *sect_list = GetSectionList();
510 m_symtab_ap.reset(new Symtab(this));
511 Mutex::Locker symtab_locker (m_symtab_ap->GetMutex());
Greg Claytonf754f882011-09-09 20:33:05 +0000512
Greg Claytona1743492012-03-13 23:14:29 +0000513 const uint32_t num_syms = m_coff_header.nsyms;
514
515 if (num_syms > 0 && m_coff_header.symoff > 0)
516 {
Greg Clayton0076e712013-06-18 00:08:58 +0000517 const uint32_t symbol_size = 18;
Greg Claytona1743492012-03-13 23:14:29 +0000518 const uint32_t addr_byte_size = GetAddressByteSize ();
519 const size_t symbol_data_size = num_syms * symbol_size;
520 // Include the 4 bytes string table size at the end of the symbols
521 DataBufferSP symtab_data_sp(m_file.ReadFileContents (m_coff_header.symoff, symbol_data_size + 4));
522 DataExtractor symtab_data (symtab_data_sp, GetByteOrder(), addr_byte_size);
Greg Claytonc7bece562013-01-25 18:06:21 +0000523 lldb::offset_t offset = symbol_data_size;
Greg Claytona1743492012-03-13 23:14:29 +0000524 const uint32_t strtab_size = symtab_data.GetU32 (&offset);
Greg Clayton0076e712013-06-18 00:08:58 +0000525 DataBufferSP strtab_data_sp(m_file.ReadFileContents (m_coff_header.symoff + symbol_data_size, strtab_size));
Greg Claytona1743492012-03-13 23:14:29 +0000526 DataExtractor strtab_data (strtab_data_sp, GetByteOrder(), addr_byte_size);
527
Greg Clayton0076e712013-06-18 00:08:58 +0000528 // First 4 bytes should be zeroed after strtab_size has been read,
529 // because it is used as offset 0 to encode a NULL string.
530 uint32_t* strtab_data_start = (uint32_t*)strtab_data_sp->GetBytes();
531 strtab_data_start[0] = 0;
532
Greg Claytona1743492012-03-13 23:14:29 +0000533 offset = 0;
534 std::string symbol_name;
535 Symbol *symbols = m_symtab_ap->Resize (num_syms);
536 for (uint32_t i=0; i<num_syms; ++i)
537 {
538 coff_symbol_t symbol;
539 const uint32_t symbol_offset = offset;
540 const char *symbol_name_cstr = NULL;
541 // If the first 4 bytes of the symbol string are zero, then we
542 // it is followed by a 4 byte string table offset. Else these
543 // 8 bytes contain the symbol name
544 if (symtab_data.GetU32 (&offset) == 0)
545 {
546 // Long string that doesn't fit into the symbol table name,
547 // so now we must read the 4 byte string table offset
548 uint32_t strtab_offset = symtab_data.GetU32 (&offset);
549 symbol_name_cstr = strtab_data.PeekCStr (strtab_offset);
550 symbol_name.assign (symbol_name_cstr);
551 }
552 else
553 {
554 // Short string that fits into the symbol table name which is 8 bytes
555 offset += sizeof(symbol.name) - 4; // Skip remaining
556 symbol_name_cstr = symtab_data.PeekCStr (symbol_offset);
557 if (symbol_name_cstr == NULL)
558 break;
559 symbol_name.assign (symbol_name_cstr, sizeof(symbol.name));
560 }
561 symbol.value = symtab_data.GetU32 (&offset);
562 symbol.sect = symtab_data.GetU16 (&offset);
563 symbol.type = symtab_data.GetU16 (&offset);
564 symbol.storage = symtab_data.GetU8 (&offset);
565 symbol.naux = symtab_data.GetU8 (&offset);
Greg Clayton037520e2012-07-18 23:18:10 +0000566 symbols[i].GetMangled ().SetValue (ConstString(symbol_name.c_str()));
Greg Clayton0076e712013-06-18 00:08:58 +0000567 if ((int16_t)symbol.sect >= 1)
568 {
569 Address symbol_addr(sect_list->GetSectionAtIndex(symbol.sect-1), symbol.value);
570 symbols[i].GetAddress() = symbol_addr;
571 }
Greg Claytona1743492012-03-13 23:14:29 +0000572
573 if (symbol.naux > 0)
Greg Clayton0076e712013-06-18 00:08:58 +0000574 {
Greg Claytona1743492012-03-13 23:14:29 +0000575 i += symbol.naux;
Greg Clayton0076e712013-06-18 00:08:58 +0000576 offset += symbol_size;
577 }
Greg Claytona1743492012-03-13 23:14:29 +0000578 }
579
580 }
Virgile Belloa4fe3a12013-08-25 13:27:20 +0000581
582 // Read export header
583 if (coff_data_dir_export_table < m_coff_header_opt.data_dirs.size()
584 && m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmsize > 0 && m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmaddr > 0)
585 {
586 export_directory_entry export_table;
587 uint32_t data_start = m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmaddr;
588 Address address(m_coff_header_opt.image_base + data_start, sect_list);
589 DataBufferSP symtab_data_sp(m_file.ReadFileContents(address.GetSection()->GetFileOffset() + address.GetOffset(), m_coff_header_opt.data_dirs[0].vmsize));
590 DataExtractor symtab_data (symtab_data_sp, GetByteOrder(), GetAddressByteSize());
591 lldb::offset_t offset = 0;
592
593 // Read export_table header
594 export_table.characteristics = symtab_data.GetU32(&offset);
595 export_table.time_date_stamp = symtab_data.GetU32(&offset);
596 export_table.major_version = symtab_data.GetU16(&offset);
597 export_table.minor_version = symtab_data.GetU16(&offset);
598 export_table.name = symtab_data.GetU32(&offset);
599 export_table.base = symtab_data.GetU32(&offset);
600 export_table.number_of_functions = symtab_data.GetU32(&offset);
601 export_table.number_of_names = symtab_data.GetU32(&offset);
602 export_table.address_of_functions = symtab_data.GetU32(&offset);
603 export_table.address_of_names = symtab_data.GetU32(&offset);
604 export_table.address_of_name_ordinals = symtab_data.GetU32(&offset);
605
606 bool has_ordinal = export_table.address_of_name_ordinals != 0;
607
608 lldb::offset_t name_offset = export_table.address_of_names - data_start;
609 lldb::offset_t name_ordinal_offset = export_table.address_of_name_ordinals - data_start;
610
611 Symbol *symbols = m_symtab_ap->Resize(export_table.number_of_names);
612
613 std::string symbol_name;
614
615 // Read each export table entry
616 for (size_t i = 0; i < export_table.number_of_names; ++i)
617 {
618 uint32_t name_ordinal = has_ordinal ? symtab_data.GetU16(&name_ordinal_offset) : i;
619 uint32_t name_address = symtab_data.GetU32(&name_offset);
620
621 const char* symbol_name_cstr = symtab_data.PeekCStr(name_address - data_start);
622 symbol_name.assign(symbol_name_cstr);
623
624 lldb::offset_t function_offset = export_table.address_of_functions - data_start + sizeof(uint32_t) * name_ordinal;
625 uint32_t function_rva = symtab_data.GetU32(&function_offset);
626
627 Address symbol_addr(m_coff_header_opt.image_base + function_rva, sect_list);
628 symbols[i].GetMangled().SetValue(ConstString(symbol_name.c_str()));
629 symbols[i].GetAddress() = symbol_addr;
630 symbols[i].SetType(lldb::eSymbolTypeCode);
631 symbols[i].SetDebug(true);
632 }
633 }
Greg Claytonf754f882011-09-09 20:33:05 +0000634 }
635 }
636 return m_symtab_ap.get();
637
638}
639
Greg Clayton3046e662013-07-10 01:23:25 +0000640bool
641ObjectFilePECOFF::IsStripped ()
Greg Claytonf754f882011-09-09 20:33:05 +0000642{
Greg Clayton3046e662013-07-10 01:23:25 +0000643 // TODO: determine this for COFF
644 return false;
645}
646
647
648
649void
650ObjectFilePECOFF::CreateSections (SectionList &unified_section_list)
651{
652 if (!m_sections_ap.get())
Greg Claytonf754f882011-09-09 20:33:05 +0000653 {
Greg Clayton3046e662013-07-10 01:23:25 +0000654 m_sections_ap.reset(new SectionList());
655
656 ModuleSP module_sp(GetModule());
657 if (module_sp)
Greg Claytonf754f882011-09-09 20:33:05 +0000658 {
Greg Clayton3046e662013-07-10 01:23:25 +0000659 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
Greg Claytona1743492012-03-13 23:14:29 +0000660 const uint32_t nsects = m_sect_headers.size();
661 ModuleSP module_sp (GetModule());
662 for (uint32_t idx = 0; idx<nsects; ++idx)
Greg Clayton28469ca2011-09-10 01:04:42 +0000663 {
Greg Claytona1743492012-03-13 23:14:29 +0000664 std::string sect_name;
665 GetSectionName (sect_name, m_sect_headers[idx]);
666 ConstString const_sect_name (sect_name.c_str());
667 static ConstString g_code_sect_name (".code");
668 static ConstString g_CODE_sect_name ("CODE");
669 static ConstString g_data_sect_name (".data");
670 static ConstString g_DATA_sect_name ("DATA");
671 static ConstString g_bss_sect_name (".bss");
672 static ConstString g_BSS_sect_name ("BSS");
673 static ConstString g_debug_sect_name (".debug");
674 static ConstString g_reloc_sect_name (".reloc");
675 static ConstString g_stab_sect_name (".stab");
676 static ConstString g_stabstr_sect_name (".stabstr");
Greg Clayton0076e712013-06-18 00:08:58 +0000677 static ConstString g_sect_name_dwarf_debug_abbrev (".debug_abbrev");
678 static ConstString g_sect_name_dwarf_debug_aranges (".debug_aranges");
679 static ConstString g_sect_name_dwarf_debug_frame (".debug_frame");
680 static ConstString g_sect_name_dwarf_debug_info (".debug_info");
681 static ConstString g_sect_name_dwarf_debug_line (".debug_line");
682 static ConstString g_sect_name_dwarf_debug_loc (".debug_loc");
683 static ConstString g_sect_name_dwarf_debug_macinfo (".debug_macinfo");
684 static ConstString g_sect_name_dwarf_debug_pubnames (".debug_pubnames");
685 static ConstString g_sect_name_dwarf_debug_pubtypes (".debug_pubtypes");
686 static ConstString g_sect_name_dwarf_debug_ranges (".debug_ranges");
687 static ConstString g_sect_name_dwarf_debug_str (".debug_str");
688 static ConstString g_sect_name_eh_frame (".eh_frame");
Greg Claytona1743492012-03-13 23:14:29 +0000689 SectionType section_type = eSectionTypeOther;
Charles Davis237ad972013-08-27 05:04:33 +0000690 if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_CODE &&
Greg Claytona1743492012-03-13 23:14:29 +0000691 ((const_sect_name == g_code_sect_name) || (const_sect_name == g_CODE_sect_name)))
692 {
693 section_type = eSectionTypeCode;
694 }
Charles Davis237ad972013-08-27 05:04:33 +0000695 else if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_INITIALIZED_DATA &&
Greg Claytona1743492012-03-13 23:14:29 +0000696 ((const_sect_name == g_data_sect_name) || (const_sect_name == g_DATA_sect_name)))
697 {
Greg Clayton28469ca2011-09-10 01:04:42 +0000698 section_type = eSectionTypeData;
Greg Claytona1743492012-03-13 23:14:29 +0000699 }
Charles Davis237ad972013-08-27 05:04:33 +0000700 else if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA &&
Greg Claytona1743492012-03-13 23:14:29 +0000701 ((const_sect_name == g_bss_sect_name) || (const_sect_name == g_BSS_sect_name)))
702 {
703 if (m_sect_headers[idx].size == 0)
704 section_type = eSectionTypeZeroFill;
705 else
706 section_type = eSectionTypeData;
707 }
708 else if (const_sect_name == g_debug_sect_name)
709 {
710 section_type = eSectionTypeDebug;
711 }
712 else if (const_sect_name == g_stabstr_sect_name)
713 {
714 section_type = eSectionTypeDataCString;
715 }
716 else if (const_sect_name == g_reloc_sect_name)
717 {
718 section_type = eSectionTypeOther;
719 }
Greg Clayton0076e712013-06-18 00:08:58 +0000720 else if (const_sect_name == g_sect_name_dwarf_debug_abbrev) section_type = eSectionTypeDWARFDebugAbbrev;
721 else if (const_sect_name == g_sect_name_dwarf_debug_aranges) section_type = eSectionTypeDWARFDebugAranges;
722 else if (const_sect_name == g_sect_name_dwarf_debug_frame) section_type = eSectionTypeDWARFDebugFrame;
723 else if (const_sect_name == g_sect_name_dwarf_debug_info) section_type = eSectionTypeDWARFDebugInfo;
724 else if (const_sect_name == g_sect_name_dwarf_debug_line) section_type = eSectionTypeDWARFDebugLine;
725 else if (const_sect_name == g_sect_name_dwarf_debug_loc) section_type = eSectionTypeDWARFDebugLoc;
726 else if (const_sect_name == g_sect_name_dwarf_debug_macinfo) section_type = eSectionTypeDWARFDebugMacInfo;
727 else if (const_sect_name == g_sect_name_dwarf_debug_pubnames) section_type = eSectionTypeDWARFDebugPubNames;
728 else if (const_sect_name == g_sect_name_dwarf_debug_pubtypes) section_type = eSectionTypeDWARFDebugPubTypes;
729 else if (const_sect_name == g_sect_name_dwarf_debug_ranges) section_type = eSectionTypeDWARFDebugRanges;
730 else if (const_sect_name == g_sect_name_dwarf_debug_str) section_type = eSectionTypeDWARFDebugStr;
731 else if (const_sect_name == g_sect_name_eh_frame) section_type = eSectionTypeEHFrame;
Charles Davis237ad972013-08-27 05:04:33 +0000732 else if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_CODE)
Greg Claytona1743492012-03-13 23:14:29 +0000733 {
734 section_type = eSectionTypeCode;
735 }
Charles Davis237ad972013-08-27 05:04:33 +0000736 else if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
Greg Claytona1743492012-03-13 23:14:29 +0000737 {
Greg Clayton28469ca2011-09-10 01:04:42 +0000738 section_type = eSectionTypeData;
Greg Claytona1743492012-03-13 23:14:29 +0000739 }
Charles Davis237ad972013-08-27 05:04:33 +0000740 else if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
Greg Claytona1743492012-03-13 23:14:29 +0000741 {
742 if (m_sect_headers[idx].size == 0)
743 section_type = eSectionTypeZeroFill;
744 else
745 section_type = eSectionTypeData;
746 }
747
748 // Use a segment ID of the segment index shifted left by 8 so they
749 // never conflict with any of the sections.
750 SectionSP section_sp (new Section (module_sp, // Module to which this section belongs
Michael Sartaina7499c92013-07-01 19:45:50 +0000751 this, // Object file to which this section belongs
Greg Claytona1743492012-03-13 23:14:29 +0000752 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
753 const_sect_name, // Name of this section
Michael Sartaina7499c92013-07-01 19:45:50 +0000754 section_type, // This section is a container of other sections.
Greg Clayton0076e712013-06-18 00:08:58 +0000755 m_coff_header_opt.image_base + m_sect_headers[idx].vmaddr, // File VM address == addresses as they are found in the object file
Greg Claytona1743492012-03-13 23:14:29 +0000756 m_sect_headers[idx].vmsize, // VM size in bytes of this section
757 m_sect_headers[idx].offset, // Offset to the data for this section in the file
Bruce Mitcheneraaa0ba32014-07-08 18:05:41 +0000758 m_sect_headers[idx].size, // Size in bytes of this section as found in the file
Greg Clayton48672af2014-06-24 22:22:43 +0000759 m_coff_header_opt.sect_alignment, // Section alignment
Greg Claytona1743492012-03-13 23:14:29 +0000760 m_sect_headers[idx].flags)); // Flags for this section
761
762 //section_sp->SetIsEncrypted (segment_is_encrypted);
763
Greg Clayton3046e662013-07-10 01:23:25 +0000764 unified_section_list.AddSection(section_sp);
765 m_sections_ap->AddSection (section_sp);
Greg Clayton28469ca2011-09-10 01:04:42 +0000766 }
Greg Claytonf754f882011-09-09 20:33:05 +0000767 }
768 }
Greg Claytonf754f882011-09-09 20:33:05 +0000769}
770
771bool
772ObjectFilePECOFF::GetUUID (UUID* uuid)
773{
774 return false;
775}
776
777uint32_t
778ObjectFilePECOFF::GetDependentModules (FileSpecList& files)
779{
780 return 0;
781}
782
783
784//----------------------------------------------------------------------
785// Dump
786//
787// Dump the specifics of the runtime file container (such as any headers
788// segments, sections, etc).
789//----------------------------------------------------------------------
790void
791ObjectFilePECOFF::Dump(Stream *s)
792{
Greg Claytona1743492012-03-13 23:14:29 +0000793 ModuleSP module_sp(GetModule());
794 if (module_sp)
Greg Claytonf754f882011-09-09 20:33:05 +0000795 {
Greg Claytona1743492012-03-13 23:14:29 +0000796 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000797 s->Printf("%p: ", static_cast<void*>(this));
Greg Claytona1743492012-03-13 23:14:29 +0000798 s->Indent();
799 s->PutCString("ObjectFilePECOFF");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000800
Greg Claytona1743492012-03-13 23:14:29 +0000801 ArchSpec header_arch;
802 GetArchitecture (header_arch);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000803
Greg Claytona1743492012-03-13 23:14:29 +0000804 *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000805
Greg Clayton3046e662013-07-10 01:23:25 +0000806 SectionList *sections = GetSectionList();
807 if (sections)
808 sections->Dump(s, NULL, true, UINT32_MAX);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000809
Greg Claytona1743492012-03-13 23:14:29 +0000810 if (m_symtab_ap.get())
811 m_symtab_ap->Dump(s, NULL, eSortOrderNone);
812
813 if (m_dos_header.e_magic)
814 DumpDOSHeader (s, m_dos_header);
815 if (m_coff_header.machine)
816 {
817 DumpCOFFHeader (s, m_coff_header);
818 if (m_coff_header.hdrsize)
819 DumpOptCOFFHeader (s, m_coff_header_opt);
820 }
821 s->EOL();
822 DumpSectionHeaders(s);
823 s->EOL();
Greg Claytonf754f882011-09-09 20:33:05 +0000824 }
Greg Claytonf754f882011-09-09 20:33:05 +0000825}
826
827//----------------------------------------------------------------------
828// DumpDOSHeader
829//
830// Dump the MS-DOS header to the specified output stream
831//----------------------------------------------------------------------
832void
833ObjectFilePECOFF::DumpDOSHeader(Stream *s, const dos_header_t& header)
834{
835 s->PutCString ("MSDOS Header\n");
836 s->Printf (" e_magic = 0x%4.4x\n", header.e_magic);
837 s->Printf (" e_cblp = 0x%4.4x\n", header.e_cblp);
838 s->Printf (" e_cp = 0x%4.4x\n", header.e_cp);
839 s->Printf (" e_crlc = 0x%4.4x\n", header.e_crlc);
840 s->Printf (" e_cparhdr = 0x%4.4x\n", header.e_cparhdr);
841 s->Printf (" e_minalloc = 0x%4.4x\n", header.e_minalloc);
842 s->Printf (" e_maxalloc = 0x%4.4x\n", header.e_maxalloc);
843 s->Printf (" e_ss = 0x%4.4x\n", header.e_ss);
844 s->Printf (" e_sp = 0x%4.4x\n", header.e_sp);
845 s->Printf (" e_csum = 0x%4.4x\n", header.e_csum);
846 s->Printf (" e_ip = 0x%4.4x\n", header.e_ip);
847 s->Printf (" e_cs = 0x%4.4x\n", header.e_cs);
848 s->Printf (" e_lfarlc = 0x%4.4x\n", header.e_lfarlc);
849 s->Printf (" e_ovno = 0x%4.4x\n", header.e_ovno);
850 s->Printf (" e_res[4] = { 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x }\n",
851 header.e_res[0],
852 header.e_res[1],
853 header.e_res[2],
854 header.e_res[3]);
855 s->Printf (" e_oemid = 0x%4.4x\n", header.e_oemid);
856 s->Printf (" e_oeminfo = 0x%4.4x\n", header.e_oeminfo);
857 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",
858 header.e_res2[0],
859 header.e_res2[1],
860 header.e_res2[2],
861 header.e_res2[3],
862 header.e_res2[4],
863 header.e_res2[5],
864 header.e_res2[6],
865 header.e_res2[7],
866 header.e_res2[8],
867 header.e_res2[9]);
868 s->Printf (" e_lfanew = 0x%8.8x\n", header.e_lfanew);
869}
870
871//----------------------------------------------------------------------
872// DumpCOFFHeader
873//
874// Dump the COFF header to the specified output stream
875//----------------------------------------------------------------------
876void
877ObjectFilePECOFF::DumpCOFFHeader(Stream *s, const coff_header_t& header)
878{
879 s->PutCString ("COFF Header\n");
880 s->Printf (" machine = 0x%4.4x\n", header.machine);
881 s->Printf (" nsects = 0x%4.4x\n", header.nsects);
882 s->Printf (" modtime = 0x%8.8x\n", header.modtime);
883 s->Printf (" symoff = 0x%8.8x\n", header.symoff);
884 s->Printf (" nsyms = 0x%8.8x\n", header.nsyms);
885 s->Printf (" hdrsize = 0x%4.4x\n", header.hdrsize);
886}
887
888//----------------------------------------------------------------------
889// DumpOptCOFFHeader
890//
891// Dump the optional COFF header to the specified output stream
892//----------------------------------------------------------------------
893void
894ObjectFilePECOFF::DumpOptCOFFHeader(Stream *s, const coff_opt_header_t& header)
895{
896 s->PutCString ("Optional COFF Header\n");
897 s->Printf (" magic = 0x%4.4x\n", header.magic);
898 s->Printf (" major_linker_version = 0x%2.2x\n", header.major_linker_version);
899 s->Printf (" minor_linker_version = 0x%2.2x\n", header.minor_linker_version);
900 s->Printf (" code_size = 0x%8.8x\n", header.code_size);
901 s->Printf (" data_size = 0x%8.8x\n", header.data_size);
902 s->Printf (" bss_size = 0x%8.8x\n", header.bss_size);
903 s->Printf (" entry = 0x%8.8x\n", header.entry);
904 s->Printf (" code_offset = 0x%8.8x\n", header.code_offset);
905 s->Printf (" data_offset = 0x%8.8x\n", header.data_offset);
Daniel Malead01b2952012-11-29 21:49:15 +0000906 s->Printf (" image_base = 0x%16.16" PRIx64 "\n", header.image_base);
Greg Claytonf754f882011-09-09 20:33:05 +0000907 s->Printf (" sect_alignment = 0x%8.8x\n", header.sect_alignment);
908 s->Printf (" file_alignment = 0x%8.8x\n", header.file_alignment);
909 s->Printf (" major_os_system_version = 0x%4.4x\n", header.major_os_system_version);
910 s->Printf (" minor_os_system_version = 0x%4.4x\n", header.minor_os_system_version);
911 s->Printf (" major_image_version = 0x%4.4x\n", header.major_image_version);
912 s->Printf (" minor_image_version = 0x%4.4x\n", header.minor_image_version);
913 s->Printf (" major_subsystem_version = 0x%4.4x\n", header.major_subsystem_version);
914 s->Printf (" minor_subsystem_version = 0x%4.4x\n", header.minor_subsystem_version);
915 s->Printf (" reserved1 = 0x%8.8x\n", header.reserved1);
916 s->Printf (" image_size = 0x%8.8x\n", header.image_size);
917 s->Printf (" header_size = 0x%8.8x\n", header.header_size);
Greg Clayton28469ca2011-09-10 01:04:42 +0000918 s->Printf (" checksum = 0x%8.8x\n", header.checksum);
Greg Claytonf754f882011-09-09 20:33:05 +0000919 s->Printf (" subsystem = 0x%4.4x\n", header.subsystem);
920 s->Printf (" dll_flags = 0x%4.4x\n", header.dll_flags);
Daniel Malead01b2952012-11-29 21:49:15 +0000921 s->Printf (" stack_reserve_size = 0x%16.16" PRIx64 "\n", header.stack_reserve_size);
922 s->Printf (" stack_commit_size = 0x%16.16" PRIx64 "\n", header.stack_commit_size);
923 s->Printf (" heap_reserve_size = 0x%16.16" PRIx64 "\n", header.heap_reserve_size);
924 s->Printf (" heap_commit_size = 0x%16.16" PRIx64 "\n", header.heap_commit_size);
Greg Claytonf754f882011-09-09 20:33:05 +0000925 s->Printf (" loader_flags = 0x%8.8x\n", header.loader_flags);
Virgile Belloffeba252014-03-08 17:15:35 +0000926 s->Printf (" num_data_dir_entries = 0x%8.8x\n", (uint32_t)header.data_dirs.size());
Greg Claytonf754f882011-09-09 20:33:05 +0000927 uint32_t i;
928 for (i=0; i<header.data_dirs.size(); i++)
929 {
Greg Clayton28469ca2011-09-10 01:04:42 +0000930 s->Printf (" data_dirs[%2u] vmaddr = 0x%8.8x, vmsize = 0x%8.8x\n",
Greg Claytonf754f882011-09-09 20:33:05 +0000931 i,
932 header.data_dirs[i].vmaddr,
933 header.data_dirs[i].vmsize);
934 }
935}
936//----------------------------------------------------------------------
937// DumpSectionHeader
938//
939// Dump a single ELF section header to the specified output stream
940//----------------------------------------------------------------------
941void
942ObjectFilePECOFF::DumpSectionHeader(Stream *s, const section_header_t& sh)
943{
944 std::string name;
945 GetSectionName(name, sh);
946 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",
947 name.c_str(),
Greg Claytonf754f882011-09-09 20:33:05 +0000948 sh.vmaddr,
Greg Clayton28469ca2011-09-10 01:04:42 +0000949 sh.vmsize,
Greg Claytonf754f882011-09-09 20:33:05 +0000950 sh.offset,
Greg Clayton28469ca2011-09-10 01:04:42 +0000951 sh.size,
Greg Claytonf754f882011-09-09 20:33:05 +0000952 sh.reloff,
953 sh.lineoff,
954 sh.nreloc,
955 sh.nline,
956 sh.flags);
957}
958
959
960//----------------------------------------------------------------------
961// DumpSectionHeaders
962//
963// Dump all of the ELF section header to the specified output stream
964//----------------------------------------------------------------------
965void
966ObjectFilePECOFF::DumpSectionHeaders(Stream *s)
967{
968
969 s->PutCString ("Section Headers\n");
Greg Clayton28469ca2011-09-10 01:04:42 +0000970 s->PutCString ("IDX name vm addr vm size file off file size reloc off line off nreloc nline flags\n");
971 s->PutCString ("==== ---------------- ---------- ---------- ---------- ---------- ---------- ---------- ------ ------ ----------\n");
Greg Claytonf754f882011-09-09 20:33:05 +0000972
973 uint32_t idx = 0;
974 SectionHeaderCollIter pos, end = m_sect_headers.end();
975
976 for (pos = m_sect_headers.begin(); pos != end; ++pos, ++idx)
977 {
Greg Clayton28469ca2011-09-10 01:04:42 +0000978 s->Printf ("[%2u] ", idx);
Greg Claytonf754f882011-09-09 20:33:05 +0000979 ObjectFilePECOFF::DumpSectionHeader(s, *pos);
980 }
981}
982
Greg Claytonf754f882011-09-09 20:33:05 +0000983bool
984ObjectFilePECOFF::GetArchitecture (ArchSpec &arch)
985{
Charles Davis237ad972013-08-27 05:04:33 +0000986 uint16_t machine = m_coff_header.machine;
987 switch (machine)
988 {
989 case llvm::COFF::IMAGE_FILE_MACHINE_AMD64:
990 case llvm::COFF::IMAGE_FILE_MACHINE_I386:
991 case llvm::COFF::IMAGE_FILE_MACHINE_POWERPC:
992 case llvm::COFF::IMAGE_FILE_MACHINE_POWERPCFP:
993 case llvm::COFF::IMAGE_FILE_MACHINE_ARM:
Saleem Abdulrasool1108cb32014-03-11 03:09:08 +0000994 case llvm::COFF::IMAGE_FILE_MACHINE_ARMNT:
Charles Davis237ad972013-08-27 05:04:33 +0000995 case llvm::COFF::IMAGE_FILE_MACHINE_THUMB:
Colin Riley6c970422013-11-22 09:35:12 +0000996 arch.SetArchitecture (eArchTypeCOFF, machine, LLDB_INVALID_CPUTYPE);
Charles Davis237ad972013-08-27 05:04:33 +0000997 return true;
998 default:
999 break;
1000 }
1001 return false;
Greg Claytonf754f882011-09-09 20:33:05 +00001002}
1003
1004ObjectFile::Type
1005ObjectFilePECOFF::CalculateType()
1006{
1007 if (m_coff_header.machine != 0)
1008 {
Charles Davis237ad972013-08-27 05:04:33 +00001009 if ((m_coff_header.flags & llvm::COFF::IMAGE_FILE_DLL) == 0)
Greg Claytonf754f882011-09-09 20:33:05 +00001010 return eTypeExecutable;
1011 else
1012 return eTypeSharedLibrary;
1013 }
1014 return eTypeExecutable;
1015}
1016
1017ObjectFile::Strata
1018ObjectFilePECOFF::CalculateStrata()
1019{
1020 return eStrataUser;
1021}
1022//------------------------------------------------------------------
1023// PluginInterface protocol
1024//------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +00001025ConstString
Greg Claytonf754f882011-09-09 20:33:05 +00001026ObjectFilePECOFF::GetPluginName()
1027{
Greg Claytonf754f882011-09-09 20:33:05 +00001028 return GetPluginNameStatic();
1029}
1030
1031uint32_t
1032ObjectFilePECOFF::GetPluginVersion()
1033{
1034 return 1;
1035}
1036