// File: /workspace/github_c_makefile_star2_O2/katlogic[P]ljx/src/host/buildvm_peobj.c
// Function: emit_peobj
void emit_peobj(BuildCtx *ctx)
{
  PEheader pehdr;
  PEsection pesect[PEOBJ_NSECTIONS];
  uint32_t sofs;
  int i, nrsym;
  union { uint8_t b; uint32_t u; } host_endian;

  sofs = sizeof(PEheader) + PEOBJ_NSECTIONS*sizeof(PEsection);

  /* Fill in PE sections. */
  memset(&pesect, 0, PEOBJ_NSECTIONS*sizeof(PEsection));
  memcpy(pesect[PEOBJ_SECT_TEXT].name, ".text", sizeof(".text")-1);
  pesect[PEOBJ_SECT_TEXT].ofs = sofs;
  sofs += (pesect[PEOBJ_SECT_TEXT].size = (uint32_t)ctx->codesz);
  pesect[PEOBJ_SECT_TEXT].relocofs = sofs;
  sofs += (pesect[PEOBJ_SECT_TEXT].nreloc = (uint16_t)ctx->nreloc) * PEOBJ_RELOC_SIZE;
  /* Flags: 60 = read+execute, 50 = align16, 20 = code. */
  pesect[PEOBJ_SECT_TEXT].flags = PEOBJ_TEXT_FLAGS;

#if LJ_TARGET_X64
  memcpy(pesect[PEOBJ_SECT_PDATA].name, ".pdata", sizeof(".pdata")-1);
  pesect[PEOBJ_SECT_PDATA].ofs = sofs;
  sofs += (pesect[PEOBJ_SECT_PDATA].size = 6*4);
  pesect[PEOBJ_SECT_PDATA].relocofs = sofs;
  sofs += (pesect[PEOBJ_SECT_PDATA].nreloc = 6) * PEOBJ_RELOC_SIZE;
  /* Flags: 40 = read, 30 = align4, 40 = initialized data. */
  pesect[PEOBJ_SECT_PDATA].flags = 0x40300040;

  memcpy(pesect[PEOBJ_SECT_XDATA].name, ".xdata", sizeof(".xdata")-1);
  pesect[PEOBJ_SECT_XDATA].ofs = sofs;
  sofs += (pesect[PEOBJ_SECT_XDATA].size = 8*2+4+6*2);  /* See below. */
  pesect[PEOBJ_SECT_XDATA].relocofs = sofs;
  sofs += (pesect[PEOBJ_SECT_XDATA].nreloc = 1) * PEOBJ_RELOC_SIZE;
  /* Flags: 40 = read, 30 = align4, 40 = initialized data. */
  pesect[PEOBJ_SECT_XDATA].flags = 0x40300040;
#elif LJ_TARGET_X86
  memcpy(pesect[PEOBJ_SECT_SXDATA].name, ".sxdata", sizeof(".sxdata")-1);
  pesect[PEOBJ_SECT_SXDATA].ofs = sofs;
  sofs += (pesect[PEOBJ_SECT_SXDATA].size = 4);
  pesect[PEOBJ_SECT_SXDATA].relocofs = sofs;
  /* Flags: 40 = read, 30 = align4, 02 = lnk_info, 40 = initialized data. */
  pesect[PEOBJ_SECT_SXDATA].flags = 0x40300240;
#endif

  memcpy(pesect[PEOBJ_SECT_RDATA_Z].name, ".rdata$Z", sizeof(".rdata$Z")-1);
  pesect[PEOBJ_SECT_RDATA_Z].ofs = sofs;
  sofs += (pesect[PEOBJ_SECT_RDATA_Z].size = (uint32_t)strlen(ctx->dasm_ident)+1);
  /* Flags: 40 = read, 30 = align4, 40 = initialized data. */
  pesect[PEOBJ_SECT_RDATA_Z].flags = 0x40300040;

  /* Fill in PE header. */
  pehdr.arch = PEOBJ_ARCH_TARGET;
  pehdr.nsects = PEOBJ_NSECTIONS;
  pehdr.time = 0;  /* Timestamp is optional. */
  pehdr.symtabofs = sofs;
  pehdr.opthdrsz = 0;
  pehdr.flags = 0;

  /* Compute the size of the symbol table:
  ** @feat.00 + nsections*2
  ** + asm_start + nsym
  ** + nrsym
  */
  nrsym = ctx->nrelocsym;
  pehdr.nsyms = 1+PEOBJ_NSECTIONS*2 + 1+ctx->nsym + nrsym;
#if LJ_TARGET_X64
  pehdr.nsyms += 1;  /* Symbol for lj_err_unwind_win. */
#endif

  /* Write PE object header and all sections. */
  owrite(ctx, &pehdr, sizeof(PEheader));
  owrite(ctx, &pesect, sizeof(PEsection)*PEOBJ_NSECTIONS);

  /* Write .text section. */
  host_endian.u = 1;
  if (host_endian.b != LJ_ENDIAN_SELECT(1, 0)) {
#if LJ_TARGET_PPC
    uint32_t *p = (uint32_t *)ctx->code;
    int n = (int)(ctx->codesz >> 2);
    for (i = 0; i < n; i++, p++)
      *p = lj_bswap(*p);  /* Byteswap .text section. */
#else
    fprintf(stderr, "Error: different byte order for host and target\n");
    exit(1);
#endif
  }
  owrite(ctx, ctx->code, ctx->codesz);
  for (i = 0; i < ctx->nreloc; i++) {
    PEreloc reloc;
    reloc.vaddr = (uint32_t)ctx->reloc[i].ofs + PEOBJ_RELOC_OFS;
    reloc.symidx = 1+2+ctx->reloc[i].sym;  /* Reloc syms are after .text sym. */
    reloc.type = ctx->reloc[i].type ? PEOBJ_RELOC_REL32 : PEOBJ_RELOC_DIR32;
    owrite(ctx, &reloc, PEOBJ_RELOC_SIZE);
  }

#if LJ_TARGET_X64
  { /* Write .pdata section. */
    uint32_t fcofs = (uint32_t)ctx->sym[ctx->nsym-1].ofs;
    uint32_t pdata[3];  /* Start of .text, end of .text and .xdata. */
    PEreloc reloc;
    pdata[0] = 0; pdata[1] = fcofs; pdata[2] = 0;
    owrite(ctx, &pdata, sizeof(pdata));
    pdata[0] = fcofs; pdata[1] = (uint32_t)ctx->codesz; pdata[2] = 20;
    owrite(ctx, &pdata, sizeof(pdata));
    reloc.vaddr = 0; reloc.symidx = 1+2+nrsym+2+2+1;
    reloc.type = PEOBJ_RELOC_ADDR32NB;
    owrite(ctx, &reloc, PEOBJ_RELOC_SIZE);
    reloc.vaddr = 4; reloc.symidx = 1+2+nrsym+2+2+1;
    reloc.type = PEOBJ_RELOC_ADDR32NB;
    owrite(ctx, &reloc, PEOBJ_RELOC_SIZE);
    reloc.vaddr = 8; reloc.symidx = 1+2+nrsym+2;
    reloc.type = PEOBJ_RELOC_ADDR32NB;
    owrite(ctx, &reloc, PEOBJ_RELOC_SIZE);
    reloc.vaddr = 12; reloc.symidx = 1+2+nrsym+2+2+1;
    reloc.type = PEOBJ_RELOC_ADDR32NB;
    owrite(ctx, &reloc, PEOBJ_RELOC_SIZE);
    reloc.vaddr = 16; reloc.symidx = 1+2+nrsym+2+2+1;
    reloc.type = PEOBJ_RELOC_ADDR32NB;
    owrite(ctx, &reloc, PEOBJ_RELOC_SIZE);
    reloc.vaddr = 20; reloc.symidx = 1+2+nrsym+2;
    reloc.type = PEOBJ_RELOC_ADDR32NB;
    owrite(ctx, &reloc, PEOBJ_RELOC_SIZE);
  }
  { /* Write .xdata section. */
    uint16_t xdata[8+2+6];
    PEreloc reloc;
    xdata[0] = 0x01|0x08|0x10;  /* Ver. 1, uhandler/ehandler, prolog size 0. */
    xdata[1] = 0x0005;  /* Number of unwind codes, no frame pointer. */
    xdata[2] = 0x4200;  /* Stack offset 4*8+8 = aword*5. */
    xdata[3] = 0x3000;  /* Push rbx. */
    xdata[4] = 0x6000;  /* Push rsi. */
    xdata[5] = 0x7000;  /* Push rdi. */
    xdata[6] = 0x5000;  /* Push rbp. */
    xdata[7] = 0;  /* Alignment. */
    xdata[8] = xdata[9] = 0;  /* Relocated address of exception handler. */
    xdata[10] = 0x01;  /* Ver. 1, no handler, prolog size 0. */
    xdata[11] = 0x1504;  /* Number of unwind codes, fp = rbp, fpofs = 16. */
    xdata[12] = 0x0300;  /* set_fpreg. */
    xdata[13] = 0x0200;  /* stack offset 0*8+8 = aword*1. */
    xdata[14] = 0x3000;  /* Push rbx. */
    xdata[15] = 0x5000;  /* Push rbp. */
    owrite(ctx, &xdata, sizeof(xdata));
    reloc.vaddr = 2*8; reloc.symidx = 1+2+nrsym+2+2;
    reloc.type = PEOBJ_RELOC_ADDR32NB;
    owrite(ctx, &reloc, PEOBJ_RELOC_SIZE);
  }
#elif LJ_TARGET_X86
  /* Write .sxdata section. */
  for (i = 0; i < nrsym; i++) {
    if (!strcmp(ctx->relocsym[i], "_lj_err_unwind_win")) {
      uint32_t symidx = 1+2+i;
      owrite(ctx, &symidx, 4);
      break;
    }
  }
  if (i == nrsym) {
    fprintf(stderr, "Error: extern lj_err_unwind_win not used\n");
    exit(1);
  }
#endif

  /* Write .rdata$Z section. */
  owrite(ctx, ctx->dasm_ident, strlen(ctx->dasm_ident)+1);

  /* Write symbol table. */
  strtab = NULL;  /* 1st pass: collect string sizes. */
  for (;;) {
    strtabofs = 4;
    /* Mark as SafeSEH compliant. */
    emit_peobj_sym(ctx, "@feat.00", 1,
		   PEOBJ_SECT_ABS, PEOBJ_TYPE_NULL, PEOBJ_SCL_STATIC);

    emit_peobj_sym_sect(ctx, pesect, PEOBJ_SECT_TEXT);
    for (i = 0; i < nrsym; i++)
      emit_peobj_sym(ctx, ctx->relocsym[i], 0,
		     PEOBJ_SECT_UNDEF, PEOBJ_TYPE_FUNC, PEOBJ_SCL_EXTERN);

#if LJ_TARGET_X64
    emit_peobj_sym_sect(ctx, pesect, PEOBJ_SECT_PDATA);
    emit_peobj_sym_sect(ctx, pesect, PEOBJ_SECT_XDATA);
    emit_peobj_sym(ctx, "lj_err_unwind_win", 0,
		   PEOBJ_SECT_UNDEF, PEOBJ_TYPE_FUNC, PEOBJ_SCL_EXTERN);
#elif LJ_TARGET_X86
    emit_peobj_sym_sect(ctx, pesect, PEOBJ_SECT_SXDATA);
#endif

    emit_peobj_sym(ctx, ctx->beginsym, 0,
		   PEOBJ_SECT_TEXT, PEOBJ_TYPE_NULL, PEOBJ_SCL_EXTERN);
    for (i = 0; i < ctx->nsym; i++)
      emit_peobj_sym(ctx, ctx->sym[i].name, (uint32_t)ctx->sym[i].ofs,
		     PEOBJ_SECT_TEXT, PEOBJ_TYPE_FUNC, PEOBJ_SCL_EXTERN);

    emit_peobj_sym_sect(ctx, pesect, PEOBJ_SECT_RDATA_Z);

    if (strtab)
      break;
    /* 2nd pass: alloc strtab, write syms and copy strings. */
    strtab = (char *)malloc(strtabofs);
    *(uint32_t *)strtab = (uint32_t)strtabofs;
  }

  /* Write string table. */
  owrite(ctx, strtab, strtabofs);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %rdi, %rbx
leaq 0x55(%rsp), %r14
xorl %r12d, %r12d
movl $0x97, %edx
movq %r14, %rdi
xorl %esi, %esi
callq 0x50c0
movl $0x7865742e, -0x5(%r14) # imm = 0x7865742E
movb $0x74, -0x1(%r14)
movl $0xb4, %ecx
movl %ecx, 0xf(%r14)
movl 0x30(%rbx), %eax
movl %eax, 0xb(%r14)
addl %eax, %ecx
movl %ecx, 0x13(%r14)
movl 0x44(%rbx), %ecx
movzwl %cx, %ecx
movw %cx, 0x1b(%r14)
imull $0xa, %ecx, %ecx
leal (%rcx,%rax), %r15d
addl $0xb4, %r15d
movabsq $0x6164702e60500020, %rdx # imm = 0x6164702E60500020
movq %rdx, 0x1f(%r14)
movw $0x6174, %dx            # imm = 0x6174
movw %dx, 0x27(%r14)
movl %r15d, 0x37(%r14)
movl $0x18, 0x33(%r14)
leal 0xcc(%rcx,%rax), %esi
movl %esi, 0x3b(%r14)
movw $0x6, 0x43(%r14)
leal 0x108(%rcx,%rax), %esi
movabsq $0x6164782e40300040, %rdi # imm = 0x6164782E40300040
movq %rdi, 0x47(%r14)
movw %dx, 0x4f(%r14)
movl %esi, 0x5f(%r14)
movl $0x20, 0x5b(%r14)
leal 0x128(%rcx,%rax), %edx
movl %edx, 0x63(%r14)
movw $0x1, 0x6b(%r14)
leal 0x132(%rcx,%rax), %eax
movl $0x40300040, %ebp       # imm = 0x40300040
movl %ebp, 0x6f(%r14)
movabsq $0x5a2461746164722e, %rcx # imm = 0x5A2461746164722E
movq %rcx, 0x73(%r14)
movl %eax, 0x87(%r14)
movq 0x88(%rbx), %rdi
callq 0x50a0
leal 0x1(%rax), %ecx
movl %ecx, 0x83(%r14)
addl %r15d, %eax
addl $0x7f, %eax
movl %ebp, 0x97(%r14)
leaq 0x3c(%rsp), %rsi
movq $0x48664, (%rsi)        # imm = 0x48664
movl %eax, 0x8(%rsi)
andl $0x0, 0x10(%rsi)
movl 0x40(%rbx), %eax
movl 0x48(%rbx), %r13d
addl %r13d, %eax
addl $0xb, %eax
movl %eax, 0xc(%rsi)
pushq $0x14
popq %rdx
movq %rbx, %rdi
callq 0x6248
leaq 0x50(%rsp), %rsi
movl $0xa0, %edx
movq %rbx, %rdi
callq 0x6248
movq 0x28(%rbx), %rsi
movq 0x30(%rbx), %rdx
movq %rbx, %rdi
callq 0x6248
leaq 0xa0(%rbx), %rbp
leaq 0x10(%rsp), %r14
pushq $0xa
popq %r15
movslq 0x44(%rbx), %rax
cmpq %rax, %r12
jge 0x9447
movl -0x8(%rbp), %eax
movl %eax, 0x10(%rsp)
movl -0x4(%rbp), %eax
addl $0x3, %eax
movl %eax, 0x14(%rsp)
xorl %eax, %eax
cmpl $0x0, (%rbp)
setne %al
leal 0x2(%rax,%rax), %eax
movw %ax, 0x18(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x6248
incq %r12
addq $0xc, %rbp
jmp 0x9404
movq 0x58(%rbx), %rax
movslq 0x40(%rbx), %rcx
shlq $0x4, %rcx
movl -0x8(%rax,%rcx), %ebp
leaq 0x10(%rsp), %r14
andl $0x0, (%r14)
movl %ebp, 0x4(%r14)
andl $0x0, 0x8(%r14)
pushq $0xc
popq %r15
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x6248
movl %ebp, (%r14)
movl 0x30(%rbx), %eax
movl %eax, 0x4(%r14)
pushq $0x14
popq %rax
movl %eax, 0x8(%r14)
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x6248
leaq 0x30(%rsp), %r15
andl $0x0, (%r15)
leal 0x5(%r13), %r14d
leal 0x7(%r13), %eax
movl %eax, 0x8(%rsp)
leal 0x8(%r13), %r12d
movl %r12d, 0x4(%r15)
movw $0x3, %ax
movw %ax, 0x8(%r15)
pushq $0xa
popq %rbp
movq %rbx, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x6248
movl $0x4, (%r15)
movl %r12d, 0x4(%r15)
movw $0x3, %ax
movw %ax, 0x8(%r15)
movq %rbx, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x6248
movl $0x8, (%r15)
movl %r14d, 0x4(%r15)
movw $0x3, %ax
movw %ax, 0x8(%r15)
movq %rbx, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x6248
movl $0xc, (%r15)
movl %r12d, 0x4(%r15)
movw $0x3, %ax
movw %ax, 0x8(%r15)
movq %rbx, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x6248
pushq $0x10
popq %rax
movl %eax, (%r15)
movl %r12d, 0x4(%r15)
movw $0x3, %ax
movw %ax, 0x8(%r15)
movw $0x3, %r12w
movq %rbx, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x6248
pushq $0x14
popq %rax
movl %eax, (%r15)
movl %r14d, 0x4(%r15)
movw %r12w, 0x8(%r15)
movw $0x3, %r12w
movq %rbx, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x6248
movaps 0x7c9a(%rip), %xmm0     # 0x11220
leaq 0x10(%rsp), %rax
movaps %xmm0, (%rax)
movaps 0x7c9b(%rip), %xmm0     # 0x11230
movaps %xmm0, 0x10(%rax)
leaq 0x10(%rsp), %rsi
pushq $0x20
popq %rdx
movq %rbx, %rdi
callq 0x6248
pushq $0x10
popq %rax
movl %eax, (%r15)
movl 0x8(%rsp), %eax
movl %eax, 0x4(%r15)
movw %r12w, 0x8(%r15)
leaq 0x30(%rsp), %rsi
movq %rbx, %rdi
movq %rbp, %rdx
callq 0x6248
movq 0x88(%rbx), %r14
movq %r14, %rdi
callq 0x50a0
leaq 0x1(%rax), %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6248
andq $0x0, 0xcc16(%rip)      # 0x16208
xorl %eax, %eax
testl %r13d, %r13d
cmovlel %eax, %r13d
pushq $0x2
popq %r14
pushq $-0x1
popq %r15
pushq $0x20
popq %rbp
movq %r13, 0x8(%rsp)
movq $0x4, 0xcbfa(%rip) # 0x16210
movq %rbx, %rdi
leaq 0x7c34(%rip), %rsi      # 0x11254
pushq $0x1
popq %rdx
pushq $-0x2
popq %rcx
xorl %r8d, %r8d
pushq $0x3
popq %r9
callq 0x974c
movq %rbx, %rdi
leaq 0x50(%rsp), %rsi
xorl %edx, %edx
callq 0x9850
xorl %r12d, %r12d
cmpq %r12, %r13
je 0x9669
movq 0x60(%rbx), %rax
movq (%rax,%r12,8), %rsi
movq %rbx, %rdi
xorl %edx, %edx
movl %r15d, %ecx
movl %ebp, %r8d
movl %r14d, %r9d
callq 0x974c
incq %r12
jmp 0x9644
movq %rbx, %rdi
leaq 0x50(%rsp), %r12
movq %r12, %rsi
pushq $0x1
popq %rdx
callq 0x9850
movq %rbx, %rdi
movq %r12, %rsi
movl %r14d, %edx
callq 0x9850
movq %rbx, %rdi
leaq 0x7bc9(%rip), %rsi      # 0x1125d
xorl %edx, %edx
movl %r15d, %ecx
movl %ebp, %r8d
movl %r14d, %r9d
callq 0x974c
movq 0x70(%rbx), %rsi
movq %rbx, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
movl %r14d, %r9d
callq 0x974c
pushq $0x8
popq %r12
xorl %r13d, %r13d
movslq 0x40(%rbx), %rax
cmpq %rax, %r13
jge 0x96f0
movq 0x58(%rbx), %rax
movq -0x8(%rax,%r12), %rsi
movl (%rax,%r12), %edx
movq %rbx, %rdi
xorl %ecx, %ecx
movl %ebp, %r8d
movl %r14d, %r9d
callq 0x974c
incq %r13
addq $0x10, %r12
jmp 0x96c1
movq %rbx, %rdi
leaq 0x50(%rsp), %rsi
pushq $0x3
popq %rdx
callq 0x9850
movq 0xcb01(%rip), %rsi      # 0x16208
movq 0xcb02(%rip), %r12      # 0x16210
testq %rsi, %rsi
jne 0x972f
movq %r12, %rdi
callq 0x5150
movq %rax, 0xcae6(%rip)      # 0x16208
movl %r12d, (%rax)
movq 0x8(%rsp), %r13
jmp 0x960b
movq %rbx, %rdi
movq %r12, %rdx
callq 0x6248
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq