1 //===-- Address.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 "lldb/Core/Address.h" 11 #include "lldb/Core/Module.h" 12 #include "lldb/Core/Section.h" 13 #include "lldb/Symbol/ObjectFile.h" 14 #include "lldb/Symbol/Variable.h" 15 #include "lldb/Symbol/VariableList.h" 16 #include "lldb/Target/ExecutionContext.h" 17 #include "lldb/Target/Process.h" 18 #include "lldb/Target/Target.h" 19 20 #include "llvm/ADT/Triple.h" 21 22 using namespace lldb; 23 using namespace lldb_private; 24 25 static size_t 26 ReadBytes (ExecutionContextScope *exe_scope, const Address &address, void *dst, size_t dst_len) 27 { 28 if (exe_scope == NULL) 29 return 0; 30 31 TargetSP target_sp (exe_scope->CalculateTarget()); 32 if (target_sp) 33 { 34 Error error; 35 bool prefer_file_cache = false; 36 return target_sp->ReadMemory (address, prefer_file_cache, dst, dst_len, error); 37 } 38 return 0; 39 } 40 41 static bool 42 GetByteOrderAndAddressSize (ExecutionContextScope *exe_scope, const Address &address, ByteOrder& byte_order, uint32_t& addr_size) 43 { 44 byte_order = eByteOrderInvalid; 45 addr_size = 0; 46 if (exe_scope == NULL) 47 return false; 48 49 TargetSP target_sp (exe_scope->CalculateTarget()); 50 if (target_sp) 51 { 52 byte_order = target_sp->GetArchitecture().GetByteOrder(); 53 addr_size = target_sp->GetArchitecture().GetAddressByteSize(); 54 } 55 56 if (byte_order == eByteOrderInvalid || addr_size == 0) 57 { 58 ModuleSP module_sp (address.GetModule()); 59 if (module_sp) 60 { 61 byte_order = module_sp->GetArchitecture().GetByteOrder(); 62 addr_size = module_sp->GetArchitecture().GetAddressByteSize(); 63 } 64 } 65 return byte_order != eByteOrderInvalid && addr_size != 0; 66 } 67 68 static uint64_t 69 ReadUIntMax64 (ExecutionContextScope *exe_scope, const Address &address, uint32_t byte_size, bool &success) 70 { 71 uint64_t uval64 = 0; 72 if (exe_scope == NULL || byte_size > sizeof(uint64_t)) 73 { 74 success = false; 75 return 0; 76 } 77 uint64_t buf = 0; 78 79 success = ReadBytes (exe_scope, address, &buf, byte_size) == byte_size; 80 if (success) 81 { 82 ByteOrder byte_order = eByteOrderInvalid; 83 uint32_t addr_size = 0; 84 if (GetByteOrderAndAddressSize (exe_scope, address, byte_order, addr_size)) 85 { 86 DataExtractor data (&buf, sizeof(buf), byte_order, addr_size); 87 uint32_t offset = 0; 88 uval64 = data.GetU64(&offset); 89 } 90 else 91 success = false; 92 } 93 return uval64; 94 } 95 96 static bool 97 ReadAddress (ExecutionContextScope *exe_scope, const Address &address, uint32_t pointer_size, Address &deref_so_addr) 98 { 99 if (exe_scope == NULL) 100 return false; 101 102 103 bool success = false; 104 addr_t deref_addr = ReadUIntMax64 (exe_scope, address, pointer_size, success); 105 if (success) 106 { 107 ExecutionContext exe_ctx; 108 exe_scope->CalculateExecutionContext(exe_ctx); 109 // If we have any sections that are loaded, try and resolve using the 110 // section load list 111 Target *target = exe_ctx.GetTargetPtr(); 112 if (target && !target->GetSectionLoadList().IsEmpty()) 113 { 114 if (target->GetSectionLoadList().ResolveLoadAddress (deref_addr, deref_so_addr)) 115 return true; 116 } 117 else 118 { 119 // If we were not running, yet able to read an integer, we must 120 // have a module 121 ModuleSP module_sp (address.GetModule()); 122 123 assert (module_sp); 124 if (module_sp->ResolveFileAddress(deref_addr, deref_so_addr)) 125 return true; 126 } 127 128 // We couldn't make "deref_addr" into a section offset value, but we were 129 // able to read the address, so we return a section offset address with 130 // no section and "deref_addr" as the offset (address). 131 deref_so_addr.SetRawAddress(deref_addr); 132 return true; 133 } 134 return false; 135 } 136 137 static bool 138 DumpUInt (ExecutionContextScope *exe_scope, const Address &address, uint32_t byte_size, Stream* strm) 139 { 140 if (exe_scope == NULL || byte_size == 0) 141 return 0; 142 std::vector<uint8_t> buf(byte_size, 0); 143 144 if (ReadBytes (exe_scope, address, &buf[0], buf.size()) == buf.size()) 145 { 146 ByteOrder byte_order = eByteOrderInvalid; 147 uint32_t addr_size = 0; 148 if (GetByteOrderAndAddressSize (exe_scope, address, byte_order, addr_size)) 149 { 150 DataExtractor data (&buf.front(), buf.size(), byte_order, addr_size); 151 152 data.Dump (strm, 153 0, // Start offset in "data" 154 eFormatHex, // Print as characters 155 buf.size(), // Size of item 156 1, // Items count 157 UINT32_MAX, // num per line 158 LLDB_INVALID_ADDRESS,// base address 159 0, // bitfield bit size 160 0); // bitfield bit offset 161 162 return true; 163 } 164 } 165 return false; 166 } 167 168 169 static size_t 170 ReadCStringFromMemory (ExecutionContextScope *exe_scope, const Address &address, Stream *strm) 171 { 172 if (exe_scope == NULL) 173 return 0; 174 const size_t k_buf_len = 256; 175 char buf[k_buf_len+1]; 176 buf[k_buf_len] = '\0'; // NULL terminate 177 178 // Byte order and address size don't matter for C string dumping.. 179 DataExtractor data (buf, sizeof(buf), lldb::endian::InlHostByteOrder(), 4); 180 size_t total_len = 0; 181 size_t bytes_read; 182 Address curr_address(address); 183 strm->PutChar ('"'); 184 while ((bytes_read = ReadBytes (exe_scope, curr_address, buf, k_buf_len)) > 0) 185 { 186 size_t len = strlen(buf); 187 if (len == 0) 188 break; 189 if (len > bytes_read) 190 len = bytes_read; 191 192 data.Dump (strm, 193 0, // Start offset in "data" 194 eFormatChar, // Print as characters 195 1, // Size of item (1 byte for a char!) 196 len, // How many bytes to print? 197 UINT32_MAX, // num per line 198 LLDB_INVALID_ADDRESS,// base address 199 0, // bitfield bit size 200 201 0); // bitfield bit offset 202 203 total_len += bytes_read; 204 205 if (len < k_buf_len) 206 break; 207 curr_address.SetOffset (curr_address.GetOffset() + bytes_read); 208 } 209 strm->PutChar ('"'); 210 return total_len; 211 } 212 213 Address::Address (lldb::addr_t abs_addr) : 214 m_section_wp (), 215 m_offset (abs_addr) 216 { 217 } 218 219 Address::Address (addr_t address, const SectionList *section_list) : 220 m_section_wp (), 221 m_offset (LLDB_INVALID_ADDRESS) 222 { 223 ResolveAddressUsingFileSections(address, section_list); 224 } 225 226 const Address& 227 Address::operator= (const Address& rhs) 228 { 229 if (this != &rhs) 230 { 231 m_section_wp = rhs.m_section_wp; 232 m_offset = rhs.m_offset; 233 } 234 return *this; 235 } 236 237 bool 238 Address::ResolveAddressUsingFileSections (addr_t file_addr, const SectionList *section_list) 239 { 240 if (section_list) 241 { 242 SectionSP section_sp (section_list->FindSectionContainingFileAddress(file_addr)); 243 m_section_wp = section_sp; 244 if (section_sp) 245 { 246 assert( section_sp->ContainsFileAddress(file_addr) ); 247 m_offset = file_addr - section_sp->GetFileAddress(); 248 return true; // Successfully transformed addr into a section offset address 249 } 250 } 251 m_offset = file_addr; 252 return false; // Failed to resolve this address to a section offset value 253 } 254 255 ModuleSP 256 Address::GetModule () const 257 { 258 lldb::ModuleSP module_sp; 259 SectionSP section_sp (GetSection()); 260 if (section_sp) 261 module_sp = section_sp->GetModule(); 262 return module_sp; 263 } 264 265 addr_t 266 Address::GetFileAddress () const 267 { 268 SectionSP section_sp (GetSection()); 269 if (section_sp) 270 { 271 addr_t sect_file_addr = section_sp->GetFileAddress(); 272 if (sect_file_addr == LLDB_INVALID_ADDRESS) 273 { 274 // Section isn't resolved, we can't return a valid file address 275 return LLDB_INVALID_ADDRESS; 276 } 277 // We have a valid file range, so we can return the file based 278 // address by adding the file base address to our offset 279 return sect_file_addr + m_offset; 280 } 281 // No section, we just return the offset since it is the value in this case 282 return m_offset; 283 } 284 285 addr_t 286 Address::GetLoadAddress (Target *target) const 287 { 288 SectionSP section_sp (GetSection()); 289 if (!section_sp) 290 { 291 // No section, we just return the offset since it is the value in this case 292 return m_offset; 293 } 294 295 if (target) 296 { 297 addr_t sect_load_addr = section_sp->GetLoadBaseAddress (target); 298 299 if (sect_load_addr != LLDB_INVALID_ADDRESS) 300 { 301 // We have a valid file range, so we can return the file based 302 // address by adding the file base address to our offset 303 return sect_load_addr + m_offset; 304 } 305 } 306 // The section isn't resolved or no process was supplied so we can't 307 // return a valid file address. 308 return LLDB_INVALID_ADDRESS; 309 } 310 311 addr_t 312 Address::GetCallableLoadAddress (Target *target) const 313 { 314 addr_t code_addr = GetLoadAddress (target); 315 316 if (target) 317 return target->GetCallableLoadAddress (code_addr, GetAddressClass()); 318 return code_addr; 319 } 320 321 bool 322 Address::SetCallableLoadAddress (lldb::addr_t load_addr, Target *target) 323 { 324 if (SetLoadAddress (load_addr, target)) 325 { 326 if (target) 327 m_offset = target->GetCallableLoadAddress(m_offset, GetAddressClass()); 328 return true; 329 } 330 return false; 331 } 332 333 addr_t 334 Address::GetOpcodeLoadAddress (Target *target) const 335 { 336 addr_t code_addr = GetLoadAddress (target); 337 if (code_addr != LLDB_INVALID_ADDRESS) 338 code_addr = target->GetOpcodeLoadAddress (code_addr, GetAddressClass()); 339 return code_addr; 340 } 341 342 bool 343 Address::SetOpcodeLoadAddress (lldb::addr_t load_addr, Target *target) 344 { 345 if (SetLoadAddress (load_addr, target)) 346 { 347 if (target) 348 m_offset = target->GetOpcodeLoadAddress (m_offset, GetAddressClass()); 349 return true; 350 } 351 return false; 352 } 353 354 bool 355 Address::Dump (Stream *s, ExecutionContextScope *exe_scope, DumpStyle style, DumpStyle fallback_style, uint32_t addr_size) const 356 { 357 // If the section was NULL, only load address is going to work. 358 SectionSP section_sp (GetSection()); 359 if (!section_sp) 360 style = DumpStyleLoadAddress; 361 362 ExecutionContext exe_ctx (exe_scope); 363 Target *target = exe_ctx.GetTargetPtr(); 364 // If addr_byte_size is UINT32_MAX, then determine the correct address 365 // byte size for the process or default to the size of addr_t 366 if (addr_size == UINT32_MAX) 367 { 368 if (target) 369 addr_size = target->GetArchitecture().GetAddressByteSize (); 370 else 371 addr_size = sizeof(addr_t); 372 } 373 374 Address so_addr; 375 switch (style) 376 { 377 case DumpStyleInvalid: 378 return false; 379 380 case DumpStyleSectionNameOffset: 381 if (section_sp) 382 { 383 section_sp->DumpName(s); 384 s->Printf (" + %llu", m_offset); 385 } 386 else 387 { 388 s->Address(m_offset, addr_size); 389 } 390 break; 391 392 case DumpStyleSectionPointerOffset: 393 s->Printf("(Section *)%p + ", section_sp.get()); 394 s->Address(m_offset, addr_size); 395 break; 396 397 case DumpStyleModuleWithFileAddress: 398 if (section_sp) 399 s->Printf("%s[", section_sp->GetModule()->GetFileSpec().GetFilename().AsCString()); 400 // Fall through 401 case DumpStyleFileAddress: 402 { 403 addr_t file_addr = GetFileAddress(); 404 if (file_addr == LLDB_INVALID_ADDRESS) 405 { 406 if (fallback_style != DumpStyleInvalid) 407 return Dump (s, exe_scope, fallback_style, DumpStyleInvalid, addr_size); 408 return false; 409 } 410 s->Address (file_addr, addr_size); 411 if (style == DumpStyleModuleWithFileAddress && section_sp) 412 s->PutChar(']'); 413 } 414 break; 415 416 case DumpStyleLoadAddress: 417 { 418 addr_t load_addr = GetLoadAddress (target); 419 if (load_addr == LLDB_INVALID_ADDRESS) 420 { 421 if (fallback_style != DumpStyleInvalid) 422 return Dump (s, exe_scope, fallback_style, DumpStyleInvalid, addr_size); 423 return false; 424 } 425 s->Address (load_addr, addr_size); 426 } 427 break; 428 429 case DumpStyleResolvedDescription: 430 case DumpStyleResolvedDescriptionNoModule: 431 if (IsSectionOffset()) 432 { 433 AddressType addr_type = eAddressTypeLoad; 434 addr_t addr = GetLoadAddress (target); 435 if (addr == LLDB_INVALID_ADDRESS) 436 { 437 addr = GetFileAddress(); 438 addr_type = eAddressTypeFile; 439 } 440 441 uint32_t pointer_size = 4; 442 ModuleSP module_sp (GetModule()); 443 if (target) 444 pointer_size = target->GetArchitecture().GetAddressByteSize(); 445 else if (module_sp) 446 pointer_size = module_sp->GetArchitecture().GetAddressByteSize(); 447 448 bool showed_info = false; 449 if (section_sp) 450 { 451 SectionType sect_type = section_sp->GetType(); 452 switch (sect_type) 453 { 454 case eSectionTypeData: 455 if (module_sp) 456 { 457 ObjectFile *objfile = module_sp->GetObjectFile(); 458 if (objfile) 459 { 460 Symtab *symtab = objfile->GetSymtab(); 461 if (symtab) 462 { 463 const addr_t file_Addr = GetFileAddress(); 464 Symbol *symbol = symtab->FindSymbolContainingFileAddress (file_Addr); 465 if (symbol) 466 { 467 const char *symbol_name = symbol->GetName().AsCString(); 468 if (symbol_name) 469 { 470 s->PutCString(symbol_name); 471 addr_t delta = file_Addr - symbol->GetAddress().GetFileAddress(); 472 if (delta) 473 s->Printf(" + %llu", delta); 474 showed_info = true; 475 } 476 } 477 } 478 } 479 } 480 break; 481 482 case eSectionTypeDataCString: 483 // Read the C string from memory and display it 484 showed_info = true; 485 ReadCStringFromMemory (exe_scope, *this, s); 486 break; 487 488 case eSectionTypeDataCStringPointers: 489 { 490 if (ReadAddress (exe_scope, *this, pointer_size, so_addr)) 491 { 492 #if VERBOSE_OUTPUT 493 s->PutCString("(char *)"); 494 so_addr.Dump(s, exe_scope, DumpStyleLoadAddress, DumpStyleFileAddress); 495 s->PutCString(": "); 496 #endif 497 showed_info = true; 498 ReadCStringFromMemory (exe_scope, so_addr, s); 499 } 500 } 501 break; 502 503 case eSectionTypeDataObjCMessageRefs: 504 { 505 if (ReadAddress (exe_scope, *this, pointer_size, so_addr)) 506 { 507 if (target && so_addr.IsSectionOffset()) 508 { 509 SymbolContext func_sc; 510 target->GetImages().ResolveSymbolContextForAddress (so_addr, 511 eSymbolContextEverything, 512 func_sc); 513 if (func_sc.function || func_sc.symbol) 514 { 515 showed_info = true; 516 #if VERBOSE_OUTPUT 517 s->PutCString ("(objc_msgref *) -> { (func*)"); 518 so_addr.Dump(s, exe_scope, DumpStyleLoadAddress, DumpStyleFileAddress); 519 #else 520 s->PutCString ("{ "); 521 #endif 522 Address cstr_addr(*this); 523 cstr_addr.SetOffset(cstr_addr.GetOffset() + pointer_size); 524 func_sc.DumpStopContext(s, exe_scope, so_addr, true, true, false); 525 if (ReadAddress (exe_scope, cstr_addr, pointer_size, so_addr)) 526 { 527 #if VERBOSE_OUTPUT 528 s->PutCString("), (char *)"); 529 so_addr.Dump(s, exe_scope, DumpStyleLoadAddress, DumpStyleFileAddress); 530 s->PutCString(" ("); 531 #else 532 s->PutCString(", "); 533 #endif 534 ReadCStringFromMemory (exe_scope, so_addr, s); 535 } 536 #if VERBOSE_OUTPUT 537 s->PutCString(") }"); 538 #else 539 s->PutCString(" }"); 540 #endif 541 } 542 } 543 } 544 } 545 break; 546 547 case eSectionTypeDataObjCCFStrings: 548 { 549 Address cfstring_data_addr(*this); 550 cfstring_data_addr.SetOffset(cfstring_data_addr.GetOffset() + (2 * pointer_size)); 551 if (ReadAddress (exe_scope, cfstring_data_addr, pointer_size, so_addr)) 552 { 553 #if VERBOSE_OUTPUT 554 s->PutCString("(CFString *) "); 555 cfstring_data_addr.Dump(s, exe_scope, DumpStyleLoadAddress, DumpStyleFileAddress); 556 s->PutCString(" -> @"); 557 #else 558 s->PutChar('@'); 559 #endif 560 if (so_addr.Dump(s, exe_scope, DumpStyleResolvedDescription)) 561 showed_info = true; 562 } 563 } 564 break; 565 566 case eSectionTypeData4: 567 // Read the 4 byte data and display it 568 showed_info = true; 569 s->PutCString("(uint32_t) "); 570 DumpUInt (exe_scope, *this, 4, s); 571 break; 572 573 case eSectionTypeData8: 574 // Read the 8 byte data and display it 575 showed_info = true; 576 s->PutCString("(uint64_t) "); 577 DumpUInt (exe_scope, *this, 8, s); 578 break; 579 580 case eSectionTypeData16: 581 // Read the 16 byte data and display it 582 showed_info = true; 583 s->PutCString("(uint128_t) "); 584 DumpUInt (exe_scope, *this, 16, s); 585 break; 586 587 case eSectionTypeDataPointers: 588 // Read the pointer data and display it 589 { 590 if (ReadAddress (exe_scope, *this, pointer_size, so_addr)) 591 { 592 s->PutCString ("(void *)"); 593 so_addr.Dump(s, exe_scope, DumpStyleLoadAddress, DumpStyleFileAddress); 594 595 showed_info = true; 596 if (so_addr.IsSectionOffset()) 597 { 598 SymbolContext pointer_sc; 599 if (target) 600 { 601 target->GetImages().ResolveSymbolContextForAddress (so_addr, 602 eSymbolContextEverything, 603 pointer_sc); 604 if (pointer_sc.function || pointer_sc.symbol) 605 { 606 s->PutCString(": "); 607 pointer_sc.DumpStopContext(s, exe_scope, so_addr, true, false, false); 608 } 609 } 610 } 611 } 612 } 613 break; 614 615 default: 616 break; 617 } 618 } 619 620 if (!showed_info) 621 { 622 if (module_sp) 623 { 624 SymbolContext sc; 625 module_sp->ResolveSymbolContextForAddress(*this, eSymbolContextEverything, sc); 626 if (sc.function || sc.symbol) 627 { 628 bool show_stop_context = true; 629 const bool show_module = (style == DumpStyleResolvedDescription); 630 const bool show_fullpaths = false; 631 const bool show_inlined_frames = true; 632 if (sc.function == NULL && sc.symbol != NULL) 633 { 634 // If we have just a symbol make sure it is in the right section 635 if (sc.symbol->ValueIsAddress()) 636 { 637 if (sc.symbol->GetAddress().GetSection() != GetSection()) 638 { 639 // don't show the module if the symbol is a trampoline symbol 640 show_stop_context = false; 641 } 642 } 643 } 644 if (show_stop_context) 645 { 646 // We have a function or a symbol from the same 647 // sections as this address. 648 sc.DumpStopContext (s, 649 exe_scope, 650 *this, 651 show_fullpaths, 652 show_module, 653 show_inlined_frames); 654 } 655 else 656 { 657 // We found a symbol but it was in a different 658 // section so it isn't the symbol we should be 659 // showing, just show the section name + offset 660 Dump (s, exe_scope, DumpStyleSectionNameOffset); 661 } 662 } 663 } 664 } 665 } 666 else 667 { 668 if (fallback_style != DumpStyleInvalid) 669 return Dump (s, exe_scope, fallback_style, DumpStyleInvalid, addr_size); 670 return false; 671 } 672 break; 673 674 case DumpStyleDetailedSymbolContext: 675 if (IsSectionOffset()) 676 { 677 ModuleSP module_sp (GetModule()); 678 if (module_sp) 679 { 680 SymbolContext sc; 681 module_sp->ResolveSymbolContextForAddress(*this, eSymbolContextEverything, sc); 682 if (sc.symbol) 683 { 684 // If we have just a symbol make sure it is in the same section 685 // as our address. If it isn't, then we might have just found 686 // the last symbol that came before the address that we are 687 // looking up that has nothing to do with our address lookup. 688 if (sc.symbol->ValueIsAddress() && sc.symbol->GetAddress().GetSection() != GetSection()) 689 sc.symbol = NULL; 690 } 691 sc.GetDescription(s, eDescriptionLevelBrief, target); 692 693 if (sc.block) 694 { 695 bool can_create = true; 696 bool get_parent_variables = true; 697 bool stop_if_block_is_inlined_function = false; 698 VariableList variable_list; 699 sc.block->AppendVariables (can_create, 700 get_parent_variables, 701 stop_if_block_is_inlined_function, 702 &variable_list); 703 704 uint32_t num_variables = variable_list.GetSize(); 705 for (uint32_t var_idx = 0; var_idx < num_variables; ++var_idx) 706 { 707 Variable *var = variable_list.GetVariableAtIndex (var_idx).get(); 708 if (var && var->LocationIsValidForAddress (*this)) 709 { 710 s->Indent(); 711 s->Printf (" Variable: id = {0x%8.8llx}, name = \"%s\", type= \"%s\", location =", 712 var->GetID(), 713 var->GetName().GetCString(), 714 var->GetType()->GetName().GetCString()); 715 var->DumpLocationForAddress(s, *this); 716 s->PutCString(", decl = "); 717 var->GetDeclaration().DumpStopContext(s, false); 718 s->EOL(); 719 } 720 } 721 } 722 } 723 } 724 else 725 { 726 if (fallback_style != DumpStyleInvalid) 727 return Dump (s, exe_scope, fallback_style, DumpStyleInvalid, addr_size); 728 return false; 729 } 730 break; 731 } 732 733 return true; 734 } 735 736 uint32_t 737 Address::CalculateSymbolContext (SymbolContext *sc, uint32_t resolve_scope) const 738 { 739 sc->Clear(); 740 // Absolute addresses don't have enough information to reconstruct even their target. 741 742 SectionSP section_sp (GetSection()); 743 if (section_sp) 744 { 745 ModuleSP module_sp (section_sp->GetModule()); 746 if (module_sp) 747 { 748 sc->module_sp = module_sp; 749 if (sc->module_sp) 750 return sc->module_sp->ResolveSymbolContextForAddress (*this, resolve_scope, *sc); 751 } 752 } 753 return 0; 754 } 755 756 ModuleSP 757 Address::CalculateSymbolContextModule () const 758 { 759 SectionSP section_sp (GetSection()); 760 if (section_sp) 761 return section_sp->GetModule(); 762 return ModuleSP(); 763 } 764 765 CompileUnit * 766 Address::CalculateSymbolContextCompileUnit () const 767 { 768 SectionSP section_sp (GetSection()); 769 if (section_sp) 770 { 771 SymbolContext sc; 772 sc.module_sp = section_sp->GetModule(); 773 if (sc.module_sp) 774 { 775 sc.module_sp->ResolveSymbolContextForAddress (*this, eSymbolContextCompUnit, sc); 776 return sc.comp_unit; 777 } 778 } 779 return NULL; 780 } 781 782 Function * 783 Address::CalculateSymbolContextFunction () const 784 { 785 SectionSP section_sp (GetSection()); 786 if (section_sp) 787 { 788 SymbolContext sc; 789 sc.module_sp = section_sp->GetModule(); 790 if (sc.module_sp) 791 { 792 sc.module_sp->ResolveSymbolContextForAddress (*this, eSymbolContextFunction, sc); 793 return sc.function; 794 } 795 } 796 return NULL; 797 } 798 799 Block * 800 Address::CalculateSymbolContextBlock () const 801 { 802 SectionSP section_sp (GetSection()); 803 if (section_sp) 804 { 805 SymbolContext sc; 806 sc.module_sp = section_sp->GetModule(); 807 if (sc.module_sp) 808 { 809 sc.module_sp->ResolveSymbolContextForAddress (*this, eSymbolContextBlock, sc); 810 return sc.block; 811 } 812 } 813 return NULL; 814 } 815 816 Symbol * 817 Address::CalculateSymbolContextSymbol () const 818 { 819 SectionSP section_sp (GetSection()); 820 if (section_sp) 821 { 822 SymbolContext sc; 823 sc.module_sp = section_sp->GetModule(); 824 if (sc.module_sp) 825 { 826 sc.module_sp->ResolveSymbolContextForAddress (*this, eSymbolContextSymbol, sc); 827 return sc.symbol; 828 } 829 } 830 return NULL; 831 } 832 833 bool 834 Address::CalculateSymbolContextLineEntry (LineEntry &line_entry) const 835 { 836 SectionSP section_sp (GetSection()); 837 if (section_sp) 838 { 839 SymbolContext sc; 840 sc.module_sp = section_sp->GetModule(); 841 if (sc.module_sp) 842 { 843 sc.module_sp->ResolveSymbolContextForAddress (*this, eSymbolContextLineEntry, sc); 844 if (sc.line_entry.IsValid()) 845 { 846 line_entry = sc.line_entry; 847 return true; 848 } 849 } 850 } 851 line_entry.Clear(); 852 return false; 853 } 854 855 int 856 Address::CompareFileAddress (const Address& a, const Address& b) 857 { 858 addr_t a_file_addr = a.GetFileAddress(); 859 addr_t b_file_addr = b.GetFileAddress(); 860 if (a_file_addr < b_file_addr) 861 return -1; 862 if (a_file_addr > b_file_addr) 863 return +1; 864 return 0; 865 } 866 867 868 int 869 Address::CompareLoadAddress (const Address& a, const Address& b, Target *target) 870 { 871 assert (target != NULL); 872 addr_t a_load_addr = a.GetLoadAddress (target); 873 addr_t b_load_addr = b.GetLoadAddress (target); 874 if (a_load_addr < b_load_addr) 875 return -1; 876 if (a_load_addr > b_load_addr) 877 return +1; 878 return 0; 879 } 880 881 int 882 Address::CompareModulePointerAndOffset (const Address& a, const Address& b) 883 { 884 ModuleSP a_module_sp (a.GetModule()); 885 ModuleSP b_module_sp (b.GetModule()); 886 Module *a_module = a_module_sp.get(); 887 Module *b_module = b_module_sp.get(); 888 if (a_module < b_module) 889 return -1; 890 if (a_module > b_module) 891 return +1; 892 // Modules are the same, just compare the file address since they should 893 // be unique 894 addr_t a_file_addr = a.GetFileAddress(); 895 addr_t b_file_addr = b.GetFileAddress(); 896 if (a_file_addr < b_file_addr) 897 return -1; 898 if (a_file_addr > b_file_addr) 899 return +1; 900 return 0; 901 } 902 903 904 size_t 905 Address::MemorySize () const 906 { 907 // Noting special for the memory size of a single Address object, 908 // it is just the size of itself. 909 return sizeof(Address); 910 } 911 912 913 //---------------------------------------------------------------------- 914 // NOTE: Be careful using this operator. It can correctly compare two 915 // addresses from the same Module correctly. It can't compare two 916 // addresses from different modules in any meaningful way, but it will 917 // compare the module pointers. 918 // 919 // To sum things up: 920 // - works great for addresses within the same module 921 // - it works for addresses across multiple modules, but don't expect the 922 // address results to make much sense 923 // 924 // This basically lets Address objects be used in ordered collection 925 // classes. 926 //---------------------------------------------------------------------- 927 928 bool 929 lldb_private::operator< (const Address& lhs, const Address& rhs) 930 { 931 ModuleSP lhs_module_sp (lhs.GetModule()); 932 ModuleSP rhs_module_sp (rhs.GetModule()); 933 Module *lhs_module = lhs_module_sp.get(); 934 Module *rhs_module = rhs_module_sp.get(); 935 if (lhs_module == rhs_module) 936 { 937 // Addresses are in the same module, just compare the file addresses 938 return lhs.GetFileAddress() < rhs.GetFileAddress(); 939 } 940 else 941 { 942 // The addresses are from different modules, just use the module 943 // pointer value to get consistent ordering 944 return lhs_module < rhs_module; 945 } 946 } 947 948 bool 949 lldb_private::operator> (const Address& lhs, const Address& rhs) 950 { 951 ModuleSP lhs_module_sp (lhs.GetModule()); 952 ModuleSP rhs_module_sp (rhs.GetModule()); 953 Module *lhs_module = lhs_module_sp.get(); 954 Module *rhs_module = rhs_module_sp.get(); 955 if (lhs_module == rhs_module) 956 { 957 // Addresses are in the same module, just compare the file addresses 958 return lhs.GetFileAddress() > rhs.GetFileAddress(); 959 } 960 else 961 { 962 // The addresses are from different modules, just use the module 963 // pointer value to get consistent ordering 964 return lhs_module > rhs_module; 965 } 966 } 967 968 969 // The operator == checks for exact equality only (same section, same offset) 970 bool 971 lldb_private::operator== (const Address& a, const Address& rhs) 972 { 973 return a.GetSection() == rhs.GetSection() && 974 a.GetOffset() == rhs.GetOffset(); 975 } 976 // The operator != checks for exact inequality only (differing section, or 977 // different offset) 978 bool 979 lldb_private::operator!= (const Address& a, const Address& rhs) 980 { 981 return a.GetSection() != rhs.GetSection() || 982 a.GetOffset() != rhs.GetOffset(); 983 } 984 985 bool 986 Address::IsLinkedAddress () const 987 { 988 SectionSP section_sp (GetSection()); 989 return section_sp && section_sp->GetLinkedSection(); 990 } 991 992 993 void 994 Address::ResolveLinkedAddress () 995 { 996 SectionSP section_sp (GetSection()); 997 if (section_sp) 998 { 999 SectionSP linked_section_sp (section_sp->GetLinkedSection()); 1000 if (linked_section_sp) 1001 { 1002 m_offset += section_sp->GetLinkedOffset(); 1003 m_section_wp = linked_section_sp; 1004 } 1005 } 1006 } 1007 1008 AddressClass 1009 Address::GetAddressClass () const 1010 { 1011 ModuleSP module_sp (GetModule()); 1012 if (module_sp) 1013 { 1014 ObjectFile *obj_file = module_sp->GetObjectFile(); 1015 if (obj_file) 1016 return obj_file->GetAddressClass (GetFileAddress()); 1017 } 1018 return eAddressClassUnknown; 1019 } 1020 1021 bool 1022 Address::SetLoadAddress (lldb::addr_t load_addr, Target *target) 1023 { 1024 if (target && target->GetSectionLoadList().ResolveLoadAddress(load_addr, *this)) 1025 return true; 1026 m_section_wp.reset(); 1027 m_offset = load_addr; 1028 return false; 1029 } 1030 1031