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/DataBuffer.h"
14 #include "lldb/Core/DataExtractor.h"
15 #include "lldb/Core/Log.h"
16 #include "lldb/Core/Module.h"
17 #include "lldb/Core/Section.h"
18 #include "lldb/Core/StreamString.h"
19 #include "lldb/Symbol/ObjectFile.h"
22 using namespace lldb_private;
24 SBSection::SBSection() : m_opaque_wp() {}
26 SBSection::SBSection(const SBSection &rhs) : m_opaque_wp(rhs.m_opaque_wp) {}
28 SBSection::SBSection(const lldb::SectionSP §ion_sp)
29 : m_opaque_wp() // Don't init with section_sp otherwise this will throw if
30 // section_sp doesn't contain a valid Section *
33 m_opaque_wp = section_sp;
36 const SBSection &SBSection::operator=(const SBSection &rhs) {
37 m_opaque_wp = rhs.m_opaque_wp;
41 SBSection::~SBSection() {}
43 bool SBSection::IsValid() const {
44 SectionSP section_sp(GetSP());
45 return section_sp && section_sp->GetModule().get() != NULL;
48 const char *SBSection::GetName() {
49 SectionSP section_sp(GetSP());
51 return section_sp->GetName().GetCString();
55 lldb::SBSection SBSection::GetParent() {
56 lldb::SBSection sb_section;
57 SectionSP section_sp(GetSP());
59 SectionSP parent_section_sp(section_sp->GetParent());
60 if (parent_section_sp)
61 sb_section.SetSP(parent_section_sp);
66 lldb::SBSection SBSection::FindSubSection(const char *sect_name) {
67 lldb::SBSection sb_section;
69 SectionSP section_sp(GetSP());
71 ConstString const_sect_name(sect_name);
73 section_sp->GetChildren().FindSectionByName(const_sect_name));
79 size_t SBSection::GetNumSubSections() {
80 SectionSP section_sp(GetSP());
82 return section_sp->GetChildren().GetSize();
86 lldb::SBSection SBSection::GetSubSectionAtIndex(size_t idx) {
87 lldb::SBSection sb_section;
88 SectionSP section_sp(GetSP());
90 sb_section.SetSP(section_sp->GetChildren().GetSectionAtIndex(idx));
94 lldb::SectionSP SBSection::GetSP() const { return m_opaque_wp.lock(); }
96 void SBSection::SetSP(const lldb::SectionSP §ion_sp) {
97 m_opaque_wp = section_sp;
100 lldb::addr_t SBSection::GetFileAddress() {
101 lldb::addr_t file_addr = LLDB_INVALID_ADDRESS;
102 SectionSP section_sp(GetSP());
104 return section_sp->GetFileAddress();
108 lldb::addr_t SBSection::GetLoadAddress(lldb::SBTarget &sb_target) {
109 TargetSP target_sp(sb_target.GetSP());
111 SectionSP section_sp(GetSP());
113 return section_sp->GetLoadBaseAddress(target_sp.get());
115 return LLDB_INVALID_ADDRESS;
118 lldb::addr_t SBSection::GetByteSize() {
119 SectionSP section_sp(GetSP());
121 return section_sp->GetByteSize();
125 uint64_t SBSection::GetFileOffset() {
126 SectionSP section_sp(GetSP());
128 ModuleSP module_sp(section_sp->GetModule());
130 ObjectFile *objfile = module_sp->GetObjectFile();
132 return objfile->GetFileOffset() + section_sp->GetFileOffset();
138 uint64_t SBSection::GetFileByteSize() {
139 SectionSP section_sp(GetSP());
141 return section_sp->GetFileSize();
145 SBData SBSection::GetSectionData() { return GetSectionData(0, UINT64_MAX); }
147 SBData SBSection::GetSectionData(uint64_t offset, uint64_t size) {
149 SectionSP section_sp(GetSP());
151 const uint64_t sect_file_size = section_sp->GetFileSize();
152 if (sect_file_size > 0) {
153 ModuleSP module_sp(section_sp->GetModule());
155 ObjectFile *objfile = module_sp->GetObjectFile();
157 const uint64_t sect_file_offset =
158 objfile->GetFileOffset() + section_sp->GetFileOffset();
159 const uint64_t file_offset = sect_file_offset + offset;
160 uint64_t file_size = size;
161 if (file_size == UINT64_MAX) {
162 file_size = section_sp->GetByteSize();
163 if (file_size > offset)
168 DataBufferSP data_buffer_sp(
169 objfile->GetFileSpec().ReadFileContents(file_offset, file_size));
170 if (data_buffer_sp && data_buffer_sp->GetByteSize() > 0) {
171 DataExtractorSP data_extractor_sp(
172 new DataExtractor(data_buffer_sp, objfile->GetByteOrder(),
173 objfile->GetAddressByteSize()));
175 sb_data.SetOpaque(data_extractor_sp);
184 SectionType SBSection::GetSectionType() {
185 SectionSP section_sp(GetSP());
186 if (section_sp.get())
187 return section_sp->GetType();
188 return eSectionTypeInvalid;
192 SBSection::GetPermissions() const
194 SectionSP section_sp(GetSP());
196 return section_sp->GetPermissions();
200 uint32_t SBSection::GetTargetByteSize() {
201 SectionSP section_sp(GetSP());
202 if (section_sp.get())
203 return section_sp->GetTargetByteSize();
207 bool SBSection::operator==(const SBSection &rhs) {
208 SectionSP lhs_section_sp(GetSP());
209 SectionSP rhs_section_sp(rhs.GetSP());
210 if (lhs_section_sp && rhs_section_sp)
211 return lhs_section_sp == rhs_section_sp;
215 bool SBSection::operator!=(const SBSection &rhs) {
216 SectionSP lhs_section_sp(GetSP());
217 SectionSP rhs_section_sp(rhs.GetSP());
218 return lhs_section_sp != rhs_section_sp;
221 bool SBSection::GetDescription(SBStream &description) {
222 Stream &strm = description.ref();
224 SectionSP section_sp(GetSP());
226 const addr_t file_addr = section_sp->GetFileAddress();
227 strm.Printf("[0x%16.16" PRIx64 "-0x%16.16" PRIx64 ") ", file_addr,
228 file_addr + section_sp->GetByteSize());
229 section_sp->DumpName(&strm);
231 strm.PutCString("No value");