From patchwork Mon Dec 8 10:44:45 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Wielaard X-Patchwork-Id: 126125 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 C7E1841F55C8 for ; Mon, 8 Dec 2025 10:45:11 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C7E1841F55C8 X-Original-To: elfutils-devel@sourceware.org Delivered-To: elfutils-devel@sourceware.org Received: from gnu.wildebeest.org (gnu.wildebeest.org [45.83.234.184]) by sourceware.org (Postfix) with ESMTPS id EDF6C41F5737 for ; Mon, 8 Dec 2025 10:44:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EDF6C41F5737 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=klomp.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=klomp.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org EDF6C41F5737 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=45.83.234.184 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1765190697; cv=none; b=PCMvTF189O8h268XtOS5r6EuRhMBsfEzcUDauNBehSMDDrj1gMZrQZy/4udyDlo+1+x/5lYRddvZPGWOW+uYcl2d5jn+zbaSrM+7iwBg3htrFfivEsW0VRYUags8/srDcfgbCWlu8VcY6dHVA6tFNekVE4NH8ePoCXPSY6iyDGA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1765190697; c=relaxed/simple; bh=5cL4syjUG7/G/T+2J34rGqO9Sq84gIZA8ikG0vbCXRA=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=EUQCvvFDXdenDgKcaKJdOqz7hexl2bs/UZyv48vMj+Rz4pi/pwF4+ICul+XQDnFq7oGx+sWAp4iPnoa04vhkM7ug6g9cL80kQc5LazbBg1vCxdo5JtkwNaPLvLbyWkrXg9OGAR0iG8BHvg64/9ED+CBmx9/eqWQaWUv7GcAh1T8= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EDF6C41F5737 Received: from mwielaar-thinkpadp1gen3.rmtnl.csb (deer0x08.wildebeest.org [172.31.17.138]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by gnu.wildebeest.org (Postfix) with ESMTPSA id 86F8E3032F85; Mon, 8 Dec 2025 11:44:55 +0100 (CET) Received: by mwielaar-thinkpadp1gen3.rmtnl.csb (Postfix, from userid 10916) id 5292215F4C5A; Mon, 08 Dec 2025 11:44:55 +0100 (CET) From: Mark Wielaard To: elfutils-devel@sourceware.org Cc: Mark Wielaard Subject: [PATCH] libelf: Add gelf_fsize main check Date: Mon, 8 Dec 2025 11:44:45 +0100 Message-ID: <20251208104445.2785906-1-mark@klomp.org> X-Mailer: git-send-email 2.51.1 MIME-Version: 1.0 X-Spam-Status: No, score=-8.7 required=5.0 tests=BAYES_00, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_DMARC_STATUS, SPF_HELO_NONE, SPF_PASS, TXREP 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: elfutils-devel@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Elfutils-devel mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: elfutils-devel-bounces~patchwork=sourceware.org@sourceware.org This test itself isn't super interesting, it just checks that all Elf_Types are handled by gelf_fsize and that the 32 vs 64 variant sizes make sense. The interesting part is that it uses the internal interface (__libelf_type_sizes) to do it. So you don't have to contruct a whole Elf handle. It mimics the support for "main checks" in libdw. It adds a way to compile an individual source file with an optional main function that can directly access the internal/static functions. To add new main check tests you have to add an #ifdef MAIN_CHECK with a main function that calls the test functions to the source file. And add two make file rules after .SECONDEXPANSION. One starting with _checks$(EXEEXT) and one starting with nodist__check_SOURCES. * libelf/gelf_fsize.c: Add MAIN_CHECK part. * libelf/Makefile.am: Add gelf_fsize main check rules. Signed-off-by: Mark Wielaard --- libelf/Makefile.am | 13 +++++++++++++ libelf/gelf_fsize.c | 46 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/libelf/Makefile.am b/libelf/Makefile.am index 05484c12c0a0..cc539c8dab3c 100644 --- a/libelf/Makefile.am +++ b/libelf/Makefile.am @@ -1,6 +1,7 @@ ## Process this file with automake to create Makefile.in ## ## Copyright (C) 1996-2010, 2015 Red Hat, Inc. +## Copyright (C) 2025 Mark J. Wielaard ## This file is part of elfutils. ## ## This file is free software; you can redistribute it and/or modify @@ -145,3 +146,15 @@ libelf.manifest: $(libelf_a_OBJECTS) MOSTLYCLEANFILES = $(am_libelf_a_OBJECTS) $(am_libelf_pic_a_OBJECTS) libelf.so.$(VERSION) CLEANFILES = libelf.so $(EXTRA_libelf_a_DEPENDENCIES) + +# Internal checks +check_PROGRAMS = gelf_fsize_check +TESTS = $(check_PROGRAMS) + +CHECK_DEF_FLAGS = $(DEFS) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) \ + $(DEFAULT_INCLUDES) -Wl,-rpath,../libelf -DMAIN_CHECK=1 + +.SECONDEXPANSION: +gelf_fsize_check$(EXEEXT): $$(filter-out $$(subst _check,,$$@).o,$(libelf_a_OBJECTS)) $$(subst _check,,$$@).c + $(AM_V_CC)$(CC) $(CHECK_DEF_FLAGS) -o $@ $^ $(filter-out libelf_pic.a,$(libelf_so_LIBS)) $(libelf_so_LDLIBS) +nodist_gelf_fsize_check_SOURCES = gelf_fsize_check.c diff --git a/libelf/gelf_fsize.c b/libelf/gelf_fsize.c index 63bcbae51a1d..6f99d83979aa 100644 --- a/libelf/gelf_fsize.c +++ b/libelf/gelf_fsize.c @@ -1,5 +1,6 @@ /* Return the size of an object file type. Copyright (C) 1998-2010, 2015 Red Hat, Inc. + Copyright (C) 2025 Mark J. Wielaard This file is part of elfutils. Written by Ulrich Drepper , 1998. @@ -102,3 +103,48 @@ gelf_fsize (Elf *elf, Elf_Type type, size_t count, unsigned int version) return count * __libelf_type_sizes[elf->class - 1][type]; } INTDEF(gelf_fsize) + + +#ifdef MAIN_CHECK +#include +#include + +void +test_fsize (Elf_Type type) +{ + printf ("Testing 0x%" PRIx32 ": ", type); + + int fsize32 = __libelf_type_sizes[ELFCLASS32 - 1][type]; + if (fsize32 <= 0) + { + printf ("Unhandled type %" PRIx32 " for ELFCLASS32\n", type); + exit (-1); + } + printf (" %d, ", fsize32); + + int fsize64 = __libelf_type_sizes[ELFCLASS64 - 1][type]; + if (fsize64 <= 0) + { + printf ("Unhandled type %" PRIx32 " for ELFCLASS64\n", type); + exit (-1); + } + printf ("%d\n", fsize64); + + /* In theory fsize for 32 bit could of course be larger than fsize + for 64 bit. But if so, better adjust this testcase and + explain. */ + if (fsize32 > fsize64) + { + printf ("fsize32 %d > fsize64 %d\n", fsize32, fsize64); + exit(-1); + } +} + +int +main (void) +{ + for (Elf_Type type = 0; type < ELF_T_NUM; type++) + test_fsize (type); + return 0; +} +#endif