LoongArch: Use functions instead of magic numbers.

Replace the magic numbers in gas(tc-loongarch.c) and
  bfd(elfnn-loongarch.c) with the functions defined in
  the howto table(elfxx-loongarch.c).

  gas/
    * config/tc-loongarch.c: use functions.

  bfd/
    * elfnn-loongarch.c: use functions.
    * elfxx-loongarch.c: define functions.
    * elfxx-loongarch.h
This commit is contained in:
liuzhensong 2022-02-21 14:13:43 +08:00
parent e36144c932
commit 748594bc07
4 changed files with 700 additions and 601 deletions

View File

@ -1446,7 +1446,6 @@ loongarch_check_offset (const Elf_Internal_Rela *rel,
return bfd_reloc_ok;
}
#define LARCH_RELOC_PERFORM_3OP(op1, op2, op3) \
({ \
bfd_reloc_status_type ret = loongarch_pop (&op2); \
@ -1459,50 +1458,21 @@ loongarch_check_offset (const Elf_Internal_Rela *rel,
ret; \
})
#define LARCH_RELOC_UINT32_BIT_MASK(bitsize) \
(~((0x1U << (bitsize)) - 1))
static bfd_reloc_status_type
loongarch_reloc_rewrite_imm_insn (const Elf_Internal_Rela *rel,
const asection *input_section ATTRIBUTE_UNUSED,
reloc_howto_type *howto, bfd *input_bfd,
bfd_byte *contents, int64_t op,
bool is_signed)
bfd_byte *contents, bfd_vma reloc_val)
{
/* Check op low bits if rightshift != 0, before rightshift */
if (howto->rightshift
&& (((0x1U << howto->rightshift) - 1) & op))
return bfd_reloc_overflow;
uint32_t imm = (uint32_t)(int32_t)(op >> howto->rightshift);
if (is_signed)
{
if (op >= 0)
{
if (LARCH_RELOC_UINT32_BIT_MASK (howto->bitsize - 1) & imm)
return bfd_reloc_overflow;
}
else
{
if ((LARCH_RELOC_UINT32_BIT_MASK (howto->bitsize - 1) & imm)
!= LARCH_RELOC_UINT32_BIT_MASK (howto->bitsize - 1))
return bfd_reloc_overflow;
}
}
else
{
if (LARCH_RELOC_UINT32_BIT_MASK (howto->bitsize) & imm)
return bfd_reloc_overflow;
}
int bits = bfd_get_reloc_size (howto) * 8;
uint32_t insn = bfd_get (bits, input_bfd, contents + rel->r_offset);
imm = imm & ((0x1U << howto->bitsize) - 1);
imm <<= howto->bitpos;
insn = ((insn & howto->src_mask)
| ((insn & (~(uint32_t) howto->dst_mask)) | imm));
if (!loongarch_adjust_reloc_bitsfield(howto, &reloc_val))
return bfd_reloc_overflow;
insn = (insn & (uint32_t)howto->src_mask)
| ((insn & (~(uint32_t)howto->dst_mask)) | reloc_val);
bfd_put (bits, input_bfd, insn, contents + rel->r_offset);
return bfd_reloc_ok;
@ -1594,19 +1564,8 @@ perform_relocation (const Elf_Internal_Rela *rel, asection *input_section,
case R_LARCH_SOP_POP_32_S_10_16:
case R_LARCH_SOP_POP_32_S_10_16_S2:
case R_LARCH_SOP_POP_32_S_5_20:
r = loongarch_pop (&opr1);
if (r != bfd_reloc_ok)
break;
r = loongarch_check_offset (rel, input_section);
if (r != bfd_reloc_ok)
break;
r = loongarch_reloc_rewrite_imm_insn (rel, input_section,
howto, input_bfd,
contents, opr1, true);
break;
case R_LARCH_SOP_POP_32_U_10_12:
case R_LARCH_SOP_POP_32_U:
r = loongarch_pop (&opr1);
if (r != bfd_reloc_ok)
break;
@ -1616,39 +1575,38 @@ perform_relocation (const Elf_Internal_Rela *rel, asection *input_section,
r = loongarch_reloc_rewrite_imm_insn (rel, input_section,
howto, input_bfd,
contents, opr1, false);
contents, (bfd_vma)opr1);
break;
case R_LARCH_SOP_POP_32_S_0_5_10_16_S2:
{
r = loongarch_pop (&opr1);
if (r != bfd_reloc_ok)
break;
if ((opr1 & 0x3) != 0)
{
r = bfd_reloc_overflow;
{
r = loongarch_pop (&opr1);
if (r != bfd_reloc_ok)
break;
}
uint32_t imm = opr1 >> howto->rightshift;
if ((imm & (~0xfffffU)) && ((imm & (~0xfffffU)) != (~0xfffffU)))
{
r = bfd_reloc_overflow;
if ((opr1 & 0x3) != 0)
{
r = bfd_reloc_overflow;
break;
}
uint32_t imm = opr1 >> howto->rightshift;
if ((imm & (~0xfffffU)) && ((imm & (~0xfffffU)) != (~0xfffffU)))
{
r = bfd_reloc_overflow;
break;
}
r = loongarch_check_offset (rel, input_section);
if (r != bfd_reloc_ok)
break;
}
r = loongarch_check_offset (rel, input_section);
if (r != bfd_reloc_ok)
insn1 = bfd_get (bits, input_bfd, contents + rel->r_offset);
insn1 = (insn1 & howto->src_mask)
| ((imm & 0xffffU) << 10)
| ((imm & 0x1f0000U) >> 16);
bfd_put (bits, input_bfd, insn1, contents + rel->r_offset);
break;
insn1 = bfd_get (bits, input_bfd, contents + rel->r_offset);
insn1 = ((insn1 & howto->src_mask)
| ((imm & 0xffffU) << 10)
| ((imm & 0x1f0000U) >> 16));
bfd_put (bits, input_bfd, insn1, contents + rel->r_offset);
break;
}
}
case R_LARCH_SOP_POP_32_S_0_10_10_16_S2:
{
@ -1681,21 +1639,6 @@ perform_relocation (const Elf_Internal_Rela *rel, asection *input_section,
break;
}
case R_LARCH_SOP_POP_32_U:
r = loongarch_pop (&opr1);
if (r != bfd_reloc_ok)
break;
if ((uint64_t)opr1 & ~(uint64_t) 0xffffffff)
r = bfd_reloc_overflow;
if (r != bfd_reloc_ok)
break;
r = loongarch_check_offset (rel, input_section);
if (r != bfd_reloc_ok)
break;
bfd_put (bits, input_bfd, opr1, contents + rel->r_offset);
break;
case R_LARCH_TLS_DTPREL32:
case R_LARCH_32:
case R_LARCH_TLS_DTPREL64:

File diff suppressed because it is too large Load Diff

View File

@ -29,3 +29,5 @@ loongarch_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code);
extern reloc_howto_type *
loongarch_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, const char *r_name);
bool loongarch_adjust_reloc_bitsfield (reloc_howto_type *howto, bfd_vma *fix_val);

View File

@ -25,6 +25,7 @@
#include "elf/loongarch.h"
#include "opcode/loongarch.h"
#include "obj-elf.h"
#include "bfd/elfxx-loongarch.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
@ -1068,13 +1069,29 @@ md_pcrel_from (fixS *fixP ATTRIBUTE_UNUSED)
return 0;
}
static void fix_reloc_insn (fixS *fixP, bfd_vma reloc_val, char *buf)
{
reloc_howto_type *howto;
insn_t insn;
howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
insn = bfd_getl32 (buf);
if (!loongarch_adjust_reloc_bitsfield(howto, &reloc_val))
as_warn_where (fixP->fx_file, fixP->fx_line, "Reloc overflow");
insn = (insn & (insn_t)howto->src_mask)
| ((insn & (~(insn_t)howto->dst_mask)) | reloc_val);
bfd_putl32 (insn, buf);
}
void
md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
{
static int64_t stack_top;
static int last_reloc_is_sop_push_pcrel_1 = 0;
int last_reloc_is_sop_push_pcrel = last_reloc_is_sop_push_pcrel_1;
insn_t insn;
last_reloc_is_sop_push_pcrel_1 = 0;
char *buf = fixP->fx_frag->fr_literal + fixP->fx_where;
@ -1083,17 +1100,17 @@ md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
case BFD_RELOC_LARCH_SOP_PUSH_TLS_TPREL:
case BFD_RELOC_LARCH_SOP_PUSH_TLS_GD:
case BFD_RELOC_LARCH_SOP_PUSH_TLS_GOT:
if (fixP->fx_addsy)
S_SET_THREAD_LOCAL (fixP->fx_addsy);
else
as_bad_where (fixP->fx_file, fixP->fx_line,
_("Relocation against a constant"));
break;
case BFD_RELOC_LARCH_SOP_PUSH_PCREL:
case BFD_RELOC_LARCH_SOP_PUSH_PLT_PCREL:
if (fixP->fx_addsy == NULL)
as_bad_where (fixP->fx_file, fixP->fx_line,
_("Relocation against a constant"));
if (fixP->fx_r_type == BFD_RELOC_LARCH_SOP_PUSH_TLS_TPREL
|| fixP->fx_r_type == BFD_RELOC_LARCH_SOP_PUSH_TLS_GD
|| fixP->fx_r_type == BFD_RELOC_LARCH_SOP_PUSH_TLS_GOT)
S_SET_THREAD_LOCAL (fixP->fx_addsy);
if (fixP->fx_r_type == BFD_RELOC_LARCH_SOP_PUSH_PCREL)
{
last_reloc_is_sop_push_pcrel_1 = 1;
@ -1106,111 +1123,18 @@ md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
break;
case BFD_RELOC_LARCH_SOP_POP_32_S_10_5:
if (!last_reloc_is_sop_push_pcrel)
break;
if ((stack_top & ~(uint64_t) 0xf) != 0x0
&& (stack_top & ~(uint64_t) 0xf) != ~(uint64_t) 0xf)
as_warn_where (fixP->fx_file, fixP->fx_line, "Reloc overflow");
insn = bfd_getl32 (buf);
insn = (insn & (~(uint32_t) 0x7c00)) | ((stack_top & 0x1f) << 10);
bfd_putl32 (insn, buf);
break;
case BFD_RELOC_LARCH_SOP_POP_32_U_10_12:
if (!last_reloc_is_sop_push_pcrel)
break;
if (stack_top & ~(uint64_t) 0xfff)
as_warn_where (fixP->fx_file, fixP->fx_line, "Reloc overflow");
insn = bfd_getl32 (buf);
insn = (insn & (~(uint32_t) 0x3ffc00)) | ((stack_top & 0xfff) << 10);
bfd_putl32 (insn, buf);
break;
case BFD_RELOC_LARCH_SOP_POP_32_S_10_12:
if (!last_reloc_is_sop_push_pcrel)
break;
if ((stack_top & ~(uint64_t) 0x7ff) != 0x0
&& (stack_top & ~(uint64_t) 0x7ff) != ~(uint64_t) 0x7ff)
as_warn_where (fixP->fx_file, fixP->fx_line, "Reloc overflow");
insn = bfd_getl32 (buf);
insn = (insn & (~(uint32_t) 0x3ffc00)) | ((stack_top & 0xfff) << 10);
bfd_putl32 (insn, buf);
break;
case BFD_RELOC_LARCH_SOP_POP_32_U_10_12:
case BFD_RELOC_LARCH_SOP_POP_32_S_10_16:
if (!last_reloc_is_sop_push_pcrel)
break;
if ((stack_top & ~(uint64_t) 0x7fff) != 0x0
&& (stack_top & ~(uint64_t) 0x7fff) != ~(uint64_t) 0x7fff)
as_warn_where (fixP->fx_file, fixP->fx_line, "Reloc overflow");
insn = bfd_getl32 (buf);
insn = (insn & 0xfc0003ff) | ((stack_top & 0xffff) << 10);
bfd_putl32 (insn, buf);
break;
case BFD_RELOC_LARCH_SOP_POP_32_S_10_16_S2:
if (!last_reloc_is_sop_push_pcrel)
break;
if ((stack_top & 0x3) != 0)
as_warn_where (fixP->fx_file, fixP->fx_line, "Reloc overflow");
stack_top >>= 2;
if ((stack_top & ~(uint64_t) 0x7fff) != 0x0
&& (stack_top & ~(uint64_t) 0x7fff) != ~(uint64_t) 0x7fff)
as_warn_where (fixP->fx_file, fixP->fx_line, "Reloc overflow");
insn = bfd_getl32 (buf);
insn = (insn & 0xfc0003ff) | ((stack_top & 0xffff) << 10);
bfd_putl32 (insn, buf);
break;
case BFD_RELOC_LARCH_SOP_POP_32_S_0_5_10_16_S2:
if (!last_reloc_is_sop_push_pcrel)
break;
if ((stack_top & 0x3) != 0)
break;
stack_top >>= 2;
if ((stack_top & ~(uint64_t) 0xfffff) != 0x0
&& (stack_top & ~(uint64_t) 0xfffff) != ~(uint64_t) 0xfffff)
as_warn_where (fixP->fx_file, fixP->fx_line, "Reloc overflow");
insn = bfd_getl32 (buf);
insn = ((insn & 0xfc0003e0)
| ((stack_top & 0xffff) << 10)
| ((stack_top & 0x1f0000) >> 16));
bfd_putl32 (insn, buf);
break;
case BFD_RELOC_LARCH_SOP_POP_32_S_5_20:
if (!last_reloc_is_sop_push_pcrel)
break;
if ((stack_top & ~(uint64_t) 0x7ffff) != 0x0
&& (stack_top & ~(uint64_t) 0x7ffff) != ~(uint64_t) 0x7ffff)
as_warn_where (fixP->fx_file, fixP->fx_line, "Reloc overflow");
insn = bfd_getl32 (buf);
insn = (insn & (~(uint32_t) 0x1ffffe0)) | ((stack_top & 0xfffff) << 5);
bfd_putl32 (insn, buf);
break;
case BFD_RELOC_LARCH_SOP_POP_32_U:
case BFD_RELOC_LARCH_SOP_POP_32_S_0_5_10_16_S2:
case BFD_RELOC_LARCH_SOP_POP_32_S_0_10_10_16_S2:
if (!last_reloc_is_sop_push_pcrel)
break;
if ((stack_top & 0x3) != 0)
as_warn_where (fixP->fx_file, fixP->fx_line, "Reloc overflow");
stack_top >>= 2;
if ((stack_top & ~(uint64_t) 0x1ffffff) != 0x0
&& (stack_top & ~(uint64_t) 0x1ffffff) != ~(uint64_t) 0x1ffffff)
as_warn_where (fixP->fx_file, fixP->fx_line, "Reloc overflow");
insn = bfd_getl32 (buf);
insn = ((insn & 0xfc000000)
| ((stack_top & 0xffff) << 10)
| ((stack_top & 0x3ff0000) >> 16));
bfd_putl32 (insn, buf);
break;
case BFD_RELOC_LARCH_SOP_POP_32_U:
if (!last_reloc_is_sop_push_pcrel)
break;
if (stack_top & ~(uint64_t) 0xffffffff)
as_warn_where (fixP->fx_file, fixP->fx_line, "Reloc overflow");
bfd_putl32 (stack_top, buf);
fix_reloc_insn (fixP, (bfd_vma)stack_top, buf);
break;
case BFD_RELOC_64: