Commit Graph

655 Commits

Author SHA1 Message Date
Waiman Long 3342973efe x86/bugs: Rename CONFIG_RETPOLINE => CONFIG_MITIGATION_RETPOLINE
JIRA: https://issues.redhat.com/browse/RHEL-31230
Conflicts:
 1) The net/netfilter/Makefile hunk is dropped due to missing
    nft_ct_fast.c file first intruduced by commit d9e789147605
    ("netfilter: nf_tables: avoid retpoline overhead for some ct
    expression calls").
 2) A merge conflict in the tools/objtool/check.c hunk due to missing
    upstream commit 9bb2ec608a20 ("objtool: Update Retpoline validation").
 3) First hunk of net/netfilter/nf_tables_core.c is dropped and a merge
    conflict in the second hunk due to missing upstream commit
    d8d760627855 ("netfilter: nf_tables: add static key to skip retpoline
    workarounds").
 4) The net/netfilter/nft_ct.c hunks are dropped due to missing upstream
    commit d9e789147605 ("netfilter: nf_tables: avoid retpoline overhead
    for some ct expression calls").

commit aefb2f2e619b6c334bcb31de830aa00ba0b11129
Author: Breno Leitao <leitao@debian.org>
Date:   Tue, 21 Nov 2023 08:07:32 -0800

    x86/bugs: Rename CONFIG_RETPOLINE            => CONFIG_MITIGATION_RETPOLINE

    Step 5/10 of the namespace unification of CPU mitigations related Kconfig options.

    [ mingo: Converted a few more uses in comments/messages as well. ]

    Suggested-by: Josh Poimboeuf <jpoimboe@kernel.org>
    Signed-off-by: Breno Leitao <leitao@debian.org>
    Signed-off-by: Ingo Molnar <mingo@kernel.org>
    Reviewed-by: Ariel Miculas <amiculas@cisco.com>
    Acked-by: Josh Poimboeuf <jpoimboe@kernel.org>
    Cc: Linus Torvalds <torvalds@linux-foundation.org>
    Link: https://lore.kernel.org/r/20231121160740.1249350-6-leitao@debian.org

Signed-off-by: Waiman Long <longman@redhat.com>
2024-07-26 14:33:35 -04:00
Waiman Long 6266ac74d7 kbuild: do not create *.prelink.o for Clang LTO or IBT
JIRA: https://issues.redhat.com/browse/RHEL-31230
Conflicts:
 1) There are two merge conflicts in scripts/Makefile.build. The first
    hunk is dropped as it is overwritten by RHEL backport of upstream
    commit b42d23065024 ("kbuild: factor out the common objtool
    arguments") earlier. The second conflict is due to the presence
    of a later upstream commit c6031b1dbbbf ("kbuild: make *.mod rule
    robust against too long argument error").
 2) A merge conflict in scripts/Makefile.lib due to the presence of
    a later upstream commit b42d23065024 ("kbuild: factor out the common
    objtool arguments").
 3) A merge conflict in scripts/mod/modpost.c and the hunk is dropped
    as the relevant code had already been deleted by RHEL backport
    of upstream commit 090feaafca ("modpost: simplify mod->name
    allocation").

commit c25e1c55822f9b3b53ccbf88b85644317a525752
Author: Masahiro Yamada <masahiroy@kernel.org>
Date:   Fri, 27 May 2022 19:01:49 +0900

    kbuild: do not create *.prelink.o for Clang LTO or IBT

    When CONFIG_LTO_CLANG=y, additional intermediate *.prelink.o is created
    for each module. Also, objtool is postponed until LLVM IR is converted
    to ELF.

    CONFIG_X86_KERNEL_IBT works in a similar way to postpone objtool until
    objects are merged together.

    This commit stops generating *.prelink.o, so the build flow will look
    similar with/without LTO.

    The following figures show how the LTO build currently works, and
    how this commit is changing it.

    Current build flow
    ==================

     [1] single-object module

                                          $(LD)
               $(CC)                     +objtool              $(LD)
        foo.c --------------------> foo.o -----> foo.prelink.o -----> foo.ko
                                  (LLVM IR)          (ELF)       |    (ELF)
                                                                 |
                                                     foo.mod.o --/
                                                     (LLVM IR)

     [2] multi-object module
                                          $(LD)
               $(CC)         $(AR)       +objtool               $(LD)
        foo1.c -----> foo1.o -----> foo.o -----> foo.prelink.o -----> foo.ko
                               |  (archive)          (ELF)       |    (ELF)
        foo2.c -----> foo2.o --/                                 |
                     (LLVM IR)                       foo.mod.o --/
                                                     (LLVM IR)

      One confusion is that foo.o in multi-object module is an archive
      despite of its suffix.

    New build flow
    ==============

     [1] single-object module

      Since there is only one object, there is no need to keep the LLVM IR.
      Use $(CC)+$(LD) to generate an ELF object in one build rule. When LTO
      is disabled, $(LD) is unneeded because $(CC) produces an ELF object.

                   $(CC)+$(LD)+objtool              $(LD)
        foo.c ----------------------------> foo.o ---------> foo.ko
                                            (ELF)     |      (ELF)
                                                      |
                                          foo.mod.o --/
                                          (LLVM IR)

     [2] multi-object module

      Previously, $(AR) was used to combine LLVM IR files into an archive,
      but there was no technical reason to do so. Use $(LD) to merge them
      into a single ELF object.

                                   $(LD)
                 $(CC)            +objtool          $(LD)
        foo1.c ---------> foo1.o ---------> foo.o ---------> foo.ko
                                     |      (ELF)     |      (ELF)
        foo2.c ---------> foo2.o ----/                |
                         (LLVM IR)        foo.mod.o --/
                                          (LLVM IR)

    Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
    Reviewed-by: Nicolas Schier <nicolas@fjasle.eu>
    Tested-by: Nathan Chancellor <nathan@kernel.org>
    Reviewed-by: Sami Tolvanen <samitolvanen@google.com>
    Tested-by: Sedat Dilek <sedat.dilek@gmail.com> # LLVM-14 (x86-64)
    Acked-by: Josh Poimboeuf <jpoimboe@kernel.org>

Signed-off-by: Waiman Long <longman@redhat.com>
2024-07-26 14:33:35 -04:00
Donald Dutile 8604122815 modpost: do not make find_tosym() return NULL
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit 1102f9f85bf66b1a7bd6a40afb40efbbe05dfc05
Author: Masahiro Yamada <masahiroy@kernel.org>
Date:   Sat Mar 23 20:45:11 2024 +0900

    modpost: do not make find_tosym() return NULL

    As mentioned in commit 397586506c3d ("modpost: Add '.ltext' and
    '.ltext.*' to TEXT_SECTIONS"), modpost can result in a segmentation
    fault due to a NULL pointer dereference in default_mismatch_handler().

    find_tosym() can return the original symbol pointer instead of NULL
    if a better one is not found.

    This fixes the reported segmentation fault.

    Fixes: a23e7584ecf3 ("modpost: unify 'sym' and 'to' in default_mismatch_handler()")
    Reported-by: Nathan Chancellor <nathan@kernel.org>
    Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:29 -04:00
Donald Dutile 5c039ddd46 modpost: Optimize symbol search from linear to binary search
JIRA: https://issues.redhat.com/browse/RHEL-28063

Conflicts: Slight source diff due to omitted commits from upstream.

commit 4074532758c5c367d3fcb8d124150824a254659d
Author: Jack Brennen <jbrennen@google.com>
Date:   Tue Sep 26 08:40:44 2023 -0400

    modpost: Optimize symbol search from linear to binary search

    Modify modpost to use binary search for converting addresses back
    into symbol references.  Previously it used linear search.

    This change saves a few seconds of wall time for defconfig builds,
    but can save several minutes on allyesconfigs.

    Before:
    $ make LLVM=1 -j128 allyesconfig vmlinux -s KCFLAGS="-Wno-error"
    $ time scripts/mod/modpost -M -m -a -N -o vmlinux.symvers vmlinux.o
    198.38user 1.27system 3:19.71elapsed

    After:
    $ make LLVM=1 -j128 allyesconfig vmlinux -s KCFLAGS="-Wno-error"
    $ time scripts/mod/modpost -M -m -a -N -o vmlinux.symvers vmlinux.o
    11.91user 0.85system 0:12.78elapsed

    Signed-off-by: Jack Brennen <jbrennen@google.com>
    Tested-by: Nick Desaulniers <ndesaulniers@google.com>
    Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:29 -04:00
Donald Dutile cf7679e379 modpost, kallsyms: Treat add '$'-prefixed symbols as mapping symbols
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit ff09f6fd297293175eaa0ed492495e36b3eb1a8e
Author: Palmer Dabbelt <palmer@rivosinc.com>
Date:   Fri Jul 21 08:01:48 2023 -0700

    modpost, kallsyms: Treat add '$'-prefixed symbols as mapping symbols

    Trying to restrict the '$'-prefix change to RISC-V caused some fallout,
    so let's just treat all those symbols as special.

    Fixes: c05780ef3c190 ("module: Ignore RISC-V mapping symbols too")
    Link: https://lore.kernel.org/all/20230712015747.77263-1-wangkefeng.wang@huawei.com/
    Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
    Reviewed-by: Masahiro Yamada <masahiroy@kernel.org>
    Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:28 -04:00
Donald Dutile 346c4d39ff module: Ignore RISC-V mapping symbols too
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit c05780ef3c190c2dafbf0be8e65d4f01103ad577
Author: Palmer Dabbelt <palmer@rivosinc.com>
Date:   Fri Jul 7 09:00:51 2023 -0700

    module: Ignore RISC-V mapping symbols too

    RISC-V has an extended form of mapping symbols that we use to encode
    the ISA when it changes in the middle of an ELF.  This trips up modpost
    as a build failure, I haven't yet verified it yet but I believe the
    kallsyms difference should result in stacks looking sane again.

    Reported-by: Randy Dunlap <rdunlap@infradead.org>
    Closes: https://lore.kernel.org/all/9d9e2902-5489-4bf0-d9cb-556c8e5d71c2@infradead.org/
    Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
    Reviewed-by: Randy Dunlap <rdunlap@infradead.org>
    Tested-by: Randy Dunlap <rdunlap@infradead.org> # build-tested
    Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:28 -04:00
Donald Dutile e0c0616853 modpost: check static EXPORT_SYMBOL* by modpost again
JIRA: https://issues.redhat.com/browse/RHEL-28063

Conflicts:
	- Only backport the scripts/mod/modpost.c portion as cs-9 never
	  backported the aborted check_local_export approach.

commit 6d62b1c46b1e6e1686a0cf6617c96c80d4ab5cd5
Author: Masahiro Yamada <masahiroy@kernel.org>
Date:   Mon Jun 12 00:50:54 2023 +0900

    modpost: check static EXPORT_SYMBOL* by modpost again

    Commit 31cb50b5590f ("kbuild: check static EXPORT_SYMBOL* by script
    instead of modpost") moved the static EXPORT_SYMBOL* check from the
    mostpost to a shell script because I thought it must be checked per
    compilation unit to avoid false negatives.

    I came up with an idea to do this in modpost, against combined ELF
    files. The relocation entries in ELF will find the correct exported
    symbol even if there exist symbols with the same name in different
    compilation units.

    Again, the same sample code.

      Makefile:

        obj-y += foo1.o foo2.o

      foo1.c:

        #include <linux/export.h>
        static void foo(void) {}
        EXPORT_SYMBOL(foo);

      foo2.c:

        void foo(void) {}

    Then, modpost can catch it correctly.

        MODPOST Module.symvers
      ERROR: modpost: vmlinux: local symbol 'foo' was exported

    Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
    Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:28 -04:00
Donald Dutile efc5790fc4 kbuild: generate KSYMTAB entries by modpost
JIRA: https://issues.redhat.com/browse/RHEL-28063

Conflicts:
 (1) Dropped patches for check-local-export; that script was temporarily
     replacing modpost, but abanadoned and modpost resumed with simpler
     addition and-or bug fixes, so skip it here.
 (2) Drop ia64 patches since RHEL doesn't support ia64, and didn't apply cleanly.
 (3) Made cmd_gensymversions genksyms exec same as cmd_gensymtypes;
     cmd_gensymversions appears to be a rhel-ism, and it has no callers/users
     under script hierarchy.

commit ddb5cdbafaaad6b99d7007ae1740403124502d03
Author: Masahiro Yamada <masahiroy@kernel.org>
Date:   Mon Jun 12 00:50:52 2023 +0900

    kbuild: generate KSYMTAB entries by modpost

    Commit 7b4537199a4a ("kbuild: link symbol CRCs at final link, removing
    CONFIG_MODULE_REL_CRCS") made modpost output CRCs in the same way
    whether the EXPORT_SYMBOL() is placed in *.c or *.S.

    For further cleanups, this commit applies a similar approach to the
    entire data structure of EXPORT_SYMBOL().

    The EXPORT_SYMBOL() compilation is split into two stages.

    When a source file is compiled, EXPORT_SYMBOL() will be converted into
    a dummy symbol in the .export_symbol section.

    For example,

        EXPORT_SYMBOL(foo);
        EXPORT_SYMBOL_NS_GPL(bar, BAR_NAMESPACE);

    will be encoded into the following assembly code:

        .section ".export_symbol","a"
        __export_symbol_foo:
                .asciz ""                      /* license */
                .asciz ""                      /* name space */
                .balign 8
                .quad foo                      /* symbol reference */
        .previous

        .section ".export_symbol","a"
        __export_symbol_bar:
                .asciz "GPL"                   /* license */
                .asciz "BAR_NAMESPACE"         /* name space */
                .balign 8
                .quad bar                      /* symbol reference */
        .previous

    They are mere markers to tell modpost the name, license, and namespace
    of the symbols. They will be dropped from the final vmlinux and modules
    because the *(.export_symbol) will go into /DISCARD/ in the linker script.

    Then, modpost extracts all the information about EXPORT_SYMBOL() from the
    .export_symbol section, and generates the final C code:

        KSYMTAB_FUNC(foo, "", "");
        KSYMTAB_FUNC(bar, "_gpl", "BAR_NAMESPACE");

    KSYMTAB_FUNC() (or KSYMTAB_DATA() if it is data) is expanded to struct
    kernel_symbol that will be linked to the vmlinux or a module.

    With this change, EXPORT_SYMBOL() works in the same way for *.c and *.S
    files, providing the following benefits.

    [1] Deprecate EXPORT_DATA_SYMBOL()

    In the old days, EXPORT_SYMBOL() was only available in C files. To export
    a symbol in *.S, EXPORT_SYMBOL() was placed in a separate *.c file.
    arch/arm/kernel/armksyms.c is one example written in the classic manner.

    Commit 22823ab419 ("EXPORT_SYMBOL() for asm") removed this limitation.
    Since then, EXPORT_SYMBOL() can be placed close to the symbol definition
    in *.S files. It was a nice improvement.

    However, as that commit mentioned, you need to use EXPORT_DATA_SYMBOL()
    for data objects on some architectures.

    In the new approach, modpost checks symbol's type (STT_FUNC or not),
    and outputs KSYMTAB_FUNC() or KSYMTAB_DATA() accordingly.

    There are only two users of EXPORT_DATA_SYMBOL:

      EXPORT_DATA_SYMBOL_GPL(empty_zero_page)    (arch/ia64/kernel/head.S)
      EXPORT_DATA_SYMBOL(ia64_ivt)               (arch/ia64/kernel/ivt.S)

    They are transformed as follows and output into .vmlinux.export.c

      KSYMTAB_DATA(empty_zero_page, "_gpl", "");
      KSYMTAB_DATA(ia64_ivt, "", "");

    The other EXPORT_SYMBOL users in ia64 assembly are output as
    KSYMTAB_FUNC().

    EXPORT_DATA_SYMBOL() is now deprecated.

    [2] merge <linux/export.h> and <asm-generic/export.h>

    There are two similar header implementations:

      include/linux/export.h        for .c files
      include/asm-generic/export.h  for .S files

    Ideally, the functionality should be consistent between them, but they
    tend to diverge.

    Commit 8651ec01da ("module: add support for symbol namespaces.") did
    not support the namespace for *.S files.

    This commit shifts the essential implementation part to C, which supports
    EXPORT_SYMBOL_NS() for *.S files.

    <asm/export.h> and <asm-generic/export.h> will remain as a wrapper of
    <linux/export.h> for a while.

    They will be removed after #include <asm/export.h> directives are all
    replaced with #include <linux/export.h>.

    [3] Implement CONFIG_TRIM_UNUSED_KSYMS in one-pass algorithm (by a later commit)

    When CONFIG_TRIM_UNUSED_KSYMS is enabled, Kbuild recursively traverses
    the directory tree to determine which EXPORT_SYMBOL to trim. If an
    EXPORT_SYMBOL turns out to be unused by anyone, Kbuild begins the
    second traverse, where some source files are recompiled with their
    EXPORT_SYMBOL() tuned into a no-op.

    We can do this better now; modpost can selectively emit KSYMTAB entries
    that are really used by modules.

    Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
    Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:28 -04:00
Donald Dutile e9773a7d97 modpost: pass struct module pointer to check_section_mismatch()
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit 94d6cb68124b7a63f24fcc345795ba5f9a27e694
Author: Masahiro Yamada <masahiroy@kernel.org>
Date:   Mon Jun 12 00:50:51 2023 +0900

    modpost: pass struct module pointer to check_section_mismatch()

    The next commit will use it.

    Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
    Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:28 -04:00
Donald Dutile 66ca54c248 modpost: fix off by one in is_executable_section()
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit 3a3f1e573a105328a2cca45a7cfbebabbf5e3192
Author: Dan Carpenter <dan.carpenter@linaro.org>
Date:   Thu Jun 8 11:23:40 2023 +0300

    modpost: fix off by one in is_executable_section()

    The > comparison should be >= to prevent an out of bounds array
    access.

    Fixes: 52dc0595d5 ("modpost: handle relocations mismatch in __ex_table.")
    Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org>
    Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:28 -04:00
Donald Dutile b360f76b6a modpost: Include '.text.*' in TEXT_SECTIONS
JIRA: https://issues.redhat.com/browse/RHEL-28063

Conflicts: Source diff since upstream commit 1e688dd2a3d6759d
           "powerpc/bug: Provide better flexibility to WARN_ON/__.."
	   not backported to RHEL9.

commit 19331e84c3873256537d446afec1f6c507f8c4ef
Author: Nathan Chancellor <nathan@kernel.org>
Date:   Tue Dec 13 11:35:29 2022 -0700

    modpost: Include '.text.*' in TEXT_SECTIONS

    Commit 6c730bfc89 ("modpost: handle -ffunction-sections") added
    ".text.*" to the OTHER_TEXT_SECTIONS macro to fix certain section
    mismatch warnings. Unfortunately, this makes it impossible for modpost
    to warn about section mismatches with LTO, which implies
    '-ffunction-sections', as all functions are put in their own
    '.text.<func_name>' sections, which may still reference functions in
    sections they are not supposed to, such as __init.

    Fix this by moving ".text.*" into TEXT_SECTIONS, so that configurations
    with '-ffunction-sections' will see warnings about mismatched sections.

    Link: https://lore.kernel.org/Y39kI3MOtVI5BAnV@google.com/
    Reported-by: Vincent Donnefort <vdonnefort@google.com>
    Reviewed-and-tested-by: Alexander Lobakin <alexandr.lobakin@intel.com>
    Reviewed-by: Sami Tolvanen <samitolvanen@google.com>
    Tested-by: Vincent Donnefort <vdonnefort@google.com>
    Signed-off-by: Nathan Chancellor <nathan@kernel.org>
    Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:28 -04:00
Donald Dutile 3ead9b7d29 modpost: detect section mismatch for R_ARM_REL32
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit 2cb749466d179e3ccfe83eb8a52dc002d07b08af
Author: Masahiro Yamada <masahiroy@kernel.org>
Date:   Thu Jun 1 21:10:01 2023 +0900

    modpost: detect section mismatch for R_ARM_REL32

    For ARM, modpost fails to detect some types of section mismatches.

      [test code]

        .section .init.data,"aw"
        bar:
                .long 0

        .section .data,"aw"
        .globl foo
        foo:
                .long bar - .

    It is apparently a bad reference, but modpost does not report anything.

    The test code above produces the following relocations.

      Relocation section '.rel.data' at offset 0xe8 contains 1 entry:
       Offset     Info    Type            Sym.Value  Sym. Name
      00000000  00000403 R_ARM_REL32       00000000   .init.data

    Currently, R_ARM_REL32 is just skipped.

    Handle it like R_ARM_ABS32.

    Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:27 -04:00
Donald Dutile b622699f48 modpost: fix section_mismatch message for R_ARM_THM_{CALL,JUMP24,JUMP19}
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit 3310bae805250aec227eb056e8e61a246678f28a
Author: Masahiro Yamada <masahiroy@kernel.org>
Date:   Thu Jun 1 21:10:00 2023 +0900

    modpost: fix section_mismatch message for R_ARM_THM_{CALL,JUMP24,JUMP19}

    addend_arm_rel() processes R_ARM_THM_CALL, R_ARM_THM_JUMP24,
    R_ARM_THM_JUMP19 in a wrong way.

    Here, test code.

    [test code for R_ARM_THM_JUMP24]

      .section .init.text,"ax"
      bar:
              bx      lr

      .section .text,"ax"
      .globl foo
      foo:
              b       bar

    [test code for R_ARM_THM_CALL]

      .section .init.text,"ax"
      bar:
              bx      lr

      .section .text,"ax"
      .globl foo
      foo:
              push    {lr}
              bl      bar
              pop     {pc}

    If you compile it with CONFIG_THUMB2_KERNEL=y, modpost will show the
    symbol name, (unknown).

      WARNING: modpost: vmlinux.o: section mismatch in reference: foo (section: .text) -> (unknown) (section: .init.text)

    (You need to use GNU linker instead of LLD to reproduce it.)

    Fix the code to make modpost show the correct symbol name. I checked
    arch/arm/kernel/module.c to learn the encoding of R_ARM_THM_CALL and
    R_ARM_THM_JUMP24. The module does not support R_ARM_THM_JUMP19, but
    I checked its encoding in ARM ARM.

    The '+4' is the compensation for pc-relative instruction. It is
    documented in "ELF for the Arm Architecture" [1].

      "If the relocation is pc-relative then compensation for the PC bias
      (the PC value is 8 bytes ahead of the executing instruction in Arm
      state and 4 bytes in Thumb state) must be encoded in the relocation
      by the object producer."

    [1]: https://github.com/ARM-software/abi-aa/blob/main/aaelf32/aaelf32.rst

    Fixes: c9698e5cd6 ("ARM: 7964/1: Detect section mismatches in thumb relocations")
    Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:27 -04:00
Donald Dutile 8a94398b32 modpost: detect section mismatch for R_ARM_THM_{MOVW_ABS_NC,MOVT_ABS}
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit cd1824fb7a377882497e8b87a6f3a9ec19be3623
Author: Masahiro Yamada <masahiroy@kernel.org>
Date:   Thu Jun 1 21:09:59 2023 +0900

    modpost: detect section mismatch for R_ARM_THM_{MOVW_ABS_NC,MOVT_ABS}

    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.rst

    Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:27 -04:00
Donald Dutile 2c38776bed modpost: refactor find_fromsym() and find_tosym()
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit b1a9651d48b42f3eddf095123c09f93e4df23060
Author: Masahiro Yamada <masahiroy@kernel.org>
Date:   Thu Jun 1 21:09:58 2023 +0900

    modpost: refactor find_fromsym() and find_tosym()

    find_fromsym() and find_tosym() are similar - both of them iterate
    in the .symtab section and return the nearest symbol.

    The difference between them is that find_tosym() allows a negative
    distance, but the distance must be less than 20.

    Factor out the common part into find_nearest_sym().

    Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
    Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:27 -04:00
Donald Dutile e9eb0e2944 modpost: detect section mismatch for R_ARM_{MOVW_ABS_NC,MOVT_ABS}
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit 12ca2c67d742d390c0aa1f8c1cfc49469df55ddf
Author: Masahiro Yamada <masahiroy@kernel.org>
Date:   Thu Jun 1 21:09:57 2023 +0900

    modpost: detect section mismatch for R_ARM_{MOVW_ABS_NC,MOVT_ABS}

    For ARM defconfig (i.e. multi_v7_defconfig), 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 0x200 contains 2 entries:
       Offset     Info    Type            Sym.Value  Sym. Name
      00000000  0000062b R_ARM_MOVW_ABS_NC 00000000   .LANCHOR0
      00000004  0000062c R_ARM_MOVT_ABS    00000000   .LANCHOR0

    Currently, R_ARM_MOVW_ABS_NC and R_ARM_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.

    The referenced symbol in relocation might be a local anchor.
    If is_valid_name() returns false, let's search for a better symbol name.

    Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:27 -04:00
Donald Dutile b893fedab2 modpost: fix section mismatch message for R_ARM_{PC24,CALL,JUMP24}
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit 56a24b8ce6a7f9c4a21b2276a8644f6f3d8fc14d
Author: Masahiro Yamada <masahiroy@kernel.org>
Date:   Thu Jun 1 21:09:56 2023 +0900

    modpost: fix section mismatch message for R_ARM_{PC24,CALL,JUMP24}

    addend_arm_rel() processes R_ARM_PC24, R_ARM_CALL, R_ARM_JUMP24 in a
    wrong way.

    Here, test code.

    [test code for R_ARM_JUMP24]

      .section .init.text,"ax"
      bar:
              bx      lr

      .section .text,"ax"
      .globl foo
      foo:
              b       bar

    [test code for R_ARM_CALL]

      .section .init.text,"ax"
      bar:
              bx      lr

      .section .text,"ax"
      .globl foo
      foo:
              push    {lr}
              bl      bar
              pop     {pc}

    If you compile it with ARM multi_v7_defconfig, modpost will show the
    symbol name, (unknown).

      WARNING: modpost: vmlinux.o: section mismatch in reference: foo (section: .text) -> (unknown) (section: .init.text)

    (You need to use GNU linker instead of LLD to reproduce it.)

    Fix the code to make modpost show the correct symbol name.

    I imported (with adjustment) sign_extend32() from include/linux/bitops.h.

    The '+8' is the compensation for pc-relative instruction. It is
    documented in "ELF for the Arm Architecture" [1].

      "If the relocation is pc-relative then compensation for the PC bias
      (the PC value is 8 bytes ahead of the executing instruction in Arm
      state and 4 bytes in Thumb state) must be encoded in the relocation
      by the object producer."

    [1]: https://github.com/ARM-software/abi-aa/blob/main/aaelf32/aaelf32.rst

    Fixes: 56a974fa2d ("kbuild: make better section mismatch reports on arm")
    Fixes: 6e2e340b59 ("ARM: 7324/1: modpost: Fix section warnings for ARM for many compilers")
    Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:27 -04:00
Donald Dutile db60df365c modpost: fix section mismatch message for R_ARM_ABS32
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit b7c63520f6703a25eebb4f8138fed764fcae1c6f
Author: Masahiro Yamada <masahiroy@kernel.org>
Date:   Thu Jun 1 21:09:55 2023 +0900

    modpost: fix section mismatch message for R_ARM_ABS32

    addend_arm_rel() processes R_ARM_ABS32 in a wrong way.

    Here, test code.

      [test code 1]

        #include <linux/init.h>

        int __initdata foo;
        int get_foo(void) { return foo; }

    If you compile it with ARM versatile_defconfig, modpost will show the
    symbol name, (unknown).

      WARNING: modpost: vmlinux.o: section mismatch in reference: get_foo (section: .text) -> (unknown) (section: .init.data)

    (You need to use GNU linker instead of LLD to reproduce it.)

    If you compile it for other architectures, modpost will show the correct
    symbol name.

      WARNING: modpost: vmlinux.o: section mismatch in reference: get_foo (section: .text) -> foo (section: .init.data)

    For R_ARM_ABS32, addend_arm_rel() sets r->r_addend to a wrong value.

    I just mimicked the code in arch/arm/kernel/module.c.

    However, there is more difficulty for ARM.

    Here, test code.

      [test code 2]

        #include <linux/init.h>

        int __initdata foo;
        int get_foo(void) { return foo; }

        int __initdata bar;
        int get_bar(void) { return bar; }

    With this commit applied, modpost will show the following messages
    for ARM versatile_defconfig:

      WARNING: modpost: vmlinux.o: section mismatch in reference: get_foo (section: .text) -> foo (section: .init.data)
      WARNING: modpost: vmlinux.o: section mismatch in reference: get_bar (section: .text) -> foo (section: .init.data)

    The reference from 'get_bar' to 'foo' seems wrong.

    I have no solution for this because it is true in assembly level.

    In the following output, relocation at 0x1c is no longer associated
    with 'bar'. The two relocation entries point to the same symbol, and
    the offset to 'bar' is encoded in the instruction 'r0, [r3, #4]'.

      Disassembly of section .text:

      00000000 <get_foo>:
         0: e59f3004          ldr     r3, [pc, #4]   @ c <get_foo+0xc>
         4: e5930000          ldr     r0, [r3]
         8: e12fff1e          bx      lr
         c: 00000000          .word   0x00000000

      00000010 <get_bar>:
        10: e59f3004          ldr     r3, [pc, #4]   @ 1c <get_bar+0xc>
        14: e5930004          ldr     r0, [r3, #4]
        18: e12fff1e          bx      lr
        1c: 00000000          .word   0x00000000

      Relocation section '.rel.text' at offset 0x244 contains 2 entries:
       Offset     Info    Type            Sym.Value  Sym. Name
      0000000c  00000c02 R_ARM_ABS32       00000000   .init.data
      0000001c  00000c02 R_ARM_ABS32       00000000   .init.data

    When find_elf_symbol() gets into a situation where relsym->st_name is
    zero, there is no guarantee to get the symbol name as written in C.

    I am keeping the current logic because it is useful in many architectures,
    but the symbol name is not always correct depending on the optimization.
    I left some comments in find_tosym().

    Fixes: 56a974fa2d ("kbuild: make better section mismatch reports on arm")
    Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:27 -04:00
Donald Dutile 1470d26ffa modpost: remove *_sections[] arrays
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit 1df380ff3018521bd1b129dff60984b61ade8cee
Author: Masahiro Yamada <masahiroy@kernel.org>
Date:   Mon May 22 01:04:23 2023 +0900

    modpost: remove *_sections[] arrays

    Use PATTERNS() macros to remove unneeded array definitions.

    Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:27 -04:00
Donald Dutile 38d8501e77 modpost: merge bad_tosec=ALL_EXIT_SECTIONS entries in sectioncheck table
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit abc23979ac90396c5a5dff03dcea198b5bd0c50d
Author: Masahiro Yamada <masahiroy@kernel.org>
Date:   Mon May 22 01:04:22 2023 +0900

    modpost: merge bad_tosec=ALL_EXIT_SECTIONS entries in sectioncheck table

    There is no distinction between TEXT_TO_ANY_EXIT and DATA_TO_ANY_EXIT.
    Just merge them.

    Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
    Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:27 -04:00
Donald Dutile 7273ed5fb7 modpost: merge fromsec=DATA_SECTIONS entries in sectioncheck table
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit d4323e83505247d2aca1e2488f69da9aab8ad03f
Author: Masahiro Yamada <masahiroy@kernel.org>
Date:   Mon May 22 01:04:21 2023 +0900

    modpost: merge fromsec=DATA_SECTIONS entries in sectioncheck table

    You can merge these entries.

    Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
    Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:27 -04:00
Donald Dutile bffd4611db modpost: remove is_shndx_special() check from section_rel(a)
JIRA: https://issues.redhat.com/browse/RHEL-28063

Conflicts: Minor source diffs but source changes the same.

commit a9bb3e5d57293773d7f925dd07e45f6e13e94947
Author: Masahiro Yamada <masahiroy@kernel.org>
Date:   Mon May 22 01:04:13 2023 +0900

    modpost: remove is_shndx_special() check from section_rel(a)

    This check is unneeded. Without it, sec_name() will returns the null
    string "", then section_mismatch() will return immediately.

    Anyway, special section indices rarely appear in these loops.

    Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
    Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:27 -04:00
Donald Dutile 4f20d0c5c5 modpost: replace r->r_offset, r->r_addend with faddr, taddr
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit 04ed3b476306c1b4c6e544e40d10f477c8193435
Author: Masahiro Yamada <masahiroy@kernel.org>
Date:   Mon May 22 01:04:12 2023 +0900

    modpost: replace r->r_offset, r->r_addend with faddr, taddr

    r_offset/r_addend holds the offset address from/to which a symbol is
    referenced. It is unclear unless you are familiar with ELF.

    Rename them to faddr, taddr, respectively. The prefix 'f' means 'from',
    't' means 'to'.

    Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
    Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:27 -04:00
Donald Dutile f381a44a70 modpost: unify 'sym' and 'to' in default_mismatch_handler()
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit a23e7584ecf33df2b27ac176185c7b030ab0736f
Author: Masahiro Yamada <masahiroy@kernel.org>
Date:   Mon May 22 01:04:11 2023 +0900

    modpost: unify 'sym' and 'to' in default_mismatch_handler()

    find_tosym() takes 'sym' and stores the return value to another
    variable 'to'. You can use the same variable because we want to
    replace the original one when appropriate.

    Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
    Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:27 -04:00
Donald Dutile dab0251acc modpost: remove unused argument from secref_whitelist()
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit 05bb0704672dec59cbdc6b901130098ecfe7a846
Author: Masahiro Yamada <masahiroy@kernel.org>
Date:   Mon May 22 01:04:09 2023 +0900

    modpost: remove unused argument from secref_whitelist()

    secref_whitelist() does not use the argument 'mismatch'.

    Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
    Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:27 -04:00
Donald Dutile eefe9fdf83 Revert "modpost: skip ELF local symbols during section mismatch check"
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit 17b53f10aba7c17e92bcf713179bc577cba059b7
Author: Masahiro Yamada <masahiroy@kernel.org>
Date:   Mon May 22 01:04:06 2023 +0900

    Revert "modpost: skip ELF local symbols during section mismatch check"

    This reverts commit a4d26f1a09.

    The variable 'fromsym' never starts with ".L" since commit 87e5b1e8f257
    ("module: Sync code of is_arm_mapping_symbol()").

    In other words, Pattern 6 is now dead code.

    Previously, the .LANCHOR1 hid the symbols listed in Pattern 2.

    87e5b1e8f257 provided a better solution.

    Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
    Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:26 -04:00
Donald Dutile d5fd3547b6 modpost: rename find_elf_symbol() and find_elf_symbol2()
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit ac263349b91bf34b7c8419f5645c84b4f88de846
Author: Masahiro Yamada <masahiroy@kernel.org>
Date:   Mon May 15 00:27:28 2023 +0900

    modpost: rename find_elf_symbol() and find_elf_symbol2()

    find_elf_symbol() and find_elf_symbol2() are not good names.

    Rename them to find_tosym(), find_fromsym(), respectively.

    Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:26 -04:00
Donald Dutile e4ff46a787 modpost: pass section index to find_elf_symbol2()
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit 9990ca35870b7c57d39f8b325d676dfd028035b4
Author: Masahiro Yamada <masahiroy@kernel.org>
Date:   Mon May 15 00:27:27 2023 +0900

    modpost: pass section index to find_elf_symbol2()

    find_elf_symbol2() converts the section index to the section name,
    then compares the two strings in each iteration. This is slow.

    It is faster to compare the section indices (i.e. integers) directly.

    Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
    Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:26 -04:00
Donald Dutile a30045a912 modpost: pass 'tosec' down to default_mismatch_handler()
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit dbf7cc2e4e78dfecad02ff17ff5c9971b42da462
Author: Masahiro Yamada <masahiroy@kernel.org>
Date:   Mon May 15 00:27:26 2023 +0900

    modpost: pass 'tosec' down to default_mismatch_handler()

    default_mismatch_handler() does not need to compute 'tosec' because
    it is calculated by the caller.

    Pass it down to default_mismatch_handler() instead of calling
    sec_name() twice.

    Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
    Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:26 -04:00
Donald Dutile b23a4497bf modpost: squash extable_mismatch_handler() into default_mismatch_handler()
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit 856567d5599e7df75d7cad1fef1311d7c1854200
Author: Masahiro Yamada <masahiroy@kernel.org>
Date:   Mon May 15 00:27:25 2023 +0900

    modpost: squash extable_mismatch_handler() into default_mismatch_handler()

    Merging these two reduces several lines of code. The extable section
    mismatch is already distinguished by EXTABLE_TO_NON_TEXT.

    Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
    Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:26 -04:00
Donald Dutile 26e8eb69ab modpost: clean up is_executable_section()
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit f4c35484e7f11458c1834b88ee55b746cdabbb09
Author: Masahiro Yamada <masahiroy@kernel.org>
Date:   Mon May 15 00:27:24 2023 +0900

    modpost: clean up is_executable_section()

    SHF_EXECINSTR is a bit flag (#define SHF_EXECINSTR 0x4).
    Compare the masked flag to '!= 0'.

    There is no good reason to stop modpost immediately even if a special
    section index is given. You will get a section mismatch error anyway.

    Also, change the return type to bool.

    Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
    Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:26 -04:00
Donald Dutile 9d51800b4a modpost: squash report_sec_mismatch() into default_mismatch_handler()
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit fc5fa862c49a4d9e23617fbda7d249d2c1b72e56
Author: Masahiro Yamada <masahiroy@kernel.org>
Date:   Mon May 15 00:27:23 2023 +0900

    modpost: squash report_sec_mismatch() into default_mismatch_handler()

    report_sec_mismatch() and default_mismatch_handler() are small enough
    to be merged together.

    Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
    Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:26 -04:00
Donald Dutile 02cb936e5b modpost: squash report_extable_warnings() into extable_mismatch_handler()
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit faee9defd8fc376864efb39b87d59f667deeb488
Author: Masahiro Yamada <masahiroy@kernel.org>
Date:   Mon May 15 00:27:22 2023 +0900

    modpost: squash report_extable_warnings() into extable_mismatch_handler()

    Collect relevant code into one place to clarify all the cases are
    covered by 'if () ... else if ... else ...'.

    Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
    Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:26 -04:00
Donald Dutile f2542bc7c3 modpost: remove get_prettyname()
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit 6691e6f5fc3e9fa76c9a50970fa851829df7d9f2
Author: Masahiro Yamada <masahiroy@kernel.org>
Date:   Mon May 15 00:27:21 2023 +0900

    modpost: remove get_prettyname()

    This is the last user of get_pretty_name() - it is just used to
    distinguish whether the symbol is a function or not. It is not
    valuable information.

    Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:26 -04:00
Donald Dutile 77a5d4b7e6 modpost: remove fromsym info in __ex_table section mismatch warning
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit 6c90d36be3e5140c93d3af360d012fa26966304a
Author: Masahiro Yamada <masahiroy@kernel.org>
Date:   Mon May 15 00:27:20 2023 +0900

    modpost: remove fromsym info in __ex_table section mismatch warning

    report_extable_warnings() prints "from" in a pretty form, but we know
    it is always located in the __ex_table section, i.e. a collection of
    struct exception_table_entry.

    It is very likely to fail to get the symbol name and ends up with
    meaningless message:

      ... in reference from the (unknown reference) (unknown) to ...

    Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:26 -04:00
Donald Dutile c43bba12c9 modpost: remove broken calculation of exception_table_entry size
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit d0acc76a49aa917c1a455d11d32d34a01e8b2835
Author: Masahiro Yamada <masahiroy@kernel.org>
Date:   Mon May 15 00:27:19 2023 +0900

    modpost: remove broken calculation of exception_table_entry size

    find_extable_entry_size() is completely broken. It has awesome comments
    about how to calculate sizeof(struct exception_table_entry).

    It was based on these assumptions:

      - struct exception_table_entry has two fields
      - both of the fields have the same size

    Then, we came up with this equation:

      (offset of the second field) * 2 == (size of struct)

    It was true for all architectures when commit 52dc0595d5 ("modpost:
    handle relocations mismatch in __ex_table.") was applied.

    Our mathematics broke when commit 548acf1923 ("x86/mm: Expand the
    exception table logic to allow new handling options") introduced the
    third field.

    Now, the definition of exception_table_entry is highly arch-dependent.

    For x86, sizeof(struct exception_table_entry) is apparently 12, but
    find_extable_entry_size() sets extable_entry_size to 8.

    I could fix it, but I do not see much value in this code.

    extable_entry_size is used just for selecting a slightly different
    error message.

    If the first field ("insn") references to a non-executable section,

        The relocation at %s+0x%lx references
        section "%s" which is not executable, IOW
        it is not possible for the kernel to fault
        at that address.  Something is seriously wrong
        and should be fixed.

    If the second field ("fixup") references to a non-executable section,

        The relocation at %s+0x%lx references
        section "%s" which is not executable, IOW
        the kernel will fault if it ever tries to
        jump to it.  Something is seriously wrong
        and should be fixed.

    Merge the two error messages rather than adding even more complexity.

    Change fatal() to error() to make it continue running and catch more
    possible errors.

    Fixes: 548acf1923 ("x86/mm: Expand the exception table logic to allow new handling options")
    Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:26 -04:00
Donald Dutile f776cbeb9c module: Ignore L0 and rename is_arm_mapping_symbol()
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit 0a3bf86092c38f7b72c56c6901c78dd302411307
Author: Tiezhu Yang <yangtiezhu@loongson.cn>
Date:   Fri Mar 31 17:15:53 2023 +0800

    module: Ignore L0 and rename is_arm_mapping_symbol()

    The L0 symbol is generated when build module on LoongArch, ignore it in
    modpost and when looking at module symbols, otherwise we can not see the
    expected call trace.

    Now is_arm_mapping_symbol() is not only for ARM, in order to reflect the
    reality, rename is_arm_mapping_symbol() to is_mapping_symbol().

    This is related with commit c17a2538704f ("mksysmap: Fix the mismatch of
    'L0' symbols in System.map").

    (1) Simple test case

      [loongson@linux hello]$ cat hello.c
      #include <linux/init.h>
      #include <linux/module.h>
      #include <linux/printk.h>

      static void test_func(void)
      {
              pr_info("This is a test\n");
              dump_stack();
      }

      static int __init hello_init(void)
      {
              pr_warn("Hello, world\n");
              test_func();

              return 0;
      }

      static void __exit hello_exit(void)
      {
              pr_warn("Goodbye\n");
      }

      module_init(hello_init);
      module_exit(hello_exit);
      MODULE_LICENSE("GPL");
      [loongson@linux hello]$ cat Makefile
      obj-m:=hello.o

      ccflags-y += -g -Og

      all:
              make -C /lib/modules/$(shell uname -r)/build/ M=$(PWD) modules
      clean:
              make -C /lib/modules/$(shell uname -r)/build/ M=$(PWD) clean

    (2) Test environment

    system: LoongArch CLFS 5.5
    https://github.com/sunhaiyong1978/CLFS-for-LoongArch/releases/tag/5.0
    It needs to update grub to avoid booting error "invalid magic number".

    kernel: 6.3-rc1 with loongson3_defconfig + CONFIG_DYNAMIC_FTRACE=y

    (3) Test result

    Without this patch:

      [root@linux hello]# insmod hello.ko
      [root@linux hello]# dmesg
      ...
      Hello, world
      This is a test
      ...
      Call Trace:
      [<9000000000223728>] show_stack+0x68/0x18c
      [<90000000013374cc>] dump_stack_lvl+0x60/0x88
      [<ffff800002050028>] L0\x01+0x20/0x2c [hello]
      [<ffff800002058028>] L0\x01+0x20/0x30 [hello]
      [<900000000022097c>] do_one_initcall+0x88/0x288
      [<90000000002df890>] do_init_module+0x54/0x200
      [<90000000002e1e18>] __do_sys_finit_module+0xc4/0x114
      [<90000000013382e8>] do_syscall+0x7c/0x94
      [<9000000000221e3c>] handle_syscall+0xbc/0x158

    With this patch:

      [root@linux hello]# insmod hello.ko
      [root@linux hello]# dmesg
      ...
      Hello, world
      This is a test
      ...
      Call Trace:
      [<9000000000223728>] show_stack+0x68/0x18c
      [<90000000013374cc>] dump_stack_lvl+0x60/0x88
      [<ffff800002050028>] test_func+0x28/0x34 [hello]
      [<ffff800002058028>] hello_init+0x28/0x38 [hello]
      [<900000000022097c>] do_one_initcall+0x88/0x288
      [<90000000002df890>] do_init_module+0x54/0x200
      [<90000000002e1e18>] __do_sys_finit_module+0xc4/0x114
      [<90000000013382e8>] do_syscall+0x7c/0x94
      [<9000000000221e3c>] handle_syscall+0xbc/0x158

    Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
    Tested-by: Youling Tang <tangyouling@loongson.cn> # for LoongArch
    Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:25 -04:00
Donald Dutile 7dbb7e3cc8 module: Move is_arm_mapping_symbol() to module_symbol.h
JIRA: https://issues.redhat.com/browse/RHEL-28063

Conflict: Slight context diff but patch changes are the same.

commit 987d2e0aaa55de40938435be760aa96428470fd6
Author: Tiezhu Yang <yangtiezhu@loongson.cn>
Date:   Fri Mar 31 17:15:52 2023 +0800

    module: Move is_arm_mapping_symbol() to module_symbol.h

    In order to avoid duplicated code, move is_arm_mapping_symbol() to
    include/linux/module_symbol.h, then remove is_arm_mapping_symbol()
    in the other places.

    Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
    Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:25 -04:00
Donald Dutile b8f8ff56f2 module: Sync code of is_arm_mapping_symbol()
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit 87e5b1e8f257023ac5c4d2b8f07716a7f3dcc8ea
Author: Tiezhu Yang <yangtiezhu@loongson.cn>
Date:   Fri Mar 31 17:15:51 2023 +0800

    module: Sync code of is_arm_mapping_symbol()

    After commit 2e3a10a155 ("ARM: avoid ARM binutils leaking ELF local
    symbols") and commit d6b732666a1b ("modpost: fix undefined behavior of
    is_arm_mapping_symbol()"), many differences of is_arm_mapping_symbol()
    exist in kernel/module/kallsyms.c and scripts/mod/modpost.c, just sync
    the code to keep consistent.

    Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
    Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:25 -04:00
Donald Dutile 5d01e286d5 modpost: Fix processing of CRCs on 32-bit build machines
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit fb27e70f6e408dee5d22b083e7a38a59e6118253
Author: Ben Hutchings <ben@decadent.org.uk>
Date:   Wed Mar 22 19:11:45 2023 +0100

    modpost: Fix processing of CRCs on 32-bit build machines

    modpost now reads CRCs from .*.cmd files, parsing them using strtol().
    This is inconsistent with its parsing of Module.symvers and with their
    definition as *unsigned* 32-bit values.

    strtol() clamps values to [LONG_MIN, LONG_MAX], and when building on a
    32-bit system this changes all CRCs >= 0x80000000 to be 0x7fffffff.

    Change extract_crcs_for_object() to use strtoul() instead.

    Cc: stable@vger.kernel.org
    Fixes: f292d875d0dc ("modpost: extract symbol versions from *.cmd files")
    Signed-off-by: Ben Hutchings <ben@decadent.org.uk>
    Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:23 -04:00
Donald Dutile a43d31cfdc modpost: error out if addend_*_rel() is not implemented for REL arch
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit 64f140417d818aa374788acc9cb8328165747262
Author: Masahiro Yamada <masahiroy@kernel.org>
Date:   Fri May 12 01:24:22 2023 +0900

    modpost: error out if addend_*_rel() is not implemented for REL arch

    The section mismatch check relies on the relocation entries.

    For REL, the addend value is implicit, so we need some code to compute
    it. Currently, EM_386, EM_ARM, and EM_MIPS are supported. This commit
    makes sure we covered all the cases.

    I believe the other architectures use RELA, where the explicit r_addend
    field exists.

    Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:22 -04:00
Donald Dutile 1e9a6cde66 modpost: Mark uuid_le type to be suitable only for MEI
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit 248043299bf61134fb675d16963e11f49e79b05b
Author: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Date:   Fri Nov 25 12:12:49 2022 +0200

    modpost: Mark uuid_le type to be suitable only for MEI

    The uuid_le type is used only in MEI ABI, do not advertise it for others.
    While at it, comment out that UUID types are not to be used in a new code.

    Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:22 -04:00
Donald Dutile 105be4935d modpost: fix array_size.cocci warning
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit 30daacc571d1416f24abd4cc49910ff9322a8cf6
Author: KaiLong Wang <wangkailong@jari.cn>
Date:   Sun Nov 13 17:29:50 2022 +0800

    modpost: fix array_size.cocci warning

    Fix following coccicheck warning:

    scripts/mod/sumversion.c:219:48-49: WARNING: Use ARRAY_SIZE
    scripts/mod/sumversion.c:156:48-49: WARNING: Use ARRAY_SIZE

    Signed-off-by: KaiLong Wang <wangkailong@jari.cn>
    Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:22 -04:00
Donald Dutile 4060329d04 modpost: Join broken long printed messages
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit 0d2573a2b7838a4f6934c2835e6730b38df4bcc9
Author: Geert Uytterhoeven <geert+renesas@glider.be>
Date:   Wed Nov 9 14:30:55 2022 +0100

    modpost: Join broken long printed messages

    Breaking long printed messages in multiple lines makes it very hard to
    look up where they originated from.

    Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
    Reviewed-by: Nicolas Schier <nicolas@fjasle.eu>
    Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:22 -04:00
Donald Dutile d2c721d567 modpost: fix module versioning when a symbol lacks valid CRC
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit 5b8a9a8fd1f0c3d55d407cf759d54ca68798d9ad
Author: Masahiro Yamada <masahiroy@kernel.org>
Date:   Tue Aug 9 23:11:17 2022 +0900

    modpost: fix module versioning when a symbol lacks valid CRC

    Since commit 7b4537199a4a ("kbuild: link symbol CRCs at final link,
    removing CONFIG_MODULE_REL_CRCS"), module versioning is broken on
    some architectures. Loading a module fails with "disagrees about
    version of symbol module_layout".

    On such architectures (e.g. ARCH=sparc build with sparc64_defconfig),
    modpost shows a warning, like follows:

      WARNING: modpost: EXPORT symbol "_mcount" [vmlinux] version generation failed, symbol will not be versioned.
      Is "_mcount" prototyped in <asm/asm-prototypes.h>?

    Previously, it was a harmless warning (CRC check was just skipped),
    but now wrong CRCs are used for comparison because invalid CRCs are
    just skipped.

      $ sparc64-linux-gnu-nm -n vmlinux
        [snip]
      0000000000c2cea0 r __ksymtab__kstrtol
      0000000000c2ceb8 r __ksymtab__kstrtoul
      0000000000c2ced0 r __ksymtab__local_bh_enable
      0000000000c2cee8 r __ksymtab__mcount
      0000000000c2cf00 r __ksymtab__printk
      0000000000c2cf18 r __ksymtab__raw_read_lock
      0000000000c2cf30 r __ksymtab__raw_read_lock_bh
        [snip]
      0000000000c53b34 D __crc__kstrtol
      0000000000c53b38 D __crc__kstrtoul
      0000000000c53b3c D __crc__local_bh_enable
      0000000000c53b40 D __crc__printk
      0000000000c53b44 D __crc__raw_read_lock
      0000000000c53b48 D __crc__raw_read_lock_bh

    Please notice __crc__mcount is missing here.

    When the module subsystem looks up a CRC that comes after, it results
    in reading out a wrong address. For example, when __crc__printk is
    needed, the module subsystem reads 0xc53b44 instead of 0xc53b40.

    All CRC entries must be output for correct index accessing. Invalid
    CRCs will be unused, but are needed to keep the one-to-one mapping
    between __ksymtab_* and __crc_*.

    The best is to fix all modpost warnings, but several warnings are still
    remaining on less popular architectures.

    Fixes: 7b4537199a4a ("kbuild: link symbol CRCs at final link, removing CONFIG_MODULE_REL_CRCS")
    Reported-by: matoro <matoro_mailinglist_kernel@matoro.tk>
    Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
    Tested-by: matoro <matoro_mailinglist_kernel@matoro.tk>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:19 -04:00
Donald Dutile 8f9b74000e modpost: remove .symbol_white_list field entirely
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit 672fb6740cbfde34f4d367ffa3c939b608a927e1
Author: Masahiro Yamada <masahiroy@kernel.org>
Date:   Mon Aug 1 18:39:02 2022 +0900

    modpost: remove .symbol_white_list field entirely

    It is not so useful to have symbol whitelists in arrays. With this
    over-engineering, the code is difficult to follow.

    Let's do it more directly, and collect the relevant code to one place.

    Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:19 -04:00
Donald Dutile f791b0b021 modpost: remove unneeded .symbol_white_list initializers
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit 1560cb0e186e83f0572a84d22e139c100060905c
Author: Masahiro Yamada <masahiroy@kernel.org>
Date:   Mon Aug 1 18:39:01 2022 +0900

    modpost: remove unneeded .symbol_white_list initializers

    The ->symbol_white_list field is referenced in secref_whitelist(),
    only when 'fromsec' is data_sections.

            /* Check for pattern 2 */
            if (match(tosec, init_exit_sections) &&
                match(fromsec, data_sections) &&
                match(fromsym, mismatch->symbol_white_list))
                    return 0;

    If .fromsec is not data sections, the .symbol_white_list member is
    not used by anyone.

    Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:19 -04:00
Donald Dutile 0a045b4260 modpost: add PATTERNS() helper macro
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit 7452dd26a59a9dfcde3f179594f3be6c4752a9a9
Author: Masahiro Yamada <masahiroy@kernel.org>
Date:   Mon Aug 1 18:39:00 2022 +0900

    modpost: add PATTERNS() helper macro

    This will be useful to define a NULL-terminated array inside a function
    call.

    Currently, string arrays passed to match() are defined in separate
    places:

        static const char *const init_sections[] = { ALL_INIT_SECTIONS, NULL };
        static const char *const text_sections[] = { ALL_TEXT_SECTIONS, NULL };
        static const char *const optim_symbols[] = { "*.constprop.*", NULL };

                ...

                /* Check for pattern 5 */
                if (match(fromsec, text_sections) &&
                    match(tosec, init_sections) &&
                    match(fromsym, optim_symbols))
                        return 0;

    With the new helper macro, you can list the patterns directly in the
    function call, like this:

                /* Check for pattern 5 */
                if (match(fromsec, PATTERNS(ALL_TEXT_SECTIONS)) &&
                    match(tosec, PATTERNS(ALL_INIT_SECTIONS)) &&
                    match(fromsym, PATTERNS("*.contprop.*")))
                        return 0;

    Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:19 -04:00
Donald Dutile aa93accd35 modpost: shorten warning messages in report_sec_mismatch()
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit 072dd2c8928f2ecdc52cdf5acf30479b327386c9
Author: Masahiro Yamada <masahiroy@kernel.org>
Date:   Mon Aug 1 18:38:59 2022 +0900

    modpost: shorten warning messages in report_sec_mismatch()

    Each section mismatch results in long warning messages. Too much.

    Make each warning fit in one line, and remove a lot of messy code.

    Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:19 -04:00
Donald Dutile 7f84870da5 Revert "Kbuild, lto, workaround: Don't warn for initcall_reference in modpost"
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit a25efd6ef1ef4c32991a1d5a013dd41e3b8f7eff
Author: Masahiro Yamada <masahiroy@kernel.org>
Date:   Sun Jul 31 02:36:36 2022 +0900

    Revert "Kbuild, lto, workaround: Don't warn for initcall_reference in modpost"

    This reverts commit 77ab21adae.

    Even after 8 years later, GCC LTO has not been upstreamed. Also, it said
    "This is a workaround". If this is needed in the future, it should be
    added in a proper way.

    Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
    Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>
    Acked-by: Jiri Slaby <jirislaby@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:19 -04:00