// File: /workspace/github_c_makefile_star2_O0/deplinenoise[P]vlink/t_elf32.c
// Function: elf32_segmentcheck
uint32_t elf32_segmentcheck(struct GlobalVars *gv)
/* 1. checks the PT_LOAD segments for intermediate uninitialized sections,
      which will be turned into initialized PROGBITS data sections
   2. sets segment permissions from contained sections
   3. calculates bytes to insert at beginning of segment to meet
      page-alignment restrictions
   4. initializes PT_PHDR segment
   Returns number of segments in list */
{
  unsigned long headersize = fff[gv->dest_format]->headersize(gv);
  struct Phdr *p,*phdrs,*first=NULL;
  struct LinkedSection *ls,*bss_start,*seg_lastdat,*seg_lastsec,*prg_lastdat;
  unsigned long foffs;
  uint32_t segcnt;

  /* find PHDR segment */
  for (phdrs=NULL,p=gv->phdrlist; p; p=p->next) {
    if ((p->flags&PHDR_USED) && p->type==PT_PHDR) {
      phdrs = p;
      break;
    }
  }

  for (p=gv->phdrlist,prg_lastdat=NULL; p; p=p->next) {
    if (p->type==PT_LOAD && (p->flags&PHDR_USED) &&
        p->start!=ADDR_NONE && p->start_vma!=ADDR_NONE) {
      unsigned long amask = (1L << p->alignment) - 1;
      long gapsize;

      bss_start = seg_lastsec = NULL;
      p->flags &= ~PHDR_PFMASK;
      p->flags |= PF_R;  /* at least 'read' should be allowed */

      /* determine initial file offset for first segment */
      if (!first) {
        first = p;
        if (phdrs) {  /* header is included in first segment */
          p->flags |= PHDR_PHDRS | PHDR_FILEHDR;
          foffs = 0;
          file_hdr_gap = (unsigned long)p->start
                          - ((unsigned long)(p->start - headersize) & ~amask)
                          - headersize;
          p->start -= headersize + file_hdr_gap;
          if (p->start_vma)
            p->start_vma -= headersize + file_hdr_gap;
        }
        else
          foffs = headersize;  /* first segment starts after header */
      }

      for (ls=(struct LinkedSection *)gv->lnksec.first,
            seg_lastdat=NULL,seg_lastsec=NULL;
           ls->n.next!=NULL;
           ls=(struct LinkedSection *)ls->n.next) {

        if (ls->copybase>=(unsigned long)p->start && ls->size &&
            (ls->copybase+ls->size)<=(unsigned long)p->mem_end &&
            (ls->flags & SF_ALLOC)) {

          p->flags |= conv_perm_to_elf(ls->protection);
          if (ls->alignment > p->alignment)
            p->alignment = ls->alignment;

          if (ls->flags & SF_UNINITIALIZED) {
            if (!bss_start)
              bss_start = ls;
          }
          else {
            if (bss_start) {
              struct LinkedSection *bssls;

              for (bssls=bss_start; bssls!=ls;
                   bssls=(struct LinkedSection *)bssls->n.next) {
                bssls->flags &= ~SF_UNINITIALIZED;
                if (bssls->type == ST_UDATA)
                  bssls->type = ST_DATA;
                bssls->filesize = bssls->size;
              }
              /* Warning: Intermediate uninitialized sections in ELF
                 segment will be turned into initialized */
              error(82,p->name,bss_start->name,
                    ((struct LinkedSection *)ls->n.pred)->name);
              bss_start = NULL;
            }
          }

          if (seg_lastsec && !bss_start) {
            if (ls->copybase >= seg_lastsec->copybase+seg_lastsec->filesize) {
              seg_lastsec->gapsize = ls->copybase - (seg_lastsec->copybase +
                                                     seg_lastsec->filesize);
            }
            else if (ls->copybase > 0) {
              ierror("elf32_segmentcheck(): overlapping sections "
                     "%s(%lx-%lx) followed by %s(%lx)",
                     seg_lastsec->name,seg_lastsec->copybase,
                     seg_lastsec->copybase + seg_lastsec->filesize,
                     ls->copybase);
            }
          }
          seg_lastsec = ls;
          if (!bss_start || !seg_lastdat)
            prg_lastdat = seg_lastdat = ls;
        }
      }

      /* calculate alignment gap for segment */
      /* @@@ do we align on LMA or VMA? */
      gapsize = (p->start & amask) - (foffs & amask);
      p->alignment_gap = gapsize<0 ? gapsize+amask+1 : gapsize;

      if (seg_lastdat)
        p->file_end = seg_lastdat->copybase + seg_lastdat->filesize;

      foffs += p->alignment_gap;
      p->offset = foffs;
      foffs += p->file_end - p->start;
    }
  }

  if (phdrs!=NULL && first!=NULL) {
    /* init PHDR segment using the first LOAD seg. which directly follows */
    phdrs->flags &= ~PHDR_PFMASK;
    phdrs->flags |= PF_R | PF_X;
    phdrs->start = first->start + sizeof(struct Elf32_Ehdr);
    phdrs->start_vma = phdrs->start;
    phdrs->mem_end = phdrs->start + headersize - sizeof(struct Elf32_Ehdr);
    phdrs->file_end = phdrs->mem_end;
    phdrs->offset = sizeof(struct Elf32_Ehdr);
  }

  for (p=gv->phdrlist,segcnt=0; p; p=p->next) {
    if ((p->flags&PHDR_USED)
        && p->start!=ADDR_NONE && p->start_vma!=ADDR_NONE) {
      segcnt++;

      if (p->type!=PT_LOAD && p->type!=PT_PHDR && p->type!=PT_NULL) {
        /* set segment permissions for remaining segment types */
        p->flags &= ~PHDR_PFMASK;
        for (ls=seg_lastdat=(struct LinkedSection *)gv->lnksec.first;
             ls->n.next!=NULL; ls=(struct LinkedSection *)ls->n.next) {
          if (ls->copybase>=(unsigned long)p->start && ls->size &&
              (ls->copybase+ls->size)<=(unsigned long)p->mem_end &&
              (ls->flags & SF_ALLOC)) {
            p->flags |= conv_perm_to_elf(ls->protection);
            seg_lastdat = ls;
          }
        }
        p->file_end = seg_lastdat->copybase + seg_lastdat->filesize;
      }
    }
  }

  return segcnt;
}

