1 //===-- SBSection.cpp -------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/API/SBSection.h"
11 #include "lldb/API/SBStream.h"
12 #include "lldb/API/SBTarget.h"
13 #include "lldb/Core/DataBuffer.h"
14 #include "lldb/Core/DataExtractor.h"
15 #include "lldb/Core/Log.h"
16 #include "lldb/Core/Module.h"
17 #include "lldb/Core/Section.h"
18 #include "lldb/Core/StreamString.h"
19 #include "lldb/Symbol/ObjectFile.h"
23 using namespace lldb_private;
26 SBSection::SBSection () :
31 SBSection::SBSection (const SBSection &rhs) :
32 m_opaque_wp (rhs.m_opaque_wp)
38 SBSection::SBSection (const lldb::SectionSP §ion_sp) :
39 m_opaque_wp () // Don't init with section_sp otherwise this will throw if section_sp doesn't contain a valid Section *
42 m_opaque_wp = section_sp;
46 SBSection::operator = (const SBSection &rhs)
48 m_opaque_wp = rhs.m_opaque_wp;
52 SBSection::~SBSection ()
57 SBSection::IsValid () const
59 SectionSP section_sp (GetSP());
60 return section_sp && section_sp->GetModule().get() != NULL;
66 SectionSP section_sp (GetSP());
68 return section_sp->GetName().GetCString();
73 SBSection::GetParent()
75 lldb::SBSection sb_section;
76 SectionSP section_sp (GetSP());
79 SectionSP parent_section_sp (section_sp->GetParent());
80 if (parent_section_sp)
81 sb_section.SetSP(parent_section_sp);
88 SBSection::FindSubSection (const char *sect_name)
90 lldb::SBSection sb_section;
93 SectionSP section_sp (GetSP());
96 ConstString const_sect_name(sect_name);
97 sb_section.SetSP(section_sp->GetChildren ().FindSectionByName(const_sect_name));
104 SBSection::GetNumSubSections ()
106 SectionSP section_sp (GetSP());
108 return section_sp->GetChildren ().GetSize();
113 SBSection::GetSubSectionAtIndex (size_t idx)
115 lldb::SBSection sb_section;
116 SectionSP section_sp (GetSP());
118 sb_section.SetSP (section_sp->GetChildren ().GetSectionAtIndex(idx));
123 SBSection::GetSP() const
125 return m_opaque_wp.lock();
129 SBSection::SetSP(const lldb::SectionSP §ion_sp)
131 m_opaque_wp = section_sp;
135 SBSection::GetFileAddress ()
137 lldb::addr_t file_addr = LLDB_INVALID_ADDRESS;
138 SectionSP section_sp (GetSP());
140 return section_sp->GetFileAddress();
145 SBSection::GetLoadAddress (lldb::SBTarget &sb_target)
147 TargetSP target_sp(sb_target.GetSP());
150 SectionSP section_sp (GetSP());
152 return section_sp->GetLoadBaseAddress(target_sp.get());
154 return LLDB_INVALID_ADDRESS;
161 SBSection::GetByteSize ()
163 SectionSP section_sp (GetSP());
165 return section_sp->GetByteSize();
170 SBSection::GetFileOffset ()
172 SectionSP section_sp (GetSP());
175 ModuleSP module_sp (section_sp->GetModule());
178 ObjectFile *objfile = module_sp->GetObjectFile();
180 return objfile->GetFileOffset() + section_sp->GetFileOffset();
187 SBSection::GetFileByteSize ()
189 SectionSP section_sp (GetSP());
191 return section_sp->GetFileSize();
196 SBSection::GetSectionData ()
198 return GetSectionData (0, UINT64_MAX);
202 SBSection::GetSectionData (uint64_t offset, uint64_t size)
205 SectionSP section_sp (GetSP());
208 const uint64_t sect_file_size = section_sp->GetFileSize();
209 if (sect_file_size > 0)
211 ModuleSP module_sp (section_sp->GetModule());
214 ObjectFile *objfile = module_sp->GetObjectFile();
217 const uint64_t sect_file_offset = objfile->GetFileOffset() + section_sp->GetFileOffset();
218 const uint64_t file_offset = sect_file_offset + offset;
219 uint64_t file_size = size;
220 if (file_size == UINT64_MAX)
222 file_size = section_sp->GetByteSize();
223 if (file_size > offset)
228 DataBufferSP data_buffer_sp (objfile->GetFileSpec().ReadFileContents (file_offset, file_size));
229 if (data_buffer_sp && data_buffer_sp->GetByteSize() > 0)
231 DataExtractorSP data_extractor_sp (new DataExtractor (data_buffer_sp,
232 objfile->GetByteOrder(),
233 objfile->GetAddressByteSize()));
235 sb_data.SetOpaque (data_extractor_sp);
245 SBSection::GetSectionType ()
247 SectionSP section_sp (GetSP());
248 if (section_sp.get())
249 return section_sp->GetType();
250 return eSectionTypeInvalid;
255 SBSection::operator == (const SBSection &rhs)
257 SectionSP lhs_section_sp (GetSP());
258 SectionSP rhs_section_sp (rhs.GetSP());
259 if (lhs_section_sp && rhs_section_sp)
260 return lhs_section_sp == rhs_section_sp;
265 SBSection::operator != (const SBSection &rhs)
267 SectionSP lhs_section_sp (GetSP());
268 SectionSP rhs_section_sp (rhs.GetSP());
269 return lhs_section_sp != rhs_section_sp;
273 SBSection::GetDescription (SBStream &description)
275 Stream &strm = description.ref();
277 SectionSP section_sp (GetSP());
280 const addr_t file_addr = section_sp->GetFileAddress();
281 strm.Printf ("[0x%16.16" PRIx64 "-0x%16.16" PRIx64 ") ", file_addr, file_addr + section_sp->GetByteSize());
282 section_sp->DumpName(&strm);
286 strm.PutCString ("No value");