Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp812778ybk; Wed, 13 May 2020 13:52:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzj0fLK4Wqiz6XxhFRb2u6mTCaILSA+DUZr493h/bllY16pbjam791N5iP2KcGaz8Fh5SnJ X-Received: by 2002:a50:eb0a:: with SMTP id y10mr1349495edp.312.1589403134091; Wed, 13 May 2020 13:52:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589403134; cv=none; d=google.com; s=arc-20160816; b=spiOQ7NPhq8+BT4EMoYz4xjA9+/ok8+9zpSmOYhjJFJK9sYUvo5bBqZmOuA7WiTvmH fzF2AptsSrqS59mlTPQd5+Hl8ET8Y6r5DHbKkmmcZuu99Y5V7VjvogB1JSZ/cf6klE51 xKuKWxyiDGCuES0YP/GssfnhuPCREmdkyTnQ8HWpUEfry5DJ1HasNT4IzTYsxXP+3r6V Oa4TOqwqh9BcyDhpyKq+p1hky3YLvNbFrjy29tOE8O5cfaMNVp87LvhdtVfKsiKKGG+V +Fkkoq31ZaSV5OSnv9Es4ZySTRAJgRc8ToXxAvZGSVuzD4lUEIgAToKIevhL/Bk8abjh HwtQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:mail-followup-to:message-id:subject:cc:to :from:date; bh=NtRkMIDISSqbiywjD7hT0nlRf+qfcCxv0FEn+hBv1uI=; b=WvKYdnPYYJSrp1LvbL0EYFwwYDEWgmOEDtds8oU+xdb6YlxRB01NCe5q6IHjl9KJmZ wqRJTBGMR9vV0ccshU89PNmhyOfyBXdJmvpQd978Mag5Xc6OGhTgUtNutrxRnzNrEpPM YXm45Z7DGIj43a4xKTGlhFb/KSgxhORWsy7OUiRkF1JAZKZGxz7L/QQsnr9NMgK+ttwv PKGncNDlQDRVBiBOaJrw6J2iIUC7oV/RxeKZx+eqLql8CWRnQemZNY2idk8F2yGGmG4U B/tMfNuZX9DVfMAQQOaTkEDJb+mWMZK107dpik1PUJbltM4NojmoetQvtTpgYWUNbnvw /a/w== ARC-Authentication-Results: i=1; mx.google.com; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=vmware.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id me26si516710ejb.309.2020.05.13.13.51.51; Wed, 13 May 2020 13:52: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; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=vmware.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389613AbgEMQ0t (ORCPT + 99 others); Wed, 13 May 2020 12:26:49 -0400 Received: from ex13-edg-ou-002.vmware.com ([208.91.0.190]:9840 "EHLO EX13-EDG-OU-002.vmware.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730831AbgEMQ0t (ORCPT ); Wed, 13 May 2020 12:26:49 -0400 Received: from sc9-mailhost3.vmware.com (10.113.161.73) by EX13-EDG-OU-002.vmware.com (10.113.208.156) with Microsoft SMTP Server id 15.0.1156.6; Wed, 13 May 2020 09:26:46 -0700 Received: from localhost (unknown [10.166.66.23]) by sc9-mailhost3.vmware.com (Postfix) with ESMTP id DC08740E40; Wed, 13 May 2020 09:26:48 -0700 (PDT) Date: Wed, 13 May 2020 09:26:48 -0700 From: Matt Helsley To: Julien Thierry CC: , Josh Poimboeuf , Peter Zijlstra , Miroslav Benes , Steven Rostedt Subject: Re: [RFC][PATCH 3/5] objtool: Add support for relocations without addends Message-ID: <20200513162648.GJ9040@rlwimi.vmware.com> Mail-Followup-To: Matt Helsley , Julien Thierry , linux-kernel@vger.kernel.org, Josh Poimboeuf , Peter Zijlstra , Miroslav Benes , Steven Rostedt References: <17ee3f6f2a246008aaae70f92df24ae92fa0e21e.1588888003.git.mhelsley@vmware.com> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Disposition: inline In-Reply-To: Received-SPF: None (EX13-EDG-OU-002.vmware.com: mhelsley@vmware.com does not designate permitted sender hosts) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, May 12, 2020 at 06:04:50PM +0100, Julien Thierry wrote: > Hi Matt, > > On 5/11/20 6:35 PM, Matt Helsley wrote: > > Currently objtool only collects information about relocations with > > addends. In recordmcount, which we are about to merge into objtool, > > some supported architectures do not use rela relocations. Since > > object files use one or the other the list can be reused. > > > > Signed-off-by: Matt Helsley > > --- > > tools/objtool/elf.c | 55 ++++++++++++++++++++++++++++++++++++--------- > > tools/objtool/elf.h | 5 ++++- > > 2 files changed, 49 insertions(+), 11 deletions(-) > > > > diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c > > index c4857fa3f1d1..cd841e3df87d 100644 > > --- a/tools/objtool/elf.c > > +++ b/tools/objtool/elf.c > > @@ -465,12 +465,14 @@ static int read_relas(struct elf *elf) > > unsigned long nr_rela, max_rela = 0, tot_rela = 0; > > list_for_each_entry(sec, &elf->sections, list) { > > - if (sec->sh.sh_type != SHT_RELA) > > + if ((sec->sh.sh_type != SHT_RELA) && > > + (sec->sh.sh_type != SHT_REL)) > > continue; > > - sec->base = find_section_by_name(elf, sec->name + 5); > > + sec->base = find_section_by_name(elf, sec->name + > > + ((sec->sh.sh_type != SHT_REL) ? 5 : 4)); > > if (!sec->base) { > > - WARN("can't find base section for rela section %s", > > + WARN("can't find base section for relocation section %s", > > sec->name); > > return -1; > > } > > @@ -486,13 +488,26 @@ static int read_relas(struct elf *elf) > > } > > memset(rela, 0, sizeof(*rela)); > > - if (!gelf_getrela(sec->data, i, &rela->rela)) { > > - WARN_ELF("gelf_getrela"); > > - return -1; > > + switch(sec->sh.sh_type) { > > + case SHT_REL: > > + if (!gelf_getrel(sec->data, i, &rela->rel)) { > > + WARN_ELF("gelf_getrel"); > > + return -1; > > + } > > + rela->addend = 0; > > + break; > > + case SHT_RELA: > > + if (!gelf_getrela(sec->data, i, &rela->rela)) { > > + WARN_ELF("gelf_getrela"); > > + return -1; > > + } > > + rela->addend = rela->rela.r_addend; > > + break; > > + default: > > + break; > > } > > rela->type = GELF_R_TYPE(rela->rela.r_info); > > - rela->addend = rela->rela.r_addend; > > rela->offset = rela->rela.r_offset; > > symndx = GELF_R_SYM(rela->rela.r_info); > > rela->sym = find_symbol_by_index(elf, symndx); > > @@ -717,17 +732,27 @@ int elf_rebuild_rela_section(struct section *sec) > > struct rela *rela; > > int nr, idx = 0, size; > > GElf_Rela *relas; > > + GElf_Rel *rels; > > nr = 0; > > list_for_each_entry(rela, &sec->rela_list, list) > > nr++; > > + /* > > + * Allocate a buffer for relocations with addends but also use > > + * it for other relocations too. The section type determines > > + * the size of the section, the buffer used, and the entries. > > + */ > > size = nr * sizeof(*relas); > > relas = malloc(size); > > if (!relas) { > > perror("malloc"); > > return -1; > > } > > + rels = (void *)relas; > > + if (sec->sh.sh_type == SHT_REL) { > > + size = nr * sizeof(*rels); > > + } > > This looks a bit error prone to me. > > What about having: > > void *rel_buf; > [...] > size = nr * (sec->sh.sh_type == SHT_REL ? sizeof(GElf_Rel) : > sizeof(GElf_Rela)); I like reducing to a single size expression but I'm not a fan of hard-coding the GElf_Rel[a] types here -- I prefer sizeof(*relas) and sizeof(*rels) since that makes it clear the sizes will match the types of the pointers that will be used to access them. So I've changed it to: size = nr * ((sec->sh.sh_type == SHT_REL) ? sizeof(*rels) : sizeof(*relas)); > rel_buf = malloc(size); > [...] > > And then casting rel_buf to the correct pointer type in the fitting switch > cases? I'm thinking it's simpler with fewer variables. I don't think moving the cast into the switch cases makes it any clearer. It's also odd because we'll keep re-initializing relas or rels to rel_buf each loop iteration. Finally, this approach has the advantage that, when reviewing the patch, it's clear that the original code handling RELA relocation entries isn't changing -- you can see it's just shifting into one of the cases (below). Do you still prefer introducing rel_buf? > > > sec->data->d_buf = relas; > > sec->data->d_size = size; > > @@ -736,9 +761,19 @@ int elf_rebuild_rela_section(struct section *sec) > > idx = 0; > > list_for_each_entry(rela, &sec->rela_list, list) { > > - relas[idx].r_offset = rela->offset; > > - relas[idx].r_addend = rela->addend; > > - relas[idx].r_info = GELF_R_INFO(rela->sym->idx, rela->type); > > + switch(sec->sh.sh_type) { > > + case SHT_REL: > > + rels[idx].r_offset = rela->offset; > > + rels[idx].r_info = GELF_R_INFO(rela->sym->idx, rela->type); > > + break; > > + case SHT_RELA: > > + relas[idx].r_addend = rela->addend; > > + relas[idx].r_offset = rela->offset; > > + relas[idx].r_info = GELF_R_INFO(rela->sym->idx, rela->type); > > + break; > > + default: > > + break; > > + } > > idx++; > > } Cheers, -Matt Helsley