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->Printf (" Variable: id = {0x%8.8llx}, name = \"%s\", type= \"%s\", location =", 711 var->GetID(), 712 var->GetName().GetCString(), 713 var->GetType()->GetName().GetCString()); 714 var->DumpLocationForAddress(s, *this); 715 s->PutCString(", decl = "); 716 var->GetDeclaration().DumpStopContext(s, false); 717 s->EOL(); 718 } 719 } 720 } 721 } 722 } 723 else 724 { 725 if (fallback_style != DumpStyleInvalid) 726 return Dump (s, exe_scope, fallback_style, DumpStyleInvalid, addr_size); 727 return false; 728 } 729 break; 730 } 731 732 return true; 733 } 734 735 uint32_t 736 Address::CalculateSymbolContext (SymbolContext *sc, uint32_t resolve_scope) const 737 { 738 sc->Clear(); 739 // Absolute addresses don't have enough information to reconstruct even their target. 740 741 SectionSP section_sp (GetSection()); 742 if (section_sp) 743 { 744 ModuleSP module_sp (section_sp->GetModule()); 745 if (module_sp) 746 { 747 sc->module_sp = module_sp; 748 if (sc->module_sp) 749 return sc->module_sp->ResolveSymbolContextForAddress (*this, resolve_scope, *sc); 750 } 751 } 752 return 0; 753 } 754 755 ModuleSP 756 Address::CalculateSymbolContextModule () const 757 { 758 SectionSP section_sp (GetSection()); 759 if (section_sp) 760 return section_sp->GetModule(); 761 return ModuleSP(); 762 } 763 764 CompileUnit * 765 Address::CalculateSymbolContextCompileUnit () const 766 { 767 SectionSP section_sp (GetSection()); 768 if (section_sp) 769 { 770 SymbolContext sc; 771 sc.module_sp = section_sp->GetModule(); 772 if (sc.module_sp) 773 { 774 sc.module_sp->ResolveSymbolContextForAddress (*this, eSymbolContextCompUnit, sc); 775 return sc.comp_unit; 776 } 777 } 778 return NULL; 779 } 780 781 Function * 782 Address::CalculateSymbolContextFunction () const 783 { 784 SectionSP section_sp (GetSection()); 785 if (section_sp) 786 { 787 SymbolContext sc; 788 sc.module_sp = section_sp->GetModule(); 789 if (sc.module_sp) 790 { 791 sc.module_sp->ResolveSymbolContextForAddress (*this, eSymbolContextFunction, sc); 792 return sc.function; 793 } 794 } 795 return NULL; 796 } 797 798 Block * 799 Address::CalculateSymbolContextBlock () const 800 { 801 SectionSP section_sp (GetSection()); 802 if (section_sp) 803 { 804 SymbolContext sc; 805 sc.module_sp = section_sp->GetModule(); 806 if (sc.module_sp) 807 { 808 sc.module_sp->ResolveSymbolContextForAddress (*this, eSymbolContextBlock, sc); 809 return sc.block; 810 } 811 } 812 return NULL; 813 } 814 815 Symbol * 816 Address::CalculateSymbolContextSymbol () const 817 { 818 SectionSP section_sp (GetSection()); 819 if (section_sp) 820 { 821 SymbolContext sc; 822 sc.module_sp = section_sp->GetModule(); 823 if (sc.module_sp) 824 { 825 sc.module_sp->ResolveSymbolContextForAddress (*this, eSymbolContextSymbol, sc); 826 return sc.symbol; 827 } 828 } 829 return NULL; 830 } 831 832 bool 833 Address::CalculateSymbolContextLineEntry (LineEntry &line_entry) const 834 { 835 SectionSP section_sp (GetSection()); 836 if (section_sp) 837 { 838 SymbolContext sc; 839 sc.module_sp = section_sp->GetModule(); 840 if (sc.module_sp) 841 { 842 sc.module_sp->ResolveSymbolContextForAddress (*this, eSymbolContextLineEntry, sc); 843 if (sc.line_entry.IsValid()) 844 { 845 line_entry = sc.line_entry; 846 return true; 847 } 848 } 849 } 850 line_entry.Clear(); 851 return false; 852 } 853 854 int 855 Address::CompareFileAddress (const Address& a, const Address& b) 856 { 857 addr_t a_file_addr = a.GetFileAddress(); 858 addr_t b_file_addr = b.GetFileAddress(); 859 if (a_file_addr < b_file_addr) 860 return -1; 861 if (a_file_addr > b_file_addr) 862 return +1; 863 return 0; 864 } 865 866 867 int 868 Address::CompareLoadAddress (const Address& a, const Address& b, Target *target) 869 { 870 assert (target != NULL); 871 addr_t a_load_addr = a.GetLoadAddress (target); 872 addr_t b_load_addr = b.GetLoadAddress (target); 873 if (a_load_addr < b_load_addr) 874 return -1; 875 if (a_load_addr > b_load_addr) 876 return +1; 877 return 0; 878 } 879 880 int 881 Address::CompareModulePointerAndOffset (const Address& a, const Address& b) 882 { 883 ModuleSP a_module_sp (a.GetModule()); 884 ModuleSP b_module_sp (b.GetModule()); 885 Module *a_module = a_module_sp.get(); 886 Module *b_module = b_module_sp.get(); 887 if (a_module < b_module) 888 return -1; 889 if (a_module > b_module) 890 return +1; 891 // Modules are the same, just compare the file address since they should 892 // be unique 893 addr_t a_file_addr = a.GetFileAddress(); 894 addr_t b_file_addr = b.GetFileAddress(); 895 if (a_file_addr < b_file_addr) 896 return -1; 897 if (a_file_addr > b_file_addr) 898 return +1; 899 return 0; 900 } 901 902 903 size_t 904 Address::MemorySize () const 905 { 906 // Noting special for the memory size of a single Address object, 907 // it is just the size of itself. 908 return sizeof(Address); 909 } 910 911 912 //---------------------------------------------------------------------- 913 // NOTE: Be careful using this operator. It can correctly compare two 914 // addresses from the same Module correctly. It can't compare two 915 // addresses from different modules in any meaningful way, but it will 916 // compare the module pointers. 917 // 918 // To sum things up: 919 // - works great for addresses within the same module 920 // - it works for addresses across multiple modules, but don't expect the 921 // address results to make much sense 922 // 923 // This basically lets Address objects be used in ordered collection 924 // classes. 925 //---------------------------------------------------------------------- 926 927 bool 928 lldb_private::operator< (const Address& lhs, const Address& rhs) 929 { 930 ModuleSP lhs_module_sp (lhs.GetModule()); 931 ModuleSP rhs_module_sp (rhs.GetModule()); 932 Module *lhs_module = lhs_module_sp.get(); 933 Module *rhs_module = rhs_module_sp.get(); 934 if (lhs_module == rhs_module) 935 { 936 // Addresses are in the same module, just compare the file addresses 937 return lhs.GetFileAddress() < rhs.GetFileAddress(); 938 } 939 else 940 { 941 // The addresses are from different modules, just use the module 942 // pointer value to get consistent ordering 943 return lhs_module < rhs_module; 944 } 945 } 946 947 bool 948 lldb_private::operator> (const Address& lhs, const Address& rhs) 949 { 950 ModuleSP lhs_module_sp (lhs.GetModule()); 951 ModuleSP rhs_module_sp (rhs.GetModule()); 952 Module *lhs_module = lhs_module_sp.get(); 953 Module *rhs_module = rhs_module_sp.get(); 954 if (lhs_module == rhs_module) 955 { 956 // Addresses are in the same module, just compare the file addresses 957 return lhs.GetFileAddress() > rhs.GetFileAddress(); 958 } 959 else 960 { 961 // The addresses are from different modules, just use the module 962 // pointer value to get consistent ordering 963 return lhs_module > rhs_module; 964 } 965 } 966 967 968 // The operator == checks for exact equality only (same section, same offset) 969 bool 970 lldb_private::operator== (const Address& a, const Address& rhs) 971 { 972 return a.GetSection() == rhs.GetSection() && 973 a.GetOffset() == rhs.GetOffset(); 974 } 975 // The operator != checks for exact inequality only (differing section, or 976 // different offset) 977 bool 978 lldb_private::operator!= (const Address& a, const Address& rhs) 979 { 980 return a.GetSection() != rhs.GetSection() || 981 a.GetOffset() != rhs.GetOffset(); 982 } 983 984 bool 985 Address::IsLinkedAddress () const 986 { 987 SectionSP section_sp (GetSection()); 988 return section_sp && section_sp->GetLinkedSection(); 989 } 990 991 992 void 993 Address::ResolveLinkedAddress () 994 { 995 SectionSP section_sp (GetSection()); 996 if (section_sp) 997 { 998 SectionSP linked_section_sp (section_sp->GetLinkedSection()); 999 if (linked_section_sp) 1000 { 1001 m_offset += section_sp->GetLinkedOffset(); 1002 m_section_wp = linked_section_sp; 1003 } 1004 } 1005 } 1006 1007 AddressClass 1008 Address::GetAddressClass () const 1009 { 1010 ModuleSP module_sp (GetModule()); 1011 if (module_sp) 1012 { 1013 ObjectFile *obj_file = module_sp->GetObjectFile(); 1014 if (obj_file) 1015 return obj_file->GetAddressClass (GetFileAddress()); 1016 } 1017 return eAddressClassUnknown; 1018 } 1019 1020 bool 1021 Address::SetLoadAddress (lldb::addr_t load_addr, Target *target) 1022 { 1023 if (target && target->GetSectionLoadList().ResolveLoadAddress(load_addr, *this)) 1024 return true; 1025 m_section_wp.reset(); 1026 m_offset = load_addr; 1027 return false; 1028 } 1029 1030