// File: /workspace/github_c_makefile_star2_O0/shuffle2[P]nx2elf/elf_eh.cpp
// Function: ElfEHInfo::MeasureFrame(eh_frame_hdr const*, unsigned long*, unsigned long*)
bool ElfEHInfo::MeasureFrame(const eh_frame_hdr* hdr,
                             uintptr_t* eh_frame_ptr,
                             size_t* eh_frame_len) {
  if (hdr->version != 1) {
    return false;
  }
#define READ_RAW(type, ptr) \
  *(type*)ptr;              \
  ptr += sizeof(type);
  auto dw_decode = [&hdr](u8 enc, const u8*& buf, uintptr_t base = 0) {
    uintptr_t val = 0;
    if (base == 0) {
      base = (uintptr_t)buf;
    }
    if (enc == DW_EH_PE_omit) {
      // XXX hack for "nonexistant" value
      return (uintptr_t)0;
    }
    switch (enc & 0x70) {
    case DW_EH_PE_absptr:
      break;
    case DW_EH_PE_pcrel:
      val += base;
      break;
    case DW_EH_PE_datarel:
      val += (uintptr_t)hdr;
      break;
    default:
      fprintf(stderr, "unexpected enc base %02x\n", enc);
      break;
    }
    switch (enc & 0x0f) {
    case DW_EH_PE_udata2:
      val += READ_RAW(u16, buf);
      break;
    case DW_EH_PE_udata4:
      val += READ_RAW(u32, buf);
      break;
    case DW_EH_PE_sdata4:
      val += READ_RAW(s32, buf);
      break;
    default:
      fprintf(stderr, "unexpected enc type %02x\n", enc);
      break;
    }
    if (enc & DW_EH_PE_indirect) {
      val = *(uintptr_t*)val;
    }
    return val;
  };
  auto dw_fde_len = [](u32* fde_len, const u8* buf) {
    u32 len = READ_RAW(u32, buf);
    if (len == 0xffffffff) {
      return false;
    }
    *fde_len = len;
    return true;
  };
#undef READ_RAW

  const u8* ptr = (const u8*)&hdr[1];
  *eh_frame_ptr = dw_decode(hdr->eh_frame_ptr_enc, ptr);
  // XXX hack: default to len(eh_frame) == 8 if fde_count == 0
  *eh_frame_len = 8;
  size_t fde_count = dw_decode(hdr->fde_count_enc, ptr);
  uintptr_t max_ptr = 0;
  for (size_t i = 0; i < fde_count; i++) {
    auto func = dw_decode(hdr->table_enc, ptr);
    auto desc = dw_decode(hdr->table_enc, ptr);
    u32 fde_len;
    if (!dw_fde_len(&fde_len, (const u8*)desc)) {
      fprintf(stderr, "reading fde %zi failed\n", i);
      continue;
    }
    max_ptr = std::max(max_ptr, desc + fde_len);
  }
  if (max_ptr) {
    *eh_frame_len = max_ptr - *eh_frame_ptr;
  }
  return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x18(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x1, %eax
je 0x24f0
movb $0x0, -0x1(%rbp)
jmp 0x2622
leaq -0x18(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x40(%rbp)
movq -0x18(%rbp), %rax
leaq -0x30(%rbp), %rdi
leaq -0x40(%rbp), %rdx
xorl %ecx, %ecx
movzbl 0x1(%rax), %esi
callq 0x2630
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
movq -0x28(%rbp), %rax
movq $0x8, (%rax)
movq -0x18(%rbp), %rax
leaq -0x30(%rbp), %rdi
leaq -0x40(%rbp), %rdx
xorl %ecx, %ecx
movzbl 0x2(%rax), %esi
callq 0x2630
movq %rax, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x58(%rbp), %rax
cmpq -0x48(%rbp), %rax
jae 0x2605
movq -0x18(%rbp), %rax
leaq -0x30(%rbp), %rdi
leaq -0x40(%rbp), %rdx
xorl %ecx, %ecx
movzbl 0x3(%rax), %esi
callq 0x2630
movq %rax, -0x60(%rbp)
movq -0x18(%rbp), %rax
leaq -0x30(%rbp), %rdi
leaq -0x40(%rbp), %rdx
xorl %ecx, %ecx
movzbl 0x3(%rax), %esi
callq 0x2630
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rdx
leaq -0x31(%rbp), %rdi
leaq -0x6c(%rbp), %rsi
callq 0x27c0
testb $0x1, %al
jne 0x25d2
movq 0x70a25(%rip), %rax     # 0x72fe0
movq (%rax), %rdi
movq -0x58(%rbp), %rdx
leaq 0x65a3b(%rip), %rsi     # 0x68004
movb $0x0, %al
callq 0x22e0
jmp 0x25f4
movq -0x68(%rbp), %rax
movl -0x6c(%rbp), %ecx
addq %rcx, %rax
movq %rax, -0x78(%rbp)
leaq -0x50(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x2810
movq (%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x255b
cmpq $0x0, -0x50(%rbp)
je 0x261e
movq -0x50(%rbp), %rcx
movq -0x20(%rbp), %rax
subq (%rax), %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x80, %rsp
popq %rbp
retq