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