blob: 8124fe94841d7c4877ecb6bf94f347112e6550c5 [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/DataBuffer.h"
14#include "lldb/Core/DataExtractor.h"
15#include "lldb/Core/Log.h"
Greg Claytoncac9c5f2011-09-24 00:52:29 +000016#include "lldb/Core/Module.h"
Greg Claytond9dc52d2011-09-24 05:04:40 +000017#include "lldb/Core/Section.h"
18#include "lldb/Core/StreamString.h"
Greg Clayton1f746072012-08-29 21:13:06 +000019#include "lldb/Symbol/ObjectFile.h"
Greg Claytoncac9c5f2011-09-24 00:52:29 +000020
Greg Claytoncac9c5f2011-09-24 00:52:29 +000021using namespace lldb;
22using namespace lldb_private;
23
Kate Stoneb9c1b512016-09-06 20:57:50 +000024SBSection::SBSection() : m_opaque_wp() {}
Greg Claytoncac9c5f2011-09-24 00:52:29 +000025
Kate Stoneb9c1b512016-09-06 20:57:50 +000026SBSection::SBSection(const SBSection &rhs) : m_opaque_wp(rhs.m_opaque_wp) {}
27
28SBSection::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 *
Greg Claytoncac9c5f2011-09-24 00:52:29 +000031{
Kate Stoneb9c1b512016-09-06 20:57:50 +000032 if (section_sp)
Greg Claytone72dfb32012-02-24 01:59:29 +000033 m_opaque_wp = section_sp;
Greg Claytoncac9c5f2011-09-24 00:52:29 +000034}
35
Kate Stoneb9c1b512016-09-06 20:57:50 +000036const SBSection &SBSection::operator=(const SBSection &rhs) {
37 m_opaque_wp = rhs.m_opaque_wp;
38 return *this;
Greg Claytoncac9c5f2011-09-24 00:52:29 +000039}
40
Kate Stoneb9c1b512016-09-06 20:57:50 +000041SBSection::~SBSection() {}
42
43bool SBSection::IsValid() const {
44 SectionSP section_sp(GetSP());
45 return section_sp && section_sp->GetModule().get() != NULL;
46}
47
48const char *SBSection::GetName() {
49 SectionSP section_sp(GetSP());
50 if (section_sp)
51 return section_sp->GetName().GetCString();
52 return NULL;
53}
54
55lldb::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
66lldb::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));
Greg Claytonfed39aa2012-06-27 22:22:28 +000074 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000075 }
76 return sb_section;
Greg Claytonfed39aa2012-06-27 22:22:28 +000077}
78
Kate Stoneb9c1b512016-09-06 20:57:50 +000079size_t SBSection::GetNumSubSections() {
80 SectionSP section_sp(GetSP());
81 if (section_sp)
82 return section_sp->GetChildren().GetSize();
83 return 0;
Greg Claytoncac9c5f2011-09-24 00:52:29 +000084}
85
Kate Stoneb9c1b512016-09-06 20:57:50 +000086lldb::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
94lldb::SectionSP SBSection::GetSP() const { return m_opaque_wp.lock(); }
95
96void SBSection::SetSP(const lldb::SectionSP &section_sp) {
97 m_opaque_wp = section_sp;
98}
99
100lldb::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
108lldb::addr_t SBSection::GetLoadAddress(lldb::SBTarget &sb_target) {
109 TargetSP target_sp(sb_target.GetSP());
110 if (target_sp) {
111 SectionSP section_sp(GetSP());
Greg Claytone72dfb32012-02-24 01:59:29 +0000112 if (section_sp)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000113 return section_sp->GetLoadBaseAddress(target_sp.get());
114 }
115 return LLDB_INVALID_ADDRESS;
116}
117
118lldb::addr_t SBSection::GetByteSize() {
119 SectionSP section_sp(GetSP());
120 if (section_sp)
121 return section_sp->GetByteSize();
122 return 0;
123}
124
125uint64_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
138uint64_t SBSection::GetFileByteSize() {
139 SectionSP section_sp(GetSP());
140 if (section_sp)
141 return section_sp->GetFileSize();
142 return 0;
143}
144
145SBData SBSection::GetSectionData() { return GetSectionData(0, UINT64_MAX); }
146
147SBData 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 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000177 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000178 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000179 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000180 }
181 return sb_data;
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000182}
183
Kate Stoneb9c1b512016-09-06 20:57:50 +0000184SectionType SBSection::GetSectionType() {
185 SectionSP section_sp(GetSP());
186 if (section_sp.get())
187 return section_sp->GetType();
188 return eSectionTypeInvalid;
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000189}
190
Abhishek Aggarwal3c7f0702016-09-08 12:22:56 +0000191uint32_t
192SBSection::GetPermissions() const
193{
194 SectionSP section_sp(GetSP());
195 if (section_sp)
196 return section_sp->GetPermissions();
197 return 0;
198}
199
Kate Stoneb9c1b512016-09-06 20:57:50 +0000200uint32_t SBSection::GetTargetByteSize() {
201 SectionSP section_sp(GetSP());
202 if (section_sp.get())
203 return section_sp->GetTargetByteSize();
204 return 0;
Greg Claytond9dc52d2011-09-24 05:04:40 +0000205}
206
Kate Stoneb9c1b512016-09-06 20:57:50 +0000207bool 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;
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000213}
214
Kate Stoneb9c1b512016-09-06 20:57:50 +0000215bool 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;
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000219}
220
Kate Stoneb9c1b512016-09-06 20:57:50 +0000221bool 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;
Matthew Gardinerc928de32014-10-22 07:22:56 +0000235}