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