From patchwork Thu Nov 28 01:23:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ihor Solodrai X-Patchwork-Id: 13887513 Received: from mail-40134.protonmail.ch (mail-40134.protonmail.ch [185.70.40.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 36C591DFE1; Thu, 28 Nov 2024 01:24:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.70.40.134 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732757045; cv=none; b=UgGQCj0UV2JwMBTHrzibaT+EYLDG6QZMrHXKH8/NMk/xIQnfzj+b6neRzcTDTyky2e+PsvauXcUBqVB46TAPrCMCs9UrFXYPtRkqz6Ph/VNFm10KshGyDq/cNPlkP/apal2DPDPXIpB8A7QWI6/Vwtm7QYiTFP96GpM39G9CNLw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732757045; c=relaxed/simple; bh=5bOnr1ypHwPs1mWwGi/AQufJtaej3OMXKl4fTb0rrYQ=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=C2erG3SE/CrjsHuFsrMZ1mFFM7lYPktp0gBhxraTryrbuMzwsQVIYVvCZMYLhHp3FInMCGfg7kmMghkKYKEs2m8cszCpXMdUx8y4oWFukG6WiFVDIfATw3bDJ+DwyEknoV1iqu5arINy8VmqKdldAMe7NvakKcvdKMyoxbe8s6c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pm.me; spf=pass smtp.mailfrom=pm.me; dkim=pass (2048-bit key) header.d=pm.me header.i=@pm.me header.b=KX0vMQam; arc=none smtp.client-ip=185.70.40.134 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=pm.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pm.me Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pm.me header.i=@pm.me header.b="KX0vMQam" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail3; t=1732757036; x=1733016236; bh=0kNK2QLWgiitAe9nVN05yGD9VfdypXNVZMc26XViufk=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector:List-Unsubscribe:List-Unsubscribe-Post; b=KX0vMQamO0lHAT3RtPwI7uPip9hbJHzjlJn5Yk0ueBwV8BHtYYjy5yyPuJNDaObTa hAIAo4e8rtGE1f32DcR+PLDaaYQkksA8IUdp8kt0OtWNasQ0Tx6+XLx+o3pnAFG6Df GQm6DFXkdFqLMDHQNVrXwAXO+LVC2clIRFhH3gqO7aHR268ees0YcfPWW2uwMYy6Ko J55CKO1FakYt6KHUFHoVSSXQ1JI28XtWIkZAj8k6NoQNakvUKZQuxsEaKWjyx25O5O cli3/Ax4gUzF5FON9pTDieqCZ7McG/MVf9SIkXD1bBEfIW10Zti+7eOjhhO8hN6gVT m6K8xaXnl58rw== Date: Thu, 28 Nov 2024 01:23:54 +0000 To: dwarves@vger.kernel.org, acme@kernel.org From: Ihor Solodrai Cc: bpf@vger.kernel.org, alan.maguire@oracle.com, eddyz87@gmail.com, andrii@kernel.org, mykolal@fb.com Subject: [RFC PATCH 2/9] btf_encoder: store,use section-relative addresses in ELF function representation Message-ID: <20241128012341.4081072-3-ihor.solodrai@pm.me> In-Reply-To: <20241128012341.4081072-1-ihor.solodrai@pm.me> References: <20241128012341.4081072-1-ihor.solodrai@pm.me> Feedback-ID: 27520582:user:proton X-Pm-Message-ID: 283d93515b51581ad1e491742b74a7cf52d79c9c Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-State: RFC From: Alan Maguire This will help us do more accurate DWARF/ELF matching. Signed-off-by: Alan Maguire Signed-off-by: Ihor Solodrai --- btf_encoder.c | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/btf_encoder.c b/btf_encoder.c index 98e4d7d..01d7094 100644 --- a/btf_encoder.c +++ b/btf_encoder.c @@ -88,6 +88,7 @@ struct btf_encoder_func_state { struct elf_function { const char *name; char *alias; + uint32_t addr; size_t prefixlen; struct btf_encoder_func_state state; }; @@ -131,6 +132,7 @@ struct btf_encoder { int allocated; int cnt; int suffix_cnt; /* number of .isra, .part etc */ + uint64_t base_addr; } functions; }; @@ -1274,13 +1276,23 @@ static int functions_cmp(const void *_a, const void *_b) { const struct elf_function *a = _a; const struct elf_function *b = _b; + int ret; /* if search key allows prefix match, verify target has matching * prefix len and prefix matches. */ if (a->prefixlen && a->prefixlen == b->prefixlen) - return strncmp(a->name, b->name, b->prefixlen); - return strcmp(a->name, b->name); + ret = strncmp(a->name, b->name, b->prefixlen); + else + ret = strcmp(a->name, b->name); + if (ret != 0) + return ret; + /* avoid address mismatch */ + if (a->addr != 0 && b->addr != 0) { + if (a->addr != b->addr) + return a->addr > b->addr ? 1 : -1; + } + return 0; } #ifndef max @@ -1301,6 +1313,7 @@ static int btf_encoder__collect_function(struct btf_encoder *encoder, GElf_Sym * { struct elf_function *new; const char *name; + uint64_t addr; if (elf_sym__type(sym) != STT_FUNC) return 0; @@ -1325,6 +1338,9 @@ static int btf_encoder__collect_function(struct btf_encoder *encoder, GElf_Sym * memset(&encoder->functions.entries[encoder->functions.cnt], 0, sizeof(*new)); encoder->functions.entries[encoder->functions.cnt].name = name; + /* convert to absoulte address for DWARF/ELF matching. */ + addr = elf_sym__value(sym); + encoder->functions.entries[encoder->functions.cnt].addr = (uint32_t)addr; if (strchr(name, '.')) { const char *suffix = strchr(name, '.'); @@ -1336,9 +1352,10 @@ static int btf_encoder__collect_function(struct btf_encoder *encoder, GElf_Sym * } static struct elf_function *btf_encoder__find_function(const struct btf_encoder *encoder, - const char *name, size_t prefixlen) + const char *name, size_t prefixlen, + uint32_t addr) { - struct elf_function key = { .name = name, .prefixlen = prefixlen }; + struct elf_function key = { .name = name, .prefixlen = prefixlen, .addr = addr }; return bsearch(&key, encoder->functions.entries, encoder->functions.cnt, sizeof(key), functions_cmp); } @@ -2086,11 +2103,16 @@ int btf_encoder__encode(struct btf_encoder *encoder) static int btf_encoder__collect_symbols(struct btf_encoder *encoder) { + bool base_addr_set = false; uint32_t sym_sec_idx; uint32_t core_id; GElf_Sym sym; elf_symtab__for_each_symbol_index(encoder->symtab, core_id, sym, sym_sec_idx) { + if (!base_addr_set && sym_sec_idx && sym_sec_idx < encoder->seccnt) { + encoder->functions.base_addr = encoder->secinfo[sym_sec_idx].addr; + base_addr_set = true; + } if (btf_encoder__collect_function(encoder, &sym)) return -1; } @@ -2543,13 +2565,16 @@ int btf_encoder__encode_cu(struct btf_encoder *encoder, struct cu *cu, struct co continue; if (encoder->functions.cnt) { const char *name; + uint64_t addr; name = function__name(fn); if (!name) continue; + addr = (uint32_t)function__addr(fn); + /* prefer exact function name match... */ - func = btf_encoder__find_function(encoder, name, 0); + func = btf_encoder__find_function(encoder, name, 0, addr); if (!func && encoder->functions.suffix_cnt && conf_load->btf_gen_optimized) { /* falling back to name.isra.0 match if no exact @@ -2560,7 +2585,7 @@ int btf_encoder__encode_cu(struct btf_encoder *encoder, struct cu *cu, struct co * in any cu. */ func = btf_encoder__find_function(encoder, name, - strlen(name)); + strlen(name), addr); if (func) { if (encoder->verbose) printf("matched function '%s' with '%s'%s\n",