1cfca06d7SDimitry Andric //===-- SBSection.cpp -----------------------------------------------------===//
2f034231aSEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f034231aSEd Maste //
7f034231aSEd Maste //===----------------------------------------------------------------------===//
8f034231aSEd Maste
9f034231aSEd Maste #include "lldb/API/SBSection.h"
10f034231aSEd Maste #include "lldb/API/SBStream.h"
11f034231aSEd Maste #include "lldb/API/SBTarget.h"
12f034231aSEd Maste #include "lldb/Core/Module.h"
13f034231aSEd Maste #include "lldb/Core/Section.h"
14f034231aSEd Maste #include "lldb/Symbol/ObjectFile.h"
1574a628f7SDimitry Andric #include "lldb/Utility/DataBuffer.h"
1674a628f7SDimitry Andric #include "lldb/Utility/DataExtractor.h"
176f8fc217SDimitry Andric #include "lldb/Utility/Instrumentation.h"
1874a628f7SDimitry Andric #include "lldb/Utility/StreamString.h"
19f034231aSEd Maste
20f034231aSEd Maste using namespace lldb;
21f034231aSEd Maste using namespace lldb_private;
22f034231aSEd Maste
SBSection()236f8fc217SDimitry Andric SBSection::SBSection() { LLDB_INSTRUMENT_VA(this); }
24f034231aSEd Maste
SBSection(const SBSection & rhs)255f29bb8aSDimitry Andric SBSection::SBSection(const SBSection &rhs) : m_opaque_wp(rhs.m_opaque_wp) {
266f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, rhs);
275f29bb8aSDimitry Andric }
28f034231aSEd Maste
SBSection(const lldb::SectionSP & section_sp)296f8fc217SDimitry Andric SBSection::SBSection(const lldb::SectionSP §ion_sp) {
306f8fc217SDimitry Andric // Don't init with section_sp otherwise this will throw if
3114f1b3e8SDimitry Andric // section_sp doesn't contain a valid Section *
32f034231aSEd Maste if (section_sp)
33f034231aSEd Maste m_opaque_wp = section_sp;
34f034231aSEd Maste }
35f034231aSEd Maste
operator =(const SBSection & rhs)3614f1b3e8SDimitry Andric const SBSection &SBSection::operator=(const SBSection &rhs) {
376f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, rhs);
385f29bb8aSDimitry Andric
39f034231aSEd Maste m_opaque_wp = rhs.m_opaque_wp;
406f8fc217SDimitry Andric return *this;
41f034231aSEd Maste }
42f034231aSEd Maste
43cfca06d7SDimitry Andric SBSection::~SBSection() = default;
44f034231aSEd Maste
IsValid() const4514f1b3e8SDimitry Andric bool SBSection::IsValid() const {
466f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
475f29bb8aSDimitry Andric return this->operator bool();
485f29bb8aSDimitry Andric }
operator bool() const495f29bb8aSDimitry Andric SBSection::operator bool() const {
506f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
515f29bb8aSDimitry Andric
52f034231aSEd Maste SectionSP section_sp(GetSP());
535f29bb8aSDimitry Andric return section_sp && section_sp->GetModule().get() != nullptr;
54f034231aSEd Maste }
55f034231aSEd Maste
GetName()5614f1b3e8SDimitry Andric const char *SBSection::GetName() {
576f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
585f29bb8aSDimitry Andric
59f034231aSEd Maste SectionSP section_sp(GetSP());
60f034231aSEd Maste if (section_sp)
61f034231aSEd Maste return section_sp->GetName().GetCString();
625f29bb8aSDimitry Andric return nullptr;
63f034231aSEd Maste }
64f034231aSEd Maste
GetParent()6514f1b3e8SDimitry Andric lldb::SBSection SBSection::GetParent() {
666f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
675f29bb8aSDimitry Andric
68f034231aSEd Maste lldb::SBSection sb_section;
69f034231aSEd Maste SectionSP section_sp(GetSP());
7014f1b3e8SDimitry Andric if (section_sp) {
71f034231aSEd Maste SectionSP parent_section_sp(section_sp->GetParent());
72f034231aSEd Maste if (parent_section_sp)
73f034231aSEd Maste sb_section.SetSP(parent_section_sp);
74f034231aSEd Maste }
756f8fc217SDimitry Andric return sb_section;
76f034231aSEd Maste }
77f034231aSEd Maste
FindSubSection(const char * sect_name)7814f1b3e8SDimitry Andric lldb::SBSection SBSection::FindSubSection(const char *sect_name) {
796f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, sect_name);
805f29bb8aSDimitry Andric
81f034231aSEd Maste lldb::SBSection sb_section;
8214f1b3e8SDimitry Andric if (sect_name) {
83f034231aSEd Maste SectionSP section_sp(GetSP());
8414f1b3e8SDimitry Andric if (section_sp) {
85f034231aSEd Maste ConstString const_sect_name(sect_name);
8614f1b3e8SDimitry Andric sb_section.SetSP(
8714f1b3e8SDimitry Andric section_sp->GetChildren().FindSectionByName(const_sect_name));
88f034231aSEd Maste }
89f034231aSEd Maste }
906f8fc217SDimitry Andric return sb_section;
91f034231aSEd Maste }
92f034231aSEd Maste
GetNumSubSections()9314f1b3e8SDimitry Andric size_t SBSection::GetNumSubSections() {
946f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
955f29bb8aSDimitry Andric
96f034231aSEd Maste SectionSP section_sp(GetSP());
97f034231aSEd Maste if (section_sp)
98f034231aSEd Maste return section_sp->GetChildren().GetSize();
99f034231aSEd Maste return 0;
100f034231aSEd Maste }
101f034231aSEd Maste
GetSubSectionAtIndex(size_t idx)10214f1b3e8SDimitry Andric lldb::SBSection SBSection::GetSubSectionAtIndex(size_t idx) {
1036f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, idx);
1045f29bb8aSDimitry Andric
105f034231aSEd Maste lldb::SBSection sb_section;
106f034231aSEd Maste SectionSP section_sp(GetSP());
107f034231aSEd Maste if (section_sp)
108f034231aSEd Maste sb_section.SetSP(section_sp->GetChildren().GetSectionAtIndex(idx));
1096f8fc217SDimitry Andric return sb_section;
110f034231aSEd Maste }
111f034231aSEd Maste
GetSP() const11214f1b3e8SDimitry Andric lldb::SectionSP SBSection::GetSP() const { return m_opaque_wp.lock(); }
113f034231aSEd Maste
SetSP(const lldb::SectionSP & section_sp)11414f1b3e8SDimitry Andric void SBSection::SetSP(const lldb::SectionSP §ion_sp) {
115f034231aSEd Maste m_opaque_wp = section_sp;
116f034231aSEd Maste }
117f034231aSEd Maste
GetFileAddress()11814f1b3e8SDimitry Andric lldb::addr_t SBSection::GetFileAddress() {
1196f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
1205f29bb8aSDimitry Andric
121f034231aSEd Maste lldb::addr_t file_addr = LLDB_INVALID_ADDRESS;
122f034231aSEd Maste SectionSP section_sp(GetSP());
123f034231aSEd Maste if (section_sp)
124f034231aSEd Maste return section_sp->GetFileAddress();
125f034231aSEd Maste return file_addr;
126f034231aSEd Maste }
127f034231aSEd Maste
GetLoadAddress(lldb::SBTarget & sb_target)12814f1b3e8SDimitry Andric lldb::addr_t SBSection::GetLoadAddress(lldb::SBTarget &sb_target) {
1296f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, sb_target);
1305f29bb8aSDimitry Andric
131f034231aSEd Maste TargetSP target_sp(sb_target.GetSP());
13214f1b3e8SDimitry Andric if (target_sp) {
133f034231aSEd Maste SectionSP section_sp(GetSP());
134f034231aSEd Maste if (section_sp)
135f034231aSEd Maste return section_sp->GetLoadBaseAddress(target_sp.get());
136f034231aSEd Maste }
137f034231aSEd Maste return LLDB_INVALID_ADDRESS;
138f034231aSEd Maste }
139f034231aSEd Maste
GetByteSize()14014f1b3e8SDimitry Andric lldb::addr_t SBSection::GetByteSize() {
1416f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
1425f29bb8aSDimitry Andric
143f034231aSEd Maste SectionSP section_sp(GetSP());
144f034231aSEd Maste if (section_sp)
145f034231aSEd Maste return section_sp->GetByteSize();
146f034231aSEd Maste return 0;
147f034231aSEd Maste }
148f034231aSEd Maste
GetFileOffset()14914f1b3e8SDimitry Andric uint64_t SBSection::GetFileOffset() {
1506f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
1515f29bb8aSDimitry Andric
152f034231aSEd Maste SectionSP section_sp(GetSP());
15314f1b3e8SDimitry Andric if (section_sp) {
154f034231aSEd Maste ModuleSP module_sp(section_sp->GetModule());
15514f1b3e8SDimitry Andric if (module_sp) {
156f034231aSEd Maste ObjectFile *objfile = module_sp->GetObjectFile();
157f034231aSEd Maste if (objfile)
158f034231aSEd Maste return objfile->GetFileOffset() + section_sp->GetFileOffset();
159f034231aSEd Maste }
160f034231aSEd Maste }
161f034231aSEd Maste return UINT64_MAX;
162f034231aSEd Maste }
163f034231aSEd Maste
GetFileByteSize()16414f1b3e8SDimitry Andric uint64_t SBSection::GetFileByteSize() {
1656f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
1665f29bb8aSDimitry Andric
167f034231aSEd Maste SectionSP section_sp(GetSP());
168f034231aSEd Maste if (section_sp)
169f034231aSEd Maste return section_sp->GetFileSize();
170f034231aSEd Maste return 0;
171f034231aSEd Maste }
172f034231aSEd Maste
GetSectionData()1735f29bb8aSDimitry Andric SBData SBSection::GetSectionData() {
1746f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
1755f29bb8aSDimitry Andric
1766f8fc217SDimitry Andric return GetSectionData(0, UINT64_MAX);
1775f29bb8aSDimitry Andric }
178f034231aSEd Maste
GetSectionData(uint64_t offset,uint64_t size)17914f1b3e8SDimitry Andric SBData SBSection::GetSectionData(uint64_t offset, uint64_t size) {
1806f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, offset, size);
1815f29bb8aSDimitry Andric
182f034231aSEd Maste SBData sb_data;
183f034231aSEd Maste SectionSP section_sp(GetSP());
18414f1b3e8SDimitry Andric if (section_sp) {
1857fa27ce4SDimitry Andric DataExtractor section_data;
1867fa27ce4SDimitry Andric section_sp->GetSectionData(section_data);
1877fa27ce4SDimitry Andric sb_data.SetOpaque(
1887fa27ce4SDimitry Andric std::make_shared<DataExtractor>(section_data, offset, size));
189f034231aSEd Maste }
1906f8fc217SDimitry Andric return sb_data;
191f034231aSEd Maste }
192f034231aSEd Maste
GetSectionType()19314f1b3e8SDimitry Andric SectionType SBSection::GetSectionType() {
1946f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
1955f29bb8aSDimitry Andric
196f034231aSEd Maste SectionSP section_sp(GetSP());
197f034231aSEd Maste if (section_sp.get())
198f034231aSEd Maste return section_sp->GetType();
199f034231aSEd Maste return eSectionTypeInvalid;
200f034231aSEd Maste }
201f034231aSEd Maste
GetPermissions() const2025f29bb8aSDimitry Andric uint32_t SBSection::GetPermissions() const {
2036f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
2045f29bb8aSDimitry Andric
205205afe67SEd Maste SectionSP section_sp(GetSP());
20614f1b3e8SDimitry Andric if (section_sp)
20714f1b3e8SDimitry Andric return section_sp->GetPermissions();
20814f1b3e8SDimitry Andric return 0;
20914f1b3e8SDimitry Andric }
21014f1b3e8SDimitry Andric
GetTargetByteSize()21114f1b3e8SDimitry Andric uint32_t SBSection::GetTargetByteSize() {
2126f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
2135f29bb8aSDimitry Andric
21414f1b3e8SDimitry Andric SectionSP section_sp(GetSP());
215205afe67SEd Maste if (section_sp.get())
216205afe67SEd Maste return section_sp->GetTargetByteSize();
217205afe67SEd Maste return 0;
218205afe67SEd Maste }
219f034231aSEd Maste
GetAlignment()2201f917f69SDimitry Andric uint32_t SBSection::GetAlignment() {
2211f917f69SDimitry Andric LLDB_INSTRUMENT_VA(this);
2221f917f69SDimitry Andric
2231f917f69SDimitry Andric SectionSP section_sp(GetSP());
2241f917f69SDimitry Andric if (section_sp.get())
2251f917f69SDimitry Andric return (1 << section_sp->GetLog2Align());
2261f917f69SDimitry Andric return 0;
2271f917f69SDimitry Andric }
2281f917f69SDimitry Andric
operator ==(const SBSection & rhs)22914f1b3e8SDimitry Andric bool SBSection::operator==(const SBSection &rhs) {
2306f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, rhs);
2315f29bb8aSDimitry Andric
232f034231aSEd Maste SectionSP lhs_section_sp(GetSP());
233f034231aSEd Maste SectionSP rhs_section_sp(rhs.GetSP());
234f034231aSEd Maste if (lhs_section_sp && rhs_section_sp)
235f034231aSEd Maste return lhs_section_sp == rhs_section_sp;
236f034231aSEd Maste return false;
237f034231aSEd Maste }
238f034231aSEd Maste
operator !=(const SBSection & rhs)23914f1b3e8SDimitry Andric bool SBSection::operator!=(const SBSection &rhs) {
2406f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, rhs);
2415f29bb8aSDimitry Andric
242f034231aSEd Maste SectionSP lhs_section_sp(GetSP());
243f034231aSEd Maste SectionSP rhs_section_sp(rhs.GetSP());
244f034231aSEd Maste return lhs_section_sp != rhs_section_sp;
245f034231aSEd Maste }
246f034231aSEd Maste
GetDescription(SBStream & description)24714f1b3e8SDimitry Andric bool SBSection::GetDescription(SBStream &description) {
2486f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, description);
2495f29bb8aSDimitry Andric
250f034231aSEd Maste Stream &strm = description.ref();
251f034231aSEd Maste
252f034231aSEd Maste SectionSP section_sp(GetSP());
25314f1b3e8SDimitry Andric if (section_sp) {
254f034231aSEd Maste const addr_t file_addr = section_sp->GetFileAddress();
25514f1b3e8SDimitry Andric strm.Printf("[0x%16.16" PRIx64 "-0x%16.16" PRIx64 ") ", file_addr,
25614f1b3e8SDimitry Andric file_addr + section_sp->GetByteSize());
257cfca06d7SDimitry Andric section_sp->DumpName(strm.AsRawOstream());
25814f1b3e8SDimitry Andric } else {
259f034231aSEd Maste strm.PutCString("No value");
260f034231aSEd Maste }
261f034231aSEd Maste
262f034231aSEd Maste return true;
263f034231aSEd Maste }
264