xref: /NextBSD/contrib/llvm/tools/lldb/include/lldb/Core/Section.h (revision 287e3b14e9552995def1802ec9c5034f4adf28ec)
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 log2align,
123              uint32_t flags,
124              uint32_t target_byte_size = 1);
125 
126     // Create a section that is a child of parent_section_sp
127     Section (const lldb::SectionSP &parent_section_sp,    // NULL for top level sections, non-NULL for child sections
128              const lldb::ModuleSP &module_sp,
129              ObjectFile *obj_file,
130              lldb::user_id_t sect_id,
131              const ConstString &name,
132              lldb::SectionType sect_type,
133              lldb::addr_t file_vm_addr,
134              lldb::addr_t vm_size,
135              lldb::offset_t file_offset,
136              lldb::offset_t file_size,
137              uint32_t log2align,
138              uint32_t flags,
139              uint32_t target_byte_size = 1);
140 
141     ~Section ();
142 
143     static int
144     Compare (const Section& a, const Section& b);
145 
146     bool
147     ContainsFileAddress (lldb::addr_t vm_addr) const;
148 
149     SectionList&
GetChildren()150     GetChildren ()
151     {
152         return m_children;
153     }
154 
155     const SectionList&
GetChildren()156     GetChildren () const
157     {
158         return m_children;
159     }
160 
161     void
162     Dump (Stream *s, Target *target, uint32_t depth) const;
163 
164     void
165     DumpName (Stream *s) const;
166 
167     lldb::addr_t
168     GetLoadBaseAddress (Target *target) const;
169 
170     bool
171     ResolveContainedAddress (lldb::addr_t offset, Address &so_addr) const;
172 
173     lldb::offset_t
GetFileOffset()174     GetFileOffset () const
175     {
176         return m_file_offset;
177     }
178 
179     void
SetFileOffset(lldb::offset_t file_offset)180     SetFileOffset (lldb::offset_t file_offset)
181     {
182         m_file_offset = file_offset;
183     }
184 
185     lldb::offset_t
GetFileSize()186     GetFileSize () const
187     {
188         return m_file_size;
189     }
190 
191     void
SetFileSize(lldb::offset_t file_size)192     SetFileSize (lldb::offset_t file_size)
193     {
194         m_file_size = file_size;
195     }
196 
197     lldb::addr_t
198     GetFileAddress () const;
199 
200     bool
201     SetFileAddress (lldb::addr_t file_addr);
202 
203     lldb::addr_t
204     GetOffset () const;
205 
206 
207     lldb::addr_t
GetByteSize()208     GetByteSize () const
209     {
210         return m_byte_size;
211     }
212 
213     void
SetByteSize(lldb::addr_t byte_size)214     SetByteSize (lldb::addr_t byte_size)
215     {
216         m_byte_size = byte_size;
217     }
218 
219     bool
IsFake()220     IsFake() const
221     {
222         return m_fake;
223     }
224 
225     void
SetIsFake(bool fake)226     SetIsFake(bool fake)
227     {
228         m_fake = fake;
229     }
230 
231     bool
IsEncrypted()232     IsEncrypted () const
233     {
234         return m_encrypted;
235     }
236 
237     void
SetIsEncrypted(bool b)238     SetIsEncrypted (bool b)
239     {
240         m_encrypted = b;
241     }
242 
243     bool
244     IsDescendant (const Section *section);
245 
246     const ConstString&
GetName()247     GetName () const
248     {
249         return m_name;
250     }
251 
252     bool
253     Slide (lldb::addr_t slide_amount, bool slide_children);
254 
255 
256     lldb::SectionType
GetType()257     GetType () const
258     {
259         return m_type;
260     }
261 
262     lldb::SectionSP
GetParent()263     GetParent () const
264     {
265         return m_parent_wp.lock();
266     }
267 
268     bool
IsThreadSpecific()269     IsThreadSpecific () const
270     {
271         return m_thread_specific;
272     }
273 
274     void
SetIsThreadSpecific(bool b)275     SetIsThreadSpecific (bool b)
276     {
277         m_thread_specific = b;
278     }
279 
280     ObjectFile *
GetObjectFile()281     GetObjectFile ()
282     {
283         return m_obj_file;
284     }
285     const ObjectFile *
GetObjectFile()286     GetObjectFile () const
287     {
288         return m_obj_file;
289     }
290 
GetLog2Align()291     uint32_t GetLog2Align()
292     {
293         return m_log2align;
294     }
295 
296     void
SetLog2Align(uint32_t align)297     SetLog2Align(uint32_t align)
298     {
299         m_log2align = align;
300     }
301 
302     // Get the number of host bytes required to hold a target byte
303     uint32_t
GetTargetByteSize()304     GetTargetByteSize() const
305     {
306         return m_target_byte_size;
307     }
308 
309 protected:
310 
311     ObjectFile      *m_obj_file;        // The object file that data for this section should be read from
312     lldb::SectionType m_type;           // The type of this section
313     lldb::SectionWP m_parent_wp;        // Weak pointer to parent section
314     ConstString     m_name;             // Name of this section
315     lldb::addr_t    m_file_addr;        // The absolute file virtual address range of this section if m_parent == NULL,
316                                         // offset from parent file virtual address if m_parent != NULL
317     lldb::addr_t    m_byte_size;        // Size in bytes that this section will occupy in memory at runtime
318     lldb::offset_t  m_file_offset;      // Object file offset (if any)
319     lldb::offset_t  m_file_size;        // Object file size (can be smaller than m_byte_size for zero filled sections...)
320     uint32_t        m_log2align;        // log_2(align) of the section (i.e. section has to be aligned to 2^m_log2align)
321     SectionList     m_children;         // Child sections
322     bool            m_fake:1,           // If true, then this section only can contain the address if one of its
323                                         // children contains an address. This allows for gaps between the children
324                                         // that are contained in the address range for this section, but do not produce
325                                         // hits unless the children contain the address.
326                     m_encrypted:1,      // Set to true if the contents are encrypted
327                     m_thread_specific:1;// This section is thread specific
328     uint32_t        m_target_byte_size; // Some architectures have non-8-bit byte size. This is specified as
329                                         // as a multiple number of a host bytes
330 private:
331     DISALLOW_COPY_AND_ASSIGN (Section);
332 };
333 
334 
335 } // namespace lldb_private
336 
337 #endif  // liblldb_Section_h_
338