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