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