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