// File: /workspace/github_c_makefile_star2_O2/deplinenoise[P]vlink/linker.c
// Function: linker_resolve
void linker_resolve(struct GlobalVars *gv)
/* Resolve all symbol references and pull the required objects into */
/* the gv->selobjects list. */
{
  bool constructors_made = FALSE;
  bool pseudo_dynlink = (fff[gv->dest_format]->flags&FFF_PSEUDO_DYNLINK)!=0;
  struct ObjectUnit *obj = (struct ObjectUnit *)gv->selobjects.first;
  static const char *pulltxt = " needed due to ";

  if (gv->dest_sharedobj || gv->dyn_exp_all) {
    gv->dynamic = TRUE;
    init_dynlink(gv);
  }
  else
    gv->dynamic = FALSE;  /* set to true, when first shared object found */

  if (gv->trace_file)
    fprintf(gv->trace_file,"\nDigesting symbol information:\n\n");

  if (obj->n.next == NULL)
    return;  /* no objects in list */

  do {
    struct Section *sec;
    struct Reloc *xref;
    struct Symbol *xdef;
    struct ObjectUnit *pull_unit;

    /* all sections of this object are checked for external references */
    for (sec=(struct Section *)obj->sections.first;
         sec->n.next!=NULL; sec=(struct Section *)sec->n.next) {

      for (xref=(struct Reloc *)sec->xrefs.first;
           xref->n.next!=NULL; xref=(struct Reloc *)xref->n.next) {

        if (xref->rtype == R_LOADREL) {
          /* addend offsets to load address, nothing to resolve */
          continue;
        }

        /* find a global symbol with this name in any object or library */
        xdef = findsymbol(gv,sec,xref->xrefname);

        if (xdef!=NULL && xref->rtype==R_LOCALPC) {
          /* R_LOCALPC only accepts symbols which are defined in the
             same ObjectUnit as the reference. */
          if (xdef->relsect->obj != obj)
            xdef = NULL;  /* discard symbol from other object */
        }

        if (xdef == NULL) {
          /* check if reference can be resolved by a linker symbol */
          if (!(xdef = lnksymbol(gv,sec,xref))) {

            /* ref. to undefined symbol is only an error for executables */
            if (!gv->dest_object && !gv->dest_sharedobj) {
              if (xref->flags & RELF_WEAK) {
                /* weak references default to 0 */
                xdef = addlnksymbol(gv,xref->xrefname,0,SYM_RELOC,0,
                                    SYMI_NOTYPE,SYMB_LOCAL,0);
                xdef->relsect = dummy_section(gv,obj);
              }
              else {
                print_function_name(sec,xref->offset);
                error(21,getobjname(sec->obj),sec->name,xref->offset,
                      xref->xrefname);
                continue;
              }
            }
            else
              continue;
          }
        }
        /* reference has been resolved after this point */
        if (trace_sym_access(gv,xref->xrefname))
          fprintf(stderr,"Symbol %s referenced from %s\n",
                  xref->xrefname,getobjname(sec->obj));

        /* turn LOCALPC into a PC reloc, once resolved by a local symbol */
        if (xref->rtype == R_LOCALPC)
          xref->rtype = R_PC;

        /* link with symbol's unit */
        if (xdef->relsect && xdef->relsect->type != ST_TMP) {
          pull_unit = xdef->relsect->obj;

          switch (pull_unit->lnkfile->type) {

            case ID_ARTIFICIAL:
              /* linker-generated object - insert it behind the curr. obj. */
              if (!(pull_unit->flags & OUF_LINKED)) {
                if (gv->map_file) {
                  fprintf(gv->map_file,
                          "artificial object (%s) created due to %s\n",
                          pull_unit->objname,xref->xrefname);
                  /* "artificial object (name.o) created due to @__name" */
                }
                insertbehind(&obj->n,&pull_unit->n);
                pull_unit->flags |= OUF_LINKED;
              }
              /* turn into a normal object */
              pull_unit->lnkfile->type = ID_OBJECT;
              xref->relocsect.symbol = xdef;  /* xref was resolved */
              break;

            case ID_SHAREDOBJ:
              if (!(pull_unit->flags & OUF_LINKED)) {
                if (gv->map_file) {
                  fprintf(gv->map_file,"%s%s%s\n",
                          pull_unit->lnkfile->pathname,pulltxt,xref->xrefname);
                  /* Example: "/usr/lib/libc.so.12.0 needed due to _atexit" */
                }
                insertbehind(&obj->n,remnode(&pull_unit->n));
                add_priptrs(gv,pull_unit);
                pull_unit->flags |= OUF_LINKED;
                reenter_global_objsyms(gv,pull_unit);
                if (!pseudo_dynlink) {
                  if (!gv->dynamic) {
                    gv->dynamic = TRUE;  /* we're doing dynamic linking! */
                    init_dynlink(gv);
                  }
                  /* declare the shared object as "needed" */
                  dyn_so_needed(gv,pull_unit);
                }
              }

              if (!gv->dest_object && !pseudo_dynlink) {
                /* ref. to a shared object's symbol needs special treatment */
                switch (xref->rtype) {
                  case R_ABS:
                  case R_PC:
                    if (xdef->info == SYMI_FUNC) {
                      /* abs. or rel. function calls create a PLT entry */
                      xdef = dyn_ext_entry(gv,xdef,xref,PLT_ENTRY);
                    }
                    else {
                      /* alloc bss space and R_COPY from shared object */
                      xdef = dyn_ext_entry(gv,xdef,xref,BSS_ENTRY);
                    }
                    break;
                  case R_GOT:
                  case R_GOTPC:
                  case R_GOTOFF:
                  case R_PLT:
                  case R_PLTPC:
                  case R_PLTOFF:
                  case R_GLOBDAT:
                  case R_JMPSLOT:
                  case R_COPY:
                    /* types are already correct for referencing a shared obj. */
                    break;
                  default:
                    ierror("linker_resolve(): Unsupported reloc %s referencing "
                           "shared object symbol %s",
                           reloc_name[xref->rtype],xref->xrefname);
                    break;
                }
                xref->relocsect.symbol = xdef;  /* xref was resolved */
              }
              break;

            case ID_LIBARCH:
              if (!(pull_unit->flags & OUF_LINKED)) {
                if (gv->map_file) {
                  fprintf(gv->map_file,"%s (%s)%s%s\n",
                          pull_unit->lnkfile->pathname,pull_unit->objname,
                          pulltxt,xref->xrefname);
                  /* "/usr/lib/libc.a (atexit.o) needed due to _atexit" */
                }
                insertbehind(&obj->n,remnode(&pull_unit->n));
                add_priptrs(gv,pull_unit);
                pull_unit->flags |= OUF_LINKED;
                reenter_global_objsyms(gv,pull_unit);
              }
              /* fall through */

            default:
              xref->relocsect.symbol = xdef;  /* xref was resolved */
              break;
          }

          /* Handle explicit GOT and PLT references */
          if (!gv->dest_object) {
            switch (xref->rtype) {
              case R_GOT:
              case R_GOTPC:
              case R_GOTOFF:
                xref->relocsect.symbol = dyn_ext_entry(gv,xdef,xref,GOT_ENTRY);
                break;
              case R_PLT:
              case R_PLTPC:
              case R_PLTOFF:
                xref->relocsect.symbol = dyn_ext_entry(gv,xdef,xref,PLT_ENTRY);
                break;
            }
          }

          /* is it a reference from a shared object to one of our symbols? */
          if (obj->lnkfile->type==ID_SHAREDOBJ
              && pull_unit->lnkfile->type!=ID_SHAREDOBJ)
            dyn_export(gv,xdef);  /* then we have to export it */
        }
        else
          xref->relocsect.symbol = xdef;

        xdef->flags |= SYMF_REFERENCED;
      }
    }

    if (obj->n.next->next == NULL && !constructors_made) {
      make_constructors(gv);  /* Con-/Destructor object always at last */
      constructors_made = TRUE;
    }
    obj = (struct ObjectUnit *)obj->n.next;
  }
  while (obj->n.next);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
movzbl 0x50(%rdi), %eax
leaq 0x27ba2(%rip), %r14     # 0x2f190
movq (%r14,%rax,8), %rax
movl 0xac(%rax), %eax
movl %eax, 0x14(%rsp)
movq 0x138(%rdi), %r15
cmpl $0x0, 0x58(%rdi)
jne 0x7613
cmpl $0x0, 0x5c(%rbx)
je 0x7ab9
movl $0x1, 0x240(%rbx)
movq %rbx, %rdi
callq 0x7ac5
movq 0xa0(%rbx), %rcx
testq %rcx, %rcx
je 0x7643
leaq 0x18b7a(%rip), %rdi     # 0x201b2
pushq $0x20
popq %rsi
pushq $0x1
popq %rdx
callq 0x5280
cmpq $0x0, (%r15)
je 0x7aaa
movl $0x0, 0x10(%rsp)
movq %r15, 0x8(%rsp)
addq $0x20, %r15
movq (%r15), %r15
cmpq $0x0, (%r15)
je 0x7a6a
leaq 0x68(%r15), %rbp
movq (%rbp), %rbp
cmpq $0x0, (%rbp)
je 0x765e
cmpb $0xd, 0x38(%rbp)
je 0x766f
movq 0x10(%rbp), %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq 0xa220
testq %rax, %rax
je 0x76ac
movq %rax, %r13
cmpb $0xc, 0x38(%rbp)
jne 0x76c4
movq 0x38(%r13), %rax
movq 0x8(%rsp), %rcx
cmpq %rcx, 0x10(%rax)
je 0x76c4
movq 0x10(%rbp), %rsi
movq %rbx, %rdi
callq 0xa922
testq %rax, %rax
je 0x7779
movq %rax, %r13
movq 0x10(%rbp), %rsi
movq %rbx, %rdi
callq 0x7039
testl %eax, %eax
jne 0x79bf
cmpb $0xc, 0x38(%rbp)
jne 0x76e2
movb $0x2, 0x38(%rbp)
movq 0x38(%r13), %rax
testq %rax, %rax
je 0x76f1
cmpb $0x4, 0x2c(%rax)
jne 0x76ff
movq %r13, 0x18(%rbp)
orb $0x2, 0x41(%r13)
jmp 0x766f
movq 0x10(%rax), %r12
movq 0x10(%r12), %rax
movzbl 0x39(%rax), %ecx
cmpl $0x8, %ecx
je 0x78e5
cmpl $0x4, %ecx
je 0x77ef
cmpl $0x3, %ecx
jne 0x7952
testb $0x1, 0x80(%r12)
jne 0x7770
movq 0x90(%rbx), %rdi
testq %rdi, %rdi
je 0x7755
movq 0x18(%r12), %rdx
movq 0x10(%rbp), %rcx
leaq 0x18aa3(%rip), %rsi     # 0x201f1
xorl %eax, %eax
callq 0x5180
movq 0x8(%rsp), %rdi
movq %r12, %rsi
callq 0x63ec
orb $0x1, 0x80(%r12)
movq 0x10(%r12), %rax
movb $0x1, 0x39(%rax)
jmp 0x7952
movzbl 0x50(%rbx), %eax
movq (%r14,%rax,8), %rax
movq 0x48(%rax), %rax
testq %rax, %rax
je 0x779e
movq %rbx, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq *%rax
testq %rax, %rax
jne 0x76c1
cmpl $0x0, 0x54(%rbx)
jne 0x766f
cmpl $0x0, 0x58(%rbx)
jne 0x766f
testb $0x1, 0x39(%rbp)
jne 0x79f9
movq 0x20(%rbp), %rsi
movq %r15, %rdi
callq 0x6e81
movq 0x10(%r15), %rdi
callq 0x6e1c
movq 0x20(%r15), %rdx
movq 0x10(%rbp), %r8
movq 0x20(%rbp), %rcx
pushq $0x15
popq %rdi
movq %rax, %rsi
xorl %eax, %eax
callq 0x6c19
jmp 0x766f
testb $0x1, 0x80(%r12)
jne 0x7891
movq 0x90(%rbx), %rdi
testq %rdi, %rdi
je 0x7827
movq 0x10(%rax), %rdx
movq 0x10(%rbp), %r8
leaq 0x18a02(%rip), %rsi     # 0x2021b
leaq 0x18982(%rip), %rcx     # 0x201a2
xorl %eax, %eax
callq 0x5180
movq %r12, %rdi
callq 0x6444
movq 0x8(%rsp), %rdi
movq %rax, %rsi
callq 0x63ec
movq %rbx, %rdi
movq %r12, %rsi
callq 0xb38e
orb $0x1, 0x80(%r12)
movq %rbx, %rdi
movq %r12, %rsi
callq 0xaaa7
testb $0x4, 0x14(%rsp)
jne 0x7956
cmpl $0x0, 0x240(%rbx)
jne 0x7881
movl $0x1, 0x240(%rbx)
movq %rbx, %rdi
callq 0x7ac5
movq 0x18(%r12), %rsi
movq %rbx, %rdi
pushq $0x8
popq %rdx
callq 0xa0a8
movl 0x14(%rsp), %ecx
andl $0x4, %ecx
movl 0x54(%rbx), %eax
orl %eax, %ecx
jne 0x7959
movzbl 0x38(%rbp), %eax
cmpl $0xf, %eax
ja 0x7a48
movl $0xc3f8, %ecx           # imm = 0xC3F8
btl %eax, %ecx
jb 0x7952
pushq $0x6
popq %rcx
btl %eax, %ecx
jae 0x7a48
cmpb $0x2, 0x42(%r13)
jne 0x7a2f
movq %rbx, %rdi
movq %r13, %rsi
movq %rbp, %rdx
pushq $0x2
jmp 0x7a3a
testb $0x1, 0x80(%r12)
jne 0x7952
movq 0x90(%rbx), %rdi
testq %rdi, %rdi
je 0x791e
movq 0x10(%rax), %rdx
movq 0x18(%r12), %rcx
movq 0x10(%rbp), %r9
leaq 0x1895e(%rip), %rsi     # 0x2026e
leaq 0x1888b(%rip), %r8      # 0x201a2
xorl %eax, %eax
callq 0x5180
movq %r12, %rdi
callq 0x6444
movq 0x8(%rsp), %rdi
movq %rax, %rsi
callq 0x63ec
movq %rbx, %rdi
movq %r12, %rsi
callq 0xb38e
orb $0x1, 0x80(%r12)
movq %rbx, %rdi
movq %r12, %rsi
callq 0xaaa7
movq %r13, 0x18(%rbp)
movl 0x54(%rbx), %eax
testl %eax, %eax
jne 0x798d
movb 0x38(%rbp), %al
addb $-0x3, %al
cmpb $0x6, %al
ja 0x798d
movzbl %al, %eax
pushq $0x77
popq %rcx
btl %eax, %ecx
jae 0x798d
leaq 0x18714(%rip), %rcx     # 0x2008c
movl (%rcx,%rax,4), %ecx
movq %rbx, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x7aee
movq %rax, 0x18(%rbp)
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rax
cmpb $0x4, 0x39(%rax)
jne 0x76f5
movq 0x10(%r12), %rax
cmpb $0x4, 0x39(%rax)
je 0x76f5
movq %rbx, %rdi
movq %r13, %rsi
callq 0x7b36
jmp 0x76f5
movq 0x2762a(%rip), %rax     # 0x2eff0
movq (%rax), %r12
movq 0x10(%rbp), %r14
movq 0x10(%r15), %rdi
callq 0x6e1c
movq %r12, %rdi
leaq 0x187f3(%rip), %rsi     # 0x201d3
movq %r14, %rdx
leaq 0x277a6(%rip), %r14     # 0x2f190
movq %rax, %rcx
xorl %eax, %eax
callq 0x5180
jmp 0x76d8
movq 0x10(%rbp), %rsi
movq %rbx, %rdi
xorl %edx, %edx
pushq $0x2
popq %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
pushq $0x0
pushq $0x1
callq 0xa880
popq %rcx
popq %rdx
movq %rax, %r13
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
callq 0xbc0c
movq %rax, 0x38(%r13)
jmp 0x76c4
movq %rbx, %rdi
movq %r13, %rsi
movq %rbp, %rdx
pushq $0x3
popq %rcx
callq 0x7aee
movq %rax, %r13
jmp 0x7952
leaq 0x278b1(%rip), %rcx     # 0x2f300
movq (%rcx,%rax,8), %rsi
movq 0x10(%rbp), %rdx
leaq 0x187c5(%rip), %rdi     # 0x20223
xorl %eax, %eax
callq 0x6b2c
jmp 0x7952
movq 0x8(%rsp), %r15
movq (%r15), %rax
movq (%rax), %rcx
testq %rcx, %rcx
setne %dl
cmpl $0x0, 0x10(%rsp)
setne %sil
orb %dl, %sil
jne 0x7a9e
movq %rbx, %rdi
callq 0xbd83
movq (%r15), %rax
movq (%rax), %rcx
pushq $0x1
popq %rdx
movl %edx, 0x10(%rsp)
movq %rax, %r15
testq %rcx, %rcx
jne 0x7655
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
andl $0x0, 0x240(%rbx)
jmp 0x7625