blob: 06a08d865f447e9884fc54deeeab39aedc0081a5 [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 Clayton3e8c25f2011-09-24 00:52:29 +000019
Greg Clayton3e8c25f2011-09-24 00:52:29 +000020
21using namespace lldb;
22using namespace lldb_private;
23
24
25SBSection::SBSection () :
Greg Clayton3508c382012-02-24 01:59:29 +000026 m_opaque_wp ()
Greg Clayton3e8c25f2011-09-24 00:52:29 +000027{
28}
29
30SBSection::SBSection (const SBSection &rhs) :
Greg Clayton3508c382012-02-24 01:59:29 +000031 m_opaque_wp (rhs.m_opaque_wp)
Greg Clayton3e8c25f2011-09-24 00:52:29 +000032{
Greg Clayton3e8c25f2011-09-24 00:52:29 +000033}
34
35
36
Greg Clayton3508c382012-02-24 01:59:29 +000037SBSection::SBSection (const lldb::SectionSP &section_sp) :
38 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 +000039{
Greg Clayton3508c382012-02-24 01:59:29 +000040 if (section_sp)
41 m_opaque_wp = section_sp;
Greg Clayton3e8c25f2011-09-24 00:52:29 +000042}
43
44const SBSection &
45SBSection::operator = (const SBSection &rhs)
46{
Greg Clayton3508c382012-02-24 01:59:29 +000047 m_opaque_wp = rhs.m_opaque_wp;
Greg Clayton3e8c25f2011-09-24 00:52:29 +000048 return *this;
49}
50
51SBSection::~SBSection ()
52{
53}
54
55bool
56SBSection::IsValid () const
57{
Greg Clayton3508c382012-02-24 01:59:29 +000058 SectionSP section_sp (GetSP());
59 return section_sp && section_sp->GetModule().get() != NULL;
Greg Clayton3e8c25f2011-09-24 00:52:29 +000060}
61
Greg Clayton980c7502011-09-24 01:37:21 +000062const char *
63SBSection::GetName ()
64{
Greg Clayton3508c382012-02-24 01:59:29 +000065 SectionSP section_sp (GetSP());
66 if (section_sp)
67 return section_sp->GetName().GetCString();
Greg Clayton980c7502011-09-24 01:37:21 +000068 return NULL;
69}
70
71
Greg Clayton3e8c25f2011-09-24 00:52:29 +000072lldb::SBSection
73SBSection::FindSubSection (const char *sect_name)
74{
75 lldb::SBSection sb_section;
Greg Clayton3508c382012-02-24 01:59:29 +000076 if (sect_name)
Greg Clayton3e8c25f2011-09-24 00:52:29 +000077 {
Greg Clayton3508c382012-02-24 01:59:29 +000078 SectionSP section_sp (GetSP());
79 if (section_sp)
80 {
81 ConstString const_sect_name(sect_name);
82 sb_section.SetSP(section_sp->GetChildren ().FindSectionByName(const_sect_name));
83 }
Greg Clayton3e8c25f2011-09-24 00:52:29 +000084 }
85 return sb_section;
86}
87
88size_t
89SBSection::GetNumSubSections ()
90{
Greg Clayton3508c382012-02-24 01:59:29 +000091 SectionSP section_sp (GetSP());
92 if (section_sp)
93 return section_sp->GetChildren ().GetSize();
Greg Clayton3e8c25f2011-09-24 00:52:29 +000094 return 0;
95}
96
97lldb::SBSection
98SBSection::GetSubSectionAtIndex (size_t idx)
99{
100 lldb::SBSection sb_section;
Greg Clayton3508c382012-02-24 01:59:29 +0000101 SectionSP section_sp (GetSP());
102 if (section_sp)
103 sb_section.SetSP (section_sp->GetChildren ().GetSectionAtIndex(idx));
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000104 return sb_section;
105}
106
Greg Clayton3508c382012-02-24 01:59:29 +0000107lldb::SectionSP
108SBSection::GetSP() const
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000109{
Greg Clayton3508c382012-02-24 01:59:29 +0000110 return m_opaque_wp.lock();
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000111}
112
113void
Greg Clayton3508c382012-02-24 01:59:29 +0000114SBSection::SetSP(const lldb::SectionSP &section_sp)
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000115{
Greg Clayton3508c382012-02-24 01:59:29 +0000116 m_opaque_wp = section_sp;
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000117}
118
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000119lldb::addr_t
120SBSection::GetFileAddress ()
121{
122 lldb::addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton3508c382012-02-24 01:59:29 +0000123 SectionSP section_sp (GetSP());
124 if (section_sp)
125 return section_sp->GetFileAddress();
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000126 return file_addr;
127}
128
129lldb::addr_t
Greg Clayton5c5a38e2012-06-27 22:22:28 +0000130SBSection::GetLoadAddress (lldb::SBTarget &sb_target)
131{
132 TargetSP target_sp(sb_target.GetSP());
133 if (target_sp)
134 {
135 SectionSP section_sp (GetSP());
136 if (section_sp)
137 return section_sp->GetLoadBaseAddress(target_sp.get());
138 }
139 return LLDB_INVALID_ADDRESS;
140
141}
142
143
144
145lldb::addr_t
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000146SBSection::GetByteSize ()
147{
Greg Clayton3508c382012-02-24 01:59:29 +0000148 SectionSP section_sp (GetSP());
149 if (section_sp)
150 return section_sp->GetByteSize();
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000151 return 0;
152}
153
154uint64_t
155SBSection::GetFileOffset ()
156{
Greg Clayton3508c382012-02-24 01:59:29 +0000157 SectionSP section_sp (GetSP());
158 if (section_sp)
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000159 {
Greg Clayton3508c382012-02-24 01:59:29 +0000160 ModuleSP module_sp (section_sp->GetModule());
161 if (module_sp)
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000162 {
Greg Clayton3508c382012-02-24 01:59:29 +0000163 ObjectFile *objfile = module_sp->GetObjectFile();
164 if (objfile)
165 return objfile->GetOffset() + section_sp->GetFileOffset();
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000166 }
167 }
Greg Clayton3508c382012-02-24 01:59:29 +0000168 return UINT64_MAX;
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000169}
170
171uint64_t
172SBSection::GetFileByteSize ()
173{
Greg Clayton3508c382012-02-24 01:59:29 +0000174 SectionSP section_sp (GetSP());
175 if (section_sp)
176 return section_sp->GetFileSize();
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000177 return 0;
178}
179
180SBData
Greg Clayton15ef51e2011-09-24 05:04:40 +0000181SBSection::GetSectionData ()
182{
183 return GetSectionData (0, UINT64_MAX);
184}
185
186SBData
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000187SBSection::GetSectionData (uint64_t offset, uint64_t size)
188{
189 SBData sb_data;
Greg Clayton3508c382012-02-24 01:59:29 +0000190 SectionSP section_sp (GetSP());
191 if (section_sp)
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000192 {
Greg Clayton3508c382012-02-24 01:59:29 +0000193 const uint64_t sect_file_size = section_sp->GetFileSize();
194 if (sect_file_size > 0)
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000195 {
Greg Clayton3508c382012-02-24 01:59:29 +0000196 ModuleSP module_sp (section_sp->GetModule());
197 if (module_sp)
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000198 {
Greg Clayton3508c382012-02-24 01:59:29 +0000199 ObjectFile *objfile = module_sp->GetObjectFile();
200 if (objfile)
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000201 {
Greg Clayton3508c382012-02-24 01:59:29 +0000202 const uint64_t sect_file_offset = objfile->GetOffset() + section_sp->GetFileOffset();
203 const uint64_t file_offset = sect_file_offset + offset;
204 uint64_t file_size = size;
205 if (file_size == UINT64_MAX)
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000206 {
Greg Clayton3508c382012-02-24 01:59:29 +0000207 file_size = section_sp->GetByteSize();
208 if (file_size > offset)
209 file_size -= offset;
210 else
211 file_size = 0;
212 }
213 DataBufferSP data_buffer_sp (objfile->GetFileSpec().ReadFileContents (file_offset, file_size));
214 if (data_buffer_sp && data_buffer_sp->GetByteSize() > 0)
215 {
216 DataExtractorSP data_extractor_sp (new DataExtractor (data_buffer_sp,
217 objfile->GetByteOrder(),
218 objfile->GetAddressByteSize()));
219
220 sb_data.SetOpaque (data_extractor_sp);
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000221 }
222 }
223 }
224 }
225 }
226 return sb_data;
227}
228
229SectionType
230SBSection::GetSectionType ()
231{
Greg Clayton3508c382012-02-24 01:59:29 +0000232 SectionSP section_sp (GetSP());
233 if (section_sp.get())
234 return section_sp->GetType();
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000235 return eSectionTypeInvalid;
236}
237
238
239bool
240SBSection::operator == (const SBSection &rhs)
241{
Greg Clayton3508c382012-02-24 01:59:29 +0000242 SectionSP lhs_section_sp (GetSP());
243 SectionSP rhs_section_sp (rhs.GetSP());
244 if (lhs_section_sp && rhs_section_sp)
245 return lhs_section_sp == rhs_section_sp;
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000246 return false;
247}
248
249bool
250SBSection::operator != (const SBSection &rhs)
251{
Greg Clayton3508c382012-02-24 01:59:29 +0000252 SectionSP lhs_section_sp (GetSP());
253 SectionSP rhs_section_sp (rhs.GetSP());
254 return lhs_section_sp != rhs_section_sp;
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000255}
256
257bool
258SBSection::GetDescription (SBStream &description)
259{
Greg Clayton96154be2011-11-13 06:57:31 +0000260 Stream &strm = description.ref();
261
Greg Clayton3508c382012-02-24 01:59:29 +0000262 SectionSP section_sp (GetSP());
263 if (section_sp)
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000264 {
Greg Clayton3508c382012-02-24 01:59:29 +0000265 const addr_t file_addr = section_sp->GetFileAddress();
266 strm.Printf ("[0x%16.16llx-0x%16.16llx) ", file_addr, file_addr + section_sp->GetByteSize());
267 section_sp->DumpName(&strm);
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000268 }
269 else
270 {
Greg Clayton96154be2011-11-13 06:57:31 +0000271 strm.PutCString ("No value");
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000272 }
273
274 return true;
275}
276