// Assembly:
subq $0x88, %rsp
movq %rdi, 0x80(%rsp)
movq 0x80(%rsp), %rax
movzbl 0x50(%rax), %eax
movl %eax, %ecx
leaq 0x1b48c(%rip), %rax     # 0x3fa30
movq (%rax,%rcx,8), %rax
movq 0x18(%rax), %rax
movq 0x80(%rsp), %rdi
callq *%rax
movq %rax, 0x78(%rsp)
movq $0x0, 0x60(%rsp)
movq $0x0, 0x68(%rsp)
movq 0x80(%rsp), %rax
movq 0x1f8(%rax), %rax
movq %rax, 0x70(%rsp)
cmpq $0x0, 0x70(%rsp)
je 0x24622
movq 0x70(%rsp), %rax
movzwl 0x24(%rax), %eax
andl $0x10, %eax
cmpl $0x0, %eax
je 0x24611
movq 0x70(%rsp), %rax
cmpl $0x6, 0x20(%rax)
jne 0x24611
movq 0x70(%rsp), %rax
movq %rax, 0x68(%rsp)
jmp 0x24622
jmp 0x24613
movq 0x70(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x70(%rsp)
jmp 0x245e1
movq 0x80(%rsp), %rax
movq 0x1f8(%rax), %rax
movq %rax, 0x70(%rsp)
movq $0x0, 0x38(%rsp)
cmpq $0x0, 0x70(%rsp)
je 0x24af5
movq 0x70(%rsp), %rax
cmpl $0x1, 0x20(%rax)
jne 0x24ae1
movq 0x70(%rsp), %rax
movzwl 0x24(%rax), %eax
andl $0x10, %eax
cmpl $0x0, %eax
je 0x24ae1
movq 0x70(%rsp), %rax
cmpq $-0x1, 0x30(%rax)
je 0x24ae1
movq 0x70(%rsp), %rax
cmpq $-0x1, 0x28(%rax)
je 0x24ae1
movq 0x70(%rsp), %rax
movzbl 0x26(%rax), %eax
movl %eax, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
subq $0x1, %rax
movq %rax, 0x20(%rsp)
movq $0x0, 0x40(%rsp)
movq $0x0, 0x50(%rsp)
movq 0x70(%rsp), %rax
movzwl 0x24(%rax), %ecx
andl $-0x10, %ecx
movw %cx, 0x24(%rax)
movq 0x70(%rsp), %rax
movzwl 0x24(%rax), %ecx
orl $0x4, %ecx
movw %cx, 0x24(%rax)
cmpq $0x0, 0x60(%rsp)
jne 0x247a1
movq 0x70(%rsp), %rax
movq %rax, 0x60(%rsp)
cmpq $0x0, 0x68(%rsp)
je 0x24795
movq 0x70(%rsp), %rax
movzwl 0x24(%rax), %ecx
orl $0xc00, %ecx            # imm = 0xC00
movw %cx, 0x24(%rax)
movq $0x0, 0x30(%rsp)
movq 0x70(%rsp), %rax
movq 0x30(%rax), %rax
movq 0x70(%rsp), %rcx
movq 0x30(%rcx), %rcx
subq 0x78(%rsp), %rcx
movq 0x20(%rsp), %rdx
xorq $-0x1, %rdx
andq %rdx, %rcx
subq %rcx, %rax
subq 0x78(%rsp), %rax
movq %rax, 0x20581(%rip)     # 0x44cd0
movq 0x78(%rsp), %rdx
addq 0x20575(%rip), %rdx     # 0x44cd0
movq 0x70(%rsp), %rax
movq 0x30(%rax), %rcx
subq %rdx, %rcx
movq %rcx, 0x30(%rax)
movq 0x70(%rsp), %rax
cmpq $0x0, 0x28(%rax)
je 0x24793
movq 0x78(%rsp), %rdx
addq 0x2054d(%rip), %rdx     # 0x44cd0
movq 0x70(%rsp), %rax
movq 0x28(%rax), %rcx
subq %rdx, %rcx
movq %rcx, 0x28(%rax)
jmp 0x2479f
movq 0x78(%rsp), %rax
movq %rax, 0x30(%rsp)
jmp 0x247a1
movq 0x80(%rsp), %rax
movq 0x1c0(%rax), %rax
movq %rax, 0x58(%rsp)
movq $0x0, 0x48(%rsp)
movq $0x0, 0x40(%rsp)
movq 0x58(%rsp), %rax
cmpq $0x0, (%rax)
je 0x24a2c
movq 0x58(%rsp), %rax
movq 0x38(%rax), %rax
movq 0x70(%rsp), %rcx
cmpq 0x30(%rcx), %rax
jb 0x24a18
movq 0x58(%rsp), %rax
cmpq $0x0, 0x48(%rax)
je 0x24a18
movq 0x58(%rsp), %rax
movq 0x38(%rax), %rax
movq 0x58(%rsp), %rcx
addq 0x48(%rcx), %rax
movq 0x70(%rsp), %rcx
cmpq 0x40(%rcx), %rax
ja 0x24a18
movq 0x58(%rsp), %rax
movzbl 0x21(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x24a18
movq 0x58(%rsp), %rax
movzbl 0x22(%rax), %edi
callq 0x24d30
movzwl %ax, %edx
movq 0x70(%rsp), %rax
movzwl 0x24(%rax), %ecx
orl %edx, %ecx
movw %cx, 0x24(%rax)
movq 0x58(%rsp), %rax
movzbl 0x23(%rax), %eax
movq 0x70(%rsp), %rcx
movzbl 0x26(%rcx), %ecx
cmpl %ecx, %eax
jle 0x2487a
movq 0x58(%rsp), %rax
movb 0x23(%rax), %cl
movq 0x70(%rsp), %rax
movb %cl, 0x26(%rax)
movq 0x58(%rsp), %rax
movzbl 0x21(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x248a2
cmpq $0x0, 0x50(%rsp)
jne 0x2489d
movq 0x58(%rsp), %rax
movq %rax, 0x50(%rsp)
jmp 0x24941
cmpq $0x0, 0x50(%rsp)
je 0x2493f
movq 0x50(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rax
cmpq 0x58(%rsp), %rax
je 0x2490b
movq 0x10(%rsp), %rax
movzbl 0x21(%rax), %ecx
andl $-0x3, %ecx
movb %cl, 0x21(%rax)
movq 0x10(%rsp), %rax
movzbl 0x20(%rax), %eax
cmpl $0x3, %eax
jne 0x248ea
movq 0x10(%rsp), %rax
movb $0x2, 0x20(%rax)
movq 0x10(%rsp), %rax
movq 0x48(%rax), %rcx
movq 0x10(%rsp), %rax
movq %rcx, 0x50(%rax)
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x10(%rsp)
jmp 0x248b8
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rsi
movq 0x50(%rsp), %rax
movq 0x18(%rax), %rdx
movq 0x58(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x18(%rax), %rcx
movl $0x52, %edi
movb $0x0, %al
callq 0x8c00
movq $0x0, 0x50(%rsp)
jmp 0x24941
cmpq $0x0, 0x40(%rsp)
je 0x249ed
cmpq $0x0, 0x50(%rsp)
jne 0x249ed
movq 0x58(%rsp), %rax
movq 0x38(%rax), %rax
movq 0x40(%rsp), %rcx
movq 0x38(%rcx), %rcx
movq 0x40(%rsp), %rdx
addq 0x50(%rdx), %rcx
cmpq %rcx, %rax
jb 0x249a2
movq 0x58(%rsp), %rax
movq 0x38(%rax), %rcx
movq 0x40(%rsp), %rax
movq 0x38(%rax), %rax
movq 0x40(%rsp), %rdx
addq 0x50(%rdx), %rax
subq %rax, %rcx
movq 0x40(%rsp), %rax
movq %rcx, 0x58(%rax)
jmp 0x249eb
movq 0x58(%rsp), %rax
cmpq $0x0, 0x38(%rax)
jbe 0x249e9
movq 0x40(%rsp), %rax
movq 0x18(%rax), %rsi
movq 0x40(%rsp), %rax
movq 0x38(%rax), %rdx
movq 0x40(%rsp), %rax
movq 0x38(%rax), %rcx
movq 0x40(%rsp), %rax
addq 0x50(%rax), %rcx
movq 0x58(%rsp), %rax
movq 0x38(%rax), %r8
leaq 0x11bd3(%rip), %rdi     # 0x365b5
movb $0x0, %al
callq 0x8ae0
jmp 0x249eb
jmp 0x249ed
movq 0x58(%rsp), %rax
movq %rax, 0x40(%rsp)
cmpq $0x0, 0x50(%rsp)
je 0x24a07
cmpq $0x0, 0x48(%rsp)
jne 0x24a16
movq 0x58(%rsp), %rax
movq %rax, 0x48(%rsp)
movq %rax, 0x38(%rsp)
jmp 0x24a18
jmp 0x24a1a
movq 0x58(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x58(%rsp)
jmp 0x247c7
movq 0x70(%rsp), %rax
movq 0x30(%rax), %rax
andq 0x20(%rsp), %rax
movq 0x30(%rsp), %rcx
andq 0x20(%rsp), %rcx
subq %rcx, %rax
movq %rax, 0x18(%rsp)
cmpq $0x0, 0x18(%rsp)
jge 0x24a69
movq 0x18(%rsp), %rax
addq 0x20(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x8(%rsp)
jmp 0x24a73
movq 0x18(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x8(%rsp), %rcx
movq 0x70(%rsp), %rax
movq %rcx, 0x50(%rax)
cmpq $0x0, 0x48(%rsp)
je 0x24aa4
movq 0x48(%rsp), %rax
movq 0x38(%rax), %rcx
movq 0x48(%rsp), %rax
addq 0x50(%rax), %rcx
movq 0x70(%rsp), %rax
movq %rcx, 0x38(%rax)
movq 0x70(%rsp), %rax
movq 0x50(%rax), %rax
addq 0x30(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x30(%rsp), %rcx
movq 0x70(%rsp), %rax
movq %rcx, 0x48(%rax)
movq 0x70(%rsp), %rax
movq 0x38(%rax), %rax
movq 0x70(%rsp), %rcx
subq 0x30(%rcx), %rax
addq 0x30(%rsp), %rax
movq %rax, 0x30(%rsp)
jmp 0x24ae3
movq 0x70(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x70(%rsp)
jmp 0x2463f
cmpq $0x0, 0x68(%rsp)
je 0x24b8f
cmpq $0x0, 0x60(%rsp)
je 0x24b8f
movq 0x68(%rsp), %rax
movzwl 0x24(%rax), %ecx
andl $-0x10, %ecx
movw %cx, 0x24(%rax)
movq 0x68(%rsp), %rax
movzwl 0x24(%rax), %ecx
orl $0x5, %ecx
movw %cx, 0x24(%rax)
movq 0x60(%rsp), %rax
movq 0x30(%rax), %rcx
addq $0x34, %rcx
movq 0x68(%rsp), %rax
movq %rcx, 0x30(%rax)
movq 0x68(%rsp), %rax
movq 0x30(%rax), %rcx
movq 0x68(%rsp), %rax
movq %rcx, 0x28(%rax)
movq 0x68(%rsp), %rax
movq 0x30(%rax), %rcx
addq 0x78(%rsp), %rcx
subq $0x34, %rcx
movq 0x68(%rsp), %rax
movq %rcx, 0x40(%rax)
movq 0x68(%rsp), %rax
movq 0x40(%rax), %rcx
movq 0x68(%rsp), %rax
movq %rcx, 0x38(%rax)
movq 0x68(%rsp), %rax
movq $0x34, 0x48(%rax)
movq 0x80(%rsp), %rax
movq 0x1f8(%rax), %rax
movq %rax, 0x70(%rsp)
movl $0x0, 0x2c(%rsp)
cmpq $0x0, 0x70(%rsp)
je 0x24d19
movq 0x70(%rsp), %rax
movzwl 0x24(%rax), %eax
andl $0x10, %eax
cmpl $0x0, %eax
je 0x24d05
movq 0x70(%rsp), %rax
cmpq $-0x1, 0x30(%rax)
je 0x24d05
movq 0x70(%rsp), %rax
cmpq $-0x1, 0x28(%rax)
je 0x24d05
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
movq 0x70(%rsp), %rax
cmpl $0x1, 0x20(%rax)
je 0x24d03
movq 0x70(%rsp), %rax
cmpl $0x6, 0x20(%rax)
je 0x24d03
movq 0x70(%rsp), %rax
cmpl $0x0, 0x20(%rax)
je 0x24d03
movq 0x70(%rsp), %rax
movzwl 0x24(%rax), %ecx
andl $-0x10, %ecx
movw %cx, 0x24(%rax)
movq 0x80(%rsp), %rax
movq 0x1c0(%rax), %rax
movq %rax, 0x48(%rsp)
movq %rax, 0x58(%rsp)
movq 0x58(%rsp), %rax
cmpq $0x0, (%rax)
je 0x24ce8
movq 0x58(%rsp), %rax
movq 0x38(%rax), %rax
movq 0x70(%rsp), %rcx
cmpq 0x30(%rcx), %rax
jb 0x24cd4
movq 0x58(%rsp), %rax
cmpq $0x0, 0x48(%rax)
je 0x24cd4
movq 0x58(%rsp), %rax
movq 0x38(%rax), %rax
movq 0x58(%rsp), %rcx
addq 0x48(%rcx), %rax
movq 0x70(%rsp), %rcx
cmpq 0x40(%rcx), %rax
ja 0x24cd4
movq 0x58(%rsp), %rax
movzbl 0x21(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x24cd4
movq 0x58(%rsp), %rax
movzbl 0x22(%rax), %edi
callq 0x24d30
movzwl %ax, %edx
movq 0x70(%rsp), %rax
movzwl 0x24(%rax), %ecx
orl %edx, %ecx
movw %cx, 0x24(%rax)
movq 0x58(%rsp), %rax
movq %rax, 0x48(%rsp)
jmp 0x24cd6
movq 0x58(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x58(%rsp)
jmp 0x24c4d
movq 0x48(%rsp), %rax
movq 0x38(%rax), %rcx
movq 0x48(%rsp), %rax
addq 0x50(%rax), %rcx
movq 0x70(%rsp), %rax
movq %rcx, 0x38(%rax)
jmp 0x24d05
jmp 0x24d07
movq 0x70(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x70(%rsp)
jmp 0x24bab
movl 0x2c(%rsp), %eax
addq $0x88, %rsp
retq
nopw %cs:(%rax,%rax)