blob: 5e1b39b67729e83f3bac4126159d2e2e7c7161d0 [file] [log] [blame]
David Srbeckyb5362472015-04-08 19:37:39 +01001/*
2 * Copyright (C) 2015 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
David Srbecky2faab002019-02-12 16:35:48 +000017#ifndef ART_LIBELFFILE_DWARF_HEADERS_H_
18#define ART_LIBELFFILE_DWARF_HEADERS_H_
David Srbeckyb5362472015-04-08 19:37:39 +010019
David Srbecky2f6cdb02015-04-11 00:17:53 +010020#include <cstdint>
21
David Brazdild9c90372016-09-14 16:53:55 +010022#include "base/array_ref.h"
David Srbecky2faab002019-02-12 16:35:48 +000023#include "dwarf/debug_frame_opcode_writer.h"
24#include "dwarf/debug_info_entry_writer.h"
25#include "dwarf/debug_line_opcode_writer.h"
26#include "dwarf/dwarf_constants.h"
27#include "dwarf/register.h"
28#include "dwarf/writer.h"
David Srbeckyb5362472015-04-08 19:37:39 +010029
30namespace art {
31namespace dwarf {
32
David Srbecky2f6cdb02015-04-11 00:17:53 +010033// Note that all headers start with 32-bit length.
34// DWARF also supports 64-bit lengths, but we never use that.
35// It is intended to support very large debug sections (>4GB),
36// and compilers are expected *not* to use it by default.
37// In particular, it is not related to machine architecture.
38
David Srbeckyad5fa8c2015-05-06 18:27:35 +010039// Write common information entry (CIE) to .debug_frame or .eh_frame section.
Vladimir Markoec7802a2015-10-01 20:57:57 +010040template<typename Vector>
David Srbecky6d8c8f02015-10-26 10:57:09 +000041void WriteCIE(bool is64bit,
42 Reg return_address_register,
43 const DebugFrameOpCodeWriter<Vector>& opcodes,
David Srbecky6d8c8f02015-10-26 10:57:09 +000044 std::vector<uint8_t>* buffer) {
Vladimir Markoec7802a2015-10-01 20:57:57 +010045 static_assert(std::is_same<typename Vector::value_type, uint8_t>::value, "Invalid value type");
46
David Srbecky6d8c8f02015-10-26 10:57:09 +000047 Writer<> writer(buffer);
David Srbeckyb5362472015-04-08 19:37:39 +010048 size_t cie_header_start_ = writer.data()->size();
David Srbecky1e271ce2021-07-12 15:14:13 +000049 writer.PushUint32(0); // Length placeholder.
50 writer.PushUint32(0xFFFFFFFF); // CIE id.
David Srbeckycd437002021-07-12 16:53:57 +010051 writer.PushUint8(4); // Version.
52 writer.PushString(""); // Augmentation.
53 writer.PushUint8(is64bit ? 8 : 4); // Address size.
54 writer.PushUint8(0); // Segment size.
Vladimir Markoec7802a2015-10-01 20:57:57 +010055 writer.PushUleb128(DebugFrameOpCodeWriter<Vector>::kCodeAlignmentFactor);
56 writer.PushSleb128(DebugFrameOpCodeWriter<Vector>::kDataAlignmentFactor);
David Srbeckyb5362472015-04-08 19:37:39 +010057 writer.PushUleb128(return_address_register.num()); // ubyte in DWARF2.
David Srbecky91cb54e2016-01-15 13:47:59 +000058 writer.PushData(opcodes.data());
David Srbeckyb5362472015-04-08 19:37:39 +010059 writer.Pad(is64bit ? 8 : 4);
David Srbecky1e271ce2021-07-12 15:14:13 +000060 writer.UpdateUint32(cie_header_start_, writer.data()->size() - cie_header_start_ - 4);
David Srbeckyb5362472015-04-08 19:37:39 +010061}
62
David Srbeckyad5fa8c2015-05-06 18:27:35 +010063// Write frame description entry (FDE) to .debug_frame or .eh_frame section.
Vladimir Marko35831e82015-09-11 11:59:18 +010064inline
David Srbecky6d8c8f02015-10-26 10:57:09 +000065void WriteFDE(bool is64bit,
David Srbecky7370d922019-02-12 14:00:30 +000066 uint64_t cie_pointer, // Offset of relevant CIE in debug_frame setcion.
David Srbecky6d8c8f02015-10-26 10:57:09 +000067 uint64_t code_address,
68 uint64_t code_size,
69 const ArrayRef<const uint8_t>& opcodes,
David Srbecky7370d922019-02-12 14:00:30 +000070 /*inout*/ std::vector<uint8_t>* buffer) {
David Srbecky6d8c8f02015-10-26 10:57:09 +000071 Writer<> writer(buffer);
David Srbeckyb5362472015-04-08 19:37:39 +010072 size_t fde_header_start = writer.data()->size();
David Srbecky1e271ce2021-07-12 15:14:13 +000073 writer.PushUint32(0); // Length placeholder.
74 writer.PushUint32(cie_pointer);
David Srbecky91b29002019-02-08 15:51:31 +000075 // Relocate code_address if it has absolute value.
David Srbecky6d8c8f02015-10-26 10:57:09 +000076 if (is64bit) {
77 writer.PushUint64(code_address);
78 writer.PushUint64(code_size);
79 } else {
80 writer.PushUint32(code_address);
81 writer.PushUint32(code_size);
David Srbeckyb5362472015-04-08 19:37:39 +010082 }
83 writer.PushUleb128(0); // Augmentation data size.
David Srbecky91cb54e2016-01-15 13:47:59 +000084 writer.PushData(opcodes.data(), opcodes.size());
David Srbeckyb5362472015-04-08 19:37:39 +010085 writer.Pad(is64bit ? 8 : 4);
David Srbecky1e271ce2021-07-12 15:14:13 +000086 writer.UpdateUint32(fde_header_start, writer.data()->size() - fde_header_start - 4);
David Srbeckyb5362472015-04-08 19:37:39 +010087}
88
89// Write compilation unit (CU) to .debug_info section.
Vladimir Markoec7802a2015-10-01 20:57:57 +010090template<typename Vector>
David Srbeckyb5362472015-04-08 19:37:39 +010091void WriteDebugInfoCU(uint32_t debug_abbrev_offset,
Vladimir Markoec7802a2015-10-01 20:57:57 +010092 const DebugInfoEntryWriter<Vector>& entries,
David Srbecky7370d922019-02-12 14:00:30 +000093 std::vector<uint8_t>* debug_info) {
Vladimir Markoec7802a2015-10-01 20:57:57 +010094 static_assert(std::is_same<typename Vector::value_type, uint8_t>::value, "Invalid value type");
95
David Srbeckyb5362472015-04-08 19:37:39 +010096 Writer<> writer(debug_info);
97 size_t start = writer.data()->size();
98 writer.PushUint32(0); // Length placeholder.
David Srbecky0fd295f2015-11-16 16:39:10 +000099 writer.PushUint16(4); // Version.
David Srbeckyb5362472015-04-08 19:37:39 +0100100 writer.PushUint32(debug_abbrev_offset);
David Srbecky2f6cdb02015-04-11 00:17:53 +0100101 writer.PushUint8(entries.Is64bit() ? 8 : 4);
102 size_t entries_offset = writer.data()->size();
David Srbecky04b05262015-11-09 18:05:48 +0000103 DCHECK_EQ(entries_offset, DebugInfoEntryWriter<Vector>::kCompilationUnitHeaderSize);
David Srbecky91cb54e2016-01-15 13:47:59 +0000104 writer.PushData(entries.data());
David Srbeckyb5362472015-04-08 19:37:39 +0100105 writer.UpdateUint32(start, writer.data()->size() - start - 4);
106}
107
108struct FileEntry {
109 std::string file_name;
110 int directory_index;
111 int modification_time;
112 int file_size;
113};
114
115// Write line table to .debug_line section.
Vladimir Markoec7802a2015-10-01 20:57:57 +0100116template<typename Vector>
David Srbeckyb5362472015-04-08 19:37:39 +0100117void WriteDebugLineTable(const std::vector<std::string>& include_directories,
118 const std::vector<FileEntry>& files,
Vladimir Markoec7802a2015-10-01 20:57:57 +0100119 const DebugLineOpCodeWriter<Vector>& opcodes,
David Srbecky7370d922019-02-12 14:00:30 +0000120 std::vector<uint8_t>* debug_line) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100121 static_assert(std::is_same<typename Vector::value_type, uint8_t>::value, "Invalid value type");
122
David Srbeckyb5362472015-04-08 19:37:39 +0100123 Writer<> writer(debug_line);
124 size_t header_start = writer.data()->size();
125 writer.PushUint32(0); // Section-length placeholder.
David Srbecky0fd295f2015-11-16 16:39:10 +0000126 writer.PushUint16(3); // .debug_line version.
David Srbeckyb5362472015-04-08 19:37:39 +0100127 size_t header_length_pos = writer.data()->size();
128 writer.PushUint32(0); // Header-length placeholder.
129 writer.PushUint8(1 << opcodes.GetCodeFactorBits());
Vladimir Markoec7802a2015-10-01 20:57:57 +0100130 writer.PushUint8(DebugLineOpCodeWriter<Vector>::kDefaultIsStmt ? 1 : 0);
131 writer.PushInt8(DebugLineOpCodeWriter<Vector>::kLineBase);
132 writer.PushUint8(DebugLineOpCodeWriter<Vector>::kLineRange);
133 writer.PushUint8(DebugLineOpCodeWriter<Vector>::kOpcodeBase);
134 static const int opcode_lengths[DebugLineOpCodeWriter<Vector>::kOpcodeBase] = {
David Srbeckyb5362472015-04-08 19:37:39 +0100135 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1 };
Vladimir Markoec7802a2015-10-01 20:57:57 +0100136 for (int i = 1; i < DebugLineOpCodeWriter<Vector>::kOpcodeBase; i++) {
David Srbeckyb5362472015-04-08 19:37:39 +0100137 writer.PushUint8(opcode_lengths[i]);
138 }
139 for (const std::string& directory : include_directories) {
140 writer.PushData(directory.data(), directory.size() + 1);
141 }
142 writer.PushUint8(0); // Terminate include_directories list.
143 for (const FileEntry& file : files) {
144 writer.PushData(file.file_name.data(), file.file_name.size() + 1);
145 writer.PushUleb128(file.directory_index);
146 writer.PushUleb128(file.modification_time);
147 writer.PushUleb128(file.file_size);
148 }
149 writer.PushUint8(0); // Terminate file list.
150 writer.UpdateUint32(header_length_pos, writer.data()->size() - header_length_pos - 4);
David Srbecky91cb54e2016-01-15 13:47:59 +0000151 writer.PushData(opcodes.data());
David Srbeckyb5362472015-04-08 19:37:39 +0100152 writer.UpdateUint32(header_start, writer.data()->size() - header_start - 4);
David Srbeckyb5362472015-04-08 19:37:39 +0100153}
154
155} // namespace dwarf
156} // namespace art
157
David Srbecky2faab002019-02-12 16:35:48 +0000158#endif // ART_LIBELFFILE_DWARF_HEADERS_H_