Received: by 2002:a05:6602:18e:0:0:0:0 with SMTP id m14csp2398984ioo; Sat, 28 May 2022 12:23:30 -0700 (PDT) X-Google-Smtp-Source: ABdhPJziV/sQNNhfCjem8a/Szto5baw+v9VXHrwiAg9R7vKvpEA+VH4pPmvcizctYDeAaFu6DaRF X-Received: by 2002:a17:90a:6481:b0:1e2:6ef9:804d with SMTP id h1-20020a17090a648100b001e26ef9804dmr11600476pjj.114.1653765810209; Sat, 28 May 2022 12:23:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1653765810; cv=none; d=google.com; s=arc-20160816; b=PNULXUsfulq3xzFJluV3TUg+gWI0BOUwhK5Gl3OXlNwC+VjM3QPf8jRPO6kwtdUial Y9dqoIMhnZv9CPEgZEkCuaX7QnsuAB0VVcW9hm4vt7G7Z3V+BvYoKuHnv1EGReF4pmCc xNJnOdwfwRjACrFMruYYLH8m6eMZrdqTP4Wx8h2nvBpRQMvdkCiTpM6Yt1Lmg03nYsra h6B5uOIZpIlmJS/EODltIAaT2HaDHhaAUunHOygM5THeJ0m8kJUjLIrVEm3LgaQCMV+0 rz0US2gcV88DFqA3z93TscYczCtr3HOVelTGdMnbQnwQbahsHWIEa0UXPk2TxCTzmtDQ n82w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:mime-version:message-id:date :dkim-signature; bh=WIlpZoYmWCFgpudwqL/wMUojf4oTctziCXwUU7IAv7Q=; b=AcuJpneMPYZ5xEuoz7bzZ/IL8HxVwC8aXuFPSywNDpQoe9s+AB+YbPWVYwWbq0TLah 4EGkyE/G5YwJd6ZsDCarHhA41yyJrcWgFHDLT8jt44bJwMqtd5Ensm1tAMu8CteublEj zBqSLMLZPFD7RRIZ45CMpG3/cvHV48aXU2YKpQEoY4ViVWzz3NJWrNIWlHewMViqjNix JQqBafHP0VfWLC9dv6s1FcC6rBZZM8PFPXsu3JN2eEC0lMd6wXo72Ul3QDOnk6gSoRBe TniKq8vDAM5W7Mtcuhw6ujnRn7L+zdEi0Ni5fXkZT5Oaq0/u2cuD0K3ESQ37i4bo9l32 RwuQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=nb9c2JGk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id 134-20020a63018c000000b003c626c711a4si11188863pgb.861.2022.05.28.12.23.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 28 May 2022 12:23:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=nb9c2JGk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 32F434CD5F; Sat, 28 May 2022 11:54:17 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241897AbiE0Fwz (ORCPT + 99 others); Fri, 27 May 2022 01:52:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33656 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235470AbiE0Fww (ORCPT ); Fri, 27 May 2022 01:52:52 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 49211E64EB for ; Thu, 26 May 2022 22:52:51 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id m9-20020a056902004900b0064fb755258dso3370059ybh.13 for ; Thu, 26 May 2022 22:52:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:message-id:mime-version:subject:from:to:cc; bh=WIlpZoYmWCFgpudwqL/wMUojf4oTctziCXwUU7IAv7Q=; b=nb9c2JGk/gzKxSUp4WF7CzQ7jBgByYOq3LdEukPp0zAzdo26/FoJNX/l5+05yQMSr0 ciMnzYrHB/JvV+dwoT8N0KOSRKmFOKug4Rj+kDpQgItkBcX/c7ZZabdsxUP7duDtC6HL HIxbNlfl9hbqQ0qU0vLcbg/SdZn3IShDRy/rZLnjVd+/2ZOVM3MVUO05cGIq7ELxMhzK CUqS1fpdwr4XGcPxniKMkKBypyEVNZ25g6ulWQqlWFhrqy/EoGn9gE//eFeLui7xOpFD N2JqhQolwFOEwLIEOpYe8rONQyWTNCpMBw4f1mqvupN3JMXKu+m+vJa4rGLqoPrzkXoU 8yHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=WIlpZoYmWCFgpudwqL/wMUojf4oTctziCXwUU7IAv7Q=; b=kDLNjktEotxxgA0TJVnKWbP/ncyqiKpkt6X9i429c8YgR2Bz/4WSWHSK8FpRp+9Pgq VBEIZ5KWr1T1oRwkblfxctyz/wHgKwc1O5bJnqVRV/z+oU+H4+Z57yyf08GY54zEjR7K UdnTtBEZ8pgzh8whzv4WR7+hFArOuzmheYUmxs4ycnglW1ZsJ0ufkfHxnPPNCAGLHiNc srhFEX4vjKsPmkMGJGf6MU6UKns7adWyjJ8IXk6MmmpNAUolv7xibGGGIoByZBuhnFlh ogPfGSTyoSi22T7u2gphqXFhyEHm5qxTNXr1W+f8TjPZn8WbXQZc6JjQvIi2WEqXndQZ jxtg== X-Gm-Message-State: AOAM5314So+v1yRa55hJHayyeCpvaP5EXYH9GwwXANF9egtz3P+DZOPK obSRK8Bp6ysTOc2GL581uSB2mKDXip3B X-Received: from maskray1.svl.corp.google.com ([2620:15c:2ce:200:a48d:4b24:4fb4:af1a]) (user=maskray job=sendgmr) by 2002:a81:2450:0:b0:2d6:aee8:f0d5 with SMTP id k77-20020a812450000000b002d6aee8f0d5mr42943671ywk.81.1653630770431; Thu, 26 May 2022 22:52:50 -0700 (PDT) Date: Thu, 26 May 2022 22:52:17 -0700 Message-Id: <20220527055217.452307-1-maskray@google.com> Mime-Version: 1.0 Subject: [PATCH] perf: Fix segbase for ld.lld linked objects From: Fangrui Song To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Ian Rogers , llvm@lists.linux.dev, Fangrui Song , Sebastian Ullrich Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.5 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=no 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 segbase is the address of .eh_frame_hdr and table_data is segbase plus the header size. find_proc_info computes segbase as `map->start + segbase - map->pgoff` which is wrong when * .eh_frame_hdr and .text are in different PT_LOAD program headers * and their p_vaddr difference does not equal their p_offset difference Since 10.0, ld.lld's default --rosegment -z noseparate-code layout has such R and RX PT_LOAD program headers. ld.lld => fail ld.lld --no-rosegment => ok ld.lld -z separate-code => ok ld.bfd -z separate-code (default for Linux/x86) => ok ld.bfd -z noseparate-code => ok To fix the issue, compute segbase as dso's base address plus PT_GNU_EH_FRAME's p_vaddr. Reported-by: Sebastian Ullrich Link: https://github.com/ClangBuiltLinux/linux/issues/1646 Signed-off-by: Fangrui Song Cc: Ian Rogers --- tools/perf/util/dso.h | 2 + tools/perf/util/unwind-libunwind-local.c | 96 ++++++++++++++++-------- 2 files changed, 67 insertions(+), 31 deletions(-) diff --git a/tools/perf/util/dso.h b/tools/perf/util/dso.h index 3a9fd4d389b5..ec4fc1a9454b 100644 --- a/tools/perf/util/dso.h +++ b/tools/perf/util/dso.h @@ -179,6 +179,7 @@ struct dso { bool loaded; u8 rel; struct build_id bid; + u64 base_addr; u64 text_offset; const char *short_name; const char *long_name; @@ -197,6 +198,7 @@ struct dso { u64 file_size; struct list_head open_entry; u64 debug_frame_offset; + u64 eh_frame_hdr_addr; u64 eh_frame_hdr_offset; } data; /* bpf prog information */ diff --git a/tools/perf/util/unwind-libunwind-local.c b/tools/perf/util/unwind-libunwind-local.c index 41e29fc7648a..7a0053954e49 100644 --- a/tools/perf/util/unwind-libunwind-local.c +++ b/tools/perf/util/unwind-libunwind-local.c @@ -169,30 +169,55 @@ static int __dw_read_encoded_value(u8 **p, u8 *end, u64 *val, __v; \ }) -static u64 elf_section_offset(int fd, const char *name) +static int elf_section_address_and_offset(int fd, const char *name, u64 *address, u64 *offset) { Elf *elf; GElf_Ehdr ehdr; GElf_Shdr shdr; - u64 offset = 0; + int ret; elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); if (elf == NULL) - return 0; + return -1; - do { - if (gelf_getehdr(elf, &ehdr) == NULL) - break; + if (gelf_getehdr(elf, &ehdr) == NULL) + goto out_err; - if (!elf_section_by_name(elf, &ehdr, &shdr, name, NULL)) - break; - - offset = shdr.sh_offset; - } while (0); + if (!elf_section_by_name(elf, &ehdr, &shdr, name, NULL)) + goto out_err; + *address = shdr.sh_addr; + *offset = shdr.sh_offset; + ret = 0; +out_err: elf_end(elf); + return ret; +} + +#ifndef NO_LIBUNWIND_DEBUG_FRAME +static u64 elf_section_offset(int fd, const char *name) +{ + u64 address, offset; + + if (elf_section_address_and_offset(fd, name, &address, &offset)) + return 0; + return offset; } +#endif + +static int elf_base_address(int fd) +{ + Elf *elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); + GElf_Phdr phdr; + int retval = 0; + + if (gelf_getphdr(elf, 0, &phdr) != NULL) + retval = phdr.p_vaddr & -getpagesize(); + + elf_end(elf); + return retval; +} #ifndef NO_LIBUNWIND_DEBUG_FRAME static int elf_is_exec(int fd, const char *name) @@ -248,8 +273,7 @@ struct eh_frame_hdr { } __packed; static int unwind_spec_ehframe(struct dso *dso, struct machine *machine, - u64 offset, u64 *table_data, u64 *segbase, - u64 *fde_count) + u64 offset, u64 *table_data_offset, u64 *fde_count) { struct eh_frame_hdr hdr; u8 *enc = (u8 *) &hdr.enc; @@ -265,35 +289,45 @@ static int unwind_spec_ehframe(struct dso *dso, struct machine *machine, dw_read_encoded_value(enc, end, hdr.eh_frame_ptr_enc); *fde_count = dw_read_encoded_value(enc, end, hdr.fde_count_enc); - *segbase = offset; - *table_data = (enc - (u8 *) &hdr) + offset; + *table_data_offset = enc - (u8 *) &hdr; return 0; } -static int read_unwind_spec_eh_frame(struct dso *dso, struct machine *machine, +static int read_unwind_spec_eh_frame(struct dso *dso, struct unwind_info *ui, u64 *table_data, u64 *segbase, u64 *fde_count) { - int ret = -EINVAL, fd; - u64 offset = dso->data.eh_frame_hdr_offset; + struct map *map; + int ret, fd; - if (offset == 0) { - fd = dso__data_get_fd(dso, machine); + if (dso->data.eh_frame_hdr_offset == 0) { + u64 addr_min = UINT64_MAX; + + fd = dso__data_get_fd(dso, ui->machine); if (fd < 0) return -EINVAL; /* Check the .eh_frame section for unwinding info */ - offset = elf_section_offset(fd, ".eh_frame_hdr"); - dso->data.eh_frame_hdr_offset = offset; + ret = elf_section_address_and_offset(fd, ".eh_frame_hdr", + &dso->data.eh_frame_hdr_addr, + &dso->data.eh_frame_hdr_offset); + maps__for_each_entry(ui->thread->maps, map) { + if (map->dso == dso && map->start < addr_min) + addr_min = map->start; + } + dso->base_addr = addr_min - elf_base_address(fd); dso__data_put_fd(dso); + if (ret || dso->data.eh_frame_hdr_offset == 0) + return -EINVAL; } - if (offset) - ret = unwind_spec_ehframe(dso, machine, offset, - table_data, segbase, - fde_count); - - return ret; + *segbase = dso->base_addr + dso->data.eh_frame_hdr_addr; + ret = unwind_spec_ehframe(dso, ui->machine, dso->data.eh_frame_hdr_offset, + table_data, fde_count); + if (ret) + return ret; + *table_data += *segbase; + return 0; } #ifndef NO_LIBUNWIND_DEBUG_FRAME @@ -388,14 +422,14 @@ find_proc_info(unw_addr_space_t as, unw_word_t ip, unw_proc_info_t *pi, pr_debug("unwind: find_proc_info dso %s\n", map->dso->name); /* Check the .eh_frame section for unwinding info */ - if (!read_unwind_spec_eh_frame(map->dso, ui->machine, + if (!read_unwind_spec_eh_frame(map->dso, ui, &table_data, &segbase, &fde_count)) { memset(&di, 0, sizeof(di)); di.format = UNW_INFO_FORMAT_REMOTE_TABLE; di.start_ip = map->start; di.end_ip = map->end; - di.u.rti.segbase = map->start + segbase - map->pgoff; - di.u.rti.table_data = map->start + table_data - map->pgoff; + di.u.rti.segbase = segbase; + di.u.rti.table_data = table_data; di.u.rti.table_len = fde_count * sizeof(struct table_entry) / sizeof(unw_word_t); ret = dwarf_search_unwind_table(as, ip, &di, pi, -- 2.36.1.124.g0e6072fb45-goog