1 //===-- ObjectFilePECOFF.cpp ------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "ObjectFilePECOFF.h" 11 #include "WindowsMiniDump.h" 12 13 #include "lldb/Core/FileSpecList.h" 14 #include "lldb/Core/Module.h" 15 #include "lldb/Core/ModuleSpec.h" 16 #include "lldb/Core/PluginManager.h" 17 #include "lldb/Core/Section.h" 18 #include "lldb/Core/StreamFile.h" 19 #include "lldb/Symbol/ObjectFile.h" 20 #include "lldb/Target/Process.h" 21 #include "lldb/Target/SectionLoadList.h" 22 #include "lldb/Target/Target.h" 23 #include "lldb/Utility/ArchSpec.h" 24 #include "lldb/Utility/DataBufferHeap.h" 25 #include "lldb/Utility/FileSpec.h" 26 #include "lldb/Utility/Log.h" 27 #include "lldb/Utility/StreamString.h" 28 #include "lldb/Utility/Timer.h" 29 #include "lldb/Utility/UUID.h" 30 #include "llvm/BinaryFormat/COFF.h" 31 32 #include "llvm/Object/COFFImportFile.h" 33 #include "llvm/Support/Error.h" 34 #include "llvm/Support/MemoryBuffer.h" 35 36 #define IMAGE_DOS_SIGNATURE 0x5A4D // MZ 37 #define IMAGE_NT_SIGNATURE 0x00004550 // PE00 38 #define OPT_HEADER_MAGIC_PE32 0x010b 39 #define OPT_HEADER_MAGIC_PE32_PLUS 0x020b 40 41 using namespace lldb; 42 using namespace lldb_private; 43 44 void ObjectFilePECOFF::Initialize() { 45 PluginManager::RegisterPlugin( 46 GetPluginNameStatic(), GetPluginDescriptionStatic(), CreateInstance, 47 CreateMemoryInstance, GetModuleSpecifications, SaveCore); 48 } 49 50 void ObjectFilePECOFF::Terminate() { 51 PluginManager::UnregisterPlugin(CreateInstance); 52 } 53 54 lldb_private::ConstString ObjectFilePECOFF::GetPluginNameStatic() { 55 static ConstString g_name("pe-coff"); 56 return g_name; 57 } 58 59 const char *ObjectFilePECOFF::GetPluginDescriptionStatic() { 60 return "Portable Executable and Common Object File Format object file reader " 61 "(32 and 64 bit)"; 62 } 63 64 ObjectFile *ObjectFilePECOFF::CreateInstance(const lldb::ModuleSP &module_sp, 65 DataBufferSP &data_sp, 66 lldb::offset_t data_offset, 67 const lldb_private::FileSpec *file, 68 lldb::offset_t file_offset, 69 lldb::offset_t length) { 70 if (!data_sp) { 71 data_sp = MapFileData(file, length, file_offset); 72 if (!data_sp) 73 return nullptr; 74 data_offset = 0; 75 } 76 77 if (!ObjectFilePECOFF::MagicBytesMatch(data_sp)) 78 return nullptr; 79 80 // Update the data to contain the entire file if it doesn't already 81 if (data_sp->GetByteSize() < length) { 82 data_sp = MapFileData(file, length, file_offset); 83 if (!data_sp) 84 return nullptr; 85 } 86 87 auto objfile_ap = llvm::make_unique<ObjectFilePECOFF>( 88 module_sp, data_sp, data_offset, file, file_offset, length); 89 if (!objfile_ap || !objfile_ap->ParseHeader()) 90 return nullptr; 91 92 // Cache coff binary. 93 if (!objfile_ap->CreateBinary()) 94 return nullptr; 95 96 return objfile_ap.release(); 97 } 98 99 ObjectFile *ObjectFilePECOFF::CreateMemoryInstance( 100 const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp, 101 const lldb::ProcessSP &process_sp, lldb::addr_t header_addr) { 102 if (!data_sp || !ObjectFilePECOFF::MagicBytesMatch(data_sp)) 103 return nullptr; 104 auto objfile_ap = llvm::make_unique<ObjectFilePECOFF>( 105 module_sp, data_sp, process_sp, header_addr); 106 if (objfile_ap.get() && objfile_ap->ParseHeader()) { 107 return objfile_ap.release(); 108 } 109 return nullptr; 110 } 111 112 size_t ObjectFilePECOFF::GetModuleSpecifications( 113 const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp, 114 lldb::offset_t data_offset, lldb::offset_t file_offset, 115 lldb::offset_t length, lldb_private::ModuleSpecList &specs) { 116 const size_t initial_count = specs.GetSize(); 117 118 if (ObjectFilePECOFF::MagicBytesMatch(data_sp)) { 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 lldb::offset_t offset = dos_header.e_lfanew; 128 uint32_t pe_signature = data.GetU32(&offset); 129 if (pe_signature != IMAGE_NT_SIGNATURE) 130 return false; 131 if (ParseCOFFHeader(data, &offset, coff_header)) { 132 ArchSpec spec; 133 if (coff_header.machine == MachineAmd64) { 134 spec.SetTriple("x86_64-pc-windows"); 135 specs.Append(ModuleSpec(file, spec)); 136 } else if (coff_header.machine == MachineX86) { 137 spec.SetTriple("i386-pc-windows"); 138 specs.Append(ModuleSpec(file, spec)); 139 spec.SetTriple("i686-pc-windows"); 140 specs.Append(ModuleSpec(file, spec)); 141 } else if (coff_header.machine == MachineArmNt) { 142 spec.SetTriple("arm-pc-windows"); 143 specs.Append(ModuleSpec(file, spec)); 144 } 145 } 146 } 147 } 148 149 return specs.GetSize() - initial_count; 150 } 151 152 bool ObjectFilePECOFF::SaveCore(const lldb::ProcessSP &process_sp, 153 const lldb_private::FileSpec &outfile, 154 lldb_private::Status &error) { 155 return SaveMiniDump(process_sp, outfile, error); 156 } 157 158 bool ObjectFilePECOFF::MagicBytesMatch(DataBufferSP &data_sp) { 159 DataExtractor data(data_sp, eByteOrderLittle, 4); 160 lldb::offset_t offset = 0; 161 uint16_t magic = data.GetU16(&offset); 162 return magic == IMAGE_DOS_SIGNATURE; 163 } 164 165 lldb::SymbolType ObjectFilePECOFF::MapSymbolType(uint16_t coff_symbol_type) { 166 // TODO: We need to complete this mapping of COFF symbol types to LLDB ones. 167 // For now, here's a hack to make sure our function have types. 168 const auto complex_type = 169 coff_symbol_type >> llvm::COFF::SCT_COMPLEX_TYPE_SHIFT; 170 if (complex_type == llvm::COFF::IMAGE_SYM_DTYPE_FUNCTION) { 171 return lldb::eSymbolTypeCode; 172 } 173 return lldb::eSymbolTypeInvalid; 174 } 175 176 bool ObjectFilePECOFF::CreateBinary() { 177 if (m_owningbin) 178 return true; 179 180 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT)); 181 182 auto binary = llvm::object::createBinary(m_file.GetPath()); 183 if (!binary) { 184 if (log) 185 log->Printf("ObjectFilePECOFF::CreateBinary() - failed to create binary " 186 "for file (%s): %s", 187 m_file ? m_file.GetPath().c_str() : "<NULL>", 188 errorToErrorCode(binary.takeError()).message().c_str()); 189 return false; 190 } 191 192 // Make sure we only handle COFF format. 193 if (!binary->getBinary()->isCOFF() && 194 !binary->getBinary()->isCOFFImportFile()) 195 return false; 196 197 m_owningbin = OWNBINType(std::move(*binary)); 198 if (log) 199 log->Printf("%p ObjectFilePECOFF::CreateBinary() module = %p (%s), file = " 200 "%s, binary = %p (Bin = %p)", 201 static_cast<void *>(this), 202 static_cast<void *>(GetModule().get()), 203 GetModule()->GetSpecificationDescription().c_str(), 204 m_file ? m_file.GetPath().c_str() : "<NULL>", 205 static_cast<void *>(m_owningbin.getPointer()), 206 static_cast<void *>(m_owningbin->getBinary())); 207 return true; 208 } 209 210 ObjectFilePECOFF::ObjectFilePECOFF(const lldb::ModuleSP &module_sp, 211 DataBufferSP &data_sp, 212 lldb::offset_t data_offset, 213 const FileSpec *file, 214 lldb::offset_t file_offset, 215 lldb::offset_t length) 216 : ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset), 217 m_dos_header(), m_coff_header(), m_coff_header_opt(), m_sect_headers(), 218 m_entry_point_address(), m_deps_filespec(), m_owningbin() { 219 ::memset(&m_dos_header, 0, sizeof(m_dos_header)); 220 ::memset(&m_coff_header, 0, sizeof(m_coff_header)); 221 ::memset(&m_coff_header_opt, 0, sizeof(m_coff_header_opt)); 222 } 223 224 ObjectFilePECOFF::ObjectFilePECOFF(const lldb::ModuleSP &module_sp, 225 DataBufferSP &header_data_sp, 226 const lldb::ProcessSP &process_sp, 227 addr_t header_addr) 228 : ObjectFile(module_sp, process_sp, header_addr, header_data_sp), 229 m_dos_header(), m_coff_header(), m_coff_header_opt(), m_sect_headers(), 230 m_entry_point_address(), m_deps_filespec(), m_owningbin() { 231 ::memset(&m_dos_header, 0, sizeof(m_dos_header)); 232 ::memset(&m_coff_header, 0, sizeof(m_coff_header)); 233 ::memset(&m_coff_header_opt, 0, sizeof(m_coff_header_opt)); 234 } 235 236 ObjectFilePECOFF::~ObjectFilePECOFF() {} 237 238 bool ObjectFilePECOFF::ParseHeader() { 239 ModuleSP module_sp(GetModule()); 240 if (module_sp) { 241 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 242 m_sect_headers.clear(); 243 m_data.SetByteOrder(eByteOrderLittle); 244 lldb::offset_t offset = 0; 245 246 if (ParseDOSHeader(m_data, m_dos_header)) { 247 offset = m_dos_header.e_lfanew; 248 uint32_t pe_signature = m_data.GetU32(&offset); 249 if (pe_signature != IMAGE_NT_SIGNATURE) 250 return false; 251 if (ParseCOFFHeader(m_data, &offset, m_coff_header)) { 252 if (m_coff_header.hdrsize > 0) 253 ParseCOFFOptionalHeader(&offset); 254 ParseSectionHeaders(offset); 255 } 256 return true; 257 } 258 } 259 return false; 260 } 261 262 bool ObjectFilePECOFF::SetLoadAddress(Target &target, addr_t value, 263 bool value_is_offset) { 264 bool changed = false; 265 ModuleSP module_sp = GetModule(); 266 if (module_sp) { 267 size_t num_loaded_sections = 0; 268 SectionList *section_list = GetSectionList(); 269 if (section_list) { 270 if (!value_is_offset) { 271 value -= m_image_base; 272 } 273 274 const size_t num_sections = section_list->GetSize(); 275 size_t sect_idx = 0; 276 277 for (sect_idx = 0; sect_idx < num_sections; ++sect_idx) { 278 // Iterate through the object file sections to find all of the sections 279 // that have SHF_ALLOC in their flag bits. 280 SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx)); 281 if (section_sp && !section_sp->IsThreadSpecific()) { 282 if (target.GetSectionLoadList().SetSectionLoadAddress( 283 section_sp, section_sp->GetFileAddress() + value)) 284 ++num_loaded_sections; 285 } 286 } 287 changed = num_loaded_sections > 0; 288 } 289 } 290 return changed; 291 } 292 293 ByteOrder ObjectFilePECOFF::GetByteOrder() const { return eByteOrderLittle; } 294 295 bool ObjectFilePECOFF::IsExecutable() const { 296 return (m_coff_header.flags & llvm::COFF::IMAGE_FILE_DLL) == 0; 297 } 298 299 uint32_t ObjectFilePECOFF::GetAddressByteSize() const { 300 if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32_PLUS) 301 return 8; 302 else if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32) 303 return 4; 304 return 4; 305 } 306 307 //---------------------------------------------------------------------- 308 // NeedsEndianSwap 309 // 310 // Return true if an endian swap needs to occur when extracting data from this 311 // file. 312 //---------------------------------------------------------------------- 313 bool ObjectFilePECOFF::NeedsEndianSwap() const { 314 #if defined(__LITTLE_ENDIAN__) 315 return false; 316 #else 317 return true; 318 #endif 319 } 320 //---------------------------------------------------------------------- 321 // ParseDOSHeader 322 //---------------------------------------------------------------------- 323 bool ObjectFilePECOFF::ParseDOSHeader(DataExtractor &data, 324 dos_header_t &dos_header) { 325 bool success = false; 326 lldb::offset_t offset = 0; 327 success = data.ValidOffsetForDataOfSize(0, sizeof(dos_header)); 328 329 if (success) { 330 dos_header.e_magic = data.GetU16(&offset); // Magic number 331 success = dos_header.e_magic == IMAGE_DOS_SIGNATURE; 332 333 if (success) { 334 dos_header.e_cblp = data.GetU16(&offset); // Bytes on last page of file 335 dos_header.e_cp = data.GetU16(&offset); // Pages in file 336 dos_header.e_crlc = data.GetU16(&offset); // Relocations 337 dos_header.e_cparhdr = 338 data.GetU16(&offset); // Size of header in paragraphs 339 dos_header.e_minalloc = 340 data.GetU16(&offset); // Minimum extra paragraphs needed 341 dos_header.e_maxalloc = 342 data.GetU16(&offset); // Maximum extra paragraphs needed 343 dos_header.e_ss = data.GetU16(&offset); // Initial (relative) SS value 344 dos_header.e_sp = data.GetU16(&offset); // Initial SP value 345 dos_header.e_csum = data.GetU16(&offset); // Checksum 346 dos_header.e_ip = data.GetU16(&offset); // Initial IP value 347 dos_header.e_cs = data.GetU16(&offset); // Initial (relative) CS value 348 dos_header.e_lfarlc = 349 data.GetU16(&offset); // File address of relocation table 350 dos_header.e_ovno = data.GetU16(&offset); // Overlay number 351 352 dos_header.e_res[0] = data.GetU16(&offset); // Reserved words 353 dos_header.e_res[1] = data.GetU16(&offset); // Reserved words 354 dos_header.e_res[2] = data.GetU16(&offset); // Reserved words 355 dos_header.e_res[3] = data.GetU16(&offset); // Reserved words 356 357 dos_header.e_oemid = 358 data.GetU16(&offset); // OEM identifier (for e_oeminfo) 359 dos_header.e_oeminfo = 360 data.GetU16(&offset); // OEM information; e_oemid specific 361 dos_header.e_res2[0] = data.GetU16(&offset); // Reserved words 362 dos_header.e_res2[1] = data.GetU16(&offset); // Reserved words 363 dos_header.e_res2[2] = data.GetU16(&offset); // Reserved words 364 dos_header.e_res2[3] = data.GetU16(&offset); // Reserved words 365 dos_header.e_res2[4] = data.GetU16(&offset); // Reserved words 366 dos_header.e_res2[5] = data.GetU16(&offset); // Reserved words 367 dos_header.e_res2[6] = data.GetU16(&offset); // Reserved words 368 dos_header.e_res2[7] = data.GetU16(&offset); // Reserved words 369 dos_header.e_res2[8] = data.GetU16(&offset); // Reserved words 370 dos_header.e_res2[9] = data.GetU16(&offset); // Reserved words 371 372 dos_header.e_lfanew = 373 data.GetU32(&offset); // File address of new exe header 374 } 375 } 376 if (!success) 377 memset(&dos_header, 0, sizeof(dos_header)); 378 return success; 379 } 380 381 //---------------------------------------------------------------------- 382 // ParserCOFFHeader 383 //---------------------------------------------------------------------- 384 bool ObjectFilePECOFF::ParseCOFFHeader(DataExtractor &data, 385 lldb::offset_t *offset_ptr, 386 coff_header_t &coff_header) { 387 bool success = 388 data.ValidOffsetForDataOfSize(*offset_ptr, sizeof(coff_header)); 389 if (success) { 390 coff_header.machine = data.GetU16(offset_ptr); 391 coff_header.nsects = data.GetU16(offset_ptr); 392 coff_header.modtime = data.GetU32(offset_ptr); 393 coff_header.symoff = data.GetU32(offset_ptr); 394 coff_header.nsyms = data.GetU32(offset_ptr); 395 coff_header.hdrsize = data.GetU16(offset_ptr); 396 coff_header.flags = data.GetU16(offset_ptr); 397 } 398 if (!success) 399 memset(&coff_header, 0, sizeof(coff_header)); 400 return success; 401 } 402 403 bool ObjectFilePECOFF::ParseCOFFOptionalHeader(lldb::offset_t *offset_ptr) { 404 bool success = false; 405 const lldb::offset_t end_offset = *offset_ptr + m_coff_header.hdrsize; 406 if (*offset_ptr < end_offset) { 407 success = true; 408 m_coff_header_opt.magic = m_data.GetU16(offset_ptr); 409 m_coff_header_opt.major_linker_version = m_data.GetU8(offset_ptr); 410 m_coff_header_opt.minor_linker_version = m_data.GetU8(offset_ptr); 411 m_coff_header_opt.code_size = m_data.GetU32(offset_ptr); 412 m_coff_header_opt.data_size = m_data.GetU32(offset_ptr); 413 m_coff_header_opt.bss_size = m_data.GetU32(offset_ptr); 414 m_coff_header_opt.entry = m_data.GetU32(offset_ptr); 415 m_coff_header_opt.code_offset = m_data.GetU32(offset_ptr); 416 417 const uint32_t addr_byte_size = GetAddressByteSize(); 418 419 if (*offset_ptr < end_offset) { 420 if (m_coff_header_opt.magic == OPT_HEADER_MAGIC_PE32) { 421 // PE32 only 422 m_coff_header_opt.data_offset = m_data.GetU32(offset_ptr); 423 } else 424 m_coff_header_opt.data_offset = 0; 425 426 if (*offset_ptr < end_offset) { 427 m_coff_header_opt.image_base = 428 m_data.GetMaxU64(offset_ptr, addr_byte_size); 429 m_coff_header_opt.sect_alignment = m_data.GetU32(offset_ptr); 430 m_coff_header_opt.file_alignment = m_data.GetU32(offset_ptr); 431 m_coff_header_opt.major_os_system_version = m_data.GetU16(offset_ptr); 432 m_coff_header_opt.minor_os_system_version = m_data.GetU16(offset_ptr); 433 m_coff_header_opt.major_image_version = m_data.GetU16(offset_ptr); 434 m_coff_header_opt.minor_image_version = m_data.GetU16(offset_ptr); 435 m_coff_header_opt.major_subsystem_version = m_data.GetU16(offset_ptr); 436 m_coff_header_opt.minor_subsystem_version = m_data.GetU16(offset_ptr); 437 m_coff_header_opt.reserved1 = m_data.GetU32(offset_ptr); 438 m_coff_header_opt.image_size = m_data.GetU32(offset_ptr); 439 m_coff_header_opt.header_size = m_data.GetU32(offset_ptr); 440 m_coff_header_opt.checksum = m_data.GetU32(offset_ptr); 441 m_coff_header_opt.subsystem = m_data.GetU16(offset_ptr); 442 m_coff_header_opt.dll_flags = m_data.GetU16(offset_ptr); 443 m_coff_header_opt.stack_reserve_size = 444 m_data.GetMaxU64(offset_ptr, addr_byte_size); 445 m_coff_header_opt.stack_commit_size = 446 m_data.GetMaxU64(offset_ptr, addr_byte_size); 447 m_coff_header_opt.heap_reserve_size = 448 m_data.GetMaxU64(offset_ptr, addr_byte_size); 449 m_coff_header_opt.heap_commit_size = 450 m_data.GetMaxU64(offset_ptr, addr_byte_size); 451 m_coff_header_opt.loader_flags = m_data.GetU32(offset_ptr); 452 uint32_t num_data_dir_entries = m_data.GetU32(offset_ptr); 453 m_coff_header_opt.data_dirs.clear(); 454 m_coff_header_opt.data_dirs.resize(num_data_dir_entries); 455 uint32_t i; 456 for (i = 0; i < num_data_dir_entries; i++) { 457 m_coff_header_opt.data_dirs[i].vmaddr = m_data.GetU32(offset_ptr); 458 m_coff_header_opt.data_dirs[i].vmsize = m_data.GetU32(offset_ptr); 459 } 460 461 m_file_offset = m_coff_header_opt.image_base; 462 m_image_base = m_coff_header_opt.image_base; 463 } 464 } 465 } 466 // Make sure we are on track for section data which follows 467 *offset_ptr = end_offset; 468 return success; 469 } 470 471 DataExtractor ObjectFilePECOFF::ReadImageData(uint32_t offset, size_t size) { 472 if (m_file) { 473 // A bit of a hack, but we intend to write to this buffer, so we can't 474 // mmap it. 475 auto buffer_sp = MapFileData(m_file, size, offset); 476 return DataExtractor(buffer_sp, GetByteOrder(), GetAddressByteSize()); 477 } 478 ProcessSP process_sp(m_process_wp.lock()); 479 DataExtractor data; 480 if (process_sp) { 481 auto data_ap = llvm::make_unique<DataBufferHeap>(size, 0); 482 Status readmem_error; 483 size_t bytes_read = 484 process_sp->ReadMemory(m_image_base + offset, data_ap->GetBytes(), 485 data_ap->GetByteSize(), readmem_error); 486 if (bytes_read == size) { 487 DataBufferSP buffer_sp(data_ap.release()); 488 data.SetData(buffer_sp, 0, buffer_sp->GetByteSize()); 489 } 490 } 491 return data; 492 } 493 494 //---------------------------------------------------------------------- 495 // ParseSectionHeaders 496 //---------------------------------------------------------------------- 497 bool ObjectFilePECOFF::ParseSectionHeaders( 498 uint32_t section_header_data_offset) { 499 const uint32_t nsects = m_coff_header.nsects; 500 m_sect_headers.clear(); 501 502 if (nsects > 0) { 503 const size_t section_header_byte_size = nsects * sizeof(section_header_t); 504 DataExtractor section_header_data = 505 ReadImageData(section_header_data_offset, section_header_byte_size); 506 507 lldb::offset_t offset = 0; 508 if (section_header_data.ValidOffsetForDataOfSize( 509 offset, section_header_byte_size)) { 510 m_sect_headers.resize(nsects); 511 512 for (uint32_t idx = 0; idx < nsects; ++idx) { 513 const void *name_data = section_header_data.GetData(&offset, 8); 514 if (name_data) { 515 memcpy(m_sect_headers[idx].name, name_data, 8); 516 m_sect_headers[idx].vmsize = section_header_data.GetU32(&offset); 517 m_sect_headers[idx].vmaddr = section_header_data.GetU32(&offset); 518 m_sect_headers[idx].size = section_header_data.GetU32(&offset); 519 m_sect_headers[idx].offset = section_header_data.GetU32(&offset); 520 m_sect_headers[idx].reloff = section_header_data.GetU32(&offset); 521 m_sect_headers[idx].lineoff = section_header_data.GetU32(&offset); 522 m_sect_headers[idx].nreloc = section_header_data.GetU16(&offset); 523 m_sect_headers[idx].nline = section_header_data.GetU16(&offset); 524 m_sect_headers[idx].flags = section_header_data.GetU32(&offset); 525 } 526 } 527 } 528 } 529 530 return !m_sect_headers.empty(); 531 } 532 533 bool ObjectFilePECOFF::GetSectionName(std::string §_name, 534 const section_header_t §) { 535 if (sect.name[0] == '/') { 536 lldb::offset_t stroff = strtoul(§.name[1], NULL, 10); 537 lldb::offset_t string_file_offset = 538 m_coff_header.symoff + (m_coff_header.nsyms * 18) + stroff; 539 const char *name = m_data.GetCStr(&string_file_offset); 540 if (name) { 541 sect_name = name; 542 return true; 543 } 544 545 return false; 546 } 547 sect_name = sect.name; 548 return true; 549 } 550 551 //---------------------------------------------------------------------- 552 // GetNListSymtab 553 //---------------------------------------------------------------------- 554 Symtab *ObjectFilePECOFF::GetSymtab() { 555 ModuleSP module_sp(GetModule()); 556 if (module_sp) { 557 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 558 if (m_symtab_ap.get() == NULL) { 559 SectionList *sect_list = GetSectionList(); 560 m_symtab_ap.reset(new Symtab(this)); 561 std::lock_guard<std::recursive_mutex> guard(m_symtab_ap->GetMutex()); 562 563 const uint32_t num_syms = m_coff_header.nsyms; 564 565 if (m_file && num_syms > 0 && m_coff_header.symoff > 0) { 566 const uint32_t symbol_size = 18; 567 const size_t symbol_data_size = num_syms * symbol_size; 568 // Include the 4-byte string table size at the end of the symbols 569 DataExtractor symtab_data = 570 ReadImageData(m_coff_header.symoff, symbol_data_size + 4); 571 lldb::offset_t offset = symbol_data_size; 572 const uint32_t strtab_size = symtab_data.GetU32(&offset); 573 if (strtab_size > 0) { 574 DataExtractor strtab_data = ReadImageData( 575 m_coff_header.symoff + symbol_data_size, strtab_size); 576 577 // First 4 bytes should be zeroed after strtab_size has been read, 578 // because it is used as offset 0 to encode a NULL string. 579 uint32_t *strtab_data_start = const_cast<uint32_t *>( 580 reinterpret_cast<const uint32_t *>(strtab_data.GetDataStart())); 581 strtab_data_start[0] = 0; 582 583 offset = 0; 584 std::string symbol_name; 585 Symbol *symbols = m_symtab_ap->Resize(num_syms); 586 for (uint32_t i = 0; i < num_syms; ++i) { 587 coff_symbol_t symbol; 588 const uint32_t symbol_offset = offset; 589 const char *symbol_name_cstr = NULL; 590 // If the first 4 bytes of the symbol string are zero, then they 591 // are followed by a 4-byte string table offset. Else these 592 // 8 bytes contain the symbol name 593 if (symtab_data.GetU32(&offset) == 0) { 594 // Long string that doesn't fit into the symbol table name, so 595 // now we must read the 4 byte string table offset 596 uint32_t strtab_offset = symtab_data.GetU32(&offset); 597 symbol_name_cstr = strtab_data.PeekCStr(strtab_offset); 598 symbol_name.assign(symbol_name_cstr); 599 } else { 600 // Short string that fits into the symbol table name which is 8 601 // bytes 602 offset += sizeof(symbol.name) - 4; // Skip remaining 603 symbol_name_cstr = symtab_data.PeekCStr(symbol_offset); 604 if (symbol_name_cstr == NULL) 605 break; 606 symbol_name.assign(symbol_name_cstr, sizeof(symbol.name)); 607 } 608 symbol.value = symtab_data.GetU32(&offset); 609 symbol.sect = symtab_data.GetU16(&offset); 610 symbol.type = symtab_data.GetU16(&offset); 611 symbol.storage = symtab_data.GetU8(&offset); 612 symbol.naux = symtab_data.GetU8(&offset); 613 symbols[i].GetMangled().SetValue(ConstString(symbol_name.c_str())); 614 if ((int16_t)symbol.sect >= 1) { 615 Address symbol_addr(sect_list->GetSectionAtIndex(symbol.sect - 1), 616 symbol.value); 617 symbols[i].GetAddressRef() = symbol_addr; 618 symbols[i].SetType(MapSymbolType(symbol.type)); 619 } 620 621 if (symbol.naux > 0) { 622 i += symbol.naux; 623 offset += symbol_size; 624 } 625 } 626 } 627 } 628 629 // Read export header 630 if (coff_data_dir_export_table < m_coff_header_opt.data_dirs.size() && 631 m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmsize > 0 && 632 m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmaddr > 0) { 633 export_directory_entry export_table; 634 uint32_t data_start = 635 m_coff_header_opt.data_dirs[coff_data_dir_export_table].vmaddr; 636 637 uint32_t address_rva = data_start; 638 if (m_file) { 639 Address address(m_coff_header_opt.image_base + data_start, sect_list); 640 address_rva = 641 address.GetSection()->GetFileOffset() + address.GetOffset(); 642 } 643 DataExtractor symtab_data = 644 ReadImageData(address_rva, m_coff_header_opt.data_dirs[0].vmsize); 645 lldb::offset_t offset = 0; 646 647 // Read export_table header 648 export_table.characteristics = symtab_data.GetU32(&offset); 649 export_table.time_date_stamp = symtab_data.GetU32(&offset); 650 export_table.major_version = symtab_data.GetU16(&offset); 651 export_table.minor_version = symtab_data.GetU16(&offset); 652 export_table.name = symtab_data.GetU32(&offset); 653 export_table.base = symtab_data.GetU32(&offset); 654 export_table.number_of_functions = symtab_data.GetU32(&offset); 655 export_table.number_of_names = symtab_data.GetU32(&offset); 656 export_table.address_of_functions = symtab_data.GetU32(&offset); 657 export_table.address_of_names = symtab_data.GetU32(&offset); 658 export_table.address_of_name_ordinals = symtab_data.GetU32(&offset); 659 660 bool has_ordinal = export_table.address_of_name_ordinals != 0; 661 662 lldb::offset_t name_offset = export_table.address_of_names - data_start; 663 lldb::offset_t name_ordinal_offset = 664 export_table.address_of_name_ordinals - data_start; 665 666 Symbol *symbols = m_symtab_ap->Resize(export_table.number_of_names); 667 668 std::string symbol_name; 669 670 // Read each export table entry 671 for (size_t i = 0; i < export_table.number_of_names; ++i) { 672 uint32_t name_ordinal = 673 has_ordinal ? symtab_data.GetU16(&name_ordinal_offset) : i; 674 uint32_t name_address = symtab_data.GetU32(&name_offset); 675 676 const char *symbol_name_cstr = 677 symtab_data.PeekCStr(name_address - data_start); 678 symbol_name.assign(symbol_name_cstr); 679 680 lldb::offset_t function_offset = export_table.address_of_functions - 681 data_start + 682 sizeof(uint32_t) * name_ordinal; 683 uint32_t function_rva = symtab_data.GetU32(&function_offset); 684 685 Address symbol_addr(m_coff_header_opt.image_base + function_rva, 686 sect_list); 687 symbols[i].GetMangled().SetValue(ConstString(symbol_name.c_str())); 688 symbols[i].GetAddressRef() = symbol_addr; 689 symbols[i].SetType(lldb::eSymbolTypeCode); 690 symbols[i].SetDebug(true); 691 } 692 } 693 m_symtab_ap->CalculateSymbolSizes(); 694 } 695 } 696 return m_symtab_ap.get(); 697 } 698 699 bool ObjectFilePECOFF::IsStripped() { 700 // TODO: determine this for COFF 701 return false; 702 } 703 704 void ObjectFilePECOFF::CreateSections(SectionList &unified_section_list) { 705 if (!m_sections_ap.get()) { 706 m_sections_ap.reset(new SectionList()); 707 708 ModuleSP module_sp(GetModule()); 709 if (module_sp) { 710 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 711 const uint32_t nsects = m_sect_headers.size(); 712 ModuleSP module_sp(GetModule()); 713 for (uint32_t idx = 0; idx < nsects; ++idx) { 714 std::string sect_name; 715 GetSectionName(sect_name, m_sect_headers[idx]); 716 ConstString const_sect_name(sect_name.c_str()); 717 static ConstString g_code_sect_name(".code"); 718 static ConstString g_CODE_sect_name("CODE"); 719 static ConstString g_data_sect_name(".data"); 720 static ConstString g_DATA_sect_name("DATA"); 721 static ConstString g_bss_sect_name(".bss"); 722 static ConstString g_BSS_sect_name("BSS"); 723 static ConstString g_debug_sect_name(".debug"); 724 static ConstString g_reloc_sect_name(".reloc"); 725 static ConstString g_stab_sect_name(".stab"); 726 static ConstString g_stabstr_sect_name(".stabstr"); 727 static ConstString g_sect_name_dwarf_debug_abbrev(".debug_abbrev"); 728 static ConstString g_sect_name_dwarf_debug_aranges(".debug_aranges"); 729 static ConstString g_sect_name_dwarf_debug_frame(".debug_frame"); 730 static ConstString g_sect_name_dwarf_debug_info(".debug_info"); 731 static ConstString g_sect_name_dwarf_debug_line(".debug_line"); 732 static ConstString g_sect_name_dwarf_debug_loc(".debug_loc"); 733 static ConstString g_sect_name_dwarf_debug_loclists(".debug_loclists"); 734 static ConstString g_sect_name_dwarf_debug_macinfo(".debug_macinfo"); 735 static ConstString g_sect_name_dwarf_debug_names(".debug_names"); 736 static ConstString g_sect_name_dwarf_debug_pubnames(".debug_pubnames"); 737 static ConstString g_sect_name_dwarf_debug_pubtypes(".debug_pubtypes"); 738 static ConstString g_sect_name_dwarf_debug_ranges(".debug_ranges"); 739 static ConstString g_sect_name_dwarf_debug_str(".debug_str"); 740 static ConstString g_sect_name_dwarf_debug_types(".debug_types"); 741 static ConstString g_sect_name_eh_frame(".eh_frame"); 742 static ConstString g_sect_name_go_symtab(".gosymtab"); 743 SectionType section_type = eSectionTypeOther; 744 if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_CODE && 745 ((const_sect_name == g_code_sect_name) || 746 (const_sect_name == g_CODE_sect_name))) { 747 section_type = eSectionTypeCode; 748 } else if (m_sect_headers[idx].flags & 749 llvm::COFF::IMAGE_SCN_CNT_INITIALIZED_DATA && 750 ((const_sect_name == g_data_sect_name) || 751 (const_sect_name == g_DATA_sect_name))) { 752 if (m_sect_headers[idx].size == 0 && m_sect_headers[idx].offset == 0) 753 section_type = eSectionTypeZeroFill; 754 else 755 section_type = eSectionTypeData; 756 } else if (m_sect_headers[idx].flags & 757 llvm::COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA && 758 ((const_sect_name == g_bss_sect_name) || 759 (const_sect_name == g_BSS_sect_name))) { 760 if (m_sect_headers[idx].size == 0) 761 section_type = eSectionTypeZeroFill; 762 else 763 section_type = eSectionTypeData; 764 } else if (const_sect_name == g_debug_sect_name) { 765 section_type = eSectionTypeDebug; 766 } else if (const_sect_name == g_stabstr_sect_name) { 767 section_type = eSectionTypeDataCString; 768 } else if (const_sect_name == g_reloc_sect_name) { 769 section_type = eSectionTypeOther; 770 } else if (const_sect_name == g_sect_name_dwarf_debug_abbrev) 771 section_type = eSectionTypeDWARFDebugAbbrev; 772 else if (const_sect_name == g_sect_name_dwarf_debug_aranges) 773 section_type = eSectionTypeDWARFDebugAranges; 774 else if (const_sect_name == g_sect_name_dwarf_debug_frame) 775 section_type = eSectionTypeDWARFDebugFrame; 776 else if (const_sect_name == g_sect_name_dwarf_debug_info) 777 section_type = eSectionTypeDWARFDebugInfo; 778 else if (const_sect_name == g_sect_name_dwarf_debug_line) 779 section_type = eSectionTypeDWARFDebugLine; 780 else if (const_sect_name == g_sect_name_dwarf_debug_loc) 781 section_type = eSectionTypeDWARFDebugLoc; 782 else if (const_sect_name == g_sect_name_dwarf_debug_loclists) 783 section_type = eSectionTypeDWARFDebugLocLists; 784 else if (const_sect_name == g_sect_name_dwarf_debug_macinfo) 785 section_type = eSectionTypeDWARFDebugMacInfo; 786 else if (const_sect_name == g_sect_name_dwarf_debug_names) 787 section_type = eSectionTypeDWARFDebugNames; 788 else if (const_sect_name == g_sect_name_dwarf_debug_pubnames) 789 section_type = eSectionTypeDWARFDebugPubNames; 790 else if (const_sect_name == g_sect_name_dwarf_debug_pubtypes) 791 section_type = eSectionTypeDWARFDebugPubTypes; 792 else if (const_sect_name == g_sect_name_dwarf_debug_ranges) 793 section_type = eSectionTypeDWARFDebugRanges; 794 else if (const_sect_name == g_sect_name_dwarf_debug_str) 795 section_type = eSectionTypeDWARFDebugStr; 796 else if (const_sect_name == g_sect_name_dwarf_debug_types) 797 section_type = eSectionTypeDWARFDebugTypes; 798 else if (const_sect_name == g_sect_name_eh_frame) 799 section_type = eSectionTypeEHFrame; 800 else if (const_sect_name == g_sect_name_go_symtab) 801 section_type = eSectionTypeGoSymtab; 802 else if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_CODE) { 803 section_type = eSectionTypeCode; 804 } else if (m_sect_headers[idx].flags & 805 llvm::COFF::IMAGE_SCN_CNT_INITIALIZED_DATA) { 806 section_type = eSectionTypeData; 807 } else if (m_sect_headers[idx].flags & 808 llvm::COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) { 809 if (m_sect_headers[idx].size == 0) 810 section_type = eSectionTypeZeroFill; 811 else 812 section_type = eSectionTypeData; 813 } 814 815 // Use a segment ID of the segment index shifted left by 8 so they 816 // never conflict with any of the sections. 817 SectionSP section_sp(new Section( 818 module_sp, // Module to which this section belongs 819 this, // Object file to which this section belongs 820 idx + 1, // Section ID is the 1 based segment index shifted right by 821 // 8 bits as not to collide with any of the 256 section IDs 822 // that are possible 823 const_sect_name, // Name of this section 824 section_type, // This section is a container of other sections. 825 m_coff_header_opt.image_base + 826 m_sect_headers[idx].vmaddr, // File VM address == addresses as 827 // they are found in the object file 828 m_sect_headers[idx].vmsize, // VM size in bytes of this section 829 m_sect_headers[idx] 830 .offset, // Offset to the data for this section in the file 831 m_sect_headers[idx] 832 .size, // Size in bytes of this section as found in the file 833 m_coff_header_opt.sect_alignment, // Section alignment 834 m_sect_headers[idx].flags)); // Flags for this section 835 836 // section_sp->SetIsEncrypted (segment_is_encrypted); 837 838 unified_section_list.AddSection(section_sp); 839 m_sections_ap->AddSection(section_sp); 840 } 841 } 842 } 843 } 844 845 bool ObjectFilePECOFF::GetUUID(UUID *uuid) { return false; } 846 847 uint32_t ObjectFilePECOFF::ParseDependentModules() { 848 ModuleSP module_sp(GetModule()); 849 if (!module_sp) 850 return 0; 851 852 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 853 if (m_deps_filespec) 854 return m_deps_filespec->GetSize(); 855 856 // Cache coff binary if it is not done yet. 857 if (!CreateBinary()) 858 return 0; 859 860 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT)); 861 if (log) 862 log->Printf("%p ObjectFilePECOFF::ParseDependentModules() module = %p " 863 "(%s), binary = %p (Bin = %p)", 864 static_cast<void *>(this), static_cast<void *>(module_sp.get()), 865 module_sp->GetSpecificationDescription().c_str(), 866 static_cast<void *>(m_owningbin.getPointer()), 867 m_owningbin ? static_cast<void *>(m_owningbin->getBinary()) 868 : nullptr); 869 870 auto COFFObj = 871 llvm::dyn_cast<llvm::object::COFFObjectFile>(m_owningbin->getBinary()); 872 if (!COFFObj) 873 return 0; 874 875 m_deps_filespec = FileSpecList(); 876 877 for (const auto &entry : COFFObj->import_directories()) { 878 llvm::StringRef dll_name; 879 auto ec = entry.getName(dll_name); 880 // Report a bogus entry. 881 if (ec != std::error_code()) { 882 if (log) 883 log->Printf("ObjectFilePECOFF::ParseDependentModules() - failed to get " 884 "import directory entry name: %s", 885 ec.message().c_str()); 886 continue; 887 } 888 889 // At this moment we only have the base name of the DLL. The full path can 890 // only be seen after the dynamic loading. Our best guess is Try to get it 891 // with the help of the object file's directory. 892 llvm::SmallString<128> dll_fullpath; 893 FileSpec dll_specs(dll_name); 894 dll_specs.GetDirectory().SetString(m_file.GetDirectory().GetCString()); 895 896 if (!llvm::sys::fs::real_path(dll_specs.GetPath(), dll_fullpath)) 897 m_deps_filespec->Append(FileSpec(dll_fullpath)); 898 else { 899 // Known DLLs or DLL not found in the object file directory. 900 m_deps_filespec->Append(FileSpec(dll_name)); 901 } 902 } 903 return m_deps_filespec->GetSize(); 904 } 905 906 uint32_t ObjectFilePECOFF::GetDependentModules(FileSpecList &files) { 907 auto num_modules = ParseDependentModules(); 908 auto original_size = files.GetSize(); 909 910 for (unsigned i = 0; i < num_modules; ++i) 911 files.AppendIfUnique(m_deps_filespec->GetFileSpecAtIndex(i)); 912 913 return files.GetSize() - original_size; 914 } 915 916 lldb_private::Address ObjectFilePECOFF::GetEntryPointAddress() { 917 if (m_entry_point_address.IsValid()) 918 return m_entry_point_address; 919 920 if (!ParseHeader() || !IsExecutable()) 921 return m_entry_point_address; 922 923 SectionList *section_list = GetSectionList(); 924 addr_t file_addr = m_coff_header_opt.entry + m_coff_header_opt.image_base; 925 926 if (!section_list) 927 m_entry_point_address.SetOffset(file_addr); 928 else 929 m_entry_point_address.ResolveAddressUsingFileSections(file_addr, section_list); 930 return m_entry_point_address; 931 } 932 933 //---------------------------------------------------------------------- 934 // Dump 935 // 936 // Dump the specifics of the runtime file container (such as any headers 937 // segments, sections, etc). 938 //---------------------------------------------------------------------- 939 void ObjectFilePECOFF::Dump(Stream *s) { 940 ModuleSP module_sp(GetModule()); 941 if (module_sp) { 942 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 943 s->Printf("%p: ", static_cast<void *>(this)); 944 s->Indent(); 945 s->PutCString("ObjectFilePECOFF"); 946 947 ArchSpec header_arch; 948 GetArchitecture(header_arch); 949 950 *s << ", file = '" << m_file 951 << "', arch = " << header_arch.GetArchitectureName() << "\n"; 952 953 SectionList *sections = GetSectionList(); 954 if (sections) 955 sections->Dump(s, NULL, true, UINT32_MAX); 956 957 if (m_symtab_ap.get()) 958 m_symtab_ap->Dump(s, NULL, eSortOrderNone); 959 960 if (m_dos_header.e_magic) 961 DumpDOSHeader(s, m_dos_header); 962 if (m_coff_header.machine) { 963 DumpCOFFHeader(s, m_coff_header); 964 if (m_coff_header.hdrsize) 965 DumpOptCOFFHeader(s, m_coff_header_opt); 966 } 967 s->EOL(); 968 DumpSectionHeaders(s); 969 s->EOL(); 970 971 DumpDependentModules(s); 972 s->EOL(); 973 } 974 } 975 976 //---------------------------------------------------------------------- 977 // DumpDOSHeader 978 // 979 // Dump the MS-DOS header to the specified output stream 980 //---------------------------------------------------------------------- 981 void ObjectFilePECOFF::DumpDOSHeader(Stream *s, const dos_header_t &header) { 982 s->PutCString("MSDOS Header\n"); 983 s->Printf(" e_magic = 0x%4.4x\n", header.e_magic); 984 s->Printf(" e_cblp = 0x%4.4x\n", header.e_cblp); 985 s->Printf(" e_cp = 0x%4.4x\n", header.e_cp); 986 s->Printf(" e_crlc = 0x%4.4x\n", header.e_crlc); 987 s->Printf(" e_cparhdr = 0x%4.4x\n", header.e_cparhdr); 988 s->Printf(" e_minalloc = 0x%4.4x\n", header.e_minalloc); 989 s->Printf(" e_maxalloc = 0x%4.4x\n", header.e_maxalloc); 990 s->Printf(" e_ss = 0x%4.4x\n", header.e_ss); 991 s->Printf(" e_sp = 0x%4.4x\n", header.e_sp); 992 s->Printf(" e_csum = 0x%4.4x\n", header.e_csum); 993 s->Printf(" e_ip = 0x%4.4x\n", header.e_ip); 994 s->Printf(" e_cs = 0x%4.4x\n", header.e_cs); 995 s->Printf(" e_lfarlc = 0x%4.4x\n", header.e_lfarlc); 996 s->Printf(" e_ovno = 0x%4.4x\n", header.e_ovno); 997 s->Printf(" e_res[4] = { 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x }\n", 998 header.e_res[0], header.e_res[1], header.e_res[2], header.e_res[3]); 999 s->Printf(" e_oemid = 0x%4.4x\n", header.e_oemid); 1000 s->Printf(" e_oeminfo = 0x%4.4x\n", header.e_oeminfo); 1001 s->Printf(" e_res2[10] = { 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, " 1002 "0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x }\n", 1003 header.e_res2[0], header.e_res2[1], header.e_res2[2], 1004 header.e_res2[3], header.e_res2[4], header.e_res2[5], 1005 header.e_res2[6], header.e_res2[7], header.e_res2[8], 1006 header.e_res2[9]); 1007 s->Printf(" e_lfanew = 0x%8.8x\n", header.e_lfanew); 1008 } 1009 1010 //---------------------------------------------------------------------- 1011 // DumpCOFFHeader 1012 // 1013 // Dump the COFF header to the specified output stream 1014 //---------------------------------------------------------------------- 1015 void ObjectFilePECOFF::DumpCOFFHeader(Stream *s, const coff_header_t &header) { 1016 s->PutCString("COFF Header\n"); 1017 s->Printf(" machine = 0x%4.4x\n", header.machine); 1018 s->Printf(" nsects = 0x%4.4x\n", header.nsects); 1019 s->Printf(" modtime = 0x%8.8x\n", header.modtime); 1020 s->Printf(" symoff = 0x%8.8x\n", header.symoff); 1021 s->Printf(" nsyms = 0x%8.8x\n", header.nsyms); 1022 s->Printf(" hdrsize = 0x%4.4x\n", header.hdrsize); 1023 } 1024 1025 //---------------------------------------------------------------------- 1026 // DumpOptCOFFHeader 1027 // 1028 // Dump the optional COFF header to the specified output stream 1029 //---------------------------------------------------------------------- 1030 void ObjectFilePECOFF::DumpOptCOFFHeader(Stream *s, 1031 const coff_opt_header_t &header) { 1032 s->PutCString("Optional COFF Header\n"); 1033 s->Printf(" magic = 0x%4.4x\n", header.magic); 1034 s->Printf(" major_linker_version = 0x%2.2x\n", 1035 header.major_linker_version); 1036 s->Printf(" minor_linker_version = 0x%2.2x\n", 1037 header.minor_linker_version); 1038 s->Printf(" code_size = 0x%8.8x\n", header.code_size); 1039 s->Printf(" data_size = 0x%8.8x\n", header.data_size); 1040 s->Printf(" bss_size = 0x%8.8x\n", header.bss_size); 1041 s->Printf(" entry = 0x%8.8x\n", header.entry); 1042 s->Printf(" code_offset = 0x%8.8x\n", header.code_offset); 1043 s->Printf(" data_offset = 0x%8.8x\n", header.data_offset); 1044 s->Printf(" image_base = 0x%16.16" PRIx64 "\n", 1045 header.image_base); 1046 s->Printf(" sect_alignment = 0x%8.8x\n", header.sect_alignment); 1047 s->Printf(" file_alignment = 0x%8.8x\n", header.file_alignment); 1048 s->Printf(" major_os_system_version = 0x%4.4x\n", 1049 header.major_os_system_version); 1050 s->Printf(" minor_os_system_version = 0x%4.4x\n", 1051 header.minor_os_system_version); 1052 s->Printf(" major_image_version = 0x%4.4x\n", 1053 header.major_image_version); 1054 s->Printf(" minor_image_version = 0x%4.4x\n", 1055 header.minor_image_version); 1056 s->Printf(" major_subsystem_version = 0x%4.4x\n", 1057 header.major_subsystem_version); 1058 s->Printf(" minor_subsystem_version = 0x%4.4x\n", 1059 header.minor_subsystem_version); 1060 s->Printf(" reserved1 = 0x%8.8x\n", header.reserved1); 1061 s->Printf(" image_size = 0x%8.8x\n", header.image_size); 1062 s->Printf(" header_size = 0x%8.8x\n", header.header_size); 1063 s->Printf(" checksum = 0x%8.8x\n", header.checksum); 1064 s->Printf(" subsystem = 0x%4.4x\n", header.subsystem); 1065 s->Printf(" dll_flags = 0x%4.4x\n", header.dll_flags); 1066 s->Printf(" stack_reserve_size = 0x%16.16" PRIx64 "\n", 1067 header.stack_reserve_size); 1068 s->Printf(" stack_commit_size = 0x%16.16" PRIx64 "\n", 1069 header.stack_commit_size); 1070 s->Printf(" heap_reserve_size = 0x%16.16" PRIx64 "\n", 1071 header.heap_reserve_size); 1072 s->Printf(" heap_commit_size = 0x%16.16" PRIx64 "\n", 1073 header.heap_commit_size); 1074 s->Printf(" loader_flags = 0x%8.8x\n", header.loader_flags); 1075 s->Printf(" num_data_dir_entries = 0x%8.8x\n", 1076 (uint32_t)header.data_dirs.size()); 1077 uint32_t i; 1078 for (i = 0; i < header.data_dirs.size(); i++) { 1079 s->Printf(" data_dirs[%2u] vmaddr = 0x%8.8x, vmsize = 0x%8.8x\n", i, 1080 header.data_dirs[i].vmaddr, header.data_dirs[i].vmsize); 1081 } 1082 } 1083 //---------------------------------------------------------------------- 1084 // DumpSectionHeader 1085 // 1086 // Dump a single ELF section header to the specified output stream 1087 //---------------------------------------------------------------------- 1088 void ObjectFilePECOFF::DumpSectionHeader(Stream *s, 1089 const section_header_t &sh) { 1090 std::string name; 1091 GetSectionName(name, sh); 1092 s->Printf("%-16s 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x 0x%4.4x " 1093 "0x%4.4x 0x%8.8x\n", 1094 name.c_str(), sh.vmaddr, sh.vmsize, sh.offset, sh.size, sh.reloff, 1095 sh.lineoff, sh.nreloc, sh.nline, sh.flags); 1096 } 1097 1098 //---------------------------------------------------------------------- 1099 // DumpSectionHeaders 1100 // 1101 // Dump all of the ELF section header to the specified output stream 1102 //---------------------------------------------------------------------- 1103 void ObjectFilePECOFF::DumpSectionHeaders(Stream *s) { 1104 1105 s->PutCString("Section Headers\n"); 1106 s->PutCString("IDX name vm addr vm size file off file " 1107 "size reloc off line off nreloc nline flags\n"); 1108 s->PutCString("==== ---------------- ---------- ---------- ---------- " 1109 "---------- ---------- ---------- ------ ------ ----------\n"); 1110 1111 uint32_t idx = 0; 1112 SectionHeaderCollIter pos, end = m_sect_headers.end(); 1113 1114 for (pos = m_sect_headers.begin(); pos != end; ++pos, ++idx) { 1115 s->Printf("[%2u] ", idx); 1116 ObjectFilePECOFF::DumpSectionHeader(s, *pos); 1117 } 1118 } 1119 1120 //---------------------------------------------------------------------- 1121 // DumpDependentModules 1122 // 1123 // Dump all of the dependent modules to the specified output stream 1124 //---------------------------------------------------------------------- 1125 void ObjectFilePECOFF::DumpDependentModules(lldb_private::Stream *s) { 1126 auto num_modules = ParseDependentModules(); 1127 if (num_modules > 0) { 1128 s->PutCString("Dependent Modules\n"); 1129 for (unsigned i = 0; i < num_modules; ++i) { 1130 auto spec = m_deps_filespec->GetFileSpecAtIndex(i); 1131 s->Printf(" %s\n", spec.GetFilename().GetCString()); 1132 } 1133 } 1134 } 1135 1136 bool ObjectFilePECOFF::IsWindowsSubsystem() { 1137 switch (m_coff_header_opt.subsystem) { 1138 case llvm::COFF::IMAGE_SUBSYSTEM_NATIVE: 1139 case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_GUI: 1140 case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_CUI: 1141 case llvm::COFF::IMAGE_SUBSYSTEM_NATIVE_WINDOWS: 1142 case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_CE_GUI: 1143 case llvm::COFF::IMAGE_SUBSYSTEM_XBOX: 1144 case llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATION: 1145 return true; 1146 default: 1147 return false; 1148 } 1149 } 1150 1151 bool ObjectFilePECOFF::GetArchitecture(ArchSpec &arch) { 1152 uint16_t machine = m_coff_header.machine; 1153 switch (machine) { 1154 case llvm::COFF::IMAGE_FILE_MACHINE_AMD64: 1155 case llvm::COFF::IMAGE_FILE_MACHINE_I386: 1156 case llvm::COFF::IMAGE_FILE_MACHINE_POWERPC: 1157 case llvm::COFF::IMAGE_FILE_MACHINE_POWERPCFP: 1158 case llvm::COFF::IMAGE_FILE_MACHINE_ARM: 1159 case llvm::COFF::IMAGE_FILE_MACHINE_ARMNT: 1160 case llvm::COFF::IMAGE_FILE_MACHINE_THUMB: 1161 arch.SetArchitecture(eArchTypeCOFF, machine, LLDB_INVALID_CPUTYPE, 1162 IsWindowsSubsystem() ? llvm::Triple::Win32 1163 : llvm::Triple::UnknownOS); 1164 return true; 1165 default: 1166 break; 1167 } 1168 return false; 1169 } 1170 1171 ObjectFile::Type ObjectFilePECOFF::CalculateType() { 1172 if (m_coff_header.machine != 0) { 1173 if ((m_coff_header.flags & llvm::COFF::IMAGE_FILE_DLL) == 0) 1174 return eTypeExecutable; 1175 else 1176 return eTypeSharedLibrary; 1177 } 1178 return eTypeExecutable; 1179 } 1180 1181 ObjectFile::Strata ObjectFilePECOFF::CalculateStrata() { return eStrataUser; } 1182 1183 //------------------------------------------------------------------ 1184 // PluginInterface protocol 1185 //------------------------------------------------------------------ 1186 ConstString ObjectFilePECOFF::GetPluginName() { return GetPluginNameStatic(); } 1187 1188 uint32_t ObjectFilePECOFF::GetPluginVersion() { return 1; } 1189