From nobody Thu May 7 19:06:17 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 84CE6C433FE for ; Fri, 20 May 2022 10:53:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348228AbiETKxr (ORCPT ); Fri, 20 May 2022 06:53:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45942 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237992AbiETKxj (ORCPT ); Fri, 20 May 2022 06:53:39 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 60D8395A0F; Fri, 20 May 2022 03:53:37 -0700 (PDT) Date: Fri, 20 May 2022 10:53:34 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1653044015; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=h898z339vXdaMvDwxvErz5Cx4C9iW5NtRzDH7SfPXTM=; b=p2kYribzlcBJ8yr/BGb+2RbVIVQFhSi47x/0dV3/xDCoJvgZyovSzdhIlSNie+4McMc78u OBQMPqc5Dx1p0AntWy0Qn4HwaS+fcfQv8CXxV8ITbr++bSOvYk1Sj7lW5UhozFdFudWfh1 WO6+01czG2jHGyfwowhDyCAon/g+7uWxZbTN0y9k/gsCqrQ+SAfTNPx+RtffgiGt9qYJRg Cs8zuO86xtwwLbq4ov2SboNaFG3ooGrm0uZcJfytCLoLKeXj6Hl0RszXIo9Sqwyb7znTGs HyNAZkwgHtpRGRqgi2pXVz+cSxkVA2mmeV+Il5wIk12qQg1HP+DMiecFkszg2A== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1653044015; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=h898z339vXdaMvDwxvErz5Cx4C9iW5NtRzDH7SfPXTM=; b=y0jCzaOQXrgxX2ifSyPWlmLErUroByJYRxrbwUETotzyp6lKA6Ut1eEbZjB7K+d80SV9mi AQr52PSmoFlMohAQ== From: "tip-bot2 for Peter Zijlstra" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: objtool/core] objtool: Fix symbol creation Cc: Nathan Chancellor , "Peter Zijlstra (Intel)" , Borislav Petkov , Josh Poimboeuf , , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: References: MIME-Version: 1.0 Message-ID: <165304401416.4207.18016822086620544220.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The following commit has been merged into the objtool/core branch of tip: Commit-ID: ead165fa1042247b033afad7be4be9b815d04ade Gitweb: https://git.kernel.org/tip/ead165fa1042247b033afad7be4be9b81= 5d04ade Author: Peter Zijlstra AuthorDate: Tue, 17 May 2022 17:42:04 +02:00 Committer: Borislav Petkov CommitterDate: Fri, 20 May 2022 12:42:06 +02:00 objtool: Fix symbol creation Nathan reported objtool failing with the following messages: warning: objtool: no non-local symbols !? warning: objtool: gelf_update_symshndx: invalid section index The problem is due to commit 4abff6d48dbc ("objtool: Fix code relocs vs weak symbols") failing to consider the case where an object would have no non-local symbols. The problem that commit tries to address is adding a STB_LOCAL symbol to the symbol table in light of the ELF spec's requirement that: In each symbol table, all symbols with STB_LOCAL binding preced the weak and global symbols. As ``Sections'' above describes, a symbol table section's sh_info section header member holds the symbol table index for the first non-local symbol. The approach taken is to find this first non-local symbol, move that to the end and then re-use the freed spot to insert a new local symbol and increment sh_info. Except it never considered the case of object files without global symbols and got a whole bunch of details wrong -- so many in fact that it is a wonder it ever worked :/ Specifically: - It failed to re-hash the symbol on the new index, so a subsequent find_symbol_by_index() would not find it at the new location and a query for the old location would now return a non-deterministic choice between the old and new symbol. - It failed to appreciate that the GElf wrappers are not a valid disk format (it works because GElf is basically Elf64 and we only support x86_64 atm.) - It failed to fully appreciate how horrible the libelf API really is and got the gelf_update_symshndx() call pretty much completely wrong; with the direct consequence that if inserting a second STB_LOCAL symbol would require moving the same STB_GLOBAL symbol again it would completely come unstuck. Write a new elf_update_symbol() function that wraps all the magic required to update or create a new symbol at a given index. Specifically, gelf_update_sym*() require an @ndx argument that is relative to the @data argument; this means you have to manually iterate the section data descriptor list and update @ndx. Fixes: 4abff6d48dbc ("objtool: Fix code relocs vs weak symbols") Reported-by: Nathan Chancellor Signed-off-by: Peter Zijlstra (Intel) Signed-off-by: Borislav Petkov Acked-by: Josh Poimboeuf Tested-by: Nathan Chancellor Cc: Link: https://lkml.kernel.org/r/YoPCTEYjoPqE4ZxB@hirez.programming.kicks-as= s.net --- tools/objtool/elf.c | 198 ++++++++++++++++++++++++++++--------------- 1 file changed, 129 insertions(+), 69 deletions(-) diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c index 583a3ec..7fb6720 100644 --- a/tools/objtool/elf.c +++ b/tools/objtool/elf.c @@ -374,6 +374,9 @@ static void elf_add_symbol(struct elf *elf, struct symb= ol *sym) struct list_head *entry; struct rb_node *pnode; =20 + INIT_LIST_HEAD(&sym->pv_target); + sym->alias =3D sym; + sym->type =3D GELF_ST_TYPE(sym->sym.st_info); sym->bind =3D GELF_ST_BIND(sym->sym.st_info); =20 @@ -438,8 +441,6 @@ static int read_symbols(struct elf *elf) return -1; } memset(sym, 0, sizeof(*sym)); - INIT_LIST_HEAD(&sym->pv_target); - sym->alias =3D sym; =20 sym->idx =3D i; =20 @@ -603,24 +604,21 @@ static void elf_dirty_reloc_sym(struct elf *elf, stru= ct symbol *sym) } =20 /* - * Move the first global symbol, as per sh_info, into a new, higher symbol - * index. This fees up the shndx for a new local symbol. + * The libelf API is terrible; gelf_update_sym*() takes a data block relat= ive + * index value, *NOT* the symbol index. As such, iterate the data blocks a= nd + * adjust index until it fits. + * + * If no data block is found, allow adding a new data block provided the i= ndex + * is only one past the end. */ -static int elf_move_global_symbol(struct elf *elf, struct section *symtab, - struct section *symtab_shndx) +static int elf_update_symbol(struct elf *elf, struct section *symtab, + struct section *symtab_shndx, struct symbol *sym) { - Elf_Data *data, *shndx_data =3D NULL; - Elf32_Word first_non_local; - struct symbol *sym; - Elf_Scn *s; - - first_non_local =3D symtab->sh.sh_info; - - sym =3D find_symbol_by_index(elf, first_non_local); - if (!sym) { - WARN("no non-local symbols !?"); - return first_non_local; - } + Elf32_Word shndx =3D sym->sec ? sym->sec->idx : SHN_UNDEF; + Elf_Data *symtab_data =3D NULL, *shndx_data =3D NULL; + Elf64_Xword entsize =3D symtab->sh.sh_entsize; + int max_idx, idx =3D sym->idx; + Elf_Scn *s, *t =3D NULL; =20 s =3D elf_getscn(elf->elf, symtab->idx); if (!s) { @@ -628,79 +626,124 @@ static int elf_move_global_symbol(struct elf *elf, s= truct section *symtab, return -1; } =20 - data =3D elf_newdata(s); - if (!data) { - WARN_ELF("elf_newdata"); - return -1; + if (symtab_shndx) { + t =3D elf_getscn(elf->elf, symtab_shndx->idx); + if (!t) { + WARN_ELF("elf_getscn"); + return -1; + } } =20 - data->d_buf =3D &sym->sym; - data->d_size =3D sizeof(sym->sym); - data->d_align =3D 1; - data->d_type =3D ELF_T_SYM; + for (;;) { + /* get next data descriptor for the relevant sections */ + symtab_data =3D elf_getdata(s, symtab_data); + if (t) + shndx_data =3D elf_getdata(t, shndx_data); =20 - sym->idx =3D symtab->sh.sh_size / sizeof(sym->sym); - elf_dirty_reloc_sym(elf, sym); + /* end-of-list */ + if (!symtab_data) { + void *buf; =20 - symtab->sh.sh_info +=3D 1; - symtab->sh.sh_size +=3D data->d_size; - symtab->changed =3D true; + if (idx) { + /* we don't do holes in symbol tables */ + WARN("index out of range"); + return -1; + } =20 - if (symtab_shndx) { - s =3D elf_getscn(elf->elf, symtab_shndx->idx); - if (!s) { - WARN_ELF("elf_getscn"); + /* if @idx =3D=3D 0, it's the next contiguous entry, create it */ + symtab_data =3D elf_newdata(s); + if (t) + shndx_data =3D elf_newdata(t); + + buf =3D calloc(1, entsize); + if (!buf) { + WARN("malloc"); + return -1; + } + + symtab_data->d_buf =3D buf; + symtab_data->d_size =3D entsize; + symtab_data->d_align =3D 1; + symtab_data->d_type =3D ELF_T_SYM; + + symtab->sh.sh_size +=3D entsize; + symtab->changed =3D true; + + if (t) { + shndx_data->d_buf =3D &sym->sec->idx; + shndx_data->d_size =3D sizeof(Elf32_Word); + shndx_data->d_align =3D sizeof(Elf32_Word); + shndx_data->d_type =3D ELF_T_WORD; + + symtab_shndx->sh.sh_size +=3D sizeof(Elf32_Word); + symtab_shndx->changed =3D true; + } + + break; + } + + /* empty blocks should not happen */ + if (!symtab_data->d_size) { + WARN("zero size data"); return -1; } =20 - shndx_data =3D elf_newdata(s); + /* is this the right block? */ + max_idx =3D symtab_data->d_size / entsize; + if (idx < max_idx) + break; + + /* adjust index and try again */ + idx -=3D max_idx; + } + + /* something went side-ways */ + if (idx < 0) { + WARN("negative index"); + return -1; + } + + /* setup extended section index magic and write the symbol */ + if (shndx >=3D SHN_UNDEF && shndx < SHN_LORESERVE) { + sym->sym.st_shndx =3D shndx; + if (!shndx_data) + shndx =3D 0; + } else { + sym->sym.st_shndx =3D SHN_XINDEX; if (!shndx_data) { - WARN_ELF("elf_newshndx_data"); + WARN("no .symtab_shndx"); return -1; } + } =20 - shndx_data->d_buf =3D &sym->sec->idx; - shndx_data->d_size =3D sizeof(Elf32_Word); - shndx_data->d_align =3D 4; - shndx_data->d_type =3D ELF_T_WORD; - - symtab_shndx->sh.sh_size +=3D 4; - symtab_shndx->changed =3D true; + if (!gelf_update_symshndx(symtab_data, shndx_data, idx, &sym->sym, shndx)= ) { + WARN_ELF("gelf_update_symshndx"); + return -1; } =20 - return first_non_local; + return 0; } =20 static struct symbol * elf_create_section_symbol(struct elf *elf, struct section *sec) { struct section *symtab, *symtab_shndx; - Elf_Data *shndx_data =3D NULL; - struct symbol *sym; - Elf32_Word shndx; + Elf32_Word first_non_local, new_idx; + struct symbol *sym, *old; =20 symtab =3D find_section_by_name(elf, ".symtab"); if (symtab) { symtab_shndx =3D find_section_by_name(elf, ".symtab_shndx"); - if (symtab_shndx) - shndx_data =3D symtab_shndx->data; } else { WARN("no .symtab"); return NULL; } =20 - sym =3D malloc(sizeof(*sym)); + sym =3D calloc(1, sizeof(*sym)); if (!sym) { perror("malloc"); return NULL; } - memset(sym, 0, sizeof(*sym)); - - sym->idx =3D elf_move_global_symbol(elf, symtab, symtab_shndx); - if (sym->idx < 0) { - WARN("elf_move_global_symbol"); - return NULL; - } =20 sym->name =3D sec->name; sym->sec =3D sec; @@ -710,24 +753,41 @@ elf_create_section_symbol(struct elf *elf, struct sec= tion *sec) // st_other 0 // st_value 0 // st_size 0 - shndx =3D sec->idx; - if (shndx >=3D SHN_UNDEF && shndx < SHN_LORESERVE) { - sym->sym.st_shndx =3D shndx; - if (!shndx_data) - shndx =3D 0; - } else { - sym->sym.st_shndx =3D SHN_XINDEX; - if (!shndx_data) { - WARN("no .symtab_shndx"); + + /* + * Move the first global symbol, as per sh_info, into a new, higher + * symbol index. This fees up a spot for a new local symbol. + */ + first_non_local =3D symtab->sh.sh_info; + new_idx =3D symtab->sh.sh_size / symtab->sh.sh_entsize; + old =3D find_symbol_by_index(elf, first_non_local); + if (old) { + old->idx =3D new_idx; + + hlist_del(&old->hash); + elf_hash_add(symbol, &old->hash, old->idx); + + elf_dirty_reloc_sym(elf, old); + + if (elf_update_symbol(elf, symtab, symtab_shndx, old)) { + WARN("elf_update_symbol move"); return NULL; } + + new_idx =3D first_non_local; } =20 - if (!gelf_update_symshndx(symtab->data, shndx_data, sym->idx, &sym->sym, = shndx)) { - WARN_ELF("gelf_update_symshndx"); + sym->idx =3D new_idx; + if (elf_update_symbol(elf, symtab, symtab_shndx, sym)) { + WARN("elf_update_symbol"); return NULL; } =20 + /* + * Either way, we added a LOCAL symbol. + */ + symtab->sh.sh_info +=3D 1; + elf_add_symbol(elf, sym); =20 return sym;