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