Skip to content

Commit 85718e6

Browse files
committed
Merge: objtool: enable CONFIG_OBJTOOL_WERROR
MR: https://gitlab.com/redhat/centos-stream/src/kernel/centos-stream-9/-/merge_requests/6677 JIRA: https://issues.redhat.com/browse/RHEL-85302 This patchset backports CONFIG_OBJTOOL_WERROR and supporting commits to rhel-9. To minimize disruption, the promotion of objtool warnings to build errors will only occur for kernel and in-tree kernel builds. This means it will not be activated for (third party) out-of-tree kernel module builds. Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com> Approved-by: Rafael Aquini <raquini@redhat.com> Approved-by: Waiman Long <longman@redhat.com> Approved-by: CKI KWF Bot <cki-ci-bot+kwf-gitlab-com@redhat.com> Merged-by: Augusto Caringi <acaringi@redhat.com>
2 parents 9f90dd4 + 62f1dab commit 85718e6

File tree

11 files changed

+195
-110
lines changed

11 files changed

+195
-110
lines changed

lib/Kconfig.debug

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,17 @@ config FRAME_POINTER
467467
config OBJTOOL
468468
bool
469469

470+
config OBJTOOL_WERROR
471+
bool "Upgrade objtool warnings to errors"
472+
depends on OBJTOOL && !COMPILE_TEST
473+
help
474+
Fail the build on objtool warnings.
475+
476+
Objtool warnings can indicate kernel instability, including boot
477+
failures. This option is highly recommended.
478+
479+
If unsure, say Y.
480+
470481
config STACK_VALIDATION
471482
bool "Compile-time stack metadata validation"
472483
depends on HAVE_STACK_VALIDATION && UNWINDER_FRAME_POINTER

redhat/configs/build_configs.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ function merge_configs()
112112

113113
if [ -n "$ENABLE_WERROR" ]; then
114114
sed -i "s|# CONFIG_WERROR is not set|CONFIG_WERROR=y|g" "$name"
115+
sed -i "s|# CONFIG_OBJTOOL_WERROR is not set|CONFIG_OBJTOOL_WERROR=y|g" "$name"
115116
fi
116117

117118
rm -f config-merged."$count" config-merging."$count"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# CONFIG_OBJTOOL_WERROR is not set

scripts/Makefile.lib

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,10 @@ objtool-args-$(CONFIG_HAVE_STATIC_CALL_INLINE) += --static-call
258258
objtool-args-$(CONFIG_HAVE_UACCESS_VALIDATION) += --uaccess
259259
objtool-args-$(CONFIG_GCOV_KERNEL) += --no-unreachable
260260
objtool-args-$(CONFIG_PREFIX_SYMBOLS) += --prefix=$(CONFIG_FUNCTION_PADDING_BYTES)
261+
# RHEL-only: don't enforce OBJTOOL_WERROR for out of tree modules
262+
ifeq ($(KBUILD_EXTMOD),)
263+
objtool-args-$(CONFIG_OBJTOOL_WERROR) += --Werror --backtrace
264+
endif
261265

262266
objtool-args = $(objtool-args-y) \
263267
$(if $(delay-objtool), --link) \

tools/objtool/builtin-check.c

Lines changed: 147 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
#include <subcmd/parse-options.h>
77
#include <string.h>
88
#include <stdlib.h>
9+
#include <fcntl.h>
10+
#include <unistd.h>
11+
#include <sys/stat.h>
12+
#include <sys/sendfile.h>
913
#include <objtool/builtin.h>
1014
#include <objtool/objtool.h>
1115

@@ -14,6 +18,8 @@
1418
"error: objtool: " format "\n", \
1519
##__VA_ARGS__)
1620

21+
const char *objname;
22+
1723
struct opts opts;
1824

