// File: /workspace/github_c_makefile_star2_O1/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), %rax
cmpq $0x0, (%rax)
je 0x818f
movq %rdi, %rbx
leaq 0x1a0dc(%rip), %r13     # 0x21fc0
xorl %r14d, %r14d
movq $0x0, 0x8(%rsp)
xorl %ebp, %ebp
movq %rax, 0x20(%rsp)
movq 0x20(%rax), %r15
cmpq $0x0, (%r15)
je 0x817d
movq %r14, 0x10(%rsp)
leaq 0x80(%r15), %rax
movq %rax, 0x18(%rsp)
movq $0x0, 0x80(%r15)
movq 0x68(%r15), %r12
cmpq $0x0, (%r12)
je 0x8003
movq 0x18(%r12), %r14
testq %r14, %r14
je 0x7ffa
movq 0x38(%r14), %rax
testq %rax, %rax
je 0x7ffa
movb 0x40(%r14), %cl
movl %ecx, %edx
andb $-0x2, %dl
cmpb $0x2, %dl
jne 0x7ffa
movzbl 0x38(%r12), %edx
cmpq $0x22, %rdx
ja 0x7f8e
movabsq $0x600000400, %rsi      # imm = 0x600000400
btq %rdx, %rsi
jae 0x7f8e
cmpb $0x3, %cl
jne 0x7f8e
movq 0x10(%rax), %rsi
movq %rbx, %rdi
callq 0xc29f
movq %rax, 0x38(%r14)
movzbl 0x38(%r12), %eax
cmpl $0x22, %eax
je 0x7fc2
cmpl $0xa, %eax
je 0x7fc2
cmpl $0x2, %eax
jne 0x7ffa
movq 0x38(%r14), %rsi
cmpq %r15, %rsi
je 0x7fb8
movq 0x18(%rsp), %rdi
callq 0x819e
jmp 0x7ffa
cmpl $0x22, %eax
je 0x7fc2
cmpl $0xa, %eax
jne 0x7ffa
movq 0x38(%r14), %rax
orb $0x4, 0x2d(%rax)
cmpl $0x0, 0x84(%rbx)
jne 0x7ffa
cmpb $0x1, 0x2c(%rax)
jne 0x7ffa
movq 0x10(%r15), %rdi
callq 0x7174
movq 0x20(%r15), %rdx
movq 0x20(%r12), %rcx
movl $0x79, %edi
movq %rax, %rsi
xorl %eax, %eax
callq 0x6f84
movq (%r12), %r12
jmp 0x7f25
movq 0x50(%r15), %r12
cmpq $0x0, (%r12)
je 0x8170
movq 0x10(%rsp), %r14
movzbl 0x38(%r12), %eax
leal -0x2(%rax), %ecx
cmpl $0x8, %ecx
ja 0x8088
movslq (%r13,%rcx,4), %rcx
addq %r13, %rcx
jmpq *%rcx
movq %rbx, %rdi
movq %r12, %rsi
movl $0x4, %edx
jmp 0x8079
movq 0x18(%r12), %rsi
cmpq %r15, %rsi
je 0x8055
movq 0x18(%rsp), %rdi
callq 0x819e
jmp 0x815f
cmpl $0x9, %eax
jg 0x8083
leal -0x3(%rax), %ecx
cmpl $0x2, %ecx
jb 0x802f
addl $-0x7, %eax
cmpl $0x2, %eax
jae 0x815f
movq %rbx, %rdi
movq %r12, %rsi
movl $0x5, %edx
callq 0x81df
jmp 0x815f
cmpl $0xa, %eax
je 0x80c0
cmpl $0x20, %eax
je 0x80ee
cmpl $0x22, %eax
jne 0x815f
movq 0x18(%r12), %rax
orb $0x4, 0x2d(%rax)
testq %r14, %r14
jne 0x811f
movq %rbx, %rdi
movq %r15, %rsi
leaq 0x1a594(%rip), %rdx     # 0x22645
callq 0xb071
testq %rax, %rax
je 0x811c
movq %rax, %r14
jmp 0x80e8
movq 0x18(%r12), %rax
orb $0x4, 0x2d(%rax)
testq %rbp, %rbp
jne 0x811f
movq %rbx, %rdi
movq %r15, %rsi
leaq 0x1a50a(%rip), %rdx     # 0x225e5
callq 0xb071
testq %rax, %rax
je 0x8118
movq %rax, %rbp
orb $0x2, 0x41(%rax)
jmp 0x811f
cmpq $0x0, 0x8(%rsp)
jne 0x815f
movq %rbx, %rdi
movq %r15, %rsi
leaq 0x1a4ed(%rip), %rdx     # 0x225f0
callq 0xb071
testq %rax, %rax
je 0x8156
movq %rax, 0x8(%rsp)
orb $0x2, 0x41(%rax)
jmp 0x815f
xorl %ebp, %ebp
jmp 0x811f
xorl %r14d, %r14d
cmpl $0x0, 0x84(%rbx)
jne 0x815f
movq 0x18(%r12), %rax
cmpb $0x1, 0x2c(%rax)
jne 0x815f
movq 0x10(%r15), %rdi
callq 0x7174
movq 0x20(%r15), %rdx
movq 0x20(%r12), %rcx
movl $0x79, %edi
movq %rax, %rsi
xorl %eax, %eax
callq 0x6f84
jmp 0x815f
movq $0x0, 0x8(%rsp)
movq (%r12), %r12
cmpq $0x0, (%r12)
jne 0x8017
jmp 0x8175
movq 0x10(%rsp), %r14
movq (%r15), %r15
jmp 0x7efb
movq 0x20(%rsp), %rax
movq (%rax), %rax
cmpq $0x0, (%rax)
jne 0x7ef2
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq