• Masahiro Yamada's avatar
    modpost: detect section mismatch for R_ARM_THM_{MOVW_ABS_NC,MOVT_ABS} · cd1824fb
    Masahiro Yamada authored
    When CONFIG_THUMB2_KERNEL is enabled, modpost fails to detect some
    types of section mismatches.
    
      [test code]
    
        #include <linux/init.h>
    
        int __initdata foo;
        int get_foo(void) { return foo; }
    
    It is apparently a bad reference, but modpost does not report anything.
    
    The test code above produces the following relocations.
    
      Relocation section '.rel.text' at offset 0x1e8 contains 2 entries:
       Offset     Info    Type            Sym.Value  Sym. Name
      00000000  0000052f R_ARM_THM_MOVW_AB 00000000   .LANCHOR0
      00000004  00000530 R_ARM_THM_MOVT_AB 00000000   .LANCHOR0
    
    Currently, R_ARM_THM_MOVW_ABS_NC and R_ARM_THM_MOVT_ABS are just skipped.
    
    Add code to handle them. I checked arch/arm/kernel/module.c to learn
    how the offset is encoded in the instruction.
    
    One more thing to note for Thumb instructions - the st_value is an odd
    value, so you need to mask the bit 0 to get the offset. Otherwise, you
    will get an off-by-one error in the nearest symbol look-up.
    
    It is documented in "ELF for the ARM Architecture" [1]:
    
      In addition to the normal rules for symbol values the following rules
      shall also apply to symbols of type STT_FUNC:
    
       * If the symbol addresses an Arm instruction, its value is the
         address of the instruction (in a relocatable object, the offset
         of the instruction from the start of the section containing it).
    
       * If the symbol addresses a Thumb instruction, its value is the
         address of the instruction with bit zero set (in a relocatable
         object, the section offset with bit zero set).
    
       * For the purposes of relocation the value used shall be the address
         of the instruction (st_value & ~1).
    
    [1]: https://github.com/ARM-software/abi-aa/blob/main/aaelf32/aaelf32.rstSigned-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
    cd1824fb
modpost.c 55.9 KB