blob: 24b94a7a53a79610dfa5704378fe2cf3e54df157 [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"
12#include "lldb/Core/DataBuffer.h"
13#include "lldb/Core/DataExtractor.h"
14#include "lldb/Core/Log.h"
Greg Clayton3e8c25f2011-09-24 00:52:29 +000015#include "lldb/Core/Module.h"
Greg Clayton15ef51e2011-09-24 05:04:40 +000016#include "lldb/Core/Section.h"
17#include "lldb/Core/StreamString.h"
Greg Clayton3e8c25f2011-09-24 00:52:29 +000018
Greg Clayton3e8c25f2011-09-24 00:52:29 +000019
20using namespace lldb;
21using namespace lldb_private;
22
23
24SBSection::SBSection () :
Greg Clayton3508c382012-02-24 01:59:29 +000025 m_opaque_wp ()
Greg Clayton3e8c25f2011-09-24 00:52:29 +000026{
27}
28
29SBSection::SBSection (const SBSection &rhs) :
Greg Clayton3508c382012-02-24 01:59:29 +000030 m_opaque_wp (rhs.m_opaque_wp)
Greg Clayton3e8c25f2011-09-24 00:52:29 +000031{
Greg Clayton3e8c25f2011-09-24 00:52:29 +000032}
33
34
35
Greg Clayton3508c382012-02-24 01:59:29 +000036SBSection::SBSection (const lldb::SectionSP &section_sp) :
37 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 +000038{
Greg Clayton3508c382012-02-24 01:59:29 +000039 if (section_sp)
40 m_opaque_wp = section_sp;
Greg Clayton3e8c25f2011-09-24 00:52:29 +000041}
42
43const SBSection &
44SBSection::operator = (const SBSection &rhs)
45{
Greg Clayton3508c382012-02-24 01:59:29 +000046 m_opaque_wp = rhs.m_opaque_wp;
Greg Clayton3e8c25f2011-09-24 00:52:29 +000047 return *this;
48}
49
50SBSection::~SBSection ()
51{
52}
53
54bool
55SBSection::IsValid () const
56{
Greg Clayton3508c382012-02-24 01:59:29 +000057 SectionSP section_sp (GetSP());
58 return section_sp && section_sp->GetModule().get() != NULL;
Greg Clayton3e8c25f2011-09-24 00:52:29 +000059}
60
Greg Clayton980c7502011-09-24 01:37:21 +000061const char *
62SBSection::GetName ()
63{
Greg Clayton3508c382012-02-24 01:59:29 +000064 SectionSP section_sp (GetSP());
65 if (section_sp)
66 return section_sp->GetName().GetCString();
Greg Clayton980c7502011-09-24 01:37:21 +000067 return NULL;
68}
69
70
Greg Clayton3e8c25f2011-09-24 00:52:29 +000071lldb::SBSection
72SBSection::FindSubSection (const char *sect_name)
73{
74 lldb::SBSection sb_section;
Greg Clayton3508c382012-02-24 01:59:29 +000075 if (sect_name)
Greg Clayton3e8c25f2011-09-24 00:52:29 +000076 {
Greg Clayton3508c382012-02-24 01:59:29 +000077 SectionSP section_sp (GetSP());
78 if (section_sp)
79 {
80 ConstString const_sect_name(sect_name);
81 sb_section.SetSP(section_sp->GetChildren ().FindSectionByName(const_sect_name));
82 }
Greg Clayton3e8c25f2011-09-24 00:52:29 +000083 }
84 return sb_section;
85}
86
87size_t
88SBSection::GetNumSubSections ()
89{
Greg Clayton3508c382012-02-24 01:59:29 +000090 SectionSP section_sp (GetSP());
91 if (section_sp)
92 return section_sp->GetChildren ().GetSize();
Greg Clayton3e8c25f2011-09-24 00:52:29 +000093 return 0;
94}
95
96lldb::SBSection
97SBSection::GetSubSectionAtIndex (size_t idx)
98{
99 lldb::SBSection sb_section;
Greg Clayton3508c382012-02-24 01:59:29 +0000100 SectionSP section_sp (GetSP());
101 if (section_sp)
102 sb_section.SetSP (section_sp->GetChildren ().GetSectionAtIndex(idx));
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000103 return sb_section;
104}
105
Greg Clayton3508c382012-02-24 01:59:29 +0000106lldb::SectionSP
107SBSection::GetSP() const
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000108{
Greg Clayton3508c382012-02-24 01:59:29 +0000109 return m_opaque_wp.lock();
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000110}
111
112void
Greg Clayton3508c382012-02-24 01:59:29 +0000113SBSection::SetSP(const lldb::SectionSP &section_sp)
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000114{
Greg Clayton3508c382012-02-24 01:59:29 +0000115 m_opaque_wp = section_sp;
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000116}
117
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000118lldb::addr_t
119SBSection::GetFileAddress ()
120{
121 lldb::addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton3508c382012-02-24 01:59:29 +0000122 SectionSP section_sp (GetSP());
123 if (section_sp)
124 return section_sp->GetFileAddress();
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000125 return file_addr;
126}
127
128lldb::addr_t
129SBSection::GetByteSize ()
130{
Greg Clayton3508c382012-02-24 01:59:29 +0000131 SectionSP section_sp (GetSP());
132 if (section_sp)
133 return section_sp->GetByteSize();
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000134 return 0;
135}
136
137uint64_t
138SBSection::GetFileOffset ()
139{
Greg Clayton3508c382012-02-24 01:59:29 +0000140 SectionSP section_sp (GetSP());
141 if (section_sp)
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000142 {
Greg Clayton3508c382012-02-24 01:59:29 +0000143 ModuleSP module_sp (section_sp->GetModule());
144 if (module_sp)
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000145 {
Greg Clayton3508c382012-02-24 01:59:29 +0000146 ObjectFile *objfile = module_sp->GetObjectFile();
147 if (objfile)
148 return objfile->GetOffset() + section_sp->GetFileOffset();
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000149 }
150 }
Greg Clayton3508c382012-02-24 01:59:29 +0000151 return UINT64_MAX;
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000152}
153
154uint64_t
155SBSection::GetFileByteSize ()
156{
Greg Clayton3508c382012-02-24 01:59:29 +0000157 SectionSP section_sp (GetSP());
158 if (section_sp)
159 return section_sp->GetFileSize();
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000160 return 0;
161}
162
163SBData
Greg Clayton15ef51e2011-09-24 05:04:40 +0000164SBSection::GetSectionData ()
165{
166 return GetSectionData (0, UINT64_MAX);
167}
168
169SBData
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000170SBSection::GetSectionData (uint64_t offset, uint64_t size)
171{
172 SBData sb_data;
Greg Clayton3508c382012-02-24 01:59:29 +0000173 SectionSP section_sp (GetSP());
174 if (section_sp)
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000175 {
Greg Clayton3508c382012-02-24 01:59:29 +0000176 const uint64_t sect_file_size = section_sp->GetFileSize();
177 if (sect_file_size > 0)
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000178 {
Greg Clayton3508c382012-02-24 01:59:29 +0000179 ModuleSP module_sp (section_sp->GetModule());
180 if (module_sp)
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000181 {
Greg Clayton3508c382012-02-24 01:59:29 +0000182 ObjectFile *objfile = module_sp->GetObjectFile();
183 if (objfile)
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000184 {
Greg Clayton3508c382012-02-24 01:59:29 +0000185 const uint64_t sect_file_offset = objfile->GetOffset() + section_sp->GetFileOffset();
186 const uint64_t file_offset = sect_file_offset + offset;
187 uint64_t file_size = size;
188 if (file_size == UINT64_MAX)
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000189 {
Greg Clayton3508c382012-02-24 01:59:29 +0000190 file_size = section_sp->GetByteSize();
191 if (file_size > offset)
192 file_size -= offset;
193 else
194 file_size = 0;
195 }
196 DataBufferSP data_buffer_sp (objfile->GetFileSpec().ReadFileContents (file_offset, file_size));
197 if (data_buffer_sp && data_buffer_sp->GetByteSize() > 0)
198 {
199 DataExtractorSP data_extractor_sp (new DataExtractor (data_buffer_sp,
200 objfile->GetByteOrder(),
201 objfile->GetAddressByteSize()));
202
203 sb_data.SetOpaque (data_extractor_sp);
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000204 }
205 }
206 }
207 }
208 }
209 return sb_data;
210}
211
212SectionType
213SBSection::GetSectionType ()
214{
Greg Clayton3508c382012-02-24 01:59:29 +0000215 SectionSP section_sp (GetSP());
216 if (section_sp.get())
217 return section_sp->GetType();
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000218 return eSectionTypeInvalid;
219}
220
221
222bool
223SBSection::operator == (const SBSection &rhs)
224{
Greg Clayton3508c382012-02-24 01:59:29 +0000225 SectionSP lhs_section_sp (GetSP());
226 SectionSP rhs_section_sp (rhs.GetSP());
227 if (lhs_section_sp && rhs_section_sp)
228 return lhs_section_sp == rhs_section_sp;
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000229 return false;
230}
231
232bool
233SBSection::operator != (const SBSection &rhs)
234{
Greg Clayton3508c382012-02-24 01:59:29 +0000235 SectionSP lhs_section_sp (GetSP());
236 SectionSP rhs_section_sp (rhs.GetSP());
237 return lhs_section_sp != rhs_section_sp;
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000238}
239
240bool
241SBSection::GetDescription (SBStream &description)
242{
Greg Clayton96154be2011-11-13 06:57:31 +0000243 Stream &strm = description.ref();
244
Greg Clayton3508c382012-02-24 01:59:29 +0000245 SectionSP section_sp (GetSP());
246 if (section_sp)
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000247 {
Greg Clayton3508c382012-02-24 01:59:29 +0000248 const addr_t file_addr = section_sp->GetFileAddress();
249 strm.Printf ("[0x%16.16llx-0x%16.16llx) ", file_addr, file_addr + section_sp->GetByteSize());
250 section_sp->DumpName(&strm);
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000251 }
252 else
253 {
Greg Clayton96154be2011-11-13 06:57:31 +0000254 strm.PutCString ("No value");
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000255 }
256
257 return true;
258}
259