// File: /workspace/github_c_makefile_star2_O3/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, 0x18b9b(%rip)     # 0x2f810
sete %bl
leaq 0x1cc89(%rip), %rdi     # 0x33908
callq 0x673f
testq %rax, %rax
je 0x16ea4
movq %rax, %r13
movb %bl, %bpl
leaq 0x1cc6e(%rip), %r14     # 0x33908
movq %r15, 0x18(%rsp)
leaq 0x14(%r13), %rsi
movl %ebp, %edi
callq 0x696f
cmpl $0x9, %eax
je 0x16cbf
movl %eax, %ebx
cmpl $0x5, %eax
je 0x16cfd
cmpl $0x4, %ebx
jne 0x16d65
leaq 0x18(%r13), %rsi
movl %ebp, %edi
callq 0x696f
testb $0x2, %al
jne 0x16d32
leaq 0x20(%r13), %rbx
movl %ebp, %edi
movq %rbx, %rsi
callq 0x696f
movq 0x20(%rsp), %rcx
leal (%rax,%rcx), %edx
movl %ebp, %edi
movq %rbx, %rsi
callq 0x69a0
leaq 0x28(%r13), %rsi
movl %ebp, %edi
movl 0x14(%rsp), %edx
jmp 0x16e55
movq 0x8(%rsp), %rdi
leaq 0xc6d4(%rip), %rbx      # 0x233dd
movq %rbx, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xbe13
testq %rax, %rax
je 0x16dad
leaq 0x28(%r13), %rsi
movl 0x10(%rax), %edx
movl %ebp, %edi
jmp 0x16e55
movq 0x8(%rsp), %rdi
leaq 0xc69f(%rip), %rbx      # 0x233dd
movq %rbx, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xbe13
testq %rax, %rax
je 0x16dca
leaq 0x28(%r13), %rsi
movl 0x10(%rax), %edx
movl %ebp, %edi
callq 0x69a0
jmp 0x16de2
cmpl $0xb, %ebx
je 0x16d73
cmpl $0x6, %ebx
jne 0x16e7c
movq 0x8(%rsp), %rdi
leaq 0xc666(%rip), %r12      # 0x233e5
movq %r12, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xbe13
testq %rax, %rax
je 0x16e2d
leaq 0x28(%r13), %rsi
movl 0x10(%rax), %edx
movl %ebp, %edi
callq 0x69a0
jmp 0x16e45
leaq 0xc97b(%rip), %rdi      # 0x2372f
leaq 0xc960(%rip), %rsi      # 0x2371b
movq %rbx, %rdx
xorl %eax, %eax
callq 0x52b0
jmp 0x16e7c
leaq 0xc95e(%rip), %rdi      # 0x2372f
leaq 0xc943(%rip), %rsi      # 0x2371b
movq %rbx, %rdx
xorl %eax, %eax
callq 0x52b0
leaq 0x2c(%r13), %rbx
movl %ebp, %edi
movq %rbx, %rsi
callq 0x696f
testl %eax, %eax
je 0x16e7c
movq 0x8(%rsp), %rdi
movq %r14, %r15
leaq 0xb7c9(%rip), %r14      # 0x225d0
movq %r14, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xbe13
testq %rax, %rax
je 0x16e5c
movl 0x10(%rax), %edx
movl %ebp, %edi
movq %rbx, %rsi
callq 0x69a0
jmp 0x16e74
leaq 0xc8fb(%rip), %rdi      # 0x2372f
leaq 0xc8e0(%rip), %rsi      # 0x2371b
movq %r12, %rdx
xorl %eax, %eax
callq 0x52b0
cmpl $0xb, %ebx
jne 0x16e7c
leaq 0x2c(%r13), %rsi
movl %ebp, %edi
movl $0x1, %edx
callq 0x69a0
jmp 0x16e7c
leaq 0xc8cc(%rip), %rdi      # 0x2372f
leaq 0xc8b1(%rip), %rsi      # 0x2371b
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 0x6ba5
movq %r14, %rdi
callq 0x673f
movq %rax, %r13
testq %rax, %rax
jne 0x16c9f
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq