180814287SRaphael Isemann //===-- SBSection.cpp -----------------------------------------------------===//
2cac9c5f9SGreg Clayton //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6cac9c5f9SGreg Clayton //
7cac9c5f9SGreg Clayton //===----------------------------------------------------------------------===//
8cac9c5f9SGreg Clayton
9cac9c5f9SGreg Clayton #include "lldb/API/SBSection.h"
10cac9c5f9SGreg Clayton #include "lldb/API/SBStream.h"
11fed39aa6SGreg Clayton #include "lldb/API/SBTarget.h"
12cac9c5f9SGreg Clayton #include "lldb/Core/Module.h"
13d9dc52dcSGreg Clayton #include "lldb/Core/Section.h"
141f746071SGreg Clayton #include "lldb/Symbol/ObjectFile.h"
15666cc0b2SZachary Turner #include "lldb/Utility/DataBuffer.h"
16666cc0b2SZachary Turner #include "lldb/Utility/DataExtractor.h"
171755f5b1SJonas Devlieghere #include "lldb/Utility/Instrumentation.h"
18bf9a7730SZachary Turner #include "lldb/Utility/StreamString.h"
19cac9c5f9SGreg Clayton
20cac9c5f9SGreg Clayton using namespace lldb;
21cac9c5f9SGreg Clayton using namespace lldb_private;
22cac9c5f9SGreg Clayton
SBSection()231755f5b1SJonas Devlieghere SBSection::SBSection() { LLDB_INSTRUMENT_VA(this); }
24cac9c5f9SGreg Clayton
SBSection(const SBSection & rhs)25baf5664fSJonas Devlieghere SBSection::SBSection(const SBSection &rhs) : m_opaque_wp(rhs.m_opaque_wp) {
261755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this, rhs);
27baf5664fSJonas Devlieghere }
28cac9c5f9SGreg Clayton
SBSection(const lldb::SectionSP & section_sp)29a3436f73SKazu Hirata SBSection::SBSection(const lldb::SectionSP §ion_sp) {
30a3436f73SKazu Hirata // Don't init with section_sp otherwise this will throw if
31b9c1b51eSKate Stone // section_sp doesn't contain a valid Section *
32e72dfb32SGreg Clayton if (section_sp)
33e72dfb32SGreg Clayton m_opaque_wp = section_sp;
34cac9c5f9SGreg Clayton }
35cac9c5f9SGreg Clayton
operator =(const SBSection & rhs)36b9c1b51eSKate Stone const SBSection &SBSection::operator=(const SBSection &rhs) {
371755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this, rhs);
38baf5664fSJonas Devlieghere
39e72dfb32SGreg Clayton m_opaque_wp = rhs.m_opaque_wp;
40d232abc3SJonas Devlieghere return *this;
41cac9c5f9SGreg Clayton }
42cac9c5f9SGreg Clayton
43866b7a65SJonas Devlieghere SBSection::~SBSection() = default;
44cac9c5f9SGreg Clayton
IsValid() const45b9c1b51eSKate Stone bool SBSection::IsValid() const {
461755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this);
477f5237bcSPavel Labath return this->operator bool();
487f5237bcSPavel Labath }
operator bool() const497f5237bcSPavel Labath SBSection::operator bool() const {
501755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this);
51baf5664fSJonas Devlieghere
52e72dfb32SGreg Clayton SectionSP section_sp(GetSP());
53248a1305SKonrad Kleine return section_sp && section_sp->GetModule().get() != nullptr;
54cac9c5f9SGreg Clayton }
55cac9c5f9SGreg Clayton
GetName()56b9c1b51eSKate Stone const char *SBSection::GetName() {
571755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this);
58baf5664fSJonas Devlieghere
59e72dfb32SGreg Clayton SectionSP section_sp(GetSP());
60e72dfb32SGreg Clayton if (section_sp)
61e72dfb32SGreg Clayton return section_sp->GetName().GetCString();
62248a1305SKonrad Kleine return nullptr;
63f644ddf4SGreg Clayton }
64f644ddf4SGreg Clayton
GetParent()65b9c1b51eSKate Stone lldb::SBSection SBSection::GetParent() {
661755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this);
67baf5664fSJonas Devlieghere
681d4c5406SGreg Clayton lldb::SBSection sb_section;
691d4c5406SGreg Clayton SectionSP section_sp(GetSP());
70b9c1b51eSKate Stone if (section_sp) {
711d4c5406SGreg Clayton SectionSP parent_section_sp(section_sp->GetParent());
721d4c5406SGreg Clayton if (parent_section_sp)
731d4c5406SGreg Clayton sb_section.SetSP(parent_section_sp);
741d4c5406SGreg Clayton }
75d232abc3SJonas Devlieghere return sb_section;
761d4c5406SGreg Clayton }
771d4c5406SGreg Clayton
FindSubSection(const char * sect_name)78b9c1b51eSKate Stone lldb::SBSection SBSection::FindSubSection(const char *sect_name) {
791755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this, sect_name);
80baf5664fSJonas Devlieghere
81cac9c5f9SGreg Clayton lldb::SBSection sb_section;
82b9c1b51eSKate Stone if (sect_name) {
83e72dfb32SGreg Clayton SectionSP section_sp(GetSP());
84b9c1b51eSKate Stone if (section_sp) {
85cac9c5f9SGreg Clayton ConstString const_sect_name(sect_name);
86b9c1b51eSKate Stone sb_section.SetSP(
87b9c1b51eSKate Stone section_sp->GetChildren().FindSectionByName(const_sect_name));
88e72dfb32SGreg Clayton }
89cac9c5f9SGreg Clayton }
90d232abc3SJonas Devlieghere return sb_section;
91cac9c5f9SGreg Clayton }
92cac9c5f9SGreg Clayton
GetNumSubSections()93b9c1b51eSKate Stone size_t SBSection::GetNumSubSections() {
941755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this);
95baf5664fSJonas Devlieghere
96e72dfb32SGreg Clayton SectionSP section_sp(GetSP());
97e72dfb32SGreg Clayton if (section_sp)
98e72dfb32SGreg Clayton return section_sp->GetChildren().GetSize();
99cac9c5f9SGreg Clayton return 0;
100cac9c5f9SGreg Clayton }
101cac9c5f9SGreg Clayton
GetSubSectionAtIndex(size_t idx)102b9c1b51eSKate Stone lldb::SBSection SBSection::GetSubSectionAtIndex(size_t idx) {
1031755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this, idx);
104baf5664fSJonas Devlieghere
105cac9c5f9SGreg Clayton lldb::SBSection sb_section;
106e72dfb32SGreg Clayton SectionSP section_sp(GetSP());
107e72dfb32SGreg Clayton if (section_sp)
108e72dfb32SGreg Clayton sb_section.SetSP(section_sp->GetChildren().GetSectionAtIndex(idx));
109d232abc3SJonas Devlieghere return sb_section;
110cac9c5f9SGreg Clayton }
111cac9c5f9SGreg Clayton
GetSP() const112b9c1b51eSKate Stone lldb::SectionSP SBSection::GetSP() const { return m_opaque_wp.lock(); }
113cac9c5f9SGreg Clayton
SetSP(const lldb::SectionSP & section_sp)114b9c1b51eSKate Stone void SBSection::SetSP(const lldb::SectionSP §ion_sp) {
115e72dfb32SGreg Clayton m_opaque_wp = section_sp;
116cac9c5f9SGreg Clayton }
117cac9c5f9SGreg Clayton
GetFileAddress()118b9c1b51eSKate Stone lldb::addr_t SBSection::GetFileAddress() {
1191755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this);
120baf5664fSJonas Devlieghere
121cac9c5f9SGreg Clayton lldb::addr_t file_addr = LLDB_INVALID_ADDRESS;
122e72dfb32SGreg Clayton SectionSP section_sp(GetSP());
123e72dfb32SGreg Clayton if (section_sp)
124e72dfb32SGreg Clayton return section_sp->GetFileAddress();
125cac9c5f9SGreg Clayton return file_addr;
126cac9c5f9SGreg Clayton }
127cac9c5f9SGreg Clayton
GetLoadAddress(lldb::SBTarget & sb_target)128b9c1b51eSKate Stone lldb::addr_t SBSection::GetLoadAddress(lldb::SBTarget &sb_target) {
1291755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this, sb_target);
130baf5664fSJonas Devlieghere
131fed39aa6SGreg Clayton TargetSP target_sp(sb_target.GetSP());
132b9c1b51eSKate Stone if (target_sp) {
133fed39aa6SGreg Clayton SectionSP section_sp(GetSP());
134fed39aa6SGreg Clayton if (section_sp)
135fed39aa6SGreg Clayton return section_sp->GetLoadBaseAddress(target_sp.get());
136fed39aa6SGreg Clayton }
137fed39aa6SGreg Clayton return LLDB_INVALID_ADDRESS;
138fed39aa6SGreg Clayton }
139fed39aa6SGreg Clayton
GetByteSize()140b9c1b51eSKate Stone lldb::addr_t SBSection::GetByteSize() {
1411755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this);
142baf5664fSJonas Devlieghere
143e72dfb32SGreg Clayton SectionSP section_sp(GetSP());
144e72dfb32SGreg Clayton if (section_sp)
145e72dfb32SGreg Clayton return section_sp->GetByteSize();
146cac9c5f9SGreg Clayton return 0;
147cac9c5f9SGreg Clayton }
148cac9c5f9SGreg Clayton
GetFileOffset()149b9c1b51eSKate Stone uint64_t SBSection::GetFileOffset() {
1501755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this);
151baf5664fSJonas Devlieghere
152e72dfb32SGreg Clayton SectionSP section_sp(GetSP());
153b9c1b51eSKate Stone if (section_sp) {
154e72dfb32SGreg Clayton ModuleSP module_sp(section_sp->GetModule());
155b9c1b51eSKate Stone if (module_sp) {
156e72dfb32SGreg Clayton ObjectFile *objfile = module_sp->GetObjectFile();
157cac9c5f9SGreg Clayton if (objfile)
1585ce9c565SGreg Clayton return objfile->GetFileOffset() + section_sp->GetFileOffset();
159cac9c5f9SGreg Clayton }
160cac9c5f9SGreg Clayton }
161e72dfb32SGreg Clayton return UINT64_MAX;
162cac9c5f9SGreg Clayton }
163cac9c5f9SGreg Clayton
GetFileByteSize()164b9c1b51eSKate Stone uint64_t SBSection::GetFileByteSize() {
1651755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this);
166baf5664fSJonas Devlieghere
167e72dfb32SGreg Clayton SectionSP section_sp(GetSP());
168e72dfb32SGreg Clayton if (section_sp)
169e72dfb32SGreg Clayton return section_sp->GetFileSize();
170cac9c5f9SGreg Clayton return 0;
171cac9c5f9SGreg Clayton }
172cac9c5f9SGreg Clayton
GetSectionData()173baf5664fSJonas Devlieghere SBData SBSection::GetSectionData() {
1741755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this);
175baf5664fSJonas Devlieghere
176d232abc3SJonas Devlieghere return GetSectionData(0, UINT64_MAX);
177baf5664fSJonas Devlieghere }
178d9dc52dcSGreg Clayton
GetSectionData(uint64_t offset,uint64_t size)179b9c1b51eSKate Stone SBData SBSection::GetSectionData(uint64_t offset, uint64_t size) {
1801755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this, offset, size);
181baf5664fSJonas Devlieghere
182cac9c5f9SGreg Clayton SBData sb_data;
183e72dfb32SGreg Clayton SectionSP section_sp(GetSP());
184b9c1b51eSKate Stone if (section_sp) {
185e72dfb32SGreg Clayton const uint64_t sect_file_size = section_sp->GetFileSize();
186b9c1b51eSKate Stone if (sect_file_size > 0) {
187e72dfb32SGreg Clayton ModuleSP module_sp(section_sp->GetModule());
188b9c1b51eSKate Stone if (module_sp) {
189e72dfb32SGreg Clayton ObjectFile *objfile = module_sp->GetObjectFile();
190b9c1b51eSKate Stone if (objfile) {
191b9c1b51eSKate Stone const uint64_t sect_file_offset =
192b9c1b51eSKate Stone objfile->GetFileOffset() + section_sp->GetFileOffset();
193cac9c5f9SGreg Clayton const uint64_t file_offset = sect_file_offset + offset;
194cac9c5f9SGreg Clayton uint64_t file_size = size;
195b9c1b51eSKate Stone if (file_size == UINT64_MAX) {
196e72dfb32SGreg Clayton file_size = section_sp->GetByteSize();
197cac9c5f9SGreg Clayton if (file_size > offset)
198cac9c5f9SGreg Clayton file_size -= offset;
199cac9c5f9SGreg Clayton else
200cac9c5f9SGreg Clayton file_size = 0;
201cac9c5f9SGreg Clayton }
20287e403aaSJonas Devlieghere auto data_buffer_sp = FileSystem::Instance().CreateDataBuffer(
2037f6a7a37SZachary Turner objfile->GetFileSpec().GetPath(), file_size, file_offset);
204b9c1b51eSKate Stone if (data_buffer_sp && data_buffer_sp->GetByteSize() > 0) {
205b9c1b51eSKate Stone DataExtractorSP data_extractor_sp(
206b9c1b51eSKate Stone new DataExtractor(data_buffer_sp, objfile->GetByteOrder(),
207cac9c5f9SGreg Clayton objfile->GetAddressByteSize()));
208cac9c5f9SGreg Clayton
209cac9c5f9SGreg Clayton sb_data.SetOpaque(data_extractor_sp);
210cac9c5f9SGreg Clayton }
211cac9c5f9SGreg Clayton }
212cac9c5f9SGreg Clayton }
213cac9c5f9SGreg Clayton }
214cac9c5f9SGreg Clayton }
215d232abc3SJonas Devlieghere return sb_data;
216cac9c5f9SGreg Clayton }
217cac9c5f9SGreg Clayton
GetSectionType()218b9c1b51eSKate Stone SectionType SBSection::GetSectionType() {
2191755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this);
220baf5664fSJonas Devlieghere
221e72dfb32SGreg Clayton SectionSP section_sp(GetSP());
222e72dfb32SGreg Clayton if (section_sp.get())
223e72dfb32SGreg Clayton return section_sp->GetType();
224cac9c5f9SGreg Clayton return eSectionTypeInvalid;
225cac9c5f9SGreg Clayton }
226cac9c5f9SGreg Clayton
GetPermissions() const227baf5664fSJonas Devlieghere uint32_t SBSection::GetPermissions() const {
2281755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this);
229baf5664fSJonas Devlieghere
2303c7f0709SAbhishek Aggarwal SectionSP section_sp(GetSP());
2313c7f0709SAbhishek Aggarwal if (section_sp)
2323c7f0709SAbhishek Aggarwal return section_sp->GetPermissions();
2333c7f0709SAbhishek Aggarwal return 0;
2343c7f0709SAbhishek Aggarwal }
2353c7f0709SAbhishek Aggarwal
GetTargetByteSize()236b9c1b51eSKate Stone uint32_t SBSection::GetTargetByteSize() {
2371755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this);
238baf5664fSJonas Devlieghere
239c928de3eSMatthew Gardiner SectionSP section_sp(GetSP());
240c928de3eSMatthew Gardiner if (section_sp.get())
241c928de3eSMatthew Gardiner return section_sp->GetTargetByteSize();
242c928de3eSMatthew Gardiner return 0;
243c928de3eSMatthew Gardiner }
244cac9c5f9SGreg Clayton
GetAlignment()245*1e3ee766SDavid M. Lary uint32_t SBSection::GetAlignment() {
246*1e3ee766SDavid M. Lary LLDB_INSTRUMENT_VA(this);
247*1e3ee766SDavid M. Lary
248*1e3ee766SDavid M. Lary SectionSP section_sp(GetSP());
249*1e3ee766SDavid M. Lary if (section_sp.get())
250*1e3ee766SDavid M. Lary return (1 << section_sp->GetLog2Align());
251*1e3ee766SDavid M. Lary return 0;
252*1e3ee766SDavid M. Lary }
253*1e3ee766SDavid M. Lary
operator ==(const SBSection & rhs)254b9c1b51eSKate Stone bool SBSection::operator==(const SBSection &rhs) {
2551755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this, rhs);
256baf5664fSJonas Devlieghere
257e72dfb32SGreg Clayton SectionSP lhs_section_sp(GetSP());
258e72dfb32SGreg Clayton SectionSP rhs_section_sp(rhs.GetSP());
259e72dfb32SGreg Clayton if (lhs_section_sp && rhs_section_sp)
260e72dfb32SGreg Clayton return lhs_section_sp == rhs_section_sp;
261cac9c5f9SGreg Clayton return false;
262cac9c5f9SGreg Clayton }
263cac9c5f9SGreg Clayton
operator !=(const SBSection & rhs)264b9c1b51eSKate Stone bool SBSection::operator!=(const SBSection &rhs) {
2651755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this, rhs);
266baf5664fSJonas Devlieghere
267e72dfb32SGreg Clayton SectionSP lhs_section_sp(GetSP());
268e72dfb32SGreg Clayton SectionSP rhs_section_sp(rhs.GetSP());
269e72dfb32SGreg Clayton return lhs_section_sp != rhs_section_sp;
270cac9c5f9SGreg Clayton }
271cac9c5f9SGreg Clayton
GetDescription(SBStream & description)272b9c1b51eSKate Stone bool SBSection::GetDescription(SBStream &description) {
2731755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this, description);
274baf5664fSJonas Devlieghere
275da7bc7d0SGreg Clayton Stream &strm = description.ref();
276da7bc7d0SGreg Clayton
277e72dfb32SGreg Clayton SectionSP section_sp(GetSP());
278b9c1b51eSKate Stone if (section_sp) {
279e72dfb32SGreg Clayton const addr_t file_addr = section_sp->GetFileAddress();
280b9c1b51eSKate Stone strm.Printf("[0x%16.16" PRIx64 "-0x%16.16" PRIx64 ") ", file_addr,
281b9c1b51eSKate Stone file_addr + section_sp->GetByteSize());
2823a168297SPavel Labath section_sp->DumpName(strm.AsRawOstream());
283b9c1b51eSKate Stone } else {
284da7bc7d0SGreg Clayton strm.PutCString("No value");
285cac9c5f9SGreg Clayton }
286cac9c5f9SGreg Clayton
287cac9c5f9SGreg Clayton return true;
288cac9c5f9SGreg Clayton }
289