#include <fcntl.h>
#include <unistd.h>
#include <libgen.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <locale.h>
#include <libintl.h>
#include <conv.h>
#include <msg.h>
#include <_elfwrap.h>
const char *
_elfwrap_msg(Msg mid)
{
return (gettext(MSG_ORIG(mid)));
}
int
main(int argc, char **argv, char **envp)
{
const char *prog, *ofile = NULL, *pstr = NULL;
int fd, var;
uchar_t class = ELFCLASS32;
ushort_t mach = EM_NONE;
ObjDesc_t odesc = { NULL, 0, 0, 0 };
(void) conv_check_native(argv, envp);
(void) setlocale(LC_MESSAGES, MSG_ORIG(MSG_STR_EMPTY));
(void) textdomain(MSG_ORIG(MSG_SUNW_OST_SGS));
(void) setvbuf(stdout, NULL, _IOLBF, 0);
(void) setvbuf(stderr, NULL, _IOLBF, 0);
prog = basename(argv[0]);
opterr = 0;
while ((var = getopt(argc, argv, MSG_ORIG(MSG_ARG_OPTIONS))) != EOF) {
switch (var) {
case '6':
if (optarg[0] != '4') {
(void) fprintf(stderr,
MSG_INTL(MSG_ARG_ILLEGAL), prog,
MSG_ORIG(MSG_ARG_6), optarg);
return (1);
}
class = ELFCLASS64;
break;
case 'o':
ofile = optarg;
break;
case 'z':
if (strncmp(optarg, MSG_ORIG(MSG_ARG_TARGET),
MSG_ARG_TARGET_SIZE) == 0)
pstr = optarg + MSG_ARG_TARGET_SIZE;
else {
(void) fprintf(stderr,
MSG_INTL(MSG_ARG_ILLEGAL), prog,
MSG_ORIG(MSG_ARG_Z), optarg);
return (1);
}
break;
case '?':
(void) fprintf(stderr, MSG_INTL(MSG_USAGE_BRIEF),
prog);
return (1);
default:
break;
}
}
argv += optind;
argc -= optind;
if (argc == 0) {
(void) fprintf(stderr, MSG_INTL(MSG_USAGE_BRIEF), prog);
return (1);
}
if (ofile == NULL)
ofile = MSG_ORIG(MSG_STR_AWRAPO);
if (pstr) {
if (strcasecmp(pstr, MSG_ORIG(MSG_TARG_SPARC)) == 0) {
if (class == ELFCLASS64)
mach = EM_SPARCV9;
else
mach = EM_SPARC;
} else if (strcasecmp(pstr, MSG_ORIG(MSG_TARG_X86)) == 0) {
if (class == ELFCLASS64)
mach = EM_AMD64;
else
mach = EM_386;
} else {
(void) fprintf(stderr, MSG_INTL(MSG_ARG_BADTARG), prog,
pstr);
return (1);
}
}
if (class == ELFCLASS64) {
if (input64(argc, argv, prog, ofile, &odesc) == 1)
return (1);
} else {
if (input32(argc, argv, prog, ofile, &odesc) == 1)
return (1);
}
if ((fd = open(ofile, (O_RDWR | O_CREAT | O_TRUNC), 0666)) < 0) {
int err = errno;
(void) fprintf(stderr, MSG_INTL(MSG_ERR_OPEN), prog,
ofile, strerror(err));
return (1);
}
if (elf_version(EV_CURRENT) == EV_NONE) {
(void) fprintf(stderr, MSG_INTL(MSG_ERR_LIBELF), prog,
EV_CURRENT);
return (1);
}
if (class == ELFCLASS64)
return (output64(prog, fd, ofile, mach, &odesc));
else
return (output32(prog, fd, ofile, mach, &odesc));
}