Received: by 2002:a25:683:0:0:0:0:0 with SMTP id 125csp521533ybg; Fri, 12 Jun 2020 07:41:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy+k3yn/NaBm+IY1PMYGrHZLm0XPM3AcZWSvmErHo94T4j3dDUDugK+Abs03+dC2caxN/rh X-Received: by 2002:aa7:d4c5:: with SMTP id t5mr11601733edr.357.1591972874840; Fri, 12 Jun 2020 07:41:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1591972874; cv=none; d=google.com; s=arc-20160816; b=Hev49HcrHUko4haYQ7/OMuqbuYE0n6GkNyDZT1pxCNY2ovErXshmZx878Y4O9rRJkF ez5lyNm1mICBZaCsqzAKujeOYbDWuzypE9RZRwkPu+6mPyU2RJ0LgR+o0+VmLIK2mXMO 2leQ+53fCj81sEApqo0nzhJe+4eSrKVrc5maDrMFZ6bA2gpC1gtMmOV3CvZbgM6nL/2j lMpKsVsAzQBANvEo05t/nmJRDPyjxT9ucyGPXfueDQRf21D4WLY/tnLt0SYUkUh/Vixa oMOxxjpyE2rOc3oF4+NzOURG9nO0H+RiABgT/6lJgrtHctQ3V9BcPDkfNrZTuzUe4SLu uT5A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:subject:cc:to :from:date:user-agent:message-id:dkim-signature; bh=l+xbgdcfE1SSxQ3zHr1yPabOzpbvtiXlQZDA6pZzvco=; b=j8tLOyVKCM7BsFOPPqYN2zk9XppIac7NyfxmhxAwjUSLe+0kNyA+JgH4QAHymFE6TA agQWNEARWJ47wTEd1zURf7m8kiEa+Dbmtf9PBHZgV3GQ6/tmQpa/jQYqcvWOPMFVUXrl EZVU8gkcs9T7tjhg5/ItdGHWG/LgcIbAHrfleZff6bwRXNW17ZunUDu4m5tSwBAWBmhp XtG4BQA1+n8fqFvpdRKMGpe38tPeFYLjCYqcBWo+0Yn1s+fv1MC22AhcHyuzC8FYdSas nUeAbiPMq/F4a0xk4bMuR+FX/NS2ZrjwzabybMiR+x5X2YKP/4zjsry9vy73hqt0/eW/ Sy6A== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=merlin.20170209 header.b=zqy3GTt5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id m10si3521084edv.322.2020.06.12.07.40.51; Fri, 12 Jun 2020 07:41:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=fail header.i=@infradead.org header.s=merlin.20170209 header.b=zqy3GTt5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726455AbgFLOiJ (ORCPT + 99 others); Fri, 12 Jun 2020 10:38:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57282 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726089AbgFLOiI (ORCPT ); Fri, 12 Jun 2020 10:38:08 -0400 Received: from merlin.infradead.org (unknown [IPv6:2001:8b0:10b:1231::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 52FB1C03E96F for ; Fri, 12 Jun 2020 07:38:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=merlin.20170209; h=Content-Type:MIME-Version:References: Subject:Cc:To:From:Date:Message-ID:Sender:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:In-Reply-To; bh=l+xbgdcfE1SSxQ3zHr1yPabOzpbvtiXlQZDA6pZzvco=; b=zqy3GTt5a9A1vSfatyBluqE+A9 KwCTh3tKztu71v1FEAdM0LlpdjZGzbWfLAVAfegOajiHeE4YsSVHfbb+8yT1W6FqwcgW5YIR2A7zg YR0rs88zUC6bpT7zYLTkgYDigj0jLmMj9WNJDzNOiyMugECUWEi639NP8Da6W307RILMUuWDycHyA TOvCawCIFqKVT9mqPnmYXsj9jPVmhv1yqhI116qBiE+jGlaDy4ODRpxyh0mhnqLbuukAvV/SgOUqU UMNC7IbDSY74mNE6EsADNHPEir4ZlppEgXosb1wwII35ukM9t1q6vTedaNYHvCnoXzY25swZSt9kG bdfliU1w==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=noisy.programming.kicks-ass.net) by merlin.infradead.org with esmtpsa (Exim 4.92.3 #3 (Red Hat Linux)) id 1jjkoI-0005Or-Lv; Fri, 12 Jun 2020 14:37:35 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id E2E34300F28; Fri, 12 Jun 2020 16:37:31 +0200 (CEST) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 0) id C5E352389EB9F; Fri, 12 Jun 2020 16:37:31 +0200 (CEST) Message-ID: <20200612143554.012293047@infradead.org> User-Agent: quilt/0.66 Date: Fri, 12 Jun 2020 16:30:36 +0200 From: Peter Zijlstra To: Josh Poimboeuf Cc: linux-kernel@vger.kernel.org, x86@kernel.org, dvyukov@google.com, elver@google.com, andreyknvl@google.com, mark.rutland@arm.com, mhelsley@vmware.com, rostedt@goodmis.org, jthierry@redhat.com, mbenes@suse.cz, peterz@infradead.org Subject: [RFC][PATCH 2/3] objtool: Provide elf_write_{insn,reloc}() References: <20200612143034.933422660@infradead.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This provides infrastructure to rewrite instructions; this is immediately useful for helping out with KCOV-vs-noinstr, but will also come in handy for a bunch of variable sized jump-label patches that are still on ice. Signed-off-by: Peter Zijlstra (Intel) --- tools/objtool/elf.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- tools/objtool/elf.h | 7 ++++++- 2 files changed, 57 insertions(+), 2 deletions(-) --- a/tools/objtool/elf.c +++ b/tools/objtool/elf.c @@ -562,8 +562,9 @@ static int read_relocs(struct elf *elf) return -1; } - reloc->sym = find_symbol_by_index(elf, symndx); reloc->sec = sec; + reloc->idx = i; + reloc->sym = find_symbol_by_index(elf, symndx); if (!reloc->sym) { WARN("can't find reloc entry symbol %d for %s", symndx, sec->name); @@ -848,6 +849,55 @@ static int elf_rebuild_rela_section(stru return 0; } + +int elf_write_insn(struct elf *elf, struct section *sec, + unsigned long offset, unsigned int len, + const char *insn) +{ + Elf_Data *data = sec->data; + + if (data->d_type != ELF_T_BYTE || data->d_off) { + WARN("write to unexpected data for section: %s", sec->name); + return -1; + } + + memcpy(data->d_buf + offset, insn, len); + elf_flagdata(data, ELF_C_SET, ELF_F_DIRTY); + + sec->changed = true; + elf->changed = true; + + return 0; +} + +int elf_write_reloc(struct elf *elf, struct reloc *reloc) +{ + struct section *sec = reloc->sec; + + if (sec->sh.sh_type == SHT_REL) { + reloc->rel.r_info = GELF_R_INFO(reloc->sym->idx, reloc->type); + reloc->rel.r_offset = reloc->offset; + + if (!gelf_update_rel(sec->data, reloc->idx, &reloc->rel)) { + WARN_ELF("gelf_update_rel"); + return -1; + } + } else { + reloc->rela.r_info = GELF_R_INFO(reloc->sym->idx, reloc->type); + reloc->rela.r_addend = reloc->addend; + reloc->rela.r_offset = reloc->offset; + + if (!gelf_update_rela(sec->data, reloc->idx, &reloc->rela)) { + WARN_ELF("gelf_update_rela"); + return -1; + } + } + + sec->changed = true; + elf->changed = true; + + return 0; +} int elf_rebuild_reloc_section(struct section *sec) { --- a/tools/objtool/elf.h +++ b/tools/objtool/elf.h @@ -67,9 +67,10 @@ struct reloc { }; struct section *sec; struct symbol *sym; - unsigned int type; unsigned long offset; + unsigned int type; int addend; + int idx; bool jump_table_start; }; @@ -122,6 +123,10 @@ struct elf *elf_open_read(const char *na struct section *elf_create_section(struct elf *elf, const char *name, size_t entsize, int nr); struct section *elf_create_reloc_section(struct elf *elf, struct section *base, int reltype); void elf_add_reloc(struct elf *elf, struct reloc *reloc); +int elf_write_insn(struct elf *elf, struct section *sec, + unsigned long offset, unsigned int len, + const char *insn); +int elf_write_reloc(struct elf *elf, struct reloc *reloc); int elf_write(struct elf *elf); void elf_close(struct elf *elf);