// File: /workspace/github_c_makefile_star2_O3/deplinenoise[P]vlink/t_elf32ppcbe.c
// Function: morphos_writeexec
static void morphos_writeexec(struct GlobalVars *gv,FILE *f)
/* creates a target-elf32morphos executable file, which in */
/* reality is a relocatable object too, but all symbol references have */
/* been resolved */
{
  struct LinkedSection *ls;
  int sdflag=0,sbflag=0;

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

    if (ls->flags & SF_SMALLDATA) {
      /* data sections should be called .sdata, and bss sections .sbss */

      if (ls->type == ST_DATA) {  /* .sdata */
        struct Section *bss_sec = find_first_bss_sec(ls);

        if (sdflag)
          error(124,fff[gv->dest_format]->tname);
        sdflag = 1;
        ls->name = sdata_name;

        if (bss_sec && (ls->filesize < ls->size)) {
          /* put bss-part of .sdata into an own section called .sbss */
          struct LinkedSection *sbss;
          struct Section *sec = bss_sec;
          struct Section *nextsec;
          struct Symbol *sym = (struct Symbol *)ls->symbols.first;
          struct Symbol *nextsym;
          unsigned long bss_offset = bss_sec->offset;
          lword bss_va = (lword)(ls->base + bss_offset);

          sbss = create_lnksect(gv,sbss_name,ST_UDATA,
                                ls->flags|SF_UNINITIALIZED,
                                ls->protection,ls->alignment);
          sbss->size = ls->size - bss_sec->offset;
          sbss->data = alloc(sbss->size);

          /* move bss sections from .sdata to .sbss */
          while (nextsec = (struct Section *)sec->n.next) {
            remnode(&sec->n);
            addtail(&sbss->sections,&sec->n);
            sec->lnksec = sbss;
            sec->offset -= bss_offset;
            sec = nextsec;
          }

          /* move symbols */
          while (nextsym = (struct Symbol *)sym->n.next) {
            /* move bss symbols to .sbss */
            if (sym->relsect->lnksec == sbss) {
              remnode(&sym->n);
              addtail(&sbss->symbols,&sym->n);
              sym->value -= bss_va;
            }
            sym = nextsym;
          }
          /* no relocs and no xrefs can occur in bss */

          ls->size -= sbss->size;  /* the bss part is gone */
        }
      }

      else if (ls->type == ST_UDATA) {
        if (sbflag)
          error(124,fff[gv->dest_format]->tname);
        sbflag = 1;
        ls->name = sbss_name;
      }
    }

    /* set filesize to memsize for all sections */
    if (!(ls->flags & SF_UNINITIALIZED))
      ls->filesize = ls->size;

    /* setup ddrelocs section, if present */
    if (!strcmp(ls->name,ddrelocs_name))
      make_ddrelocs(gv,ls);
  }

  elf32_writeobject(gv,f,EM_PPC,_BIG_ENDIAN_,ppc32_reloc_vlink2elf);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, 0x20(%rsp)
movq %rdi, %rbx
movq 0x1c0(%rdi), %r15
cmpq $0x0, (%r15)
je 0x19ba0
leaq 0x161f7(%rip), %r12     # 0x2fb9e
leaq 0x8bfa(%rip), %r13      # 0x225a8
leaq 0x157cb(%rip), %rbp     # 0x2f180
movl $0x0, 0x4(%rsp)
xorl %r14d, %r14d
movq %rbx, 0x18(%rsp)
testb $0x4, 0x21(%r15)
je 0x19b69
movzbl 0x20(%r15), %eax
cmpl $0x3, %eax
je 0x19b42
cmpl $0x2, %eax
jne 0x19b69
movl %r14d, (%rsp)
movq %r15, %rdi
callq 0xbdfa
movq %rax, %r14
cmpl $0x0, 0x4(%rsp)
je 0x19a15
movzbl 0x50(%rbx), %eax
movq (%rbp,%rax,8), %rax
movq (%rax), %rsi
movl $0x7c, %edi
xorl %eax, %eax
callq 0x6f14
leaq 0x8b85(%rip), %rax      # 0x225a1
movq %rax, 0x18(%r15)
movl $0x1, 0x4(%rsp)
testq %r14, %r14
je 0x19b3c
movq 0x50(%r15), %rax
cmpq 0x48(%r15), %rax
jae 0x19b3c
movq 0x40(%r15), %rax
movq %rax, 0x8(%rsp)
movq 0xb0(%r15), %rbp
movq 0x38(%r14), %rax
movq %rax, 0x10(%rsp)
movzbl 0x21(%r15), %ecx
movzbl 0x22(%r15), %r8d
movzbl 0x23(%r15), %r9d
orl $0x2, %ecx
movq %rbx, %rdi
movq %r13, %rsi
movl $0x3, %edx
callq 0xc37c
movq %rax, %r13
movq 0x48(%r15), %rdi
subq 0x38(%r14), %rdi
movq %rdi, 0x48(%rax)
callq 0x6638
movq %rax, 0x78(%r13)
movq (%r14), %rax
testq %rax, %rax
je 0x19acc
leaq 0x60(%r13), %r12
movq %rax, %rbx
movq %r14, %rdi
callq 0x6759
movq %r12, %rdi
movq %r14, %rsi
callq 0x6729
movq %r13, 0x18(%r14)
movq 0x10(%rsp), %rax
subq %rax, 0x38(%r14)
movq (%rbx), %rax
movq %rbx, %r14
testq %rax, %rax
jne 0x19a9e
movq 0x10(%rsp), %rcx
movq (%rbp), %rax
testq %rax, %rax
leaq 0x160bf(%rip), %r12     # 0x2fb9e
je 0x19b21
leaq 0xb0(%r13), %rbx
addq %rcx, 0x8(%rsp)
movq %rax, %r14
movq 0x38(%rbp), %rax
cmpq %r13, 0x18(%rax)
jne 0x19b16
movq %rbp, %rdi
callq 0x6759
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x6729
movq 0x8(%rsp), %rax
subq %rax, 0x30(%rbp)
movq (%r14), %rax
movq %r14, %rbp
testq %rax, %rax
jne 0x19aed
movq 0x48(%r13), %rax
subq %rax, 0x48(%r15)
movq 0x18(%rsp), %rbx
leaq 0x8a73(%rip), %r13      # 0x225a8
leaq 0x15644(%rip), %rbp     # 0x2f180
movl (%rsp), %r14d
jmp 0x19b69
testl %r14d, %r14d
je 0x19b5f
movzbl 0x50(%rbx), %eax
movq (%rbp,%rax,8), %rax
movq (%rax), %rsi
movl $0x7c, %edi
xorl %eax, %eax
callq 0x6f14
movq %r13, 0x18(%r15)
movl $0x1, %r14d
testb $0x2, 0x21(%r15)
jne 0x19b78
movq 0x48(%r15), %rax
movq %rax, 0x50(%r15)
movq 0x18(%r15), %rdi
movq %r12, %rsi
callq 0x5160
testl %eax, %eax
jne 0x19b93
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1a10a
movq (%r15), %r15
cmpq $0x0, (%r15)
jne 0x199c5
leaq 0x158(%rip), %r8        # 0x19cff
movq %rbx, %rdi
movq 0x20(%rsp), %rsi
movl $0x14, %edx
movl $0x1, %ecx
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x186f7