// File: /workspace/github_c_makefile_star2_O2/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, 0x18(%rsp)
movq %rsi, 0x20(%rsp)
movq %rdi, 0x8(%rsp)
xorl %ebp, %ebp
cmpb $0x1, 0x19de7(%rip)     # 0x2f820
sete %bpl
leaq 0xb9b9(%rip), %r13      # 0x213fd
leaq 0xab95(%rip), %r15      # 0x205e0
leaq 0x1e2a6(%rip), %rdi     # 0x33cf8
callq 0x642a
testq %rax, %rax
je 0x15c31
movq %rax, %rbx
leaq 0x14(%rax), %rsi
movl %ebp, %edi
callq 0x664d
cmpl $0x9, %eax
je 0x15a85
movl %eax, %r14d
cmpl $0x5, %eax
je 0x15ac3
cmpl $0x4, %r14d
jne 0x15b23
leaq 0x18(%rbx), %rsi
movl %ebp, %edi
callq 0x664d
testb $0x2, %al
jne 0x15af4
leaq 0x20(%rbx), %r14
movl %ebp, %edi
movq %r14, %rsi
callq 0x664d
movq 0x18(%rsp), %rcx
leal (%rax,%rcx), %edx
movl %ebp, %edi
movq %r14, %rsi
callq 0x6681
leaq 0x28(%rbx), %rsi
movl %ebp, %edi
movl 0x14(%rsp), %edx
jmp 0x15bf9
movq 0x8(%rsp), %rdi
movq %r13, %r14
movq %r13, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xb7e9
testq %rax, %rax
je 0x15b69
leaq 0x28(%rbx), %rsi
movl 0x10(%rax), %edx
movl %ebp, %edi
jmp 0x15bf9
movq 0x8(%rsp), %rdi
movq %r13, %r14
movq %r13, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xb7e9
testq %rax, %rax
je 0x15b7f
leaq 0x28(%rbx), %rsi
movl 0x10(%rax), %edx
movl %ebp, %edi
callq 0x6681
jmp 0x15b97
cmpl $0xb, %r14d
je 0x15b33
cmpl $0x6, %r14d
jne 0x15c18
movq 0x8(%rsp), %rdi
leaq 0xb8c6(%rip), %r12      # 0x21405
movq %r12, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xb7e9
testq %rax, %rax
je 0x15bd2
leaq 0x28(%rbx), %rsi
movl 0x10(%rax), %edx
movl %ebp, %edi
callq 0x6681
jmp 0x15bea
leaq 0xbbdf(%rip), %rdi      # 0x2174f
leaq 0xbbc4(%rip), %rsi      # 0x2173b
movq %r14, %rdx
jmp 0x15c11
leaq 0xbbc9(%rip), %rdi      # 0x2174f
leaq 0xbbae(%rip), %rsi      # 0x2173b
movq %r14, %rdx
xorl %eax, %eax
callq 0x6b2c
leaq 0x2c(%rbx), %r14
movl %ebp, %edi
movq %r14, %rsi
callq 0x664d
testl %eax, %eax
je 0x15c18
movq 0x8(%rsp), %rdi
movq %r15, %r12
movq %r15, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xb7e9
testq %rax, %rax
je 0x15c00
movl 0x10(%rax), %edx
movl %ebp, %edi
movq %r14, %rsi
jmp 0x15bf9
leaq 0xbb76(%rip), %rdi      # 0x2174f
leaq 0xbb5b(%rip), %rsi      # 0x2173b
movq %r12, %rdx
xorl %eax, %eax
callq 0x6b2c
cmpl $0xb, %r14d
jne 0x15c18
leaq 0x2c(%rbx), %rsi
movl %ebp, %edi
pushq $0x1
popq %rdx
callq 0x6681
jmp 0x15c18
leaq 0xbb48(%rip), %rdi      # 0x2174f
leaq 0xbb2d(%rip), %rsi      # 0x2173b
movq %r12, %rdx
xorl %eax, %eax
callq 0x6b2c
addq $0x10, %rbx
movq 0x20(%rsp), %rdi
movq %rbx, %rsi
pushq $0x28
popq %rdx
callq 0x6817
jmp 0x15a4b
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq