blob: 9da5d170da9e0c9e2e313e03e0aeb292d6505af9 [file] [log] [blame]
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001//===-- 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"
Greg Claytonfed39aa2012-06-27 22:22:28 +000012#include "lldb/API/SBTarget.h"
Greg Claytoncac9c5f2011-09-24 00:52:29 +000013#include "lldb/Core/Module.h"
Greg Claytond9dc52d2011-09-24 05:04:40 +000014#include "lldb/Core/Section.h"
Greg Clayton1f746072012-08-29 21:13:06 +000015#include "lldb/Symbol/ObjectFile.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000016#include "lldb/Utility/DataBuffer.h"
Zachary Turner7f6a7a32017-03-06 23:42:14 +000017#include "lldb/Utility/DataBufferLLVM.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000018#include "lldb/Utility/DataExtractor.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000019#include "lldb/Utility/Log.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000020#include "lldb/Utility/StreamString.h"
Greg Claytoncac9c5f2011-09-24 00:52:29 +000021
Greg Claytoncac9c5f2011-09-24 00:52:29 +000022using namespace lldb;
23using namespace lldb_private;
24
Kate Stoneb9c1b512016-09-06 20:57:50 +000025SBSection::SBSection() : m_opaque_wp() {}
Greg Claytoncac9c5f2011-09-24 00:52:29 +000026
Kate Stoneb9c1b512016-09-06 20:57:50 +000027SBSection::SBSection(const SBSection &rhs) : m_opaque_wp(rhs.m_opaque_wp) {}
28
29SBSection::SBSection(const lldb::SectionSP &section_sp)
30 : m_opaque_wp() // Don't init with section_sp otherwise this will throw if
31 // section_sp doesn't contain a valid Section *
Greg Claytoncac9c5f2011-09-24 00:52:29 +000032{
Kate Stoneb9c1b512016-09-06 20:57:50 +000033 if (section_sp)
Greg Claytone72dfb32012-02-24 01:59:29 +000034 m_opaque_wp = section_sp;
Greg Claytoncac9c5f2011-09-24 00:52:29 +000035}
36
Kate Stoneb9c1b512016-09-06 20:57:50 +000037const SBSection &SBSection::operator=(const SBSection &rhs) {
38 m_opaque_wp = rhs.m_opaque_wp;
39 return *this;
Greg Claytoncac9c5f2011-09-24 00:52:29 +000040}
41
Kate Stoneb9c1b512016-09-06 20:57:50 +000042SBSection::~SBSection() {}
43
44bool SBSection::IsValid() const {
45 SectionSP section_sp(GetSP());
46 return section_sp && section_sp->GetModule().get() != NULL;
47}
48
49const char *SBSection::GetName() {
50 SectionSP section_sp(GetSP());
51 if (section_sp)
52 return section_sp->GetName().GetCString();
53 return NULL;
54}
55
56lldb::SBSection SBSection::GetParent() {
57 lldb::SBSection sb_section;
58 SectionSP section_sp(GetSP());
59 if (section_sp) {
60 SectionSP parent_section_sp(section_sp->GetParent());
61 if (parent_section_sp)
62 sb_section.SetSP(parent_section_sp);
63 }
64 return sb_section;
65}
66
67lldb::SBSection SBSection::FindSubSection(const char *sect_name) {
68 lldb::SBSection sb_section;
69 if (sect_name) {
70 SectionSP section_sp(GetSP());
71 if (section_sp) {
72 ConstString const_sect_name(sect_name);
73 sb_section.SetSP(
74 section_sp->GetChildren().FindSectionByName(const_sect_name));
Greg Claytonfed39aa2012-06-27 22:22:28 +000075 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000076 }
77 return sb_section;
Greg Claytonfed39aa2012-06-27 22:22:28 +000078}
79
Kate Stoneb9c1b512016-09-06 20:57:50 +000080size_t SBSection::GetNumSubSections() {
81 SectionSP section_sp(GetSP());
82 if (section_sp)
83 return section_sp->GetChildren().GetSize();
84 return 0;
Greg Claytoncac9c5f2011-09-24 00:52:29 +000085}
86
Kate Stoneb9c1b512016-09-06 20:57:50 +000087lldb::SBSection SBSection::GetSubSectionAtIndex(size_t idx) {
88 lldb::SBSection sb_section;
89 SectionSP section_sp(GetSP());
90 if (section_sp)
91 sb_section.SetSP(section_sp->GetChildren().GetSectionAtIndex(idx));
92 return sb_section;
93}
94
95lldb::SectionSP SBSection::GetSP() const { return m_opaque_wp.lock(); }
96
97void SBSection::SetSP(const lldb::SectionSP &section_sp) {
98 m_opaque_wp = section_sp;
99}
100
101lldb::addr_t SBSection::GetFileAddress() {
102 lldb::addr_t file_addr = LLDB_INVALID_ADDRESS;
103 SectionSP section_sp(GetSP());
104 if (section_sp)
105 return section_sp->GetFileAddress();
106 return file_addr;
107}
108
109lldb::addr_t SBSection::GetLoadAddress(lldb::SBTarget &sb_target) {
110 TargetSP target_sp(sb_target.GetSP());
111 if (target_sp) {
112 SectionSP section_sp(GetSP());
Greg Claytone72dfb32012-02-24 01:59:29 +0000113 if (section_sp)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000114 return section_sp->GetLoadBaseAddress(target_sp.get());
115 }
116 return LLDB_INVALID_ADDRESS;
117}
118
119lldb::addr_t SBSection::GetByteSize() {
120 SectionSP section_sp(GetSP());
121 if (section_sp)
122 return section_sp->GetByteSize();
123 return 0;
124}
125
126uint64_t SBSection::GetFileOffset() {
127 SectionSP section_sp(GetSP());
128 if (section_sp) {
129 ModuleSP module_sp(section_sp->GetModule());
130 if (module_sp) {
131 ObjectFile *objfile = module_sp->GetObjectFile();
132 if (objfile)
133 return objfile->GetFileOffset() + section_sp->GetFileOffset();
134 }
135 }
136 return UINT64_MAX;
137}
138
139uint64_t SBSection::GetFileByteSize() {
140 SectionSP section_sp(GetSP());
141 if (section_sp)
142 return section_sp->GetFileSize();
143 return 0;
144}
145
146SBData SBSection::GetSectionData() { return GetSectionData(0, UINT64_MAX); }
147
148SBData SBSection::GetSectionData(uint64_t offset, uint64_t size) {
149 SBData sb_data;
150 SectionSP section_sp(GetSP());
151 if (section_sp) {
152 const uint64_t sect_file_size = section_sp->GetFileSize();
153 if (sect_file_size > 0) {
154 ModuleSP module_sp(section_sp->GetModule());
155 if (module_sp) {
156 ObjectFile *objfile = module_sp->GetObjectFile();
157 if (objfile) {
158 const uint64_t sect_file_offset =
159 objfile->GetFileOffset() + section_sp->GetFileOffset();
160 const uint64_t file_offset = sect_file_offset + offset;
161 uint64_t file_size = size;
162 if (file_size == UINT64_MAX) {
163 file_size = section_sp->GetByteSize();
164 if (file_size > offset)
165 file_size -= offset;
166 else
167 file_size = 0;
168 }
Zachary Turner7f6a7a32017-03-06 23:42:14 +0000169 auto data_buffer_sp = DataBufferLLVM::CreateSliceFromPath(
170 objfile->GetFileSpec().GetPath(), file_size, file_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000171 if (data_buffer_sp && data_buffer_sp->GetByteSize() > 0) {
172 DataExtractorSP data_extractor_sp(
173 new DataExtractor(data_buffer_sp, objfile->GetByteOrder(),
174 objfile->GetAddressByteSize()));
175
176 sb_data.SetOpaque(data_extractor_sp);
177 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000178 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000179 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000180 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000181 }
182 return sb_data;
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000183}
184
Kate Stoneb9c1b512016-09-06 20:57:50 +0000185SectionType SBSection::GetSectionType() {
186 SectionSP section_sp(GetSP());
187 if (section_sp.get())
188 return section_sp->GetType();
189 return eSectionTypeInvalid;
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000190}
191
Abhishek Aggarwal3c7f0702016-09-08 12:22:56 +0000192uint32_t
193SBSection::GetPermissions() const
194{
195 SectionSP section_sp(GetSP());
196 if (section_sp)
197 return section_sp->GetPermissions();
198 return 0;
199}
200
Kate Stoneb9c1b512016-09-06 20:57:50 +0000201uint32_t SBSection::GetTargetByteSize() {
202 SectionSP section_sp(GetSP());
203 if (section_sp.get())
204 return section_sp->GetTargetByteSize();
205 return 0;
Greg Claytond9dc52d2011-09-24 05:04:40 +0000206}
207
Kate Stoneb9c1b512016-09-06 20:57:50 +0000208bool SBSection::operator==(const SBSection &rhs) {
209 SectionSP lhs_section_sp(GetSP());
210 SectionSP rhs_section_sp(rhs.GetSP());
211 if (lhs_section_sp && rhs_section_sp)
212 return lhs_section_sp == rhs_section_sp;
213 return false;
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000214}
215
Kate Stoneb9c1b512016-09-06 20:57:50 +0000216bool SBSection::operator!=(const SBSection &rhs) {
217 SectionSP lhs_section_sp(GetSP());
218 SectionSP rhs_section_sp(rhs.GetSP());
219 return lhs_section_sp != rhs_section_sp;
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000220}
221
Kate Stoneb9c1b512016-09-06 20:57:50 +0000222bool SBSection::GetDescription(SBStream &description) {
223 Stream &strm = description.ref();
224
225 SectionSP section_sp(GetSP());
226 if (section_sp) {
227 const addr_t file_addr = section_sp->GetFileAddress();
228 strm.Printf("[0x%16.16" PRIx64 "-0x%16.16" PRIx64 ") ", file_addr,
229 file_addr + section_sp->GetByteSize());
230 section_sp->DumpName(&strm);
231 } else {
232 strm.PutCString("No value");
233 }
234
235 return true;
Matthew Gardinerc928de32014-10-22 07:22:56 +0000236}