blob: 83e5f5ad39a3cd414e4bc0bf9ac3a99320986b4c [file] [log] [blame]
Tong Shen547cdfd2014-08-05 01:54:19 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "leb128.h"
18#include "utils.h"
19
20#include "dwarf_cfi.h"
21
22namespace art {
23
24void DW_CFA_advance_loc(std::vector<uint8_t>* buf, uint32_t increment) {
25 if (increment < 64) {
26 // Encoding in opcode.
27 buf->push_back(0x1 << 6 | increment);
28 } else if (increment < 256) {
29 // Single byte delta.
30 buf->push_back(0x02);
31 buf->push_back(increment);
32 } else if (increment < 256 * 256) {
33 // Two byte delta.
34 buf->push_back(0x03);
35 buf->push_back(increment & 0xff);
36 buf->push_back((increment >> 8) & 0xff);
37 } else {
38 // Four byte delta.
39 buf->push_back(0x04);
40 PushWord(buf, increment);
41 }
42}
43
44void DW_CFA_offset_extended_sf(std::vector<uint8_t>* buf, int reg, int32_t offset) {
45 buf->push_back(0x11);
46 EncodeUnsignedLeb128(reg, buf);
47 EncodeSignedLeb128(offset, buf);
48}
49
50void DW_CFA_offset(std::vector<uint8_t>* buf, int reg, uint32_t offset) {
51 buf->push_back((0x2 << 6) | reg);
52 EncodeUnsignedLeb128(offset, buf);
53}
54
55void DW_CFA_def_cfa_offset(std::vector<uint8_t>* buf, int32_t offset) {
56 buf->push_back(0x0e);
57 EncodeUnsignedLeb128(offset, buf);
58}
59
60void DW_CFA_remember_state(std::vector<uint8_t>* buf) {
61 buf->push_back(0x0a);
62}
63
64void DW_CFA_restore_state(std::vector<uint8_t>* buf) {
65 buf->push_back(0x0b);
66}
67
Yevgeny Roubane3ea8382014-08-08 16:29:38 +070068void WriteFDEHeader(std::vector<uint8_t>* buf, bool is_64bit) {
Tong Shen547cdfd2014-08-05 01:54:19 -070069 // 'length' (filled in by other functions).
Yevgeny Roubane3ea8382014-08-08 16:29:38 +070070 if (is_64bit) {
71 PushWord(buf, 0xffffffff); // Indicates 64bit
72 PushWord(buf, 0);
73 PushWord(buf, 0);
74 } else {
75 PushWord(buf, 0);
76 }
Tong Shen547cdfd2014-08-05 01:54:19 -070077
78 // 'CIE_pointer' (filled in by linker).
Yevgeny Roubane3ea8382014-08-08 16:29:38 +070079 if (is_64bit) {
80 PushWord(buf, 0);
81 PushWord(buf, 0);
82 } else {
83 PushWord(buf, 0);
84 }
Tong Shen547cdfd2014-08-05 01:54:19 -070085
86 // 'initial_location' (filled in by linker).
Yevgeny Roubane3ea8382014-08-08 16:29:38 +070087 if (is_64bit) {
88 PushWord(buf, 0);
89 PushWord(buf, 0);
90 } else {
91 PushWord(buf, 0);
92 }
Tong Shen547cdfd2014-08-05 01:54:19 -070093
94 // 'address_range' (filled in by other functions).
Yevgeny Roubane3ea8382014-08-08 16:29:38 +070095 if (is_64bit) {
96 PushWord(buf, 0);
97 PushWord(buf, 0);
98 } else {
99 PushWord(buf, 0);
100 }
Tong Shen547cdfd2014-08-05 01:54:19 -0700101
102 // Augmentation length: 0
103 buf->push_back(0);
104}
105
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700106void WriteFDEAddressRange(std::vector<uint8_t>* buf, uint64_t data, bool is_64bit) {
107 const size_t kOffsetOfAddressRange = is_64bit? 28 : 12;
108 CHECK(buf->size() >= kOffsetOfAddressRange + (is_64bit? 8 : 4));
Tong Shen547cdfd2014-08-05 01:54:19 -0700109
110 uint8_t *p = buf->data() + kOffsetOfAddressRange;
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700111 if (is_64bit) {
112 p[0] = data;
113 p[1] = data >> 8;
114 p[2] = data >> 16;
115 p[3] = data >> 24;
116 p[4] = data >> 32;
117 p[5] = data >> 40;
118 p[6] = data >> 48;
119 p[7] = data >> 56;
120 } else {
121 p[0] = data;
122 p[1] = data >> 8;
123 p[2] = data >> 16;
124 p[3] = data >> 24;
125 }
Tong Shen547cdfd2014-08-05 01:54:19 -0700126}
127
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700128void WriteCFILength(std::vector<uint8_t>* buf, bool is_64bit) {
129 uint64_t length = is_64bit ? buf->size() - 12 : buf->size() - 4;
Tong Shen547cdfd2014-08-05 01:54:19 -0700130 DCHECK_EQ((length & 0x3), 0U);
Tong Shen547cdfd2014-08-05 01:54:19 -0700131
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700132 uint8_t *p = is_64bit? buf->data() + 4 : buf->data();
133 if (is_64bit) {
134 p[0] = length;
135 p[1] = length >> 8;
136 p[2] = length >> 16;
137 p[3] = length >> 24;
138 p[4] = length >> 32;
139 p[5] = length >> 40;
140 p[6] = length >> 48;
141 p[7] = length >> 56;
142 } else {
143 p[0] = length;
144 p[1] = length >> 8;
145 p[2] = length >> 16;
146 p[3] = length >> 24;
147 }
Tong Shen547cdfd2014-08-05 01:54:19 -0700148}
149
150void PadCFI(std::vector<uint8_t>* buf) {
151 while (buf->size() & 0x3) {
152 buf->push_back(0);
153 }
154}
155
156} // namespace art