// File: /workspace/github_c_makefile_star2_O2/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, 0x18(%rsp)
movq %rdi, %rbx
leaq 0x1c0(%rdi), %r15
leaq 0x17b91(%rip), %r14     # 0x2ff8e
leaq 0x81b4(%rip), %r13      # 0x205b8
pushq $0x1
popq %r12
movl $0x0, 0x4(%rsp)
xorl %ebp, %ebp
movq %rdi, 0x20(%rsp)
movq (%r15), %r15
cmpq $0x0, (%r15)
je 0x185ea
testb $0x4, 0x21(%r15)
je 0x185b7
movzbl 0x20(%r15), %eax
cmpl $0x3, %eax
je 0x1851a
cmpl $0x2, %eax
jne 0x185b7
movq %r15, %rdi
callq 0xb7d0
movq %rax, %r12
testl %ebp, %ebp
je 0x18471
movzbl 0x50(%rbx), %eax
leaq 0x16d30(%rip), %rcx     # 0x2f190
movq (%rcx,%rax,8), %rax
movq (%rax), %rsi
pushq $0x7c
popq %rdi
xorl %eax, %eax
callq 0x6c19
leaq 0x8139(%rip), %rax      # 0x205b1
movq %rax, 0x18(%r15)
testq %r12, %r12
je 0x18548
movq 0x50(%r15), %rax
cmpq 0x48(%r15), %rax
jae 0x18548
movq 0x40(%r15), %rax
movq %rax, 0x8(%rsp)
movq 0xb0(%r15), %r14
movq 0x38(%r12), %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
pushq $0x3
popq %rdx
callq 0xbcdb
movq %rax, %r13
movq 0x48(%r15), %rdi
subq 0x38(%r12), %rdi
movq %rdi, 0x48(%rax)
callq 0x6340
movq %rax, 0x78(%r13)
leaq 0x60(%r13), %rbp
movq (%r12), %rbx
testq %rbx, %rbx
je 0x18551
movq %r12, %rdi
callq 0x6444
movq %rbp, %rdi
movq %r12, %rsi
callq 0x6414
movq %r13, 0x18(%r12)
movq 0x10(%rsp), %rax
subq %rax, 0x38(%r12)
movq %rbx, %r12
jmp 0x184ea
cmpl $0x0, 0x4(%rsp)
je 0x1853d
movzbl 0x50(%rbx), %eax
leaq 0x16c64(%rip), %rcx     # 0x2f190
movq (%rcx,%rax,8), %rax
movq (%rax), %rsi
pushq $0x7c
popq %rdi
xorl %eax, %eax
callq 0x6c19
movq %r13, 0x18(%r15)
movl %r12d, 0x4(%rsp)
jmp 0x185b7
pushq $0x1
popq %r12
movl %r12d, %ebp
jmp 0x185b7
leaq 0xb0(%r13), %rbx
movq 0x10(%rsp), %rax
addq %rax, 0x8(%rsp)
pushq $0x1
popq %r12
movq %r14, %rbp
movq (%r14), %r14
testq %r14, %r14
je 0x18599
movq 0x38(%rbp), %rax
cmpq %r13, 0x18(%rax)
jne 0x18566
movq %rbp, %rdi
callq 0x6444
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x6414
movq 0x8(%rsp), %rax
subq %rax, 0x30(%rbp)
jmp 0x18566
movq 0x48(%r13), %rax
subq %rax, 0x48(%r15)
movl %r12d, %ebp
movq 0x20(%rsp), %rbx
leaq 0x179de(%rip), %r14     # 0x2ff8e
leaq 0x8001(%rip), %r13      # 0x205b8
testb $0x2, 0x21(%r15)
jne 0x185c6
movq 0x48(%r15), %rax
movq %rax, 0x50(%r15)
movq 0x18(%r15), %rdi
movq %r14, %rsi
callq 0x5160
testl %eax, %eax
jne 0x18417
movq %rbx, %rdi
movq %r15, %rsi
callq 0x18b38
jmp 0x18417
leaq 0x13d(%rip), %r8        # 0x1872e
pushq $0x14
popq %rdx
pushq $0x1
popq %rcx
movq %rbx, %rdi
movq 0x18(%rsp), %rsi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x172df