kit

kit
git clone https://git.ryansepassi.com/git/kit.git
Log | Files | Refs | README

reloc.c (1922B)


      1 /* Arch-neutral relocation descriptors + the shared row-table lookup.
      2  *
      3  * This is the format-neutral obj-core half of the relocation descriptor:
      4  * the arch-independent data-word kinds whose width and (absent)
      5  * classification are the same on every target.  The arch-family kinds live
      6  * in src/arch/<arch>/reloc.c and are stitched in by reloc_desc() in the
      7  * link layer.  See doc/plan/RELOC.md (WS-B). */
      8 
      9 #include "obj/reloc.h"
     10 
     11 const RelocDesc* reloc_desc_row_find(const RelocDescRow* rows, u32 n,
     12                                      RelocKind k) {
     13   u32 i;
     14   for (i = 0; i < n; ++i)
     15     if (rows[i].kind == (u16)k) return &rows[i].desc;
     16   return NULL;
     17 }
     18 
     19 /* Arch-independent kinds: same width / byte encoding on every target.
     20  *
     21  * The COFF section-relative kinds (SECREL / SECTION / ADDR32NB) are
     22  * likewise arch-neutral data words and belong here; the COFF *instruction*
     23  * relocs (R_COFF_AARCH64_*) are AArch64 ISA encoders and live in that
     24  * arch's slice.
     25  *
     26  * R_PLT32 is neutral for width (4) but its IS_BRANCH classification is
     27  * arch-local — true on x86_64 / RISC-V, false on AArch64.  The branch flag
     28  * is therefore set in the x64 / riscv slices (which reloc_desc() consults
     29  * first); this row supplies both the width fallback and the correct
     30  * "not a branch" answer on AArch64. */
     31 static const RelocDescRow neutral_rows[] = {
     32     {R_ABS8, {1, 0}},          {R_ABS16, {2, 0}},
     33     {R_ABS32, {4, 0}},         {R_ABS64, {8, 0}},
     34     {R_PREL16, {2, 0}},        {R_TPOFF64, {8, 0}},
     35     {R_REL32, {4, 0}},         {R_REL64, {8, 0}},
     36     {R_PC32, {4, 0}},          {R_PC64, {8, 0}},
     37     {R_GOT32, {4, 0}},         {R_PLT32, {4, 0}},
     38     {R_COFF_SECREL, {4, RELOC_IS_SECREL}},
     39     {R_COFF_SECTION, {2, 0}},
     40     {R_COFF_ADDR32NB, {4, 0}},
     41 };
     42 
     43 const RelocDesc* reloc_desc_neutral(RelocKind k) {
     44   return reloc_desc_row_find(
     45       neutral_rows, (u32)(sizeof neutral_rows / sizeof neutral_rows[0]), k);
     46 }