blob: 7ad7df963a749fc33496406268f0c59a79b95049 [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 Turner6f9e6902017-03-03 20:56:28 +000018#include "lldb/Utility/Log.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000019#include "lldb/Utility/StreamString.h"
Greg Claytoncac9c5f2011-09-24 00:52:29 +000020
Greg Claytoncac9c5f2011-09-24 00:52:29 +000021using namespace lldb;
22using namespace lldb_private;
23
Jonas Devliegherebaf56642019-03-06 00:06:00 +000024SBSection::SBSection() : m_opaque_wp() {
25 LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBSection);
26}
Greg Claytoncac9c5f2011-09-24 00:52:29 +000027
Jonas Devliegherebaf56642019-03-06 00:06:00 +000028SBSection::SBSection(const SBSection &rhs) : m_opaque_wp(rhs.m_opaque_wp) {
29 LLDB_RECORD_CONSTRUCTOR(SBSection, (const lldb::SBSection &), rhs);
30}
Kate Stoneb9c1b512016-09-06 20:57:50 +000031
32SBSection::SBSection(const lldb::SectionSP &section_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 Claytoncac9c5f2011-09-24 00:52:29 +000035{
Kate Stoneb9c1b512016-09-06 20:57:50 +000036 if (section_sp)
Greg Claytone72dfb32012-02-24 01:59:29 +000037 m_opaque_wp = section_sp;
Greg Claytoncac9c5f2011-09-24 00:52:29 +000038}
39
Kate Stoneb9c1b512016-09-06 20:57:50 +000040const SBSection &SBSection::operator=(const SBSection &rhs) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000041 LLDB_RECORD_METHOD(const lldb::SBSection &,
42 SBSection, operator=,(const lldb::SBSection &), rhs);
43
Kate Stoneb9c1b512016-09-06 20:57:50 +000044 m_opaque_wp = rhs.m_opaque_wp;
45 return *this;
Greg Claytoncac9c5f2011-09-24 00:52:29 +000046}
47
Kate Stoneb9c1b512016-09-06 20:57:50 +000048SBSection::~SBSection() {}
49
50bool SBSection::IsValid() const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000051 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBSection, IsValid);
52
Kate Stoneb9c1b512016-09-06 20:57:50 +000053 SectionSP section_sp(GetSP());
54 return section_sp && section_sp->GetModule().get() != NULL;
55}
56
57const char *SBSection::GetName() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000058 LLDB_RECORD_METHOD_NO_ARGS(const char *, SBSection, GetName);
59
Kate Stoneb9c1b512016-09-06 20:57:50 +000060 SectionSP section_sp(GetSP());
61 if (section_sp)
62 return section_sp->GetName().GetCString();
63 return NULL;
64}
65
66lldb::SBSection SBSection::GetParent() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000067 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBSection, SBSection, GetParent);
68
Kate Stoneb9c1b512016-09-06 20:57:50 +000069 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 Devliegherebaf56642019-03-06 00:06:00 +000076 return LLDB_RECORD_RESULT(sb_section);
Kate Stoneb9c1b512016-09-06 20:57:50 +000077}
78
79lldb::SBSection SBSection::FindSubSection(const char *sect_name) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000080 LLDB_RECORD_METHOD(lldb::SBSection, SBSection, FindSubSection, (const char *),
81 sect_name);
82
Kate Stoneb9c1b512016-09-06 20:57:50 +000083 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 Claytonfed39aa2012-06-27 22:22:28 +000090 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000091 }
Jonas Devliegherebaf56642019-03-06 00:06:00 +000092 return LLDB_RECORD_RESULT(sb_section);
Greg Claytonfed39aa2012-06-27 22:22:28 +000093}
94
Kate Stoneb9c1b512016-09-06 20:57:50 +000095size_t SBSection::GetNumSubSections() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000096 LLDB_RECORD_METHOD_NO_ARGS(size_t, SBSection, GetNumSubSections);
97
Kate Stoneb9c1b512016-09-06 20:57:50 +000098 SectionSP section_sp(GetSP());
99 if (section_sp)
100 return section_sp->GetChildren().GetSize();
101 return 0;
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000102}
103
Kate Stoneb9c1b512016-09-06 20:57:50 +0000104lldb::SBSection SBSection::GetSubSectionAtIndex(size_t idx) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000105 LLDB_RECORD_METHOD(lldb::SBSection, SBSection, GetSubSectionAtIndex, (size_t),
106 idx);
107
Kate Stoneb9c1b512016-09-06 20:57:50 +0000108 lldb::SBSection sb_section;
109 SectionSP section_sp(GetSP());
110 if (section_sp)
111 sb_section.SetSP(section_sp->GetChildren().GetSectionAtIndex(idx));
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000112 return LLDB_RECORD_RESULT(sb_section);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000113}
114
115lldb::SectionSP SBSection::GetSP() const { return m_opaque_wp.lock(); }
116
117void SBSection::SetSP(const lldb::SectionSP &section_sp) {
118 m_opaque_wp = section_sp;
119}
120
121lldb::addr_t SBSection::GetFileAddress() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000122 LLDB_RECORD_METHOD_NO_ARGS(lldb::addr_t, SBSection, GetFileAddress);
123
Kate Stoneb9c1b512016-09-06 20:57:50 +0000124 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
131lldb::addr_t SBSection::GetLoadAddress(lldb::SBTarget &sb_target) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000132 LLDB_RECORD_METHOD(lldb::addr_t, SBSection, GetLoadAddress,
133 (lldb::SBTarget &), sb_target);
134
Kate Stoneb9c1b512016-09-06 20:57:50 +0000135 TargetSP target_sp(sb_target.GetSP());
136 if (target_sp) {
137 SectionSP section_sp(GetSP());
Greg Claytone72dfb32012-02-24 01:59:29 +0000138 if (section_sp)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000139 return section_sp->GetLoadBaseAddress(target_sp.get());
140 }
141 return LLDB_INVALID_ADDRESS;
142}
143
144lldb::addr_t SBSection::GetByteSize() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000145 LLDB_RECORD_METHOD_NO_ARGS(lldb::addr_t, SBSection, GetByteSize);
146
Kate Stoneb9c1b512016-09-06 20:57:50 +0000147 SectionSP section_sp(GetSP());
148 if (section_sp)
149 return section_sp->GetByteSize();
150 return 0;
151}
152
153uint64_t SBSection::GetFileOffset() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000154 LLDB_RECORD_METHOD_NO_ARGS(uint64_t, SBSection, GetFileOffset);
155
Kate Stoneb9c1b512016-09-06 20:57:50 +0000156 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
168uint64_t SBSection::GetFileByteSize() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000169 LLDB_RECORD_METHOD_NO_ARGS(uint64_t, SBSection, GetFileByteSize);
170
Kate Stoneb9c1b512016-09-06 20:57:50 +0000171 SectionSP section_sp(GetSP());
172 if (section_sp)
173 return section_sp->GetFileSize();
174 return 0;
175}
176
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000177SBData SBSection::GetSectionData() {
178 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBData, SBSection, GetSectionData);
179
180 return LLDB_RECORD_RESULT(GetSectionData(0, UINT64_MAX));
181}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000182
183SBData SBSection::GetSectionData(uint64_t offset, uint64_t size) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000184 LLDB_RECORD_METHOD(lldb::SBData, SBSection, GetSectionData,
185 (uint64_t, uint64_t), offset, size);
186
Kate Stoneb9c1b512016-09-06 20:57:50 +0000187 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 Devlieghere87e403a2018-11-12 21:24:50 +0000207 auto data_buffer_sp = FileSystem::Instance().CreateDataBuffer(
Zachary Turner7f6a7a32017-03-06 23:42:14 +0000208 objfile->GetFileSpec().GetPath(), file_size, file_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000209 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 Claytoncac9c5f2011-09-24 00:52:29 +0000216 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000217 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000218 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000219 }
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000220 return LLDB_RECORD_RESULT(sb_data);
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000221}
222
Kate Stoneb9c1b512016-09-06 20:57:50 +0000223SectionType SBSection::GetSectionType() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000224 LLDB_RECORD_METHOD_NO_ARGS(lldb::SectionType, SBSection, GetSectionType);
225
Kate Stoneb9c1b512016-09-06 20:57:50 +0000226 SectionSP section_sp(GetSP());
227 if (section_sp.get())
228 return section_sp->GetType();
229 return eSectionTypeInvalid;
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000230}
231
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000232uint32_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 Aggarwal3c7f0702016-09-08 12:22:56 +0000239}
240
Kate Stoneb9c1b512016-09-06 20:57:50 +0000241uint32_t SBSection::GetTargetByteSize() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000242 LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBSection, GetTargetByteSize);
243
Kate Stoneb9c1b512016-09-06 20:57:50 +0000244 SectionSP section_sp(GetSP());
245 if (section_sp.get())
246 return section_sp->GetTargetByteSize();
247 return 0;
Greg Claytond9dc52d2011-09-24 05:04:40 +0000248}
249
Kate Stoneb9c1b512016-09-06 20:57:50 +0000250bool SBSection::operator==(const SBSection &rhs) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000251 LLDB_RECORD_METHOD(bool, SBSection, operator==,(const lldb::SBSection &),
252 rhs);
253
Kate Stoneb9c1b512016-09-06 20:57:50 +0000254 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 Claytoncac9c5f2011-09-24 00:52:29 +0000259}
260
Kate Stoneb9c1b512016-09-06 20:57:50 +0000261bool SBSection::operator!=(const SBSection &rhs) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000262 LLDB_RECORD_METHOD(bool, SBSection, operator!=,(const lldb::SBSection &),
263 rhs);
264
Kate Stoneb9c1b512016-09-06 20:57:50 +0000265 SectionSP lhs_section_sp(GetSP());
266 SectionSP rhs_section_sp(rhs.GetSP());
267 return lhs_section_sp != rhs_section_sp;
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000268}
269
Kate Stoneb9c1b512016-09-06 20:57:50 +0000270bool SBSection::GetDescription(SBStream &description) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000271 LLDB_RECORD_METHOD(bool, SBSection, GetDescription, (lldb::SBStream &),
272 description);
273
Kate Stoneb9c1b512016-09-06 20:57:50 +0000274 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 Gardinerc928de32014-10-22 07:22:56 +0000287}