Received: by 2002:ac0:da4c:0:0:0:0:0 with SMTP id a12csp1370714imi; Sat, 23 Jul 2022 03:58:33 -0700 (PDT) X-Google-Smtp-Source: AGRyM1v6bPEq8l3rvdONdALcflmOYOmOPG02CJODnDIaVhx7AOCqUC5cvDG/COJ9SR3UhVdPiDXG X-Received: by 2002:a17:907:9710:b0:72b:3271:c9f with SMTP id jg16-20020a170907971000b0072b32710c9fmr3006619ejc.91.1658573913730; Sat, 23 Jul 2022 03:58:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658573913; cv=none; d=google.com; s=arc-20160816; b=Mz86xlnnYWuFL06zgFr0+zuK3v2SEvOmzcLSl2UjCqCFlbaQmW6tz4LqD/1qDVMGlt gE0d9e3b50ry6WB4rBwdQ6jLUxjet9NZ74fgJBUqU5JFoI1zsTyKf+I0CGUN4UM1wU5p Bq3Ov2e62/9/OZcH+7YBSOkFWnabkK0ROjzlZGhk9pPUdiwuTPUHbGK890cc8FPGcokE KwTa/bUrm+oHVW/Ea0OlbvTJd8fZ6lGGMJWQfk45fsgeN0b1Nl6kUd4S2IYOWOvMV0S9 ViDk6Tw9LvlvAKFxzc7E3uIiZPJ+H93UbOQw7Zou6572GChoZJL394hOkHyqdG2zaJgn Nrew== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=IBD/+eHhxnbltyqt+Qc6LA88pnFKJVfg7Nav6bykDlQ=; b=LshVtYBT8JgdhStU4GA8hcRsLNGDnQxhBWif80VyljrnioejCJ4xUlmU45p8BZ/026 gMQlyxuIBaSQW7yAt7N9cF8zO8HYY43jVcgAQVbW9AVB9nvF/TgVPa4lRwgEJQ+Piob/ UhOLtLxG7iqF2oj0ubcaUn9ubmfW8K/K7N6blHQ0qt3IJFG8VE+C+OFbxVQb+92ZQiv/ egOUuPYJHQh2fASFLZv+9cnVhOGlgpYWTUzaXU3p5VD9swJWJz53k+ksl6Mt1Pze8RPc QMI/FtLZ/5sNoXKTwIJZBiPFDNuwRQIAWdA55hVTCV8Fj9R7eZ7P/jj0K37ZdWSjkFzA uUzw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=U2DfzOs+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id cw21-20020a170906479500b006e7fe1e4eb4si8774017ejc.847.2022.07.23.03.58.09; Sat, 23 Jul 2022 03:58:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=U2DfzOs+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237942AbiGWKBB (ORCPT + 99 others); Sat, 23 Jul 2022 06:01:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46868 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237838AbiGWKAI (ORCPT ); Sat, 23 Jul 2022 06:00:08 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7F76E76E85; Sat, 23 Jul 2022 02:58:19 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id BA52E611CD; Sat, 23 Jul 2022 09:58:18 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C9E20C341CA; Sat, 23 Jul 2022 09:58:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1658570298; bh=OPAVYdq/Nb9TUWPRrzva7ewq9dIjGGbmoWDHPMaNNOQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=U2DfzOs+JrKeusYswmHCCga+9HJDJ41zIxYDcNx1mYN9OFVDrk1vfp2X51aZIgZu/ KpbIIjKFaAoVls0tJomRDSoAZQ2vCKZRbgzCsnTaCtiIAaMKehZpcGAf1Gk4QcsZRu puWU3IvU7eFxcsVHZhLMwiGPrhc6eBAtq/+7lqP0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Nick Desaulniers , Fangrui Song , "Peter Zijlstra (Intel)" , Ben Hutchings Subject: [PATCH 5.10 037/148] objtool: Fix .symtab_shndx handling for elf_create_undef_symbol() Date: Sat, 23 Jul 2022 11:54:09 +0200 Message-Id: <20220723095234.704707161@linuxfoundation.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220723095224.302504400@linuxfoundation.org> References: <20220723095224.302504400@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Peter Zijlstra commit 584fd3b31889852d0d6f3dd1e3d8e9619b660d2c upstream. When an ELF object uses extended symbol section indexes (IOW it has a .symtab_shndx section), these must be kept in sync with the regular symbol table (.symtab). So for every new symbol we emit, make sure to also emit a .symtab_shndx value to keep the arrays of equal size. Note: since we're writing an UNDEF symbol, most GElf_Sym fields will be 0 and we can repurpose one (st_size) to host the 0 for the xshndx value. Fixes: 2f2f7e47f052 ("objtool: Add elf_create_undef_symbol()") Reported-by: Nick Desaulniers Suggested-by: Fangrui Song Signed-off-by: Peter Zijlstra (Intel) Tested-by: Nick Desaulniers Link: https://lkml.kernel.org/r/YL3q1qFO9QIRL/BA@hirez.programming.kicks-ass.net Signed-off-by: Ben Hutchings Signed-off-by: Greg Kroah-Hartman --- tools/objtool/elf.c | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) --- a/tools/objtool/elf.c +++ b/tools/objtool/elf.c @@ -768,7 +768,7 @@ static int elf_add_string(struct elf *el struct symbol *elf_create_undef_symbol(struct elf *elf, const char *name) { - struct section *symtab; + struct section *symtab, *symtab_shndx; struct symbol *sym; Elf_Data *data; Elf_Scn *s; @@ -819,6 +819,29 @@ struct symbol *elf_create_undef_symbol(s symtab->len += data->d_size; symtab->changed = true; + symtab_shndx = find_section_by_name(elf, ".symtab_shndx"); + if (symtab_shndx) { + s = elf_getscn(elf->elf, symtab_shndx->idx); + if (!s) { + WARN_ELF("elf_getscn"); + return NULL; + } + + data = elf_newdata(s); + if (!data) { + WARN_ELF("elf_newdata"); + return NULL; + } + + data->d_buf = &sym->sym.st_size; /* conveniently 0 */ + data->d_size = sizeof(Elf32_Word); + data->d_align = 4; + data->d_type = ELF_T_WORD; + + symtab_shndx->len += 4; + symtab_shndx->changed = true; + } + sym->sec = find_section_by_index(elf, 0); elf_add_symbol(elf, sym);