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