// File: /workspace/github_c_makefile_star2_O2/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, 0x18c11(%rip)     # 0x2f820
sete %bl
andl $0x0, 0x1d10f(%rip)     # 0x33d28
leaq 0x1d110(%rip), %rdi     # 0x33d30
callq 0x63c8
cmpb $0x0, 0x6c(%r14)
je 0x16c3b
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0xc(%rsp), %eax
movb %bl, %al
movl %eax, 0xc(%rsp)
movq %r14, 0x10(%rsp)
leaq 0x138(%r14), %rax
xorl %ebp, %ebp
xorl %r12d, %r12d
xorl %r14d, %r14d
movq (%rax), %rax
cmpq $0x0, (%rax)
je 0x16d6e
movq %rax, 0x18(%rsp)
leaq 0x50(%rax), %r15
movq %r15, %rdi
callq 0x642a
testq %rax, %rax
je 0x16d64
movq %rax, %r13
xorl %ebx, %ebx
cmpb $0x64, 0x20(%rax)
jne 0x16ca9
movq 0x18(%r13), %rax
testq %rax, %rax
je 0x16ca9
cmpb $0x0, (%rax)
sete %al
testl %ebp, %ebp
setne %cl
orb %al, %cl
movl %ebp, %ebx
je 0x16d2d
testq %r14, %r14
jne 0x16cc6
leaq 0xab2f(%rip), %rdi      # 0x217e4
leaq 0xab18(%rip), %rsi      # 0x217d4
xorl %eax, %eax
callq 0x6b2c
xorl %r14d, %r14d
movl %ebx, %ebp
movq 0x18(%r14), %rax
cmpq $0x7fff, %rax           # imm = 0x7FFF
jb 0x16ced
leaq 0xab1a(%rip), %rdi      # 0x217f5
leaq 0xaaf2(%rip), %rsi      # 0x217d4
xorl %eax, %eax
callq 0x6b2c
movq 0x18(%r14), %rax
incq %rax
movq %rax, 0x18(%r14)
movq 0x18(%r13), %rsi
testq %rsi, %rsi
je 0x16d0a
leaq 0x38(%r14), %rdi
callq 0x155c2
movl %eax, %eax
jmp 0x16d0c
xorl %eax, %eax
movq %rax, 0x18(%r13)
testl %ebp, %ebp
je 0x16d18
movq %rax, 0x10(%r14)
addq $0xc, %r12
leaq 0x20(%r14), %rdi
movq %r13, %rsi
callq 0x6414
jmp 0x16c6f
addq $0xc, %r12
pushq $0x68
popq %rdi
callq 0x638a
movq %rax, %r14
leaq 0x20(%rax), %rdi
callq 0x63c8
movq $0x1000, 0x58(%r14)     # imm = 0x1000
leaq 0x1cfdc(%rip), %rdi     # 0x33d30
movq %r14, %rsi
callq 0x6414
pushq $0x1
popq %rbp
jmp 0x16cc8
movq 0x18(%rsp), %rax
jmp 0x16c59
testq %r12, %r12
leaq 0x1cfb8(%rip), %rbx     # 0x33d30
movq 0x10(%rsp), %r14
je 0x16c2c
movq %r12, %rdi
callq 0x6340
movq %rax, %r15
leaq 0xaaf1(%rip), %rsi      # 0x21886
movq %r14, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xb7e9
testq %rax, %rax
je 0x16dcd
movq %rax, %r13
cmpq $0x0, 0x48(%rax)
je 0x16deb
leaq 0xaa67(%rip), %rdi      # 0x21824
leaq 0xaa10(%rip), %rsi      # 0x217d4
xorl %eax, %eax
callq 0x6b2c
jmp 0x16deb
leaq 0xaab2(%rip), %rsi      # 0x21886
pushq $0x1
popq %r8
pushq $0x2
popq %rdx
movq %r14, %rdi
xorl %ecx, %ecx
movl %edx, %r9d
callq 0xbcdb
movq %rax, %r13
movq %r12, 0x50(%r13)
movq %r12, 0x48(%r13)
movq %r15, 0x78(%r13)
leaq 0xaa3f(%rip), %rdi      # 0x2183d
callq 0x169fe
movl %eax, 0x1cf1f(%rip)     # 0x33d28
leaq 0x80(%r13), %rax
movq %rax, 0x18(%rsp)
movq (%rbx), %rbx
cmpq $0x0, (%rbx)
je 0x16c2c
movq %r13, %r12
movq 0x10(%rbx), %rdx
movswl 0x18(%rbx), %r9d
movl 0x60(%rbx), %eax
movl %eax, (%rsp)
xorl %ebp, %ebp
movl 0xc(%rsp), %edi
movq %r15, %rsi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x16f65
movq %rbx, 0x20(%rsp)
leaq 0x20(%rbx), %r13
leaq 0x14(%r15), %rbx
addq $0xc, %r15
movl $0x0, 0x10(%rsp)
movq (%r13), %r13
cmpq $0x0, (%r13)
je 0x16f58
movzbl 0x20(%r13), %eax
cmpl $0x24, %eax
je 0x16ea2
cmpl $0x44, %eax
je 0x16e85
cmpl $0x64, %eax
je 0x16ec3
jmp 0x16ec5
testl %ebp, %ebp
je 0x16ec3
cmpq $0x0, 0x10(%r13)
je 0x16e9d
movl 0x10(%rsp), %ecx
subl %ecx, 0x24(%r13)
andq $0x0, 0x10(%r13)
pushq $0x1
popq %rbp
jmp 0x16ec5
cmpq $0x0, 0x18(%r13)
movl 0x24(%r13), %ecx
je 0x16eb6
pushq $0x1
popq %rbp
movl %ecx, 0x10(%rsp)
jmp 0x16ec5
subl 0x10(%rsp), %ecx
movl %ecx, 0x24(%r13)
andq $0x0, 0x10(%r13)
xorl %ebp, %ebp
cmpq $0x0, 0x10(%r13)
je 0x16f27
pushq $0x40
popq %rdi
callq 0x638a
movq %rax, %r14
movq 0x10(%r13), %rax
movl 0x38(%rax), %ecx
addl %ecx, 0x24(%r13)
movq 0x18(%rax), %rax
movq %rax, 0x18(%r14)
movq %rbx, %rax
subq 0x78(%r12), %rax
movq %rax, 0x20(%r14)
movl 0x24(%r13), %eax
movq %rax, 0x28(%r14)
movb $0x1, 0x38(%r14)
xorl %edi, %edi
movq %r14, %rsi
xorl %edx, %edx
pushq $0x20
popq %rcx
pushq $-0x1
popq %r8
callq 0xac9f
movq 0x18(%rsp), %rdi
movq %r14, %rsi
callq 0x6414
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 %r15, %rsi
callq 0x16f65
addq $0xc, %rbx
addq $0xc, %r15
jmp 0x16e60
movq 0x20(%rsp), %rbx
movq %r12, %r13
jmp 0x16e15