1925
static const char * const check_usage[] = {
@@ -71,7 +77,7 @@ const struct option check_options[] = {
7177
OPT_BOOLEAN('i', "ibt", &opts.ibt, "validate and annotate IBT"),
7278
OPT_BOOLEAN('m', "mcount", &opts.mcount, "annotate mcount/fentry calls for ftrace"),
7379
OPT_BOOLEAN('n', "noinstr", &opts.noinstr, "validate noinstr rules"),
74-
OPT_BOOLEAN('o', "orc", &opts.orc, "generate ORC metadata"),
80+
OPT_BOOLEAN(0, "orc", &opts.orc, "generate ORC metadata"),
7581
OPT_BOOLEAN('r', "retpoline", &opts.retpoline, "validate and annotate retpoline usage"),
7682
OPT_BOOLEAN(0, "rethunk", &opts.rethunk, "validate and annotate rethunk usage"),
7783
OPT_BOOLEAN(0, "unret", &opts.unret, "validate entry unret placement"),
@@ -83,15 +89,16 @@ const struct option check_options[] = {
8389
OPT_CALLBACK_OPTARG(0, "dump", NULL, NULL, "orc", "dump metadata", parse_dump),
8490

8591
OPT_GROUP("Options:"),
86-
OPT_BOOLEAN(0, "backtrace", &opts.backtrace, "unwind on error"),
87-
OPT_BOOLEAN(0, "backup", &opts.backup, "create .orig files before modification"),
88-
OPT_BOOLEAN(0, "dry-run", &opts.dryrun, "don't write modifications"),
89-
OPT_BOOLEAN(0, "link", &opts.link, "object is a linked object"),
90-
OPT_BOOLEAN(0, "module", &opts.module, "object is part of a kernel module"),
91-
OPT_BOOLEAN(0, "mnop", &opts.mnop, "nop out mcount call sites"),
92-
OPT_BOOLEAN(0, "no-unreachable", &opts.no_unreachable, "skip 'unreachable instruction' warnings"),
93-
OPT_BOOLEAN(0, "sec-address", &opts.sec_address, "print section addresses in warnings"),
94-
OPT_BOOLEAN(0, "stats", &opts.stats, "print statistics"),
92+
OPT_BOOLEAN(0, "backtrace", &opts.backtrace, "unwind on error"),
93+
OPT_BOOLEAN(0, "dry-run", &opts.dryrun, "don't write modifications"),
94+
OPT_BOOLEAN(0, "link", &opts.link, "object is a linked object"),
95+
OPT_BOOLEAN(0, "module", &opts.module, "object is part of a kernel module"),
96+
OPT_BOOLEAN(0, "mnop", &opts.mnop, "nop out mcount call sites"),
97+
OPT_BOOLEAN(0, "no-unreachable", &opts.no_unreachable, "skip 'unreachable instruction' warnings"),
98+
OPT_STRING('o', "output", &opts.output, "file", "output file name"),
99+
OPT_BOOLEAN(0, "sec-address", &opts.sec_address, "print section addresses in warnings"),
100+
OPT_BOOLEAN(0, "stats", &opts.stats, "print statistics"),
101+
OPT_BOOLEAN(0, "Werror", &opts.werror, "return error on warnings"),
95102

96103
OPT_END(),
97104
};
@@ -190,37 +197,157 @@ static bool link_opts_valid(struct objtool_file *file)
190197
return true;
191198
}
192199

200+
static int copy_file(const char *src, const char *dst)
201+
{
202+
size_t to_copy, copied;
203+
int dst_fd, src_fd;
204+
struct stat stat;
205+
off_t offset = 0;
206+
207+
src_fd = open(src, O_RDONLY);
208+
if (src_fd == -1) {
209+
ERROR("can't open '%s' for reading", src);
210+
return 1;
211+
}
212+
213+
dst_fd = open(dst, O_WRONLY | O_CREAT | O_TRUNC, 0400);
214+
if (dst_fd == -1) {
215+
ERROR("can't open '%s' for writing", dst);
216+
return 1;
217+
}
218+
219+
if (fstat(src_fd, &stat) == -1) {
220+
perror("fstat");
221+
return 1;
222+
}
223+
224+
if (fchmod(dst_fd, stat.st_mode) == -1) {
225+
perror("fchmod");
226+
return 1;
227+
}
228+
229+
for (to_copy = stat.st_size; to_copy > 0; to_copy -= copied) {
230+
copied = sendfile(dst_fd, src_fd, &offset, to_copy);
231+
if (copied == -1) {
232+
perror("sendfile");
233+
return 1;
234+
}
235+
}
236+
237+
close(dst_fd);
238+
close(src_fd);
239+
return 0;
240+
}
241+
242+
static char **save_argv(int argc, const char **argv)
243+
{
244+
char **orig_argv;
245+
246+
orig_argv = calloc(argc, sizeof(char *));
247+
if (!orig_argv) {
248+
perror("calloc");
249+
return NULL;
250+
}
251+
252+
for (int i = 0; i < argc; i++) {
253+
orig_argv[i] = strdup(argv[i]);
254+
if (!orig_argv[i]) {
255+
perror("strdup");
256+
return NULL;
257+
}
258+
};
259+
260+
return orig_argv;
261+
}
262+
263+
#define ORIG_SUFFIX ".orig"
264+
193265
int objtool_run(int argc, const char **argv)
194266
{
195-
const char *objname;
196267
struct objtool_file *file;
197-
int ret;
268+
char *backup = NULL;
269+
char **orig_argv;
270+
int ret = 0;
198271

199-
argc = cmd_parse_options(argc, argv, check_usage);
200-
objname = argv[0];
272+
orig_argv = save_argv(argc, argv);
273+
if (!orig_argv)
274+
return 1;
275+
276+
cmd_parse_options(argc, argv, check_usage);
201277

202278
if (!opts_valid())
203279
return 1;
204280

281+
objname = argv[0];
282+
205283
if (opts.dump_orc)
206284
return orc_dump(objname);
207285

286+
if (!opts.dryrun && opts.output) {
287+
/* copy original .o file to output file */
288+
if (copy_file(objname, opts.output))
289+
return 1;
290+
291+
/* from here on, work directly on the output file */
292+
objname = opts.output;
293+
}
294+
208295
file = objtool_open_read(objname);
209296
if (!file)
210-
return 1;
297+
goto err;
211298

212299
if (!mnop_opts_valid())
213-
return 1;
300+
goto err;
214301

215302
if (!link_opts_valid(file))
216-
return 1;
303+
goto err;
217304

218305
ret = check(file);
219306
if (ret)
220-
return ret;
307+
goto err;
221308

222-
if (file->elf->changed)
223-
return elf_write(file->elf);
309+
if (!opts.dryrun && file->elf->changed && elf_write(file->elf))
310+
goto err;
224311

225312
return 0;
313+
314+
err:
315+
if (opts.dryrun)
316+
goto err_msg;
317+
318+
if (opts.output) {
319+
unlink(opts.output);
320+
goto err_msg;
321+
}
322+
323+
/*
324+
* Make a backup before kbuild deletes the file so the error
325+
* can be recreated without recompiling or relinking.
326+
*/
327+
backup = malloc(strlen(objname) + strlen(ORIG_SUFFIX) + 1);
328+
if (!backup) {
329+
perror("malloc");
330+
return 1;
331+
}
332+
333+
strcpy(backup, objname);
334+
strcat(backup, ORIG_SUFFIX);
335+
if (copy_file(objname, backup))
336+
return 1;
337+
338+
err_msg:
339+
fprintf(stderr, "%s", orig_argv[0]);
340+
341+
for (int i = 1; i < argc; i++) {
342+
char *arg = orig_argv[i];
343+
344+
if (backup && !strcmp(arg, objname))
345+
fprintf(stderr, " %s -o %s", backup, objname);
346+
else
347+
fprintf(stderr, " %s", arg);
348+
}
349+
350+
fprintf(stderr, "\n");
351+
352+
return 1;
226353
}

tools/objtool/check.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ static bool __dead_end_function(struct objtool_file *file, struct symbol *func,
188188
"__stack_chk_fail",
189189
"__tdx_hypercall_failed",
190190
"__ubsan_handle_builtin_unreachable",
191+
"acpi_processor_ffh_play_dead",
191192
"arch_call_rest_init",
192193
"cpu_bringup_and_idle",
193194
"cpu_startup_entry",
@@ -4507,8 +4508,10 @@ int check(struct objtool_file *file)
45074508
init_cfi_state(&force_undefined_cfi);
45084509
force_undefined_cfi.force_undefined = true;
45094510

4510-
if (!cfi_hash_alloc(1UL << (file->elf->symbol_bits - 3)))
4511+
if (!cfi_hash_alloc(1UL << (file->elf->symbol_bits - 3))) {
4512+
ret = -1;
45114513
goto out;
4514+
}
45124515

45134516
cfi_hash_add(&init_cfi);
45144517
cfi_hash_add(&func_cfi);
@@ -4525,7 +4528,7 @@ int check(struct objtool_file *file)
45254528
if (opts.retpoline) {
45264529
ret = validate_retpoline(file);
45274530
if (ret < 0)
4528-
return ret;
4531+
goto out;
45294532
warnings += ret;
45304533
}
45314534

@@ -4561,7 +4564,7 @@ int check(struct objtool_file *file)
45614564
*/
45624565
ret = validate_unrets(file);
45634566
if (ret < 0)
4564-
return ret;
4567+
goto out;
45654568
warnings += ret;
45664569
}
45674570

@@ -4638,9 +4641,18 @@ int check(struct objtool_file *file)
46384641

46394642
out:
46404643
/*
4641-
* For now, don't fail the kernel build on fatal warnings. These
4642-
* errors are still fairly common due to the growing matrix of
4643-
* supported toolchains and their recent pace of change.
4644+
* CONFIG_OBJTOOL_WERROR upgrades all warnings (and errors) to actual
4645+
* errors.
4646+
*
4647+
* Note that even "fatal" type errors don't actually return an error
4648+
* without CONFIG_OBJTOOL_WERROR. That probably needs improved at some
4649+
* point.
46444650
*/
4651+
if (opts.werror && (ret || warnings)) {
4652+
if (warnings)
4653+
WARN("%d warning(s) upgraded to errors", warnings);
4654+
return 1;
4655+
}
4656+
46454657
return 0;
46464658
}

