// File: /workspace/github_c_makefile_star2_O1/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, 0x14(%rsp)
cmpb $0x1, 0x1770e(%rip)     # 0x2f810
sete %bl
movl $0x0, 0x1b829(%rip)     # 0x33938
leaq 0x1b82a(%rip), %rdi     # 0x33940
callq 0x670a
cmpb $0x0, 0x6c(%r14)
je 0x18131
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb %bl, 0x10(%rsp)
movq %r14, 0x20(%rsp)
movq 0x138(%r14), %rax
cmpq $0x0, (%rax)
je 0x18265
leaq 0xb682(%rip), %rbp      # 0x237d4
xorl %r14d, %r14d
xorl %r15d, %r15d
xorl %ebx, %ebx
movq %rax, 0x18(%rsp)
leaq 0x50(%rax), %r13
movq %r13, %rdi
callq 0x676f
testq %rax, %rax
je 0x18251
movq %rax, %r12
cmpb $0x64, 0x20(%r12)
jne 0x181d0
movq 0x18(%r12), %rax
testq %rax, %rax
je 0x181d0
cmpb $0x0, (%rax)
sete %al
testl %ebx, %ebx
setne %cl
orb %al, %cl
jne 0x181d2
addq $0xc, %r15
movl $0x68, %edi
callq 0x66b6
movq %rax, %r14
leaq 0x20(%rax), %rdi
callq 0x670a
movq $0x1000, 0x58(%r14)     # imm = 0x1000
leaq 0x1b77f(%rip), %rdi     # 0x33940
movq %r14, %rsi
callq 0x6759
movl $0x1, %ebx
jmp 0x181d2
xorl %ebx, %ebx
testq %r14, %r14
jne 0x181e8
leaq 0xb606(%rip), %rdi      # 0x237e4
movq %rbp, %rsi
xorl %eax, %eax
callq 0x52b0
cmpq $0x7fff, 0x18(%r14)     # imm = 0x7FFF
jb 0x18203
leaq 0xb5fc(%rip), %rdi      # 0x237f5
movq %rbp, %rsi
xorl %eax, %eax
callq 0x52b0
incq 0x18(%r14)
movq 0x18(%r12), %rsi
testq %rsi, %rsi
je 0x1821e
leaq 0x38(%r14), %rdi
callq 0x169a2
movl %eax, %eax
jmp 0x18220
xorl %eax, %eax
movq %rax, 0x18(%r12)
testl %ebx, %ebx
je 0x1822d
movq %rax, 0x10(%r14)
addq $0xc, %r15
leaq 0x20(%r14), %rdi
movq %r12, %rsi
callq 0x6759
movq %r13, %rdi
callq 0x676f
movq %rax, %r12
testq %rax, %rax
jne 0x18177
movq 0x18(%rsp), %rax
movq (%rax), %rax
cmpq $0x0, (%rax)
jne 0x1815a
jmp 0x18268
xorl %r15d, %r15d
testq %r15, %r15
movb 0x10(%rsp), %bl
movq 0x20(%rsp), %r12
je 0x18122
movq %r15, %rdi
callq 0x6668
movq %rax, %r14
leaq 0xb5fa(%rip), %rsi      # 0x23886
movq %r12, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xbeb0
testq %rax, %rax
je 0x182c4
movq %rax, %r13
cmpq $0x0, 0x48(%rax)
je 0x182e9
leaq 0xb570(%rip), %rdi      # 0x23824
leaq 0xb519(%rip), %rsi      # 0x237d4
xorl %eax, %eax
callq 0x52b0
jmp 0x182e9
leaq 0xb5bb(%rip), %rsi      # 0x23886
movq %r12, %rdi
movl $0x2, %edx
xorl %ecx, %ecx
movl $0x1, %r8d
movl $0x2, %r9d
callq 0xc429
movq %rax, %r13
movq %r15, 0x50(%r13)
movq %r15, 0x48(%r13)
movq %r13, 0x18(%rsp)
movq %r14, 0x78(%r13)
leaq 0x174df(%rip), %rdi     # 0x2f7e0
leaq 0xb535(%rip), %rsi      # 0x2383d
callq 0x169a2
movl %eax, 0x1b625(%rip)     # 0x33938
movq 0x1b626(%rip), %r13     # 0x33940
cmpq $0x0, (%r13)
je 0x18122
movl 0x14(%rsp), %eax
movb %bl, %al
movl %eax, 0x14(%rsp)
movq 0x18(%rsp), %rax
leaq 0x80(%rax), %r15
movq 0x10(%r13), %rdx
movswl 0x18(%r13), %r9d
movl 0x60(%r13), %eax
movl %eax, (%rsp)
movl 0x14(%rsp), %edi
movq %r14, %rsi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x184a1
leaq 0xc(%r14), %r12
movq %r13, 0x20(%rsp)
movq 0x20(%r13), %r13
cmpq $0x0, (%r13)
je 0x18485
addq $0x14, %r14
movl $0x0, 0x10(%rsp)
xorl %ebx, %ebx
movzbl 0x20(%r13), %eax
cmpl $0x24, %eax
je 0x183ba
cmpl $0x44, %eax
je 0x18398
cmpl $0x64, %eax
je 0x183e0
jmp 0x183e2
testl %ebx, %ebx
je 0x183e0
movl $0x1, %ebx
cmpq $0x0, 0x10(%r13)
je 0x183e2
movl 0x10(%rsp), %eax
subl %eax, 0x24(%r13)
movq $0x0, 0x10(%r13)
jmp 0x183e2
cmpq $0x0, 0x18(%r13)
movl 0x24(%r13), %eax
movl $0x1, %ebx
je 0x183d0
movl %eax, 0x10(%rsp)
jmp 0x183e2
subl 0x10(%rsp), %eax
movl %eax, 0x24(%r13)
movq $0x0, 0x10(%r13)
xorl %ebx, %ebx
cmpq $0x0, 0x10(%r13)
je 0x18448
movl $0x40, %edi
callq 0x66b6
movq %rax, %rbp
movq 0x10(%r13), %rax
movl 0x38(%rax), %ecx
addl %ecx, 0x24(%r13)
movq 0x18(%rax), %rax
movq %rax, 0x18(%rbp)
movq %r14, %rax
movq 0x18(%rsp), %rcx
subq 0x78(%rcx), %rax
movq %rax, 0x20(%rbp)
movl 0x24(%r13), %eax
movq %rax, 0x28(%rbp)
movb $0x1, 0x38(%rbp)
xorl %edi, %edi
movq %rbp, %rsi
xorl %edx, %edx
movl $0x20, %ecx
movq $-0x1, %r8
callq 0xb2b7
movq %r15, %rdi
movq %rbp, %rsi
callq 0x6759
movq 0x18(%r13), %rdx
movzbl 0x20(%r13), %ecx
movsbl 0x21(%r13), %r8d
movswl 0x22(%r13), %r9d
movl 0x24(%r13), %eax
movl %eax, (%rsp)
movl 0x14(%rsp), %edi
movq %r12, %rsi
callq 0x184a1
addq $0xc, %r12
movq (%r13), %r13
addq $0xc, %r14
cmpq $0x0, (%r13)
jne 0x18382
movq 0x20(%rsp), %r13
movq (%r13), %r13
cmpq $0x0, (%r13)
movq %r12, %r14
jne 0x1833b
jmp 0x18122