// File: /workspace/github_c_makefile_star2_O3/deplinenoise[P]vlink/t_elf32.c
// Function: elf32_makestabs
void elf32_makestabs(struct GlobalVars *gv)
/* create .stab, .stabstr und .rela.stab sections from StabDebug records */
{
  static const char *fn = "elf32_makestabs";
  bool be = elf32_endianess == _BIG_ENDIAN_;

  stabdebugidx = 0;
  initlist(&elf32stabcompunits);

  if (gv->strip_symbols < STRIP_DEBUG) {
    struct StabCompUnit *cu = NULL;
    bool so_found = FALSE;
    struct LinkedSection *ls;
    struct ObjectUnit *obj;
    struct StabDebug *stab;
    unsigned long stabsize = 0;

    /* determine size of .stab section to generate */
    for (obj=(struct ObjectUnit *)gv->selobjects.first,stabsize=0;
         obj->n.next!=NULL; obj=(struct ObjectUnit *)obj->n.next) {

      while (stab=(struct StabDebug *)remhead(&obj->stabs)) {

        if (stab->n_type==N_SO && stab->name.ptr!=NULL) {
          if (*stab->name.ptr != '\0') {
            if (!so_found) {
              /* allocate and initialize new compilation unit */
              stabsize += sizeof(struct nlist32);  /* comp.unit header */
              cu = newCompUnit();
              addtail(&elf32stabcompunits,&cu->n);
              so_found = TRUE;
            }
          }
        }
        else
          so_found = FALSE;

        if (cu == NULL)
          ierror("%s: N_SO missing",fn);
        if (cu->entries >= 0x7fff)
          ierror("%s: too many stab-entries for compilation unit",fn);
        stabsize += sizeof(struct nlist32);  /* stab entry */
        cu->entries++;

        /* register symbol name in comp.unit's string table */
        if (stab->name.ptr)
          stab->name.idx = elf32_addstrlist(&cu->strtab,stab->name.ptr);
        else
          stab->name.idx = 0;

        if (so_found)
          cu->nameidx = stab->name.idx;  /* set comp.unit name */

        /* append stab entry to current comp.unit */
        addtail(&cu->stabs,&stab->n);
      }
    }

    if (stabsize) {
      /* create .stab section with contents for real */
      struct nlist32 *nlst = alloc(stabsize);

      if (ls = find_lnksec(gv,".stab",0,0,0,0)) {
        if (ls->size > 0)
          ierror("%s: .stab already in use",fn);
      }
      else
        ls = create_lnksect(gv,".stab",ST_DATA,0,SP_READ,2);
      ls->size = ls->filesize = stabsize;
      ls->data = (uint8_t *)nlst;

      /* allocate SHDR name for .stabstr */
      stabdebugidx = elf32_addshdrstr(".stabstr");

      /* transfer nodes from compilation units to section's data area */
      for (cu=(struct StabCompUnit *)elf32stabcompunits.first;
           cu->n.next!=NULL; cu=(struct StabCompUnit *)cu->n.next) {
        bool within_fun = FALSE;
        uint32_t fun_addr = 0;

        /* write compilation unit stab header containing number
           of stabs and length of string-table */
        write_nlist(be,nlst++,cu->nameidx,0,0,cu->entries,
                    cu->strtab.nextindex);

        /* write comp.unit's stabs */
        for (stab=(struct StabDebug *)cu->stabs.first;
             stab->n.next!=NULL; stab=(struct StabDebug *)stab->n.next) {

          switch (stab->n_type) {
            case N_SO:
              within_fun = FALSE;
              break;
            case N_FUN:
              if (stab->name.idx) {
                within_fun = TRUE;
                fun_addr = stab->n_value;
              }
              else {  /* function end address needs to be relative (size) */
                within_fun = FALSE;
                stab->n_value -= fun_addr;
                stab->relsect = NULL;
              }
              break;
            case N_SLINE:
              if (within_fun && stab->relsect) {
                /* convert address into a function-offset */
                stab->n_value -= fun_addr;
                stab->relsect = NULL;
              }
              break;
          }

          /* add relocation, if required */
          if (stab->relsect) {
            struct Reloc *r = alloczero(sizeof(struct Reloc));

            stab->n_value += stab->relsect->offset;
            r->relocsect.lnk = stab->relsect->lnksec;
            r->offset = (uint8_t *)&nlst->n_value - ls->data;
            r->addend = (lword)stab->n_value;
            r->rtype = R_ABS;
            addreloc(NULL,r,0,32,-1);
            addtail(&ls->relocs,&r->n);
          }

          /* write stab entry to section */
          write_nlist(be,nlst++,stab->name.idx,stab->n_type,
                      stab->n_othr,stab->n_desc,stab->n_value);
        }
      }
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %r14
movl $0x0, 0xc(%rsp)
cmpb $0x1, 0x1788f(%rip)     # 0x2f810
sete %bl
movl $0x0, 0x1b9aa(%rip)     # 0x33938
leaq 0x1b9ab(%rip), %rdi     # 0x33940
callq 0x66da
cmpb $0x0, 0x6c(%r14)
je 0x17fb0
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x138(%r14), %rcx
cmpq $0x0, (%rcx)
je 0x17fa1
movq %r14, 0x10(%rsp)
movl 0xc(%rsp), %eax
movb %bl, %al
movl %eax, 0xc(%rsp)
xorl %r14d, %r14d
xorl %r15d, %r15d
xorl %ebp, %ebp
movq %rcx, 0x18(%rsp)
leaq 0x50(%rcx), %r13
movq %r13, %rdi
callq 0x673f
testq %rax, %rax
je 0x180ea
movq %rax, %rbx
xorl %r12d, %r12d
cmpb $0x64, 0x20(%rbx)
jne 0x18019
movq 0x18(%rbx), %rax
testq %rax, %rax
je 0x18019
cmpb $0x0, (%rax)
sete %al
testl %ebp, %ebp
setne %cl
orb %al, %cl
movl %ebp, %r12d
je 0x180af
testq %r14, %r14
jne 0x18036
leaq 0xb79f(%rip), %rdi      # 0x237c4
leaq 0xb788(%rip), %rsi      # 0x237b4
xorl %eax, %eax
callq 0x52b0
xorl %r14d, %r14d
movl %r12d, %ebp
movq 0x18(%r14), %rax
cmpq $0x7fff, %rax           # imm = 0x7FFF
jb 0x1805e
leaq 0xb789(%rip), %rdi      # 0x237d5
leaq 0xb761(%rip), %rsi      # 0x237b4
xorl %eax, %eax
callq 0x52b0
movq 0x18(%r14), %rax
incq %rax
movq %rax, 0x18(%r14)
movq 0x18(%rbx), %rsi
testq %rsi, %rsi
je 0x1807b
leaq 0x38(%r14), %rdi
callq 0x167f2
movl %eax, %eax
jmp 0x1807d
xorl %eax, %eax
movq %rax, 0x18(%rbx)
testl %ebp, %ebp
je 0x18089
movq %rax, 0x10(%r14)
addq $0xc, %r15
leaq 0x20(%r14), %rdi
movq %rbx, %rsi
callq 0x6729
movq %r13, %rdi
callq 0x673f
movq %rax, %rbx
testq %rax, %rax
jne 0x17ff1
jmp 0x180ea
addq $0xc, %r15
movl $0x68, %edi
callq 0x6686
movq %rax, %r14
leaq 0x20(%rax), %rdi
callq 0x66da
movq $0x1000, 0x58(%r14)     # imm = 0x1000
leaq 0x1b868(%rip), %rdi     # 0x33940
movq %r14, %rsi
callq 0x6729
movl $0x1, %ebp
jmp 0x18039
movq 0x18(%rsp), %rcx
movq (%rcx), %rcx
cmpq $0x0, (%rcx)
jne 0x17fd4
testq %r15, %r15
movq 0x10(%rsp), %rbx
je 0x17fa1
movq %r15, %rdi
callq 0x6638
movq %rax, %r14
leaq 0xb74a(%rip), %rsi      # 0x23866
movq %rbx, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xbe13
testq %rax, %rax
je 0x18154
movq %rax, %r12
cmpq $0x0, 0x48(%rax)
je 0x18179
leaq 0xb6c0(%rip), %rdi      # 0x23804
leaq 0xb669(%rip), %rsi      # 0x237b4
xorl %eax, %eax
callq 0x52b0
jmp 0x18179
leaq 0xb70b(%rip), %rsi      # 0x23866
movq %rbx, %rdi
movl $0x2, %edx
xorl %ecx, %ecx
movl $0x1, %r8d
movl $0x2, %r9d
callq 0xc37c
movq %rax, %r12
movq %r15, 0x50(%r12)
movq %r15, 0x48(%r12)
movq %r12, 0x18(%rsp)
movq %r14, 0x78(%r12)
leaq 0x1764c(%rip), %rdi     # 0x2f7e0
leaq 0xb682(%rip), %rsi      # 0x2381d
callq 0x167f2
movl %eax, 0x1b792(%rip)     # 0x33938
movq 0x1b793(%rip), %rbx     # 0x33940
cmpq $0x0, (%rbx)
je 0x17fa1
movq 0x18(%rsp), %rax
leaq 0x80(%rax), %r15
movq 0x10(%rbx), %rdx
movswl 0x18(%rbx), %r9d
movl 0x60(%rbx), %eax
movl %eax, (%rsp)
movl 0xc(%rsp), %edi
movq %r14, %rsi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x18328
leaq 0xc(%r14), %r12
movq %rbx, 0x20(%rsp)
movq 0x20(%rbx), %r13
cmpq $0x0, (%r13)
je 0x1830e
addq $0x14, %r14
movl $0x0, 0x10(%rsp)
xorl %ebp, %ebp
movzbl 0x20(%r13), %eax
cmpl $0x24, %eax
je 0x18241
cmpl $0x44, %eax
je 0x1821f
cmpl $0x64, %eax
je 0x18267
jmp 0x18269
testl %ebp, %ebp
je 0x18267
movl $0x1, %ebp
cmpq $0x0, 0x10(%r13)
je 0x18269
movl 0x10(%rsp), %ecx
subl %ecx, 0x24(%r13)
movq $0x0, 0x10(%r13)
jmp 0x18269
cmpq $0x0, 0x18(%r13)
movl 0x24(%r13), %ecx
movl $0x1, %ebp
je 0x18257
movl %ecx, 0x10(%rsp)
jmp 0x18269
subl 0x10(%rsp), %ecx
movl %ecx, 0x24(%r13)
movq $0x0, 0x10(%r13)
xorl %ebp, %ebp
cmpq $0x0, 0x10(%r13)
je 0x182d3
movl $0x40, %edi
callq 0x6686
movq %rax, %rbx
movq 0x10(%r13), %rax
movl 0x38(%rax), %ecx
addl %ecx, 0x24(%r13)
movq 0x18(%rax), %rax
movq %rax, 0x18(%rbx)
movq %r14, %rax
movq 0x18(%rsp), %rcx
subq 0x78(%rcx), %rax
movq %rax, 0x20(%rbx)
movl 0x24(%r13), %eax
movq %rax, 0x28(%rbx)
movb $0x1, 0x38(%rbx)
xorl %edi, %edi
movq %rbx, %rsi
xorl %edx, %edx
movl $0x20, %ecx
movq $-0x1, %r8
callq 0xb1f1
movq %r15, %rdi
movq %rbx, %rsi
callq 0x6729
movb 0x20(%r13), %al
movq 0x18(%r13), %rdx
movsbl 0x21(%r13), %r8d
movswl 0x22(%r13), %r9d
movl 0x24(%r13), %ecx
movl %ecx, (%rsp)
movzbl %al, %ecx
movl 0xc(%rsp), %edi
movq %r12, %rsi
callq 0x18328
addq $0xc, %r12
movq (%r13), %r13
addq $0xc, %r14
cmpq $0x0, (%r13)
jne 0x18209
movq 0x20(%rsp), %rbx
movq (%rbx), %rbx
cmpq $0x0, (%rbx)
movq %r12, %r14
jne 0x181c3
jmp 0x17fa1