Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp5223imw; Tue, 12 Jul 2022 13:24:35 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vLYWsgPHxsFzO3fwA3YQc5RNb4eQl8dnCAu28qIiuWbvJPYYZ6AgSEc0mOYQje1FAFuc4Q X-Received: by 2002:a17:907:e87:b0:72b:4a74:e30b with SMTP id ho7-20020a1709070e8700b0072b4a74e30bmr15520664ejc.222.1657656938579; Tue, 12 Jul 2022 13:15:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657656938; cv=none; d=google.com; s=arc-20160816; b=oPrBenUsBCtY2RK0DO6AX/eRJC+iNPMq6IEM9oj5SH1dtn8AYYjsejJTrADIpkN1za Q/J3wCTFNUzbOKwTOB1nOawGJI+eCisatL2S/OtX4rHFG68SQRuv2jJbY37CyJLgzqVK 6ZxvLWEfM0Odjiy0xK088S90ngwtMuWE0xvie45GcdJFHNBJibiRNVCs95h/wah8NAXU kyN3ABlRieF6F64uPSCrh/AhEyfFT3D8SdFq5MOvdqvDeA1qSI8Hu+xeQknc1V1j4tOm GFhRRdu2fluD/x5pXnwhAaPC0uVaqe55J7zBaRGpygZi5eDsRK8C5tLQSW6CutTzK1Ky sVNQ== 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=oKWl7Kw6eHh4vm+ZxPlhb7asDJgP/B5nqW6meWgybEo=; b=S2zxKivzeuceDy1lF2FmuxD+nIUM2QRS8ozxRMs//E5IHqP96wN36Gp9ZewM5t0uXS TtZBK563FhPYwtaoH73s2X8wbO2FHhUY4pXs68ORVEXva2pUmwYwqBiGKv/vWm/WmpID BdSJ0zMQXg9X2AKy6EOYtTCiSWqgYlYStEdClL5NM5fXDn9ifVJmK0SAtWnmWFmoTdpC oEEjkVptDZO7MqiSTwg6cBNodf8M6QxYRpyl3rFuV6FZ6XnvkV6/3ID1zhmVcvIN/GlG StmVPW7KirsyvGxhmfU7bnWUyUhvQtMEoWNfiJlVxMteTrcvRVv5GKbtmigJW8s6RHiG mm3A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=woq1JB9Q; 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 dz13-20020a0564021d4d00b0043ac3e75000si17195191edb.412.2022.07.12.13.15.09; Tue, 12 Jul 2022 13:15:38 -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=woq1JB9Q; 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 S230198AbiGLSmk (ORCPT + 99 others); Tue, 12 Jul 2022 14:42:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33376 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233825AbiGLSmG (ORCPT ); Tue, 12 Jul 2022 14:42:06 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 914ABD7B93; Tue, 12 Jul 2022 11:41:30 -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 ams.source.kernel.org (Postfix) with ESMTPS id 22932B81BAB; Tue, 12 Jul 2022 18:41:29 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4CE72C3411E; Tue, 12 Jul 2022 18:41:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1657651287; bh=VvU/1RP84JrPAl0/5+ZpTqj7LWQ1cv8/7kvVC/IdFmQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=woq1JB9Q6t/3oVfIgbNtDH6jGIN0ArYpaL2RMXx4N8eOnCUh7X9S5Va4oNoTmwPpv 4bOoVYKkjzZr5PzlL3msp+pi6sd8kMJrJQSngEu46wtJTQZuk4ASWUSrTj4UGfVVNx Ca+Mkk+WKi+a+puKl2JzeZTh/2gRSKVdwxXcHXL8= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, "Peter Zijlstra (Intel)" , Borislav Petkov , Ingo Molnar , Miroslav Benes , Ben Hutchings Subject: [PATCH 5.10 030/130] objtool: Add elf_create_undef_symbol() Date: Tue, 12 Jul 2022 20:37:56 +0200 Message-Id: <20220712183247.779099232@linuxfoundation.org> X-Mailer: git-send-email 2.37.0 In-Reply-To: <20220712183246.394947160@linuxfoundation.org> References: <20220712183246.394947160@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.7 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,T_SCC_BODY_TEXT_LINE 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 2f2f7e47f0525cbaad5dd9675fd9d8aa8da12046 upstream. Allow objtool to create undefined symbols; this allows creating relocations to symbols not currently in the symbol table. Signed-off-by: Peter Zijlstra (Intel) Signed-off-by: Borislav Petkov Signed-off-by: Ingo Molnar Reviewed-by: Miroslav Benes Link: https://lkml.kernel.org/r/20210326151300.064743095@infradead.org Signed-off-by: Ben Hutchings Signed-off-by: Greg Kroah-Hartman --- tools/objtool/elf.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++ tools/objtool/elf.h | 1 2 files changed, 61 insertions(+) --- a/tools/objtool/elf.c +++ b/tools/objtool/elf.c @@ -766,6 +766,66 @@ static int elf_add_string(struct elf *el return len; } +struct symbol *elf_create_undef_symbol(struct elf *elf, const char *name) +{ + struct section *symtab; + struct symbol *sym; + Elf_Data *data; + Elf_Scn *s; + + sym = malloc(sizeof(*sym)); + if (!sym) { + perror("malloc"); + return NULL; + } + memset(sym, 0, sizeof(*sym)); + + sym->name = strdup(name); + + sym->sym.st_name = elf_add_string(elf, NULL, sym->name); + if (sym->sym.st_name == -1) + return NULL; + + sym->sym.st_info = GELF_ST_INFO(STB_GLOBAL, STT_NOTYPE); + // st_other 0 + // st_shndx 0 + // st_value 0 + // st_size 0 + + symtab = find_section_by_name(elf, ".symtab"); + if (!symtab) { + WARN("can't find .symtab"); + return NULL; + } + + s = elf_getscn(elf->elf, symtab->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; + data->d_size = sizeof(sym->sym); + data->d_align = 1; + + sym->idx = symtab->len / sizeof(sym->sym); + + symtab->len += data->d_size; + symtab->changed = true; + + sym->sec = find_section_by_index(elf, 0); + + elf_add_symbol(elf, sym); + + return sym; +} + struct section *elf_create_section(struct elf *elf, const char *name, unsigned int sh_flags, size_t entsize, int nr) { --- a/tools/objtool/elf.h +++ b/tools/objtool/elf.h @@ -133,6 +133,7 @@ int elf_write_insn(struct elf *elf, stru unsigned long offset, unsigned int len, const char *insn); int elf_write_reloc(struct elf *elf, struct reloc *reloc); +struct symbol *elf_create_undef_symbol(struct elf *elf, const char *name); int elf_write(struct elf *elf); void elf_close(struct elf *elf);