Skip to content

Commit d107b32

Browse files
petrpavlujpoimboe
authored andcommitted
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: 2c05ca0 ("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>
1 parent fd4eeb3 commit d107b32

1 file changed

Lines changed: 6 additions & 7 deletions

File tree

tools/objtool/elf.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,14 @@
1818
#include <errno.h>
1919
#include <libgen.h>
2020
#include <ctype.h>
21+
#include <linux/align.h>
22+
#include <linux/kernel.h>
2123
#include <linux/interval_tree_generic.h>
24+
#include <linux/log2.h>
2225
#include <objtool/builtin.h>
2326
#include <objtool/elf.h>
2427
#include <objtool/warn.h>
2528

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-
3029
static inline u32 str_hash(const char *str)
3130
{
3231
return jhash(str, strlen(str), 0);
@@ -1336,7 +1335,7 @@ unsigned int elf_add_string(struct elf *elf, struct section *strtab, const char
13361335
return -1;
13371336
}
13381337

1339-
offset = ALIGN_UP(strtab->sh.sh_size, strtab->sh.sh_addralign);
1338+
offset = ALIGN(strtab->sh.sh_size, strtab->sh.sh_addralign);
13401339

13411340
if (!elf_add_data(elf, strtab, str, strlen(str) + 1))
13421341
return -1;
@@ -1378,7 +1377,7 @@ void *elf_add_data(struct elf *elf, struct section *sec, const void *data, size_
13781377
sec->data->d_size = size;
13791378
sec->data->d_align = 1;
13801379

1381-
offset = ALIGN_UP(sec->sh.sh_size, sec->sh.sh_addralign);
1380+
offset = ALIGN(sec->sh.sh_size, sec->sh.sh_addralign);
13821381
sec->sh.sh_size = offset + size;
13831382

13841383
mark_sec_changed(elf, sec, true);
@@ -1502,7 +1501,7 @@ static int elf_alloc_reloc(struct elf *elf, struct section *rsec)
15021501
rsec->data->d_size = nr_relocs_new * elf_rela_size(elf);
15031502
rsec->sh.sh_size = rsec->data->d_size;
15041503

1505-
nr_alloc = MAX(64, ALIGN_UP_POW2(nr_relocs_new));
1504+
nr_alloc = max(64UL, roundup_pow_of_two(nr_relocs_new));
15061505
if (nr_alloc <= rsec->nr_alloc_relocs)
15071506
return 0;
15081507

0 commit comments

Comments
 (0)