1278332Semaste//===-- CompactUnwindInfo.h -------------------------------------*- C++ -*-===//
2278332Semaste//
3278332Semaste//                     The LLVM Compiler Infrastructure
4278332Semaste//
5278332Semaste// This file is distributed under the University of Illinois Open Source
6278332Semaste// License. See LICENSE.TXT for details.
7278332Semaste//
8278332Semaste//===----------------------------------------------------------------------===//
9278332Semaste
10278332Semaste#ifndef liblldb_CompactUnwindInfo_h_
11278332Semaste#define liblldb_CompactUnwindInfo_h_
12278332Semaste
13278332Semaste#include <vector>
14278332Semaste
15278332Semaste#include "lldb/Core/DataExtractor.h"
16278332Semaste#include "lldb/Core/RangeMap.h"
17278332Semaste#include "lldb/Host/Mutex.h"
18278332Semaste#include "lldb/Symbol/ObjectFile.h"
19278332Semaste#include "lldb/Symbol/UnwindPlan.h"
20278332Semaste#include "lldb/lldb-private.h"
21278332Semaste
22278332Semastenamespace lldb_private {
23278332Semaste
24278332Semaste// Compact Unwind info is an unwind format used on Darwin.  The unwind instructions
25278332Semaste// for typical compiler-generated functions can be expressed in a 32-bit encoding.
26278332Semaste// The format includes a two-level index so the unwind information for a function
27278332Semaste// can be found by two binary searches in the section.  It can represent both
28278332Semaste// stack frames that use a frame-pointer register and frameless functions, on
29278332Semaste// i386/x86_64 for instance.  When a function is too complex to be represented in
30278332Semaste// the compact unwind format, it calls out to eh_frame unwind instructions.
31278332Semaste
32278332Semaste// On Mac OS X / iOS, a function will have either a compact unwind representation
33278332Semaste// or an eh_frame representation.  If lldb is going to benefit  from the compiler's
34278332Semaste// description about saved register locations, it must be able to read both
35278332Semaste// sources of information.
36278332Semaste
37278332Semasteclass CompactUnwindInfo
38278332Semaste{
39278332Semastepublic:
40278332Semaste
41278332Semaste    CompactUnwindInfo (ObjectFile& objfile,
42278332Semaste                       lldb::SectionSP& section);
43278332Semaste
44278332Semaste    ~CompactUnwindInfo();
45278332Semaste
46278332Semaste    bool
47278332Semaste    GetUnwindPlan (Target &target, Address addr, UnwindPlan& unwind_plan);
48278332Semaste
49278332Semaste    bool
50278332Semaste    IsValid (const lldb::ProcessSP &process_sp);
51278332Semaste
52278332Semasteprivate:
53278332Semaste
54278332Semaste
55278332Semaste    // The top level index entries of the compact unwind info
56278332Semaste    //   (internal representation of struct unwind_info_section_header_index_entry)
57278332Semaste    // There are relatively few of these (one per 500/1000 functions, depending on format) so
58278332Semaste    // creating them on first scan will not be too costly.
59278332Semaste    struct UnwindIndex
60278332Semaste    {
61278332Semaste        uint32_t        function_offset; // The offset of the first function covered by this index
62278332Semaste        uint32_t        second_level;    // The offset (inside unwind_info sect) to the second level page for this index
63278332Semaste        // (either UNWIND_SECOND_LEVEL_REGULAR or UNWIND_SECOND_LEVEL_COMPRESSED)
64278332Semaste        uint32_t        lsda_array_start;// The offset (inside unwind_info sect) LSDA array for this index
65278332Semaste        uint32_t        lsda_array_end;  // The offset to the LSDA array for the NEXT index
66278332Semaste        bool            sentinal_entry;  // There is an empty index at the end which provides the upper bound of
67278332Semaste        // function addresses that are described
68278332Semaste
69278332Semaste        UnwindIndex() :
70278332Semaste        function_offset (0),
71278332Semaste        second_level (0),
72278332Semaste        lsda_array_start(0),
73278332Semaste        lsda_array_end(0),
74278332Semaste        sentinal_entry (false)
75278332Semaste        { }
76278332Semaste
77278332Semaste        bool
78278332Semaste        operator< (const CompactUnwindInfo::UnwindIndex& rhs) const
79278332Semaste        {
80278332Semaste            return function_offset < rhs.function_offset;
81278332Semaste        }
82278332Semaste
83278332Semaste        bool
84278332Semaste        operator== (const CompactUnwindInfo::UnwindIndex& rhs) const
85278332Semaste        {
86278332Semaste            return function_offset == rhs.function_offset;
87278332Semaste        }
88278332Semaste
89278332Semaste    };
90278332Semaste
91278332Semaste    // An internal object used to store the information we retrieve about a function --
92278332Semaste    // the encoding bits and possibly the LSDA/personality function.
93278332Semaste    struct FunctionInfo
94278332Semaste    {
95278332Semaste        uint32_t  encoding;                   // compact encoding 32-bit value for this function
96278332Semaste        Address   lsda_address;               // the address of the LSDA data for this function
97278332Semaste        Address   personality_ptr_address;    // the address where the personality routine addr can be found
98278332Semaste
99278332Semaste        uint32_t  valid_range_offset_start;   // first offset that this encoding is valid for (start of the function)
100278332Semaste        uint32_t  valid_range_offset_end;     // the offset of the start of the next function
101278332Semaste        FunctionInfo () : encoding(0), lsda_address(), personality_ptr_address(), valid_range_offset_start(0), valid_range_offset_end(0) { }
102278332Semaste    };
103278332Semaste
104278332Semaste    struct UnwindHeader
105278332Semaste    {
106278332Semaste        uint32_t    version;
107278332Semaste        uint32_t    common_encodings_array_offset;
108278332Semaste        uint32_t    common_encodings_array_count;
109278332Semaste        uint32_t    personality_array_offset;
110278332Semaste        uint32_t    personality_array_count;
111278332Semaste
112278332Semaste        UnwindHeader () : common_encodings_array_offset (0), common_encodings_array_count (0), personality_array_offset (0), personality_array_count (0) { }
113278332Semaste    };
114278332Semaste
115278332Semaste    void
116278332Semaste    ScanIndex(const lldb::ProcessSP &process_sp);
117278332Semaste
118278332Semaste    bool
119278332Semaste    GetCompactUnwindInfoForFunction (Target &target, Address address, FunctionInfo &unwind_info);
120278332Semaste
121278332Semaste    lldb::offset_t
122278332Semaste    BinarySearchRegularSecondPage (uint32_t entry_page_offset, uint32_t entry_count, uint32_t function_offset, uint32_t *entry_func_start_offset, uint32_t *entry_func_end_offset);
123278332Semaste
124278332Semaste    uint32_t
125278332Semaste    BinarySearchCompressedSecondPage (uint32_t entry_page_offset, uint32_t entry_count, uint32_t function_offset_to_find, uint32_t function_offset_base, uint32_t *entry_func_start_offset, uint32_t *entry_func_end_offset);
126278332Semaste
127278332Semaste    uint32_t
128278332Semaste    GetLSDAForFunctionOffset (uint32_t lsda_offset, uint32_t lsda_count, uint32_t function_offset);
129278332Semaste
130278332Semaste    bool
131278332Semaste    CreateUnwindPlan_x86_64 (Target &target, FunctionInfo &function_info, UnwindPlan &unwind_plan, Address pc_or_function_start);
132278332Semaste
133278332Semaste    bool
134278332Semaste    CreateUnwindPlan_i386 (Target &target, FunctionInfo &function_info, UnwindPlan &unwind_plan, Address pc_or_function_start);
135278332Semaste
136278332Semaste    ObjectFile                  &m_objfile;
137278332Semaste    lldb::SectionSP             m_section_sp;
138278332Semaste    lldb::DataBufferSP          m_section_contents_if_encrypted; // if the binary is encrypted, read the sect contents
139278332Semaste                                                                 // out of live memory and cache them here
140278332Semaste    Mutex                       m_mutex;
141278332Semaste    std::vector<UnwindIndex>    m_indexes;
142278332Semaste
143278332Semaste    LazyBool                    m_indexes_computed;         // eLazyBoolYes once we've tried to parse the unwind info
144278332Semaste                                                            // eLazyBoolNo means we cannot parse the unwind info & should not retry
145278332Semaste                                                            // eLazyBoolCalculate means we haven't tried to parse it yet
146278332Semaste
147278332Semaste    DataExtractor               m_unwindinfo_data;
148278332Semaste    bool                        m_unwindinfo_data_computed; // true once we've mapped in the unwindinfo data
149278332Semaste
150278332Semaste    UnwindHeader                m_unwind_header;
151278332Semaste};
152278332Semaste
153278332Semaste} // namespace lldb_private
154278332Semaste
155278332Semaste#endif  // liblldb_CompactUnwindInfo_h_
156