// File: /workspace/github_c_makefile_star2_O3/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 0x138(%rdi), %rcx
movq (%rcx), %rax
testq %rax, %rax
je 0x80f2
movq %rdi, %rbx
leaq 0x1a15c(%rip), %r13     # 0x21fb4
xorl %r14d, %r14d
movq $0x0, 0x8(%rsp)
xorl %ebp, %ebp
movq %rcx, 0x20(%rsp)
movq 0x20(%rcx), %r15
cmpq $0x0, (%r15)
movq %rax, %rcx
je 0x80e6
movq %r14, 0x10(%rsp)
leaq 0x80(%r15), %rax
movq %rax, 0x18(%rsp)
movq $0x0, 0x80(%r15)
movq 0x68(%r15), %r12
cmpq $0x0, (%r12)
je 0x7f6d
movq 0x18(%r12), %r14
testq %r14, %r14
je 0x7f64
movq 0x38(%r14), %rax
testq %rax, %rax
je 0x7f64
movb 0x40(%r14), %dl
movl %edx, %ecx
andb $-0x2, %cl
cmpb $0x2, %cl
jne 0x7f64
movzbl 0x38(%r12), %ecx
cmpq $0x22, %rcx
ja 0x7f0a
movabsq $0x600000400, %rsi      # imm = 0x600000400
btq %rcx, %rsi
jae 0x7f0a
cmpb $0x3, %dl
jne 0x7f0a
movq 0x10(%rax), %rsi
movq %rbx, %rdi
callq 0xc1f2
movq %rax, 0x38(%r14)
movb 0x38(%r12), %cl
cmpb $0x22, %cl
je 0x7f30
movzbl %cl, %ecx
cmpl $0xa, %ecx
je 0x7f30
cmpl $0x2, %ecx
jne 0x7f64
cmpq %r15, %rax
je 0x7f64
movq 0x18(%rsp), %rdi
movq %rax, %rsi
callq 0x8101
jmp 0x7f64
orb $0x4, 0x2d(%rax)
cmpl $0x0, 0x84(%rbx)
jne 0x7f64
cmpb $0x1, 0x2c(%rax)
jne 0x7f64
movq 0x10(%r15), %rdi
callq 0x7104
movq 0x20(%r15), %rdx
movq 0x20(%r12), %rcx
movl $0x79, %edi
movq %rax, %rsi
xorl %eax, %eax
callq 0x6f14
movq (%r12), %r12
jmp 0x7e9c
movq 0x50(%r15), %r12
cmpq $0x0, (%r12)
je 0x80cc
movq 0x10(%rsp), %r14
movzbl 0x38(%r12), %eax
leal -0x2(%rax), %ecx
cmpl $0x8, %ecx
ja 0x7fa6
movslq (%r13,%rcx,4), %rax
addq %r13, %rax
jmpq *%rax
movq %rbx, %rdi
movq %r12, %rsi
movl $0x4, %edx
jmp 0x7ff5
cmpl $0x20, %eax
je 0x804a
cmpl $0x22, %eax
jne 0x80bb
movq 0x18(%r12), %rax
orb $0x4, 0x2d(%rax)
testq %r14, %r14
jne 0x8086
movq %rbx, %rdi
movq %r15, %rsi
leaq 0x1a65e(%rip), %rdx     # 0x22635
callq 0xafab
testq %rax, %rax
je 0x8083
movq %rax, %r14
jmp 0x8044
movq %rbx, %rdi
movq %r12, %rsi
movl $0x5, %edx
callq 0x8142
jmp 0x80bb
movq 0x18(%r12), %rsi
cmpq %r15, %rsi
je 0x80bb
movq 0x18(%rsp), %rdi
callq 0x8101
jmp 0x80bb
movq 0x18(%r12), %rax
orb $0x4, 0x2d(%rax)
testq %rbp, %rbp
jne 0x8086
movq %rbx, %rdi
movq %r15, %rsi
leaq 0x1a59e(%rip), %rdx     # 0x225d5
callq 0xafab
testq %rax, %rax
je 0x8074
movq %rax, %rbp
orb $0x2, 0x41(%rax)
jmp 0x8086
cmpq $0x0, 0x8(%rsp)
jne 0x80bb
movq %rbx, %rdi
movq %r15, %rsi
leaq 0x1a581(%rip), %rdx     # 0x225e0
callq 0xafab
testq %rax, %rax
je 0x8078
movq %rax, 0x8(%rsp)
orb $0x2, 0x41(%rax)
jmp 0x80bb
xorl %ebp, %ebp
jmp 0x8086
movq $0x0, 0x8(%rsp)
jmp 0x80bb
xorl %r14d, %r14d
cmpl $0x0, 0x84(%rbx)
jne 0x80bb
movq 0x18(%r12), %rax
cmpb $0x1, 0x2c(%rax)
jne 0x80bb
movq 0x10(%r15), %rdi
callq 0x7104
movq 0x20(%r15), %rdx
movq 0x20(%r12), %rcx
movl $0x79, %edi
movq %rax, %rsi
xorl %eax, %eax
callq 0x6f14
movq (%r12), %r12
cmpq $0x0, (%r12)
jne 0x7f81
jmp 0x80d1
movq 0x10(%rsp), %r14
movq (%r15), %r15
cmpq $0x0, (%r15)
jne 0x7e7c
movq 0x20(%rsp), %rax
movq (%rax), %rcx
movq (%rcx), %rax
testq %rax, %rax
jne 0x7e66
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq