From ad49149131b7552cf1c4fc41370cc055d9b4a80e Mon Sep 17 00:00:00 2001 From: riastradh Date: Mon, 1 Apr 2024 18:33:22 +0000 Subject: [PATCH] elftoolchain: Be consistent about which ELF header files we use. 1. For tools that use elftoolchain: always use elftoolchain's elfdefinitions.h. Don't even think about looking at the host's sys/exec_elf.h, which makes no sense and should never happen. (ELF tools that don't use elftoolchain, like m68k-elf2coff, continue to use nbincludes/sys/exec_elf.h. But no more nbincludes hacks in elftoolchain.) 2. For kernel components (solaris, zfs, dtrace): always use sys/exec_elf.h, even in Solaris components via sys/elf.h. elfdefinitions.h is not wired up in the kernel build at all. 3. For most userland components that involve libelf: use elfdefinitions.h via libelf header files (libelf.h, gelf.h). libdtrace in particular requires _all_ R_* reloc type definitions, but sys/exec_elf.h brings in only the _current machine's_ R_* reloc type definitions. (While here: Use uintptr_t instead of Elf_Addr for pointer-to-integer cast, since Elf_Addr is MD and provided only by sys/exec_elf.h, not by elfdefinitions.h.) And most userland components using libelf don't rely on any properties of the current machine from sys/exec_elf.h, so they can use libelf's elfdefinition.h. Exceptions: - dtrace drti.c relies on link.h -> link_elf.h -> sys/exec_elf.h, but it also relies on sys/dtrace.h -> sys/elf.h -> elfdefinitions.h like other userland components using sys/elf.h. - kdump-ioctl.c uses sys/exec_elf.h directly and sys/dtrace.h -> sys/elf.h -> elfdefinitions like other userland components using sys/elf.h. - t_ptrace_wait.c (via t_ptrace_core_wait.h) uses libelf to parse core files, but relies on sys/exec_elf.h for struct netbsd_elfcore_procinfo. None of these exceptions needs all R_* reloc type definitions, so as a workaround, we can just suppress libelf's elfdefinitions.h by defining _SYS_ELFDEFINITIONS_H_ and use sys/exec_elf.h in these exceptions. And undo the whole BUILTIN_ELF_HEADERS mistake. This was: - half bogus workarounds for missing build_install dependencies in tools/Makefile, which are no longer missing now, and - half futile attempt to use src/sys/sys/exec_elf.h via nbincludes in tools involving libelf instead of libelf's elfdefinitions.h, which collides. Longer-term, we may wish to unify sys/exec_elf.h and libelf's elfdefinitions.h, so we don't have to play these games. But at least now the games are limited to three .c files (one of which is generated by Makefile.ioctl-c), rather than haphazardly applied tree-wide by monstrous kludges in widely used .h files with broken hackarounds to get the tools build lurching to completion. --- .../elftoolchain/dist/libelf/_libelf_config.h | 26 +------------------ .../bsd/elftoolchain/dist/libelf/libelf.h | 22 +++------------- .../bsd/elftoolchain/lib/libdwarf/Makefile | 1 - .../osnet/dist/lib/libdtrace/common/drti.c | 19 ++++++++++++++ .../dist/lib/libdtrace/common/dt_module.c | 4 +-- external/cddl/osnet/sys/elf.h | 8 +++--- external/cddl/osnet/sys/sys/elf.h | 21 ++++++++++++--- tests/lib/libc/sys/t_ptrace_wait.c | 19 ++++++++++++-- tools/ctfconvert/Makefile | 6 ++--- tools/ctfmerge/Makefile | 6 ++--- tools/elftoolchain/libdwarf/Makefile | 5 ++-- tools/elftoolchain/libelf/Makefile | 4 +-- tools/libctf/Makefile | 3 +-- usr.bin/kdump/Makefile.ioctl-c | 3 ++- usr.sbin/tprof/ksyms.c | 6 ++--- 15 files changed, 81 insertions(+), 72 deletions(-) diff --git a/external/bsd/elftoolchain/dist/libelf/_libelf_config.h b/external/bsd/elftoolchain/dist/libelf/_libelf_config.h index fa648210ad1b..44a574a3b41b 100644 --- a/external/bsd/elftoolchain/dist/libelf/_libelf_config.h +++ b/external/bsd/elftoolchain/dist/libelf/_libelf_config.h @@ -1,4 +1,4 @@ -/* $NetBSD: _libelf_config.h,v 1.6 2024/03/03 17:37:33 christos Exp $ */ +/* $NetBSD: _libelf_config.h,v 1.7 2024/04/01 18:33:22 riastradh Exp $ */ /*- * Copyright (c) 2008-2011 Joseph Koshy @@ -28,28 +28,6 @@ * Id: _libelf_config.h 3975 2022-04-30 20:10:58Z jkoshy */ -#if defined(__NetBSD__) || defined(HAVE_NBTOOL_CONFIG_H) - -#include - -#if !defined(ARCH_ELFSIZE) -#error ARCH_ELFSIZE is not defined. -#endif - -#if ARCH_ELFSIZE == 32 -#define LIBELF_ARCH ELF32_MACHDEP_ID -#define LIBELF_BYTEORDER ELF32_MACHDEP_ENDIANNESS -#define LIBELF_CLASS ELFCLASS32 -#define Elf_Note Elf32_Nhdr -#else -#define LIBELF_ARCH ELF64_MACHDEP_ID -#define LIBELF_BYTEORDER ELF64_MACHDEP_ENDIANNESS -#define LIBELF_CLASS ELFCLASS64 -#define Elf_Note Elf64_Nhdr -#endif - -#endif /* __NetBSD__ || HAVE_NBTOOL_CONFIG_H */ - /* * Downstream projects can replace the following placeholder with a custom * definition of LIBELF_BYTEORDER, if the host's native byte order cannot @@ -57,7 +35,6 @@ */ /* @LIBELF-DEFINE-HOST-BYTEORDER@ */ -#ifndef HAVE_NBTOOL_CONFIG_H #if !defined(LIBELF_BYTEORDER) /* @@ -79,4 +56,3 @@ #endif /* defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) */ #endif /* !defined(LIBELF_BYTEORDER) */ -#endif /* HAVE_NBTOOL_CONFIG_H */ diff --git a/external/bsd/elftoolchain/dist/libelf/libelf.h b/external/bsd/elftoolchain/dist/libelf/libelf.h index 2452460959f5..bdae2fa719bb 100644 --- a/external/bsd/elftoolchain/dist/libelf/libelf.h +++ b/external/bsd/elftoolchain/dist/libelf/libelf.h @@ -1,4 +1,4 @@ -/* $NetBSD: libelf.h,v 1.6 2024/03/03 17:37:34 christos Exp $ */ +/* $NetBSD: libelf.h,v 1.7 2024/04/01 18:33:22 riastradh Exp $ */ /*- * Copyright (c) 2006,2008-2010 Joseph Koshy @@ -33,26 +33,12 @@ #if HAVE_NBTOOL_CONFIG_H # include "nbtool_config.h" -#endif - - -#ifdef BUILTIN_ELF_HEADERS -# include -# include -# include "elfdefinitions.h" -#elif HAVE_NBTOOL_CONFIG_H -# include -#elif defined(__NetBSD__) -# include -# include -#elif defined(__FreeBSD__) -# include -# include -# include #else - #error "No valid elf headers" +# include #endif +#include "elfdefinitions.h" + /* Library private data structures */ typedef struct _Elf Elf; typedef struct _Elf_Scn Elf_Scn; diff --git a/external/bsd/elftoolchain/lib/libdwarf/Makefile b/external/bsd/elftoolchain/lib/libdwarf/Makefile index 63e42753ef51..5ffbb9ccd1f4 100644 --- a/external/bsd/elftoolchain/lib/libdwarf/Makefile +++ b/external/bsd/elftoolchain/lib/libdwarf/Makefile @@ -82,7 +82,6 @@ GENSRCS= dwarf_pubnames.c dwarf_pubtypes.c dwarf_weaks.c \ dwarf_pro_funcs.c dwarf_pro_types.c \ dwarf_pro_vars.c CLEANFILES= ${GENSRCS} -CPPFLAGS+= -DBUILTIN_ELF_HEADERS CPPFLAGS+= -I. -I${SRCDIR} -I${TOP}/common -I${TOP}/libelf LIBDPLIBS+= elf ${.CURDIR}/../libelf diff --git a/external/cddl/osnet/dist/lib/libdtrace/common/drti.c b/external/cddl/osnet/dist/lib/libdtrace/common/drti.c index bc639f20eaad..af25f1a05db0 100644 --- a/external/cddl/osnet/dist/lib/libdtrace/common/drti.c +++ b/external/cddl/osnet/dist/lib/libdtrace/common/drti.c @@ -24,6 +24,25 @@ * Use is subject to license terms. */ +/* + * XXX Hack: Force the use of sys/exec_elf.h, not elfdefinitions.h. + * Why? + * + * - sys/dtrace.h is needed because (XXX why?). + * => sys/dtrace.h pulls in sys/ctf_api.h + * => which pulls in sys/elf.h + * => which pulls in elfdefinitions.h (XXX should it?) + * + * - link.h is needed for Link_map. + * => link.h pulls in link_elf.h + * => which pulls in sys/exec_elf.h + * + * But elfdefinitions.h and sys/exec_elf.h collide over most basic ELF + * definitions. Since we don't need elfdefinitions.h otherwise, prefer + * sys/exec_elf.h. + */ +#define _SYS_ELFDEFINITIONS_H_ + #include #include #include diff --git a/external/cddl/osnet/dist/lib/libdtrace/common/dt_module.c b/external/cddl/osnet/dist/lib/libdtrace/common/dt_module.c index f7356fabfdcf..9c5ae64315eb 100644 --- a/external/cddl/osnet/dist/lib/libdtrace/common/dt_module.c +++ b/external/cddl/osnet/dist/lib/libdtrace/common/dt_module.c @@ -125,7 +125,7 @@ dt_module_syminit32(dt_module_t *dmp) dmp->dm_sec_offsets[sym->st_shndx]; #endif #ifdef __NetBSD__ - sym->st_value += (Elf_Addr) dmp->dm_reloc_offset; + sym->st_value += (uintptr_t) dmp->dm_reloc_offset; #endif } @@ -177,7 +177,7 @@ dt_module_syminit64(dt_module_t *dmp) dmp->dm_sec_offsets[sym->st_shndx]; #endif #ifdef __NetBSD__ - sym->st_value += (Elf_Addr) dmp->dm_reloc_offset; + sym->st_value += (uintptr_t) dmp->dm_reloc_offset; #endif } diff --git a/external/cddl/osnet/sys/elf.h b/external/cddl/osnet/sys/elf.h index 9cde17aa6612..8595e93161f9 100644 --- a/external/cddl/osnet/sys/elf.h +++ b/external/cddl/osnet/sys/elf.h @@ -1,4 +1,4 @@ -/* $NetBSD: elf.h,v 1.1 2010/02/21 01:46:35 darran Exp $ */ +/* $NetBSD: elf.h,v 1.2 2024/04/01 18:33:23 riastradh Exp $ */ /*- * Copyright (c) 2009 The NetBSD Foundation, Inc. @@ -30,9 +30,11 @@ #define _ELF_H_ #if HAVE_NBTOOL_CONFIG_H -#include +# include "nbtool_config.h" #else -#include +# include #endif +#include "elfdefinitions.h" + #endif diff --git a/external/cddl/osnet/sys/sys/elf.h b/external/cddl/osnet/sys/sys/elf.h index 92f43cb35390..479aca6f47e6 100644 --- a/external/cddl/osnet/sys/sys/elf.h +++ b/external/cddl/osnet/sys/sys/elf.h @@ -1,4 +1,4 @@ -/* $NetBSD: elf.h,v 1.6 2018/05/28 21:05:10 chs Exp $ */ +/* $NetBSD: elf.h,v 1.7 2024/04/01 18:33:23 riastradh Exp $ */ /* * CDDL HEADER START @@ -30,12 +30,25 @@ #define _SYS__ELF_SOLARIS_H_ #if HAVE_NBTOOL_CONFIG_H -#include +# include "nbtool_config.h" #else -#include +# include +#endif + +/* + * XXX In the kernel (e.g., zfs module build, or anything else using + * sys/dtrace.h -> sys/ctf_api.h -> sys/elf.h), sys/elfdefinitions.h + * isn't available -- it's a header file for the userland libelf. We + * could maybe make it available, but sys/exec_elf.h -- which would + * collide with sys/elfdefinitions.h -- provides all the definitions + * users need, so we'll just use that for now. + */ +#ifdef _KERNEL +# include +#else +# include #endif -#define SHT_SUNW_dof 0x6ffffff4 #define EM_AMD64 EM_X86_64 #define __ELF_WORD_SIZE ELFSIZE diff --git a/tests/lib/libc/sys/t_ptrace_wait.c b/tests/lib/libc/sys/t_ptrace_wait.c index 69874423d2ee..b2686320f422 100644 --- a/tests/lib/libc/sys/t_ptrace_wait.c +++ b/tests/lib/libc/sys/t_ptrace_wait.c @@ -1,4 +1,4 @@ -/* $NetBSD: t_ptrace_wait.c,v 1.191 2020/05/05 02:06:08 kamil Exp $ */ +/* $NetBSD: t_ptrace_wait.c,v 1.192 2024/04/01 18:33:23 riastradh Exp $ */ /*- * Copyright (c) 2016, 2017, 2018, 2019, 2020 The NetBSD Foundation, Inc. @@ -26,8 +26,23 @@ * POSSIBILITY OF SUCH DAMAGE. */ +/* + * XXX Hack: Force the use of sys/exec_elf.h, not elfdefinitions.h. + * Why? + * + * - libelf.h and gelf.h are needed for parsing core files in + * t_ptrace_core_wait.h. + * + * - sys/exec_elf.h is needed for struct netbsd_elfcore_procinfo also + * in t_ptrace_core_wait.h. + * + * libelf.h and gelf.h pull in elfdefinitions.h, but that conflicts + * with sys/exec_elf.h on most basic ELF definitions. + */ +#define _SYS_ELFDEFINITIONS_H_ + #include -__RCSID("$NetBSD: t_ptrace_wait.c,v 1.191 2020/05/05 02:06:08 kamil Exp $"); +__RCSID("$NetBSD: t_ptrace_wait.c,v 1.192 2024/04/01 18:33:23 riastradh Exp $"); #define __LEGACY_PT_LWPINFO diff --git a/tools/ctfconvert/Makefile b/tools/ctfconvert/Makefile index 357104aa0043..2dcfebb63fdf 100644 --- a/tools/ctfconvert/Makefile +++ b/tools/ctfconvert/Makefile @@ -1,4 +1,4 @@ -# $NetBSD: Makefile,v 1.10 2022/04/18 19:40:06 jkoshy Exp $ +# $NetBSD: Makefile,v 1.11 2024/04/01 18:33:23 riastradh Exp $ .include @@ -37,7 +37,8 @@ HOST_CPPFLAGS+= -I${OSNETDIR}/sys \ -I${OPENSOLARIS_DISTDIR}/tools/ctf/cvt \ -I${OPENSOLARIS_DISTDIR}/uts/common \ -I${LIBELF_DIR} \ - -I${LIBDWARF_DIR} + -I${LIBDWARF_DIR} \ + -I${TOOLDIR}/include HOST_CPPFLAGS+= -DHAVE_NBTOOL_CONFIG_H=1 @@ -57,5 +58,4 @@ DPADD+= ${ELFOBJ}/libelf.a .PATH: ${OPENSOLARIS_DISTDIR}/tools/ctf/common .PATH: ${OPENSOLARIS_DISTDIR}/tools/ctf/cvt -.include "${.CURDIR}/../Makefile.nbincludes" .include "${.CURDIR}/../Makefile.host" diff --git a/tools/ctfmerge/Makefile b/tools/ctfmerge/Makefile index cefb7179f857..928b367cc65d 100644 --- a/tools/ctfmerge/Makefile +++ b/tools/ctfmerge/Makefile @@ -1,4 +1,4 @@ -# $NetBSD: Makefile,v 1.11 2022/04/18 19:40:06 jkoshy Exp $ +# $NetBSD: Makefile,v 1.12 2024/04/01 18:33:23 riastradh Exp $ .include @@ -17,7 +17,8 @@ HOST_CPPFLAGS+= -I${OSNETDIR}/sys \ -I${OPENSOLARIS_DISTDIR}/tools/ctf/cvt \ -I${OPENSOLARIS_DISTDIR}/uts/common \ -I${LIBELF_DIR} \ - -I${LIBDWARF_DIR} + -I${LIBDWARF_DIR} \ + -I${TOOLDIR}/include HOST_CPPFLAGS+= -DHAVE_NBTOOL_CONFIG_H=1 @@ -37,5 +38,4 @@ HOST_CFLAGS+= -pthread .PATH: ${OPENSOLARIS_DISTDIR}/tools/ctf/common .PATH: ${OPENSOLARIS_DISTDIR}/tools/ctf/cvt -.include "${.CURDIR}/../Makefile.nbincludes" .include "${.CURDIR}/../Makefile.host" diff --git a/tools/elftoolchain/libdwarf/Makefile b/tools/elftoolchain/libdwarf/Makefile index 5164e89fafc3..29e3ec669b09 100644 --- a/tools/elftoolchain/libdwarf/Makefile +++ b/tools/elftoolchain/libdwarf/Makefile @@ -1,4 +1,4 @@ -# $NetBSD: Makefile,v 1.6 2024/03/30 22:39:53 riastradh Exp $ +# $NetBSD: Makefile,v 1.7 2024/04/01 18:33:23 riastradh Exp $ HOSTLIB=dwarf @@ -73,12 +73,11 @@ COMPATLIB_NO_LIB= yes # only the include files, not the library LIBDWARF_OBJDIR!= cd ${.CURDIR} && ${PRINTOBJDIR} -CPPFLAGS+= -DBUILTIN_ELF_HEADERS CPPFLAGS+= -I${.CURDIR}/../../compat CPPFLAGS+= -I${.CURDIR}/../../common CPPFLAGS+= -I${LIBDWARF_OBJDIR}/../common CPPFLAGS+= -I${LIBDWARF_DIR} -I${LIBELF_DIR} -I${COMMON_DIR} -CPPFLAGS+= -I${TOOLDIR}/include -I${TOOLDIR}/include/nbinclude +CPPFLAGS+= -I${TOOLDIR}/include BUILD_OSTYPE!= uname -s diff --git a/tools/elftoolchain/libelf/Makefile b/tools/elftoolchain/libelf/Makefile index b4a911a5267e..af02ee042d9d 100644 --- a/tools/elftoolchain/libelf/Makefile +++ b/tools/elftoolchain/libelf/Makefile @@ -1,4 +1,4 @@ -# $NetBSD: Makefile,v 1.2 2024/03/03 17:37:58 christos Exp $ +# $NetBSD: Makefile,v 1.3 2024/04/01 18:33:24 riastradh Exp $ HOSTLIB= elf @@ -54,7 +54,7 @@ CLEANFILES+= ${LIBELF_GENSRCS} CPPFLAGS+= -I${.CURDIR}/../../compat CPPFLAGS+= -I${LIBELF_DIR} -I${LIBELF_DIR}/../common CPPFLAGS+= -I${LIBELF_OBJDIR}/../common -CPPFLAGS+= -I${TOOLDIR}/include -I${TOOLDIR}/include/nbinclude +CPPFLAGS+= -I${TOOLDIR}/include CPPFLAGS+= -DLIBELF_TEST_HOOKS diff --git a/tools/libctf/Makefile b/tools/libctf/Makefile index 374f97751e24..f7068d48bf35 100644 --- a/tools/libctf/Makefile +++ b/tools/libctf/Makefile @@ -1,4 +1,4 @@ -# $NetBSD: Makefile,v 1.9 2022/05/03 10:50:42 jkoshy Exp $ +# $NetBSD: Makefile,v 1.10 2024/04/01 18:33:24 riastradh Exp $ HOSTLIB= ctf @@ -62,5 +62,4 @@ ${TOOLDIR}/lib/libctf.a:: libctf.a mkdir -p ${TOOLDIR}/lib ${HOST_INSTALL_FILE} -m ${BINMODE} libctf.a ${.TARGET} -.include "${.CURDIR}/../Makefile.nbincludes" .include diff --git a/usr.bin/kdump/Makefile.ioctl-c b/usr.bin/kdump/Makefile.ioctl-c index 98f753a13617..0bb624ee0560 100644 --- a/usr.bin/kdump/Makefile.ioctl-c +++ b/usr.bin/kdump/Makefile.ioctl-c @@ -1,4 +1,4 @@ -# $NetBSD: Makefile.ioctl-c,v 1.39 2022/11/12 02:20:16 mrg Exp $ +# $NetBSD: Makefile.ioctl-c,v 1.40 2024/04/01 18:33:24 riastradh Exp $ # NOTE: needs to be previously .included for NETBSDSRCDIR @@ -63,6 +63,7 @@ CPPFLAGS+= -D__SAVAGE_DRM_H__ # Dup ioctls #CPPFLAGS+= -D__RADEON_DRM_H__ # Dup ioctls CPPFLAGS+= -D__MACH64_DRM_H__ # Dup ioctls CPPFLAGS+= -D__MGA_DRM_H__ # Dup ioctls +CPPFLAGS+= -D_SYS_ELFDEFINITIONS_H_ # collides with sys/exec_elf.h ${PROG}-ioctl.c: mkioctls Makefile ${DEPFILES} ${SETFILES} ${_MKTARGET_CREATE} diff --git a/usr.sbin/tprof/ksyms.c b/usr.sbin/tprof/ksyms.c index 5d7492ea1e47..a674db2457e0 100644 --- a/usr.sbin/tprof/ksyms.c +++ b/usr.sbin/tprof/ksyms.c @@ -1,4 +1,4 @@ -/* $NetBSD: ksyms.c,v 1.2 2022/12/01 00:43:27 ryo Exp $ */ +/* $NetBSD: ksyms.c,v 1.3 2024/04/01 18:33:24 riastradh Exp $ */ /* * Copyright (c) 2010,2011,2012 YAMAMOTO Takashi, @@ -28,7 +28,7 @@ #include #ifndef lint -__RCSID("$NetBSD: ksyms.c,v 1.2 2022/12/01 00:43:27 ryo Exp $"); +__RCSID("$NetBSD: ksyms.c,v 1.3 2024/04/01 18:33:24 riastradh Exp $"); #endif /* not lint */ #include @@ -118,7 +118,7 @@ ksymload(size_t *nsymp) if (st == NULL) { goto elffail; } - if (ELF_ST_TYPE(st->st_info) != STT_FUNC) { + if (GELF_ST_TYPE(st->st_info) != STT_FUNC) { continue; } sym = emalloc(sizeof(*sym));