blob: 9ed979b41ae3d8d95a84b1055cbdc32abf1c2e3b [file] [log] [blame]
Mark Mentovai4febb342022-09-07 10:34:05 -04001// Copyright 2010 Google LLC
jimblandy3e768ed2010-03-16 16:31:49 +00002//
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.
Mark Mentovai4febb342022-09-07 10:34:05 -040013// * Neither the name of Google LLC nor the names of its
jimblandy3e768ed2010-03-16 16:31:49 +000014// 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 <jimb@mozilla.com> <jimb@red-bean.com>
30
31// cfi_assembler.cc: Implementation of google_breakpad::CFISection class.
32// See cfi_assembler.h for details.
33
jimblandy3e768ed2010-03-16 16:31:49 +000034#include "common/dwarf/cfi_assembler.h"
jimblandy3e768ed2010-03-16 16:31:49 +000035
ted.mielczareke1930982010-06-25 16:57:07 +000036#include <assert.h>
37#include <stdlib.h>
38
jimblandy3e768ed2010-03-16 16:31:49 +000039namespace google_breakpad {
40
Mike Frysinger09b05692020-06-23 18:55:43 -040041CFISection& CFISection::CIEHeader(uint64_t code_alignment_factor,
jimblandy3e768ed2010-03-16 16:31:49 +000042 int data_alignment_factor,
43 unsigned return_address_register,
ted.mielczarek@gmail.comaeffe102013-03-06 14:04:42 +000044 uint8_t version,
Mike Frysinger09b05692020-06-23 18:55:43 -040045 const string& augmentation,
Scott Graham1f574b52016-11-01 15:05:10 -070046 bool dwarf64,
47 uint8_t address_size,
48 uint8_t segment_size) {
jimblandy3e768ed2010-03-16 16:31:49 +000049 assert(!entry_length_);
50 entry_length_ = new PendingLength();
jimblandya76aaa12010-03-16 16:37:50 +000051 in_fde_ = false;
jimblandy3e768ed2010-03-16 16:31:49 +000052
53 if (dwarf64) {
ted.mielczarek4f456b82010-06-25 16:57:02 +000054 D32(kDwarf64InitialLengthMarker);
jimblandy3e768ed2010-03-16 16:31:49 +000055 D64(entry_length_->length);
56 entry_length_->start = Here();
ted.mielczarek4f456b82010-06-25 16:57:02 +000057 D64(eh_frame_ ? kEHFrame64CIEIdentifier : kDwarf64CIEIdentifier);
jimblandy3e768ed2010-03-16 16:31:49 +000058 } else {
59 D32(entry_length_->length);
60 entry_length_->start = Here();
ted.mielczarek4f456b82010-06-25 16:57:02 +000061 D32(eh_frame_ ? kEHFrame32CIEIdentifier : kDwarf32CIEIdentifier);
jimblandy3e768ed2010-03-16 16:31:49 +000062 }
63 D8(version);
64 AppendCString(augmentation);
Scott Graham1f574b52016-11-01 15:05:10 -070065 if (version >= 4) {
66 D8(address_size);
67 D8(segment_size);
68 }
jimblandy3e768ed2010-03-16 16:31:49 +000069 ULEB128(code_alignment_factor);
70 LEB128(data_alignment_factor);
71 if (version == 1)
72 D8(return_address_register);
73 else
74 ULEB128(return_address_register);
75 return *this;
76}
77
Mike Frysinger09b05692020-06-23 18:55:43 -040078CFISection& CFISection::FDEHeader(Label cie_pointer,
ted.mielczarek@gmail.comaeffe102013-03-06 14:04:42 +000079 uint64_t initial_location,
80 uint64_t address_range,
jimblandy3e768ed2010-03-16 16:31:49 +000081 bool dwarf64) {
82 assert(!entry_length_);
83 entry_length_ = new PendingLength();
jimblandya76aaa12010-03-16 16:37:50 +000084 in_fde_ = true;
85 fde_start_address_ = initial_location;
jimblandy3e768ed2010-03-16 16:31:49 +000086
87 if (dwarf64) {
88 D32(0xffffffff);
89 D64(entry_length_->length);
90 entry_length_->start = Here();
jimblandya76aaa12010-03-16 16:37:50 +000091 if (eh_frame_)
92 D64(Here() - cie_pointer);
93 else
94 D64(cie_pointer);
jimblandy3e768ed2010-03-16 16:31:49 +000095 } else {
96 D32(entry_length_->length);
97 entry_length_->start = Here();
jimblandya76aaa12010-03-16 16:37:50 +000098 if (eh_frame_)
99 D32(Here() - cie_pointer);
100 else
101 D32(cie_pointer);
jimblandy3e768ed2010-03-16 16:31:49 +0000102 }
jimblandya76aaa12010-03-16 16:37:50 +0000103 EncodedPointer(initial_location);
104 // The FDE length in an .eh_frame section uses the same encoding as the
105 // initial location, but ignores the base address (selected by the upper
106 // nybble of the encoding), as it's a length, not an address that can be
107 // made relative.
108 EncodedPointer(address_range,
109 DwarfPointerEncoding(pointer_encoding_ & 0x0f));
jimblandy3e768ed2010-03-16 16:31:49 +0000110 return *this;
111}
112
Mike Frysinger09b05692020-06-23 18:55:43 -0400113CFISection& CFISection::FinishEntry() {
jimblandy3e768ed2010-03-16 16:31:49 +0000114 assert(entry_length_);
Tyrel Russell0622f682021-05-26 08:35:33 -0400115 Align(address_size_, DW_CFA_nop);
jimblandy3e768ed2010-03-16 16:31:49 +0000116 entry_length_->length = Here() - entry_length_->start;
117 delete entry_length_;
118 entry_length_ = NULL;
jimblandya76aaa12010-03-16 16:37:50 +0000119 in_fde_ = false;
jimblandy3e768ed2010-03-16 16:31:49 +0000120 return *this;
121}
122
Mike Frysinger09b05692020-06-23 18:55:43 -0400123CFISection& CFISection::EncodedPointer(uint64_t address,
jimblandya76aaa12010-03-16 16:37:50 +0000124 DwarfPointerEncoding encoding,
Mike Frysinger09b05692020-06-23 18:55:43 -0400125 const EncodedPointerBases& bases) {
jimblandya76aaa12010-03-16 16:37:50 +0000126 // Omitted data is extremely easy to emit.
Tyrel Russell0622f682021-05-26 08:35:33 -0400127 if (encoding == DW_EH_PE_omit)
jimblandya76aaa12010-03-16 16:37:50 +0000128 return *this;
129
Tyrel Russell0622f682021-05-26 08:35:33 -0400130 // If (encoding & DW_EH_PE_indirect) != 0, then we assume
jimblandya76aaa12010-03-16 16:37:50 +0000131 // that ADDRESS is the address at which the pointer is stored --- in
132 // other words, that bit has no effect on how we write the pointer.
Tyrel Russell0622f682021-05-26 08:35:33 -0400133 encoding = DwarfPointerEncoding(encoding & ~DW_EH_PE_indirect);
jimblandya76aaa12010-03-16 16:37:50 +0000134
135 // Find the base address to which this pointer is relative. The upper
136 // nybble of the encoding specifies this.
ted.mielczarek@gmail.comaeffe102013-03-06 14:04:42 +0000137 uint64_t base;
jimblandya76aaa12010-03-16 16:37:50 +0000138 switch (encoding & 0xf0) {
Tyrel Russell0622f682021-05-26 08:35:33 -0400139 case DW_EH_PE_absptr: base = 0; break;
140 case DW_EH_PE_pcrel: base = bases.cfi + Size(); break;
141 case DW_EH_PE_textrel: base = bases.text; break;
142 case DW_EH_PE_datarel: base = bases.data; break;
143 case DW_EH_PE_funcrel: base = fde_start_address_; break;
144 case DW_EH_PE_aligned: base = 0; break;
jimblandya76aaa12010-03-16 16:37:50 +0000145 default: abort();
146 };
147
148 // Make ADDRESS relative. Yes, this is appropriate even for "absptr"
149 // values; see gcc/unwind-pe.h.
150 address -= base;
151
152 // Align the pointer, if required.
Tyrel Russell0622f682021-05-26 08:35:33 -0400153 if ((encoding & 0xf0) == DW_EH_PE_aligned)
jimblandya76aaa12010-03-16 16:37:50 +0000154 Align(AddressSize());
155
156 // Append ADDRESS to this section in the appropriate form. For the
157 // fixed-width forms, we don't need to differentiate between signed and
158 // unsigned encodings, because ADDRESS has already been extended to 64
159 // bits before it was passed to us.
160 switch (encoding & 0x0f) {
Tyrel Russell0622f682021-05-26 08:35:33 -0400161 case DW_EH_PE_absptr:
jimblandya76aaa12010-03-16 16:37:50 +0000162 Address(address);
163 break;
164
Tyrel Russell0622f682021-05-26 08:35:33 -0400165 case DW_EH_PE_uleb128:
jimblandya76aaa12010-03-16 16:37:50 +0000166 ULEB128(address);
167 break;
168
Tyrel Russell0622f682021-05-26 08:35:33 -0400169 case DW_EH_PE_sleb128:
jimblandya76aaa12010-03-16 16:37:50 +0000170 LEB128(address);
171 break;
172
Tyrel Russell0622f682021-05-26 08:35:33 -0400173 case DW_EH_PE_udata2:
174 case DW_EH_PE_sdata2:
jimblandya76aaa12010-03-16 16:37:50 +0000175 D16(address);
176 break;
177
Tyrel Russell0622f682021-05-26 08:35:33 -0400178 case DW_EH_PE_udata4:
179 case DW_EH_PE_sdata4:
jimblandya76aaa12010-03-16 16:37:50 +0000180 D32(address);
181 break;
182
Tyrel Russell0622f682021-05-26 08:35:33 -0400183 case DW_EH_PE_udata8:
184 case DW_EH_PE_sdata8:
jimblandya76aaa12010-03-16 16:37:50 +0000185 D64(address);
186 break;
187
188 default:
189 abort();
190 }
191
192 return *this;
jimblandy3e768ed2010-03-16 16:31:49 +0000193};
jimblandya76aaa12010-03-16 16:37:50 +0000194
ted.mielczarek@gmail.comaeffe102013-03-06 14:04:42 +0000195const uint32_t CFISection::kDwarf64InitialLengthMarker;
196const uint32_t CFISection::kDwarf32CIEIdentifier;
197const uint64_t CFISection::kDwarf64CIEIdentifier;
198const uint32_t CFISection::kEHFrame32CIEIdentifier;
199const uint64_t CFISection::kEHFrame64CIEIdentifier;
ted.mielczarek4f456b82010-06-25 16:57:02 +0000200
jimblandya76aaa12010-03-16 16:37:50 +0000201} // namespace google_breakpad