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 
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"
16 #include "lldb/Utility/DataBuffer.h"
17 #include "lldb/Utility/DataExtractor.h"
18 #include "lldb/Utility/StreamString.h"
19 
20 using namespace lldb;
21 using namespace lldb_private;
22 
23 SBSection::SBSection() { LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBSection); }
24 
25 SBSection::SBSection(const SBSection &rhs) : m_opaque_wp(rhs.m_opaque_wp) {
26   LLDB_RECORD_CONSTRUCTOR(SBSection, (const lldb::SBSection &), rhs);
27 }
28 
29 SBSection::SBSection(const lldb::SectionSP &section_sp) {
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 
36 const SBSection &SBSection::operator=(const SBSection &rhs) {
37   LLDB_RECORD_METHOD(const lldb::SBSection &,
38                      SBSection, operator=,(const lldb::SBSection &), rhs);
39 
40   m_opaque_wp = rhs.m_opaque_wp;
41   return LLDB_RECORD_RESULT(*this);
42 }
43 
44 SBSection::~SBSection() = default;
45 
46 bool SBSection::IsValid() const {
47   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBSection, IsValid);
48   return this->operator bool();
49 }
50 SBSection::operator bool() const {
51   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBSection, operator bool);
52 
53   SectionSP section_sp(GetSP());
54   return section_sp && section_sp->GetModule().get() != nullptr;
55 }
56 
57 const char *SBSection::GetName() {
58   LLDB_RECORD_METHOD_NO_ARGS(const char *, SBSection, GetName);
59 
60   SectionSP section_sp(GetSP());
61   if (section_sp)
62     return section_sp->GetName().GetCString();
63   return nullptr;
64 }
65 
66 lldb::SBSection SBSection::GetParent() {
67   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBSection, SBSection, GetParent);
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 LLDB_RECORD_RESULT(sb_section);
77 }
78 
79 lldb::SBSection SBSection::FindSubSection(const char *sect_name) {
80   LLDB_RECORD_METHOD(lldb::SBSection, SBSection, FindSubSection, (const char *),
81                      sect_name);
82 
83   lldb::SBSection sb_section;
84   if (sect_name) {
85     SectionSP section_sp(GetSP());
86     if (section_sp) {
87       ConstString const_sect_name(sect_name);
88       sb_section.SetSP(
89           section_sp->GetChildren().FindSectionByName(const_sect_name));
90     }
91   }
92   return LLDB_RECORD_RESULT(sb_section);
93 }
94 
95 size_t SBSection::GetNumSubSections() {
96   LLDB_RECORD_METHOD_NO_ARGS(size_t, SBSection, GetNumSubSections);
97 
98   SectionSP section_sp(GetSP());
99   if (section_sp)
100     return section_sp->GetChildren().GetSize();
101   return 0;
102 }
103 
104 lldb::SBSection SBSection::GetSubSectionAtIndex(size_t idx) {
105   LLDB_RECORD_METHOD(lldb::SBSection, SBSection, GetSubSectionAtIndex, (size_t),
106                      idx);
107 
108   lldb::SBSection sb_section;
109   SectionSP section_sp(GetSP());
110   if (section_sp)
111     sb_section.SetSP(section_sp->GetChildren().GetSectionAtIndex(idx));
112   return LLDB_RECORD_RESULT(sb_section);
113 }
114 
115 lldb::SectionSP SBSection::GetSP() const { return m_opaque_wp.lock(); }
116 
117 void SBSection::SetSP(const lldb::SectionSP &section_sp) {
118   m_opaque_wp = section_sp;
119 }
120 
121 lldb::addr_t SBSection::GetFileAddress() {
122   LLDB_RECORD_METHOD_NO_ARGS(lldb::addr_t, SBSection, GetFileAddress);
123 
124   lldb::addr_t file_addr = LLDB_INVALID_ADDRESS;
125   SectionSP section_sp(GetSP());
126   if (section_sp)
127     return section_sp->GetFileAddress();
128   return file_addr;
129 }
130 
131 lldb::addr_t SBSection::GetLoadAddress(lldb::SBTarget &sb_target) {
132   LLDB_RECORD_METHOD(lldb::addr_t, SBSection, GetLoadAddress,
133                      (lldb::SBTarget &), sb_target);
134 
135   TargetSP target_sp(sb_target.GetSP());
136   if (target_sp) {
137     SectionSP section_sp(GetSP());
138     if (section_sp)
139       return section_sp->GetLoadBaseAddress(target_sp.get());
140   }
141   return LLDB_INVALID_ADDRESS;
142 }
143 
144 lldb::addr_t SBSection::GetByteSize() {
145   LLDB_RECORD_METHOD_NO_ARGS(lldb::addr_t, SBSection, GetByteSize);
146 
147   SectionSP section_sp(GetSP());
148   if (section_sp)
149     return section_sp->GetByteSize();
150   return 0;
151 }
152 
153 uint64_t SBSection::GetFileOffset() {
154   LLDB_RECORD_METHOD_NO_ARGS(uint64_t, SBSection, GetFileOffset);
155 
156   SectionSP section_sp(GetSP());
157   if (section_sp) {
158     ModuleSP module_sp(section_sp->GetModule());
159     if (module_sp) {
160       ObjectFile *objfile = module_sp->GetObjectFile();
161       if (objfile)
162         return objfile->GetFileOffset() + section_sp->GetFileOffset();
163     }
164   }
165   return UINT64_MAX;
166 }
167 
168 uint64_t SBSection::GetFileByteSize() {
169   LLDB_RECORD_METHOD_NO_ARGS(uint64_t, SBSection, GetFileByteSize);
170 
171   SectionSP section_sp(GetSP());
172   if (section_sp)
173     return section_sp->GetFileSize();
174   return 0;
175 }
176 
177 SBData SBSection::GetSectionData() {
178   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBData, SBSection, GetSectionData);
179 
180   return LLDB_RECORD_RESULT(GetSectionData(0, UINT64_MAX));
181 }
182 
183 SBData SBSection::GetSectionData(uint64_t offset, uint64_t size) {
184   LLDB_RECORD_METHOD(lldb::SBData, SBSection, GetSectionData,
185                      (uint64_t, uint64_t), offset, size);
186 
187   SBData sb_data;
188   SectionSP section_sp(GetSP());
189   if (section_sp) {
190     const uint64_t sect_file_size = section_sp->GetFileSize();
191     if (sect_file_size > 0) {
192       ModuleSP module_sp(section_sp->GetModule());
193       if (module_sp) {
194         ObjectFile *objfile = module_sp->GetObjectFile();
195         if (objfile) {
196           const uint64_t sect_file_offset =
197               objfile->GetFileOffset() + section_sp->GetFileOffset();
198           const uint64_t file_offset = sect_file_offset + offset;
199           uint64_t file_size = size;
200           if (file_size == UINT64_MAX) {
201             file_size = section_sp->GetByteSize();
202             if (file_size > offset)
203               file_size -= offset;
204             else
205               file_size = 0;
206           }
207           auto data_buffer_sp = FileSystem::Instance().CreateDataBuffer(
208               objfile->GetFileSpec().GetPath(), file_size, file_offset);
209           if (data_buffer_sp && data_buffer_sp->GetByteSize() > 0) {
210             DataExtractorSP data_extractor_sp(
211                 new DataExtractor(data_buffer_sp, objfile->GetByteOrder(),
212                                   objfile->GetAddressByteSize()));
213 
214             sb_data.SetOpaque(data_extractor_sp);
215           }
216         }
217       }
218     }
219   }
220   return LLDB_RECORD_RESULT(sb_data);
221 }
222 
223 SectionType SBSection::GetSectionType() {
224   LLDB_RECORD_METHOD_NO_ARGS(lldb::SectionType, SBSection, GetSectionType);
225 
226   SectionSP section_sp(GetSP());
227   if (section_sp.get())
228     return section_sp->GetType();
229   return eSectionTypeInvalid;
230 }
231 
232 uint32_t SBSection::GetPermissions() const {
233   LLDB_RECORD_METHOD_CONST_NO_ARGS(uint32_t, SBSection, GetPermissions);
234 
235   SectionSP section_sp(GetSP());
236   if (section_sp)
237     return section_sp->GetPermissions();
238   return 0;
239 }
240 
241 uint32_t SBSection::GetTargetByteSize() {
242   LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBSection, GetTargetByteSize);
243 
244   SectionSP section_sp(GetSP());
245   if (section_sp.get())
246     return section_sp->GetTargetByteSize();
247   return 0;
248 }
249 
250 bool SBSection::operator==(const SBSection &rhs) {
251   LLDB_RECORD_METHOD(bool, SBSection, operator==,(const lldb::SBSection &),
252                      rhs);
253 
254   SectionSP lhs_section_sp(GetSP());
255   SectionSP rhs_section_sp(rhs.GetSP());
256   if (lhs_section_sp && rhs_section_sp)
257     return lhs_section_sp == rhs_section_sp;
258   return false;
259 }
260 
261 bool SBSection::operator!=(const SBSection &rhs) {
262   LLDB_RECORD_METHOD(bool, SBSection, operator!=,(const lldb::SBSection &),
263                      rhs);
264 
265   SectionSP lhs_section_sp(GetSP());
266   SectionSP rhs_section_sp(rhs.GetSP());
267   return lhs_section_sp != rhs_section_sp;
268 }
269 
270 bool SBSection::GetDescription(SBStream &description) {
271   LLDB_RECORD_METHOD(bool, SBSection, GetDescription, (lldb::SBStream &),
272                      description);
273 
274   Stream &strm = description.ref();
275 
276   SectionSP section_sp(GetSP());
277   if (section_sp) {
278     const addr_t file_addr = section_sp->GetFileAddress();
279     strm.Printf("[0x%16.16" PRIx64 "-0x%16.16" PRIx64 ") ", file_addr,
280                 file_addr + section_sp->GetByteSize());
281     section_sp->DumpName(strm.AsRawOstream());
282   } else {
283     strm.PutCString("No value");
284   }
285 
286   return true;
287 }
288 
289 namespace lldb_private {
290 namespace repro {
291 
292 template <>
293 void RegisterMethods<SBSection>(Registry &R) {
294   LLDB_REGISTER_CONSTRUCTOR(SBSection, ());
295   LLDB_REGISTER_CONSTRUCTOR(SBSection, (const lldb::SBSection &));
296   LLDB_REGISTER_METHOD(const lldb::SBSection &,
297                        SBSection, operator=,(const lldb::SBSection &));
298   LLDB_REGISTER_METHOD_CONST(bool, SBSection, IsValid, ());
299   LLDB_REGISTER_METHOD_CONST(bool, SBSection, operator bool, ());
300   LLDB_REGISTER_METHOD(const char *, SBSection, GetName, ());
301   LLDB_REGISTER_METHOD(lldb::SBSection, SBSection, GetParent, ());
302   LLDB_REGISTER_METHOD(lldb::SBSection, SBSection, FindSubSection,
303                        (const char *));
304   LLDB_REGISTER_METHOD(size_t, SBSection, GetNumSubSections, ());
305   LLDB_REGISTER_METHOD(lldb::SBSection, SBSection, GetSubSectionAtIndex,
306                        (size_t));
307   LLDB_REGISTER_METHOD(lldb::addr_t, SBSection, GetFileAddress, ());
308   LLDB_REGISTER_METHOD(lldb::addr_t, SBSection, GetLoadAddress,
309                        (lldb::SBTarget &));
310   LLDB_REGISTER_METHOD(lldb::addr_t, SBSection, GetByteSize, ());
311   LLDB_REGISTER_METHOD(uint64_t, SBSection, GetFileOffset, ());
312   LLDB_REGISTER_METHOD(uint64_t, SBSection, GetFileByteSize, ());
313   LLDB_REGISTER_METHOD(lldb::SBData, SBSection, GetSectionData, ());
314   LLDB_REGISTER_METHOD(lldb::SBData, SBSection, GetSectionData,
315                        (uint64_t, uint64_t));
316   LLDB_REGISTER_METHOD(lldb::SectionType, SBSection, GetSectionType, ());
317   LLDB_REGISTER_METHOD_CONST(uint32_t, SBSection, GetPermissions, ());
318   LLDB_REGISTER_METHOD(uint32_t, SBSection, GetTargetByteSize, ());
319   LLDB_REGISTER_METHOD(bool, SBSection, operator==,(const lldb::SBSection &));
320   LLDB_REGISTER_METHOD(bool, SBSection, operator!=,(const lldb::SBSection &));
321   LLDB_REGISTER_METHOD(bool, SBSection, GetDescription, (lldb::SBStream &));
322 }
323 
324 }
325 }
326