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