Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp2872603ybk; Tue, 12 May 2020 10:07:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzC09UZWMiExp2ChBLEeuDgmE7zb5J89YLZP+Jt8WuX5Zs8P61NZVX2rGGaBRtqecjH1ynZ X-Received: by 2002:aa7:cd7b:: with SMTP id ca27mr10359755edb.51.1589303258095; Tue, 12 May 2020 10:07:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589303258; cv=none; d=google.com; s=arc-20160816; b=K3WRAX3D8NyRgc1E7lkbS8q7/VZpVgB36ddSdEfUnt32JphTilZjAzm83+oo/NTtpn w8YwVwT1lkM1gFv96uIYdqFPOB1z6557DMWzOsyUjshuSd7SVdNmAOQ4H8NhrusKIetY 8ErCDuXAw4ITOBCr6Vs/pDtcTPfw7BYeexjUUqu/Ev86T6JlO9TEOtB5GSPZV6mGyEvx pYv2YPMm0L0zjGJ7Y3MCuPwnKgKra5k+y7NW3FHrwHoTcrIdf/h8+k2WWespTeXQAH/Y OKciLkj6ou3epk7B6KSyt2E/42fMd7ieDVs9LwvPzGmwkcVarjuRv0F9PDrodLkEMkWu q/vQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:dkim-signature; bh=NLHEG7EdM8b0tl+foDcZcXI3wJwu5RrDTNPT3yWr9qc=; b=bAiRyVkyKm3uW3KsTWLYlxOg6YEvD7ox0cholaGYy75pdXfXA9/kOs1um5e/2a3l42 7bSIDDCt8rQ+hfUxVFbe2uxGDYwix8HXR7u8ArkfugWb+IeVxwXkF5axvuABi69k18eX 5f3G5N4ytnVmLNaH9tbZQBm3OlcahlJ0rCd/r9LgVXjTPWOcxqHItArMOLjNpxY8Jbtk VcstMNQfU/obYg6VznMSuEJOxoQ5C0c4IdsecBggLF9yCUO6MUt0a8YRFfP+7mDZ7Nuw jn6885pge9CBfU7FmQ4KUdSy4pMtqAdUMle6t42q6f6A/btZmpDQo0gN50VJtdHc1Fk4 uL/w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="Fpz/s2Sy"; 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=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id m20si4574792ejr.172.2020.05.12.10.07.09; Tue, 12 May 2020 10:07:38 -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=pass header.i=@redhat.com header.s=mimecast20190719 header.b="Fpz/s2Sy"; 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=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729416AbgELRFB (ORCPT + 99 others); Tue, 12 May 2020 13:05:01 -0400 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:30463 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726465AbgELRFB (ORCPT ); Tue, 12 May 2020 13:05:01 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1589303099; h=from:from: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=NLHEG7EdM8b0tl+foDcZcXI3wJwu5RrDTNPT3yWr9qc=; b=Fpz/s2SyxALH/oz8Oh4wOfxiLOobsEpwCYd08i/BdCXtoN7JT2PDZLPgorg1yzWG4tgtnw 4b7QAINgI7Auh5VQLCa0P95XrBH26e4VLkyFS3z0sXNbiOz+I2qBEuixk5Er8cFUtvnEQs eMTYyweDeSpVxpw8L1tbLl7O0LlHMCI= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-204-tSk2wHEfMfG2YJ1Afgc1Fg-1; Tue, 12 May 2020 13:04:58 -0400 X-MC-Unique: tSk2wHEfMfG2YJ1Afgc1Fg-1 Received: by mail-wr1-f70.google.com with SMTP id x8so7219530wrl.16 for ; Tue, 12 May 2020 10:04:57 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=NLHEG7EdM8b0tl+foDcZcXI3wJwu5RrDTNPT3yWr9qc=; b=T/UbPkYIDYr1PvxAEp7P/9WtVSBbXdhDo8dp9KbRWaOEz4X66TKPdnujs0CniHU9W4 ZC+dYleAGNY28JOml435YLoddkX7Yjp9b7iDVzv3ht2PEPtxCMhq0NZtcDK8T3EvAKr6 AITkEffGOCT4Tep2215vIPSpNRk67w6ZC1lsCqW0VK9Pbdv0+pOW05pyB86si3cGEGg8 /Bt/zG716jnAkKteca1r3+Sn2o/Y0Ln6tEnIfLmdco/mOc6Y1enP7kH4qo8lKdT8/Cd7 k1V+0n/jQnElMvoygnHXn2cXRvNmeCKLDAObSgT+13ApHDiyzBFTOpUDLjJ3Obd6atPE JcCw== X-Gm-Message-State: AGi0Pubk7XO7avyoXmyDE0JKjx8Ib/at/fKZfKjaT+GScMD/ueELTYaj MZxSsPOSqcQ2bNxS1EcHkJLYAsTwA2lh8HLMsUUE5cvvL6D0bwwfh5B2VgThR0TRwQ30xpn5DYN pHpRPI2i6y4ECuN3ahdgbDocg X-Received: by 2002:a1c:a557:: with SMTP id o84mr18315898wme.165.1589303093649; Tue, 12 May 2020 10:04:53 -0700 (PDT) X-Received: by 2002:a1c:a557:: with SMTP id o84mr18315879wme.165.1589303093421; Tue, 12 May 2020 10:04:53 -0700 (PDT) Received: from ?IPv6:2a01:cb14:58d:8400:ecf6:58e2:9c06:a308? ([2a01:cb14:58d:8400:ecf6:58e2:9c06:a308]) by smtp.gmail.com with ESMTPSA id n13sm3379791wrs.2.2020.05.12.10.04.52 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 12 May 2020 10:04:52 -0700 (PDT) Subject: Re: [RFC][PATCH 3/5] objtool: Add support for relocations without addends To: Matt Helsley , linux-kernel@vger.kernel.org Cc: Josh Poimboeuf , Peter Zijlstra , Miroslav Benes , Steven Rostedt References: <17ee3f6f2a246008aaae70f92df24ae92fa0e21e.1588888003.git.mhelsley@vmware.com> From: Julien Thierry Message-ID: Date: Tue, 12 May 2020 18:04:50 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.5.0 MIME-Version: 1.0 In-Reply-To: <17ee3f6f2a246008aaae70f92df24ae92fa0e21e.1588888003.git.mhelsley@vmware.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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)); rel_buf = malloc(size); [...] And then casting rel_buf to the correct pointer type in the fitting switch cases? > > 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++; > } > > diff --git a/tools/objtool/elf.h b/tools/objtool/elf.h > index 0b79c2353a21..71bd47055963 100644 > --- a/tools/objtool/elf.h > +++ b/tools/objtool/elf.h > @@ -61,7 +61,10 @@ struct symbol { > struct rela { > struct list_head list; > struct hlist_node hash; > - GElf_Rela rela; > + union { > + GElf_Rela rela; > + GElf_Rel rel; > + }; > struct section *sec; > struct symbol *sym; > unsigned int type; > -- Julien Thierry