blob: 7ab02a40f9a712e398a623b831e339e42c2696bf [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"
13#include "lldb/Symbol/ObjectFile.h"
Greg Claytonf5e56de2010-09-14 23:36:40 +000014#include "lldb/Target/ExecutionContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015#include "lldb/Target/Process.h"
16#include "lldb/Target/Target.h"
17
Greg Clayton3f5c08f2011-05-18 22:01:49 +000018#include "llvm/ADT/Triple.h"
19
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020using namespace lldb;
21using namespace lldb_private;
22
23static size_t
24ReadBytes (ExecutionContextScope *exe_scope, const Address &address, void *dst, size_t dst_len)
25{
26 if (exe_scope == NULL)
27 return 0;
28
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029 Target *target = exe_scope->CalculateTarget();
30 if (target)
31 {
32 Error error;
Greg Claytondb598232011-01-07 01:57:07 +000033 bool prefer_file_cache = false;
34 return target->ReadMemory (address, prefer_file_cache, dst, dst_len, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035 }
36 return 0;
37}
38
39static bool
40GetByteOrderAndAddressSize (ExecutionContextScope *exe_scope, const Address &address, ByteOrder& byte_order, uint32_t& addr_size)
41{
42 byte_order = eByteOrderInvalid;
43 addr_size = 0;
44 if (exe_scope == NULL)
45 return false;
46
Greg Clayton514487e2011-02-15 21:59:32 +000047 Target *target = exe_scope->CalculateTarget();
48 if (target)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000049 {
Greg Clayton514487e2011-02-15 21:59:32 +000050 byte_order = target->GetArchitecture().GetByteOrder();
51 addr_size = target->GetArchitecture().GetAddressByteSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000052 }
53
54 if (byte_order == eByteOrderInvalid || addr_size == 0)
55 {
56 Module *module = address.GetModule();
57 if (module)
58 {
Greg Clayton514487e2011-02-15 21:59:32 +000059 byte_order = module->GetArchitecture().GetByteOrder();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000060 addr_size = module->GetArchitecture().GetAddressByteSize();
61 }
62 }
63 return byte_order != eByteOrderInvalid && addr_size != 0;
64}
65
66static uint64_t
67ReadUIntMax64 (ExecutionContextScope *exe_scope, const Address &address, uint32_t byte_size, bool &success)
68{
69 uint64_t uval64 = 0;
70 if (exe_scope == NULL || byte_size > sizeof(uint64_t))
71 {
72 success = false;
73 return 0;
74 }
75 uint64_t buf;
76
77 success = ReadBytes (exe_scope, address, &buf, byte_size) == byte_size;
78 if (success)
79 {
80 ByteOrder byte_order = eByteOrderInvalid;
81 uint32_t addr_size = 0;
82 if (GetByteOrderAndAddressSize (exe_scope, address, byte_order, addr_size))
83 {
84 DataExtractor data (&buf, sizeof(buf), byte_order, addr_size);
85 uint32_t offset = 0;
86 uval64 = data.GetU64(&offset);
87 }
88 else
89 success = false;
90 }
91 return uval64;
92}
93
94static bool
95ReadAddress (ExecutionContextScope *exe_scope, const Address &address, uint32_t pointer_size, Address &deref_so_addr)
96{
97 if (exe_scope == NULL)
98 return false;
99
100
101 bool success = false;
102 addr_t deref_addr = ReadUIntMax64 (exe_scope, address, pointer_size, success);
103 if (success)
104 {
Greg Claytonf5e56de2010-09-14 23:36:40 +0000105 ExecutionContext exe_ctx;
Greg Clayton0603aa92010-10-04 01:05:56 +0000106 exe_scope->CalculateExecutionContext(exe_ctx);
Greg Claytonf5e56de2010-09-14 23:36:40 +0000107 // If we have any sections that are loaded, try and resolve using the
108 // section load list
109 if (exe_ctx.target && !exe_ctx.target->GetSectionLoadList().IsEmpty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000110 {
Greg Claytonf5e56de2010-09-14 23:36:40 +0000111 if (exe_ctx.target->GetSectionLoadList().ResolveLoadAddress (deref_addr, deref_so_addr))
112 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000113 }
114 else
115 {
Greg Claytonf5e56de2010-09-14 23:36:40 +0000116 // If we were not running, yet able to read an integer, we must
117 // have a module
118 Module *module = address.GetModule();
119 assert (module);
120 if (module->ResolveFileAddress(deref_addr, deref_so_addr))
121 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000122 }
Greg Claytonf5e56de2010-09-14 23:36:40 +0000123
124 // We couldn't make "deref_addr" into a section offset value, but we were
125 // able to read the address, so we return a section offset address with
126 // no section and "deref_addr" as the offset (address).
127 deref_so_addr.SetSection(NULL);
128 deref_so_addr.SetOffset(deref_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000129 return true;
130 }
131 return false;
132}
133
134static bool
135DumpUInt (ExecutionContextScope *exe_scope, const Address &address, uint32_t byte_size, Stream* strm)
136{
Greg Clayton471b31c2010-07-20 22:52:08 +0000137 if (exe_scope == NULL || byte_size == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000138 return 0;
139 std::vector<uint8_t> buf(byte_size, 0);
140
141 if (ReadBytes (exe_scope, address, &buf[0], buf.size()) == buf.size())
142 {
143 ByteOrder byte_order = eByteOrderInvalid;
144 uint32_t addr_size = 0;
145 if (GetByteOrderAndAddressSize (exe_scope, address, byte_order, addr_size))
146 {
Greg Clayton471b31c2010-07-20 22:52:08 +0000147 DataExtractor data (&buf.front(), buf.size(), byte_order, addr_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000148
149 data.Dump (strm,
150 0, // Start offset in "data"
151 eFormatHex, // Print as characters
152 buf.size(), // Size of item
153 1, // Items count
154 UINT32_MAX, // num per line
155 LLDB_INVALID_ADDRESS,// base address
156 0, // bitfield bit size
157 0); // bitfield bit offset
158
159 return true;
160 }
161 }
162 return false;
163}
164
165
166static size_t
167ReadCStringFromMemory (ExecutionContextScope *exe_scope, const Address &address, Stream *strm)
168{
169 if (exe_scope == NULL)
170 return 0;
171 const size_t k_buf_len = 256;
172 char buf[k_buf_len+1];
173 buf[k_buf_len] = '\0'; // NULL terminate
174
Greg Clayton710dd5a2011-01-08 20:28:42 +0000175 // Byte order and address size don't matter for C string dumping..
Greg Clayton7fb56d02011-02-01 01:31:41 +0000176 DataExtractor data (buf, sizeof(buf), lldb::endian::InlHostByteOrder(), 4);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000177 size_t total_len = 0;
178 size_t bytes_read;
179 Address curr_address(address);
180 strm->PutChar ('"');
181 while ((bytes_read = ReadBytes (exe_scope, curr_address, buf, k_buf_len)) > 0)
182 {
183 size_t len = strlen(buf);
184 if (len == 0)
185 break;
186 if (len > bytes_read)
187 len = bytes_read;
188
189 data.Dump (strm,
190 0, // Start offset in "data"
191 eFormatChar, // Print as characters
192 1, // Size of item (1 byte for a char!)
193 len, // How many bytes to print?
194 UINT32_MAX, // num per line
195 LLDB_INVALID_ADDRESS,// base address
196 0, // bitfield bit size
197
198 0); // bitfield bit offset
199
200 total_len += bytes_read;
201
202 if (len < k_buf_len)
203 break;
204 curr_address.SetOffset (curr_address.GetOffset() + bytes_read);
205 }
206 strm->PutChar ('"');
207 return total_len;
208}
209
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000210Address::Address (addr_t address, const SectionList * sections) :
211 m_section (NULL),
212 m_offset (LLDB_INVALID_ADDRESS)
213{
214 ResolveAddressUsingFileSections(address, sections);
215}
216
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000217const Address&
218Address::operator= (const Address& rhs)
219{
220 if (this != &rhs)
221 {
222 m_section = rhs.m_section;
223 m_offset = rhs.m_offset;
224 }
225 return *this;
226}
227
228bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000229Address::ResolveAddressUsingFileSections (addr_t addr, const SectionList *sections)
230{
231 if (sections)
232 m_section = sections->FindSectionContainingFileAddress(addr).get();
233 else
234 m_section = NULL;
235
236 if (m_section != NULL)
237 {
238 assert( m_section->ContainsFileAddress(addr) );
239 m_offset = addr - m_section->GetFileAddress();
240 return true; // Successfully transformed addr into a section offset address
241 }
242
243 m_offset = addr;
244 return false; // Failed to resolve this address to a section offset value
245}
246
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000247Module *
248Address::GetModule () const
249{
250 if (m_section)
251 return m_section->GetModule();
252 return NULL;
253}
254
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000255addr_t
256Address::GetFileAddress () const
257{
258 if (m_section != NULL)
259 {
260 addr_t sect_file_addr = m_section->GetFileAddress();
261 if (sect_file_addr == LLDB_INVALID_ADDRESS)
262 {
263 // Section isn't resolved, we can't return a valid file address
264 return LLDB_INVALID_ADDRESS;
265 }
266 // We have a valid file range, so we can return the file based
267 // address by adding the file base address to our offset
268 return sect_file_addr + m_offset;
269 }
270 // No section, we just return the offset since it is the value in this case
271 return m_offset;
272}
273
274addr_t
Greg Claytonf5e56de2010-09-14 23:36:40 +0000275Address::GetLoadAddress (Target *target) const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000276{
Greg Claytonf5e56de2010-09-14 23:36:40 +0000277 if (m_section == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000278 {
Greg Claytonf5e56de2010-09-14 23:36:40 +0000279 // No section, we just return the offset since it is the value in this case
280 return m_offset;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000281 }
Greg Claytonf5e56de2010-09-14 23:36:40 +0000282
283 if (target)
284 {
285 addr_t sect_load_addr = m_section->GetLoadBaseAddress (target);
286
287 if (sect_load_addr != LLDB_INVALID_ADDRESS)
288 {
289 // We have a valid file range, so we can return the file based
290 // address by adding the file base address to our offset
291 return sect_load_addr + m_offset;
292 }
293 }
294 // The section isn't resolved or no process was supplied so we can't
295 // return a valid file address.
296 return LLDB_INVALID_ADDRESS;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000297}
298
Greg Clayton3f5c08f2011-05-18 22:01:49 +0000299addr_t
300Address::GetCallableLoadAddress (Target *target) const
301{
302 addr_t code_addr = GetLoadAddress (target);
303
Greg Clayton92bb12c2011-05-19 18:17:41 +0000304 // Make sure we have section, otherwise the call to GetAddressClass() will
305 // fail because it uses the section to get to the module.
Greg Clayton3f5c08f2011-05-18 22:01:49 +0000306 if (m_section && code_addr != LLDB_INVALID_ADDRESS)
307 {
308 switch (target->GetArchitecture().GetMachine())
309 {
310 case llvm::Triple::arm:
311 case llvm::Triple::thumb:
312 // Check if bit zero it no set?
313 if ((code_addr & 1ull) == 0)
314 {
315 // Bit zero isn't set, check if the address is a multiple of 2?
316 if (code_addr & 2ull)
317 {
318 // The address is a multiple of 2 so it must be thumb, set bit zero
319 code_addr |= 1ull;
320 }
321 else if (GetAddressClass() == eAddressClassCodeAlternateISA)
322 {
323 // We checked the address and the address claims to be the alternate ISA
324 // which means thumb, so set bit zero.
325 code_addr |= 1ull;
326 }
327 }
328 break;
329
330 default:
331 break;
332 }
333 }
334 return code_addr;
335}
336
Greg Clayton92bb12c2011-05-19 18:17:41 +0000337addr_t
338Address::GetOpcodeLoadAddress (Target *target) const
339{
340 addr_t code_addr = GetLoadAddress (target);
341
342 if (code_addr != LLDB_INVALID_ADDRESS)
343 {
344 switch (target->GetArchitecture().GetMachine())
345 {
346 case llvm::Triple::arm:
347 case llvm::Triple::thumb:
348 // Strip bit zero to make sure we end up on an opcode boundary
349 return code_addr & ~(1ull);
350 break;
351
352 default:
353 break;
354 }
355 }
356 return code_addr;
357}
358
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000359bool
Greg Claytondda4f7b2010-06-30 23:03:03 +0000360Address::Dump (Stream *s, ExecutionContextScope *exe_scope, DumpStyle style, DumpStyle fallback_style, uint32_t addr_size) const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000361{
362 // If the section was NULL, only load address is going to work.
363 if (m_section == NULL)
364 style = DumpStyleLoadAddress;
365
Greg Clayton54b8b8c2010-07-01 01:26:43 +0000366 Target *target = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000367 Process *process = NULL;
368 if (exe_scope)
Greg Clayton54b8b8c2010-07-01 01:26:43 +0000369 {
370 target = exe_scope->CalculateTarget();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000371 process = exe_scope->CalculateProcess();
Greg Clayton54b8b8c2010-07-01 01:26:43 +0000372 }
Greg Claytondda4f7b2010-06-30 23:03:03 +0000373 // If addr_byte_size is UINT32_MAX, then determine the correct address
374 // byte size for the process or default to the size of addr_t
375 if (addr_size == UINT32_MAX)
376 {
377 if (process)
Greg Clayton514487e2011-02-15 21:59:32 +0000378 addr_size = target->GetArchitecture().GetAddressByteSize ();
Greg Claytondda4f7b2010-06-30 23:03:03 +0000379 else
380 addr_size = sizeof(addr_t);
381 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000382
Greg Claytonc9800662010-09-10 01:30:46 +0000383 Address so_addr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000384 switch (style)
385 {
Greg Claytonc982c762010-07-09 20:39:50 +0000386 case DumpStyleInvalid:
387 return false;
388
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000389 case DumpStyleSectionNameOffset:
390 if (m_section != NULL)
391 {
392 m_section->DumpName(s);
393 s->Printf (" + %llu", m_offset);
394 }
395 else
396 {
Greg Claytondda4f7b2010-06-30 23:03:03 +0000397 s->Address(m_offset, addr_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000398 }
399 break;
400
401 case DumpStyleSectionPointerOffset:
Greg Claytondda4f7b2010-06-30 23:03:03 +0000402 s->Printf("(Section *)%.*p + ", (int)sizeof(void*) * 2, m_section);
403 s->Address(m_offset, addr_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000404 break;
405
406 case DumpStyleModuleWithFileAddress:
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000407 if (m_section)
408 s->Printf("%s[", m_section->GetModule()->GetFileSpec().GetFilename().AsCString());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000409 // Fall through
410 case DumpStyleFileAddress:
411 {
412 addr_t file_addr = GetFileAddress();
413 if (file_addr == LLDB_INVALID_ADDRESS)
414 {
415 if (fallback_style != DumpStyleInvalid)
Greg Claytondda4f7b2010-06-30 23:03:03 +0000416 return Dump (s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000417 return false;
418 }
419 s->Address (file_addr, addr_size);
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000420 if (style == DumpStyleModuleWithFileAddress && m_section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000421 s->PutChar(']');
422 }
423 break;
424
425 case DumpStyleLoadAddress:
426 {
Greg Claytonf5e56de2010-09-14 23:36:40 +0000427 addr_t load_addr = GetLoadAddress (target);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000428 if (load_addr == LLDB_INVALID_ADDRESS)
429 {
430 if (fallback_style != DumpStyleInvalid)
Greg Claytondda4f7b2010-06-30 23:03:03 +0000431 return Dump (s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000432 return false;
433 }
434 s->Address (load_addr, addr_size);
435 }
436 break;
437
438 case DumpStyleResolvedDescription:
Greg Clayton54b8b8c2010-07-01 01:26:43 +0000439 case DumpStyleResolvedDescriptionNoModule:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000440 if (IsSectionOffset())
441 {
Greg Claytone0d378b2011-03-24 21:19:54 +0000442 AddressType addr_type = eAddressTypeLoad;
Greg Claytonf5e56de2010-09-14 23:36:40 +0000443 addr_t addr = GetLoadAddress (target);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000444 if (addr == LLDB_INVALID_ADDRESS)
445 {
446 addr = GetFileAddress();
447 addr_type = eAddressTypeFile;
448 }
449
450 uint32_t pointer_size = 4;
Greg Claytonc9800662010-09-10 01:30:46 +0000451 Module *module = GetModule();
Greg Clayton514487e2011-02-15 21:59:32 +0000452 if (target)
453 pointer_size = target->GetArchitecture().GetAddressByteSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000454 else if (module)
455 pointer_size = module->GetArchitecture().GetAddressByteSize();
456
457 bool showed_info = false;
458 const Section *section = GetSection();
459 if (section)
460 {
Greg Clayton70e33eb2010-07-21 21:49:46 +0000461 SectionType sect_type = section->GetType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000462 switch (sect_type)
463 {
Greg Clayton89411422010-10-08 00:21:05 +0000464 case eSectionTypeData:
465 if (module)
466 {
467 ObjectFile *objfile = module->GetObjectFile();
468 if (objfile)
469 {
470 Symtab *symtab = objfile->GetSymtab();
471 if (symtab)
472 {
473 const addr_t file_Addr = GetFileAddress();
474 Symbol *symbol = symtab->FindSymbolContainingFileAddress (file_Addr);
475 if (symbol)
476 {
477 const char *symbol_name = symbol->GetName().AsCString();
478 if (symbol_name)
479 {
480 s->PutCString(symbol_name);
481 addr_t delta = file_Addr - symbol->GetAddressRangePtr()->GetBaseAddress().GetFileAddress();
482 if (delta)
483 s->Printf(" + %llu", delta);
484 showed_info = true;
485 }
486 }
487 }
488 }
489 }
490 break;
491
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000492 case eSectionTypeDataCString:
493 // Read the C string from memory and display it
494 showed_info = true;
495 ReadCStringFromMemory (exe_scope, *this, s);
496 break;
497
498 case eSectionTypeDataCStringPointers:
499 {
500 if (ReadAddress (exe_scope, *this, pointer_size, so_addr))
501 {
502#if VERBOSE_OUTPUT
503 s->PutCString("(char *)");
504 so_addr.Dump(s, exe_scope, DumpStyleLoadAddress, DumpStyleFileAddress);
505 s->PutCString(": ");
506#endif
507 showed_info = true;
508 ReadCStringFromMemory (exe_scope, so_addr, s);
509 }
510 }
511 break;
512
513 case eSectionTypeDataObjCMessageRefs:
514 {
515 if (ReadAddress (exe_scope, *this, pointer_size, so_addr))
516 {
Greg Clayton54b8b8c2010-07-01 01:26:43 +0000517 if (target && so_addr.IsSectionOffset())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000518 {
Greg Claytonc9800662010-09-10 01:30:46 +0000519 SymbolContext func_sc;
Greg Clayton54b8b8c2010-07-01 01:26:43 +0000520 target->GetImages().ResolveSymbolContextForAddress (so_addr,
521 eSymbolContextEverything,
522 func_sc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000523 if (func_sc.function || func_sc.symbol)
524 {
525 showed_info = true;
526#if VERBOSE_OUTPUT
527 s->PutCString ("(objc_msgref *) -> { (func*)");
528 so_addr.Dump(s, exe_scope, DumpStyleLoadAddress, DumpStyleFileAddress);
529#else
530 s->PutCString ("{ ");
531#endif
532 Address cstr_addr(*this);
533 cstr_addr.SetOffset(cstr_addr.GetOffset() + pointer_size);
Greg Clayton6dadd502010-09-02 21:44:10 +0000534 func_sc.DumpStopContext(s, exe_scope, so_addr, true, true, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000535 if (ReadAddress (exe_scope, cstr_addr, pointer_size, so_addr))
536 {
537#if VERBOSE_OUTPUT
538 s->PutCString("), (char *)");
539 so_addr.Dump(s, exe_scope, DumpStyleLoadAddress, DumpStyleFileAddress);
540 s->PutCString(" (");
541#else
542 s->PutCString(", ");
543#endif
544 ReadCStringFromMemory (exe_scope, so_addr, s);
545 }
546#if VERBOSE_OUTPUT
547 s->PutCString(") }");
548#else
549 s->PutCString(" }");
550#endif
551 }
552 }
553 }
554 }
555 break;
556
557 case eSectionTypeDataObjCCFStrings:
558 {
559 Address cfstring_data_addr(*this);
560 cfstring_data_addr.SetOffset(cfstring_data_addr.GetOffset() + (2 * pointer_size));
561 if (ReadAddress (exe_scope, cfstring_data_addr, pointer_size, so_addr))
562 {
563#if VERBOSE_OUTPUT
564 s->PutCString("(CFString *) ");
565 cfstring_data_addr.Dump(s, exe_scope, DumpStyleLoadAddress, DumpStyleFileAddress);
566 s->PutCString(" -> @");
567#else
568 s->PutChar('@');
569#endif
570 if (so_addr.Dump(s, exe_scope, DumpStyleResolvedDescription))
571 showed_info = true;
572 }
573 }
574 break;
575
576 case eSectionTypeData4:
577 // Read the 4 byte data and display it
578 showed_info = true;
579 s->PutCString("(uint32_t) ");
580 DumpUInt (exe_scope, *this, 4, s);
581 break;
582
583 case eSectionTypeData8:
584 // Read the 8 byte data and display it
585 showed_info = true;
586 s->PutCString("(uint64_t) ");
587 DumpUInt (exe_scope, *this, 8, s);
588 break;
589
590 case eSectionTypeData16:
591 // Read the 16 byte data and display it
592 showed_info = true;
593 s->PutCString("(uint128_t) ");
594 DumpUInt (exe_scope, *this, 16, s);
595 break;
596
597 case eSectionTypeDataPointers:
598 // Read the pointer data and display it
599 {
600 if (ReadAddress (exe_scope, *this, pointer_size, so_addr))
601 {
602 s->PutCString ("(void *)");
603 so_addr.Dump(s, exe_scope, DumpStyleLoadAddress, DumpStyleFileAddress);
604
605 showed_info = true;
606 if (so_addr.IsSectionOffset())
607 {
Greg Claytonc9800662010-09-10 01:30:46 +0000608 SymbolContext pointer_sc;
Greg Claytondda4f7b2010-06-30 23:03:03 +0000609 if (target)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000610 {
Greg Claytondda4f7b2010-06-30 23:03:03 +0000611 target->GetImages().ResolveSymbolContextForAddress (so_addr,
612 eSymbolContextEverything,
613 pointer_sc);
614 if (pointer_sc.function || pointer_sc.symbol)
615 {
616 s->PutCString(": ");
Greg Clayton6dadd502010-09-02 21:44:10 +0000617 pointer_sc.DumpStopContext(s, exe_scope, so_addr, true, false, false);
Greg Claytondda4f7b2010-06-30 23:03:03 +0000618 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000619 }
620 }
621 }
622 }
623 break;
Greg Claytonc982c762010-07-09 20:39:50 +0000624
625 default:
626 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000627 }
628 }
629
630 if (!showed_info)
631 {
632 if (module)
633 {
Greg Claytonc9800662010-09-10 01:30:46 +0000634 SymbolContext sc;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000635 module->ResolveSymbolContextForAddress(*this, eSymbolContextEverything, sc);
636 if (sc.function || sc.symbol)
637 {
638 bool show_stop_context = true;
Greg Clayton8dc0a982010-09-07 21:56:53 +0000639 const bool show_module = (style == DumpStyleResolvedDescription);
640 const bool show_fullpaths = false;
Greg Clayton513c26c2011-01-29 07:10:55 +0000641 const bool show_inlined_frames = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000642 if (sc.function == NULL && sc.symbol != NULL)
643 {
644 // If we have just a symbol make sure it is in the right section
645 if (sc.symbol->GetAddressRangePtr())
646 {
647 if (sc.symbol->GetAddressRangePtr()->GetBaseAddress().GetSection() != GetSection())
Greg Clayton54b8b8c2010-07-01 01:26:43 +0000648 {
649 // don't show the module if the symbol is a trampoline symbol
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000650 show_stop_context = false;
Greg Clayton54b8b8c2010-07-01 01:26:43 +0000651 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000652 }
653 }
654 if (show_stop_context)
655 {
656 // We have a function or a symbol from the same
657 // sections as this address.
Greg Clayton8dc0a982010-09-07 21:56:53 +0000658 sc.DumpStopContext (s,
659 exe_scope,
660 *this,
661 show_fullpaths,
662 show_module,
663 show_inlined_frames);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000664 }
665 else
666 {
667 // We found a symbol but it was in a different
668 // section so it isn't the symbol we should be
669 // showing, just show the section name + offset
670 Dump (s, exe_scope, DumpStyleSectionNameOffset);
671 }
672 }
673 }
674 }
675 }
676 else
677 {
678 if (fallback_style != DumpStyleInvalid)
Greg Claytondda4f7b2010-06-30 23:03:03 +0000679 return Dump (s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000680 return false;
681 }
682 break;
Greg Claytondda4f7b2010-06-30 23:03:03 +0000683
684 case DumpStyleDetailedSymbolContext:
685 if (IsSectionOffset())
686 {
Greg Claytonc9800662010-09-10 01:30:46 +0000687 Module *module = GetModule();
Greg Claytondda4f7b2010-06-30 23:03:03 +0000688 if (module)
689 {
Greg Claytonc9800662010-09-10 01:30:46 +0000690 SymbolContext sc;
Greg Claytondda4f7b2010-06-30 23:03:03 +0000691 module->ResolveSymbolContextForAddress(*this, eSymbolContextEverything, sc);
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000692 if (sc.symbol)
Greg Claytondda4f7b2010-06-30 23:03:03 +0000693 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000694 // If we have just a symbol make sure it is in the same section
695 // as our address. If it isn't, then we might have just found
696 // the last symbol that came before the address that we are
697 // looking up that has nothing to do with our address lookup.
698 if (sc.symbol->GetAddressRangePtr() && sc.symbol->GetAddressRangePtr()->GetBaseAddress().GetSection() != GetSection())
699 sc.symbol = NULL;
Greg Claytondda4f7b2010-06-30 23:03:03 +0000700 }
Greg Claytonf5e56de2010-09-14 23:36:40 +0000701 sc.GetDescription(s, eDescriptionLevelBrief, target);
Greg Claytondda4f7b2010-06-30 23:03:03 +0000702 }
703 }
704 if (fallback_style != DumpStyleInvalid)
705 return Dump (s, exe_scope, fallback_style, DumpStyleInvalid, addr_size);
706 return false;
707 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000708 }
709
710 return true;
711}
712
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000713void
714Address::CalculateSymbolContext (SymbolContext *sc)
715{
716 sc->Clear();
717 // Absolute addresses don't have enough information to reconstruct even their target.
718 if (m_section == NULL)
719 return;
720
721 if (m_section->GetModule())
722 {
723 sc->module_sp = m_section->GetModule()->GetSP();
724 if (sc->module_sp)
725 sc->module_sp->ResolveSymbolContextForAddress (*this, eSymbolContextEverything, *sc);
726 }
727}
728
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000729int
730Address::CompareFileAddress (const Address& a, const Address& b)
731{
732 addr_t a_file_addr = a.GetFileAddress();
733 addr_t b_file_addr = b.GetFileAddress();
734 if (a_file_addr < b_file_addr)
735 return -1;
736 if (a_file_addr > b_file_addr)
737 return +1;
738 return 0;
739}
740
741
742int
Greg Claytonf5e56de2010-09-14 23:36:40 +0000743Address::CompareLoadAddress (const Address& a, const Address& b, Target *target)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000744{
Greg Claytonf5e56de2010-09-14 23:36:40 +0000745 assert (target != NULL);
746 addr_t a_load_addr = a.GetLoadAddress (target);
747 addr_t b_load_addr = b.GetLoadAddress (target);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000748 if (a_load_addr < b_load_addr)
749 return -1;
750 if (a_load_addr > b_load_addr)
751 return +1;
752 return 0;
753}
754
755int
756Address::CompareModulePointerAndOffset (const Address& a, const Address& b)
757{
758 Module *a_module = a.GetModule ();
759 Module *b_module = b.GetModule ();
760 if (a_module < b_module)
761 return -1;
762 if (a_module > b_module)
763 return +1;
764 // Modules are the same, just compare the file address since they should
765 // be unique
766 addr_t a_file_addr = a.GetFileAddress();
767 addr_t b_file_addr = b.GetFileAddress();
768 if (a_file_addr < b_file_addr)
769 return -1;
770 if (a_file_addr > b_file_addr)
771 return +1;
772 return 0;
773}
774
775
776size_t
777Address::MemorySize () const
778{
779 // Noting special for the memory size of a single Address object,
780 // it is just the size of itself.
781 return sizeof(Address);
782}
783
784
Greg Claytonb0848c52011-01-08 00:05:12 +0000785//----------------------------------------------------------------------
786// NOTE: Be careful using this operator. It can correctly compare two
787// addresses from the same Module correctly. It can't compare two
788// addresses from different modules in any meaningful way, but it will
789// compare the module pointers.
790//
791// To sum things up:
792// - works great for addresses within the same module
793// - it works for addresses across multiple modules, but don't expect the
794// address results to make much sense
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000795//
Greg Claytonb0848c52011-01-08 00:05:12 +0000796// This basically lets Address objects be used in ordered collection
797// classes.
798//----------------------------------------------------------------------
799
800bool
801lldb_private::operator< (const Address& lhs, const Address& rhs)
802{
803 Module *lhs_module = lhs.GetModule();
804 Module *rhs_module = rhs.GetModule();
805 if (lhs_module == rhs_module)
806 {
807 // Addresses are in the same module, just compare the file addresses
808 return lhs.GetFileAddress() < rhs.GetFileAddress();
809 }
810 else
811 {
812 // The addresses are from different modules, just use the module
813 // pointer value to get consistent ordering
814 return lhs_module < rhs_module;
815 }
816}
817
818bool
819lldb_private::operator> (const Address& lhs, const Address& rhs)
820{
821 Module *lhs_module = lhs.GetModule();
822 Module *rhs_module = rhs.GetModule();
823 if (lhs_module == rhs_module)
824 {
825 // Addresses are in the same module, just compare the file addresses
826 return lhs.GetFileAddress() > rhs.GetFileAddress();
827 }
828 else
829 {
830 // The addresses are from different modules, just use the module
831 // pointer value to get consistent ordering
832 return lhs_module > rhs_module;
833 }
834}
835
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000836
837// The operator == checks for exact equality only (same section, same offset)
838bool
839lldb_private::operator== (const Address& a, const Address& rhs)
840{
841 return a.GetSection() == rhs.GetSection() &&
842 a.GetOffset() == rhs.GetOffset();
843}
844// The operator != checks for exact inequality only (differing section, or
845// different offset)
846bool
847lldb_private::operator!= (const Address& a, const Address& rhs)
848{
849 return a.GetSection() != rhs.GetSection() ||
850 a.GetOffset() != rhs.GetOffset();
851}
852
853bool
854Address::IsLinkedAddress () const
855{
856 return m_section && m_section->GetLinkedSection();
857}
858
859
860void
861Address::ResolveLinkedAddress ()
862{
863 if (m_section)
864 {
865 const Section *linked_section = m_section->GetLinkedSection();
866 if (linked_section)
867 {
868 m_offset += m_section->GetLinkedOffset();
869 m_section = linked_section;
870 }
871 }
872}
Greg Claytonded470d2011-03-19 01:12:21 +0000873
Greg Claytone0d378b2011-03-24 21:19:54 +0000874AddressClass
Greg Claytonded470d2011-03-19 01:12:21 +0000875Address::GetAddressClass () const
876{
877 Module *module = GetModule();
878 if (module)
879 {
880 ObjectFile *obj_file = module->GetObjectFile();
881 if (obj_file)
882 return obj_file->GetAddressClass (GetFileAddress());
883 }
884 return eAddressClassUnknown;
885}
Greg Claytoncd482e32011-05-18 01:58:14 +0000886
887bool
888Address::SetLoadAddress (lldb::addr_t load_addr, Target *target)
889{
890 if (target && target->GetSectionLoadList().ResolveLoadAddress(load_addr, *this))
891 return true;
892 m_section = NULL;
893 m_offset = load_addr;
894 return false;
895}
896