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