Received: by 2002:a25:ef43:0:0:0:0:0 with SMTP id w3csp959964ybm; Wed, 27 May 2020 12:13:43 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxLpEsHMenTP/7Fql+dCDhdPVNERmvlfrcnwVldTcKTenILcqDKITNx8YLGKJMZFfO4ZIS3 X-Received: by 2002:a17:906:144e:: with SMTP id q14mr7472611ejc.450.1590606823081; Wed, 27 May 2020 12:13:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1590606823; cv=none; d=google.com; s=arc-20160816; b=AaQf46iQsuwRFRe+r1iCJ9fWT5I3cDUOuQFh26dwlplfWbVQdFw2FYBiMhhfzih3uc 78tt0bEb7mjNXL5/rWJ2KWh+5vEdDx2jmyqTnhkD/DGZurzECKcRgMkxvXibtilXW82m nOquA9EJ3RdjN9IQKoTvpDGuwdHnk6iNZTPZF/p2Mm8f+8jU+43P2I2rLrNIpOl5kf8l UnbPQCGxdWl4llDbsn48AsVMbeYl4uWf0vrDzCMQJQ73nyEzh94JMJMS6/r77LHc/FnZ zZJbpV46uxb2TbaMMhFQRA5uSEh62OMjNATTcf8xBi2+oC/Xg5ONOZRzKqQg6d1Fwop2 l6+Q== 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:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=7Yx0CUGljOfWBDZHvMPMZ2MrHD1PSuQMyv5eRlWQ9IM=; b=aJ0galbXT1iNKbfWfAC6dQDuSfW+x1aEtuSGRoi0e7W+ZuPEnltPuiO3n8op/yFXh+ j+s3XsamEKGyEdqduI2ltLwwiBD/LuMZFvMr9K+7jBZ1bDX6YF2qNMEuy/uCm2rum9qw G0EEIg9uKJ8ZnSubKcJLu5KCAeBYLvO3s5/yj7pYne3iNYEsgiDtTYdG3cs7rFhkiNB0 MRf4rFjMF6I3k6bJ8RgrfVBN7GGsR7bOJdRhtirXcSMOiuGPOVuy7Dl+Ij66Jzw+ALmA 03dJ7Pj6rjsFOiHsB3X9hYno2icaA21SQTLWYwc8vRDc7unfeBPDfP6Uqa48mS/uS841 o8OA== 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 o22si2451417edi.120.2020.05.27.12.13.19; Wed, 27 May 2020 12:13:43 -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 S2391371AbgE0Qmu (ORCPT + 98 others); Wed, 27 May 2020 12:42:50 -0400 Received: from ex13-edg-ou-001.vmware.com ([208.91.0.189]:13180 "EHLO EX13-EDG-OU-001.vmware.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2391355AbgE0Qms (ORCPT ); Wed, 27 May 2020 12:42:48 -0400 Received: from sc9-mailhost3.vmware.com (10.113.161.73) by EX13-EDG-OU-001.vmware.com (10.113.208.155) with Microsoft SMTP Server id 15.0.1156.6; Wed, 27 May 2020 09:42:44 -0700 Received: from sc9-mailhost2.vmware.com (unknown [10.200.192.41]) by sc9-mailhost3.vmware.com (Postfix) with ESMTP id 40F434046A; Wed, 27 May 2020 09:42:47 -0700 (PDT) From: Matt Helsley To: CC: Josh Poimboeuf , Peter Zijlstra , Steven Rostedt , Matt Helsley Subject: [RFC][PATCH 3/3] objtool: Add support for relocations without addends Date: Wed, 27 May 2020 09:42:33 -0700 Message-ID: <933488b79c91ba040e68847fe53531fd97b18e07.1590597288.git.mhelsley@vmware.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII Received-SPF: None (EX13-EDG-OU-001.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 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 | 109 ++++++++++++++++++++++++++++++++++++-------- tools/objtool/elf.h | 5 +- 2 files changed, 95 insertions(+), 19 deletions(-) diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c index a162bc383945..cc7b628b1566 100644 --- a/tools/objtool/elf.c +++ b/tools/objtool/elf.c @@ -7,6 +7,7 @@ * Copyright (C) 2014 Seth Jennings */ +#define _GNU_SOURCE #include #include #include @@ -490,6 +491,32 @@ void elf_add_reloc(struct elf *elf, struct reloc *reloc) elf_hash_add(elf->reloc_hash, &reloc->hash, reloc_hash(reloc)); } +static int read_rel_reloc(struct section *sec, int i, struct reloc *reloc, unsigned int *symndx) +{ + if (!gelf_getrel(sec->data, i, &reloc->rel)) { + WARN_ELF("gelf_getrel"); + return -1; + } + reloc->type = GELF_R_TYPE(reloc->rel.r_info); + reloc->addend = 0; + reloc->offset = reloc->rel.r_offset; + *symndx = GELF_R_SYM(reloc->rel.r_info); + return 0; +} + +static int read_rela_reloc(struct section *sec, int i, struct reloc *reloc, unsigned int *symndx) +{ + if (!gelf_getrela(sec->data, i, &reloc->rela)) { + WARN_ELF("gelf_getrela"); + return -1; + } + reloc->type = GELF_R_TYPE(reloc->rela.r_info); + reloc->addend = reloc->rela.r_addend; + reloc->offset = reloc->rela.r_offset; + *symndx = GELF_R_SYM(reloc->rela.r_info); + return 0; +} + static int read_relocs(struct elf *elf) { struct section *sec; @@ -497,17 +524,21 @@ static int read_relocs(struct elf *elf) int i; unsigned int symndx; unsigned long nr_reloc, max_reloc = 0, tot_reloc = 0; + int (*read_reloc)(struct section *, int, struct reloc *, unsigned int *); list_for_each_entry(sec, &elf->sections, list) { - if (sec->sh.sh_type != SHT_RELA) - continue; + switch(sec->sh.sh_type) { + case SHT_REL: read_reloc = read_rel_reloc; break; + case SHT_RELA: read_reloc = read_rela_reloc; break; + default: continue; + } if (sec->sh.sh_flags & SHF_INFO_LINK) { sec->base = find_section_by_index(elf, sec->sh.sh_info); } else if (strncmp(sec->name, ".rel", 4) == 0) { WARN("Using naming heuristic to find base of reloc section %s", sec->name); - sec->base = find_section_by_name(elf, sec->name + 5); + sec->base = find_section_by_name(elf, strchrnul(sec->name + 1, '.')); } if (!sec->base) { WARN("can't find base section for reloc section %s", @@ -525,16 +556,8 @@ static int read_relocs(struct elf *elf) return -1; } memset(reloc, 0, sizeof(*reloc)); - - if (!gelf_getrela(sec->data, i, &reloc->rela)) { - WARN_ELF("gelf_getrela"); + if (read_reloc(sec, i, reloc, &symndx)) return -1; - } - - reloc->type = GELF_R_TYPE(reloc->rela.r_info); - reloc->addend = reloc->rela.r_addend; - reloc->offset = reloc->rela.r_offset; - symndx = GELF_R_SYM(reloc->rela.r_info); reloc->sym = find_symbol_by_index(elf, symndx); reloc->sec = sec; if (!reloc->sym) { @@ -752,16 +775,50 @@ struct section *elf_create_reloc_section(struct elf *elf, struct section *base) return sec; } -int elf_rebuild_reloc_section(struct section *sec) +static int elf_rebuild_rel_reloc_section(struct section *sec, int nr) { struct reloc *reloc; - int nr, idx = 0, size; - GElf_Rela *relocs; + int idx = 0, size; + GElf_Rel *relocs; + + /* + * 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(*relocs); + relocs = malloc(size); + if (!relocs) { + perror("malloc"); + return -1; + } - nr = 0; - list_for_each_entry(reloc, &sec->reloc_list, list) - nr++; + sec->data->d_buf = relocs; + sec->data->d_size = size; + + sec->sh.sh_size = size; + + idx = 0; + list_for_each_entry(reloc, &sec->reloc_list, list) { + relocs[idx].r_offset = reloc->offset; + relocs[idx].r_info = GELF_R_INFO(reloc->sym->idx, reloc->type); + idx++; + } + + return 0; +} + +static int elf_rebuild_rela_reloc_section(struct section *sec, int nr) +{ + struct reloc *reloc; + int idx = 0, size; + GElf_Rela *relocs; + /* + * 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(*relocs); relocs = malloc(size); if (!relocs) { @@ -785,6 +842,22 @@ int elf_rebuild_reloc_section(struct section *sec) return 0; } +int elf_rebuild_reloc_section(struct section *sec) +{ + struct reloc *reloc; + int nr; + + nr = 0; + list_for_each_entry(reloc, &sec->reloc_list, list) + nr++; + + switch(sec->sh.sh_type) { + case SHT_REL: return elf_rebuild_rel_reloc_section(sec, nr); + case SHT_RELA: return elf_rebuild_rela_reloc_section(sec, nr); + default: return -1; + } +} + int elf_write(const struct elf *elf) { struct section *sec; diff --git a/tools/objtool/elf.h b/tools/objtool/elf.h index 6ad759fd778e..048200af8267 100644 --- a/tools/objtool/elf.h +++ b/tools/objtool/elf.h @@ -61,7 +61,10 @@ struct symbol { struct reloc { 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; -- 2.20.1