1 // Copyright (c) 2010, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 //     * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //     * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 //     * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 
30 // Original author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com>
31 
32 // cfi_assembler.cc: Implementation of google_breakpad::CFISection class.
33 // See cfi_assembler.h for details.
34 
35 #include "common/dwarf/cfi_assembler.h"
36 
37 #include <assert.h>
38 #include <stdlib.h>
39 
40 namespace google_breakpad {
41 
42 using dwarf2reader::DwarfPointerEncoding;
43 
CIEHeader(uint64_t code_alignment_factor,int data_alignment_factor,unsigned return_address_register,uint8_t version,const string & augmentation,bool dwarf64,uint8_t address_size,uint8_t segment_size)44 CFISection &CFISection::CIEHeader(uint64_t code_alignment_factor,
45                                   int data_alignment_factor,
46                                   unsigned return_address_register,
47                                   uint8_t version,
48                                   const string &augmentation,
49                                   bool dwarf64,
50                                   uint8_t address_size,
51                                   uint8_t segment_size) {
52   assert(!entry_length_);
53   entry_length_ = new PendingLength();
54   in_fde_ = false;
55 
56   if (dwarf64) {
57     D32(kDwarf64InitialLengthMarker);
58     D64(entry_length_->length);
59     entry_length_->start = Here();
60     D64(eh_frame_ ? kEHFrame64CIEIdentifier : kDwarf64CIEIdentifier);
61   } else {
62     D32(entry_length_->length);
63     entry_length_->start = Here();
64     D32(eh_frame_ ? kEHFrame32CIEIdentifier : kDwarf32CIEIdentifier);
65   }
66   D8(version);
67   AppendCString(augmentation);
68   if (version >= 4) {
69     D8(address_size);
70     D8(segment_size);
71   }
72   ULEB128(code_alignment_factor);
73   LEB128(data_alignment_factor);
74   if (version == 1)
75     D8(return_address_register);
76   else
77     ULEB128(return_address_register);
78   return *this;
79 }
80 
FDEHeader(Label cie_pointer,uint64_t initial_location,uint64_t address_range,bool dwarf64)81 CFISection &CFISection::FDEHeader(Label cie_pointer,
82                                   uint64_t initial_location,
83                                   uint64_t address_range,
84                                   bool dwarf64) {
85   assert(!entry_length_);
86   entry_length_ = new PendingLength();
87   in_fde_ = true;
88   fde_start_address_ = initial_location;
89 
90   if (dwarf64) {
91     D32(0xffffffff);
92     D64(entry_length_->length);
93     entry_length_->start = Here();
94     if (eh_frame_)
95       D64(Here() - cie_pointer);
96     else
97       D64(cie_pointer);
98   } else {
99     D32(entry_length_->length);
100     entry_length_->start = Here();
101     if (eh_frame_)
102       D32(Here() - cie_pointer);
103     else
104       D32(cie_pointer);
105   }
106   EncodedPointer(initial_location);
107   // The FDE length in an .eh_frame section uses the same encoding as the
108   // initial location, but ignores the base address (selected by the upper
109   // nybble of the encoding), as it's a length, not an address that can be
110   // made relative.
111   EncodedPointer(address_range,
112                  DwarfPointerEncoding(pointer_encoding_ & 0x0f));
113   return *this;
114 }
115 
FinishEntry()116 CFISection &CFISection::FinishEntry() {
117   assert(entry_length_);
118   Align(address_size_, dwarf2reader::DW_CFA_nop);
119   entry_length_->length = Here() - entry_length_->start;
120   delete entry_length_;
121   entry_length_ = NULL;
122   in_fde_ = false;
123   return *this;
124 }
125 
EncodedPointer(uint64_t address,DwarfPointerEncoding encoding,const EncodedPointerBases & bases)126 CFISection &CFISection::EncodedPointer(uint64_t address,
127                                        DwarfPointerEncoding encoding,
128                                        const EncodedPointerBases &bases) {
129   // Omitted data is extremely easy to emit.
130   if (encoding == dwarf2reader::DW_EH_PE_omit)
131     return *this;
132 
133   // If (encoding & dwarf2reader::DW_EH_PE_indirect) != 0, then we assume
134   // that ADDRESS is the address at which the pointer is stored --- in
135   // other words, that bit has no effect on how we write the pointer.
136   encoding = DwarfPointerEncoding(encoding & ~dwarf2reader::DW_EH_PE_indirect);
137 
138   // Find the base address to which this pointer is relative. The upper
139   // nybble of the encoding specifies this.
140   uint64_t base;
141   switch (encoding & 0xf0) {
142     case dwarf2reader::DW_EH_PE_absptr:  base = 0;                  break;
143     case dwarf2reader::DW_EH_PE_pcrel:   base = bases.cfi + Size(); break;
144     case dwarf2reader::DW_EH_PE_textrel: base = bases.text;         break;
145     case dwarf2reader::DW_EH_PE_datarel: base = bases.data;         break;
146     case dwarf2reader::DW_EH_PE_funcrel: base = fde_start_address_; break;
147     case dwarf2reader::DW_EH_PE_aligned: base = 0;                  break;
148     default: abort();
149   };
150 
151   // Make ADDRESS relative. Yes, this is appropriate even for "absptr"
152   // values; see gcc/unwind-pe.h.
153   address -= base;
154 
155   // Align the pointer, if required.
156   if ((encoding & 0xf0) == dwarf2reader::DW_EH_PE_aligned)
157     Align(AddressSize());
158 
159   // Append ADDRESS to this section in the appropriate form. For the
160   // fixed-width forms, we don't need to differentiate between signed and
161   // unsigned encodings, because ADDRESS has already been extended to 64
162   // bits before it was passed to us.
163   switch (encoding & 0x0f) {
164     case dwarf2reader::DW_EH_PE_absptr:
165       Address(address);
166       break;
167 
168     case dwarf2reader::DW_EH_PE_uleb128:
169       ULEB128(address);
170       break;
171 
172     case dwarf2reader::DW_EH_PE_sleb128:
173       LEB128(address);
174       break;
175 
176     case dwarf2reader::DW_EH_PE_udata2:
177     case dwarf2reader::DW_EH_PE_sdata2:
178       D16(address);
179       break;
180 
181     case dwarf2reader::DW_EH_PE_udata4:
182     case dwarf2reader::DW_EH_PE_sdata4:
183       D32(address);
184       break;
185 
186     case dwarf2reader::DW_EH_PE_udata8:
187     case dwarf2reader::DW_EH_PE_sdata8:
188       D64(address);
189       break;
190 
191     default:
192       abort();
193   }
194 
195   return *this;
196 };
197 
198 const uint32_t CFISection::kDwarf64InitialLengthMarker;
199 const uint32_t CFISection::kDwarf32CIEIdentifier;
200 const uint64_t CFISection::kDwarf64CIEIdentifier;
201 const uint32_t CFISection::kEHFrame32CIEIdentifier;
202 const uint64_t CFISection::kEHFrame64CIEIdentifier;
203 
204 } // namespace google_breakpad
205