blob: 52c6ac8fb65ca64d820cf6940a2e49dddd063f44 [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"
Adrian McCarthyf7d18932015-11-20 23:09:11 +000011#include "WindowsMiniDump.h"
Greg Claytonf754f882011-09-09 20:33:05 +000012
Charles Davis237ad972013-08-27 05:04:33 +000013#include "llvm/Support/COFF.h"
Greg Claytonf754f882011-09-09 20:33:05 +000014
15#include "lldb/Core/ArchSpec.h"
16#include "lldb/Core/DataBuffer.h"
17#include "lldb/Host/FileSpec.h"
18#include "lldb/Core/FileSpecList.h"
19#include "lldb/Core/Module.h"
Greg Claytonf4d6de62013-04-24 22:29:28 +000020#include "lldb/Core/ModuleSpec.h"
Greg Claytonf754f882011-09-09 20:33:05 +000021#include "lldb/Core/PluginManager.h"
22#include "lldb/Core/Section.h"
23#include "lldb/Core/StreamFile.h"
24#include "lldb/Core/StreamString.h"
25#include "lldb/Core/Timer.h"
26#include "lldb/Core/UUID.h"
27#include "lldb/Symbol/ObjectFile.h"
Adrian McCarthyf7d18932015-11-20 23:09:11 +000028#include "lldb/Target/Process.h"
Virgile Bello2756adf2014-03-08 17:17:20 +000029#include "lldb/Target/SectionLoadList.h"
30#include "lldb/Target/Target.h"
Greg Claytonf754f882011-09-09 20:33:05 +000031
Greg Claytonf754f882011-09-09 20:33:05 +000032#define IMAGE_DOS_SIGNATURE 0x5A4D // MZ
Greg Claytonf754f882011-09-09 20:33:05 +000033#define IMAGE_NT_SIGNATURE 0x00004550 // PE00
34#define OPT_HEADER_MAGIC_PE32 0x010b
35#define OPT_HEADER_MAGIC_PE32_PLUS 0x020b
36
Greg Claytonf754f882011-09-09 20:33:05 +000037using namespace lldb;
38using namespace lldb_private;
39
40void
41ObjectFilePECOFF::Initialize()
42{
43 PluginManager::RegisterPlugin (GetPluginNameStatic(),
44 GetPluginDescriptionStatic(),
Greg Claytonc9660542012-02-05 02:38:54 +000045 CreateInstance,
Greg Claytonf4d6de62013-04-24 22:29:28 +000046 CreateMemoryInstance,
Adrian McCarthyf7d18932015-11-20 23:09:11 +000047 GetModuleSpecifications,
48 SaveCore);
Greg Claytonf754f882011-09-09 20:33:05 +000049}
50
51void
52ObjectFilePECOFF::Terminate()
53{
54 PluginManager::UnregisterPlugin (CreateInstance);
55}
56
57
Greg Clayton57abc5d2013-05-10 21:47:16 +000058lldb_private::ConstString
Greg Claytonf754f882011-09-09 20:33:05 +000059ObjectFilePECOFF::GetPluginNameStatic()
60{
Greg Clayton57abc5d2013-05-10 21:47:16 +000061 static ConstString g_name("pe-coff");
62 return g_name;
Greg Claytonf754f882011-09-09 20:33:05 +000063}
64
65const char *
66ObjectFilePECOFF::GetPluginDescriptionStatic()
67{
68 return "Portable Executable and Common Object File Format object file reader (32 and 64 bit)";
69}
70
71
72ObjectFile *
Greg Clayton5ce9c562013-02-06 17:22:03 +000073ObjectFilePECOFF::CreateInstance (const lldb::ModuleSP &module_sp,
74 DataBufferSP& data_sp,
75 lldb::offset_t data_offset,
76 const lldb_private::FileSpec* file,
77 lldb::offset_t file_offset,
78 lldb::offset_t length)
Greg Claytonf754f882011-09-09 20:33:05 +000079{
Greg Clayton5ce9c562013-02-06 17:22:03 +000080 if (!data_sp)
Greg Claytonf754f882011-09-09 20:33:05 +000081 {
Greg Clayton736888c2015-02-23 23:47:09 +000082 data_sp = file->MemoryMapFileContentsIfLocal(file_offset, length);
Greg Clayton5ce9c562013-02-06 17:22:03 +000083 data_offset = 0;
84 }
85
86 if (ObjectFilePECOFF::MagicBytesMatch(data_sp))
87 {
88 // Update the data to contain the entire file if it doesn't already
89 if (data_sp->GetByteSize() < length)
Greg Clayton736888c2015-02-23 23:47:09 +000090 data_sp = file->MemoryMapFileContentsIfLocal(file_offset, length);
Greg Clayton7b0992d2013-04-18 22:45:39 +000091 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 +000092 if (objfile_ap.get() && objfile_ap->ParseHeader())
93 return objfile_ap.release();
94 }
95 return NULL;
96}
97
Greg Claytonc9660542012-02-05 02:38:54 +000098ObjectFile *
Greg Claytone72dfb32012-02-24 01:59:29 +000099ObjectFilePECOFF::CreateMemoryInstance (const lldb::ModuleSP &module_sp,
Greg Claytonc9660542012-02-05 02:38:54 +0000100 lldb::DataBufferSP& data_sp,
101 const lldb::ProcessSP &process_sp,
102 lldb::addr_t header_addr)
103{
104 return NULL;
105}
106
Greg Claytonf4d6de62013-04-24 22:29:28 +0000107size_t
108ObjectFilePECOFF::GetModuleSpecifications (const lldb_private::FileSpec& file,
109 lldb::DataBufferSP& data_sp,
110 lldb::offset_t data_offset,
111 lldb::offset_t file_offset,
112 lldb::offset_t length,
113 lldb_private::ModuleSpecList &specs)
114{
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000115 const size_t initial_count = specs.GetSize();
116
117 if (ObjectFilePECOFF::MagicBytesMatch(data_sp))
118 {
119 DataExtractor data;
120 data.SetData(data_sp, data_offset, length);
121 data.SetByteOrder(eByteOrderLittle);
122
123 dos_header_t dos_header;
124 coff_header_t coff_header;
125
126 if (ParseDOSHeader(data, dos_header))
127 {
128 lldb::offset_t offset = dos_header.e_lfanew;
129 uint32_t pe_signature = data.GetU32(&offset);
130 if (pe_signature != IMAGE_NT_SIGNATURE)
131 return false;
132 if (ParseCOFFHeader(data, &offset, coff_header))
133 {
134 ArchSpec spec;
Zachary Turnerad587ae42014-07-28 16:44:49 +0000135 if (coff_header.machine == MachineAmd64)
Zachary Turner5e6f4522015-01-22 18:59:05 +0000136 {
Zachary Turnerad587ae42014-07-28 16:44:49 +0000137 spec.SetTriple("x86_64-pc-windows");
Zachary Turner5e6f4522015-01-22 18:59:05 +0000138 specs.Append(ModuleSpec(file, spec));
139 }
Zachary Turnerad587ae42014-07-28 16:44:49 +0000140 else if (coff_header.machine == MachineX86)
Zachary Turner5e6f4522015-01-22 18:59:05 +0000141 {
Zachary Turnerad587ae42014-07-28 16:44:49 +0000142 spec.SetTriple("i386-pc-windows");
Zachary Turner5e6f4522015-01-22 18:59:05 +0000143 specs.Append(ModuleSpec(file, spec));
144 spec.SetTriple("i686-pc-windows");
145 specs.Append(ModuleSpec(file, spec));
146 }
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000147 }
148 }
149 }
150
151 return specs.GetSize() - initial_count;
Greg Claytonf4d6de62013-04-24 22:29:28 +0000152}
153
Adrian McCarthyf7d18932015-11-20 23:09:11 +0000154bool
155ObjectFilePECOFF::SaveCore(const lldb::ProcessSP &process_sp,
156 const lldb_private::FileSpec &outfile,
157 lldb_private::Error &error)
158{
159 return SaveMiniDump(process_sp, outfile, error);
160}
161
Greg Claytonf4d6de62013-04-24 22:29:28 +0000162
Greg Claytonf754f882011-09-09 20:33:05 +0000163bool
Greg Clayton5ce9c562013-02-06 17:22:03 +0000164ObjectFilePECOFF::MagicBytesMatch (DataBufferSP& data_sp)
Greg Claytonf754f882011-09-09 20:33:05 +0000165{
Greg Clayton5ce9c562013-02-06 17:22:03 +0000166 DataExtractor data(data_sp, eByteOrderLittle, 4);
Greg Claytonc7bece562013-01-25 18:06:21 +0000167 lldb::offset_t offset = 0;
Greg Claytonf754f882011-09-09 20:33:05 +0000168 uint16_t magic = data.GetU16 (&offset);
169 return magic == IMAGE_DOS_SIGNATURE;
170}
171
Adrian McCarthyc35b91c2016-01-26 00:58:09 +0000172lldb::SymbolType
173ObjectFilePECOFF::MapSymbolType(uint16_t coff_symbol_type)
174{
175 // TODO: We need to complete this mapping of COFF symbol types to LLDB ones.
176 // For now, here's a hack to make sure our function have types.
177 const auto complex_type = coff_symbol_type >> llvm::COFF::SCT_COMPLEX_TYPE_SHIFT;
178 if (complex_type == llvm::COFF::IMAGE_SYM_DTYPE_FUNCTION)
179 {
180 return lldb::eSymbolTypeCode;
181 }
182 return lldb::eSymbolTypeInvalid;
183}
Greg Claytonf754f882011-09-09 20:33:05 +0000184
Greg Claytone72dfb32012-02-24 01:59:29 +0000185ObjectFilePECOFF::ObjectFilePECOFF (const lldb::ModuleSP &module_sp,
Greg Clayton5ce9c562013-02-06 17:22:03 +0000186 DataBufferSP& data_sp,
187 lldb::offset_t data_offset,
Greg Claytonf754f882011-09-09 20:33:05 +0000188 const FileSpec* file,
Greg Clayton5ce9c562013-02-06 17:22:03 +0000189 lldb::offset_t file_offset,
190 lldb::offset_t length) :
191 ObjectFile (module_sp, file, file_offset, length, data_sp, data_offset),
Greg Claytonf754f882011-09-09 20:33:05 +0000192 m_dos_header (),
193 m_coff_header (),
194 m_coff_header_opt (),
195 m_sect_headers ()
196{
197 ::memset (&m_dos_header, 0, sizeof(m_dos_header));
198 ::memset (&m_coff_header, 0, sizeof(m_coff_header));
199 ::memset (&m_coff_header_opt, 0, sizeof(m_coff_header_opt));
200}
201
202
203ObjectFilePECOFF::~ObjectFilePECOFF()
204{
205}
206
207
208bool
209ObjectFilePECOFF::ParseHeader ()
210{
Greg Claytona1743492012-03-13 23:14:29 +0000211 ModuleSP module_sp(GetModule());
212 if (module_sp)
Greg Claytonf754f882011-09-09 20:33:05 +0000213 {
Greg Claytona1743492012-03-13 23:14:29 +0000214 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
215 m_sect_headers.clear();
216 m_data.SetByteOrder (eByteOrderLittle);
Greg Claytonc7bece562013-01-25 18:06:21 +0000217 lldb::offset_t offset = 0;
Greg Claytona1743492012-03-13 23:14:29 +0000218
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000219 if (ParseDOSHeader(m_data, m_dos_header))
Greg Clayton28469ca2011-09-10 01:04:42 +0000220 {
Greg Claytona1743492012-03-13 23:14:29 +0000221 offset = m_dos_header.e_lfanew;
222 uint32_t pe_signature = m_data.GetU32 (&offset);
223 if (pe_signature != IMAGE_NT_SIGNATURE)
224 return false;
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000225 if (ParseCOFFHeader(m_data, &offset, m_coff_header))
Greg Claytona1743492012-03-13 23:14:29 +0000226 {
227 if (m_coff_header.hdrsize > 0)
228 ParseCOFFOptionalHeader(&offset);
229 ParseSectionHeaders (offset);
230 }
Greg Claytona1743492012-03-13 23:14:29 +0000231 return true;
Greg Clayton28469ca2011-09-10 01:04:42 +0000232 }
Greg Claytonf754f882011-09-09 20:33:05 +0000233 }
234 return false;
235}
236
Virgile Bello2756adf2014-03-08 17:17:20 +0000237bool
238ObjectFilePECOFF::SetLoadAddress(Target &target, addr_t value, bool value_is_offset)
239{
240 bool changed = false;
241 ModuleSP module_sp = GetModule();
242 if (module_sp)
243 {
244 size_t num_loaded_sections = 0;
245 SectionList *section_list = GetSectionList ();
246 if (section_list)
247 {
248 if (!value_is_offset)
249 {
250 value -= m_image_base;
251 }
252
253 const size_t num_sections = section_list->GetSize();
254 size_t sect_idx = 0;
255
256 for (sect_idx = 0; sect_idx < num_sections; ++sect_idx)
257 {
258 // Iterate through the object file sections to find all
259 // of the sections that have SHF_ALLOC in their flag bits.
260 SectionSP section_sp (section_list->GetSectionAtIndex (sect_idx));
261 if (section_sp && !section_sp->IsThreadSpecific())
262 {
263 if (target.GetSectionLoadList().SetSectionLoadAddress (section_sp, section_sp->GetFileAddress() + value))
264 ++num_loaded_sections;
265 }
266 }
267 changed = num_loaded_sections > 0;
Virgile Bello2756adf2014-03-08 17:17:20 +0000268 }
269 }
270 return changed;
271}
272
Greg Claytonf754f882011-09-09 20:33:05 +0000273
274ByteOrder
275ObjectFilePECOFF::GetByteOrder () const
276{
277 return eByteOrderLittle;
278}
279
280bool
281ObjectFilePECOFF::IsExecutable() const
282{
Charles Davis237ad972013-08-27 05:04:33 +0000283 return (m_coff_header.flags & llvm::COFF::IMAGE_FILE_DLL) == 0;
Greg Claytonf754f882011-09-09 20:33:05 +0000284}
285
Greg Claytonc7bece562013-01-25 18:06:21 +0000286uint32_t
Greg Claytonf754f882011-09-09 20:33:05 +0000287ObjectFilePECOFF::GetAddressByteSize () const
288{
289 if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32_PLUS)
290 return 8;
291 else if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32)
292 return 4;
293 return 4;
294}
295
296//----------------------------------------------------------------------
297// NeedsEndianSwap
298//
299// Return true if an endian swap needs to occur when extracting data
300// from this file.
301//----------------------------------------------------------------------
302bool
303ObjectFilePECOFF::NeedsEndianSwap() const
304{
305#if defined(__LITTLE_ENDIAN__)
306 return false;
307#else
308 return true;
309#endif
310}
311//----------------------------------------------------------------------
312// ParseDOSHeader
313//----------------------------------------------------------------------
314bool
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000315ObjectFilePECOFF::ParseDOSHeader (DataExtractor &data, dos_header_t &dos_header)
Greg Claytonf754f882011-09-09 20:33:05 +0000316{
317 bool success = false;
Greg Claytonc7bece562013-01-25 18:06:21 +0000318 lldb::offset_t offset = 0;
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000319 success = data.ValidOffsetForDataOfSize(0, sizeof(dos_header));
Greg Claytonf754f882011-09-09 20:33:05 +0000320
321 if (success)
322 {
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000323 dos_header.e_magic = data.GetU16(&offset); // Magic number
324 success = dos_header.e_magic == IMAGE_DOS_SIGNATURE;
Greg Claytonf754f882011-09-09 20:33:05 +0000325
326 if (success)
327 {
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000328 dos_header.e_cblp = data.GetU16(&offset); // Bytes on last page of file
329 dos_header.e_cp = data.GetU16(&offset); // Pages in file
330 dos_header.e_crlc = data.GetU16(&offset); // Relocations
331 dos_header.e_cparhdr = data.GetU16(&offset); // Size of header in paragraphs
332 dos_header.e_minalloc = data.GetU16(&offset); // Minimum extra paragraphs needed
333 dos_header.e_maxalloc = data.GetU16(&offset); // Maximum extra paragraphs needed
334 dos_header.e_ss = data.GetU16(&offset); // Initial (relative) SS value
335 dos_header.e_sp = data.GetU16(&offset); // Initial SP value
336 dos_header.e_csum = data.GetU16(&offset); // Checksum
337 dos_header.e_ip = data.GetU16(&offset); // Initial IP value
338 dos_header.e_cs = data.GetU16(&offset); // Initial (relative) CS value
339 dos_header.e_lfarlc = data.GetU16(&offset); // File address of relocation table
340 dos_header.e_ovno = data.GetU16(&offset); // Overlay number
Greg Claytonf754f882011-09-09 20:33:05 +0000341
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000342 dos_header.e_res[0] = data.GetU16(&offset); // Reserved words
343 dos_header.e_res[1] = data.GetU16(&offset); // Reserved words
344 dos_header.e_res[2] = data.GetU16(&offset); // Reserved words
345 dos_header.e_res[3] = data.GetU16(&offset); // Reserved words
Greg Claytonf754f882011-09-09 20:33:05 +0000346
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000347 dos_header.e_oemid = data.GetU16(&offset); // OEM identifier (for e_oeminfo)
348 dos_header.e_oeminfo = data.GetU16(&offset); // OEM information; e_oemid specific
349 dos_header.e_res2[0] = data.GetU16(&offset); // Reserved words
350 dos_header.e_res2[1] = data.GetU16(&offset); // Reserved words
351 dos_header.e_res2[2] = data.GetU16(&offset); // Reserved words
352 dos_header.e_res2[3] = data.GetU16(&offset); // Reserved words
353 dos_header.e_res2[4] = data.GetU16(&offset); // Reserved words
354 dos_header.e_res2[5] = data.GetU16(&offset); // Reserved words
355 dos_header.e_res2[6] = data.GetU16(&offset); // Reserved words
356 dos_header.e_res2[7] = data.GetU16(&offset); // Reserved words
357 dos_header.e_res2[8] = data.GetU16(&offset); // Reserved words
358 dos_header.e_res2[9] = data.GetU16(&offset); // Reserved words
Greg Claytonf754f882011-09-09 20:33:05 +0000359
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000360 dos_header.e_lfanew = data.GetU32(&offset); // File address of new exe header
Greg Claytonf754f882011-09-09 20:33:05 +0000361 }
362 }
363 if (!success)
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000364 memset(&dos_header, 0, sizeof(dos_header));
Greg Claytonf754f882011-09-09 20:33:05 +0000365 return success;
366}
367
368
369//----------------------------------------------------------------------
370// ParserCOFFHeader
371//----------------------------------------------------------------------
372bool
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000373ObjectFilePECOFF::ParseCOFFHeader(DataExtractor &data, lldb::offset_t *offset_ptr, coff_header_t &coff_header)
Greg Claytonf754f882011-09-09 20:33:05 +0000374{
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000375 bool success = data.ValidOffsetForDataOfSize (*offset_ptr, sizeof(coff_header));
Greg Claytonf754f882011-09-09 20:33:05 +0000376 if (success)
377 {
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000378 coff_header.machine = data.GetU16(offset_ptr);
379 coff_header.nsects = data.GetU16(offset_ptr);
380 coff_header.modtime = data.GetU32(offset_ptr);
381 coff_header.symoff = data.GetU32(offset_ptr);
382 coff_header.nsyms = data.GetU32(offset_ptr);
383 coff_header.hdrsize = data.GetU16(offset_ptr);
384 coff_header.flags = data.GetU16(offset_ptr);
Greg Claytonf754f882011-09-09 20:33:05 +0000385 }
386 if (!success)
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000387 memset(&coff_header, 0, sizeof(coff_header));
Greg Claytonf754f882011-09-09 20:33:05 +0000388 return success;
389}
390
391bool
Greg Claytonc7bece562013-01-25 18:06:21 +0000392ObjectFilePECOFF::ParseCOFFOptionalHeader(lldb::offset_t *offset_ptr)
Greg Claytonf754f882011-09-09 20:33:05 +0000393{
394 bool success = false;
Greg Claytonc7bece562013-01-25 18:06:21 +0000395 const lldb::offset_t end_offset = *offset_ptr + m_coff_header.hdrsize;
Greg Claytonf754f882011-09-09 20:33:05 +0000396 if (*offset_ptr < end_offset)
397 {
398 success = true;
399 m_coff_header_opt.magic = m_data.GetU16(offset_ptr);
400 m_coff_header_opt.major_linker_version = m_data.GetU8 (offset_ptr);
401 m_coff_header_opt.minor_linker_version = m_data.GetU8 (offset_ptr);
402 m_coff_header_opt.code_size = m_data.GetU32(offset_ptr);
403 m_coff_header_opt.data_size = m_data.GetU32(offset_ptr);
404 m_coff_header_opt.bss_size = m_data.GetU32(offset_ptr);
405 m_coff_header_opt.entry = m_data.GetU32(offset_ptr);
406 m_coff_header_opt.code_offset = m_data.GetU32(offset_ptr);
407
408 const uint32_t addr_byte_size = GetAddressByteSize ();
409
410 if (*offset_ptr < end_offset)
411 {
412 if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32)
413 {
414 // PE32 only
415 m_coff_header_opt.data_offset = m_data.GetU32(offset_ptr);
416 }
417 else
418 m_coff_header_opt.data_offset = 0;
419
420 if (*offset_ptr < end_offset)
421 {
422 m_coff_header_opt.image_base = m_data.GetMaxU64 (offset_ptr, addr_byte_size);
423 m_coff_header_opt.sect_alignment = m_data.GetU32(offset_ptr);
424 m_coff_header_opt.file_alignment = m_data.GetU32(offset_ptr);
425 m_coff_header_opt.major_os_system_version = m_data.GetU16(offset_ptr);
426 m_coff_header_opt.minor_os_system_version = m_data.GetU16(offset_ptr);
427 m_coff_header_opt.major_image_version = m_data.GetU16(offset_ptr);
428 m_coff_header_opt.minor_image_version = m_data.GetU16(offset_ptr);
429 m_coff_header_opt.major_subsystem_version = m_data.GetU16(offset_ptr);
430 m_coff_header_opt.minor_subsystem_version = m_data.GetU16(offset_ptr);
431 m_coff_header_opt.reserved1 = m_data.GetU32(offset_ptr);
432 m_coff_header_opt.image_size = m_data.GetU32(offset_ptr);
433 m_coff_header_opt.header_size = m_data.GetU32(offset_ptr);
Greg Clayton28469ca2011-09-10 01:04:42 +0000434 m_coff_header_opt.checksum = m_data.GetU32(offset_ptr);
Greg Claytonf754f882011-09-09 20:33:05 +0000435 m_coff_header_opt.subsystem = m_data.GetU16(offset_ptr);
436 m_coff_header_opt.dll_flags = m_data.GetU16(offset_ptr);
437 m_coff_header_opt.stack_reserve_size = m_data.GetMaxU64 (offset_ptr, addr_byte_size);
438 m_coff_header_opt.stack_commit_size = m_data.GetMaxU64 (offset_ptr, addr_byte_size);
439 m_coff_header_opt.heap_reserve_size = m_data.GetMaxU64 (offset_ptr, addr_byte_size);
440 m_coff_header_opt.heap_commit_size = m_data.GetMaxU64 (offset_ptr, addr_byte_size);
441 m_coff_header_opt.loader_flags = m_data.GetU32(offset_ptr);
442 uint32_t num_data_dir_entries = m_data.GetU32(offset_ptr);
443 m_coff_header_opt.data_dirs.clear();
444 m_coff_header_opt.data_dirs.resize(num_data_dir_entries);
445 uint32_t i;
446 for (i=0; i<num_data_dir_entries; i++)
447 {
448 m_coff_header_opt.data_dirs[i].vmaddr = m_data.GetU32(offset_ptr);
449 m_coff_header_opt.data_dirs[i].vmsize = m_data.GetU32(offset_ptr);
450 }
Virgile Bello2756adf2014-03-08 17:17:20 +0000451
452 m_file_offset = m_coff_header_opt.image_base;
453 m_image_base = m_coff_header_opt.image_base;
Greg Claytonf754f882011-09-09 20:33:05 +0000454 }
455 }
456 }
457 // Make sure we are on track for section data which follows
458 *offset_ptr = end_offset;
459 return success;
460}
461
462
463//----------------------------------------------------------------------
464// ParseSectionHeaders
465//----------------------------------------------------------------------
466bool
467ObjectFilePECOFF::ParseSectionHeaders (uint32_t section_header_data_offset)
468{
469 const uint32_t nsects = m_coff_header.nsects;
470 m_sect_headers.clear();
471
472 if (nsects > 0)
473 {
474 const uint32_t addr_byte_size = GetAddressByteSize ();
475 const size_t section_header_byte_size = nsects * sizeof(section_header_t);
476 DataBufferSP section_header_data_sp(m_file.ReadFileContents (section_header_data_offset, section_header_byte_size));
477 DataExtractor section_header_data (section_header_data_sp, GetByteOrder(), addr_byte_size);
478
Greg Claytonc7bece562013-01-25 18:06:21 +0000479 lldb::offset_t offset = 0;
Greg Claytonf754f882011-09-09 20:33:05 +0000480 if (section_header_data.ValidOffsetForDataOfSize (offset, section_header_byte_size))
481 {
482 m_sect_headers.resize(nsects);
483
484 for (uint32_t idx = 0; idx<nsects; ++idx)
485 {
486 const void *name_data = section_header_data.GetData(&offset, 8);
487 if (name_data)
488 {
489 memcpy(m_sect_headers[idx].name, name_data, 8);
490 m_sect_headers[idx].vmsize = section_header_data.GetU32(&offset);
491 m_sect_headers[idx].vmaddr = section_header_data.GetU32(&offset);
492 m_sect_headers[idx].size = section_header_data.GetU32(&offset);
493 m_sect_headers[idx].offset = section_header_data.GetU32(&offset);
494 m_sect_headers[idx].reloff = section_header_data.GetU32(&offset);
495 m_sect_headers[idx].lineoff = section_header_data.GetU32(&offset);
496 m_sect_headers[idx].nreloc = section_header_data.GetU16(&offset);
497 m_sect_headers[idx].nline = section_header_data.GetU16(&offset);
498 m_sect_headers[idx].flags = section_header_data.GetU32(&offset);
499 }
500 }
501 }
502 }
503
504 return m_sect_headers.empty() == false;
505}
506
507bool
508ObjectFilePECOFF::GetSectionName(std::string& sect_name, const section_header_t& sect)
509{
510 if (sect.name[0] == '/')
511 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000512 lldb::offset_t stroff = strtoul(&sect.name[1], NULL, 10);
513 lldb::offset_t string_file_offset = m_coff_header.symoff + (m_coff_header.nsyms * 18) + stroff;
Greg Claytonf754f882011-09-09 20:33:05 +0000514 const char *name = m_data.GetCStr (&string_file_offset);
515 if (name)
516 {
517 sect_name = name;
518 return true;
519 }
520
521 return false;
522 }
523 sect_name = sect.name;
524 return true;
525}
526
527//----------------------------------------------------------------------
528// GetNListSymtab
529//----------------------------------------------------------------------
530Symtab *
Greg Clayton3046e662013-07-10 01:23:25 +0000531ObjectFilePECOFF::GetSymtab()
Greg Claytonf754f882011-09-09 20:33:05 +0000532{
Greg Claytona1743492012-03-13 23:14:29 +0000533 ModuleSP module_sp(GetModule());
534 if (module_sp)
Greg Claytonf754f882011-09-09 20:33:05 +0000535 {
Greg Claytona1743492012-03-13 23:14:29 +0000536 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
537 if (m_symtab_ap.get() == NULL)
Greg Claytonf754f882011-09-09 20:33:05 +0000538 {
Greg Claytona1743492012-03-13 23:14:29 +0000539 SectionList *sect_list = GetSectionList();
540 m_symtab_ap.reset(new Symtab(this));
541 Mutex::Locker symtab_locker (m_symtab_ap->GetMutex());
Greg Claytonf754f882011-09-09 20:33:05 +0000542
Greg Claytona1743492012-03-13 23:14:29 +0000543 const uint32_t num_syms = m_coff_header.nsyms;
544
545 if (num_syms > 0 && m_coff_header.symoff > 0)
546 {
Greg Clayton0076e712013-06-18 00:08:58 +0000547 const uint32_t symbol_size = 18;
Greg Claytona1743492012-03-13 23:14:29 +0000548 const uint32_t addr_byte_size = GetAddressByteSize ();
Adrian McCarthyc35b91c2016-01-26 00:58:09 +0000549 const size_t symbol_data_size = num_syms * symbol_size;
550 // Include the 4-byte string table size at the end of the symbols
Greg Claytona1743492012-03-13 23:14:29 +0000551 DataBufferSP symtab_data_sp(m_file.ReadFileContents (m_coff_header.symoff, symbol_data_size + 4));
552 DataExtractor symtab_data (symtab_data_sp, GetByteOrder(), addr_byte_size);
Greg Claytonc7bece562013-01-25 18:06:21 +0000553 lldb::offset_t offset = symbol_data_size;
Greg Claytona1743492012-03-13 23:14:29 +0000554 const uint32_t strtab_size = symtab_data.GetU32 (&offset);
Greg Clayton0076e712013-06-18 00:08:58 +0000555 DataBufferSP strtab_data_sp(m_file.ReadFileContents (m_coff_header.symoff + symbol_data_size, strtab_size));
Greg Claytona1743492012-03-13 23:14:29 +0000556 DataExtractor strtab_data (strtab_data_sp, GetByteOrder(), addr_byte_size);
557
Greg Clayton0076e712013-06-18 00:08:58 +0000558 // First 4 bytes should be zeroed after strtab_size has been read,
559 // because it is used as offset 0 to encode a NULL string.
560 uint32_t* strtab_data_start = (uint32_t*)strtab_data_sp->GetBytes();
561 strtab_data_start[0] = 0;
562
Greg Claytona1743492012-03-13 23:14:29 +0000563 offset = 0;
564 std::string symbol_name;
565 Symbol *symbols = m_symtab_ap->Resize (num_syms);
566 for (uint32_t i=0; i<num_syms; ++i)
567 {
568 coff_symbol_t symbol;
569 const uint32_t symbol_offset = offset;
570 const char *symbol_name_cstr = NULL;
Adrian McCarthyc35b91c2016-01-26 00:58:09 +0000571 // If the first 4 bytes of the symbol string are zero, then they
572 // are followed by a 4-byte string table offset. Else these
Greg Claytona1743492012-03-13 23:14:29 +0000573 // 8 bytes contain the symbol name
574 if (symtab_data.GetU32 (&offset) == 0)
575 {
576 // Long string that doesn't fit into the symbol table name,
577 // so now we must read the 4 byte string table offset
578 uint32_t strtab_offset = symtab_data.GetU32 (&offset);
579 symbol_name_cstr = strtab_data.PeekCStr (strtab_offset);
580 symbol_name.assign (symbol_name_cstr);
581 }
582 else
583 {
584 // Short string that fits into the symbol table name which is 8 bytes
585 offset += sizeof(symbol.name) - 4; // Skip remaining
586 symbol_name_cstr = symtab_data.PeekCStr (symbol_offset);
587 if (symbol_name_cstr == NULL)
588 break;
589 symbol_name.assign (symbol_name_cstr, sizeof(symbol.name));
590 }
591 symbol.value = symtab_data.GetU32 (&offset);
592 symbol.sect = symtab_data.GetU16 (&offset);
593 symbol.type = symtab_data.GetU16 (&offset);
594 symbol.storage = symtab_data.GetU8 (&offset);
595 symbol.naux = symtab_data.GetU8 (&offset);
Greg Clayton037520e2012-07-18 23:18:10 +0000596 symbols[i].GetMangled ().SetValue (ConstString(symbol_name.c_str()));
Greg Clayton0076e712013-06-18 00:08:58 +0000597 if ((int16_t)symbol.sect >= 1)
598 {
599 Address symbol_addr(sect_list->GetSectionAtIndex(symbol.sect-1), symbol.value);
Greg Clayton358cf1e2015-06-25 21:46:34 +0000600 symbols[i].GetAddressRef() = symbol_addr;
Adrian McCarthyc35b91c2016-01-26 00:58:09 +0000601 symbols[i].SetType(MapSymbolType(symbol.type));
Greg Clayton0076e712013-06-18 00:08:58 +0000602 }
Greg Claytona1743492012-03-13 23:14:29 +0000603
604 if (symbol.naux > 0)
Greg Clayton0076e712013-06-18 00:08:58 +0000605 {
Greg Claytona1743492012-03-13 23:14:29 +0000606 i += symbol.naux;
Greg Clayton0076e712013-06-18 00:08:58 +0000607 offset += symbol_size;
608 }
Greg Claytona1743492012-03-13 23:14:29 +0000609 }
610
611 }
Virgile Belloa4fe3a12013-08-25 13:27:20 +0000612
613 // Read export header
614 if (coff_data_dir_export_table < m_coff_header_opt.data_dirs.size()
615 && 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)
616 {
617 export_directory_entry export_table;
618 uint32_t data_start = m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmaddr;
619 Address address(m_coff_header_opt.image_base + data_start, sect_list);
620 DataBufferSP symtab_data_sp(m_file.ReadFileContents(address.GetSection()->GetFileOffset() + address.GetOffset(), m_coff_header_opt.data_dirs[0].vmsize));
621 DataExtractor symtab_data (symtab_data_sp, GetByteOrder(), GetAddressByteSize());
622 lldb::offset_t offset = 0;
623
624 // Read export_table header
625 export_table.characteristics = symtab_data.GetU32(&offset);
626 export_table.time_date_stamp = symtab_data.GetU32(&offset);
627 export_table.major_version = symtab_data.GetU16(&offset);
628 export_table.minor_version = symtab_data.GetU16(&offset);
629 export_table.name = symtab_data.GetU32(&offset);
630 export_table.base = symtab_data.GetU32(&offset);
631 export_table.number_of_functions = symtab_data.GetU32(&offset);
632 export_table.number_of_names = symtab_data.GetU32(&offset);
633 export_table.address_of_functions = symtab_data.GetU32(&offset);
634 export_table.address_of_names = symtab_data.GetU32(&offset);
635 export_table.address_of_name_ordinals = symtab_data.GetU32(&offset);
636
637 bool has_ordinal = export_table.address_of_name_ordinals != 0;
638
639 lldb::offset_t name_offset = export_table.address_of_names - data_start;
640 lldb::offset_t name_ordinal_offset = export_table.address_of_name_ordinals - data_start;
641
642 Symbol *symbols = m_symtab_ap->Resize(export_table.number_of_names);
643
644 std::string symbol_name;
645
646 // Read each export table entry
647 for (size_t i = 0; i < export_table.number_of_names; ++i)
648 {
649 uint32_t name_ordinal = has_ordinal ? symtab_data.GetU16(&name_ordinal_offset) : i;
650 uint32_t name_address = symtab_data.GetU32(&name_offset);
651
652 const char* symbol_name_cstr = symtab_data.PeekCStr(name_address - data_start);
653 symbol_name.assign(symbol_name_cstr);
654
655 lldb::offset_t function_offset = export_table.address_of_functions - data_start + sizeof(uint32_t) * name_ordinal;
656 uint32_t function_rva = symtab_data.GetU32(&function_offset);
657
658 Address symbol_addr(m_coff_header_opt.image_base + function_rva, sect_list);
659 symbols[i].GetMangled().SetValue(ConstString(symbol_name.c_str()));
Greg Clayton358cf1e2015-06-25 21:46:34 +0000660 symbols[i].GetAddressRef() = symbol_addr;
Virgile Belloa4fe3a12013-08-25 13:27:20 +0000661 symbols[i].SetType(lldb::eSymbolTypeCode);
662 symbols[i].SetDebug(true);
663 }
664 }
Adrian McCarthy225d3ea2016-01-21 20:58:12 +0000665 m_symtab_ap->CalculateSymbolSizes();
Greg Claytonf754f882011-09-09 20:33:05 +0000666 }
667 }
668 return m_symtab_ap.get();
669
670}
671
Greg Clayton3046e662013-07-10 01:23:25 +0000672bool
673ObjectFilePECOFF::IsStripped ()
Greg Claytonf754f882011-09-09 20:33:05 +0000674{
Greg Clayton3046e662013-07-10 01:23:25 +0000675 // TODO: determine this for COFF
676 return false;
677}
678
679
680
681void
682ObjectFilePECOFF::CreateSections (SectionList &unified_section_list)
683{
684 if (!m_sections_ap.get())
Greg Claytonf754f882011-09-09 20:33:05 +0000685 {
Greg Clayton3046e662013-07-10 01:23:25 +0000686 m_sections_ap.reset(new SectionList());
687
688 ModuleSP module_sp(GetModule());
689 if (module_sp)
Greg Claytonf754f882011-09-09 20:33:05 +0000690 {
Greg Clayton3046e662013-07-10 01:23:25 +0000691 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
Greg Claytona1743492012-03-13 23:14:29 +0000692 const uint32_t nsects = m_sect_headers.size();
693 ModuleSP module_sp (GetModule());
694 for (uint32_t idx = 0; idx<nsects; ++idx)
Greg Clayton28469ca2011-09-10 01:04:42 +0000695 {
Greg Claytona1743492012-03-13 23:14:29 +0000696 std::string sect_name;
697 GetSectionName (sect_name, m_sect_headers[idx]);
698 ConstString const_sect_name (sect_name.c_str());
699 static ConstString g_code_sect_name (".code");
700 static ConstString g_CODE_sect_name ("CODE");
701 static ConstString g_data_sect_name (".data");
702 static ConstString g_DATA_sect_name ("DATA");
703 static ConstString g_bss_sect_name (".bss");
704 static ConstString g_BSS_sect_name ("BSS");
705 static ConstString g_debug_sect_name (".debug");
706 static ConstString g_reloc_sect_name (".reloc");
707 static ConstString g_stab_sect_name (".stab");
708 static ConstString g_stabstr_sect_name (".stabstr");
Greg Clayton0076e712013-06-18 00:08:58 +0000709 static ConstString g_sect_name_dwarf_debug_abbrev (".debug_abbrev");
710 static ConstString g_sect_name_dwarf_debug_aranges (".debug_aranges");
711 static ConstString g_sect_name_dwarf_debug_frame (".debug_frame");
712 static ConstString g_sect_name_dwarf_debug_info (".debug_info");
713 static ConstString g_sect_name_dwarf_debug_line (".debug_line");
714 static ConstString g_sect_name_dwarf_debug_loc (".debug_loc");
715 static ConstString g_sect_name_dwarf_debug_macinfo (".debug_macinfo");
716 static ConstString g_sect_name_dwarf_debug_pubnames (".debug_pubnames");
717 static ConstString g_sect_name_dwarf_debug_pubtypes (".debug_pubtypes");
718 static ConstString g_sect_name_dwarf_debug_ranges (".debug_ranges");
719 static ConstString g_sect_name_dwarf_debug_str (".debug_str");
720 static ConstString g_sect_name_eh_frame (".eh_frame");
Ryan Brown65d4d5c2015-09-16 21:20:44 +0000721 static ConstString g_sect_name_go_symtab (".gosymtab");
Greg Claytona1743492012-03-13 23:14:29 +0000722 SectionType section_type = eSectionTypeOther;
Charles Davis237ad972013-08-27 05:04:33 +0000723 if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_CODE &&
Greg Claytona1743492012-03-13 23:14:29 +0000724 ((const_sect_name == g_code_sect_name) || (const_sect_name == g_CODE_sect_name)))
725 {
726 section_type = eSectionTypeCode;
727 }
Charles Davis237ad972013-08-27 05:04:33 +0000728 else if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_INITIALIZED_DATA &&
Greg Claytona1743492012-03-13 23:14:29 +0000729 ((const_sect_name == g_data_sect_name) || (const_sect_name == g_DATA_sect_name)))
730 {
Greg Clayton28469ca2011-09-10 01:04:42 +0000731 section_type = eSectionTypeData;
Greg Claytona1743492012-03-13 23:14:29 +0000732 }
Charles Davis237ad972013-08-27 05:04:33 +0000733 else if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA &&
Greg Claytona1743492012-03-13 23:14:29 +0000734 ((const_sect_name == g_bss_sect_name) || (const_sect_name == g_BSS_sect_name)))
735 {
736 if (m_sect_headers[idx].size == 0)
737 section_type = eSectionTypeZeroFill;
738 else
739 section_type = eSectionTypeData;
740 }
741 else if (const_sect_name == g_debug_sect_name)
742 {
743 section_type = eSectionTypeDebug;
744 }
745 else if (const_sect_name == g_stabstr_sect_name)
746 {
747 section_type = eSectionTypeDataCString;
748 }
749 else if (const_sect_name == g_reloc_sect_name)
750 {
751 section_type = eSectionTypeOther;
752 }
Greg Clayton0076e712013-06-18 00:08:58 +0000753 else if (const_sect_name == g_sect_name_dwarf_debug_abbrev) section_type = eSectionTypeDWARFDebugAbbrev;
754 else if (const_sect_name == g_sect_name_dwarf_debug_aranges) section_type = eSectionTypeDWARFDebugAranges;
755 else if (const_sect_name == g_sect_name_dwarf_debug_frame) section_type = eSectionTypeDWARFDebugFrame;
756 else if (const_sect_name == g_sect_name_dwarf_debug_info) section_type = eSectionTypeDWARFDebugInfo;
757 else if (const_sect_name == g_sect_name_dwarf_debug_line) section_type = eSectionTypeDWARFDebugLine;
758 else if (const_sect_name == g_sect_name_dwarf_debug_loc) section_type = eSectionTypeDWARFDebugLoc;
759 else if (const_sect_name == g_sect_name_dwarf_debug_macinfo) section_type = eSectionTypeDWARFDebugMacInfo;
760 else if (const_sect_name == g_sect_name_dwarf_debug_pubnames) section_type = eSectionTypeDWARFDebugPubNames;
761 else if (const_sect_name == g_sect_name_dwarf_debug_pubtypes) section_type = eSectionTypeDWARFDebugPubTypes;
762 else if (const_sect_name == g_sect_name_dwarf_debug_ranges) section_type = eSectionTypeDWARFDebugRanges;
763 else if (const_sect_name == g_sect_name_dwarf_debug_str) section_type = eSectionTypeDWARFDebugStr;
764 else if (const_sect_name == g_sect_name_eh_frame) section_type = eSectionTypeEHFrame;
Ryan Brown65d4d5c2015-09-16 21:20:44 +0000765 else if (const_sect_name == g_sect_name_go_symtab) section_type = eSectionTypeGoSymtab;
Charles Davis237ad972013-08-27 05:04:33 +0000766 else if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_CODE)
Greg Claytona1743492012-03-13 23:14:29 +0000767 {
768 section_type = eSectionTypeCode;
769 }
Charles Davis237ad972013-08-27 05:04:33 +0000770 else if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
Greg Claytona1743492012-03-13 23:14:29 +0000771 {
Greg Clayton28469ca2011-09-10 01:04:42 +0000772 section_type = eSectionTypeData;
Greg Claytona1743492012-03-13 23:14:29 +0000773 }
Charles Davis237ad972013-08-27 05:04:33 +0000774 else if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
Greg Claytona1743492012-03-13 23:14:29 +0000775 {
776 if (m_sect_headers[idx].size == 0)
777 section_type = eSectionTypeZeroFill;
778 else
779 section_type = eSectionTypeData;
780 }
781
782 // Use a segment ID of the segment index shifted left by 8 so they
783 // never conflict with any of the sections.
784 SectionSP section_sp (new Section (module_sp, // Module to which this section belongs
Michael Sartaina7499c92013-07-01 19:45:50 +0000785 this, // Object file to which this section belongs
Greg Claytona1743492012-03-13 23:14:29 +0000786 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
787 const_sect_name, // Name of this section
Michael Sartaina7499c92013-07-01 19:45:50 +0000788 section_type, // This section is a container of other sections.
Greg Clayton0076e712013-06-18 00:08:58 +0000789 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 +0000790 m_sect_headers[idx].vmsize, // VM size in bytes of this section
791 m_sect_headers[idx].offset, // Offset to the data for this section in the file
Bruce Mitcheneraaa0ba32014-07-08 18:05:41 +0000792 m_sect_headers[idx].size, // Size in bytes of this section as found in the file
Greg Clayton48672af2014-06-24 22:22:43 +0000793 m_coff_header_opt.sect_alignment, // Section alignment
Greg Claytona1743492012-03-13 23:14:29 +0000794 m_sect_headers[idx].flags)); // Flags for this section
795
796 //section_sp->SetIsEncrypted (segment_is_encrypted);
797
Greg Clayton3046e662013-07-10 01:23:25 +0000798 unified_section_list.AddSection(section_sp);
799 m_sections_ap->AddSection (section_sp);
Greg Clayton28469ca2011-09-10 01:04:42 +0000800 }
Greg Claytonf754f882011-09-09 20:33:05 +0000801 }
802 }
Greg Claytonf754f882011-09-09 20:33:05 +0000803}
804
805bool
806ObjectFilePECOFF::GetUUID (UUID* uuid)
807{
808 return false;
809}
810
811uint32_t
812ObjectFilePECOFF::GetDependentModules (FileSpecList& files)
813{
814 return 0;
815}
816
817
818//----------------------------------------------------------------------
819// Dump
820//
821// Dump the specifics of the runtime file container (such as any headers
822// segments, sections, etc).
823//----------------------------------------------------------------------
824void
825ObjectFilePECOFF::Dump(Stream *s)
826{
Greg Claytona1743492012-03-13 23:14:29 +0000827 ModuleSP module_sp(GetModule());
828 if (module_sp)
Greg Claytonf754f882011-09-09 20:33:05 +0000829 {
Greg Claytona1743492012-03-13 23:14:29 +0000830 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000831 s->Printf("%p: ", static_cast<void*>(this));
Greg Claytona1743492012-03-13 23:14:29 +0000832 s->Indent();
833 s->PutCString("ObjectFilePECOFF");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000834
Greg Claytona1743492012-03-13 23:14:29 +0000835 ArchSpec header_arch;
836 GetArchitecture (header_arch);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000837
Greg Claytona1743492012-03-13 23:14:29 +0000838 *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000839
Greg Clayton3046e662013-07-10 01:23:25 +0000840 SectionList *sections = GetSectionList();
841 if (sections)
842 sections->Dump(s, NULL, true, UINT32_MAX);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000843
Greg Claytona1743492012-03-13 23:14:29 +0000844 if (m_symtab_ap.get())
845 m_symtab_ap->Dump(s, NULL, eSortOrderNone);
846
847 if (m_dos_header.e_magic)
848 DumpDOSHeader (s, m_dos_header);
849 if (m_coff_header.machine)
850 {
851 DumpCOFFHeader (s, m_coff_header);
852 if (m_coff_header.hdrsize)
853 DumpOptCOFFHeader (s, m_coff_header_opt);
854 }
855 s->EOL();
856 DumpSectionHeaders(s);
857 s->EOL();
Greg Claytonf754f882011-09-09 20:33:05 +0000858 }
Greg Claytonf754f882011-09-09 20:33:05 +0000859}
860
861//----------------------------------------------------------------------
862// DumpDOSHeader
863//
864// Dump the MS-DOS header to the specified output stream
865//----------------------------------------------------------------------
866void
867ObjectFilePECOFF::DumpDOSHeader(Stream *s, const dos_header_t& header)
868{
869 s->PutCString ("MSDOS Header\n");
870 s->Printf (" e_magic = 0x%4.4x\n", header.e_magic);
871 s->Printf (" e_cblp = 0x%4.4x\n", header.e_cblp);
872 s->Printf (" e_cp = 0x%4.4x\n", header.e_cp);
873 s->Printf (" e_crlc = 0x%4.4x\n", header.e_crlc);
874 s->Printf (" e_cparhdr = 0x%4.4x\n", header.e_cparhdr);
875 s->Printf (" e_minalloc = 0x%4.4x\n", header.e_minalloc);
876 s->Printf (" e_maxalloc = 0x%4.4x\n", header.e_maxalloc);
877 s->Printf (" e_ss = 0x%4.4x\n", header.e_ss);
878 s->Printf (" e_sp = 0x%4.4x\n", header.e_sp);
879 s->Printf (" e_csum = 0x%4.4x\n", header.e_csum);
880 s->Printf (" e_ip = 0x%4.4x\n", header.e_ip);
881 s->Printf (" e_cs = 0x%4.4x\n", header.e_cs);
882 s->Printf (" e_lfarlc = 0x%4.4x\n", header.e_lfarlc);
883 s->Printf (" e_ovno = 0x%4.4x\n", header.e_ovno);
884 s->Printf (" e_res[4] = { 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x }\n",
885 header.e_res[0],
886 header.e_res[1],
887 header.e_res[2],
888 header.e_res[3]);
889 s->Printf (" e_oemid = 0x%4.4x\n", header.e_oemid);
890 s->Printf (" e_oeminfo = 0x%4.4x\n", header.e_oeminfo);
891 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",
892 header.e_res2[0],
893 header.e_res2[1],
894 header.e_res2[2],
895 header.e_res2[3],
896 header.e_res2[4],
897 header.e_res2[5],
898 header.e_res2[6],
899 header.e_res2[7],
900 header.e_res2[8],
901 header.e_res2[9]);
902 s->Printf (" e_lfanew = 0x%8.8x\n", header.e_lfanew);
903}
904
905//----------------------------------------------------------------------
906// DumpCOFFHeader
907//
908// Dump the COFF header to the specified output stream
909//----------------------------------------------------------------------
910void
911ObjectFilePECOFF::DumpCOFFHeader(Stream *s, const coff_header_t& header)
912{
913 s->PutCString ("COFF Header\n");
914 s->Printf (" machine = 0x%4.4x\n", header.machine);
915 s->Printf (" nsects = 0x%4.4x\n", header.nsects);
916 s->Printf (" modtime = 0x%8.8x\n", header.modtime);
917 s->Printf (" symoff = 0x%8.8x\n", header.symoff);
918 s->Printf (" nsyms = 0x%8.8x\n", header.nsyms);
919 s->Printf (" hdrsize = 0x%4.4x\n", header.hdrsize);
920}
921
922//----------------------------------------------------------------------
923// DumpOptCOFFHeader
924//
925// Dump the optional COFF header to the specified output stream
926//----------------------------------------------------------------------
927void
928ObjectFilePECOFF::DumpOptCOFFHeader(Stream *s, const coff_opt_header_t& header)
929{
930 s->PutCString ("Optional COFF Header\n");
931 s->Printf (" magic = 0x%4.4x\n", header.magic);
932 s->Printf (" major_linker_version = 0x%2.2x\n", header.major_linker_version);
933 s->Printf (" minor_linker_version = 0x%2.2x\n", header.minor_linker_version);
934 s->Printf (" code_size = 0x%8.8x\n", header.code_size);
935 s->Printf (" data_size = 0x%8.8x\n", header.data_size);
936 s->Printf (" bss_size = 0x%8.8x\n", header.bss_size);
937 s->Printf (" entry = 0x%8.8x\n", header.entry);
938 s->Printf (" code_offset = 0x%8.8x\n", header.code_offset);
939 s->Printf (" data_offset = 0x%8.8x\n", header.data_offset);
Daniel Malead01b2952012-11-29 21:49:15 +0000940 s->Printf (" image_base = 0x%16.16" PRIx64 "\n", header.image_base);
Greg Claytonf754f882011-09-09 20:33:05 +0000941 s->Printf (" sect_alignment = 0x%8.8x\n", header.sect_alignment);
942 s->Printf (" file_alignment = 0x%8.8x\n", header.file_alignment);
943 s->Printf (" major_os_system_version = 0x%4.4x\n", header.major_os_system_version);
944 s->Printf (" minor_os_system_version = 0x%4.4x\n", header.minor_os_system_version);
945 s->Printf (" major_image_version = 0x%4.4x\n", header.major_image_version);
946 s->Printf (" minor_image_version = 0x%4.4x\n", header.minor_image_version);
947 s->Printf (" major_subsystem_version = 0x%4.4x\n", header.major_subsystem_version);
948 s->Printf (" minor_subsystem_version = 0x%4.4x\n", header.minor_subsystem_version);
949 s->Printf (" reserved1 = 0x%8.8x\n", header.reserved1);
950 s->Printf (" image_size = 0x%8.8x\n", header.image_size);
951 s->Printf (" header_size = 0x%8.8x\n", header.header_size);
Greg Clayton28469ca2011-09-10 01:04:42 +0000952 s->Printf (" checksum = 0x%8.8x\n", header.checksum);
Greg Claytonf754f882011-09-09 20:33:05 +0000953 s->Printf (" subsystem = 0x%4.4x\n", header.subsystem);
954 s->Printf (" dll_flags = 0x%4.4x\n", header.dll_flags);
Daniel Malead01b2952012-11-29 21:49:15 +0000955 s->Printf (" stack_reserve_size = 0x%16.16" PRIx64 "\n", header.stack_reserve_size);
956 s->Printf (" stack_commit_size = 0x%16.16" PRIx64 "\n", header.stack_commit_size);
957 s->Printf (" heap_reserve_size = 0x%16.16" PRIx64 "\n", header.heap_reserve_size);
958 s->Printf (" heap_commit_size = 0x%16.16" PRIx64 "\n", header.heap_commit_size);
Greg Claytonf754f882011-09-09 20:33:05 +0000959 s->Printf (" loader_flags = 0x%8.8x\n", header.loader_flags);
Virgile Belloffeba252014-03-08 17:15:35 +0000960 s->Printf (" num_data_dir_entries = 0x%8.8x\n", (uint32_t)header.data_dirs.size());
Greg Claytonf754f882011-09-09 20:33:05 +0000961 uint32_t i;
962 for (i=0; i<header.data_dirs.size(); i++)
963 {
Greg Clayton28469ca2011-09-10 01:04:42 +0000964 s->Printf (" data_dirs[%2u] vmaddr = 0x%8.8x, vmsize = 0x%8.8x\n",
Greg Claytonf754f882011-09-09 20:33:05 +0000965 i,
966 header.data_dirs[i].vmaddr,
967 header.data_dirs[i].vmsize);
968 }
969}
970//----------------------------------------------------------------------
971// DumpSectionHeader
972//
973// Dump a single ELF section header to the specified output stream
974//----------------------------------------------------------------------
975void
976ObjectFilePECOFF::DumpSectionHeader(Stream *s, const section_header_t& sh)
977{
978 std::string name;
979 GetSectionName(name, sh);
980 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",
981 name.c_str(),
Greg Claytonf754f882011-09-09 20:33:05 +0000982 sh.vmaddr,
Greg Clayton28469ca2011-09-10 01:04:42 +0000983 sh.vmsize,
Greg Claytonf754f882011-09-09 20:33:05 +0000984 sh.offset,
Greg Clayton28469ca2011-09-10 01:04:42 +0000985 sh.size,
Greg Claytonf754f882011-09-09 20:33:05 +0000986 sh.reloff,
987 sh.lineoff,
988 sh.nreloc,
989 sh.nline,
990 sh.flags);
991}
992
993
994//----------------------------------------------------------------------
995// DumpSectionHeaders
996//
997// Dump all of the ELF section header to the specified output stream
998//----------------------------------------------------------------------
999void
1000ObjectFilePECOFF::DumpSectionHeaders(Stream *s)
1001{
1002
1003 s->PutCString ("Section Headers\n");
Greg Clayton28469ca2011-09-10 01:04:42 +00001004 s->PutCString ("IDX name vm addr vm size file off file size reloc off line off nreloc nline flags\n");
1005 s->PutCString ("==== ---------------- ---------- ---------- ---------- ---------- ---------- ---------- ------ ------ ----------\n");
Greg Claytonf754f882011-09-09 20:33:05 +00001006
1007 uint32_t idx = 0;
1008 SectionHeaderCollIter pos, end = m_sect_headers.end();
1009
1010 for (pos = m_sect_headers.begin(); pos != end; ++pos, ++idx)
1011 {
Greg Clayton28469ca2011-09-10 01:04:42 +00001012 s->Printf ("[%2u] ", idx);
Greg Claytonf754f882011-09-09 20:33:05 +00001013 ObjectFilePECOFF::DumpSectionHeader(s, *pos);
1014 }
1015}
1016
Greg Claytonf754f882011-09-09 20:33:05 +00001017bool
1018ObjectFilePECOFF::GetArchitecture (ArchSpec &arch)
1019{
Charles Davis237ad972013-08-27 05:04:33 +00001020 uint16_t machine = m_coff_header.machine;
1021 switch (machine)
1022 {
1023 case llvm::COFF::IMAGE_FILE_MACHINE_AMD64:
1024 case llvm::COFF::IMAGE_FILE_MACHINE_I386:
1025 case llvm::COFF::IMAGE_FILE_MACHINE_POWERPC:
1026 case llvm::COFF::IMAGE_FILE_MACHINE_POWERPCFP:
1027 case llvm::COFF::IMAGE_FILE_MACHINE_ARM:
Saleem Abdulrasool1108cb32014-03-11 03:09:08 +00001028 case llvm::COFF::IMAGE_FILE_MACHINE_ARMNT:
Charles Davis237ad972013-08-27 05:04:33 +00001029 case llvm::COFF::IMAGE_FILE_MACHINE_THUMB:
Colin Riley6c970422013-11-22 09:35:12 +00001030 arch.SetArchitecture (eArchTypeCOFF, machine, LLDB_INVALID_CPUTYPE);
Charles Davis237ad972013-08-27 05:04:33 +00001031 return true;
1032 default:
1033 break;
1034 }
1035 return false;
Greg Claytonf754f882011-09-09 20:33:05 +00001036}
1037
1038ObjectFile::Type
1039ObjectFilePECOFF::CalculateType()
1040{
1041 if (m_coff_header.machine != 0)
1042 {
Charles Davis237ad972013-08-27 05:04:33 +00001043 if ((m_coff_header.flags & llvm::COFF::IMAGE_FILE_DLL) == 0)
Greg Claytonf754f882011-09-09 20:33:05 +00001044 return eTypeExecutable;
1045 else
1046 return eTypeSharedLibrary;
1047 }
1048 return eTypeExecutable;
1049}
1050
1051ObjectFile::Strata
1052ObjectFilePECOFF::CalculateStrata()
1053{
1054 return eStrataUser;
1055}
1056//------------------------------------------------------------------
1057// PluginInterface protocol
1058//------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +00001059ConstString
Greg Claytonf754f882011-09-09 20:33:05 +00001060ObjectFilePECOFF::GetPluginName()
1061{
Greg Claytonf754f882011-09-09 20:33:05 +00001062 return GetPluginNameStatic();
1063}
1064
1065uint32_t
1066ObjectFilePECOFF::GetPluginVersion()
1067{
1068 return 1;
1069}
1070