1 //===-- Address.cpp ---------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Core/Address.h"
11 #include "lldb/Core/DumpDataExtractor.h"
12 #include "lldb/Core/Module.h"
13 #include "lldb/Core/ModuleList.h" // for ModuleList
14 #include "lldb/Core/Section.h"
15 #include "lldb/Symbol/Block.h"
16 #include "lldb/Symbol/Declaration.h" // for Declaration
17 #include "lldb/Symbol/LineEntry.h" // for LineEntry
18 #include "lldb/Symbol/ObjectFile.h"
19 #include "lldb/Symbol/Symbol.h" // for Symbol
20 #include "lldb/Symbol/SymbolContext.h" // for SymbolContext
21 #include "lldb/Symbol/SymbolVendor.h"
22 #include "lldb/Symbol/Symtab.h" // for Symtab
23 #include "lldb/Symbol/Type.h" // for Type
24 #include "lldb/Symbol/Variable.h"
25 #include "lldb/Symbol/VariableList.h"
26 #include "lldb/Target/ExecutionContext.h"
27 #include "lldb/Target/ExecutionContextScope.h" // for ExecutionContextScope
28 #include "lldb/Target/Process.h"
29 #include "lldb/Target/SectionLoadList.h"
30 #include "lldb/Target/Target.h"
31 #include "lldb/Utility/ConstString.h" // for ConstString
32 #include "lldb/Utility/DataExtractor.h" // for DataExtractor
33 #include "lldb/Utility/Endian.h" // for InlHostByteOrder
34 #include "lldb/Utility/FileSpec.h" // for FileSpec
35 #include "lldb/Utility/Status.h" // for Status
36 #include "lldb/Utility/Stream.h" // for Stream
37 #include "lldb/Utility/StreamString.h" // for StreamString
39 #include "llvm/ADT/StringRef.h" // for StringRef
40 #include "llvm/ADT/Triple.h"
41 #include "llvm/Support/Compiler.h" // for LLVM_FALLTHROUGH
43 #include <cstdint> // for uint8_t, uint32_t
44 #include <memory> // for shared_ptr, operator!=
45 #include <vector> // for vector
47 #include <assert.h> // for assert
48 #include <inttypes.h> // for PRIu64, PRIx64
49 #include <string.h> // for size_t, strlen
51 namespace lldb_private {
54 namespace lldb_private {
59 using namespace lldb_private;
61 static size_t ReadBytes(ExecutionContextScope *exe_scope,
62 const Address &address, void *dst, size_t dst_len) {
63 if (exe_scope == nullptr)
66 TargetSP target_sp(exe_scope->CalculateTarget());
69 bool prefer_file_cache = false;
70 return target_sp->ReadMemory(address, prefer_file_cache, dst, dst_len,
76 static bool GetByteOrderAndAddressSize(ExecutionContextScope *exe_scope,
77 const Address &address,
78 ByteOrder &byte_order,
79 uint32_t &addr_size) {
80 byte_order = eByteOrderInvalid;
82 if (exe_scope == nullptr)
85 TargetSP target_sp(exe_scope->CalculateTarget());
87 byte_order = target_sp->GetArchitecture().GetByteOrder();
88 addr_size = target_sp->GetArchitecture().GetAddressByteSize();
91 if (byte_order == eByteOrderInvalid || addr_size == 0) {
92 ModuleSP module_sp(address.GetModule());
94 byte_order = module_sp->GetArchitecture().GetByteOrder();
95 addr_size = module_sp->GetArchitecture().GetAddressByteSize();
98 return byte_order != eByteOrderInvalid && addr_size != 0;
101 static uint64_t ReadUIntMax64(ExecutionContextScope *exe_scope,
102 const Address &address, uint32_t byte_size,
105 if (exe_scope == nullptr || byte_size > sizeof(uint64_t)) {
111 success = ReadBytes(exe_scope, address, &buf, byte_size) == byte_size;
113 ByteOrder byte_order = eByteOrderInvalid;
114 uint32_t addr_size = 0;
115 if (GetByteOrderAndAddressSize(exe_scope, address, byte_order, addr_size)) {
116 DataExtractor data(&buf, sizeof(buf), byte_order, addr_size);
117 lldb::offset_t offset = 0;
118 uval64 = data.GetU64(&offset);
125 static bool ReadAddress(ExecutionContextScope *exe_scope,
126 const Address &address, uint32_t pointer_size,
127 Address &deref_so_addr) {
128 if (exe_scope == nullptr)
131 bool success = false;
132 addr_t deref_addr = ReadUIntMax64(exe_scope, address, pointer_size, success);
134 ExecutionContext exe_ctx;
135 exe_scope->CalculateExecutionContext(exe_ctx);
136 // If we have any sections that are loaded, try and resolve using the
138 Target *target = exe_ctx.GetTargetPtr();
139 if (target && !target->GetSectionLoadList().IsEmpty()) {
140 if (target->GetSectionLoadList().ResolveLoadAddress(deref_addr,
144 // If we were not running, yet able to read an integer, we must
146 ModuleSP module_sp(address.GetModule());
149 if (module_sp->ResolveFileAddress(deref_addr, deref_so_addr))
153 // We couldn't make "deref_addr" into a section offset value, but we were
154 // able to read the address, so we return a section offset address with
155 // no section and "deref_addr" as the offset (address).
156 deref_so_addr.SetRawAddress(deref_addr);
162 static bool DumpUInt(ExecutionContextScope *exe_scope, const Address &address,
163 uint32_t byte_size, Stream *strm) {
164 if (exe_scope == nullptr || byte_size == 0)
166 std::vector<uint8_t> buf(byte_size, 0);
168 if (ReadBytes(exe_scope, address, &buf[0], buf.size()) == buf.size()) {
169 ByteOrder byte_order = eByteOrderInvalid;
170 uint32_t addr_size = 0;
171 if (GetByteOrderAndAddressSize(exe_scope, address, byte_order, addr_size)) {
172 DataExtractor data(&buf.front(), buf.size(), byte_order, addr_size);
174 DumpDataExtractor(data, strm,
175 0, // Start offset in "data"
176 eFormatHex, // Print as characters
177 buf.size(), // Size of item
179 UINT32_MAX, // num per line
180 LLDB_INVALID_ADDRESS, // base address
181 0, // bitfield bit size
182 0); // bitfield bit offset
190 static size_t ReadCStringFromMemory(ExecutionContextScope *exe_scope,
191 const Address &address, Stream *strm) {
192 if (exe_scope == nullptr)
194 const size_t k_buf_len = 256;
195 char buf[k_buf_len + 1];
196 buf[k_buf_len] = '\0'; // NULL terminate
198 // Byte order and address size don't matter for C string dumping..
199 DataExtractor data(buf, sizeof(buf), endian::InlHostByteOrder(), 4);
200 size_t total_len = 0;
202 Address curr_address(address);
204 while ((bytes_read = ReadBytes(exe_scope, curr_address, buf, k_buf_len)) >
206 size_t len = strlen(buf);
209 if (len > bytes_read)
212 DumpDataExtractor(data, strm,
213 0, // Start offset in "data"
214 eFormatChar, // Print as characters
215 1, // Size of item (1 byte for a char!)
216 len, // How many bytes to print?
217 UINT32_MAX, // num per line
218 LLDB_INVALID_ADDRESS, // base address
219 0, // bitfield bit size
221 0); // bitfield bit offset
223 total_len += bytes_read;
227 curr_address.SetOffset(curr_address.GetOffset() + bytes_read);
233 Address::Address(lldb::addr_t abs_addr) : m_section_wp(), m_offset(abs_addr) {}
235 Address::Address(addr_t address, const SectionList *section_list)
236 : m_section_wp(), m_offset(LLDB_INVALID_ADDRESS) {
237 ResolveAddressUsingFileSections(address, section_list);
240 const Address &Address::operator=(const Address &rhs) {
242 m_section_wp = rhs.m_section_wp;
243 m_offset = rhs.m_offset;
248 bool Address::ResolveAddressUsingFileSections(addr_t file_addr,
249 const SectionList *section_list) {
251 SectionSP section_sp(
252 section_list->FindSectionContainingFileAddress(file_addr));
253 m_section_wp = section_sp;
255 assert(section_sp->ContainsFileAddress(file_addr));
256 m_offset = file_addr - section_sp->GetFileAddress();
257 return true; // Successfully transformed addr into a section offset
261 m_offset = file_addr;
262 return false; // Failed to resolve this address to a section offset value
265 ModuleSP Address::GetModule() const {
266 lldb::ModuleSP module_sp;
267 SectionSP section_sp(GetSection());
269 module_sp = section_sp->GetModule();
273 addr_t Address::GetFileAddress() const {
274 SectionSP section_sp(GetSection());
276 addr_t sect_file_addr = section_sp->GetFileAddress();
277 if (sect_file_addr == LLDB_INVALID_ADDRESS) {
278 // Section isn't resolved, we can't return a valid file address
279 return LLDB_INVALID_ADDRESS;
281 // We have a valid file range, so we can return the file based
282 // address by adding the file base address to our offset
283 return sect_file_addr + m_offset;
284 } else if (SectionWasDeletedPrivate()) {
285 // Used to have a valid section but it got deleted so the
286 // offset doesn't mean anything without the section
287 return LLDB_INVALID_ADDRESS;
289 // No section, we just return the offset since it is the value in this case
293 addr_t Address::GetLoadAddress(Target *target) const {
294 SectionSP section_sp(GetSection());
297 addr_t sect_load_addr = section_sp->GetLoadBaseAddress(target);
299 if (sect_load_addr != LLDB_INVALID_ADDRESS) {
300 // We have a valid file range, so we can return the file based
301 // address by adding the file base address to our offset
302 return sect_load_addr + m_offset;
305 } else if (SectionWasDeletedPrivate()) {
306 // Used to have a valid section but it got deleted so the
307 // offset doesn't mean anything without the section
308 return LLDB_INVALID_ADDRESS;
310 // We don't have a section so the offset is the load address
313 // The section isn't resolved or an invalid target was passed in
314 // so we can't return a valid load address.
315 return LLDB_INVALID_ADDRESS;
318 addr_t Address::GetCallableLoadAddress(Target *target, bool is_indirect) const {
319 addr_t code_addr = LLDB_INVALID_ADDRESS;
321 if (is_indirect && target) {
322 ProcessSP processSP = target->GetProcessSP();
325 code_addr = processSP->ResolveIndirectFunction(this, error);
326 if (!error.Success())
327 code_addr = LLDB_INVALID_ADDRESS;
330 code_addr = GetLoadAddress(target);
333 if (code_addr == LLDB_INVALID_ADDRESS)
337 return target->GetCallableLoadAddress(code_addr, GetAddressClass());
341 bool Address::SetCallableLoadAddress(lldb::addr_t load_addr, Target *target) {
342 if (SetLoadAddress(load_addr, target)) {
344 m_offset = target->GetCallableLoadAddress(m_offset, GetAddressClass());
350 addr_t Address::GetOpcodeLoadAddress(Target *target,
351 AddressClass addr_class) const {
352 addr_t code_addr = GetLoadAddress(target);
353 if (code_addr != LLDB_INVALID_ADDRESS) {
354 if (addr_class == eAddressClassInvalid)
355 addr_class = GetAddressClass();
356 code_addr = target->GetOpcodeLoadAddress(code_addr, addr_class);
361 bool Address::SetOpcodeLoadAddress(lldb::addr_t load_addr, Target *target,
362 AddressClass addr_class,
363 bool allow_section_end) {
364 if (SetLoadAddress(load_addr, target, allow_section_end)) {
366 if (addr_class == eAddressClassInvalid)
367 addr_class = GetAddressClass();
368 m_offset = target->GetOpcodeLoadAddress(m_offset, addr_class);
375 bool Address::Dump(Stream *s, ExecutionContextScope *exe_scope, DumpStyle style,
376 DumpStyle fallback_style, uint32_t addr_size) const {
377 // If the section was nullptr, only load address is going to work unless we
379 // trying to deref a pointer
380 SectionSP section_sp(GetSection());
381 if (!section_sp && style != DumpStyleResolvedPointerDescription)
382 style = DumpStyleLoadAddress;
384 ExecutionContext exe_ctx(exe_scope);
385 Target *target = exe_ctx.GetTargetPtr();
386 // If addr_byte_size is UINT32_MAX, then determine the correct address
387 // byte size for the process or default to the size of addr_t
388 if (addr_size == UINT32_MAX) {
390 addr_size = target->GetArchitecture().GetAddressByteSize();
392 addr_size = sizeof(addr_t);
397 case DumpStyleInvalid:
400 case DumpStyleSectionNameOffset:
402 section_sp->DumpName(s);
403 s->Printf(" + %" PRIu64, m_offset);
405 s->Address(m_offset, addr_size);
409 case DumpStyleSectionPointerOffset:
410 s->Printf("(Section *)%p + ", static_cast<void *>(section_sp.get()));
411 s->Address(m_offset, addr_size);
414 case DumpStyleModuleWithFileAddress:
416 ModuleSP module_sp = section_sp->GetModule();
418 s->Printf("%s[", module_sp->GetFileSpec().GetFilename().AsCString(
421 s->Printf("%s[", "<Unknown>");
424 case DumpStyleFileAddress: {
425 addr_t file_addr = GetFileAddress();
426 if (file_addr == LLDB_INVALID_ADDRESS) {
427 if (fallback_style != DumpStyleInvalid)
428 return Dump(s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
431 s->Address(file_addr, addr_size);
432 if (style == DumpStyleModuleWithFileAddress && section_sp)
436 case DumpStyleLoadAddress: {
437 addr_t load_addr = GetLoadAddress(target);
441 * Display address in compressed form for MIPS16 or microMIPS
442 * if the address belongs to eAddressClassCodeAlternateISA.
445 const llvm::Triple::ArchType llvm_arch =
446 target->GetArchitecture().GetMachine();
447 if (llvm_arch == llvm::Triple::mips ||
448 llvm_arch == llvm::Triple::mipsel ||
449 llvm_arch == llvm::Triple::mips64 ||
450 llvm_arch == llvm::Triple::mips64el)
451 load_addr = GetCallableLoadAddress(target);
454 if (load_addr == LLDB_INVALID_ADDRESS) {
455 if (fallback_style != DumpStyleInvalid)
456 return Dump(s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
459 s->Address(load_addr, addr_size);
462 case DumpStyleResolvedDescription:
463 case DumpStyleResolvedDescriptionNoModule:
464 case DumpStyleResolvedDescriptionNoFunctionArguments:
465 case DumpStyleNoFunctionName:
466 if (IsSectionOffset()) {
467 uint32_t pointer_size = 4;
468 ModuleSP module_sp(GetModule());
470 pointer_size = target->GetArchitecture().GetAddressByteSize();
472 pointer_size = module_sp->GetArchitecture().GetAddressByteSize();
474 bool showed_info = false;
476 SectionType sect_type = section_sp->GetType();
478 case eSectionTypeData:
480 SymbolVendor *sym_vendor = module_sp->GetSymbolVendor();
482 Symtab *symtab = sym_vendor->GetSymtab();
484 const addr_t file_Addr = GetFileAddress();
486 symtab->FindSymbolContainingFileAddress(file_Addr);
488 const char *symbol_name = symbol->GetName().AsCString();
490 s->PutCString(symbol_name);
492 file_Addr - symbol->GetAddressRef().GetFileAddress();
494 s->Printf(" + %" PRIu64, delta);
503 case eSectionTypeDataCString:
504 // Read the C string from memory and display it
506 ReadCStringFromMemory(exe_scope, *this, s);
509 case eSectionTypeDataCStringPointers:
510 if (ReadAddress(exe_scope, *this, pointer_size, so_addr)) {
512 s->PutCString("(char *)");
513 so_addr.Dump(s, exe_scope, DumpStyleLoadAddress,
514 DumpStyleFileAddress);
518 ReadCStringFromMemory(exe_scope, so_addr, s);
522 case eSectionTypeDataObjCMessageRefs:
523 if (ReadAddress(exe_scope, *this, pointer_size, so_addr)) {
524 if (target && so_addr.IsSectionOffset()) {
525 SymbolContext func_sc;
526 target->GetImages().ResolveSymbolContextForAddress(
527 so_addr, eSymbolContextEverything, func_sc);
528 if (func_sc.function != nullptr || func_sc.symbol != nullptr) {
531 s->PutCString("(objc_msgref *) -> { (func*)");
532 so_addr.Dump(s, exe_scope, DumpStyleLoadAddress,
533 DumpStyleFileAddress);
537 Address cstr_addr(*this);
538 cstr_addr.SetOffset(cstr_addr.GetOffset() + pointer_size);
539 func_sc.DumpStopContext(s, exe_scope, so_addr, true, true,
541 if (ReadAddress(exe_scope, cstr_addr, pointer_size, so_addr)) {
543 s->PutCString("), (char *)");
544 so_addr.Dump(s, exe_scope, DumpStyleLoadAddress,
545 DumpStyleFileAddress);
550 ReadCStringFromMemory(exe_scope, so_addr, s);
553 s->PutCString(") }");
562 case eSectionTypeDataObjCCFStrings: {
563 Address cfstring_data_addr(*this);
564 cfstring_data_addr.SetOffset(cfstring_data_addr.GetOffset() +
566 if (ReadAddress(exe_scope, cfstring_data_addr, pointer_size,
569 s->PutCString("(CFString *) ");
570 cfstring_data_addr.Dump(s, exe_scope, DumpStyleLoadAddress,
571 DumpStyleFileAddress);
572 s->PutCString(" -> @");
576 if (so_addr.Dump(s, exe_scope, DumpStyleResolvedDescription))
581 case eSectionTypeData4:
582 // Read the 4 byte data and display it
584 s->PutCString("(uint32_t) ");
585 DumpUInt(exe_scope, *this, 4, s);
588 case eSectionTypeData8:
589 // Read the 8 byte data and display it
591 s->PutCString("(uint64_t) ");
592 DumpUInt(exe_scope, *this, 8, s);
595 case eSectionTypeData16:
596 // Read the 16 byte data and display it
598 s->PutCString("(uint128_t) ");
599 DumpUInt(exe_scope, *this, 16, s);
602 case eSectionTypeDataPointers:
603 // Read the pointer data and display it
604 if (ReadAddress(exe_scope, *this, pointer_size, so_addr)) {
605 s->PutCString("(void *)");
606 so_addr.Dump(s, exe_scope, DumpStyleLoadAddress,
607 DumpStyleFileAddress);
610 if (so_addr.IsSectionOffset()) {
611 SymbolContext pointer_sc;
613 target->GetImages().ResolveSymbolContextForAddress(
614 so_addr, eSymbolContextEverything, pointer_sc);
615 if (pointer_sc.function != nullptr ||
616 pointer_sc.symbol != nullptr) {
618 pointer_sc.DumpStopContext(s, exe_scope, so_addr, true, false,
634 module_sp->ResolveSymbolContextForAddress(
635 *this, eSymbolContextEverything, sc);
636 if (sc.function || sc.symbol) {
637 bool show_stop_context = true;
638 const bool show_module = (style == DumpStyleResolvedDescription);
639 const bool show_fullpaths = false;
640 const bool show_inlined_frames = true;
641 const bool show_function_arguments =
642 (style != DumpStyleResolvedDescriptionNoFunctionArguments);
643 const bool show_function_name = (style != DumpStyleNoFunctionName);
644 if (sc.function == nullptr && sc.symbol != nullptr) {
645 // If we have just a symbol make sure it is in the right section
646 if (sc.symbol->ValueIsAddress()) {
647 if (sc.symbol->GetAddressRef().GetSection() != GetSection()) {
648 // don't show the module if the symbol is a trampoline symbol
649 show_stop_context = false;
653 if (show_stop_context) {
654 // We have a function or a symbol from the same
655 // sections as this address.
656 sc.DumpStopContext(s, exe_scope, *this, show_fullpaths,
657 show_module, show_inlined_frames,
658 show_function_arguments, show_function_name);
660 // We found a symbol but it was in a different
661 // section so it isn't the symbol we should be
662 // showing, just show the section name + offset
663 Dump(s, exe_scope, DumpStyleSectionNameOffset);
669 if (fallback_style != DumpStyleInvalid)
670 return Dump(s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
675 case DumpStyleDetailedSymbolContext:
676 if (IsSectionOffset()) {
677 ModuleSP module_sp(GetModule());
680 module_sp->ResolveSymbolContextForAddress(
681 *this, eSymbolContextEverything | eSymbolContextVariable, sc);
683 // If we have just a symbol make sure it is in the same section
684 // as our address. If it isn't, then we might have just found
685 // the last symbol that came before the address that we are
686 // looking up that has nothing to do with our address lookup.
687 if (sc.symbol->ValueIsAddress() &&
688 sc.symbol->GetAddressRef().GetSection() != GetSection())
691 sc.GetDescription(s, eDescriptionLevelBrief, target);
694 bool can_create = true;
695 bool get_parent_variables = true;
696 bool stop_if_block_is_inlined_function = false;
697 VariableList variable_list;
698 sc.block->AppendVariables(can_create, get_parent_variables,
699 stop_if_block_is_inlined_function,
700 [](Variable *) { return true; },
703 const size_t num_variables = variable_list.GetSize();
704 for (size_t var_idx = 0; var_idx < num_variables; ++var_idx) {
705 Variable *var = variable_list.GetVariableAtIndex(var_idx).get();
706 if (var && var->LocationIsValidForAddress(*this)) {
708 s->Printf(" Variable: id = {0x%8.8" PRIx64 "}, name = \"%s\"",
709 var->GetID(), var->GetName().GetCString());
710 Type *type = var->GetType();
712 s->Printf(", type = \"%s\"", type->GetName().GetCString());
714 s->PutCString(", type = <unknown>");
715 s->PutCString(", location = ");
716 var->DumpLocationForAddress(s, *this);
717 s->PutCString(", decl = ");
718 var->GetDeclaration().DumpStopContext(s, false);
725 if (fallback_style != DumpStyleInvalid)
726 return Dump(s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
731 case DumpStyleResolvedPointerDescription: {
732 Process *process = exe_ctx.GetProcessPtr();
734 addr_t load_addr = GetLoadAddress(target);
735 if (load_addr != LLDB_INVALID_ADDRESS) {
737 addr_t dereferenced_load_addr =
738 process->ReadPointerFromMemory(load_addr, memory_error);
739 if (dereferenced_load_addr != LLDB_INVALID_ADDRESS) {
740 Address dereferenced_addr;
741 if (dereferenced_addr.SetLoadAddress(dereferenced_load_addr,
744 if (dereferenced_addr.Dump(&strm, exe_scope,
745 DumpStyleResolvedDescription,
746 DumpStyleInvalid, addr_size)) {
747 s->Address(dereferenced_load_addr, addr_size, " -> ", " ");
748 s->Write(strm.GetString().data(), strm.GetSize());
755 if (fallback_style != DumpStyleInvalid)
756 return Dump(s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
764 bool Address::SectionWasDeleted() const {
767 return SectionWasDeletedPrivate();
770 bool Address::SectionWasDeletedPrivate() const {
771 lldb::SectionWP empty_section_wp;
773 // If either call to "std::weak_ptr::owner_before(...) value returns true,
775 // indicates that m_section_wp once contained (possibly still does) a
777 // to a valid shared pointer. This helps us know if we had a valid reference
779 // a section which is now invalid because the module it was in was
781 // or if the address doesn't have a valid reference to a section.
782 return empty_section_wp.owner_before(m_section_wp) ||
783 m_section_wp.owner_before(empty_section_wp);
786 uint32_t Address::CalculateSymbolContext(SymbolContext *sc,
787 uint32_t resolve_scope) const {
789 // Absolute addresses don't have enough information to reconstruct even their
792 SectionSP section_sp(GetSection());
794 ModuleSP module_sp(section_sp->GetModule());
796 sc->module_sp = module_sp;
798 return sc->module_sp->ResolveSymbolContextForAddress(
799 *this, resolve_scope, *sc);
805 ModuleSP Address::CalculateSymbolContextModule() const {
806 SectionSP section_sp(GetSection());
808 return section_sp->GetModule();
812 CompileUnit *Address::CalculateSymbolContextCompileUnit() const {
813 SectionSP section_sp(GetSection());
816 sc.module_sp = section_sp->GetModule();
818 sc.module_sp->ResolveSymbolContextForAddress(*this,
819 eSymbolContextCompUnit, sc);
826 Function *Address::CalculateSymbolContextFunction() const {
827 SectionSP section_sp(GetSection());
830 sc.module_sp = section_sp->GetModule();
832 sc.module_sp->ResolveSymbolContextForAddress(*this,
833 eSymbolContextFunction, sc);
840 Block *Address::CalculateSymbolContextBlock() const {
841 SectionSP section_sp(GetSection());
844 sc.module_sp = section_sp->GetModule();
846 sc.module_sp->ResolveSymbolContextForAddress(*this, eSymbolContextBlock,
854 Symbol *Address::CalculateSymbolContextSymbol() const {
855 SectionSP section_sp(GetSection());
858 sc.module_sp = section_sp->GetModule();
860 sc.module_sp->ResolveSymbolContextForAddress(*this, eSymbolContextSymbol,
868 bool Address::CalculateSymbolContextLineEntry(LineEntry &line_entry) const {
869 SectionSP section_sp(GetSection());
872 sc.module_sp = section_sp->GetModule();
874 sc.module_sp->ResolveSymbolContextForAddress(*this,
875 eSymbolContextLineEntry, sc);
876 if (sc.line_entry.IsValid()) {
877 line_entry = sc.line_entry;
886 int Address::CompareFileAddress(const Address &a, const Address &b) {
887 addr_t a_file_addr = a.GetFileAddress();
888 addr_t b_file_addr = b.GetFileAddress();
889 if (a_file_addr < b_file_addr)
891 if (a_file_addr > b_file_addr)
896 int Address::CompareLoadAddress(const Address &a, const Address &b,
898 assert(target != nullptr);
899 addr_t a_load_addr = a.GetLoadAddress(target);
900 addr_t b_load_addr = b.GetLoadAddress(target);
901 if (a_load_addr < b_load_addr)
903 if (a_load_addr > b_load_addr)
908 int Address::CompareModulePointerAndOffset(const Address &a, const Address &b) {
909 ModuleSP a_module_sp(a.GetModule());
910 ModuleSP b_module_sp(b.GetModule());
911 Module *a_module = a_module_sp.get();
912 Module *b_module = b_module_sp.get();
913 if (a_module < b_module)
915 if (a_module > b_module)
917 // Modules are the same, just compare the file address since they should
919 addr_t a_file_addr = a.GetFileAddress();
920 addr_t b_file_addr = b.GetFileAddress();
921 if (a_file_addr < b_file_addr)
923 if (a_file_addr > b_file_addr)
928 size_t Address::MemorySize() const {
929 // Noting special for the memory size of a single Address object,
930 // it is just the size of itself.
931 return sizeof(Address);
934 //----------------------------------------------------------------------
935 // NOTE: Be careful using this operator. It can correctly compare two
936 // addresses from the same Module correctly. It can't compare two
937 // addresses from different modules in any meaningful way, but it will
938 // compare the module pointers.
941 // - works great for addresses within the same module
942 // - it works for addresses across multiple modules, but don't expect the
943 // address results to make much sense
945 // This basically lets Address objects be used in ordered collection
947 //----------------------------------------------------------------------
949 bool lldb_private::operator<(const Address &lhs, const Address &rhs) {
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 // Addresses are in the same module, just compare the file addresses
956 return lhs.GetFileAddress() < rhs.GetFileAddress();
958 // The addresses are from different modules, just use the module
959 // pointer value to get consistent ordering
960 return lhs_module < rhs_module;
964 bool lldb_private::operator>(const Address &lhs, const Address &rhs) {
965 ModuleSP lhs_module_sp(lhs.GetModule());
966 ModuleSP rhs_module_sp(rhs.GetModule());
967 Module *lhs_module = lhs_module_sp.get();
968 Module *rhs_module = rhs_module_sp.get();
969 if (lhs_module == rhs_module) {
970 // Addresses are in the same module, just compare the file addresses
971 return lhs.GetFileAddress() > rhs.GetFileAddress();
973 // The addresses are from different modules, just use the module
974 // pointer value to get consistent ordering
975 return lhs_module > rhs_module;
979 // The operator == checks for exact equality only (same section, same offset)
980 bool lldb_private::operator==(const Address &a, const Address &rhs) {
981 return a.GetOffset() == rhs.GetOffset() && a.GetSection() == rhs.GetSection();
984 // The operator != checks for exact inequality only (differing section, or
986 bool lldb_private::operator!=(const Address &a, const Address &rhs) {
987 return a.GetOffset() != rhs.GetOffset() || a.GetSection() != rhs.GetSection();
990 AddressClass Address::GetAddressClass() const {
991 ModuleSP module_sp(GetModule());
993 ObjectFile *obj_file = module_sp->GetObjectFile();
995 // Give the symbol vendor a chance to add to the unified section list.
996 module_sp->GetSymbolVendor();
997 return obj_file->GetAddressClass(GetFileAddress());
1000 return eAddressClassUnknown;
1003 bool Address::SetLoadAddress(lldb::addr_t load_addr, Target *target,
1004 bool allow_section_end) {
1005 if (target && target->GetSectionLoadList().ResolveLoadAddress(
1006 load_addr, *this, allow_section_end))
1008 m_section_wp.reset();
1009 m_offset = load_addr;