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
40namespace google_breakpad {
41
42using dwarf2reader::DwarfPointerEncoding;
43
44CFISection &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  assert(!entry_length_);
51  entry_length_ = new PendingLength();
52  in_fde_ = false;
53
54  if (dwarf64) {
55    D32(kDwarf64InitialLengthMarker);
56    D64(entry_length_->length);
57    entry_length_->start = Here();
58    D64(eh_frame_ ? kEHFrame64CIEIdentifier : kDwarf64CIEIdentifier);
59  } else {
60    D32(entry_length_->length);
61    entry_length_->start = Here();
62    D32(eh_frame_ ? kEHFrame32CIEIdentifier : kDwarf32CIEIdentifier);
63  }
64  D8(version);
65  AppendCString(augmentation);
66  ULEB128(code_alignment_factor);
67  LEB128(data_alignment_factor);
68  if (version == 1)
69    D8(return_address_register);
70  else
71    ULEB128(return_address_register);
72  return *this;
73}
74
75CFISection &CFISection::FDEHeader(Label cie_pointer,
76                                  uint64_t initial_location,
77                                  uint64_t address_range,
78                                  bool dwarf64) {
79  assert(!entry_length_);
80  entry_length_ = new PendingLength();
81  in_fde_ = true;
82  fde_start_address_ = initial_location;
83
84  if (dwarf64) {
85    D32(0xffffffff);
86    D64(entry_length_->length);
87    entry_length_->start = Here();
88    if (eh_frame_)
89      D64(Here() - cie_pointer);
90    else
91      D64(cie_pointer);
92  } else {
93    D32(entry_length_->length);
94    entry_length_->start = Here();
95    if (eh_frame_)
96      D32(Here() - cie_pointer);
97    else
98      D32(cie_pointer);
99  }
100  EncodedPointer(initial_location);
101  // The FDE length in an .eh_frame section uses the same encoding as the
102  // initial location, but ignores the base address (selected by the upper
103  // nybble of the encoding), as it's a length, not an address that can be
104  // made relative.
105  EncodedPointer(address_range,
106                 DwarfPointerEncoding(pointer_encoding_ & 0x0f));
107  return *this;
108}
109
110CFISection &CFISection::FinishEntry() {
111  assert(entry_length_);
112  Align(address_size_, dwarf2reader::DW_CFA_nop);
113  entry_length_->length = Here() - entry_length_->start;
114  delete entry_length_;
115  entry_length_ = NULL;
116  in_fde_ = false;
117  return *this;
118}
119
120CFISection &CFISection::EncodedPointer(uint64_t address,
121                                       DwarfPointerEncoding encoding,
122                                       const EncodedPointerBases &bases) {
123  // Omitted data is extremely easy to emit.
124  if (encoding == dwarf2reader::DW_EH_PE_omit)
125    return *this;
126
127  // If (encoding & dwarf2reader::DW_EH_PE_indirect) != 0, then we assume
128  // that ADDRESS is the address at which the pointer is stored --- in
129  // other words, that bit has no effect on how we write the pointer.
130  encoding = DwarfPointerEncoding(encoding & ~dwarf2reader::DW_EH_PE_indirect);
131
132  // Find the base address to which this pointer is relative. The upper
133  // nybble of the encoding specifies this.
134  uint64_t base;
135  switch (encoding & 0xf0) {
136    case dwarf2reader::DW_EH_PE_absptr:  base = 0;                  break;
137    case dwarf2reader::DW_EH_PE_pcrel:   base = bases.cfi + Size(); break;
138    case dwarf2reader::DW_EH_PE_textrel: base = bases.text;         break;
139    case dwarf2reader::DW_EH_PE_datarel: base = bases.data;         break;
140    case dwarf2reader::DW_EH_PE_funcrel: base = fde_start_address_; break;
141    case dwarf2reader::DW_EH_PE_aligned: base = 0;                  break;
142    default: abort();
143  };
144
145  // Make ADDRESS relative. Yes, this is appropriate even for "absptr"
146  // values; see gcc/unwind-pe.h.
147  address -= base;
148
149  // Align the pointer, if required.
150  if ((encoding & 0xf0) == dwarf2reader::DW_EH_PE_aligned)
151    Align(AddressSize());
152
153  // Append ADDRESS to this section in the appropriate form. For the
154  // fixed-width forms, we don't need to differentiate between signed and
155  // unsigned encodings, because ADDRESS has already been extended to 64
156  // bits before it was passed to us.
157  switch (encoding & 0x0f) {
158    case dwarf2reader::DW_EH_PE_absptr:
159      Address(address);
160      break;
161
162    case dwarf2reader::DW_EH_PE_uleb128:
163      ULEB128(address);
164      break;
165
166    case dwarf2reader::DW_EH_PE_sleb128:
167      LEB128(address);
168      break;
169
170    case dwarf2reader::DW_EH_PE_udata2:
171    case dwarf2reader::DW_EH_PE_sdata2:
172      D16(address);
173      break;
174
175    case dwarf2reader::DW_EH_PE_udata4:
176    case dwarf2reader::DW_EH_PE_sdata4:
177      D32(address);
178      break;
179
180    case dwarf2reader::DW_EH_PE_udata8:
181    case dwarf2reader::DW_EH_PE_sdata8:
182      D64(address);
183      break;
184
185    default:
186      abort();
187  }
188
189  return *this;
190};
191
192const uint32_t CFISection::kDwarf64InitialLengthMarker;
193const uint32_t CFISection::kDwarf32CIEIdentifier;
194const uint64_t CFISection::kDwarf64CIEIdentifier;
195const uint32_t CFISection::kEHFrame32CIEIdentifier;
196const uint64_t CFISection::kEHFrame64CIEIdentifier;
197
198} // namespace google_breakpad
199