tools/objtool/elf.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,9 +1389,6 @@ int elf_write(struct elf *elf)
13891389
struct section *sec;
13901390
Elf_Scn *s;
13911391

1392-
if (opts.dryrun)
1393-
return 0;
1394-
13951392
/* Update changed relocation sections and section headers: */
13961393
list_for_each_entry(sec, &elf->sections, list) {
13971394
if (sec->truncate)

tools/objtool/include/objtool/builtin.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,15 @@ struct opts {
2929

3030
/* options: */
3131
bool backtrace;
32-
bool backup;
3332
bool dryrun;
3433
bool link;
3534
bool mnop;
3635
bool module;
3736
bool no_unreachable;
37+
const char *output;
3838
bool sec_address;
3939
bool stats;
40+
bool werror;
4041
};
4142

4243
extern struct opts opts;

tools/objtool/include/objtool/warn.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@ static inline char *offstr(struct section *sec, unsigned long offset)
4343

4444
#define WARN(format, ...) \
4545
fprintf(stderr, \
46-
"%s: warning: objtool: " format "\n", \
47-
objname, ##__VA_ARGS__)
46+
"%s: %s: objtool: " format "\n", \
47+
objname, \
48+
opts.werror ? "error" : "warning", \
49+
##__VA_ARGS__)
4850

4951
#define WARN_FUNC(format, sec, offset, ...) \
5052
({ \

0 commit comments

Comments
 (0)