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