// File: /workspace/github_c_makefile_star2_O1/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 0x95e2
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 0x2ed8e(%rip), %rax     # 0x37fd0
movq (%rax), %rdi
leaq 0x23f03(%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 0x948c
leaq 0x30(%rsp), %rsi
movl %ebp, %edi
callq 0x8260
testl %eax, %eax
jne 0x94ab
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 0x94ca
movq %rax, %r14
cmpl $0x464c457f, (%rax)     # imm = 0x464C457F
jne 0x94cf
movzwl 0x10(%r14), %eax
cmpl $0x2, %eax
je 0x92da
cmpl $0x3, %eax
jne 0x94ee
movb $0x1, %al
jmp 0x92dc
xorl %eax, %eax
movq 0x8(%rsp), %rcx
movb %al, 0x50(%rcx)
movzwl 0x3c(%r14), %r13d
movq 0x28(%r14), %r10
addq %r14, %r10
testq %r13, %r13
movq %r13, 0x20(%rsp)
movq %r10, 0x28(%rsp)
je 0x9355
movzwl 0x3e(%r14), %eax
shll $0x6, %eax
movq 0x18(%rax,%r10), %rbx
addq %r14, %rbx
shll $0x6, %r13d
xorl %ebp, %ebp
leaq 0x23ee8(%rip), %r12     # 0x2d204
movq $0x0, 0x10(%rsp)
movl (%r10,%rbp), %edi
addq %rbx, %rdi
movq %r12, %rsi
callq 0x81e0
movq 0x28(%rsp), %r10
testl %eax, %eax
jne 0x934a
movq 0x18(%r10,%rbp), %rax
addq %r14, %rax
movq %rax, 0x10(%rsp)
addq $0x40, %rbp
cmpq %rbp, %r13
jne 0x9325
jmp 0x935e
movq $0x0, 0x10(%rsp)
movq 0x20(%rsp), %r8
testq %r8, %r8
je 0x945b
xorl %ebp, %ebp
leaq 0xc0(%rsp), %rdi
movq %rbp, %rsi
shlq $0x6, %rsi
cmpl $0x2, 0x4(%r10,%rsi)
jne 0x944f
addq %r10, %rsi
movq 0x20(%rsi), %rcx
movq 0x38(%rsi), %r9
movq %rcx, %rax
xorl %edx, %edx
divq %r9
cmpq %rcx, %r9
ja 0x944f
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 0x943f
movzwl 0x6(%r12), %eax
testq %rax, %rax
je 0x943f
cmpw 0x3c(%r14), %ax
ja 0x943f
movq 0x10(%r12), %rdx
testq %rdx, %rdx
je 0x943f
shll $0x6, %eax
testb $0x4, 0x8(%r10,%rax)
je 0x946d
addq %r10, %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 0x10(%rsp), %r8
movq %rdi, %r15
callq 0x99b0
movq 0x18(%rsp), %rdi
movq %r15, %rsi
callq 0x9620
movq %r15, %rdi
callq 0x9a48
movq 0x20(%rsp), %r8
movq %r15, %rdi
movq 0x28(%rsp), %r10
incq %rbx
addq $0x18, %r12
cmpq %rbx, %r13
ja 0x93b7
incq %rbp
cmpq %r8, %rbp
jne 0x9376
addq $0x328, %rsp            # imm = 0x328
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x23d98(%rip), %rdi     # 0x2d20c
leaq 0x23cf0(%rip), %rsi     # 0x2d16b
leaq 0x23cf8(%rip), %rcx     # 0x2d17a
movl $0x44, %edx
callq 0x80e0
leaq 0x23cd1(%rip), %rdi     # 0x2d164
leaq 0x23cd1(%rip), %rsi     # 0x2d16b
leaq 0x23cd9(%rip), %rcx     # 0x2d17a
movl $0x13, %edx
callq 0x80e0
leaq 0x23ce7(%rip), %rdi     # 0x2d199
leaq 0x23cb2(%rip), %rsi     # 0x2d16b
leaq 0x23cba(%rip), %rcx     # 0x2d17a
movl $0x16, %edx
callq 0x80e0
callq 0x83f6
leaq 0x23ce4(%rip), %rdi     # 0x2d1ba
leaq 0x23c8e(%rip), %rsi     # 0x2d16b
leaq 0x23c96(%rip), %rcx     # 0x2d17a
movl $0x1f, %edx
callq 0x80e0
movq 0x2eafb(%rip), %rdi     # 0x37ff0
leaq 0x23cce(%rip), %rsi     # 0x2d1ca
movl $0x39, %edx
callq 0x81a0
movq 0x2eae3(%rip), %rdi     # 0x37ff0
callq 0x80a0
callq 0x80d0
jmp 0x951b
jmp 0x951b
movq %rax, %r15
jmp 0x9530
movq %rax, %r15
leaq 0xc0(%rsp), %rdi
callq 0x9a48
movq 0x18(%rsp), %rdi
callq 0x9650
movq 0x8(%rsp), %rcx
movq (%rcx), %rdi
addq $0x10, %rcx
cmpq %rcx, %rdi
je 0x9556
movq (%rcx), %rsi
incq %rsi
callq 0x8170
movq %r15, %rdi
callq 0x8270