// File: /workspace/github_c_makefile_star2_O2/deplinenoise[P]vlink/linker.c
// Function: linker_relrefs
void linker_relrefs(struct GlobalVars *gv)
/* All relocations and unresolved xrefs with a relative reference
   to other sections are collected. A second task is to detect
   and handle base-relative and GOT/PLT references. */
{
  struct ObjectUnit *obj;
  struct Section *sec;
  struct Symbol *sdabase = NULL;
  struct Symbol *sda2base = NULL;
  struct Symbol *r13init = NULL;

  for (obj=(struct ObjectUnit *)gv->selobjects.first;
       obj->n.next!=NULL; obj=(struct ObjectUnit *)obj->n.next) {

    for (sec=(struct Section *)obj->sections.first;
         sec->n.next!=NULL; sec=(struct Section *)sec->n.next) {
      struct RelRef **rr = &sec->relrefs;
      struct Symbol *xdef;
      struct Reloc *xref,*reloc;

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

        if (xdef = xref->relocsect.symbol) {
          if (xdef->relsect!=NULL &&
              (xdef->type==SYM_RELOC || xdef->type==SYM_COMMON)) {

            if ((xref->rtype==R_SD || xref->rtype==R_SD21 ||
                 xref->rtype==R_MOSDREL) && xdef->type==SYM_COMMON) {
                /* small data common symbol - assign .scommon section */
                xdef->relsect = scommon_section(gv,xdef->relsect->obj);
            }

            if (xref->rtype==R_PC && xdef->relsect!=sec) {
              /* relative reference to different section */
              addrelref(rr,xdef->relsect);
            }

            else if (xref->rtype==R_SD || xref->rtype==R_MOSDREL) {
              /* other section is accessed base relative from this one */
              xdef->relsect->flags |= SF_SMALLDATA;
              if (!gv->textbaserel && xdef->relsect->type==ST_CODE)
                error(121,getobjname(sec->obj),sec->name,xref->offset);
            }
          }
        }
      }

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

        if (reloc->rtype==R_PC && reloc->relocsect.ptr!=sec) {
          /* relative reference to different section */
          addrelref(rr,reloc->relocsect.ptr);
        }

        else if (reloc->rtype==R_GOT || reloc->rtype==R_GOTPC) {
          /* a local relocation to a GOT entry may create that entry */
          dyn_reloc_entry(gv,reloc,GOT_LOCAL);
        }

        else if (reloc->rtype==R_PLT || reloc->rtype==R_PLTPC) {
          /* a local relocation to a PLT entry may create that entry */
          dyn_reloc_entry(gv,reloc,PLT_LOCAL);
        }

        else if (reloc->rtype == R_SD) {
          /* other section is accessed base relative from this one */
          reloc->relocsect.ptr->flags |= SF_SMALLDATA;
          if (!sdabase) {
            /* R_SD relocation implies a reference to _SDA_BASE_ */
            if (sdabase = find_any_symbol(gv,sec,sdabase_name))
              sdabase->flags |= SYMF_REFERENCED;
          }
          if (!gv->textbaserel && reloc->relocsect.ptr->type==ST_CODE)
            error(121,getobjname(sec->obj),sec->name,reloc->offset);
        }

        else if (reloc->rtype == R_SD2) {
          if (!sda2base) {
            /* R_SD2 relocation implies a reference to _SDA2_BASE_ */
            if (sda2base = find_any_symbol(gv,sec,sda2base_name))
              sda2base->flags |= SYMF_REFERENCED;
          }
        }

        else if (reloc->rtype == R_MOSDREL) {
          /* other section is accessed base relative from this one */
          reloc->relocsect.ptr->flags |= SF_SMALLDATA;
          if (!r13init) {
            /* R_MOSDREL relocation implies a reference to __r13_init */
            if (r13init = find_any_symbol(gv,sec,r13init_name))
              r13init->flags |= SYMF_REFERENCED;
          }
          if (!gv->textbaserel && reloc->relocsect.ptr->type==ST_CODE)
            error(121,getobjname(sec->obj),sec->name,reloc->offset);
        }

      }
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbx
leaq 0x138(%rdi), %rax
leaq 0x1844a(%rip), %r12     # 0x1ffb4
xorl %r15d, %r15d
movq $0x0, 0x8(%rsp)
movq $0x0, 0x18(%rsp)
movq (%rax), %rax
cmpq $0x0, (%rax)
je 0x7dfa
movq %rax, 0x20(%rsp)
leaq 0x20(%rax), %rbp
movq (%rbp), %rbp
cmpq $0x0, (%rbp)
je 0x7df0
leaq 0x80(%rbp), %rax
movq %rax, 0x10(%rsp)
andq $0x0, 0x80(%rbp)
leaq 0x68(%rbp), %r13
movq (%r13), %r13
cmpq $0x0, (%r13)
je 0x7c86
movq 0x18(%r13), %r14
testq %r14, %r14
je 0x7bbc
movq 0x38(%r14), %rax
testq %rax, %rax
je 0x7bbc
movb 0x40(%r14), %dl
movl %edx, %ecx
andb $-0x2, %cl
cmpb $0x2, %cl
jne 0x7bbc
movzbl 0x38(%r13), %ecx
cmpq $0x22, %rcx
ja 0x7c1f
movabsq $0x600000400, %rsi      # imm = 0x600000400
btq %rcx, %rsi
jae 0x7c1f
cmpb $0x3, %dl
jne 0x7c1f
movq 0x10(%rax), %rsi
movq %rbx, %rdi
callq 0xbb74
movq %rax, 0x38(%r14)
movb 0x38(%r13), %cl
cmpb $0x22, %cl
je 0x7c48
movzbl %cl, %ecx
cmpl $0xa, %ecx
je 0x7c48
cmpl $0x2, %ecx
jne 0x7bbc
cmpq %rbp, %rax
je 0x7bbc
movq 0x10(%rsp), %rdi
movq %rax, %rsi
callq 0x7e09
jmp 0x7bbc
orb $0x4, 0x2d(%rax)
cmpl $0x0, 0x84(%rbx)
jne 0x7bbc
cmpb $0x1, 0x2c(%rax)
jne 0x7bbc
movq 0x10(%rbp), %rdi
callq 0x6e1c
movq 0x20(%rbp), %rdx
movq 0x20(%r13), %rcx
pushq $0x79
popq %rdi
movq %rax, %rsi
xorl %eax, %eax
callq 0x6c19
jmp 0x7bbc
leaq 0x50(%rbp), %r13
pushq $0x4
popq %r14
movq (%r13), %r13
cmpq $0x0, (%r13)
je 0x7b95
movzbl 0x38(%r13), %eax
leal -0x2(%rax), %ecx
cmpl $0x8, %ecx
ja 0x7cc3
movslq (%r12,%rcx,4), %rax
addq %r12, %rax
jmpq *%rax
movq %rbx, %rdi
movq %r13, %rsi
movl %r14d, %edx
callq 0x7e48
jmp 0x7c8e
cmpl $0x20, %eax
je 0x7d67
cmpl $0x22, %eax
jne 0x7c8e
movq 0x18(%r13), %rax
orb $0x4, 0x2d(%rax)
movq 0x18(%rsp), %rax
testq %rax, %rax
jne 0x7dad
movq %rbx, %rdi
movq %rbp, %rsi
leaq 0x18951(%rip), %rdx     # 0x20645
callq 0xaa79
testq %rax, %rax
je 0x7dab
orb $0x2, 0x41(%rax)
jmp 0x7dad
movq %rbx, %rdi
movq %r13, %rsi
pushq $0x5
popq %rdx
callq 0x7e48
jmp 0x7c8e
movq 0x18(%r13), %rsi
cmpq %rbp, %rsi
je 0x7c8e
movq 0x10(%rsp), %rdi
callq 0x7e09
jmp 0x7c8e
movq 0x18(%r13), %rax
orb $0x4, 0x2d(%rax)
testq %r15, %r15
jne 0x7db2
movq %rbx, %rdi
movq %rbp, %rsi
leaq 0x18891(%rip), %rdx     # 0x205e5
callq 0xaa79
testq %rax, %rax
je 0x7da6
movq %rax, %r15
orb $0x2, 0x41(%rax)
jmp 0x7db2
cmpq $0x0, 0x8(%rsp)
jne 0x7c8e
movq %rbx, %rdi
movq %rbp, %rsi
leaq 0x18870(%rip), %rdx     # 0x205f0
callq 0xaa79
movl $0x0, %ecx
movq %rcx, 0x8(%rsp)
testq %rax, %rax
je 0x7c8e
orb $0x2, 0x41(%rax)
movq %rax, 0x8(%rsp)
jmp 0x7c8e
xorl %r15d, %r15d
jmp 0x7db2
xorl %eax, %eax
movq %rax, 0x18(%rsp)
cmpl $0x0, 0x84(%rbx)
jne 0x7c8e
movq 0x18(%r13), %rax
cmpb $0x1, 0x2c(%rax)
jne 0x7c8e
movq 0x10(%rbp), %rdi
callq 0x6e1c
movq 0x20(%rbp), %rdx
movq 0x20(%r13), %rcx
pushq $0x79
popq %rdi
movq %rax, %rsi
xorl %eax, %eax
callq 0x6c19
jmp 0x7c8e
movq 0x20(%rsp), %rax
jmp 0x7b7f
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq