// File: /workspace/github_c_makefile_star2_O1/deplinenoise[P]vlink/targets.c
// Function: calc_relocs
void calc_relocs(struct GlobalVars *gv,struct LinkedSection *ls)
/* calculate and insert all relocations of a section */
{
  const char *fn = "calc_reloc(): ";
  struct Reloc *r;

  if (ls == NULL)
    return;

  for (r=(struct Reloc *)ls->relocs.first; r->n.next!=NULL;
       r=(struct Reloc *)r->n.next) {
    lword s,a,p,val;

    if (r->relocsect.lnk == NULL) {
      if (r->flags & RELF_DYNLINK)
        continue;  /* NULL, because it was resolved by a shared object */
      else
        ierror("calc_relocs: Reloc type %d (%s) at %s+0x%lx (addend 0x%llx)"
               " is missing a relocsect.lnk",
               (int)r->rtype,reloc_name[r->rtype],ls->name,r->offset,r->addend);
    }

    s = r->relocsect.lnk->base;
    a = r->addend;
    p = ls->base + r->offset;
    val = 0;

    switch (r->rtype) {

      case R_NONE:
        continue;

      case R_ABS:
        val = s+a;
        break;

      case R_PC:
        val = s+a-p;
        break;

#if 0 /* @@@ shouldn't occur - already resolved by linker_relocate() ??? */
      case R_SD:
        val = s+a - _SDA_BASE_; /* @@@ */
        break;
#endif

      default:
        ierror("%sReloc type %d (%s) is currently not supported",
               fn,(int)r->rtype,reloc_name[r->rtype]);
        break;
    }

    if (val = writesection(gv,ls->data+r->offset,r,val)) {
      struct RelocInsert *ri;

      /* Calculated value doesn't fit into relocation type x ... */
      if (ri = r->insert)
        error(35,gv->dest_name,ls->name,r->offset,val,reloc_name[r->rtype],
              (int)ri->bpos,(int)ri->bsiz,ri->mask);
      else
        ierror("%sReloc (%s+%lx), type=%s, without RelocInsert",
               fn,ls->name,r->offset,reloc_name[r->rtype]);
    }
  }
}

// Assembly:
testq %rsi, %rsi
je 0xb925
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %rbx
movq 0x80(%rsi), %r15
cmpq $0x0, (%r15)
je 0xb917
movq %rdi, %r14
leaq 0x23af6(%rip), %r12     # 0x2f2f0
leaq 0x16fa1(%rip), %rbp     # 0x227a2
cmpq $0x0, 0x18(%r15)
jne 0xb836
cmpb $0x3f, 0x39(%r15)
ja 0xb90a
movzbl 0x38(%r15), %esi
movq (%r12,%rsi,8), %rdx
movq 0x18(%rbx), %rcx
movq 0x20(%r15), %r8
movq 0x28(%r15), %r9
leaq 0x16f82(%rip), %rdi     # 0x227b1
xorl %eax, %eax
callq 0x52b0
movzbl 0x38(%r15), %edx
testl %edx, %edx
je 0xb90a
movq 0x18(%r15), %rax
movq 0x28(%r15), %rcx
movq 0x40(%rax), %rax
cmpl $0x2, %edx
je 0xb85e
cmpl $0x1, %edx
jne 0xb86e
addq %rax, %rcx
jmp 0xb887
movq 0x40(%rbx), %rdx
addq %rax, %rcx
addq 0x20(%r15), %rdx
subq %rdx, %rcx
jmp 0xb887
movl %edx, %eax
movq (%r12,%rax,8), %rcx
leaq 0x16f8d(%rip), %rdi     # 0x22808
movq %rbp, %rsi
xorl %eax, %eax
callq 0x52b0
xorl %ecx, %ecx
movq 0x78(%rbx), %rsi
addq 0x20(%r15), %rsi
movq %r14, %rdi
movq %r15, %rdx
callq 0xb5b0
testq %rax, %rax
je 0xb90a
movq 0x30(%r15), %r10
testq %r10, %r10
je 0xb8e8
movq 0x48(%r14), %rsi
movq 0x18(%rbx), %rdx
movq 0x20(%r15), %rcx
movzbl 0x38(%r15), %edi
movq (%r12,%rdi,8), %r9
movzwl 0x8(%r10), %r11d
movzwl 0xa(%r10), %r13d
subq $0x8, %rsp
movl $0x23, %edi
movq %rax, %r8
xorl %eax, %eax
pushq 0x10(%r10)
pushq %r13
pushq %r11
callq 0x6f84
addq $0x20, %rsp
jmp 0xb90a
movq 0x18(%rbx), %rdx
movq 0x20(%r15), %rcx
movzbl 0x38(%r15), %eax
movq (%r12,%rax,8), %r8
leaq 0x16f38(%rip), %rdi     # 0x22838
movq %rbp, %rsi
xorl %eax, %eax
callq 0x52b0
movq (%r15), %r15
cmpq $0x0, (%r15)
jne 0xb801
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq