summaryrefslogtreecommitdiffstats
path: root/compiler/utils/dwarf_cfi.cc
blob: a7e09c6517a239a8960b7b26dcea3914552abf2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "leb128.h"
#include "utils.h"

#include "dwarf_cfi.h"

namespace art {

void DW_CFA_advance_loc(std::vector<uint8_t>* buf, uint32_t increment) {
  if (increment < 64) {
    // Encoding in opcode.
    buf->push_back(0x1 << 6 | increment);
  } else if (increment < 256) {
    // Single byte delta.
    buf->push_back(0x02);
    buf->push_back(increment);
  } else if (increment < 256 * 256) {
    // Two byte delta.
    buf->push_back(0x03);
    buf->push_back(increment & 0xff);
    buf->push_back((increment >> 8) & 0xff);
  } else {
    // Four byte delta.
    buf->push_back(0x04);
    Push32(buf, increment);
  }
}

void DW_CFA_offset_extended_sf(std::vector<uint8_t>* buf, int reg, int32_t offset) {
  buf->push_back(0x11);
  EncodeUnsignedLeb128(reg, buf);
  EncodeSignedLeb128(offset, buf);
}

void DW_CFA_offset(std::vector<uint8_t>* buf, int reg, uint32_t offset) {
  buf->push_back((0x2 << 6) | reg);
  EncodeUnsignedLeb128(offset, buf);
}

void DW_CFA_def_cfa_offset(std::vector<uint8_t>* buf, int32_t offset) {
  buf->push_back(0x0e);
  EncodeUnsignedLeb128(offset, buf);
}

void DW_CFA_remember_state(std::vector<uint8_t>* buf) {
  buf->push_back(0x0a);
}

void DW_CFA_restore_state(std::vector<uint8_t>* buf) {
  buf->push_back(0x0b);
}

void WriteFDEHeader(std::vector<uint8_t>* buf, bool is_64bit) {
  // 'length' (filled in by other functions).
  if (is_64bit) {
    Push32(buf, 0xffffffff);  // Indicates 64bit
    Push32(buf, 0);
    Push32(buf, 0);
  } else {
    Push32(buf, 0);
  }

  // 'CIE_pointer' (filled in by linker).
  if (is_64bit) {
    Push32(buf, 0);
    Push32(buf, 0);
  } else {
    Push32(buf, 0);
  }

  // 'initial_location' (filled in by linker).
  if (is_64bit) {
    Push32(buf, 0);
    Push32(buf, 0);
  } else {
    Push32(buf, 0);
  }

  // 'address_range' (filled in by other functions).
  if (is_64bit) {
    Push32(buf, 0);
    Push32(buf, 0);
  } else {
    Push32(buf, 0);
  }

  // Augmentation length: 0
  buf->push_back(0);
}

void WriteFDEAddressRange(std::vector<uint8_t>* buf, uint64_t data, bool is_64bit) {
  const size_t kOffsetOfAddressRange = is_64bit? 28 : 12;
  CHECK(buf->size() >= kOffsetOfAddressRange + (is_64bit? 8 : 4));

  uint8_t *p = buf->data() + kOffsetOfAddressRange;
  if (is_64bit) {
    p[0] = data;
    p[1] = data >> 8;
    p[2] = data >> 16;
    p[3] = data >> 24;
    p[4] = data >> 32;
    p[5] = data >> 40;
    p[6] = data >> 48;
    p[7] = data >> 56;
  } else {
    p[0] = data;
    p[1] = data >> 8;
    p[2] = data >> 16;
    p[3] = data >> 24;
  }
}

void WriteCFILength(std::vector<uint8_t>* buf, bool is_64bit) {
  uint64_t length = is_64bit ? buf->size() - 12 : buf->size() - 4;
  DCHECK_EQ((length & 0x3), 0U);

  uint8_t *p = is_64bit? buf->data() + 4 : buf->data();
  if (is_64bit) {
    p[0] = length;
    p[1] = length >> 8;
    p[2] = length >> 16;
    p[3] = length >> 24;
    p[4] = length >> 32;
    p[5] = length >> 40;
    p[6] = length >> 48;
    p[7] = length >> 56;
  } else {
    p[0] = length;
    p[1] = length >> 8;
    p[2] = length >> 16;
    p[3] = length >> 24;
  }
}

void PadCFI(std::vector<uint8_t>* buf) {
  while (buf->size() & 0x3) {
    buf->push_back(0);
  }
}

}  // namespace art