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"
12 #include "lldb/Core/ArchSpec.h" // for ArchSpec
13 #include "lldb/Core/DumpDataExtractor.h"
14 #include "lldb/Core/Module.h"
15 #include "lldb/Core/ModuleList.h" // for ModuleList
16 #include "lldb/Core/Section.h"
17 #include "lldb/Symbol/Block.h"
18 #include "lldb/Symbol/Declaration.h" // for Declaration
19 #include "lldb/Symbol/LineEntry.h" // for LineEntry
20 #include "lldb/Symbol/ObjectFile.h"
21 #include "lldb/Symbol/Symbol.h" // for Symbol
22 #include "lldb/Symbol/SymbolContext.h" // for SymbolContext
23 #include "lldb/Symbol/SymbolVendor.h"
24 #include "lldb/Symbol/Symtab.h" // for Symtab
25 #include "lldb/Symbol/Type.h" // for Type
26 #include "lldb/Symbol/Variable.h"
27 #include "lldb/Symbol/VariableList.h"
28 #include "lldb/Target/ExecutionContext.h"
29 #include "lldb/Target/ExecutionContextScope.h" // for ExecutionContextScope
30 #include "lldb/Target/Process.h"
31 #include "lldb/Target/SectionLoadList.h"
32 #include "lldb/Target/Target.h"
33 #include "lldb/Utility/ConstString.h" // for ConstString
34 #include "lldb/Utility/DataExtractor.h" // for DataExtractor
35 #include "lldb/Utility/Endian.h" // for InlHostByteOrder
36 #include "lldb/Utility/FileSpec.h" // for FileSpec
37 #include "lldb/Utility/Status.h" // for Status
38 #include "lldb/Utility/Stream.h" // for Stream
39 #include "lldb/Utility/StreamString.h" // for StreamString
41 #include "llvm/ADT/StringRef.h" // for StringRef
42 #include "llvm/ADT/Triple.h"
43 #include "llvm/Support/Compiler.h" // for LLVM_FALLTHROUGH
45 #include <cstdint> // for uint8_t, uint32_t
46 #include <memory> // for shared_ptr, operator!=
47 #include <vector> // for vector
49 #include <assert.h> // for assert
50 #include <inttypes.h> // for PRIu64, PRIx64
51 #include <string.h> // for size_t, strlen
53 namespace lldb_private {
56 namespace lldb_private {
61 using namespace lldb_private;
63 static size_t ReadBytes(ExecutionContextScope *exe_scope,
64 const Address &address, void *dst, size_t dst_len) {
65 if (exe_scope == nullptr)
68 TargetSP target_sp(exe_scope->CalculateTarget());
71 bool prefer_file_cache = false;
72 return target_sp->ReadMemory(address, prefer_file_cache, dst, dst_len,
78 static bool GetByteOrderAndAddressSize(ExecutionContextScope *exe_scope,
79 const Address &address,
80 ByteOrder &byte_order,
81 uint32_t &addr_size) {
82 byte_order = eByteOrderInvalid;
84 if (exe_scope == nullptr)
87 TargetSP target_sp(exe_scope->CalculateTarget());
89 byte_order = target_sp->GetArchitecture().GetByteOrder();
90 addr_size = target_sp->GetArchitecture().GetAddressByteSize();
93 if (byte_order == eByteOrderInvalid || addr_size == 0) {
94 ModuleSP module_sp(address.GetModule());
96 byte_order = module_sp->GetArchitecture().GetByteOrder();
97 addr_size = module_sp->GetArchitecture().GetAddressByteSize();
100 return byte_order != eByteOrderInvalid && addr_size != 0;
103 static uint64_t ReadUIntMax64(ExecutionContextScope *exe_scope,
104 const Address &address, uint32_t byte_size,
107 if (exe_scope == nullptr || byte_size > sizeof(uint64_t)) {
113 success = ReadBytes(exe_scope, address, &buf, byte_size) == byte_size;
115 ByteOrder byte_order = eByteOrderInvalid;
116 uint32_t addr_size = 0;
117 if (GetByteOrderAndAddressSize(exe_scope, address, byte_order, addr_size)) {
118 DataExtractor data(&buf, sizeof(buf), byte_order, addr_size);
119 lldb::offset_t offset = 0;
120 uval64 = data.GetU64(&offset);
127 static bool ReadAddress(ExecutionContextScope *exe_scope,
128 const Address &address, uint32_t pointer_size,
129 Address &deref_so_addr) {
130 if (exe_scope == nullptr)
133 bool success = false;
134 addr_t deref_addr = ReadUIntMax64(exe_scope, address, pointer_size, success);
136 ExecutionContext exe_ctx;
137 exe_scope->CalculateExecutionContext(exe_ctx);
138 // If we have any sections that are loaded, try and resolve using the
140 Target *target = exe_ctx.GetTargetPtr();
141 if (target && !target->GetSectionLoadList().IsEmpty()) {
142 if (target->GetSectionLoadList().ResolveLoadAddress(deref_addr,
146 // If we were not running, yet able to read an integer, we must
148 ModuleSP module_sp(address.GetModule());
151 if (module_sp->ResolveFileAddress(deref_addr, deref_so_addr))
155 // We couldn't make "deref_addr" into a section offset value, but we were
156 // able to read the address, so we return a section offset address with
157 // no section and "deref_addr" as the offset (address).
158 deref_so_addr.SetRawAddress(deref_addr);
164 static bool DumpUInt(ExecutionContextScope *exe_scope, const Address &address,
165 uint32_t byte_size, Stream *strm) {
166 if (exe_scope == nullptr || byte_size == 0)
168 std::vector<uint8_t> buf(byte_size, 0);
170 if (ReadBytes(exe_scope, address, &buf[0], buf.size()) == buf.size()) {
171 ByteOrder byte_order = eByteOrderInvalid;
172 uint32_t addr_size = 0;
173 if (GetByteOrderAndAddressSize(exe_scope, address, byte_order, addr_size)) {
174 DataExtractor data(&buf.front(), buf.size(), byte_order, addr_size);
176 DumpDataExtractor(data, strm,
177 0, // Start offset in "data"
178 eFormatHex, // Print as characters
179 buf.size(), // Size of item
181 UINT32_MAX, // num per line
182 LLDB_INVALID_ADDRESS, // base address
183 0, // bitfield bit size
184 0); // bitfield bit offset
192 static size_t ReadCStringFromMemory(ExecutionContextScope *exe_scope,
193 const Address &address, Stream *strm) {
194 if (exe_scope == nullptr)
196 const size_t k_buf_len = 256;
197 char buf[k_buf_len + 1];
198 buf[k_buf_len] = '\0'; // NULL terminate
200 // Byte order and address size don't matter for C string dumping..
201 DataExtractor data(buf, sizeof(buf), endian::InlHostByteOrder(), 4);
202 size_t total_len = 0;
204 Address curr_address(address);
206 while ((bytes_read = ReadBytes(exe_scope, curr_address, buf, k_buf_len)) >
208 size_t len = strlen(buf);
211 if (len > bytes_read)
214 DumpDataExtractor(data, strm,
215 0, // Start offset in "data"
216 eFormatChar, // Print as characters
217 1, // Size of item (1 byte for a char!)
218 len, // How many bytes to print?
219 UINT32_MAX, // num per line
220 LLDB_INVALID_ADDRESS, // base address
221 0, // bitfield bit size
223 0); // bitfield bit offset
225 total_len += bytes_read;
229 curr_address.SetOffset(curr_address.GetOffset() + bytes_read);
235 Address::Address(lldb::addr_t abs_addr) : m_section_wp(), m_offset(abs_addr) {}
237 Address::Address(addr_t address, const SectionList *section_list)
238 : m_section_wp(), m_offset(LLDB_INVALID_ADDRESS) {
239 ResolveAddressUsingFileSections(address, section_list);
242 const Address &Address::operator=(const Address &rhs) {
244 m_section_wp = rhs.m_section_wp;
245 m_offset = rhs.m_offset;
250 bool Address::ResolveAddressUsingFileSections(addr_t file_addr,
251 const SectionList *section_list) {
253 SectionSP section_sp(
254 section_list->FindSectionContainingFileAddress(file_addr));
255 m_section_wp = section_sp;
257 assert(section_sp->ContainsFileAddress(file_addr));
258 m_offset = file_addr - section_sp->GetFileAddress();
259 return true; // Successfully transformed addr into a section offset
263 m_offset = file_addr;
264 return false; // Failed to resolve this address to a section offset value
267 ModuleSP Address::GetModule() const {
268 lldb::ModuleSP module_sp;
269 SectionSP section_sp(GetSection());
271 module_sp = section_sp->GetModule();
275 addr_t Address::GetFileAddress() const {
276 SectionSP section_sp(GetSection());
278 addr_t sect_file_addr = section_sp->GetFileAddress();
279 if (sect_file_addr == LLDB_INVALID_ADDRESS) {
280 // Section isn't resolved, we can't return a valid file address
281 return LLDB_INVALID_ADDRESS;
283 // We have a valid file range, so we can return the file based
284 // address by adding the file base address to our offset
285 return sect_file_addr + m_offset;
286 } else if (SectionWasDeletedPrivate()) {
287 // Used to have a valid section but it got deleted so the
288 // offset doesn't mean anything without the section
289 return LLDB_INVALID_ADDRESS;
291 // No section, we just return the offset since it is the value in this case
295 addr_t Address::GetLoadAddress(Target *target) const {
296 SectionSP section_sp(GetSection());
299 addr_t sect_load_addr = section_sp->GetLoadBaseAddress(target);
301 if (sect_load_addr != LLDB_INVALID_ADDRESS) {
302 // We have a valid file range, so we can return the file based
303 // address by adding the file base address to our offset
304 return sect_load_addr + m_offset;
307 } else if (SectionWasDeletedPrivate()) {
308 // Used to have a valid section but it got deleted so the
309 // offset doesn't mean anything without the section
310 return LLDB_INVALID_ADDRESS;
312 // We don't have a section so the offset is the load address
315 // The section isn't resolved or an invalid target was passed in
316 // so we can't return a valid load address.
317 return LLDB_INVALID_ADDRESS;
320 addr_t Address::GetCallableLoadAddress(Target *target, bool is_indirect) const {
321 addr_t code_addr = LLDB_INVALID_ADDRESS;
323 if (is_indirect && target) {
324 ProcessSP processSP = target->GetProcessSP();
327 code_addr = processSP->ResolveIndirectFunction(this, error);
328 if (!error.Success())
329 code_addr = LLDB_INVALID_ADDRESS;
332 code_addr = GetLoadAddress(target);
335 if (code_addr == LLDB_INVALID_ADDRESS)
339 return target->GetCallableLoadAddress(code_addr, GetAddressClass());
343 bool Address::SetCallableLoadAddress(lldb::addr_t load_addr, Target *target) {
344 if (SetLoadAddress(load_addr, target)) {
346 m_offset = target->GetCallableLoadAddress(m_offset, GetAddressClass());
352 addr_t Address::GetOpcodeLoadAddress(Target *target,
353 AddressClass addr_class) const {
354 addr_t code_addr = GetLoadAddress(target);
355 if (code_addr != LLDB_INVALID_ADDRESS) {
356 if (addr_class == eAddressClassInvalid)
357 addr_class = GetAddressClass();
358 code_addr = target->GetOpcodeLoadAddress(code_addr, addr_class);
363 bool Address::SetOpcodeLoadAddress(lldb::addr_t load_addr, Target *target,
364 AddressClass addr_class,
365 bool allow_section_end) {
366 if (SetLoadAddress(load_addr, target, allow_section_end)) {
368 if (addr_class == eAddressClassInvalid)
369 addr_class = GetAddressClass();
370 m_offset = target->GetOpcodeLoadAddress(m_offset, addr_class);
377 bool Address::Dump(Stream *s, ExecutionContextScope *exe_scope, DumpStyle style,
378 DumpStyle fallback_style, uint32_t addr_size) const {
379 // If the section was nullptr, only load address is going to work unless we
381 // trying to deref a pointer
382 SectionSP section_sp(GetSection());
383 if (!section_sp && style != DumpStyleResolvedPointerDescription)
384 style = DumpStyleLoadAddress;
386 ExecutionContext exe_ctx(exe_scope);
387 Target *target = exe_ctx.GetTargetPtr();
388 // If addr_byte_size is UINT32_MAX, then determine the correct address
389 // byte size for the process or default to the size of addr_t
390 if (addr_size == UINT32_MAX) {
392 addr_size = target->GetArchitecture().GetAddressByteSize();
394 addr_size = sizeof(addr_t);
399 case DumpStyleInvalid:
402 case DumpStyleSectionNameOffset:
404 section_sp->DumpName(s);
405 s->Printf(" + %" PRIu64, m_offset);
407 s->Address(m_offset, addr_size);
411 case DumpStyleSectionPointerOffset:
412 s->Printf("(Section *)%p + ", static_cast<void *>(section_sp.get()));
413 s->Address(m_offset, addr_size);
416 case DumpStyleModuleWithFileAddress:
418 ModuleSP module_sp = section_sp->GetModule();
420 s->Printf("%s[", module_sp->GetFileSpec().GetFilename().AsCString(
423 s->Printf("%s[", "<Unknown>");
426 case DumpStyleFileAddress: {
427 addr_t file_addr = GetFileAddress();
428 if (file_addr == LLDB_INVALID_ADDRESS) {
429 if (fallback_style != DumpStyleInvalid)
430 return Dump(s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
433 s->Address(file_addr, addr_size);
434 if (style == DumpStyleModuleWithFileAddress && section_sp)
438 case DumpStyleLoadAddress: {
439 addr_t load_addr = GetLoadAddress(target);
443 * Display address in compressed form for MIPS16 or microMIPS
444 * if the address belongs to eAddressClassCodeAlternateISA.
447 const llvm::Triple::ArchType llvm_arch =
448 target->GetArchitecture().GetMachine();
449 if (llvm_arch == llvm::Triple::mips ||
450 llvm_arch == llvm::Triple::mipsel ||
451 llvm_arch == llvm::Triple::mips64 ||
452 llvm_arch == llvm::Triple::mips64el)
453 load_addr = GetCallableLoadAddress(target);
456 if (load_addr == LLDB_INVALID_ADDRESS) {
457 if (fallback_style != DumpStyleInvalid)
458 return Dump(s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
461 s->Address(load_addr, addr_size);
464 case DumpStyleResolvedDescription:
465 case DumpStyleResolvedDescriptionNoModule:
466 case DumpStyleResolvedDescriptionNoFunctionArguments:
467 case DumpStyleNoFunctionName:
468 if (IsSectionOffset()) {
469 uint32_t pointer_size = 4;
470 ModuleSP module_sp(GetModule());
472 pointer_size = target->GetArchitecture().GetAddressByteSize();
474 pointer_size = module_sp->GetArchitecture().GetAddressByteSize();
476 bool showed_info = false;
478 SectionType sect_type = section_sp->GetType();
480 case eSectionTypeData:
482 SymbolVendor *sym_vendor = module_sp->GetSymbolVendor();
484 Symtab *symtab = sym_vendor->GetSymtab();
486 const addr_t file_Addr = GetFileAddress();
488 symtab->FindSymbolContainingFileAddress(file_Addr);
490 const char *symbol_name = symbol->GetName().AsCString();
492 s->PutCString(symbol_name);
494 file_Addr - symbol->GetAddressRef().GetFileAddress();
496 s->Printf(" + %" PRIu64, delta);
505 case eSectionTypeDataCString:
506 // Read the C string from memory and display it
508 ReadCStringFromMemory(exe_scope, *this, s);
511 case eSectionTypeDataCStringPointers:
512 if (ReadAddress(exe_scope, *this, pointer_size, so_addr)) {
514 s->PutCString("(char *)");
515 so_addr.Dump(s, exe_scope, DumpStyleLoadAddress,
516 DumpStyleFileAddress);
520 ReadCStringFromMemory(exe_scope, so_addr, s);
524 case eSectionTypeDataObjCMessageRefs:
525 if (ReadAddress(exe_scope, *this, pointer_size, so_addr)) {
526 if (target && so_addr.IsSectionOffset()) {
527 SymbolContext func_sc;
528 target->GetImages().ResolveSymbolContextForAddress(
529 so_addr, eSymbolContextEverything, func_sc);
530 if (func_sc.function != nullptr || func_sc.symbol != nullptr) {
533 s->PutCString("(objc_msgref *) -> { (func*)");
534 so_addr.Dump(s, exe_scope, DumpStyleLoadAddress,
535 DumpStyleFileAddress);
539 Address cstr_addr(*this);
540 cstr_addr.SetOffset(cstr_addr.GetOffset() + pointer_size);
541 func_sc.DumpStopContext(s, exe_scope, so_addr, true, true,
543 if (ReadAddress(exe_scope, cstr_addr, pointer_size, so_addr)) {
545 s->PutCString("), (char *)");
546 so_addr.Dump(s, exe_scope, DumpStyleLoadAddress,
547 DumpStyleFileAddress);
552 ReadCStringFromMemory(exe_scope, so_addr, s);
555 s->PutCString(") }");
564 case eSectionTypeDataObjCCFStrings: {
565 Address cfstring_data_addr(*this);
566 cfstring_data_addr.SetOffset(cfstring_data_addr.GetOffset() +
568 if (ReadAddress(exe_scope, cfstring_data_addr, pointer_size,
571 s->PutCString("(CFString *) ");
572 cfstring_data_addr.Dump(s, exe_scope, DumpStyleLoadAddress,
573 DumpStyleFileAddress);
574 s->PutCString(" -> @");
578 if (so_addr.Dump(s, exe_scope, DumpStyleResolvedDescription))
583 case eSectionTypeData4:
584 // Read the 4 byte data and display it
586 s->PutCString("(uint32_t) ");
587 DumpUInt(exe_scope, *this, 4, s);
590 case eSectionTypeData8:
591 // Read the 8 byte data and display it
593 s->PutCString("(uint64_t) ");
594 DumpUInt(exe_scope, *this, 8, s);
597 case eSectionTypeData16:
598 // Read the 16 byte data and display it
600 s->PutCString("(uint128_t) ");
601 DumpUInt(exe_scope, *this, 16, s);
604 case eSectionTypeDataPointers:
605 // Read the pointer data and display it
606 if (ReadAddress(exe_scope, *this, pointer_size, so_addr)) {
607 s->PutCString("(void *)");
608 so_addr.Dump(s, exe_scope, DumpStyleLoadAddress,
609 DumpStyleFileAddress);
612 if (so_addr.IsSectionOffset()) {
613 SymbolContext pointer_sc;
615 target->GetImages().ResolveSymbolContextForAddress(
616 so_addr, eSymbolContextEverything, pointer_sc);
617 if (pointer_sc.function != nullptr ||
618 pointer_sc.symbol != nullptr) {
620 pointer_sc.DumpStopContext(s, exe_scope, so_addr, true, false,
636 module_sp->ResolveSymbolContextForAddress(
637 *this, eSymbolContextEverything, sc);
638 if (sc.function || sc.symbol) {
639 bool show_stop_context = true;
640 const bool show_module = (style == DumpStyleResolvedDescription);
641 const bool show_fullpaths = false;
642 const bool show_inlined_frames = true;
643 const bool show_function_arguments =
644 (style != DumpStyleResolvedDescriptionNoFunctionArguments);
645 const bool show_function_name = (style != DumpStyleNoFunctionName);
646 if (sc.function == nullptr && sc.symbol != nullptr) {
647 // If we have just a symbol make sure it is in the right section
648 if (sc.symbol->ValueIsAddress()) {
649 if (sc.symbol->GetAddressRef().GetSection() != GetSection()) {
650 // don't show the module if the symbol is a trampoline symbol
651 show_stop_context = false;
655 if (show_stop_context) {
656 // We have a function or a symbol from the same
657 // sections as this address.
658 sc.DumpStopContext(s, exe_scope, *this, show_fullpaths,
659 show_module, show_inlined_frames,
660 show_function_arguments, show_function_name);
662 // We found a symbol but it was in a different
663 // section so it isn't the symbol we should be
664 // showing, just show the section name + offset
665 Dump(s, exe_scope, DumpStyleSectionNameOffset);
671 if (fallback_style != DumpStyleInvalid)
672 return Dump(s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
677 case DumpStyleDetailedSymbolContext:
678 if (IsSectionOffset()) {
679 ModuleSP module_sp(GetModule());
682 module_sp->ResolveSymbolContextForAddress(
683 *this, eSymbolContextEverything | eSymbolContextVariable, sc);
685 // If we have just a symbol make sure it is in the same section
686 // as our address. If it isn't, then we might have just found
687 // the last symbol that came before the address that we are
688 // looking up that has nothing to do with our address lookup.
689 if (sc.symbol->ValueIsAddress() &&
690 sc.symbol->GetAddressRef().GetSection() != GetSection())
693 sc.GetDescription(s, eDescriptionLevelBrief, target);
696 bool can_create = true;
697 bool get_parent_variables = true;
698 bool stop_if_block_is_inlined_function = false;
699 VariableList variable_list;
700 sc.block->AppendVariables(can_create, get_parent_variables,
701 stop_if_block_is_inlined_function,
702 [](Variable *) { return true; },
705 const size_t num_variables = variable_list.GetSize();
706 for (size_t var_idx = 0; var_idx < num_variables; ++var_idx) {
707 Variable *var = variable_list.GetVariableAtIndex(var_idx).get();
708 if (var && var->LocationIsValidForAddress(*this)) {
710 s->Printf(" Variable: id = {0x%8.8" PRIx64 "}, name = \"%s\"",
711 var->GetID(), var->GetName().GetCString());
712 Type *type = var->GetType();
714 s->Printf(", type = \"%s\"", type->GetName().GetCString());
716 s->PutCString(", type = <unknown>");
717 s->PutCString(", location = ");
718 var->DumpLocationForAddress(s, *this);
719 s->PutCString(", decl = ");
720 var->GetDeclaration().DumpStopContext(s, false);
727 if (fallback_style != DumpStyleInvalid)
728 return Dump(s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
733 case DumpStyleResolvedPointerDescription: {
734 Process *process = exe_ctx.GetProcessPtr();
736 addr_t load_addr = GetLoadAddress(target);
737 if (load_addr != LLDB_INVALID_ADDRESS) {
739 addr_t dereferenced_load_addr =
740 process->ReadPointerFromMemory(load_addr, memory_error);
741 if (dereferenced_load_addr != LLDB_INVALID_ADDRESS) {
742 Address dereferenced_addr;
743 if (dereferenced_addr.SetLoadAddress(dereferenced_load_addr,
746 if (dereferenced_addr.Dump(&strm, exe_scope,
747 DumpStyleResolvedDescription,
748 DumpStyleInvalid, addr_size)) {
749 s->Address(dereferenced_load_addr, addr_size, " -> ", " ");
750 s->Write(strm.GetString().data(), strm.GetSize());
757 if (fallback_style != DumpStyleInvalid)
758 return Dump(s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
766 bool Address::SectionWasDeleted() const {
769 return SectionWasDeletedPrivate();
772 bool Address::SectionWasDeletedPrivate() const {
773 lldb::SectionWP empty_section_wp;
775 // If either call to "std::weak_ptr::owner_before(...) value returns true,
777 // indicates that m_section_wp once contained (possibly still does) a
779 // to a valid shared pointer. This helps us know if we had a valid reference
781 // a section which is now invalid because the module it was in was
783 // or if the address doesn't have a valid reference to a section.
784 return empty_section_wp.owner_before(m_section_wp) ||
785 m_section_wp.owner_before(empty_section_wp);
788 uint32_t Address::CalculateSymbolContext(SymbolContext *sc,
789 uint32_t resolve_scope) const {
791 // Absolute addresses don't have enough information to reconstruct even their
794 SectionSP section_sp(GetSection());
796 ModuleSP module_sp(section_sp->GetModule());
798 sc->module_sp = module_sp;
800 return sc->module_sp->ResolveSymbolContextForAddress(
801 *this, resolve_scope, *sc);
807 ModuleSP Address::CalculateSymbolContextModule() const {
808 SectionSP section_sp(GetSection());
810 return section_sp->GetModule();
814 CompileUnit *Address::CalculateSymbolContextCompileUnit() const {
815 SectionSP section_sp(GetSection());
818 sc.module_sp = section_sp->GetModule();
820 sc.module_sp->ResolveSymbolContextForAddress(*this,
821 eSymbolContextCompUnit, sc);
828 Function *Address::CalculateSymbolContextFunction() const {
829 SectionSP section_sp(GetSection());
832 sc.module_sp = section_sp->GetModule();
834 sc.module_sp->ResolveSymbolContextForAddress(*this,
835 eSymbolContextFunction, sc);
842 Block *Address::CalculateSymbolContextBlock() const {
843 SectionSP section_sp(GetSection());
846 sc.module_sp = section_sp->GetModule();
848 sc.module_sp->ResolveSymbolContextForAddress(*this, eSymbolContextBlock,
856 Symbol *Address::CalculateSymbolContextSymbol() const {
857 SectionSP section_sp(GetSection());
860 sc.module_sp = section_sp->GetModule();
862 sc.module_sp->ResolveSymbolContextForAddress(*this, eSymbolContextSymbol,
870 bool Address::CalculateSymbolContextLineEntry(LineEntry &line_entry) const {
871 SectionSP section_sp(GetSection());
874 sc.module_sp = section_sp->GetModule();
876 sc.module_sp->ResolveSymbolContextForAddress(*this,
877 eSymbolContextLineEntry, sc);
878 if (sc.line_entry.IsValid()) {
879 line_entry = sc.line_entry;
888 int Address::CompareFileAddress(const Address &a, const Address &b) {
889 addr_t a_file_addr = a.GetFileAddress();
890 addr_t b_file_addr = b.GetFileAddress();
891 if (a_file_addr < b_file_addr)
893 if (a_file_addr > b_file_addr)
898 int Address::CompareLoadAddress(const Address &a, const Address &b,
900 assert(target != nullptr);
901 addr_t a_load_addr = a.GetLoadAddress(target);
902 addr_t b_load_addr = b.GetLoadAddress(target);
903 if (a_load_addr < b_load_addr)
905 if (a_load_addr > b_load_addr)
910 int Address::CompareModulePointerAndOffset(const Address &a, const Address &b) {
911 ModuleSP a_module_sp(a.GetModule());
912 ModuleSP b_module_sp(b.GetModule());
913 Module *a_module = a_module_sp.get();
914 Module *b_module = b_module_sp.get();
915 if (a_module < b_module)
917 if (a_module > b_module)
919 // Modules are the same, just compare the file address since they should
921 addr_t a_file_addr = a.GetFileAddress();
922 addr_t b_file_addr = b.GetFileAddress();
923 if (a_file_addr < b_file_addr)
925 if (a_file_addr > b_file_addr)
930 size_t Address::MemorySize() const {
931 // Noting special for the memory size of a single Address object,
932 // it is just the size of itself.
933 return sizeof(Address);
936 //----------------------------------------------------------------------
937 // NOTE: Be careful using this operator. It can correctly compare two
938 // addresses from the same Module correctly. It can't compare two
939 // addresses from different modules in any meaningful way, but it will
940 // compare the module pointers.
943 // - works great for addresses within the same module
944 // - it works for addresses across multiple modules, but don't expect the
945 // address results to make much sense
947 // This basically lets Address objects be used in ordered collection
949 //----------------------------------------------------------------------
951 bool lldb_private::operator<(const Address &lhs, const Address &rhs) {
952 ModuleSP lhs_module_sp(lhs.GetModule());
953 ModuleSP rhs_module_sp(rhs.GetModule());
954 Module *lhs_module = lhs_module_sp.get();
955 Module *rhs_module = rhs_module_sp.get();
956 if (lhs_module == rhs_module) {
957 // Addresses are in the same module, just compare the file addresses
958 return lhs.GetFileAddress() < rhs.GetFileAddress();
960 // The addresses are from different modules, just use the module
961 // pointer value to get consistent ordering
962 return lhs_module < rhs_module;
966 bool lldb_private::operator>(const Address &lhs, const Address &rhs) {
967 ModuleSP lhs_module_sp(lhs.GetModule());
968 ModuleSP rhs_module_sp(rhs.GetModule());
969 Module *lhs_module = lhs_module_sp.get();
970 Module *rhs_module = rhs_module_sp.get();
971 if (lhs_module == rhs_module) {
972 // Addresses are in the same module, just compare the file addresses
973 return lhs.GetFileAddress() > rhs.GetFileAddress();
975 // The addresses are from different modules, just use the module
976 // pointer value to get consistent ordering
977 return lhs_module > rhs_module;
981 // The operator == checks for exact equality only (same section, same offset)
982 bool lldb_private::operator==(const Address &a, const Address &rhs) {
983 return a.GetOffset() == rhs.GetOffset() && a.GetSection() == rhs.GetSection();
986 // The operator != checks for exact inequality only (differing section, or
988 bool lldb_private::operator!=(const Address &a, const Address &rhs) {
989 return a.GetOffset() != rhs.GetOffset() || a.GetSection() != rhs.GetSection();
992 AddressClass Address::GetAddressClass() const {
993 ModuleSP module_sp(GetModule());
995 ObjectFile *obj_file = module_sp->GetObjectFile();
997 // Give the symbol vendor a chance to add to the unified section list.
998 module_sp->GetSymbolVendor();
999 return obj_file->GetAddressClass(GetFileAddress());
1002 return eAddressClassUnknown;
1005 bool Address::SetLoadAddress(lldb::addr_t load_addr, Target *target,
1006 bool allow_section_end) {
1007 if (target && target->GetSectionLoadList().ResolveLoadAddress(
1008 load_addr, *this, allow_section_end))
1010 m_section_wp.reset();
1011 m_offset = load_addr;