1 //===-- SBSection.cpp -----------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lldb/API/SBSection.h"
10 #include "lldb/API/SBStream.h"
11 #include "lldb/API/SBTarget.h"
12 #include "lldb/Core/Module.h"
13 #include "lldb/Core/Section.h"
14 #include "lldb/Symbol/ObjectFile.h"
15 #include "lldb/Utility/DataBuffer.h"
16 #include "lldb/Utility/DataExtractor.h"
17 #include "lldb/Utility/Instrumentation.h"
18 #include "lldb/Utility/StreamString.h"
21 using namespace lldb_private;
23 SBSection::SBSection() { LLDB_INSTRUMENT_VA(this); }
25 SBSection::SBSection(const SBSection &rhs) : m_opaque_wp(rhs.m_opaque_wp) {
26 LLDB_INSTRUMENT_VA(this, rhs);
29 SBSection::SBSection(const lldb::SectionSP §ion_sp) {
30 // Don't init with section_sp otherwise this will throw if
31 // section_sp doesn't contain a valid Section *
33 m_opaque_wp = section_sp;
36 const SBSection &SBSection::operator=(const SBSection &rhs) {
37 LLDB_INSTRUMENT_VA(this, rhs);
39 m_opaque_wp = rhs.m_opaque_wp;
43 SBSection::~SBSection() = default;
45 bool SBSection::IsValid() const {
46 LLDB_INSTRUMENT_VA(this);
47 return this->operator bool();
49 SBSection::operator bool() const {
50 LLDB_INSTRUMENT_VA(this);
52 SectionSP section_sp(GetSP());
53 return section_sp && section_sp->GetModule().get() != nullptr;
56 const char *SBSection::GetName() {
57 LLDB_INSTRUMENT_VA(this);
59 SectionSP section_sp(GetSP());
61 return section_sp->GetName().GetCString();
65 lldb::SBSection SBSection::GetParent() {
66 LLDB_INSTRUMENT_VA(this);
68 lldb::SBSection sb_section;
69 SectionSP section_sp(GetSP());
71 SectionSP parent_section_sp(section_sp->GetParent());
72 if (parent_section_sp)
73 sb_section.SetSP(parent_section_sp);
78 lldb::SBSection SBSection::FindSubSection(const char *sect_name) {
79 LLDB_INSTRUMENT_VA(this, sect_name);
81 lldb::SBSection sb_section;
83 SectionSP section_sp(GetSP());
85 ConstString const_sect_name(sect_name);
87 section_sp->GetChildren().FindSectionByName(const_sect_name));
93 size_t SBSection::GetNumSubSections() {
94 LLDB_INSTRUMENT_VA(this);
96 SectionSP section_sp(GetSP());
98 return section_sp->GetChildren().GetSize();
102 lldb::SBSection SBSection::GetSubSectionAtIndex(size_t idx) {
103 LLDB_INSTRUMENT_VA(this, idx);
105 lldb::SBSection sb_section;
106 SectionSP section_sp(GetSP());
108 sb_section.SetSP(section_sp->GetChildren().GetSectionAtIndex(idx));
112 lldb::SectionSP SBSection::GetSP() const { return m_opaque_wp.lock(); }
114 void SBSection::SetSP(const lldb::SectionSP §ion_sp) {
115 m_opaque_wp = section_sp;
118 lldb::addr_t SBSection::GetFileAddress() {
119 LLDB_INSTRUMENT_VA(this);
121 lldb::addr_t file_addr = LLDB_INVALID_ADDRESS;
122 SectionSP section_sp(GetSP());
124 return section_sp->GetFileAddress();
128 lldb::addr_t SBSection::GetLoadAddress(lldb::SBTarget &sb_target) {
129 LLDB_INSTRUMENT_VA(this, sb_target);
131 TargetSP target_sp(sb_target.GetSP());
133 SectionSP section_sp(GetSP());
135 return section_sp->GetLoadBaseAddress(target_sp.get());
137 return LLDB_INVALID_ADDRESS;
140 lldb::addr_t SBSection::GetByteSize() {
141 LLDB_INSTRUMENT_VA(this);
143 SectionSP section_sp(GetSP());
145 return section_sp->GetByteSize();
149 uint64_t SBSection::GetFileOffset() {
150 LLDB_INSTRUMENT_VA(this);
152 SectionSP section_sp(GetSP());
154 ModuleSP module_sp(section_sp->GetModule());
156 ObjectFile *objfile = module_sp->GetObjectFile();
158 return objfile->GetFileOffset() + section_sp->GetFileOffset();
164 uint64_t SBSection::GetFileByteSize() {
165 LLDB_INSTRUMENT_VA(this);
167 SectionSP section_sp(GetSP());
169 return section_sp->GetFileSize();
173 SBData SBSection::GetSectionData() {
174 LLDB_INSTRUMENT_VA(this);
176 return GetSectionData(0, UINT64_MAX);
179 SBData SBSection::GetSectionData(uint64_t offset, uint64_t size) {
180 LLDB_INSTRUMENT_VA(this, offset, size);
183 SectionSP section_sp(GetSP());
185 const uint64_t sect_file_size = section_sp->GetFileSize();
186 if (sect_file_size > 0) {
187 ModuleSP module_sp(section_sp->GetModule());
189 ObjectFile *objfile = module_sp->GetObjectFile();
191 const uint64_t sect_file_offset =
192 objfile->GetFileOffset() + section_sp->GetFileOffset();
193 const uint64_t file_offset = sect_file_offset + offset;
194 uint64_t file_size = size;
195 if (file_size == UINT64_MAX) {
196 file_size = section_sp->GetByteSize();
197 if (file_size > offset)
202 auto data_buffer_sp = FileSystem::Instance().CreateDataBuffer(
203 objfile->GetFileSpec().GetPath(), file_size, file_offset);
204 if (data_buffer_sp && data_buffer_sp->GetByteSize() > 0) {
205 DataExtractorSP data_extractor_sp(
206 new DataExtractor(data_buffer_sp, objfile->GetByteOrder(),
207 objfile->GetAddressByteSize()));
209 sb_data.SetOpaque(data_extractor_sp);
218 SectionType SBSection::GetSectionType() {
219 LLDB_INSTRUMENT_VA(this);
221 SectionSP section_sp(GetSP());
222 if (section_sp.get())
223 return section_sp->GetType();
224 return eSectionTypeInvalid;
227 uint32_t SBSection::GetPermissions() const {
228 LLDB_INSTRUMENT_VA(this);
230 SectionSP section_sp(GetSP());
232 return section_sp->GetPermissions();
236 uint32_t SBSection::GetTargetByteSize() {
237 LLDB_INSTRUMENT_VA(this);
239 SectionSP section_sp(GetSP());
240 if (section_sp.get())
241 return section_sp->GetTargetByteSize();
245 bool SBSection::operator==(const SBSection &rhs) {
246 LLDB_INSTRUMENT_VA(this, rhs);
248 SectionSP lhs_section_sp(GetSP());
249 SectionSP rhs_section_sp(rhs.GetSP());
250 if (lhs_section_sp && rhs_section_sp)
251 return lhs_section_sp == rhs_section_sp;
255 bool SBSection::operator!=(const SBSection &rhs) {
256 LLDB_INSTRUMENT_VA(this, rhs);
258 SectionSP lhs_section_sp(GetSP());
259 SectionSP rhs_section_sp(rhs.GetSP());
260 return lhs_section_sp != rhs_section_sp;
263 bool SBSection::GetDescription(SBStream &description) {
264 LLDB_INSTRUMENT_VA(this, description);
266 Stream &strm = description.ref();
268 SectionSP section_sp(GetSP());
270 const addr_t file_addr = section_sp->GetFileAddress();
271 strm.Printf("[0x%16.16" PRIx64 "-0x%16.16" PRIx64 ") ", file_addr,
272 file_addr + section_sp->GetByteSize());
273 section_sp->DumpName(strm.AsRawOstream());
275 strm.PutCString("No value");