Bug 3243 - Mips R_MIPS_16 relocation incorrectly handled
Summary: Mips R_MIPS_16 relocation incorrectly handled
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: 2.15
: P2 critical
Target Milestone: ---
Assignee: unassigned
Depends on:
Reported: 2006-09-22 04:07 UTC by Carl van Schaik
Modified: 2022-06-14 02:10 UTC (History)
1 user (show)

See Also:
Host: ia32-linux
Target: mips-linux
Last reconfirmed:


Note You need to log in before you can comment on or make changes to this bug.
Description Carl van Schaik 2006-09-22 04:07:54 UTC
R_MIPS_16 is incorrectly handled when at a half-word offset:

See below:

00000000 <structure>:
   8:   00000001        movf    zero,zero,$fcc0
   c:   00000130        tge     zero,zero,0x4
                        10: R_MIPS_32   kdebug_init()
                        14: R_MIPS_32   kdebug_entry(void*)
                        50: R_MIPS_32   _memory_descriptors_offset
                        56: R_MIPS_16   _memory_descriptors_size
  a4:   0000003f        0x3f

After linking, the values that should be at offset 0x56, is actually placed at
0x60 in the elf output.
Comment 1 cvs-commit@gcc.gnu.org 2022-06-14 00:35:34 UTC
The master branch has been updated by Alan Modra <amodra@sourceware.org>:


commit d712f2768ac5e71027657d85b921fc0e85d94bcd
Author: Alan Modra <amodra@gmail.com>
Date:   Sun Jun 12 16:29:05 2022 +0930

    MIPS should not be using BFD_RELOC_16 for its R_MIPS_16 relocation,
    since R_MIPS_16 specifies a 16-bit field in a 32-bit word.
    BFD_RELOC_16, emitted by generic code to handle fixups on 16-bit data
    directives, expects fixups to operate on the whole of a 16-bit word.
    This patch corrects the problem by using BFD_RELOC_MIPS_16, a new bfd
    reloc that is used to generate R_MIPS_16.  BFD_RELOC_16 is handled in
    md_apply_fix for cases where the fixup can be applied at assembly
    time.  Like BFD_RELOC_8, BFD_RELOC_16 now has no corresponding object
    file relocation, and thus .half, .hword, .short and .dc.w must be
    resolved at assembly time.  BFD_RELOC_MIPS_REL16 is removed by this
    patch since it isn't used.
            PR 3243
            PR 26542
            * reloc.c (BFD_RELOC_MIPS_16): Rename from BFD_RELOC_MIPS_REL16.
            * elf32-mips.c (mips_reloc_map): Map BFD_RELOC_MIPS_16 to R_MIPS_16.
            * elf64-mips.c (mips_reloc_map): Likewise, delete BFD_RELOC_MIPS_REL16.
            * elfn32-mips.c (mips_reloc_map): Likewise.
            * libbfd.h: Regenerate.
            * bfd-in2.h: Regenerate.
            * config/tc-mips.c (append_insn): Handle BFD_RELOC_MIPS_16.
            (macro_build): Likewise.
            (mips_percent_op <%half>): Generate BFD_RELOC_MIPS_16.
            (md_apply_fix): Handle BFD_RELOC_16 and BFD_RELOC_MIPS_16 when fx_done.
            * testsuite/ld-mips-elf/reloc-local-overflow.d,
            * testsuite/ld-mips-elf/reloc-local-overflow.s: Rewrite.
Comment 2 Alan Modra 2022-06-14 02:10:51 UTC
Fixed, you now can't use R_MIPS_16 on data.