blob: b76714bb175b0335d8ea8e262064d5bdc4c5313e [file] [log] [blame]
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001//===-- SBSection.cpp -------------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Greg Claytoncac9c5f2011-09-24 00:52:29 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "lldb/API/SBSection.h"
10#include "lldb/API/SBStream.h"
Greg Claytonfed39aa2012-06-27 22:22:28 +000011#include "lldb/API/SBTarget.h"
Greg Claytoncac9c5f2011-09-24 00:52:29 +000012#include "lldb/Core/Module.h"
Greg Claytond9dc52d2011-09-24 05:04:40 +000013#include "lldb/Core/Section.h"
Greg Clayton1f746072012-08-29 21:13:06 +000014#include "lldb/Symbol/ObjectFile.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000015#include "lldb/Utility/DataBuffer.h"
16#include "lldb/Utility/DataExtractor.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000017#include "lldb/Utility/Log.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000018#include "lldb/Utility/StreamString.h"
Greg Claytoncac9c5f2011-09-24 00:52:29 +000019
Greg Claytoncac9c5f2011-09-24 00:52:29 +000020using namespace lldb;
21using namespace lldb_private;
22
Kate Stoneb9c1b512016-09-06 20:57:50 +000023SBSection::SBSection() : m_opaque_wp() {}
Greg Claytoncac9c5f2011-09-24 00:52:29 +000024
Kate Stoneb9c1b512016-09-06 20:57:50 +000025SBSection::SBSection(const SBSection &rhs) : m_opaque_wp(rhs.m_opaque_wp) {}
26
27SBSection::SBSection(const lldb::SectionSP &section_sp)
28 : m_opaque_wp() // Don't init with section_sp otherwise this will throw if
29 // section_sp doesn't contain a valid Section *
Greg Claytoncac9c5f2011-09-24 00:52:29 +000030{
Kate Stoneb9c1b512016-09-06 20:57:50 +000031 if (section_sp)
Greg Claytone72dfb32012-02-24 01:59:29 +000032 m_opaque_wp = section_sp;
Greg Claytoncac9c5f2011-09-24 00:52:29 +000033}
34
Kate Stoneb9c1b512016-09-06 20:57:50 +000035const SBSection &SBSection::operator=(const SBSection &rhs) {
36 m_opaque_wp = rhs.m_opaque_wp;
37 return *this;
Greg Claytoncac9c5f2011-09-24 00:52:29 +000038}
39
Kate Stoneb9c1b512016-09-06 20:57:50 +000040SBSection::~SBSection() {}
41
42bool SBSection::IsValid() const {
43 SectionSP section_sp(GetSP());
44 return section_sp && section_sp->GetModule().get() != NULL;
45}
46
47const char *SBSection::GetName() {
48 SectionSP section_sp(GetSP());
49 if (section_sp)
50 return section_sp->GetName().GetCString();
51 return NULL;
52}
53
54lldb::SBSection SBSection::GetParent() {
55 lldb::SBSection sb_section;
56 SectionSP section_sp(GetSP());
57 if (section_sp) {
58 SectionSP parent_section_sp(section_sp->GetParent());
59 if (parent_section_sp)
60 sb_section.SetSP(parent_section_sp);
61 }
62 return sb_section;
63}
64
65lldb::SBSection SBSection::FindSubSection(const char *sect_name) {
66 lldb::SBSection sb_section;
67 if (sect_name) {
68 SectionSP section_sp(GetSP());
69 if (section_sp) {
70 ConstString const_sect_name(sect_name);
71 sb_section.SetSP(
72 section_sp->GetChildren().FindSectionByName(const_sect_name));
Greg Claytonfed39aa2012-06-27 22:22:28 +000073 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000074 }
75 return sb_section;
Greg Claytonfed39aa2012-06-27 22:22:28 +000076}
77
Kate Stoneb9c1b512016-09-06 20:57:50 +000078size_t SBSection::GetNumSubSections() {
79 SectionSP section_sp(GetSP());
80 if (section_sp)
81 return section_sp->GetChildren().GetSize();
82 return 0;
Greg Claytoncac9c5f2011-09-24 00:52:29 +000083}
84
Kate Stoneb9c1b512016-09-06 20:57:50 +000085lldb::SBSection SBSection::GetSubSectionAtIndex(size_t idx) {
86 lldb::SBSection sb_section;
87 SectionSP section_sp(GetSP());
88 if (section_sp)
89 sb_section.SetSP(section_sp->GetChildren().GetSectionAtIndex(idx));
90 return sb_section;
91}
92
93lldb::SectionSP SBSection::GetSP() const { return m_opaque_wp.lock(); }
94
95void SBSection::SetSP(const lldb::SectionSP &section_sp) {
96 m_opaque_wp = section_sp;
97}
98
99lldb::addr_t SBSection::GetFileAddress() {
100 lldb::addr_t file_addr = LLDB_INVALID_ADDRESS;
101 SectionSP section_sp(GetSP());
102 if (section_sp)
103 return section_sp->GetFileAddress();
104 return file_addr;
105}
106
107lldb::addr_t SBSection::GetLoadAddress(lldb::SBTarget &sb_target) {
108 TargetSP target_sp(sb_target.GetSP());
109 if (target_sp) {
110 SectionSP section_sp(GetSP());
Greg Claytone72dfb32012-02-24 01:59:29 +0000111 if (section_sp)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000112 return section_sp->GetLoadBaseAddress(target_sp.get());
113 }
114 return LLDB_INVALID_ADDRESS;
115}
116
117lldb::addr_t SBSection::GetByteSize() {
118 SectionSP section_sp(GetSP());
119 if (section_sp)
120 return section_sp->GetByteSize();
121 return 0;
122}
123
124uint64_t SBSection::GetFileOffset() {
125 SectionSP section_sp(GetSP());
126 if (section_sp) {
127 ModuleSP module_sp(section_sp->GetModule());
128 if (module_sp) {
129 ObjectFile *objfile = module_sp->GetObjectFile();
130 if (objfile)
131 return objfile->GetFileOffset() + section_sp->GetFileOffset();
132 }
133 }
134 return UINT64_MAX;
135}
136
137uint64_t SBSection::GetFileByteSize() {
138 SectionSP section_sp(GetSP());
139 if (section_sp)
140 return section_sp->GetFileSize();
141 return 0;
142}
143
144SBData SBSection::GetSectionData() { return GetSectionData(0, UINT64_MAX); }
145
146SBData SBSection::GetSectionData(uint64_t offset, uint64_t size) {
147 SBData sb_data;
148 SectionSP section_sp(GetSP());
149 if (section_sp) {
150 const uint64_t sect_file_size = section_sp->GetFileSize();
151 if (sect_file_size > 0) {
152 ModuleSP module_sp(section_sp->GetModule());
153 if (module_sp) {
154 ObjectFile *objfile = module_sp->GetObjectFile();
155 if (objfile) {
156 const uint64_t sect_file_offset =
157 objfile->GetFileOffset() + section_sp->GetFileOffset();
158 const uint64_t file_offset = sect_file_offset + offset;
159 uint64_t file_size = size;
160 if (file_size == UINT64_MAX) {
161 file_size = section_sp->GetByteSize();
162 if (file_size > offset)
163 file_size -= offset;
164 else
165 file_size = 0;
166 }
Jonas Devlieghere87e403a2018-11-12 21:24:50 +0000167 auto data_buffer_sp = FileSystem::Instance().CreateDataBuffer(
Zachary Turner7f6a7a32017-03-06 23:42:14 +0000168 objfile->GetFileSpec().GetPath(), file_size, file_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000169 if (data_buffer_sp && data_buffer_sp->GetByteSize() > 0) {
170 DataExtractorSP data_extractor_sp(
171 new DataExtractor(data_buffer_sp, objfile->GetByteOrder(),
172 objfile->GetAddressByteSize()));
173
174 sb_data.SetOpaque(data_extractor_sp);
175 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000176 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000177 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000178 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000179 }
180 return sb_data;
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000181}
182
Kate Stoneb9c1b512016-09-06 20:57:50 +0000183SectionType SBSection::GetSectionType() {
184 SectionSP section_sp(GetSP());
185 if (section_sp.get())
186 return section_sp->GetType();
187 return eSectionTypeInvalid;
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000188}
189
Abhishek Aggarwal3c7f0702016-09-08 12:22:56 +0000190uint32_t
191SBSection::GetPermissions() const
192{
193 SectionSP section_sp(GetSP());
194 if (section_sp)
195 return section_sp->GetPermissions();
196 return 0;
197}
198
Kate Stoneb9c1b512016-09-06 20:57:50 +0000199uint32_t SBSection::GetTargetByteSize() {
200 SectionSP section_sp(GetSP());
201 if (section_sp.get())
202 return section_sp->GetTargetByteSize();
203 return 0;
Greg Claytond9dc52d2011-09-24 05:04:40 +0000204}
205
Kate Stoneb9c1b512016-09-06 20:57:50 +0000206bool SBSection::operator==(const SBSection &rhs) {
207 SectionSP lhs_section_sp(GetSP());
208 SectionSP rhs_section_sp(rhs.GetSP());
209 if (lhs_section_sp && rhs_section_sp)
210 return lhs_section_sp == rhs_section_sp;
211 return false;
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000212}
213
Kate Stoneb9c1b512016-09-06 20:57:50 +0000214bool SBSection::operator!=(const SBSection &rhs) {
215 SectionSP lhs_section_sp(GetSP());
216 SectionSP rhs_section_sp(rhs.GetSP());
217 return lhs_section_sp != rhs_section_sp;
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000218}
219
Kate Stoneb9c1b512016-09-06 20:57:50 +0000220bool SBSection::GetDescription(SBStream &description) {
221 Stream &strm = description.ref();
222
223 SectionSP section_sp(GetSP());
224 if (section_sp) {
225 const addr_t file_addr = section_sp->GetFileAddress();
226 strm.Printf("[0x%16.16" PRIx64 "-0x%16.16" PRIx64 ") ", file_addr,
227 file_addr + section_sp->GetByteSize());
228 section_sp->DumpName(&strm);
229 } else {
230 strm.PutCString("No value");
231 }
232
233 return true;
Matthew Gardinerc928de32014-10-22 07:22:56 +0000234}