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 #if !defined(V8_TARGET_ARCH_X64) && !defined(V8_TARGET_ARCH_ARM) && \
11     !defined(V8_TARGET_ARCH_ARM64)
12 
13 // Placeholders for unsupported architectures.
14 
15 namespace v8 {
16 namespace internal {
17 
18 const int EhFrameConstants::kCodeAlignmentFactor = 1;
19 const int EhFrameConstants::kDataAlignmentFactor = 1;
20 
WriteReturnAddressRegisterCode()21 void EhFrameWriter::WriteReturnAddressRegisterCode() { UNIMPLEMENTED(); }
22 
WriteInitialStateInCie()23 void EhFrameWriter::WriteInitialStateInCie() { UNIMPLEMENTED(); }
24 
RegisterToDwarfCode(Register)25 int EhFrameWriter::RegisterToDwarfCode(Register) {
26   UNIMPLEMENTED();
27   return -1;
28 }
29 
30 #ifdef ENABLE_DISASSEMBLER
31 
DwarfRegisterCodeToString(int)32 const char* EhFrameDisassembler::DwarfRegisterCodeToString(int) {
33   UNIMPLEMENTED();
34   return nullptr;
35 }
36 
37 #endif
38 
39 }  // namespace internal
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
WriteEmptyEhFrame(std::ostream & stream)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 
EhFrameWriter(Zone * zone)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 
Initialize()80 void EhFrameWriter::Initialize() {
81   DCHECK_EQ(writer_state_, InternalState::kUndefined);
82   eh_frame_buffer_.reserve(128);
83   writer_state_ = InternalState::kInitialized;
84   WriteCie();
85   WriteFdeHeader();
86 }
87 
WriteCie()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 
WriteFdeHeader()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 
WriteEhFrameHdr(int code_size)156 void EhFrameWriter::WriteEhFrameHdr(int code_size) {
157   DCHECK_EQ(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 
WritePaddingToAlignedSize(int unpadded_size)238 void EhFrameWriter::WritePaddingToAlignedSize(int unpadded_size) {
239   DCHECK_EQ(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 
AdvanceLocation(int pc_offset)250 void EhFrameWriter::AdvanceLocation(int pc_offset) {
251   DCHECK_EQ(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, 0u);
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 
SetBaseAddressOffset(int base_offset)276 void EhFrameWriter::SetBaseAddressOffset(int base_offset) {
277   DCHECK_EQ(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 
SetBaseAddressRegister(Register base_register)284 void EhFrameWriter::SetBaseAddressRegister(Register base_register) {
285   DCHECK_EQ(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 
SetBaseAddressRegisterAndOffset(Register base_register,int base_offset)292 void EhFrameWriter::SetBaseAddressRegisterAndOffset(Register base_register,
293                                                     int base_offset) {
294   DCHECK_EQ(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 
RecordRegisterSavedToStack(int register_code,int offset)304 void EhFrameWriter::RecordRegisterSavedToStack(int register_code, int offset) {
305   DCHECK_EQ(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 
RecordRegisterNotModified(Register name)321 void EhFrameWriter::RecordRegisterNotModified(Register name) {
322   DCHECK_EQ(writer_state_, InternalState::kInitialized);
323   WriteOpcode(EhFrameConstants::DwarfOpcodes::kSameValue);
324   WriteULeb128(RegisterToDwarfCode(name));
325 }
326 
RecordRegisterFollowsInitialRule(Register name)327 void EhFrameWriter::RecordRegisterFollowsInitialRule(Register name) {
328   DCHECK_EQ(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 
Finish(int code_size)336 void EhFrameWriter::Finish(int code_size) {
337   DCHECK_EQ(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 
GetEhFrame(CodeDesc * desc)362 void EhFrameWriter::GetEhFrame(CodeDesc* desc) {
363   DCHECK_EQ(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 
WriteULeb128(uint32_t value)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 
WriteSLeb128(int32_t value)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 
GetNextULeb128()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 
GetNextSLeb128()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
DecodeULeb128(const byte * encoded,int * encoded_size)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
DecodeSLeb128(const byte * encoded,int * encoded_size)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:
StreamModifiersScope(std::ostream * stream)456   explicit StreamModifiersScope(std::ostream* stream)
457       : stream_(stream), flags_(stream->flags()) {}
~StreamModifiersScope()458   ~StreamModifiersScope() { stream_->flags(flags_); }
459 
460  private:
461   std::ostream* stream_;
462   std::ios::fmtflags flags_;
463 };
464 
465 }  // namespace
466 
467 // static
DumpDwarfDirectives(std::ostream & stream,const byte * start,const byte * end)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 
DisassembleToStream(std::ostream & stream)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 =
581       ReadUnalignedUInt32(reinterpret_cast<Address>(start_)) + kInt32Size;
582   const int fde_offset = cie_size;
583 
584   const byte* cie_directives_start =
585       start_ + EhFrameConstants::kInitialStateOffsetInCie;
586   const byte* cie_directives_end = start_ + cie_size;
587   DCHECK_LE(cie_directives_start, cie_directives_end);
588 
589   stream << reinterpret_cast<const void*>(start_) << "  .eh_frame: CIE\n";
590   DumpDwarfDirectives(stream, cie_directives_start, cie_directives_end);
591 
592   Address procedure_offset_address =
593       reinterpret_cast<Address>(start_) + fde_offset +
594       EhFrameConstants::kProcedureAddressOffsetInFde;
595   int32_t procedure_offset =
596       ReadUnalignedValue<int32_t>(procedure_offset_address);
597 
598   Address procedure_size_address = reinterpret_cast<Address>(start_) +
599                                    fde_offset +
600                                    EhFrameConstants::kProcedureSizeOffsetInFde;
601   uint32_t procedure_size = ReadUnalignedUInt32(procedure_size_address);
602 
603   const byte* fde_start = start_ + fde_offset;
604   stream << reinterpret_cast<const void*>(fde_start) << "  .eh_frame: FDE\n"
605          << reinterpret_cast<const void*>(procedure_offset_address)
606          << "  | procedure_offset=" << procedure_offset << '\n'
607          << reinterpret_cast<const void*>(procedure_size_address)
608          << "  | procedure_size=" << procedure_size << '\n';
609 
610   const int fde_directives_offset = fde_offset + 4 * kInt32Size + 1;
611 
612   const byte* fde_directives_start = start_ + fde_directives_offset;
613   const byte* fde_directives_end = end_ - EhFrameConstants::kEhFrameHdrSize -
614                                    EhFrameConstants::kEhFrameTerminatorSize;
615   DCHECK_LE(fde_directives_start, fde_directives_end);
616 
617   DumpDwarfDirectives(stream, fde_directives_start, fde_directives_end);
618 
619   const byte* fde_terminator_start = fde_directives_end;
620   stream << reinterpret_cast<const void*>(fde_terminator_start)
621          << "  .eh_frame: terminator\n";
622 
623   const byte* eh_frame_hdr_start =
624       fde_terminator_start + EhFrameConstants::kEhFrameTerminatorSize;
625   stream << reinterpret_cast<const void*>(eh_frame_hdr_start)
626          << "  .eh_frame_hdr\n";
627 }
628 
629 #endif
630 
631 }  // namespace internal
632 }  // namespace v8
633