// File: /workspace/github_c_makefile_star2_O3/buptlxb[P]xRay/src/binary.cpp
// Function: BinInfo::BinInfo(char const*)
BinInfo::BinInfo(const char *file_name):file_name(file_name), mapStart(NULL)
{
    fprintf(stderr, "Start processing %s\n", file_name);
    // 1.open file
    int fd = open(file_name, O_RDONLY);
    assert(fd!=-1);
    // 2.calculate elf size
    struct stat statbuf;
    assert(fstat(fd, &statbuf)==0);
    elf_size = statbuf.st_size;
    // 3.mmap elf
    mapStart = mmap(NULL, elf_size, PROT_READ, MAP_PRIVATE, fd, 0);
    if(mapStart==MAP_FAILED) {
        perror("Map ELF: ");
        abort();
    }
    // 4.isElf
    assert(isElf(mapStart));
    // 5.judgeIsSharedObject
    Elf64_Ehdr *elf_header = (Elf64_Ehdr*)mapStart;
    Elf64_Half elf_type = elf_header->e_type;
    if(elf_type==ET_EXEC)
        isSharedObjectFile = false;
    else if(elf_type==ET_DYN)
        isSharedObjectFile = true;
    else{
        cerr<<"xRay can only handle execute file and shared object file!"<<endl;
        abort();
    }

    Elf64_Half section_num = elf_header->e_shnum;
    Elf64_Shdr *section_table = (Elf64_Shdr*)((ADDR)mapStart+elf_header->e_shoff);
    char *shstrtab = (char *)((ADDR)mapStart + section_table[elf_header->e_shstrndx].sh_offset);
    char *strtab = NULL;
    for(int i=0; i<section_num; i++) {
        if (!strcmp(shstrtab + section_table[i].sh_name, ".strtab")) {
            strtab = (char *)((ADDR)mapStart + section_table[i].sh_offset);
            //printf("0x%lx\n", section_table[i].sh_offset);
        }
    }
    for(int i=0; i<section_num; i++) {
        if (section_table[i].sh_type != SHT_SYMTAB)
            continue;
        Elf64_Sym *sym_table = (Elf64_Sym *)((ADDR)mapStart+section_table[i].sh_offset);
        Elf64_Xword sym_num = section_table[i].sh_size / section_table[i].sh_entsize;

        for (int i = 0; i < sym_num; i++) {
            if (ELF64_ST_TYPE(sym_table[i].st_info) != STT_FUNC)
                continue;
            if (sym_table[i].st_shndx == SHN_UNDEF || sym_table[i].st_shndx > elf_header->e_shnum)
                continue;
            if (sym_table[i].st_size == 0)
                continue;
            Elf64_Shdr *reside_section = section_table + sym_table[i].st_shndx;
            assert(reside_section->sh_flags & SHF_EXECINSTR);

            ADDR funcVirStart = sym_table[i].st_value;
            ADDR funcMapStart = funcVirStart - reside_section->sh_addr + reside_section->sh_offset + (ADDR)mapStart;
            ADDR funcMapEnd = funcMapStart + sym_table[i].st_size;
            char *funcName = strtab + sym_table[i].st_name;
            //puts(strtab + sym_table[i].st_name);
            Function func(funcMapStart, funcMapEnd, funcVirStart, funcName);
            funcInfo.push_back(func);
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x328, %rsp            # imm = 0x328
movq %rsi, %r15
movq %rdi, %rbx
leaq 0xc0(%rsp), %rdx
callq 0x9552
xorl %eax, %eax
movq %rax, 0x28(%rbx)
leaq 0x38(%rbx), %rcx
movq %rcx, 0x18(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x38(%rbx)
movq %rbx, 0x8(%rsp)
movq %rax, 0x48(%rbx)
movq 0x2ee0e(%rip), %rax     # 0x37fd0
movq (%rax), %rdi
leaq 0x23f83(%rip), %rsi     # 0x2d14f
movq %r15, %rdx
xorl %eax, %eax
callq 0x81f0
movq %r15, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x8100
movl %eax, %ebp
cmpl $-0x1, %eax
je 0x93fc
leaq 0x30(%rsp), %rsi
movl %ebp, %edi
callq 0x8260
testl %eax, %eax
jne 0x941b
movq 0x60(%rsp), %rsi
movq 0x8(%rsp), %rbx
movq %rsi, 0x30(%rbx)
xorl %edi, %edi
movl $0x1, %edx
movl $0x2, %ecx
movl %ebp, %r8d
xorl %r9d, %r9d
callq 0x8280
movq %rax, 0x28(%rbx)
cmpq $-0x1, %rax
je 0x943a
movq %rax, %r15
cmpl $0x464c457f, (%rax)     # imm = 0x464C457F
jne 0x943f
movzwl 0x10(%r15), %eax
cmpl $0x2, %eax
je 0x925a
cmpl $0x3, %eax
jne 0x945e
movb $0x1, %al
jmp 0x925c
xorl %eax, %eax
movq 0x8(%rsp), %rcx
movb %al, 0x50(%rcx)
movzwl 0x3c(%r15), %eax
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x93cb
movq 0x28(%r15), %r14
addq %r15, %r14
movzwl 0x3e(%r15), %eax
shll $0x6, %eax
movq 0x18(%rax,%r14), %rbx
addq %r15, %rbx
movq 0x10(%rsp), %r13
shll $0x6, %r13d
xorl %ebp, %ebp
leaq 0x23f64(%rip), %r12     # 0x2d204
movq $0x0, 0x20(%rsp)
movl (%r14,%rbp), %edi
addq %rbx, %rdi
movq %r12, %rsi
callq 0x81e0
testl %eax, %eax
jne 0x92c9
movq 0x18(%r14,%rbp), %rax
addq %r15, %rax
movq %rax, 0x20(%rsp)
addq $0x40, %rbp
cmpq %rbp, %r13
jne 0x92a9
cmpq $0x0, 0x10(%rsp)
je 0x93cb
xorl %ebp, %ebp
leaq 0xc0(%rsp), %rdi
movq %r15, 0x28(%rsp)
movq %rbp, %rsi
shlq $0x6, %rsi
cmpl $0x2, 0x4(%r14,%rsi)
jne 0x93bd
addq %r14, %rsi
movq 0x20(%rsi), %rcx
movq 0x38(%rsi), %r8
movq %rcx, %rax
xorl %edx, %edx
divq %r8
cmpq %rcx, %r8
ja 0x93bd
movq %rax, %r13
movq 0x18(%rsi), %r12
movq 0x8(%rsp), %rax
addq 0x28(%rax), %r12
xorl %ebx, %ebx
movb 0x4(%r12), %al
andb $0xf, %al
cmpb $0x2, %al
jne 0x93ad
movzwl 0x6(%r12), %eax
testq %rax, %rax
je 0x93ad
cmpw 0x3c(%r15), %ax
ja 0x93ad
movq 0x10(%r12), %rdx
testq %rdx, %rdx
je 0x93ad
shll $0x6, %eax
testb $0x4, 0x8(%r14,%rax)
je 0x93dd
addq %r14, %rax
movq 0x8(%r12), %rcx
movq %rcx, %rsi
subq 0x10(%rax), %rsi
addq 0x18(%rax), %rsi
movq 0x8(%rsp), %rax
addq 0x28(%rax), %rsi
addq %rsi, %rdx
movl (%r12), %r8d
addq 0x20(%rsp), %r8
movq %rdi, %r15
callq 0x993c
movq 0x18(%rsp), %rdi
movq %r15, %rsi
callq 0x9590
movq %r15, %rdi
callq 0x99cc
movq %r15, %rdi
movq 0x28(%rsp), %r15
incq %rbx
addq $0x18, %r12
cmpq %rbx, %r13
ja 0x932e
incq %rbp
cmpq 0x10(%rsp), %rbp
jne 0x92ed
addq $0x328, %rsp            # imm = 0x328
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x23e28(%rip), %rdi     # 0x2d20c
leaq 0x23d80(%rip), %rsi     # 0x2d16b
leaq 0x23d88(%rip), %rcx     # 0x2d17a
movl $0x44, %edx
callq 0x80e0
leaq 0x23d61(%rip), %rdi     # 0x2d164
leaq 0x23d61(%rip), %rsi     # 0x2d16b
leaq 0x23d69(%rip), %rcx     # 0x2d17a
movl $0x13, %edx
callq 0x80e0
leaq 0x23d77(%rip), %rdi     # 0x2d199
leaq 0x23d42(%rip), %rsi     # 0x2d16b
leaq 0x23d4a(%rip), %rcx     # 0x2d17a
movl $0x16, %edx
callq 0x80e0
callq 0x83f6
leaq 0x23d74(%rip), %rdi     # 0x2d1ba
leaq 0x23d1e(%rip), %rsi     # 0x2d16b
leaq 0x23d26(%rip), %rcx     # 0x2d17a
movl $0x1f, %edx
callq 0x80e0
movq 0x2eb8b(%rip), %rdi     # 0x37ff0
leaq 0x23d5e(%rip), %rsi     # 0x2d1ca
movl $0x39, %edx
callq 0x81a0
movq 0x2eb73(%rip), %rdi     # 0x37ff0
callq 0x80a0
callq 0x80d0
jmp 0x948b
jmp 0x948b
movq %rax, %r15
jmp 0x94a0
movq %rax, %r15
leaq 0xc0(%rsp), %rdi
callq 0x99cc
movq 0x18(%rsp), %rdi
callq 0x95c0
movq 0x8(%rsp), %rcx
movq (%rcx), %rdi
addq $0x10, %rcx
cmpq %rcx, %rdi
je 0x94c6
movq (%rcx), %rsi
incq %rsi
callq 0x8170
movq %r15, %rdi
callq 0x8270