// File: /workspace/github_c_makefile_star2_O1/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 0x27843(%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 0x795e
cmpl $0x0, 0x5c(%rbx)
je 0x7988
movl $0x1, 0x240(%rbx)
movq %rbx, %rdi
callq 0xa6c9
movzbl 0x50(%rbx), %eax
movq (%r14,%rax,8), %rax
movq 0x58(%rax), %rax
testq %rax, %rax
je 0x7992
movq %rbx, %rdi
callq *%rax
jmp 0x7992
movl $0x0, 0x240(%rbx)
movq 0xa0(%rbx), %rcx
testq %rcx, %rcx
je 0x79b4
leaq 0x1a80d(%rip), %rdi     # 0x221b2
movl $0x20, %esi
movl $0x1, %edx
callq 0x5270
cmpq $0x0, (%rbp)
je 0x7e64
movl $0x0, 0x8(%rsp)
movq 0x20(%rbp), %r15
cmpq $0x0, (%r15)
movq %rbp, (%rsp)
je 0x7e2e
movq 0x68(%r15), %r13
cmpq $0x0, (%r13)
je 0x7e21
leaq 0x10(%r15), %rax
movq %rax, 0x10(%rsp)
cmpb $0xd, 0x38(%r13)
je 0x7d72
movq 0x10(%r13), %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq 0xa82c
testq %rax, %rax
je 0x7a25
movq %rax, %r12
cmpb $0xc, 0x38(%r13)
jne 0x7a28
movq 0x38(%r12), %rax
cmpq %rbp, 0x10(%rax)
je 0x7a28
xorl %r12d, %r12d
leaq 0x10(%r13), %r14
testq %r12, %r12
jne 0x7a78
movq (%r14), %rsi
movq %rbx, %rdi
callq 0xaf21
movq %rax, %r12
testq %rax, %rax
jne 0x7a6f
movzbl 0x50(%rbx), %eax
leaq 0x27731(%rip), %rcx     # 0x2f180
movq (%rcx,%rax,8), %rax
movq 0x48(%rax), %rax
testq %rax, %rax
je 0x7a6c
movq %rbx, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq *%rax
movq %rax, %r12
jmp 0x7a6f
xorl %r12d, %r12d
testq %r12, %r12
je 0x7b21
movq (%r14), %rsi
movq %rbx, %rdi
callq 0x730e
testl %eax, %eax
jne 0x7d27
cmpb $0xc, 0x38(%r13)
jne 0x7a97
movb $0x2, 0x38(%r13)
movq 0x38(%r12), %rax
testq %rax, %rax
je 0x7aa7
cmpb $0x4, 0x2c(%rax)
jne 0x7ab0
movq %r12, 0x18(%r13)
jmp 0x7d6c
movq 0x10(%rax), %rbp
movq 0x10(%rbp), %rax
movzbl 0x39(%rax), %ecx
cmpl $0x8, %ecx
je 0x7c89
cmpl $0x4, %ecx
je 0x7b75
cmpl $0x3, %ecx
jne 0x7cef
testb $0x1, 0x80(%rbp)
jne 0x7b14
movq 0x90(%rbx), %rdi
testq %rdi, %rdi
je 0x7b01
movq 0x18(%rbp), %rdx
movq (%r14), %rcx
leaq 0x1a6f7(%rip), %rsi     # 0x221f1
xorl %eax, %eax
callq 0x5180
movq (%rsp), %rdi
movq %rbp, %rsi
callq 0x6731
orb $0x1, 0x80(%rbp)
movq 0x10(%rbp), %rax
movb $0x1, 0x39(%rax)
jmp 0x7cef
cmpl $0x0, 0x54(%rbx)
jne 0x7d72
cmpl $0x0, 0x58(%rbx)
jne 0x7d72
testb $0x1, 0x39(%r13)
jne 0x7dac
movq 0x20(%r13), %rsi
movq %r15, %rdi
callq 0x71d9
movq 0x10(%r15), %rdi
callq 0x7174
movq 0x20(%r15), %rdx
movq 0x10(%r13), %r8
movq 0x20(%r13), %rcx
movl $0x15, %edi
movq %rax, %rsi
xorl %eax, %eax
callq 0x6f84
jmp 0x7d72
testb $0x1, 0x80(%rbp)
jne 0x7c2b
movq 0x90(%rbx), %rdi
testq %rdi, %rdi
je 0x7baa
movq 0x10(%rax), %rdx
movq (%r14), %r8
leaq 0x1a67f(%rip), %rsi     # 0x2221b
leaq 0x1a5ff(%rip), %rcx     # 0x221a2
xorl %eax, %eax
callq 0x5180
movq %rbp, %rdi
callq 0x6789
movq (%rsp), %rdi
movq %rax, %rsi
callq 0x6731
movq %rbx, %rdi
movq %rbp, %rsi
callq 0xb9f5
orb $0x1, 0x80(%rbp)
movq %rbx, %rdi
movq %rbp, %rsi
callq 0xb09f
testb $0x4, 0xc(%rsp)
jne 0x7c2b
cmpl $0x0, 0x240(%rbx)
jne 0x7c1a
movl $0x1, 0x240(%rbx)
movq %rbx, %rdi
callq 0xa6c9
movzbl 0x50(%rbx), %eax
leaq 0x27578(%rip), %rcx     # 0x2f180
movq (%rcx,%rax,8), %rax
movq 0x58(%rax), %rax
testq %rax, %rax
je 0x7c1a
movq %rbx, %rdi
callq *%rax
movq 0x18(%rbp), %rsi
movq %rbx, %rdi
movl $0x8, %edx
callq 0xa709
movl 0xc(%rsp), %eax
andl $0x4, %eax
orl 0x54(%rbx), %eax
jne 0x7cf3
movzbl 0x38(%r13), %eax
cmpl $0xf, %eax
ja 0x7e00
movl $0xc3f8, %ecx           # imm = 0xC3F8
btl %eax, %ecx
jb 0x7cef
movl $0x6, %ecx
btl %eax, %ecx
jae 0x7e00
cmpb $0x2, 0x42(%r12)
jne 0x7de4
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rdx
movl $0x2, %ecx
callq 0x7e73
movq %rax, %r12
jmp 0x7cef
testb $0x1, 0x80(%rbp)
jne 0x7cef
movq 0x90(%rbx), %rdi
testq %rdi, %rdi
je 0x7cbe
movq 0x10(%rax), %rdx
movq 0x18(%rbp), %rcx
movq (%r14), %r9
leaq 0x1a5be(%rip), %rsi     # 0x2226e
leaq 0x1a4eb(%rip), %r8      # 0x221a2
xorl %eax, %eax
callq 0x5180
movq %rbp, %rdi
callq 0x6789
movq (%rsp), %rdi
movq %rax, %rsi
callq 0x6731
movq %rbx, %rdi
movq %rbp, %rsi
callq 0xb9f5
orb $0x1, 0x80(%rbp)
movq %rbx, %rdi
movq %rbp, %rsi
callq 0xb09f
movq %r12, 0x18(%r13)
cmpl $0x0, 0x54(%rbx)
jne 0x7d50
movzbl 0x38(%r13), %eax
leal -0x7(%rax), %ecx
cmpl $0x3, %ecx
jb 0x7d39
addl $-0x3, %eax
cmpl $0x2, %eax
ja 0x7d50
movq %rbx, %rdi
movq %r12, %rsi
movl $0x1, %edx
callq 0xa709
testq %rax, %rax
cmoveq %r12, %rax
jmp 0x7d4c
movq %r14, %rdi
movq 0x10(%rsp), %rsi
callq 0x541d
jmp 0x7a8b
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rdx
movl $0x2, %ecx
callq 0x7e73
movq %rax, 0x18(%r13)
movq (%rsp), %rax
movq 0x10(%rax), %rax
cmpb $0x4, 0x39(%rax)
jne 0x7d68
movq 0x10(%rbp), %rax
cmpb $0x4, 0x39(%rax)
jne 0x7d86
movq (%rsp), %rbp
orb $0x2, 0x41(%r12)
movq (%r13), %r13
cmpq $0x0, (%r13)
jne 0x79f1
jmp 0x7e21
movb 0x41(%r12), %al
cmpb $0x3f, %al
movq (%rsp), %rbp
ja 0x7d6c
orb $-0x80, %al
movb %al, 0x41(%r12)
movq %rbx, %rdi
movq %r12, %rsi
movl $0x7, %edx
callq 0xa709
jmp 0x7d6c
movq (%r14), %rsi
movq %rbx, %rdi
xorl %edx, %edx
movl $0x2, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
pushq $0x0
pushq $0x1
callq 0xae7f
addq $0x10, %rsp
movq %rax, %r12
movq %rbx, %rdi
movq %rbp, %rsi
callq 0xc365
movq %rax, 0x38(%r12)
jmp 0x7a78
movq %rbx, %rdi
movq %r12, %rsi
movl $0x3, %edx
callq 0xa709
testq %rax, %rax
cmovneq %rax, %r12
jmp 0x7cef
leaq 0x274e9(%rip), %rcx     # 0x2f2f0
movq (%rcx,%rax,8), %rsi
movq (%r14), %rdx
leaq 0x1a40e(%rip), %rdi     # 0x22223
xorl %eax, %eax
callq 0x52b0
jmp 0x7cef
movq (%r15), %r15
cmpq $0x0, (%r15)
jne 0x79d9
movq (%rbp), %rax
cmpq $0x0, (%rax)
setne %al
cmpl $0x0, 0x8(%rsp)
setne %cl
orb %al, %cl
jne 0x7e55
movq %rbx, %rdi
callq 0xc4d1
movl $0x1, 0x8(%rsp)
movq (%rbp), %rbp
cmpq $0x0, (%rbp)
jne 0x79c7
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq