From patchwork Mon Jul 27 09:20:05 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Beulich X-Patchwork-Id: 139991 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A24224BA2E15 for ; Mon, 27 Jul 2026 09:20:45 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A24224BA2E15 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=suse.com header.i=@suse.com header.a=rsa-sha256 header.s=google header.b=X8W2KkJh X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [IPv6:2a00:1450:4864:20::42d]) by sourceware.org (Postfix) with ESMTPS id D6B184BA5433 for ; Mon, 27 Jul 2026 09:20:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D6B184BA5433 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org D6B184BA5433 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::42d ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1785144008; cv=none; b=w7iLxTA+jWS8z+fVn6llZvByl4aqZ9+CMnhudwvG6Z9gkeNJww5iXXG2VugZ3c72AJKkrDPBLYiT0ImpxHB7HSrb+M7fz1vMY0YnOQm898qvL+eNHoGYXFcQsl2W2LYUzXFHcnTpQUQBlU8WXIpTAyxkcpQuqkJPOSo3ZNlafzs= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1785144008; c=relaxed/simple; bh=Lsjjix8YW8R1IL7U1hvb/l177Jp/RjKWs03mSkUnagM=; h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:From:To; b=FY8LCNky2IYrWGmHaUjliuBgo7Ilh+N+1iK0yrQ+4qk55H6lR/dMHsdWUbxo8WCJvjU2mmURpF/6SS+Vmbk0aHVnMTwVQ2d5cKdyi0994V27aZPqpAwPuo4vYhBTseA38CCHDvxxeUmTCsg3D7ZxPfM5ssYUowdQ3LQ4oDFm3rU= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=suse.com header.i=@suse.com header.a=rsa-sha256 header.s=google header.b=X8W2KkJh DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D6B184BA5433 Received: by mail-wr1-x42d.google.com with SMTP id ffacd0b85a97d-47f7027ca11so1260432f8f.3 for ; Mon, 27 Jul 2026 02:20:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1785144006; x=1785748806; darn=sourceware.org; h=content-transfer-encoding:content-type:in-reply-to:autocrypt :content-language:references:to:from:subject:user-agent:mime-version :date:message-id:from:to:cc:subject:date:message-id:reply-to :content-type; bh=cGXghtDdb1EheT91fh49eozejVsyI7Q5CF+fF/cYuxI=; b=X8W2KkJhAuziB9e+/daHKMtERVVeDwcP6H8tbUneLZ0orcZ4Qo5oyHzQRgALawMKB7 BBGK8k1Fd0yvHdfNsM+Mj5aMdG8szMAjunzIwP2rfffW2HptKluuVXgTIlF17iqSVhFt O3ZvDDUWKLN0od0hxd7gLTa8iwHeNIE9ZxorOe8uVmdiXEAkkvg3WjzN6eBQDwf5nF+r W2XsZ096CLBxnY9Sl/gQWFxShilJqPzExmH94noTbIx5GWG+naKWvMHzI9vQWlNLrexH NVYSQeGWxgotSjNLfqLbB/EEFV7rSLbMXfRZIdofaMiPUOGHLME7+07JzIN6FIi1O7fV iHng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785144006; x=1785748806; h=content-transfer-encoding:content-type:in-reply-to:autocrypt :content-language:references:to:from:subject:user-agent:mime-version :date:message-id:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=cGXghtDdb1EheT91fh49eozejVsyI7Q5CF+fF/cYuxI=; b=YqEOeIuW+13G2JdytjOxvgmbbi1n8ug1DcMzFzwxdmwKqncy1iQGiLIKB9EOkZJ94a ugIfQAiumnUZ1nBytK1vOL7XZeV0yu8iVX0WxzenqcyOuk4ymeoNTQVgNoatDD1AyyCj oXi3naXfVhzJS03u2s1wyhRDiQ5JuBkmfbowSWOBftOzLUPxS+Gyr9eEa+4yVZ92jQqh X1HhayK5R/TBEVut53anS8fjyCwZtQcFccAHbEDmTP0rlUMfrtKsi3Mp35deSfgDWYHz M6mCPsKHLAEmAyI26F63ISv6r0d1o73ZzG86YTvDQ4f36XnWD5CN6FRe1DaUpiwwQkHB rXoA== X-Gm-Message-State: AOJu0YyjfoMgWH9TLP+TOb5lMaEMRDS4GDK09iC3nECBIkRRjbK0/OSM coILb4CaQRUzTg78kMmFxP+urSBSrutfOkUSvwbcdfjUiME7eUJPO5OmM/ZwcwAEUSeqnUH6MNg sCTbXgw== X-Gm-Gg: AR+sD11JyeG6HCjjj8PljNI5I2pdVFW6bY9+JWl+7ZLA0hKRpilpdCDleZV8VvvBh50 q4AF/0RA6MsHKZHA3GnSZ4DLGfJY7+mleZEV8mzCuAnZpcOx82egHAW7efp1JoYCjz5CRGPwWRX e85ROyElctKftzguM2BXiuLaROsBFxW9b+CZvmBAuwB9Z4irO84bQhQWYVfNwJd7OKLHd2OxfmP 8RiA2WCE2mZKPjEgj1IHvHvB0GEuuB4ai71h87YO68I6OjUSvfeT/EqabBrp1cC1HwDQs5a8uqx Gsnej374drMqO2hELLFVMV1t+rH6vBa3kIHa5RAJi8iblehmGRIbNDJrlkuASdcttdWnq27i50r Jj2VqSyKP0pQrGw6TfbovrEaePXErbnp8bxUpGWjJqFG6eTn7KOugpa5RB2ZIzJLKciNaIf19um DHHwQ6hY/ii9e1fkVmnv5dDQDTHLY3TocmR7gXWh7Cm4vxefjqKARO1Sm5yjvzau/W/A== X-Received: by 2002:a05:600c:c87:b0:495:3e81:76fa with SMTP id 5b1f17b1804b1-496b56ff0fdmr101596835e9.15.1785144006521; Mon, 27 Jul 2026 02:20:06 -0700 (PDT) Received: from [10.156.60.236] (ip-037-024-206-209.um08.pools.vodafone-ip.de. [37.24.206.209]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4957bffc501sm187516125e9.4.2026.07.27.02.20.06 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 27 Jul 2026 02:20:06 -0700 (PDT) Message-ID: Date: Mon, 27 Jul 2026 11:20:05 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: [PATCH 3/5] readelf: fold get_{32,64}bit_elf_symbols() From: Jan Beulich To: Binutils References: Content-Language: en-US Autocrypt: addr=jbeulich@suse.com; keydata= xsDiBFk3nEQRBADAEaSw6zC/EJkiwGPXbWtPxl2xCdSoeepS07jW8UgcHNurfHvUzogEq5xk hu507c3BarVjyWCJOylMNR98Yd8VqD9UfmX0Hb8/BrA+Hl6/DB/eqGptrf4BSRwcZQM32aZK 7Pj2XbGWIUrZrd70x1eAP9QE3P79Y2oLrsCgbZJfEwCgvz9JjGmQqQkRiTVzlZVCJYcyGGsD /0tbFCzD2h20ahe8rC1gbb3K3qk+LpBtvjBu1RY9drYk0NymiGbJWZgab6t1jM7sk2vuf0Py O9Hf9XBmK0uE9IgMaiCpc32XV9oASz6UJebwkX+zF2jG5I1BfnO9g7KlotcA/v5ClMjgo6Gl MDY4HxoSRu3i1cqqSDtVlt+AOVBJBACrZcnHAUSuCXBPy0jOlBhxPqRWv6ND4c9PH1xjQ3NP nxJuMBS8rnNg22uyfAgmBKNLpLgAGVRMZGaGoJObGf72s6TeIqKJo/LtggAS9qAUiuKVnygo 3wjfkS9A3DRO+SpU7JqWdsveeIQyeyEJ/8PTowmSQLakF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M_VALID_EF, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE, TXREP shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org X-BeenThere: binutils@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: binutils-bounces~patchwork=sourceware.org@sourceware.org PR binutils/34356 They're identical except for the types used and the order of fields processed. The latter doesn't matter for correctness, and the former can be addressed by compiling the same code twice. --- a/binutils/readelf.c +++ b/binutils/readelf.c @@ -8006,13 +8006,14 @@ get_section_headers (Filedata *filedata, return Elf64_get_section_headers (filedata, probe); } +#else /* ElfXX */ + static Elf_Internal_Sym * -get_32bit_elf_symbols (Filedata *filedata, - Elf_Internal_Shdr *section, - uint64_t *num_syms_return) +ElfXX(_get_symbols) (Filedata *filedata, const Elf_Internal_Shdr *section, + uint64_t *num_syms_return) { uint64_t number = 0; - Elf32_External_Sym * esyms = NULL; + ElfXX(_External_Sym) * esyms = NULL; Elf_External_Sym_Shndx * shndx = NULL; Elf_Internal_Sym * isyms = NULL; Elf_Internal_Sym * psym; @@ -8045,7 +8046,7 @@ get_32bit_elf_symbols (Filedata *filedat number = section->sh_size / section->sh_entsize; - if (number * sizeof (Elf32_External_Sym) > section->sh_size + 1) + if (number * sizeof (*esyms) > section->sh_size + 1) { error (_("Size (%#" PRIx64 ") of section %s " "is not a multiple of its sh_entsize (%#" PRIx64 ")\n"), @@ -8055,8 +8056,8 @@ get_32bit_elf_symbols (Filedata *filedat goto exit_point; } - esyms = (Elf32_External_Sym *) get_data (NULL, filedata, section->sh_offset, 1, - section->sh_size, _("symbols")); + esyms = get_data (NULL, filedata, section->sh_offset, 1, section->sh_size, + _("symbols")); if (esyms == NULL) goto exit_point; @@ -8104,11 +8105,13 @@ get_32bit_elf_symbols (Filedata *filedat psym->st_value = BYTE_GET (esyms[j].st_value); psym->st_size = BYTE_GET (esyms[j].st_size); psym->st_shndx = BYTE_GET (esyms[j].st_shndx); + if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL) psym->st_shndx = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j])); else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff)) psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff); + psym->st_info = BYTE_GET (esyms[j].st_info); psym->st_other = BYTE_GET (esyms[j].st_other); } @@ -8123,124 +8126,9 @@ get_32bit_elf_symbols (Filedata *filedat return isyms; } -static Elf_Internal_Sym * -get_64bit_elf_symbols (Filedata *filedata, - Elf_Internal_Shdr *section, - uint64_t *num_syms_return) -{ - uint64_t number = 0; - Elf64_External_Sym * esyms = NULL; - Elf_External_Sym_Shndx * shndx = NULL; - Elf_Internal_Sym * isyms = NULL; - Elf_Internal_Sym * psym; - unsigned int j; - elf_section_list * entry; - - if (section->sh_size == 0) - { - if (num_syms_return != NULL) - * num_syms_return = 0; - return NULL; - } - - /* Run some sanity checks first. */ - if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size) - { - error (_("Section %s has an invalid sh_entsize of %#" PRIx64 "\n"), - printable_section_name (filedata, section), - section->sh_entsize); - goto exit_point; - } - - if (section->sh_size > filedata->file_size) - { - error (_("Section %s has an invalid sh_size of %#" PRIx64 "\n"), - printable_section_name (filedata, section), - section->sh_size); - goto exit_point; - } - - number = section->sh_size / section->sh_entsize; - - if (number * sizeof (Elf64_External_Sym) > section->sh_size + 1) - { - error (_("Size (%#" PRIx64 ") of section %s " - "is not a multiple of its sh_entsize (%#" PRIx64 ")\n"), - section->sh_size, - printable_section_name (filedata, section), - section->sh_entsize); - goto exit_point; - } - - esyms = (Elf64_External_Sym *) get_data (NULL, filedata, section->sh_offset, 1, - section->sh_size, _("symbols")); - if (!esyms) - goto exit_point; - - shndx = NULL; - for (entry = filedata->symtab_shndx_list; entry != NULL; entry = entry->next) - { - if (entry->hdr->sh_link != (size_t) (section - filedata->section_headers)) - continue; - - if (shndx != NULL) - { - error (_("Multiple symbol table index sections associated with the same symbol section\n")); - free (shndx); - } - - shndx = (Elf_External_Sym_Shndx *) get_data (NULL, filedata, - entry->hdr->sh_offset, - 1, entry->hdr->sh_size, - _("symbol table section indices")); - if (shndx == NULL) - goto exit_point; - - /* PR17531: file: heap-buffer-overflow */ - if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number) - { - error (_("Index section %s has an sh_size of %#" PRIx64 " - expected %#" PRIx64 "\n"), - printable_section_name (filedata, entry->hdr), - entry->hdr->sh_size, - section->sh_size); - goto exit_point; - } - } - - isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym)); - - if (isyms == NULL) - { - error (_("Out of memory reading %" PRIu64 " symbols\n"), number); - goto exit_point; - } - - for (j = 0, psym = isyms; j < number; j++, psym++) - { - psym->st_name = BYTE_GET (esyms[j].st_name); - psym->st_info = BYTE_GET (esyms[j].st_info); - psym->st_other = BYTE_GET (esyms[j].st_other); - psym->st_shndx = BYTE_GET (esyms[j].st_shndx); - - if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL) - psym->st_shndx - = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j])); - else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff)) - psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff); - - psym->st_value = BYTE_GET (esyms[j].st_value); - psym->st_size = BYTE_GET (esyms[j].st_size); - } - - exit_point: - free (shndx); - free (esyms); - - if (num_syms_return != NULL) - * num_syms_return = isyms == NULL ? 0 : number; +#endif /* ElfXX */ - return isyms; -} +#ifndef ElfXX static Elf_Internal_Sym * get_elf_symbols (Filedata *filedata, @@ -8248,9 +8136,9 @@ get_elf_symbols (Filedata *filedata, uint64_t *num_syms_return) { if (is_32bit_elf) - return get_32bit_elf_symbols (filedata, section, num_syms_return); + return Elf32_get_symbols (filedata, section, num_syms_return); else - return get_64bit_elf_symbols (filedata, section, num_syms_return); + return Elf64_get_symbols (filedata, section, num_syms_return); } static const char *