1 //===-- SBSection.cpp -------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/API/SBSection.h"
11 #include "lldb/API/SBStream.h"
12 #include "lldb/API/SBTarget.h"
13 #include "lldb/Core/Module.h"
14 #include "lldb/Core/Section.h"
15 #include "lldb/Symbol/ObjectFile.h"
16 #include "lldb/Utility/DataBuffer.h"
17 #include "lldb/Utility/DataBufferLLVM.h"
18 #include "lldb/Utility/DataExtractor.h"
19 #include "lldb/Utility/Log.h"
20 #include "lldb/Utility/StreamString.h"
23 using namespace lldb_private;
25 SBSection::SBSection() : m_opaque_wp() {}
27 SBSection::SBSection(const SBSection &rhs) : m_opaque_wp(rhs.m_opaque_wp) {}
29 SBSection::SBSection(const lldb::SectionSP §ion_sp)
30 : m_opaque_wp() // Don't init with section_sp otherwise this will throw if
31 // section_sp doesn't contain a valid Section *
34 m_opaque_wp = section_sp;
37 const SBSection &SBSection::operator=(const SBSection &rhs) {
38 m_opaque_wp = rhs.m_opaque_wp;
42 SBSection::~SBSection() {}
44 bool SBSection::IsValid() const {
45 SectionSP section_sp(GetSP());
46 return section_sp && section_sp->GetModule().get() != NULL;
49 const char *SBSection::GetName() {
50 SectionSP section_sp(GetSP());
52 return section_sp->GetName().GetCString();
56 lldb::SBSection SBSection::GetParent() {
57 lldb::SBSection sb_section;
58 SectionSP section_sp(GetSP());
60 SectionSP parent_section_sp(section_sp->GetParent());
61 if (parent_section_sp)
62 sb_section.SetSP(parent_section_sp);
67 lldb::SBSection SBSection::FindSubSection(const char *sect_name) {
68 lldb::SBSection sb_section;
70 SectionSP section_sp(GetSP());
72 ConstString const_sect_name(sect_name);
74 section_sp->GetChildren().FindSectionByName(const_sect_name));
80 size_t SBSection::GetNumSubSections() {
81 SectionSP section_sp(GetSP());
83 return section_sp->GetChildren().GetSize();
87 lldb::SBSection SBSection::GetSubSectionAtIndex(size_t idx) {
88 lldb::SBSection sb_section;
89 SectionSP section_sp(GetSP());
91 sb_section.SetSP(section_sp->GetChildren().GetSectionAtIndex(idx));
95 lldb::SectionSP SBSection::GetSP() const { return m_opaque_wp.lock(); }
97 void SBSection::SetSP(const lldb::SectionSP §ion_sp) {
98 m_opaque_wp = section_sp;
101 lldb::addr_t SBSection::GetFileAddress() {
102 lldb::addr_t file_addr = LLDB_INVALID_ADDRESS;
103 SectionSP section_sp(GetSP());
105 return section_sp->GetFileAddress();
109 lldb::addr_t SBSection::GetLoadAddress(lldb::SBTarget &sb_target) {
110 TargetSP target_sp(sb_target.GetSP());
112 SectionSP section_sp(GetSP());
114 return section_sp->GetLoadBaseAddress(target_sp.get());
116 return LLDB_INVALID_ADDRESS;
119 lldb::addr_t SBSection::GetByteSize() {
120 SectionSP section_sp(GetSP());
122 return section_sp->GetByteSize();
126 uint64_t SBSection::GetFileOffset() {
127 SectionSP section_sp(GetSP());
129 ModuleSP module_sp(section_sp->GetModule());
131 ObjectFile *objfile = module_sp->GetObjectFile();
133 return objfile->GetFileOffset() + section_sp->GetFileOffset();
139 uint64_t SBSection::GetFileByteSize() {
140 SectionSP section_sp(GetSP());
142 return section_sp->GetFileSize();
146 SBData SBSection::GetSectionData() { return GetSectionData(0, UINT64_MAX); }
148 SBData SBSection::GetSectionData(uint64_t offset, uint64_t size) {
150 SectionSP section_sp(GetSP());
152 const uint64_t sect_file_size = section_sp->GetFileSize();
153 if (sect_file_size > 0) {
154 ModuleSP module_sp(section_sp->GetModule());
156 ObjectFile *objfile = module_sp->GetObjectFile();
158 const uint64_t sect_file_offset =
159 objfile->GetFileOffset() + section_sp->GetFileOffset();
160 const uint64_t file_offset = sect_file_offset + offset;
161 uint64_t file_size = size;
162 if (file_size == UINT64_MAX) {
163 file_size = section_sp->GetByteSize();
164 if (file_size > offset)
169 auto data_buffer_sp = DataBufferLLVM::CreateSliceFromPath(
170 objfile->GetFileSpec().GetPath(), file_size, file_offset);
171 if (data_buffer_sp && data_buffer_sp->GetByteSize() > 0) {
172 DataExtractorSP data_extractor_sp(
173 new DataExtractor(data_buffer_sp, objfile->GetByteOrder(),
174 objfile->GetAddressByteSize()));
176 sb_data.SetOpaque(data_extractor_sp);
185 SectionType SBSection::GetSectionType() {
186 SectionSP section_sp(GetSP());
187 if (section_sp.get())
188 return section_sp->GetType();
189 return eSectionTypeInvalid;
193 SBSection::GetPermissions() const
195 SectionSP section_sp(GetSP());
197 return section_sp->GetPermissions();
201 uint32_t SBSection::GetTargetByteSize() {
202 SectionSP section_sp(GetSP());
203 if (section_sp.get())
204 return section_sp->GetTargetByteSize();
208 bool SBSection::operator==(const SBSection &rhs) {
209 SectionSP lhs_section_sp(GetSP());
210 SectionSP rhs_section_sp(rhs.GetSP());
211 if (lhs_section_sp && rhs_section_sp)
212 return lhs_section_sp == rhs_section_sp;
216 bool SBSection::operator!=(const SBSection &rhs) {
217 SectionSP lhs_section_sp(GetSP());
218 SectionSP rhs_section_sp(rhs.GetSP());
219 return lhs_section_sp != rhs_section_sp;
222 bool SBSection::GetDescription(SBStream &description) {
223 Stream &strm = description.ref();
225 SectionSP section_sp(GetSP());
227 const addr_t file_addr = section_sp->GetFileAddress();
228 strm.Printf("[0x%16.16" PRIx64 "-0x%16.16" PRIx64 ") ", file_addr,
229 file_addr + section_sp->GetByteSize());
230 section_sp->DumpName(&strm);
232 strm.PutCString("No value");