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