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