// File: /workspace/github_c_makefile_star2_O0/deplinenoise[P]vlink/t_elf32.c
// Function: elf32_reloc
static void elf32_reloc(struct GlobalVars *gv,struct Elf32_Ehdr *ehdr,
                        struct ObjectUnit *ou,struct Elf32_Shdr *shdr,
                        char *shstrtab,bool be,
                        uint8_t (*reloc_elf2vlink)(uint8_t,struct RelocInsert *))
/* Read ELF32 relocations, which are relative to a defined symbol, into
   the section's reloc-list. If the symbol is undefined, create an
   external reference on it, with the supplied relocation type. */
{
  uint8_t *data = (uint8_t *)ehdr + read32(be,shdr->sh_offset);
  bool is_rela = read32(be,shdr->sh_type) == SHT_RELA;
  unsigned long entsize = read32(be,shdr->sh_entsize);
  uint32_t symndx = read32(be,shdr->sh_link);
  int nrelocs = (int)(read32(be,shdr->sh_size) / (uint32_t)entsize);
  char *sec_name = shstrtab + read32(be,shdr->sh_name);
  struct LinkFile *lf = ou->lnkfile;
  struct Section *sec;

  if (gv->strip_symbols>=STRIP_DEBUG &&
      (!strncmp(sec_name,".rel.debug",10) ||
       !strncmp(sec_name,".rel.stab",9) ||
       !strncmp(sec_name,".rela.debug",11) ||
       !strncmp(sec_name,".rela.stab",10)))
    return;   /* ignore debugging sections when -S or -s is given */

  if ((data < lf->data) || 
      (data + read32(be,shdr->sh_size) > lf->data + lf->length)) {
    error(51,lf->pathname,"reloc",lf->objname);  /* illegal offset */
  }
  if (!(sec = find_sect_id(ou,read32(be,shdr->sh_info)))) {
    /* a section with this index doesn't exist! */
    error(52,lf->pathname,shstrtab + read32(be,shdr->sh_name),
          getobjname(ou),(int)read32(be,shdr->sh_info));
  }

  for (; nrelocs; nrelocs--,data+=entsize) {
    struct Elf32_Rela *elfrel = (struct Elf32_Rela *)data;
    struct Elf32_Shdr *symhdr = elf32_shdr(lf,ehdr,symndx);
    struct Elf32_Sym *sym = elf32_symtab(lf,ehdr,symndx) +
                            ELF32_R_SYM(read32(be,elfrel->r_info));
    uint32_t offs = read32(be,elfrel->r_offset);
    uint32_t shndx = (uint32_t)read16(be,sym->st_shndx);
    char *xrefname = NULL;
    struct Section *relsec=NULL;
    struct Reloc *r;
    struct RelocInsert ri,*ri_ptr;
    lword a;
    uint8_t rtype;

    memset(&ri,0,sizeof(struct RelocInsert));
    rtype = reloc_elf2vlink(ELF32_R_TYPE(read32(be,elfrel->r_info)),&ri);
    if (rtype == R_NONE)
      continue;

    /* if addend is not defined in Reloc, read it directly from the section */
    if (is_rela)
      a = (int32_t)read32(be,elfrel->r_addend);
    else
      a = (int32_t)readsection(gv,rtype,sec->data+offs,ri.bpos,ri.bsiz,ri.mask);

    if (shndx == SHN_UNDEF || shndx == SHN_COMMON) {
      /* undefined or common symbol - create external reference */
      xrefname = elf32_strtab(lf,ehdr,read32(be,symhdr->sh_link)) +
                              read32(be,sym->st_name);
      relsec = NULL;
    }
    else if (ELF32_ST_TYPE(*sym->st_info) == STT_SECTION) {
      /* a normal relocation, with an offset relative to a section base */
      relsec = find_sect_id(ou,shndx);
    }
    else if (ELF32_ST_TYPE(*sym->st_info)<STT_SECTION && shndx<SHN_ABS) {
      /* relocations, which are relative to a known symbol */
      relsec = find_sect_id(ou,shndx);
      a += (lword)read32(be,sym->st_value);
    }
    else
      ierror("elf32_reloc(): %s (%s): Only relocations which are relative "
             "to a section, function or object are supported "
             "(sym=%s, ST_TYPE=%d)",lf->pathname,lf->objname,
             elf32_strtab(lf,ehdr,read32(be,symhdr->sh_link)) +
                          read32(be,sym->st_name),
             ELF32_ST_TYPE(*sym->st_info));

    r = newreloc(gv,sec,xrefname,relsec,0,(unsigned long)offs,rtype,a);
    for (ri_ptr=&ri; ri_ptr; ri_ptr=ri_ptr->next)
      addreloc(sec,r,ri_ptr->bpos,ri_ptr->bsiz,ri_ptr->mask);

    /* referenced symbol is weak? */
    if (xrefname!=NULL && ELF32_ST_BIND(*sym->st_info)==STB_WEAK)
      r->flags |= RELF_WEAK;

    /* make sure that section data reflects this addend for other formats */
    if (is_rela)
      writesection(gv,sec->data+offs,r,a);
  }
}

// Assembly:
subq $0x178, %rsp            # imm = 0x178
movq 0x180(%rsp), %rax
movq %rdi, 0x170(%rsp)
movq %rsi, 0x168(%rsp)
movq %rdx, 0x160(%rsp)
movq %rcx, 0x158(%rsp)
movq %r8, 0x150(%rsp)
movl %r9d, 0x14c(%rsp)
movq 0x168(%rsp), %rax
movq %rax, 0x98(%rsp)
movl 0x14c(%rsp), %edi
movq 0x158(%rsp), %rsi
addq $0x10, %rsi
callq 0x8060
movl %eax, %ecx
movq 0x98(%rsp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, 0x140(%rsp)
movl 0x14c(%rsp), %edi
movq 0x158(%rsp), %rsi
addq $0x4, %rsi
callq 0x8060
cmpl $0x4, %eax
sete %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x13c(%rsp)
movl 0x14c(%rsp), %edi
movq 0x158(%rsp), %rsi
addq $0x24, %rsi
callq 0x8060
movl %eax, %eax
movq %rax, 0x130(%rsp)
movl 0x14c(%rsp), %edi
movq 0x158(%rsp), %rsi
addq $0x18, %rsi
callq 0x8060
movl %eax, 0x12c(%rsp)
movl 0x14c(%rsp), %edi
movq 0x158(%rsp), %rsi
addq $0x14, %rsi
callq 0x8060
movq 0x130(%rsp), %rcx
xorl %edx, %edx
divl %ecx
movl %eax, 0x128(%rsp)
movq 0x150(%rsp), %rax
movq %rax, 0xa0(%rsp)
movl 0x14c(%rsp), %edi
movq 0x158(%rsp), %rsi
callq 0x8060
movl %eax, %ecx
movq 0xa0(%rsp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, 0x120(%rsp)
movq 0x160(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0x118(%rsp)
movq 0x170(%rsp), %rax
movzbl 0x6c(%rax), %eax
cmpl $0x1, %eax
jl 0x21041
movq 0x120(%rsp), %rdi
leaq 0x156ed(%rip), %rsi     # 0x366c0
movl $0xa, %edx
callq 0x5050
cmpl $0x0, %eax
je 0x2103c
movq 0x120(%rsp), %rdi
leaq 0x156da(%rip), %rsi     # 0x366cb
movl $0x9, %edx
callq 0x5050
cmpl $0x0, %eax
je 0x2103c
movq 0x120(%rsp), %rdi
leaq 0x156c6(%rip), %rsi     # 0x366d5
movl $0xb, %edx
callq 0x5050
cmpl $0x0, %eax
je 0x2103c
movq 0x120(%rsp), %rdi
leaq 0x156b4(%rip), %rsi     # 0x366e1
movl $0xa, %edx
callq 0x5050
cmpl $0x0, %eax
jne 0x21041
jmp 0x216e2
movq 0x140(%rsp), %rax
movq 0x118(%rsp), %rcx
cmpq 0x28(%rcx), %rax
jb 0x210ab
movq 0x140(%rsp), %rax
movq %rax, 0x90(%rsp)
movl 0x14c(%rsp), %edi
movq 0x158(%rsp), %rsi
addq $0x14, %rsi
callq 0x8060
movl %eax, %ecx
movq 0x90(%rsp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq 0x118(%rsp), %rcx
movq 0x28(%rcx), %rcx
movq 0x118(%rsp), %rdx
addq 0x30(%rdx), %rcx
cmpq %rcx, %rax
jbe 0x210d6
movq 0x118(%rsp), %rax
movq 0x10(%rax), %rsi
movq 0x118(%rsp), %rax
movq 0x20(%rax), %rcx
movl $0x33, %edi
leaq 0x15e54(%rip), %rdx     # 0x36f23
movb $0x0, %al
callq 0x8c00
movq 0x160(%rsp), %rax
movq %rax, 0x88(%rsp)
movl 0x14c(%rsp), %edi
movq 0x158(%rsp), %rsi
addq $0x1c, %rsi
callq 0x8060
movq 0x88(%rsp), %rdi
movl %eax, %esi
callq 0x11fe0
movq %rax, 0x110(%rsp)
cmpq $0x0, %rax
jne 0x211b0
movq 0x118(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0x70(%rsp)
movq 0x150(%rsp), %rax
movq %rax, 0x68(%rsp)
movl 0x14c(%rsp), %edi
movq 0x158(%rsp), %rsi
callq 0x8060
movl %eax, %ecx
movq 0x68(%rsp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, 0x78(%rsp)
movq 0x160(%rsp), %rdi
callq 0x8f00
movq %rax, 0x80(%rsp)
movl 0x14c(%rsp), %edi
movq 0x158(%rsp), %rsi
addq $0x1c, %rsi
callq 0x8060
movq 0x70(%rsp), %rsi
movq 0x78(%rsp), %rdx
movq 0x80(%rsp), %rcx
movl %eax, %r8d
movl $0x34, %edi
movb $0x0, %al
callq 0x8c00
jmp 0x211b2
cmpl $0x0, 0x128(%rsp)
je 0x216e2
movq 0x140(%rsp), %rax
movq %rax, 0x108(%rsp)
movq 0x118(%rsp), %rdi
movq 0x168(%rsp), %rsi
movl 0x12c(%rsp), %eax
movzwl %ax, %edx
callq 0x1f7f0
movq %rax, 0x100(%rsp)
movq 0x118(%rsp), %rdi
movq 0x168(%rsp), %rsi
movl 0x12c(%rsp), %edx
callq 0x1fa90
movq %rax, 0x58(%rsp)
movl 0x14c(%rsp), %edi
movq 0x108(%rsp), %rsi
addq $0x4, %rsi
callq 0x8060
movl %eax, %ecx
movq 0x58(%rsp), %rax
shrl $0x8, %ecx
movl %ecx, %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, 0xf8(%rsp)
movl 0x14c(%rsp), %edi
movq 0x108(%rsp), %rsi
callq 0x8060
movl %eax, 0xf4(%rsp)
movl 0x14c(%rsp), %edi
movq 0xf8(%rsp), %rsi
addq $0xe, %rsi
callq 0x8010
movzwl %ax, %eax
movl %eax, 0xf0(%rsp)
movq $0x0, 0xe8(%rsp)
movq $0x0, 0xe0(%rsp)
leaq 0xc0(%rsp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x5100
movq 0x180(%rsp), %rax
movq %rax, 0x60(%rsp)
movl 0x14c(%rsp), %edi
movq 0x108(%rsp), %rsi
addq $0x4, %rsi
callq 0x8060
movl %eax, %ecx
movq 0x60(%rsp), %rax
leaq 0xc0(%rsp), %rsi
movzbl %cl, %edi
callq *%rax
movb %al, 0xaf(%rsp)
movzbl 0xaf(%rsp), %eax
cmpl $0x0, %eax
jne 0x21306
jmp 0x216b4
cmpl $0x0, 0x13c(%rsp)
je 0x21334
movl 0x14c(%rsp), %edi
movq 0x108(%rsp), %rsi
addq $0x8, %rsi
callq 0x8060
cltq
movq %rax, 0xb0(%rsp)
jmp 0x2138c
movq 0x170(%rsp), %rdi
movb 0xaf(%rsp), %sil
movq 0x110(%rsp), %rax
movq 0x40(%rax), %rdx
movl 0xf4(%rsp), %eax
addq %rax, %rdx
movw 0xc8(%rsp), %cx
movw 0xca(%rsp), %ax
movq 0xd0(%rsp), %r9
movzbl %sil, %esi
movzwl %cx, %ecx
movzwl %ax, %r8d
callq 0x10eb0
cltq
movq %rax, 0xb0(%rsp)
cmpl $0x0, 0xf0(%rsp)
je 0x213a7
cmpl $0xfff2, 0xf0(%rsp) # imm = 0xFFF2
jne 0x21428
movq 0x118(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0x168(%rsp), %rax
movq %rax, 0x48(%rsp)
movl 0x14c(%rsp), %edi
movq 0x100(%rsp), %rsi
addq $0x18, %rsi
callq 0x8060
movq 0x40(%rsp), %rdi
movq 0x48(%rsp), %rsi
movl %eax, %edx
callq 0x1f960
movq %rax, 0x50(%rsp)
movl 0x14c(%rsp), %edi
movq 0xf8(%rsp), %rsi
callq 0x8060
movl %eax, %ecx
movq 0x50(%rsp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, 0xe8(%rsp)
movq $0x0, 0xe0(%rsp)
jmp 0x2157e
movq 0xf8(%rsp), %rax
movzbl 0xc(%rax), %eax
andl $0xf, %eax
cmpl $0x3, %eax
jne 0x2145d
movq 0x160(%rsp), %rdi
movl 0xf0(%rsp), %esi
callq 0x11fe0
movq %rax, 0xe0(%rsp)
jmp 0x2157c
movq 0xf8(%rsp), %rax
movzbl 0xc(%rax), %eax
andl $0xf, %eax
cmpl $0x3, %eax
jge 0x214c9
cmpl $0xfff1, 0xf0(%rsp) # imm = 0xFFF1
jae 0x214c9
movq 0x160(%rsp), %rdi
movl 0xf0(%rsp), %esi
callq 0x11fe0
movq %rax, 0xe0(%rsp)
movl 0x14c(%rsp), %edi
movq 0xf8(%rsp), %rsi
addq $0x4, %rsi
callq 0x8060
movl %eax, %eax
addq 0xb0(%rsp), %rax
movq %rax, 0xb0(%rsp)
jmp 0x2157a
movq 0x118(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0x30(%rsp)
movq 0x118(%rsp), %rax
movq 0x20(%rax), %rax
movq %rax, 0x38(%rsp)
movq 0x118(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x168(%rsp), %rax
movq %rax, 0x20(%rsp)
movl 0x14c(%rsp), %edi
movq 0x100(%rsp), %rsi
addq $0x18, %rsi
callq 0x8060
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rsi
movl %eax, %edx
callq 0x1f960
movq %rax, 0x28(%rsp)
movl 0x14c(%rsp), %edi
movq 0xf8(%rsp), %rsi
callq 0x8060
movq 0x28(%rsp), %rcx
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rdx
movl %eax, %eax
addq %rax, %rcx
movq 0xf8(%rsp), %rax
movzbl 0xc(%rax), %r8d
andl $0xf, %r8d
leaq 0x15179(%rip), %rdi     # 0x366ec
movb $0x0, %al
callq 0x8ae0
jmp 0x2157c
jmp 0x2157e
movq 0x170(%rsp), %rdi
movq 0x110(%rsp), %rsi
movq 0xe8(%rsp), %rdx
movq 0xe0(%rsp), %rcx
movl 0xf4(%rsp), %eax
movl %eax, %r9d
movb 0xaf(%rsp), %r10b
movq 0xb0(%rsp), %rax
xorl %r8d, %r8d
movzbl %r10b, %r10d
movl %r10d, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x10850
movq %rax, 0xd8(%rsp)
leaq 0xc0(%rsp), %rax
movq %rax, 0xb8(%rsp)
cmpq $0x0, 0xb8(%rsp)
je 0x21644
movq 0x110(%rsp), %rdi
movq 0xd8(%rsp), %rsi
movq 0xb8(%rsp), %rax
movw 0x8(%rax), %cx
movq 0xb8(%rsp), %rax
movw 0xa(%rax), %ax
movq 0xb8(%rsp), %rdx
movq 0x10(%rdx), %r8
movzwl %cx, %edx
movzwl %ax, %ecx
callq 0x109b0
movq 0xb8(%rsp), %rax
movq (%rax), %rax
movq %rax, 0xb8(%rsp)
jmp 0x215e5
cmpq $0x0, 0xe8(%rsp)
je 0x21675
movq 0xf8(%rsp), %rax
movzbl 0xc(%rax), %eax
sarl $0x4, %eax
cmpl $0x2, %eax
jne 0x21675
movq 0xd8(%rsp), %rax
movzbl 0x39(%rax), %ecx
orl $0x1, %ecx
movb %cl, 0x39(%rax)
cmpl $0x0, 0x13c(%rsp)
je 0x216b2
movq 0x170(%rsp), %rdi
movq 0x110(%rsp), %rax
movq 0x40(%rax), %rsi
movl 0xf4(%rsp), %eax
addq %rax, %rsi
movq 0xd8(%rsp), %rdx
movq 0xb0(%rsp), %rcx
callq 0x110c0
jmp 0x216b4
movl 0x128(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x128(%rsp)
movq 0x130(%rsp), %rax
addq 0x140(%rsp), %rax
movq %rax, 0x140(%rsp)
jmp 0x211b2
addq $0x178, %rsp            # imm = 0x178
retq
nopw (%rax,%rax)