]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Core/Address.cpp
Upgrade our copies of clang, llvm, lldb and compiler-rt to r312293 from
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Core / Address.cpp
1 //===-- Address.cpp ---------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "lldb/Core/Address.h"
11
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
40
41 #include "llvm/ADT/StringRef.h" // for StringRef
42 #include "llvm/ADT/Triple.h"
43 #include "llvm/Support/Compiler.h" // for LLVM_FALLTHROUGH
44
45 #include <cstdint> // for uint8_t, uint32_t
46 #include <memory>  // for shared_ptr, operator!=
47 #include <vector>  // for vector
48
49 #include <assert.h>   // for assert
50 #include <inttypes.h> // for PRIu64, PRIx64
51 #include <string.h>   // for size_t, strlen
52
53 namespace lldb_private {
54 class CompileUnit;
55 }
56 namespace lldb_private {
57 class Function;
58 }
59
60 using namespace lldb;
61 using namespace lldb_private;
62
63 static size_t ReadBytes(ExecutionContextScope *exe_scope,
64                         const Address &address, void *dst, size_t dst_len) {
65   if (exe_scope == nullptr)
66     return 0;
67
68   TargetSP target_sp(exe_scope->CalculateTarget());
69   if (target_sp) {
70     Status error;
71     bool prefer_file_cache = false;
72     return target_sp->ReadMemory(address, prefer_file_cache, dst, dst_len,
73                                  error);
74   }
75   return 0;
76 }
77
78 static bool GetByteOrderAndAddressSize(ExecutionContextScope *exe_scope,
79                                        const Address &address,
80                                        ByteOrder &byte_order,
81                                        uint32_t &addr_size) {
82   byte_order = eByteOrderInvalid;
83   addr_size = 0;
84   if (exe_scope == nullptr)
85     return false;
86
87   TargetSP target_sp(exe_scope->CalculateTarget());
88   if (target_sp) {
89     byte_order = target_sp->GetArchitecture().GetByteOrder();
90     addr_size = target_sp->GetArchitecture().GetAddressByteSize();
91   }
92
93   if (byte_order == eByteOrderInvalid || addr_size == 0) {
94     ModuleSP module_sp(address.GetModule());
95     if (module_sp) {
96       byte_order = module_sp->GetArchitecture().GetByteOrder();
97       addr_size = module_sp->GetArchitecture().GetAddressByteSize();
98     }
99   }
100   return byte_order != eByteOrderInvalid && addr_size != 0;
101 }
102
103 static uint64_t ReadUIntMax64(ExecutionContextScope *exe_scope,
104                               const Address &address, uint32_t byte_size,
105                               bool &success) {
106   uint64_t uval64 = 0;
107   if (exe_scope == nullptr || byte_size > sizeof(uint64_t)) {
108     success = false;
109     return 0;
110   }
111   uint64_t buf = 0;
112
113   success = ReadBytes(exe_scope, address, &buf, byte_size) == byte_size;
114   if (success) {
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);
121     } else
122       success = false;
123   }
124   return uval64;
125 }
126
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)
131     return false;
132
133   bool success = false;
134   addr_t deref_addr = ReadUIntMax64(exe_scope, address, pointer_size, success);
135   if (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
139     // section load list
140     Target *target = exe_ctx.GetTargetPtr();
141     if (target && !target->GetSectionLoadList().IsEmpty()) {
142       if (target->GetSectionLoadList().ResolveLoadAddress(deref_addr,
143                                                           deref_so_addr))
144         return true;
145     } else {
146       // If we were not running, yet able to read an integer, we must
147       // have a module
148       ModuleSP module_sp(address.GetModule());
149
150       assert(module_sp);
151       if (module_sp->ResolveFileAddress(deref_addr, deref_so_addr))
152         return true;
153     }
154
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);
159     return true;
160   }
161   return false;
162 }
163
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)
167     return 0;
168   std::vector<uint8_t> buf(byte_size, 0);
169
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);
175
176       DumpDataExtractor(data, strm,
177                         0,                    // Start offset in "data"
178                         eFormatHex,           // Print as characters
179                         buf.size(),           // Size of item
180                         1,                    // Items count
181                         UINT32_MAX,           // num per line
182                         LLDB_INVALID_ADDRESS, // base address
183                         0,                    // bitfield bit size
184                         0);                   // bitfield bit offset
185
186       return true;
187     }
188   }
189   return false;
190 }
191
192 static size_t ReadCStringFromMemory(ExecutionContextScope *exe_scope,
193                                     const Address &address, Stream *strm) {
194   if (exe_scope == nullptr)
195     return 0;
196   const size_t k_buf_len = 256;
197   char buf[k_buf_len + 1];
198   buf[k_buf_len] = '\0'; // NULL terminate
199
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;
203   size_t bytes_read;
204   Address curr_address(address);
205   strm->PutChar('"');
206   while ((bytes_read = ReadBytes(exe_scope, curr_address, buf, k_buf_len)) >
207          0) {
208     size_t len = strlen(buf);
209     if (len == 0)
210       break;
211     if (len > bytes_read)
212       len = bytes_read;
213
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
222
223                       0); // bitfield bit offset
224
225     total_len += bytes_read;
226
227     if (len < k_buf_len)
228       break;
229     curr_address.SetOffset(curr_address.GetOffset() + bytes_read);
230   }
231   strm->PutChar('"');
232   return total_len;
233 }
234
235 Address::Address(lldb::addr_t abs_addr) : m_section_wp(), m_offset(abs_addr) {}
236
237 Address::Address(addr_t address, const SectionList *section_list)
238     : m_section_wp(), m_offset(LLDB_INVALID_ADDRESS) {
239   ResolveAddressUsingFileSections(address, section_list);
240 }
241
242 const Address &Address::operator=(const Address &rhs) {
243   if (this != &rhs) {
244     m_section_wp = rhs.m_section_wp;
245     m_offset = rhs.m_offset;
246   }
247   return *this;
248 }
249
250 bool Address::ResolveAddressUsingFileSections(addr_t file_addr,
251                                               const SectionList *section_list) {
252   if (section_list) {
253     SectionSP section_sp(
254         section_list->FindSectionContainingFileAddress(file_addr));
255     m_section_wp = section_sp;
256     if (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
260                    // address
261     }
262   }
263   m_offset = file_addr;
264   return false; // Failed to resolve this address to a section offset value
265 }
266
267 ModuleSP Address::GetModule() const {
268   lldb::ModuleSP module_sp;
269   SectionSP section_sp(GetSection());
270   if (section_sp)
271     module_sp = section_sp->GetModule();
272   return module_sp;
273 }
274
275 addr_t Address::GetFileAddress() const {
276   SectionSP section_sp(GetSection());
277   if (section_sp) {
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;
282     }
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;
290   }
291   // No section, we just return the offset since it is the value in this case
292   return m_offset;
293 }
294
295 addr_t Address::GetLoadAddress(Target *target) const {
296   SectionSP section_sp(GetSection());
297   if (section_sp) {
298     if (target) {
299       addr_t sect_load_addr = section_sp->GetLoadBaseAddress(target);
300
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;
305       }
306     }
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;
311   } else {
312     // We don't have a section so the offset is the load address
313     return m_offset;
314   }
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;
318 }
319
320 addr_t Address::GetCallableLoadAddress(Target *target, bool is_indirect) const {
321   addr_t code_addr = LLDB_INVALID_ADDRESS;
322
323   if (is_indirect && target) {
324     ProcessSP processSP = target->GetProcessSP();
325     Status error;
326     if (processSP) {
327       code_addr = processSP->ResolveIndirectFunction(this, error);
328       if (!error.Success())
329         code_addr = LLDB_INVALID_ADDRESS;
330     }
331   } else {
332     code_addr = GetLoadAddress(target);
333   }
334
335   if (code_addr == LLDB_INVALID_ADDRESS)
336     return code_addr;
337
338   if (target)
339     return target->GetCallableLoadAddress(code_addr, GetAddressClass());
340   return code_addr;
341 }
342
343 bool Address::SetCallableLoadAddress(lldb::addr_t load_addr, Target *target) {
344   if (SetLoadAddress(load_addr, target)) {
345     if (target)
346       m_offset = target->GetCallableLoadAddress(m_offset, GetAddressClass());
347     return true;
348   }
349   return false;
350 }
351
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);
359   }
360   return code_addr;
361 }
362
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)) {
367     if (target) {
368       if (addr_class == eAddressClassInvalid)
369         addr_class = GetAddressClass();
370       m_offset = target->GetOpcodeLoadAddress(m_offset, addr_class);
371     }
372     return true;
373   }
374   return false;
375 }
376
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
380   // are
381   // trying to deref a pointer
382   SectionSP section_sp(GetSection());
383   if (!section_sp && style != DumpStyleResolvedPointerDescription)
384     style = DumpStyleLoadAddress;
385
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) {
391     if (target)
392       addr_size = target->GetArchitecture().GetAddressByteSize();
393     else
394       addr_size = sizeof(addr_t);
395   }
396
397   Address so_addr;
398   switch (style) {
399   case DumpStyleInvalid:
400     return false;
401
402   case DumpStyleSectionNameOffset:
403     if (section_sp) {
404       section_sp->DumpName(s);
405       s->Printf(" + %" PRIu64, m_offset);
406     } else {
407       s->Address(m_offset, addr_size);
408     }
409     break;
410
411   case DumpStyleSectionPointerOffset:
412     s->Printf("(Section *)%p + ", static_cast<void *>(section_sp.get()));
413     s->Address(m_offset, addr_size);
414     break;
415
416   case DumpStyleModuleWithFileAddress:
417     if (section_sp) {
418       ModuleSP module_sp = section_sp->GetModule();
419       if (module_sp)
420         s->Printf("%s[", module_sp->GetFileSpec().GetFilename().AsCString(
421                              "<Unknown>"));
422       else
423         s->Printf("%s[", "<Unknown>");
424     }
425     LLVM_FALLTHROUGH;
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);
431       return false;
432     }
433     s->Address(file_addr, addr_size);
434     if (style == DumpStyleModuleWithFileAddress && section_sp)
435       s->PutChar(']');
436   } break;
437
438   case DumpStyleLoadAddress: {
439     addr_t load_addr = GetLoadAddress(target);
440
441     /*
442      * MIPS:
443      * Display address in compressed form for MIPS16 or microMIPS
444      * if the address belongs to eAddressClassCodeAlternateISA.
445     */
446     if (target) {
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);
454     }
455
456     if (load_addr == LLDB_INVALID_ADDRESS) {
457       if (fallback_style != DumpStyleInvalid)
458         return Dump(s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
459       return false;
460     }
461     s->Address(load_addr, addr_size);
462   } break;
463
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());
471       if (target)
472         pointer_size = target->GetArchitecture().GetAddressByteSize();
473       else if (module_sp)
474         pointer_size = module_sp->GetArchitecture().GetAddressByteSize();
475
476       bool showed_info = false;
477       if (section_sp) {
478         SectionType sect_type = section_sp->GetType();
479         switch (sect_type) {
480         case eSectionTypeData:
481           if (module_sp) {
482             SymbolVendor *sym_vendor = module_sp->GetSymbolVendor();
483             if (sym_vendor) {
484               Symtab *symtab = sym_vendor->GetSymtab();
485               if (symtab) {
486                 const addr_t file_Addr = GetFileAddress();
487                 Symbol *symbol =
488                     symtab->FindSymbolContainingFileAddress(file_Addr);
489                 if (symbol) {
490                   const char *symbol_name = symbol->GetName().AsCString();
491                   if (symbol_name) {
492                     s->PutCString(symbol_name);
493                     addr_t delta =
494                         file_Addr - symbol->GetAddressRef().GetFileAddress();
495                     if (delta)
496                       s->Printf(" + %" PRIu64, delta);
497                     showed_info = true;
498                   }
499                 }
500               }
501             }
502           }
503           break;
504
505         case eSectionTypeDataCString:
506           // Read the C string from memory and display it
507           showed_info = true;
508           ReadCStringFromMemory(exe_scope, *this, s);
509           break;
510
511         case eSectionTypeDataCStringPointers:
512           if (ReadAddress(exe_scope, *this, pointer_size, so_addr)) {
513 #if VERBOSE_OUTPUT
514             s->PutCString("(char *)");
515             so_addr.Dump(s, exe_scope, DumpStyleLoadAddress,
516                          DumpStyleFileAddress);
517             s->PutCString(": ");
518 #endif
519             showed_info = true;
520             ReadCStringFromMemory(exe_scope, so_addr, s);
521           }
522           break;
523
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) {
531                 showed_info = true;
532 #if VERBOSE_OUTPUT
533                 s->PutCString("(objc_msgref *) -> { (func*)");
534                 so_addr.Dump(s, exe_scope, DumpStyleLoadAddress,
535                              DumpStyleFileAddress);
536 #else
537                 s->PutCString("{ ");
538 #endif
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,
542                                         false, true, true);
543                 if (ReadAddress(exe_scope, cstr_addr, pointer_size, so_addr)) {
544 #if VERBOSE_OUTPUT
545                   s->PutCString("), (char *)");
546                   so_addr.Dump(s, exe_scope, DumpStyleLoadAddress,
547                                DumpStyleFileAddress);
548                   s->PutCString(" (");
549 #else
550                   s->PutCString(", ");
551 #endif
552                   ReadCStringFromMemory(exe_scope, so_addr, s);
553                 }
554 #if VERBOSE_OUTPUT
555                 s->PutCString(") }");
556 #else
557                 s->PutCString(" }");
558 #endif
559               }
560             }
561           }
562           break;
563
564         case eSectionTypeDataObjCCFStrings: {
565           Address cfstring_data_addr(*this);
566           cfstring_data_addr.SetOffset(cfstring_data_addr.GetOffset() +
567                                        (2 * pointer_size));
568           if (ReadAddress(exe_scope, cfstring_data_addr, pointer_size,
569                           so_addr)) {
570 #if VERBOSE_OUTPUT
571             s->PutCString("(CFString *) ");
572             cfstring_data_addr.Dump(s, exe_scope, DumpStyleLoadAddress,
573                                     DumpStyleFileAddress);
574             s->PutCString(" -> @");
575 #else
576             s->PutChar('@');
577 #endif
578             if (so_addr.Dump(s, exe_scope, DumpStyleResolvedDescription))
579               showed_info = true;
580           }
581         } break;
582
583         case eSectionTypeData4:
584           // Read the 4 byte data and display it
585           showed_info = true;
586           s->PutCString("(uint32_t) ");
587           DumpUInt(exe_scope, *this, 4, s);
588           break;
589
590         case eSectionTypeData8:
591           // Read the 8 byte data and display it
592           showed_info = true;
593           s->PutCString("(uint64_t) ");
594           DumpUInt(exe_scope, *this, 8, s);
595           break;
596
597         case eSectionTypeData16:
598           // Read the 16 byte data and display it
599           showed_info = true;
600           s->PutCString("(uint128_t) ");
601           DumpUInt(exe_scope, *this, 16, s);
602           break;
603
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);
610
611             showed_info = true;
612             if (so_addr.IsSectionOffset()) {
613               SymbolContext pointer_sc;
614               if (target) {
615                 target->GetImages().ResolveSymbolContextForAddress(
616                     so_addr, eSymbolContextEverything, pointer_sc);
617                 if (pointer_sc.function != nullptr ||
618                     pointer_sc.symbol != nullptr) {
619                   s->PutCString(": ");
620                   pointer_sc.DumpStopContext(s, exe_scope, so_addr, true, false,
621                                              false, true, true);
622                 }
623               }
624             }
625           }
626           break;
627
628         default:
629           break;
630         }
631       }
632
633       if (!showed_info) {
634         if (module_sp) {
635           SymbolContext sc;
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;
652                 }
653               }
654             }
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);
661             } else {
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);
666             }
667           }
668         }
669       }
670     } else {
671       if (fallback_style != DumpStyleInvalid)
672         return Dump(s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
673       return false;
674     }
675     break;
676
677   case DumpStyleDetailedSymbolContext:
678     if (IsSectionOffset()) {
679       ModuleSP module_sp(GetModule());
680       if (module_sp) {
681         SymbolContext sc;
682         module_sp->ResolveSymbolContextForAddress(
683             *this, eSymbolContextEverything | eSymbolContextVariable, sc);
684         if (sc.symbol) {
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())
691             sc.symbol = nullptr;
692         }
693         sc.GetDescription(s, eDescriptionLevelBrief, target);
694
695         if (sc.block) {
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; },
703                                     &variable_list);
704
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)) {
709               s->Indent();
710               s->Printf("   Variable: id = {0x%8.8" PRIx64 "}, name = \"%s\"",
711                         var->GetID(), var->GetName().GetCString());
712               Type *type = var->GetType();
713               if (type)
714                 s->Printf(", type = \"%s\"", type->GetName().GetCString());
715               else
716                 s->PutCString(", type = <unknown>");
717               s->PutCString(", location = ");
718               var->DumpLocationForAddress(s, *this);
719               s->PutCString(", decl = ");
720               var->GetDeclaration().DumpStopContext(s, false);
721               s->EOL();
722             }
723           }
724         }
725       }
726     } else {
727       if (fallback_style != DumpStyleInvalid)
728         return Dump(s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
729       return false;
730     }
731     break;
732
733   case DumpStyleResolvedPointerDescription: {
734     Process *process = exe_ctx.GetProcessPtr();
735     if (process) {
736       addr_t load_addr = GetLoadAddress(target);
737       if (load_addr != LLDB_INVALID_ADDRESS) {
738         Status memory_error;
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,
744                                                target)) {
745             StreamString strm;
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());
751               return true;
752             }
753           }
754         }
755       }
756     }
757     if (fallback_style != DumpStyleInvalid)
758       return Dump(s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
759     return false;
760   } break;
761   }
762
763   return true;
764 }
765
766 bool Address::SectionWasDeleted() const {
767   if (GetSection())
768     return false;
769   return SectionWasDeletedPrivate();
770 }
771
772 bool Address::SectionWasDeletedPrivate() const {
773   lldb::SectionWP empty_section_wp;
774
775   // If either call to "std::weak_ptr::owner_before(...) value returns true,
776   // this
777   // indicates that m_section_wp once contained (possibly still does) a
778   // reference
779   // to a valid shared pointer. This helps us know if we had a valid reference
780   // to
781   // a section which is now invalid because the module it was in was
782   // unloaded/deleted,
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);
786 }
787
788 uint32_t Address::CalculateSymbolContext(SymbolContext *sc,
789                                          uint32_t resolve_scope) const {
790   sc->Clear(false);
791   // Absolute addresses don't have enough information to reconstruct even their
792   // target.
793
794   SectionSP section_sp(GetSection());
795   if (section_sp) {
796     ModuleSP module_sp(section_sp->GetModule());
797     if (module_sp) {
798       sc->module_sp = module_sp;
799       if (sc->module_sp)
800         return sc->module_sp->ResolveSymbolContextForAddress(
801             *this, resolve_scope, *sc);
802     }
803   }
804   return 0;
805 }
806
807 ModuleSP Address::CalculateSymbolContextModule() const {
808   SectionSP section_sp(GetSection());
809   if (section_sp)
810     return section_sp->GetModule();
811   return ModuleSP();
812 }
813
814 CompileUnit *Address::CalculateSymbolContextCompileUnit() const {
815   SectionSP section_sp(GetSection());
816   if (section_sp) {
817     SymbolContext sc;
818     sc.module_sp = section_sp->GetModule();
819     if (sc.module_sp) {
820       sc.module_sp->ResolveSymbolContextForAddress(*this,
821                                                    eSymbolContextCompUnit, sc);
822       return sc.comp_unit;
823     }
824   }
825   return nullptr;
826 }
827
828 Function *Address::CalculateSymbolContextFunction() const {
829   SectionSP section_sp(GetSection());
830   if (section_sp) {
831     SymbolContext sc;
832     sc.module_sp = section_sp->GetModule();
833     if (sc.module_sp) {
834       sc.module_sp->ResolveSymbolContextForAddress(*this,
835                                                    eSymbolContextFunction, sc);
836       return sc.function;
837     }
838   }
839   return nullptr;
840 }
841
842 Block *Address::CalculateSymbolContextBlock() const {
843   SectionSP section_sp(GetSection());
844   if (section_sp) {
845     SymbolContext sc;
846     sc.module_sp = section_sp->GetModule();
847     if (sc.module_sp) {
848       sc.module_sp->ResolveSymbolContextForAddress(*this, eSymbolContextBlock,
849                                                    sc);
850       return sc.block;
851     }
852   }
853   return nullptr;
854 }
855
856 Symbol *Address::CalculateSymbolContextSymbol() const {
857   SectionSP section_sp(GetSection());
858   if (section_sp) {
859     SymbolContext sc;
860     sc.module_sp = section_sp->GetModule();
861     if (sc.module_sp) {
862       sc.module_sp->ResolveSymbolContextForAddress(*this, eSymbolContextSymbol,
863                                                    sc);
864       return sc.symbol;
865     }
866   }
867   return nullptr;
868 }
869
870 bool Address::CalculateSymbolContextLineEntry(LineEntry &line_entry) const {
871   SectionSP section_sp(GetSection());
872   if (section_sp) {
873     SymbolContext sc;
874     sc.module_sp = section_sp->GetModule();
875     if (sc.module_sp) {
876       sc.module_sp->ResolveSymbolContextForAddress(*this,
877                                                    eSymbolContextLineEntry, sc);
878       if (sc.line_entry.IsValid()) {
879         line_entry = sc.line_entry;
880         return true;
881       }
882     }
883   }
884   line_entry.Clear();
885   return false;
886 }
887
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)
892     return -1;
893   if (a_file_addr > b_file_addr)
894     return +1;
895   return 0;
896 }
897
898 int Address::CompareLoadAddress(const Address &a, const Address &b,
899                                 Target *target) {
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)
904     return -1;
905   if (a_load_addr > b_load_addr)
906     return +1;
907   return 0;
908 }
909
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)
916     return -1;
917   if (a_module > b_module)
918     return +1;
919   // Modules are the same, just compare the file address since they should
920   // be unique
921   addr_t a_file_addr = a.GetFileAddress();
922   addr_t b_file_addr = b.GetFileAddress();
923   if (a_file_addr < b_file_addr)
924     return -1;
925   if (a_file_addr > b_file_addr)
926     return +1;
927   return 0;
928 }
929
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);
934 }
935
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.
941 //
942 // To sum things up:
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
946 //
947 // This basically lets Address objects be used in ordered collection
948 // classes.
949 //----------------------------------------------------------------------
950
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();
959   } else {
960     // The addresses are from different modules, just use the module
961     // pointer value to get consistent ordering
962     return lhs_module < rhs_module;
963   }
964 }
965
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();
974   } else {
975     // The addresses are from different modules, just use the module
976     // pointer value to get consistent ordering
977     return lhs_module > rhs_module;
978   }
979 }
980
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();
984 }
985
986 // The operator != checks for exact inequality only (differing section, or
987 // different offset)
988 bool lldb_private::operator!=(const Address &a, const Address &rhs) {
989   return a.GetOffset() != rhs.GetOffset() || a.GetSection() != rhs.GetSection();
990 }
991
992 AddressClass Address::GetAddressClass() const {
993   ModuleSP module_sp(GetModule());
994   if (module_sp) {
995     ObjectFile *obj_file = module_sp->GetObjectFile();
996     if (obj_file) {
997       // Give the symbol vendor a chance to add to the unified section list.
998       module_sp->GetSymbolVendor();
999       return obj_file->GetAddressClass(GetFileAddress());
1000     }
1001   }
1002   return eAddressClassUnknown;
1003 }
1004
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))
1009     return true;
1010   m_section_wp.reset();
1011   m_offset = load_addr;
1012   return false;
1013 }