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: Replace custom macros in elf.c with shared ones

The source file tools/objtool/elf.c defines the macros ALIGN_UP(),
ALIGN_UP_POW2() and MAX(). These macros unnecessarily duplicate
functionality already available under tools/include/, specifically ALIGN(),
roundup_pow_of_two() and max().

More importantly, the definition of ALIGN_UP_POW2() is incorrect when the
input is 1, as it results in a call to __builtin_clz(0), which produces an
undefined result. This issue impacts the function elf_alloc_reloc(). When
adding the first relocation to a section, the function allocates an
undefined number of relocations.

Replace the custom macros with the shared functionality to resolve these
issues.

Fixes: 2c05ca026218 ("objtool: Add elf_create_reloc() and elf_init_reloc()")
Signed-off-by: Petr Pavlu <petr.pavlu@suse.com>
Link: https://patch.msgid.link/20260126151356.3924887-1-petr.pavlu@suse.com
Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>

authored by

Petr Pavlu and committed by
Josh Poimboeuf
d107b326 fd4eeb30

+6 -7
+6 -7
tools/objtool/elf.c
··· 18 18 #include <errno.h> 19 19 #include <libgen.h> 20 20 #include <ctype.h> 21 + #include <linux/align.h> 22 + #include <linux/kernel.h> 21 23 #include <linux/interval_tree_generic.h> 24 + #include <linux/log2.h> 22 25 #include <objtool/builtin.h> 23 26 #include <objtool/elf.h> 24 27 #include <objtool/warn.h> 25 - 26 - #define ALIGN_UP(x, align_to) (((x) + ((align_to)-1)) & ~((align_to)-1)) 27 - #define ALIGN_UP_POW2(x) (1U << ((8 * sizeof(x)) - __builtin_clz((x) - 1U))) 28 - #define MAX(a, b) ((a) > (b) ? (a) : (b)) 29 28 30 29 static inline u32 str_hash(const char *str) 31 30 { ··· 1335 1336 return -1; 1336 1337 } 1337 1338 1338 - offset = ALIGN_UP(strtab->sh.sh_size, strtab->sh.sh_addralign); 1339 + offset = ALIGN(strtab->sh.sh_size, strtab->sh.sh_addralign); 1339 1340 1340 1341 if (!elf_add_data(elf, strtab, str, strlen(str) + 1)) 1341 1342 return -1; ··· 1377 1378 sec->data->d_size = size; 1378 1379 sec->data->d_align = 1; 1379 1380 1380 - offset = ALIGN_UP(sec->sh.sh_size, sec->sh.sh_addralign); 1381 + offset = ALIGN(sec->sh.sh_size, sec->sh.sh_addralign); 1381 1382 sec->sh.sh_size = offset + size; 1382 1383 1383 1384 mark_sec_changed(elf, sec, true); ··· 1501 1502 rsec->data->d_size = nr_relocs_new * elf_rela_size(elf); 1502 1503 rsec->sh.sh_size = rsec->data->d_size; 1503 1504 1504 - nr_alloc = MAX(64, ALIGN_UP_POW2(nr_relocs_new)); 1505 + nr_alloc = max(64UL, roundup_pow_of_two(nr_relocs_new)); 1505 1506 if (nr_alloc <= rsec->nr_alloc_relocs) 1506 1507 return 0; 1507 1508