blob: 60eb5fcfe02908cffb9c9f38f148ec06c4dcc4e8 [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"
Walter Erquinigo344546b2016-10-17 20:28:19 +000017#include "lldb/Core/DataBufferHeap.h"
Greg Claytonf754f882011-09-09 20:33:05 +000018#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"
Greg Claytonf754f882011-09-09 20:33:05 +000024#include "lldb/Core/Timer.h"
25#include "lldb/Core/UUID.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000026#include "lldb/Host/FileSpec.h"
Greg Claytonf754f882011-09-09 20:33:05 +000027#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"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000031#include "lldb/Utility/StreamString.h"
Greg Claytonf754f882011-09-09 20:33:05 +000032
Kate Stoneb9c1b512016-09-06 20:57:50 +000033#define IMAGE_DOS_SIGNATURE 0x5A4D // MZ
34#define IMAGE_NT_SIGNATURE 0x00004550 // PE00
35#define OPT_HEADER_MAGIC_PE32 0x010b
36#define OPT_HEADER_MAGIC_PE32_PLUS 0x020b
Greg Claytonf754f882011-09-09 20:33:05 +000037
Greg Claytonf754f882011-09-09 20:33:05 +000038using namespace lldb;
39using namespace lldb_private;
40
Kate Stoneb9c1b512016-09-06 20:57:50 +000041void ObjectFilePECOFF::Initialize() {
42 PluginManager::RegisterPlugin(
43 GetPluginNameStatic(), GetPluginDescriptionStatic(), CreateInstance,
44 CreateMemoryInstance, GetModuleSpecifications, SaveCore);
Greg Claytonf754f882011-09-09 20:33:05 +000045}
46
Kate Stoneb9c1b512016-09-06 20:57:50 +000047void ObjectFilePECOFF::Terminate() {
48 PluginManager::UnregisterPlugin(CreateInstance);
Greg Claytonf754f882011-09-09 20:33:05 +000049}
50
Kate Stoneb9c1b512016-09-06 20:57:50 +000051lldb_private::ConstString ObjectFilePECOFF::GetPluginNameStatic() {
52 static ConstString g_name("pe-coff");
53 return g_name;
Greg Claytonf754f882011-09-09 20:33:05 +000054}
55
Kate Stoneb9c1b512016-09-06 20:57:50 +000056const char *ObjectFilePECOFF::GetPluginDescriptionStatic() {
57 return "Portable Executable and Common Object File Format object file reader "
58 "(32 and 64 bit)";
Greg Claytonf754f882011-09-09 20:33:05 +000059}
60
Kate Stoneb9c1b512016-09-06 20:57:50 +000061ObjectFile *ObjectFilePECOFF::CreateInstance(const lldb::ModuleSP &module_sp,
62 DataBufferSP &data_sp,
63 lldb::offset_t data_offset,
64 const lldb_private::FileSpec *file,
65 lldb::offset_t file_offset,
66 lldb::offset_t length) {
67 if (!data_sp) {
68 data_sp = file->MemoryMapFileContentsIfLocal(file_offset, length);
69 data_offset = 0;
70 }
Greg Claytonf754f882011-09-09 20:33:05 +000071
Kate Stoneb9c1b512016-09-06 20:57:50 +000072 if (ObjectFilePECOFF::MagicBytesMatch(data_sp)) {
73 // Update the data to contain the entire file if it doesn't already
74 if (data_sp->GetByteSize() < length)
75 data_sp = file->MemoryMapFileContentsIfLocal(file_offset, length);
76 std::unique_ptr<ObjectFile> objfile_ap(new ObjectFilePECOFF(
77 module_sp, data_sp, data_offset, file, file_offset, length));
78 if (objfile_ap.get() && objfile_ap->ParseHeader())
79 return objfile_ap.release();
80 }
81 return NULL;
Greg Claytonf754f882011-09-09 20:33:05 +000082}
83
Kate Stoneb9c1b512016-09-06 20:57:50 +000084ObjectFile *ObjectFilePECOFF::CreateMemoryInstance(
85 const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp,
86 const lldb::ProcessSP &process_sp, lldb::addr_t header_addr) {
Walter Erquinigo344546b2016-10-17 20:28:19 +000087 if (!data_sp || !ObjectFilePECOFF::MagicBytesMatch(data_sp))
88 return nullptr;
89 auto objfile_ap = llvm::make_unique<ObjectFilePECOFF>(
90 module_sp, data_sp, process_sp, header_addr);
91 if (objfile_ap.get() && objfile_ap->ParseHeader()) {
92 return objfile_ap.release();
93 }
94 return nullptr;
Greg Claytonc9660542012-02-05 02:38:54 +000095}
96
Kate Stoneb9c1b512016-09-06 20:57:50 +000097size_t ObjectFilePECOFF::GetModuleSpecifications(
98 const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp,
99 lldb::offset_t data_offset, lldb::offset_t file_offset,
100 lldb::offset_t length, lldb_private::ModuleSpecList &specs) {
101 const size_t initial_count = specs.GetSize();
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000102
Kate Stoneb9c1b512016-09-06 20:57:50 +0000103 if (ObjectFilePECOFF::MagicBytesMatch(data_sp)) {
104 DataExtractor data;
105 data.SetData(data_sp, data_offset, length);
106 data.SetByteOrder(eByteOrderLittle);
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000107
Kate Stoneb9c1b512016-09-06 20:57:50 +0000108 dos_header_t dos_header;
109 coff_header_t coff_header;
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000110
Kate Stoneb9c1b512016-09-06 20:57:50 +0000111 if (ParseDOSHeader(data, dos_header)) {
112 lldb::offset_t offset = dos_header.e_lfanew;
113 uint32_t pe_signature = data.GetU32(&offset);
114 if (pe_signature != IMAGE_NT_SIGNATURE)
115 return false;
116 if (ParseCOFFHeader(data, &offset, coff_header)) {
117 ArchSpec spec;
118 if (coff_header.machine == MachineAmd64) {
119 spec.SetTriple("x86_64-pc-windows");
120 specs.Append(ModuleSpec(file, spec));
121 } else if (coff_header.machine == MachineX86) {
122 spec.SetTriple("i386-pc-windows");
123 specs.Append(ModuleSpec(file, spec));
124 spec.SetTriple("i686-pc-windows");
125 specs.Append(ModuleSpec(file, spec));
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000126 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000127 }
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000128 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000129 }
Virgile Bello89eb1ba2014-03-09 09:59:36 +0000130
Kate Stoneb9c1b512016-09-06 20:57:50 +0000131 return specs.GetSize() - initial_count;
Greg Claytonf4d6de62013-04-24 22:29:28 +0000132}
133
Kate Stoneb9c1b512016-09-06 20:57:50 +0000134bool ObjectFilePECOFF::SaveCore(const lldb::ProcessSP &process_sp,
135 const lldb_private::FileSpec &outfile,
136 lldb_private::Error &error) {
137 return SaveMiniDump(process_sp, outfile, error);
Adrian McCarthyf7d18932015-11-20 23:09:11 +0000138}
139
Kate Stoneb9c1b512016-09-06 20:57:50 +0000140bool ObjectFilePECOFF::MagicBytesMatch(DataBufferSP &data_sp) {
141 DataExtractor data(data_sp, eByteOrderLittle, 4);
142 lldb::offset_t offset = 0;
143 uint16_t magic = data.GetU16(&offset);
144 return magic == IMAGE_DOS_SIGNATURE;
145}
Greg Claytonf4d6de62013-04-24 22:29:28 +0000146
Kate Stoneb9c1b512016-09-06 20:57:50 +0000147lldb::SymbolType ObjectFilePECOFF::MapSymbolType(uint16_t coff_symbol_type) {
148 // TODO: We need to complete this mapping of COFF symbol types to LLDB ones.
149 // For now, here's a hack to make sure our function have types.
150 const auto complex_type =
151 coff_symbol_type >> llvm::COFF::SCT_COMPLEX_TYPE_SHIFT;
152 if (complex_type == llvm::COFF::IMAGE_SYM_DTYPE_FUNCTION) {
153 return lldb::eSymbolTypeCode;
154 }
155 return lldb::eSymbolTypeInvalid;
156}
157
158ObjectFilePECOFF::ObjectFilePECOFF(const lldb::ModuleSP &module_sp,
159 DataBufferSP &data_sp,
160 lldb::offset_t data_offset,
161 const FileSpec *file,
162 lldb::offset_t file_offset,
163 lldb::offset_t length)
164 : ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset),
165 m_dos_header(), m_coff_header(), m_coff_header_opt(), m_sect_headers(),
166 m_entry_point_address() {
167 ::memset(&m_dos_header, 0, sizeof(m_dos_header));
168 ::memset(&m_coff_header, 0, sizeof(m_coff_header));
169 ::memset(&m_coff_header_opt, 0, sizeof(m_coff_header_opt));
170}
171
Walter Erquinigo344546b2016-10-17 20:28:19 +0000172ObjectFilePECOFF::ObjectFilePECOFF(const lldb::ModuleSP &module_sp,
173 DataBufferSP &header_data_sp,
174 const lldb::ProcessSP &process_sp,
175 addr_t header_addr)
176 : ObjectFile(module_sp, process_sp, header_addr, header_data_sp),
177 m_dos_header(), m_coff_header(), m_coff_header_opt(), m_sect_headers(),
178 m_entry_point_address() {
179 ::memset(&m_dos_header, 0, sizeof(m_dos_header));
180 ::memset(&m_coff_header, 0, sizeof(m_coff_header));
181 ::memset(&m_coff_header_opt, 0, sizeof(m_coff_header_opt));
182}
183
Kate Stoneb9c1b512016-09-06 20:57:50 +0000184ObjectFilePECOFF::~ObjectFilePECOFF() {}
185
186bool ObjectFilePECOFF::ParseHeader() {
187 ModuleSP module_sp(GetModule());
188 if (module_sp) {
189 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
190 m_sect_headers.clear();
191 m_data.SetByteOrder(eByteOrderLittle);
Greg Claytonc7bece562013-01-25 18:06:21 +0000192 lldb::offset_t offset = 0;
Greg Claytonf754f882011-09-09 20:33:05 +0000193
Kate Stoneb9c1b512016-09-06 20:57:50 +0000194 if (ParseDOSHeader(m_data, m_dos_header)) {
195 offset = m_dos_header.e_lfanew;
196 uint32_t pe_signature = m_data.GetU32(&offset);
197 if (pe_signature != IMAGE_NT_SIGNATURE)
198 return false;
199 if (ParseCOFFHeader(m_data, &offset, m_coff_header)) {
200 if (m_coff_header.hdrsize > 0)
201 ParseCOFFOptionalHeader(&offset);
202 ParseSectionHeaders(offset);
203 }
204 return true;
Adrian McCarthyc35b91c2016-01-26 00:58:09 +0000205 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000206 }
207 return false;
Adrian McCarthyc35b91c2016-01-26 00:58:09 +0000208}
Greg Claytonf754f882011-09-09 20:33:05 +0000209
Kate Stoneb9c1b512016-09-06 20:57:50 +0000210bool ObjectFilePECOFF::SetLoadAddress(Target &target, addr_t value,
211 bool value_is_offset) {
212 bool changed = false;
213 ModuleSP module_sp = GetModule();
214 if (module_sp) {
215 size_t num_loaded_sections = 0;
216 SectionList *section_list = GetSectionList();
217 if (section_list) {
218 if (!value_is_offset) {
219 value -= m_image_base;
220 }
Greg Claytonf754f882011-09-09 20:33:05 +0000221
Kate Stoneb9c1b512016-09-06 20:57:50 +0000222 const size_t num_sections = section_list->GetSize();
223 size_t sect_idx = 0;
Greg Claytonf754f882011-09-09 20:33:05 +0000224
Kate Stoneb9c1b512016-09-06 20:57:50 +0000225 for (sect_idx = 0; sect_idx < num_sections; ++sect_idx) {
226 // Iterate through the object file sections to find all
227 // of the sections that have SHF_ALLOC in their flag bits.
228 SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx));
229 if (section_sp && !section_sp->IsThreadSpecific()) {
230 if (target.GetSectionLoadList().SetSectionLoadAddress(
231 section_sp, section_sp->GetFileAddress() + value))
232 ++num_loaded_sections;
Greg Clayton28469ca2011-09-10 01:04:42 +0000233 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000234 }
235 changed = num_loaded_sections > 0;
Greg Claytonf754f882011-09-09 20:33:05 +0000236 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000237 }
238 return changed;
Greg Claytonf754f882011-09-09 20:33:05 +0000239}
240
Kate Stoneb9c1b512016-09-06 20:57:50 +0000241ByteOrder ObjectFilePECOFF::GetByteOrder() const { return eByteOrderLittle; }
Virgile Bello2756adf2014-03-08 17:17:20 +0000242
Kate Stoneb9c1b512016-09-06 20:57:50 +0000243bool ObjectFilePECOFF::IsExecutable() const {
244 return (m_coff_header.flags & llvm::COFF::IMAGE_FILE_DLL) == 0;
Virgile Bello2756adf2014-03-08 17:17:20 +0000245}
246
Kate Stoneb9c1b512016-09-06 20:57:50 +0000247uint32_t ObjectFilePECOFF::GetAddressByteSize() const {
248 if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32_PLUS)
249 return 8;
250 else if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32)
Greg Claytonf754f882011-09-09 20:33:05 +0000251 return 4;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000252 return 4;
Greg Claytonf754f882011-09-09 20:33:05 +0000253}
254
255//----------------------------------------------------------------------
256// NeedsEndianSwap
257//
Kate Stoneb9c1b512016-09-06 20:57:50 +0000258// Return true if an endian swap needs to occur when extracting data
Greg Claytonf754f882011-09-09 20:33:05 +0000259// from this file.
260//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000261bool ObjectFilePECOFF::NeedsEndianSwap() const {
Greg Claytonf754f882011-09-09 20:33:05 +0000262#if defined(__LITTLE_ENDIAN__)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000263 return false;
Greg Claytonf754f882011-09-09 20:33:05 +0000264#else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000265 return true;
Greg Claytonf754f882011-09-09 20:33:05 +0000266#endif
267}
268//----------------------------------------------------------------------
269// ParseDOSHeader
270//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000271bool ObjectFilePECOFF::ParseDOSHeader(DataExtractor &data,
272 dos_header_t &dos_header) {
273 bool success = false;
274 lldb::offset_t offset = 0;
275 success = data.ValidOffsetForDataOfSize(0, sizeof(dos_header));
Greg Claytonf754f882011-09-09 20:33:05 +0000276
Kate Stoneb9c1b512016-09-06 20:57:50 +0000277 if (success) {
278 dos_header.e_magic = data.GetU16(&offset); // Magic number
279 success = dos_header.e_magic == IMAGE_DOS_SIGNATURE;
280
281 if (success) {
282 dos_header.e_cblp = data.GetU16(&offset); // Bytes on last page of file
283 dos_header.e_cp = data.GetU16(&offset); // Pages in file
284 dos_header.e_crlc = data.GetU16(&offset); // Relocations
285 dos_header.e_cparhdr =
286 data.GetU16(&offset); // Size of header in paragraphs
287 dos_header.e_minalloc =
288 data.GetU16(&offset); // Minimum extra paragraphs needed
289 dos_header.e_maxalloc =
290 data.GetU16(&offset); // Maximum extra paragraphs needed
291 dos_header.e_ss = data.GetU16(&offset); // Initial (relative) SS value
292 dos_header.e_sp = data.GetU16(&offset); // Initial SP value
293 dos_header.e_csum = data.GetU16(&offset); // Checksum
294 dos_header.e_ip = data.GetU16(&offset); // Initial IP value
295 dos_header.e_cs = data.GetU16(&offset); // Initial (relative) CS value
296 dos_header.e_lfarlc =
297 data.GetU16(&offset); // File address of relocation table
298 dos_header.e_ovno = data.GetU16(&offset); // Overlay number
299
300 dos_header.e_res[0] = data.GetU16(&offset); // Reserved words
301 dos_header.e_res[1] = data.GetU16(&offset); // Reserved words
302 dos_header.e_res[2] = data.GetU16(&offset); // Reserved words
303 dos_header.e_res[3] = data.GetU16(&offset); // Reserved words
304
305 dos_header.e_oemid =
306 data.GetU16(&offset); // OEM identifier (for e_oeminfo)
307 dos_header.e_oeminfo =
308 data.GetU16(&offset); // OEM information; e_oemid specific
309 dos_header.e_res2[0] = data.GetU16(&offset); // Reserved words
310 dos_header.e_res2[1] = data.GetU16(&offset); // Reserved words
311 dos_header.e_res2[2] = data.GetU16(&offset); // Reserved words
312 dos_header.e_res2[3] = data.GetU16(&offset); // Reserved words
313 dos_header.e_res2[4] = data.GetU16(&offset); // Reserved words
314 dos_header.e_res2[5] = data.GetU16(&offset); // Reserved words
315 dos_header.e_res2[6] = data.GetU16(&offset); // Reserved words
316 dos_header.e_res2[7] = data.GetU16(&offset); // Reserved words
317 dos_header.e_res2[8] = data.GetU16(&offset); // Reserved words
318 dos_header.e_res2[9] = data.GetU16(&offset); // Reserved words
319
320 dos_header.e_lfanew =
321 data.GetU32(&offset); // File address of new exe header
322 }
323 }
324 if (!success)
325 memset(&dos_header, 0, sizeof(dos_header));
326 return success;
327}
Greg Claytonf754f882011-09-09 20:33:05 +0000328
329//----------------------------------------------------------------------
330// ParserCOFFHeader
331//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000332bool ObjectFilePECOFF::ParseCOFFHeader(DataExtractor &data,
333 lldb::offset_t *offset_ptr,
334 coff_header_t &coff_header) {
335 bool success =
336 data.ValidOffsetForDataOfSize(*offset_ptr, sizeof(coff_header));
337 if (success) {
338 coff_header.machine = data.GetU16(offset_ptr);
339 coff_header.nsects = data.GetU16(offset_ptr);
340 coff_header.modtime = data.GetU32(offset_ptr);
341 coff_header.symoff = data.GetU32(offset_ptr);
342 coff_header.nsyms = data.GetU32(offset_ptr);
343 coff_header.hdrsize = data.GetU16(offset_ptr);
344 coff_header.flags = data.GetU16(offset_ptr);
345 }
346 if (!success)
347 memset(&coff_header, 0, sizeof(coff_header));
348 return success;
Greg Claytonf754f882011-09-09 20:33:05 +0000349}
350
Kate Stoneb9c1b512016-09-06 20:57:50 +0000351bool ObjectFilePECOFF::ParseCOFFOptionalHeader(lldb::offset_t *offset_ptr) {
352 bool success = false;
353 const lldb::offset_t end_offset = *offset_ptr + m_coff_header.hdrsize;
354 if (*offset_ptr < end_offset) {
355 success = true;
356 m_coff_header_opt.magic = m_data.GetU16(offset_ptr);
357 m_coff_header_opt.major_linker_version = m_data.GetU8(offset_ptr);
358 m_coff_header_opt.minor_linker_version = m_data.GetU8(offset_ptr);
359 m_coff_header_opt.code_size = m_data.GetU32(offset_ptr);
360 m_coff_header_opt.data_size = m_data.GetU32(offset_ptr);
361 m_coff_header_opt.bss_size = m_data.GetU32(offset_ptr);
362 m_coff_header_opt.entry = m_data.GetU32(offset_ptr);
363 m_coff_header_opt.code_offset = m_data.GetU32(offset_ptr);
Greg Claytonf754f882011-09-09 20:33:05 +0000364
Kate Stoneb9c1b512016-09-06 20:57:50 +0000365 const uint32_t addr_byte_size = GetAddressByteSize();
Greg Claytonf754f882011-09-09 20:33:05 +0000366
Kate Stoneb9c1b512016-09-06 20:57:50 +0000367 if (*offset_ptr < end_offset) {
368 if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32) {
369 // PE32 only
370 m_coff_header_opt.data_offset = m_data.GetU32(offset_ptr);
371 } else
372 m_coff_header_opt.data_offset = 0;
Virgile Bello2756adf2014-03-08 17:17:20 +0000373
Kate Stoneb9c1b512016-09-06 20:57:50 +0000374 if (*offset_ptr < end_offset) {
375 m_coff_header_opt.image_base =
376 m_data.GetMaxU64(offset_ptr, addr_byte_size);
377 m_coff_header_opt.sect_alignment = m_data.GetU32(offset_ptr);
378 m_coff_header_opt.file_alignment = m_data.GetU32(offset_ptr);
379 m_coff_header_opt.major_os_system_version = m_data.GetU16(offset_ptr);
380 m_coff_header_opt.minor_os_system_version = m_data.GetU16(offset_ptr);
381 m_coff_header_opt.major_image_version = m_data.GetU16(offset_ptr);
382 m_coff_header_opt.minor_image_version = m_data.GetU16(offset_ptr);
383 m_coff_header_opt.major_subsystem_version = m_data.GetU16(offset_ptr);
384 m_coff_header_opt.minor_subsystem_version = m_data.GetU16(offset_ptr);
385 m_coff_header_opt.reserved1 = m_data.GetU32(offset_ptr);
386 m_coff_header_opt.image_size = m_data.GetU32(offset_ptr);
387 m_coff_header_opt.header_size = m_data.GetU32(offset_ptr);
388 m_coff_header_opt.checksum = m_data.GetU32(offset_ptr);
389 m_coff_header_opt.subsystem = m_data.GetU16(offset_ptr);
390 m_coff_header_opt.dll_flags = m_data.GetU16(offset_ptr);
391 m_coff_header_opt.stack_reserve_size =
392 m_data.GetMaxU64(offset_ptr, addr_byte_size);
393 m_coff_header_opt.stack_commit_size =
394 m_data.GetMaxU64(offset_ptr, addr_byte_size);
395 m_coff_header_opt.heap_reserve_size =
396 m_data.GetMaxU64(offset_ptr, addr_byte_size);
397 m_coff_header_opt.heap_commit_size =
398 m_data.GetMaxU64(offset_ptr, addr_byte_size);
399 m_coff_header_opt.loader_flags = m_data.GetU32(offset_ptr);
400 uint32_t num_data_dir_entries = m_data.GetU32(offset_ptr);
401 m_coff_header_opt.data_dirs.clear();
402 m_coff_header_opt.data_dirs.resize(num_data_dir_entries);
403 uint32_t i;
404 for (i = 0; i < num_data_dir_entries; i++) {
405 m_coff_header_opt.data_dirs[i].vmaddr = m_data.GetU32(offset_ptr);
406 m_coff_header_opt.data_dirs[i].vmsize = m_data.GetU32(offset_ptr);
Greg Claytonf754f882011-09-09 20:33:05 +0000407 }
Greg Claytonf754f882011-09-09 20:33:05 +0000408
Kate Stoneb9c1b512016-09-06 20:57:50 +0000409 m_file_offset = m_coff_header_opt.image_base;
410 m_image_base = m_coff_header_opt.image_base;
411 }
412 }
413 }
414 // Make sure we are on track for section data which follows
415 *offset_ptr = end_offset;
416 return success;
417}
Greg Claytonf754f882011-09-09 20:33:05 +0000418
Walter Erquinigo344546b2016-10-17 20:28:19 +0000419DataExtractor ObjectFilePECOFF::ReadImageData(uint32_t offset, size_t size) {
420 if (m_file) {
421 DataBufferSP buffer_sp(m_file.ReadFileContents(offset, size));
422 return DataExtractor(buffer_sp, GetByteOrder(), GetAddressByteSize());
423 }
424 ProcessSP process_sp(m_process_wp.lock());
425 DataExtractor data;
426 if (process_sp) {
427 auto data_ap = llvm::make_unique<DataBufferHeap>(size, 0);
428 Error readmem_error;
429 size_t bytes_read =
430 process_sp->ReadMemory(m_image_base + offset, data_ap->GetBytes(),
431 data_ap->GetByteSize(), readmem_error);
432 if (bytes_read == size) {
433 DataBufferSP buffer_sp(data_ap.release());
434 data.SetData(buffer_sp, 0, buffer_sp->GetByteSize());
435 }
436 }
437 return data;
438}
439
Greg Claytonf754f882011-09-09 20:33:05 +0000440//----------------------------------------------------------------------
441// ParseSectionHeaders
442//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000443bool ObjectFilePECOFF::ParseSectionHeaders(
444 uint32_t section_header_data_offset) {
445 const uint32_t nsects = m_coff_header.nsects;
446 m_sect_headers.clear();
Greg Claytonf754f882011-09-09 20:33:05 +0000447
Kate Stoneb9c1b512016-09-06 20:57:50 +0000448 if (nsects > 0) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000449 const size_t section_header_byte_size = nsects * sizeof(section_header_t);
Walter Erquinigo344546b2016-10-17 20:28:19 +0000450 DataExtractor section_header_data =
451 ReadImageData(section_header_data_offset, section_header_byte_size);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000452
453 lldb::offset_t offset = 0;
454 if (section_header_data.ValidOffsetForDataOfSize(
455 offset, section_header_byte_size)) {
456 m_sect_headers.resize(nsects);
457
458 for (uint32_t idx = 0; idx < nsects; ++idx) {
459 const void *name_data = section_header_data.GetData(&offset, 8);
460 if (name_data) {
461 memcpy(m_sect_headers[idx].name, name_data, 8);
462 m_sect_headers[idx].vmsize = section_header_data.GetU32(&offset);
463 m_sect_headers[idx].vmaddr = section_header_data.GetU32(&offset);
464 m_sect_headers[idx].size = section_header_data.GetU32(&offset);
465 m_sect_headers[idx].offset = section_header_data.GetU32(&offset);
466 m_sect_headers[idx].reloff = section_header_data.GetU32(&offset);
467 m_sect_headers[idx].lineoff = section_header_data.GetU32(&offset);
468 m_sect_headers[idx].nreloc = section_header_data.GetU16(&offset);
469 m_sect_headers[idx].nline = section_header_data.GetU16(&offset);
470 m_sect_headers[idx].flags = section_header_data.GetU32(&offset);
Greg Claytonf754f882011-09-09 20:33:05 +0000471 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000472 }
Greg Claytonf754f882011-09-09 20:33:05 +0000473 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000474 }
475
476 return m_sect_headers.empty() == false;
Greg Claytonf754f882011-09-09 20:33:05 +0000477}
478
Kate Stoneb9c1b512016-09-06 20:57:50 +0000479bool ObjectFilePECOFF::GetSectionName(std::string &sect_name,
480 const section_header_t &sect) {
481 if (sect.name[0] == '/') {
482 lldb::offset_t stroff = strtoul(&sect.name[1], NULL, 10);
483 lldb::offset_t string_file_offset =
484 m_coff_header.symoff + (m_coff_header.nsyms * 18) + stroff;
485 const char *name = m_data.GetCStr(&string_file_offset);
486 if (name) {
487 sect_name = name;
488 return true;
Greg Claytonf754f882011-09-09 20:33:05 +0000489 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000490
491 return false;
492 }
493 sect_name = sect.name;
494 return true;
495}
Greg Claytonf754f882011-09-09 20:33:05 +0000496
497//----------------------------------------------------------------------
498// GetNListSymtab
499//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000500Symtab *ObjectFilePECOFF::GetSymtab() {
501 ModuleSP module_sp(GetModule());
502 if (module_sp) {
503 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
504 if (m_symtab_ap.get() == NULL) {
505 SectionList *sect_list = GetSectionList();
506 m_symtab_ap.reset(new Symtab(this));
507 std::lock_guard<std::recursive_mutex> guard(m_symtab_ap->GetMutex());
508
509 const uint32_t num_syms = m_coff_header.nsyms;
510
Walter Erquinigo344546b2016-10-17 20:28:19 +0000511 if (m_file && num_syms > 0 && m_coff_header.symoff > 0) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000512 const uint32_t symbol_size = 18;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000513 const size_t symbol_data_size = num_syms * symbol_size;
514 // Include the 4-byte string table size at the end of the symbols
Walter Erquinigo344546b2016-10-17 20:28:19 +0000515 DataExtractor symtab_data =
516 ReadImageData(m_coff_header.symoff, symbol_data_size + 4);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000517 lldb::offset_t offset = symbol_data_size;
518 const uint32_t strtab_size = symtab_data.GetU32(&offset);
Walter Erquinigo344546b2016-10-17 20:28:19 +0000519 if (strtab_size > 0) {
520 DataExtractor strtab_data = ReadImageData(
521 m_coff_header.symoff + symbol_data_size, strtab_size);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000522
Walter Erquinigo344546b2016-10-17 20:28:19 +0000523 // First 4 bytes should be zeroed after strtab_size has been read,
524 // because it is used as offset 0 to encode a NULL string.
525 uint32_t *strtab_data_start = (uint32_t *)strtab_data.GetDataStart();
526 strtab_data_start[0] = 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000527
Walter Erquinigo344546b2016-10-17 20:28:19 +0000528 offset = 0;
529 std::string symbol_name;
530 Symbol *symbols = m_symtab_ap->Resize(num_syms);
531 for (uint32_t i = 0; i < num_syms; ++i) {
532 coff_symbol_t symbol;
533 const uint32_t symbol_offset = offset;
534 const char *symbol_name_cstr = NULL;
535 // If the first 4 bytes of the symbol string are zero, then they
536 // are followed by a 4-byte string table offset. Else these
537 // 8 bytes contain the symbol name
538 if (symtab_data.GetU32(&offset) == 0) {
539 // Long string that doesn't fit into the symbol table name,
540 // so now we must read the 4 byte string table offset
541 uint32_t strtab_offset = symtab_data.GetU32(&offset);
542 symbol_name_cstr = strtab_data.PeekCStr(strtab_offset);
543 symbol_name.assign(symbol_name_cstr);
544 } else {
545 // Short string that fits into the symbol table name which is 8
546 // bytes
547 offset += sizeof(symbol.name) - 4; // Skip remaining
548 symbol_name_cstr = symtab_data.PeekCStr(symbol_offset);
549 if (symbol_name_cstr == NULL)
550 break;
551 symbol_name.assign(symbol_name_cstr, sizeof(symbol.name));
552 }
553 symbol.value = symtab_data.GetU32(&offset);
554 symbol.sect = symtab_data.GetU16(&offset);
555 symbol.type = symtab_data.GetU16(&offset);
556 symbol.storage = symtab_data.GetU8(&offset);
557 symbol.naux = symtab_data.GetU8(&offset);
558 symbols[i].GetMangled().SetValue(ConstString(symbol_name.c_str()));
559 if ((int16_t)symbol.sect >= 1) {
560 Address symbol_addr(sect_list->GetSectionAtIndex(symbol.sect - 1),
561 symbol.value);
562 symbols[i].GetAddressRef() = symbol_addr;
563 symbols[i].SetType(MapSymbolType(symbol.type));
564 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000565
Walter Erquinigo344546b2016-10-17 20:28:19 +0000566 if (symbol.naux > 0) {
567 i += symbol.naux;
568 offset += symbol_size;
569 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000570 }
571 }
572 }
573
574 // Read export header
575 if (coff_data_dir_export_table < m_coff_header_opt.data_dirs.size() &&
576 m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmsize > 0 &&
577 m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmaddr > 0) {
578 export_directory_entry export_table;
579 uint32_t data_start =
580 m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmaddr;
Walter Erquinigo344546b2016-10-17 20:28:19 +0000581
582 uint32_t address_rva = data_start;
583 if (m_file) {
584 Address address(m_coff_header_opt.image_base + data_start, sect_list);
585 address_rva =
586 address.GetSection()->GetFileOffset() + address.GetOffset();
587 }
588 DataExtractor symtab_data =
589 ReadImageData(address_rva, m_coff_header_opt.data_dirs[0].vmsize);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000590 lldb::offset_t offset = 0;
591
592 // Read export_table header
593 export_table.characteristics = symtab_data.GetU32(&offset);
594 export_table.time_date_stamp = symtab_data.GetU32(&offset);
595 export_table.major_version = symtab_data.GetU16(&offset);
596 export_table.minor_version = symtab_data.GetU16(&offset);
597 export_table.name = symtab_data.GetU32(&offset);
598 export_table.base = symtab_data.GetU32(&offset);
599 export_table.number_of_functions = symtab_data.GetU32(&offset);
600 export_table.number_of_names = symtab_data.GetU32(&offset);
601 export_table.address_of_functions = symtab_data.GetU32(&offset);
602 export_table.address_of_names = symtab_data.GetU32(&offset);
603 export_table.address_of_name_ordinals = symtab_data.GetU32(&offset);
604
605 bool has_ordinal = export_table.address_of_name_ordinals != 0;
606
607 lldb::offset_t name_offset = export_table.address_of_names - data_start;
608 lldb::offset_t name_ordinal_offset =
609 export_table.address_of_name_ordinals - data_start;
610
611 Symbol *symbols = m_symtab_ap->Resize(export_table.number_of_names);
612
613 std::string symbol_name;
614
615 // Read each export table entry
616 for (size_t i = 0; i < export_table.number_of_names; ++i) {
617 uint32_t name_ordinal =
618 has_ordinal ? symtab_data.GetU16(&name_ordinal_offset) : i;
619 uint32_t name_address = symtab_data.GetU32(&name_offset);
620
621 const char *symbol_name_cstr =
622 symtab_data.PeekCStr(name_address - data_start);
623 symbol_name.assign(symbol_name_cstr);
624
625 lldb::offset_t function_offset = export_table.address_of_functions -
626 data_start +
627 sizeof(uint32_t) * name_ordinal;
628 uint32_t function_rva = symtab_data.GetU32(&function_offset);
629
630 Address symbol_addr(m_coff_header_opt.image_base + function_rva,
631 sect_list);
632 symbols[i].GetMangled().SetValue(ConstString(symbol_name.c_str()));
633 symbols[i].GetAddressRef() = symbol_addr;
634 symbols[i].SetType(lldb::eSymbolTypeCode);
635 symbols[i].SetDebug(true);
636 }
637 }
638 m_symtab_ap->CalculateSymbolSizes();
639 }
640 }
641 return m_symtab_ap.get();
642}
643
644bool ObjectFilePECOFF::IsStripped() {
645 // TODO: determine this for COFF
646 return false;
647}
648
649void ObjectFilePECOFF::CreateSections(SectionList &unified_section_list) {
650 if (!m_sections_ap.get()) {
651 m_sections_ap.reset(new SectionList());
652
Greg Claytona1743492012-03-13 23:14:29 +0000653 ModuleSP module_sp(GetModule());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000654 if (module_sp) {
655 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
656 const uint32_t nsects = m_sect_headers.size();
657 ModuleSP module_sp(GetModule());
658 for (uint32_t idx = 0; idx < nsects; ++idx) {
659 std::string sect_name;
660 GetSectionName(sect_name, m_sect_headers[idx]);
661 ConstString const_sect_name(sect_name.c_str());
662 static ConstString g_code_sect_name(".code");
663 static ConstString g_CODE_sect_name("CODE");
664 static ConstString g_data_sect_name(".data");
665 static ConstString g_DATA_sect_name("DATA");
666 static ConstString g_bss_sect_name(".bss");
667 static ConstString g_BSS_sect_name("BSS");
668 static ConstString g_debug_sect_name(".debug");
669 static ConstString g_reloc_sect_name(".reloc");
670 static ConstString g_stab_sect_name(".stab");
671 static ConstString g_stabstr_sect_name(".stabstr");
672 static ConstString g_sect_name_dwarf_debug_abbrev(".debug_abbrev");
673 static ConstString g_sect_name_dwarf_debug_aranges(".debug_aranges");
674 static ConstString g_sect_name_dwarf_debug_frame(".debug_frame");
675 static ConstString g_sect_name_dwarf_debug_info(".debug_info");
676 static ConstString g_sect_name_dwarf_debug_line(".debug_line");
677 static ConstString g_sect_name_dwarf_debug_loc(".debug_loc");
678 static ConstString g_sect_name_dwarf_debug_macinfo(".debug_macinfo");
679 static ConstString g_sect_name_dwarf_debug_pubnames(".debug_pubnames");
680 static ConstString g_sect_name_dwarf_debug_pubtypes(".debug_pubtypes");
681 static ConstString g_sect_name_dwarf_debug_ranges(".debug_ranges");
682 static ConstString g_sect_name_dwarf_debug_str(".debug_str");
683 static ConstString g_sect_name_eh_frame(".eh_frame");
684 static ConstString g_sect_name_go_symtab(".gosymtab");
685 SectionType section_type = eSectionTypeOther;
686 if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_CODE &&
687 ((const_sect_name == g_code_sect_name) ||
688 (const_sect_name == g_CODE_sect_name))) {
689 section_type = eSectionTypeCode;
690 } else if (m_sect_headers[idx].flags &
691 llvm::COFF::IMAGE_SCN_CNT_INITIALIZED_DATA &&
692 ((const_sect_name == g_data_sect_name) ||
693 (const_sect_name == g_DATA_sect_name))) {
694 section_type = eSectionTypeData;
695 } else if (m_sect_headers[idx].flags &
696 llvm::COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA &&
697 ((const_sect_name == g_bss_sect_name) ||
698 (const_sect_name == g_BSS_sect_name))) {
699 if (m_sect_headers[idx].size == 0)
700 section_type = eSectionTypeZeroFill;
701 else
702 section_type = eSectionTypeData;
703 } else if (const_sect_name == g_debug_sect_name) {
704 section_type = eSectionTypeDebug;
705 } else if (const_sect_name == g_stabstr_sect_name) {
706 section_type = eSectionTypeDataCString;
707 } else if (const_sect_name == g_reloc_sect_name) {
708 section_type = eSectionTypeOther;
709 } else if (const_sect_name == g_sect_name_dwarf_debug_abbrev)
710 section_type = eSectionTypeDWARFDebugAbbrev;
711 else if (const_sect_name == g_sect_name_dwarf_debug_aranges)
712 section_type = eSectionTypeDWARFDebugAranges;
713 else if (const_sect_name == g_sect_name_dwarf_debug_frame)
714 section_type = eSectionTypeDWARFDebugFrame;
715 else if (const_sect_name == g_sect_name_dwarf_debug_info)
716 section_type = eSectionTypeDWARFDebugInfo;
717 else if (const_sect_name == g_sect_name_dwarf_debug_line)
718 section_type = eSectionTypeDWARFDebugLine;
719 else if (const_sect_name == g_sect_name_dwarf_debug_loc)
720 section_type = eSectionTypeDWARFDebugLoc;
721 else if (const_sect_name == g_sect_name_dwarf_debug_macinfo)
722 section_type = eSectionTypeDWARFDebugMacInfo;
723 else if (const_sect_name == g_sect_name_dwarf_debug_pubnames)
724 section_type = eSectionTypeDWARFDebugPubNames;
725 else if (const_sect_name == g_sect_name_dwarf_debug_pubtypes)
726 section_type = eSectionTypeDWARFDebugPubTypes;
727 else if (const_sect_name == g_sect_name_dwarf_debug_ranges)
728 section_type = eSectionTypeDWARFDebugRanges;
729 else if (const_sect_name == g_sect_name_dwarf_debug_str)
730 section_type = eSectionTypeDWARFDebugStr;
731 else if (const_sect_name == g_sect_name_eh_frame)
732 section_type = eSectionTypeEHFrame;
733 else if (const_sect_name == g_sect_name_go_symtab)
734 section_type = eSectionTypeGoSymtab;
735 else if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_CODE) {
736 section_type = eSectionTypeCode;
737 } else if (m_sect_headers[idx].flags &
738 llvm::COFF::IMAGE_SCN_CNT_INITIALIZED_DATA) {
739 section_type = eSectionTypeData;
740 } else if (m_sect_headers[idx].flags &
741 llvm::COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) {
742 if (m_sect_headers[idx].size == 0)
743 section_type = eSectionTypeZeroFill;
744 else
745 section_type = eSectionTypeData;
Greg Claytonf754f882011-09-09 20:33:05 +0000746 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000747
748 // Use a segment ID of the segment index shifted left by 8 so they
749 // never conflict with any of the sections.
750 SectionSP section_sp(new Section(
751 module_sp, // Module to which this section belongs
752 this, // Object file to which this section belongs
753 idx + 1, // Section ID is the 1 based segment index shifted right by
754 // 8 bits as not to collide with any of the 256 section IDs
755 // that are possible
756 const_sect_name, // Name of this section
757 section_type, // This section is a container of other sections.
758 m_coff_header_opt.image_base +
759 m_sect_headers[idx].vmaddr, // File VM address == addresses as
760 // they are found in the object file
761 m_sect_headers[idx].vmsize, // VM size in bytes of this section
762 m_sect_headers[idx]
763 .offset, // Offset to the data for this section in the file
764 m_sect_headers[idx]
765 .size, // Size in bytes of this section as found in the file
766 m_coff_header_opt.sect_alignment, // Section alignment
767 m_sect_headers[idx].flags)); // Flags for this section
768
769 // section_sp->SetIsEncrypted (segment_is_encrypted);
770
771 unified_section_list.AddSection(section_sp);
772 m_sections_ap->AddSection(section_sp);
773 }
Greg Claytonf754f882011-09-09 20:33:05 +0000774 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000775 }
Greg Claytonf754f882011-09-09 20:33:05 +0000776}
777
Kate Stoneb9c1b512016-09-06 20:57:50 +0000778bool ObjectFilePECOFF::GetUUID(UUID *uuid) { return false; }
779
780uint32_t ObjectFilePECOFF::GetDependentModules(FileSpecList &files) {
781 return 0;
Greg Clayton3046e662013-07-10 01:23:25 +0000782}
783
Kate Stoneb9c1b512016-09-06 20:57:50 +0000784lldb_private::Address ObjectFilePECOFF::GetEntryPointAddress() {
785 if (m_entry_point_address.IsValid())
Stephane Sezer8e38c662016-03-23 18:00:13 +0000786 return m_entry_point_address;
Stephane Sezer8e38c662016-03-23 18:00:13 +0000787
Kate Stoneb9c1b512016-09-06 20:57:50 +0000788 if (!ParseHeader() || !IsExecutable())
789 return m_entry_point_address;
790
791 SectionList *section_list = GetSectionList();
792 addr_t offset = m_coff_header_opt.entry;
793
794 if (!section_list)
795 m_entry_point_address.SetOffset(offset);
796 else
797 m_entry_point_address.ResolveAddressUsingFileSections(offset, section_list);
798 return m_entry_point_address;
799}
Greg Claytonf754f882011-09-09 20:33:05 +0000800
801//----------------------------------------------------------------------
802// Dump
803//
804// Dump the specifics of the runtime file container (such as any headers
805// segments, sections, etc).
806//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000807void ObjectFilePECOFF::Dump(Stream *s) {
808 ModuleSP module_sp(GetModule());
809 if (module_sp) {
810 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
811 s->Printf("%p: ", static_cast<void *>(this));
812 s->Indent();
813 s->PutCString("ObjectFilePECOFF");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000814
Kate Stoneb9c1b512016-09-06 20:57:50 +0000815 ArchSpec header_arch;
816 GetArchitecture(header_arch);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000817
Kate Stoneb9c1b512016-09-06 20:57:50 +0000818 *s << ", file = '" << m_file
819 << "', arch = " << header_arch.GetArchitectureName() << "\n";
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000820
Kate Stoneb9c1b512016-09-06 20:57:50 +0000821 SectionList *sections = GetSectionList();
822 if (sections)
823 sections->Dump(s, NULL, true, UINT32_MAX);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000824
Kate Stoneb9c1b512016-09-06 20:57:50 +0000825 if (m_symtab_ap.get())
826 m_symtab_ap->Dump(s, NULL, eSortOrderNone);
Greg Claytona1743492012-03-13 23:14:29 +0000827
Kate Stoneb9c1b512016-09-06 20:57:50 +0000828 if (m_dos_header.e_magic)
829 DumpDOSHeader(s, m_dos_header);
830 if (m_coff_header.machine) {
831 DumpCOFFHeader(s, m_coff_header);
832 if (m_coff_header.hdrsize)
833 DumpOptCOFFHeader(s, m_coff_header_opt);
Greg Claytonf754f882011-09-09 20:33:05 +0000834 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000835 s->EOL();
836 DumpSectionHeaders(s);
837 s->EOL();
838 }
Greg Claytonf754f882011-09-09 20:33:05 +0000839}
840
841//----------------------------------------------------------------------
842// DumpDOSHeader
843//
844// Dump the MS-DOS header to the specified output stream
845//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000846void ObjectFilePECOFF::DumpDOSHeader(Stream *s, const dos_header_t &header) {
847 s->PutCString("MSDOS Header\n");
848 s->Printf(" e_magic = 0x%4.4x\n", header.e_magic);
849 s->Printf(" e_cblp = 0x%4.4x\n", header.e_cblp);
850 s->Printf(" e_cp = 0x%4.4x\n", header.e_cp);
851 s->Printf(" e_crlc = 0x%4.4x\n", header.e_crlc);
852 s->Printf(" e_cparhdr = 0x%4.4x\n", header.e_cparhdr);
853 s->Printf(" e_minalloc = 0x%4.4x\n", header.e_minalloc);
854 s->Printf(" e_maxalloc = 0x%4.4x\n", header.e_maxalloc);
855 s->Printf(" e_ss = 0x%4.4x\n", header.e_ss);
856 s->Printf(" e_sp = 0x%4.4x\n", header.e_sp);
857 s->Printf(" e_csum = 0x%4.4x\n", header.e_csum);
858 s->Printf(" e_ip = 0x%4.4x\n", header.e_ip);
859 s->Printf(" e_cs = 0x%4.4x\n", header.e_cs);
860 s->Printf(" e_lfarlc = 0x%4.4x\n", header.e_lfarlc);
861 s->Printf(" e_ovno = 0x%4.4x\n", header.e_ovno);
862 s->Printf(" e_res[4] = { 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x }\n",
863 header.e_res[0], header.e_res[1], header.e_res[2], header.e_res[3]);
864 s->Printf(" e_oemid = 0x%4.4x\n", header.e_oemid);
865 s->Printf(" e_oeminfo = 0x%4.4x\n", header.e_oeminfo);
866 s->Printf(" e_res2[10] = { 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, "
867 "0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x }\n",
868 header.e_res2[0], header.e_res2[1], header.e_res2[2],
869 header.e_res2[3], header.e_res2[4], header.e_res2[5],
870 header.e_res2[6], header.e_res2[7], header.e_res2[8],
871 header.e_res2[9]);
872 s->Printf(" e_lfanew = 0x%8.8x\n", header.e_lfanew);
Greg Claytonf754f882011-09-09 20:33:05 +0000873}
874
875//----------------------------------------------------------------------
876// DumpCOFFHeader
877//
878// Dump the COFF header to the specified output stream
879//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000880void ObjectFilePECOFF::DumpCOFFHeader(Stream *s, const coff_header_t &header) {
881 s->PutCString("COFF Header\n");
882 s->Printf(" machine = 0x%4.4x\n", header.machine);
883 s->Printf(" nsects = 0x%4.4x\n", header.nsects);
884 s->Printf(" modtime = 0x%8.8x\n", header.modtime);
885 s->Printf(" symoff = 0x%8.8x\n", header.symoff);
886 s->Printf(" nsyms = 0x%8.8x\n", header.nsyms);
887 s->Printf(" hdrsize = 0x%4.4x\n", header.hdrsize);
Greg Claytonf754f882011-09-09 20:33:05 +0000888}
889
890//----------------------------------------------------------------------
891// DumpOptCOFFHeader
892//
893// Dump the optional COFF header to the specified output stream
894//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000895void ObjectFilePECOFF::DumpOptCOFFHeader(Stream *s,
896 const coff_opt_header_t &header) {
897 s->PutCString("Optional COFF Header\n");
898 s->Printf(" magic = 0x%4.4x\n", header.magic);
899 s->Printf(" major_linker_version = 0x%2.2x\n",
900 header.major_linker_version);
901 s->Printf(" minor_linker_version = 0x%2.2x\n",
902 header.minor_linker_version);
903 s->Printf(" code_size = 0x%8.8x\n", header.code_size);
904 s->Printf(" data_size = 0x%8.8x\n", header.data_size);
905 s->Printf(" bss_size = 0x%8.8x\n", header.bss_size);
906 s->Printf(" entry = 0x%8.8x\n", header.entry);
907 s->Printf(" code_offset = 0x%8.8x\n", header.code_offset);
908 s->Printf(" data_offset = 0x%8.8x\n", header.data_offset);
909 s->Printf(" image_base = 0x%16.16" PRIx64 "\n",
910 header.image_base);
911 s->Printf(" sect_alignment = 0x%8.8x\n", header.sect_alignment);
912 s->Printf(" file_alignment = 0x%8.8x\n", header.file_alignment);
913 s->Printf(" major_os_system_version = 0x%4.4x\n",
914 header.major_os_system_version);
915 s->Printf(" minor_os_system_version = 0x%4.4x\n",
916 header.minor_os_system_version);
917 s->Printf(" major_image_version = 0x%4.4x\n",
918 header.major_image_version);
919 s->Printf(" minor_image_version = 0x%4.4x\n",
920 header.minor_image_version);
921 s->Printf(" major_subsystem_version = 0x%4.4x\n",
922 header.major_subsystem_version);
923 s->Printf(" minor_subsystem_version = 0x%4.4x\n",
924 header.minor_subsystem_version);
925 s->Printf(" reserved1 = 0x%8.8x\n", header.reserved1);
926 s->Printf(" image_size = 0x%8.8x\n", header.image_size);
927 s->Printf(" header_size = 0x%8.8x\n", header.header_size);
928 s->Printf(" checksum = 0x%8.8x\n", header.checksum);
929 s->Printf(" subsystem = 0x%4.4x\n", header.subsystem);
930 s->Printf(" dll_flags = 0x%4.4x\n", header.dll_flags);
931 s->Printf(" stack_reserve_size = 0x%16.16" PRIx64 "\n",
932 header.stack_reserve_size);
933 s->Printf(" stack_commit_size = 0x%16.16" PRIx64 "\n",
934 header.stack_commit_size);
935 s->Printf(" heap_reserve_size = 0x%16.16" PRIx64 "\n",
936 header.heap_reserve_size);
937 s->Printf(" heap_commit_size = 0x%16.16" PRIx64 "\n",
938 header.heap_commit_size);
939 s->Printf(" loader_flags = 0x%8.8x\n", header.loader_flags);
940 s->Printf(" num_data_dir_entries = 0x%8.8x\n",
941 (uint32_t)header.data_dirs.size());
942 uint32_t i;
943 for (i = 0; i < header.data_dirs.size(); i++) {
944 s->Printf(" data_dirs[%2u] vmaddr = 0x%8.8x, vmsize = 0x%8.8x\n", i,
945 header.data_dirs[i].vmaddr, header.data_dirs[i].vmsize);
946 }
Greg Claytonf754f882011-09-09 20:33:05 +0000947}
948//----------------------------------------------------------------------
949// DumpSectionHeader
950//
951// Dump a single ELF section header to the specified output stream
952//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000953void ObjectFilePECOFF::DumpSectionHeader(Stream *s,
954 const section_header_t &sh) {
955 std::string name;
956 GetSectionName(name, sh);
957 s->Printf("%-16s 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x 0x%4.4x "
958 "0x%4.4x 0x%8.8x\n",
959 name.c_str(), sh.vmaddr, sh.vmsize, sh.offset, sh.size, sh.reloff,
960 sh.lineoff, sh.nreloc, sh.nline, sh.flags);
Greg Claytonf754f882011-09-09 20:33:05 +0000961}
962
Greg Claytonf754f882011-09-09 20:33:05 +0000963//----------------------------------------------------------------------
964// DumpSectionHeaders
965//
966// Dump all of the ELF section header to the specified output stream
967//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000968void ObjectFilePECOFF::DumpSectionHeaders(Stream *s) {
969
970 s->PutCString("Section Headers\n");
971 s->PutCString("IDX name vm addr vm size file off file "
972 "size reloc off line off nreloc nline flags\n");
973 s->PutCString("==== ---------------- ---------- ---------- ---------- "
974 "---------- ---------- ---------- ------ ------ ----------\n");
975
976 uint32_t idx = 0;
977 SectionHeaderCollIter pos, end = m_sect_headers.end();
978
979 for (pos = m_sect_headers.begin(); pos != end; ++pos, ++idx) {
980 s->Printf("[%2u] ", idx);
981 ObjectFilePECOFF::DumpSectionHeader(s, *pos);
982 }
Greg Claytonf754f882011-09-09 20:33:05 +0000983}
984
Zachary Turnerfb3b3bd2016-09-20 20:44:50 +0000985bool ObjectFilePECOFF::IsWindowsSubsystem() {
986 switch (m_coff_header_opt.subsystem) {
987 case llvm::COFF::IMAGE_SUBSYSTEM_NATIVE:
988 case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_GUI:
989 case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_CUI:
990 case llvm::COFF::IMAGE_SUBSYSTEM_NATIVE_WINDOWS:
991 case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_CE_GUI:
992 case llvm::COFF::IMAGE_SUBSYSTEM_XBOX:
993 case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATION:
994 return true;
995 default:
996 return false;
997 }
998}
999
Kate Stoneb9c1b512016-09-06 20:57:50 +00001000bool ObjectFilePECOFF::GetArchitecture(ArchSpec &arch) {
1001 uint16_t machine = m_coff_header.machine;
1002 switch (machine) {
1003 case llvm::COFF::IMAGE_FILE_MACHINE_AMD64:
1004 case llvm::COFF::IMAGE_FILE_MACHINE_I386:
1005 case llvm::COFF::IMAGE_FILE_MACHINE_POWERPC:
1006 case llvm::COFF::IMAGE_FILE_MACHINE_POWERPCFP:
1007 case llvm::COFF::IMAGE_FILE_MACHINE_ARM:
1008 case llvm::COFF::IMAGE_FILE_MACHINE_ARMNT:
1009 case llvm::COFF::IMAGE_FILE_MACHINE_THUMB:
Zachary Turnerfb3b3bd2016-09-20 20:44:50 +00001010 arch.SetArchitecture(eArchTypeCOFF, machine, LLDB_INVALID_CPUTYPE,
1011 IsWindowsSubsystem() ? llvm::Triple::Win32
1012 : llvm::Triple::UnknownOS);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001013 return true;
1014 default:
1015 break;
1016 }
1017 return false;
Greg Claytonf754f882011-09-09 20:33:05 +00001018}
1019
Kate Stoneb9c1b512016-09-06 20:57:50 +00001020ObjectFile::Type ObjectFilePECOFF::CalculateType() {
1021 if (m_coff_header.machine != 0) {
1022 if ((m_coff_header.flags & llvm::COFF::IMAGE_FILE_DLL) == 0)
1023 return eTypeExecutable;
1024 else
1025 return eTypeSharedLibrary;
1026 }
1027 return eTypeExecutable;
Greg Claytonf754f882011-09-09 20:33:05 +00001028}
1029
Kate Stoneb9c1b512016-09-06 20:57:50 +00001030ObjectFile::Strata ObjectFilePECOFF::CalculateStrata() { return eStrataUser; }
Greg Claytonf754f882011-09-09 20:33:05 +00001031//------------------------------------------------------------------
1032// PluginInterface protocol
1033//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00001034ConstString ObjectFilePECOFF::GetPluginName() { return GetPluginNameStatic(); }
Greg Claytonf754f882011-09-09 20:33:05 +00001035
Kate Stoneb9c1b512016-09-06 20:57:50 +00001036uint32_t ObjectFilePECOFF::GetPluginVersion() { return 1; }