1254721Semaste//===-- SBSection.cpp -------------------------------------------*- C++ -*-===//
2254721Semaste//
3254721Semaste//                     The LLVM Compiler Infrastructure
4254721Semaste//
5254721Semaste// This file is distributed under the University of Illinois Open Source
6254721Semaste// License. See LICENSE.TXT for details.
7254721Semaste//
8254721Semaste//===----------------------------------------------------------------------===//
9254721Semaste
10254721Semaste#include "lldb/API/SBSection.h"
11254721Semaste#include "lldb/API/SBStream.h"
12254721Semaste#include "lldb/API/SBTarget.h"
13254721Semaste#include "lldb/Core/DataBuffer.h"
14254721Semaste#include "lldb/Core/DataExtractor.h"
15254721Semaste#include "lldb/Core/Log.h"
16254721Semaste#include "lldb/Core/Module.h"
17254721Semaste#include "lldb/Core/Section.h"
18254721Semaste#include "lldb/Core/StreamString.h"
19254721Semaste#include "lldb/Symbol/ObjectFile.h"
20254721Semaste
21254721Semaste
22254721Semasteusing namespace lldb;
23254721Semasteusing namespace lldb_private;
24254721Semaste
25254721Semaste
26254721SemasteSBSection::SBSection () :
27254721Semaste    m_opaque_wp ()
28254721Semaste{
29254721Semaste}
30254721Semaste
31254721SemasteSBSection::SBSection (const SBSection &rhs) :
32254721Semaste    m_opaque_wp (rhs.m_opaque_wp)
33254721Semaste{
34254721Semaste}
35254721Semaste
36254721Semaste
37254721Semaste
38254721SemasteSBSection::SBSection (const lldb::SectionSP &section_sp) :
39254721Semaste    m_opaque_wp () // Don't init with section_sp otherwise this will throw if section_sp doesn't contain a valid Section *
40254721Semaste{
41254721Semaste    if (section_sp)
42254721Semaste        m_opaque_wp = section_sp;
43254721Semaste}
44254721Semaste
45254721Semasteconst SBSection &
46254721SemasteSBSection::operator = (const SBSection &rhs)
47254721Semaste{
48254721Semaste    m_opaque_wp = rhs.m_opaque_wp;
49254721Semaste    return *this;
50254721Semaste}
51254721Semaste
52254721SemasteSBSection::~SBSection ()
53254721Semaste{
54254721Semaste}
55254721Semaste
56254721Semastebool
57254721SemasteSBSection::IsValid () const
58254721Semaste{
59254721Semaste    SectionSP section_sp (GetSP());
60254721Semaste    return section_sp && section_sp->GetModule().get() != NULL;
61254721Semaste}
62254721Semaste
63254721Semasteconst char *
64254721SemasteSBSection::GetName ()
65254721Semaste{
66254721Semaste    SectionSP section_sp (GetSP());
67254721Semaste    if (section_sp)
68254721Semaste        return section_sp->GetName().GetCString();
69254721Semaste    return NULL;
70254721Semaste}
71254721Semaste
72254721Semastelldb::SBSection
73254721SemasteSBSection::GetParent()
74254721Semaste{
75254721Semaste    lldb::SBSection sb_section;
76254721Semaste    SectionSP section_sp (GetSP());
77254721Semaste    if (section_sp)
78254721Semaste    {
79254721Semaste        SectionSP parent_section_sp (section_sp->GetParent());
80254721Semaste        if (parent_section_sp)
81254721Semaste            sb_section.SetSP(parent_section_sp);
82254721Semaste    }
83254721Semaste    return sb_section;
84254721Semaste}
85254721Semaste
86254721Semaste
87254721Semastelldb::SBSection
88254721SemasteSBSection::FindSubSection (const char *sect_name)
89254721Semaste{
90254721Semaste    lldb::SBSection sb_section;
91254721Semaste    if (sect_name)
92254721Semaste    {
93254721Semaste        SectionSP section_sp (GetSP());
94254721Semaste        if (section_sp)
95254721Semaste        {
96254721Semaste            ConstString const_sect_name(sect_name);
97254721Semaste            sb_section.SetSP(section_sp->GetChildren ().FindSectionByName(const_sect_name));
98254721Semaste        }
99254721Semaste    }
100254721Semaste    return sb_section;
101254721Semaste}
102254721Semaste
103254721Semastesize_t
104254721SemasteSBSection::GetNumSubSections ()
105254721Semaste{
106254721Semaste    SectionSP section_sp (GetSP());
107254721Semaste    if (section_sp)
108254721Semaste        return section_sp->GetChildren ().GetSize();
109254721Semaste    return 0;
110254721Semaste}
111254721Semaste
112254721Semastelldb::SBSection
113254721SemasteSBSection::GetSubSectionAtIndex (size_t idx)
114254721Semaste{
115254721Semaste    lldb::SBSection sb_section;
116254721Semaste    SectionSP section_sp (GetSP());
117254721Semaste    if (section_sp)
118254721Semaste        sb_section.SetSP (section_sp->GetChildren ().GetSectionAtIndex(idx));
119254721Semaste    return sb_section;
120254721Semaste}
121254721Semaste
122254721Semastelldb::SectionSP
123254721SemasteSBSection::GetSP() const
124254721Semaste{
125254721Semaste    return m_opaque_wp.lock();
126254721Semaste}
127254721Semaste
128254721Semastevoid
129254721SemasteSBSection::SetSP(const lldb::SectionSP &section_sp)
130254721Semaste{
131254721Semaste    m_opaque_wp = section_sp;
132254721Semaste}
133254721Semaste
134254721Semastelldb::addr_t
135254721SemasteSBSection::GetFileAddress ()
136254721Semaste{
137254721Semaste    lldb::addr_t file_addr = LLDB_INVALID_ADDRESS;
138254721Semaste    SectionSP section_sp (GetSP());
139254721Semaste    if (section_sp)
140254721Semaste        return section_sp->GetFileAddress();
141254721Semaste    return file_addr;
142254721Semaste}
143254721Semaste
144254721Semastelldb::addr_t
145254721SemasteSBSection::GetLoadAddress (lldb::SBTarget &sb_target)
146254721Semaste{
147254721Semaste    TargetSP target_sp(sb_target.GetSP());
148254721Semaste    if (target_sp)
149254721Semaste    {
150254721Semaste        SectionSP section_sp (GetSP());
151254721Semaste        if (section_sp)
152254721Semaste            return section_sp->GetLoadBaseAddress(target_sp.get());
153254721Semaste    }
154254721Semaste    return LLDB_INVALID_ADDRESS;
155254721Semaste
156254721Semaste}
157254721Semaste
158254721Semaste
159254721Semaste
160254721Semastelldb::addr_t
161254721SemasteSBSection::GetByteSize ()
162254721Semaste{
163254721Semaste    SectionSP section_sp (GetSP());
164254721Semaste    if (section_sp)
165254721Semaste        return section_sp->GetByteSize();
166254721Semaste    return 0;
167254721Semaste}
168254721Semaste
169254721Semasteuint64_t
170254721SemasteSBSection::GetFileOffset ()
171254721Semaste{
172254721Semaste    SectionSP section_sp (GetSP());
173254721Semaste    if (section_sp)
174254721Semaste    {
175254721Semaste        ModuleSP module_sp (section_sp->GetModule());
176254721Semaste        if (module_sp)
177254721Semaste        {
178254721Semaste            ObjectFile *objfile = module_sp->GetObjectFile();
179254721Semaste            if (objfile)
180254721Semaste                return objfile->GetFileOffset() + section_sp->GetFileOffset();
181254721Semaste        }
182254721Semaste    }
183254721Semaste    return UINT64_MAX;
184254721Semaste}
185254721Semaste
186254721Semasteuint64_t
187254721SemasteSBSection::GetFileByteSize ()
188254721Semaste{
189254721Semaste    SectionSP section_sp (GetSP());
190254721Semaste    if (section_sp)
191254721Semaste        return section_sp->GetFileSize();
192254721Semaste    return 0;
193254721Semaste}
194254721Semaste
195254721SemasteSBData
196254721SemasteSBSection::GetSectionData ()
197254721Semaste{
198254721Semaste    return GetSectionData (0, UINT64_MAX);
199254721Semaste}
200254721Semaste
201254721SemasteSBData
202254721SemasteSBSection::GetSectionData (uint64_t offset, uint64_t size)
203254721Semaste{
204254721Semaste    SBData sb_data;
205254721Semaste    SectionSP section_sp (GetSP());
206254721Semaste    if (section_sp)
207254721Semaste    {
208254721Semaste        const uint64_t sect_file_size = section_sp->GetFileSize();
209254721Semaste        if (sect_file_size > 0)
210254721Semaste        {
211254721Semaste            ModuleSP module_sp (section_sp->GetModule());
212254721Semaste            if (module_sp)
213254721Semaste            {
214254721Semaste                ObjectFile *objfile = module_sp->GetObjectFile();
215254721Semaste                if (objfile)
216254721Semaste                {
217254721Semaste                    const uint64_t sect_file_offset = objfile->GetFileOffset() + section_sp->GetFileOffset();
218254721Semaste                    const uint64_t file_offset = sect_file_offset + offset;
219254721Semaste                    uint64_t file_size = size;
220254721Semaste                    if (file_size == UINT64_MAX)
221254721Semaste                    {
222254721Semaste                        file_size = section_sp->GetByteSize();
223254721Semaste                        if (file_size > offset)
224254721Semaste                            file_size -= offset;
225254721Semaste                        else
226254721Semaste                            file_size = 0;
227254721Semaste                    }
228254721Semaste                    DataBufferSP data_buffer_sp (objfile->GetFileSpec().ReadFileContents (file_offset, file_size));
229254721Semaste                    if (data_buffer_sp && data_buffer_sp->GetByteSize() > 0)
230254721Semaste                    {
231254721Semaste                        DataExtractorSP data_extractor_sp (new DataExtractor (data_buffer_sp,
232254721Semaste                                                                              objfile->GetByteOrder(),
233254721Semaste                                                                              objfile->GetAddressByteSize()));
234254721Semaste
235254721Semaste                        sb_data.SetOpaque (data_extractor_sp);
236254721Semaste                    }
237254721Semaste                }
238254721Semaste            }
239254721Semaste        }
240254721Semaste    }
241254721Semaste    return sb_data;
242254721Semaste}
243254721Semaste
244254721SemasteSectionType
245254721SemasteSBSection::GetSectionType ()
246254721Semaste{
247254721Semaste    SectionSP section_sp (GetSP());
248254721Semaste    if (section_sp.get())
249254721Semaste        return section_sp->GetType();
250254721Semaste    return eSectionTypeInvalid;
251254721Semaste}
252254721Semaste
253254721Semaste
254254721Semastebool
255254721SemasteSBSection::operator == (const SBSection &rhs)
256254721Semaste{
257254721Semaste    SectionSP lhs_section_sp (GetSP());
258254721Semaste    SectionSP rhs_section_sp (rhs.GetSP());
259254721Semaste    if (lhs_section_sp && rhs_section_sp)
260254721Semaste        return lhs_section_sp == rhs_section_sp;
261254721Semaste    return false;
262254721Semaste}
263254721Semaste
264254721Semastebool
265254721SemasteSBSection::operator != (const SBSection &rhs)
266254721Semaste{
267254721Semaste    SectionSP lhs_section_sp (GetSP());
268254721Semaste    SectionSP rhs_section_sp (rhs.GetSP());
269254721Semaste    return lhs_section_sp != rhs_section_sp;
270254721Semaste}
271254721Semaste
272254721Semastebool
273254721SemasteSBSection::GetDescription (SBStream &description)
274254721Semaste{
275254721Semaste    Stream &strm = description.ref();
276254721Semaste
277254721Semaste    SectionSP section_sp (GetSP());
278254721Semaste    if (section_sp)
279254721Semaste    {
280254721Semaste        const addr_t file_addr = section_sp->GetFileAddress();
281254721Semaste        strm.Printf ("[0x%16.16" PRIx64 "-0x%16.16" PRIx64 ") ", file_addr, file_addr + section_sp->GetByteSize());
282254721Semaste        section_sp->DumpName(&strm);
283254721Semaste    }
284254721Semaste    else
285254721Semaste    {
286254721Semaste        strm.PutCString ("No value");
287254721Semaste    }
288254721Semaste
289254721Semaste    return true;
290254721Semaste}
291254721Semaste
292