blob: 809eca60c6838cef9f7b53100796c15b5d084475 [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 +000021
22using namespace lldb;
23using namespace lldb_private;
24
25
26SBSection::SBSection () :
Greg Claytone72dfb32012-02-24 01:59:29 +000027 m_opaque_wp ()
Greg Claytoncac9c5f2011-09-24 00:52:29 +000028{
29}
30
31SBSection::SBSection (const SBSection &rhs) :
Greg Claytone72dfb32012-02-24 01:59:29 +000032 m_opaque_wp (rhs.m_opaque_wp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +000033{
Greg Claytoncac9c5f2011-09-24 00:52:29 +000034}
35
36
37
Greg Claytone72dfb32012-02-24 01:59:29 +000038SBSection::SBSection (const lldb::SectionSP &section_sp) :
39 m_opaque_wp () // Don't init with section_sp otherwise this will throw if section_sp doesn't contain a valid Section *
Greg Claytoncac9c5f2011-09-24 00:52:29 +000040{
Greg Claytone72dfb32012-02-24 01:59:29 +000041 if (section_sp)
42 m_opaque_wp = section_sp;
Greg Claytoncac9c5f2011-09-24 00:52:29 +000043}
44
45const SBSection &
46SBSection::operator = (const SBSection &rhs)
47{
Greg Claytone72dfb32012-02-24 01:59:29 +000048 m_opaque_wp = rhs.m_opaque_wp;
Greg Claytoncac9c5f2011-09-24 00:52:29 +000049 return *this;
50}
51
52SBSection::~SBSection ()
53{
54}
55
56bool
57SBSection::IsValid () const
58{
Greg Claytone72dfb32012-02-24 01:59:29 +000059 SectionSP section_sp (GetSP());
60 return section_sp && section_sp->GetModule().get() != NULL;
Greg Claytoncac9c5f2011-09-24 00:52:29 +000061}
62
Greg Claytonf644ddf2011-09-24 01:37:21 +000063const char *
64SBSection::GetName ()
65{
Greg Claytone72dfb32012-02-24 01:59:29 +000066 SectionSP section_sp (GetSP());
67 if (section_sp)
68 return section_sp->GetName().GetCString();
Greg Claytonf644ddf2011-09-24 01:37:21 +000069 return NULL;
70}
71
Greg Clayton1d4c5402013-06-13 21:23:23 +000072lldb::SBSection
73SBSection::GetParent()
74{
75 lldb::SBSection sb_section;
76 SectionSP section_sp (GetSP());
77 if (section_sp)
78 {
79 SectionSP parent_section_sp (section_sp->GetParent());
80 if (parent_section_sp)
81 sb_section.SetSP(parent_section_sp);
82 }
83 return sb_section;
84}
85
Greg Claytonf644ddf2011-09-24 01:37:21 +000086
Greg Claytoncac9c5f2011-09-24 00:52:29 +000087lldb::SBSection
88SBSection::FindSubSection (const char *sect_name)
89{
90 lldb::SBSection sb_section;
Greg Claytone72dfb32012-02-24 01:59:29 +000091 if (sect_name)
Greg Claytoncac9c5f2011-09-24 00:52:29 +000092 {
Greg Claytone72dfb32012-02-24 01:59:29 +000093 SectionSP section_sp (GetSP());
94 if (section_sp)
95 {
96 ConstString const_sect_name(sect_name);
97 sb_section.SetSP(section_sp->GetChildren ().FindSectionByName(const_sect_name));
98 }
Greg Claytoncac9c5f2011-09-24 00:52:29 +000099 }
100 return sb_section;
101}
102
103size_t
104SBSection::GetNumSubSections ()
105{
Greg Claytone72dfb32012-02-24 01:59:29 +0000106 SectionSP section_sp (GetSP());
107 if (section_sp)
108 return section_sp->GetChildren ().GetSize();
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000109 return 0;
110}
111
112lldb::SBSection
113SBSection::GetSubSectionAtIndex (size_t idx)
114{
115 lldb::SBSection sb_section;
Greg Claytone72dfb32012-02-24 01:59:29 +0000116 SectionSP section_sp (GetSP());
117 if (section_sp)
118 sb_section.SetSP (section_sp->GetChildren ().GetSectionAtIndex(idx));
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000119 return sb_section;
120}
121
Greg Claytone72dfb32012-02-24 01:59:29 +0000122lldb::SectionSP
123SBSection::GetSP() const
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000124{
Greg Claytone72dfb32012-02-24 01:59:29 +0000125 return m_opaque_wp.lock();
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000126}
127
128void
Greg Claytone72dfb32012-02-24 01:59:29 +0000129SBSection::SetSP(const lldb::SectionSP &section_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000130{
Greg Claytone72dfb32012-02-24 01:59:29 +0000131 m_opaque_wp = section_sp;
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000132}
133
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000134lldb::addr_t
135SBSection::GetFileAddress ()
136{
137 lldb::addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Claytone72dfb32012-02-24 01:59:29 +0000138 SectionSP section_sp (GetSP());
139 if (section_sp)
140 return section_sp->GetFileAddress();
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000141 return file_addr;
142}
143
144lldb::addr_t
Greg Claytonfed39aa2012-06-27 22:22:28 +0000145SBSection::GetLoadAddress (lldb::SBTarget &sb_target)
146{
147 TargetSP target_sp(sb_target.GetSP());
148 if (target_sp)
149 {
150 SectionSP section_sp (GetSP());
151 if (section_sp)
152 return section_sp->GetLoadBaseAddress(target_sp.get());
153 }
154 return LLDB_INVALID_ADDRESS;
155
156}
157
158
159
160lldb::addr_t
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000161SBSection::GetByteSize ()
162{
Greg Claytone72dfb32012-02-24 01:59:29 +0000163 SectionSP section_sp (GetSP());
164 if (section_sp)
165 return section_sp->GetByteSize();
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000166 return 0;
167}
168
169uint64_t
170SBSection::GetFileOffset ()
171{
Greg Claytone72dfb32012-02-24 01:59:29 +0000172 SectionSP section_sp (GetSP());
173 if (section_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000174 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000175 ModuleSP module_sp (section_sp->GetModule());
176 if (module_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000177 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000178 ObjectFile *objfile = module_sp->GetObjectFile();
179 if (objfile)
Greg Clayton5ce9c562013-02-06 17:22:03 +0000180 return objfile->GetFileOffset() + section_sp->GetFileOffset();
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000181 }
182 }
Greg Claytone72dfb32012-02-24 01:59:29 +0000183 return UINT64_MAX;
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000184}
185
186uint64_t
187SBSection::GetFileByteSize ()
188{
Greg Claytone72dfb32012-02-24 01:59:29 +0000189 SectionSP section_sp (GetSP());
190 if (section_sp)
191 return section_sp->GetFileSize();
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000192 return 0;
193}
194
195SBData
Greg Claytond9dc52d2011-09-24 05:04:40 +0000196SBSection::GetSectionData ()
197{
198 return GetSectionData (0, UINT64_MAX);
199}
200
201SBData
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000202SBSection::GetSectionData (uint64_t offset, uint64_t size)
203{
204 SBData sb_data;
Greg Claytone72dfb32012-02-24 01:59:29 +0000205 SectionSP section_sp (GetSP());
206 if (section_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000207 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000208 const uint64_t sect_file_size = section_sp->GetFileSize();
209 if (sect_file_size > 0)
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000210 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000211 ModuleSP module_sp (section_sp->GetModule());
212 if (module_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000213 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000214 ObjectFile *objfile = module_sp->GetObjectFile();
215 if (objfile)
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000216 {
Greg Clayton5ce9c562013-02-06 17:22:03 +0000217 const uint64_t sect_file_offset = objfile->GetFileOffset() + section_sp->GetFileOffset();
Greg Claytone72dfb32012-02-24 01:59:29 +0000218 const uint64_t file_offset = sect_file_offset + offset;
219 uint64_t file_size = size;
220 if (file_size == UINT64_MAX)
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000221 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000222 file_size = section_sp->GetByteSize();
223 if (file_size > offset)
224 file_size -= offset;
225 else
226 file_size = 0;
227 }
228 DataBufferSP data_buffer_sp (objfile->GetFileSpec().ReadFileContents (file_offset, file_size));
229 if (data_buffer_sp && data_buffer_sp->GetByteSize() > 0)
230 {
231 DataExtractorSP data_extractor_sp (new DataExtractor (data_buffer_sp,
232 objfile->GetByteOrder(),
233 objfile->GetAddressByteSize()));
234
235 sb_data.SetOpaque (data_extractor_sp);
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000236 }
237 }
238 }
239 }
240 }
241 return sb_data;
242}
243
244SectionType
245SBSection::GetSectionType ()
246{
Greg Claytone72dfb32012-02-24 01:59:29 +0000247 SectionSP section_sp (GetSP());
248 if (section_sp.get())
249 return section_sp->GetType();
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000250 return eSectionTypeInvalid;
251}
252
Matthew Gardinerc928de32014-10-22 07:22:56 +0000253uint32_t
254SBSection::GetTargetByteSize ()
255{
256 SectionSP section_sp (GetSP());
257 if (section_sp.get())
258 return section_sp->GetTargetByteSize();
259 return 0;
260}
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000261
262bool
263SBSection::operator == (const SBSection &rhs)
264{
Greg Claytone72dfb32012-02-24 01:59:29 +0000265 SectionSP lhs_section_sp (GetSP());
266 SectionSP rhs_section_sp (rhs.GetSP());
267 if (lhs_section_sp && rhs_section_sp)
268 return lhs_section_sp == rhs_section_sp;
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000269 return false;
270}
271
272bool
273SBSection::operator != (const SBSection &rhs)
274{
Greg Claytone72dfb32012-02-24 01:59:29 +0000275 SectionSP lhs_section_sp (GetSP());
276 SectionSP rhs_section_sp (rhs.GetSP());
277 return lhs_section_sp != rhs_section_sp;
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000278}
279
280bool
281SBSection::GetDescription (SBStream &description)
282{
Greg Claytonda7bc7d2011-11-13 06:57:31 +0000283 Stream &strm = description.ref();
284
Greg Claytone72dfb32012-02-24 01:59:29 +0000285 SectionSP section_sp (GetSP());
286 if (section_sp)
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000287 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000288 const addr_t file_addr = section_sp->GetFileAddress();
Daniel Malead01b2952012-11-29 21:49:15 +0000289 strm.Printf ("[0x%16.16" PRIx64 "-0x%16.16" PRIx64 ") ", file_addr, file_addr + section_sp->GetByteSize());
Greg Claytone72dfb32012-02-24 01:59:29 +0000290 section_sp->DumpName(&strm);
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000291 }
292 else
293 {
Greg Claytonda7bc7d2011-11-13 06:57:31 +0000294 strm.PutCString ("No value");
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000295 }
296
297 return true;
298}
299