blob: 74bb65f21ee77a19233cb205b40374d7be57bab7 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- Symtab.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 <map>
11
12#include "lldb/Core/Module.h"
13#include "lldb/Core/RegularExpression.h"
14#include "lldb/Core/Timer.h"
15#include "lldb/Symbol/ObjectFile.h"
16#include "lldb/Symbol/Symtab.h"
17
18using namespace lldb;
19using namespace lldb_private;
20
21
22
23Symtab::Symtab(ObjectFile *objfile) :
24 m_objfile(objfile),
25 m_symbols(),
26 m_addr_indexes(),
27 m_name_to_index()
28{
29}
30
31Symtab::~Symtab()
32{
33}
34
35void
36Symtab::Reserve(uint32_t count)
37{
38 m_symbols.reserve (count);
39}
40
41Symbol *
42Symtab::Resize(uint32_t count)
43{
44 m_symbols.resize (count);
45 return &m_symbols[0];
46}
47
48uint32_t
49Symtab::AddSymbol(const Symbol& symbol)
50{
51 uint32_t symbol_idx = m_symbols.size();
52 m_name_to_index.Clear();
53 m_addr_indexes.clear();
54 m_symbols.push_back(symbol);
55 return symbol_idx;
56}
57
58size_t
59Symtab::GetNumSymbols() const
60{
61 return m_symbols.size();
62}
63
64void
65Symtab::Dump(Stream *s, Process *process) const
66{
67 const_iterator pos;
68 s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
69 s->Indent();
70 const FileSpec &file_spec = m_objfile->GetFileSpec();
71 const char * object_name = NULL;
72 if (m_objfile->GetModule())
73 object_name = m_objfile->GetModule()->GetObjectName().GetCString();
74
75 if (file_spec)
76 s->Printf("Symtab, file = %s/%s%s%s%s, num_symbols = %u:\n",
77 file_spec.GetDirectory().AsCString(),
78 file_spec.GetFilename().AsCString(),
79 object_name ? "(" : "",
80 object_name ? object_name : "",
81 object_name ? ")" : "",
82 m_symbols.size());
83 else
84 s->Printf("Symtab, num_symbols = %u:\n", m_symbols.size());
85 s->IndentMore();
86
87 if (!m_symbols.empty())
88 {
89 const_iterator begin = m_symbols.begin();
90 const_iterator end = m_symbols.end();
91 DumpSymbolHeader (s);
92 for (pos = m_symbols.begin(); pos != end; ++pos)
93 {
94 s->Indent();
95 pos->Dump(s, process, std::distance(begin, pos));
96 }
97 }
98 s->IndentLess ();
99}
100
101void
102Symtab::Dump(Stream *s, Process *process, std::vector<uint32_t>& indexes) const
103{
104 const size_t num_symbols = GetNumSymbols();
105 s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
106 s->Indent();
107 s->Printf("Symtab %u symbol indexes (%u symbols total):\n", indexes.size(), m_symbols.size());
108 s->IndentMore();
109
110 if (!indexes.empty())
111 {
112 std::vector<uint32_t>::const_iterator pos;
113 std::vector<uint32_t>::const_iterator end = indexes.end();
114 DumpSymbolHeader (s);
115 for (pos = indexes.begin(); pos != end; ++pos)
116 {
117 uint32_t idx = *pos;
118 if (idx < num_symbols)
119 {
120 s->Indent();
121 m_symbols[idx].Dump(s, process, idx);
122 }
123 }
124 }
125 s->IndentLess ();
126}
127
128void
129Symtab::DumpSymbolHeader (Stream *s)
130{
131 s->Indent(" Debug symbol\n");
132 s->Indent(" |Synthetic symbol\n");
133 s->Indent(" ||Externally Visible\n");
134 s->Indent(" |||\n");
135 s->Indent("Index UserID DSX Type File Address/Value Load Address Size Flags Name\n");
136 s->Indent("------- ------ --- ------------ ------------------ ------------------ ------------------ ---------- ----------------------------------\n");
137}
138
Greg Clayton0ad086f2010-09-07 17:36:17 +0000139
140static int
141CompareSymbolID (const void *key, const void *p)
142{
143 const user_id_t match_uid = *(user_id_t*) key;
144 const user_id_t symbol_uid = ((Symbol *)p)->GetID();
145 if (match_uid < symbol_uid)
146 return -1;
147 if (match_uid > symbol_uid)
148 return 1;
149 return 0;
150}
151
152Symbol *
153Symtab::FindSymbolByID (lldb::user_id_t symbol_uid) const
154{
155 Symbol *symbol = (Symbol*)::bsearch (&symbol_uid,
156 &m_symbols[0],
157 m_symbols.size(),
158 sizeof(Symbol),
159 CompareSymbolID);
160 return symbol;
161}
162
163
Chris Lattner24943d22010-06-08 16:52:24 +0000164Symbol *
165Symtab::SymbolAtIndex(uint32_t idx)
166{
167 if (idx < m_symbols.size())
168 return &m_symbols[idx];
169 return NULL;
170}
171
172
173const Symbol *
174Symtab::SymbolAtIndex(uint32_t idx) const
175{
176 if (idx < m_symbols.size())
177 return &m_symbols[idx];
178 return NULL;
179}
180
181//----------------------------------------------------------------------
182// InitNameIndexes
183//----------------------------------------------------------------------
184void
185Symtab::InitNameIndexes()
186{
187 Timer scoped_timer (__PRETTY_FUNCTION__, "%s", __PRETTY_FUNCTION__);
188 // Create the name index vector to be able to quickly search by name
189 const size_t count = m_symbols.size();
190 assert(m_objfile != NULL);
191 assert(m_objfile->GetModule() != NULL);
192 m_name_to_index.Reserve (count);
193
194 UniqueCStringMap<uint32_t>::Entry entry;
195
196 for (entry.value = 0; entry.value < count; ++entry.value)
197 {
198 const Symbol *symbol = &m_symbols[entry.value];
199
200 // Don't let trampolines get into the lookup by name map
201 // If we ever need the trampoline symbols to be searchable by name
202 // we can remove this and then possibly add a new bool to any of the
203 // Symtab functions that lookup symbols by name to indicate if they
204 // want trampolines.
205 if (symbol->IsTrampoline())
206 continue;
207
208 const Mangled &mangled = symbol->GetMangled();
209 entry.cstring = mangled.GetMangledName().GetCString();
210 if (entry.cstring && entry.cstring[0])
211 m_name_to_index.Append (entry);
212
213 entry.cstring = mangled.GetDemangledName().GetCString();
214 if (entry.cstring && entry.cstring[0])
215 m_name_to_index.Append (entry);
216 }
217 m_name_to_index.Sort();
218}
219
220uint32_t
221Symtab::AppendSymbolIndexesWithType(SymbolType symbol_type, std::vector<uint32_t>& indexes, uint32_t start_idx, uint32_t end_index) const
222{
223 uint32_t prev_size = indexes.size();
224
225 const uint32_t count = std::min<uint32_t> (m_symbols.size(), end_index);
226
227 for (uint32_t i = start_idx; i < count; ++i)
228 {
229 if (symbol_type == eSymbolTypeAny || m_symbols[i].GetType() == symbol_type)
230 indexes.push_back(i);
231 }
232
233 return indexes.size() - prev_size;
234}
235
236struct SymbolSortInfo
237{
238 const bool sort_by_load_addr;
239 const Symbol *symbols;
240};
241
Owen Andersonc3d68552010-06-16 17:34:05 +0000242namespace {
243 struct SymbolIndexComparator {
244 const std::vector<Symbol>& symbols;
245 SymbolIndexComparator(const std::vector<Symbol>& s) : symbols(s) { }
246 bool operator()(uint32_t index_a, uint32_t index_b) {
247 addr_t value_a;
248 addr_t value_b;
249 if (symbols[index_a].GetValue().GetSection() == symbols[index_b].GetValue().GetSection()) {
250 value_a = symbols[index_a].GetValue ().GetOffset();
251 value_b = symbols[index_b].GetValue ().GetOffset();
252 } else {
253 value_a = symbols[index_a].GetValue ().GetFileAddress();
254 value_b = symbols[index_b].GetValue ().GetFileAddress();
255 }
Chris Lattner24943d22010-06-08 16:52:24 +0000256
Owen Andersonc3d68552010-06-16 17:34:05 +0000257 if (value_a == value_b) {
258 // The if the values are equal, use the original symbol user ID
259 lldb::user_id_t uid_a = symbols[index_a].GetID();
260 lldb::user_id_t uid_b = symbols[index_b].GetID();
261 if (uid_a < uid_b)
262 return true;
263 if (uid_a > uid_b)
264 return false;
265 return false;
266 } else if (value_a < value_b)
267 return true;
268
269 return false;
270 }
271 };
Eli Friedman020f3532010-06-10 23:36:31 +0000272}
273
Chris Lattner24943d22010-06-08 16:52:24 +0000274void
275Symtab::SortSymbolIndexesByValue (std::vector<uint32_t>& indexes, bool remove_duplicates) const
276{
Owen Andersonc3d68552010-06-16 17:34:05 +0000277 Timer scoped_timer (__PRETTY_FUNCTION__,__PRETTY_FUNCTION__);
Chris Lattner24943d22010-06-08 16:52:24 +0000278 // No need to sort if we have zero or one items...
279 if (indexes.size() <= 1)
280 return;
281
Owen Andersona1e14752010-06-17 00:51:12 +0000282 // Sort the indexes in place using std::stable_sort.
283 // NOTE: The use of std::stable_sort instead of std::sort here is strictly for performance,
284 // not correctness. The indexes vector tends to be "close" to sorted, which the
285 // stable sort handles better.
Owen Andersonc3d68552010-06-16 17:34:05 +0000286 std::stable_sort(indexes.begin(), indexes.end(), SymbolIndexComparator(m_symbols));
Chris Lattner24943d22010-06-08 16:52:24 +0000287
288 // Remove any duplicates if requested
289 if (remove_duplicates)
290 std::unique(indexes.begin(), indexes.end());
291}
292
293uint32_t
294Symtab::AppendSymbolIndexesWithName(const ConstString& symbol_name, std::vector<uint32_t>& indexes)
295{
296 Timer scoped_timer (__PRETTY_FUNCTION__, "%s", __PRETTY_FUNCTION__);
297 if (symbol_name)
298 {
299 const size_t old_size = indexes.size();
300 if (m_name_to_index.IsEmpty())
301 InitNameIndexes();
302
303 const char *symbol_cstr = symbol_name.GetCString();
304 const UniqueCStringMap<uint32_t>::Entry *entry_ptr;
305 for (entry_ptr = m_name_to_index.FindFirstValueForName (symbol_cstr);
306 entry_ptr!= NULL;
307 entry_ptr = m_name_to_index.FindNextValueForName (symbol_cstr, entry_ptr))
308 {
309 indexes.push_back (entry_ptr->value);
310 }
311 return indexes.size() - old_size;
312 }
313 return 0;
314}
315
316uint32_t
317Symtab::AppendSymbolIndexesWithNameAndType(const ConstString& symbol_name, SymbolType symbol_type, std::vector<uint32_t>& indexes)
318{
319 if (AppendSymbolIndexesWithName(symbol_name, indexes) > 0)
320 {
321 std::vector<uint32_t>::iterator pos = indexes.begin();
322 while (pos != indexes.end())
323 {
324 if (symbol_type == eSymbolTypeAny || m_symbols[*pos].GetType() == symbol_type)
325 ++pos;
326 else
327 indexes.erase(pos);
328 }
329 }
330 return indexes.size();
331}
332
333uint32_t
334Symtab::AppendSymbolIndexesMatchingRegExAndType (const RegularExpression &regexp, SymbolType symbol_type, std::vector<uint32_t>& indexes)
335{
336 uint32_t prev_size = indexes.size();
337 uint32_t sym_end = m_symbols.size();
338
339 for (int i = 0; i < sym_end; i++)
340 {
341 if (symbol_type == eSymbolTypeAny || m_symbols[i].GetType() == symbol_type)
342 {
343 const char *name = m_symbols[i].GetMangled().GetName().AsCString();
344 if (name)
345 {
346 if (regexp.Execute (name))
347 indexes.push_back(i);
348 }
349 }
350 }
351 return indexes.size() - prev_size;
352
353}
354
355Symbol *
356Symtab::FindSymbolWithType(SymbolType symbol_type, uint32_t& start_idx)
357{
358 const size_t count = m_symbols.size();
359 for (uint32_t idx = start_idx; idx < count; ++idx)
360 {
361 if (symbol_type == eSymbolTypeAny || m_symbols[idx].GetType() == symbol_type)
362 {
363 start_idx = idx;
364 return &m_symbols[idx];
365 }
366 }
367 return NULL;
368}
369
370const Symbol *
371Symtab::FindSymbolWithType(SymbolType symbol_type, uint32_t& start_idx) const
372{
373 const size_t count = m_symbols.size();
374 for (uint32_t idx = start_idx; idx < count; ++idx)
375 {
376 if (symbol_type == eSymbolTypeAny || m_symbols[idx].GetType() == symbol_type)
377 {
378 start_idx = idx;
379 return &m_symbols[idx];
380 }
381 }
382 return NULL;
383}
384
385size_t
386Symtab::FindAllSymbolsWithNameAndType (const ConstString &name, SymbolType symbol_type, std::vector<uint32_t>& symbol_indexes)
387{
388 Timer scoped_timer (__PRETTY_FUNCTION__, "%s", __PRETTY_FUNCTION__);
389 // Initialize all of the lookup by name indexes before converting NAME
390 // to a uniqued string NAME_STR below.
391 if (m_name_to_index.IsEmpty())
392 InitNameIndexes();
393
394 if (name)
395 {
396 // The string table did have a string that matched, but we need
397 // to check the symbols and match the symbol_type if any was given.
398 AppendSymbolIndexesWithNameAndType(name, symbol_type, symbol_indexes);
399 }
400 return symbol_indexes.size();
401}
402
403size_t
404Symtab::FindAllSymbolsMatchingRexExAndType (const RegularExpression &regex, SymbolType symbol_type, std::vector<uint32_t>& symbol_indexes)
405{
406 AppendSymbolIndexesMatchingRegExAndType(regex, symbol_type, symbol_indexes);
407 return symbol_indexes.size();
408}
409
410Symbol *
411Symtab::FindFirstSymbolWithNameAndType (const ConstString &name, SymbolType symbol_type)
412{
413 Timer scoped_timer (__PRETTY_FUNCTION__, "%s", __PRETTY_FUNCTION__);
414 if (m_name_to_index.IsEmpty())
415 InitNameIndexes();
416
417 if (name)
418 {
419 std::vector<uint32_t> matching_indexes;
420 // The string table did have a string that matched, but we need
421 // to check the symbols and match the symbol_type if any was given.
422 if (AppendSymbolIndexesWithNameAndType(name, symbol_type, matching_indexes))
423 {
424 std::vector<uint32_t>::const_iterator pos, end = matching_indexes.end();
425 for (pos = matching_indexes.begin(); pos != end; ++pos)
426 {
427 Symbol *symbol = SymbolAtIndex(*pos);
428
429 if (symbol->Compare(name, symbol_type))
430 return symbol;
431 }
432 }
433 }
434 return NULL;
435}
436
437typedef struct
438{
439 const Symtab *symtab;
440 const addr_t file_addr;
441 Symbol *match_symbol;
442 const uint32_t *match_index_ptr;
443 addr_t match_offset;
444} SymbolSearchInfo;
445
446static int
447SymbolWithFileAddress (SymbolSearchInfo *info, const uint32_t *index_ptr)
448{
449 const Symbol *curr_symbol = info->symtab->SymbolAtIndex (index_ptr[0]);
450 if (curr_symbol == NULL)
451 return -1;
452
453 const addr_t info_file_addr = info->file_addr;
454
455 // lldb::Symbol::GetAddressRangePtr() will only return a non NULL address
456 // range if the symbol has a section!
457 const AddressRange *curr_range = curr_symbol->GetAddressRangePtr();
458 if (curr_range)
459 {
460 const addr_t curr_file_addr = curr_range->GetBaseAddress().GetFileAddress();
461 if (info_file_addr < curr_file_addr)
462 return -1;
463 if (info_file_addr > curr_file_addr)
464 return +1;
465 info->match_symbol = const_cast<Symbol *>(curr_symbol);
466 info->match_index_ptr = index_ptr;
467 return 0;
468 }
469
470 return -1;
471}
472
473static int
474SymbolWithClosestFileAddress (SymbolSearchInfo *info, const uint32_t *index_ptr)
475{
476 const Symbol *symbol = info->symtab->SymbolAtIndex (index_ptr[0]);
477 if (symbol == NULL)
478 return -1;
479
480 const addr_t info_file_addr = info->file_addr;
481 const AddressRange *curr_range = symbol->GetAddressRangePtr();
482 if (curr_range)
483 {
484 const addr_t curr_file_addr = curr_range->GetBaseAddress().GetFileAddress();
485 if (info_file_addr < curr_file_addr)
486 return -1;
487
488 // Since we are finding the closest symbol that is greater than or equal
489 // to 'info->file_addr' we set the symbol here. This will get set
490 // multiple times, but after the search is done it will contain the best
491 // symbol match
492 info->match_symbol = const_cast<Symbol *>(symbol);
493 info->match_index_ptr = index_ptr;
494 info->match_offset = info_file_addr - curr_file_addr;
495
496 if (info_file_addr > curr_file_addr)
497 return +1;
498 return 0;
499 }
500 return -1;
501}
502
503static SymbolSearchInfo
504FindIndexPtrForSymbolContainingAddress(Symtab* symtab, addr_t file_addr, const uint32_t* indexes, uint32_t num_indexes)
505{
506 SymbolSearchInfo info = { symtab, file_addr, NULL, NULL, 0 };
507 bsearch(&info, indexes, num_indexes, sizeof(uint32_t), (comparison_function)SymbolWithClosestFileAddress);
508 return info;
509}
510
511
512void
513Symtab::InitAddressIndexes()
514{
515 if (m_addr_indexes.empty())
516 {
517 AppendSymbolIndexesWithType (eSymbolTypeFunction, m_addr_indexes);
518 AppendSymbolIndexesWithType (eSymbolTypeGlobal, m_addr_indexes);
519 AppendSymbolIndexesWithType (eSymbolTypeStatic, m_addr_indexes);
520 AppendSymbolIndexesWithType (eSymbolTypeCode, m_addr_indexes);
521 AppendSymbolIndexesWithType (eSymbolTypeTrampoline, m_addr_indexes);
522 AppendSymbolIndexesWithType (eSymbolTypeData, m_addr_indexes);
523 SortSymbolIndexesByValue(m_addr_indexes, true);
524 m_addr_indexes.push_back(UINT32_MAX); // Terminator for bsearch since we might need to look at the next symbol
525 }
526}
527
528size_t
529Symtab::CalculateSymbolSize (Symbol *symbol)
530{
Greg Clayton53d68e72010-07-20 22:52:08 +0000531 if (m_symbols.empty())
532 return 0;
533
Chris Lattner24943d22010-06-08 16:52:24 +0000534 // Make sure this symbol is from this symbol table...
Greg Clayton53d68e72010-07-20 22:52:08 +0000535 if (symbol < &m_symbols.front() || symbol > &m_symbols.back())
Chris Lattner24943d22010-06-08 16:52:24 +0000536 return 0;
537
538 // See if this symbol already has a byte size?
539 size_t byte_size = symbol->GetByteSize();
540
541 if (byte_size)
542 {
543 // It does, just return it
544 return byte_size;
545 }
546
547 // Else if this is an address based symbol, figure out the delta between
548 // it and the next address based symbol
549 if (symbol->GetAddressRangePtr())
550 {
551 if (m_addr_indexes.empty())
552 InitAddressIndexes();
553 const size_t num_addr_indexes = m_addr_indexes.size();
Greg Clayton53d68e72010-07-20 22:52:08 +0000554 SymbolSearchInfo info = FindIndexPtrForSymbolContainingAddress(this, symbol->GetAddressRangePtr()->GetBaseAddress().GetFileAddress(), &m_addr_indexes.front(), num_addr_indexes);
Chris Lattner24943d22010-06-08 16:52:24 +0000555 if (info.match_index_ptr != NULL)
556 {
557 const lldb::addr_t curr_file_addr = symbol->GetAddressRangePtr()->GetBaseAddress().GetFileAddress();
558 // We can figure out the address range of all symbols except the
559 // last one by taking the delta between the current symbol and
560 // the next symbol
561
Greg Clayton53d68e72010-07-20 22:52:08 +0000562 for (uint32_t addr_index = info.match_index_ptr - &m_addr_indexes.front() + 1;
Chris Lattner24943d22010-06-08 16:52:24 +0000563 addr_index < num_addr_indexes;
564 ++addr_index)
565 {
566 Symbol *next_symbol = SymbolAtIndex(m_addr_indexes[addr_index]);
567 if (next_symbol == NULL)
568 break;
569
570 assert (next_symbol->GetAddressRangePtr());
571 const lldb::addr_t next_file_addr = next_symbol->GetAddressRangePtr()->GetBaseAddress().GetFileAddress();
572 if (next_file_addr > curr_file_addr)
573 {
574 byte_size = next_file_addr - curr_file_addr;
575 symbol->GetAddressRangePtr()->SetByteSize(byte_size);
576 symbol->SetSizeIsSynthesized(true);
577 break;
578 }
579 }
580 }
581 }
582 return byte_size;
583}
584
585Symbol *
586Symtab::FindSymbolWithFileAddress (addr_t file_addr)
587{
588 if (m_addr_indexes.empty())
589 InitAddressIndexes();
590
591 SymbolSearchInfo info = { this, file_addr, NULL, NULL, 0 };
592
593 uint32_t* match = (uint32_t*)bsearch(&info, &m_addr_indexes[0], m_addr_indexes.size(), sizeof(uint32_t), (comparison_function)SymbolWithFileAddress);
594 if (match)
595 return SymbolAtIndex (*match);
596 return NULL;
597}
598
599
600Symbol *
601Symtab::FindSymbolContainingFileAddress (addr_t file_addr, const uint32_t* indexes, uint32_t num_indexes)
602{
603 SymbolSearchInfo info = { this, file_addr, NULL, NULL, 0 };
604
605 bsearch(&info, indexes, num_indexes, sizeof(uint32_t), (comparison_function)SymbolWithClosestFileAddress);
606
607 if (info.match_symbol)
608 {
609 if (info.match_offset < CalculateSymbolSize(info.match_symbol))
610 return info.match_symbol;
611 }
612 return NULL;
613}
614
615Symbol *
616Symtab::FindSymbolContainingFileAddress (addr_t file_addr)
617{
618 if (m_addr_indexes.empty())
619 InitAddressIndexes();
620
621 return FindSymbolContainingFileAddress (file_addr, &m_addr_indexes[0], m_addr_indexes.size());
622}
623