blob: f7dfdd9803e0e6bf00a3a08b41469b2eda326c8a [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..
Bruce Mitchener9ccb9702015-11-07 04:40:13 +0000182 DataExtractor data (buf, sizeof(buf), 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{
Jim Ingham1460e4b2014-01-10 23:46:59 +0000331 addr_t code_addr = LLDB_INVALID_ADDRESS;
332
333 if (is_indirect && target)
334 {
Matt Kopec00049b82013-02-27 20:13:38 +0000335 ProcessSP processSP = target->GetProcessSP();
336 Error error;
337 if (processSP.get())
Jim Ingham1460e4b2014-01-10 23:46:59 +0000338 {
339 code_addr = processSP->ResolveIndirectFunction(this, error);
340 if (!error.Success())
341 code_addr = LLDB_INVALID_ADDRESS;
342 }
Matt Kopec00049b82013-02-27 20:13:38 +0000343 }
Jim Ingham1460e4b2014-01-10 23:46:59 +0000344 else
345 {
346 code_addr = GetLoadAddress (target);
347 }
348
349 if (code_addr == LLDB_INVALID_ADDRESS)
350 return code_addr;
351
Greg Claytonf3ef3d22011-05-22 22:46:53 +0000352 if (target)
353 return target->GetCallableLoadAddress (code_addr, GetAddressClass());
Greg Clayton3f5c08f2011-05-18 22:01:49 +0000354 return code_addr;
355}
356
Greg Claytoncff851a2011-05-22 04:32:55 +0000357bool
358Address::SetCallableLoadAddress (lldb::addr_t load_addr, Target *target)
359{
360 if (SetLoadAddress (load_addr, target))
361 {
Greg Claytonf3ef3d22011-05-22 22:46:53 +0000362 if (target)
363 m_offset = target->GetCallableLoadAddress(m_offset, GetAddressClass());
Greg Claytoncff851a2011-05-22 04:32:55 +0000364 return true;
365 }
366 return false;
367}
368
Greg Clayton92bb12c2011-05-19 18:17:41 +0000369addr_t
Tamas Berghammer25b9f7e2015-09-07 09:58:09 +0000370Address::GetOpcodeLoadAddress (Target *target, AddressClass addr_class) const
Greg Clayton92bb12c2011-05-19 18:17:41 +0000371{
372 addr_t code_addr = GetLoadAddress (target);
Greg Clayton92bb12c2011-05-19 18:17:41 +0000373 if (code_addr != LLDB_INVALID_ADDRESS)
Tamas Berghammer25b9f7e2015-09-07 09:58:09 +0000374 {
375 if (addr_class == eAddressClassInvalid)
376 addr_class = GetAddressClass();
377 code_addr = target->GetOpcodeLoadAddress (code_addr, addr_class);
378 }
Greg Clayton92bb12c2011-05-19 18:17:41 +0000379 return code_addr;
380}
381
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000382bool
Tamas Berghammer25b9f7e2015-09-07 09:58:09 +0000383Address::SetOpcodeLoadAddress (lldb::addr_t load_addr, Target *target, AddressClass addr_class)
Greg Claytoncff851a2011-05-22 04:32:55 +0000384{
385 if (SetLoadAddress (load_addr, target))
386 {
Greg Claytonf3ef3d22011-05-22 22:46:53 +0000387 if (target)
Tamas Berghammer25b9f7e2015-09-07 09:58:09 +0000388 {
389 if (addr_class == eAddressClassInvalid)
390 addr_class = GetAddressClass();
391 m_offset = target->GetOpcodeLoadAddress (m_offset, addr_class);
392 }
Greg Claytoncff851a2011-05-22 04:32:55 +0000393 return true;
394 }
395 return false;
396}
397
398bool
Greg Claytondda4f7b2010-06-30 23:03:03 +0000399Address::Dump (Stream *s, ExecutionContextScope *exe_scope, DumpStyle style, DumpStyle fallback_style, uint32_t addr_size) const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000400{
Greg Claytonc3a86bf2012-07-11 22:18:24 +0000401 // If the section was NULL, only load address is going to work unless we are
402 // trying to deref a pointer
Greg Claytone72dfb32012-02-24 01:59:29 +0000403 SectionSP section_sp (GetSection());
Greg Claytonc3a86bf2012-07-11 22:18:24 +0000404 if (!section_sp && style != DumpStyleResolvedPointerDescription)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000405 style = DumpStyleLoadAddress;
406
Greg Claytond9e416c2012-02-18 05:35:26 +0000407 ExecutionContext exe_ctx (exe_scope);
408 Target *target = exe_ctx.GetTargetPtr();
Greg Claytondda4f7b2010-06-30 23:03:03 +0000409 // 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 {
Greg Claytond9e416c2012-02-18 05:35:26 +0000413 if (target)
Greg Clayton514487e2011-02-15 21:59:32 +0000414 addr_size = target->GetArchitecture().GetAddressByteSize ();
Greg Claytondda4f7b2010-06-30 23:03:03 +0000415 else
416 addr_size = sizeof(addr_t);
417 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000418
Greg Claytonc9800662010-09-10 01:30:46 +0000419 Address so_addr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000420 switch (style)
421 {
Greg Claytonc982c762010-07-09 20:39:50 +0000422 case DumpStyleInvalid:
423 return false;
424
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000425 case DumpStyleSectionNameOffset:
Greg Claytone72dfb32012-02-24 01:59:29 +0000426 if (section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000427 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000428 section_sp->DumpName(s);
Daniel Malea48184602013-04-22 20:59:13 +0000429 s->Printf (" + %" PRIu64, m_offset.load());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000430 }
431 else
432 {
Greg Claytondda4f7b2010-06-30 23:03:03 +0000433 s->Address(m_offset, addr_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000434 }
435 break;
436
437 case DumpStyleSectionPointerOffset:
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000438 s->Printf("(Section *)%p + ", static_cast<void*>(section_sp.get()));
Greg Claytondda4f7b2010-06-30 23:03:03 +0000439 s->Address(m_offset, addr_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000440 break;
441
442 case DumpStyleModuleWithFileAddress:
Greg Claytone72dfb32012-02-24 01:59:29 +0000443 if (section_sp)
Jim Ingham4af59612014-12-19 19:20:44 +0000444 {
Jim Inghamcbff63a2016-01-29 20:21:33 +0000445 ModuleSP module_sp = section_sp->GetModule();
446 if (module_sp)
447 s->Printf("%s[", module_sp->GetFileSpec().GetFilename().AsCString("<Unknown>"));
448 else
449 s->Printf("%s[","<Unknown>");
Jim Ingham4af59612014-12-19 19:20:44 +0000450 }
Jason Molenda62e06812016-02-16 04:14:33 +0000451 LLVM_FALLTHROUGH;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000452 case DumpStyleFileAddress:
453 {
454 addr_t file_addr = GetFileAddress();
455 if (file_addr == LLDB_INVALID_ADDRESS)
456 {
457 if (fallback_style != DumpStyleInvalid)
Greg Claytondda4f7b2010-06-30 23:03:03 +0000458 return Dump (s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000459 return false;
460 }
461 s->Address (file_addr, addr_size);
Greg Claytone72dfb32012-02-24 01:59:29 +0000462 if (style == DumpStyleModuleWithFileAddress && section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000463 s->PutChar(']');
464 }
465 break;
466
467 case DumpStyleLoadAddress:
468 {
Greg Claytonf5e56de2010-09-14 23:36:40 +0000469 addr_t load_addr = GetLoadAddress (target);
Jaydeep Patil44d07fc2015-09-22 06:36:56 +0000470
471 /*
472 * MIPS:
473 * Display address in compressed form for MIPS16 or microMIPS
474 * if the address belongs to eAddressClassCodeAlternateISA.
475 */
476 if (target)
477 {
478 const llvm::Triple::ArchType llvm_arch = target->GetArchitecture().GetMachine();
479 if (llvm_arch == llvm::Triple::mips || llvm_arch == llvm::Triple::mipsel
480 || llvm_arch == llvm::Triple::mips64 || llvm_arch == llvm::Triple::mips64el)
481 load_addr = GetCallableLoadAddress (target);
482 }
483
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000484 if (load_addr == LLDB_INVALID_ADDRESS)
485 {
486 if (fallback_style != DumpStyleInvalid)
Greg Claytondda4f7b2010-06-30 23:03:03 +0000487 return Dump (s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000488 return false;
489 }
490 s->Address (load_addr, addr_size);
491 }
492 break;
493
494 case DumpStyleResolvedDescription:
Greg Clayton54b8b8c2010-07-01 01:26:43 +0000495 case DumpStyleResolvedDescriptionNoModule:
Jason Molendaaff1b352014-10-10 23:07:36 +0000496 case DumpStyleResolvedDescriptionNoFunctionArguments:
Jason Molendac980fa92015-02-13 23:24:21 +0000497 case DumpStyleNoFunctionName:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000498 if (IsSectionOffset())
499 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000500 uint32_t pointer_size = 4;
Greg Claytone72dfb32012-02-24 01:59:29 +0000501 ModuleSP module_sp (GetModule());
Greg Clayton514487e2011-02-15 21:59:32 +0000502 if (target)
503 pointer_size = target->GetArchitecture().GetAddressByteSize();
Greg Claytone72dfb32012-02-24 01:59:29 +0000504 else if (module_sp)
505 pointer_size = module_sp->GetArchitecture().GetAddressByteSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000506
507 bool showed_info = false;
Greg Claytone72dfb32012-02-24 01:59:29 +0000508 if (section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000509 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000510 SectionType sect_type = section_sp->GetType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000511 switch (sect_type)
512 {
Greg Clayton89411422010-10-08 00:21:05 +0000513 case eSectionTypeData:
Greg Claytone72dfb32012-02-24 01:59:29 +0000514 if (module_sp)
Greg Clayton89411422010-10-08 00:21:05 +0000515 {
Michael Sartaina7499c92013-07-01 19:45:50 +0000516 SymbolVendor *sym_vendor = module_sp->GetSymbolVendor();
517 if (sym_vendor)
Greg Clayton89411422010-10-08 00:21:05 +0000518 {
Michael Sartaina7499c92013-07-01 19:45:50 +0000519 Symtab *symtab = sym_vendor->GetSymtab();
Greg Clayton89411422010-10-08 00:21:05 +0000520 if (symtab)
521 {
522 const addr_t file_Addr = GetFileAddress();
523 Symbol *symbol = symtab->FindSymbolContainingFileAddress (file_Addr);
524 if (symbol)
525 {
526 const char *symbol_name = symbol->GetName().AsCString();
527 if (symbol_name)
528 {
529 s->PutCString(symbol_name);
Greg Clayton358cf1e2015-06-25 21:46:34 +0000530 addr_t delta = file_Addr - symbol->GetAddressRef().GetFileAddress();
Greg Clayton89411422010-10-08 00:21:05 +0000531 if (delta)
Daniel Malead01b2952012-11-29 21:49:15 +0000532 s->Printf(" + %" PRIu64, delta);
Greg Clayton89411422010-10-08 00:21:05 +0000533 showed_info = true;
534 }
535 }
536 }
537 }
538 }
539 break;
540
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000541 case eSectionTypeDataCString:
542 // Read the C string from memory and display it
543 showed_info = true;
544 ReadCStringFromMemory (exe_scope, *this, s);
545 break;
546
547 case eSectionTypeDataCStringPointers:
548 {
549 if (ReadAddress (exe_scope, *this, pointer_size, so_addr))
550 {
551#if VERBOSE_OUTPUT
552 s->PutCString("(char *)");
553 so_addr.Dump(s, exe_scope, DumpStyleLoadAddress, DumpStyleFileAddress);
554 s->PutCString(": ");
555#endif
556 showed_info = true;
557 ReadCStringFromMemory (exe_scope, so_addr, s);
558 }
559 }
560 break;
561
562 case eSectionTypeDataObjCMessageRefs:
563 {
564 if (ReadAddress (exe_scope, *this, pointer_size, so_addr))
565 {
Greg Clayton54b8b8c2010-07-01 01:26:43 +0000566 if (target && so_addr.IsSectionOffset())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000567 {
Greg Claytonc9800662010-09-10 01:30:46 +0000568 SymbolContext func_sc;
Greg Clayton54b8b8c2010-07-01 01:26:43 +0000569 target->GetImages().ResolveSymbolContextForAddress (so_addr,
Greg Claytond9e416c2012-02-18 05:35:26 +0000570 eSymbolContextEverything,
571 func_sc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000572 if (func_sc.function || func_sc.symbol)
573 {
574 showed_info = true;
575#if VERBOSE_OUTPUT
576 s->PutCString ("(objc_msgref *) -> { (func*)");
577 so_addr.Dump(s, exe_scope, DumpStyleLoadAddress, DumpStyleFileAddress);
578#else
579 s->PutCString ("{ ");
580#endif
581 Address cstr_addr(*this);
582 cstr_addr.SetOffset(cstr_addr.GetOffset() + pointer_size);
Jason Molendac980fa92015-02-13 23:24:21 +0000583 func_sc.DumpStopContext(s, exe_scope, so_addr, true, true, false, true, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000584 if (ReadAddress (exe_scope, cstr_addr, pointer_size, so_addr))
585 {
586#if VERBOSE_OUTPUT
587 s->PutCString("), (char *)");
588 so_addr.Dump(s, exe_scope, DumpStyleLoadAddress, DumpStyleFileAddress);
589 s->PutCString(" (");
590#else
591 s->PutCString(", ");
592#endif
593 ReadCStringFromMemory (exe_scope, so_addr, s);
594 }
595#if VERBOSE_OUTPUT
596 s->PutCString(") }");
597#else
598 s->PutCString(" }");
599#endif
600 }
601 }
602 }
603 }
604 break;
605
606 case eSectionTypeDataObjCCFStrings:
607 {
608 Address cfstring_data_addr(*this);
609 cfstring_data_addr.SetOffset(cfstring_data_addr.GetOffset() + (2 * pointer_size));
610 if (ReadAddress (exe_scope, cfstring_data_addr, pointer_size, so_addr))
611 {
612#if VERBOSE_OUTPUT
613 s->PutCString("(CFString *) ");
614 cfstring_data_addr.Dump(s, exe_scope, DumpStyleLoadAddress, DumpStyleFileAddress);
615 s->PutCString(" -> @");
616#else
617 s->PutChar('@');
618#endif
619 if (so_addr.Dump(s, exe_scope, DumpStyleResolvedDescription))
620 showed_info = true;
621 }
622 }
623 break;
624
625 case eSectionTypeData4:
626 // Read the 4 byte data and display it
627 showed_info = true;
628 s->PutCString("(uint32_t) ");
629 DumpUInt (exe_scope, *this, 4, s);
630 break;
631
632 case eSectionTypeData8:
633 // Read the 8 byte data and display it
634 showed_info = true;
635 s->PutCString("(uint64_t) ");
636 DumpUInt (exe_scope, *this, 8, s);
637 break;
638
639 case eSectionTypeData16:
640 // Read the 16 byte data and display it
641 showed_info = true;
642 s->PutCString("(uint128_t) ");
643 DumpUInt (exe_scope, *this, 16, s);
644 break;
645
646 case eSectionTypeDataPointers:
647 // Read the pointer data and display it
648 {
649 if (ReadAddress (exe_scope, *this, pointer_size, so_addr))
650 {
651 s->PutCString ("(void *)");
652 so_addr.Dump(s, exe_scope, DumpStyleLoadAddress, DumpStyleFileAddress);
653
654 showed_info = true;
655 if (so_addr.IsSectionOffset())
656 {
Greg Claytonc9800662010-09-10 01:30:46 +0000657 SymbolContext pointer_sc;
Greg Claytondda4f7b2010-06-30 23:03:03 +0000658 if (target)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000659 {
Greg Claytondda4f7b2010-06-30 23:03:03 +0000660 target->GetImages().ResolveSymbolContextForAddress (so_addr,
Greg Claytond9e416c2012-02-18 05:35:26 +0000661 eSymbolContextEverything,
662 pointer_sc);
Greg Claytondda4f7b2010-06-30 23:03:03 +0000663 if (pointer_sc.function || pointer_sc.symbol)
664 {
665 s->PutCString(": ");
Jason Molendac980fa92015-02-13 23:24:21 +0000666 pointer_sc.DumpStopContext(s, exe_scope, so_addr, true, false, false, true, true);
Greg Claytondda4f7b2010-06-30 23:03:03 +0000667 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000668 }
669 }
670 }
671 }
672 break;
Greg Claytonc982c762010-07-09 20:39:50 +0000673
674 default:
675 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000676 }
677 }
678
679 if (!showed_info)
680 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000681 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000682 {
Greg Claytonc9800662010-09-10 01:30:46 +0000683 SymbolContext sc;
Greg Clayton2501e5e2015-01-15 02:59:20 +0000684 module_sp->ResolveSymbolContextForAddress(*this, eSymbolContextEverything | eSymbolContextVariable, sc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000685 if (sc.function || sc.symbol)
686 {
687 bool show_stop_context = true;
Greg Clayton8dc0a982010-09-07 21:56:53 +0000688 const bool show_module = (style == DumpStyleResolvedDescription);
689 const bool show_fullpaths = false;
Greg Clayton513c26c2011-01-29 07:10:55 +0000690 const bool show_inlined_frames = true;
Jason Molendaaff1b352014-10-10 23:07:36 +0000691 const bool show_function_arguments = (style != DumpStyleResolvedDescriptionNoFunctionArguments);
Jason Molendac980fa92015-02-13 23:24:21 +0000692 const bool show_function_name = (style != DumpStyleNoFunctionName);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000693 if (sc.function == NULL && sc.symbol != NULL)
694 {
695 // If we have just a symbol make sure it is in the right section
Greg Claytone7612132012-03-07 21:03:09 +0000696 if (sc.symbol->ValueIsAddress())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000697 {
Greg Clayton358cf1e2015-06-25 21:46:34 +0000698 if (sc.symbol->GetAddressRef().GetSection() != GetSection())
Greg Clayton54b8b8c2010-07-01 01:26:43 +0000699 {
700 // don't show the module if the symbol is a trampoline symbol
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000701 show_stop_context = false;
Greg Clayton54b8b8c2010-07-01 01:26:43 +0000702 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000703 }
704 }
705 if (show_stop_context)
706 {
707 // We have a function or a symbol from the same
708 // sections as this address.
Greg Clayton8dc0a982010-09-07 21:56:53 +0000709 sc.DumpStopContext (s,
710 exe_scope,
711 *this,
712 show_fullpaths,
713 show_module,
Jason Molendaaff1b352014-10-10 23:07:36 +0000714 show_inlined_frames,
Jason Molendac980fa92015-02-13 23:24:21 +0000715 show_function_arguments,
716 show_function_name);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000717 }
718 else
719 {
720 // We found a symbol but it was in a different
721 // section so it isn't the symbol we should be
722 // showing, just show the section name + offset
723 Dump (s, exe_scope, DumpStyleSectionNameOffset);
724 }
725 }
726 }
727 }
728 }
729 else
730 {
731 if (fallback_style != DumpStyleInvalid)
Greg Claytondda4f7b2010-06-30 23:03:03 +0000732 return Dump (s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000733 return false;
734 }
735 break;
Greg Claytondda4f7b2010-06-30 23:03:03 +0000736
737 case DumpStyleDetailedSymbolContext:
738 if (IsSectionOffset())
739 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000740 ModuleSP module_sp (GetModule());
741 if (module_sp)
Greg Claytondda4f7b2010-06-30 23:03:03 +0000742 {
Greg Claytonc9800662010-09-10 01:30:46 +0000743 SymbolContext sc;
Greg Clayton2501e5e2015-01-15 02:59:20 +0000744 module_sp->ResolveSymbolContextForAddress(*this, eSymbolContextEverything | eSymbolContextVariable, sc);
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000745 if (sc.symbol)
Greg Claytondda4f7b2010-06-30 23:03:03 +0000746 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000747 // If we have just a symbol make sure it is in the same section
748 // as our address. If it isn't, then we might have just found
749 // the last symbol that came before the address that we are
750 // looking up that has nothing to do with our address lookup.
Greg Clayton358cf1e2015-06-25 21:46:34 +0000751 if (sc.symbol->ValueIsAddress() && sc.symbol->GetAddressRef().GetSection() != GetSection())
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000752 sc.symbol = NULL;
Greg Claytondda4f7b2010-06-30 23:03:03 +0000753 }
Greg Claytonf5e56de2010-09-14 23:36:40 +0000754 sc.GetDescription(s, eDescriptionLevelBrief, target);
Greg Claytonc749eb82011-07-11 05:12:02 +0000755
756 if (sc.block)
757 {
758 bool can_create = true;
759 bool get_parent_variables = true;
760 bool stop_if_block_is_inlined_function = false;
761 VariableList variable_list;
762 sc.block->AppendVariables (can_create,
Tamas Berghammer72ac8a82016-02-25 12:23:37 +0000763 get_parent_variables,
764 stop_if_block_is_inlined_function,
765 [](Variable*) { return true; },
Greg Claytonc749eb82011-07-11 05:12:02 +0000766 &variable_list);
Tamas Berghammer72ac8a82016-02-25 12:23:37 +0000767
Greg Claytonc7bece562013-01-25 18:06:21 +0000768 const size_t num_variables = variable_list.GetSize();
769 for (size_t var_idx = 0; var_idx < num_variables; ++var_idx)
Greg Claytonc749eb82011-07-11 05:12:02 +0000770 {
771 Variable *var = variable_list.GetVariableAtIndex (var_idx).get();
772 if (var && var->LocationIsValidForAddress (*this))
773 {
Greg Claytonc4a8a762012-05-15 18:43:44 +0000774 s->Indent();
Ilia K6e465122015-04-28 12:45:57 +0000775 s->Printf (" Variable: id = {0x%8.8" PRIx64 "}, name = \"%s\"",
Greg Claytonc749eb82011-07-11 05:12:02 +0000776 var->GetID(),
Ilia K6e465122015-04-28 12:45:57 +0000777 var->GetName().GetCString());
778 Type *type = var->GetType();
779 if (type)
780 s->Printf(", type = \"%s\"", type->GetName().GetCString());
781 else
782 s->PutCString(", type = <unknown>");
783 s->PutCString(", location = ");
Greg Claytonc749eb82011-07-11 05:12:02 +0000784 var->DumpLocationForAddress(s, *this);
785 s->PutCString(", decl = ");
786 var->GetDeclaration().DumpStopContext(s, false);
787 s->EOL();
788 }
789 }
790 }
Greg Claytondda4f7b2010-06-30 23:03:03 +0000791 }
792 }
Greg Claytonc749eb82011-07-11 05:12:02 +0000793 else
794 {
795 if (fallback_style != DumpStyleInvalid)
796 return Dump (s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
797 return false;
798 }
Greg Claytondda4f7b2010-06-30 23:03:03 +0000799 break;
Greg Claytonc3a86bf2012-07-11 22:18:24 +0000800 case DumpStyleResolvedPointerDescription:
801 {
802 Process *process = exe_ctx.GetProcessPtr();
803 if (process)
804 {
805 addr_t load_addr = GetLoadAddress (target);
806 if (load_addr != LLDB_INVALID_ADDRESS)
807 {
808 Error memory_error;
809 addr_t dereferenced_load_addr = process->ReadPointerFromMemory(load_addr, memory_error);
810 if (dereferenced_load_addr != LLDB_INVALID_ADDRESS)
811 {
812 Address dereferenced_addr;
813 if (dereferenced_addr.SetLoadAddress(dereferenced_load_addr, target))
814 {
815 StreamString strm;
816 if (dereferenced_addr.Dump (&strm, exe_scope, DumpStyleResolvedDescription, DumpStyleInvalid, addr_size))
817 {
818 s->Address (dereferenced_load_addr, addr_size, " -> ", " ");
819 s->Write(strm.GetData(), strm.GetSize());
820 return true;
821 }
822 }
823 }
824 }
825 }
826 if (fallback_style != DumpStyleInvalid)
827 return Dump (s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
828 return false;
829 }
830 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000831 }
832
833 return true;
834}
835
Greg Claytoncae56522013-11-06 02:29:13 +0000836bool
837Address::SectionWasDeleted() const
838{
Greg Claytonb35db632013-11-09 00:03:31 +0000839 if (GetSection())
840 return false;
841 return SectionWasDeletedPrivate();
842}
843
844bool
845Address::SectionWasDeletedPrivate() const
846{
Greg Claytoncae56522013-11-06 02:29:13 +0000847 lldb::SectionWP empty_section_wp;
848
849 // If either call to "std::weak_ptr::owner_before(...) value returns true, this
850 // indicates that m_section_wp once contained (possibly still does) a reference
851 // to a valid shared pointer. This helps us know if we had a valid reference to
852 // a section which is now invalid because the module it was in was unloaded/deleted,
853 // or if the address doesn't have a valid reference to a section.
854 return empty_section_wp.owner_before(m_section_wp) || m_section_wp.owner_before(empty_section_wp);
855}
856
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000857uint32_t
Greg Clayton6f6bf262011-12-10 21:05:26 +0000858Address::CalculateSymbolContext (SymbolContext *sc, uint32_t resolve_scope) const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000859{
Greg Clayton72310352013-02-23 04:12:47 +0000860 sc->Clear(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000861 // Absolute addresses don't have enough information to reconstruct even their target.
Greg Claytone72dfb32012-02-24 01:59:29 +0000862
863 SectionSP section_sp (GetSection());
864 if (section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000865 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000866 ModuleSP module_sp (section_sp->GetModule());
867 if (module_sp)
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000868 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000869 sc->module_sp = module_sp;
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000870 if (sc->module_sp)
871 return sc->module_sp->ResolveSymbolContextForAddress (*this, resolve_scope, *sc);
872 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000873 }
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000874 return 0;
875}
876
Greg Claytone72dfb32012-02-24 01:59:29 +0000877ModuleSP
Greg Clayton6f6bf262011-12-10 21:05:26 +0000878Address::CalculateSymbolContextModule () const
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000879{
Greg Claytone72dfb32012-02-24 01:59:29 +0000880 SectionSP section_sp (GetSection());
881 if (section_sp)
882 return section_sp->GetModule();
883 return ModuleSP();
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000884}
885
886CompileUnit *
Greg Clayton6f6bf262011-12-10 21:05:26 +0000887Address::CalculateSymbolContextCompileUnit () const
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000888{
Greg Claytone72dfb32012-02-24 01:59:29 +0000889 SectionSP section_sp (GetSection());
890 if (section_sp)
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000891 {
892 SymbolContext sc;
Greg Claytone72dfb32012-02-24 01:59:29 +0000893 sc.module_sp = section_sp->GetModule();
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000894 if (sc.module_sp)
895 {
896 sc.module_sp->ResolveSymbolContextForAddress (*this, eSymbolContextCompUnit, sc);
897 return sc.comp_unit;
898 }
899 }
900 return NULL;
901}
902
903Function *
Greg Clayton6f6bf262011-12-10 21:05:26 +0000904Address::CalculateSymbolContextFunction () const
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000905{
Greg Claytone72dfb32012-02-24 01:59:29 +0000906 SectionSP section_sp (GetSection());
907 if (section_sp)
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000908 {
909 SymbolContext sc;
Greg Claytone72dfb32012-02-24 01:59:29 +0000910 sc.module_sp = section_sp->GetModule();
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000911 if (sc.module_sp)
912 {
913 sc.module_sp->ResolveSymbolContextForAddress (*this, eSymbolContextFunction, sc);
914 return sc.function;
915 }
916 }
917 return NULL;
918}
919
920Block *
Greg Clayton6f6bf262011-12-10 21:05:26 +0000921Address::CalculateSymbolContextBlock () const
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000922{
Greg Claytone72dfb32012-02-24 01:59:29 +0000923 SectionSP section_sp (GetSection());
924 if (section_sp)
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000925 {
926 SymbolContext sc;
Greg Claytone72dfb32012-02-24 01:59:29 +0000927 sc.module_sp = section_sp->GetModule();
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000928 if (sc.module_sp)
929 {
930 sc.module_sp->ResolveSymbolContextForAddress (*this, eSymbolContextBlock, sc);
931 return sc.block;
932 }
933 }
934 return NULL;
935}
936
937Symbol *
Greg Clayton6f6bf262011-12-10 21:05:26 +0000938Address::CalculateSymbolContextSymbol () const
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000939{
Greg Claytone72dfb32012-02-24 01:59:29 +0000940 SectionSP section_sp (GetSection());
941 if (section_sp)
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000942 {
943 SymbolContext sc;
Greg Claytone72dfb32012-02-24 01:59:29 +0000944 sc.module_sp = section_sp->GetModule();
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000945 if (sc.module_sp)
946 {
947 sc.module_sp->ResolveSymbolContextForAddress (*this, eSymbolContextSymbol, sc);
948 return sc.symbol;
949 }
950 }
951 return NULL;
952}
953
954bool
Greg Clayton6f6bf262011-12-10 21:05:26 +0000955Address::CalculateSymbolContextLineEntry (LineEntry &line_entry) const
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000956{
Greg Claytone72dfb32012-02-24 01:59:29 +0000957 SectionSP section_sp (GetSection());
958 if (section_sp)
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000959 {
960 SymbolContext sc;
Greg Claytone72dfb32012-02-24 01:59:29 +0000961 sc.module_sp = section_sp->GetModule();
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000962 if (sc.module_sp)
963 {
964 sc.module_sp->ResolveSymbolContextForAddress (*this, eSymbolContextLineEntry, sc);
965 if (sc.line_entry.IsValid())
966 {
967 line_entry = sc.line_entry;
968 return true;
969 }
970 }
971 }
972 line_entry.Clear();
973 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000974}
975
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000976int
977Address::CompareFileAddress (const Address& a, const Address& b)
978{
979 addr_t a_file_addr = a.GetFileAddress();
980 addr_t b_file_addr = b.GetFileAddress();
981 if (a_file_addr < b_file_addr)
982 return -1;
983 if (a_file_addr > b_file_addr)
984 return +1;
985 return 0;
986}
987
988
989int
Greg Claytonf5e56de2010-09-14 23:36:40 +0000990Address::CompareLoadAddress (const Address& a, const Address& b, Target *target)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000991{
Greg Claytonf5e56de2010-09-14 23:36:40 +0000992 assert (target != NULL);
993 addr_t a_load_addr = a.GetLoadAddress (target);
994 addr_t b_load_addr = b.GetLoadAddress (target);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000995 if (a_load_addr < b_load_addr)
996 return -1;
997 if (a_load_addr > b_load_addr)
998 return +1;
999 return 0;
1000}
1001
1002int
1003Address::CompareModulePointerAndOffset (const Address& a, const Address& b)
1004{
Greg Claytone72dfb32012-02-24 01:59:29 +00001005 ModuleSP a_module_sp (a.GetModule());
1006 ModuleSP b_module_sp (b.GetModule());
1007 Module *a_module = a_module_sp.get();
1008 Module *b_module = b_module_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001009 if (a_module < b_module)
1010 return -1;
1011 if (a_module > b_module)
1012 return +1;
1013 // Modules are the same, just compare the file address since they should
1014 // be unique
1015 addr_t a_file_addr = a.GetFileAddress();
1016 addr_t b_file_addr = b.GetFileAddress();
1017 if (a_file_addr < b_file_addr)
1018 return -1;
1019 if (a_file_addr > b_file_addr)
1020 return +1;
1021 return 0;
1022}
1023
1024
1025size_t
1026Address::MemorySize () const
1027{
1028 // Noting special for the memory size of a single Address object,
1029 // it is just the size of itself.
1030 return sizeof(Address);
1031}
1032
1033
Greg Claytonb0848c52011-01-08 00:05:12 +00001034//----------------------------------------------------------------------
1035// NOTE: Be careful using this operator. It can correctly compare two
1036// addresses from the same Module correctly. It can't compare two
1037// addresses from different modules in any meaningful way, but it will
1038// compare the module pointers.
1039//
1040// To sum things up:
1041// - works great for addresses within the same module
1042// - it works for addresses across multiple modules, but don't expect the
1043// address results to make much sense
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001044//
Greg Claytonb0848c52011-01-08 00:05:12 +00001045// This basically lets Address objects be used in ordered collection
1046// classes.
1047//----------------------------------------------------------------------
1048
1049bool
1050lldb_private::operator< (const Address& lhs, const Address& rhs)
1051{
Greg Claytone72dfb32012-02-24 01:59:29 +00001052 ModuleSP lhs_module_sp (lhs.GetModule());
1053 ModuleSP rhs_module_sp (rhs.GetModule());
1054 Module *lhs_module = lhs_module_sp.get();
1055 Module *rhs_module = rhs_module_sp.get();
Greg Claytonb0848c52011-01-08 00:05:12 +00001056 if (lhs_module == rhs_module)
1057 {
1058 // Addresses are in the same module, just compare the file addresses
1059 return lhs.GetFileAddress() < rhs.GetFileAddress();
1060 }
1061 else
1062 {
1063 // The addresses are from different modules, just use the module
1064 // pointer value to get consistent ordering
1065 return lhs_module < rhs_module;
1066 }
1067}
1068
1069bool
1070lldb_private::operator> (const Address& lhs, const Address& rhs)
1071{
Greg Claytone72dfb32012-02-24 01:59:29 +00001072 ModuleSP lhs_module_sp (lhs.GetModule());
1073 ModuleSP rhs_module_sp (rhs.GetModule());
1074 Module *lhs_module = lhs_module_sp.get();
1075 Module *rhs_module = rhs_module_sp.get();
Greg Claytonb0848c52011-01-08 00:05:12 +00001076 if (lhs_module == rhs_module)
1077 {
1078 // Addresses are in the same module, just compare the file addresses
1079 return lhs.GetFileAddress() > rhs.GetFileAddress();
1080 }
1081 else
1082 {
1083 // The addresses are from different modules, just use the module
1084 // pointer value to get consistent ordering
1085 return lhs_module > rhs_module;
1086 }
1087}
1088
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001089
1090// The operator == checks for exact equality only (same section, same offset)
1091bool
1092lldb_private::operator== (const Address& a, const Address& rhs)
1093{
Jim Inghamd3480f52013-06-06 22:16:56 +00001094 return a.GetOffset() == rhs.GetOffset() &&
1095 a.GetSection() == rhs.GetSection();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001096}
1097// The operator != checks for exact inequality only (differing section, or
1098// different offset)
1099bool
1100lldb_private::operator!= (const Address& a, const Address& rhs)
1101{
Jim Inghamd3480f52013-06-06 22:16:56 +00001102 return a.GetOffset() != rhs.GetOffset() ||
1103 a.GetSection() != rhs.GetSection();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001104}
1105
Greg Claytone0d378b2011-03-24 21:19:54 +00001106AddressClass
Greg Claytonded470d2011-03-19 01:12:21 +00001107Address::GetAddressClass () const
1108{
Greg Claytone72dfb32012-02-24 01:59:29 +00001109 ModuleSP module_sp (GetModule());
1110 if (module_sp)
Greg Claytonded470d2011-03-19 01:12:21 +00001111 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001112 ObjectFile *obj_file = module_sp->GetObjectFile();
Greg Claytonded470d2011-03-19 01:12:21 +00001113 if (obj_file)
Michael Sartaina7499c92013-07-01 19:45:50 +00001114 {
1115 // Give the symbol vendor a chance to add to the unified section list.
1116 module_sp->GetSymbolVendor();
Greg Claytonded470d2011-03-19 01:12:21 +00001117 return obj_file->GetAddressClass (GetFileAddress());
Michael Sartaina7499c92013-07-01 19:45:50 +00001118 }
Greg Claytonded470d2011-03-19 01:12:21 +00001119 }
1120 return eAddressClassUnknown;
1121}
Greg Claytoncd482e32011-05-18 01:58:14 +00001122
1123bool
1124Address::SetLoadAddress (lldb::addr_t load_addr, Target *target)
1125{
1126 if (target && target->GetSectionLoadList().ResolveLoadAddress(load_addr, *this))
1127 return true;
Greg Claytone72dfb32012-02-24 01:59:29 +00001128 m_section_wp.reset();
Greg Claytoncd482e32011-05-18 01:58:14 +00001129 m_offset = load_addr;
1130 return false;
1131}
1132