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