blob: b3d1a47b807f787bb4a43166185ca79d2fdcd965 [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
68void WriteFDEHeader(std::vector<uint8_t>* buf) {
69 // 'length' (filled in by other functions).
70 PushWord(buf, 0);
71
72 // 'CIE_pointer' (filled in by linker).
73 PushWord(buf, 0);
74
75 // 'initial_location' (filled in by linker).
76 PushWord(buf, 0);
77
78 // 'address_range' (filled in by other functions).
79 PushWord(buf, 0);
80
81 // Augmentation length: 0
82 buf->push_back(0);
83}
84
85void WriteFDEAddressRange(std::vector<uint8_t>* buf, uint32_t data) {
86 const int kOffsetOfAddressRange = 12;
87 CHECK(buf->size() >= kOffsetOfAddressRange + sizeof(uint32_t));
88
89 uint8_t *p = buf->data() + kOffsetOfAddressRange;
90 p[0] = data;
91 p[1] = data >> 8;
92 p[2] = data >> 16;
93 p[3] = data >> 24;
94}
95
96void WriteCFILength(std::vector<uint8_t>* buf) {
97 uint32_t length = buf->size() - 4;
98 DCHECK_EQ((length & 0x3), 0U);
99 DCHECK_GT(length, 4U);
100
101 uint8_t *p = buf->data();
102 p[0] = length;
103 p[1] = length >> 8;
104 p[2] = length >> 16;
105 p[3] = length >> 24;
106}
107
108void PadCFI(std::vector<uint8_t>* buf) {
109 while (buf->size() & 0x3) {
110 buf->push_back(0);
111 }
112}
113
114} // namespace art