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