blob: 0f55f0169f3b10f75727d93cf05470750b6feecf [file] [log] [blame]
Greg Clayton3e8c25f2011-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 Clayton5c5a38e2012-06-27 22:22:28 +000012#include "lldb/API/SBTarget.h"
Greg Clayton3e8c25f2011-09-24 00:52:29 +000013#include "lldb/Core/DataBuffer.h"
14#include "lldb/Core/DataExtractor.h"
15#include "lldb/Core/Log.h"
Greg Clayton3e8c25f2011-09-24 00:52:29 +000016#include "lldb/Core/Module.h"
Greg Clayton15ef51e2011-09-24 05:04:40 +000017#include "lldb/Core/Section.h"
18#include "lldb/Core/StreamString.h"
Greg Clayton49ce8962012-08-29 21:13:06 +000019#include "lldb/Symbol/ObjectFile.h"
Greg Clayton3e8c25f2011-09-24 00:52:29 +000020
Greg Clayton3e8c25f2011-09-24 00:52:29 +000021
22using namespace lldb;
23using namespace lldb_private;
24
25
26SBSection::SBSection () :
Greg Clayton3508c382012-02-24 01:59:29 +000027 m_opaque_wp ()
Greg Clayton3e8c25f2011-09-24 00:52:29 +000028{
29}
30
31SBSection::SBSection (const SBSection &rhs) :
Greg Clayton3508c382012-02-24 01:59:29 +000032 m_opaque_wp (rhs.m_opaque_wp)
Greg Clayton3e8c25f2011-09-24 00:52:29 +000033{
Greg Clayton3e8c25f2011-09-24 00:52:29 +000034}
35
36
37
Greg Clayton3508c382012-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 Clayton3e8c25f2011-09-24 00:52:29 +000040{
Greg Clayton3508c382012-02-24 01:59:29 +000041 if (section_sp)
42 m_opaque_wp = section_sp;
Greg Clayton3e8c25f2011-09-24 00:52:29 +000043}
44
45const SBSection &
46SBSection::operator = (const SBSection &rhs)
47{
Greg Clayton3508c382012-02-24 01:59:29 +000048 m_opaque_wp = rhs.m_opaque_wp;
Greg Clayton3e8c25f2011-09-24 00:52:29 +000049 return *this;
50}
51
52SBSection::~SBSection ()
53{
54}
55
56bool
57SBSection::IsValid () const
58{
Greg Clayton3508c382012-02-24 01:59:29 +000059 SectionSP section_sp (GetSP());
60 return section_sp && section_sp->GetModule().get() != NULL;
Greg Clayton3e8c25f2011-09-24 00:52:29 +000061}
62
Greg Clayton980c7502011-09-24 01:37:21 +000063const char *
64SBSection::GetName ()
65{
Greg Clayton3508c382012-02-24 01:59:29 +000066 SectionSP section_sp (GetSP());
67 if (section_sp)
68 return section_sp->GetName().GetCString();
Greg Clayton980c7502011-09-24 01:37:21 +000069 return NULL;
70}
71
72
Greg Clayton3e8c25f2011-09-24 00:52:29 +000073lldb::SBSection
74SBSection::FindSubSection (const char *sect_name)
75{
76 lldb::SBSection sb_section;
Greg Clayton3508c382012-02-24 01:59:29 +000077 if (sect_name)
Greg Clayton3e8c25f2011-09-24 00:52:29 +000078 {
Greg Clayton3508c382012-02-24 01:59:29 +000079 SectionSP section_sp (GetSP());
80 if (section_sp)
81 {
82 ConstString const_sect_name(sect_name);
83 sb_section.SetSP(section_sp->GetChildren ().FindSectionByName(const_sect_name));
84 }
Greg Clayton3e8c25f2011-09-24 00:52:29 +000085 }
86 return sb_section;
87}
88
89size_t
90SBSection::GetNumSubSections ()
91{
Greg Clayton3508c382012-02-24 01:59:29 +000092 SectionSP section_sp (GetSP());
93 if (section_sp)
94 return section_sp->GetChildren ().GetSize();
Greg Clayton3e8c25f2011-09-24 00:52:29 +000095 return 0;
96}
97
98lldb::SBSection
99SBSection::GetSubSectionAtIndex (size_t idx)
100{
101 lldb::SBSection sb_section;
Greg Clayton3508c382012-02-24 01:59:29 +0000102 SectionSP section_sp (GetSP());
103 if (section_sp)
104 sb_section.SetSP (section_sp->GetChildren ().GetSectionAtIndex(idx));
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000105 return sb_section;
106}
107
Greg Clayton3508c382012-02-24 01:59:29 +0000108lldb::SectionSP
109SBSection::GetSP() const
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000110{
Greg Clayton3508c382012-02-24 01:59:29 +0000111 return m_opaque_wp.lock();
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000112}
113
114void
Greg Clayton3508c382012-02-24 01:59:29 +0000115SBSection::SetSP(const lldb::SectionSP &section_sp)
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000116{
Greg Clayton3508c382012-02-24 01:59:29 +0000117 m_opaque_wp = section_sp;
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000118}
119
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000120lldb::addr_t
121SBSection::GetFileAddress ()
122{
123 lldb::addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton3508c382012-02-24 01:59:29 +0000124 SectionSP section_sp (GetSP());
125 if (section_sp)
126 return section_sp->GetFileAddress();
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000127 return file_addr;
128}
129
130lldb::addr_t
Greg Clayton5c5a38e2012-06-27 22:22:28 +0000131SBSection::GetLoadAddress (lldb::SBTarget &sb_target)
132{
133 TargetSP target_sp(sb_target.GetSP());
134 if (target_sp)
135 {
136 SectionSP section_sp (GetSP());
137 if (section_sp)
138 return section_sp->GetLoadBaseAddress(target_sp.get());
139 }
140 return LLDB_INVALID_ADDRESS;
141
142}
143
144
145
146lldb::addr_t
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000147SBSection::GetByteSize ()
148{
Greg Clayton3508c382012-02-24 01:59:29 +0000149 SectionSP section_sp (GetSP());
150 if (section_sp)
151 return section_sp->GetByteSize();
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000152 return 0;
153}
154
155uint64_t
156SBSection::GetFileOffset ()
157{
Greg Clayton3508c382012-02-24 01:59:29 +0000158 SectionSP section_sp (GetSP());
159 if (section_sp)
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000160 {
Greg Clayton3508c382012-02-24 01:59:29 +0000161 ModuleSP module_sp (section_sp->GetModule());
162 if (module_sp)
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000163 {
Greg Clayton3508c382012-02-24 01:59:29 +0000164 ObjectFile *objfile = module_sp->GetObjectFile();
165 if (objfile)
166 return objfile->GetOffset() + section_sp->GetFileOffset();
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000167 }
168 }
Greg Clayton3508c382012-02-24 01:59:29 +0000169 return UINT64_MAX;
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000170}
171
172uint64_t
173SBSection::GetFileByteSize ()
174{
Greg Clayton3508c382012-02-24 01:59:29 +0000175 SectionSP section_sp (GetSP());
176 if (section_sp)
177 return section_sp->GetFileSize();
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000178 return 0;
179}
180
181SBData
Greg Clayton15ef51e2011-09-24 05:04:40 +0000182SBSection::GetSectionData ()
183{
184 return GetSectionData (0, UINT64_MAX);
185}
186
187SBData
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000188SBSection::GetSectionData (uint64_t offset, uint64_t size)
189{
190 SBData sb_data;
Greg Clayton3508c382012-02-24 01:59:29 +0000191 SectionSP section_sp (GetSP());
192 if (section_sp)
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000193 {
Greg Clayton3508c382012-02-24 01:59:29 +0000194 const uint64_t sect_file_size = section_sp->GetFileSize();
195 if (sect_file_size > 0)
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000196 {
Greg Clayton3508c382012-02-24 01:59:29 +0000197 ModuleSP module_sp (section_sp->GetModule());
198 if (module_sp)
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000199 {
Greg Clayton3508c382012-02-24 01:59:29 +0000200 ObjectFile *objfile = module_sp->GetObjectFile();
201 if (objfile)
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000202 {
Greg Clayton3508c382012-02-24 01:59:29 +0000203 const uint64_t sect_file_offset = objfile->GetOffset() + section_sp->GetFileOffset();
204 const uint64_t file_offset = sect_file_offset + offset;
205 uint64_t file_size = size;
206 if (file_size == UINT64_MAX)
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000207 {
Greg Clayton3508c382012-02-24 01:59:29 +0000208 file_size = section_sp->GetByteSize();
209 if (file_size > offset)
210 file_size -= offset;
211 else
212 file_size = 0;
213 }
214 DataBufferSP data_buffer_sp (objfile->GetFileSpec().ReadFileContents (file_offset, file_size));
215 if (data_buffer_sp && data_buffer_sp->GetByteSize() > 0)
216 {
217 DataExtractorSP data_extractor_sp (new DataExtractor (data_buffer_sp,
218 objfile->GetByteOrder(),
219 objfile->GetAddressByteSize()));
220
221 sb_data.SetOpaque (data_extractor_sp);
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000222 }
223 }
224 }
225 }
226 }
227 return sb_data;
228}
229
230SectionType
231SBSection::GetSectionType ()
232{
Greg Clayton3508c382012-02-24 01:59:29 +0000233 SectionSP section_sp (GetSP());
234 if (section_sp.get())
235 return section_sp->GetType();
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000236 return eSectionTypeInvalid;
237}
238
239
240bool
241SBSection::operator == (const SBSection &rhs)
242{
Greg Clayton3508c382012-02-24 01:59:29 +0000243 SectionSP lhs_section_sp (GetSP());
244 SectionSP rhs_section_sp (rhs.GetSP());
245 if (lhs_section_sp && rhs_section_sp)
246 return lhs_section_sp == rhs_section_sp;
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000247 return false;
248}
249
250bool
251SBSection::operator != (const SBSection &rhs)
252{
Greg Clayton3508c382012-02-24 01:59:29 +0000253 SectionSP lhs_section_sp (GetSP());
254 SectionSP rhs_section_sp (rhs.GetSP());
255 return lhs_section_sp != rhs_section_sp;
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000256}
257
258bool
259SBSection::GetDescription (SBStream &description)
260{
Greg Clayton96154be2011-11-13 06:57:31 +0000261 Stream &strm = description.ref();
262
Greg Clayton3508c382012-02-24 01:59:29 +0000263 SectionSP section_sp (GetSP());
264 if (section_sp)
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000265 {
Greg Clayton3508c382012-02-24 01:59:29 +0000266 const addr_t file_addr = section_sp->GetFileAddress();
267 strm.Printf ("[0x%16.16llx-0x%16.16llx) ", file_addr, file_addr + section_sp->GetByteSize());
268 section_sp->DumpName(&strm);
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000269 }
270 else
271 {
Greg Clayton96154be2011-11-13 06:57:31 +0000272 strm.PutCString ("No value");
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000273 }
274
275 return true;
276}
277