// File: /workspace/github_c_makefile_star2_O2/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, %r14
subq $0x10, %rsp
andl $0x0, 0x8(%rsp)
andl $0x0, (%rsp)
leaq 0x1957f(%rip), %rdi     # 0x2f730
leaq 0x195d8(%rip), %rsi     # 0x2f790
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x14bb2
popq %rcx
popq %rdx
movl 0x19589(%rip), %eax     # 0x2f758
subl 0x1db3b(%rip), %eax     # 0x33d10
movl %eax, 0x1db39(%rip)     # 0x33d14
leaq 0x1c0(%r14), %rbp
movq 0x1c0(%r14), %rax
movq (%rax), %rcx
testq %rcx, %rcx
je 0x161fa
andl $0x0, 0x10(%rax)
movq %rcx, %rax
jmp 0x161e9
leaq 0x1f8(%r14), %r12
movq %r12, %r15
movq (%r15), %r15
testq %r15, %r15
je 0x16387
cmpl $0x1, 0x20(%r15)
jne 0x16204
movzwl 0x24(%r15), %eax
testb $0x10, %al
je 0x16204
cmpq $-0x1, 0x30(%r15)
je 0x16204
cmpq $-0x1, 0x28(%r15)
je 0x16204
movq 0x1dae3(%rip), %rcx     # 0x33d18
addq 0x50(%r15), %rcx
movq %rcx, 0x1dad8(%rip)     # 0x33d18
movq 0x48(%r15), %rbx
testw $0xc00, %ax             # imm = 0xC00
je 0x1627b
movzbl 0x50(%r14), %eax
leaq 0x18f3a(%rip), %rcx     # 0x2f190
movq (%rcx,%rax,8), %rax
movq %r14, %rdi
callq *0x18(%rax)
movq 0x1da89(%rip), %rcx     # 0x33cf0
addq %rcx, %rbx
addq %rax, %rbx
addq 0x1daa4(%rip), %rcx     # 0x33d18
movq %rcx, 0x1da9d(%rip)     # 0x33d18
cmpq %rcx, %rbx
je 0x16295
movq 0x8(%r15), %rsi
leaq 0xb515(%rip), %rdi      # 0x217a0
movq %rbx, %rdx
xorl %eax, %eax
callq 0x6b2c
movq %rbp, %rbx
movq (%rbx), %rbx
cmpq $0x0, (%rbx)
je 0x16204
movq 0x38(%rbx), %rax
cmpq 0x30(%r15), %rax
jb 0x16298
addq 0x48(%rbx), %rax
cmpq 0x40(%r15), %rax
ja 0x16298
movzbl 0x21(%rbx), %r13d
testb $0x1, %r13b
je 0x16298
cmpl $0x0, 0x10(%rbx)
jne 0x16298
cmpb $0x3, 0x20(%rbx)
sete %al
andl $0x2, %r13d
shrl %r13d
orb %al, %r13b
movzbl %r13b, %esi
movl 0x1da2b(%rip), %eax     # 0x33d10
movl %eax, 0x10(%rbx)
movzbl 0x22(%rbx), %eax
movl %eax, %ecx
shrl %ecx
andl $0x1, %ecx
andl $0x4, %eax
leal (%rax,%rcx), %edx
addl $0x2, %edx
movq %rbx, %rdi
callq 0x16425
movq 0x1da0d(%rip), %rax     # 0x33d18
movq %r12, %rcx
movq (%rcx), %rcx
testq %rcx, %rcx
je 0x16332
cmpl $0x1, 0x20(%rcx)
je 0x1630e
testb $0x10, 0x24(%rcx)
je 0x1630e
movq 0x30(%rcx), %rdx
cmpq 0x38(%rbx), %rdx
jne 0x1630e
movq %rax, 0x48(%rcx)
jmp 0x1630e
cmpl $0x0, 0x54(%r14)
je 0x16344
testb %r13b, %r13b
jne 0x16356
addq 0x48(%rbx), %rax
jmp 0x1634f
movq 0x58(%rbx), %rcx
addq 0x50(%rbx), %rcx
addq %rcx, %rax
movq %rax, 0x1d9c2(%rip)     # 0x33d18
movl 0x40(%rbx), %ecx
movzwl 0x10(%rbx), %eax
leaq 0x193cc(%rip), %rdi     # 0x2f730
leaq 0x19425(%rip), %rsi     # 0x2f790
leaq 0xa2de(%rip), %rdx      # 0x20650
xorl %r8d, %r8d
xorl %r9d, %r9d
pushq %rax
pushq $0x3
callq 0x14bb2
popq %rcx
popq %rdx
jmp 0x16298
leaq 0x193a2(%rip), %rbx     # 0x2f730
leaq 0x193fb(%rip), %r14     # 0x2f790
leaq 0xa2b4(%rip), %r15      # 0x20650
movq (%rbp), %rbp
cmpq $0x0, (%rbp)
je 0x16416
cmpl $0x0, 0x10(%rbp)
jne 0x1639c
movb $0x1, %al
cmpb $0x3, 0x20(%rbp)
je 0x163bc
movb 0x21(%rbp), %al
andb $0x2, %al
shrb %al
movzbl %al, %r12d
movl 0x1d94a(%rip), %eax     # 0x33d10
movl %eax, 0x10(%rbp)
movzbl 0x22(%rbp), %edx
movl %edx, %eax
shrl %eax
andl $0x1, %eax
andl $0x4, %edx
orl %eax, %edx
movq %rbp, %rdi
movl %r12d, %esi
callq 0x16425
testb %r12b, %r12b
jne 0x163f4
movq 0x48(%rbp), %rax
addq %rax, 0x1d924(%rip)     # 0x33d18
movl 0x40(%rbp), %ecx
movzwl 0x10(%rbp), %eax
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
xorl %r8d, %r8d
xorl %r9d, %r9d
pushq %rax
pushq $0x3
callq 0x14bb2
popq %rcx
popq %rdx
jmp 0x1639c
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq