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.
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.
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]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
29 * Wrap data in an elf file.
46 return (gettext(MSG_ORIG(mid
)));
50 main(int argc
, char **argv
, char **envp
)
52 const char *prog
, *ofile
= NULL
, *pstr
= NULL
;
54 uchar_t
class = ELFCLASS32
;
55 ushort_t mach
= EM_NONE
;
56 ObjDesc_t odesc
= { NULL
, 0, 0, 0 };
59 * If we're on a 64-bit kernel, try to exec a full 64-bit version of
60 * the binary. If successful, conv_check_native() won't return.
62 (void) conv_check_native(argv
, envp
);
67 (void) setlocale(LC_MESSAGES
, MSG_ORIG(MSG_STR_EMPTY
));
68 (void) textdomain(MSG_ORIG(MSG_SUNW_OST_SGS
));
70 (void) setvbuf(stdout
, NULL
, _IOLBF
, 0);
71 (void) setvbuf(stderr
, NULL
, _IOLBF
, 0);
73 prog
= basename(argv
[0]);
75 while ((var
= getopt(argc
, argv
, MSG_ORIG(MSG_ARG_OPTIONS
))) != EOF
) {
77 case '6': /* Create a 64-bit object */
78 if (optarg
[0] != '4') {
79 (void) fprintf(stderr
,
80 MSG_INTL(MSG_ARG_ILLEGAL
), prog
,
81 MSG_ORIG(MSG_ARG_6
), optarg
);
86 case 'o': /* output file name */
89 case 'z': /* output file platform */
90 if (strncmp(optarg
, MSG_ORIG(MSG_ARG_TARGET
),
91 MSG_ARG_TARGET_SIZE
) == 0)
92 pstr
= optarg
+ MSG_ARG_TARGET_SIZE
;
94 (void) fprintf(stderr
,
95 MSG_INTL(MSG_ARG_ILLEGAL
), prog
,
96 MSG_ORIG(MSG_ARG_Z
), optarg
);
101 (void) fprintf(stderr
, MSG_INTL(MSG_USAGE_BRIEF
),
110 * Verify that we have at least one input data file, and if no output
111 * file has been specified, provide a default. Update argc and argv
112 * for input() to continue processing any input files.
117 (void) fprintf(stderr
, MSG_INTL(MSG_USAGE_BRIEF
), prog
);
121 ofile
= MSG_ORIG(MSG_STR_AWRAPO
);
124 * If the user specified a target, use it to determine the machine type
125 * for the output object. If no target is specified, we leave "mach" as
126 * EM_NONE. output() will replace EM_NONE with the appropriate machine
127 * code for the system running elfwrap(1).
130 if (strcasecmp(pstr
, MSG_ORIG(MSG_TARG_SPARC
)) == 0) {
131 if (class == ELFCLASS64
)
136 } else if (strcasecmp(pstr
, MSG_ORIG(MSG_TARG_X86
)) == 0) {
137 if (class == ELFCLASS64
)
143 (void) fprintf(stderr
, MSG_INTL(MSG_ARG_BADTARG
), prog
,
150 * Create the input information for the new image.
152 if (class == ELFCLASS64
) {
153 if (input64(argc
, argv
, prog
, ofile
, &odesc
) == 1)
156 if (input32(argc
, argv
, prog
, ofile
, &odesc
) == 1)
161 * Create and truncate the output file.
163 if ((fd
= open(ofile
, (O_RDWR
| O_CREAT
| O_TRUNC
), 0666)) < 0) {
165 (void) fprintf(stderr
, MSG_INTL(MSG_ERR_OPEN
), prog
,
166 ofile
, strerror(err
));
171 * Initialize libelf, and create the new ELF file as the class dictates.
173 if (elf_version(EV_CURRENT
) == EV_NONE
) {
174 (void) fprintf(stderr
, MSG_INTL(MSG_ERR_LIBELF
), prog
,
178 if (class == ELFCLASS64
)
179 return (output64(prog
, fd
, ofile
, mach
, &odesc
));
181 return (output32(prog
, fd
, ofile
, mach
, &odesc
));