xref: /onnv-gate/usr/src/cmd/sgs/elfwrap/common/main.c (revision 6234:7f51286f5faf)
1*6234Srie /*
2*6234Srie  * CDDL HEADER START
3*6234Srie  *
4*6234Srie  * The contents of this file are subject to the terms of the
5*6234Srie  * Common Development and Distribution License (the "License").
6*6234Srie  * You may not use this file except in compliance with the License.
7*6234Srie  *
8*6234Srie  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*6234Srie  * or http://www.opensolaris.org/os/licensing.
10*6234Srie  * See the License for the specific language governing permissions
11*6234Srie  * and limitations under the License.
12*6234Srie  *
13*6234Srie  * When distributing Covered Code, include this CDDL HEADER in each
14*6234Srie  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*6234Srie  * If applicable, add the following below this CDDL HEADER, with the
16*6234Srie  * fields enclosed by brackets "[]" replaced with your own identifying
17*6234Srie  * information: Portions Copyright [yyyy] [name of copyright owner]
18*6234Srie  *
19*6234Srie  * CDDL HEADER END
20*6234Srie  */
21*6234Srie 
22*6234Srie /*
23*6234Srie  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24*6234Srie  * Use is subject to license terms.
25*6234Srie  */
26*6234Srie #pragma ident	"%Z%%M%	%I%	%E% SMI"
27*6234Srie 
28*6234Srie /*
29*6234Srie  * Wrap data in an elf file.
30*6234Srie  */
31*6234Srie #include	<fcntl.h>
32*6234Srie #include	<unistd.h>
33*6234Srie #include	<libgen.h>
34*6234Srie #include	<errno.h>
35*6234Srie #include	<stdio.h>
36*6234Srie #include	<string.h>
37*6234Srie #include	<locale.h>
38*6234Srie #include	<libintl.h>
39*6234Srie #include	<conv.h>
40*6234Srie #include	<msg.h>
41*6234Srie #include	<_elfwrap.h>
42*6234Srie 
43*6234Srie const char *
_elfwrap_msg(Msg mid)44*6234Srie _elfwrap_msg(Msg mid)
45*6234Srie {
46*6234Srie 	return (gettext(MSG_ORIG(mid)));
47*6234Srie }
48*6234Srie 
49*6234Srie int
main(int argc,char ** argv,char ** envp)50*6234Srie main(int argc, char **argv, char **envp)
51*6234Srie {
52*6234Srie 	const char	*prog, *ofile = NULL, *pstr = NULL;
53*6234Srie 	int		fd, var;
54*6234Srie 	uchar_t		class = ELFCLASS32;
55*6234Srie 	ushort_t	mach = EM_NONE;
56*6234Srie 	ObjDesc_t	odesc = { NULL, 0, 0, 0 };
57*6234Srie 
58*6234Srie 	/*
59*6234Srie 	 * If we're on a 64-bit kernel, try to exec a full 64-bit version of
60*6234Srie 	 * the binary.  If successful, conv_check_native() won't return.
61*6234Srie 	 */
62*6234Srie 	(void) conv_check_native(argv, envp);
63*6234Srie 
64*6234Srie 	/*
65*6234Srie 	 * Establish locale.
66*6234Srie 	 */
67*6234Srie 	(void) setlocale(LC_MESSAGES, MSG_ORIG(MSG_STR_EMPTY));
68*6234Srie 	(void) textdomain(MSG_ORIG(MSG_SUNW_OST_SGS));
69*6234Srie 
70*6234Srie 	(void) setvbuf(stdout, NULL, _IOLBF, 0);
71*6234Srie 	(void) setvbuf(stderr, NULL, _IOLBF, 0);
72*6234Srie 
73*6234Srie 	prog = basename(argv[0]);
74*6234Srie 	opterr = 0;
75*6234Srie 	while ((var = getopt(argc, argv, MSG_ORIG(MSG_ARG_OPTIONS))) != EOF) {
76*6234Srie 		switch (var) {
77*6234Srie 		case '6':			/* Create a 64-bit object */
78*6234Srie 			if (optarg[0] != '4') {
79*6234Srie 				(void) fprintf(stderr,
80*6234Srie 				    MSG_INTL(MSG_ARG_ILLEGAL), prog,
81*6234Srie 				    MSG_ORIG(MSG_ARG_6), optarg);
82*6234Srie 				return (1);
83*6234Srie 			}
84*6234Srie 			class = ELFCLASS64;
85*6234Srie 			break;
86*6234Srie 		case 'o':			/* output file name */
87*6234Srie 			ofile = optarg;
88*6234Srie 			break;
89*6234Srie 		case 'z':			/* output file platform */
90*6234Srie 			if (strncmp(optarg, MSG_ORIG(MSG_ARG_TARGET),
91*6234Srie 			    MSG_ARG_TARGET_SIZE) == 0)
92*6234Srie 				pstr = optarg + MSG_ARG_TARGET_SIZE;
93*6234Srie 			else {
94*6234Srie 				(void) fprintf(stderr,
95*6234Srie 				    MSG_INTL(MSG_ARG_ILLEGAL), prog,
96*6234Srie 				    MSG_ORIG(MSG_ARG_Z), optarg);
97*6234Srie 				return (1);
98*6234Srie 			}
99*6234Srie 			break;
100*6234Srie 		case '?':
101*6234Srie 			(void) fprintf(stderr, MSG_INTL(MSG_USAGE_BRIEF),
102*6234Srie 			    prog);
103*6234Srie 			return (1);
104*6234Srie 		default:
105*6234Srie 			break;
106*6234Srie 		}
107*6234Srie 	}
108*6234Srie 
109*6234Srie 	/*
110*6234Srie 	 * Verify that we have at least one input data file, and if no output
111*6234Srie 	 * file has been specified, provide a default.  Update argc and argv
112*6234Srie 	 * for input() to continue processing any input files.
113*6234Srie 	 */
114*6234Srie 	argv += optind;
115*6234Srie 	argc -= optind;
116*6234Srie 	if (argc == 0) {
117*6234Srie 		(void) fprintf(stderr, MSG_INTL(MSG_USAGE_BRIEF), prog);
118*6234Srie 		return (1);
119*6234Srie 	}
120*6234Srie 	if (ofile == NULL)
121*6234Srie 		ofile = MSG_ORIG(MSG_STR_AWRAPO);
122*6234Srie 
123*6234Srie 	/*
124*6234Srie 	 * If the user specified a target, use it to determine the machine type
125*6234Srie 	 * for the output object.  If no target is specified, we leave "mach" as
126*6234Srie 	 * EM_NONE.  output() will replace EM_NONE with the appropriate machine
127*6234Srie 	 * code for the system running elfwrap(1).
128*6234Srie 	 */
129*6234Srie 	if (pstr) {
130*6234Srie 		if (strcasecmp(pstr, MSG_ORIG(MSG_TARG_SPARC)) == 0) {
131*6234Srie 			if (class == ELFCLASS64)
132*6234Srie 				mach = EM_SPARCV9;
133*6234Srie 			else
134*6234Srie 				mach = EM_SPARC;
135*6234Srie 
136*6234Srie 		} else if (strcasecmp(pstr, MSG_ORIG(MSG_TARG_X86)) == 0) {
137*6234Srie 			if (class == ELFCLASS64)
138*6234Srie 				mach = EM_AMD64;
139*6234Srie 			else
140*6234Srie 				mach = EM_386;
141*6234Srie 
142*6234Srie 		} else {
143*6234Srie 			(void) fprintf(stderr, MSG_INTL(MSG_ARG_BADTARG), prog,
144*6234Srie 			    pstr);
145*6234Srie 			return (1);
146*6234Srie 		}
147*6234Srie 	}
148*6234Srie 
149*6234Srie 	/*
150*6234Srie 	 * Create the input information for the new image.
151*6234Srie 	 */
152*6234Srie 	if (class == ELFCLASS64) {
153*6234Srie 		if (input64(argc, argv, prog, ofile, &odesc) == 1)
154*6234Srie 			return (1);
155*6234Srie 	} else {
156*6234Srie 		if (input32(argc, argv, prog, ofile, &odesc) == 1)
157*6234Srie 			return (1);
158*6234Srie 	}
159*6234Srie 
160*6234Srie 	/*
161*6234Srie 	 * Create and truncate the output file.
162*6234Srie 	 */
163*6234Srie 	if ((fd = open(ofile, (O_RDWR | O_CREAT | O_TRUNC), 0666)) < 0) {
164*6234Srie 		int err = errno;
165*6234Srie 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_OPEN), prog,
166*6234Srie 		    ofile, strerror(err));
167*6234Srie 		return (1);
168*6234Srie 	}
169*6234Srie 
170*6234Srie 	/*
171*6234Srie 	 * Initialize libelf, and create the new ELF file as the class dictates.
172*6234Srie 	 */
173*6234Srie 	if (elf_version(EV_CURRENT) == EV_NONE) {
174*6234Srie 		(void) fprintf(stderr, MSG_INTL(MSG_ERR_LIBELF), prog,
175*6234Srie 		    EV_CURRENT);
176*6234Srie 		return (1);
177*6234Srie 	}
178*6234Srie 	if (class == ELFCLASS64)
179*6234Srie 		return (output64(prog, fd, ofile, mach, &odesc));
180*6234Srie 	else
181*6234Srie 		return (output32(prog, fd, ofile, mach, &odesc));
182*6234Srie }
183