// File: /workspace/github_c_makefile_star2_O2/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 0x9470
andq $0x0, 0x28(%rbx)
leaq 0x38(%rbx), %rax
movq %rax, 0x10(%rsp)
andq $0x0, 0x48(%rbx)
xorps %xmm0, %xmm0
movq %rbx, 0x8(%rsp)
movups %xmm0, 0x38(%rbx)
movq 0x2cf36(%rip), %rax     # 0x35fd0
movq (%rax), %rdi
leaq 0x210ab(%rip), %rsi     # 0x2a14f
movq %r15, %rdx
xorl %eax, %eax
callq 0x8220
movq %r15, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x8120
movl %eax, %ebp
cmpl $-0x1, %eax
je 0x92ab
leaq 0x30(%rsp), %rsi
movl %ebp, %edi
callq 0x8290
testl %eax, %eax
jne 0x92c4
movq 0x60(%rsp), %rsi
movq 0x8(%rsp), %rbx
movq %rsi, 0x30(%rbx)
pushq $0x1
popq %rdx
pushq $0x2
popq %rcx
xorl %edi, %edi
movl %ebp, %r8d
xorl %r9d, %r9d
callq 0x82b0
movq %rax, 0x28(%rbx)
cmpq $-0x1, %rax
je 0x92dd
movq %rax, %rsi
cmpl $0x464c457f, (%rax)     # imm = 0x464C457F
jne 0x92ee
movzwl 0x10(%rsi), %eax
cmpl $0x2, %eax
je 0x912d
cmpl $0x3, %eax
jne 0x9307
movb $0x1, %al
jmp 0x912f
xorl %eax, %eax
movq 0x8(%rsp), %rcx
movb %al, 0x50(%rcx)
movzwl 0x3c(%rsi), %r13d
movq 0x28(%rsi), %r14
addq %rsi, %r14
movzwl 0x3e(%rsi), %eax
shll $0x6, %eax
movq 0x18(%rax,%r14), %rbx
addq %rsi, %rbx
movq %r13, 0x28(%rsp)
shll $0x6, %r13d
xorl %ebp, %ebp
leaq 0x210a0(%rip), %r12     # 0x2a204
movq $0x0, 0x18(%rsp)
movq %rsi, 0x20(%rsp)
cmpq %rbp, %r13
je 0x91a2
movl (%r14,%rbp), %edi
addq %rbx, %rdi
movq %r12, %rsi
callq 0x8210
movq 0x20(%rsp), %rsi
testl %eax, %eax
jne 0x919c
movq 0x18(%r14,%rbp), %rax
addq %rsi, %rax
movq %rax, 0x18(%rsp)
addq $0x40, %rbp
jmp 0x9172
xorl %ebx, %ebx
leaq 0xc0(%rsp), %r15
cmpq 0x28(%rsp), %rbx
je 0x927c
movq %rbx, %rcx
shlq $0x6, %rcx
cmpl $0x2, 0x4(%r14,%rcx)
jne 0x9274
addq %r14, %rcx
movq 0x18(%rcx), %rbp
movq 0x20(%rcx), %rax
movq 0x8(%rsp), %rdx
addq 0x28(%rdx), %rbp
xorl %edx, %edx
divq 0x38(%rcx)
movq %rax, %r13
xorl %r12d, %r12d
cmpq %r12, %r13
jbe 0x9274
movb 0x4(%rbp), %al
andb $0xf, %al
cmpb $0x2, %al
jne 0x9268
movzwl 0x6(%rbp), %eax
testq %rax, %rax
je 0x9268
cmpw 0x3c(%rsi), %ax
ja 0x9268
movq 0x10(%rbp), %rdx
testq %rdx, %rdx
je 0x9268
shll $0x6, %eax
testb $0x4, 0x8(%r14,%rax)
je 0x928e
addq %r14, %rax
movq 0x8(%rbp), %rcx
movq %rcx, %rsi
subq 0x10(%rax), %rsi
addq 0x18(%rax), %rsi
movq 0x8(%rsp), %rax
addq 0x28(%rax), %rsi
addq %rsi, %rdx
movl (%rbp), %r8d
addq 0x18(%rsp), %r8
movq %r15, %rdi
callq 0x9834
movq 0x10(%rsp), %rdi
movq %r15, %rsi
callq 0x94ac
movq %r15, %rdi
callq 0x98c0
movq 0x20(%rsp), %rsi
incq %r12
addq $0x18, %rbp
jmp 0x91ea
incq %rbx
jmp 0x91ac
addq $0x328, %rsp            # imm = 0x328
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x20f77(%rip), %rdi     # 0x2a20c
leaq 0x20ecf(%rip), %rsi     # 0x2a16b
leaq 0x20ed7(%rip), %rcx     # 0x2a17a
pushq $0x44
popq %rdx
callq 0x8100
leaq 0x20eb2(%rip), %rdi     # 0x2a164
leaq 0x20eb2(%rip), %rsi     # 0x2a16b
leaq 0x20eba(%rip), %rcx     # 0x2a17a
pushq $0x13
jmp 0x92a5
leaq 0x20ece(%rip), %rdi     # 0x2a199
leaq 0x20e99(%rip), %rsi     # 0x2a16b
leaq 0x20ea1(%rip), %rcx     # 0x2a17a
pushq $0x16
jmp 0x92a5
leaq 0x20ecc(%rip), %rdi     # 0x2a1b0
callq 0x8150
callq 0x80f0
leaq 0x20ec5(%rip), %rdi     # 0x2a1ba
leaq 0x20e6f(%rip), %rsi     # 0x2a16b
leaq 0x20e77(%rip), %rcx     # 0x2a17a
pushq $0x1f
jmp 0x92a5
movq 0x2cce2(%rip), %rdi     # 0x35ff0
leaq 0x20eb5(%rip), %rsi     # 0x2a1ca
callq 0x8180
movq %rax, %rdi
callq 0x80c0
callq 0x80f0
jmp 0x933d
jmp 0x933d
movq %rax, %r15
leaq 0xc0(%rsp), %rdi
callq 0x98c0
jmp 0x9340
movq %rax, %r15
movq 0x10(%rsp), %rdi
callq 0x94dc
movq 0x8(%rsp), %rdi
callq 0x80b0
movq %r15, %rdi
callq 0x82a0