// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, -0x38(%rbp)
movb (%rsi), %al
cmpb $0x1, %al
jne 0x24f0
movq %rcx, %r14
movq %rdx, %r13
movb %al, -0x29(%rbp)
leaq -0x38(%rbp), %rbx
leaq -0x58(%rbp), %r15
movq %rbx, (%r15)
leaq 0x4(%rsi), %rax
leaq -0x50(%rbp), %r12
movq %rax, (%r12)
movzbl 0x1(%rsi), %esi
movq %r15, %rdi
movq %r12, %rdx
callq 0x2504
movq %r13, -0x40(%rbp)
movq %rax, (%r13)
movq %r14, -0x48(%rbp)
movq $0x8, (%r14)
movq (%rbx), %rax
movzbl 0x2(%rax), %esi
movq %r15, %rdi
movq %r12, %rdx
callq 0x2504
movq %rax, %r13
xorl %r14d, %r14d
xorl %ebx, %ebx
cmpq %rbx, %r13
je 0x24da
movq -0x38(%rbp), %rax
movzbl 0x3(%rax), %esi
movq %r15, %rdi
movq %r12, %rdx
callq 0x2504
movq -0x38(%rbp), %rax
movzbl 0x3(%rax), %esi
movq %r15, %rdi
movq %r12, %rdx
callq 0x2504
movl (%rax), %ecx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
cmpq %rdx, %rcx
je 0x24bd
addq %rcx, %rax
cmpq %rax, %r14
cmovbeq %rax, %r14
incq %rbx
jmp 0x2477
movq 0x14b14(%rip), %rax     # 0x16fd8
movq (%rax), %rdi
leaq 0xfb36(%rip), %rsi      # 0x12004
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2260
jmp 0x24b8
testq %r14, %r14
je 0x24ed
movq -0x40(%rbp), %rax
subq (%rax), %r14
movq -0x48(%rbp), %rax
movq %r14, (%rax)
movb -0x29(%rbp), %al
cmpb $0x1, %al
sete %al
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq