Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 1 | //===-- SBSection.cpp -------------------------------------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "lldb/API/SBSection.h" |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame^] | 10 | #include "SBReproducerPrivate.h" |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 11 | #include "lldb/API/SBStream.h" |
Greg Clayton | fed39aa | 2012-06-27 22:22:28 +0000 | [diff] [blame] | 12 | #include "lldb/API/SBTarget.h" |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 13 | #include "lldb/Core/Module.h" |
Greg Clayton | d9dc52d | 2011-09-24 05:04:40 +0000 | [diff] [blame] | 14 | #include "lldb/Core/Section.h" |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 15 | #include "lldb/Symbol/ObjectFile.h" |
Zachary Turner | 666cc0b | 2017-03-04 01:30:05 +0000 | [diff] [blame] | 16 | #include "lldb/Utility/DataBuffer.h" |
| 17 | #include "lldb/Utility/DataExtractor.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 18 | #include "lldb/Utility/Log.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 19 | #include "lldb/Utility/StreamString.h" |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 20 | |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 21 | using namespace lldb; |
| 22 | using namespace lldb_private; |
| 23 | |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame^] | 24 | SBSection::SBSection() : m_opaque_wp() { |
| 25 | LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBSection); |
| 26 | } |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 27 | |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame^] | 28 | SBSection::SBSection(const SBSection &rhs) : m_opaque_wp(rhs.m_opaque_wp) { |
| 29 | LLDB_RECORD_CONSTRUCTOR(SBSection, (const lldb::SBSection &), rhs); |
| 30 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 31 | |
| 32 | SBSection::SBSection(const lldb::SectionSP §ion_sp) |
| 33 | : m_opaque_wp() // Don't init with section_sp otherwise this will throw if |
| 34 | // section_sp doesn't contain a valid Section * |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 35 | { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 36 | if (section_sp) |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 37 | m_opaque_wp = section_sp; |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 40 | const SBSection &SBSection::operator=(const SBSection &rhs) { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame^] | 41 | LLDB_RECORD_METHOD(const lldb::SBSection &, |
| 42 | SBSection, operator=,(const lldb::SBSection &), rhs); |
| 43 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 44 | m_opaque_wp = rhs.m_opaque_wp; |
| 45 | return *this; |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 48 | SBSection::~SBSection() {} |
| 49 | |
| 50 | bool SBSection::IsValid() const { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame^] | 51 | LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBSection, IsValid); |
| 52 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 53 | SectionSP section_sp(GetSP()); |
| 54 | return section_sp && section_sp->GetModule().get() != NULL; |
| 55 | } |
| 56 | |
| 57 | const char *SBSection::GetName() { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame^] | 58 | LLDB_RECORD_METHOD_NO_ARGS(const char *, SBSection, GetName); |
| 59 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 60 | SectionSP section_sp(GetSP()); |
| 61 | if (section_sp) |
| 62 | return section_sp->GetName().GetCString(); |
| 63 | return NULL; |
| 64 | } |
| 65 | |
| 66 | lldb::SBSection SBSection::GetParent() { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame^] | 67 | LLDB_RECORD_METHOD_NO_ARGS(lldb::SBSection, SBSection, GetParent); |
| 68 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 69 | lldb::SBSection sb_section; |
| 70 | SectionSP section_sp(GetSP()); |
| 71 | if (section_sp) { |
| 72 | SectionSP parent_section_sp(section_sp->GetParent()); |
| 73 | if (parent_section_sp) |
| 74 | sb_section.SetSP(parent_section_sp); |
| 75 | } |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame^] | 76 | return LLDB_RECORD_RESULT(sb_section); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | lldb::SBSection SBSection::FindSubSection(const char *sect_name) { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame^] | 80 | LLDB_RECORD_METHOD(lldb::SBSection, SBSection, FindSubSection, (const char *), |
| 81 | sect_name); |
| 82 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 83 | lldb::SBSection sb_section; |
| 84 | if (sect_name) { |
| 85 | SectionSP section_sp(GetSP()); |
| 86 | if (section_sp) { |
| 87 | ConstString const_sect_name(sect_name); |
| 88 | sb_section.SetSP( |
| 89 | section_sp->GetChildren().FindSectionByName(const_sect_name)); |
Greg Clayton | fed39aa | 2012-06-27 22:22:28 +0000 | [diff] [blame] | 90 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 91 | } |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame^] | 92 | return LLDB_RECORD_RESULT(sb_section); |
Greg Clayton | fed39aa | 2012-06-27 22:22:28 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 95 | size_t SBSection::GetNumSubSections() { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame^] | 96 | LLDB_RECORD_METHOD_NO_ARGS(size_t, SBSection, GetNumSubSections); |
| 97 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 98 | SectionSP section_sp(GetSP()); |
| 99 | if (section_sp) |
| 100 | return section_sp->GetChildren().GetSize(); |
| 101 | return 0; |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 104 | lldb::SBSection SBSection::GetSubSectionAtIndex(size_t idx) { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame^] | 105 | LLDB_RECORD_METHOD(lldb::SBSection, SBSection, GetSubSectionAtIndex, (size_t), |
| 106 | idx); |
| 107 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 108 | lldb::SBSection sb_section; |
| 109 | SectionSP section_sp(GetSP()); |
| 110 | if (section_sp) |
| 111 | sb_section.SetSP(section_sp->GetChildren().GetSectionAtIndex(idx)); |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame^] | 112 | return LLDB_RECORD_RESULT(sb_section); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | lldb::SectionSP SBSection::GetSP() const { return m_opaque_wp.lock(); } |
| 116 | |
| 117 | void SBSection::SetSP(const lldb::SectionSP §ion_sp) { |
| 118 | m_opaque_wp = section_sp; |
| 119 | } |
| 120 | |
| 121 | lldb::addr_t SBSection::GetFileAddress() { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame^] | 122 | LLDB_RECORD_METHOD_NO_ARGS(lldb::addr_t, SBSection, GetFileAddress); |
| 123 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 124 | lldb::addr_t file_addr = LLDB_INVALID_ADDRESS; |
| 125 | SectionSP section_sp(GetSP()); |
| 126 | if (section_sp) |
| 127 | return section_sp->GetFileAddress(); |
| 128 | return file_addr; |
| 129 | } |
| 130 | |
| 131 | lldb::addr_t SBSection::GetLoadAddress(lldb::SBTarget &sb_target) { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame^] | 132 | LLDB_RECORD_METHOD(lldb::addr_t, SBSection, GetLoadAddress, |
| 133 | (lldb::SBTarget &), sb_target); |
| 134 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 135 | TargetSP target_sp(sb_target.GetSP()); |
| 136 | if (target_sp) { |
| 137 | SectionSP section_sp(GetSP()); |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 138 | if (section_sp) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 139 | return section_sp->GetLoadBaseAddress(target_sp.get()); |
| 140 | } |
| 141 | return LLDB_INVALID_ADDRESS; |
| 142 | } |
| 143 | |
| 144 | lldb::addr_t SBSection::GetByteSize() { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame^] | 145 | LLDB_RECORD_METHOD_NO_ARGS(lldb::addr_t, SBSection, GetByteSize); |
| 146 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 147 | SectionSP section_sp(GetSP()); |
| 148 | if (section_sp) |
| 149 | return section_sp->GetByteSize(); |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | uint64_t SBSection::GetFileOffset() { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame^] | 154 | LLDB_RECORD_METHOD_NO_ARGS(uint64_t, SBSection, GetFileOffset); |
| 155 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 156 | SectionSP section_sp(GetSP()); |
| 157 | if (section_sp) { |
| 158 | ModuleSP module_sp(section_sp->GetModule()); |
| 159 | if (module_sp) { |
| 160 | ObjectFile *objfile = module_sp->GetObjectFile(); |
| 161 | if (objfile) |
| 162 | return objfile->GetFileOffset() + section_sp->GetFileOffset(); |
| 163 | } |
| 164 | } |
| 165 | return UINT64_MAX; |
| 166 | } |
| 167 | |
| 168 | uint64_t SBSection::GetFileByteSize() { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame^] | 169 | LLDB_RECORD_METHOD_NO_ARGS(uint64_t, SBSection, GetFileByteSize); |
| 170 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 171 | SectionSP section_sp(GetSP()); |
| 172 | if (section_sp) |
| 173 | return section_sp->GetFileSize(); |
| 174 | return 0; |
| 175 | } |
| 176 | |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame^] | 177 | SBData SBSection::GetSectionData() { |
| 178 | LLDB_RECORD_METHOD_NO_ARGS(lldb::SBData, SBSection, GetSectionData); |
| 179 | |
| 180 | return LLDB_RECORD_RESULT(GetSectionData(0, UINT64_MAX)); |
| 181 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 182 | |
| 183 | SBData SBSection::GetSectionData(uint64_t offset, uint64_t size) { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame^] | 184 | LLDB_RECORD_METHOD(lldb::SBData, SBSection, GetSectionData, |
| 185 | (uint64_t, uint64_t), offset, size); |
| 186 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 187 | SBData sb_data; |
| 188 | SectionSP section_sp(GetSP()); |
| 189 | if (section_sp) { |
| 190 | const uint64_t sect_file_size = section_sp->GetFileSize(); |
| 191 | if (sect_file_size > 0) { |
| 192 | ModuleSP module_sp(section_sp->GetModule()); |
| 193 | if (module_sp) { |
| 194 | ObjectFile *objfile = module_sp->GetObjectFile(); |
| 195 | if (objfile) { |
| 196 | const uint64_t sect_file_offset = |
| 197 | objfile->GetFileOffset() + section_sp->GetFileOffset(); |
| 198 | const uint64_t file_offset = sect_file_offset + offset; |
| 199 | uint64_t file_size = size; |
| 200 | if (file_size == UINT64_MAX) { |
| 201 | file_size = section_sp->GetByteSize(); |
| 202 | if (file_size > offset) |
| 203 | file_size -= offset; |
| 204 | else |
| 205 | file_size = 0; |
| 206 | } |
Jonas Devlieghere | 87e403a | 2018-11-12 21:24:50 +0000 | [diff] [blame] | 207 | auto data_buffer_sp = FileSystem::Instance().CreateDataBuffer( |
Zachary Turner | 7f6a7a3 | 2017-03-06 23:42:14 +0000 | [diff] [blame] | 208 | objfile->GetFileSpec().GetPath(), file_size, file_offset); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 209 | if (data_buffer_sp && data_buffer_sp->GetByteSize() > 0) { |
| 210 | DataExtractorSP data_extractor_sp( |
| 211 | new DataExtractor(data_buffer_sp, objfile->GetByteOrder(), |
| 212 | objfile->GetAddressByteSize())); |
| 213 | |
| 214 | sb_data.SetOpaque(data_extractor_sp); |
| 215 | } |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 216 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 217 | } |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 218 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 219 | } |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame^] | 220 | return LLDB_RECORD_RESULT(sb_data); |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 223 | SectionType SBSection::GetSectionType() { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame^] | 224 | LLDB_RECORD_METHOD_NO_ARGS(lldb::SectionType, SBSection, GetSectionType); |
| 225 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 226 | SectionSP section_sp(GetSP()); |
| 227 | if (section_sp.get()) |
| 228 | return section_sp->GetType(); |
| 229 | return eSectionTypeInvalid; |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame^] | 232 | uint32_t SBSection::GetPermissions() const { |
| 233 | LLDB_RECORD_METHOD_CONST_NO_ARGS(uint32_t, SBSection, GetPermissions); |
| 234 | |
| 235 | SectionSP section_sp(GetSP()); |
| 236 | if (section_sp) |
| 237 | return section_sp->GetPermissions(); |
| 238 | return 0; |
Abhishek Aggarwal | 3c7f070 | 2016-09-08 12:22:56 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 241 | uint32_t SBSection::GetTargetByteSize() { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame^] | 242 | LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBSection, GetTargetByteSize); |
| 243 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 244 | SectionSP section_sp(GetSP()); |
| 245 | if (section_sp.get()) |
| 246 | return section_sp->GetTargetByteSize(); |
| 247 | return 0; |
Greg Clayton | d9dc52d | 2011-09-24 05:04:40 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 250 | bool SBSection::operator==(const SBSection &rhs) { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame^] | 251 | LLDB_RECORD_METHOD(bool, SBSection, operator==,(const lldb::SBSection &), |
| 252 | rhs); |
| 253 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 254 | SectionSP lhs_section_sp(GetSP()); |
| 255 | SectionSP rhs_section_sp(rhs.GetSP()); |
| 256 | if (lhs_section_sp && rhs_section_sp) |
| 257 | return lhs_section_sp == rhs_section_sp; |
| 258 | return false; |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 261 | bool SBSection::operator!=(const SBSection &rhs) { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame^] | 262 | LLDB_RECORD_METHOD(bool, SBSection, operator!=,(const lldb::SBSection &), |
| 263 | rhs); |
| 264 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 265 | SectionSP lhs_section_sp(GetSP()); |
| 266 | SectionSP rhs_section_sp(rhs.GetSP()); |
| 267 | return lhs_section_sp != rhs_section_sp; |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 270 | bool SBSection::GetDescription(SBStream &description) { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame^] | 271 | LLDB_RECORD_METHOD(bool, SBSection, GetDescription, (lldb::SBStream &), |
| 272 | description); |
| 273 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 274 | Stream &strm = description.ref(); |
| 275 | |
| 276 | SectionSP section_sp(GetSP()); |
| 277 | if (section_sp) { |
| 278 | const addr_t file_addr = section_sp->GetFileAddress(); |
| 279 | strm.Printf("[0x%16.16" PRIx64 "-0x%16.16" PRIx64 ") ", file_addr, |
| 280 | file_addr + section_sp->GetByteSize()); |
| 281 | section_sp->DumpName(&strm); |
| 282 | } else { |
| 283 | strm.PutCString("No value"); |
| 284 | } |
| 285 | |
| 286 | return true; |
Matthew Gardiner | c928de3 | 2014-10-22 07:22:56 +0000 | [diff] [blame] | 287 | } |