blob: 0f1a9a994b910a3a048f518c4e54ebea046a768a [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;
44 return *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);
51
Kate Stoneb9c1b512016-09-06 20:57:50 +000052 SectionSP section_sp(GetSP());
53 return section_sp && section_sp->GetModule().get() != NULL;
54}
55
56const char *SBSection::GetName() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000057 LLDB_RECORD_METHOD_NO_ARGS(const char *, SBSection, GetName);
58
Kate Stoneb9c1b512016-09-06 20:57:50 +000059 SectionSP section_sp(GetSP());
60 if (section_sp)
61 return section_sp->GetName().GetCString();
62 return NULL;
63}
64
65lldb::SBSection SBSection::GetParent() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000066 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBSection, SBSection, GetParent);
67
Kate Stoneb9c1b512016-09-06 20:57:50 +000068 lldb::SBSection sb_section;
69 SectionSP section_sp(GetSP());
70 if (section_sp) {
71 SectionSP parent_section_sp(section_sp->GetParent());
72 if (parent_section_sp)
73 sb_section.SetSP(parent_section_sp);
74 }
Jonas Devliegherebaf56642019-03-06 00:06:00 +000075 return LLDB_RECORD_RESULT(sb_section);
Kate Stoneb9c1b512016-09-06 20:57:50 +000076}
77
78lldb::SBSection SBSection::FindSubSection(const char *sect_name) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000079 LLDB_RECORD_METHOD(lldb::SBSection, SBSection, FindSubSection, (const char *),
80 sect_name);
81
Kate Stoneb9c1b512016-09-06 20:57:50 +000082 lldb::SBSection sb_section;
83 if (sect_name) {
84 SectionSP section_sp(GetSP());
85 if (section_sp) {
86 ConstString const_sect_name(sect_name);
87 sb_section.SetSP(
88 section_sp->GetChildren().FindSectionByName(const_sect_name));
Greg Claytonfed39aa2012-06-27 22:22:28 +000089 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000090 }
Jonas Devliegherebaf56642019-03-06 00:06:00 +000091 return LLDB_RECORD_RESULT(sb_section);
Greg Claytonfed39aa2012-06-27 22:22:28 +000092}
93
Kate Stoneb9c1b512016-09-06 20:57:50 +000094size_t SBSection::GetNumSubSections() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000095 LLDB_RECORD_METHOD_NO_ARGS(size_t, SBSection, GetNumSubSections);
96
Kate Stoneb9c1b512016-09-06 20:57:50 +000097 SectionSP section_sp(GetSP());
98 if (section_sp)
99 return section_sp->GetChildren().GetSize();
100 return 0;
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000101}
102
Kate Stoneb9c1b512016-09-06 20:57:50 +0000103lldb::SBSection SBSection::GetSubSectionAtIndex(size_t idx) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000104 LLDB_RECORD_METHOD(lldb::SBSection, SBSection, GetSubSectionAtIndex, (size_t),
105 idx);
106
Kate Stoneb9c1b512016-09-06 20:57:50 +0000107 lldb::SBSection sb_section;
108 SectionSP section_sp(GetSP());
109 if (section_sp)
110 sb_section.SetSP(section_sp->GetChildren().GetSectionAtIndex(idx));
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000111 return LLDB_RECORD_RESULT(sb_section);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000112}
113
114lldb::SectionSP SBSection::GetSP() const { return m_opaque_wp.lock(); }
115
116void SBSection::SetSP(const lldb::SectionSP &section_sp) {
117 m_opaque_wp = section_sp;
118}
119
120lldb::addr_t SBSection::GetFileAddress() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000121 LLDB_RECORD_METHOD_NO_ARGS(lldb::addr_t, SBSection, GetFileAddress);
122
Kate Stoneb9c1b512016-09-06 20:57:50 +0000123 lldb::addr_t file_addr = LLDB_INVALID_ADDRESS;
124 SectionSP section_sp(GetSP());
125 if (section_sp)
126 return section_sp->GetFileAddress();
127 return file_addr;
128}
129
130lldb::addr_t SBSection::GetLoadAddress(lldb::SBTarget &sb_target) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000131 LLDB_RECORD_METHOD(lldb::addr_t, SBSection, GetLoadAddress,
132 (lldb::SBTarget &), sb_target);
133
Kate Stoneb9c1b512016-09-06 20:57:50 +0000134 TargetSP target_sp(sb_target.GetSP());
135 if (target_sp) {
136 SectionSP section_sp(GetSP());
Greg Claytone72dfb32012-02-24 01:59:29 +0000137 if (section_sp)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000138 return section_sp->GetLoadBaseAddress(target_sp.get());
139 }
140 return LLDB_INVALID_ADDRESS;
141}
142
143lldb::addr_t SBSection::GetByteSize() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000144 LLDB_RECORD_METHOD_NO_ARGS(lldb::addr_t, SBSection, GetByteSize);
145
Kate Stoneb9c1b512016-09-06 20:57:50 +0000146 SectionSP section_sp(GetSP());
147 if (section_sp)
148 return section_sp->GetByteSize();
149 return 0;
150}
151
152uint64_t SBSection::GetFileOffset() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000153 LLDB_RECORD_METHOD_NO_ARGS(uint64_t, SBSection, GetFileOffset);
154
Kate Stoneb9c1b512016-09-06 20:57:50 +0000155 SectionSP section_sp(GetSP());
156 if (section_sp) {
157 ModuleSP module_sp(section_sp->GetModule());
158 if (module_sp) {
159 ObjectFile *objfile = module_sp->GetObjectFile();
160 if (objfile)
161 return objfile->GetFileOffset() + section_sp->GetFileOffset();
162 }
163 }
164 return UINT64_MAX;
165}
166
167uint64_t SBSection::GetFileByteSize() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000168 LLDB_RECORD_METHOD_NO_ARGS(uint64_t, SBSection, GetFileByteSize);
169
Kate Stoneb9c1b512016-09-06 20:57:50 +0000170 SectionSP section_sp(GetSP());
171 if (section_sp)
172 return section_sp->GetFileSize();
173 return 0;
174}
175
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000176SBData SBSection::GetSectionData() {
177 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBData, SBSection, GetSectionData);
178
179 return LLDB_RECORD_RESULT(GetSectionData(0, UINT64_MAX));
180}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000181
182SBData SBSection::GetSectionData(uint64_t offset, uint64_t size) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000183 LLDB_RECORD_METHOD(lldb::SBData, SBSection, GetSectionData,
184 (uint64_t, uint64_t), offset, size);
185
Kate Stoneb9c1b512016-09-06 20:57:50 +0000186 SBData sb_data;
187 SectionSP section_sp(GetSP());
188 if (section_sp) {
189 const uint64_t sect_file_size = section_sp->GetFileSize();
190 if (sect_file_size > 0) {
191 ModuleSP module_sp(section_sp->GetModule());
192 if (module_sp) {
193 ObjectFile *objfile = module_sp->GetObjectFile();
194 if (objfile) {
195 const uint64_t sect_file_offset =
196 objfile->GetFileOffset() + section_sp->GetFileOffset();
197 const uint64_t file_offset = sect_file_offset + offset;
198 uint64_t file_size = size;
199 if (file_size == UINT64_MAX) {
200 file_size = section_sp->GetByteSize();
201 if (file_size > offset)
202 file_size -= offset;
203 else
204 file_size = 0;
205 }
Jonas Devlieghere87e403a2018-11-12 21:24:50 +0000206 auto data_buffer_sp = FileSystem::Instance().CreateDataBuffer(
Zachary Turner7f6a7a32017-03-06 23:42:14 +0000207 objfile->GetFileSpec().GetPath(), file_size, file_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000208 if (data_buffer_sp && data_buffer_sp->GetByteSize() > 0) {
209 DataExtractorSP data_extractor_sp(
210 new DataExtractor(data_buffer_sp, objfile->GetByteOrder(),
211 objfile->GetAddressByteSize()));
212
213 sb_data.SetOpaque(data_extractor_sp);
214 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000215 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000216 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000217 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000218 }
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000219 return LLDB_RECORD_RESULT(sb_data);
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000220}
221
Kate Stoneb9c1b512016-09-06 20:57:50 +0000222SectionType SBSection::GetSectionType() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000223 LLDB_RECORD_METHOD_NO_ARGS(lldb::SectionType, SBSection, GetSectionType);
224
Kate Stoneb9c1b512016-09-06 20:57:50 +0000225 SectionSP section_sp(GetSP());
226 if (section_sp.get())
227 return section_sp->GetType();
228 return eSectionTypeInvalid;
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000229}
230
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000231uint32_t SBSection::GetPermissions() const {
232 LLDB_RECORD_METHOD_CONST_NO_ARGS(uint32_t, SBSection, GetPermissions);
233
234 SectionSP section_sp(GetSP());
235 if (section_sp)
236 return section_sp->GetPermissions();
237 return 0;
Abhishek Aggarwal3c7f0702016-09-08 12:22:56 +0000238}
239
Kate Stoneb9c1b512016-09-06 20:57:50 +0000240uint32_t SBSection::GetTargetByteSize() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000241 LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBSection, GetTargetByteSize);
242
Kate Stoneb9c1b512016-09-06 20:57:50 +0000243 SectionSP section_sp(GetSP());
244 if (section_sp.get())
245 return section_sp->GetTargetByteSize();
246 return 0;
Greg Claytond9dc52d2011-09-24 05:04:40 +0000247}
248
Kate Stoneb9c1b512016-09-06 20:57:50 +0000249bool SBSection::operator==(const SBSection &rhs) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000250 LLDB_RECORD_METHOD(bool, SBSection, operator==,(const lldb::SBSection &),
251 rhs);
252
Kate Stoneb9c1b512016-09-06 20:57:50 +0000253 SectionSP lhs_section_sp(GetSP());
254 SectionSP rhs_section_sp(rhs.GetSP());
255 if (lhs_section_sp && rhs_section_sp)
256 return lhs_section_sp == rhs_section_sp;
257 return false;
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000258}
259
Kate Stoneb9c1b512016-09-06 20:57:50 +0000260bool SBSection::operator!=(const SBSection &rhs) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000261 LLDB_RECORD_METHOD(bool, SBSection, operator!=,(const lldb::SBSection &),
262 rhs);
263
Kate Stoneb9c1b512016-09-06 20:57:50 +0000264 SectionSP lhs_section_sp(GetSP());
265 SectionSP rhs_section_sp(rhs.GetSP());
266 return lhs_section_sp != rhs_section_sp;
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000267}
268
Kate Stoneb9c1b512016-09-06 20:57:50 +0000269bool SBSection::GetDescription(SBStream &description) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000270 LLDB_RECORD_METHOD(bool, SBSection, GetDescription, (lldb::SBStream &),
271 description);
272
Kate Stoneb9c1b512016-09-06 20:57:50 +0000273 Stream &strm = description.ref();
274
275 SectionSP section_sp(GetSP());
276 if (section_sp) {
277 const addr_t file_addr = section_sp->GetFileAddress();
278 strm.Printf("[0x%16.16" PRIx64 "-0x%16.16" PRIx64 ") ", file_addr,
279 file_addr + section_sp->GetByteSize());
280 section_sp->DumpName(&strm);
281 } else {
282 strm.PutCString("No value");
283 }
284
285 return true;
Matthew Gardinerc928de32014-10-22 07:22:56 +0000286}