1 //===-- Section.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/Section.h"
11 #include "lldb/Core/Module.h"
12 #include "lldb/Symbol/ObjectFile.h"
13 #include "lldb/Target/SectionLoadList.h"
14 #include "lldb/Target/Target.h"
15
16 using namespace lldb;
17 using namespace lldb_private;
18
Section(const ModuleSP & module_sp,ObjectFile * obj_file,user_id_t sect_id,const ConstString & name,SectionType sect_type,addr_t file_addr,addr_t byte_size,lldb::offset_t file_offset,lldb::offset_t file_size,uint32_t flags)19 Section::Section (const ModuleSP &module_sp,
20 ObjectFile *obj_file,
21 user_id_t sect_id,
22 const ConstString &name,
23 SectionType sect_type,
24 addr_t file_addr,
25 addr_t byte_size,
26 lldb::offset_t file_offset,
27 lldb::offset_t file_size,
28 uint32_t flags) :
29 ModuleChild (module_sp),
30 UserID (sect_id),
31 Flags (flags),
32 m_obj_file (obj_file),
33 m_type (sect_type),
34 m_parent_wp (),
35 m_name (name),
36 m_file_addr (file_addr),
37 m_byte_size (byte_size),
38 m_file_offset (file_offset),
39 m_file_size (file_size),
40 m_children (),
41 m_fake (false),
42 m_encrypted (false),
43 m_thread_specific (false)
44 {
45 // printf ("Section::Section(%p): module=%p, sect_id = 0x%16.16" PRIx64 ", addr=[0x%16.16" PRIx64 " - 0x%16.16" PRIx64 "), file [0x%16.16" PRIx64 " - 0x%16.16" PRIx64 "), flags = 0x%8.8x, name = %s\n",
46 // this, module_sp.get(), sect_id, file_addr, file_addr + byte_size, file_offset, file_offset + file_size, flags, name.GetCString());
47 }
48
Section(const lldb::SectionSP & parent_section_sp,const ModuleSP & module_sp,ObjectFile * obj_file,user_id_t sect_id,const ConstString & name,SectionType sect_type,addr_t file_addr,addr_t byte_size,lldb::offset_t file_offset,lldb::offset_t file_size,uint32_t flags)49 Section::Section (const lldb::SectionSP &parent_section_sp,
50 const ModuleSP &module_sp,
51 ObjectFile *obj_file,
52 user_id_t sect_id,
53 const ConstString &name,
54 SectionType sect_type,
55 addr_t file_addr,
56 addr_t byte_size,
57 lldb::offset_t file_offset,
58 lldb::offset_t file_size,
59 uint32_t flags) :
60 ModuleChild (module_sp),
61 UserID (sect_id),
62 Flags (flags),
63 m_obj_file (obj_file),
64 m_type (sect_type),
65 m_parent_wp (),
66 m_name (name),
67 m_file_addr (file_addr),
68 m_byte_size (byte_size),
69 m_file_offset (file_offset),
70 m_file_size (file_size),
71 m_children (),
72 m_fake (false),
73 m_encrypted (false),
74 m_thread_specific (false)
75 {
76 // printf ("Section::Section(%p): module=%p, sect_id = 0x%16.16" PRIx64 ", addr=[0x%16.16" PRIx64 " - 0x%16.16" PRIx64 "), file [0x%16.16" PRIx64 " - 0x%16.16" PRIx64 "), flags = 0x%8.8x, name = %s.%s\n",
77 // this, module_sp.get(), sect_id, file_addr, file_addr + byte_size, file_offset, file_offset + file_size, flags, parent_section_sp->GetName().GetCString(), name.GetCString());
78 if (parent_section_sp)
79 m_parent_wp = parent_section_sp;
80 }
81
~Section()82 Section::~Section()
83 {
84 // printf ("Section::~Section(%p)\n", this);
85 }
86
87 addr_t
GetFileAddress() const88 Section::GetFileAddress () const
89 {
90 SectionSP parent_sp (GetParent ());
91 if (parent_sp)
92 {
93 // This section has a parent which means m_file_addr is an offset into
94 // the parent section, so the file address for this section is the file
95 // address of the parent plus the offset
96 return parent_sp->GetFileAddress() + m_file_addr;
97 }
98 // This section has no parent, so m_file_addr is the file base address
99 return m_file_addr;
100 }
101
102 bool
SetFileAddress(lldb::addr_t file_addr)103 Section::SetFileAddress (lldb::addr_t file_addr)
104 {
105 SectionSP parent_sp (GetParent ());
106 if (parent_sp)
107 {
108 if (m_file_addr >= file_addr)
109 return parent_sp->SetFileAddress (m_file_addr - file_addr);
110 return false;
111 }
112 else
113 {
114 // This section has no parent, so m_file_addr is the file base address
115 m_file_addr = file_addr;
116 return true;
117 }
118 }
119
120 lldb::addr_t
GetOffset() const121 Section::GetOffset () const
122 {
123 // This section has a parent which means m_file_addr is an offset.
124 SectionSP parent_sp (GetParent ());
125 if (parent_sp)
126 return m_file_addr;
127
128 // This section has no parent, so there is no offset to be had
129 return 0;
130 }
131
132 addr_t
GetLoadBaseAddress(Target * target) const133 Section::GetLoadBaseAddress (Target *target) const
134 {
135 addr_t load_base_addr = LLDB_INVALID_ADDRESS;
136 SectionSP parent_sp (GetParent ());
137 if (parent_sp)
138 {
139 load_base_addr = parent_sp->GetLoadBaseAddress (target);
140 if (load_base_addr != LLDB_INVALID_ADDRESS)
141 load_base_addr += GetOffset();
142 }
143 else
144 {
145 load_base_addr = target->GetSectionLoadList().GetSectionLoadAddress (const_cast<Section *>(this)->shared_from_this());
146 }
147 return load_base_addr;
148 }
149
150 bool
ResolveContainedAddress(addr_t offset,Address & so_addr) const151 Section::ResolveContainedAddress (addr_t offset, Address &so_addr) const
152 {
153 const size_t num_children = m_children.GetSize();
154 if (num_children > 0)
155 {
156 for (size_t i=0; i<num_children; i++)
157 {
158 Section* child_section = m_children.GetSectionAtIndex (i).get();
159
160 addr_t child_offset = child_section->GetOffset();
161 if (child_offset <= offset && offset - child_offset < child_section->GetByteSize())
162 return child_section->ResolveContainedAddress (offset - child_offset, so_addr);
163 }
164 }
165 so_addr.SetOffset(offset);
166 so_addr.SetSection(const_cast<Section *>(this)->shared_from_this());
167
168 #ifdef LLDB_CONFIGURATION_DEBUG
169 // For debug builds, ensure that there are no orphaned (i.e., moduleless) sections.
170 assert(GetModule().get());
171 #endif
172 return true;
173 }
174
175 bool
ContainsFileAddress(addr_t vm_addr) const176 Section::ContainsFileAddress (addr_t vm_addr) const
177 {
178 const addr_t file_addr = GetFileAddress();
179 if (file_addr != LLDB_INVALID_ADDRESS)
180 {
181 if (file_addr <= vm_addr)
182 {
183 const addr_t offset = vm_addr - file_addr;
184 return offset < GetByteSize();
185 }
186 }
187 return false;
188 }
189
190 int
Compare(const Section & a,const Section & b)191 Section::Compare (const Section& a, const Section& b)
192 {
193 if (&a == &b)
194 return 0;
195
196 const ModuleSP a_module_sp = a.GetModule();
197 const ModuleSP b_module_sp = b.GetModule();
198 if (a_module_sp == b_module_sp)
199 {
200 user_id_t a_sect_uid = a.GetID();
201 user_id_t b_sect_uid = b.GetID();
202 if (a_sect_uid < b_sect_uid)
203 return -1;
204 if (a_sect_uid > b_sect_uid)
205 return 1;
206 return 0;
207 }
208 else
209 {
210 // The modules are different, just compare the module pointers
211 if (a_module_sp.get() < b_module_sp.get())
212 return -1;
213 else
214 return 1; // We already know the modules aren't equal
215 }
216 }
217
218
219 void
Dump(Stream * s,Target * target,uint32_t depth) const220 Section::Dump (Stream *s, Target *target, uint32_t depth) const
221 {
222 // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
223 s->Indent();
224 s->Printf("0x%8.8" PRIx64 " %-16s ", GetID(), GetSectionTypeAsCString (m_type));
225 bool resolved = true;
226 addr_t addr = LLDB_INVALID_ADDRESS;
227
228 if (GetByteSize() == 0)
229 s->Printf("%39s", "");
230 else
231 {
232 if (target)
233 addr = GetLoadBaseAddress (target);
234
235 if (addr == LLDB_INVALID_ADDRESS)
236 {
237 if (target)
238 resolved = false;
239 addr = GetFileAddress();
240 }
241
242 VMRange range(addr, addr + m_byte_size);
243 range.Dump (s, 0);
244 }
245
246 s->Printf("%c 0x%8.8" PRIx64 " 0x%8.8" PRIx64 " 0x%8.8x ", resolved ? ' ' : '*', m_file_offset, m_file_size, Get());
247
248 DumpName (s);
249
250 s->EOL();
251
252 if (depth > 0)
253 m_children.Dump(s, target, false, depth - 1);
254 }
255
256 void
DumpName(Stream * s) const257 Section::DumpName (Stream *s) const
258 {
259 SectionSP parent_sp (GetParent ());
260 if (parent_sp)
261 {
262 parent_sp->DumpName (s);
263 s->PutChar('.');
264 }
265 else
266 {
267 // The top most section prints the module basename
268 const char * name = NULL;
269 ModuleSP module_sp (GetModule());
270 const FileSpec &file_spec = m_obj_file->GetFileSpec();
271
272 if (m_obj_file)
273 name = file_spec.GetFilename().AsCString();
274 if ((!name || !name[0]) && module_sp)
275 name = module_sp->GetFileSpec().GetFilename().AsCString();
276 if (name && name[0])
277 s->Printf("%s.", name);
278 }
279 m_name.Dump(s);
280 }
281
282 bool
IsDescendant(const Section * section)283 Section::IsDescendant (const Section *section)
284 {
285 if (this == section)
286 return true;
287 SectionSP parent_sp (GetParent ());
288 if (parent_sp)
289 return parent_sp->IsDescendant (section);
290 return false;
291 }
292
293 bool
Slide(addr_t slide_amount,bool slide_children)294 Section::Slide (addr_t slide_amount, bool slide_children)
295 {
296 if (m_file_addr != LLDB_INVALID_ADDRESS)
297 {
298 if (slide_amount == 0)
299 return true;
300
301 m_file_addr += slide_amount;
302
303 if (slide_children)
304 m_children.Slide (slide_amount, slide_children);
305
306 return true;
307 }
308 return false;
309 }
310
311 #pragma mark SectionList
312
SectionList()313 SectionList::SectionList () :
314 m_sections()
315 {
316 }
317
318
~SectionList()319 SectionList::~SectionList ()
320 {
321 }
322
323 SectionList &
operator =(const SectionList & rhs)324 SectionList::operator = (const SectionList& rhs)
325 {
326 if (this != &rhs)
327 m_sections = rhs.m_sections;
328 return *this;
329 }
330
331 size_t
AddSection(const lldb::SectionSP & section_sp)332 SectionList::AddSection (const lldb::SectionSP& section_sp)
333 {
334 assert (section_sp.get());
335 size_t section_index = m_sections.size();
336 m_sections.push_back(section_sp);
337 return section_index;
338 }
339
340 // Warning, this can be slow as it's removing items from a std::vector.
341 bool
DeleteSection(size_t idx)342 SectionList::DeleteSection (size_t idx)
343 {
344 if (idx < m_sections.size())
345 {
346 m_sections.erase (m_sections.begin() + idx);
347 return true;
348 }
349 return false;
350 }
351
352 size_t
FindSectionIndex(const Section * sect)353 SectionList::FindSectionIndex (const Section* sect)
354 {
355 iterator sect_iter;
356 iterator begin = m_sections.begin();
357 iterator end = m_sections.end();
358 for (sect_iter = begin; sect_iter != end; ++sect_iter)
359 {
360 if (sect_iter->get() == sect)
361 {
362 // The secton was already in this section list
363 return std::distance (begin, sect_iter);
364 }
365 }
366 return UINT32_MAX;
367 }
368
369 size_t
AddUniqueSection(const lldb::SectionSP & sect_sp)370 SectionList::AddUniqueSection (const lldb::SectionSP& sect_sp)
371 {
372 size_t sect_idx = FindSectionIndex (sect_sp.get());
373 if (sect_idx == UINT32_MAX)
374 {
375 sect_idx = AddSection (sect_sp);
376 }
377 return sect_idx;
378 }
379
380 bool
ReplaceSection(user_id_t sect_id,const lldb::SectionSP & sect_sp,uint32_t depth)381 SectionList::ReplaceSection (user_id_t sect_id, const lldb::SectionSP& sect_sp, uint32_t depth)
382 {
383 iterator sect_iter, end = m_sections.end();
384 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter)
385 {
386 if ((*sect_iter)->GetID() == sect_id)
387 {
388 *sect_iter = sect_sp;
389 return true;
390 }
391 else if (depth > 0)
392 {
393 if ((*sect_iter)->GetChildren().ReplaceSection(sect_id, sect_sp, depth - 1))
394 return true;
395 }
396 }
397 return false;
398 }
399
400 size_t
GetNumSections(uint32_t depth) const401 SectionList::GetNumSections (uint32_t depth) const
402 {
403 size_t count = m_sections.size();
404 if (depth > 0)
405 {
406 const_iterator sect_iter, end = m_sections.end();
407 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter)
408 {
409 count += (*sect_iter)->GetChildren().GetNumSections(depth - 1);
410 }
411 }
412 return count;
413 }
414
415 SectionSP
GetSectionAtIndex(size_t idx) const416 SectionList::GetSectionAtIndex (size_t idx) const
417 {
418 SectionSP sect_sp;
419 if (idx < m_sections.size())
420 sect_sp = m_sections[idx];
421 return sect_sp;
422 }
423
424 SectionSP
FindSectionByName(const ConstString & section_dstr) const425 SectionList::FindSectionByName (const ConstString §ion_dstr) const
426 {
427 SectionSP sect_sp;
428 // Check if we have a valid section string
429 if (section_dstr && !m_sections.empty())
430 {
431 const_iterator sect_iter;
432 const_iterator end = m_sections.end();
433 for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter)
434 {
435 Section *child_section = sect_iter->get();
436 assert (child_section);
437 if (child_section->GetName() == section_dstr)
438 {
439 sect_sp = *sect_iter;
440 }
441 else
442 {
443 sect_sp = child_section->GetChildren().FindSectionByName(section_dstr);
444 }
445 }
446 }
447 return sect_sp;
448 }
449
450 SectionSP
FindSectionByID(user_id_t sect_id) const451 SectionList::FindSectionByID (user_id_t sect_id) const
452 {
453 SectionSP sect_sp;
454 if (sect_id)
455 {
456 const_iterator sect_iter;
457 const_iterator end = m_sections.end();
458 for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter)
459 {
460 if ((*sect_iter)->GetID() == sect_id)
461 {
462 sect_sp = *sect_iter;
463 break;
464 }
465 else
466 {
467 sect_sp = (*sect_iter)->GetChildren().FindSectionByID (sect_id);
468 }
469 }
470 }
471 return sect_sp;
472 }
473
474
475 SectionSP
FindSectionByType(SectionType sect_type,bool check_children,size_t start_idx) const476 SectionList::FindSectionByType (SectionType sect_type, bool check_children, size_t start_idx) const
477 {
478 SectionSP sect_sp;
479 size_t num_sections = m_sections.size();
480 for (size_t idx = start_idx; idx < num_sections; ++idx)
481 {
482 if (m_sections[idx]->GetType() == sect_type)
483 {
484 sect_sp = m_sections[idx];
485 break;
486 }
487 else if (check_children)
488 {
489 sect_sp = m_sections[idx]->GetChildren().FindSectionByType (sect_type, check_children, 0);
490 if (sect_sp)
491 break;
492 }
493 }
494 return sect_sp;
495 }
496
497 SectionSP
FindSectionContainingFileAddress(addr_t vm_addr,uint32_t depth) const498 SectionList::FindSectionContainingFileAddress (addr_t vm_addr, uint32_t depth) const
499 {
500 SectionSP sect_sp;
501 const_iterator sect_iter;
502 const_iterator end = m_sections.end();
503 for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter)
504 {
505 Section *sect = sect_iter->get();
506 if (sect->ContainsFileAddress (vm_addr))
507 {
508 // The file address is in this section. We need to make sure one of our child
509 // sections doesn't contain this address as well as obeying the depth limit
510 // that was passed in.
511 if (depth > 0)
512 sect_sp = sect->GetChildren().FindSectionContainingFileAddress(vm_addr, depth - 1);
513
514 if (sect_sp.get() == NULL && !sect->IsFake())
515 sect_sp = *sect_iter;
516 }
517 }
518 return sect_sp;
519 }
520
521 bool
ContainsSection(user_id_t sect_id) const522 SectionList::ContainsSection(user_id_t sect_id) const
523 {
524 return FindSectionByID (sect_id).get() != NULL;
525 }
526
527 void
Dump(Stream * s,Target * target,bool show_header,uint32_t depth) const528 SectionList::Dump (Stream *s, Target *target, bool show_header, uint32_t depth) const
529 {
530 bool target_has_loaded_sections = target && !target->GetSectionLoadList().IsEmpty();
531 if (show_header && !m_sections.empty())
532 {
533 s->Indent();
534 s->Printf( "SectID Type %s Address File Off. File Size Flags Section Name\n", target_has_loaded_sections ? "Load" : "File");
535 s->Indent();
536 s->PutCString("---------- ---------------- --------------------------------------- ---------- ---------- ---------- ----------------------------\n");
537 }
538
539
540 const_iterator sect_iter;
541 const_iterator end = m_sections.end();
542 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter)
543 {
544 (*sect_iter)->Dump(s, target_has_loaded_sections ? target : NULL, depth);
545 }
546
547 if (show_header && !m_sections.empty())
548 s->IndentLess();
549
550 }
551
552 size_t
Slide(addr_t slide_amount,bool slide_children)553 SectionList::Slide (addr_t slide_amount, bool slide_children)
554 {
555 size_t count = 0;
556 const_iterator pos, end = m_sections.end();
557 for (pos = m_sections.begin(); pos != end; ++pos)
558 {
559 if ((*pos)->Slide(slide_amount, slide_children))
560 ++count;
561 }
562 return count;
563 }
564