Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

objtool: Warn on functions with ambiguous -ffunction-sections section names

When compiled with -ffunction-sections, a function named startup() will
be placed in .text.startup. However, .text.startup is also used by the
compiler for functions with __attribute__((constructor)).

That creates an ambiguity for the vmlinux linker script, which needs to
differentiate those two cases.

Similar naming conflicts exist for functions named exit(), split(),
unlikely(), hot() and unknown().

One potential solution would be to use '#ifdef CC_USING_FUNCTION_SECTIONS'
to create two distinct implementations of the TEXT_MAIN macro. However,
-ffunction-sections can be (and is) enabled or disabled on a per-object
basis (for example via ccflags-y or AUTOFDO_PROFILE).

So the recently unified TEXT_MAIN macro (commit 1ba9f8979426
("vmlinux.lds: Unify TEXT_MAIN, DATA_MAIN, and related macros")) is
necessary. This means there's no way for the linker script to
disambiguate things.

Instead, use objtool to warn on any function names whose resulting
section names might create ambiguity when the kernel is compiled (in
whole or in part) with -ffunction-sections.

Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: live-patching@vger.kernel.org
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Link: https://patch.msgid.link/65fedea974fe14be487c8867a0b8d0e4a294ce1e.1762991150.git.jpoimboe@kernel.org

authored by

Josh Poimboeuf and committed by
Ingo Molnar
9c7dc1dd 0330b7fb

+55
+15
include/asm-generic/vmlinux.lds.h
··· 97 97 * Other .text.* sections that are typically grouped separately, such as 98 98 * .text.unlikely or .text.hot, must be matched explicitly before using 99 99 * TEXT_MAIN. 100 + * 101 + * NOTE: builds *with* and *without* -ffunction-sections are both supported by 102 + * this single macro. Even with -ffunction-sections, there may be some objects 103 + * NOT compiled with the flag due to the use of a specific Makefile override 104 + * like cflags-y or AUTOFDO_PROFILE_foo.o. So this single catchall rule is 105 + * needed to support mixed object builds. 106 + * 107 + * One implication is that functions named startup(), exit(), split(), 108 + * unlikely(), hot(), and unknown() are not allowed in the kernel due to the 109 + * ambiguity of their section names with -ffunction-sections. For example, 110 + * .text.startup could be __attribute__((constructor)) code in a *non* 111 + * ffunction-sections object, which should be placed in .init.text; or it could 112 + * be an actual function named startup() in an ffunction-sections object, which 113 + * should be placed in .text. Objtool will detect and complain about any such 114 + * ambiguously named functions. 100 115 */ 101 116 #define TEXT_MAIN \ 102 117 .text \
+7
tools/objtool/Documentation/objtool.txt
··· 456 456 these special names and does not use module_init() / module_exit() 457 457 macros to create them. 458 458 459 + 13. file.o: warning: func() function name creates ambiguity with -ffunctions-sections 460 + 461 + Functions named startup(), exit(), split(), unlikely(), hot(), and 462 + unknown() are not allowed due to the ambiguity of their section 463 + names when compiled with -ffunction-sections. For more information, 464 + see the comment above TEXT_MAIN in include/asm-generic/vmlinux.lds.h. 465 + 459 466 460 467 If the error doesn't seem to make sense, it could be a bug in objtool. 461 468 Feel free to ask objtool maintainers for help.
+33
tools/objtool/check.c
··· 2663 2663 return 0; 2664 2664 } 2665 2665 2666 + /* 2667 + * Certain function names are disallowed due to section name ambiguities 2668 + * introduced by -ffunction-sections. 2669 + * 2670 + * See the comment above TEXT_MAIN in include/asm-generic/vmlinux.lds.h. 2671 + */ 2672 + static int validate_function_names(struct objtool_file *file) 2673 + { 2674 + struct symbol *func; 2675 + int warnings = 0; 2676 + 2677 + for_each_sym(file->elf, func) { 2678 + if (!is_func_sym(func)) 2679 + continue; 2680 + 2681 + if (!strcmp(func->name, "startup") || strstarts(func->name, "startup.") || 2682 + !strcmp(func->name, "exit") || strstarts(func->name, "exit.") || 2683 + !strcmp(func->name, "split") || strstarts(func->name, "split.") || 2684 + !strcmp(func->name, "unlikely") || strstarts(func->name, "unlikely.") || 2685 + !strcmp(func->name, "hot") || strstarts(func->name, "hot.") || 2686 + !strcmp(func->name, "unknown") || strstarts(func->name, "unknown.")) { 2687 + 2688 + WARN("%s() function name creates ambiguity with -ffunction-sections", 2689 + func->name); 2690 + warnings++; 2691 + } 2692 + } 2693 + 2694 + return warnings; 2695 + } 2696 + 2666 2697 static bool is_special_call(struct instruction *insn) 2667 2698 { 2668 2699 if (insn->type == INSN_CALL) { ··· 4962 4931 4963 4932 if (!nr_insns) 4964 4933 goto out; 4934 + 4935 + warnings += validate_function_names(file); 4965 4936 4966 4937 if (opts.retpoline) 4967 4938 warnings += validate_retpoline(file);