Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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" |
Greg Clayton | f5e56de | 2010-09-14 23:36:40 +0000 | [diff] [blame] | 13 | #include "lldb/Target/Target.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 14 | |
| 15 | using namespace lldb; |
| 16 | using namespace lldb_private; |
| 17 | |
| 18 | Section::Section |
| 19 | ( |
| 20 | Section *parent, |
| 21 | Module* module, |
| 22 | user_id_t sect_id, |
| 23 | const ConstString &name, |
| 24 | SectionType sect_type, |
| 25 | addr_t file_addr, |
| 26 | addr_t byte_size, |
| 27 | uint64_t file_offset, |
| 28 | uint64_t file_size, |
| 29 | uint32_t flags |
| 30 | ) : |
| 31 | ModuleChild (module), |
| 32 | UserID (sect_id), |
| 33 | Flags (flags), |
| 34 | m_parent (parent), |
| 35 | m_name (name), |
| 36 | m_type (sect_type), |
| 37 | m_file_addr (file_addr), |
| 38 | m_byte_size (byte_size), |
| 39 | m_file_offset (file_offset), |
| 40 | m_file_size (file_size), |
| 41 | m_children (), |
| 42 | m_fake (false), |
| 43 | m_linked_section(NULL), |
| 44 | m_linked_offset (0) |
| 45 | { |
| 46 | } |
| 47 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 48 | Section::~Section() |
| 49 | { |
| 50 | } |
| 51 | |
| 52 | |
| 53 | // Get a valid shared pointer to this section object |
| 54 | SectionSP |
| 55 | Section::GetSharedPointer() const |
| 56 | { |
| 57 | SectionSP this_sp; |
| 58 | if (m_parent) |
| 59 | this_sp = m_parent->GetChildren().GetSharedPointer (this, false); |
| 60 | else |
| 61 | { |
| 62 | ObjectFile *objfile = m_module->GetObjectFile(); |
| 63 | if (objfile) |
| 64 | { |
| 65 | SectionList *section_list = objfile->GetSectionList(); |
| 66 | if (section_list) |
| 67 | this_sp = section_list->GetSharedPointer (this, false); |
| 68 | } |
| 69 | } |
| 70 | return this_sp; |
| 71 | } |
| 72 | |
| 73 | |
| 74 | |
| 75 | ConstString& |
| 76 | Section::GetName() |
| 77 | { |
| 78 | if (m_linked_section) |
| 79 | return const_cast<Section *>(m_linked_section)->GetName(); |
| 80 | return m_name; |
| 81 | } |
| 82 | |
| 83 | const ConstString& |
| 84 | Section::GetName() const |
| 85 | { |
| 86 | if (m_linked_section) |
| 87 | return m_linked_section->GetName(); |
| 88 | return m_name; |
| 89 | } |
| 90 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 91 | addr_t |
| 92 | Section::GetFileAddress () const |
| 93 | { |
| 94 | if (m_parent) |
| 95 | { |
| 96 | // This section has a parent which means m_file_addr is an offset into |
| 97 | // the parent section, so the file address for this section is the file |
| 98 | // address of the parent plus the offset |
| 99 | return m_parent->GetFileAddress() + m_file_addr; |
| 100 | } |
| 101 | // This section has no parent, so m_file_addr is the file base address |
| 102 | return m_file_addr; |
| 103 | } |
| 104 | |
| 105 | addr_t |
| 106 | Section::GetLinkedFileAddress () const |
| 107 | { |
| 108 | if (m_linked_section) |
| 109 | return m_linked_section->GetFileAddress() + m_linked_offset; |
| 110 | return LLDB_INVALID_ADDRESS; |
| 111 | } |
| 112 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 113 | |
| 114 | addr_t |
Greg Clayton | f5e56de | 2010-09-14 23:36:40 +0000 | [diff] [blame] | 115 | Section::GetLoadBaseAddress (Target *target) const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 116 | { |
| 117 | addr_t load_base_addr = LLDB_INVALID_ADDRESS; |
| 118 | if (m_linked_section) |
| 119 | { |
Greg Clayton | f5e56de | 2010-09-14 23:36:40 +0000 | [diff] [blame] | 120 | load_base_addr = m_linked_section->GetLoadBaseAddress(target) + m_linked_offset; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 121 | } |
| 122 | else |
| 123 | if (m_parent) |
| 124 | { |
Greg Clayton | f5e56de | 2010-09-14 23:36:40 +0000 | [diff] [blame] | 125 | load_base_addr = m_parent->GetLoadBaseAddress (target); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 126 | if (load_base_addr != LLDB_INVALID_ADDRESS) |
| 127 | load_base_addr += GetOffset(); |
| 128 | } |
| 129 | else |
| 130 | { |
Greg Clayton | f5e56de | 2010-09-14 23:36:40 +0000 | [diff] [blame] | 131 | load_base_addr = target->GetSectionLoadList().GetSectionLoadAddress (this); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | return load_base_addr; |
| 135 | } |
| 136 | |
| 137 | bool |
| 138 | Section::ResolveContainedAddress (addr_t offset, Address &so_addr) const |
| 139 | { |
| 140 | const uint32_t num_children = m_children.GetSize(); |
| 141 | if (num_children > 0) |
| 142 | { |
| 143 | for (uint32_t i=0; i<num_children; i++) |
| 144 | { |
| 145 | Section* child_section = m_children.GetSectionAtIndex (i).get(); |
| 146 | |
| 147 | addr_t child_offset = child_section->GetOffset(); |
| 148 | if (child_offset <= offset && offset - child_offset < child_section->GetByteSize()) |
| 149 | return child_section->ResolveContainedAddress (offset - child_offset, so_addr); |
| 150 | } |
| 151 | } |
| 152 | if (m_linked_section) |
| 153 | { |
| 154 | so_addr.SetOffset(m_linked_offset + offset); |
| 155 | so_addr.SetSection(m_linked_section); |
| 156 | } |
| 157 | else |
| 158 | { |
| 159 | so_addr.SetOffset(offset); |
| 160 | so_addr.SetSection(this); |
| 161 | } |
| 162 | return true; |
| 163 | } |
| 164 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 165 | bool |
| 166 | Section::ContainsFileAddress (addr_t vm_addr) const |
| 167 | { |
| 168 | const addr_t file_addr = GetFileAddress(); |
| 169 | if (file_addr != LLDB_INVALID_ADDRESS) |
| 170 | { |
| 171 | if (file_addr <= vm_addr) |
| 172 | { |
| 173 | const addr_t offset = vm_addr - file_addr; |
| 174 | return offset < GetByteSize(); |
| 175 | } |
| 176 | } |
| 177 | return false; |
| 178 | } |
| 179 | |
| 180 | bool |
| 181 | Section::ContainsLinkedFileAddress (addr_t vm_addr) const |
| 182 | { |
| 183 | const addr_t linked_file_addr = GetLinkedFileAddress(); |
| 184 | if (linked_file_addr != LLDB_INVALID_ADDRESS) |
| 185 | { |
| 186 | if (linked_file_addr <= vm_addr) |
| 187 | { |
| 188 | const addr_t offset = vm_addr - linked_file_addr; |
| 189 | return offset < GetByteSize(); |
| 190 | } |
| 191 | } |
| 192 | return false; |
| 193 | } |
| 194 | |
| 195 | int |
| 196 | Section::Compare (const Section& a, const Section& b) |
| 197 | { |
| 198 | if (&a == &b) |
| 199 | return 0; |
| 200 | |
| 201 | const Module* a_module = a.GetModule(); |
| 202 | const Module* b_module = b.GetModule(); |
| 203 | if (a_module == b_module) |
| 204 | { |
| 205 | user_id_t a_sect_uid = a.GetID(); |
| 206 | user_id_t b_sect_uid = b.GetID(); |
| 207 | if (a_sect_uid < b_sect_uid) |
| 208 | return -1; |
| 209 | if (a_sect_uid > b_sect_uid) |
| 210 | return 1; |
| 211 | return 0; |
| 212 | } |
| 213 | else |
| 214 | { |
| 215 | // The modules are different, just compare the module pointers |
| 216 | if (a_module < b_module) |
| 217 | return -1; |
| 218 | else |
| 219 | return 1; // We already know the modules aren't equal |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | |
| 224 | void |
Greg Clayton | 10177aa | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 225 | Section::Dump (Stream *s, Target *target, uint32_t depth) const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 226 | { |
Greg Clayton | 8941142 | 2010-10-08 00:21:05 +0000 | [diff] [blame] | 227 | // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 228 | s->Indent(); |
Greg Clayton | 8941142 | 2010-10-08 00:21:05 +0000 | [diff] [blame] | 229 | s->Printf("0x%8.8x %-14s ", GetID(), GetSectionTypeAsCString (m_type)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 230 | bool resolved = true; |
| 231 | addr_t addr = LLDB_INVALID_ADDRESS; |
| 232 | |
| 233 | if (GetByteSize() == 0) |
| 234 | s->Printf("%39s", ""); |
| 235 | else |
| 236 | { |
Greg Clayton | f669358 | 2010-12-07 18:05:22 +0000 | [diff] [blame] | 237 | if (target && m_linked_section == NULL) |
Greg Clayton | f5e56de | 2010-09-14 23:36:40 +0000 | [diff] [blame] | 238 | addr = GetLoadBaseAddress (target); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 239 | |
| 240 | if (addr == LLDB_INVALID_ADDRESS) |
| 241 | { |
Greg Clayton | f5e56de | 2010-09-14 23:36:40 +0000 | [diff] [blame] | 242 | if (target) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 243 | resolved = false; |
| 244 | addr = GetFileAddress(); |
| 245 | } |
| 246 | |
| 247 | VMRange range(addr, addr + m_byte_size); |
| 248 | range.Dump (s, 0); |
| 249 | } |
| 250 | |
Greg Clayton | 73b472d | 2010-10-27 03:32:59 +0000 | [diff] [blame] | 251 | s->Printf("%c 0x%8.8llx 0x%8.8llx 0x%8.8x ", resolved ? ' ' : '*', m_file_offset, m_file_size, Get()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 252 | |
| 253 | DumpName (s); |
| 254 | |
| 255 | s->EOL(); |
| 256 | |
| 257 | if (m_linked_section) |
| 258 | { |
| 259 | addr = LLDB_INVALID_ADDRESS; |
Greg Clayton | f669358 | 2010-12-07 18:05:22 +0000 | [diff] [blame] | 260 | resolved = true; |
Greg Clayton | f5e56de | 2010-09-14 23:36:40 +0000 | [diff] [blame] | 261 | if (target) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 262 | { |
Greg Clayton | f5e56de | 2010-09-14 23:36:40 +0000 | [diff] [blame] | 263 | addr = m_linked_section->GetLoadBaseAddress(target); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 264 | if (addr != LLDB_INVALID_ADDRESS) |
| 265 | addr += m_linked_offset; |
| 266 | } |
| 267 | |
| 268 | if (addr == LLDB_INVALID_ADDRESS) |
| 269 | { |
Greg Clayton | f5e56de | 2010-09-14 23:36:40 +0000 | [diff] [blame] | 270 | if (target) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 271 | resolved = false; |
| 272 | addr = m_linked_section->GetFileAddress() + m_linked_offset; |
| 273 | } |
| 274 | |
Greg Clayton | 65e364e | 2010-12-07 07:37:38 +0000 | [diff] [blame] | 275 | int indent = 26 + s->GetIndentLevel(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 276 | s->Printf("%*.*s", indent, indent, ""); |
| 277 | VMRange linked_range(addr, addr + m_byte_size); |
| 278 | linked_range.Dump (s, 0); |
| 279 | indent = 3 * (sizeof(uint32_t) * 2 + 2 + 1) + 1; |
| 280 | s->Printf("%c%*.*s", resolved ? ' ' : '*', indent, indent, ""); |
| 281 | |
| 282 | m_linked_section->DumpName(s); |
| 283 | s->Printf(" + 0x%llx\n", m_linked_offset); |
| 284 | } |
| 285 | |
Greg Clayton | 10177aa | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 286 | if (depth > 0) |
| 287 | m_children.Dump(s, target, false, depth - 1); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | void |
| 291 | Section::DumpName (Stream *s) const |
| 292 | { |
Greg Clayton | 65e364e | 2010-12-07 07:37:38 +0000 | [diff] [blame] | 293 | if (m_parent == NULL) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 294 | { |
| 295 | // The top most section prints the module basename |
| 296 | const char *module_basename = m_module->GetFileSpec().GetFilename().AsCString(); |
| 297 | if (module_basename && module_basename[0]) |
| 298 | s->Printf("%s.", module_basename); |
| 299 | } |
| 300 | else |
| 301 | { |
| 302 | m_parent->DumpName (s); |
| 303 | s->PutChar('.'); |
| 304 | } |
| 305 | m_name.Dump(s); |
| 306 | } |
| 307 | |
| 308 | //---------------------------------------------------------------------- |
| 309 | // Get the section data from a complete contiguous copy of the |
| 310 | // entire executable image. |
| 311 | //---------------------------------------------------------------------- |
| 312 | size_t |
| 313 | Section::GetSectionDataFromImage (const DataExtractor& image_data, DataExtractor& section_data) const |
| 314 | { |
| 315 | size_t file_size = GetByteSize(); |
| 316 | if (file_size > 0) |
| 317 | { |
| 318 | off_t file_offset = GetFileOffset(); |
| 319 | if (section_data.SetData (image_data, file_offset, file_size) == file_size) |
| 320 | return true; |
| 321 | } |
| 322 | return false; |
| 323 | } |
| 324 | |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 325 | size_t |
| 326 | Section::ReadSectionDataFromObjectFile (const ObjectFile* objfile, off_t section_offset, void *dst, size_t dst_len) const |
| 327 | { |
| 328 | if (objfile && dst && dst_len) |
| 329 | { |
| 330 | const FileSpec& file = objfile->GetFileSpec(); |
| 331 | |
| 332 | if (file) |
| 333 | { |
| 334 | off_t section_file_offset = GetFileOffset() + objfile->GetOffset() + section_offset; |
| 335 | return file.ReadFileContents (section_file_offset, dst, dst_len); |
| 336 | } |
| 337 | } |
| 338 | return 0; |
| 339 | } |
| 340 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 341 | //---------------------------------------------------------------------- |
| 342 | // Get the section data the file on disk |
| 343 | //---------------------------------------------------------------------- |
| 344 | size_t |
| 345 | Section::ReadSectionDataFromObjectFile(const ObjectFile* objfile, DataExtractor& section_data) const |
| 346 | { |
| 347 | if (objfile == NULL) |
| 348 | return 0; |
| 349 | |
| 350 | const FileSpec& file = objfile->GetFileSpec(); |
| 351 | |
| 352 | if (file) |
| 353 | { |
| 354 | size_t section_file_size = GetByteSize(); |
| 355 | if (section_file_size > 0) |
| 356 | { |
| 357 | off_t section_file_offset = GetFileOffset() + objfile->GetOffset(); |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 358 | DataBufferSP section_data_sp(file.ReadFileContents(section_file_offset, section_file_size)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 359 | |
| 360 | section_data.SetByteOrder(objfile->GetByteOrder()); |
| 361 | section_data.SetAddressByteSize(objfile->GetAddressByteSize()); |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 362 | return section_data.SetData (section_data_sp); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 363 | } |
| 364 | } |
| 365 | return 0; |
| 366 | } |
| 367 | |
| 368 | size_t |
| 369 | Section::MemoryMapSectionDataFromObjectFile(const ObjectFile* objfile, DataExtractor& section_data) const |
| 370 | { |
| 371 | if (objfile == NULL) |
| 372 | return 0; |
| 373 | |
| 374 | const FileSpec& file = objfile->GetFileSpec(); |
| 375 | |
| 376 | if (file) |
| 377 | { |
| 378 | size_t section_file_size = GetFileSize(); |
| 379 | if (section_file_size > 0) |
| 380 | { |
| 381 | off_t section_file_offset = GetFileOffset() + objfile->GetOffset(); |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 382 | DataBufferSP section_data_sp(file.MemoryMapFileContents(section_file_offset, section_file_size)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 383 | section_data.SetByteOrder(objfile->GetByteOrder()); |
| 384 | section_data.SetAddressByteSize(objfile->GetAddressByteSize()); |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 385 | return section_data.SetData (section_data_sp); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 386 | } |
| 387 | } |
| 388 | return 0; |
| 389 | } |
| 390 | |
| 391 | bool |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 392 | Section::IsDescendant (const Section *section) |
| 393 | { |
| 394 | if (this == section) |
| 395 | return true; |
| 396 | if (m_parent) |
| 397 | return m_parent->IsDescendant (section); |
| 398 | return false; |
| 399 | } |
| 400 | |
| 401 | bool |
| 402 | Section::Slide (addr_t slide_amount, bool slide_children) |
| 403 | { |
| 404 | if (m_file_addr != LLDB_INVALID_ADDRESS) |
| 405 | { |
| 406 | if (slide_amount == 0) |
| 407 | return true; |
| 408 | |
| 409 | m_file_addr += slide_amount; |
| 410 | |
| 411 | if (slide_children) |
| 412 | m_children.Slide (slide_amount, slide_children); |
| 413 | |
| 414 | return true; |
| 415 | } |
| 416 | return false; |
| 417 | } |
| 418 | |
| 419 | void |
| 420 | Section::SetLinkedLocation (const Section *linked_section, uint64_t linked_offset) |
| 421 | { |
| 422 | if (linked_section) |
| 423 | m_module = linked_section->GetModule(); |
| 424 | m_linked_section = linked_section; |
| 425 | m_linked_offset = linked_offset; |
| 426 | } |
| 427 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 428 | #pragma mark SectionList |
| 429 | |
| 430 | SectionList::SectionList () : |
| 431 | m_sections() |
| 432 | { |
| 433 | } |
| 434 | |
| 435 | |
| 436 | SectionList::~SectionList () |
| 437 | { |
| 438 | } |
| 439 | |
| 440 | uint32_t |
| 441 | SectionList::AddSection (SectionSP& sect_sp) |
| 442 | { |
| 443 | uint32_t section_index = m_sections.size(); |
| 444 | m_sections.push_back(sect_sp); |
| 445 | return section_index; |
| 446 | } |
| 447 | |
| 448 | uint32_t |
| 449 | SectionList::FindSectionIndex (const Section* sect) |
| 450 | { |
| 451 | iterator sect_iter; |
| 452 | iterator begin = m_sections.begin(); |
| 453 | iterator end = m_sections.end(); |
| 454 | for (sect_iter = begin; sect_iter != end; ++sect_iter) |
| 455 | { |
| 456 | if (sect_iter->get() == sect) |
| 457 | { |
| 458 | // The secton was already in this section list |
| 459 | return std::distance (begin, sect_iter); |
| 460 | } |
| 461 | } |
| 462 | return UINT32_MAX; |
| 463 | } |
| 464 | |
| 465 | uint32_t |
| 466 | SectionList::AddUniqueSection (SectionSP& sect_sp) |
| 467 | { |
| 468 | uint32_t sect_idx = FindSectionIndex (sect_sp.get()); |
| 469 | if (sect_idx == UINT32_MAX) |
| 470 | sect_idx = AddSection (sect_sp); |
| 471 | return sect_idx; |
| 472 | } |
| 473 | |
| 474 | |
| 475 | bool |
| 476 | SectionList::ReplaceSection (user_id_t sect_id, SectionSP& sect_sp, uint32_t depth) |
| 477 | { |
| 478 | iterator sect_iter, end = m_sections.end(); |
| 479 | for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter) |
| 480 | { |
| 481 | if ((*sect_iter)->GetID() == sect_id) |
| 482 | { |
| 483 | *sect_iter = sect_sp; |
| 484 | return true; |
| 485 | } |
| 486 | else if (depth > 0) |
| 487 | { |
| 488 | if ((*sect_iter)->GetChildren().ReplaceSection(sect_id, sect_sp, depth - 1)) |
| 489 | return true; |
| 490 | } |
| 491 | } |
| 492 | return false; |
| 493 | } |
| 494 | |
| 495 | |
| 496 | size_t |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 497 | SectionList::GetNumSections (uint32_t depth) const |
| 498 | { |
| 499 | size_t count = m_sections.size(); |
| 500 | if (depth > 0) |
| 501 | { |
| 502 | const_iterator sect_iter, end = m_sections.end(); |
| 503 | for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter) |
| 504 | { |
| 505 | count += (*sect_iter)->GetChildren().GetNumSections(depth - 1); |
| 506 | } |
| 507 | } |
| 508 | return count; |
| 509 | } |
| 510 | |
| 511 | SectionSP |
| 512 | SectionList::GetSectionAtIndex (uint32_t idx) const |
| 513 | { |
| 514 | SectionSP sect_sp; |
| 515 | if (idx < m_sections.size()) |
| 516 | sect_sp = m_sections[idx]; |
| 517 | return sect_sp; |
| 518 | } |
| 519 | |
| 520 | SectionSP |
| 521 | SectionList::FindSectionByName (const ConstString §ion_dstr) const |
| 522 | { |
| 523 | SectionSP sect_sp; |
| 524 | // Check if we have a valid section string |
| 525 | if (section_dstr) |
| 526 | { |
| 527 | const_iterator sect_iter; |
| 528 | const_iterator end = m_sections.end(); |
| 529 | for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter) |
| 530 | { |
| 531 | if ((*sect_iter)->GetName() == section_dstr) |
| 532 | { |
| 533 | sect_sp = *sect_iter; |
| 534 | } |
| 535 | else |
| 536 | { |
| 537 | sect_sp = (*sect_iter)->GetChildren().FindSectionByName(section_dstr); |
| 538 | } |
| 539 | } |
| 540 | } |
| 541 | return sect_sp; |
| 542 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 543 | |
| 544 | SectionSP |
| 545 | SectionList::FindSectionByID (user_id_t sect_id) const |
| 546 | { |
| 547 | SectionSP sect_sp; |
| 548 | if (sect_id) |
| 549 | { |
| 550 | const_iterator sect_iter; |
| 551 | const_iterator end = m_sections.end(); |
| 552 | for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter) |
| 553 | { |
| 554 | if ((*sect_iter)->GetID() == sect_id) |
| 555 | { |
| 556 | sect_sp = *sect_iter; |
| 557 | break; |
| 558 | } |
| 559 | else |
| 560 | { |
| 561 | sect_sp = (*sect_iter)->GetChildren().FindSectionByID (sect_id); |
| 562 | } |
| 563 | } |
| 564 | } |
| 565 | return sect_sp; |
| 566 | } |
| 567 | |
Greg Clayton | 70e33eb | 2010-07-21 21:49:46 +0000 | [diff] [blame] | 568 | |
| 569 | SectionSP |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame^] | 570 | SectionList::FindSectionByType (SectionType sect_type, bool check_children, uint32_t start_idx) const |
Greg Clayton | 70e33eb | 2010-07-21 21:49:46 +0000 | [diff] [blame] | 571 | { |
| 572 | SectionSP sect_sp; |
| 573 | uint32_t num_sections = m_sections.size(); |
| 574 | for (uint32_t idx = start_idx; idx < num_sections; ++idx) |
| 575 | { |
| 576 | if (m_sections[idx]->GetType() == sect_type) |
| 577 | { |
| 578 | sect_sp = m_sections[idx]; |
| 579 | break; |
| 580 | } |
Greg Clayton | 4ceb998 | 2010-07-21 22:54:26 +0000 | [diff] [blame] | 581 | else if (check_children) |
| 582 | { |
| 583 | sect_sp = m_sections[idx]->GetChildren().FindSectionByType (sect_type, check_children, 0); |
| 584 | if (sect_sp) |
| 585 | break; |
| 586 | } |
Greg Clayton | 70e33eb | 2010-07-21 21:49:46 +0000 | [diff] [blame] | 587 | } |
| 588 | return sect_sp; |
| 589 | } |
| 590 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 591 | SectionSP |
| 592 | SectionList::GetSharedPointer (const Section *section, bool check_children) const |
| 593 | { |
| 594 | SectionSP sect_sp; |
| 595 | if (section) |
| 596 | { |
| 597 | const_iterator sect_iter; |
| 598 | const_iterator end = m_sections.end(); |
| 599 | for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter) |
| 600 | { |
| 601 | if (sect_iter->get() == section) |
| 602 | { |
| 603 | sect_sp = *sect_iter; |
| 604 | break; |
| 605 | } |
| 606 | else if (check_children) |
| 607 | { |
| 608 | sect_sp = (*sect_iter)->GetChildren().GetSharedPointer (section, true); |
| 609 | } |
| 610 | } |
| 611 | } |
| 612 | return sect_sp; |
| 613 | } |
| 614 | |
| 615 | |
| 616 | |
| 617 | SectionSP |
| 618 | SectionList::FindSectionContainingFileAddress (addr_t vm_addr, uint32_t depth) const |
| 619 | { |
| 620 | SectionSP sect_sp; |
| 621 | const_iterator sect_iter; |
| 622 | const_iterator end = m_sections.end(); |
| 623 | for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter) |
| 624 | { |
| 625 | Section *sect = sect_iter->get(); |
| 626 | if (sect->ContainsFileAddress (vm_addr)) |
| 627 | { |
| 628 | // The file address is in this section. We need to make sure one of our child |
| 629 | // sections doesn't contain this address as well as obeying the depth limit |
| 630 | // that was passed in. |
| 631 | if (depth > 0) |
| 632 | sect_sp = sect->GetChildren().FindSectionContainingFileAddress(vm_addr, depth - 1); |
| 633 | |
| 634 | if (sect_sp.get() == NULL && !sect->IsFake()) |
| 635 | sect_sp = *sect_iter; |
| 636 | } |
| 637 | } |
| 638 | return sect_sp; |
| 639 | } |
| 640 | |
| 641 | |
| 642 | SectionSP |
Greg Clayton | 016a95e | 2010-09-14 02:20:48 +0000 | [diff] [blame] | 643 | SectionList::FindSectionContainingLinkedFileAddress (addr_t vm_addr, uint32_t depth) const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 644 | { |
| 645 | SectionSP sect_sp; |
| 646 | const_iterator sect_iter; |
| 647 | const_iterator end = m_sections.end(); |
| 648 | for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter) |
| 649 | { |
| 650 | Section *sect = sect_iter->get(); |
| 651 | if (sect->ContainsLinkedFileAddress (vm_addr)) |
| 652 | { |
| 653 | sect_sp = *sect_iter; |
Greg Clayton | 016a95e | 2010-09-14 02:20:48 +0000 | [diff] [blame] | 654 | } |
| 655 | else if (depth > 0) |
| 656 | { |
| 657 | sect_sp = sect->GetChildren().FindSectionContainingLinkedFileAddress (vm_addr, depth - 1); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 658 | } |
| 659 | } |
| 660 | return sect_sp; |
| 661 | } |
| 662 | |
| 663 | bool |
| 664 | SectionList::ContainsSection(user_id_t sect_id) const |
| 665 | { |
| 666 | return FindSectionByID (sect_id).get() != NULL; |
| 667 | } |
| 668 | |
| 669 | void |
Greg Clayton | 10177aa | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 670 | SectionList::Dump (Stream *s, Target *target, bool show_header, uint32_t depth) const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 671 | { |
Greg Clayton | f669358 | 2010-12-07 18:05:22 +0000 | [diff] [blame] | 672 | bool target_has_loaded_sections = target && !target->GetSectionLoadList().IsEmpty(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 673 | if (show_header && !m_sections.empty()) |
| 674 | { |
Greg Clayton | 8941142 | 2010-10-08 00:21:05 +0000 | [diff] [blame] | 675 | // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this); |
| 676 | // s->Indent(); |
| 677 | // s->PutCString( "SectionList\n"); |
| 678 | // s->IndentMore(); |
| 679 | // s->Printf("%*s", 2*(sizeof(void *) + 2), ""); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 680 | s->Indent(); |
Greg Clayton | f669358 | 2010-12-07 18:05:22 +0000 | [diff] [blame] | 681 | s->Printf("SectID Type %s Address File Off. File Size Flags Section Name\n", target_has_loaded_sections ? "Load" : "File"); |
Greg Clayton | 8941142 | 2010-10-08 00:21:05 +0000 | [diff] [blame] | 682 | // s->Printf("%*s", 2*(sizeof(void *) + 2), ""); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 683 | s->Indent(); |
Greg Clayton | 8941142 | 2010-10-08 00:21:05 +0000 | [diff] [blame] | 684 | s->PutCString("---------- -------------- --------------------------------------- ---------- ---------- ---------- ----------------------------\n"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | |
| 688 | const_iterator sect_iter; |
| 689 | const_iterator end = m_sections.end(); |
| 690 | for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter) |
| 691 | { |
Greg Clayton | 10177aa | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 692 | (*sect_iter)->Dump(s, target_has_loaded_sections ? target : NULL, depth); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | if (show_header && !m_sections.empty()) |
| 696 | s->IndentLess(); |
| 697 | |
| 698 | } |
| 699 | |
| 700 | size_t |
| 701 | SectionList::Slide (addr_t slide_amount, bool slide_children) |
| 702 | { |
| 703 | size_t count = 0; |
| 704 | const_iterator pos, end = m_sections.end(); |
| 705 | for (pos = m_sections.begin(); pos != end; ++pos) |
| 706 | { |
| 707 | if ((*pos)->Slide(slide_amount, slide_children)) |
| 708 | ++count; |
| 709 | } |
| 710 | return count; |
| 711 | } |
| 712 | |