Section.h revision b5a8f1498e1ddaeed5187a878d57ea0b74af9c26
1//===-- Section.h -----------------------------------------------*- 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#ifndef liblldb_Section_h_
11#define liblldb_Section_h_
12
13#include "lldb/lldb-private.h"
14#include "lldb/Core/AddressRange.h"
15#include "lldb/Core/Flags.h"
16#include "lldb/Core/ModuleChild.h"
17#include "lldb/Core/ConstString.h"
18#include "lldb/Core/UserID.h"
19#include "lldb/Core/VMRange.h"
20#include <limits.h>
21
22namespace lldb_private {
23
24class SectionList
25{
26public:
27    typedef std::vector<lldb::SectionSP>  collection;
28    typedef collection::iterator        iterator;
29    typedef collection::const_iterator  const_iterator;
30
31    SectionList();
32
33    virtual
34    ~SectionList();
35
36    uint32_t
37    AddSection (lldb::SectionSP& sect_sp);
38
39    uint32_t
40    AddUniqueSection (lldb::SectionSP& sect_sp);
41
42    uint32_t
43    FindSectionIndex (const Section* sect);
44
45    bool
46    ContainsSection(lldb::user_id_t sect_id) const;
47
48    void
49    Dump (Stream *s, Target *target, bool show_header, uint32_t depth) const;
50
51    lldb::SectionSP
52    FindSectionByName (const ConstString &section_dstr) const;
53
54    lldb::SectionSP
55    FindSectionByID (lldb::user_id_t sect_id) const;
56
57    lldb::SectionSP
58    FindSectionByType (lldb::SectionType sect_type, bool check_children, uint32_t start_idx = 0) const;
59
60    lldb::SectionSP
61    GetSharedPointer (const Section *section, bool check_children) const;
62
63    lldb::SectionSP
64    FindSectionContainingFileAddress (lldb::addr_t addr, uint32_t depth = UINT32_MAX) const;
65
66    lldb::SectionSP
67    FindSectionContainingLinkedFileAddress (lldb::addr_t vm_addr, uint32_t depth) const;
68
69    bool
70    GetSectionData (const DataExtractor& module_data, DataExtractor& section_data) const;
71
72    // Get the number of sections in this list only
73    size_t
74    GetSize () const
75    {
76        return m_sections.size();
77    }
78
79    // Get the number of sections in this list, and any contained child sections
80    size_t
81    GetNumSections (uint32_t depth) const;
82
83    bool
84    ReplaceSection (lldb::user_id_t sect_id, lldb::SectionSP& sect_sp, uint32_t depth = UINT32_MAX);
85
86    lldb::SectionSP
87    GetSectionAtIndex (uint32_t idx) const;
88
89    size_t
90    Slide (lldb::addr_t slide_amount, bool slide_children);
91
92protected:
93    collection  m_sections;
94};
95
96
97class Section :
98    public ModuleChild,
99    public UserID,
100    public Flags
101{
102public:
103    Section (
104        Section *parent,    // NULL for top level sections, non-NULL for child sections
105        Module* module,
106        lldb::user_id_t sect_id,
107        const ConstString &name,
108        lldb::SectionType sect_type,
109        lldb::addr_t file_vm_addr,
110        lldb::addr_t vm_size,
111        uint64_t file_offset,
112        uint64_t file_size,
113        uint32_t flags);
114
115    ~Section ();
116
117    static int
118    Compare (const Section& a, const Section& b);
119
120    // Get a valid shared pointer to this section object
121    lldb::SectionSP
122    GetSharedPointer() const;
123
124    bool
125    ContainsFileAddress (lldb::addr_t vm_addr) const;
126
127    SectionList&
128    GetChildren ()
129    {
130        return m_children;
131    }
132
133    const SectionList&
134    GetChildren () const
135    {
136        return m_children;
137    }
138
139    void
140    Dump (Stream *s, Target *target, uint32_t depth) const;
141
142    void
143    DumpName (Stream *s) const;
144
145    lldb::addr_t
146    GetLoadBaseAddress (Target *target) const;
147
148    bool
149    ResolveContainedAddress (lldb::addr_t offset, Address &so_addr) const;
150
151    uint64_t
152    GetFileOffset () const
153    {
154        return m_file_offset;
155    }
156
157    void
158    SetFileOffset (uint64_t file_offset)
159    {
160        m_file_offset = file_offset;
161    }
162
163    uint64_t
164    GetFileSize () const
165    {
166        return m_file_size;
167    }
168
169    void
170    SetFileSize (uint64_t file_size)
171    {
172        m_file_size = file_size;
173    }
174
175    lldb::addr_t
176    GetFileAddress () const;
177
178    lldb::addr_t
179    GetOffset () const
180    {
181        // This section has a parent which means m_file_addr is an offset.
182        if (m_parent)
183            return m_file_addr;
184
185        // This section has no parent, so there is no offset to be had
186        return 0;
187    }
188
189
190    lldb::addr_t
191    GetByteSize () const
192    {
193        return m_byte_size;
194    }
195
196    void
197    SetByteSize (lldb::addr_t byte_size)
198    {
199        m_byte_size = byte_size;
200    }
201
202    bool
203    IsFake() const
204    {
205        return m_fake;
206    }
207
208    void
209    SetIsFake(bool fake)
210    {
211        m_fake = fake;
212    }
213
214    bool
215    IsEncrypted () const
216    {
217        return m_encrypted;
218    }
219
220    void
221    SetIsEncrypted (bool b)
222    {
223        m_encrypted = b;
224    }
225
226    bool
227    IsDescendant (const Section *section);
228
229    ConstString&
230    GetName ();
231
232    const ConstString&
233    GetName () const;
234
235    bool
236    Slide (lldb::addr_t slide_amount, bool slide_children);
237
238    void
239    SetLinkedLocation (const Section *linked_section, uint64_t linked_offset);
240
241    bool
242    ContainsLinkedFileAddress (lldb::addr_t vm_addr) const;
243
244    const Section *
245    GetLinkedSection () const
246    {
247        return m_linked_section;
248    }
249
250    uint64_t
251    GetLinkedOffset () const
252    {
253        return m_linked_offset;
254    }
255
256    lldb::addr_t
257    GetLinkedFileAddress () const;
258
259    lldb::SectionType
260    GetType () const
261    {
262        return m_type;
263    }
264
265protected:
266
267    Section *       m_parent;           // Parent section or NULL if no parent.
268    ConstString     m_name;             // Name of this section
269    lldb::SectionType m_type;           // The type of this section
270    lldb::addr_t    m_file_addr;        // The absolute file virtual address range of this section if m_parent == NULL,
271                                        // offset from parent file virtual address if m_parent != NULL
272    lldb::addr_t    m_byte_size;        // Size in bytes that this section will occupy in memory at runtime
273    uint64_t        m_file_offset;      // Object file offset (if any)
274    uint64_t        m_file_size;        // Object file size (can be smaller than m_byte_size for zero filled sections...)
275    SectionList     m_children;         // Child sections
276    bool            m_fake:1,           // If true, then this section only can contain the address if one of its
277                                        // children contains an address. This allows for gaps between the children
278                                        // that are contained in the address range for this section, but do not produce
279                                        // hits unless the children contain the address.
280                    m_encrypted:1;      // Set to true if the contents are encrypted
281    const Section * m_linked_section;
282    uint64_t        m_linked_offset;
283private:
284    DISALLOW_COPY_AND_ASSIGN (Section);
285};
286
287
288} // namespace lldb_private
289
290#endif  // liblldb_Section_h_
291