]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - source/API/SBSection.cpp
Vendor import of lldb trunk r290819:
[FreeBSD/FreeBSD.git] / source / API / SBSection.cpp
1 //===-- SBSection.cpp -------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "lldb/API/SBSection.h"
11 #include "lldb/API/SBStream.h"
12 #include "lldb/API/SBTarget.h"
13 #include "lldb/Core/DataBuffer.h"
14 #include "lldb/Core/DataExtractor.h"
15 #include "lldb/Core/Log.h"
16 #include "lldb/Core/Module.h"
17 #include "lldb/Core/Section.h"
18 #include "lldb/Core/StreamString.h"
19 #include "lldb/Symbol/ObjectFile.h"
20
21 using namespace lldb;
22 using namespace lldb_private;
23
24 SBSection::SBSection() : m_opaque_wp() {}
25
26 SBSection::SBSection(const SBSection &rhs) : m_opaque_wp(rhs.m_opaque_wp) {}
27
28 SBSection::SBSection(const lldb::SectionSP &section_sp)
29     : m_opaque_wp() // Don't init with section_sp otherwise this will throw if
30                     // section_sp doesn't contain a valid Section *
31 {
32   if (section_sp)
33     m_opaque_wp = section_sp;
34 }
35
36 const SBSection &SBSection::operator=(const SBSection &rhs) {
37   m_opaque_wp = rhs.m_opaque_wp;
38   return *this;
39 }
40
41 SBSection::~SBSection() {}
42
43 bool SBSection::IsValid() const {
44   SectionSP section_sp(GetSP());
45   return section_sp && section_sp->GetModule().get() != NULL;
46 }
47
48 const char *SBSection::GetName() {
49   SectionSP section_sp(GetSP());
50   if (section_sp)
51     return section_sp->GetName().GetCString();
52   return NULL;
53 }
54
55 lldb::SBSection SBSection::GetParent() {
56   lldb::SBSection sb_section;
57   SectionSP section_sp(GetSP());
58   if (section_sp) {
59     SectionSP parent_section_sp(section_sp->GetParent());
60     if (parent_section_sp)
61       sb_section.SetSP(parent_section_sp);
62   }
63   return sb_section;
64 }
65
66 lldb::SBSection SBSection::FindSubSection(const char *sect_name) {
67   lldb::SBSection sb_section;
68   if (sect_name) {
69     SectionSP section_sp(GetSP());
70     if (section_sp) {
71       ConstString const_sect_name(sect_name);
72       sb_section.SetSP(
73           section_sp->GetChildren().FindSectionByName(const_sect_name));
74     }
75   }
76   return sb_section;
77 }
78
79 size_t SBSection::GetNumSubSections() {
80   SectionSP section_sp(GetSP());
81   if (section_sp)
82     return section_sp->GetChildren().GetSize();
83   return 0;
84 }
85
86 lldb::SBSection SBSection::GetSubSectionAtIndex(size_t idx) {
87   lldb::SBSection sb_section;
88   SectionSP section_sp(GetSP());
89   if (section_sp)
90     sb_section.SetSP(section_sp->GetChildren().GetSectionAtIndex(idx));
91   return sb_section;
92 }
93
94 lldb::SectionSP SBSection::GetSP() const { return m_opaque_wp.lock(); }
95
96 void SBSection::SetSP(const lldb::SectionSP &section_sp) {
97   m_opaque_wp = section_sp;
98 }
99
100 lldb::addr_t SBSection::GetFileAddress() {
101   lldb::addr_t file_addr = LLDB_INVALID_ADDRESS;
102   SectionSP section_sp(GetSP());
103   if (section_sp)
104     return section_sp->GetFileAddress();
105   return file_addr;
106 }
107
108 lldb::addr_t SBSection::GetLoadAddress(lldb::SBTarget &sb_target) {
109   TargetSP target_sp(sb_target.GetSP());
110   if (target_sp) {
111     SectionSP section_sp(GetSP());
112     if (section_sp)
113       return section_sp->GetLoadBaseAddress(target_sp.get());
114   }
115   return LLDB_INVALID_ADDRESS;
116 }
117
118 lldb::addr_t SBSection::GetByteSize() {
119   SectionSP section_sp(GetSP());
120   if (section_sp)
121     return section_sp->GetByteSize();
122   return 0;
123 }
124
125 uint64_t SBSection::GetFileOffset() {
126   SectionSP section_sp(GetSP());
127   if (section_sp) {
128     ModuleSP module_sp(section_sp->GetModule());
129     if (module_sp) {
130       ObjectFile *objfile = module_sp->GetObjectFile();
131       if (objfile)
132         return objfile->GetFileOffset() + section_sp->GetFileOffset();
133     }
134   }
135   return UINT64_MAX;
136 }
137
138 uint64_t SBSection::GetFileByteSize() {
139   SectionSP section_sp(GetSP());
140   if (section_sp)
141     return section_sp->GetFileSize();
142   return 0;
143 }
144
145 SBData SBSection::GetSectionData() { return GetSectionData(0, UINT64_MAX); }
146
147 SBData SBSection::GetSectionData(uint64_t offset, uint64_t size) {
148   SBData sb_data;
149   SectionSP section_sp(GetSP());
150   if (section_sp) {
151     const uint64_t sect_file_size = section_sp->GetFileSize();
152     if (sect_file_size > 0) {
153       ModuleSP module_sp(section_sp->GetModule());
154       if (module_sp) {
155         ObjectFile *objfile = module_sp->GetObjectFile();
156         if (objfile) {
157           const uint64_t sect_file_offset =
158               objfile->GetFileOffset() + section_sp->GetFileOffset();
159           const uint64_t file_offset = sect_file_offset + offset;
160           uint64_t file_size = size;
161           if (file_size == UINT64_MAX) {
162             file_size = section_sp->GetByteSize();
163             if (file_size > offset)
164               file_size -= offset;
165             else
166               file_size = 0;
167           }
168           DataBufferSP data_buffer_sp(
169               objfile->GetFileSpec().ReadFileContents(file_offset, file_size));
170           if (data_buffer_sp && data_buffer_sp->GetByteSize() > 0) {
171             DataExtractorSP data_extractor_sp(
172                 new DataExtractor(data_buffer_sp, objfile->GetByteOrder(),
173                                   objfile->GetAddressByteSize()));
174
175             sb_data.SetOpaque(data_extractor_sp);
176           }
177         }
178       }
179     }
180   }
181   return sb_data;
182 }
183
184 SectionType SBSection::GetSectionType() {
185   SectionSP section_sp(GetSP());
186   if (section_sp.get())
187     return section_sp->GetType();
188   return eSectionTypeInvalid;
189 }
190
191 uint32_t
192 SBSection::GetPermissions() const
193 {
194     SectionSP section_sp(GetSP());
195     if (section_sp)
196         return section_sp->GetPermissions();
197     return 0;
198 }
199
200 uint32_t SBSection::GetTargetByteSize() {
201   SectionSP section_sp(GetSP());
202   if (section_sp.get())
203     return section_sp->GetTargetByteSize();
204   return 0;
205 }
206
207 bool SBSection::operator==(const SBSection &rhs) {
208   SectionSP lhs_section_sp(GetSP());
209   SectionSP rhs_section_sp(rhs.GetSP());
210   if (lhs_section_sp && rhs_section_sp)
211     return lhs_section_sp == rhs_section_sp;
212   return false;
213 }
214
215 bool SBSection::operator!=(const SBSection &rhs) {
216   SectionSP lhs_section_sp(GetSP());
217   SectionSP rhs_section_sp(rhs.GetSP());
218   return lhs_section_sp != rhs_section_sp;
219 }
220
221 bool SBSection::GetDescription(SBStream &description) {
222   Stream &strm = description.ref();
223
224   SectionSP section_sp(GetSP());
225   if (section_sp) {
226     const addr_t file_addr = section_sp->GetFileAddress();
227     strm.Printf("[0x%16.16" PRIx64 "-0x%16.16" PRIx64 ") ", file_addr,
228                 file_addr + section_sp->GetByteSize());
229     section_sp->DumpName(&strm);
230   } else {
231     strm.PutCString("No value");
232   }
233
234   return true;
235 }