11.144Sriastrad/*	$NetBSD: exec_elf32.c,v 1.144 2024/12/06 16:19:41 riastradh Exp $	*/
21.1Sfvdl
31.1Sfvdl/*
41.9Scgd * Copyright (c) 1996 Christopher G. Demetriou
51.1Sfvdl * All rights reserved.
61.1Sfvdl *
71.1Sfvdl * Redistribution and use in source and binary forms, with or without
81.1Sfvdl * modification, are permitted provided that the following conditions
91.1Sfvdl * are met:
101.1Sfvdl * 1. Redistributions of source code must retain the above copyright
111.1Sfvdl *    notice, this list of conditions and the following disclaimer.
121.1Sfvdl * 2. Redistributions in binary form must reproduce the above copyright
131.1Sfvdl *    notice, this list of conditions and the following disclaimer in the
141.1Sfvdl *    documentation and/or other materials provided with the distribution.
151.137Sad * 3. All advertising materials mentioning features or use of this software
161.137Sad *    must display the following acknowledgement:
171.137Sad *      This product includes software developed by Christopher G. Demetriou
181.137Sad *	for the NetBSD Project.
191.137Sad * 4. The name of the author may not be used to endorse or promote products
201.1Sfvdl *    derived from this software without specific prior written permission
211.1Sfvdl *
221.1Sfvdl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
231.1Sfvdl * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
241.1Sfvdl * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
251.1Sfvdl * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
261.1Sfvdl * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
271.1Sfvdl * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
281.1Sfvdl * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
291.1Sfvdl * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
301.1Sfvdl * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
311.1Sfvdl * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
321.1Sfvdl */
331.69Slukem
341.69Slukem#include <sys/cdefs.h>
351.144Sriastrad__KERNEL_RCSID(0, "$NetBSD: exec_elf32.c,v 1.144 2024/12/06 16:19:41 riastradh Exp $");
361.30Sthorpej
371.137Sad#define	ELFSIZE	32
381.8Schristos
391.137Sad#include "exec_elf.c"
401.90Schristos
411.137Sad#include <sys/module.h>
421.128Schristos
431.142Schristos#define ELF32_AUXSIZE (ELF_AUX_ENTRIES * sizeof(Aux32Info) \
441.142Schristos    + MAXPATHLEN + ALIGN(1))
451.137Sad
461.143SpgoyetteMODULE(MODULE_CLASS_EXEC, exec_elf32, NULL);
471.137Sad
481.137Sadstatic struct execsw exec_elf32_execsw[] = {
491.139Schristos	{
501.139Schristos		.es_hdrsz = sizeof (Elf32_Ehdr),
511.139Schristos	  	.es_makecmds = exec_elf32_makecmds,
521.139Schristos	  	.u = {
531.139Schristos			.elf_probe_func = netbsd_elf32_probe,
541.139Schristos		},
551.139Schristos		.es_emul = &emul_netbsd,
561.141Smaxv		.es_prio = EXECSW_PRIO_FIRST,
571.139Schristos		.es_arglen = ELF32_AUXSIZE,
581.139Schristos		.es_copyargs = elf32_copyargs,
591.139Schristos		.es_setregs = NULL,
601.139Schristos		.es_coredump = coredump_elf32,
611.140Srjs		.es_setup_stack = exec_setup_stack,
621.139Schristos	},
631.137Sad#if EXEC_ELF_NOTELESS
641.139Schristos	{
651.139Schristos		.es_hdrsz = sizeof (Elf32_Ehdr),
661.139Schristos	  	.es_makecmds = exec_elf32_makecmds,
671.139Schristos	  	.u {
681.139Schristos			elf_probe_func = NULL,
691.139Schristos		},
701.139Schristos		.es_emul = &emul_netbsd,
711.139Schristos		.es_prio = EXECSW_PRIO_LAST,
721.139Schristos		.es_arglen = ELF32_AUXSIZE,
731.139Schristos		.es_copyargs = elf32_copyargs,
741.139Schristos		.es_setregs = NULL,
751.139Schristos		.es_coredump = coredump_elf32,
761.139Schristos		.es_setup_stack = exec_setup_stack,
771.139Schristos	},
781.137Sad#endif
791.137Sad};
801.137Sad
811.137Sadstatic int
821.137Sadexec_elf32_modcmd(modcmd_t cmd, void *arg)
831.137Sad{
841.141Smaxv#if ARCH_ELFSIZE == 64
851.141Smaxv	/*
861.141Smaxv	 * If we are on a 64bit system, we don't want the 32bit execsw[] to be
871.141Smaxv	 * added in the global array, because the exec_elf32 module only works
881.141Smaxv	 * on 32bit systems.
891.141Smaxv	 *
901.141Smaxv	 * However, we need the exec_elf32 module, because it will make the 32bit
911.141Smaxv	 * functions available for netbsd32 and linux32.
921.141Smaxv	 *
931.141Smaxv	 * Therefore, allow this module on 64bit systems, but make it dormant.
941.141Smaxv	 */
951.137Sad
961.141Smaxv	(void)exec_elf32_execsw; /* unused */
971.141Smaxv
981.141Smaxv	switch (cmd) {
991.141Smaxv	case MODULE_CMD_INIT:
1001.141Smaxv	case MODULE_CMD_FINI:
1011.141Smaxv		return 0;
1021.141Smaxv	default:
1031.144Sriastrad		return SET_ERROR(ENOTTY);
1041.141Smaxv	}
1051.141Smaxv#else /* ARCH_ELFSIZE == 64 */
1061.137Sad	switch (cmd) {
1071.137Sad	case MODULE_CMD_INIT:
1081.137Sad		return exec_add(exec_elf32_execsw,
1091.137Sad		    __arraycount(exec_elf32_execsw));
1101.137Sad
1111.137Sad	case MODULE_CMD_FINI:
1121.137Sad		return exec_remove(exec_elf32_execsw,
1131.137Sad		    __arraycount(exec_elf32_execsw));
1141.1Sfvdl
1151.1Sfvdl	default:
1161.144Sriastrad		return SET_ERROR(ENOTTY);
1171.137Sad        }
1181.141Smaxv#endif /* ARCH_ELFSIZE == 64 */
1191.1Sfvdl}
120