1 //===-- ObjectFilePECOFF.cpp ------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "ObjectFilePECOFF.h"
11 #include "WindowsMiniDump.h"
13 #include "llvm/Support/COFF.h"
15 #include "lldb/Core/ArchSpec.h"
16 #include "lldb/Core/DataBuffer.h"
17 #include "lldb/Core/DataBufferHeap.h"
18 #include "lldb/Core/FileSpecList.h"
19 #include "lldb/Core/Module.h"
20 #include "lldb/Core/ModuleSpec.h"
21 #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/Host/FileSpec.h"
28 #include "lldb/Symbol/ObjectFile.h"
29 #include "lldb/Target/Process.h"
30 #include "lldb/Target/SectionLoadList.h"
31 #include "lldb/Target/Target.h"
33 #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
39 using namespace lldb_private;
41 void ObjectFilePECOFF::Initialize() {
42 PluginManager::RegisterPlugin(
43 GetPluginNameStatic(), GetPluginDescriptionStatic(), CreateInstance,
44 CreateMemoryInstance, GetModuleSpecifications, SaveCore);
47 void ObjectFilePECOFF::Terminate() {
48 PluginManager::UnregisterPlugin(CreateInstance);
51 lldb_private::ConstString ObjectFilePECOFF::GetPluginNameStatic() {
52 static ConstString g_name("pe-coff");
56 const char *ObjectFilePECOFF::GetPluginDescriptionStatic() {
57 return "Portable Executable and Common Object File Format object file reader "
61 ObjectFile *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) {
68 data_sp = file->MemoryMapFileContentsIfLocal(file_offset, length);
72 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();
84 ObjectFile *ObjectFilePECOFF::CreateMemoryInstance(
85 const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp,
86 const lldb::ProcessSP &process_sp, lldb::addr_t header_addr) {
87 if (!data_sp || !ObjectFilePECOFF::MagicBytesMatch(data_sp))
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();
97 size_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();
103 if (ObjectFilePECOFF::MagicBytesMatch(data_sp)) {
105 data.SetData(data_sp, data_offset, length);
106 data.SetByteOrder(eByteOrderLittle);
108 dos_header_t dos_header;
109 coff_header_t coff_header;
111 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)
116 if (ParseCOFFHeader(data, &offset, coff_header)) {
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));
131 return specs.GetSize() - initial_count;
134 bool 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);
140 bool 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;
147 lldb::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;
155 return lldb::eSymbolTypeInvalid;
158 ObjectFilePECOFF::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));
172 ObjectFilePECOFF::ObjectFilePECOFF(const lldb::ModuleSP &module_sp,
173 DataBufferSP &header_data_sp,
174 const lldb::ProcessSP &process_sp,
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));
184 ObjectFilePECOFF::~ObjectFilePECOFF() {}
186 bool ObjectFilePECOFF::ParseHeader() {
187 ModuleSP module_sp(GetModule());
189 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
190 m_sect_headers.clear();
191 m_data.SetByteOrder(eByteOrderLittle);
192 lldb::offset_t offset = 0;
194 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)
199 if (ParseCOFFHeader(m_data, &offset, m_coff_header)) {
200 if (m_coff_header.hdrsize > 0)
201 ParseCOFFOptionalHeader(&offset);
202 ParseSectionHeaders(offset);
210 bool ObjectFilePECOFF::SetLoadAddress(Target &target, addr_t value,
211 bool value_is_offset) {
212 bool changed = false;
213 ModuleSP module_sp = GetModule();
215 size_t num_loaded_sections = 0;
216 SectionList *section_list = GetSectionList();
218 if (!value_is_offset) {
219 value -= m_image_base;
222 const size_t num_sections = section_list->GetSize();
225 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;
235 changed = num_loaded_sections > 0;
241 ByteOrder ObjectFilePECOFF::GetByteOrder() const { return eByteOrderLittle; }
243 bool ObjectFilePECOFF::IsExecutable() const {
244 return (m_coff_header.flags & llvm::COFF::IMAGE_FILE_DLL) == 0;
247 uint32_t ObjectFilePECOFF::GetAddressByteSize() const {
248 if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32_PLUS)
250 else if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32)
255 //----------------------------------------------------------------------
258 // Return true if an endian swap needs to occur when extracting data
260 //----------------------------------------------------------------------
261 bool ObjectFilePECOFF::NeedsEndianSwap() const {
262 #if defined(__LITTLE_ENDIAN__)
268 //----------------------------------------------------------------------
270 //----------------------------------------------------------------------
271 bool 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));
278 dos_header.e_magic = data.GetU16(&offset); // Magic number
279 success = dos_header.e_magic == IMAGE_DOS_SIGNATURE;
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
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
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
320 dos_header.e_lfanew =
321 data.GetU32(&offset); // File address of new exe header
325 memset(&dos_header, 0, sizeof(dos_header));
329 //----------------------------------------------------------------------
331 //----------------------------------------------------------------------
332 bool ObjectFilePECOFF::ParseCOFFHeader(DataExtractor &data,
333 lldb::offset_t *offset_ptr,
334 coff_header_t &coff_header) {
336 data.ValidOffsetForDataOfSize(*offset_ptr, sizeof(coff_header));
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);
347 memset(&coff_header, 0, sizeof(coff_header));
351 bool 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) {
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);
365 const uint32_t addr_byte_size = GetAddressByteSize();
367 if (*offset_ptr < end_offset) {
368 if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32) {
370 m_coff_header_opt.data_offset = m_data.GetU32(offset_ptr);
372 m_coff_header_opt.data_offset = 0;
374 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);
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);
409 m_file_offset = m_coff_header_opt.image_base;
410 m_image_base = m_coff_header_opt.image_base;
414 // Make sure we are on track for section data which follows
415 *offset_ptr = end_offset;
419 DataExtractor ObjectFilePECOFF::ReadImageData(uint32_t offset, size_t size) {
421 DataBufferSP buffer_sp(m_file.ReadFileContents(offset, size));
422 return DataExtractor(buffer_sp, GetByteOrder(), GetAddressByteSize());
424 ProcessSP process_sp(m_process_wp.lock());
427 auto data_ap = llvm::make_unique<DataBufferHeap>(size, 0);
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());
440 //----------------------------------------------------------------------
441 // ParseSectionHeaders
442 //----------------------------------------------------------------------
443 bool ObjectFilePECOFF::ParseSectionHeaders(
444 uint32_t section_header_data_offset) {
445 const uint32_t nsects = m_coff_header.nsects;
446 m_sect_headers.clear();
449 const size_t section_header_byte_size = nsects * sizeof(section_header_t);
450 DataExtractor section_header_data =
451 ReadImageData(section_header_data_offset, section_header_byte_size);
453 lldb::offset_t offset = 0;
454 if (section_header_data.ValidOffsetForDataOfSize(
455 offset, section_header_byte_size)) {
456 m_sect_headers.resize(nsects);
458 for (uint32_t idx = 0; idx < nsects; ++idx) {
459 const void *name_data = section_header_data.GetData(&offset, 8);
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);
476 return m_sect_headers.empty() == false;
479 bool ObjectFilePECOFF::GetSectionName(std::string §_name,
480 const section_header_t §) {
481 if (sect.name[0] == '/') {
482 lldb::offset_t stroff = strtoul(§.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);
493 sect_name = sect.name;
497 //----------------------------------------------------------------------
499 //----------------------------------------------------------------------
500 Symtab *ObjectFilePECOFF::GetSymtab() {
501 ModuleSP module_sp(GetModule());
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());
509 const uint32_t num_syms = m_coff_header.nsyms;
511 if (m_file && num_syms > 0 && m_coff_header.symoff > 0) {
512 const uint32_t symbol_size = 18;
513 const size_t symbol_data_size = num_syms * symbol_size;
514 // Include the 4-byte string table size at the end of the symbols
515 DataExtractor symtab_data =
516 ReadImageData(m_coff_header.symoff, symbol_data_size + 4);
517 lldb::offset_t offset = symbol_data_size;
518 const uint32_t strtab_size = symtab_data.GetU32(&offset);
519 if (strtab_size > 0) {
520 DataExtractor strtab_data = ReadImageData(
521 m_coff_header.symoff + symbol_data_size, strtab_size);
523 // 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;
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);
545 // Short string that fits into the symbol table name which is 8
547 offset += sizeof(symbol.name) - 4; // Skip remaining
548 symbol_name_cstr = symtab_data.PeekCStr(symbol_offset);
549 if (symbol_name_cstr == NULL)
551 symbol_name.assign(symbol_name_cstr, sizeof(symbol.name));
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),
562 symbols[i].GetAddressRef() = symbol_addr;
563 symbols[i].SetType(MapSymbolType(symbol.type));
566 if (symbol.naux > 0) {
568 offset += symbol_size;
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;
582 uint32_t address_rva = data_start;
584 Address address(m_coff_header_opt.image_base + data_start, sect_list);
586 address.GetSection()->GetFileOffset() + address.GetOffset();
588 DataExtractor symtab_data =
589 ReadImageData(address_rva, m_coff_header_opt.data_dirs[0].vmsize);
590 lldb::offset_t offset = 0;
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);
605 bool has_ordinal = export_table.address_of_name_ordinals != 0;
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;
611 Symbol *symbols = m_symtab_ap->Resize(export_table.number_of_names);
613 std::string symbol_name;
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);
621 const char *symbol_name_cstr =
622 symtab_data.PeekCStr(name_address - data_start);
623 symbol_name.assign(symbol_name_cstr);
625 lldb::offset_t function_offset = export_table.address_of_functions -
627 sizeof(uint32_t) * name_ordinal;
628 uint32_t function_rva = symtab_data.GetU32(&function_offset);
630 Address symbol_addr(m_coff_header_opt.image_base + function_rva,
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);
638 m_symtab_ap->CalculateSymbolSizes();
641 return m_symtab_ap.get();
644 bool ObjectFilePECOFF::IsStripped() {
645 // TODO: determine this for COFF
649 void ObjectFilePECOFF::CreateSections(SectionList &unified_section_list) {
650 if (!m_sections_ap.get()) {
651 m_sections_ap.reset(new SectionList());
653 ModuleSP module_sp(GetModule());
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;
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;
745 section_type = eSectionTypeData;
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
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
763 .offset, // Offset to the data for this section in the file
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
769 // section_sp->SetIsEncrypted (segment_is_encrypted);
771 unified_section_list.AddSection(section_sp);
772 m_sections_ap->AddSection(section_sp);
778 bool ObjectFilePECOFF::GetUUID(UUID *uuid) { return false; }
780 uint32_t ObjectFilePECOFF::GetDependentModules(FileSpecList &files) {
784 lldb_private::Address ObjectFilePECOFF::GetEntryPointAddress() {
785 if (m_entry_point_address.IsValid())
786 return m_entry_point_address;
788 if (!ParseHeader() || !IsExecutable())
789 return m_entry_point_address;
791 SectionList *section_list = GetSectionList();
792 addr_t offset = m_coff_header_opt.entry;
795 m_entry_point_address.SetOffset(offset);
797 m_entry_point_address.ResolveAddressUsingFileSections(offset, section_list);
798 return m_entry_point_address;
801 //----------------------------------------------------------------------
804 // Dump the specifics of the runtime file container (such as any headers
805 // segments, sections, etc).
806 //----------------------------------------------------------------------
807 void ObjectFilePECOFF::Dump(Stream *s) {
808 ModuleSP module_sp(GetModule());
810 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
811 s->Printf("%p: ", static_cast<void *>(this));
813 s->PutCString("ObjectFilePECOFF");
815 ArchSpec header_arch;
816 GetArchitecture(header_arch);
818 *s << ", file = '" << m_file
819 << "', arch = " << header_arch.GetArchitectureName() << "\n";
821 SectionList *sections = GetSectionList();
823 sections->Dump(s, NULL, true, UINT32_MAX);
825 if (m_symtab_ap.get())
826 m_symtab_ap->Dump(s, NULL, eSortOrderNone);
828 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);
836 DumpSectionHeaders(s);
841 //----------------------------------------------------------------------
844 // Dump the MS-DOS header to the specified output stream
845 //----------------------------------------------------------------------
846 void 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],
872 s->Printf(" e_lfanew = 0x%8.8x\n", header.e_lfanew);
875 //----------------------------------------------------------------------
878 // Dump the COFF header to the specified output stream
879 //----------------------------------------------------------------------
880 void 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);
890 //----------------------------------------------------------------------
893 // Dump the optional COFF header to the specified output stream
894 //----------------------------------------------------------------------
895 void 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",
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());
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);
948 //----------------------------------------------------------------------
951 // Dump a single ELF section header to the specified output stream
952 //----------------------------------------------------------------------
953 void ObjectFilePECOFF::DumpSectionHeader(Stream *s,
954 const section_header_t &sh) {
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 "
959 name.c_str(), sh.vmaddr, sh.vmsize, sh.offset, sh.size, sh.reloff,
960 sh.lineoff, sh.nreloc, sh.nline, sh.flags);
963 //----------------------------------------------------------------------
964 // DumpSectionHeaders
966 // Dump all of the ELF section header to the specified output stream
967 //----------------------------------------------------------------------
968 void ObjectFilePECOFF::DumpSectionHeaders(Stream *s) {
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");
977 SectionHeaderCollIter pos, end = m_sect_headers.end();
979 for (pos = m_sect_headers.begin(); pos != end; ++pos, ++idx) {
980 s->Printf("[%2u] ", idx);
981 ObjectFilePECOFF::DumpSectionHeader(s, *pos);
985 bool 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:
1000 bool ObjectFilePECOFF::GetArchitecture(ArchSpec &arch) {
1001 uint16_t machine = m_coff_header.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:
1010 arch.SetArchitecture(eArchTypeCOFF, machine, LLDB_INVALID_CPUTYPE,
1011 IsWindowsSubsystem() ? llvm::Triple::Win32
1012 : llvm::Triple::UnknownOS);
1020 ObjectFile::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;
1025 return eTypeSharedLibrary;
1027 return eTypeExecutable;
1030 ObjectFile::Strata ObjectFilePECOFF::CalculateStrata() { return eStrataUser; }
1031 //------------------------------------------------------------------
1032 // PluginInterface protocol
1033 //------------------------------------------------------------------
1034 ConstString ObjectFilePECOFF::GetPluginName() { return GetPluginNameStatic(); }
1036 uint32_t ObjectFilePECOFF::GetPluginVersion() { return 1; }