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