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 m_symtab_ap->CalculateSymbolSizes(); 653 } 654 } 655 return m_symtab_ap.get(); 656 657 } 658 659 bool 660 ObjectFilePECOFF::IsStripped () 661 { 662 // TODO: determine this for COFF 663 return false; 664 } 665 666 667 668 void 669 ObjectFilePECOFF::CreateSections (SectionList &unified_section_list) 670 { 671 if (!m_sections_ap.get()) 672 { 673 m_sections_ap.reset(new SectionList()); 674 675 ModuleSP module_sp(GetModule()); 676 if (module_sp) 677 { 678 lldb_private::Mutex::Locker locker(module_sp->GetMutex()); 679 const uint32_t nsects = m_sect_headers.size(); 680 ModuleSP module_sp (GetModule()); 681 for (uint32_t idx = 0; idx<nsects; ++idx) 682 { 683 std::string sect_name; 684 GetSectionName (sect_name, m_sect_headers[idx]); 685 ConstString const_sect_name (sect_name.c_str()); 686 static ConstString g_code_sect_name (".code"); 687 static ConstString g_CODE_sect_name ("CODE"); 688 static ConstString g_data_sect_name (".data"); 689 static ConstString g_DATA_sect_name ("DATA"); 690 static ConstString g_bss_sect_name (".bss"); 691 static ConstString g_BSS_sect_name ("BSS"); 692 static ConstString g_debug_sect_name (".debug"); 693 static ConstString g_reloc_sect_name (".reloc"); 694 static ConstString g_stab_sect_name (".stab"); 695 static ConstString g_stabstr_sect_name (".stabstr"); 696 static ConstString g_sect_name_dwarf_debug_abbrev (".debug_abbrev"); 697 static ConstString g_sect_name_dwarf_debug_aranges (".debug_aranges"); 698 static ConstString g_sect_name_dwarf_debug_frame (".debug_frame"); 699 static ConstString g_sect_name_dwarf_debug_info (".debug_info"); 700 static ConstString g_sect_name_dwarf_debug_line (".debug_line"); 701 static ConstString g_sect_name_dwarf_debug_loc (".debug_loc"); 702 static ConstString g_sect_name_dwarf_debug_macinfo (".debug_macinfo"); 703 static ConstString g_sect_name_dwarf_debug_pubnames (".debug_pubnames"); 704 static ConstString g_sect_name_dwarf_debug_pubtypes (".debug_pubtypes"); 705 static ConstString g_sect_name_dwarf_debug_ranges (".debug_ranges"); 706 static ConstString g_sect_name_dwarf_debug_str (".debug_str"); 707 static ConstString g_sect_name_eh_frame (".eh_frame"); 708 static ConstString g_sect_name_go_symtab (".gosymtab"); 709 SectionType section_type = eSectionTypeOther; 710 if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_CODE && 711 ((const_sect_name == g_code_sect_name) || (const_sect_name == g_CODE_sect_name))) 712 { 713 section_type = eSectionTypeCode; 714 } 715 else if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_INITIALIZED_DATA && 716 ((const_sect_name == g_data_sect_name) || (const_sect_name == g_DATA_sect_name))) 717 { 718 section_type = eSectionTypeData; 719 } 720 else if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA && 721 ((const_sect_name == g_bss_sect_name) || (const_sect_name == g_BSS_sect_name))) 722 { 723 if (m_sect_headers[idx].size == 0) 724 section_type = eSectionTypeZeroFill; 725 else 726 section_type = eSectionTypeData; 727 } 728 else if (const_sect_name == g_debug_sect_name) 729 { 730 section_type = eSectionTypeDebug; 731 } 732 else if (const_sect_name == g_stabstr_sect_name) 733 { 734 section_type = eSectionTypeDataCString; 735 } 736 else if (const_sect_name == g_reloc_sect_name) 737 { 738 section_type = eSectionTypeOther; 739 } 740 else if (const_sect_name == g_sect_name_dwarf_debug_abbrev) section_type = eSectionTypeDWARFDebugAbbrev; 741 else if (const_sect_name == g_sect_name_dwarf_debug_aranges) section_type = eSectionTypeDWARFDebugAranges; 742 else if (const_sect_name == g_sect_name_dwarf_debug_frame) section_type = eSectionTypeDWARFDebugFrame; 743 else if (const_sect_name == g_sect_name_dwarf_debug_info) section_type = eSectionTypeDWARFDebugInfo; 744 else if (const_sect_name == g_sect_name_dwarf_debug_line) section_type = eSectionTypeDWARFDebugLine; 745 else if (const_sect_name == g_sect_name_dwarf_debug_loc) section_type = eSectionTypeDWARFDebugLoc; 746 else if (const_sect_name == g_sect_name_dwarf_debug_macinfo) section_type = eSectionTypeDWARFDebugMacInfo; 747 else if (const_sect_name == g_sect_name_dwarf_debug_pubnames) section_type = eSectionTypeDWARFDebugPubNames; 748 else if (const_sect_name == g_sect_name_dwarf_debug_pubtypes) section_type = eSectionTypeDWARFDebugPubTypes; 749 else if (const_sect_name == g_sect_name_dwarf_debug_ranges) section_type = eSectionTypeDWARFDebugRanges; 750 else if (const_sect_name == g_sect_name_dwarf_debug_str) section_type = eSectionTypeDWARFDebugStr; 751 else if (const_sect_name == g_sect_name_eh_frame) section_type = eSectionTypeEHFrame; 752 else if (const_sect_name == g_sect_name_go_symtab) section_type = eSectionTypeGoSymtab; 753 else if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_CODE) 754 { 755 section_type = eSectionTypeCode; 756 } 757 else if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_INITIALIZED_DATA) 758 { 759 section_type = eSectionTypeData; 760 } 761 else if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) 762 { 763 if (m_sect_headers[idx].size == 0) 764 section_type = eSectionTypeZeroFill; 765 else 766 section_type = eSectionTypeData; 767 } 768 769 // Use a segment ID of the segment index shifted left by 8 so they 770 // never conflict with any of the sections. 771 SectionSP section_sp (new Section (module_sp, // Module to which this section belongs 772 this, // Object file to which this section belongs 773 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 774 const_sect_name, // Name of this section 775 section_type, // This section is a container of other sections. 776 m_coff_header_opt.image_base + m_sect_headers[idx].vmaddr, // File VM address == addresses as they are found in the object file 777 m_sect_headers[idx].vmsize, // VM size in bytes of this section 778 m_sect_headers[idx].offset, // Offset to the data for this section in the file 779 m_sect_headers[idx].size, // Size in bytes of this section as found in the file 780 m_coff_header_opt.sect_alignment, // Section alignment 781 m_sect_headers[idx].flags)); // Flags for this section 782 783 //section_sp->SetIsEncrypted (segment_is_encrypted); 784 785 unified_section_list.AddSection(section_sp); 786 m_sections_ap->AddSection (section_sp); 787 } 788 } 789 } 790 } 791 792 bool 793 ObjectFilePECOFF::GetUUID (UUID* uuid) 794 { 795 return false; 796 } 797 798 uint32_t 799 ObjectFilePECOFF::GetDependentModules (FileSpecList& files) 800 { 801 return 0; 802 } 803 804 805 //---------------------------------------------------------------------- 806 // Dump 807 // 808 // Dump the specifics of the runtime file container (such as any headers 809 // segments, sections, etc). 810 //---------------------------------------------------------------------- 811 void 812 ObjectFilePECOFF::Dump(Stream *s) 813 { 814 ModuleSP module_sp(GetModule()); 815 if (module_sp) 816 { 817 lldb_private::Mutex::Locker locker(module_sp->GetMutex()); 818 s->Printf("%p: ", static_cast<void*>(this)); 819 s->Indent(); 820 s->PutCString("ObjectFilePECOFF"); 821 822 ArchSpec header_arch; 823 GetArchitecture (header_arch); 824 825 *s << ", file = '" << m_file << "', arch = " << header_arch.GetArchitectureName() << "\n"; 826 827 SectionList *sections = GetSectionList(); 828 if (sections) 829 sections->Dump(s, NULL, true, UINT32_MAX); 830 831 if (m_symtab_ap.get()) 832 m_symtab_ap->Dump(s, NULL, eSortOrderNone); 833 834 if (m_dos_header.e_magic) 835 DumpDOSHeader (s, m_dos_header); 836 if (m_coff_header.machine) 837 { 838 DumpCOFFHeader (s, m_coff_header); 839 if (m_coff_header.hdrsize) 840 DumpOptCOFFHeader (s, m_coff_header_opt); 841 } 842 s->EOL(); 843 DumpSectionHeaders(s); 844 s->EOL(); 845 } 846 } 847 848 //---------------------------------------------------------------------- 849 // DumpDOSHeader 850 // 851 // Dump the MS-DOS header to the specified output stream 852 //---------------------------------------------------------------------- 853 void 854 ObjectFilePECOFF::DumpDOSHeader(Stream *s, const dos_header_t& header) 855 { 856 s->PutCString ("MSDOS Header\n"); 857 s->Printf (" e_magic = 0x%4.4x\n", header.e_magic); 858 s->Printf (" e_cblp = 0x%4.4x\n", header.e_cblp); 859 s->Printf (" e_cp = 0x%4.4x\n", header.e_cp); 860 s->Printf (" e_crlc = 0x%4.4x\n", header.e_crlc); 861 s->Printf (" e_cparhdr = 0x%4.4x\n", header.e_cparhdr); 862 s->Printf (" e_minalloc = 0x%4.4x\n", header.e_minalloc); 863 s->Printf (" e_maxalloc = 0x%4.4x\n", header.e_maxalloc); 864 s->Printf (" e_ss = 0x%4.4x\n", header.e_ss); 865 s->Printf (" e_sp = 0x%4.4x\n", header.e_sp); 866 s->Printf (" e_csum = 0x%4.4x\n", header.e_csum); 867 s->Printf (" e_ip = 0x%4.4x\n", header.e_ip); 868 s->Printf (" e_cs = 0x%4.4x\n", header.e_cs); 869 s->Printf (" e_lfarlc = 0x%4.4x\n", header.e_lfarlc); 870 s->Printf (" e_ovno = 0x%4.4x\n", header.e_ovno); 871 s->Printf (" e_res[4] = { 0x%4.4x, 0x%4.4x, 0x%4.4x, 0x%4.4x }\n", 872 header.e_res[0], 873 header.e_res[1], 874 header.e_res[2], 875 header.e_res[3]); 876 s->Printf (" e_oemid = 0x%4.4x\n", header.e_oemid); 877 s->Printf (" e_oeminfo = 0x%4.4x\n", header.e_oeminfo); 878 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", 879 header.e_res2[0], 880 header.e_res2[1], 881 header.e_res2[2], 882 header.e_res2[3], 883 header.e_res2[4], 884 header.e_res2[5], 885 header.e_res2[6], 886 header.e_res2[7], 887 header.e_res2[8], 888 header.e_res2[9]); 889 s->Printf (" e_lfanew = 0x%8.8x\n", header.e_lfanew); 890 } 891 892 //---------------------------------------------------------------------- 893 // DumpCOFFHeader 894 // 895 // Dump the COFF header to the specified output stream 896 //---------------------------------------------------------------------- 897 void 898 ObjectFilePECOFF::DumpCOFFHeader(Stream *s, const coff_header_t& header) 899 { 900 s->PutCString ("COFF Header\n"); 901 s->Printf (" machine = 0x%4.4x\n", header.machine); 902 s->Printf (" nsects = 0x%4.4x\n", header.nsects); 903 s->Printf (" modtime = 0x%8.8x\n", header.modtime); 904 s->Printf (" symoff = 0x%8.8x\n", header.symoff); 905 s->Printf (" nsyms = 0x%8.8x\n", header.nsyms); 906 s->Printf (" hdrsize = 0x%4.4x\n", header.hdrsize); 907 } 908 909 //---------------------------------------------------------------------- 910 // DumpOptCOFFHeader 911 // 912 // Dump the optional COFF header to the specified output stream 913 //---------------------------------------------------------------------- 914 void 915 ObjectFilePECOFF::DumpOptCOFFHeader(Stream *s, const coff_opt_header_t& header) 916 { 917 s->PutCString ("Optional COFF Header\n"); 918 s->Printf (" magic = 0x%4.4x\n", header.magic); 919 s->Printf (" major_linker_version = 0x%2.2x\n", header.major_linker_version); 920 s->Printf (" minor_linker_version = 0x%2.2x\n", header.minor_linker_version); 921 s->Printf (" code_size = 0x%8.8x\n", header.code_size); 922 s->Printf (" data_size = 0x%8.8x\n", header.data_size); 923 s->Printf (" bss_size = 0x%8.8x\n", header.bss_size); 924 s->Printf (" entry = 0x%8.8x\n", header.entry); 925 s->Printf (" code_offset = 0x%8.8x\n", header.code_offset); 926 s->Printf (" data_offset = 0x%8.8x\n", header.data_offset); 927 s->Printf (" image_base = 0x%16.16" PRIx64 "\n", header.image_base); 928 s->Printf (" sect_alignment = 0x%8.8x\n", header.sect_alignment); 929 s->Printf (" file_alignment = 0x%8.8x\n", header.file_alignment); 930 s->Printf (" major_os_system_version = 0x%4.4x\n", header.major_os_system_version); 931 s->Printf (" minor_os_system_version = 0x%4.4x\n", header.minor_os_system_version); 932 s->Printf (" major_image_version = 0x%4.4x\n", header.major_image_version); 933 s->Printf (" minor_image_version = 0x%4.4x\n", header.minor_image_version); 934 s->Printf (" major_subsystem_version = 0x%4.4x\n", header.major_subsystem_version); 935 s->Printf (" minor_subsystem_version = 0x%4.4x\n", header.minor_subsystem_version); 936 s->Printf (" reserved1 = 0x%8.8x\n", header.reserved1); 937 s->Printf (" image_size = 0x%8.8x\n", header.image_size); 938 s->Printf (" header_size = 0x%8.8x\n", header.header_size); 939 s->Printf (" checksum = 0x%8.8x\n", header.checksum); 940 s->Printf (" subsystem = 0x%4.4x\n", header.subsystem); 941 s->Printf (" dll_flags = 0x%4.4x\n", header.dll_flags); 942 s->Printf (" stack_reserve_size = 0x%16.16" PRIx64 "\n", header.stack_reserve_size); 943 s->Printf (" stack_commit_size = 0x%16.16" PRIx64 "\n", header.stack_commit_size); 944 s->Printf (" heap_reserve_size = 0x%16.16" PRIx64 "\n", header.heap_reserve_size); 945 s->Printf (" heap_commit_size = 0x%16.16" PRIx64 "\n", header.heap_commit_size); 946 s->Printf (" loader_flags = 0x%8.8x\n", header.loader_flags); 947 s->Printf (" num_data_dir_entries = 0x%8.8x\n", (uint32_t)header.data_dirs.size()); 948 uint32_t i; 949 for (i=0; i<header.data_dirs.size(); i++) 950 { 951 s->Printf (" data_dirs[%2u] vmaddr = 0x%8.8x, vmsize = 0x%8.8x\n", 952 i, 953 header.data_dirs[i].vmaddr, 954 header.data_dirs[i].vmsize); 955 } 956 } 957 //---------------------------------------------------------------------- 958 // DumpSectionHeader 959 // 960 // Dump a single ELF section header to the specified output stream 961 //---------------------------------------------------------------------- 962 void 963 ObjectFilePECOFF::DumpSectionHeader(Stream *s, const section_header_t& sh) 964 { 965 std::string name; 966 GetSectionName(name, sh); 967 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", 968 name.c_str(), 969 sh.vmaddr, 970 sh.vmsize, 971 sh.offset, 972 sh.size, 973 sh.reloff, 974 sh.lineoff, 975 sh.nreloc, 976 sh.nline, 977 sh.flags); 978 } 979 980 981 //---------------------------------------------------------------------- 982 // DumpSectionHeaders 983 // 984 // Dump all of the ELF section header to the specified output stream 985 //---------------------------------------------------------------------- 986 void 987 ObjectFilePECOFF::DumpSectionHeaders(Stream *s) 988 { 989 990 s->PutCString ("Section Headers\n"); 991 s->PutCString ("IDX name vm addr vm size file off file size reloc off line off nreloc nline flags\n"); 992 s->PutCString ("==== ---------------- ---------- ---------- ---------- ---------- ---------- ---------- ------ ------ ----------\n"); 993 994 uint32_t idx = 0; 995 SectionHeaderCollIter pos, end = m_sect_headers.end(); 996 997 for (pos = m_sect_headers.begin(); pos != end; ++pos, ++idx) 998 { 999 s->Printf ("[%2u] ", idx); 1000 ObjectFilePECOFF::DumpSectionHeader(s, *pos); 1001 } 1002 } 1003 1004 bool 1005 ObjectFilePECOFF::GetArchitecture (ArchSpec &arch) 1006 { 1007 uint16_t machine = m_coff_header.machine; 1008 switch (machine) 1009 { 1010 case llvm::COFF::IMAGE_FILE_MACHINE_AMD64: 1011 case llvm::COFF::IMAGE_FILE_MACHINE_I386: 1012 case llvm::COFF::IMAGE_FILE_MACHINE_POWERPC: 1013 case llvm::COFF::IMAGE_FILE_MACHINE_POWERPCFP: 1014 case llvm::COFF::IMAGE_FILE_MACHINE_ARM: 1015 case llvm::COFF::IMAGE_FILE_MACHINE_ARMNT: 1016 case llvm::COFF::IMAGE_FILE_MACHINE_THUMB: 1017 arch.SetArchitecture (eArchTypeCOFF, machine, LLDB_INVALID_CPUTYPE); 1018 return true; 1019 default: 1020 break; 1021 } 1022 return false; 1023 } 1024 1025 ObjectFile::Type 1026 ObjectFilePECOFF::CalculateType() 1027 { 1028 if (m_coff_header.machine != 0) 1029 { 1030 if ((m_coff_header.flags & llvm::COFF::IMAGE_FILE_DLL) == 0) 1031 return eTypeExecutable; 1032 else 1033 return eTypeSharedLibrary; 1034 } 1035 return eTypeExecutable; 1036 } 1037 1038 ObjectFile::Strata 1039 ObjectFilePECOFF::CalculateStrata() 1040 { 1041 return eStrataUser; 1042 } 1043 //------------------------------------------------------------------ 1044 // PluginInterface protocol 1045 //------------------------------------------------------------------ 1046 ConstString 1047 ObjectFilePECOFF::GetPluginName() 1048 { 1049 return GetPluginNameStatic(); 1050 } 1051 1052 uint32_t 1053 ObjectFilePECOFF::GetPluginVersion() 1054 { 1055 return 1; 1056 } 1057 1058