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