LLDB  mainline
SBSection.cpp
Go to the documentation of this file.
1 //===-- SBSection.cpp -------------------------------------------*- C++ -*-===//
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 
9 #include "lldb/API/SBSection.h"
10 #include "SBReproducerPrivate.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"
19 
20 using namespace lldb;
21 using namespace lldb_private;
22 
23 SBSection::SBSection() : m_opaque_wp() {
25 }
26 
27 SBSection::SBSection(const SBSection &rhs) : m_opaque_wp(rhs.m_opaque_wp) {
29 }
30 
31 SBSection::SBSection(const lldb::SectionSP &section_sp)
32  : m_opaque_wp() // Don't init with section_sp otherwise this will throw if
33  // section_sp doesn't contain a valid Section *
34 {
35  if (section_sp)
36  m_opaque_wp = section_sp;
37 }
38 
41  SBSection, operator=,(const lldb::SBSection &), rhs);
42 
43  m_opaque_wp = rhs.m_opaque_wp;
44  return LLDB_RECORD_RESULT(*this);
45 }
46 
48 
49 bool SBSection::IsValid() const {
51  return this->operator bool();
52 }
53 SBSection::operator bool() const {
54  LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBSection, operator bool);
55 
56  SectionSP section_sp(GetSP());
57  return section_sp && section_sp->GetModule().get() != NULL;
58 }
59 
60 const char *SBSection::GetName() {
62 
63  SectionSP section_sp(GetSP());
64  if (section_sp)
65  return section_sp->GetName().GetCString();
66  return NULL;
67 }
68 
71 
72  lldb::SBSection sb_section;
73  SectionSP section_sp(GetSP());
74  if (section_sp) {
75  SectionSP parent_section_sp(section_sp->GetParent());
76  if (parent_section_sp)
77  sb_section.SetSP(parent_section_sp);
78  }
79  return LLDB_RECORD_RESULT(sb_section);
80 }
81 
84  sect_name);
85 
86  lldb::SBSection sb_section;
87  if (sect_name) {
88  SectionSP section_sp(GetSP());
89  if (section_sp) {
90  ConstString const_sect_name(sect_name);
91  sb_section.SetSP(
92  section_sp->GetChildren().FindSectionByName(const_sect_name));
93  }
94  }
95  return LLDB_RECORD_RESULT(sb_section);
96 }
97 
100 
101  SectionSP section_sp(GetSP());
102  if (section_sp)
103  return section_sp->GetChildren().GetSize();
104  return 0;
105 }
106 
109  idx);
110 
111  lldb::SBSection sb_section;
112  SectionSP section_sp(GetSP());
113  if (section_sp)
114  sb_section.SetSP(section_sp->GetChildren().GetSectionAtIndex(idx));
115  return LLDB_RECORD_RESULT(sb_section);
116 }
117 
118 lldb::SectionSP SBSection::GetSP() const { return m_opaque_wp.lock(); }
119 
120 void SBSection::SetSP(const lldb::SectionSP &section_sp) {
121  m_opaque_wp = section_sp;
122 }
123 
126 
128  SectionSP section_sp(GetSP());
129  if (section_sp)
130  return section_sp->GetFileAddress();
131  return file_addr;
132 }
133 
136  (lldb::SBTarget &), sb_target);
137 
138  TargetSP target_sp(sb_target.GetSP());
139  if (target_sp) {
140  SectionSP section_sp(GetSP());
141  if (section_sp)
142  return section_sp->GetLoadBaseAddress(target_sp.get());
143  }
144  return LLDB_INVALID_ADDRESS;
145 }
146 
149 
150  SectionSP section_sp(GetSP());
151  if (section_sp)
152  return section_sp->GetByteSize();
153  return 0;
154 }
155 
158 
159  SectionSP section_sp(GetSP());
160  if (section_sp) {
161  ModuleSP module_sp(section_sp->GetModule());
162  if (module_sp) {
163  ObjectFile *objfile = module_sp->GetObjectFile();
164  if (objfile)
165  return objfile->GetFileOffset() + section_sp->GetFileOffset();
166  }
167  }
168  return UINT64_MAX;
169 }
170 
173 
174  SectionSP section_sp(GetSP());
175  if (section_sp)
176  return section_sp->GetFileSize();
177  return 0;
178 }
179 
182 
184 }
185 
186 SBData SBSection::GetSectionData(uint64_t offset, uint64_t size) {
188  (uint64_t, uint64_t), offset, size);
189 
190  SBData sb_data;
191  SectionSP section_sp(GetSP());
192  if (section_sp) {
193  const uint64_t sect_file_size = section_sp->GetFileSize();
194  if (sect_file_size > 0) {
195  ModuleSP module_sp(section_sp->GetModule());
196  if (module_sp) {
197  ObjectFile *objfile = module_sp->GetObjectFile();
198  if (objfile) {
199  const uint64_t sect_file_offset =
200  objfile->GetFileOffset() + section_sp->GetFileOffset();
201  const uint64_t file_offset = sect_file_offset + offset;
202  uint64_t file_size = size;
203  if (file_size == UINT64_MAX) {
204  file_size = section_sp->GetByteSize();
205  if (file_size > offset)
206  file_size -= offset;
207  else
208  file_size = 0;
209  }
210  auto data_buffer_sp = FileSystem::Instance().CreateDataBuffer(
211  objfile->GetFileSpec().GetPath(), file_size, file_offset);
212  if (data_buffer_sp && data_buffer_sp->GetByteSize() > 0) {
213  DataExtractorSP data_extractor_sp(
214  new DataExtractor(data_buffer_sp, objfile->GetByteOrder(),
215  objfile->GetAddressByteSize()));
216 
217  sb_data.SetOpaque(data_extractor_sp);
218  }
219  }
220  }
221  }
222  }
223  return LLDB_RECORD_RESULT(sb_data);
224 }
225 
228 
229  SectionSP section_sp(GetSP());
230  if (section_sp.get())
231  return section_sp->GetType();
232  return eSectionTypeInvalid;
233 }
234 
237 
238  SectionSP section_sp(GetSP());
239  if (section_sp)
240  return section_sp->GetPermissions();
241  return 0;
242 }
243 
246 
247  SectionSP section_sp(GetSP());
248  if (section_sp.get())
249  return section_sp->GetTargetByteSize();
250  return 0;
251 }
252 
254  LLDB_RECORD_METHOD(bool, SBSection, operator==,(const lldb::SBSection &),
255  rhs);
256 
257  SectionSP lhs_section_sp(GetSP());
258  SectionSP rhs_section_sp(rhs.GetSP());
259  if (lhs_section_sp && rhs_section_sp)
260  return lhs_section_sp == rhs_section_sp;
261  return false;
262 }
263 
265  LLDB_RECORD_METHOD(bool, SBSection, operator!=,(const lldb::SBSection &),
266  rhs);
267 
268  SectionSP lhs_section_sp(GetSP());
269  SectionSP rhs_section_sp(rhs.GetSP());
270  return lhs_section_sp != rhs_section_sp;
271 }
272 
275  description);
276 
277  Stream &strm = description.ref();
278 
279  SectionSP section_sp(GetSP());
280  if (section_sp) {
281  const addr_t file_addr = section_sp->GetFileAddress();
282  strm.Printf("[0x%16.16" PRIx64 "-0x%16.16" PRIx64 ") ", file_addr,
283  file_addr + section_sp->GetByteSize());
284  section_sp->DumpName(&strm);
285  } else {
286  strm.PutCString("No value");
287  }
288 
289  return true;
290 }
291 
292 namespace lldb_private {
293 namespace repro {
294 
295 template <>
300  SBSection, operator=,(const lldb::SBSection &));
302  LLDB_REGISTER_METHOD_CONST(bool, SBSection, operator bool, ());
303  LLDB_REGISTER_METHOD(const char *, SBSection, GetName, ());
306  (const char *));
309  (size_t));
312  (lldb::SBTarget &));
318  (uint64_t, uint64_t));
322  LLDB_REGISTER_METHOD(bool, SBSection, operator==,(const lldb::SBSection &));
323  LLDB_REGISTER_METHOD(bool, SBSection, operator!=,(const lldb::SBSection &));
325 }
326 
327 }
328 }
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:61
The registry contains a unique mapping between functions and their ID.
An data extractor class.
Definition: DataExtractor.h:47
size_t GetNumSubSections()
Definition: SBSection.cpp:98
lldb::SBSection FindSubSection(const char *sect_name)
Definition: SBSection.cpp:82
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
#define LLDB_RECORD_CONSTRUCTOR_NO_ARGS(Class)
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
const char * GetName()
Definition: SBSection.cpp:60
virtual FileSpec & GetFileSpec()
Get accessor to the object file specification.
Definition: ObjectFile.h:269
bool GetDescription(lldb::SBStream &description)
Definition: SBSection.cpp:273
SectionType GetSectionType()
Definition: SBSection.cpp:226
lldb::SBSection GetSubSectionAtIndex(size_t idx)
Definition: SBSection.cpp:107
uint32_t GetTargetByteSize()
Return the size of a target&#39;s byte represented by this section in numbers of host bytes...
Definition: SBSection.cpp:244
void SetOpaque(const lldb::DataExtractorSP &data_sp)
Definition: SBData.cpp:46
A plug-in interface definition class for object file parsers.
Definition: ObjectFile.h:58
lldb::addr_t GetLoadAddress(lldb::SBTarget &target)
Definition: SBSection.cpp:134
uint64_t GetFileByteSize()
Definition: SBSection.cpp:171
virtual lldb::addr_t GetFileOffset() const
Returns the offset into a file at which this object resides.
Definition: ObjectFile.h:260
#define LLDB_REGISTER_CONSTRUCTOR(Class, Signature)
#define LLDB_RECORD_METHOD_NO_ARGS(Result, Class, Method)
#define LLDB_INVALID_ADDRESS
Invalid value definitions.
Definition: lldb-defines.h:85
lldb::TargetSP GetSP() const
Definition: SBTarget.cpp:600
void RegisterMethods< SBSection >(Registry &R)
Definition: SBSection.cpp:296
uint64_t GetFileOffset()
Definition: SBSection.cpp:156
#define LLDB_RECORD_CONSTRUCTOR(Class, Signature,...)
#define LLDB_RECORD_METHOD(Result, Class, Method, Signature,...)
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:106
lldb::SBData GetSectionData()
Definition: SBSection.cpp:180
uint32_t GetPermissions() const
Gets the permissions (RWX) of the section of the object file.
Definition: SBSection.cpp:235
lldb::addr_t GetFileAddress()
Definition: SBSection.cpp:124
lldb_private::Stream & ref()
Definition: SBStream.cpp:162
bool operator==(const lldb::SBSection &rhs)
Definition: SBSection.cpp:253
lldb::addr_t GetByteSize()
Definition: SBSection.cpp:147
bool IsValid() const
Definition: SBSection.cpp:49
uint64_t addr_t
Definition: lldb-types.h:83
bool operator!=(const lldb::SBSection &rhs)
Definition: SBSection.cpp:264
A uniqued constant string class.
Definition: ConstString.h:38
const lldb::SBSection & operator=(const lldb::SBSection &rhs)
Definition: SBSection.cpp:39
Definition: SBAddress.h:15
#define LLDB_REGISTER_METHOD(Result, Class, Method, Signature)
#define LLDB_RECORD_METHOD_CONST_NO_ARGS(Result, Class, Method)
virtual uint32_t GetAddressByteSize() const =0
Gets the address size in bytes for the current object file.
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition: FileSpec.cpp:376
#define UINT64_MAX
Definition: lldb-defines.h:35
#define LLDB_RECORD_RESULT(Result)
#define LLDB_REGISTER_METHOD_CONST(Result, Class, Method, Signature)
lldb::SBSection GetParent()
Definition: SBSection.cpp:69
virtual lldb::ByteOrder GetByteOrder() const =0
Gets whether endian swapping should occur when extracting data from this object file.