// File: /workspace/github_c_makefile_star2_O3/deplinenoise[P]vlink/t_elf32.c
// Function: elf32_makeshdrs
void elf32_makeshdrs(struct GlobalVars *gv)
/* generate all ELF section headers */
{
  struct Phdr *p,*p2;
  struct LinkedSection *ls;
  unsigned long poffs;

  /* touch elf32symlist, to make sure it is initialized */
  elf32_addsym(&elf32symlist,&elf32stringlist,NULL,0,0,0,0,0);

  /* offset, to find section-symbols by section header index */
  secsyms = (int)elf32symlist.nextindex - (int)elf32shdridx;

  /* reset index */
  for (ls=(struct LinkedSection *)gv->lnksec.first;
       ls->n.next!=NULL; ls=(struct LinkedSection *)ls->n.next)
    ls->index = 0;

  for (p=gv->phdrlist; p; p=p->next) {
    if (p->type==PT_LOAD && (p->flags&PHDR_USED) &&
        p->start!=ADDR_NONE && p->start_vma!=ADDR_NONE) {
      /* check file offset for current segment */
      elf32offset += p->alignment_gap;
      poffs = p->offset;
      if (p->flags & (PHDR_PHDRS|PHDR_FILEHDR)) {
        poffs += fff[gv->dest_format]->headersize(gv) + file_hdr_gap;
        elf32offset += file_hdr_gap;
      }
      if (poffs != elf32offset) {
        ierror("elf32_makeshdrs(): PHDR \"%s\" offs %lu != %lu\n",
               p->name,poffs,elf32offset);
      }

      /* find sections which belong to this segment and set their index */
      for (ls=(struct LinkedSection *)gv->lnksec.first;
           ls->n.next!=NULL; ls=(struct LinkedSection *)ls->n.next) {
        if (ls->copybase>=(unsigned long)p->start &&
            (ls->copybase+ls->size)<=(unsigned long)p->mem_end &&
            (ls->flags & SF_ALLOC) && ls->index==0) {
          uint32_t f = SHF_ALLOC;
          bool bss = ls->type==ST_UDATA || (ls->flags&SF_UNINITIALIZED);

          ls->index = (int)elf32shdridx;
          f |= (ls->protection & SP_WRITE) ? SHF_WRITE : 0;
          f |= (ls->protection & SP_EXEC) ? SHF_EXECINSTR : 0;
          add_section_shdr(ls,bss,f);

          /* check if section included in other non-LOAD segments as well */
          for (p2=gv->phdrlist; p2; p2=p2->next) {
            if (p2->type!=PT_LOAD && (p2->flags&PHDR_USED) &&
                p2->start==(lword)ls->copybase)
              p2->offset = elf32offset;
          }

          /* update file offset */
          if (gv->dest_object) {
            if (!bss)
              elf32offset += ls->size;
          }
          else {
            elf32offset += ls->filesize + ls->gapsize;
          }

          /* add section symbol (without name) */
          elf32_addsym(&elf32symlist,&elf32stringlist,noname,
                       (uint32_t)ls->base,0,STB_LOCAL,STT_SECTION,
                       (uint16_t)ls->index);
        }
      }
    }
  }

  /* unallocated sections at last */
  for (ls=(struct LinkedSection *)gv->lnksec.first;
       ls->n.next!=NULL; ls=(struct LinkedSection *)ls->n.next) {
    if (ls->index == 0) {
      uint32_t f = 0;
      bool bss = ls->type==ST_UDATA || (ls->flags&SF_UNINITIALIZED);

      ls->index = (int)elf32shdridx;
      f |= (ls->protection & SP_WRITE) ? SHF_WRITE : 0;
      f |= (ls->protection & SP_EXEC) ? SHF_EXECINSTR : 0;
      add_section_shdr(ls,bss,f);
      /* update file offset */
      if (!bss)
        elf32offset += ls->size;
      /* add section symbol (without name) */
      elf32_addsym(&elf32symlist,&elf32stringlist,noname,(uint32_t)ls->base,0,
                   STB_LOCAL,STT_SECTION,(uint16_t)ls->index);
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
xorl %eax, %eax
leaq 0x182d2(%rip), %rdi     # 0x2f720
leaq 0x1832b(%rip), %rsi     # 0x2f780
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
pushq %rax
pushq %rax
callq 0x15d58
addq $0x10, %rsp
movl 0x182d8(%rip), %eax     # 0x2f748
subl 0x1c4aa(%rip), %eax     # 0x33920
movl %eax, 0x1c4a8(%rip)     # 0x33924
movq 0x1c0(%rbx), %r14
movq (%r14), %rax
testq %rax, %rax
je 0x174a0
movq %r14, %rcx
movl $0x0, 0x10(%rcx)
movq %rax, %rcx
movq (%rax), %rax
testq %rax, %rax
jne 0x1748e
movq 0x1f8(%rbx), %r13
testq %r13, %r13
je 0x17682
leaq 0x182c9(%rip), %r15     # 0x2f780
leaq 0xb182(%rip), %r12      # 0x22640
cmpl $0x1, 0x20(%r13)
jne 0x1766e
movzwl 0x24(%r13), %eax
testb $0x10, %al
je 0x1766e
cmpq $-0x1, 0x30(%r13)
je 0x1766e
cmpq $-0x1, 0x28(%r13)
je 0x1766e
movq 0x1c435(%rip), %rcx     # 0x33928
addq 0x50(%r13), %rcx
movq %rcx, 0x1c42a(%rip)     # 0x33928
movq 0x48(%r13), %rbp
testl $0xc00, %eax            # imm = 0xC00
je 0x17539
movzbl 0x50(%rbx), %eax
leaq 0x17c6c(%rip), %rcx     # 0x2f180
movq (%rcx,%rax,8), %rax
movq %rbx, %rdi
callq *0x18(%rax)
movq 0x1c3db(%rip), %rcx     # 0x33900
addq %rcx, %rbp
addq %rax, %rbp
addq 0x1c3f6(%rip), %rcx     # 0x33928
movq %rcx, 0x1c3ef(%rip)     # 0x33928
cmpq %rcx, %rbp
je 0x17553
movq 0x8(%r13), %rsi
leaq 0xc237(%rip), %rdi      # 0x23780
movq %rbp, %rdx
xorl %eax, %eax
callq 0x52b0
movq 0x1c0(%rbx), %rbp
movq (%rbp), %rax
testq %rax, %rax
je 0x1766e
movq 0x38(%rbp), %rcx
cmpq 0x30(%r13), %rcx
jb 0x17663
addq 0x48(%rbp), %rcx
cmpq 0x40(%r13), %rcx
ja 0x17663
movzbl 0x21(%rbp), %r14d
testb $0x1, %r14b
je 0x17663
cmpl $0x0, 0x10(%rbp)
jne 0x17663
cmpb $0x3, 0x20(%rbp)
sete %al
andl $0x2, %r14d
shrl %r14d
orb %al, %r14b
movzbl %r14b, %esi
movl 0x1c369(%rip), %eax     # 0x33920
movl %eax, 0x10(%rbp)
movzbl 0x22(%rbp), %eax
movl %eax, %ecx
shrl %ecx
andl $0x1, %ecx
andl $0x4, %eax
leal (%rax,%rcx), %edx
addl $0x2, %edx
movq %rbp, %rdi
callq 0x17738
movq 0x1f8(%rbx), %rax
testq %rax, %rax
je 0x1760b
movq 0x1c33f(%rip), %rcx     # 0x33928
cmpl $0x1, 0x20(%rax)
je 0x17603
testb $0x10, 0x24(%rax)
je 0x17603
movq 0x30(%rax), %rdx
cmpq 0x38(%rbp), %rdx
jne 0x17603
movq %rcx, 0x48(%rax)
movq (%rax), %rax
testq %rax, %rax
jne 0x175e9
cmpl $0x0, 0x54(%rbx)
je 0x17623
testb %r14b, %r14b
jne 0x17639
movq 0x1c30b(%rip), %rax     # 0x33928
addq 0x48(%rbp), %rax
jmp 0x17632
movq 0x58(%rbp), %rax
addq 0x50(%rbp), %rax
addq 0x1c2f6(%rip), %rax     # 0x33928
movq %rax, 0x1c2ef(%rip)     # 0x33928
movl 0x40(%rbp), %ecx
movzwl 0x10(%rbp), %eax
leaq 0x180d9(%rip), %rdi     # 0x2f720
movq %r15, %rsi
movq %r12, %rdx
xorl %r8d, %r8d
xorl %r9d, %r9d
pushq %rax
pushq $0x3
callq 0x15d58
addq $0x10, %rsp
movq (%rbp), %rax
movq %rax, %rbp
movq (%rax), %rax
jmp 0x1755e
movq (%r13), %r13
testq %r13, %r13
jne 0x174be
movq 0x1c0(%rbx), %r14
movq (%r14), %rax
testq %rax, %rax
je 0x17729
leaq 0x1808b(%rip), %rbx     # 0x2f720
leaq 0x180e4(%rip), %r15     # 0x2f780
leaq 0xaf9d(%rip), %r12      # 0x22640
cmpl $0x0, 0x10(%r14)
jne 0x1771a
movb $0x1, %al
cmpb $0x3, 0x20(%r14)
je 0x176bb
movb 0x21(%r14), %al
andb $0x2, %al
shrb %al
movzbl %al, %ebp
movl 0x1c25c(%rip), %eax     # 0x33920
movl %eax, 0x10(%r14)
movzbl 0x22(%r14), %edx
movl %edx, %eax
shrl %eax
andl $0x1, %eax
andl $0x4, %edx
orl %eax, %edx
movq %r14, %rdi
movl %ebp, %esi
callq 0x17738
testb %bpl, %bpl
jne 0x176f3
movq 0x48(%r14), %rax
addq %rax, 0x1c235(%rip)     # 0x33928
movl 0x40(%r14), %ecx
movzwl 0x10(%r14), %eax
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
xorl %r8d, %r8d
xorl %r9d, %r9d
pushq %rax
pushq $0x3
callq 0x15d58
addq $0x10, %rsp
movq (%r14), %rax
movq %rax, %r14
movq (%rax), %rax
testq %rax, %rax
jne 0x176a3
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq