// File: /workspace/github_c_makefile_star2_O3/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 0x278e3(%rip), %r14     # 0x2f180
movq (%r14,%rax,8), %rax
movl 0xac(%rax), %eax
movl %eax, 0xc(%rsp)
movq 0x138(%rdi), %rbp
cmpl $0x0, 0x58(%rdi)
jne 0x78be
cmpl $0x0, 0x5c(%rbx)
je 0x78e8
movl $0x1, 0x240(%rbx)
movq %rbx, %rdi
callq 0xa5c4
movzbl 0x50(%rbx), %eax
movq (%r14,%rax,8), %rax
movq 0x58(%rax), %rax
testq %rax, %rax
je 0x78f2
movq %rbx, %rdi
callq *%rax
jmp 0x78f2
movl $0x0, 0x240(%rbx)
movq 0xa0(%rbx), %rcx
testq %rcx, %rcx
je 0x7914
leaq 0x1a89d(%rip), %rdi     # 0x221a2
movl $0x20, %esi
movl $0x1, %edx
callq 0x5270
movq (%rbp), %rax
testq %rax, %rax
je 0x7dd6
movl $0x0, 0x8(%rsp)
movq 0x20(%rbp), %r15
movq (%r15), %rcx
testq %rcx, %rcx
movq %rbp, (%rsp)
je 0x7d99
movq 0x68(%r15), %r13
cmpq $0x0, (%r13)
movq %rcx, %rax
je 0x7d86
leaq 0x10(%r15), %rax
movq %rax, 0x10(%rsp)
cmpb $0xd, 0x38(%r13)
je 0x7cd4
leaq 0x10(%r13), %r14
movq 0x10(%r13), %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq 0xa728
testq %rax, %rax
je 0x7990
movq %rax, %r12
cmpb $0xc, 0x38(%r13)
jne 0x79a7
movq 0x38(%r12), %rax
cmpq %rbp, 0x10(%rax)
je 0x79a7
movq (%r14), %rsi
movq %rbx, %rdi
callq 0xae55
testq %rax, %rax
je 0x7a50
movq %rax, %r12
movq (%r14), %rsi
movq %rbx, %rdi
callq 0x72df
testl %eax, %eax
jne 0x7c89
cmpb $0xc, 0x38(%r13)
jne 0x79c6
movb $0x2, 0x38(%r13)
movq 0x38(%r12), %rax
testq %rax, %rax
je 0x79d6
cmpb $0x4, 0x2c(%rax)
jne 0x79df
movq %r12, 0x18(%r13)
jmp 0x7cce
movq 0x10(%rax), %rbp
movq 0x10(%rbp), %rax
movzbl 0x39(%rax), %ecx
cmpl $0x8, %ecx
je 0x7bea
cmpl $0x4, %ecx
je 0x7ad0
cmpl $0x3, %ecx
jne 0x7c50
testb $0x1, 0x80(%rbp)
jne 0x7a47
movq 0x90(%rbx), %rdi
testq %rdi, %rdi
je 0x7a30
movq 0x18(%rbp), %rdx
movq (%r14), %rcx
leaq 0x1a7b8(%rip), %rsi     # 0x221e1
xorl %eax, %eax
callq 0x5180
movq (%rsp), %rdi
movq %rbp, %rsi
callq 0x6701
orb $0x1, 0x80(%rbp)
movq 0x10(%rbp), %rax
movb $0x1, 0x39(%rax)
jmp 0x7c50
movzbl 0x50(%rbx), %eax
leaq 0x27725(%rip), %rcx     # 0x2f180
movq (%rcx,%rax,8), %rax
movq 0x48(%rax), %rax
testq %rax, %rax
je 0x7a7c
movq %rbx, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq *%rax
testq %rax, %rax
jne 0x79a4
cmpl $0x0, 0x54(%rbx)
jne 0x7cd4
cmpl $0x0, 0x58(%rbx)
jne 0x7cd4
testb $0x1, 0x39(%r13)
jne 0x7d0e
movq 0x20(%r13), %rsi
movq %r15, %rdi
callq 0x7169
movq 0x10(%r15), %rdi
callq 0x7104
movq 0x20(%r15), %rdx
movq 0x10(%r13), %r8
movq 0x20(%r13), %rcx
movl $0x15, %edi
movq %rax, %rsi
xorl %eax, %eax
callq 0x6f14
jmp 0x7cd4
testb $0x1, 0x80(%rbp)
jne 0x7b8a
movq 0x90(%rbx), %rdi
testq %rdi, %rdi
je 0x7b05
movq 0x10(%rax), %rdx
movq (%r14), %r8
leaq 0x1a714(%rip), %rsi     # 0x2220b
leaq 0x1a694(%rip), %rcx     # 0x22192
xorl %eax, %eax
callq 0x5180
movq %rbp, %rdi
callq 0x6759
movq (%rsp), %rdi
movq %rax, %rsi
callq 0x6701
movq %rbx, %rdi
movq %rbp, %rsi
callq 0xb915
orb $0x1, 0x80(%rbp)
movq %rbx, %rdi
movq %rbp, %rsi
callq 0xafd9
testb $0x4, 0xc(%rsp)
jne 0x7c54
cmpl $0x0, 0x240(%rbx)
jne 0x7b79
movl $0x1, 0x240(%rbx)
movq %rbx, %rdi
callq 0xa5c4
movzbl 0x50(%rbx), %eax
leaq 0x27619(%rip), %rcx     # 0x2f180
movq (%rcx,%rax,8), %rax
movq 0x58(%rax), %rax
testq %rax, %rax
je 0x7b79
movq %rbx, %rdi
callq *%rax
movq 0x18(%rbp), %rsi
movq %rbx, %rdi
movl $0x8, %edx
callq 0xa604
movl 0xc(%rsp), %ecx
andl $0x4, %ecx
movl 0x54(%rbx), %eax
orl %eax, %ecx
jne 0x7c57
movzbl 0x38(%r13), %eax
cmpl $0xf, %eax
ja 0x7d62
movl $0xc3f8, %ecx           # imm = 0xC3F8
btl %eax, %ecx
jb 0x7c50
movl $0x6, %ecx
btl %eax, %ecx
jae 0x7d62
cmpb $0x2, 0x42(%r12)
jne 0x7d46
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rdx
movl $0x2, %ecx
callq 0x7de5
movq %rax, %r12
jmp 0x7c50
testb $0x1, 0x80(%rbp)
jne 0x7c50
movq 0x90(%rbx), %rdi
testq %rdi, %rdi
je 0x7c1f
movq 0x10(%rax), %rdx
movq 0x18(%rbp), %rcx
movq (%r14), %r9
leaq 0x1a64d(%rip), %rsi     # 0x2225e
leaq 0x1a57a(%rip), %r8      # 0x22192
xorl %eax, %eax
callq 0x5180
movq %rbp, %rdi
callq 0x6759
movq (%rsp), %rdi
movq %rax, %rsi
callq 0x6701
movq %rbx, %rdi
movq %rbp, %rsi
callq 0xb915
orb $0x1, 0x80(%rbp)
movq %rbx, %rdi
movq %rbp, %rsi
callq 0xafd9
movq %r12, 0x18(%r13)
movl 0x54(%rbx), %eax
testl %eax, %eax
jne 0x7cb2
movzbl 0x38(%r13), %eax
leal -0x7(%rax), %ecx
cmpl $0x3, %ecx
jb 0x7c9b
addl $-0x3, %eax
cmpl $0x2, %eax
ja 0x7cb2
movq %rbx, %rdi
movq %r12, %rsi
movl $0x1, %edx
callq 0xa604
testq %rax, %rax
cmoveq %r12, %rax
jmp 0x7cae
movq %r14, %rdi
movq 0x10(%rsp), %rsi
callq 0x541c
jmp 0x79ba
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rdx
movl $0x2, %ecx
callq 0x7de5
movq %rax, 0x18(%r13)
movq (%rsp), %rax
movq 0x10(%rax), %rax
cmpb $0x4, 0x39(%rax)
jne 0x7cca
movq 0x10(%rbp), %rax
cmpb $0x4, 0x39(%rax)
jne 0x7ce8
movq (%rsp), %rbp
orb $0x2, 0x41(%r12)
movq (%r13), %r13
cmpq $0x0, (%r13)
jne 0x7958
jmp 0x7d83
movb 0x41(%r12), %al
cmpb $0x3f, %al
movq (%rsp), %rbp
ja 0x7cce
orb $-0x80, %al
movb %al, 0x41(%r12)
movq %rbx, %rdi
movq %r12, %rsi
movl $0x7, %edx
callq 0xa604
jmp 0x7cce
movq (%r14), %rsi
movq %rbx, %rdi
xorl %edx, %edx
movl $0x2, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
pushq $0x0
pushq $0x1
callq 0xadaf
addq $0x10, %rsp
movq %rax, %r12
movq %rbx, %rdi
movq %rbp, %rsi
callq 0xc2b8
movq %rax, 0x38(%r12)
jmp 0x79a7
movq %rbx, %rdi
movq %r12, %rsi
movl $0x3, %edx
callq 0xa604
testq %rax, %rax
cmovneq %rax, %r12
jmp 0x7c50
leaq 0x27587(%rip), %rcx     # 0x2f2f0
movq (%rcx,%rax,8), %rsi
movq (%r14), %rdx
leaq 0x1a49c(%rip), %rdi     # 0x22213
xorl %eax, %eax
callq 0x52b0
jmp 0x7c50
movq (%r15), %rax
movq (%rax), %rcx
movq %rax, %r15
testq %rcx, %rcx
jne 0x793d
movq (%rbp), %rax
movq (%rax), %rcx
testq %rcx, %rcx
setne %dl
cmpl $0x0, 0x8(%rsp)
setne %sil
orb %dl, %sil
jne 0x7dc7
movq %rbx, %rdi
callq 0xc424
movq (%rbp), %rax
movq (%rax), %rcx
movl $0x1, 0x8(%rsp)
movq %rax, %rbp
movq %rcx, %rax
testq %rcx, %rcx
jne 0x7929
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq