Skip to content

Commit 51f6463

Browse files
olsajiriAlexei Starovoitov
authored andcommitted
tools/resolve_btfids: Fix sections with wrong alignment
The data of compressed section should be aligned to 4 (for 32bit) or 8 (for 64 bit) bytes. The binutils ld sets sh_addralign to 1, which makes libelf fail with misaligned section error during the update as reported by Jesper: FAILED elf_update(WRITE): invalid section alignment While waiting for ld fix, we can fix compressed sections sh_addralign value manually. Adding warning in -vv mode when the fix is triggered: $ ./tools/bpf/resolve_btfids/resolve_btfids -vv vmlinux ... section(36) .comment, size 44, link 0, flags 30, type=1 section(37) .debug_aranges, size 45684, link 0, flags 800, type=1 - fixing wrong alignment sh_addralign 16, expected 8 section(38) .debug_info, size 129104957, link 0, flags 800, type=1 - fixing wrong alignment sh_addralign 1, expected 8 section(39) .debug_abbrev, size 1152583, link 0, flags 800, type=1 - fixing wrong alignment sh_addralign 1, expected 8 section(40) .debug_line, size 7374522, link 0, flags 800, type=1 - fixing wrong alignment sh_addralign 1, expected 8 section(41) .debug_frame, size 702463, link 0, flags 800, type=1 section(42) .debug_str, size 1017571, link 0, flags 830, type=1 - fixing wrong alignment sh_addralign 1, expected 8 section(43) .debug_loc, size 3019453, link 0, flags 800, type=1 - fixing wrong alignment sh_addralign 1, expected 8 section(44) .debug_ranges, size 1744583, link 0, flags 800, type=1 - fixing wrong alignment sh_addralign 16, expected 8 section(45) .symtab, size 2955888, link 46, flags 0, type=2 section(46) .strtab, size 2613072, link 0, flags 0, type=3 ... update ok for vmlinux Another workaround is to disable compressed debug info data CONFIG_DEBUG_INFO_COMPRESSED kernel option. Fixes: fbbb68d ("bpf: Add resolve_btfids tool to resolve BTF IDs in ELF object") Reported-by: Jesper Dangaard Brouer <[email protected]> Signed-off-by: Jiri Olsa <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Jesper Dangaard Brouer <[email protected]> Acked-by: Yonghong Song <[email protected]> Cc: Mark Wielaard <[email protected]> Cc: Nick Clifton <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 1e891e5 commit 51f6463

File tree

1 file changed

+36
-0
lines changed
  • tools/bpf/resolve_btfids

1 file changed

+36
-0
lines changed

tools/bpf/resolve_btfids/main.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,39 @@ static struct btf_id *add_symbol(struct rb_root *root, char *name, size_t size)
233233
return btf_id__add(root, id, false);
234234
}
235235

236+
/*
237+
* The data of compressed section should be aligned to 4
238+
* (for 32bit) or 8 (for 64 bit) bytes. The binutils ld
239+
* sets sh_addralign to 1, which makes libelf fail with
240+
* misaligned section error during the update:
241+
* FAILED elf_update(WRITE): invalid section alignment
242+
*
243+
* While waiting for ld fix, we fix the compressed sections
244+
* sh_addralign value manualy.
245+
*/
246+
static int compressed_section_fix(Elf *elf, Elf_Scn *scn, GElf_Shdr *sh)
247+
{
248+
int expected = gelf_getclass(elf) == ELFCLASS32 ? 4 : 8;
249+
250+
if (!(sh->sh_flags & SHF_COMPRESSED))
251+
return 0;
252+
253+
if (sh->sh_addralign == expected)
254+
return 0;
255+
256+
pr_debug2(" - fixing wrong alignment sh_addralign %u, expected %u\n",
257+
sh->sh_addralign, expected);
258+
259+
sh->sh_addralign = expected;
260+
261+
if (gelf_update_shdr(scn, sh) == 0) {
262+
printf("FAILED cannot update section header: %s\n",
263+
elf_errmsg(-1));
264+
return -1;
265+
}
266+
return 0;
267+
}
268+
236269
static int elf_collect(struct object *obj)
237270
{
238271
Elf_Scn *scn = NULL;
@@ -309,6 +342,9 @@ static int elf_collect(struct object *obj)
309342
obj->efile.idlist_shndx = idx;
310343
obj->efile.idlist_addr = sh.sh_addr;
311344
}
345+
346+
if (compressed_section_fix(elf, scn, &sh))
347+
return -1;
312348
}
313349

314350
return 0;

0 commit comments

Comments
 (0)