From patchwork Wed Oct 2 23:52:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Brennan X-Patchwork-Id: 13820572 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) (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 35774199249; Wed, 2 Oct 2024 23:53:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=205.220.177.32 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727913198; cv=fail; b=VGwYioowDmgHoeZHiSYakQSS4L8X5bUuHTOngeBUkw8MeVDnZOw1esTn+V2h03PnLygG3fGQwnYI11nQZgos7QfviUrNtYznaGdvnTka0XHpmzJKwM0QkzLhuDFAOccsCAOq2fUn3/2JrBOHs2q3BBrMnGHUAMV1W4ixqLK3kl8= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727913198; c=relaxed/simple; bh=nJ54YNPaypTPpJbuW6KySZNX5+pOYtAVFtU5ajR2yAU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=AF8W15AtL/BmU53FR2RU5ADq17W4v5Yi33yRW+3Jj18HfqH6BKWWuOdPzxVUs8HXcm24wRpyC0UIc3L26BNA6powbiQ7UQ0eemZft9xd5z2wgKicyVnBwY85UgnRHUgAKEqiW2oQqytj9fEyvzGg5vhWtcV4VDpC1LabM9qQqJs= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com; spf=pass smtp.mailfrom=oracle.com; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b=dQj6ZOmi; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=vThWW4iI; arc=fail smtp.client-ip=205.220.177.32 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oracle.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="dQj6ZOmi"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="vThWW4iI" Received: from pps.filterd (m0246630.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 492MghJe031788; Wed, 2 Oct 2024 23:53:05 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h= from:to:cc:subject:date:message-id:in-reply-to:references :content-transfer-encoding:content-type:mime-version; s= corp-2023-11-20; bh=Bbyzpcs81t4JJ1/b4UtooRDZxXMzXkoXC1/woHpThUg=; b= dQj6ZOmicCmJyW23QJ5znFncI5XgWMX7HUxXfUGABug5eXgINZZTpg6wDCgULcFx 4oeidiUqTjeKNs4qjOVm0YCf9YJrw1zTvh3vPAfFD3jYoyWWKt1KQLYRQV/8Ubzx ALhTDBxTpcwR1omFO3fCNRwEmV8dxo72uKvcgDlcj66HXfz/YArEnK+D38/VKNSE QE74xYslB4AerBmyTlfl5B6ccDjwKwv4nLg+OhlJpXv6893B5maUgNePK/KODEeu lroqtaAZojmESrClOc3/6A/2u6JlDEjLukSGv0YO4VA9Uv4yeZb5Prhn8dP72wjW Hw/n1fnTeMcdEmkEBODhFg== Received: from phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta02.appoci.oracle.com [147.154.114.232]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 41x87dauf9-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 02 Oct 2024 23:53:05 +0000 (GMT) Received: from pps.filterd (phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 492LVZ2A028553; Wed, 2 Oct 2024 23:53:04 GMT Received: from nam11-dm6-obe.outbound.protection.outlook.com (mail-dm6nam11lp2175.outbound.protection.outlook.com [104.47.57.175]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 41x889r8kw-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 02 Oct 2024 23:53:04 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=czkqklhio0ERuv1Pa9Q1sLbBSRN4DPKZlphahOTPpGYI07186a/2s8rwqhpY/raNwDoPWwMGsy+03netCCELrgqj7TlQQo3DgVTkEWy20yIFRit4YtmdBxkWF0YGGmp+LAkEPLqStrI62HvaWe1qTintZVdt/Vsm/LXpAbIWp5Ru5nahR7ivQbUWHUNZePfgiix5GJv2f4dXOQaZIDWEgMiU05mdHQjYoP0i8LDiG+A9QNwZD3rMDuPEz+j7WZ3nk3CII0XKpxNuxBuxYmT9yoNy0J+pom1IX+CRSn9D/y38lGJM70hP4YiZ+eD3II7ThFFOqxylyJTfHQh4mT6PRQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=Bbyzpcs81t4JJ1/b4UtooRDZxXMzXkoXC1/woHpThUg=; b=WwjxffJCSsbEAnvZs8ahiTicN8cLXJ0CQRlOExE2tJoTXHUEiIsBcjfn3zMeT2uOBjhQfVWQVILn/Hp10FI84q/4mXD1tSsc85v+o+RiBtAa9ccL3vTatP9F3KtJ6euEAbnbSTAFfZf2wNOF6qozFH89RZqtOfktxtiOoZI8cUfAnu2+xAW/Yy+rUu0x6QVhz+uyqrekf9rDmC5EA5Us+l9mZR1LSZzUd92ZrToXf4cAgR3OM3bCOWZQUzQU7+4fsWEA4Rr/vJry0JNW+iSEDPYS3UWK+B31Thwq/fC0JyrkMhvt7MjZ9hk7gsUGMh795ziW17BFcMz9H4GWp1rysA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Bbyzpcs81t4JJ1/b4UtooRDZxXMzXkoXC1/woHpThUg=; b=vThWW4iIWW23zW1SVGLtpVeZhal5kNXMlR+e1ag+KYpfD/YOnN6sugtLmow7SbVVN7jtmHhXVsU5hg4hQu7fq+5lbN3PWk8wZAjviSTmjICkCjdfEUruQfze01JSxrKigeHuDtEyIhEUEBVPwT5dNZCFL/XcnUAuvp5iYwjVscc= Received: from PH8PR10MB6597.namprd10.prod.outlook.com (2603:10b6:510:226::20) by SA2PR10MB4458.namprd10.prod.outlook.com (2603:10b6:806:f8::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8026.16; Wed, 2 Oct 2024 23:53:01 +0000 Received: from PH8PR10MB6597.namprd10.prod.outlook.com ([fe80::6874:4af6:bf0a:6ca]) by PH8PR10MB6597.namprd10.prod.outlook.com ([fe80::6874:4af6:bf0a:6ca%3]) with mapi id 15.20.8026.016; Wed, 2 Oct 2024 23:53:01 +0000 From: Stephen Brennan To: Arnaldo Carvalho de Melo Cc: bpf@vger.kernel.org, dwarves@vger.kernel.org, linux-debuggers@vger.kernel.org, Stephen Brennan , Alan Maguire Subject: [PATCH dwarves v3 2/5] btf_encoder: stop indexing symbols for VARs Date: Wed, 2 Oct 2024 16:52:44 -0700 Message-ID: <20241002235253.487251-3-stephen.s.brennan@oracle.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20241002235253.487251-1-stephen.s.brennan@oracle.com> References: <20241002235253.487251-1-stephen.s.brennan@oracle.com> X-ClientProxiedBy: BN8PR12CA0014.namprd12.prod.outlook.com (2603:10b6:408:60::27) To PH8PR10MB6597.namprd10.prod.outlook.com (2603:10b6:510:226::20) Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PH8PR10MB6597:EE_|SA2PR10MB4458:EE_ X-MS-Office365-Filtering-Correlation-Id: 1d09dde5-f5d0-4dac-6a5f-08dce33d599b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|10070799003|376014|366016; X-Microsoft-Antispam-Message-Info: cyZjSTcayOyjlUbT4MnMX6pDDGHlGyOqoYDr4y1g3qpnb+1KgMbjuqIWeiql9xCKEhyKC4AF2vBGr0L8wpTOXkm4SEk33+LqAanATEiWB814H0/x3996AcmTyq5qDsIG12Y5L2StFqKdhXV3Pe34YACzGChuJm/TAIHJ7K/j+LYplPbNm5GAxemsslJ7KqKDyBdiNpkLjfoWhivsCClXdwpKGHZkWsKS4dP672h8oGV0XfTdszr2SmSkS60LYtXnpd+GXqswE1VMVvHoKjB3mzDHwnApyezc+0w+kdLqMPOhRdPfQIaYctSvVUejJV3ZsmUDyOdTf45d+Eb9TG03Gnry4mabSsQYx7a/Dqe1fATuvp0zmhXTo/zyCMIRAE14AKVfpAIQKdkgDOwUM3E0t4lp9Hj37Ws7Jm9C9rUB3rkwN+XtscUOif0E8/VouwqLeahrLSTS+2Q5OhccrlF8L0J6nTSwbrqsW6sM9kyf+gusslf496pS6Qp1QpsWSfngDscnCJ+/KKuY9L8Jr46S/VujbLmohOJvj46dDeuc2R58OodhPoIkx86tIGQDMfeG3MC7gC/FZHFDw5vwY64UrLTng0qNzBxRr5dp/pPWTuh96rswrj+SKze0N+UCM6Xa1DbST9WSpjLyjSUOWUVdtDhOVm96OrWZtGizJu24BFoyPn1EpUWiZdyI0d9t8m2kILgW6ud0gxGtYpjYADu0oeUK/ysiRkqWRyQX7IVuOm7OdpKQkXipk8C4jp3xLvySiMQxO+HTHrITS6S1wjVA/xeGwUXtS80yM0ts8kxZF6NPZ+ivWjpmhRAZyLs2h85VFEoCLQil6A2KumCP/whaqwaB+qhRmJDoK3pL+mlg1UREEwOV1fcrFGcLPErTEBU9H/9JK1u4J34LyNI8IES6sazfsNB1qoNT5ariINpZFn4D/XMe3aUqaHH26WlnPEDfI8M58sss/pLOm3FfptjS8sIx5R5TGXys9Ak4QDZs/l8L52Ya2EitC5gZ6Dl78z1EQAY74aptzODPSv8OCQiDpwy9xvZyC8eOuGFdaF1HXNw7WIE0qvQjacDBAnneiJYAbqZ/D0JY+PcbAdskD9IodtjW1MkKZf/MZhsxSqY9x8Ifdp0E9WXZKRlXKWiHoA1YALhoWeGiNvj2UGo9zOvnZbVEs/1LYZeli4QbGzJRGRKt8UnrDN1WN6EVi/dmeaI+B2MuzlOfuj99ltLJF5y7YlToR0SSFiSN0aG2AaL9ko8UyZWIwxQuy2dyfpZRFtDKnAlrXiPLSd4RI9jQu7hFUQ== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:PH8PR10MB6597.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(1800799024)(10070799003)(376014)(366016);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: N2pwun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fJLj8NQ3A0gFshVttAqHX21fZAWhjpdePx6z07XSoIXUIHeRnEO4RP/tVXJS0DJuvgYlvOKYi0gWRLU6hXIQTObkpk7JRs+1qpVoQFJdAHue/UB3aXzB9QygrijBAanctrmjLtLGFtBE+eDrw35TwhACApTZ5P8fYO/TMKfJ9i5SlHTmF9SEiqfgGd+/Dc= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 1d09dde5-f5d0-4dac-6a5f-08dce33d599b X-MS-Exchange-CrossTenant-AuthSource: PH8PR10MB6597.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Oct 2024 23:53:01.7244 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 8Xw/kxxUusRm686dsd7s6pdG6mcgRGXFu/kC3nRD1PfF3wWK49veVfe5UZQhyAQEosM/8i322ESqmR2INsDq39YpUMbp7sCn/ENXECB5ipE= X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA2PR10MB4458 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1051,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-10-02_21,2024-09-30_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 adultscore=0 mlxscore=0 malwarescore=0 mlxlogscore=999 phishscore=0 suspectscore=0 spamscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2408220000 definitions=main-2410020169 X-Proofpoint-GUID: r32tOQnZ4qPhqsNxerHIVAy92sKI2qjt X-Proofpoint-ORIG-GUID: r32tOQnZ4qPhqsNxerHIVAy92sKI2qjt Currently we index symbols from the percpu ELF section, and when processing DWARF variables for inclusion, we check whether the variable matches an existing symbol. The matched symbol is used for three purposes: 1. When no symbol of the same address is found, the variable is skipped. This can occur because the symbol name was an invalid BTF identifier, and so it did not get indexed. Or more commonly, it can be because the variable is not stored in the per-cpu section, and thus was not indexed. 2. If the symbol offset is 0, then we compare the DWARF variable's name against the symbol name to filter out "special" DWARF variables. 3. We use the symbol size in the DATASEC entry for the variable. For 1, we don't need the symbol table: we can simply check the DWARF variable name directly, and we can use the variable address to determine the ELF section it is contained in. For 3, we also don't need the symbol table: we can use the variable's size information from DWARF. Issue 2 is more complicated, but thanks to the addition of the "artificial" and "top_level" flags, many of the "special" DWARF variables can be directly filtered out, and the few remaining problematic variables can be filtered by name from a kernel-specific list of patterns. This allows the symbol table index to be removed. The benefit of removing this index is twofold. First, handling variable addresses is simplified, since we don't need to know whether the file is ET_REL. Second, this will make it easier to output variables that aren't just percpu, since we won't need to index variables from all ELF sections. Signed-off-by: Stephen Brennan Reviewed-by: Alan Maguire --- btf_encoder.c | 250 +++++++++++++++++++------------------------------- 1 file changed, 96 insertions(+), 154 deletions(-) diff --git a/btf_encoder.c b/btf_encoder.c index 652a945..31a418a 100644 --- a/btf_encoder.c +++ b/btf_encoder.c @@ -93,16 +93,11 @@ struct elf_function { struct btf_encoder_func_state state; }; -struct var_info { - uint64_t addr; - const char *name; - uint32_t sz; -}; - struct elf_secinfo { uint64_t addr; const char *name; uint64_t sz; + uint32_t type; }; /* @@ -125,17 +120,11 @@ struct btf_encoder { gen_floats, skip_encoding_decl_tag, tag_kfuncs, - is_rel, gen_distilled_base; uint32_t array_index_id; struct elf_secinfo *secinfo; size_t seccnt; - struct { - struct var_info *vars; - int var_cnt; - int allocated; - uint32_t shndx; - } percpu; + size_t percpu_shndx; int encode_vars; struct { struct elf_function *entries; @@ -2098,111 +2087,18 @@ int btf_encoder__encode(struct btf_encoder *encoder) return err; } -static int percpu_var_cmp(const void *_a, const void *_b) -{ - const struct var_info *a = _a; - const struct var_info *b = _b; - - if (a->addr == b->addr) - return 0; - return a->addr < b->addr ? -1 : 1; -} - -static bool btf_encoder__percpu_var_exists(struct btf_encoder *encoder, uint64_t addr, uint32_t *sz, const char **name) -{ - struct var_info key = { .addr = addr }; - const struct var_info *p = bsearch(&key, encoder->percpu.vars, encoder->percpu.var_cnt, - sizeof(encoder->percpu.vars[0]), percpu_var_cmp); - if (!p) - return false; - - *sz = p->sz; - *name = p->name; - return true; -} - -static int btf_encoder__collect_percpu_var(struct btf_encoder *encoder, GElf_Sym *sym, size_t sym_sec_idx) -{ - const char *sym_name; - uint64_t addr; - uint32_t size; - - /* compare a symbol's shndx to determine if it's a percpu variable */ - if (sym_sec_idx != encoder->percpu.shndx) - return 0; - if (elf_sym__type(sym) != STT_OBJECT) - return 0; - - addr = elf_sym__value(sym); - - size = elf_sym__size(sym); - if (!size) - return 0; /* ignore zero-sized symbols */ - - sym_name = elf_sym__name(sym, encoder->symtab); - if (!btf_name_valid(sym_name)) { - dump_invalid_symbol("Found symbol of invalid name when encoding btf", - sym_name, encoder->verbose, encoder->force); - if (encoder->force) - return 0; - return -1; - } - - if (encoder->verbose) - printf("Found per-CPU symbol '%s' at address 0x%" PRIx64 "\n", sym_name, addr); - - /* Make sure addr is section-relative. For kernel modules (which are - * ET_REL files) this is already the case. For vmlinux (which is an - * ET_EXEC file) we need to subtract the section address. - */ - if (!encoder->is_rel) - addr -= encoder->secinfo[encoder->percpu.shndx].addr; - - if (encoder->percpu.var_cnt == encoder->percpu.allocated) { - struct var_info *new; - - new = reallocarray_grow(encoder->percpu.vars, - &encoder->percpu.allocated, - sizeof(*encoder->percpu.vars)); - if (!new) { - fprintf(stderr, "Failed to allocate memory for variables\n"); - return -1; - } - encoder->percpu.vars = new; - } - encoder->percpu.vars[encoder->percpu.var_cnt].addr = addr; - encoder->percpu.vars[encoder->percpu.var_cnt].sz = size; - encoder->percpu.vars[encoder->percpu.var_cnt].name = sym_name; - encoder->percpu.var_cnt++; - - return 0; -} -static int btf_encoder__collect_symbols(struct btf_encoder *encoder, bool collect_percpu_vars) +static int btf_encoder__collect_symbols(struct btf_encoder *encoder) { - Elf32_Word sym_sec_idx; + uint32_t sym_sec_idx; uint32_t core_id; GElf_Sym sym; - /* cache variables' addresses, preparing for searching in symtab. */ - encoder->percpu.var_cnt = 0; - - /* search within symtab for percpu variables */ elf_symtab__for_each_symbol_index(encoder->symtab, core_id, sym, sym_sec_idx) { - if (collect_percpu_vars && btf_encoder__collect_percpu_var(encoder, &sym, sym_sec_idx)) - return -1; if (btf_encoder__collect_function(encoder, &sym)) return -1; } - if (collect_percpu_vars) { - if (encoder->percpu.var_cnt) - qsort(encoder->percpu.vars, encoder->percpu.var_cnt, sizeof(encoder->percpu.vars[0]), percpu_var_cmp); - - if (encoder->verbose) - printf("Found %d per-CPU variables!\n", encoder->percpu.var_cnt); - } - if (encoder->functions.cnt) { qsort(encoder->functions.entries, encoder->functions.cnt, sizeof(encoder->functions.entries[0]), functions_cmp); @@ -2224,15 +2120,54 @@ static bool ftype__has_arg_names(const struct ftype *ftype) return true; } +static int get_elf_section(struct btf_encoder *encoder, unsigned long addr) +{ + /* Start at index 1 to ignore initial SHT_NULL section */ + for (int i = 1; i < encoder->seccnt; i++) + /* Variables are only present in PROGBITS or NOBITS (.bss) */ + if ((encoder->secinfo[i].type == SHT_PROGBITS || + encoder->secinfo[i].type == SHT_NOBITS) && + encoder->secinfo[i].addr <= addr && + (addr - encoder->secinfo[i].addr) < encoder->secinfo[i].sz) + return i; + return -ENOENT; +} + +/* + * Filter out variables / symbol names with common prefixes and no useful + * values. Prefixes should be added sparingly, and it should be objectively + * obvious that they are not useful. + */ +static bool filter_variable_name(const char *name) +{ + static const struct { char *s; size_t len; } skip[] = { + #define X(str) {str, sizeof(str) - 1} + X("__UNIQUE_ID"), + X("__tpstrtab_"), + X("__exitcall_"), + X("__func_stack_frame_non_standard_") + #undef X + }; + int i; + + if (*name != '_') + return false; + + for (i = 0; i < ARRAY_SIZE(skip); i++) { + if (strncmp(name, skip[i].s, skip[i].len) == 0) + return true; + } + return false; +} + static int btf_encoder__encode_cu_variables(struct btf_encoder *encoder) { struct cu *cu = encoder->cu; uint32_t core_id; struct tag *pos; int err = -1; - struct elf_secinfo *pcpu_scn = &encoder->secinfo[encoder->percpu.shndx]; - if (encoder->percpu.shndx == 0 || !encoder->symtab) + if (encoder->percpu_shndx == 0 || !encoder->symtab) return 0; if (encoder->verbose) @@ -2240,59 +2175,69 @@ static int btf_encoder__encode_cu_variables(struct btf_encoder *encoder) cu__for_each_variable(cu, core_id, pos) { struct variable *var = tag__variable(pos); - uint32_t size, type, linkage; - const char *name, *dwarf_name; + uint32_t type, linkage; + const char *name; struct llvm_annotation *annot; const struct tag *tag; + size_t shndx, size; uint64_t addr; int id; + /* Skip incomplete (non-defining) declarations */ if (var->declaration && !var->spec) continue; - /* percpu variables are allocated in global space */ - if (variable__scope(var) != VSCOPE_GLOBAL && !var->spec) + /* + * top_level: indicates that the variable is declared at the top + * level of the CU, and thus it is globally scoped. + * artificial: indicates that the variable is a compiler-generated + * "fake" variable that doesn't appear in the source. + * scope: set by pahole to indicate the type of storage the + * variable has. GLOBAL indicates it is stored in static + * memory (as opposed to a stack variable or register) + * + * Some variables are "top_level" but not GLOBAL: + * e.g. current_stack_pointer, which is a register variable, + * despite having global CU-declarations. We don't want that, + * since no code could actually find this variable. + * Some variables are GLOBAL but not top_level: + * e.g. function static variables + */ + if (!var->top_level || var->artificial || var->scope != VSCOPE_GLOBAL) continue; /* addr has to be recorded before we follow spec */ addr = var->ip.addr; - dwarf_name = variable__name(var); - /* Make sure addr is section-relative. DWARF, unlike ELF, - * always contains virtual symbol addresses, so subtract - * the section address unconditionally. - */ - if (addr < pcpu_scn->addr || addr >= pcpu_scn->addr + pcpu_scn->sz) + /* Get the ELF section info for the variable */ + shndx = get_elf_section(encoder, addr); + if (shndx != encoder->percpu_shndx) continue; - addr -= pcpu_scn->addr; - if (!btf_encoder__percpu_var_exists(encoder, addr, &size, &name)) - continue; /* not a per-CPU variable */ + /* Convert addr to section relative */ + addr -= encoder->secinfo[shndx].addr; - /* A lot of "special" DWARF variables (e.g, __UNIQUE_ID___xxx) - * have addr == 0, which is the same as, say, valid - * fixed_percpu_data per-CPU variable. To distinguish between - * them, additionally compare DWARF and ELF symbol names. If - * DWARF doesn't provide proper name, pessimistically assume - * bad variable. - * - * Examples of such special variables are: - * - * 1. __ADDRESSABLE(sym), which are forcely emitted as symbols. - * 2. __UNIQUE_ID(prefix), which are introduced to generate unique ids. - * 3. __exitcall(fn), functions which are labeled as exit calls. - * - * This is relevant only for vmlinux image, as for kernel - * modules per-CPU data section has non-zero offset so all - * per-CPU symbols have non-zero values. - */ - if (var->ip.addr == 0) { - if (!dwarf_name || strcmp(dwarf_name, name)) + /* DWARF specification reference should be followed, because + * information like the name & type may not be present on var */ + if (var->spec) + var = var->spec; + + name = variable__name(var); + if (!name) + continue; + + /* Check for invalid BTF names */ + if (!btf_name_valid(name)) { + dump_invalid_symbol("Found invalid variable name when encoding btf", + name, encoder->verbose, encoder->force); + if (encoder->force) continue; + else + return -1; } - if (var->spec) - var = var->spec; + if (filter_variable_name(name)) + continue; if (var->ip.tag.type == 0) { fprintf(stderr, "error: found variable '%s' in CU '%s' that has void type\n", @@ -2304,9 +2249,10 @@ static int btf_encoder__encode_cu_variables(struct btf_encoder *encoder) } tag = cu__type(cu, var->ip.tag.type); - if (tag__size(tag, cu) == 0) { + size = tag__size(tag, cu); + if (size == 0) { if (encoder->verbose) - fprintf(stderr, "Ignoring zero-sized per-CPU variable '%s'...\n", dwarf_name ?: ""); + fprintf(stderr, "Ignoring zero-sized per-CPU variable '%s'...\n", name); continue; } @@ -2388,8 +2334,6 @@ struct btf_encoder *btf_encoder__new(struct cu *cu, const char *detached_filenam goto out_delete; } - encoder->is_rel = ehdr.e_type == ET_REL; - switch (ehdr.e_ident[EI_DATA]) { case ELFDATA2LSB: btf__set_endianness(encoder->btf, BTF_LITTLE_ENDIAN); @@ -2430,15 +2374,16 @@ struct btf_encoder *btf_encoder__new(struct cu *cu, const char *detached_filenam encoder->secinfo[shndx].addr = shdr.sh_addr; encoder->secinfo[shndx].sz = shdr.sh_size; encoder->secinfo[shndx].name = secname; + encoder->secinfo[shndx].type = shdr.sh_type; if (strcmp(secname, PERCPU_SECTION) == 0) - encoder->percpu.shndx = shndx; + encoder->percpu_shndx = shndx; } - if (!encoder->percpu.shndx && encoder->verbose) + if (!encoder->percpu_shndx && encoder->verbose) printf("%s: '%s' doesn't have '%s' section\n", __func__, cu->filename, PERCPU_SECTION); - if (btf_encoder__collect_symbols(encoder, encoder->encode_vars & BTF_VAR_PERCPU)) + if (btf_encoder__collect_symbols(encoder)) goto out_delete; if (encoder->verbose) @@ -2480,9 +2425,6 @@ void btf_encoder__delete(struct btf_encoder *encoder) encoder->functions.allocated = encoder->functions.cnt = 0; free(encoder->functions.entries); encoder->functions.entries = NULL; - encoder->percpu.allocated = encoder->percpu.var_cnt = 0; - free(encoder->percpu.vars); - encoder->percpu.vars = NULL; free(encoder); }