blob: 14e1e14f59aa1c848979c86e28ac2232c8a30e5d [file] [log] [blame]
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001//===-- SBSection.cpp -------------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Claytoncac9c5f2011-09-24 00:52:29 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "lldb/API/SBSection.h"
Jonas Devliegherebaf56642019-03-06 00:06:00 +000010#include "SBReproducerPrivate.h"
Greg Claytoncac9c5f2011-09-24 00:52:29 +000011#include "lldb/API/SBStream.h"
Greg Claytonfed39aa2012-06-27 22:22:28 +000012#include "lldb/API/SBTarget.h"
Greg Claytoncac9c5f2011-09-24 00:52:29 +000013#include "lldb/Core/Module.h"
Greg Claytond9dc52d2011-09-24 05:04:40 +000014#include "lldb/Core/Section.h"
Greg Clayton1f746072012-08-29 21:13:06 +000015#include "lldb/Symbol/ObjectFile.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000016#include "lldb/Utility/DataBuffer.h"
17#include "lldb/Utility/DataExtractor.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000018#include "lldb/Utility/StreamString.h"
Greg Claytoncac9c5f2011-09-24 00:52:29 +000019
Greg Claytoncac9c5f2011-09-24 00:52:29 +000020using namespace lldb;
21using namespace lldb_private;
22
Jonas Devliegherebaf56642019-03-06 00:06:00 +000023SBSection::SBSection() : m_opaque_wp() {
24 LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBSection);
25}
Greg Claytoncac9c5f2011-09-24 00:52:29 +000026
Jonas Devliegherebaf56642019-03-06 00:06:00 +000027SBSection::SBSection(const SBSection &rhs) : m_opaque_wp(rhs.m_opaque_wp) {
28 LLDB_RECORD_CONSTRUCTOR(SBSection, (const lldb::SBSection &), rhs);
29}
Kate Stoneb9c1b512016-09-06 20:57:50 +000030
31SBSection::SBSection(const lldb::SectionSP &section_sp)
32 : m_opaque_wp() // Don't init with section_sp otherwise this will throw if
33 // section_sp doesn't contain a valid Section *
Greg Claytoncac9c5f2011-09-24 00:52:29 +000034{
Kate Stoneb9c1b512016-09-06 20:57:50 +000035 if (section_sp)
Greg Claytone72dfb32012-02-24 01:59:29 +000036 m_opaque_wp = section_sp;
Greg Claytoncac9c5f2011-09-24 00:52:29 +000037}
38
Kate Stoneb9c1b512016-09-06 20:57:50 +000039const SBSection &SBSection::operator=(const SBSection &rhs) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000040 LLDB_RECORD_METHOD(const lldb::SBSection &,
41 SBSection, operator=,(const lldb::SBSection &), rhs);
42
Kate Stoneb9c1b512016-09-06 20:57:50 +000043 m_opaque_wp = rhs.m_opaque_wp;
Jonas Devlieghere306809f2019-04-03 21:31:22 +000044 return LLDB_RECORD_RESULT(*this);
Greg Claytoncac9c5f2011-09-24 00:52:29 +000045}
46
Kate Stoneb9c1b512016-09-06 20:57:50 +000047SBSection::~SBSection() {}
48
49bool SBSection::IsValid() const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000050 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBSection, IsValid);
Pavel Labath7f5237b2019-03-11 13:58:46 +000051 return this->operator bool();
52}
53SBSection::operator bool() const {
54 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBSection, operator bool);
Jonas Devliegherebaf56642019-03-06 00:06:00 +000055
Kate Stoneb9c1b512016-09-06 20:57:50 +000056 SectionSP section_sp(GetSP());
Konrad Kleine248a1302019-05-23 11:14:47 +000057 return section_sp && section_sp->GetModule().get() != nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +000058}
59
60const char *SBSection::GetName() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000061 LLDB_RECORD_METHOD_NO_ARGS(const char *, SBSection, GetName);
62
Kate Stoneb9c1b512016-09-06 20:57:50 +000063 SectionSP section_sp(GetSP());
64 if (section_sp)
65 return section_sp->GetName().GetCString();
Konrad Kleine248a1302019-05-23 11:14:47 +000066 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +000067}
68
69lldb::SBSection SBSection::GetParent() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000070 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBSection, SBSection, GetParent);
71
Kate Stoneb9c1b512016-09-06 20:57:50 +000072 lldb::SBSection sb_section;
73 SectionSP section_sp(GetSP());
74 if (section_sp) {
75 SectionSP parent_section_sp(section_sp->GetParent());
76 if (parent_section_sp)
77 sb_section.SetSP(parent_section_sp);
78 }
Jonas Devliegherebaf56642019-03-06 00:06:00 +000079 return LLDB_RECORD_RESULT(sb_section);
Kate Stoneb9c1b512016-09-06 20:57:50 +000080}
81
82lldb::SBSection SBSection::FindSubSection(const char *sect_name) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000083 LLDB_RECORD_METHOD(lldb::SBSection, SBSection, FindSubSection, (const char *),
84 sect_name);
85
Kate Stoneb9c1b512016-09-06 20:57:50 +000086 lldb::SBSection sb_section;
87 if (sect_name) {
88 SectionSP section_sp(GetSP());
89 if (section_sp) {
90 ConstString const_sect_name(sect_name);
91 sb_section.SetSP(
92 section_sp->GetChildren().FindSectionByName(const_sect_name));
Greg Claytonfed39aa2012-06-27 22:22:28 +000093 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000094 }
Jonas Devliegherebaf56642019-03-06 00:06:00 +000095 return LLDB_RECORD_RESULT(sb_section);
Greg Claytonfed39aa2012-06-27 22:22:28 +000096}
97
Kate Stoneb9c1b512016-09-06 20:57:50 +000098size_t SBSection::GetNumSubSections() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000099 LLDB_RECORD_METHOD_NO_ARGS(size_t, SBSection, GetNumSubSections);
100
Kate Stoneb9c1b512016-09-06 20:57:50 +0000101 SectionSP section_sp(GetSP());
102 if (section_sp)
103 return section_sp->GetChildren().GetSize();
104 return 0;
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000105}
106
Kate Stoneb9c1b512016-09-06 20:57:50 +0000107lldb::SBSection SBSection::GetSubSectionAtIndex(size_t idx) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000108 LLDB_RECORD_METHOD(lldb::SBSection, SBSection, GetSubSectionAtIndex, (size_t),
109 idx);
110
Kate Stoneb9c1b512016-09-06 20:57:50 +0000111 lldb::SBSection sb_section;
112 SectionSP section_sp(GetSP());
113 if (section_sp)
114 sb_section.SetSP(section_sp->GetChildren().GetSectionAtIndex(idx));
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000115 return LLDB_RECORD_RESULT(sb_section);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000116}
117
118lldb::SectionSP SBSection::GetSP() const { return m_opaque_wp.lock(); }
119
120void SBSection::SetSP(const lldb::SectionSP &section_sp) {
121 m_opaque_wp = section_sp;
122}
123
124lldb::addr_t SBSection::GetFileAddress() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000125 LLDB_RECORD_METHOD_NO_ARGS(lldb::addr_t, SBSection, GetFileAddress);
126
Kate Stoneb9c1b512016-09-06 20:57:50 +0000127 lldb::addr_t file_addr = LLDB_INVALID_ADDRESS;
128 SectionSP section_sp(GetSP());
129 if (section_sp)
130 return section_sp->GetFileAddress();
131 return file_addr;
132}
133
134lldb::addr_t SBSection::GetLoadAddress(lldb::SBTarget &sb_target) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000135 LLDB_RECORD_METHOD(lldb::addr_t, SBSection, GetLoadAddress,
136 (lldb::SBTarget &), sb_target);
137
Kate Stoneb9c1b512016-09-06 20:57:50 +0000138 TargetSP target_sp(sb_target.GetSP());
139 if (target_sp) {
140 SectionSP section_sp(GetSP());
Greg Claytone72dfb32012-02-24 01:59:29 +0000141 if (section_sp)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000142 return section_sp->GetLoadBaseAddress(target_sp.get());
143 }
144 return LLDB_INVALID_ADDRESS;
145}
146
147lldb::addr_t SBSection::GetByteSize() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000148 LLDB_RECORD_METHOD_NO_ARGS(lldb::addr_t, SBSection, GetByteSize);
149
Kate Stoneb9c1b512016-09-06 20:57:50 +0000150 SectionSP section_sp(GetSP());
151 if (section_sp)
152 return section_sp->GetByteSize();
153 return 0;
154}
155
156uint64_t SBSection::GetFileOffset() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000157 LLDB_RECORD_METHOD_NO_ARGS(uint64_t, SBSection, GetFileOffset);
158
Kate Stoneb9c1b512016-09-06 20:57:50 +0000159 SectionSP section_sp(GetSP());
160 if (section_sp) {
161 ModuleSP module_sp(section_sp->GetModule());
162 if (module_sp) {
163 ObjectFile *objfile = module_sp->GetObjectFile();
164 if (objfile)
165 return objfile->GetFileOffset() + section_sp->GetFileOffset();
166 }
167 }
168 return UINT64_MAX;
169}
170
171uint64_t SBSection::GetFileByteSize() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000172 LLDB_RECORD_METHOD_NO_ARGS(uint64_t, SBSection, GetFileByteSize);
173
Kate Stoneb9c1b512016-09-06 20:57:50 +0000174 SectionSP section_sp(GetSP());
175 if (section_sp)
176 return section_sp->GetFileSize();
177 return 0;
178}
179
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000180SBData SBSection::GetSectionData() {
181 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBData, SBSection, GetSectionData);
182
183 return LLDB_RECORD_RESULT(GetSectionData(0, UINT64_MAX));
184}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000185
186SBData SBSection::GetSectionData(uint64_t offset, uint64_t size) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000187 LLDB_RECORD_METHOD(lldb::SBData, SBSection, GetSectionData,
188 (uint64_t, uint64_t), offset, size);
189
Kate Stoneb9c1b512016-09-06 20:57:50 +0000190 SBData sb_data;
191 SectionSP section_sp(GetSP());
192 if (section_sp) {
193 const uint64_t sect_file_size = section_sp->GetFileSize();
194 if (sect_file_size > 0) {
195 ModuleSP module_sp(section_sp->GetModule());
196 if (module_sp) {
197 ObjectFile *objfile = module_sp->GetObjectFile();
198 if (objfile) {
199 const uint64_t sect_file_offset =
200 objfile->GetFileOffset() + section_sp->GetFileOffset();
201 const uint64_t file_offset = sect_file_offset + offset;
202 uint64_t file_size = size;
203 if (file_size == UINT64_MAX) {
204 file_size = section_sp->GetByteSize();
205 if (file_size > offset)
206 file_size -= offset;
207 else
208 file_size = 0;
209 }
Jonas Devlieghere87e403a2018-11-12 21:24:50 +0000210 auto data_buffer_sp = FileSystem::Instance().CreateDataBuffer(
Zachary Turner7f6a7a32017-03-06 23:42:14 +0000211 objfile->GetFileSpec().GetPath(), file_size, file_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000212 if (data_buffer_sp && data_buffer_sp->GetByteSize() > 0) {
213 DataExtractorSP data_extractor_sp(
214 new DataExtractor(data_buffer_sp, objfile->GetByteOrder(),
215 objfile->GetAddressByteSize()));
216
217 sb_data.SetOpaque(data_extractor_sp);
218 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000219 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000220 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000221 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000222 }
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000223 return LLDB_RECORD_RESULT(sb_data);
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000224}
225
Kate Stoneb9c1b512016-09-06 20:57:50 +0000226SectionType SBSection::GetSectionType() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000227 LLDB_RECORD_METHOD_NO_ARGS(lldb::SectionType, SBSection, GetSectionType);
228
Kate Stoneb9c1b512016-09-06 20:57:50 +0000229 SectionSP section_sp(GetSP());
230 if (section_sp.get())
231 return section_sp->GetType();
232 return eSectionTypeInvalid;
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000233}
234
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000235uint32_t SBSection::GetPermissions() const {
236 LLDB_RECORD_METHOD_CONST_NO_ARGS(uint32_t, SBSection, GetPermissions);
237
238 SectionSP section_sp(GetSP());
239 if (section_sp)
240 return section_sp->GetPermissions();
241 return 0;
Abhishek Aggarwal3c7f0702016-09-08 12:22:56 +0000242}
243
Kate Stoneb9c1b512016-09-06 20:57:50 +0000244uint32_t SBSection::GetTargetByteSize() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000245 LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBSection, GetTargetByteSize);
246
Kate Stoneb9c1b512016-09-06 20:57:50 +0000247 SectionSP section_sp(GetSP());
248 if (section_sp.get())
249 return section_sp->GetTargetByteSize();
250 return 0;
Greg Claytond9dc52d2011-09-24 05:04:40 +0000251}
252
Kate Stoneb9c1b512016-09-06 20:57:50 +0000253bool SBSection::operator==(const SBSection &rhs) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000254 LLDB_RECORD_METHOD(bool, SBSection, operator==,(const lldb::SBSection &),
255 rhs);
256
Kate Stoneb9c1b512016-09-06 20:57:50 +0000257 SectionSP lhs_section_sp(GetSP());
258 SectionSP rhs_section_sp(rhs.GetSP());
259 if (lhs_section_sp && rhs_section_sp)
260 return lhs_section_sp == rhs_section_sp;
261 return false;
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000262}
263
Kate Stoneb9c1b512016-09-06 20:57:50 +0000264bool SBSection::operator!=(const SBSection &rhs) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000265 LLDB_RECORD_METHOD(bool, SBSection, operator!=,(const lldb::SBSection &),
266 rhs);
267
Kate Stoneb9c1b512016-09-06 20:57:50 +0000268 SectionSP lhs_section_sp(GetSP());
269 SectionSP rhs_section_sp(rhs.GetSP());
270 return lhs_section_sp != rhs_section_sp;
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000271}
272
Kate Stoneb9c1b512016-09-06 20:57:50 +0000273bool SBSection::GetDescription(SBStream &description) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000274 LLDB_RECORD_METHOD(bool, SBSection, GetDescription, (lldb::SBStream &),
275 description);
276
Kate Stoneb9c1b512016-09-06 20:57:50 +0000277 Stream &strm = description.ref();
278
279 SectionSP section_sp(GetSP());
280 if (section_sp) {
281 const addr_t file_addr = section_sp->GetFileAddress();
282 strm.Printf("[0x%16.16" PRIx64 "-0x%16.16" PRIx64 ") ", file_addr,
283 file_addr + section_sp->GetByteSize());
284 section_sp->DumpName(&strm);
285 } else {
286 strm.PutCString("No value");
287 }
288
289 return true;
Matthew Gardinerc928de32014-10-22 07:22:56 +0000290}
Michal Gornyae211ec2019-03-19 17:13:13 +0000291
292namespace lldb_private {
293namespace repro {
294
295template <>
296void RegisterMethods<SBSection>(Registry &R) {
297 LLDB_REGISTER_CONSTRUCTOR(SBSection, ());
298 LLDB_REGISTER_CONSTRUCTOR(SBSection, (const lldb::SBSection &));
299 LLDB_REGISTER_METHOD(const lldb::SBSection &,
300 SBSection, operator=,(const lldb::SBSection &));
301 LLDB_REGISTER_METHOD_CONST(bool, SBSection, IsValid, ());
302 LLDB_REGISTER_METHOD_CONST(bool, SBSection, operator bool, ());
303 LLDB_REGISTER_METHOD(const char *, SBSection, GetName, ());
304 LLDB_REGISTER_METHOD(lldb::SBSection, SBSection, GetParent, ());
305 LLDB_REGISTER_METHOD(lldb::SBSection, SBSection, FindSubSection,
306 (const char *));
307 LLDB_REGISTER_METHOD(size_t, SBSection, GetNumSubSections, ());
308 LLDB_REGISTER_METHOD(lldb::SBSection, SBSection, GetSubSectionAtIndex,
309 (size_t));
310 LLDB_REGISTER_METHOD(lldb::addr_t, SBSection, GetFileAddress, ());
311 LLDB_REGISTER_METHOD(lldb::addr_t, SBSection, GetLoadAddress,
312 (lldb::SBTarget &));
313 LLDB_REGISTER_METHOD(lldb::addr_t, SBSection, GetByteSize, ());
314 LLDB_REGISTER_METHOD(uint64_t, SBSection, GetFileOffset, ());
315 LLDB_REGISTER_METHOD(uint64_t, SBSection, GetFileByteSize, ());
316 LLDB_REGISTER_METHOD(lldb::SBData, SBSection, GetSectionData, ());
317 LLDB_REGISTER_METHOD(lldb::SBData, SBSection, GetSectionData,
318 (uint64_t, uint64_t));
319 LLDB_REGISTER_METHOD(lldb::SectionType, SBSection, GetSectionType, ());
320 LLDB_REGISTER_METHOD_CONST(uint32_t, SBSection, GetPermissions, ());
321 LLDB_REGISTER_METHOD(uint32_t, SBSection, GetTargetByteSize, ());
322 LLDB_REGISTER_METHOD(bool, SBSection, operator==,(const lldb::SBSection &));
323 LLDB_REGISTER_METHOD(bool, SBSection, operator!=,(const lldb::SBSection &));
324 LLDB_REGISTER_METHOD(bool, SBSection, GetDescription, (lldb::SBStream &));
325}
326
327}
328}