// File: /workspace/github_c_makefile_star2_O1/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 0x18122(%rip), %rdi     # 0x2f720
leaq 0x1817b(%rip), %rsi     # 0x2f780
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
pushq %rax
pushq %rax
callq 0x15f0e
addq $0x10, %rsp
movl 0x18128(%rip), %eax     # 0x2f748
subl 0x1c2fa(%rip), %eax     # 0x33920
movl %eax, 0x1c2f8(%rip)     # 0x33924
movq 0x1c0(%rbx), %rax
cmpq $0x0, (%rax)
je 0x17645
movl $0x0, 0x10(%rax)
movq (%rax), %rax
jmp 0x17633
movq 0x1f8(%rbx), %r13
testq %r13, %r13
je 0x17811
leaq 0x18124(%rip), %r15     # 0x2f780
leaq 0xafed(%rip), %r12      # 0x22650
cmpl $0x1, 0x20(%r13)
jne 0x17804
movzwl 0x24(%r13), %eax
testb $0x10, %al
je 0x17804
cmpq $-0x1, 0x30(%r13)
je 0x17804
cmpq $-0x1, 0x28(%r13)
je 0x17804
movq 0x50(%r13), %rcx
addq %rcx, 0x1c28c(%rip)     # 0x33928
movq 0x48(%r13), %rbp
testl $0xc00, %eax            # imm = 0xC00
je 0x176d0
movzbl 0x50(%rbx), %eax
leaq 0x17ace(%rip), %rcx     # 0x2f180
movq (%rcx,%rax,8), %rax
movq %rbx, %rdi
callq *0x18(%rax)
movq 0x1c23d(%rip), %rcx     # 0x33900
addq %rcx, %rbp
addq %rax, %rbp
addq %rcx, 0x1c258(%rip)     # 0x33928
movq 0x1c251(%rip), %rcx     # 0x33928
cmpq %rcx, %rbp
je 0x176f1
movq 0x8(%r13), %rsi
leaq 0xc0b9(%rip), %rdi      # 0x237a0
movq %rbp, %rdx
xorl %eax, %eax
callq 0x52b0
movq 0x1c0(%rbx), %rbp
cmpq $0x0, (%rbp)
je 0x17804
movq 0x38(%rbp), %rax
cmpq 0x30(%r13), %rax
jb 0x177fb
addq 0x48(%rbp), %rax
cmpq 0x40(%r13), %rax
ja 0x177fb
movzbl 0x21(%rbp), %r14d
testb $0x1, %r14b
je 0x177fb
cmpl $0x0, 0x10(%rbp)
jne 0x177fb
cmpb $0x3, 0x20(%rbp)
sete %al
andl $0x2, %r14d
shrl %r14d
orb %al, %r14b
movzbl %r14b, %esi
movl 0x1c1cd(%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 0x178bc
movq 0x1f8(%rbx), %rax
testq %rax, %rax
je 0x177a7
movq 0x1c1a3(%rip), %rcx     # 0x33928
cmpl $0x1, 0x20(%rax)
je 0x1779f
testb $0x10, 0x24(%rax)
je 0x1779f
movq 0x30(%rax), %rdx
cmpq 0x38(%rbp), %rdx
jne 0x1779f
movq %rcx, 0x48(%rax)
movq (%rax), %rax
testq %rax, %rax
jne 0x17785
cmpl $0x0, 0x54(%rbx)
je 0x177bf
testb %r14b, %r14b
jne 0x177d5
movq 0x1c16f(%rip), %rax     # 0x33928
addq 0x48(%rbp), %rax
jmp 0x177ce
movq 0x58(%rbp), %rax
addq 0x50(%rbp), %rax
addq 0x1c15a(%rip), %rax     # 0x33928
movq %rax, 0x1c153(%rip)     # 0x33928
movl 0x40(%rbp), %ecx
movzwl 0x10(%rbp), %eax
leaq 0x17f3d(%rip), %rdi     # 0x2f720
movq %r15, %rsi
movq %r12, %rdx
xorl %r8d, %r8d
xorl %r9d, %r9d
pushq %rax
pushq $0x3
callq 0x15f0e
addq $0x10, %rsp
movq (%rbp), %rbp
jmp 0x176f8
movq (%r13), %r13
testq %r13, %r13
jne 0x17663
movq 0x1c0(%rbx), %rbx
cmpq $0x0, (%rbx)
je 0x178ad
leaq 0x17ef7(%rip), %r14     # 0x2f720
leaq 0x17f50(%rip), %r15     # 0x2f780
leaq 0xae19(%rip), %r12      # 0x22650
cmpl $0x0, 0x10(%rbx)
jne 0x178a4
movb $0x1, %al
cmpb $0x3, 0x20(%rbx)
je 0x1784c
movb 0x21(%rbx), %al
andb $0x2, %al
shrb %al
movzbl %al, %ebp
movl 0x1c0cb(%rip), %eax     # 0x33920
movl %eax, 0x10(%rbx)
movzbl 0x22(%rbx), %edx
movl %edx, %eax
shrl %eax
andl $0x1, %eax
andl $0x4, %edx
orl %eax, %edx
movq %rbx, %rdi
movl %ebp, %esi
callq 0x178bc
testb %bpl, %bpl
jne 0x17882
movq 0x48(%rbx), %rax
addq %rax, 0x1c0a6(%rip)     # 0x33928
movl 0x40(%rbx), %ecx
movzwl 0x10(%rbx), %eax
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
xorl %r8d, %r8d
xorl %r9d, %r9d
pushq %rax
pushq $0x3
callq 0x15f0e
addq $0x10, %rsp
movq (%rbx), %rbx
cmpq $0x0, (%rbx)
jne 0x17837
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq