// File: /workspace/github_c_makefile_star2_O2/fanf2[P]luajit-2/src/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;

  host_endian.u = 1;
  if (host_endian.b != LJ_ENDIAN_SELECT(1, 0)) {
    fprintf(stderr, "Error: different byte order for host and target\n");
    exit(1);
  }

  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 = 0x60500020;

#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 = 3*4);
  pesect[PEOBJ_SECT_PDATA].relocofs = sofs;
  sofs += (pesect[PEOBJ_SECT_PDATA].nreloc = 3) * 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);  /* 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;
#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_win64. */
#endif

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

  /* Write .text section. */
  owrite(ctx, ctx->code, ctx->codesz);
  for (i = 0; i < ctx->nreloc; i++) {
    PEreloc reloc;
    reloc.vaddr = (uint32_t)ctx->reloc[i].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 pdata[3];  /* Start of .text, end of .text and .xdata. */
    PEreloc reloc;
    pdata[0] = 0; pdata[1] = (uint32_t)ctx->codesz; pdata[2] = 0;
    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);
  }
  { /* Write .xdata section. */
    uint16_t xdata[8+2];
    PEreloc reloc;
    xdata[0] = 0x01|0x08|0x10;  /* Ver. 1, uhander/ehandler, prolog size 0. */
    xdata[1] = 5;  /* 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. */
    owrite(ctx, &xdata, sizeof(xdata));
    reloc.vaddr = sizeof(xdata)-4; reloc.symidx = 1+2+nrsym+2+2;
    reloc.type = PEOBJ_RELOC_ADDR32NB;
    owrite(ctx, &reloc, PEOBJ_RELOC_SIZE);
  }
#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_win64", 0,
		   PEOBJ_SECT_UNDEF, PEOBJ_TYPE_FUNC, PEOBJ_SCL_EXTERN);
#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 0x40c0
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 $0xc, 0x33(%r14)
leal 0xc0(%rcx,%rax), %esi
movl %esi, 0x3b(%r14)
movw $0x3, 0x43(%r14)
leal 0xde(%rcx,%rax), %esi
movabsq $0x6164782e40300040, %rdi # imm = 0x6164782E40300040
movq %rdi, 0x47(%r14)
movw %dx, 0x4f(%r14)
movl %esi, 0x5f(%r14)
movl $0x14, 0x5b(%r14)
leal 0xf2(%rcx,%rax), %edx
movl %edx, 0x63(%r14)
movw $0x1, 0x6b(%r14)
leal 0xfc(%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 0x80(%rbx), %rdi
callq 0x40a0
leal 0x1(%rax), %ecx
movl %ecx, 0x83(%r14)
addl %r15d, %eax
addl $0x49, %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 0x5133
leaq 0x50(%rsp), %rsi
movl $0xa0, %edx
movq %rbx, %rdi
callq 0x5133
movq 0x28(%rbx), %rsi
movq 0x30(%rbx), %rdx
movq %rbx, %rdi
callq 0x5133
leaq 0x98(%rbx), %rbp
leaq 0x10(%rsp), %r14
pushq $0xa
popq %r15
movslq 0x44(%rbx), %rax
cmpq %rax, %r12
jge 0x86b7
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 0x5133
incq %r12
addq $0xc, %rbp
jmp 0x8674
leaq 0x10(%rsp), %r14
andl $0x0, (%r14)
movl 0x30(%rbx), %eax
movl %eax, 0x4(%r14)
andl $0x0, 0x8(%r14)
pushq $0xc
popq %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x5133
leaq 0x30(%rsp), %r15
andl $0x0, (%r15)
leal 0x5(%r13), %eax
movl %eax, 0xc(%rsp)
leal 0x7(%r13), %eax
movl %eax, (%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 0x5133
movl $0x4, (%r15)
movl %r12d, 0x4(%r15)
movw $0x3, %ax
movw %ax, 0x8(%r15)
movq %rbx, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x5133
movl $0x8, (%r15)
movl 0xc(%rsp), %eax
movl %eax, 0x4(%r15)
movw $0x3, %ax
movw %ax, 0x8(%r15)
movw $0x3, %r12w
movq %rbx, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x5133
movaps 0x7228(%rip), %xmm0     # 0xf990
movaps %xmm0, (%r14)
andl $0x0, 0x10(%r14)
leaq 0x10(%rsp), %rsi
pushq $0x14
popq %rdx
movq %rbx, %rdi
callq 0x5133
movl $0x10, (%r15)
movl (%rsp), %eax
movl %eax, 0x4(%r15)
movw %r12w, 0x8(%r15)
leaq 0x30(%rsp), %rsi
movq %rbx, %rdi
movq %rbp, %rdx
callq 0x5133
movq 0x80(%rbx), %r14
movq %r14, %rdi
callq 0x40a0
leaq 0x1(%rax), %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x5133
andq $0x0, 0xb9de(%rip)      # 0x141a8
xorl %eax, %eax
testl %r13d, %r13d
cmovlel %eax, %r13d
pushq $0x2
popq %r14
pushq $-0x1
popq %r15
pushq $0x20
popq %rbp
movq %r13, (%rsp)
movq $0x4, 0xb9c3(%rip) # 0x141b0
movq %rbx, %rdi
leaq 0x71bd(%rip), %rsi      # 0xf9b4
pushq $0x1
popq %rdx
pushq $-0x2
popq %rcx
xorl %r8d, %r8d
pushq $0x3
popq %r9
callq 0x8922
movq %rbx, %rdi
leaq 0x50(%rsp), %rsi
xorl %edx, %edx
callq 0x8a26
xorl %r12d, %r12d
cmpq %r12, %r13
je 0x8840
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 0x8922
incq %r12
jmp 0x881b
movq %rbx, %rdi
leaq 0x50(%rsp), %r12
movq %r12, %rsi
pushq $0x1
popq %rdx
callq 0x8a26
movq %rbx, %rdi
movq %r12, %rsi
movl %r14d, %edx
callq 0x8a26
movq %rbx, %rdi
leaq 0x7152(%rip), %rsi      # 0xf9bd
xorl %edx, %edx
movl %r15d, %ecx
movl %ebp, %r8d
movl %r14d, %r9d
callq 0x8922
movq 0x70(%rbx), %rsi
movq %rbx, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
movl %r14d, %r9d
callq 0x8922
pushq $0x8
popq %r12
xorl %r13d, %r13d
movslq 0x40(%rbx), %rax
cmpq %rax, %r13
jge 0x88c7
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 0x8922
incq %r13
addq $0x10, %r12
jmp 0x8898
movq %rbx, %rdi
leaq 0x50(%rsp), %rsi
pushq $0x3
popq %rdx
callq 0x8a26
movq 0xb8ca(%rip), %rsi      # 0x141a8
movq 0xb8cb(%rip), %r12      # 0x141b0
testq %rsi, %rsi
jne 0x8905
movq %r12, %rdi
callq 0x4150
movq %rax, 0xb8af(%rip)      # 0x141a8
movl %r12d, (%rax)
movq (%rsp), %r13
jmp 0x87e2
movq %rbx, %rdi
movq %r12, %rdx
callq 0x5133
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq