// File: /workspace/github_c_makefile_star2_O1/deplinenoise[P]vlink/t_elf32.c
// Function: elf32_writeshdrs
void elf32_writeshdrs(struct GlobalVars *gv,FILE *f,uint32_t reloffset,
                      uint32_t stabndx)
/* write all section headers */
{
  const char *fn = "elf32_writeshdrs():";
  bool be = elf32_endianess == _BIG_ENDIAN_;
  struct LinkedSection *ls;
  struct ShdrNode *shn;
  uint32_t type;

  while (shn = (struct ShdrNode *)remhead(&shdrlist)) {
    type = read32(be,shn->s.sh_type);

    /* handle REL and RELA sections */
    if (type == SHT_RELA || type == SHT_REL) {
      if (read32(be,shn->s.sh_flags) & SHF_ALLOC) {
        /* allocated, so probably dynamic relocs: link to .dynsym */
        if (ls = find_lnksec(gv,dynsym_name,0,0,0,0))
          write32(be,shn->s.sh_link,(uint32_t)ls->index);
        else
          ierror("%s %s",fn,dynsym_name);

        if (read32(be,shn->s.sh_info) != 0) {
          /* link to .plt requested in info field */
          if (ls = find_lnksec(gv,plt_name,0,0,0,0))
            write32(be,shn->s.sh_info,(uint32_t)ls->index);
          else
            ierror("%s %s",fn,plt_name);
        }
      }
      else {
        /* patch correct sh_offset and sh_link for reloc header */
        write32(be,shn->s.sh_offset,read32(be,shn->s.sh_offset)+reloffset);
        write32(be,shn->s.sh_link,stabndx);
      }
    }

    /* handle HASH sections, which need a .dynsym link */
    else if (type == SHT_HASH) {
      if (ls = find_lnksec(gv,dynsym_name,0,0,0,0))
        write32(be,shn->s.sh_link,(uint32_t)ls->index);
      else
        ierror("%s %s",fn,dynsym_name);
    }

    /* handle DYNAMIC and DYNSYM sections */
    else if (type==SHT_DYNAMIC || type==SHT_DYNSYM) {
      /* write .dynstr link */
      if (ls = find_lnksec(gv,dynstr_name,0,0,0,0))
        write32(be,shn->s.sh_link,(uint32_t)ls->index);
      else
        ierror("%s %s",fn,dynstr_name);

      if (type == SHT_DYNSYM) {
        write32(be,shn->s.sh_info,1);  /* @@ FIXME! number of local symbols */
      }
    }

    fwritex(f,&shn->s,sizeof(struct Elf32_Shdr));
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %ecx, 0x14(%rsp)
movq %rdx, 0x20(%rsp)
movq %rsi, %r15
movq %rdi, 0x8(%rsp)
xorl %ebp, %ebp
cmpb $0x1, 0x189dc(%rip)     # 0x2f810
sete %bl
leaq 0x1caca(%rip), %rdi     # 0x33908
callq 0x676f
testq %rax, %rax
je 0x17063
movq %rax, %r13
movb %bl, %bpl
leaq 0x1caaf(%rip), %r14     # 0x33908
movq %r15, 0x18(%rsp)
leaq 0x14(%r13), %rsi
movl %ebp, %edi
callq 0x69b3
cmpl $0x9, %eax
je 0x16e7e
movl %eax, %ebx
cmpl $0x5, %eax
je 0x16ebc
cmpl $0x4, %ebx
jne 0x16f24
leaq 0x18(%r13), %rsi
movl %ebp, %edi
callq 0x69b3
testb $0x2, %al
jne 0x16ef1
leaq 0x20(%r13), %rbx
movl %ebp, %edi
movq %rbx, %rsi
callq 0x69b3
movq 0x20(%rsp), %rcx
leal (%rax,%rcx), %edx
movl %ebp, %edi
movq %rbx, %rsi
callq 0x6a19
leaq 0x28(%r13), %rsi
movl %ebp, %edi
movl 0x14(%rsp), %edx
jmp 0x17014
movq 0x8(%rsp), %rdi
leaq 0xc535(%rip), %rbx      # 0x233fd
movq %rbx, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xbeb0
testq %rax, %rax
je 0x16f6c
leaq 0x28(%r13), %rsi
movl 0x10(%rax), %edx
movl %ebp, %edi
jmp 0x17014
movq 0x8(%rsp), %rdi
leaq 0xc500(%rip), %rbx      # 0x233fd
movq %rbx, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xbeb0
testq %rax, %rax
je 0x16f89
leaq 0x28(%r13), %rsi
movl 0x10(%rax), %edx
movl %ebp, %edi
callq 0x6a19
jmp 0x16fa1
cmpl $0xb, %ebx
je 0x16f32
cmpl $0x6, %ebx
jne 0x1703b
movq 0x8(%rsp), %rdi
leaq 0xc4c7(%rip), %r12      # 0x23405
movq %r12, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xbeb0
testq %rax, %rax
je 0x16fec
leaq 0x28(%r13), %rsi
movl 0x10(%rax), %edx
movl %ebp, %edi
callq 0x6a19
jmp 0x17004
leaq 0xc7dc(%rip), %rdi      # 0x2374f
leaq 0xc7c1(%rip), %rsi      # 0x2373b
movq %rbx, %rdx
xorl %eax, %eax
callq 0x52b0
jmp 0x1703b
leaq 0xc7bf(%rip), %rdi      # 0x2374f
leaq 0xc7a4(%rip), %rsi      # 0x2373b
movq %rbx, %rdx
xorl %eax, %eax
callq 0x52b0
leaq 0x2c(%r13), %rbx
movl %ebp, %edi
movq %rbx, %rsi
callq 0x69b3
testl %eax, %eax
je 0x1703b
movq 0x8(%rsp), %rdi
movq %r14, %r15
leaq 0xb61a(%rip), %r14      # 0x225e0
movq %r14, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xbeb0
testq %rax, %rax
je 0x1701b
movl 0x10(%rax), %edx
movl %ebp, %edi
movq %rbx, %rsi
callq 0x6a19
jmp 0x17033
leaq 0xc75c(%rip), %rdi      # 0x2374f
leaq 0xc741(%rip), %rsi      # 0x2373b
movq %r12, %rdx
xorl %eax, %eax
callq 0x52b0
cmpl $0xb, %ebx
jne 0x1703b
leaq 0x2c(%r13), %rsi
movl %ebp, %edi
movl $0x1, %edx
callq 0x6a19
jmp 0x1703b
leaq 0xc72d(%rip), %rdi      # 0x2374f
leaq 0xc712(%rip), %rsi      # 0x2373b
movq %r14, %rdx
xorl %eax, %eax
callq 0x52b0
movq %r15, %r14
movq 0x18(%rsp), %r15
addq $0x10, %r13
movl $0x28, %edx
movq %r15, %rdi
movq %r13, %rsi
callq 0x6c1a
movq %r14, %rdi
callq 0x676f
movq %rax, %r13
testq %rax, %rax
jne 0x16e5e
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq