LLDB mainline
SBSection.cpp
Go to the documentation of this file.
1//===-- SBSection.cpp -----------------------------------------------------===//
2//
3// 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
6//
7//===----------------------------------------------------------------------===//
8
10#include "lldb/API/SBStream.h"
11#include "lldb/API/SBTarget.h"
12#include "lldb/Core/Module.h"
13#include "lldb/Core/Section.h"
20
21using namespace lldb;
22using namespace lldb_private;
23
25
29
31 // Don't init with section_sp otherwise this will throw if
32 // section_sp doesn't contain a valid Section *
33 if (section_sp)
34 m_opaque_wp = section_sp;
35}
36
38 LLDB_INSTRUMENT_VA(this, rhs);
39
41 return *this;
42}
43
44SBSection::~SBSection() = default;
45
46bool SBSection::IsValid() const {
48 return this->operator bool();
49}
50SBSection::operator bool() const {
52
53 SectionSP section_sp(GetSP());
54 return section_sp && section_sp->GetModule().get() != nullptr;
55}
56
57const char *SBSection::GetName() {
59
60 SectionSP section_sp(GetSP());
61 if (section_sp)
62 return StringPool::GetSystemPool().Intern(section_sp->GetName());
63 return nullptr;
64}
65
68
69 lldb::SBSection sb_section;
70 SectionSP section_sp(GetSP());
71 if (section_sp) {
72 SectionSP parent_section_sp(section_sp->GetParent());
73 if (parent_section_sp)
74 sb_section.SetSP(parent_section_sp);
75 }
76 return sb_section;
77}
78
80 LLDB_INSTRUMENT_VA(this, sect_name);
81
82 lldb::SBSection sb_section;
83 if (sect_name) {
84 SectionSP section_sp(GetSP());
85 if (section_sp) {
86 sb_section.SetSP(section_sp->GetChildren().FindSectionByName(sect_name));
87 }
88 }
89 return sb_section;
90}
91
94
95 SectionSP section_sp(GetSP());
96 if (section_sp)
97 return section_sp->GetChildren().GetSize();
98 return 0;
99}
100
102 LLDB_INSTRUMENT_VA(this, idx);
103
104 lldb::SBSection sb_section;
105 SectionSP section_sp(GetSP());
106 if (section_sp)
107 sb_section.SetSP(section_sp->GetChildren().GetSectionAtIndex(idx));
108 return sb_section;
109}
110
112
113void SBSection::SetSP(const lldb::SectionSP &section_sp) {
114 m_opaque_wp = section_sp;
115}
116
118 LLDB_INSTRUMENT_VA(this);
119
121 SectionSP section_sp(GetSP());
122 if (section_sp)
123 return section_sp->GetFileAddress();
124 return file_addr;
125}
126
128 LLDB_INSTRUMENT_VA(this, sb_target);
129
130 TargetSP target_sp(sb_target.GetSP());
131 if (target_sp) {
132 SectionSP section_sp(GetSP());
133 if (section_sp)
134 return section_sp->GetLoadBaseAddress(target_sp.get());
135 }
137}
138
140 LLDB_INSTRUMENT_VA(this);
141
142 SectionSP section_sp(GetSP());
143 if (section_sp)
144 return section_sp->GetByteSize();
145 return 0;
146}
147
149 LLDB_INSTRUMENT_VA(this);
150
151 SectionSP section_sp(GetSP());
152 if (section_sp) {
153 ModuleSP module_sp(section_sp->GetModule());
154 if (module_sp) {
155 ObjectFile *objfile = module_sp->GetObjectFile();
156 if (objfile)
157 return objfile->GetFileOffset() + section_sp->GetFileOffset();
158 }
159 }
160 return UINT64_MAX;
161}
162
164 LLDB_INSTRUMENT_VA(this);
165
166 SectionSP section_sp(GetSP());
167 if (section_sp)
168 return section_sp->GetFileSize();
169 return 0;
170}
171
177
178SBData SBSection::GetSectionData(uint64_t offset, uint64_t size) {
179 LLDB_INSTRUMENT_VA(this, offset, size);
180
181 SBData sb_data;
182 SectionSP section_sp(GetSP());
183 if (section_sp) {
184 DataExtractor section_data;
185 section_sp->GetSectionData(section_data);
186 sb_data.SetOpaque(
187 std::make_shared<DataExtractor>(section_data, offset, size));
188 }
189 return sb_data;
190}
191
193 LLDB_INSTRUMENT_VA(this);
194
195 SectionSP section_sp(GetSP());
196 if (section_sp.get())
197 return section_sp->GetType();
198 return eSectionTypeInvalid;
199}
200
202 LLDB_INSTRUMENT_VA(this);
203
204 SectionSP section_sp(GetSP());
205 if (section_sp)
206 return section_sp->GetPermissions();
207 return 0;
208}
209
211 LLDB_INSTRUMENT_VA(this);
212
213 return 1;
214}
215
217 LLDB_INSTRUMENT_VA(this);
218
219 SectionSP section_sp(GetSP());
220 if (section_sp.get())
221 return (1 << section_sp->GetLog2Align());
222 return 0;
223}
224
226 LLDB_INSTRUMENT_VA(this, rhs);
227
228 SectionSP lhs_section_sp(GetSP());
229 SectionSP rhs_section_sp(rhs.GetSP());
230 if (lhs_section_sp && rhs_section_sp)
231 return lhs_section_sp == rhs_section_sp;
232 return false;
233}
234
236 LLDB_INSTRUMENT_VA(this, rhs);
237
238 SectionSP lhs_section_sp(GetSP());
239 SectionSP rhs_section_sp(rhs.GetSP());
240 return lhs_section_sp != rhs_section_sp;
241}
242
244 LLDB_INSTRUMENT_VA(this, description);
245
246 Stream &strm = description.ref();
247
248 SectionSP section_sp(GetSP());
249 if (section_sp) {
250 const addr_t file_addr = section_sp->GetFileAddress();
251 strm.Printf("[0x%16.16" PRIx64 "-0x%16.16" PRIx64 ") ", file_addr,
252 file_addr + section_sp->GetByteSize());
253 section_sp->DumpName(strm.AsRawOstream());
254 } else {
255 strm.PutCString("No value");
256 }
257
258 return true;
259}
#define LLDB_INSTRUMENT_VA(...)
void SetOpaque(const lldb::DataExtractorSP &data_sp)
Definition SBData.cpp:47
uint32_t GetAlignment()
Return the alignment of the section in bytes.
uint64_t GetFileOffset()
uint32_t GetPermissions() const
Gets the permissions (RWX) of the section of the object file.
lldb::SectionSP GetSP() const
bool IsValid() const
Definition SBSection.cpp:46
lldb::addr_t GetByteSize()
const lldb::SBSection & operator=(const lldb::SBSection &rhs)
Definition SBSection.cpp:37
lldb::SBSection FindSubSection(const char *sect_name)
Definition SBSection.cpp:79
lldb::SBSection GetParent()
Definition SBSection.cpp:66
bool operator==(const lldb::SBSection &rhs)
SectionType GetSectionType()
lldb::SectionWP m_opaque_wp
Definition SBSection.h:96
size_t GetNumSubSections()
Definition SBSection.cpp:92
lldb::addr_t GetFileAddress()
lldb::addr_t GetLoadAddress(lldb::SBTarget &target)
bool GetDescription(lldb::SBStream &description)
bool operator!=(const lldb::SBSection &rhs)
lldb::SBData GetSectionData()
const char * GetName()
Definition SBSection.cpp:57
uint64_t GetFileByteSize()
uint32_t GetTargetByteSize()
void SetSP(const lldb::SectionSP &section_sp)
lldb::SBSection GetSubSectionAtIndex(size_t idx)
lldb_private::Stream & ref()
Definition SBStream.cpp:179
lldb::TargetSP GetSP() const
Definition SBTarget.cpp:600
An data extractor class.
A plug-in interface definition class for object file parsers.
Definition ObjectFile.h:47
virtual lldb::addr_t GetFileOffset() const
Returns the offset into a file at which this object resides.
Definition ObjectFile.h:276
A stream class that can stream formatted output to a file.
Definition Stream.h:28
llvm::raw_ostream & AsRawOstream()
Returns a raw_ostream that forwards the data to this Stream object.
Definition Stream.h:405
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition Stream.cpp:134
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition Stream.cpp:63
const char * Intern(llvm::StringRef str) const
Definition StringPool.h:78
static StringPoolRef GetSystemPool()
The system pool. Only valid between Initialize and Terminate.
#define UINT64_MAX
#define LLDB_INVALID_ADDRESS
A class that represents a running process on the host machine.
std::shared_ptr< lldb_private::Section > SectionSP
uint64_t addr_t
Definition lldb-types.h:80
std::shared_ptr< lldb_private::Target > TargetSP
@ eSectionTypeInvalid
std::shared_ptr< lldb_private::Module > ModuleSP