Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 "elf_writer_quick.h" |
| 18 | |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 19 | #include <unordered_map> |
| 20 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 21 | #include "base/logging.h" |
| 22 | #include "base/unix_file/fd_file.h" |
Brian Carlstrom | c6dfdac | 2013-08-26 18:57:31 -0700 | [diff] [blame] | 23 | #include "buffered_output_stream.h" |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 24 | #include "compiled_method.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 25 | #include "driver/compiler_driver.h" |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 26 | #include "driver/compiler_options.h" |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 27 | #include "dwarf.h" |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 28 | #include "dwarf/debug_frame_writer.h" |
| 29 | #include "dwarf/debug_line_writer.h" |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 30 | #include "elf_builder.h" |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 31 | #include "elf_file.h" |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 32 | #include "elf_utils.h" |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 33 | #include "file_output_stream.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 34 | #include "globals.h" |
Andreas Gampe | 7927380 | 2014-08-05 20:21:05 -0700 | [diff] [blame] | 35 | #include "leb128.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 36 | #include "oat.h" |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 37 | #include "oat_writer.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 38 | #include "utils.h" |
| 39 | |
| 40 | namespace art { |
| 41 | |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 42 | static void PushByte(std::vector<uint8_t>* buf, int data) { |
| 43 | buf->push_back(data & 0xff); |
| 44 | } |
| 45 | |
| 46 | static uint32_t PushStr(std::vector<uint8_t>* buf, const char* str, const char* def = nullptr) { |
| 47 | if (str == nullptr) { |
| 48 | str = def; |
| 49 | } |
| 50 | |
| 51 | uint32_t offset = buf->size(); |
| 52 | for (size_t i = 0; str[i] != '\0'; ++i) { |
| 53 | buf->push_back(str[i]); |
| 54 | } |
| 55 | buf->push_back('\0'); |
| 56 | return offset; |
| 57 | } |
| 58 | |
| 59 | static uint32_t PushStr(std::vector<uint8_t>* buf, const std::string &str) { |
| 60 | uint32_t offset = buf->size(); |
| 61 | buf->insert(buf->end(), str.begin(), str.end()); |
| 62 | buf->push_back('\0'); |
| 63 | return offset; |
| 64 | } |
| 65 | |
Andreas Gampe | 7927380 | 2014-08-05 20:21:05 -0700 | [diff] [blame] | 66 | static void UpdateWord(std::vector<uint8_t>* buf, int offset, int data) { |
| 67 | (*buf)[offset+0] = data; |
| 68 | (*buf)[offset+1] = data >> 8; |
| 69 | (*buf)[offset+2] = data >> 16; |
| 70 | (*buf)[offset+3] = data >> 24; |
| 71 | } |
| 72 | |
Andreas Gampe | 7927380 | 2014-08-05 20:21:05 -0700 | [diff] [blame] | 73 | static void PushHalf(std::vector<uint8_t>* buf, int data) { |
| 74 | buf->push_back(data & 0xff); |
| 75 | buf->push_back((data >> 8) & 0xff); |
| 76 | } |
| 77 | |
Nicolas Geoffray | f9b87b1 | 2014-09-02 08:12:09 +0000 | [diff] [blame] | 78 | template <typename Elf_Word, typename Elf_Sword, typename Elf_Addr, |
| 79 | typename Elf_Dyn, typename Elf_Sym, typename Elf_Ehdr, |
| 80 | typename Elf_Phdr, typename Elf_Shdr> |
| 81 | bool ElfWriterQuick<Elf_Word, Elf_Sword, Elf_Addr, Elf_Dyn, |
Nicolas Geoffray | f9b87b1 | 2014-09-02 08:12:09 +0000 | [diff] [blame] | 82 | Elf_Sym, Elf_Ehdr, Elf_Phdr, Elf_Shdr>::Create(File* elf_file, |
Brian Carlstrom | b12f347 | 2014-06-11 14:54:46 -0700 | [diff] [blame] | 83 | OatWriter* oat_writer, |
| 84 | const std::vector<const DexFile*>& dex_files, |
| 85 | const std::string& android_root, |
| 86 | bool is_host, |
| 87 | const CompilerDriver& driver) { |
| 88 | ElfWriterQuick elf_writer(driver, elf_file); |
| 89 | return elf_writer.Write(oat_writer, dex_files, android_root, is_host); |
| 90 | } |
| 91 | |
Andreas Gampe | 7927380 | 2014-08-05 20:21:05 -0700 | [diff] [blame] | 92 | std::vector<uint8_t>* ConstructCIEFrameX86(bool is_x86_64) { |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 93 | std::vector<uint8_t>* cfi_info = new std::vector<uint8_t>; |
Andreas Gampe | 7927380 | 2014-08-05 20:21:05 -0700 | [diff] [blame] | 94 | |
| 95 | // Length (will be filled in later in this routine). |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 96 | if (is_x86_64) { |
Vladimir Marko | 80b96d1 | 2015-02-19 15:50:28 +0000 | [diff] [blame] | 97 | Push32(cfi_info, 0xffffffff); // Indicates 64bit |
| 98 | Push32(cfi_info, 0); |
| 99 | Push32(cfi_info, 0); |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 100 | } else { |
Vladimir Marko | 80b96d1 | 2015-02-19 15:50:28 +0000 | [diff] [blame] | 101 | Push32(cfi_info, 0); |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 102 | } |
Andreas Gampe | 7927380 | 2014-08-05 20:21:05 -0700 | [diff] [blame] | 103 | |
| 104 | // CIE id: always 0. |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 105 | if (is_x86_64) { |
Vladimir Marko | 80b96d1 | 2015-02-19 15:50:28 +0000 | [diff] [blame] | 106 | Push32(cfi_info, 0); |
| 107 | Push32(cfi_info, 0); |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 108 | } else { |
Vladimir Marko | 80b96d1 | 2015-02-19 15:50:28 +0000 | [diff] [blame] | 109 | Push32(cfi_info, 0); |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 110 | } |
Andreas Gampe | 7927380 | 2014-08-05 20:21:05 -0700 | [diff] [blame] | 111 | |
| 112 | // Version: always 1. |
| 113 | cfi_info->push_back(0x01); |
| 114 | |
| 115 | // Augmentation: 'zR\0' |
| 116 | cfi_info->push_back(0x7a); |
| 117 | cfi_info->push_back(0x52); |
| 118 | cfi_info->push_back(0x0); |
| 119 | |
| 120 | // Code alignment: 1. |
| 121 | EncodeUnsignedLeb128(1, cfi_info); |
| 122 | |
| 123 | // Data alignment. |
| 124 | if (is_x86_64) { |
| 125 | EncodeSignedLeb128(-8, cfi_info); |
| 126 | } else { |
| 127 | EncodeSignedLeb128(-4, cfi_info); |
| 128 | } |
| 129 | |
| 130 | // Return address register. |
| 131 | if (is_x86_64) { |
| 132 | // R16(RIP) |
| 133 | cfi_info->push_back(0x10); |
| 134 | } else { |
| 135 | // R8(EIP) |
| 136 | cfi_info->push_back(0x08); |
| 137 | } |
| 138 | |
| 139 | // Augmentation length: 1. |
| 140 | cfi_info->push_back(1); |
| 141 | |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 142 | // Augmentation data. |
| 143 | if (is_x86_64) { |
| 144 | // 0x04 ((DW_EH_PE_absptr << 4) | DW_EH_PE_udata8). |
| 145 | cfi_info->push_back(0x04); |
| 146 | } else { |
| 147 | // 0x03 ((DW_EH_PE_absptr << 4) | DW_EH_PE_udata4). |
| 148 | cfi_info->push_back(0x03); |
| 149 | } |
Andreas Gampe | 7927380 | 2014-08-05 20:21:05 -0700 | [diff] [blame] | 150 | |
| 151 | // Initial instructions. |
| 152 | if (is_x86_64) { |
| 153 | // DW_CFA_def_cfa R7(RSP) 8. |
| 154 | cfi_info->push_back(0x0c); |
| 155 | cfi_info->push_back(0x07); |
| 156 | cfi_info->push_back(0x08); |
| 157 | |
| 158 | // DW_CFA_offset R16(RIP) 1 (* -8). |
| 159 | cfi_info->push_back(0x90); |
| 160 | cfi_info->push_back(0x01); |
| 161 | } else { |
| 162 | // DW_CFA_def_cfa R4(ESP) 4. |
| 163 | cfi_info->push_back(0x0c); |
| 164 | cfi_info->push_back(0x04); |
| 165 | cfi_info->push_back(0x04); |
| 166 | |
| 167 | // DW_CFA_offset R8(EIP) 1 (* -4). |
| 168 | cfi_info->push_back(0x88); |
| 169 | cfi_info->push_back(0x01); |
| 170 | } |
| 171 | |
| 172 | // Padding to a multiple of 4 |
| 173 | while ((cfi_info->size() & 3) != 0) { |
| 174 | // DW_CFA_nop is encoded as 0. |
| 175 | cfi_info->push_back(0); |
| 176 | } |
| 177 | |
| 178 | // Set the length of the CIE inside the generated bytes. |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 179 | if (is_x86_64) { |
| 180 | uint32_t length = cfi_info->size() - 12; |
| 181 | UpdateWord(cfi_info, 4, length); |
| 182 | } else { |
| 183 | uint32_t length = cfi_info->size() - 4; |
| 184 | UpdateWord(cfi_info, 0, length); |
| 185 | } |
Andreas Gampe | 7927380 | 2014-08-05 20:21:05 -0700 | [diff] [blame] | 186 | return cfi_info; |
| 187 | } |
| 188 | |
| 189 | std::vector<uint8_t>* ConstructCIEFrame(InstructionSet isa) { |
| 190 | switch (isa) { |
| 191 | case kX86: |
| 192 | return ConstructCIEFrameX86(false); |
| 193 | case kX86_64: |
| 194 | return ConstructCIEFrameX86(true); |
| 195 | |
| 196 | default: |
| 197 | // Not implemented. |
| 198 | return nullptr; |
| 199 | } |
| 200 | } |
| 201 | |
Ian Rogers | 0279ebb | 2014-10-08 17:27:48 -0700 | [diff] [blame] | 202 | class OatWriterWrapper FINAL : public CodeOutput { |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 203 | public: |
| 204 | explicit OatWriterWrapper(OatWriter* oat_writer) : oat_writer_(oat_writer) {} |
| 205 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 206 | void SetCodeOffset(size_t offset) { |
| 207 | oat_writer_->SetOatDataOffset(offset); |
| 208 | } |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 209 | bool Write(OutputStream* out) OVERRIDE { |
| 210 | return oat_writer_->Write(out); |
| 211 | } |
| 212 | private: |
Ian Rogers | 0279ebb | 2014-10-08 17:27:48 -0700 | [diff] [blame] | 213 | OatWriter* const oat_writer_; |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 214 | }; |
| 215 | |
| 216 | template <typename Elf_Word, typename Elf_Sword, typename Elf_Addr, |
| 217 | typename Elf_Dyn, typename Elf_Sym, typename Elf_Ehdr, |
| 218 | typename Elf_Phdr, typename Elf_Shdr> |
| 219 | static void WriteDebugSymbols(const CompilerDriver* compiler_driver, |
| 220 | ElfBuilder<Elf_Word, Elf_Sword, Elf_Addr, Elf_Dyn, |
| 221 | Elf_Sym, Elf_Ehdr, Elf_Phdr, Elf_Shdr>* builder, |
| 222 | OatWriter* oat_writer); |
| 223 | |
Nicolas Geoffray | f9b87b1 | 2014-09-02 08:12:09 +0000 | [diff] [blame] | 224 | template <typename Elf_Word, typename Elf_Sword, typename Elf_Addr, |
| 225 | typename Elf_Dyn, typename Elf_Sym, typename Elf_Ehdr, |
| 226 | typename Elf_Phdr, typename Elf_Shdr> |
| 227 | bool ElfWriterQuick<Elf_Word, Elf_Sword, Elf_Addr, Elf_Dyn, |
| 228 | Elf_Sym, Elf_Ehdr, Elf_Phdr, Elf_Shdr>::Write(OatWriter* oat_writer, |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 229 | const std::vector<const DexFile*>& dex_files_unused ATTRIBUTE_UNUSED, |
| 230 | const std::string& android_root_unused ATTRIBUTE_UNUSED, |
| 231 | bool is_host_unused ATTRIBUTE_UNUSED) { |
Andreas Gampe | 7927380 | 2014-08-05 20:21:05 -0700 | [diff] [blame] | 232 | constexpr bool debug = false; |
Brian Carlstrom | b12f347 | 2014-06-11 14:54:46 -0700 | [diff] [blame] | 233 | const OatHeader& oat_header = oat_writer->GetOatHeader(); |
Nicolas Geoffray | f9b87b1 | 2014-09-02 08:12:09 +0000 | [diff] [blame] | 234 | Elf_Word oat_data_size = oat_header.GetExecutableOffset(); |
Brian Carlstrom | b12f347 | 2014-06-11 14:54:46 -0700 | [diff] [blame] | 235 | uint32_t oat_exec_size = oat_writer->GetSize() - oat_data_size; |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 236 | uint32_t oat_bss_size = oat_writer->GetBssSize(); |
Brian Carlstrom | b12f347 | 2014-06-11 14:54:46 -0700 | [diff] [blame] | 237 | |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 238 | OatWriterWrapper wrapper(oat_writer); |
| 239 | |
| 240 | std::unique_ptr<ElfBuilder<Elf_Word, Elf_Sword, Elf_Addr, Elf_Dyn, |
| 241 | Elf_Sym, Elf_Ehdr, Elf_Phdr, Elf_Shdr> > builder( |
| 242 | new ElfBuilder<Elf_Word, Elf_Sword, Elf_Addr, Elf_Dyn, |
| 243 | Elf_Sym, Elf_Ehdr, Elf_Phdr, Elf_Shdr>( |
| 244 | &wrapper, |
| 245 | elf_file_, |
| 246 | compiler_driver_->GetInstructionSet(), |
| 247 | 0, |
| 248 | oat_data_size, |
| 249 | oat_data_size, |
| 250 | oat_exec_size, |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 251 | RoundUp(oat_data_size + oat_exec_size, kPageSize), |
| 252 | oat_bss_size, |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 253 | compiler_driver_->GetCompilerOptions().GetIncludeDebugSymbols(), |
| 254 | debug)); |
Alex Light | 78382fa | 2014-06-06 15:45:32 -0700 | [diff] [blame] | 255 | |
Brian Carlstrom | 18a49cc | 2014-08-29 16:20:48 -0700 | [diff] [blame] | 256 | if (!builder->Init()) { |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 257 | return false; |
| 258 | } |
| 259 | |
Andreas Gampe | 7927380 | 2014-08-05 20:21:05 -0700 | [diff] [blame] | 260 | if (compiler_driver_->GetCompilerOptions().GetIncludeDebugSymbols()) { |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 261 | WriteDebugSymbols(compiler_driver_, builder.get(), oat_writer); |
Brian Carlstrom | b12f347 | 2014-06-11 14:54:46 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 264 | if (compiler_driver_->GetCompilerOptions().GetIncludePatchInformation()) { |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 265 | ElfRawSectionBuilder<Elf_Word, Elf_Sword, Elf_Shdr> oat_patches( |
| 266 | ".oat_patches", SHT_OAT_PATCH, 0, NULL, 0, sizeof(uintptr_t), sizeof(uintptr_t)); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 267 | const std::vector<uintptr_t>& locations = oat_writer->GetAbsolutePatchLocations(); |
| 268 | const uint8_t* begin = reinterpret_cast<const uint8_t*>(&locations[0]); |
| 269 | const uint8_t* end = begin + locations.size() * sizeof(locations[0]); |
| 270 | oat_patches.GetBuffer()->assign(begin, end); |
| 271 | if (debug) { |
| 272 | LOG(INFO) << "Prepared .oat_patches for " << locations.size() << " patches."; |
| 273 | } |
Brian Carlstrom | 18a49cc | 2014-08-29 16:20:48 -0700 | [diff] [blame] | 274 | builder->RegisterRawSection(oat_patches); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 275 | } |
| 276 | |
Brian Carlstrom | 18a49cc | 2014-08-29 16:20:48 -0700 | [diff] [blame] | 277 | return builder->Write(); |
Brian Carlstrom | b12f347 | 2014-06-11 14:54:46 -0700 | [diff] [blame] | 278 | } |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 279 | |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 280 | // TODO: rewriting it using DexFile::DecodeDebugInfo needs unneeded stuff. |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 281 | static void GetLineInfoForJava(const uint8_t* dbgstream, DefaultSrcMap* dex2line) { |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 282 | if (dbgstream == nullptr) { |
| 283 | return; |
| 284 | } |
| 285 | |
| 286 | int adjopcode; |
| 287 | uint32_t dex_offset = 0; |
| 288 | uint32_t java_line = DecodeUnsignedLeb128(&dbgstream); |
| 289 | |
| 290 | // skip parameters |
| 291 | for (uint32_t param_count = DecodeUnsignedLeb128(&dbgstream); param_count != 0; --param_count) { |
| 292 | DecodeUnsignedLeb128(&dbgstream); |
| 293 | } |
| 294 | |
| 295 | for (bool is_end = false; is_end == false; ) { |
| 296 | uint8_t opcode = *dbgstream; |
| 297 | dbgstream++; |
| 298 | switch (opcode) { |
| 299 | case DexFile::DBG_END_SEQUENCE: |
| 300 | is_end = true; |
| 301 | break; |
| 302 | |
| 303 | case DexFile::DBG_ADVANCE_PC: |
| 304 | dex_offset += DecodeUnsignedLeb128(&dbgstream); |
| 305 | break; |
| 306 | |
| 307 | case DexFile::DBG_ADVANCE_LINE: |
| 308 | java_line += DecodeSignedLeb128(&dbgstream); |
| 309 | break; |
| 310 | |
| 311 | case DexFile::DBG_START_LOCAL: |
| 312 | case DexFile::DBG_START_LOCAL_EXTENDED: |
| 313 | DecodeUnsignedLeb128(&dbgstream); |
| 314 | DecodeUnsignedLeb128(&dbgstream); |
| 315 | DecodeUnsignedLeb128(&dbgstream); |
| 316 | |
| 317 | if (opcode == DexFile::DBG_START_LOCAL_EXTENDED) { |
| 318 | DecodeUnsignedLeb128(&dbgstream); |
| 319 | } |
| 320 | break; |
| 321 | |
| 322 | case DexFile::DBG_END_LOCAL: |
| 323 | case DexFile::DBG_RESTART_LOCAL: |
| 324 | DecodeUnsignedLeb128(&dbgstream); |
| 325 | break; |
| 326 | |
| 327 | case DexFile::DBG_SET_PROLOGUE_END: |
| 328 | case DexFile::DBG_SET_EPILOGUE_BEGIN: |
| 329 | case DexFile::DBG_SET_FILE: |
| 330 | break; |
| 331 | |
| 332 | default: |
| 333 | adjopcode = opcode - DexFile::DBG_FIRST_SPECIAL; |
| 334 | dex_offset += adjopcode / DexFile::DBG_LINE_RANGE; |
| 335 | java_line += DexFile::DBG_LINE_BASE + (adjopcode % DexFile::DBG_LINE_RANGE); |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 336 | dex2line->push_back({dex_offset, static_cast<int32_t>(java_line)}); |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 337 | break; |
| 338 | } |
Andreas Gampe | 7927380 | 2014-08-05 20:21:05 -0700 | [diff] [blame] | 339 | } |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 340 | } |
| 341 | |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 342 | /* |
| 343 | * @brief Generate the DWARF debug_info and debug_abbrev sections |
| 344 | * @param oat_writer The Oat file Writer. |
| 345 | * @param dbg_info Compilation unit information. |
| 346 | * @param dbg_abbrev Abbreviations used to generate dbg_info. |
| 347 | * @param dbg_str Debug strings. |
| 348 | */ |
| 349 | static void FillInCFIInformation(OatWriter* oat_writer, |
| 350 | std::vector<uint8_t>* dbg_info, |
| 351 | std::vector<uint8_t>* dbg_abbrev, |
| 352 | std::vector<uint8_t>* dbg_str, |
| 353 | std::vector<uint8_t>* dbg_line, |
| 354 | uint32_t text_section_offset) { |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 355 | const std::vector<OatWriter::DebugInfo>& method_infos = oat_writer->GetCFIMethodInfo(); |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 356 | |
| 357 | uint32_t producer_str_offset = PushStr(dbg_str, "Android dex2oat"); |
| 358 | |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 359 | constexpr bool use_64bit_addresses = false; |
| 360 | |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 361 | // Create the debug_abbrev section with boilerplate information. |
| 362 | // We only care about low_pc and high_pc right now for the compilation |
| 363 | // unit and methods. |
| 364 | |
| 365 | // Tag 1: Compilation unit: DW_TAG_compile_unit. |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 366 | PushByte(dbg_abbrev, 1); |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 367 | PushByte(dbg_abbrev, dwarf::DW_TAG_compile_unit); |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 368 | |
| 369 | // There are children (the methods). |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 370 | PushByte(dbg_abbrev, dwarf::DW_CHILDREN_yes); |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 371 | |
| 372 | // DW_AT_producer DW_FORM_data1. |
| 373 | // REVIEW: we can get rid of dbg_str section if |
| 374 | // DW_FORM_string (immediate string) was used everywhere instead of |
| 375 | // DW_FORM_strp (ref to string from .debug_str section). |
| 376 | // DW_FORM_strp makes sense only if we reuse the strings. |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 377 | PushByte(dbg_abbrev, dwarf::DW_AT_producer); |
| 378 | PushByte(dbg_abbrev, dwarf::DW_FORM_strp); |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 379 | |
| 380 | // DW_LANG_Java DW_FORM_data1. |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 381 | PushByte(dbg_abbrev, dwarf::DW_AT_language); |
| 382 | PushByte(dbg_abbrev, dwarf::DW_FORM_data1); |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 383 | |
| 384 | // DW_AT_low_pc DW_FORM_addr. |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 385 | PushByte(dbg_abbrev, dwarf::DW_AT_low_pc); |
| 386 | PushByte(dbg_abbrev, dwarf::DW_FORM_addr); |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 387 | |
| 388 | // DW_AT_high_pc DW_FORM_addr. |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 389 | PushByte(dbg_abbrev, dwarf::DW_AT_high_pc); |
| 390 | PushByte(dbg_abbrev, dwarf::DW_FORM_addr); |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 391 | |
| 392 | if (dbg_line != nullptr) { |
| 393 | // DW_AT_stmt_list DW_FORM_sec_offset. |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 394 | PushByte(dbg_abbrev, dwarf::DW_AT_stmt_list); |
| 395 | PushByte(dbg_abbrev, dwarf::DW_FORM_data4); |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 396 | } |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 397 | |
| 398 | // End of DW_TAG_compile_unit. |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 399 | PushByte(dbg_abbrev, 0); // DW_AT. |
| 400 | PushByte(dbg_abbrev, 0); // DW_FORM. |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 401 | |
| 402 | // Tag 2: Compilation unit: DW_TAG_subprogram. |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 403 | PushByte(dbg_abbrev, 2); |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 404 | PushByte(dbg_abbrev, dwarf::DW_TAG_subprogram); |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 405 | |
| 406 | // There are no children. |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 407 | PushByte(dbg_abbrev, dwarf::DW_CHILDREN_no); |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 408 | |
| 409 | // Name of the method. |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 410 | PushByte(dbg_abbrev, dwarf::DW_AT_name); |
| 411 | PushByte(dbg_abbrev, dwarf::DW_FORM_strp); |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 412 | |
| 413 | // DW_AT_low_pc DW_FORM_addr. |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 414 | PushByte(dbg_abbrev, dwarf::DW_AT_low_pc); |
| 415 | PushByte(dbg_abbrev, dwarf::DW_FORM_addr); |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 416 | |
| 417 | // DW_AT_high_pc DW_FORM_addr. |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 418 | PushByte(dbg_abbrev, dwarf::DW_AT_high_pc); |
| 419 | PushByte(dbg_abbrev, dwarf::DW_FORM_addr); |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 420 | |
| 421 | // End of DW_TAG_subprogram. |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 422 | PushByte(dbg_abbrev, 0); // DW_AT. |
| 423 | PushByte(dbg_abbrev, 0); // DW_FORM. |
| 424 | |
| 425 | // End of abbrevs for compilation unit |
| 426 | PushByte(dbg_abbrev, 0); |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 427 | |
| 428 | // Start the debug_info section with the header information |
| 429 | // 'unit_length' will be filled in later. |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 430 | int cunit_length = dbg_info->size(); |
Vladimir Marko | 80b96d1 | 2015-02-19 15:50:28 +0000 | [diff] [blame] | 431 | Push32(dbg_info, 0); |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 432 | |
| 433 | // 'version' - 3. |
| 434 | PushHalf(dbg_info, 3); |
| 435 | |
| 436 | // Offset into .debug_abbrev section (always 0). |
Vladimir Marko | 80b96d1 | 2015-02-19 15:50:28 +0000 | [diff] [blame] | 437 | Push32(dbg_info, 0); |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 438 | |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 439 | // Address size: 4 or 8. |
| 440 | PushByte(dbg_info, use_64bit_addresses ? 8 : 4); |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 441 | |
| 442 | // Start the description for the compilation unit. |
| 443 | // This uses tag 1. |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 444 | PushByte(dbg_info, 1); |
| 445 | |
| 446 | // The producer is Android dex2oat. |
Vladimir Marko | 80b96d1 | 2015-02-19 15:50:28 +0000 | [diff] [blame] | 447 | Push32(dbg_info, producer_str_offset); |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 448 | |
| 449 | // The language is Java. |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 450 | PushByte(dbg_info, dwarf::DW_LANG_Java); |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 451 | |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 452 | // low_pc and high_pc. |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 453 | uint32_t cunit_low_pc = static_cast<uint32_t>(-1); |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 454 | uint32_t cunit_high_pc = 0; |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 455 | for (auto method_info : method_infos) { |
| 456 | cunit_low_pc = std::min(cunit_low_pc, method_info.low_pc_); |
| 457 | cunit_high_pc = std::max(cunit_high_pc, method_info.high_pc_); |
| 458 | } |
| 459 | Push32(dbg_info, cunit_low_pc + text_section_offset); |
| 460 | Push32(dbg_info, cunit_high_pc + text_section_offset); |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 461 | |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 462 | if (dbg_line != nullptr) { |
| 463 | // Line number table offset. |
| 464 | Push32(dbg_info, dbg_line->size()); |
| 465 | } |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 466 | |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 467 | for (auto method_info : method_infos) { |
| 468 | // Start a new TAG: subroutine (2). |
| 469 | PushByte(dbg_info, 2); |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 470 | |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 471 | // Enter name, low_pc, high_pc. |
| 472 | Push32(dbg_info, PushStr(dbg_str, method_info.method_name_)); |
| 473 | Push32(dbg_info, method_info.low_pc_ + text_section_offset); |
| 474 | Push32(dbg_info, method_info.high_pc_ + text_section_offset); |
| 475 | } |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 476 | |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 477 | if (dbg_line != nullptr) { |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 478 | // TODO: in gdb info functions <regexp> - reports Java functions, but |
| 479 | // source file is <unknown> because .debug_line is formed as one |
| 480 | // compilation unit. To fix this it is possible to generate |
| 481 | // a separate compilation unit for every distinct Java source. |
| 482 | // Each of the these compilation units can have several non-adjacent |
| 483 | // method ranges. |
| 484 | |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 485 | std::vector<dwarf::DebugLineWriter<>::FileEntry> files; |
| 486 | std::unordered_map<std::string, size_t> files_map; |
| 487 | std::vector<std::string> directories; |
| 488 | std::unordered_map<std::string, size_t> directories_map; |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 489 | |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 490 | int code_factor_bits_ = 0; |
| 491 | int isa = -1; |
| 492 | switch (oat_writer->GetOatHeader().GetInstructionSet()) { |
David Srbecky | 189ae82 | 2015-04-04 10:15:17 +0100 | [diff] [blame^] | 493 | case kArm: // arm actually means thumb2. |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 494 | case kThumb2: |
| 495 | code_factor_bits_ = 1; // 16-bit instuctions |
| 496 | isa = 1; // DW_ISA_ARM_thumb. |
| 497 | break; |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 498 | case kArm64: |
| 499 | case kMips: |
| 500 | case kMips64: |
| 501 | code_factor_bits_ = 2; // 32-bit instructions |
| 502 | break; |
| 503 | case kNone: |
| 504 | case kX86: |
| 505 | case kX86_64: |
| 506 | break; |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 507 | } |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 508 | |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 509 | dwarf::DebugLineOpCodeWriter<> opcodes(use_64bit_addresses, code_factor_bits_); |
| 510 | opcodes.SetAddress(text_section_offset + cunit_low_pc); |
| 511 | if (isa != -1) { |
| 512 | opcodes.SetISA(isa); |
| 513 | } |
| 514 | DefaultSrcMap dex2line_map; |
| 515 | for (size_t i = 0; i < method_infos.size(); i++) { |
| 516 | const OatWriter::DebugInfo& method_info = method_infos[i]; |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 517 | |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 518 | // Addresses in the line table should be unique and increasing. |
| 519 | if (method_info.deduped_) { |
| 520 | continue; |
| 521 | } |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 522 | |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 523 | // Get and deduplicate directory and filename. |
| 524 | int file_index = 0; // 0 - primary source file of the compilation. |
| 525 | if (method_info.src_file_name_ != nullptr) { |
| 526 | std::string file_name(method_info.src_file_name_); |
| 527 | size_t file_name_slash = file_name.find_last_of('/'); |
| 528 | std::string class_name(method_info.class_descriptor_); |
| 529 | size_t class_name_slash = class_name.find_last_of('/'); |
| 530 | std::string full_path(file_name); |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 531 | |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 532 | // Guess directory from package name. |
| 533 | int directory_index = 0; // 0 - current directory of the compilation. |
| 534 | if (file_name_slash == std::string::npos && // Just filename. |
| 535 | class_name.front() == 'L' && // Type descriptor for a class. |
| 536 | class_name_slash != std::string::npos) { // Has package name. |
| 537 | std::string package_name = class_name.substr(1, class_name_slash - 1); |
| 538 | auto it = directories_map.find(package_name); |
| 539 | if (it == directories_map.end()) { |
| 540 | directory_index = 1 + directories.size(); |
| 541 | directories_map.emplace(package_name, directory_index); |
| 542 | directories.push_back(package_name); |
| 543 | } else { |
| 544 | directory_index = it->second; |
| 545 | } |
| 546 | full_path = package_name + "/" + file_name; |
Yevgeny Rouban | 33ac819 | 2014-08-19 18:39:57 +0700 | [diff] [blame] | 547 | } |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 548 | |
| 549 | // Add file entry. |
| 550 | auto it2 = files_map.find(full_path); |
| 551 | if (it2 == files_map.end()) { |
| 552 | file_index = 1 + files.size(); |
| 553 | files_map.emplace(full_path, file_index); |
| 554 | files.push_back(dwarf::DebugLineWriter<>::FileEntry { |
| 555 | file_name, |
| 556 | directory_index, |
| 557 | 0, // Modification time - NA. |
| 558 | 0, // File size - NA. |
| 559 | }); |
| 560 | } else { |
| 561 | file_index = it2->second; |
| 562 | } |
| 563 | } |
| 564 | opcodes.SetFile(file_index); |
| 565 | |
| 566 | // Generate mapping opcodes from PC to Java lines. |
| 567 | dex2line_map.clear(); |
| 568 | GetLineInfoForJava(method_info.dbgstream_, &dex2line_map); |
| 569 | uint32_t low_pc = text_section_offset + method_info.low_pc_; |
| 570 | if (file_index != 0 && !dex2line_map.empty()) { |
| 571 | bool first = true; |
| 572 | for (SrcMapElem pc2dex : method_info.compiled_method_->GetSrcMappingTable()) { |
| 573 | uint32_t pc = pc2dex.from_; |
| 574 | int dex = pc2dex.to_; |
| 575 | auto dex2line = dex2line_map.Find(static_cast<uint32_t>(dex)); |
| 576 | if (dex2line.first) { |
| 577 | int line = dex2line.second; |
| 578 | if (first) { |
| 579 | first = false; |
| 580 | if (pc > 0) { |
| 581 | // Assume that any preceding code is prologue. |
| 582 | int first_line = dex2line_map.front().to_; |
| 583 | // Prologue is not a sensible place for a breakpoint. |
| 584 | opcodes.NegateStmt(); |
| 585 | opcodes.AddRow(low_pc, first_line); |
| 586 | opcodes.NegateStmt(); |
| 587 | opcodes.SetPrologueEnd(); |
| 588 | } |
| 589 | opcodes.AddRow(low_pc + pc, line); |
| 590 | } else if (line != opcodes.CurrentLine()) { |
| 591 | opcodes.AddRow(low_pc + pc, line); |
| 592 | } |
| 593 | } |
| 594 | } |
| 595 | } else { |
| 596 | // line 0 - instruction cannot be attributed to any source line. |
| 597 | opcodes.AddRow(low_pc, 0); |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 598 | } |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 599 | } |
| 600 | |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 601 | opcodes.AdvancePC(text_section_offset + cunit_high_pc); |
| 602 | opcodes.EndSequence(); |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 603 | |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 604 | dwarf::DebugLineWriter<> dbg_line_writer(dbg_line); |
| 605 | dbg_line_writer.WriteTable(directories, files, opcodes); |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 606 | } |
| 607 | |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 608 | // One byte terminator. |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 609 | PushByte(dbg_info, 0); |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 610 | |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 611 | // We have now walked all the methods. Fill in lengths. |
| 612 | UpdateWord(dbg_info, cunit_length, dbg_info->size() - cunit_length - 4); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 613 | } |
| 614 | |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 615 | template <typename Elf_Word, typename Elf_Sword, typename Elf_Addr, |
| 616 | typename Elf_Dyn, typename Elf_Sym, typename Elf_Ehdr, |
| 617 | typename Elf_Phdr, typename Elf_Shdr> |
Andreas Gampe | 8683038 | 2014-12-12 21:41:29 -0800 | [diff] [blame] | 618 | // Do not inline to avoid Clang stack frame problems. b/18738594 |
| 619 | NO_INLINE |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 620 | static void WriteDebugSymbols(const CompilerDriver* compiler_driver, |
| 621 | ElfBuilder<Elf_Word, Elf_Sword, Elf_Addr, Elf_Dyn, |
| 622 | Elf_Sym, Elf_Ehdr, Elf_Phdr, Elf_Shdr>* builder, |
| 623 | OatWriter* oat_writer) { |
| 624 | std::unique_ptr<std::vector<uint8_t>> cfi_info( |
| 625 | ConstructCIEFrame(compiler_driver->GetInstructionSet())); |
| 626 | |
Ian Rogers | 0279ebb | 2014-10-08 17:27:48 -0700 | [diff] [blame] | 627 | Elf_Addr text_section_address = builder->GetTextBuilder().GetSection()->sh_addr; |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 628 | |
| 629 | // Iterate over the compiled methods. |
| 630 | const std::vector<OatWriter::DebugInfo>& method_info = oat_writer->GetCFIMethodInfo(); |
Ian Rogers | 0279ebb | 2014-10-08 17:27:48 -0700 | [diff] [blame] | 631 | ElfSymtabBuilder<Elf_Word, Elf_Sword, Elf_Addr, Elf_Sym, Elf_Shdr>* symtab = |
| 632 | builder->GetSymtabBuilder(); |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 633 | for (auto it = method_info.begin(); it != method_info.end(); ++it) { |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 634 | uint32_t low_pc = it->low_pc_; |
| 635 | // Add in code delta, e.g., thumb bit 0 for Thumb2 code. |
| 636 | low_pc += it->compiled_method_->CodeDelta(); |
| 637 | symtab->AddSymbol(it->method_name_, &builder->GetTextBuilder(), low_pc, |
| 638 | true, it->high_pc_ - it->low_pc_, STB_GLOBAL, STT_FUNC); |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 639 | |
Ningsheng Jian | f973455 | 2014-10-27 14:56:34 +0800 | [diff] [blame] | 640 | // Conforming to aaelf, add $t mapping symbol to indicate start of a sequence of thumb2 |
| 641 | // instructions, so that disassembler tools can correctly disassemble. |
| 642 | if (it->compiled_method_->GetInstructionSet() == kThumb2) { |
| 643 | symtab->AddSymbol("$t", &builder->GetTextBuilder(), it->low_pc_ & ~1, true, |
| 644 | 0, STB_LOCAL, STT_NOTYPE); |
| 645 | } |
| 646 | |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 647 | // Include CFI for compiled method, if possible. |
| 648 | if (cfi_info.get() != nullptr) { |
| 649 | DCHECK(it->compiled_method_ != nullptr); |
| 650 | |
| 651 | // Copy in the FDE, if present |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 652 | const SwapVector<uint8_t>* fde = it->compiled_method_->GetCFIInfo(); |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 653 | if (fde != nullptr) { |
| 654 | // Copy the information into cfi_info and then fix the address in the new copy. |
| 655 | int cur_offset = cfi_info->size(); |
| 656 | cfi_info->insert(cfi_info->end(), fde->begin(), fde->end()); |
| 657 | |
| 658 | bool is_64bit = *(reinterpret_cast<const uint32_t*>(fde->data())) == 0xffffffff; |
| 659 | |
| 660 | // Set the 'CIE_pointer' field. |
| 661 | uint64_t CIE_pointer = cur_offset + (is_64bit ? 12 : 4); |
| 662 | uint64_t offset_to_update = CIE_pointer; |
| 663 | if (is_64bit) { |
| 664 | (*cfi_info)[offset_to_update+0] = CIE_pointer; |
| 665 | (*cfi_info)[offset_to_update+1] = CIE_pointer >> 8; |
| 666 | (*cfi_info)[offset_to_update+2] = CIE_pointer >> 16; |
| 667 | (*cfi_info)[offset_to_update+3] = CIE_pointer >> 24; |
| 668 | (*cfi_info)[offset_to_update+4] = CIE_pointer >> 32; |
| 669 | (*cfi_info)[offset_to_update+5] = CIE_pointer >> 40; |
| 670 | (*cfi_info)[offset_to_update+6] = CIE_pointer >> 48; |
| 671 | (*cfi_info)[offset_to_update+7] = CIE_pointer >> 56; |
| 672 | } else { |
| 673 | (*cfi_info)[offset_to_update+0] = CIE_pointer; |
| 674 | (*cfi_info)[offset_to_update+1] = CIE_pointer >> 8; |
| 675 | (*cfi_info)[offset_to_update+2] = CIE_pointer >> 16; |
| 676 | (*cfi_info)[offset_to_update+3] = CIE_pointer >> 24; |
| 677 | } |
| 678 | |
| 679 | // Set the 'initial_location' field. |
| 680 | offset_to_update += is_64bit ? 8 : 4; |
| 681 | if (is_64bit) { |
| 682 | const uint64_t quick_code_start = it->low_pc_ + text_section_address; |
| 683 | (*cfi_info)[offset_to_update+0] = quick_code_start; |
| 684 | (*cfi_info)[offset_to_update+1] = quick_code_start >> 8; |
| 685 | (*cfi_info)[offset_to_update+2] = quick_code_start >> 16; |
| 686 | (*cfi_info)[offset_to_update+3] = quick_code_start >> 24; |
| 687 | (*cfi_info)[offset_to_update+4] = quick_code_start >> 32; |
| 688 | (*cfi_info)[offset_to_update+5] = quick_code_start >> 40; |
| 689 | (*cfi_info)[offset_to_update+6] = quick_code_start >> 48; |
| 690 | (*cfi_info)[offset_to_update+7] = quick_code_start >> 56; |
| 691 | } else { |
| 692 | const uint32_t quick_code_start = it->low_pc_ + text_section_address; |
| 693 | (*cfi_info)[offset_to_update+0] = quick_code_start; |
| 694 | (*cfi_info)[offset_to_update+1] = quick_code_start >> 8; |
| 695 | (*cfi_info)[offset_to_update+2] = quick_code_start >> 16; |
| 696 | (*cfi_info)[offset_to_update+3] = quick_code_start >> 24; |
| 697 | } |
| 698 | } |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | bool hasCFI = (cfi_info.get() != nullptr); |
| 703 | bool hasLineInfo = false; |
| 704 | for (auto& dbg_info : oat_writer->GetCFIMethodInfo()) { |
| 705 | if (dbg_info.dbgstream_ != nullptr && |
| 706 | !dbg_info.compiled_method_->GetSrcMappingTable().empty()) { |
| 707 | hasLineInfo = true; |
| 708 | break; |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | if (hasLineInfo || hasCFI) { |
| 713 | ElfRawSectionBuilder<Elf_Word, Elf_Sword, Elf_Shdr> debug_info(".debug_info", |
| 714 | SHT_PROGBITS, |
| 715 | 0, nullptr, 0, 1, 0); |
| 716 | ElfRawSectionBuilder<Elf_Word, Elf_Sword, Elf_Shdr> debug_abbrev(".debug_abbrev", |
| 717 | SHT_PROGBITS, |
| 718 | 0, nullptr, 0, 1, 0); |
| 719 | ElfRawSectionBuilder<Elf_Word, Elf_Sword, Elf_Shdr> debug_str(".debug_str", |
| 720 | SHT_PROGBITS, |
| 721 | 0, nullptr, 0, 1, 0); |
| 722 | ElfRawSectionBuilder<Elf_Word, Elf_Sword, Elf_Shdr> debug_line(".debug_line", |
| 723 | SHT_PROGBITS, |
| 724 | 0, nullptr, 0, 1, 0); |
| 725 | |
| 726 | FillInCFIInformation(oat_writer, debug_info.GetBuffer(), |
| 727 | debug_abbrev.GetBuffer(), debug_str.GetBuffer(), |
| 728 | hasLineInfo ? debug_line.GetBuffer() : nullptr, |
| 729 | text_section_address); |
| 730 | |
| 731 | builder->RegisterRawSection(debug_info); |
| 732 | builder->RegisterRawSection(debug_abbrev); |
| 733 | |
| 734 | if (hasCFI) { |
| 735 | ElfRawSectionBuilder<Elf_Word, Elf_Sword, Elf_Shdr> eh_frame(".eh_frame", |
| 736 | SHT_PROGBITS, |
| 737 | SHF_ALLOC, |
| 738 | nullptr, 0, 4, 0); |
| 739 | eh_frame.SetBuffer(std::move(*cfi_info.get())); |
| 740 | builder->RegisterRawSection(eh_frame); |
| 741 | } |
| 742 | |
| 743 | if (hasLineInfo) { |
| 744 | builder->RegisterRawSection(debug_line); |
| 745 | } |
| 746 | |
| 747 | builder->RegisterRawSection(debug_str); |
| 748 | } |
| 749 | } |
| 750 | |
Nicolas Geoffray | f9b87b1 | 2014-09-02 08:12:09 +0000 | [diff] [blame] | 751 | // Explicit instantiations |
| 752 | template class ElfWriterQuick<Elf32_Word, Elf32_Sword, Elf32_Addr, Elf32_Dyn, |
| 753 | Elf32_Sym, Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr>; |
| 754 | template class ElfWriterQuick<Elf64_Word, Elf64_Sword, Elf64_Addr, Elf64_Dyn, |
| 755 | Elf64_Sym, Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr>; |
| 756 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 757 | } // namespace art |