blob: ab2be36f415eb91051eb64aa950235ba0111b2fc [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
172
Greg Claytone72dfb32012-02-24 01:59:29 +0000173ObjectFilePECOFF::ObjectFilePECOFF (const lldb::ModuleSP &module_sp,
Greg Clayton5ce9c562013-02-06 17:22:03 +0000174 DataBufferSP& data_sp,
175 lldb::offset_t data_offset,
Greg Claytonf754f882011-09-09 20:33:05 +0000176 const FileSpec* file,
Greg Clayton5ce9c562013-02-06 17:22:03 +0000177 lldb::offset_t file_offset,
178 lldb::offset_t length) :
179 ObjectFile (module_sp, file, file_offset, length, data_sp, data_offset),
Greg Claytonf754f882011-09-09 20:33:05 +0000180 m_dos_header (),
181 m_coff_header (),
182 m_coff_header_opt (),
183 m_sect_headers ()
184{
185 ::memset (&m_dos_header, 0, sizeof(m_dos_header));
186 ::memset (&m_coff_header, 0, sizeof(m_coff_header));
187 ::memset (&m_coff_header_opt, 0, sizeof(m_coff_header_opt));
188}
189
190
191ObjectFilePECOFF::~ObjectFilePECOFF()
192{
193}
194
195
196bool
197ObjectFilePECOFF::ParseHeader ()
198{
Greg Claytona1743492012-03-13 23:14:29 +0000199 ModuleSP module_sp(GetModule());
200 if (module_sp)
Greg Claytonf754f882011-09-09 20:33:05 +0000201 {
Greg Claytona1743492012-03-13 23:14:29 +0000202 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
203 m_sect_headers.clear();
204 m_data.SetByteOrder (eByteOrderLittle);
Greg Claytonc7bece562013-01-25 18:06:21 +0000205 lldb::offset_t offset = 0;
Greg Claytona1743492012-03-13 23:14:29 +0000206
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000207 if (ParseDOSHeader(m_data, m_dos_header))
Greg Clayton28469ca2011-09-10 01:04:42 +0000208 {
Greg Claytona1743492012-03-13 23:14:29 +0000209 offset = m_dos_header.e_lfanew;
210 uint32_t pe_signature = m_data.GetU32 (&offset);
211 if (pe_signature != IMAGE_NT_SIGNATURE)
212 return false;
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000213 if (ParseCOFFHeader(m_data, &offset, m_coff_header))
Greg Claytona1743492012-03-13 23:14:29 +0000214 {
215 if (m_coff_header.hdrsize > 0)
216 ParseCOFFOptionalHeader(&offset);
217 ParseSectionHeaders (offset);
218 }
Greg Claytona1743492012-03-13 23:14:29 +0000219 return true;
Greg Clayton28469ca2011-09-10 01:04:42 +0000220 }
Greg Claytonf754f882011-09-09 20:33:05 +0000221 }
222 return false;
223}
224
Virgile Bello2756adf2014-03-08 17:17:20 +0000225bool
226ObjectFilePECOFF::SetLoadAddress(Target &target, addr_t value, bool value_is_offset)
227{
228 bool changed = false;
229 ModuleSP module_sp = GetModule();
230 if (module_sp)
231 {
232 size_t num_loaded_sections = 0;
233 SectionList *section_list = GetSectionList ();
234 if (section_list)
235 {
236 if (!value_is_offset)
237 {
238 value -= m_image_base;
239 }
240
241 const size_t num_sections = section_list->GetSize();
242 size_t sect_idx = 0;
243
244 for (sect_idx = 0; sect_idx < num_sections; ++sect_idx)
245 {
246 // Iterate through the object file sections to find all
247 // of the sections that have SHF_ALLOC in their flag bits.
248 SectionSP section_sp (section_list->GetSectionAtIndex (sect_idx));
249 if (section_sp && !section_sp->IsThreadSpecific())
250 {
251 if (target.GetSectionLoadList().SetSectionLoadAddress (section_sp, section_sp->GetFileAddress() + value))
252 ++num_loaded_sections;
253 }
254 }
255 changed = num_loaded_sections > 0;
Virgile Bello2756adf2014-03-08 17:17:20 +0000256 }
257 }
258 return changed;
259}
260
Greg Claytonf754f882011-09-09 20:33:05 +0000261
262ByteOrder
263ObjectFilePECOFF::GetByteOrder () const
264{
265 return eByteOrderLittle;
266}
267
268bool
269ObjectFilePECOFF::IsExecutable() const
270{
Charles Davis237ad972013-08-27 05:04:33 +0000271 return (m_coff_header.flags & llvm::COFF::IMAGE_FILE_DLL) == 0;
Greg Claytonf754f882011-09-09 20:33:05 +0000272}
273
Greg Claytonc7bece562013-01-25 18:06:21 +0000274uint32_t
Greg Claytonf754f882011-09-09 20:33:05 +0000275ObjectFilePECOFF::GetAddressByteSize () const
276{
277 if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32_PLUS)
278 return 8;
279 else if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32)
280 return 4;
281 return 4;
282}
283
284//----------------------------------------------------------------------
285// NeedsEndianSwap
286//
287// Return true if an endian swap needs to occur when extracting data
288// from this file.
289//----------------------------------------------------------------------
290bool
291ObjectFilePECOFF::NeedsEndianSwap() const
292{
293#if defined(__LITTLE_ENDIAN__)
294 return false;
295#else
296 return true;
297#endif
298}
299//----------------------------------------------------------------------
300// ParseDOSHeader
301//----------------------------------------------------------------------
302bool
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000303ObjectFilePECOFF::ParseDOSHeader (DataExtractor &data, dos_header_t &dos_header)
Greg Claytonf754f882011-09-09 20:33:05 +0000304{
305 bool success = false;
Greg Claytonc7bece562013-01-25 18:06:21 +0000306 lldb::offset_t offset = 0;
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000307 success = data.ValidOffsetForDataOfSize(0, sizeof(dos_header));
Greg Claytonf754f882011-09-09 20:33:05 +0000308
309 if (success)
310 {
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000311 dos_header.e_magic = data.GetU16(&offset); // Magic number
312 success = dos_header.e_magic == IMAGE_DOS_SIGNATURE;
Greg Claytonf754f882011-09-09 20:33:05 +0000313
314 if (success)
315 {
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000316 dos_header.e_cblp = data.GetU16(&offset); // Bytes on last page of file
317 dos_header.e_cp = data.GetU16(&offset); // Pages in file
318 dos_header.e_crlc = data.GetU16(&offset); // Relocations
319 dos_header.e_cparhdr = data.GetU16(&offset); // Size of header in paragraphs
320 dos_header.e_minalloc = data.GetU16(&offset); // Minimum extra paragraphs needed
321 dos_header.e_maxalloc = data.GetU16(&offset); // Maximum extra paragraphs needed
322 dos_header.e_ss = data.GetU16(&offset); // Initial (relative) SS value
323 dos_header.e_sp = data.GetU16(&offset); // Initial SP value
324 dos_header.e_csum = data.GetU16(&offset); // Checksum
325 dos_header.e_ip = data.GetU16(&offset); // Initial IP value
326 dos_header.e_cs = data.GetU16(&offset); // Initial (relative) CS value
327 dos_header.e_lfarlc = data.GetU16(&offset); // File address of relocation table
328 dos_header.e_ovno = data.GetU16(&offset); // Overlay number
Greg Claytonf754f882011-09-09 20:33:05 +0000329
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000330 dos_header.e_res[0] = data.GetU16(&offset); // Reserved words
331 dos_header.e_res[1] = data.GetU16(&offset); // Reserved words
332 dos_header.e_res[2] = data.GetU16(&offset); // Reserved words
333 dos_header.e_res[3] = data.GetU16(&offset); // Reserved words
Greg Claytonf754f882011-09-09 20:33:05 +0000334
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000335 dos_header.e_oemid = data.GetU16(&offset); // OEM identifier (for e_oeminfo)
336 dos_header.e_oeminfo = data.GetU16(&offset); // OEM information; e_oemid specific
337 dos_header.e_res2[0] = data.GetU16(&offset); // Reserved words
338 dos_header.e_res2[1] = data.GetU16(&offset); // Reserved words
339 dos_header.e_res2[2] = data.GetU16(&offset); // Reserved words
340 dos_header.e_res2[3] = data.GetU16(&offset); // Reserved words
341 dos_header.e_res2[4] = data.GetU16(&offset); // Reserved words
342 dos_header.e_res2[5] = data.GetU16(&offset); // Reserved words
343 dos_header.e_res2[6] = data.GetU16(&offset); // Reserved words
344 dos_header.e_res2[7] = data.GetU16(&offset); // Reserved words
345 dos_header.e_res2[8] = data.GetU16(&offset); // Reserved words
346 dos_header.e_res2[9] = data.GetU16(&offset); // Reserved words
Greg Claytonf754f882011-09-09 20:33:05 +0000347
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000348 dos_header.e_lfanew = data.GetU32(&offset); // File address of new exe header
Greg Claytonf754f882011-09-09 20:33:05 +0000349 }
350 }
351 if (!success)
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000352 memset(&dos_header, 0, sizeof(dos_header));
Greg Claytonf754f882011-09-09 20:33:05 +0000353 return success;
354}
355
356
357//----------------------------------------------------------------------
358// ParserCOFFHeader
359//----------------------------------------------------------------------
360bool
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000361ObjectFilePECOFF::ParseCOFFHeader(DataExtractor &data, lldb::offset_t *offset_ptr, coff_header_t &coff_header)
Greg Claytonf754f882011-09-09 20:33:05 +0000362{
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000363 bool success = data.ValidOffsetForDataOfSize (*offset_ptr, sizeof(coff_header));
Greg Claytonf754f882011-09-09 20:33:05 +0000364 if (success)
365 {
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000366 coff_header.machine = data.GetU16(offset_ptr);
367 coff_header.nsects = data.GetU16(offset_ptr);
368 coff_header.modtime = data.GetU32(offset_ptr);
369 coff_header.symoff = data.GetU32(offset_ptr);
370 coff_header.nsyms = data.GetU32(offset_ptr);
371 coff_header.hdrsize = data.GetU16(offset_ptr);
372 coff_header.flags = data.GetU16(offset_ptr);
Greg Claytonf754f882011-09-09 20:33:05 +0000373 }
374 if (!success)
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000375 memset(&coff_header, 0, sizeof(coff_header));
Greg Claytonf754f882011-09-09 20:33:05 +0000376 return success;
377}
378
379bool
Greg Claytonc7bece562013-01-25 18:06:21 +0000380ObjectFilePECOFF::ParseCOFFOptionalHeader(lldb::offset_t *offset_ptr)
Greg Claytonf754f882011-09-09 20:33:05 +0000381{
382 bool success = false;
Greg Claytonc7bece562013-01-25 18:06:21 +0000383 const lldb::offset_t end_offset = *offset_ptr + m_coff_header.hdrsize;
Greg Claytonf754f882011-09-09 20:33:05 +0000384 if (*offset_ptr < end_offset)
385 {
386 success = true;
387 m_coff_header_opt.magic = m_data.GetU16(offset_ptr);
388 m_coff_header_opt.major_linker_version = m_data.GetU8 (offset_ptr);
389 m_coff_header_opt.minor_linker_version = m_data.GetU8 (offset_ptr);
390 m_coff_header_opt.code_size = m_data.GetU32(offset_ptr);
391 m_coff_header_opt.data_size = m_data.GetU32(offset_ptr);
392 m_coff_header_opt.bss_size = m_data.GetU32(offset_ptr);
393 m_coff_header_opt.entry = m_data.GetU32(offset_ptr);
394 m_coff_header_opt.code_offset = m_data.GetU32(offset_ptr);
395
396 const uint32_t addr_byte_size = GetAddressByteSize ();
397
398 if (*offset_ptr < end_offset)
399 {
400 if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32)
401 {
402 // PE32 only
403 m_coff_header_opt.data_offset = m_data.GetU32(offset_ptr);
404 }
405 else
406 m_coff_header_opt.data_offset = 0;
407
408 if (*offset_ptr < end_offset)
409 {
410 m_coff_header_opt.image_base = m_data.GetMaxU64 (offset_ptr, addr_byte_size);
411 m_coff_header_opt.sect_alignment = m_data.GetU32(offset_ptr);
412 m_coff_header_opt.file_alignment = m_data.GetU32(offset_ptr);
413 m_coff_header_opt.major_os_system_version = m_data.GetU16(offset_ptr);
414 m_coff_header_opt.minor_os_system_version = m_data.GetU16(offset_ptr);
415 m_coff_header_opt.major_image_version = m_data.GetU16(offset_ptr);
416 m_coff_header_opt.minor_image_version = m_data.GetU16(offset_ptr);
417 m_coff_header_opt.major_subsystem_version = m_data.GetU16(offset_ptr);
418 m_coff_header_opt.minor_subsystem_version = m_data.GetU16(offset_ptr);
419 m_coff_header_opt.reserved1 = m_data.GetU32(offset_ptr);
420 m_coff_header_opt.image_size = m_data.GetU32(offset_ptr);
421 m_coff_header_opt.header_size = m_data.GetU32(offset_ptr);
Greg Clayton28469ca2011-09-10 01:04:42 +0000422 m_coff_header_opt.checksum = m_data.GetU32(offset_ptr);
Greg Claytonf754f882011-09-09 20:33:05 +0000423 m_coff_header_opt.subsystem = m_data.GetU16(offset_ptr);
424 m_coff_header_opt.dll_flags = m_data.GetU16(offset_ptr);
425 m_coff_header_opt.stack_reserve_size = m_data.GetMaxU64 (offset_ptr, addr_byte_size);
426 m_coff_header_opt.stack_commit_size = m_data.GetMaxU64 (offset_ptr, addr_byte_size);
427 m_coff_header_opt.heap_reserve_size = m_data.GetMaxU64 (offset_ptr, addr_byte_size);
428 m_coff_header_opt.heap_commit_size = m_data.GetMaxU64 (offset_ptr, addr_byte_size);
429 m_coff_header_opt.loader_flags = m_data.GetU32(offset_ptr);
430 uint32_t num_data_dir_entries = m_data.GetU32(offset_ptr);
431 m_coff_header_opt.data_dirs.clear();
432 m_coff_header_opt.data_dirs.resize(num_data_dir_entries);
433 uint32_t i;
434 for (i=0; i<num_data_dir_entries; i++)
435 {
436 m_coff_header_opt.data_dirs[i].vmaddr = m_data.GetU32(offset_ptr);
437 m_coff_header_opt.data_dirs[i].vmsize = m_data.GetU32(offset_ptr);
438 }
Virgile Bello2756adf2014-03-08 17:17:20 +0000439
440 m_file_offset = m_coff_header_opt.image_base;
441 m_image_base = m_coff_header_opt.image_base;
Greg Claytonf754f882011-09-09 20:33:05 +0000442 }
443 }
444 }
445 // Make sure we are on track for section data which follows
446 *offset_ptr = end_offset;
447 return success;
448}
449
450
451//----------------------------------------------------------------------
452// ParseSectionHeaders
453//----------------------------------------------------------------------
454bool
455ObjectFilePECOFF::ParseSectionHeaders (uint32_t section_header_data_offset)
456{
457 const uint32_t nsects = m_coff_header.nsects;
458 m_sect_headers.clear();
459
460 if (nsects > 0)
461 {
462 const uint32_t addr_byte_size = GetAddressByteSize ();
463 const size_t section_header_byte_size = nsects * sizeof(section_header_t);
464 DataBufferSP section_header_data_sp(m_file.ReadFileContents (section_header_data_offset, section_header_byte_size));
465 DataExtractor section_header_data (section_header_data_sp, GetByteOrder(), addr_byte_size);
466
Greg Claytonc7bece562013-01-25 18:06:21 +0000467 lldb::offset_t offset = 0;
Greg Claytonf754f882011-09-09 20:33:05 +0000468 if (section_header_data.ValidOffsetForDataOfSize (offset, section_header_byte_size))
469 {
470 m_sect_headers.resize(nsects);
471
472 for (uint32_t idx = 0; idx<nsects; ++idx)
473 {
474 const void *name_data = section_header_data.GetData(&offset, 8);
475 if (name_data)
476 {
477 memcpy(m_sect_headers[idx].name, name_data, 8);
478 m_sect_headers[idx].vmsize = section_header_data.GetU32(&offset);
479 m_sect_headers[idx].vmaddr = section_header_data.GetU32(&offset);
480 m_sect_headers[idx].size = section_header_data.GetU32(&offset);
481 m_sect_headers[idx].offset = section_header_data.GetU32(&offset);
482 m_sect_headers[idx].reloff = section_header_data.GetU32(&offset);
483 m_sect_headers[idx].lineoff = section_header_data.GetU32(&offset);
484 m_sect_headers[idx].nreloc = section_header_data.GetU16(&offset);
485 m_sect_headers[idx].nline = section_header_data.GetU16(&offset);
486 m_sect_headers[idx].flags = section_header_data.GetU32(&offset);
487 }
488 }
489 }
490 }
491
492 return m_sect_headers.empty() == false;
493}
494
495bool
496ObjectFilePECOFF::GetSectionName(std::string& sect_name, const section_header_t& sect)
497{
498 if (sect.name[0] == '/')
499 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000500 lldb::offset_t stroff = strtoul(&sect.name[1], NULL, 10);
501 lldb::offset_t string_file_offset = m_coff_header.symoff + (m_coff_header.nsyms * 18) + stroff;
Greg Claytonf754f882011-09-09 20:33:05 +0000502 const char *name = m_data.GetCStr (&string_file_offset);
503 if (name)
504 {
505 sect_name = name;
506 return true;
507 }
508
509 return false;
510 }
511 sect_name = sect.name;
512 return true;
513}
514
515//----------------------------------------------------------------------
516// GetNListSymtab
517//----------------------------------------------------------------------
518Symtab *
Greg Clayton3046e662013-07-10 01:23:25 +0000519ObjectFilePECOFF::GetSymtab()
Greg Claytonf754f882011-09-09 20:33:05 +0000520{
Greg Claytona1743492012-03-13 23:14:29 +0000521 ModuleSP module_sp(GetModule());
522 if (module_sp)
Greg Claytonf754f882011-09-09 20:33:05 +0000523 {
Greg Claytona1743492012-03-13 23:14:29 +0000524 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
525 if (m_symtab_ap.get() == NULL)
Greg Claytonf754f882011-09-09 20:33:05 +0000526 {
Greg Claytona1743492012-03-13 23:14:29 +0000527 SectionList *sect_list = GetSectionList();
528 m_symtab_ap.reset(new Symtab(this));
529 Mutex::Locker symtab_locker (m_symtab_ap->GetMutex());
Greg Claytonf754f882011-09-09 20:33:05 +0000530
Greg Claytona1743492012-03-13 23:14:29 +0000531 const uint32_t num_syms = m_coff_header.nsyms;
532
533 if (num_syms > 0 && m_coff_header.symoff > 0)
534 {
Greg Clayton0076e712013-06-18 00:08:58 +0000535 const uint32_t symbol_size = 18;
Greg Claytona1743492012-03-13 23:14:29 +0000536 const uint32_t addr_byte_size = GetAddressByteSize ();
537 const size_t symbol_data_size = num_syms * symbol_size;
538 // Include the 4 bytes string table size at the end of the symbols
539 DataBufferSP symtab_data_sp(m_file.ReadFileContents (m_coff_header.symoff, symbol_data_size + 4));
540 DataExtractor symtab_data (symtab_data_sp, GetByteOrder(), addr_byte_size);
Greg Claytonc7bece562013-01-25 18:06:21 +0000541 lldb::offset_t offset = symbol_data_size;
Greg Claytona1743492012-03-13 23:14:29 +0000542 const uint32_t strtab_size = symtab_data.GetU32 (&offset);
Greg Clayton0076e712013-06-18 00:08:58 +0000543 DataBufferSP strtab_data_sp(m_file.ReadFileContents (m_coff_header.symoff + symbol_data_size, strtab_size));
Greg Claytona1743492012-03-13 23:14:29 +0000544 DataExtractor strtab_data (strtab_data_sp, GetByteOrder(), addr_byte_size);
545
Greg Clayton0076e712013-06-18 00:08:58 +0000546 // First 4 bytes should be zeroed after strtab_size has been read,
547 // because it is used as offset 0 to encode a NULL string.
548 uint32_t* strtab_data_start = (uint32_t*)strtab_data_sp->GetBytes();
549 strtab_data_start[0] = 0;
550
Greg Claytona1743492012-03-13 23:14:29 +0000551 offset = 0;
552 std::string symbol_name;
553 Symbol *symbols = m_symtab_ap->Resize (num_syms);
554 for (uint32_t i=0; i<num_syms; ++i)
555 {
556 coff_symbol_t symbol;
557 const uint32_t symbol_offset = offset;
558 const char *symbol_name_cstr = NULL;
559 // If the first 4 bytes of the symbol string are zero, then we
560 // it is followed by a 4 byte string table offset. Else these
561 // 8 bytes contain the symbol name
562 if (symtab_data.GetU32 (&offset) == 0)
563 {
564 // Long string that doesn't fit into the symbol table name,
565 // so now we must read the 4 byte string table offset
566 uint32_t strtab_offset = symtab_data.GetU32 (&offset);
567 symbol_name_cstr = strtab_data.PeekCStr (strtab_offset);
568 symbol_name.assign (symbol_name_cstr);
569 }
570 else
571 {
572 // Short string that fits into the symbol table name which is 8 bytes
573 offset += sizeof(symbol.name) - 4; // Skip remaining
574 symbol_name_cstr = symtab_data.PeekCStr (symbol_offset);
575 if (symbol_name_cstr == NULL)
576 break;
577 symbol_name.assign (symbol_name_cstr, sizeof(symbol.name));
578 }
579 symbol.value = symtab_data.GetU32 (&offset);
580 symbol.sect = symtab_data.GetU16 (&offset);
581 symbol.type = symtab_data.GetU16 (&offset);
582 symbol.storage = symtab_data.GetU8 (&offset);
583 symbol.naux = symtab_data.GetU8 (&offset);
Greg Clayton037520e2012-07-18 23:18:10 +0000584 symbols[i].GetMangled ().SetValue (ConstString(symbol_name.c_str()));
Greg Clayton0076e712013-06-18 00:08:58 +0000585 if ((int16_t)symbol.sect >= 1)
586 {
587 Address symbol_addr(sect_list->GetSectionAtIndex(symbol.sect-1), symbol.value);
Greg Clayton358cf1e2015-06-25 21:46:34 +0000588 symbols[i].GetAddressRef() = symbol_addr;
Greg Clayton0076e712013-06-18 00:08:58 +0000589 }
Greg Claytona1743492012-03-13 23:14:29 +0000590
591 if (symbol.naux > 0)
Greg Clayton0076e712013-06-18 00:08:58 +0000592 {
Greg Claytona1743492012-03-13 23:14:29 +0000593 i += symbol.naux;
Greg Clayton0076e712013-06-18 00:08:58 +0000594 offset += symbol_size;
595 }
Greg Claytona1743492012-03-13 23:14:29 +0000596 }
597
598 }
Virgile Belloa4fe3a12013-08-25 13:27:20 +0000599
600 // Read export header
601 if (coff_data_dir_export_table < m_coff_header_opt.data_dirs.size()
602 && 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)
603 {
604 export_directory_entry export_table;
605 uint32_t data_start = m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmaddr;
606 Address address(m_coff_header_opt.image_base + data_start, sect_list);
607 DataBufferSP symtab_data_sp(m_file.ReadFileContents(address.GetSection()->GetFileOffset() + address.GetOffset(), m_coff_header_opt.data_dirs[0].vmsize));
608 DataExtractor symtab_data (symtab_data_sp, GetByteOrder(), GetAddressByteSize());
609 lldb::offset_t offset = 0;
610
611 // Read export_table header
612 export_table.characteristics = symtab_data.GetU32(&offset);
613 export_table.time_date_stamp = symtab_data.GetU32(&offset);
614 export_table.major_version = symtab_data.GetU16(&offset);
615 export_table.minor_version = symtab_data.GetU16(&offset);
616 export_table.name = symtab_data.GetU32(&offset);
617 export_table.base = symtab_data.GetU32(&offset);
618 export_table.number_of_functions = symtab_data.GetU32(&offset);
619 export_table.number_of_names = symtab_data.GetU32(&offset);
620 export_table.address_of_functions = symtab_data.GetU32(&offset);
621 export_table.address_of_names = symtab_data.GetU32(&offset);
622 export_table.address_of_name_ordinals = symtab_data.GetU32(&offset);
623
624 bool has_ordinal = export_table.address_of_name_ordinals != 0;
625
626 lldb::offset_t name_offset = export_table.address_of_names - data_start;
627 lldb::offset_t name_ordinal_offset = export_table.address_of_name_ordinals - data_start;
628
629 Symbol *symbols = m_symtab_ap->Resize(export_table.number_of_names);
630
631 std::string symbol_name;
632
633 // Read each export table entry
634 for (size_t i = 0; i < export_table.number_of_names; ++i)
635 {
636 uint32_t name_ordinal = has_ordinal ? symtab_data.GetU16(&name_ordinal_offset) : i;
637 uint32_t name_address = symtab_data.GetU32(&name_offset);
638
639 const char* symbol_name_cstr = symtab_data.PeekCStr(name_address - data_start);
640 symbol_name.assign(symbol_name_cstr);
641
642 lldb::offset_t function_offset = export_table.address_of_functions - data_start + sizeof(uint32_t) * name_ordinal;
643 uint32_t function_rva = symtab_data.GetU32(&function_offset);
644
645 Address symbol_addr(m_coff_header_opt.image_base + function_rva, sect_list);
646 symbols[i].GetMangled().SetValue(ConstString(symbol_name.c_str()));
Greg Clayton358cf1e2015-06-25 21:46:34 +0000647 symbols[i].GetAddressRef() = symbol_addr;
Virgile Belloa4fe3a12013-08-25 13:27:20 +0000648 symbols[i].SetType(lldb::eSymbolTypeCode);
649 symbols[i].SetDebug(true);
650 }
651 }
Adrian McCarthy225d3ea2016-01-21 20:58:12 +0000652 m_symtab_ap->CalculateSymbolSizes();
Greg Claytonf754f882011-09-09 20:33:05 +0000653 }
654 }
655 return m_symtab_ap.get();
656
657}
658
Greg Clayton3046e662013-07-10 01:23:25 +0000659bool
660ObjectFilePECOFF::IsStripped ()
Greg Claytonf754f882011-09-09 20:33:05 +0000661{
Greg Clayton3046e662013-07-10 01:23:25 +0000662 // TODO: determine this for COFF
663 return false;
664}
665
666
667
668void
669ObjectFilePECOFF::CreateSections (SectionList &unified_section_list)
670{
671 if (!m_sections_ap.get())
Greg Claytonf754f882011-09-09 20:33:05 +0000672 {
Greg Clayton3046e662013-07-10 01:23:25 +0000673 m_sections_ap.reset(new SectionList());
674
675 ModuleSP module_sp(GetModule());
676 if (module_sp)
Greg Claytonf754f882011-09-09 20:33:05 +0000677 {
Greg Clayton3046e662013-07-10 01:23:25 +0000678 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
Greg Claytona1743492012-03-13 23:14:29 +0000679 const uint32_t nsects = m_sect_headers.size();
680 ModuleSP module_sp (GetModule());
681 for (uint32_t idx = 0; idx<nsects; ++idx)
Greg Clayton28469ca2011-09-10 01:04:42 +0000682 {
Greg Claytona1743492012-03-13 23:14:29 +0000683 std::string sect_name;
684 GetSectionName (sect_name, m_sect_headers[idx]);
685 ConstString const_sect_name (sect_name.c_str());
686 static ConstString g_code_sect_name (".code");
687 static ConstString g_CODE_sect_name ("CODE");
688 static ConstString g_data_sect_name (".data");
689 static ConstString g_DATA_sect_name ("DATA");
690 static ConstString g_bss_sect_name (".bss");
691 static ConstString g_BSS_sect_name ("BSS");
692 static ConstString g_debug_sect_name (".debug");
693 static ConstString g_reloc_sect_name (".reloc");
694 static ConstString g_stab_sect_name (".stab");
695 static ConstString g_stabstr_sect_name (".stabstr");
Greg Clayton0076e712013-06-18 00:08:58 +0000696 static ConstString g_sect_name_dwarf_debug_abbrev (".debug_abbrev");
697 static ConstString g_sect_name_dwarf_debug_aranges (".debug_aranges");
698 static ConstString g_sect_name_dwarf_debug_frame (".debug_frame");
699 static ConstString g_sect_name_dwarf_debug_info (".debug_info");
700 static ConstString g_sect_name_dwarf_debug_line (".debug_line");
701 static ConstString g_sect_name_dwarf_debug_loc (".debug_loc");
702 static ConstString g_sect_name_dwarf_debug_macinfo (".debug_macinfo");
703 static ConstString g_sect_name_dwarf_debug_pubnames (".debug_pubnames");
704 static ConstString g_sect_name_dwarf_debug_pubtypes (".debug_pubtypes");
705 static ConstString g_sect_name_dwarf_debug_ranges (".debug_ranges");
706 static ConstString g_sect_name_dwarf_debug_str (".debug_str");
707 static ConstString g_sect_name_eh_frame (".eh_frame");
Ryan Brown65d4d5c2015-09-16 21:20:44 +0000708 static ConstString g_sect_name_go_symtab (".gosymtab");
Greg Claytona1743492012-03-13 23:14:29 +0000709 SectionType section_type = eSectionTypeOther;
Charles Davis237ad972013-08-27 05:04:33 +0000710 if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_CODE &&
Greg Claytona1743492012-03-13 23:14:29 +0000711 ((const_sect_name == g_code_sect_name) || (const_sect_name == g_CODE_sect_name)))
712 {
713 section_type = eSectionTypeCode;
714 }
Charles Davis237ad972013-08-27 05:04:33 +0000715 else if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_INITIALIZED_DATA &&
Greg Claytona1743492012-03-13 23:14:29 +0000716 ((const_sect_name == g_data_sect_name) || (const_sect_name == g_DATA_sect_name)))
717 {
Greg Clayton28469ca2011-09-10 01:04:42 +0000718 section_type = eSectionTypeData;
Greg Claytona1743492012-03-13 23:14:29 +0000719 }
Charles Davis237ad972013-08-27 05:04:33 +0000720 else if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA &&
Greg Claytona1743492012-03-13 23:14:29 +0000721 ((const_sect_name == g_bss_sect_name) || (const_sect_name == g_BSS_sect_name)))
722 {
723 if (m_sect_headers[idx].size == 0)
724 section_type = eSectionTypeZeroFill;
725 else
726 section_type = eSectionTypeData;
727 }
728 else if (const_sect_name == g_debug_sect_name)
729 {
730 section_type = eSectionTypeDebug;
731 }
732 else if (const_sect_name == g_stabstr_sect_name)
733 {
734 section_type = eSectionTypeDataCString;
735 }
736 else if (const_sect_name == g_reloc_sect_name)
737 {
738 section_type = eSectionTypeOther;
739 }
Greg Clayton0076e712013-06-18 00:08:58 +0000740 else if (const_sect_name == g_sect_name_dwarf_debug_abbrev) section_type = eSectionTypeDWARFDebugAbbrev;
741 else if (const_sect_name == g_sect_name_dwarf_debug_aranges) section_type = eSectionTypeDWARFDebugAranges;
742 else if (const_sect_name == g_sect_name_dwarf_debug_frame) section_type = eSectionTypeDWARFDebugFrame;
743 else if (const_sect_name == g_sect_name_dwarf_debug_info) section_type = eSectionTypeDWARFDebugInfo;
744 else if (const_sect_name == g_sect_name_dwarf_debug_line) section_type = eSectionTypeDWARFDebugLine;
745 else if (const_sect_name == g_sect_name_dwarf_debug_loc) section_type = eSectionTypeDWARFDebugLoc;
746 else if (const_sect_name == g_sect_name_dwarf_debug_macinfo) section_type = eSectionTypeDWARFDebugMacInfo;
747 else if (const_sect_name == g_sect_name_dwarf_debug_pubnames) section_type = eSectionTypeDWARFDebugPubNames;
748 else if (const_sect_name == g_sect_name_dwarf_debug_pubtypes) section_type = eSectionTypeDWARFDebugPubTypes;
749 else if (const_sect_name == g_sect_name_dwarf_debug_ranges) section_type = eSectionTypeDWARFDebugRanges;
750 else if (const_sect_name == g_sect_name_dwarf_debug_str) section_type = eSectionTypeDWARFDebugStr;
751 else if (const_sect_name == g_sect_name_eh_frame) section_type = eSectionTypeEHFrame;
Ryan Brown65d4d5c2015-09-16 21:20:44 +0000752 else if (const_sect_name == g_sect_name_go_symtab) section_type = eSectionTypeGoSymtab;
Charles Davis237ad972013-08-27 05:04:33 +0000753 else if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_CODE)
Greg Claytona1743492012-03-13 23:14:29 +0000754 {
755 section_type = eSectionTypeCode;
756 }
Charles Davis237ad972013-08-27 05:04:33 +0000757 else if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
Greg Claytona1743492012-03-13 23:14:29 +0000758 {
Greg Clayton28469ca2011-09-10 01:04:42 +0000759 section_type = eSectionTypeData;
Greg Claytona1743492012-03-13 23:14:29 +0000760 }
Charles Davis237ad972013-08-27 05:04:33 +0000761 else if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
Greg Claytona1743492012-03-13 23:14:29 +0000762 {
763 if (m_sect_headers[idx].size == 0)
764 section_type = eSectionTypeZeroFill;
765 else
766 section_type = eSectionTypeData;
767 }
768
769 // Use a segment ID of the segment index shifted left by 8 so they
770 // never conflict with any of the sections.
771 SectionSP section_sp (new Section (module_sp, // Module to which this section belongs
Michael Sartaina7499c92013-07-01 19:45:50 +0000772 this, // Object file to which this section belongs
Greg Claytona1743492012-03-13 23:14:29 +0000773 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
774 const_sect_name, // Name of this section
Michael Sartaina7499c92013-07-01 19:45:50 +0000775 section_type, // This section is a container of other sections.
Greg Clayton0076e712013-06-18 00:08:58 +0000776 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 +0000777 m_sect_headers[idx].vmsize, // VM size in bytes of this section
778 m_sect_headers[idx].offset, // Offset to the data for this section in the file
Bruce Mitcheneraaa0ba32014-07-08 18:05:41 +0000779 m_sect_headers[idx].size, // Size in bytes of this section as found in the file
Greg Clayton48672af2014-06-24 22:22:43 +0000780 m_coff_header_opt.sect_alignment, // Section alignment
Greg Claytona1743492012-03-13 23:14:29 +0000781 m_sect_headers[idx].flags)); // Flags for this section
782
783 //section_sp->SetIsEncrypted (segment_is_encrypted);
784
Greg Clayton3046e662013-07-10 01:23:25 +0000785 unified_section_list.AddSection(section_sp);
786 m_sections_ap->AddSection (section_sp);
Greg Clayton28469ca2011-09-10 01:04:42 +0000787 }
Greg Claytonf754f882011-09-09 20:33:05 +0000788 }
789 }
Greg Claytonf754f882011-09-09 20:33:05 +0000790}
791
792bool
793ObjectFilePECOFF::GetUUID (UUID* uuid)
794{
795 return false;
796}
797
798uint32_t
799ObjectFilePECOFF::GetDependentModules (FileSpecList& files)
800{
801 return 0;
802}
803
804
805//----------------------------------------------------------------------
806// Dump
807//
808// Dump the specifics of the runtime file container (such as any headers
809// segments, sections, etc).
810//----------------------------------------------------------------------
811void
812ObjectFilePECOFF::Dump(Stream *s)
813{
Greg Claytona1743492012-03-13 23:14:29 +0000814 ModuleSP module_sp(GetModule());
815 if (module_sp)
Greg Claytonf754f882011-09-09 20:33:05 +0000816 {
Greg Claytona1743492012-03-13 23:14:29 +0000817 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000818 s->Printf("%p: ", static_cast<void*>(this));
Greg Claytona1743492012-03-13 23:14:29 +0000819 s->Indent();
820 s->PutCString("ObjectFilePECOFF");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000821
Greg Claytona1743492012-03-13 23:14:29 +0000822 ArchSpec header_arch;
823 GetArchitecture (header_arch);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000824
Greg Claytona1743492012-03-13 23:14:29 +0000825 *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n";
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000826
Greg Clayton3046e662013-07-10 01:23:25 +0000827 SectionList *sections = GetSectionList();
828 if (sections)
829 sections->Dump(s, NULL, true, UINT32_MAX);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000830
Greg Claytona1743492012-03-13 23:14:29 +0000831 if (m_symtab_ap.get())
832 m_symtab_ap->Dump(s, NULL, eSortOrderNone);
833
834 if (m_dos_header.e_magic)
835 DumpDOSHeader (s, m_dos_header);
836 if (m_coff_header.machine)
837 {
838 DumpCOFFHeader (s, m_coff_header);
839 if (m_coff_header.hdrsize)
840 DumpOptCOFFHeader (s, m_coff_header_opt);
841 }
842 s->EOL();
843 DumpSectionHeaders(s);
844 s->EOL();
Greg Claytonf754f882011-09-09 20:33:05 +0000845 }
Greg Claytonf754f882011-09-09 20:33:05 +0000846}
847
848//----------------------------------------------------------------------
849// DumpDOSHeader
850//
851// Dump the MS-DOS header to the specified output stream
852//----------------------------------------------------------------------
853void
854ObjectFilePECOFF::DumpDOSHeader(Stream *s, const dos_header_t& header)
855{
856 s->PutCString ("MSDOS Header\n");
857 s->Printf (" e_magic = 0x%4.4x\n", header.e_magic);
858 s->Printf (" e_cblp = 0x%4.4x\n", header.e_cblp);
859 s->Printf (" e_cp = 0x%4.4x\n", header.e_cp);
860 s->Printf (" e_crlc = 0x%4.4x\n", header.e_crlc);
861 s->Printf (" e_cparhdr = 0x%4.4x\n", header.e_cparhdr);
862 s->Printf (" e_minalloc = 0x%4.4x\n", header.e_minalloc);
863 s->Printf (" e_maxalloc = 0x%4.4x\n", header.e_maxalloc);
864 s->Printf (" e_ss = 0x%4.4x\n", header.e_ss);
865 s->Printf (" e_sp = 0x%4.4x\n", header.e_sp);
866 s->Printf (" e_csum = 0x%4.4x\n", header.e_csum);
867 s->Printf (" e_ip = 0x%4.4x\n", header.e_ip);
868 s->Printf (" e_cs = 0x%4.4x\n", header.e_cs);
869 s->Printf (" e_lfarlc = 0x%4.4x\n", header.e_lfarlc);
870 s->Printf (" e_ovno = 0x%4.4x\n", header.e_ovno);
871 s->Printf (" e_res[4] = { 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x }\n",
872 header.e_res[0],
873 header.e_res[1],
874 header.e_res[2],
875 header.e_res[3]);
876 s->Printf (" e_oemid = 0x%4.4x\n", header.e_oemid);
877 s->Printf (" e_oeminfo = 0x%4.4x\n", header.e_oeminfo);
878 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",
879 header.e_res2[0],
880 header.e_res2[1],
881 header.e_res2[2],
882 header.e_res2[3],
883 header.e_res2[4],
884 header.e_res2[5],
885 header.e_res2[6],
886 header.e_res2[7],
887 header.e_res2[8],
888 header.e_res2[9]);
889 s->Printf (" e_lfanew = 0x%8.8x\n", header.e_lfanew);
890}
891
892//----------------------------------------------------------------------
893// DumpCOFFHeader
894//
895// Dump the COFF header to the specified output stream
896//----------------------------------------------------------------------
897void
898ObjectFilePECOFF::DumpCOFFHeader(Stream *s, const coff_header_t& header)
899{
900 s->PutCString ("COFF Header\n");
901 s->Printf (" machine = 0x%4.4x\n", header.machine);
902 s->Printf (" nsects = 0x%4.4x\n", header.nsects);
903 s->Printf (" modtime = 0x%8.8x\n", header.modtime);
904 s->Printf (" symoff = 0x%8.8x\n", header.symoff);
905 s->Printf (" nsyms = 0x%8.8x\n", header.nsyms);
906 s->Printf (" hdrsize = 0x%4.4x\n", header.hdrsize);
907}
908
909//----------------------------------------------------------------------
910// DumpOptCOFFHeader
911//
912// Dump the optional COFF header to the specified output stream
913//----------------------------------------------------------------------
914void
915ObjectFilePECOFF::DumpOptCOFFHeader(Stream *s, const coff_opt_header_t& header)
916{
917 s->PutCString ("Optional COFF Header\n");
918 s->Printf (" magic = 0x%4.4x\n", header.magic);
919 s->Printf (" major_linker_version = 0x%2.2x\n", header.major_linker_version);
920 s->Printf (" minor_linker_version = 0x%2.2x\n", header.minor_linker_version);
921 s->Printf (" code_size = 0x%8.8x\n", header.code_size);
922 s->Printf (" data_size = 0x%8.8x\n", header.data_size);
923 s->Printf (" bss_size = 0x%8.8x\n", header.bss_size);
924 s->Printf (" entry = 0x%8.8x\n", header.entry);
925 s->Printf (" code_offset = 0x%8.8x\n", header.code_offset);
926 s->Printf (" data_offset = 0x%8.8x\n", header.data_offset);
Daniel Malead01b2952012-11-29 21:49:15 +0000927 s->Printf (" image_base = 0x%16.16" PRIx64 "\n", header.image_base);
Greg Claytonf754f882011-09-09 20:33:05 +0000928 s->Printf (" sect_alignment = 0x%8.8x\n", header.sect_alignment);
929 s->Printf (" file_alignment = 0x%8.8x\n", header.file_alignment);
930 s->Printf (" major_os_system_version = 0x%4.4x\n", header.major_os_system_version);
931 s->Printf (" minor_os_system_version = 0x%4.4x\n", header.minor_os_system_version);
932 s->Printf (" major_image_version = 0x%4.4x\n", header.major_image_version);
933 s->Printf (" minor_image_version = 0x%4.4x\n", header.minor_image_version);
934 s->Printf (" major_subsystem_version = 0x%4.4x\n", header.major_subsystem_version);
935 s->Printf (" minor_subsystem_version = 0x%4.4x\n", header.minor_subsystem_version);
936 s->Printf (" reserved1 = 0x%8.8x\n", header.reserved1);
937 s->Printf (" image_size = 0x%8.8x\n", header.image_size);
938 s->Printf (" header_size = 0x%8.8x\n", header.header_size);
Greg Clayton28469ca2011-09-10 01:04:42 +0000939 s->Printf (" checksum = 0x%8.8x\n", header.checksum);
Greg Claytonf754f882011-09-09 20:33:05 +0000940 s->Printf (" subsystem = 0x%4.4x\n", header.subsystem);
941 s->Printf (" dll_flags = 0x%4.4x\n", header.dll_flags);
Daniel Malead01b2952012-11-29 21:49:15 +0000942 s->Printf (" stack_reserve_size = 0x%16.16" PRIx64 "\n", header.stack_reserve_size);
943 s->Printf (" stack_commit_size = 0x%16.16" PRIx64 "\n", header.stack_commit_size);
944 s->Printf (" heap_reserve_size = 0x%16.16" PRIx64 "\n", header.heap_reserve_size);
945 s->Printf (" heap_commit_size = 0x%16.16" PRIx64 "\n", header.heap_commit_size);
Greg Claytonf754f882011-09-09 20:33:05 +0000946 s->Printf (" loader_flags = 0x%8.8x\n", header.loader_flags);
Virgile Belloffeba252014-03-08 17:15:35 +0000947 s->Printf (" num_data_dir_entries = 0x%8.8x\n", (uint32_t)header.data_dirs.size());
Greg Claytonf754f882011-09-09 20:33:05 +0000948 uint32_t i;
949 for (i=0; i<header.data_dirs.size(); i++)
950 {
Greg Clayton28469ca2011-09-10 01:04:42 +0000951 s->Printf (" data_dirs[%2u] vmaddr = 0x%8.8x, vmsize = 0x%8.8x\n",
Greg Claytonf754f882011-09-09 20:33:05 +0000952 i,
953 header.data_dirs[i].vmaddr,
954 header.data_dirs[i].vmsize);
955 }
956}
957//----------------------------------------------------------------------
958// DumpSectionHeader
959//
960// Dump a single ELF section header to the specified output stream
961//----------------------------------------------------------------------
962void
963ObjectFilePECOFF::DumpSectionHeader(Stream *s, const section_header_t& sh)
964{
965 std::string name;
966 GetSectionName(name, sh);
967 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",
968 name.c_str(),
Greg Claytonf754f882011-09-09 20:33:05 +0000969 sh.vmaddr,
Greg Clayton28469ca2011-09-10 01:04:42 +0000970 sh.vmsize,
Greg Claytonf754f882011-09-09 20:33:05 +0000971 sh.offset,
Greg Clayton28469ca2011-09-10 01:04:42 +0000972 sh.size,
Greg Claytonf754f882011-09-09 20:33:05 +0000973 sh.reloff,
974 sh.lineoff,
975 sh.nreloc,
976 sh.nline,
977 sh.flags);
978}
979
980
981//----------------------------------------------------------------------
982// DumpSectionHeaders
983//
984// Dump all of the ELF section header to the specified output stream
985//----------------------------------------------------------------------
986void
987ObjectFilePECOFF::DumpSectionHeaders(Stream *s)
988{
989
990 s->PutCString ("Section Headers\n");
Greg Clayton28469ca2011-09-10 01:04:42 +0000991 s->PutCString ("IDX name vm addr vm size file off file size reloc off line off nreloc nline flags\n");
992 s->PutCString ("==== ---------------- ---------- ---------- ---------- ---------- ---------- ---------- ------ ------ ----------\n");
Greg Claytonf754f882011-09-09 20:33:05 +0000993
994 uint32_t idx = 0;
995 SectionHeaderCollIter pos, end = m_sect_headers.end();
996
997 for (pos = m_sect_headers.begin(); pos != end; ++pos, ++idx)
998 {
Greg Clayton28469ca2011-09-10 01:04:42 +0000999 s->Printf ("[%2u] ", idx);
Greg Claytonf754f882011-09-09 20:33:05 +00001000 ObjectFilePECOFF::DumpSectionHeader(s, *pos);
1001 }
1002}
1003
Greg Claytonf754f882011-09-09 20:33:05 +00001004bool
1005ObjectFilePECOFF::GetArchitecture (ArchSpec &arch)
1006{
Charles Davis237ad972013-08-27 05:04:33 +00001007 uint16_t machine = m_coff_header.machine;
1008 switch (machine)
1009 {
1010 case llvm::COFF::IMAGE_FILE_MACHINE_AMD64:
1011 case llvm::COFF::IMAGE_FILE_MACHINE_I386:
1012 case llvm::COFF::IMAGE_FILE_MACHINE_POWERPC:
1013 case llvm::COFF::IMAGE_FILE_MACHINE_POWERPCFP:
1014 case llvm::COFF::IMAGE_FILE_MACHINE_ARM:
Saleem Abdulrasool1108cb32014-03-11 03:09:08 +00001015 case llvm::COFF::IMAGE_FILE_MACHINE_ARMNT:
Charles Davis237ad972013-08-27 05:04:33 +00001016 case llvm::COFF::IMAGE_FILE_MACHINE_THUMB:
Colin Riley6c970422013-11-22 09:35:12 +00001017 arch.SetArchitecture (eArchTypeCOFF, machine, LLDB_INVALID_CPUTYPE);
Charles Davis237ad972013-08-27 05:04:33 +00001018 return true;
1019 default:
1020 break;
1021 }
1022 return false;
Greg Claytonf754f882011-09-09 20:33:05 +00001023}
1024
1025ObjectFile::Type
1026ObjectFilePECOFF::CalculateType()
1027{
1028 if (m_coff_header.machine != 0)
1029 {
Charles Davis237ad972013-08-27 05:04:33 +00001030 if ((m_coff_header.flags & llvm::COFF::IMAGE_FILE_DLL) == 0)
Greg Claytonf754f882011-09-09 20:33:05 +00001031 return eTypeExecutable;
1032 else
1033 return eTypeSharedLibrary;
1034 }
1035 return eTypeExecutable;
1036}
1037
1038ObjectFile::Strata
1039ObjectFilePECOFF::CalculateStrata()
1040{
1041 return eStrataUser;
1042}
1043//------------------------------------------------------------------
1044// PluginInterface protocol
1045//------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +00001046ConstString
Greg Claytonf754f882011-09-09 20:33:05 +00001047ObjectFilePECOFF::GetPluginName()
1048{
Greg Claytonf754f882011-09-09 20:33:05 +00001049 return GetPluginNameStatic();
1050}
1051
1052uint32_t
1053ObjectFilePECOFF::GetPluginVersion()
1054{
1055 return 1;
1056}
1057