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"
19
20using namespace lldb;
21using namespace lldb_private;
22
24
28
30 // Don't init with section_sp otherwise this will throw if
31 // section_sp doesn't contain a valid Section *
32 if (section_sp)
33 m_opaque_wp = section_sp;
34}
35
37 LLDB_INSTRUMENT_VA(this, rhs);
38
40 return *this;
41}
42
43SBSection::~SBSection() = default;
44
45bool SBSection::IsValid() const {
47 return this->operator bool();
48}
49SBSection::operator bool() const {
51
52 SectionSP section_sp(GetSP());
53 return section_sp && section_sp->GetModule().get() != nullptr;
54}
55
56const char *SBSection::GetName() {
58
59 SectionSP section_sp(GetSP());
60 if (section_sp)
61 return section_sp->GetName().GetCString();
62 return nullptr;
63}
64
67
68 lldb::SBSection sb_section;
69 SectionSP section_sp(GetSP());
70 if (section_sp) {
71 SectionSP parent_section_sp(section_sp->GetParent());
72 if (parent_section_sp)
73 sb_section.SetSP(parent_section_sp);
74 }
75 return sb_section;
76}
77
79 LLDB_INSTRUMENT_VA(this, sect_name);
80
81 lldb::SBSection sb_section;
82 if (sect_name) {
83 SectionSP section_sp(GetSP());
84 if (section_sp) {
85 sb_section.SetSP(section_sp->GetChildren().FindSectionByName(sect_name));
86 }
87 }
88 return sb_section;
89}
90
93
94 SectionSP section_sp(GetSP());
95 if (section_sp)
96 return section_sp->GetChildren().GetSize();
97 return 0;
98}
99
101 LLDB_INSTRUMENT_VA(this, idx);
102
103 lldb::SBSection sb_section;
104 SectionSP section_sp(GetSP());
105 if (section_sp)
106 sb_section.SetSP(section_sp->GetChildren().GetSectionAtIndex(idx));
107 return sb_section;
108}
109
111
112void SBSection::SetSP(const lldb::SectionSP &section_sp) {
113 m_opaque_wp = section_sp;
114}
115
117 LLDB_INSTRUMENT_VA(this);
118
120 SectionSP section_sp(GetSP());
121 if (section_sp)
122 return section_sp->GetFileAddress();
123 return file_addr;
124}
125
127 LLDB_INSTRUMENT_VA(this, sb_target);
128
129 TargetSP target_sp(sb_target.GetSP());
130 if (target_sp) {
131 SectionSP section_sp(GetSP());
132 if (section_sp)
133 return section_sp->GetLoadBaseAddress(target_sp.get());
134 }
136}
137
139 LLDB_INSTRUMENT_VA(this);
140
141 SectionSP section_sp(GetSP());
142 if (section_sp)
143 return section_sp->GetByteSize();
144 return 0;
145}
146
148 LLDB_INSTRUMENT_VA(this);
149
150 SectionSP section_sp(GetSP());
151 if (section_sp) {
152 ModuleSP module_sp(section_sp->GetModule());
153 if (module_sp) {
154 ObjectFile *objfile = module_sp->GetObjectFile();
155 if (objfile)
156 return objfile->GetFileOffset() + section_sp->GetFileOffset();
157 }
158 }
159 return UINT64_MAX;
160}
161
163 LLDB_INSTRUMENT_VA(this);
164
165 SectionSP section_sp(GetSP());
166 if (section_sp)
167 return section_sp->GetFileSize();
168 return 0;
169}
170
176
177SBData SBSection::GetSectionData(uint64_t offset, uint64_t size) {
178 LLDB_INSTRUMENT_VA(this, offset, size);
179
180 SBData sb_data;
181 SectionSP section_sp(GetSP());
182 if (section_sp) {
183 DataExtractor section_data;
184 section_sp->GetSectionData(section_data);
185 sb_data.SetOpaque(
186 std::make_shared<DataExtractor>(section_data, offset, size));
187 }
188 return sb_data;
189}
190
192 LLDB_INSTRUMENT_VA(this);
193
194 SectionSP section_sp(GetSP());
195 if (section_sp.get())
196 return section_sp->GetType();
197 return eSectionTypeInvalid;
198}
199
201 LLDB_INSTRUMENT_VA(this);
202
203 SectionSP section_sp(GetSP());
204 if (section_sp)
205 return section_sp->GetPermissions();
206 return 0;
207}
208
210 LLDB_INSTRUMENT_VA(this);
211
212 return 1;
213}
214
216 LLDB_INSTRUMENT_VA(this);
217
218 SectionSP section_sp(GetSP());
219 if (section_sp.get())
220 return (1 << section_sp->GetLog2Align());
221 return 0;
222}
223
225 LLDB_INSTRUMENT_VA(this, rhs);
226
227 SectionSP lhs_section_sp(GetSP());
228 SectionSP rhs_section_sp(rhs.GetSP());
229 if (lhs_section_sp && rhs_section_sp)
230 return lhs_section_sp == rhs_section_sp;
231 return false;
232}
233
235 LLDB_INSTRUMENT_VA(this, rhs);
236
237 SectionSP lhs_section_sp(GetSP());
238 SectionSP rhs_section_sp(rhs.GetSP());
239 return lhs_section_sp != rhs_section_sp;
240}
241
243 LLDB_INSTRUMENT_VA(this, description);
244
245 Stream &strm = description.ref();
246
247 SectionSP section_sp(GetSP());
248 if (section_sp) {
249 const addr_t file_addr = section_sp->GetFileAddress();
250 strm.Printf("[0x%16.16" PRIx64 "-0x%16.16" PRIx64 ") ", file_addr,
251 file_addr + section_sp->GetByteSize());
252 section_sp->DumpName(strm.AsRawOstream());
253 } else {
254 strm.PutCString("No value");
255 }
256
257 return true;
258}
#define LLDB_INSTRUMENT_VA(...)
void SetOpaque(const lldb::DataExtractorSP &data_sp)
Definition SBData.cpp:45
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:45
lldb::addr_t GetByteSize()
const lldb::SBSection & operator=(const lldb::SBSection &rhs)
Definition SBSection.cpp:36
lldb::SBSection FindSubSection(const char *sect_name)
Definition SBSection.cpp:78
lldb::SBSection GetParent()
Definition SBSection.cpp:65
bool operator==(const lldb::SBSection &rhs)
SectionType GetSectionType()
lldb::SectionWP m_opaque_wp
Definition SBSection.h:96
size_t GetNumSubSections()
Definition SBSection.cpp:91
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:56
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:178
lldb::TargetSP GetSP() const
Definition SBTarget.cpp:595
An data extractor class.
A plug-in interface definition class for object file parsers.
Definition ObjectFile.h:46
virtual lldb::addr_t GetFileOffset() const
Returns the offset into a file at which this object resides.
Definition ObjectFile.h:271
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
#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