xref: /illumos-gate/usr/src/cmd/sgs/libconv/common/arch.c (revision 1a2d662a91cee3bf82f41cd47c7ae6f3825d9db2)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include	<stdio.h>
28 #include	<unistd.h>
29 #include	<string.h>
30 #include	<sys/systeminfo.h>
31 #include	"_conv.h"
32 #include	"arch_msg.h"
33 
34 /*
35  * Determine if the 32-bit or 64-bit kernel is running.
36  * Return the corresponding EI_CLASS constant.
37  */
38 int
39 conv_sys_eclass(void)
40 {
41 	char buf[BUFSIZ];
42 
43 	/*
44 	 * SI_ISALIST will return -1 on pre-2.6 machines,
45 	 * which is fine - it can't be a 64-bit kernel.
46 	 */
47 	if (sysinfo(SI_ISALIST, buf, BUFSIZ) == -1)
48 		return (ELFCLASS32);
49 
50 	if ((strstr(buf, MSG_ORIG(MSG_ARCH_SPARCV9)) != NULL) ||
51 	    (strstr(buf, MSG_ORIG(MSG_ARCH_AMD64)) != NULL))
52 		return (ELFCLASS64);
53 
54 	return (ELFCLASS32);
55 }
56 
57 #if	defined(_LP64)
58 /* ARGSUSED */
59 uchar_t
60 conv_check_native(char **argv, char **envp)
61 {
62 	/* 64-bit version does nothing */
63 	return (ELFCLASS64);
64 }
65 
66 #else
67 
68 /*
69  * Wrapper for isaexec(3c) that allows disabling 64-bit counterpart execution
70  * via setting LD_NOEXEC64=yes.
71  *
72  * The only callers are 32-bit sgs applications.  These applications determine
73  * whether a 64-bit counterpart is available via this routine, and if not,
74  * control is passed back to the caller, who will then complete execution.
75  * Note, isaexec() will eventually fall through to looking for a 32-bit
76  * counterpart (ie. sparcv7, or sparc), but as none of the callers provide these
77  * counterparts, we simply return to the caller.
78  */
79 uchar_t
80 conv_check_native(char **argv, char **envp)
81 {
82 	const char	*str;
83 
84 	/*
85 	 * LD_NOEXEC_64 defined in the environment prevents the isaexec() call.
86 	 * This is used by the test suite to test 32-bit support libraries.
87 	 */
88 	if (((str = getenv(MSG_ORIG(MSG_LD_NOEXEC64))) != NULL) && *str)
89 		return (ELFCLASS32);
90 
91 	if ((str = getexecname()) != NULL)
92 		(void) isaexec(str, argv, envp);
93 	return (ELFCLASS32);
94 }
95 #endif
96