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