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