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