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