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 have a
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 no
155 // 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 address by
282 // 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 offset doesn't
286 // 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 address
301 // 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 offset doesn't
307 // 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 so we can't
314 // 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 == AddressClass::eInvalid)
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 == AddressClass::eInvalid)
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
378 // are trying to deref a pointer
379 SectionSP section_sp(GetSection());
380 if (!section_sp && style != DumpStyleResolvedPointerDescription)
381 style = DumpStyleLoadAddress;
383 ExecutionContext exe_ctx(exe_scope);
384 Target *target = exe_ctx.GetTargetPtr();
385 // If addr_byte_size is UINT32_MAX, then determine the correct address byte
386 // size for the process or default to the size of addr_t
387 if (addr_size == UINT32_MAX) {
389 addr_size = target->GetArchitecture().GetAddressByteSize();
391 addr_size = sizeof(addr_t);
396 case DumpStyleInvalid:
399 case DumpStyleSectionNameOffset:
401 section_sp->DumpName(s);
402 s->Printf(" + %" PRIu64, m_offset);
404 s->Address(m_offset, addr_size);
408 case DumpStyleSectionPointerOffset:
409 s->Printf("(Section *)%p + ", static_cast<void *>(section_sp.get()));
410 s->Address(m_offset, addr_size);
413 case DumpStyleModuleWithFileAddress:
415 ModuleSP module_sp = section_sp->GetModule();
417 s->Printf("%s[", module_sp->GetFileSpec().GetFilename().AsCString(
420 s->Printf("%s[", "<Unknown>");
423 case DumpStyleFileAddress: {
424 addr_t file_addr = GetFileAddress();
425 if (file_addr == LLDB_INVALID_ADDRESS) {
426 if (fallback_style != DumpStyleInvalid)
427 return Dump(s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
430 s->Address(file_addr, addr_size);
431 if (style == DumpStyleModuleWithFileAddress && section_sp)
435 case DumpStyleLoadAddress: {
436 addr_t load_addr = GetLoadAddress(target);
440 * Display address in compressed form for MIPS16 or microMIPS
441 * if the address belongs to AddressClass::eCodeAlternateISA.
444 const llvm::Triple::ArchType llvm_arch =
445 target->GetArchitecture().GetMachine();
446 if (llvm_arch == llvm::Triple::mips ||
447 llvm_arch == llvm::Triple::mipsel ||
448 llvm_arch == llvm::Triple::mips64 ||
449 llvm_arch == llvm::Triple::mips64el)
450 load_addr = GetCallableLoadAddress(target);
453 if (load_addr == LLDB_INVALID_ADDRESS) {
454 if (fallback_style != DumpStyleInvalid)
455 return Dump(s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
458 s->Address(load_addr, addr_size);
461 case DumpStyleResolvedDescription:
462 case DumpStyleResolvedDescriptionNoModule:
463 case DumpStyleResolvedDescriptionNoFunctionArguments:
464 case DumpStyleNoFunctionName:
465 if (IsSectionOffset()) {
466 uint32_t pointer_size = 4;
467 ModuleSP module_sp(GetModule());
469 pointer_size = target->GetArchitecture().GetAddressByteSize();
471 pointer_size = module_sp->GetArchitecture().GetAddressByteSize();
473 bool showed_info = false;
475 SectionType sect_type = section_sp->GetType();
477 case eSectionTypeData:
479 SymbolVendor *sym_vendor = module_sp->GetSymbolVendor();
481 Symtab *symtab = sym_vendor->GetSymtab();
483 const addr_t file_Addr = GetFileAddress();
485 symtab->FindSymbolContainingFileAddress(file_Addr);
487 const char *symbol_name = symbol->GetName().AsCString();
489 s->PutCString(symbol_name);
491 file_Addr - symbol->GetAddressRef().GetFileAddress();
493 s->Printf(" + %" PRIu64, delta);
502 case eSectionTypeDataCString:
503 // Read the C string from memory and display it
505 ReadCStringFromMemory(exe_scope, *this, s);
508 case eSectionTypeDataCStringPointers:
509 if (ReadAddress(exe_scope, *this, pointer_size, so_addr)) {
511 s->PutCString("(char *)");
512 so_addr.Dump(s, exe_scope, DumpStyleLoadAddress,
513 DumpStyleFileAddress);
517 ReadCStringFromMemory(exe_scope, so_addr, s);
521 case eSectionTypeDataObjCMessageRefs:
522 if (ReadAddress(exe_scope, *this, pointer_size, so_addr)) {
523 if (target && so_addr.IsSectionOffset()) {
524 SymbolContext func_sc;
525 target->GetImages().ResolveSymbolContextForAddress(
526 so_addr, eSymbolContextEverything, func_sc);
527 if (func_sc.function != nullptr || func_sc.symbol != nullptr) {
530 s->PutCString("(objc_msgref *) -> { (func*)");
531 so_addr.Dump(s, exe_scope, DumpStyleLoadAddress,
532 DumpStyleFileAddress);
536 Address cstr_addr(*this);
537 cstr_addr.SetOffset(cstr_addr.GetOffset() + pointer_size);
538 func_sc.DumpStopContext(s, exe_scope, so_addr, true, true,
540 if (ReadAddress(exe_scope, cstr_addr, pointer_size, so_addr)) {
542 s->PutCString("), (char *)");
543 so_addr.Dump(s, exe_scope, DumpStyleLoadAddress,
544 DumpStyleFileAddress);
549 ReadCStringFromMemory(exe_scope, so_addr, s);
552 s->PutCString(") }");
561 case eSectionTypeDataObjCCFStrings: {
562 Address cfstring_data_addr(*this);
563 cfstring_data_addr.SetOffset(cfstring_data_addr.GetOffset() +
565 if (ReadAddress(exe_scope, cfstring_data_addr, pointer_size,
568 s->PutCString("(CFString *) ");
569 cfstring_data_addr.Dump(s, exe_scope, DumpStyleLoadAddress,
570 DumpStyleFileAddress);
571 s->PutCString(" -> @");
575 if (so_addr.Dump(s, exe_scope, DumpStyleResolvedDescription))
580 case eSectionTypeData4:
581 // Read the 4 byte data and display it
583 s->PutCString("(uint32_t) ");
584 DumpUInt(exe_scope, *this, 4, s);
587 case eSectionTypeData8:
588 // Read the 8 byte data and display it
590 s->PutCString("(uint64_t) ");
591 DumpUInt(exe_scope, *this, 8, s);
594 case eSectionTypeData16:
595 // Read the 16 byte data and display it
597 s->PutCString("(uint128_t) ");
598 DumpUInt(exe_scope, *this, 16, s);
601 case eSectionTypeDataPointers:
602 // Read the pointer data and display it
603 if (ReadAddress(exe_scope, *this, pointer_size, so_addr)) {
604 s->PutCString("(void *)");
605 so_addr.Dump(s, exe_scope, DumpStyleLoadAddress,
606 DumpStyleFileAddress);
609 if (so_addr.IsSectionOffset()) {
610 SymbolContext pointer_sc;
612 target->GetImages().ResolveSymbolContextForAddress(
613 so_addr, eSymbolContextEverything, pointer_sc);
614 if (pointer_sc.function != nullptr ||
615 pointer_sc.symbol != nullptr) {
617 pointer_sc.DumpStopContext(s, exe_scope, so_addr, true, false,
633 module_sp->ResolveSymbolContextForAddress(
634 *this, eSymbolContextEverything, sc);
635 if (sc.function || sc.symbol) {
636 bool show_stop_context = true;
637 const bool show_module = (style == DumpStyleResolvedDescription);
638 const bool show_fullpaths = false;
639 const bool show_inlined_frames = true;
640 const bool show_function_arguments =
641 (style != DumpStyleResolvedDescriptionNoFunctionArguments);
642 const bool show_function_name = (style != DumpStyleNoFunctionName);
643 if (sc.function == nullptr && sc.symbol != nullptr) {
644 // If we have just a symbol make sure it is in the right section
645 if (sc.symbol->ValueIsAddress()) {
646 if (sc.symbol->GetAddressRef().GetSection() != GetSection()) {
647 // don't show the module if the symbol is a trampoline symbol
648 show_stop_context = false;
652 if (show_stop_context) {
653 // We have a function or a symbol from the same sections as this
655 sc.DumpStopContext(s, exe_scope, *this, show_fullpaths,
656 show_module, show_inlined_frames,
657 show_function_arguments, show_function_name);
659 // We found a symbol but it was in a different section so it
660 // isn't the symbol we should be showing, just show the section
662 Dump(s, exe_scope, DumpStyleSectionNameOffset);
668 if (fallback_style != DumpStyleInvalid)
669 return Dump(s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
674 case DumpStyleDetailedSymbolContext:
675 if (IsSectionOffset()) {
676 ModuleSP module_sp(GetModule());
679 module_sp->ResolveSymbolContextForAddress(
680 *this, eSymbolContextEverything | eSymbolContextVariable, sc);
682 // If we have just a symbol make sure it is in the same section as
683 // our address. If it isn't, then we might have just found the last
684 // symbol that came before the address that we are looking up that
685 // has nothing to do with our address lookup.
686 if (sc.symbol->ValueIsAddress() &&
687 sc.symbol->GetAddressRef().GetSection() != GetSection())
690 sc.GetDescription(s, eDescriptionLevelBrief, target);
693 bool can_create = true;
694 bool get_parent_variables = true;
695 bool stop_if_block_is_inlined_function = false;
696 VariableList variable_list;
697 sc.block->AppendVariables(can_create, get_parent_variables,
698 stop_if_block_is_inlined_function,
699 [](Variable *) { return true; },
702 const size_t num_variables = variable_list.GetSize();
703 for (size_t var_idx = 0; var_idx < num_variables; ++var_idx) {
704 Variable *var = variable_list.GetVariableAtIndex(var_idx).get();
705 if (var && var->LocationIsValidForAddress(*this)) {
707 s->Printf(" Variable: id = {0x%8.8" PRIx64 "}, name = \"%s\"",
708 var->GetID(), var->GetName().GetCString());
709 Type *type = var->GetType();
711 s->Printf(", type = \"%s\"", type->GetName().GetCString());
713 s->PutCString(", type = <unknown>");
714 s->PutCString(", location = ");
715 var->DumpLocationForAddress(s, *this);
716 s->PutCString(", decl = ");
717 var->GetDeclaration().DumpStopContext(s, false);
724 if (fallback_style != DumpStyleInvalid)
725 return Dump(s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
730 case DumpStyleResolvedPointerDescription: {
731 Process *process = exe_ctx.GetProcessPtr();
733 addr_t load_addr = GetLoadAddress(target);
734 if (load_addr != LLDB_INVALID_ADDRESS) {
736 addr_t dereferenced_load_addr =
737 process->ReadPointerFromMemory(load_addr, memory_error);
738 if (dereferenced_load_addr != LLDB_INVALID_ADDRESS) {
739 Address dereferenced_addr;
740 if (dereferenced_addr.SetLoadAddress(dereferenced_load_addr,
743 if (dereferenced_addr.Dump(&strm, exe_scope,
744 DumpStyleResolvedDescription,
745 DumpStyleInvalid, addr_size)) {
746 s->Address(dereferenced_load_addr, addr_size, " -> ", " ");
747 s->Write(strm.GetString().data(), strm.GetSize());
754 if (fallback_style != DumpStyleInvalid)
755 return Dump(s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
763 bool Address::SectionWasDeleted() const {
766 return SectionWasDeletedPrivate();
769 bool Address::SectionWasDeletedPrivate() const {
770 lldb::SectionWP empty_section_wp;
772 // If either call to "std::weak_ptr::owner_before(...) value returns true,
773 // this indicates that m_section_wp once contained (possibly still does) a
774 // reference to a valid shared pointer. This helps us know if we had a valid
775 // reference to a section which is now invalid because the module it was in
776 // was unloaded/deleted, or if the address doesn't have a valid reference to
778 return empty_section_wp.owner_before(m_section_wp) ||
779 m_section_wp.owner_before(empty_section_wp);
782 uint32_t Address::CalculateSymbolContext(SymbolContext *sc,
783 uint32_t resolve_scope) const {
785 // Absolute addresses don't have enough information to reconstruct even their
788 SectionSP section_sp(GetSection());
790 ModuleSP module_sp(section_sp->GetModule());
792 sc->module_sp = module_sp;
794 return sc->module_sp->ResolveSymbolContextForAddress(
795 *this, resolve_scope, *sc);
801 ModuleSP Address::CalculateSymbolContextModule() const {
802 SectionSP section_sp(GetSection());
804 return section_sp->GetModule();
808 CompileUnit *Address::CalculateSymbolContextCompileUnit() const {
809 SectionSP section_sp(GetSection());
812 sc.module_sp = section_sp->GetModule();
814 sc.module_sp->ResolveSymbolContextForAddress(*this,
815 eSymbolContextCompUnit, sc);
822 Function *Address::CalculateSymbolContextFunction() const {
823 SectionSP section_sp(GetSection());
826 sc.module_sp = section_sp->GetModule();
828 sc.module_sp->ResolveSymbolContextForAddress(*this,
829 eSymbolContextFunction, sc);
836 Block *Address::CalculateSymbolContextBlock() const {
837 SectionSP section_sp(GetSection());
840 sc.module_sp = section_sp->GetModule();
842 sc.module_sp->ResolveSymbolContextForAddress(*this, eSymbolContextBlock,
850 Symbol *Address::CalculateSymbolContextSymbol() const {
851 SectionSP section_sp(GetSection());
854 sc.module_sp = section_sp->GetModule();
856 sc.module_sp->ResolveSymbolContextForAddress(*this, eSymbolContextSymbol,
864 bool Address::CalculateSymbolContextLineEntry(LineEntry &line_entry) const {
865 SectionSP section_sp(GetSection());
868 sc.module_sp = section_sp->GetModule();
870 sc.module_sp->ResolveSymbolContextForAddress(*this,
871 eSymbolContextLineEntry, sc);
872 if (sc.line_entry.IsValid()) {
873 line_entry = sc.line_entry;
882 int Address::CompareFileAddress(const Address &a, const Address &b) {
883 addr_t a_file_addr = a.GetFileAddress();
884 addr_t b_file_addr = b.GetFileAddress();
885 if (a_file_addr < b_file_addr)
887 if (a_file_addr > b_file_addr)
892 int Address::CompareLoadAddress(const Address &a, const Address &b,
894 assert(target != nullptr);
895 addr_t a_load_addr = a.GetLoadAddress(target);
896 addr_t b_load_addr = b.GetLoadAddress(target);
897 if (a_load_addr < b_load_addr)
899 if (a_load_addr > b_load_addr)
904 int Address::CompareModulePointerAndOffset(const Address &a, const Address &b) {
905 ModuleSP a_module_sp(a.GetModule());
906 ModuleSP b_module_sp(b.GetModule());
907 Module *a_module = a_module_sp.get();
908 Module *b_module = b_module_sp.get();
909 if (a_module < b_module)
911 if (a_module > b_module)
913 // Modules are the same, just compare the file address since they should be
915 addr_t a_file_addr = a.GetFileAddress();
916 addr_t b_file_addr = b.GetFileAddress();
917 if (a_file_addr < b_file_addr)
919 if (a_file_addr > b_file_addr)
924 size_t Address::MemorySize() const {
925 // Noting special for the memory size of a single Address object, it is just
926 // the size of itself.
927 return sizeof(Address);
930 //----------------------------------------------------------------------
931 // NOTE: Be careful using this operator. It can correctly compare two
932 // addresses from the same Module correctly. It can't compare two addresses
933 // from different modules in any meaningful way, but it will compare the module
937 // - works great for addresses within the same module - it works for addresses
938 // across multiple modules, but don't expect the
939 // address results to make much sense
941 // This basically lets Address objects be used in ordered collection classes.
942 //----------------------------------------------------------------------
944 bool lldb_private::operator<(const Address &lhs, const Address &rhs) {
945 ModuleSP lhs_module_sp(lhs.GetModule());
946 ModuleSP rhs_module_sp(rhs.GetModule());
947 Module *lhs_module = lhs_module_sp.get();
948 Module *rhs_module = rhs_module_sp.get();
949 if (lhs_module == rhs_module) {
950 // Addresses are in the same module, just compare the file addresses
951 return lhs.GetFileAddress() < rhs.GetFileAddress();
953 // The addresses are from different modules, just use the module pointer
954 // value to get consistent ordering
955 return lhs_module < rhs_module;
959 bool lldb_private::operator>(const Address &lhs, const Address &rhs) {
960 ModuleSP lhs_module_sp(lhs.GetModule());
961 ModuleSP rhs_module_sp(rhs.GetModule());
962 Module *lhs_module = lhs_module_sp.get();
963 Module *rhs_module = rhs_module_sp.get();
964 if (lhs_module == rhs_module) {
965 // Addresses are in the same module, just compare the file addresses
966 return lhs.GetFileAddress() > rhs.GetFileAddress();
968 // The addresses are from different modules, just use the module pointer
969 // value to get consistent ordering
970 return lhs_module > rhs_module;
974 // The operator == checks for exact equality only (same section, same offset)
975 bool lldb_private::operator==(const Address &a, const Address &rhs) {
976 return a.GetOffset() == rhs.GetOffset() && a.GetSection() == rhs.GetSection();
979 // The operator != checks for exact inequality only (differing section, or
981 bool lldb_private::operator!=(const Address &a, const Address &rhs) {
982 return a.GetOffset() != rhs.GetOffset() || a.GetSection() != rhs.GetSection();
985 AddressClass Address::GetAddressClass() const {
986 ModuleSP module_sp(GetModule());
988 ObjectFile *obj_file = module_sp->GetObjectFile();
990 // Give the symbol vendor a chance to add to the unified section list.
991 module_sp->GetSymbolVendor();
992 return obj_file->GetAddressClass(GetFileAddress());
995 return AddressClass::eUnknown;
998 bool Address::SetLoadAddress(lldb::addr_t load_addr, Target *target,
999 bool allow_section_end) {
1000 if (target && target->GetSectionLoadList().ResolveLoadAddress(
1001 load_addr, *this, allow_section_end))
1003 m_section_wp.reset();
1004 m_offset = load_addr;