15ffd83dbSDimitry Andric //===-- SBSection.cpp -----------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "lldb/API/SBSection.h"
100b57cec5SDimitry Andric #include "lldb/API/SBStream.h"
110b57cec5SDimitry Andric #include "lldb/API/SBTarget.h"
120b57cec5SDimitry Andric #include "lldb/Core/Module.h"
130b57cec5SDimitry Andric #include "lldb/Core/Section.h"
140b57cec5SDimitry Andric #include "lldb/Symbol/ObjectFile.h"
150b57cec5SDimitry Andric #include "lldb/Utility/DataBuffer.h"
160b57cec5SDimitry Andric #include "lldb/Utility/DataExtractor.h"
1704eeddc0SDimitry Andric #include "lldb/Utility/Instrumentation.h"
180b57cec5SDimitry Andric #include "lldb/Utility/StreamString.h"
190b57cec5SDimitry Andric 
200b57cec5SDimitry Andric using namespace lldb;
210b57cec5SDimitry Andric using namespace lldb_private;
220b57cec5SDimitry Andric 
SBSection()2304eeddc0SDimitry Andric SBSection::SBSection() { LLDB_INSTRUMENT_VA(this); }
240b57cec5SDimitry Andric 
SBSection(const SBSection & rhs)250b57cec5SDimitry Andric SBSection::SBSection(const SBSection &rhs) : m_opaque_wp(rhs.m_opaque_wp) {
2604eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, rhs);
270b57cec5SDimitry Andric }
280b57cec5SDimitry Andric 
SBSection(const lldb::SectionSP & section_sp)2904eeddc0SDimitry Andric SBSection::SBSection(const lldb::SectionSP &section_sp) {
3004eeddc0SDimitry Andric   // Don't init with section_sp otherwise this will throw if
310b57cec5SDimitry Andric   // section_sp doesn't contain a valid Section *
320b57cec5SDimitry Andric   if (section_sp)
330b57cec5SDimitry Andric     m_opaque_wp = section_sp;
340b57cec5SDimitry Andric }
350b57cec5SDimitry Andric 
operator =(const SBSection & rhs)360b57cec5SDimitry Andric const SBSection &SBSection::operator=(const SBSection &rhs) {
3704eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, rhs);
380b57cec5SDimitry Andric 
390b57cec5SDimitry Andric   m_opaque_wp = rhs.m_opaque_wp;
4004eeddc0SDimitry Andric   return *this;
410b57cec5SDimitry Andric }
420b57cec5SDimitry Andric 
435ffd83dbSDimitry Andric SBSection::~SBSection() = default;
440b57cec5SDimitry Andric 
IsValid() const450b57cec5SDimitry Andric bool SBSection::IsValid() const {
4604eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
470b57cec5SDimitry Andric   return this->operator bool();
480b57cec5SDimitry Andric }
operator bool() const490b57cec5SDimitry Andric SBSection::operator bool() const {
5004eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
510b57cec5SDimitry Andric 
520b57cec5SDimitry Andric   SectionSP section_sp(GetSP());
530b57cec5SDimitry Andric   return section_sp && section_sp->GetModule().get() != nullptr;
540b57cec5SDimitry Andric }
550b57cec5SDimitry Andric 
GetName()560b57cec5SDimitry Andric const char *SBSection::GetName() {
5704eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
580b57cec5SDimitry Andric 
590b57cec5SDimitry Andric   SectionSP section_sp(GetSP());
600b57cec5SDimitry Andric   if (section_sp)
610b57cec5SDimitry Andric     return section_sp->GetName().GetCString();
620b57cec5SDimitry Andric   return nullptr;
630b57cec5SDimitry Andric }
640b57cec5SDimitry Andric 
GetParent()650b57cec5SDimitry Andric lldb::SBSection SBSection::GetParent() {
6604eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
670b57cec5SDimitry Andric 
680b57cec5SDimitry Andric   lldb::SBSection sb_section;
690b57cec5SDimitry Andric   SectionSP section_sp(GetSP());
700b57cec5SDimitry Andric   if (section_sp) {
710b57cec5SDimitry Andric     SectionSP parent_section_sp(section_sp->GetParent());
720b57cec5SDimitry Andric     if (parent_section_sp)
730b57cec5SDimitry Andric       sb_section.SetSP(parent_section_sp);
740b57cec5SDimitry Andric   }
7504eeddc0SDimitry Andric   return sb_section;
760b57cec5SDimitry Andric }
770b57cec5SDimitry Andric 
FindSubSection(const char * sect_name)780b57cec5SDimitry Andric lldb::SBSection SBSection::FindSubSection(const char *sect_name) {
7904eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, sect_name);
800b57cec5SDimitry Andric 
810b57cec5SDimitry Andric   lldb::SBSection sb_section;
820b57cec5SDimitry Andric   if (sect_name) {
830b57cec5SDimitry Andric     SectionSP section_sp(GetSP());
840b57cec5SDimitry Andric     if (section_sp) {
850b57cec5SDimitry Andric       ConstString const_sect_name(sect_name);
860b57cec5SDimitry Andric       sb_section.SetSP(
870b57cec5SDimitry Andric           section_sp->GetChildren().FindSectionByName(const_sect_name));
880b57cec5SDimitry Andric     }
890b57cec5SDimitry Andric   }
9004eeddc0SDimitry Andric   return sb_section;
910b57cec5SDimitry Andric }
920b57cec5SDimitry Andric 
GetNumSubSections()930b57cec5SDimitry Andric size_t SBSection::GetNumSubSections() {
9404eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
950b57cec5SDimitry Andric 
960b57cec5SDimitry Andric   SectionSP section_sp(GetSP());
970b57cec5SDimitry Andric   if (section_sp)
980b57cec5SDimitry Andric     return section_sp->GetChildren().GetSize();
990b57cec5SDimitry Andric   return 0;
1000b57cec5SDimitry Andric }
1010b57cec5SDimitry Andric 
GetSubSectionAtIndex(size_t idx)1020b57cec5SDimitry Andric lldb::SBSection SBSection::GetSubSectionAtIndex(size_t idx) {
10304eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, idx);
1040b57cec5SDimitry Andric 
1050b57cec5SDimitry Andric   lldb::SBSection sb_section;
1060b57cec5SDimitry Andric   SectionSP section_sp(GetSP());
1070b57cec5SDimitry Andric   if (section_sp)
1080b57cec5SDimitry Andric     sb_section.SetSP(section_sp->GetChildren().GetSectionAtIndex(idx));
10904eeddc0SDimitry Andric   return sb_section;
1100b57cec5SDimitry Andric }
1110b57cec5SDimitry Andric 
GetSP() const1120b57cec5SDimitry Andric lldb::SectionSP SBSection::GetSP() const { return m_opaque_wp.lock(); }
1130b57cec5SDimitry Andric 
SetSP(const lldb::SectionSP & section_sp)1140b57cec5SDimitry Andric void SBSection::SetSP(const lldb::SectionSP &section_sp) {
1150b57cec5SDimitry Andric   m_opaque_wp = section_sp;
1160b57cec5SDimitry Andric }
1170b57cec5SDimitry Andric 
GetFileAddress()1180b57cec5SDimitry Andric lldb::addr_t SBSection::GetFileAddress() {
11904eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
1200b57cec5SDimitry Andric 
1210b57cec5SDimitry Andric   lldb::addr_t file_addr = LLDB_INVALID_ADDRESS;
1220b57cec5SDimitry Andric   SectionSP section_sp(GetSP());
1230b57cec5SDimitry Andric   if (section_sp)
1240b57cec5SDimitry Andric     return section_sp->GetFileAddress();
1250b57cec5SDimitry Andric   return file_addr;
1260b57cec5SDimitry Andric }
1270b57cec5SDimitry Andric 
GetLoadAddress(lldb::SBTarget & sb_target)1280b57cec5SDimitry Andric lldb::addr_t SBSection::GetLoadAddress(lldb::SBTarget &sb_target) {
12904eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, sb_target);
1300b57cec5SDimitry Andric 
1310b57cec5SDimitry Andric   TargetSP target_sp(sb_target.GetSP());
1320b57cec5SDimitry Andric   if (target_sp) {
1330b57cec5SDimitry Andric     SectionSP section_sp(GetSP());
1340b57cec5SDimitry Andric     if (section_sp)
1350b57cec5SDimitry Andric       return section_sp->GetLoadBaseAddress(target_sp.get());
1360b57cec5SDimitry Andric   }
1370b57cec5SDimitry Andric   return LLDB_INVALID_ADDRESS;
1380b57cec5SDimitry Andric }
1390b57cec5SDimitry Andric 
GetByteSize()1400b57cec5SDimitry Andric lldb::addr_t SBSection::GetByteSize() {
14104eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
1420b57cec5SDimitry Andric 
1430b57cec5SDimitry Andric   SectionSP section_sp(GetSP());
1440b57cec5SDimitry Andric   if (section_sp)
1450b57cec5SDimitry Andric     return section_sp->GetByteSize();
1460b57cec5SDimitry Andric   return 0;
1470b57cec5SDimitry Andric }
1480b57cec5SDimitry Andric 
GetFileOffset()1490b57cec5SDimitry Andric uint64_t SBSection::GetFileOffset() {
15004eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
1510b57cec5SDimitry Andric 
1520b57cec5SDimitry Andric   SectionSP section_sp(GetSP());
1530b57cec5SDimitry Andric   if (section_sp) {
1540b57cec5SDimitry Andric     ModuleSP module_sp(section_sp->GetModule());
1550b57cec5SDimitry Andric     if (module_sp) {
1560b57cec5SDimitry Andric       ObjectFile *objfile = module_sp->GetObjectFile();
1570b57cec5SDimitry Andric       if (objfile)
1580b57cec5SDimitry Andric         return objfile->GetFileOffset() + section_sp->GetFileOffset();
1590b57cec5SDimitry Andric     }
1600b57cec5SDimitry Andric   }
1610b57cec5SDimitry Andric   return UINT64_MAX;
1620b57cec5SDimitry Andric }
1630b57cec5SDimitry Andric 
GetFileByteSize()1640b57cec5SDimitry Andric uint64_t SBSection::GetFileByteSize() {
16504eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
1660b57cec5SDimitry Andric 
1670b57cec5SDimitry Andric   SectionSP section_sp(GetSP());
1680b57cec5SDimitry Andric   if (section_sp)
1690b57cec5SDimitry Andric     return section_sp->GetFileSize();
1700b57cec5SDimitry Andric   return 0;
1710b57cec5SDimitry Andric }
1720b57cec5SDimitry Andric 
GetSectionData()1730b57cec5SDimitry Andric SBData SBSection::GetSectionData() {
17404eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
1750b57cec5SDimitry Andric 
17604eeddc0SDimitry Andric   return GetSectionData(0, UINT64_MAX);
1770b57cec5SDimitry Andric }
1780b57cec5SDimitry Andric 
GetSectionData(uint64_t offset,uint64_t size)1790b57cec5SDimitry Andric SBData SBSection::GetSectionData(uint64_t offset, uint64_t size) {
18004eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, offset, size);
1810b57cec5SDimitry Andric 
1820b57cec5SDimitry Andric   SBData sb_data;
1830b57cec5SDimitry Andric   SectionSP section_sp(GetSP());
1840b57cec5SDimitry Andric   if (section_sp) {
185*fe013be4SDimitry Andric     DataExtractor section_data;
186*fe013be4SDimitry Andric     section_sp->GetSectionData(section_data);
187*fe013be4SDimitry Andric     sb_data.SetOpaque(
188*fe013be4SDimitry Andric         std::make_shared<DataExtractor>(section_data, offset, size));
1890b57cec5SDimitry Andric   }
19004eeddc0SDimitry Andric   return sb_data;
1910b57cec5SDimitry Andric }
1920b57cec5SDimitry Andric 
GetSectionType()1930b57cec5SDimitry Andric SectionType SBSection::GetSectionType() {
19404eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
1950b57cec5SDimitry Andric 
1960b57cec5SDimitry Andric   SectionSP section_sp(GetSP());
1970b57cec5SDimitry Andric   if (section_sp.get())
1980b57cec5SDimitry Andric     return section_sp->GetType();
1990b57cec5SDimitry Andric   return eSectionTypeInvalid;
2000b57cec5SDimitry Andric }
2010b57cec5SDimitry Andric 
GetPermissions() const2020b57cec5SDimitry Andric uint32_t SBSection::GetPermissions() const {
20304eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
2040b57cec5SDimitry Andric 
2050b57cec5SDimitry Andric   SectionSP section_sp(GetSP());
2060b57cec5SDimitry Andric   if (section_sp)
2070b57cec5SDimitry Andric     return section_sp->GetPermissions();
2080b57cec5SDimitry Andric   return 0;
2090b57cec5SDimitry Andric }
2100b57cec5SDimitry Andric 
GetTargetByteSize()2110b57cec5SDimitry Andric uint32_t SBSection::GetTargetByteSize() {
21204eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
2130b57cec5SDimitry Andric 
2140b57cec5SDimitry Andric   SectionSP section_sp(GetSP());
2150b57cec5SDimitry Andric   if (section_sp.get())
2160b57cec5SDimitry Andric     return section_sp->GetTargetByteSize();
2170b57cec5SDimitry Andric   return 0;
2180b57cec5SDimitry Andric }
2190b57cec5SDimitry Andric 
GetAlignment()220753f127fSDimitry Andric uint32_t SBSection::GetAlignment() {
221753f127fSDimitry Andric   LLDB_INSTRUMENT_VA(this);
222753f127fSDimitry Andric 
223753f127fSDimitry Andric   SectionSP section_sp(GetSP());
224753f127fSDimitry Andric   if (section_sp.get())
225753f127fSDimitry Andric     return (1 << section_sp->GetLog2Align());
226753f127fSDimitry Andric   return 0;
227753f127fSDimitry Andric }
228753f127fSDimitry Andric 
operator ==(const SBSection & rhs)2290b57cec5SDimitry Andric bool SBSection::operator==(const SBSection &rhs) {
23004eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, rhs);
2310b57cec5SDimitry Andric 
2320b57cec5SDimitry Andric   SectionSP lhs_section_sp(GetSP());
2330b57cec5SDimitry Andric   SectionSP rhs_section_sp(rhs.GetSP());
2340b57cec5SDimitry Andric   if (lhs_section_sp && rhs_section_sp)
2350b57cec5SDimitry Andric     return lhs_section_sp == rhs_section_sp;
2360b57cec5SDimitry Andric   return false;
2370b57cec5SDimitry Andric }
2380b57cec5SDimitry Andric 
operator !=(const SBSection & rhs)2390b57cec5SDimitry Andric bool SBSection::operator!=(const SBSection &rhs) {
24004eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, rhs);
2410b57cec5SDimitry Andric 
2420b57cec5SDimitry Andric   SectionSP lhs_section_sp(GetSP());
2430b57cec5SDimitry Andric   SectionSP rhs_section_sp(rhs.GetSP());
2440b57cec5SDimitry Andric   return lhs_section_sp != rhs_section_sp;
2450b57cec5SDimitry Andric }
2460b57cec5SDimitry Andric 
GetDescription(SBStream & description)2470b57cec5SDimitry Andric bool SBSection::GetDescription(SBStream &description) {
24804eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, description);
2490b57cec5SDimitry Andric 
2500b57cec5SDimitry Andric   Stream &strm = description.ref();
2510b57cec5SDimitry Andric 
2520b57cec5SDimitry Andric   SectionSP section_sp(GetSP());
2530b57cec5SDimitry Andric   if (section_sp) {
2540b57cec5SDimitry Andric     const addr_t file_addr = section_sp->GetFileAddress();
2550b57cec5SDimitry Andric     strm.Printf("[0x%16.16" PRIx64 "-0x%16.16" PRIx64 ") ", file_addr,
2560b57cec5SDimitry Andric                 file_addr + section_sp->GetByteSize());
2575ffd83dbSDimitry Andric     section_sp->DumpName(strm.AsRawOstream());
2580b57cec5SDimitry Andric   } else {
2590b57cec5SDimitry Andric     strm.PutCString("No value");
2600b57cec5SDimitry Andric   }
2610b57cec5SDimitry Andric 
2620b57cec5SDimitry Andric   return true;
2630b57cec5SDimitry Andric }
264