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