// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x58, %rsp
movq %rdi, 0x50(%rsp)
movq %rsi, 0x48(%rsp)
movl %edx, 0x44(%rsp)
movl %ecx, 0x40(%rsp)
leaq 0x124ee(%rip), %rax     # 0x3659b
movq %rax, 0x38(%rsp)
movsbl 0x1c217(%rip), %eax     # 0x402d0
cmpl $0x1, %eax
sete %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x34(%rsp)
leaq 0x20c09(%rip), %rdi     # 0x44cd8
callq 0x74d0
movq %rax, 0x20(%rsp)
cmpq $0x0, %rax
je 0x2439e
movl 0x34(%rsp), %edi
movq 0x20(%rsp), %rsi
addq $0x10, %rsi
addq $0x4, %rsi
callq 0x8060
movl %eax, 0x1c(%rsp)
cmpl $0x4, 0x1c(%rsp)
je 0x2410f
cmpl $0x9, 0x1c(%rsp)
jne 0x24277
movl 0x34(%rsp), %edi
movq 0x20(%rsp), %rsi
addq $0x10, %rsi
addq $0x8, %rsi
callq 0x8060
andl $0x2, %eax
cmpl $0x0, %eax
je 0x24214
movq 0x50(%rsp), %rdi
leaq 0x12114(%rip), %rsi     # 0x36251
xorl %r9d, %r9d
movl %r9d, %edx
movl %r9d, %ecx
movl %r9d, %r8d
callq 0x12110
movq %rax, 0x28(%rsp)
cmpq $0x0, %rax
je 0x24179
movl 0x34(%rsp), %edi
movq 0x20(%rsp), %rsi
addq $0x10, %rsi
addq $0x18, %rsi
movq 0x28(%rsp), %rax
movl 0x10(%rax), %edx
callq 0x8130
jmp 0x24193
movq 0x38(%rsp), %rsi
leaq 0x1242a(%rip), %rdi     # 0x365af
leaq 0x120c5(%rip), %rdx     # 0x36251
movb $0x0, %al
callq 0x8ae0
movl 0x34(%rsp), %edi
movq 0x20(%rsp), %rsi
addq $0x10, %rsi
addq $0x1c, %rsi
callq 0x8060
cmpl $0x0, %eax
je 0x24212
movq 0x50(%rsp), %rdi
leaq 0x11426(%rip), %rsi     # 0x355e0
xorl %r9d, %r9d
movl %r9d, %edx
movl %r9d, %ecx
movl %r9d, %r8d
callq 0x12110
movq %rax, 0x28(%rsp)
cmpq $0x0, %rax
je 0x241f6
movl 0x34(%rsp), %edi
movq 0x20(%rsp), %rsi
addq $0x10, %rsi
addq $0x1c, %rsi
movq 0x28(%rsp), %rax
movl 0x10(%rax), %edx
callq 0x8130
jmp 0x24210
movq 0x38(%rsp), %rsi
leaq 0x123ad(%rip), %rdi     # 0x365af
leaq 0x113d7(%rip), %rdx     # 0x355e0
movb $0x0, %al
callq 0x8ae0
jmp 0x24212
jmp 0x24272
movl 0x34(%rsp), %eax
movl %eax, 0xc(%rsp)
movq 0x20(%rsp), %rax
addq $0x10, %rax
addq $0x10, %rax
movq %rax, 0x10(%rsp)
movl 0x34(%rsp), %edi
movq 0x20(%rsp), %rsi
addq $0x10, %rsi
addq $0x10, %rsi
callq 0x8060
movl 0xc(%rsp), %edi
movq 0x10(%rsp), %rsi
movl %eax, %edx
addl 0x44(%rsp), %edx
callq 0x8130
movl 0x34(%rsp), %edi
movq 0x20(%rsp), %rsi
addq $0x10, %rsi
addq $0x18, %rsi
movl 0x40(%rsp), %edx
callq 0x8130
jmp 0x24381
cmpl $0x5, 0x1c(%rsp)
jne 0x242e5
movq 0x50(%rsp), %rdi
leaq 0x11fc7(%rip), %rsi     # 0x36251
xorl %r9d, %r9d
movl %r9d, %edx
movl %r9d, %ecx
movl %r9d, %r8d
callq 0x12110
movq %rax, 0x28(%rsp)
cmpq $0x0, %rax
je 0x242c6
movl 0x34(%rsp), %edi
movq 0x20(%rsp), %rsi
addq $0x10, %rsi
addq $0x18, %rsi
movq 0x28(%rsp), %rax
movl 0x10(%rax), %edx
callq 0x8130
jmp 0x242e0
movq 0x38(%rsp), %rsi
leaq 0x122dd(%rip), %rdi     # 0x365af
leaq 0x11f78(%rip), %rdx     # 0x36251
movb $0x0, %al
callq 0x8ae0
jmp 0x2437f
cmpl $0x6, 0x1c(%rsp)
je 0x242f7
cmpl $0xb, 0x1c(%rsp)
jne 0x2437d
movq 0x50(%rsp), %rdi
leaq 0x11f56(%rip), %rsi     # 0x36259
xorl %r9d, %r9d
movl %r9d, %edx
movl %r9d, %ecx
movl %r9d, %r8d
callq 0x12110
movq %rax, 0x28(%rsp)
cmpq $0x0, %rax
je 0x2433f
movl 0x34(%rsp), %edi
movq 0x20(%rsp), %rsi
addq $0x10, %rsi
addq $0x18, %rsi
movq 0x28(%rsp), %rax
movl 0x10(%rax), %edx
callq 0x8130
jmp 0x24359
movq 0x38(%rsp), %rsi
leaq 0x12264(%rip), %rdi     # 0x365af
leaq 0x11f07(%rip), %rdx     # 0x36259
movb $0x0, %al
callq 0x8ae0
cmpl $0xb, 0x1c(%rsp)
jne 0x2437b
movl 0x34(%rsp), %edi
movq 0x20(%rsp), %rsi
addq $0x10, %rsi
addq $0x1c, %rsi
movl $0x1, %edx
callq 0x8130
jmp 0x2437d
jmp 0x2437f
jmp 0x24381
movq 0x48(%rsp), %rdi
movq 0x20(%rsp), %rsi
addq $0x10, %rsi
movl $0x28, %edx
callq 0x8470
jmp 0x240c8
addq $0x58, %rsp
retq
nopw %cs:(%rax,%rax)