blob: 3b97253d0402786196c0080f1b250d3db59e8c03 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- 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"
Greg Clayton1f746072012-08-29 21:13:06 +000013#include "lldb/Symbol/Block.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000014#include "lldb/Symbol/ObjectFile.h"
Greg Claytonc749eb82011-07-11 05:12:02 +000015#include "lldb/Symbol/Variable.h"
16#include "lldb/Symbol/VariableList.h"
Greg Claytonf5e56de2010-09-14 23:36:40 +000017#include "lldb/Target/ExecutionContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018#include "lldb/Target/Process.h"
Greg Claytond5944cd2013-12-06 01:12:00 +000019#include "lldb/Target/SectionLoadList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include "lldb/Target/Target.h"
Michael Sartaina7499c92013-07-01 19:45:50 +000021#include "lldb/Symbol/SymbolVendor.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022
Greg Clayton3f5c08f2011-05-18 22:01:49 +000023#include "llvm/ADT/Triple.h"
24
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025using namespace lldb;
26using namespace lldb_private;
27
28static size_t
29ReadBytes (ExecutionContextScope *exe_scope, const Address &address, void *dst, size_t dst_len)
30{
31 if (exe_scope == NULL)
32 return 0;
33
Greg Claytond9e416c2012-02-18 05:35:26 +000034 TargetSP target_sp (exe_scope->CalculateTarget());
35 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036 {
37 Error error;
Greg Claytondb598232011-01-07 01:57:07 +000038 bool prefer_file_cache = false;
Greg Claytond9e416c2012-02-18 05:35:26 +000039 return target_sp->ReadMemory (address, prefer_file_cache, dst, dst_len, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040 }
41 return 0;
42}
43
44static bool
45GetByteOrderAndAddressSize (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
Greg Claytond9e416c2012-02-18 05:35:26 +000052 TargetSP target_sp (exe_scope->CalculateTarget());
53 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000054 {
Greg Claytond9e416c2012-02-18 05:35:26 +000055 byte_order = target_sp->GetArchitecture().GetByteOrder();
56 addr_size = target_sp->GetArchitecture().GetAddressByteSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000057 }
58
59 if (byte_order == eByteOrderInvalid || addr_size == 0)
60 {
Greg Claytone72dfb32012-02-24 01:59:29 +000061 ModuleSP module_sp (address.GetModule());
62 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000063 {
Greg Claytone72dfb32012-02-24 01:59:29 +000064 byte_order = module_sp->GetArchitecture().GetByteOrder();
65 addr_size = module_sp->GetArchitecture().GetAddressByteSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000066 }
67 }
68 return byte_order != eByteOrderInvalid && addr_size != 0;
69}
70
71static uint64_t
72ReadUIntMax64 (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 }
Johnny Chen40e35922011-08-25 17:40:39 +000080 uint64_t buf = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000081
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);
Greg Claytonc7bece562013-01-25 18:06:21 +000090 lldb::offset_t offset = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000091 uval64 = data.GetU64(&offset);
92 }
93 else
94 success = false;
95 }
96 return uval64;
97}
98
99static bool
100ReadAddress (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 {
Greg Claytonf5e56de2010-09-14 23:36:40 +0000110 ExecutionContext exe_ctx;
Greg Clayton0603aa92010-10-04 01:05:56 +0000111 exe_scope->CalculateExecutionContext(exe_ctx);
Greg Claytonf5e56de2010-09-14 23:36:40 +0000112 // If we have any sections that are loaded, try and resolve using the
113 // section load list
Greg Claytonc14ee322011-09-22 04:58:26 +0000114 Target *target = exe_ctx.GetTargetPtr();
115 if (target && !target->GetSectionLoadList().IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000116 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000117 if (target->GetSectionLoadList().ResolveLoadAddress (deref_addr, deref_so_addr))
Greg Claytonf5e56de2010-09-14 23:36:40 +0000118 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000119 }
120 else
121 {
Greg Claytonf5e56de2010-09-14 23:36:40 +0000122 // If we were not running, yet able to read an integer, we must
123 // have a module
Greg Claytone72dfb32012-02-24 01:59:29 +0000124 ModuleSP module_sp (address.GetModule());
125
126 assert (module_sp);
127 if (module_sp->ResolveFileAddress(deref_addr, deref_so_addr))
Greg Claytonf5e56de2010-09-14 23:36:40 +0000128 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000129 }
Greg Claytonf5e56de2010-09-14 23:36:40 +0000130
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).
Greg Claytone72dfb32012-02-24 01:59:29 +0000134 deref_so_addr.SetRawAddress(deref_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000135 return true;
136 }
137 return false;
138}
139
140static bool
141DumpUInt (ExecutionContextScope *exe_scope, const Address &address, uint32_t byte_size, Stream* strm)
142{
Greg Clayton471b31c2010-07-20 22:52:08 +0000143 if (exe_scope == NULL || byte_size == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000144 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 {
Greg Clayton471b31c2010-07-20 22:52:08 +0000153 DataExtractor data (&buf.front(), buf.size(), byte_order, addr_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000154
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
172static size_t
173ReadCStringFromMemory (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
Greg Clayton710dd5a2011-01-08 20:28:42 +0000181 // Byte order and address size don't matter for C string dumping..
Greg Clayton7fb56d02011-02-01 01:31:41 +0000182 DataExtractor data (buf, sizeof(buf), lldb::endian::InlHostByteOrder(), 4);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000183 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
Greg Claytone72dfb32012-02-24 01:59:29 +0000216Address::Address (lldb::addr_t abs_addr) :
217 m_section_wp (),
218 m_offset (abs_addr)
219{
220}
221
222Address::Address (addr_t address, const SectionList *section_list) :
223 m_section_wp (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000224 m_offset (LLDB_INVALID_ADDRESS)
225{
Greg Claytone72dfb32012-02-24 01:59:29 +0000226 ResolveAddressUsingFileSections(address, section_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000227}
228
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000229const Address&
230Address::operator= (const Address& rhs)
231{
232 if (this != &rhs)
233 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000234 m_section_wp = rhs.m_section_wp;
Daniel Malea48184602013-04-22 20:59:13 +0000235 m_offset = rhs.m_offset.load();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000236 }
237 return *this;
238}
239
240bool
Greg Claytone72dfb32012-02-24 01:59:29 +0000241Address::ResolveAddressUsingFileSections (addr_t file_addr, const SectionList *section_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000242{
Greg Claytone72dfb32012-02-24 01:59:29 +0000243 if (section_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000244 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000245 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 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000253 }
Greg Claytone72dfb32012-02-24 01:59:29 +0000254 m_offset = file_addr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000255 return false; // Failed to resolve this address to a section offset value
256}
257
Greg Claytone1cd1be2012-01-29 20:56:30 +0000258ModuleSP
Greg Claytone72dfb32012-02-24 01:59:29 +0000259Address::GetModule () const
Greg Claytone1cd1be2012-01-29 20:56:30 +0000260{
261 lldb::ModuleSP module_sp;
Greg Claytone72dfb32012-02-24 01:59:29 +0000262 SectionSP section_sp (GetSection());
263 if (section_sp)
264 module_sp = section_sp->GetModule();
Greg Claytone1cd1be2012-01-29 20:56:30 +0000265 return module_sp;
266}
267
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000268addr_t
269Address::GetFileAddress () const
270{
Greg Claytone72dfb32012-02-24 01:59:29 +0000271 SectionSP section_sp (GetSection());
272 if (section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000273 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000274 addr_t sect_file_addr = section_sp->GetFileAddress();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000275 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 }
Greg Claytonb35db632013-11-09 00:03:31 +0000284 else if (SectionWasDeletedPrivate())
Greg Claytoncae56522013-11-06 02:29:13 +0000285 {
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 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000290 // No section, we just return the offset since it is the value in this case
291 return m_offset;
292}
293
294addr_t
Greg Claytonf5e56de2010-09-14 23:36:40 +0000295Address::GetLoadAddress (Target *target) const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000296{
Greg Claytone72dfb32012-02-24 01:59:29 +0000297 SectionSP section_sp (GetSection());
Greg Claytoncae56522013-11-06 02:29:13 +0000298 if (section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000299 {
Greg Claytoncae56522013-11-06 02:29:13 +0000300 if (target)
Greg Claytonf5e56de2010-09-14 23:36:40 +0000301 {
Greg Claytoncae56522013-11-06 02:29:13 +0000302 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 }
Greg Claytonf5e56de2010-09-14 23:36:40 +0000310 }
311 }
Greg Claytonb35db632013-11-09 00:03:31 +0000312 else if (SectionWasDeletedPrivate())
Greg Claytoncae56522013-11-06 02:29:13 +0000313 {
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.
Greg Claytonf5e56de2010-09-14 23:36:40 +0000325 return LLDB_INVALID_ADDRESS;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000326}
327
Greg Clayton3f5c08f2011-05-18 22:01:49 +0000328addr_t
Matt Kopec00049b82013-02-27 20:13:38 +0000329Address::GetCallableLoadAddress (Target *target, bool is_indirect) const
Greg Clayton3f5c08f2011-05-18 22:01:49 +0000330{
Matt Kopec00049b82013-02-27 20:13:38 +0000331 if (is_indirect && target) {
332 ProcessSP processSP = target->GetProcessSP();
333 Error error;
334 if (processSP.get())
335 return processSP->ResolveIndirectFunction(this, error);
336 }
337
Greg Clayton3f5c08f2011-05-18 22:01:49 +0000338 addr_t code_addr = GetLoadAddress (target);
Greg Claytonf3ef3d22011-05-22 22:46:53 +0000339
340 if (target)
341 return target->GetCallableLoadAddress (code_addr, GetAddressClass());
Greg Clayton3f5c08f2011-05-18 22:01:49 +0000342 return code_addr;
343}
344
Greg Claytoncff851a2011-05-22 04:32:55 +0000345bool
346Address::SetCallableLoadAddress (lldb::addr_t load_addr, Target *target)
347{
348 if (SetLoadAddress (load_addr, target))
349 {
Greg Claytonf3ef3d22011-05-22 22:46:53 +0000350 if (target)
351 m_offset = target->GetCallableLoadAddress(m_offset, GetAddressClass());
Greg Claytoncff851a2011-05-22 04:32:55 +0000352 return true;
353 }
354 return false;
355}
356
Greg Clayton92bb12c2011-05-19 18:17:41 +0000357addr_t
358Address::GetOpcodeLoadAddress (Target *target) const
359{
360 addr_t code_addr = GetLoadAddress (target);
Greg Clayton92bb12c2011-05-19 18:17:41 +0000361 if (code_addr != LLDB_INVALID_ADDRESS)
Greg Claytonf3ef3d22011-05-22 22:46:53 +0000362 code_addr = target->GetOpcodeLoadAddress (code_addr, GetAddressClass());
Greg Clayton92bb12c2011-05-19 18:17:41 +0000363 return code_addr;
364}
365
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000366bool
Greg Claytoncff851a2011-05-22 04:32:55 +0000367Address::SetOpcodeLoadAddress (lldb::addr_t load_addr, Target *target)
368{
369 if (SetLoadAddress (load_addr, target))
370 {
Greg Claytonf3ef3d22011-05-22 22:46:53 +0000371 if (target)
372 m_offset = target->GetOpcodeLoadAddress (m_offset, GetAddressClass());
Greg Claytoncff851a2011-05-22 04:32:55 +0000373 return true;
374 }
375 return false;
376}
377
378bool
Greg Claytondda4f7b2010-06-30 23:03:03 +0000379Address::Dump (Stream *s, ExecutionContextScope *exe_scope, DumpStyle style, DumpStyle fallback_style, uint32_t addr_size) const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000380{
Greg Claytonc3a86bf2012-07-11 22:18:24 +0000381 // If the section was NULL, only load address is going to work unless we are
382 // trying to deref a pointer
Greg Claytone72dfb32012-02-24 01:59:29 +0000383 SectionSP section_sp (GetSection());
Greg Claytonc3a86bf2012-07-11 22:18:24 +0000384 if (!section_sp && style != DumpStyleResolvedPointerDescription)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000385 style = DumpStyleLoadAddress;
386
Greg Claytond9e416c2012-02-18 05:35:26 +0000387 ExecutionContext exe_ctx (exe_scope);
388 Target *target = exe_ctx.GetTargetPtr();
Greg Claytondda4f7b2010-06-30 23:03:03 +0000389 // If addr_byte_size is UINT32_MAX, then determine the correct address
390 // byte size for the process or default to the size of addr_t
391 if (addr_size == UINT32_MAX)
392 {
Greg Claytond9e416c2012-02-18 05:35:26 +0000393 if (target)
Greg Clayton514487e2011-02-15 21:59:32 +0000394 addr_size = target->GetArchitecture().GetAddressByteSize ();
Greg Claytondda4f7b2010-06-30 23:03:03 +0000395 else
396 addr_size = sizeof(addr_t);
397 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000398
Greg Claytonc9800662010-09-10 01:30:46 +0000399 Address so_addr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000400 switch (style)
401 {
Greg Claytonc982c762010-07-09 20:39:50 +0000402 case DumpStyleInvalid:
403 return false;
404
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000405 case DumpStyleSectionNameOffset:
Greg Claytone72dfb32012-02-24 01:59:29 +0000406 if (section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000407 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000408 section_sp->DumpName(s);
Daniel Malea48184602013-04-22 20:59:13 +0000409 s->Printf (" + %" PRIu64, m_offset.load());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000410 }
411 else
412 {
Greg Claytondda4f7b2010-06-30 23:03:03 +0000413 s->Address(m_offset, addr_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000414 }
415 break;
416
417 case DumpStyleSectionPointerOffset:
Greg Claytone72dfb32012-02-24 01:59:29 +0000418 s->Printf("(Section *)%p + ", section_sp.get());
Greg Claytondda4f7b2010-06-30 23:03:03 +0000419 s->Address(m_offset, addr_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000420 break;
421
422 case DumpStyleModuleWithFileAddress:
Greg Claytone72dfb32012-02-24 01:59:29 +0000423 if (section_sp)
424 s->Printf("%s[", section_sp->GetModule()->GetFileSpec().GetFilename().AsCString());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000425 // Fall through
426 case DumpStyleFileAddress:
427 {
428 addr_t file_addr = GetFileAddress();
429 if (file_addr == LLDB_INVALID_ADDRESS)
430 {
431 if (fallback_style != DumpStyleInvalid)
Greg Claytondda4f7b2010-06-30 23:03:03 +0000432 return Dump (s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000433 return false;
434 }
435 s->Address (file_addr, addr_size);
Greg Claytone72dfb32012-02-24 01:59:29 +0000436 if (style == DumpStyleModuleWithFileAddress && section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000437 s->PutChar(']');
438 }
439 break;
440
441 case DumpStyleLoadAddress:
442 {
Greg Claytonf5e56de2010-09-14 23:36:40 +0000443 addr_t load_addr = GetLoadAddress (target);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000444 if (load_addr == LLDB_INVALID_ADDRESS)
445 {
446 if (fallback_style != DumpStyleInvalid)
Greg Claytondda4f7b2010-06-30 23:03:03 +0000447 return Dump (s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000448 return false;
449 }
450 s->Address (load_addr, addr_size);
451 }
452 break;
453
454 case DumpStyleResolvedDescription:
Greg Clayton54b8b8c2010-07-01 01:26:43 +0000455 case DumpStyleResolvedDescriptionNoModule:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000456 if (IsSectionOffset())
457 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000458 uint32_t pointer_size = 4;
Greg Claytone72dfb32012-02-24 01:59:29 +0000459 ModuleSP module_sp (GetModule());
Greg Clayton514487e2011-02-15 21:59:32 +0000460 if (target)
461 pointer_size = target->GetArchitecture().GetAddressByteSize();
Greg Claytone72dfb32012-02-24 01:59:29 +0000462 else if (module_sp)
463 pointer_size = module_sp->GetArchitecture().GetAddressByteSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000464
465 bool showed_info = false;
Greg Claytone72dfb32012-02-24 01:59:29 +0000466 if (section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000467 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000468 SectionType sect_type = section_sp->GetType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000469 switch (sect_type)
470 {
Greg Clayton89411422010-10-08 00:21:05 +0000471 case eSectionTypeData:
Greg Claytone72dfb32012-02-24 01:59:29 +0000472 if (module_sp)
Greg Clayton89411422010-10-08 00:21:05 +0000473 {
Michael Sartaina7499c92013-07-01 19:45:50 +0000474 SymbolVendor *sym_vendor = module_sp->GetSymbolVendor();
475 if (sym_vendor)
Greg Clayton89411422010-10-08 00:21:05 +0000476 {
Michael Sartaina7499c92013-07-01 19:45:50 +0000477 Symtab *symtab = sym_vendor->GetSymtab();
Greg Clayton89411422010-10-08 00:21:05 +0000478 if (symtab)
479 {
480 const addr_t file_Addr = GetFileAddress();
481 Symbol *symbol = symtab->FindSymbolContainingFileAddress (file_Addr);
482 if (symbol)
483 {
484 const char *symbol_name = symbol->GetName().AsCString();
485 if (symbol_name)
486 {
487 s->PutCString(symbol_name);
Greg Claytone7612132012-03-07 21:03:09 +0000488 addr_t delta = file_Addr - symbol->GetAddress().GetFileAddress();
Greg Clayton89411422010-10-08 00:21:05 +0000489 if (delta)
Daniel Malead01b2952012-11-29 21:49:15 +0000490 s->Printf(" + %" PRIu64, delta);
Greg Clayton89411422010-10-08 00:21:05 +0000491 showed_info = true;
492 }
493 }
494 }
495 }
496 }
497 break;
498
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000499 case eSectionTypeDataCString:
500 // Read the C string from memory and display it
501 showed_info = true;
502 ReadCStringFromMemory (exe_scope, *this, s);
503 break;
504
505 case eSectionTypeDataCStringPointers:
506 {
507 if (ReadAddress (exe_scope, *this, pointer_size, so_addr))
508 {
509#if VERBOSE_OUTPUT
510 s->PutCString("(char *)");
511 so_addr.Dump(s, exe_scope, DumpStyleLoadAddress, DumpStyleFileAddress);
512 s->PutCString(": ");
513#endif
514 showed_info = true;
515 ReadCStringFromMemory (exe_scope, so_addr, s);
516 }
517 }
518 break;
519
520 case eSectionTypeDataObjCMessageRefs:
521 {
522 if (ReadAddress (exe_scope, *this, pointer_size, so_addr))
523 {
Greg Clayton54b8b8c2010-07-01 01:26:43 +0000524 if (target && so_addr.IsSectionOffset())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000525 {
Greg Claytonc9800662010-09-10 01:30:46 +0000526 SymbolContext func_sc;
Greg Clayton54b8b8c2010-07-01 01:26:43 +0000527 target->GetImages().ResolveSymbolContextForAddress (so_addr,
Greg Claytond9e416c2012-02-18 05:35:26 +0000528 eSymbolContextEverything,
529 func_sc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000530 if (func_sc.function || func_sc.symbol)
531 {
532 showed_info = true;
533#if VERBOSE_OUTPUT
534 s->PutCString ("(objc_msgref *) -> { (func*)");
535 so_addr.Dump(s, exe_scope, DumpStyleLoadAddress, DumpStyleFileAddress);
536#else
537 s->PutCString ("{ ");
538#endif
539 Address cstr_addr(*this);
540 cstr_addr.SetOffset(cstr_addr.GetOffset() + pointer_size);
Greg Clayton6dadd502010-09-02 21:44:10 +0000541 func_sc.DumpStopContext(s, exe_scope, so_addr, true, true, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000542 if (ReadAddress (exe_scope, cstr_addr, pointer_size, so_addr))
543 {
544#if VERBOSE_OUTPUT
545 s->PutCString("), (char *)");
546 so_addr.Dump(s, exe_scope, DumpStyleLoadAddress, DumpStyleFileAddress);
547 s->PutCString(" (");
548#else
549 s->PutCString(", ");
550#endif
551 ReadCStringFromMemory (exe_scope, so_addr, s);
552 }
553#if VERBOSE_OUTPUT
554 s->PutCString(") }");
555#else
556 s->PutCString(" }");
557#endif
558 }
559 }
560 }
561 }
562 break;
563
564 case eSectionTypeDataObjCCFStrings:
565 {
566 Address cfstring_data_addr(*this);
567 cfstring_data_addr.SetOffset(cfstring_data_addr.GetOffset() + (2 * pointer_size));
568 if (ReadAddress (exe_scope, cfstring_data_addr, pointer_size, so_addr))
569 {
570#if VERBOSE_OUTPUT
571 s->PutCString("(CFString *) ");
572 cfstring_data_addr.Dump(s, exe_scope, DumpStyleLoadAddress, DumpStyleFileAddress);
573 s->PutCString(" -> @");
574#else
575 s->PutChar('@');
576#endif
577 if (so_addr.Dump(s, exe_scope, DumpStyleResolvedDescription))
578 showed_info = true;
579 }
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 {
607 if (ReadAddress (exe_scope, *this, pointer_size, so_addr))
608 {
609 s->PutCString ("(void *)");
610 so_addr.Dump(s, exe_scope, DumpStyleLoadAddress, DumpStyleFileAddress);
611
612 showed_info = true;
613 if (so_addr.IsSectionOffset())
614 {
Greg Claytonc9800662010-09-10 01:30:46 +0000615 SymbolContext pointer_sc;
Greg Claytondda4f7b2010-06-30 23:03:03 +0000616 if (target)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000617 {
Greg Claytondda4f7b2010-06-30 23:03:03 +0000618 target->GetImages().ResolveSymbolContextForAddress (so_addr,
Greg Claytond9e416c2012-02-18 05:35:26 +0000619 eSymbolContextEverything,
620 pointer_sc);
Greg Claytondda4f7b2010-06-30 23:03:03 +0000621 if (pointer_sc.function || pointer_sc.symbol)
622 {
623 s->PutCString(": ");
Greg Clayton6dadd502010-09-02 21:44:10 +0000624 pointer_sc.DumpStopContext(s, exe_scope, so_addr, true, false, false);
Greg Claytondda4f7b2010-06-30 23:03:03 +0000625 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000626 }
627 }
628 }
629 }
630 break;
Greg Claytonc982c762010-07-09 20:39:50 +0000631
632 default:
633 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000634 }
635 }
636
637 if (!showed_info)
638 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000639 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000640 {
Greg Claytonc9800662010-09-10 01:30:46 +0000641 SymbolContext sc;
Greg Claytone72dfb32012-02-24 01:59:29 +0000642 module_sp->ResolveSymbolContextForAddress(*this, eSymbolContextEverything, sc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000643 if (sc.function || sc.symbol)
644 {
645 bool show_stop_context = true;
Greg Clayton8dc0a982010-09-07 21:56:53 +0000646 const bool show_module = (style == DumpStyleResolvedDescription);
647 const bool show_fullpaths = false;
Greg Clayton513c26c2011-01-29 07:10:55 +0000648 const bool show_inlined_frames = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000649 if (sc.function == NULL && sc.symbol != NULL)
650 {
651 // If we have just a symbol make sure it is in the right section
Greg Claytone7612132012-03-07 21:03:09 +0000652 if (sc.symbol->ValueIsAddress())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000653 {
Greg Claytone7612132012-03-07 21:03:09 +0000654 if (sc.symbol->GetAddress().GetSection() != GetSection())
Greg Clayton54b8b8c2010-07-01 01:26:43 +0000655 {
656 // don't show the module if the symbol is a trampoline symbol
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000657 show_stop_context = false;
Greg Clayton54b8b8c2010-07-01 01:26:43 +0000658 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000659 }
660 }
661 if (show_stop_context)
662 {
663 // We have a function or a symbol from the same
664 // sections as this address.
Greg Clayton8dc0a982010-09-07 21:56:53 +0000665 sc.DumpStopContext (s,
666 exe_scope,
667 *this,
668 show_fullpaths,
669 show_module,
670 show_inlined_frames);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000671 }
672 else
673 {
674 // We found a symbol but it was in a different
675 // section so it isn't the symbol we should be
676 // showing, just show the section name + offset
677 Dump (s, exe_scope, DumpStyleSectionNameOffset);
678 }
679 }
680 }
681 }
682 }
683 else
684 {
685 if (fallback_style != DumpStyleInvalid)
Greg Claytondda4f7b2010-06-30 23:03:03 +0000686 return Dump (s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000687 return false;
688 }
689 break;
Greg Claytondda4f7b2010-06-30 23:03:03 +0000690
691 case DumpStyleDetailedSymbolContext:
692 if (IsSectionOffset())
693 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000694 ModuleSP module_sp (GetModule());
695 if (module_sp)
Greg Claytondda4f7b2010-06-30 23:03:03 +0000696 {
Greg Claytonc9800662010-09-10 01:30:46 +0000697 SymbolContext sc;
Greg Claytone72dfb32012-02-24 01:59:29 +0000698 module_sp->ResolveSymbolContextForAddress(*this, eSymbolContextEverything, sc);
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000699 if (sc.symbol)
Greg Claytondda4f7b2010-06-30 23:03:03 +0000700 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000701 // If we have just a symbol make sure it is in the same section
702 // as our address. If it isn't, then we might have just found
703 // the last symbol that came before the address that we are
704 // looking up that has nothing to do with our address lookup.
Greg Claytone7612132012-03-07 21:03:09 +0000705 if (sc.symbol->ValueIsAddress() && sc.symbol->GetAddress().GetSection() != GetSection())
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000706 sc.symbol = NULL;
Greg Claytondda4f7b2010-06-30 23:03:03 +0000707 }
Greg Claytonf5e56de2010-09-14 23:36:40 +0000708 sc.GetDescription(s, eDescriptionLevelBrief, target);
Greg Claytonc749eb82011-07-11 05:12:02 +0000709
710 if (sc.block)
711 {
712 bool can_create = true;
713 bool get_parent_variables = true;
714 bool stop_if_block_is_inlined_function = false;
715 VariableList variable_list;
716 sc.block->AppendVariables (can_create,
717 get_parent_variables,
718 stop_if_block_is_inlined_function,
719 &variable_list);
720
Greg Claytonc7bece562013-01-25 18:06:21 +0000721 const size_t num_variables = variable_list.GetSize();
722 for (size_t var_idx = 0; var_idx < num_variables; ++var_idx)
Greg Claytonc749eb82011-07-11 05:12:02 +0000723 {
724 Variable *var = variable_list.GetVariableAtIndex (var_idx).get();
725 if (var && var->LocationIsValidForAddress (*this))
726 {
Greg Claytonc4a8a762012-05-15 18:43:44 +0000727 s->Indent();
Daniel Malead01b2952012-11-29 21:49:15 +0000728 s->Printf (" Variable: id = {0x%8.8" PRIx64 "}, name = \"%s\", type= \"%s\", location =",
Greg Claytonc749eb82011-07-11 05:12:02 +0000729 var->GetID(),
730 var->GetName().GetCString(),
731 var->GetType()->GetName().GetCString());
732 var->DumpLocationForAddress(s, *this);
733 s->PutCString(", decl = ");
734 var->GetDeclaration().DumpStopContext(s, false);
735 s->EOL();
736 }
737 }
738 }
Greg Claytondda4f7b2010-06-30 23:03:03 +0000739 }
740 }
Greg Claytonc749eb82011-07-11 05:12:02 +0000741 else
742 {
743 if (fallback_style != DumpStyleInvalid)
744 return Dump (s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
745 return false;
746 }
Greg Claytondda4f7b2010-06-30 23:03:03 +0000747 break;
Greg Claytonc3a86bf2012-07-11 22:18:24 +0000748 case DumpStyleResolvedPointerDescription:
749 {
750 Process *process = exe_ctx.GetProcessPtr();
751 if (process)
752 {
753 addr_t load_addr = GetLoadAddress (target);
754 if (load_addr != LLDB_INVALID_ADDRESS)
755 {
756 Error memory_error;
757 addr_t dereferenced_load_addr = process->ReadPointerFromMemory(load_addr, memory_error);
758 if (dereferenced_load_addr != LLDB_INVALID_ADDRESS)
759 {
760 Address dereferenced_addr;
761 if (dereferenced_addr.SetLoadAddress(dereferenced_load_addr, target))
762 {
763 StreamString strm;
764 if (dereferenced_addr.Dump (&strm, exe_scope, DumpStyleResolvedDescription, DumpStyleInvalid, addr_size))
765 {
766 s->Address (dereferenced_load_addr, addr_size, " -> ", " ");
767 s->Write(strm.GetData(), strm.GetSize());
768 return true;
769 }
770 }
771 }
772 }
773 }
774 if (fallback_style != DumpStyleInvalid)
775 return Dump (s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
776 return false;
777 }
778 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000779 }
780
781 return true;
782}
783
Greg Claytoncae56522013-11-06 02:29:13 +0000784bool
785Address::SectionWasDeleted() const
786{
Greg Claytonb35db632013-11-09 00:03:31 +0000787 if (GetSection())
788 return false;
789 return SectionWasDeletedPrivate();
790}
791
792bool
793Address::SectionWasDeletedPrivate() const
794{
Greg Claytoncae56522013-11-06 02:29:13 +0000795 lldb::SectionWP empty_section_wp;
796
797 // If either call to "std::weak_ptr::owner_before(...) value returns true, this
798 // indicates that m_section_wp once contained (possibly still does) a reference
799 // to a valid shared pointer. This helps us know if we had a valid reference to
800 // a section which is now invalid because the module it was in was unloaded/deleted,
801 // or if the address doesn't have a valid reference to a section.
802 return empty_section_wp.owner_before(m_section_wp) || m_section_wp.owner_before(empty_section_wp);
803}
804
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000805uint32_t
Greg Clayton6f6bf262011-12-10 21:05:26 +0000806Address::CalculateSymbolContext (SymbolContext *sc, uint32_t resolve_scope) const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000807{
Greg Clayton72310352013-02-23 04:12:47 +0000808 sc->Clear(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000809 // Absolute addresses don't have enough information to reconstruct even their target.
Greg Claytone72dfb32012-02-24 01:59:29 +0000810
811 SectionSP section_sp (GetSection());
812 if (section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000813 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000814 ModuleSP module_sp (section_sp->GetModule());
815 if (module_sp)
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000816 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000817 sc->module_sp = module_sp;
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000818 if (sc->module_sp)
819 return sc->module_sp->ResolveSymbolContextForAddress (*this, resolve_scope, *sc);
820 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000821 }
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000822 return 0;
823}
824
Greg Claytone72dfb32012-02-24 01:59:29 +0000825ModuleSP
Greg Clayton6f6bf262011-12-10 21:05:26 +0000826Address::CalculateSymbolContextModule () const
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000827{
Greg Claytone72dfb32012-02-24 01:59:29 +0000828 SectionSP section_sp (GetSection());
829 if (section_sp)
830 return section_sp->GetModule();
831 return ModuleSP();
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000832}
833
834CompileUnit *
Greg Clayton6f6bf262011-12-10 21:05:26 +0000835Address::CalculateSymbolContextCompileUnit () const
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000836{
Greg Claytone72dfb32012-02-24 01:59:29 +0000837 SectionSP section_sp (GetSection());
838 if (section_sp)
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000839 {
840 SymbolContext sc;
Greg Claytone72dfb32012-02-24 01:59:29 +0000841 sc.module_sp = section_sp->GetModule();
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000842 if (sc.module_sp)
843 {
844 sc.module_sp->ResolveSymbolContextForAddress (*this, eSymbolContextCompUnit, sc);
845 return sc.comp_unit;
846 }
847 }
848 return NULL;
849}
850
851Function *
Greg Clayton6f6bf262011-12-10 21:05:26 +0000852Address::CalculateSymbolContextFunction () const
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000853{
Greg Claytone72dfb32012-02-24 01:59:29 +0000854 SectionSP section_sp (GetSection());
855 if (section_sp)
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000856 {
857 SymbolContext sc;
Greg Claytone72dfb32012-02-24 01:59:29 +0000858 sc.module_sp = section_sp->GetModule();
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000859 if (sc.module_sp)
860 {
861 sc.module_sp->ResolveSymbolContextForAddress (*this, eSymbolContextFunction, sc);
862 return sc.function;
863 }
864 }
865 return NULL;
866}
867
868Block *
Greg Clayton6f6bf262011-12-10 21:05:26 +0000869Address::CalculateSymbolContextBlock () const
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000870{
Greg Claytone72dfb32012-02-24 01:59:29 +0000871 SectionSP section_sp (GetSection());
872 if (section_sp)
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000873 {
874 SymbolContext sc;
Greg Claytone72dfb32012-02-24 01:59:29 +0000875 sc.module_sp = section_sp->GetModule();
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000876 if (sc.module_sp)
877 {
878 sc.module_sp->ResolveSymbolContextForAddress (*this, eSymbolContextBlock, sc);
879 return sc.block;
880 }
881 }
882 return NULL;
883}
884
885Symbol *
Greg Clayton6f6bf262011-12-10 21:05:26 +0000886Address::CalculateSymbolContextSymbol () const
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000887{
Greg Claytone72dfb32012-02-24 01:59:29 +0000888 SectionSP section_sp (GetSection());
889 if (section_sp)
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000890 {
891 SymbolContext sc;
Greg Claytone72dfb32012-02-24 01:59:29 +0000892 sc.module_sp = section_sp->GetModule();
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000893 if (sc.module_sp)
894 {
895 sc.module_sp->ResolveSymbolContextForAddress (*this, eSymbolContextSymbol, sc);
896 return sc.symbol;
897 }
898 }
899 return NULL;
900}
901
902bool
Greg Clayton6f6bf262011-12-10 21:05:26 +0000903Address::CalculateSymbolContextLineEntry (LineEntry &line_entry) const
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000904{
Greg Claytone72dfb32012-02-24 01:59:29 +0000905 SectionSP section_sp (GetSection());
906 if (section_sp)
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000907 {
908 SymbolContext sc;
Greg Claytone72dfb32012-02-24 01:59:29 +0000909 sc.module_sp = section_sp->GetModule();
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000910 if (sc.module_sp)
911 {
912 sc.module_sp->ResolveSymbolContextForAddress (*this, eSymbolContextLineEntry, sc);
913 if (sc.line_entry.IsValid())
914 {
915 line_entry = sc.line_entry;
916 return true;
917 }
918 }
919 }
920 line_entry.Clear();
921 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000922}
923
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000924int
925Address::CompareFileAddress (const Address& a, const Address& b)
926{
927 addr_t a_file_addr = a.GetFileAddress();
928 addr_t b_file_addr = b.GetFileAddress();
929 if (a_file_addr < b_file_addr)
930 return -1;
931 if (a_file_addr > b_file_addr)
932 return +1;
933 return 0;
934}
935
936
937int
Greg Claytonf5e56de2010-09-14 23:36:40 +0000938Address::CompareLoadAddress (const Address& a, const Address& b, Target *target)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000939{
Greg Claytonf5e56de2010-09-14 23:36:40 +0000940 assert (target != NULL);
941 addr_t a_load_addr = a.GetLoadAddress (target);
942 addr_t b_load_addr = b.GetLoadAddress (target);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000943 if (a_load_addr < b_load_addr)
944 return -1;
945 if (a_load_addr > b_load_addr)
946 return +1;
947 return 0;
948}
949
950int
951Address::CompareModulePointerAndOffset (const Address& a, const Address& b)
952{
Greg Claytone72dfb32012-02-24 01:59:29 +0000953 ModuleSP a_module_sp (a.GetModule());
954 ModuleSP b_module_sp (b.GetModule());
955 Module *a_module = a_module_sp.get();
956 Module *b_module = b_module_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000957 if (a_module < b_module)
958 return -1;
959 if (a_module > b_module)
960 return +1;
961 // Modules are the same, just compare the file address since they should
962 // be unique
963 addr_t a_file_addr = a.GetFileAddress();
964 addr_t b_file_addr = b.GetFileAddress();
965 if (a_file_addr < b_file_addr)
966 return -1;
967 if (a_file_addr > b_file_addr)
968 return +1;
969 return 0;
970}
971
972
973size_t
974Address::MemorySize () const
975{
976 // Noting special for the memory size of a single Address object,
977 // it is just the size of itself.
978 return sizeof(Address);
979}
980
981
Greg Claytonb0848c52011-01-08 00:05:12 +0000982//----------------------------------------------------------------------
983// NOTE: Be careful using this operator. It can correctly compare two
984// addresses from the same Module correctly. It can't compare two
985// addresses from different modules in any meaningful way, but it will
986// compare the module pointers.
987//
988// To sum things up:
989// - works great for addresses within the same module
990// - it works for addresses across multiple modules, but don't expect the
991// address results to make much sense
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000992//
Greg Claytonb0848c52011-01-08 00:05:12 +0000993// This basically lets Address objects be used in ordered collection
994// classes.
995//----------------------------------------------------------------------
996
997bool
998lldb_private::operator< (const Address& lhs, const Address& rhs)
999{
Greg Claytone72dfb32012-02-24 01:59:29 +00001000 ModuleSP lhs_module_sp (lhs.GetModule());
1001 ModuleSP rhs_module_sp (rhs.GetModule());
1002 Module *lhs_module = lhs_module_sp.get();
1003 Module *rhs_module = rhs_module_sp.get();
Greg Claytonb0848c52011-01-08 00:05:12 +00001004 if (lhs_module == rhs_module)
1005 {
1006 // Addresses are in the same module, just compare the file addresses
1007 return lhs.GetFileAddress() < rhs.GetFileAddress();
1008 }
1009 else
1010 {
1011 // The addresses are from different modules, just use the module
1012 // pointer value to get consistent ordering
1013 return lhs_module < rhs_module;
1014 }
1015}
1016
1017bool
1018lldb_private::operator> (const Address& lhs, const Address& rhs)
1019{
Greg Claytone72dfb32012-02-24 01:59:29 +00001020 ModuleSP lhs_module_sp (lhs.GetModule());
1021 ModuleSP rhs_module_sp (rhs.GetModule());
1022 Module *lhs_module = lhs_module_sp.get();
1023 Module *rhs_module = rhs_module_sp.get();
Greg Claytonb0848c52011-01-08 00:05:12 +00001024 if (lhs_module == rhs_module)
1025 {
1026 // Addresses are in the same module, just compare the file addresses
1027 return lhs.GetFileAddress() > rhs.GetFileAddress();
1028 }
1029 else
1030 {
1031 // The addresses are from different modules, just use the module
1032 // pointer value to get consistent ordering
1033 return lhs_module > rhs_module;
1034 }
1035}
1036
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001037
1038// The operator == checks for exact equality only (same section, same offset)
1039bool
1040lldb_private::operator== (const Address& a, const Address& rhs)
1041{
Jim Inghamd3480f52013-06-06 22:16:56 +00001042 return a.GetOffset() == rhs.GetOffset() &&
1043 a.GetSection() == rhs.GetSection();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001044}
1045// The operator != checks for exact inequality only (differing section, or
1046// different offset)
1047bool
1048lldb_private::operator!= (const Address& a, const Address& rhs)
1049{
Jim Inghamd3480f52013-06-06 22:16:56 +00001050 return a.GetOffset() != rhs.GetOffset() ||
1051 a.GetSection() != rhs.GetSection();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001052}
1053
Greg Claytone0d378b2011-03-24 21:19:54 +00001054AddressClass
Greg Claytonded470d2011-03-19 01:12:21 +00001055Address::GetAddressClass () const
1056{
Greg Claytone72dfb32012-02-24 01:59:29 +00001057 ModuleSP module_sp (GetModule());
1058 if (module_sp)
Greg Claytonded470d2011-03-19 01:12:21 +00001059 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001060 ObjectFile *obj_file = module_sp->GetObjectFile();
Greg Claytonded470d2011-03-19 01:12:21 +00001061 if (obj_file)
Michael Sartaina7499c92013-07-01 19:45:50 +00001062 {
1063 // Give the symbol vendor a chance to add to the unified section list.
1064 module_sp->GetSymbolVendor();
Greg Claytonded470d2011-03-19 01:12:21 +00001065 return obj_file->GetAddressClass (GetFileAddress());
Michael Sartaina7499c92013-07-01 19:45:50 +00001066 }
Greg Claytonded470d2011-03-19 01:12:21 +00001067 }
1068 return eAddressClassUnknown;
1069}
Greg Claytoncd482e32011-05-18 01:58:14 +00001070
1071bool
1072Address::SetLoadAddress (lldb::addr_t load_addr, Target *target)
1073{
1074 if (target && target->GetSectionLoadList().ResolveLoadAddress(load_addr, *this))
1075 return true;
Greg Claytone72dfb32012-02-24 01:59:29 +00001076 m_section_wp.reset();
Greg Claytoncd482e32011-05-18 01:58:14 +00001077 m_offset = load_addr;
1078 return false;
1079}
1080