blob: 1fd9119a9835a5572a2a33b653d08444b94630e9 [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"
Eugene Zelenko896ddd02016-03-02 01:09:03 +000011
12// C Includes
13// C++ Includes
14#include "llvm/ADT/Triple.h"
15
16// Other libraries and framework includes
17// Project includes
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018#include "lldb/Core/Module.h"
19#include "lldb/Core/Section.h"
Greg Clayton1f746072012-08-29 21:13:06 +000020#include "lldb/Symbol/Block.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021#include "lldb/Symbol/ObjectFile.h"
Greg Claytonc749eb82011-07-11 05:12:02 +000022#include "lldb/Symbol/Variable.h"
23#include "lldb/Symbol/VariableList.h"
Greg Claytonf5e56de2010-09-14 23:36:40 +000024#include "lldb/Target/ExecutionContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025#include "lldb/Target/Process.h"
Greg Claytond5944cd2013-12-06 01:12:00 +000026#include "lldb/Target/SectionLoadList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027#include "lldb/Target/Target.h"
Michael Sartaina7499c92013-07-01 19:45:50 +000028#include "lldb/Symbol/SymbolVendor.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029
30using namespace lldb;
31using namespace lldb_private;
32
33static size_t
34ReadBytes (ExecutionContextScope *exe_scope, const Address &address, void *dst, size_t dst_len)
35{
Eugene Zelenko896ddd02016-03-02 01:09:03 +000036 if (exe_scope == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000037 return 0;
38
Greg Claytond9e416c2012-02-18 05:35:26 +000039 TargetSP target_sp (exe_scope->CalculateTarget());
40 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000041 {
42 Error error;
Greg Claytondb598232011-01-07 01:57:07 +000043 bool prefer_file_cache = false;
Greg Claytond9e416c2012-02-18 05:35:26 +000044 return target_sp->ReadMemory (address, prefer_file_cache, dst, dst_len, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000045 }
46 return 0;
47}
48
49static bool
50GetByteOrderAndAddressSize (ExecutionContextScope *exe_scope, const Address &address, ByteOrder& byte_order, uint32_t& addr_size)
51{
52 byte_order = eByteOrderInvalid;
53 addr_size = 0;
Eugene Zelenko896ddd02016-03-02 01:09:03 +000054 if (exe_scope == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000055 return false;
56
Greg Claytond9e416c2012-02-18 05:35:26 +000057 TargetSP target_sp (exe_scope->CalculateTarget());
58 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000059 {
Greg Claytond9e416c2012-02-18 05:35:26 +000060 byte_order = target_sp->GetArchitecture().GetByteOrder();
61 addr_size = target_sp->GetArchitecture().GetAddressByteSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000062 }
63
64 if (byte_order == eByteOrderInvalid || addr_size == 0)
65 {
Greg Claytone72dfb32012-02-24 01:59:29 +000066 ModuleSP module_sp (address.GetModule());
67 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000068 {
Greg Claytone72dfb32012-02-24 01:59:29 +000069 byte_order = module_sp->GetArchitecture().GetByteOrder();
70 addr_size = module_sp->GetArchitecture().GetAddressByteSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000071 }
72 }
73 return byte_order != eByteOrderInvalid && addr_size != 0;
74}
75
76static uint64_t
77ReadUIntMax64 (ExecutionContextScope *exe_scope, const Address &address, uint32_t byte_size, bool &success)
78{
79 uint64_t uval64 = 0;
Eugene Zelenko896ddd02016-03-02 01:09:03 +000080 if (exe_scope == nullptr || byte_size > sizeof(uint64_t))
Chris Lattner30fdc8d2010-06-08 16:52:24 +000081 {
82 success = false;
83 return 0;
84 }
Johnny Chen40e35922011-08-25 17:40:39 +000085 uint64_t buf = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000086
87 success = ReadBytes (exe_scope, address, &buf, byte_size) == byte_size;
88 if (success)
89 {
90 ByteOrder byte_order = eByteOrderInvalid;
91 uint32_t addr_size = 0;
92 if (GetByteOrderAndAddressSize (exe_scope, address, byte_order, addr_size))
93 {
94 DataExtractor data (&buf, sizeof(buf), byte_order, addr_size);
Greg Claytonc7bece562013-01-25 18:06:21 +000095 lldb::offset_t offset = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000096 uval64 = data.GetU64(&offset);
97 }
98 else
99 success = false;
100 }
101 return uval64;
102}
103
104static bool
105ReadAddress (ExecutionContextScope *exe_scope, const Address &address, uint32_t pointer_size, Address &deref_so_addr)
106{
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000107 if (exe_scope == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000108 return false;
109
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000110 bool success = false;
111 addr_t deref_addr = ReadUIntMax64 (exe_scope, address, pointer_size, success);
112 if (success)
113 {
Greg Claytonf5e56de2010-09-14 23:36:40 +0000114 ExecutionContext exe_ctx;
Greg Clayton0603aa92010-10-04 01:05:56 +0000115 exe_scope->CalculateExecutionContext(exe_ctx);
Greg Claytonf5e56de2010-09-14 23:36:40 +0000116 // If we have any sections that are loaded, try and resolve using the
117 // section load list
Greg Claytonc14ee322011-09-22 04:58:26 +0000118 Target *target = exe_ctx.GetTargetPtr();
119 if (target && !target->GetSectionLoadList().IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000120 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000121 if (target->GetSectionLoadList().ResolveLoadAddress (deref_addr, deref_so_addr))
Greg Claytonf5e56de2010-09-14 23:36:40 +0000122 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000123 }
124 else
125 {
Greg Claytonf5e56de2010-09-14 23:36:40 +0000126 // If we were not running, yet able to read an integer, we must
127 // have a module
Greg Claytone72dfb32012-02-24 01:59:29 +0000128 ModuleSP module_sp (address.GetModule());
129
130 assert (module_sp);
131 if (module_sp->ResolveFileAddress(deref_addr, deref_so_addr))
Greg Claytonf5e56de2010-09-14 23:36:40 +0000132 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000133 }
Greg Claytonf5e56de2010-09-14 23:36:40 +0000134
135 // We couldn't make "deref_addr" into a section offset value, but we were
136 // able to read the address, so we return a section offset address with
137 // no section and "deref_addr" as the offset (address).
Greg Claytone72dfb32012-02-24 01:59:29 +0000138 deref_so_addr.SetRawAddress(deref_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000139 return true;
140 }
141 return false;
142}
143
144static bool
145DumpUInt (ExecutionContextScope *exe_scope, const Address &address, uint32_t byte_size, Stream* strm)
146{
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000147 if (exe_scope == nullptr || byte_size == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000148 return 0;
149 std::vector<uint8_t> buf(byte_size, 0);
150
151 if (ReadBytes (exe_scope, address, &buf[0], buf.size()) == buf.size())
152 {
153 ByteOrder byte_order = eByteOrderInvalid;
154 uint32_t addr_size = 0;
155 if (GetByteOrderAndAddressSize (exe_scope, address, byte_order, addr_size))
156 {
Greg Clayton471b31c2010-07-20 22:52:08 +0000157 DataExtractor data (&buf.front(), buf.size(), byte_order, addr_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000158
159 data.Dump (strm,
160 0, // Start offset in "data"
161 eFormatHex, // Print as characters
162 buf.size(), // Size of item
163 1, // Items count
164 UINT32_MAX, // num per line
165 LLDB_INVALID_ADDRESS,// base address
166 0, // bitfield bit size
167 0); // bitfield bit offset
168
169 return true;
170 }
171 }
172 return false;
173}
174
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000175static size_t
176ReadCStringFromMemory (ExecutionContextScope *exe_scope, const Address &address, Stream *strm)
177{
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000178 if (exe_scope == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000179 return 0;
180 const size_t k_buf_len = 256;
181 char buf[k_buf_len+1];
182 buf[k_buf_len] = '\0'; // NULL terminate
183
Greg Clayton710dd5a2011-01-08 20:28:42 +0000184 // Byte order and address size don't matter for C string dumping..
Bruce Mitchener9ccb9702015-11-07 04:40:13 +0000185 DataExtractor data (buf, sizeof(buf), endian::InlHostByteOrder(), 4);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000186 size_t total_len = 0;
187 size_t bytes_read;
188 Address curr_address(address);
189 strm->PutChar ('"');
190 while ((bytes_read = ReadBytes (exe_scope, curr_address, buf, k_buf_len)) > 0)
191 {
192 size_t len = strlen(buf);
193 if (len == 0)
194 break;
195 if (len > bytes_read)
196 len = bytes_read;
197
198 data.Dump (strm,
199 0, // Start offset in "data"
200 eFormatChar, // Print as characters
201 1, // Size of item (1 byte for a char!)
202 len, // How many bytes to print?
203 UINT32_MAX, // num per line
204 LLDB_INVALID_ADDRESS,// base address
205 0, // bitfield bit size
206
207 0); // bitfield bit offset
208
209 total_len += bytes_read;
210
211 if (len < k_buf_len)
212 break;
213 curr_address.SetOffset (curr_address.GetOffset() + bytes_read);
214 }
215 strm->PutChar ('"');
216 return total_len;
217}
218
Greg Claytone72dfb32012-02-24 01:59:29 +0000219Address::Address (lldb::addr_t abs_addr) :
220 m_section_wp (),
221 m_offset (abs_addr)
222{
223}
224
225Address::Address (addr_t address, const SectionList *section_list) :
226 m_section_wp (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000227 m_offset (LLDB_INVALID_ADDRESS)
228{
Greg Claytone72dfb32012-02-24 01:59:29 +0000229 ResolveAddressUsingFileSections(address, section_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000230}
231
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000232const Address&
233Address::operator= (const Address& rhs)
234{
235 if (this != &rhs)
236 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000237 m_section_wp = rhs.m_section_wp;
Daniel Malea48184602013-04-22 20:59:13 +0000238 m_offset = rhs.m_offset.load();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000239 }
240 return *this;
241}
242
243bool
Greg Claytone72dfb32012-02-24 01:59:29 +0000244Address::ResolveAddressUsingFileSections (addr_t file_addr, const SectionList *section_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000245{
Greg Claytone72dfb32012-02-24 01:59:29 +0000246 if (section_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000247 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000248 SectionSP section_sp (section_list->FindSectionContainingFileAddress(file_addr));
249 m_section_wp = section_sp;
250 if (section_sp)
251 {
252 assert( section_sp->ContainsFileAddress(file_addr) );
253 m_offset = file_addr - section_sp->GetFileAddress();
254 return true; // Successfully transformed addr into a section offset address
255 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000256 }
Greg Claytone72dfb32012-02-24 01:59:29 +0000257 m_offset = file_addr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000258 return false; // Failed to resolve this address to a section offset value
259}
260
Greg Claytone1cd1be2012-01-29 20:56:30 +0000261ModuleSP
Greg Claytone72dfb32012-02-24 01:59:29 +0000262Address::GetModule () const
Greg Claytone1cd1be2012-01-29 20:56:30 +0000263{
264 lldb::ModuleSP module_sp;
Greg Claytone72dfb32012-02-24 01:59:29 +0000265 SectionSP section_sp (GetSection());
266 if (section_sp)
267 module_sp = section_sp->GetModule();
Greg Claytone1cd1be2012-01-29 20:56:30 +0000268 return module_sp;
269}
270
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000271addr_t
272Address::GetFileAddress () const
273{
Greg Claytone72dfb32012-02-24 01:59:29 +0000274 SectionSP section_sp (GetSection());
275 if (section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000276 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000277 addr_t sect_file_addr = section_sp->GetFileAddress();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000278 if (sect_file_addr == LLDB_INVALID_ADDRESS)
279 {
280 // Section isn't resolved, we can't return a valid file address
281 return LLDB_INVALID_ADDRESS;
282 }
283 // We have a valid file range, so we can return the file based
284 // address by adding the file base address to our offset
285 return sect_file_addr + m_offset;
286 }
Greg Claytonb35db632013-11-09 00:03:31 +0000287 else if (SectionWasDeletedPrivate())
Greg Claytoncae56522013-11-06 02:29:13 +0000288 {
289 // Used to have a valid section but it got deleted so the
290 // offset doesn't mean anything without the section
291 return LLDB_INVALID_ADDRESS;
292 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000293 // No section, we just return the offset since it is the value in this case
294 return m_offset;
295}
296
297addr_t
Greg Claytonf5e56de2010-09-14 23:36:40 +0000298Address::GetLoadAddress (Target *target) const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000299{
Greg Claytone72dfb32012-02-24 01:59:29 +0000300 SectionSP section_sp (GetSection());
Greg Claytoncae56522013-11-06 02:29:13 +0000301 if (section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000302 {
Greg Claytoncae56522013-11-06 02:29:13 +0000303 if (target)
Greg Claytonf5e56de2010-09-14 23:36:40 +0000304 {
Greg Claytoncae56522013-11-06 02:29:13 +0000305 addr_t sect_load_addr = section_sp->GetLoadBaseAddress (target);
306
307 if (sect_load_addr != LLDB_INVALID_ADDRESS)
308 {
309 // We have a valid file range, so we can return the file based
310 // address by adding the file base address to our offset
311 return sect_load_addr + m_offset;
312 }
Greg Claytonf5e56de2010-09-14 23:36:40 +0000313 }
314 }
Greg Claytonb35db632013-11-09 00:03:31 +0000315 else if (SectionWasDeletedPrivate())
Greg Claytoncae56522013-11-06 02:29:13 +0000316 {
317 // Used to have a valid section but it got deleted so the
318 // offset doesn't mean anything without the section
319 return LLDB_INVALID_ADDRESS;
320 }
321 else
322 {
323 // We don't have a section so the offset is the load address
324 return m_offset;
325 }
326 // The section isn't resolved or an invalid target was passed in
327 // so we can't return a valid load address.
Greg Claytonf5e56de2010-09-14 23:36:40 +0000328 return LLDB_INVALID_ADDRESS;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000329}
330
Greg Clayton3f5c08f2011-05-18 22:01:49 +0000331addr_t
Matt Kopec00049b82013-02-27 20:13:38 +0000332Address::GetCallableLoadAddress (Target *target, bool is_indirect) const
Greg Clayton3f5c08f2011-05-18 22:01:49 +0000333{
Jim Ingham1460e4b2014-01-10 23:46:59 +0000334 addr_t code_addr = LLDB_INVALID_ADDRESS;
335
336 if (is_indirect && target)
337 {
Matt Kopec00049b82013-02-27 20:13:38 +0000338 ProcessSP processSP = target->GetProcessSP();
339 Error error;
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000340 if (processSP)
Jim Ingham1460e4b2014-01-10 23:46:59 +0000341 {
342 code_addr = processSP->ResolveIndirectFunction(this, error);
343 if (!error.Success())
344 code_addr = LLDB_INVALID_ADDRESS;
345 }
Matt Kopec00049b82013-02-27 20:13:38 +0000346 }
Jim Ingham1460e4b2014-01-10 23:46:59 +0000347 else
348 {
349 code_addr = GetLoadAddress (target);
350 }
351
352 if (code_addr == LLDB_INVALID_ADDRESS)
353 return code_addr;
354
Greg Claytonf3ef3d22011-05-22 22:46:53 +0000355 if (target)
356 return target->GetCallableLoadAddress (code_addr, GetAddressClass());
Greg Clayton3f5c08f2011-05-18 22:01:49 +0000357 return code_addr;
358}
359
Greg Claytoncff851a2011-05-22 04:32:55 +0000360bool
361Address::SetCallableLoadAddress (lldb::addr_t load_addr, Target *target)
362{
363 if (SetLoadAddress (load_addr, target))
364 {
Greg Claytonf3ef3d22011-05-22 22:46:53 +0000365 if (target)
366 m_offset = target->GetCallableLoadAddress(m_offset, GetAddressClass());
Greg Claytoncff851a2011-05-22 04:32:55 +0000367 return true;
368 }
369 return false;
370}
371
Greg Clayton92bb12c2011-05-19 18:17:41 +0000372addr_t
Tamas Berghammer25b9f7e2015-09-07 09:58:09 +0000373Address::GetOpcodeLoadAddress (Target *target, AddressClass addr_class) const
Greg Clayton92bb12c2011-05-19 18:17:41 +0000374{
375 addr_t code_addr = GetLoadAddress (target);
Greg Clayton92bb12c2011-05-19 18:17:41 +0000376 if (code_addr != LLDB_INVALID_ADDRESS)
Tamas Berghammer25b9f7e2015-09-07 09:58:09 +0000377 {
378 if (addr_class == eAddressClassInvalid)
379 addr_class = GetAddressClass();
380 code_addr = target->GetOpcodeLoadAddress (code_addr, addr_class);
381 }
Greg Clayton92bb12c2011-05-19 18:17:41 +0000382 return code_addr;
383}
384
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000385bool
Tamas Berghammer25b9f7e2015-09-07 09:58:09 +0000386Address::SetOpcodeLoadAddress (lldb::addr_t load_addr, Target *target, AddressClass addr_class)
Greg Claytoncff851a2011-05-22 04:32:55 +0000387{
388 if (SetLoadAddress (load_addr, target))
389 {
Greg Claytonf3ef3d22011-05-22 22:46:53 +0000390 if (target)
Tamas Berghammer25b9f7e2015-09-07 09:58:09 +0000391 {
392 if (addr_class == eAddressClassInvalid)
393 addr_class = GetAddressClass();
394 m_offset = target->GetOpcodeLoadAddress (m_offset, addr_class);
395 }
Greg Claytoncff851a2011-05-22 04:32:55 +0000396 return true;
397 }
398 return false;
399}
400
401bool
Greg Claytondda4f7b2010-06-30 23:03:03 +0000402Address::Dump (Stream *s, ExecutionContextScope *exe_scope, DumpStyle style, DumpStyle fallback_style, uint32_t addr_size) const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000403{
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000404 // If the section was nullptr, only load address is going to work unless we are
Greg Claytonc3a86bf2012-07-11 22:18:24 +0000405 // trying to deref a pointer
Greg Claytone72dfb32012-02-24 01:59:29 +0000406 SectionSP section_sp (GetSection());
Greg Claytonc3a86bf2012-07-11 22:18:24 +0000407 if (!section_sp && style != DumpStyleResolvedPointerDescription)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000408 style = DumpStyleLoadAddress;
409
Greg Claytond9e416c2012-02-18 05:35:26 +0000410 ExecutionContext exe_ctx (exe_scope);
411 Target *target = exe_ctx.GetTargetPtr();
Greg Claytondda4f7b2010-06-30 23:03:03 +0000412 // If addr_byte_size is UINT32_MAX, then determine the correct address
413 // byte size for the process or default to the size of addr_t
414 if (addr_size == UINT32_MAX)
415 {
Greg Claytond9e416c2012-02-18 05:35:26 +0000416 if (target)
Greg Clayton514487e2011-02-15 21:59:32 +0000417 addr_size = target->GetArchitecture().GetAddressByteSize ();
Greg Claytondda4f7b2010-06-30 23:03:03 +0000418 else
419 addr_size = sizeof(addr_t);
420 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000421
Greg Claytonc9800662010-09-10 01:30:46 +0000422 Address so_addr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000423 switch (style)
424 {
Greg Claytonc982c762010-07-09 20:39:50 +0000425 case DumpStyleInvalid:
426 return false;
427
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000428 case DumpStyleSectionNameOffset:
Greg Claytone72dfb32012-02-24 01:59:29 +0000429 if (section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000430 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000431 section_sp->DumpName(s);
Daniel Malea48184602013-04-22 20:59:13 +0000432 s->Printf (" + %" PRIu64, m_offset.load());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000433 }
434 else
435 {
Greg Claytondda4f7b2010-06-30 23:03:03 +0000436 s->Address(m_offset, addr_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000437 }
438 break;
439
440 case DumpStyleSectionPointerOffset:
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000441 s->Printf("(Section *)%p + ", static_cast<void*>(section_sp.get()));
Greg Claytondda4f7b2010-06-30 23:03:03 +0000442 s->Address(m_offset, addr_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000443 break;
444
445 case DumpStyleModuleWithFileAddress:
Greg Claytone72dfb32012-02-24 01:59:29 +0000446 if (section_sp)
Jim Ingham4af59612014-12-19 19:20:44 +0000447 {
Jim Inghamcbff63a2016-01-29 20:21:33 +0000448 ModuleSP module_sp = section_sp->GetModule();
449 if (module_sp)
450 s->Printf("%s[", module_sp->GetFileSpec().GetFilename().AsCString("<Unknown>"));
451 else
452 s->Printf("%s[","<Unknown>");
Jim Ingham4af59612014-12-19 19:20:44 +0000453 }
Jason Molenda62e06812016-02-16 04:14:33 +0000454 LLVM_FALLTHROUGH;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000455 case DumpStyleFileAddress:
456 {
457 addr_t file_addr = GetFileAddress();
458 if (file_addr == LLDB_INVALID_ADDRESS)
459 {
460 if (fallback_style != DumpStyleInvalid)
Greg Claytondda4f7b2010-06-30 23:03:03 +0000461 return Dump (s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000462 return false;
463 }
464 s->Address (file_addr, addr_size);
Greg Claytone72dfb32012-02-24 01:59:29 +0000465 if (style == DumpStyleModuleWithFileAddress && section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000466 s->PutChar(']');
467 }
468 break;
469
470 case DumpStyleLoadAddress:
471 {
Greg Claytonf5e56de2010-09-14 23:36:40 +0000472 addr_t load_addr = GetLoadAddress (target);
Jaydeep Patil44d07fc2015-09-22 06:36:56 +0000473
474 /*
475 * MIPS:
476 * Display address in compressed form for MIPS16 or microMIPS
477 * if the address belongs to eAddressClassCodeAlternateISA.
478 */
479 if (target)
480 {
481 const llvm::Triple::ArchType llvm_arch = target->GetArchitecture().GetMachine();
482 if (llvm_arch == llvm::Triple::mips || llvm_arch == llvm::Triple::mipsel
483 || llvm_arch == llvm::Triple::mips64 || llvm_arch == llvm::Triple::mips64el)
484 load_addr = GetCallableLoadAddress (target);
485 }
486
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000487 if (load_addr == LLDB_INVALID_ADDRESS)
488 {
489 if (fallback_style != DumpStyleInvalid)
Greg Claytondda4f7b2010-06-30 23:03:03 +0000490 return Dump (s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000491 return false;
492 }
493 s->Address (load_addr, addr_size);
494 }
495 break;
496
497 case DumpStyleResolvedDescription:
Greg Clayton54b8b8c2010-07-01 01:26:43 +0000498 case DumpStyleResolvedDescriptionNoModule:
Jason Molendaaff1b352014-10-10 23:07:36 +0000499 case DumpStyleResolvedDescriptionNoFunctionArguments:
Jason Molendac980fa92015-02-13 23:24:21 +0000500 case DumpStyleNoFunctionName:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000501 if (IsSectionOffset())
502 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000503 uint32_t pointer_size = 4;
Greg Claytone72dfb32012-02-24 01:59:29 +0000504 ModuleSP module_sp (GetModule());
Greg Clayton514487e2011-02-15 21:59:32 +0000505 if (target)
506 pointer_size = target->GetArchitecture().GetAddressByteSize();
Greg Claytone72dfb32012-02-24 01:59:29 +0000507 else if (module_sp)
508 pointer_size = module_sp->GetArchitecture().GetAddressByteSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000509
510 bool showed_info = false;
Greg Claytone72dfb32012-02-24 01:59:29 +0000511 if (section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000512 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000513 SectionType sect_type = section_sp->GetType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000514 switch (sect_type)
515 {
Greg Clayton89411422010-10-08 00:21:05 +0000516 case eSectionTypeData:
Greg Claytone72dfb32012-02-24 01:59:29 +0000517 if (module_sp)
Greg Clayton89411422010-10-08 00:21:05 +0000518 {
Michael Sartaina7499c92013-07-01 19:45:50 +0000519 SymbolVendor *sym_vendor = module_sp->GetSymbolVendor();
520 if (sym_vendor)
Greg Clayton89411422010-10-08 00:21:05 +0000521 {
Michael Sartaina7499c92013-07-01 19:45:50 +0000522 Symtab *symtab = sym_vendor->GetSymtab();
Greg Clayton89411422010-10-08 00:21:05 +0000523 if (symtab)
524 {
525 const addr_t file_Addr = GetFileAddress();
526 Symbol *symbol = symtab->FindSymbolContainingFileAddress (file_Addr);
527 if (symbol)
528 {
529 const char *symbol_name = symbol->GetName().AsCString();
530 if (symbol_name)
531 {
532 s->PutCString(symbol_name);
Greg Clayton358cf1e2015-06-25 21:46:34 +0000533 addr_t delta = file_Addr - symbol->GetAddressRef().GetFileAddress();
Greg Clayton89411422010-10-08 00:21:05 +0000534 if (delta)
Daniel Malead01b2952012-11-29 21:49:15 +0000535 s->Printf(" + %" PRIu64, delta);
Greg Clayton89411422010-10-08 00:21:05 +0000536 showed_info = true;
537 }
538 }
539 }
540 }
541 }
542 break;
543
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000544 case eSectionTypeDataCString:
545 // Read the C string from memory and display it
546 showed_info = true;
547 ReadCStringFromMemory (exe_scope, *this, s);
548 break;
549
550 case eSectionTypeDataCStringPointers:
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000551 if (ReadAddress(exe_scope, *this, pointer_size, so_addr))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000552 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000553#if VERBOSE_OUTPUT
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000554 s->PutCString("(char *)");
555 so_addr.Dump(s, exe_scope, DumpStyleLoadAddress, DumpStyleFileAddress);
556 s->PutCString(": ");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000557#endif
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000558 showed_info = true;
559 ReadCStringFromMemory(exe_scope, so_addr, s);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000560 }
561 break;
562
563 case eSectionTypeDataObjCMessageRefs:
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000564 if (ReadAddress(exe_scope, *this, pointer_size, so_addr))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000565 {
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000566 if (target && so_addr.IsSectionOffset())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000567 {
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000568 SymbolContext func_sc;
569 target->GetImages().ResolveSymbolContextForAddress(so_addr,
570 eSymbolContextEverything,
571 func_sc);
572 if (func_sc.function != nullptr || func_sc.symbol != nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000573 {
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000574 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);
583 func_sc.DumpStopContext(s, exe_scope, so_addr, true, true, false, true, true);
584 if (ReadAddress(exe_scope, cstr_addr, pointer_size, so_addr))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000585 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000586#if VERBOSE_OUTPUT
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000587 s->PutCString("), (char *)");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000588 so_addr.Dump(s, exe_scope, DumpStyleLoadAddress, DumpStyleFileAddress);
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000589 s->PutCString(" (");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000590#else
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000591 s->PutCString(", ");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000592#endif
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000593 ReadCStringFromMemory (exe_scope, so_addr, s);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000594 }
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000595#if VERBOSE_OUTPUT
596 s->PutCString(") }");
597#else
598 s->PutCString(" }");
599#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000600 }
601 }
602 }
603 break;
604
605 case eSectionTypeDataObjCCFStrings:
606 {
607 Address cfstring_data_addr(*this);
608 cfstring_data_addr.SetOffset(cfstring_data_addr.GetOffset() + (2 * pointer_size));
609 if (ReadAddress (exe_scope, cfstring_data_addr, pointer_size, so_addr))
610 {
611#if VERBOSE_OUTPUT
612 s->PutCString("(CFString *) ");
613 cfstring_data_addr.Dump(s, exe_scope, DumpStyleLoadAddress, DumpStyleFileAddress);
614 s->PutCString(" -> @");
615#else
616 s->PutChar('@');
617#endif
618 if (so_addr.Dump(s, exe_scope, DumpStyleResolvedDescription))
619 showed_info = true;
620 }
621 }
622 break;
623
624 case eSectionTypeData4:
625 // Read the 4 byte data and display it
626 showed_info = true;
627 s->PutCString("(uint32_t) ");
628 DumpUInt (exe_scope, *this, 4, s);
629 break;
630
631 case eSectionTypeData8:
632 // Read the 8 byte data and display it
633 showed_info = true;
634 s->PutCString("(uint64_t) ");
635 DumpUInt (exe_scope, *this, 8, s);
636 break;
637
638 case eSectionTypeData16:
639 // Read the 16 byte data and display it
640 showed_info = true;
641 s->PutCString("(uint128_t) ");
642 DumpUInt (exe_scope, *this, 16, s);
643 break;
644
645 case eSectionTypeDataPointers:
646 // Read the pointer data and display it
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000647 if (ReadAddress(exe_scope, *this, pointer_size, so_addr))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000648 {
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000649 s->PutCString ("(void *)");
650 so_addr.Dump(s, exe_scope, DumpStyleLoadAddress, DumpStyleFileAddress);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000651
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000652 showed_info = true;
653 if (so_addr.IsSectionOffset())
654 {
655 SymbolContext pointer_sc;
656 if (target)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000657 {
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000658 target->GetImages().ResolveSymbolContextForAddress(so_addr,
659 eSymbolContextEverything,
660 pointer_sc);
661 if (pointer_sc.function != nullptr || pointer_sc.symbol != nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000662 {
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000663 s->PutCString(": ");
664 pointer_sc.DumpStopContext(s, exe_scope, so_addr, true, false, false, true, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000665 }
666 }
667 }
668 }
669 break;
Greg Claytonc982c762010-07-09 20:39:50 +0000670
671 default:
672 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000673 }
674 }
675
676 if (!showed_info)
677 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000678 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000679 {
Greg Claytonc9800662010-09-10 01:30:46 +0000680 SymbolContext sc;
Greg Clayton2501e5e2015-01-15 02:59:20 +0000681 module_sp->ResolveSymbolContextForAddress(*this, eSymbolContextEverything | eSymbolContextVariable, sc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000682 if (sc.function || sc.symbol)
683 {
684 bool show_stop_context = true;
Greg Clayton8dc0a982010-09-07 21:56:53 +0000685 const bool show_module = (style == DumpStyleResolvedDescription);
686 const bool show_fullpaths = false;
Greg Clayton513c26c2011-01-29 07:10:55 +0000687 const bool show_inlined_frames = true;
Jason Molendaaff1b352014-10-10 23:07:36 +0000688 const bool show_function_arguments = (style != DumpStyleResolvedDescriptionNoFunctionArguments);
Jason Molendac980fa92015-02-13 23:24:21 +0000689 const bool show_function_name = (style != DumpStyleNoFunctionName);
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000690 if (sc.function == nullptr && sc.symbol != nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000691 {
692 // If we have just a symbol make sure it is in the right section
Greg Claytone7612132012-03-07 21:03:09 +0000693 if (sc.symbol->ValueIsAddress())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000694 {
Greg Clayton358cf1e2015-06-25 21:46:34 +0000695 if (sc.symbol->GetAddressRef().GetSection() != GetSection())
Greg Clayton54b8b8c2010-07-01 01:26:43 +0000696 {
697 // don't show the module if the symbol is a trampoline symbol
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000698 show_stop_context = false;
Greg Clayton54b8b8c2010-07-01 01:26:43 +0000699 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000700 }
701 }
702 if (show_stop_context)
703 {
704 // We have a function or a symbol from the same
705 // sections as this address.
Greg Clayton8dc0a982010-09-07 21:56:53 +0000706 sc.DumpStopContext (s,
707 exe_scope,
708 *this,
709 show_fullpaths,
710 show_module,
Jason Molendaaff1b352014-10-10 23:07:36 +0000711 show_inlined_frames,
Jason Molendac980fa92015-02-13 23:24:21 +0000712 show_function_arguments,
713 show_function_name);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000714 }
715 else
716 {
717 // We found a symbol but it was in a different
718 // section so it isn't the symbol we should be
719 // showing, just show the section name + offset
720 Dump (s, exe_scope, DumpStyleSectionNameOffset);
721 }
722 }
723 }
724 }
725 }
726 else
727 {
728 if (fallback_style != DumpStyleInvalid)
Greg Claytondda4f7b2010-06-30 23:03:03 +0000729 return Dump (s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000730 return false;
731 }
732 break;
Greg Claytondda4f7b2010-06-30 23:03:03 +0000733
734 case DumpStyleDetailedSymbolContext:
735 if (IsSectionOffset())
736 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000737 ModuleSP module_sp (GetModule());
738 if (module_sp)
Greg Claytondda4f7b2010-06-30 23:03:03 +0000739 {
Greg Claytonc9800662010-09-10 01:30:46 +0000740 SymbolContext sc;
Greg Clayton2501e5e2015-01-15 02:59:20 +0000741 module_sp->ResolveSymbolContextForAddress(*this, eSymbolContextEverything | eSymbolContextVariable, sc);
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000742 if (sc.symbol)
Greg Claytondda4f7b2010-06-30 23:03:03 +0000743 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000744 // If we have just a symbol make sure it is in the same section
745 // as our address. If it isn't, then we might have just found
746 // the last symbol that came before the address that we are
747 // looking up that has nothing to do with our address lookup.
Greg Clayton358cf1e2015-06-25 21:46:34 +0000748 if (sc.symbol->ValueIsAddress() && sc.symbol->GetAddressRef().GetSection() != GetSection())
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000749 sc.symbol = nullptr;
Greg Claytondda4f7b2010-06-30 23:03:03 +0000750 }
Greg Claytonf5e56de2010-09-14 23:36:40 +0000751 sc.GetDescription(s, eDescriptionLevelBrief, target);
Greg Claytonc749eb82011-07-11 05:12:02 +0000752
753 if (sc.block)
754 {
755 bool can_create = true;
756 bool get_parent_variables = true;
757 bool stop_if_block_is_inlined_function = false;
758 VariableList variable_list;
759 sc.block->AppendVariables (can_create,
Tamas Berghammer72ac8a82016-02-25 12:23:37 +0000760 get_parent_variables,
761 stop_if_block_is_inlined_function,
762 [](Variable*) { return true; },
Greg Claytonc749eb82011-07-11 05:12:02 +0000763 &variable_list);
Tamas Berghammer72ac8a82016-02-25 12:23:37 +0000764
Greg Claytonc7bece562013-01-25 18:06:21 +0000765 const size_t num_variables = variable_list.GetSize();
766 for (size_t var_idx = 0; var_idx < num_variables; ++var_idx)
Greg Claytonc749eb82011-07-11 05:12:02 +0000767 {
768 Variable *var = variable_list.GetVariableAtIndex (var_idx).get();
769 if (var && var->LocationIsValidForAddress (*this))
770 {
Greg Claytonc4a8a762012-05-15 18:43:44 +0000771 s->Indent();
Ilia K6e465122015-04-28 12:45:57 +0000772 s->Printf (" Variable: id = {0x%8.8" PRIx64 "}, name = \"%s\"",
Greg Claytonc749eb82011-07-11 05:12:02 +0000773 var->GetID(),
Ilia K6e465122015-04-28 12:45:57 +0000774 var->GetName().GetCString());
775 Type *type = var->GetType();
776 if (type)
777 s->Printf(", type = \"%s\"", type->GetName().GetCString());
778 else
779 s->PutCString(", type = <unknown>");
780 s->PutCString(", location = ");
Greg Claytonc749eb82011-07-11 05:12:02 +0000781 var->DumpLocationForAddress(s, *this);
782 s->PutCString(", decl = ");
783 var->GetDeclaration().DumpStopContext(s, false);
784 s->EOL();
785 }
786 }
787 }
Greg Claytondda4f7b2010-06-30 23:03:03 +0000788 }
789 }
Greg Claytonc749eb82011-07-11 05:12:02 +0000790 else
791 {
792 if (fallback_style != DumpStyleInvalid)
793 return Dump (s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
794 return false;
795 }
Greg Claytondda4f7b2010-06-30 23:03:03 +0000796 break;
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000797
Greg Claytonc3a86bf2012-07-11 22:18:24 +0000798 case DumpStyleResolvedPointerDescription:
799 {
800 Process *process = exe_ctx.GetProcessPtr();
801 if (process)
802 {
803 addr_t load_addr = GetLoadAddress (target);
804 if (load_addr != LLDB_INVALID_ADDRESS)
805 {
806 Error memory_error;
807 addr_t dereferenced_load_addr = process->ReadPointerFromMemory(load_addr, memory_error);
808 if (dereferenced_load_addr != LLDB_INVALID_ADDRESS)
809 {
810 Address dereferenced_addr;
811 if (dereferenced_addr.SetLoadAddress(dereferenced_load_addr, target))
812 {
813 StreamString strm;
814 if (dereferenced_addr.Dump (&strm, exe_scope, DumpStyleResolvedDescription, DumpStyleInvalid, addr_size))
815 {
816 s->Address (dereferenced_load_addr, addr_size, " -> ", " ");
817 s->Write(strm.GetData(), strm.GetSize());
818 return true;
819 }
820 }
821 }
822 }
823 }
824 if (fallback_style != DumpStyleInvalid)
825 return Dump (s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
826 return false;
827 }
828 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000829 }
830
831 return true;
832}
833
Greg Claytoncae56522013-11-06 02:29:13 +0000834bool
835Address::SectionWasDeleted() const
836{
Greg Claytonb35db632013-11-09 00:03:31 +0000837 if (GetSection())
838 return false;
839 return SectionWasDeletedPrivate();
840}
841
842bool
843Address::SectionWasDeletedPrivate() const
844{
Greg Claytoncae56522013-11-06 02:29:13 +0000845 lldb::SectionWP empty_section_wp;
846
847 // If either call to "std::weak_ptr::owner_before(...) value returns true, this
848 // indicates that m_section_wp once contained (possibly still does) a reference
849 // to a valid shared pointer. This helps us know if we had a valid reference to
850 // a section which is now invalid because the module it was in was unloaded/deleted,
851 // or if the address doesn't have a valid reference to a section.
852 return empty_section_wp.owner_before(m_section_wp) || m_section_wp.owner_before(empty_section_wp);
853}
854
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000855uint32_t
Greg Clayton6f6bf262011-12-10 21:05:26 +0000856Address::CalculateSymbolContext (SymbolContext *sc, uint32_t resolve_scope) const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000857{
Greg Clayton72310352013-02-23 04:12:47 +0000858 sc->Clear(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000859 // Absolute addresses don't have enough information to reconstruct even their target.
Greg Claytone72dfb32012-02-24 01:59:29 +0000860
861 SectionSP section_sp (GetSection());
862 if (section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000863 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000864 ModuleSP module_sp (section_sp->GetModule());
865 if (module_sp)
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000866 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000867 sc->module_sp = module_sp;
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000868 if (sc->module_sp)
869 return sc->module_sp->ResolveSymbolContextForAddress (*this, resolve_scope, *sc);
870 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000871 }
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000872 return 0;
873}
874
Greg Claytone72dfb32012-02-24 01:59:29 +0000875ModuleSP
Greg Clayton6f6bf262011-12-10 21:05:26 +0000876Address::CalculateSymbolContextModule () const
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000877{
Greg Claytone72dfb32012-02-24 01:59:29 +0000878 SectionSP section_sp (GetSection());
879 if (section_sp)
880 return section_sp->GetModule();
881 return ModuleSP();
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000882}
883
884CompileUnit *
Greg Clayton6f6bf262011-12-10 21:05:26 +0000885Address::CalculateSymbolContextCompileUnit () const
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000886{
Greg Claytone72dfb32012-02-24 01:59:29 +0000887 SectionSP section_sp (GetSection());
888 if (section_sp)
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000889 {
890 SymbolContext sc;
Greg Claytone72dfb32012-02-24 01:59:29 +0000891 sc.module_sp = section_sp->GetModule();
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000892 if (sc.module_sp)
893 {
894 sc.module_sp->ResolveSymbolContextForAddress (*this, eSymbolContextCompUnit, sc);
895 return sc.comp_unit;
896 }
897 }
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000898 return nullptr;
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000899}
900
901Function *
Greg Clayton6f6bf262011-12-10 21:05:26 +0000902Address::CalculateSymbolContextFunction () const
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000903{
Greg Claytone72dfb32012-02-24 01:59:29 +0000904 SectionSP section_sp (GetSection());
905 if (section_sp)
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000906 {
907 SymbolContext sc;
Greg Claytone72dfb32012-02-24 01:59:29 +0000908 sc.module_sp = section_sp->GetModule();
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000909 if (sc.module_sp)
910 {
911 sc.module_sp->ResolveSymbolContextForAddress (*this, eSymbolContextFunction, sc);
912 return sc.function;
913 }
914 }
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000915 return nullptr;
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000916}
917
918Block *
Greg Clayton6f6bf262011-12-10 21:05:26 +0000919Address::CalculateSymbolContextBlock () const
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000920{
Greg Claytone72dfb32012-02-24 01:59:29 +0000921 SectionSP section_sp (GetSection());
922 if (section_sp)
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000923 {
924 SymbolContext sc;
Greg Claytone72dfb32012-02-24 01:59:29 +0000925 sc.module_sp = section_sp->GetModule();
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000926 if (sc.module_sp)
927 {
928 sc.module_sp->ResolveSymbolContextForAddress (*this, eSymbolContextBlock, sc);
929 return sc.block;
930 }
931 }
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000932 return nullptr;
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000933}
934
935Symbol *
Greg Clayton6f6bf262011-12-10 21:05:26 +0000936Address::CalculateSymbolContextSymbol () const
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000937{
Greg Claytone72dfb32012-02-24 01:59:29 +0000938 SectionSP section_sp (GetSection());
939 if (section_sp)
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000940 {
941 SymbolContext sc;
Greg Claytone72dfb32012-02-24 01:59:29 +0000942 sc.module_sp = section_sp->GetModule();
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000943 if (sc.module_sp)
944 {
945 sc.module_sp->ResolveSymbolContextForAddress (*this, eSymbolContextSymbol, sc);
946 return sc.symbol;
947 }
948 }
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000949 return nullptr;
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000950}
951
952bool
Greg Clayton6f6bf262011-12-10 21:05:26 +0000953Address::CalculateSymbolContextLineEntry (LineEntry &line_entry) const
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000954{
Greg Claytone72dfb32012-02-24 01:59:29 +0000955 SectionSP section_sp (GetSection());
956 if (section_sp)
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000957 {
958 SymbolContext sc;
Greg Claytone72dfb32012-02-24 01:59:29 +0000959 sc.module_sp = section_sp->GetModule();
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000960 if (sc.module_sp)
961 {
962 sc.module_sp->ResolveSymbolContextForAddress (*this, eSymbolContextLineEntry, sc);
963 if (sc.line_entry.IsValid())
964 {
965 line_entry = sc.line_entry;
966 return true;
967 }
968 }
969 }
970 line_entry.Clear();
971 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000972}
973
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000974int
975Address::CompareFileAddress (const Address& a, const Address& b)
976{
977 addr_t a_file_addr = a.GetFileAddress();
978 addr_t b_file_addr = b.GetFileAddress();
979 if (a_file_addr < b_file_addr)
980 return -1;
981 if (a_file_addr > b_file_addr)
982 return +1;
983 return 0;
984}
985
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000986int
Greg Claytonf5e56de2010-09-14 23:36:40 +0000987Address::CompareLoadAddress (const Address& a, const Address& b, Target *target)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000988{
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000989 assert(target != nullptr);
Greg Claytonf5e56de2010-09-14 23:36:40 +0000990 addr_t a_load_addr = a.GetLoadAddress (target);
991 addr_t b_load_addr = b.GetLoadAddress (target);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000992 if (a_load_addr < b_load_addr)
993 return -1;
994 if (a_load_addr > b_load_addr)
995 return +1;
996 return 0;
997}
998
999int
1000Address::CompareModulePointerAndOffset (const Address& a, const Address& b)
1001{
Greg Claytone72dfb32012-02-24 01:59:29 +00001002 ModuleSP a_module_sp (a.GetModule());
1003 ModuleSP b_module_sp (b.GetModule());
1004 Module *a_module = a_module_sp.get();
1005 Module *b_module = b_module_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001006 if (a_module < b_module)
1007 return -1;
1008 if (a_module > b_module)
1009 return +1;
1010 // Modules are the same, just compare the file address since they should
1011 // be unique
1012 addr_t a_file_addr = a.GetFileAddress();
1013 addr_t b_file_addr = b.GetFileAddress();
1014 if (a_file_addr < b_file_addr)
1015 return -1;
1016 if (a_file_addr > b_file_addr)
1017 return +1;
1018 return 0;
1019}
1020
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001021size_t
1022Address::MemorySize () const
1023{
1024 // Noting special for the memory size of a single Address object,
1025 // it is just the size of itself.
1026 return sizeof(Address);
1027}
1028
Greg Claytonb0848c52011-01-08 00:05:12 +00001029//----------------------------------------------------------------------
1030// NOTE: Be careful using this operator. It can correctly compare two
1031// addresses from the same Module correctly. It can't compare two
1032// addresses from different modules in any meaningful way, but it will
1033// compare the module pointers.
1034//
1035// To sum things up:
1036// - works great for addresses within the same module
1037// - it works for addresses across multiple modules, but don't expect the
1038// address results to make much sense
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001039//
Greg Claytonb0848c52011-01-08 00:05:12 +00001040// This basically lets Address objects be used in ordered collection
1041// classes.
1042//----------------------------------------------------------------------
1043
1044bool
1045lldb_private::operator< (const Address& lhs, const Address& rhs)
1046{
Greg Claytone72dfb32012-02-24 01:59:29 +00001047 ModuleSP lhs_module_sp (lhs.GetModule());
1048 ModuleSP rhs_module_sp (rhs.GetModule());
1049 Module *lhs_module = lhs_module_sp.get();
1050 Module *rhs_module = rhs_module_sp.get();
Greg Claytonb0848c52011-01-08 00:05:12 +00001051 if (lhs_module == rhs_module)
1052 {
1053 // Addresses are in the same module, just compare the file addresses
1054 return lhs.GetFileAddress() < rhs.GetFileAddress();
1055 }
1056 else
1057 {
1058 // The addresses are from different modules, just use the module
1059 // pointer value to get consistent ordering
1060 return lhs_module < rhs_module;
1061 }
1062}
1063
1064bool
1065lldb_private::operator> (const Address& lhs, const Address& rhs)
1066{
Greg Claytone72dfb32012-02-24 01:59:29 +00001067 ModuleSP lhs_module_sp (lhs.GetModule());
1068 ModuleSP rhs_module_sp (rhs.GetModule());
1069 Module *lhs_module = lhs_module_sp.get();
1070 Module *rhs_module = rhs_module_sp.get();
Greg Claytonb0848c52011-01-08 00:05:12 +00001071 if (lhs_module == rhs_module)
1072 {
1073 // Addresses are in the same module, just compare the file addresses
1074 return lhs.GetFileAddress() > rhs.GetFileAddress();
1075 }
1076 else
1077 {
1078 // The addresses are from different modules, just use the module
1079 // pointer value to get consistent ordering
1080 return lhs_module > rhs_module;
1081 }
1082}
1083
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001084// The operator == checks for exact equality only (same section, same offset)
1085bool
1086lldb_private::operator== (const Address& a, const Address& rhs)
1087{
Jim Inghamd3480f52013-06-06 22:16:56 +00001088 return a.GetOffset() == rhs.GetOffset() &&
1089 a.GetSection() == rhs.GetSection();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001090}
Eugene Zelenko896ddd02016-03-02 01:09:03 +00001091
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001092// The operator != checks for exact inequality only (differing section, or
1093// different offset)
1094bool
1095lldb_private::operator!= (const Address& a, const Address& rhs)
1096{
Jim Inghamd3480f52013-06-06 22:16:56 +00001097 return a.GetOffset() != rhs.GetOffset() ||
1098 a.GetSection() != rhs.GetSection();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001099}
1100
Greg Claytone0d378b2011-03-24 21:19:54 +00001101AddressClass
Greg Claytonded470d2011-03-19 01:12:21 +00001102Address::GetAddressClass () const
1103{
Greg Claytone72dfb32012-02-24 01:59:29 +00001104 ModuleSP module_sp (GetModule());
1105 if (module_sp)
Greg Claytonded470d2011-03-19 01:12:21 +00001106 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001107 ObjectFile *obj_file = module_sp->GetObjectFile();
Greg Claytonded470d2011-03-19 01:12:21 +00001108 if (obj_file)
Michael Sartaina7499c92013-07-01 19:45:50 +00001109 {
1110 // Give the symbol vendor a chance to add to the unified section list.
1111 module_sp->GetSymbolVendor();
Greg Claytonded470d2011-03-19 01:12:21 +00001112 return obj_file->GetAddressClass (GetFileAddress());
Michael Sartaina7499c92013-07-01 19:45:50 +00001113 }
Greg Claytonded470d2011-03-19 01:12:21 +00001114 }
1115 return eAddressClassUnknown;
1116}
Greg Claytoncd482e32011-05-18 01:58:14 +00001117
1118bool
1119Address::SetLoadAddress (lldb::addr_t load_addr, Target *target)
1120{
1121 if (target && target->GetSectionLoadList().ResolveLoadAddress(load_addr, *this))
1122 return true;
Greg Claytone72dfb32012-02-24 01:59:29 +00001123 m_section_wp.reset();
Greg Claytoncd482e32011-05-18 01:58:14 +00001124 m_offset = load_addr;
1125 return false;
1126}