Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "src/eh-frame.h" | 5 #include "src/eh-frame.h" |
| 6 #include "src/objects-inl.h" | 6 |
| 7 #include "src/objects.h" | 7 #include <iomanip> |
| 8 #include <ostream> | |
| 9 | |
| 10 #if !defined(V8_TARGET_ARCH_X64) && !defined(V8_TARGET_ARCH_ARM) && \ | |
| 11 !defined(V8_TARGET_ARCH_ARM64) | |
| 12 | |
| 13 // Placeholders for unsupported architectures. | |
| 8 | 14 |
| 9 namespace v8 { | 15 namespace v8 { |
| 10 namespace internal { | 16 namespace internal { |
| 11 | 17 |
| 12 static const int DW_EH_PE_pcrel = 0x10; | 18 STATIC_CONST_MEMBER_DEFINITION const int |
| 13 static const int DW_EH_PE_datarel = 0x30; | 19 EhFrameConstants::kDataAlignmentFactor = 1; |
| 14 static const int DW_EH_PE_udata4 = 0x03; | 20 |
| 15 static const int DW_EH_PE_sdata4 = 0x0b; | 21 void EhFrameWriter::WriteReturnAddressRegisterCode() { UNIMPLEMENTED(); } |
| 16 | 22 |
| 17 const int EhFrameHdr::kCIESize = 0; | 23 void EhFrameWriter::WriteInitialStateInCIE() { UNIMPLEMENTED(); } |
| 18 | 24 |
| 19 static const int kVersionSize = 1; | 25 int EhFrameWriter::RegisterToDwarfCode(Register) { |
| 20 static const int kEncodingSpecifiersSize = 3; | 26 UNIMPLEMENTED(); |
| 21 | 27 return -1; |
| 22 // | 28 } |
| 23 // In order to calculate offsets in the .eh_frame_hdr, we must know the layout | 29 |
| 24 // of the DSO generated by perf inject, which is assumed to be the following: | 30 #ifdef ENABLE_DISASSEMBLER |
| 25 // | 31 |
| 26 // | ... | | | 32 const char* EhFrameDisassembler::DwarfRegisterCodeToString(int) { |
| 27 // +---------------+ <-- (F) --- | Larger offsets in file | 33 UNIMPLEMENTED(); |
| 28 // | | ^ | | 34 return nullptr; |
| 29 // | Instructions | | .text v | 35 } |
| 30 // | | v | 36 |
| 31 // +---------------+ <-- (E) --- | 37 #endif |
| 32 // |///////////////| | |
| 33 // |////Padding////| | |
| 34 // |///////////////| | |
| 35 // +---------------+ <-- (D) --- | |
| 36 // | | ^ | |
| 37 // | CIE | | | |
| 38 // | | | | |
| 39 // +---------------+ <-- (C) | .eh_frame | |
| 40 // | | | | |
| 41 // | FDE | | | |
| 42 // | | v | |
| 43 // +---------------+ <-- (B) --- | |
| 44 // | version | ^ | |
| 45 // +---------------+ | | |
| 46 // | encoding | | | |
| 47 // | specifiers | | | |
| 48 // +---------------+ <---(A) | .eh_frame_hdr | |
| 49 // | offset to | | | |
| 50 // | .eh_frame | | | |
| 51 // +---------------+ | | |
| 52 // | ... | ... | |
| 53 // | |
| 54 // (F) is aligned at a 16-byte boundary. | |
| 55 // (D) is aligned at a 8-byte boundary. | |
| 56 // (B) is aligned at a 4-byte boundary. | |
| 57 // (E), (C) and (A) have no alignment requirements. | |
| 58 // | |
| 59 // The distance between (A) and (B) is 4 bytes. | |
| 60 // | |
| 61 // The size of the .eh_frame is required to be a multiple of the pointer size, | |
| 62 // which means that (B) will be naturally aligned to a 4-byte boundary on all | |
| 63 // the architectures we support. | |
| 64 // | |
| 65 // Because (E) has no alignment requirements, there is padding between (E) and | |
| 66 // (D). (F) is aligned at a 16-byte boundary, thus to a 8-byte one as well. | |
| 67 // | |
| 68 EhFrameHdr::EhFrameHdr(Code* code) { | |
| 69 int code_size = code->is_crankshafted() ? code->safepoint_table_offset() | |
| 70 : code->instruction_size(); | |
| 71 version_ = 1; | |
| 72 eh_frame_ptr_encoding_ = DW_EH_PE_sdata4 | DW_EH_PE_pcrel; | |
| 73 lut_size_encoding_ = DW_EH_PE_udata4; | |
| 74 lut_entries_encoding_ = DW_EH_PE_sdata4 | DW_EH_PE_datarel; | |
| 75 | |
| 76 // .eh_frame pointer and LUT | |
| 77 if (code->has_unwinding_info()) { | |
| 78 DCHECK_GE(code->unwinding_info_size(), EhFrameHdr::kRecordSize); | |
| 79 int eh_frame_size = code->unwinding_info_size() - EhFrameHdr::kRecordSize; | |
| 80 | |
| 81 offset_to_eh_frame_ = | |
| 82 -(eh_frame_size + kVersionSize + kEncodingSpecifiersSize); // A -> D | |
| 83 lut_entries_number_ = 1; | |
| 84 offset_to_procedure_ = -(RoundUp(code_size, 8) + eh_frame_size); // B -> F | |
| 85 offset_to_fde_ = -(eh_frame_size - kCIESize); // B -> C | |
| 86 } else { | |
| 87 // Create a dummy table | |
| 88 offset_to_eh_frame_ = 0; | |
| 89 lut_entries_number_ = 0; | |
| 90 offset_to_procedure_ = 0; | |
| 91 offset_to_fde_ = 0; | |
| 92 } | |
| 93 } | |
| 94 | 38 |
| 95 } // namespace internal | 39 } // namespace internal |
| 96 } // namespace v8 | 40 } // namespace v8 |
| 41 | |
| 42 #endif | |
| 43 | |
| 44 namespace v8 { | |
| 45 namespace internal { | |
| 46 | |
| 47 STATIC_CONST_MEMBER_DEFINITION const int | |
| 48 EhFrameConstants::kEhFrameTerminatorSize; | |
| 49 | |
| 50 STATIC_CONST_MEMBER_DEFINITION const int EhFrameConstants::kEhFrameHdrVersion; | |
| 51 | |
| 52 // The dummy eh_frame_hdr is a hack to trigger fp-based unwinding in Linux | |
| 53 // perf compiled with libunwind support when collecting DWARF-based call graphs. | |
| 54 // | |
| 55 // kDummyEhFrame is effectively a valid header with an empty look up table. | |
| 56 // | |
| 57 STATIC_CONST_MEMBER_DEFINITION const byte EhFrameConstants::kDummyEhFrame[] = { | |
| 58 kEhFrameHdrVersion, | |
| 59 // .eh_frame pointer encoding specifier. | |
| 60 kSData4 | kPcRel, | |
| 61 // LUT size encoding. | |
| 62 kUData4, | |
| 63 // LUT entries encoding. | |
| 64 kSData4 | kDataRel, | |
| 65 // Dummy pointers and 0 entries in the LUT. | |
| 66 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; | |
| 67 | |
| 68 STATIC_CONST_MEMBER_DEFINITION const uint32_t EhFrameWriter::kInt32Placeholder; | |
| 69 | |
| 70 EhFrameWriter::EhFrameWriter(Zone* zone) | |
| 71 : cie_size_(0), | |
| 72 last_pc_offset_(0), | |
| 73 eh_frame_finalised_(false), | |
| 74 base_register_(no_reg), | |
| 75 base_offset_(0), | |
| 76 eh_frame_buffer_(zone) { | |
| 77 eh_frame_buffer_.reserve(128); | |
| 78 WriteCIE(); | |
| 79 WriteFDEHeader(); | |
| 80 } | |
| 81 | |
| 82 void EhFrameWriter::WriteCIE() { | |
| 83 static const int kCIEIdentifier = 0; | |
| 84 static const int kCIEVersion = 3; | |
| 85 static const int kCodeAlignmentFactor = 1; | |
| 86 static const int kAugmentationDataSize = 2; | |
| 87 static const byte kAugmentationString[] = {'z', 'L', 'R', 0}; | |
| 88 | |
| 89 int size_offset = eh_frame_offset(); | |
| 90 WriteInt32(kInt32Placeholder); | |
| 91 | |
| 92 int record_start_offset = eh_frame_offset(); | |
| 93 WriteInt32(kCIEIdentifier); | |
| 94 WriteByte(kCIEVersion); | |
| 95 | |
| 96 WriteBytes(&kAugmentationString[0], sizeof(kAugmentationString)); | |
| 97 | |
| 98 WriteSLEB128(kCodeAlignmentFactor); | |
| 99 WriteSLEB128(EhFrameConstants::kDataAlignmentFactor); | |
| 100 | |
| 101 WriteReturnAddressRegisterCode(); | |
| 102 | |
| 103 WriteByte(kAugmentationDataSize); | |
| 104 WriteByte(EhFrameConstants::kOmit); | |
| 105 WriteByte(EhFrameConstants::kSData4 | EhFrameConstants::kPcRel); | |
| 106 | |
| 107 DCHECK_EQ(eh_frame_offset() - size_offset, | |
| 108 EhFrameConstants::kInitialStateOffsetInCIE); | |
| 109 WriteInitialStateInCIE(); | |
| 110 | |
| 111 WritePaddingTo8ByteAlignment(); | |
| 112 | |
| 113 int record_end_offset = eh_frame_offset(); | |
| 114 int encoded_cie_size = record_end_offset - record_start_offset; | |
| 115 cie_size_ = record_end_offset - size_offset; | |
|
rmcilroy
2016/07/07 10:11:03
Some comments here like WriteFDEHeader
Stefano Sanfilippo
2016/07/07 10:57:00
Done.
| |
| 116 | |
| 117 PatchInt32(size_offset, encoded_cie_size); | |
| 118 } | |
| 119 | |
| 120 void EhFrameWriter::WriteFDEHeader() { | |
| 121 DCHECK_NE(cie_size_, 0); | |
| 122 | |
| 123 // Placeholder for size of the FDE. | |
| 124 DCHECK_EQ(eh_frame_offset(), fde_offset()); | |
| 125 WriteInt32(kInt32Placeholder); | |
| 126 | |
| 127 // Backwards offset to the CIE. | |
| 128 WriteInt32(cie_size_ + kInt32Size); | |
| 129 | |
| 130 // Placeholder for pointer to procedure. | |
| 131 DCHECK_EQ(eh_frame_offset(), GetProcedureAddressOffset()); | |
| 132 WriteInt32(kInt32Placeholder); | |
| 133 | |
| 134 // Placeholder for size of the procedure. | |
| 135 DCHECK_EQ(eh_frame_offset(), GetProcedureSizeOffset()); | |
| 136 WriteInt32(kInt32Placeholder); | |
| 137 | |
| 138 // No augmentation data. | |
| 139 WriteByte(0); | |
| 140 } | |
| 141 | |
| 142 void EhFrameWriter::WriteEhFrameHdr(int code_size) { | |
| 143 DCHECK(!eh_frame_finalised_); | |
| 144 | |
| 145 // | |
| 146 // In order to calculate offsets in the .eh_frame_hdr, we must know the layout | |
| 147 // of the DSO generated by perf inject, which is assumed to be the following: | |
| 148 // | |
| 149 // | ... | | | |
| 150 // +---------------+ <-- (F) --- | Larger offsets in file | |
| 151 // | | ^ | | |
| 152 // | Instructions | | .text v | |
| 153 // | | v | |
| 154 // +---------------+ <-- (E) --- | |
| 155 // |///////////////| | |
| 156 // |////Padding////| | |
| 157 // |///////////////| | |
| 158 // +---------------+ <-- (D) --- | |
| 159 // | | ^ | |
| 160 // | CIE | | | |
| 161 // | | | | |
| 162 // +---------------+ <-- (C) | | |
| 163 // | | | .eh_frame | |
| 164 // | FDE | | | |
| 165 // | | | | |
| 166 // +---------------+ | | |
| 167 // | terminator | v | |
| 168 // +---------------+ <-- (B) --- | |
| 169 // | version | ^ | |
| 170 // +---------------+ | | |
| 171 // | encoding | | | |
| 172 // | specifiers | | | |
| 173 // +---------------+ <---(A) | .eh_frame_hdr | |
| 174 // | offset to | | | |
| 175 // | .eh_frame | | | |
| 176 // +---------------+ | | |
| 177 // | ... | ... | |
| 178 // | |
| 179 // (F) is aligned to a 16-byte boundary. | |
| 180 // (D) is aligned to a 8-byte boundary. | |
| 181 // (B) is aligned to a 4-byte boundary. | |
| 182 // (C) is aligned to an addressing unit size boundary. | |
| 183 // (E) and (A) have no alignment requirements. | |
| 184 // | |
| 185 // The distance between (A) and (B) is 4 bytes. | |
| 186 // | |
| 187 // The size of the FDE is required to be a multiple of the pointer size, which | |
| 188 // means that (B) will be naturally aligned to a 4-byte boundary on all the | |
| 189 // architectures we support. | |
| 190 // | |
| 191 // Because (E) has no alignment requirements, there is padding between (E) and | |
| 192 // (D). (F) is aligned at a 16-byte boundary, thus to a 8-byte one as well. | |
| 193 // | |
| 194 | |
| 195 int eh_frame_size = eh_frame_offset(); | |
| 196 | |
| 197 WriteByte(EhFrameConstants::kEhFrameHdrVersion); | |
| 198 | |
| 199 // .eh_frame pointer encoding specifier. | |
| 200 WriteByte(EhFrameConstants::kSData4 | EhFrameConstants::kPcRel); | |
| 201 // LUT size encoding specifier. | |
| 202 WriteByte(EhFrameConstants::kUData4); | |
| 203 // LUT entries encoding specifier. | |
| 204 WriteByte(EhFrameConstants::kSData4 | EhFrameConstants::kDataRel); | |
| 205 | |
| 206 // Pointer to .eh_frame, relative to this offset (A -> D in the diagram). | |
| 207 WriteInt32(-(eh_frame_size + EhFrameConstants::kFdeVersionSize + | |
| 208 EhFrameConstants::kFdeEncodingSpecifiersSize)); | |
| 209 | |
| 210 // Number of entries in the LUT, one for the only routine. | |
| 211 WriteInt32(1); | |
| 212 | |
| 213 // Pointer to the start of the routine, relative to the beginning of the | |
| 214 // .eh_frame_hdr (B -> F in the diagram). | |
| 215 WriteInt32(-(RoundUp(code_size, 8) + eh_frame_size)); | |
| 216 | |
| 217 // Pointer to the start of the associated FDE, relative to the start of the | |
| 218 // .eh_frame_hdr (B -> C in the diagram). | |
| 219 WriteInt32(-(eh_frame_size - cie_size_)); | |
| 220 | |
| 221 DCHECK_EQ(eh_frame_offset() - eh_frame_size, | |
| 222 EhFrameConstants::kEhFrameHdrSize); | |
| 223 } | |
| 224 | |
| 225 void EhFrameWriter::WritePaddingTo8ByteAlignment() { | |
| 226 DCHECK(!eh_frame_finalised_); | |
| 227 | |
| 228 int unpadded_size = eh_frame_offset(); | |
| 229 int padded_size = RoundUp(unpadded_size, 8); | |
| 230 int padding_size = padded_size - unpadded_size; | |
| 231 | |
| 232 byte nop = static_cast<byte>(EhFrameConstants::DwarfOpcodes::kNop); | |
| 233 static const byte kPadding[] = {nop, nop, nop, nop, nop, nop, nop, nop}; | |
| 234 DCHECK_LE(padding_size, static_cast<int>(sizeof(kPadding))); | |
| 235 WriteBytes(&kPadding[0], padding_size); | |
| 236 } | |
| 237 | |
| 238 void EhFrameWriter::AdvanceLocation(int pc_offset) { | |
| 239 DCHECK(!eh_frame_finalised_); | |
| 240 DCHECK_GE(pc_offset, last_pc_offset_); | |
| 241 uint32_t delta = pc_offset - last_pc_offset_; | |
| 242 | |
| 243 if (delta <= EhFrameConstants::kLocationMask) { | |
| 244 WriteByte((EhFrameConstants::kLocationTag | |
| 245 << EhFrameConstants::kLocationMaskSize) | | |
| 246 (delta & EhFrameConstants::kLocationMask)); | |
| 247 } else if (delta <= kMaxUInt8) { | |
| 248 WriteOpcode(EhFrameConstants::DwarfOpcodes::kAdvanceLoc1); | |
| 249 WriteByte(delta); | |
| 250 } else if (delta <= kMaxUInt16) { | |
| 251 WriteOpcode(EhFrameConstants::DwarfOpcodes::kAdvanceLoc2); | |
| 252 WriteInt16(delta); | |
| 253 } else { | |
| 254 WriteOpcode(EhFrameConstants::DwarfOpcodes::kAdvanceLoc4); | |
| 255 WriteInt32(delta); | |
| 256 } | |
| 257 | |
| 258 last_pc_offset_ = pc_offset; | |
| 259 } | |
| 260 | |
| 261 void EhFrameWriter::SetBaseAddressOffset(int base_offset) { | |
| 262 DCHECK(!eh_frame_finalised_); | |
| 263 DCHECK_GE(base_offset, 0); | |
| 264 WriteOpcode(EhFrameConstants::DwarfOpcodes::kDefCfaOffset); | |
| 265 WriteULEB128(base_offset); | |
| 266 base_offset_ = base_offset; | |
| 267 } | |
| 268 | |
| 269 void EhFrameWriter::SetBaseAddressRegister(Register base_register) { | |
| 270 DCHECK(!eh_frame_finalised_); | |
| 271 int code = RegisterToDwarfCode(base_register); | |
| 272 WriteOpcode(EhFrameConstants::DwarfOpcodes::kDefCfaRegister); | |
| 273 WriteULEB128(code); | |
| 274 base_register_ = base_register; | |
| 275 } | |
| 276 | |
| 277 void EhFrameWriter::SetBaseAddressRegisterAndOffset(Register base_register, | |
| 278 int base_offset) { | |
| 279 DCHECK(!eh_frame_finalised_); | |
| 280 int code = RegisterToDwarfCode(base_register); | |
| 281 WriteOpcode(EhFrameConstants::DwarfOpcodes::kDefCfa); | |
| 282 WriteULEB128(code); | |
| 283 WriteULEB128(base_offset); | |
| 284 base_offset_ = base_offset; | |
| 285 base_register_ = base_register; | |
| 286 } | |
| 287 | |
| 288 void EhFrameWriter::RecordRegisterSavedToStack(int register_code, int offset) { | |
| 289 DCHECK(!eh_frame_finalised_); | |
| 290 DCHECK_EQ(offset % EhFrameConstants::kDataAlignmentFactor, 0); | |
| 291 int factored_offset = offset / EhFrameConstants::kDataAlignmentFactor; | |
| 292 if (factored_offset >= 0) { | |
| 293 DCHECK_LE(register_code, EhFrameConstants::kSavedRegisterMask); | |
| 294 WriteByte((EhFrameConstants::kSavedRegisterTag | |
| 295 << EhFrameConstants::kSavedRegisterMaskSize) | | |
| 296 (register_code & EhFrameConstants::kSavedRegisterMask)); | |
| 297 WriteULEB128(factored_offset); | |
| 298 } else { | |
| 299 WriteOpcode(EhFrameConstants::DwarfOpcodes::kOffsetExtendedSf); | |
| 300 WriteULEB128(register_code); | |
| 301 WriteSLEB128(factored_offset); | |
| 302 } | |
| 303 } | |
| 304 | |
| 305 void EhFrameWriter::RecordRegisterIsValid(Register name) { | |
| 306 DCHECK(!eh_frame_finalised_); | |
| 307 WriteOpcode(EhFrameConstants::DwarfOpcodes::kSameValue); | |
| 308 WriteULEB128(RegisterToDwarfCode(name)); | |
| 309 } | |
| 310 | |
| 311 void EhFrameWriter::RecordRegisterFollowsInitialRule(Register name) { | |
| 312 DCHECK(!eh_frame_finalised_); | |
| 313 int code = RegisterToDwarfCode(name); | |
| 314 DCHECK_LE(code, EhFrameConstants::kFollowInitialRuleMask); | |
| 315 WriteByte((EhFrameConstants::kFollowInitialRuleTag | |
| 316 << EhFrameConstants::kFollowInitialRuleMaskSize) | | |
| 317 (code & EhFrameConstants::kFollowInitialRuleMask)); | |
| 318 } | |
| 319 | |
| 320 void EhFrameWriter::Finish(int code_size) { | |
| 321 DCHECK(!eh_frame_finalised_); | |
| 322 DCHECK_GE(eh_frame_offset(), cie_size_); | |
| 323 | |
| 324 WritePaddingTo8ByteAlignment(); | |
| 325 | |
| 326 // Write the size of the FDE now that we know it. | |
| 327 // The encoded size does not include the size field itself. | |
| 328 int encoded_fde_size = eh_frame_offset() - fde_offset() - kInt32Size; | |
| 329 PatchInt32(fde_offset(), encoded_fde_size); | |
| 330 | |
| 331 // Write the size and offset to procedure. | |
| 332 PatchInt32(GetProcedureAddressOffset(), | |
| 333 -(RoundUp(code_size, 8) + GetProcedureAddressOffset())); | |
| 334 PatchInt32(GetProcedureSizeOffset(), code_size); | |
| 335 | |
| 336 // Terminate the .eh_frame. | |
| 337 static const byte kTerminator[EhFrameConstants::kEhFrameTerminatorSize] = {0}; | |
| 338 WriteBytes(&kTerminator[0], EhFrameConstants::kEhFrameTerminatorSize); | |
| 339 | |
| 340 WriteEhFrameHdr(code_size); | |
| 341 | |
| 342 eh_frame_finalised_ = true; | |
| 343 } | |
| 344 | |
| 345 void EhFrameWriter::GetEhFrame(CodeDesc* desc) { | |
| 346 DCHECK(eh_frame_finalised_); | |
| 347 desc->unwinding_info_size = static_cast<int>(eh_frame_buffer_.size()); | |
| 348 desc->unwinding_info = eh_frame_buffer_.data(); | |
| 349 } | |
| 350 | |
| 351 void EhFrameWriter::WriteULEB128(uint32_t value) { | |
| 352 do { | |
| 353 byte chunk = value & 0x7f; | |
| 354 value >>= 7; | |
| 355 if (value != 0) chunk |= 0x80; | |
| 356 WriteByte(chunk); | |
| 357 } while (value != 0); | |
| 358 } | |
| 359 | |
| 360 void EhFrameWriter::WriteSLEB128(int32_t value) { | |
| 361 static const int kSignBitMask = 0x40; | |
| 362 bool done; | |
| 363 do { | |
| 364 byte chunk = value & 0x7f; | |
| 365 value >>= 7; | |
| 366 done = ((value == 0) && ((chunk & kSignBitMask) == 0)) || | |
| 367 ((value == -1) && ((chunk & kSignBitMask) != 0)); | |
| 368 if (!done) chunk |= 0x80; | |
| 369 WriteByte(chunk); | |
| 370 } while (!done); | |
| 371 } | |
| 372 | |
| 373 uint32_t EhFrameIterator::GetNextULEB128() { | |
| 374 int size = 0; | |
| 375 uint32_t result = DecodeULEB128(next_, &size); | |
| 376 DCHECK_LE(next_ + size, end_); | |
| 377 next_ += size; | |
| 378 return result; | |
| 379 } | |
| 380 | |
| 381 int32_t EhFrameIterator::GetNextSLEB128() { | |
| 382 int size = 0; | |
| 383 int32_t result = DecodeSLEB128(next_, &size); | |
| 384 DCHECK_LE(next_ + size, end_); | |
| 385 next_ += size; | |
| 386 return result; | |
| 387 } | |
| 388 | |
| 389 // static | |
| 390 uint32_t EhFrameIterator::DecodeULEB128(const byte* encoded, | |
| 391 int* encoded_size) { | |
| 392 const byte* current = encoded; | |
| 393 uint32_t result = 0; | |
| 394 int shift = 0; | |
| 395 | |
| 396 do { | |
| 397 DCHECK_LT(shift, 8 * static_cast<int>(sizeof(result))); | |
| 398 result |= (*current & 0x7f) << shift; | |
| 399 shift += 7; | |
| 400 } while (*current++ >= 128); | |
| 401 | |
| 402 DCHECK_NOT_NULL(encoded_size); | |
| 403 *encoded_size = static_cast<int>(current - encoded); | |
| 404 | |
| 405 return result; | |
| 406 } | |
| 407 | |
| 408 // static | |
| 409 int32_t EhFrameIterator::DecodeSLEB128(const byte* encoded, int* encoded_size) { | |
| 410 static const byte kSignBitMask = 0x40; | |
| 411 | |
| 412 const byte* current = encoded; | |
| 413 int32_t result = 0; | |
| 414 int shift = 0; | |
| 415 byte chunk; | |
| 416 | |
| 417 do { | |
| 418 chunk = *current++; | |
| 419 DCHECK_LT(shift, 8 * static_cast<int>(sizeof(result))); | |
| 420 result |= (chunk & 0x7f) << shift; | |
| 421 shift += 7; | |
| 422 } while (chunk >= 128); | |
| 423 | |
| 424 // Sign extend the result if the last chunk has the sign bit set. | |
| 425 if (chunk & kSignBitMask) result |= (~0ull) << shift; | |
| 426 | |
| 427 DCHECK_NOT_NULL(encoded_size); | |
| 428 *encoded_size = static_cast<int>(current - encoded); | |
| 429 | |
| 430 return result; | |
| 431 } | |
| 432 | |
| 433 #ifdef ENABLE_DISASSEMBLER | |
| 434 | |
| 435 namespace { | |
| 436 | |
| 437 class StreamModifiersScope final { | |
| 438 public: | |
| 439 explicit StreamModifiersScope(std::ostream* stream) | |
| 440 : stream_(stream), flags_(stream->flags()) {} | |
| 441 ~StreamModifiersScope() { stream_->flags(flags_); } | |
| 442 | |
| 443 private: | |
| 444 std::ostream* stream_; | |
| 445 std::ios::fmtflags flags_; | |
| 446 }; | |
| 447 | |
| 448 } // namespace | |
| 449 | |
| 450 // static | |
| 451 void EhFrameDisassembler::DumpDWARFDirectives(std::ostream& stream, // NOLINT | |
| 452 const byte* start, | |
| 453 const byte* end) { | |
| 454 StreamModifiersScope modifiers_scope(&stream); | |
| 455 | |
| 456 EhFrameIterator eh_frame_iterator(start, end); | |
| 457 uint32_t offset_in_procedure = 0; | |
| 458 | |
| 459 while (!eh_frame_iterator.Done()) { | |
| 460 stream << eh_frame_iterator.current_address() << " "; | |
| 461 | |
| 462 byte bytecode = eh_frame_iterator.GetNextByte(); | |
| 463 | |
| 464 if (((bytecode >> EhFrameConstants::kLocationMaskSize) & 0xff) == | |
| 465 EhFrameConstants::kLocationTag) { | |
| 466 int value = bytecode & EhFrameConstants::kLocationMask; | |
| 467 offset_in_procedure += value; | |
| 468 stream << "| pc_offset=" << std::dec << offset_in_procedure | |
| 469 << " (delta=0x" << std::hex << value << ")\n"; | |
| 470 continue; | |
| 471 } | |
| 472 | |
| 473 if (((bytecode >> EhFrameConstants::kSavedRegisterMaskSize) & 0xff) == | |
| 474 EhFrameConstants::kSavedRegisterTag) { | |
| 475 int decoded_offset = static_cast<int>(eh_frame_iterator.GetNextULEB128()); | |
| 476 stream << "| " << DwarfRegisterCodeToString( | |
| 477 bytecode & EhFrameConstants::kLocationMask) | |
| 478 << " saved at base" << std::showpos << std::dec | |
| 479 << decoded_offset * EhFrameConstants::kDataAlignmentFactor << '\n'; | |
| 480 continue; | |
| 481 } | |
| 482 | |
| 483 if (((bytecode >> EhFrameConstants::kFollowInitialRuleMaskSize) & 0xff) == | |
| 484 EhFrameConstants::kFollowInitialRuleTag) { | |
| 485 stream << "| " << DwarfRegisterCodeToString( | |
| 486 bytecode & EhFrameConstants::kLocationMask) | |
| 487 << " follows initial rule\n"; | |
| 488 continue; | |
| 489 } | |
| 490 | |
| 491 switch (static_cast<EhFrameConstants::DwarfOpcodes>(bytecode)) { | |
| 492 case EhFrameConstants::DwarfOpcodes::kOffsetExtendedSf: { | |
| 493 stream << "| " | |
| 494 << DwarfRegisterCodeToString(eh_frame_iterator.GetNextULEB128()); | |
| 495 int32_t decoded_offset = eh_frame_iterator.GetNextSLEB128(); | |
| 496 stream << " saved at base" << std::showpos << std::dec | |
| 497 << decoded_offset * EhFrameConstants::kDataAlignmentFactor | |
| 498 << '\n'; | |
| 499 } | |
| 500 case EhFrameConstants::DwarfOpcodes::kAdvanceLoc1: { | |
| 501 unsigned value = eh_frame_iterator.GetNextByte(); | |
| 502 offset_in_procedure += value; | |
| 503 stream << "| pc_offset=" << std::dec << offset_in_procedure | |
| 504 << " (delta=0x" << std::hex << value << ")\n"; | |
| 505 break; | |
| 506 } | |
| 507 case EhFrameConstants::DwarfOpcodes::kAdvanceLoc2: { | |
| 508 uint16_t value = eh_frame_iterator.GetNextUInt16(); | |
| 509 offset_in_procedure += value; | |
| 510 stream << "| pc_offset=" << std::dec << offset_in_procedure | |
| 511 << " (delta=0x" << std::hex << value << ")\n"; | |
| 512 break; | |
| 513 } | |
| 514 case EhFrameConstants::DwarfOpcodes::kAdvanceLoc4: { | |
| 515 uint32_t value = eh_frame_iterator.GetNextUInt32(); | |
| 516 offset_in_procedure += value; | |
| 517 stream << "| pc_offset=" << std::dec << offset_in_procedure | |
| 518 << " (delta=0x" << std::hex << value << ")\n"; | |
| 519 break; | |
| 520 } | |
| 521 case EhFrameConstants::DwarfOpcodes::kDefCfa: { | |
| 522 int base_register = eh_frame_iterator.GetNextULEB128(); | |
| 523 int base_offset = eh_frame_iterator.GetNextULEB128(); | |
| 524 stream << "| base_register=" << DwarfRegisterCodeToString(base_register) | |
| 525 << ", base_offset=0x" << std::hex << base_offset << '\n'; | |
| 526 break; | |
| 527 } | |
| 528 case EhFrameConstants::DwarfOpcodes::kDefCfaOffset: { | |
| 529 stream << "| base_offset=0x" << std::hex | |
| 530 << eh_frame_iterator.GetNextULEB128() << '\n'; | |
| 531 break; | |
| 532 } | |
| 533 case EhFrameConstants::DwarfOpcodes::kDefCfaRegister: { | |
| 534 stream << "| base_register=" | |
| 535 << DwarfRegisterCodeToString(eh_frame_iterator.GetNextULEB128()) | |
| 536 << '\n'; | |
| 537 break; | |
| 538 } | |
| 539 case EhFrameConstants::DwarfOpcodes::kSameValue: { | |
| 540 stream << "| " | |
| 541 << DwarfRegisterCodeToString(eh_frame_iterator.GetNextULEB128()) | |
| 542 << " to initial value\n"; | |
| 543 break; | |
| 544 } | |
| 545 case EhFrameConstants::DwarfOpcodes::kNop: | |
| 546 stream << "| nop\n"; | |
| 547 break; | |
| 548 default: | |
| 549 UNREACHABLE(); | |
| 550 return; | |
| 551 } | |
| 552 } | |
| 553 } | |
| 554 | |
| 555 // static | |
| 556 void EhFrameDisassembler::DisassembleToStream(std::ostream& stream) { // NOLINT | |
| 557 // The encoded CIE size does not include the size field itself. | |
| 558 const int cie_size = ReadUnalignedUInt32(start_) + kInt32Size; | |
| 559 const int fde_offset = cie_size; | |
| 560 | |
| 561 const byte* cie_directives_start = | |
| 562 start_ + EhFrameConstants::kInitialStateOffsetInCIE; | |
| 563 const byte* cie_directives_end = start_ + cie_size; | |
| 564 DCHECK_LE(cie_directives_start, cie_directives_end); | |
| 565 | |
| 566 stream << reinterpret_cast<const void*>(start_) << " .eh_frame: CIE\n"; | |
| 567 DumpDWARFDirectives(stream, cie_directives_start, cie_directives_end); | |
| 568 | |
| 569 const byte* procedure_offset_address = | |
| 570 start_ + fde_offset + EhFrameConstants::kProcedureAddressOffsetInFde; | |
| 571 int32_t procedure_offset = | |
| 572 ReadUnalignedValue<int32_t>(procedure_offset_address); | |
| 573 | |
| 574 const byte* procedure_size_address = | |
| 575 start_ + fde_offset + EhFrameConstants::kProcedureSizeOffsetInFde; | |
| 576 uint32_t procedure_size = ReadUnalignedUInt32(procedure_size_address); | |
| 577 | |
| 578 const byte* fde_start = start_ + fde_offset; | |
| 579 stream << reinterpret_cast<const void*>(fde_start) << " .eh_frame: FDE\n" | |
| 580 << reinterpret_cast<const void*>(procedure_offset_address) | |
| 581 << " | procedure_offset=" << procedure_offset << '\n' | |
| 582 << reinterpret_cast<const void*>(procedure_size_address) | |
| 583 << " | procedure_size=" << procedure_size << '\n'; | |
| 584 | |
| 585 const int fde_directives_offset = fde_offset + 4 * kInt32Size + 1; | |
| 586 | |
| 587 const byte* fde_directives_start = start_ + fde_directives_offset; | |
| 588 const byte* fde_directives_end = end_ - EhFrameConstants::kEhFrameHdrSize - | |
| 589 EhFrameConstants::kEhFrameTerminatorSize; | |
| 590 DCHECK_LE(fde_directives_start, fde_directives_end); | |
| 591 | |
| 592 DumpDWARFDirectives(stream, fde_directives_start, fde_directives_end); | |
| 593 | |
| 594 const byte* fde_terminator_start = fde_directives_end; | |
| 595 stream << reinterpret_cast<const void*>(fde_terminator_start) | |
| 596 << " .eh_frame: terminator\n"; | |
| 597 | |
| 598 const byte* eh_frame_hdr_start = | |
| 599 fde_terminator_start + EhFrameConstants::kEhFrameTerminatorSize; | |
| 600 stream << reinterpret_cast<const void*>(eh_frame_hdr_start) | |
| 601 << " .eh_frame_hdr\n"; | |
| 602 } | |
| 603 | |
| 604 #endif | |
| 605 | |
| 606 } // namespace internal | |
| 607 } // namespace v8 | |
| OLD | NEW |