// File: /workspace/github_c_makefile_star2_O2/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 0xb2c7
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x80(%rsi), %r15
leaq 0x24168(%rip), %r12     # 0x2f300
leaq 0x15603(%rip), %rbp     # 0x207a2
movq (%r15), %r15
cmpq $0x0, (%r15)
je 0xb2b9
movq 0x18(%r15), %rax
testq %rax, %rax
jne 0xb1e3
cmpb $0x3f, 0x39(%r15)
ja 0xb19f
movzbl 0x38(%r15), %esi
movq (%r12,%rsi,8), %rdx
movq 0x18(%rbx), %rcx
movq 0x20(%r15), %r8
movq 0x28(%r15), %r9
leaq 0x155d9(%rip), %rdi     # 0x207b1
xorl %eax, %eax
callq 0x6b2c
movq 0x18(%r15), %rax
movzbl 0x38(%r15), %edx
testl %edx, %edx
je 0xb19f
movq 0x40(%rax), %rax
movq 0x28(%r15), %rcx
cmpl $0x2, %edx
je 0xb203
cmpl $0x1, %edx
jne 0xb213
addq %rax, %rcx
jmp 0xb22c
movq 0x40(%rbx), %rdx
addq %rax, %rcx
addq 0x20(%r15), %rdx
subq %rdx, %rcx
jmp 0xb22c
movl %edx, %eax
movq (%r12,%rax,8), %rcx
leaq 0x155e8(%rip), %rdi     # 0x20808
movq %rbp, %rsi
xorl %eax, %eax
callq 0x6b2c
xorl %ecx, %ecx
movq 0x78(%rbx), %rsi
addq 0x20(%r15), %rsi
movq %r14, %rdi
movq %r15, %rdx
callq 0xaf7b
testq %rax, %rax
je 0xb19f
movq 0x30(%r15), %r10
testq %r10, %r10
je 0xb292
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
pushq $0x23
popq %rdi
movq %rax, %r8
xorl %eax, %eax
pushq 0x10(%r10)
pushq %r13
pushq %r11
callq 0x6c19
addq $0x20, %rsp
jmp 0xb19f
movq 0x18(%rbx), %rdx
movq 0x20(%r15), %rcx
movzbl 0x38(%r15), %eax
movq (%r12,%rax,8), %r8
leaq 0x1558e(%rip), %rdi     # 0x20838
movq %rbp, %rsi
xorl %eax, %eax
callq 0x6b2c
jmp 0xb19f
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq