summaryrefslogtreecommitdiffstats
path: root/lldb/source/API/SBSection.cpp
blob: dedcc7a28500ad6bb317c02bec4f3dade095b002 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
//===-- SBSection.cpp -------------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "lldb/API/SBSection.h"
#include "lldb/API/SBStream.h"
#include "lldb/Core/DataBuffer.h"
#include "lldb/Core/DataExtractor.h"
#include "lldb/Core/Log.h"
#include "lldb/Core/Module.h"
#include "lldb/Core/Section.h"
#include "lldb/Core/StreamString.h"

namespace lldb_private 
{
    // We need a section implementation to hold onto a reference to the module
    // since if the module goes away and we have anyone still holding onto a 
    // SBSection object, we could crash.
    class SectionImpl
    {
    public:
        SectionImpl (const lldb_private::Section *section = NULL) :
            m_module_sp (),
            m_section (section)
        {
            if (section)
                m_module_sp = section->GetModule()->shared_from_this();
        }
        
        SectionImpl (const SectionImpl &rhs) :
            m_module_sp (rhs.m_module_sp),
            m_section   (rhs.m_section)
        {
        }

        bool 
        IsValid () const
        {
            return m_section != NULL;
        }

        void
        operator = (const SectionImpl &rhs)
        {
            m_module_sp = rhs.m_module_sp;
            m_section = rhs.m_section;
        }

        void
        operator =(const lldb_private::Section *section)
        {
            m_section = section;
            if (section)
                m_module_sp.reset(section->GetModule());
            else
                m_module_sp.reset();
        }

        const lldb_private::Section *
        GetSection () const
        {
            return m_section;
        }

        Module *
        GetModule()
        {
            return m_module_sp.get();
        }

        const lldb::ModuleSP &
        GetModuleSP() const
        {
            return m_module_sp;
        }
    protected:
        lldb::ModuleSP m_module_sp;
        const lldb_private::Section *m_section;
    };
}

using namespace lldb;
using namespace lldb_private;


SBSection::SBSection () :
    m_opaque_ap ()
{
}

SBSection::SBSection (const SBSection &rhs) :
    m_opaque_ap ()
{
    if (rhs.IsValid())
        m_opaque_ap.reset (new SectionImpl (*rhs.m_opaque_ap));
}



SBSection::SBSection (const lldb_private::Section *section) :
    m_opaque_ap ()
{
    if (section)
        m_opaque_ap.reset (new SectionImpl(section));
}

const SBSection &
SBSection::operator = (const SBSection &rhs)
{
    if (this != &rhs && rhs.IsValid())
        m_opaque_ap.reset (new SectionImpl(*rhs.m_opaque_ap));
    else
        m_opaque_ap.reset ();
    return *this;
}

SBSection::~SBSection ()
{
}

bool
SBSection::IsValid () const
{
    return m_opaque_ap.get() != NULL && m_opaque_ap->IsValid();
}

const char *
SBSection::GetName ()
{
    if (IsValid())
        return m_opaque_ap->GetSection()->GetName().GetCString();
    return NULL;
}


lldb::SBSection
SBSection::FindSubSection (const char *sect_name)
{
    lldb::SBSection sb_section;
    if (sect_name && IsValid())
    {
        ConstString const_sect_name(sect_name);
        sb_section.SetSection(m_opaque_ap->GetSection()->GetChildren ().FindSectionByName(const_sect_name).get());
    }
    return sb_section;
}

size_t
SBSection::GetNumSubSections ()
{
    if (IsValid())
        return m_opaque_ap->GetSection()->GetChildren ().GetSize();
    return 0;
}

lldb::SBSection
SBSection::GetSubSectionAtIndex (size_t idx)
{
    lldb::SBSection sb_section;
    if (IsValid())
        sb_section.SetSection(m_opaque_ap->GetSection()->GetChildren ().GetSectionAtIndex(idx).get());
    return sb_section;
}

const lldb_private::Section *
SBSection::GetSection()
{
    if (m_opaque_ap.get())
        return m_opaque_ap->GetSection();
    return NULL;
}

void
SBSection::SetSection (const lldb_private::Section *section)
{
    m_opaque_ap.reset (new SectionImpl(section));
}




lldb::addr_t
SBSection::GetFileAddress ()
{
    lldb::addr_t file_addr = LLDB_INVALID_ADDRESS;
    if (IsValid())
        return m_opaque_ap->GetSection()->GetFileAddress();
    return file_addr;
}

lldb::addr_t
SBSection::GetByteSize ()
{
    if (IsValid())
    {
        const Section *section = m_opaque_ap->GetSection();
        if (section)
            return section->GetByteSize();
    }
    return 0;
}

uint64_t
SBSection::GetFileOffset ()
{
    if (IsValid())
    {
        const Section *section = m_opaque_ap->GetSection();
        if (section)
        {
            Module *module = m_opaque_ap->GetModule();
            if (module)
            {
                ObjectFile *objfile = module->GetObjectFile();
                if (objfile)
                    return objfile->GetOffset() + section->GetFileOffset();
            }
            return section->GetFileOffset();
        }
    }
    return 0;
}

uint64_t
SBSection::GetFileByteSize ()
{
    if (IsValid())
    {
        const Section *section = m_opaque_ap->GetSection();
        if (section)
            return section->GetFileSize();
    }
    return 0;
}

SBData
SBSection::GetSectionData ()
{
    return GetSectionData (0, UINT64_MAX);
}

SBData
SBSection::GetSectionData (uint64_t offset, uint64_t size)
{
    SBData sb_data;
    if (IsValid())
    {
        const Section *section = m_opaque_ap->GetSection();
        if (section)
        {
            const uint64_t sect_file_size = section->GetFileSize();
            if (sect_file_size > 0)
            {
                Module *module = m_opaque_ap->GetModule();
                if (module)
                {
                    ObjectFile *objfile = module->GetObjectFile();
                    if (objfile)
                    {
                        const uint64_t sect_file_offset = objfile->GetOffset() + section->GetFileOffset();
                        const uint64_t file_offset = sect_file_offset + offset;
                        uint64_t file_size = size;
                        if (file_size == UINT64_MAX)
                        {
                            file_size = section->GetByteSize();
                            if (file_size > offset)
                                file_size -= offset;
                            else
                                file_size = 0;
                        }
                        DataBufferSP data_buffer_sp (objfile->GetFileSpec().ReadFileContents (file_offset, file_size));
                        if (data_buffer_sp && data_buffer_sp->GetByteSize() > 0)
                        {
                            DataExtractorSP data_extractor_sp (new DataExtractor (data_buffer_sp, 
                                                                                  objfile->GetByteOrder(), 
                                                                                  objfile->GetAddressByteSize()));
                            
                            sb_data.SetOpaque (data_extractor_sp);
                        }
                    }
                }
            }
        }
    }
    return sb_data;
}

SectionType
SBSection::GetSectionType ()
{
    if (m_opaque_ap.get())
    {
        const Section *section = m_opaque_ap->GetSection();
        if (section)
            return section->GetType();
    }
    return eSectionTypeInvalid;
}


bool
SBSection::operator == (const SBSection &rhs)
{
    SectionImpl *lhs_ptr = m_opaque_ap.get();
    SectionImpl *rhs_ptr = rhs.m_opaque_ap.get();
    if (lhs_ptr && rhs_ptr)
        return lhs_ptr->GetSection() == rhs_ptr->GetSection();
    return false;
}

bool
SBSection::operator != (const SBSection &rhs)
{
    SectionImpl *lhs_ptr = m_opaque_ap.get();
    SectionImpl *rhs_ptr = rhs.m_opaque_ap.get();
    if (lhs_ptr && rhs_ptr)
        return lhs_ptr->GetSection() != rhs_ptr->GetSection();
    return false;
}

bool
SBSection::GetDescription (SBStream &description)
{
    Stream &strm = description.ref();

    if (IsValid())
    {
        const Section *section = m_opaque_ap->GetSection();
        const addr_t file_addr = section->GetFileAddress();
        strm.Printf ("[0x%16.16llx-0x%16.16llx) ", file_addr, file_addr + section->GetByteSize());
        section->DumpName(&strm);
    }
    else
    {
        strm.PutCString ("No value");
    }

    return true;
}

OpenPOWER on IntegriCloud