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