summaryrefslogtreecommitdiff
path: root/usr/src/cmd/sgs/elfwrap
diff options
context:
space:
mode:
authorrie <none@none>2008-03-19 22:37:49 -0700
committerrie <none@none>2008-03-19 22:37:49 -0700
commit2722387f30847b828b57ba9ca59d2b47ee9244ec (patch)
treeb784c8c4a92df871d71f31a2c778ba2d0e5735ff /usr/src/cmd/sgs/elfwrap
parent811fc8e17e28618ca5149abc18f576709fc5875b (diff)
downloadillumos-joyent-2722387f30847b828b57ba9ca59d2b47ee9244ec.tar.gz
PSARC/2008/198: elfwrap - wrap data in an ELF file
6675591 elfwrap - wrap data in an ELF file
Diffstat (limited to 'usr/src/cmd/sgs/elfwrap')
-rw-r--r--usr/src/cmd/sgs/elfwrap/Makefile29
-rw-r--r--usr/src/cmd/sgs/elfwrap/Makefile.com78
-rw-r--r--usr/src/cmd/sgs/elfwrap/Makefile.targ93
-rw-r--r--usr/src/cmd/sgs/elfwrap/amd64/Makefile43
-rw-r--r--usr/src/cmd/sgs/elfwrap/common/_elfwrap.h112
-rw-r--r--usr/src/cmd/sgs/elfwrap/common/elfwrap.c580
-rw-r--r--usr/src/cmd/sgs/elfwrap/common/elfwrap.msg88
-rw-r--r--usr/src/cmd/sgs/elfwrap/common/lintsup.c70
-rw-r--r--usr/src/cmd/sgs/elfwrap/common/machine.c75
-rw-r--r--usr/src/cmd/sgs/elfwrap/common/main.c182
-rw-r--r--usr/src/cmd/sgs/elfwrap/i386/Makefile33
-rw-r--r--usr/src/cmd/sgs/elfwrap/sparc/Makefile33
-rw-r--r--usr/src/cmd/sgs/elfwrap/sparcv9/Makefile43
13 files changed, 1459 insertions, 0 deletions
diff --git a/usr/src/cmd/sgs/elfwrap/Makefile b/usr/src/cmd/sgs/elfwrap/Makefile
new file mode 100644
index 0000000000..de669c580c
--- /dev/null
+++ b/usr/src/cmd/sgs/elfwrap/Makefile
@@ -0,0 +1,29 @@
+#
+# CDDL HEADER START
+#
+# The contents of this file are subject to the terms of the
+# Common Development and Distribution License (the "License").
+# You may not use this file except in compliance with the License.
+#
+# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+# or http://www.opensolaris.org/os/licensing.
+# See the License for the specific language governing permissions
+# and limitations under the License.
+#
+# When distributing Covered Code, include this CDDL HEADER in each
+# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+# If applicable, add the following below this CDDL HEADER, with the
+# fields enclosed by brackets "[]" replaced with your own identifying
+# information: Portions Copyright [yyyy] [name of copyright owner]
+#
+# CDDL HEADER END
+#
+
+#
+# Copyright 2008 Sun Microsystems, Inc. All rights reserved.
+# Use is subject to license terms.
+#
+# ident "%Z%%M% %I% %E% SMI"
+#
+
+include $(SRC)/cmd/sgs/Makefile.sub
diff --git a/usr/src/cmd/sgs/elfwrap/Makefile.com b/usr/src/cmd/sgs/elfwrap/Makefile.com
new file mode 100644
index 0000000000..f269ee99b0
--- /dev/null
+++ b/usr/src/cmd/sgs/elfwrap/Makefile.com
@@ -0,0 +1,78 @@
+#
+# CDDL HEADER START
+#
+# The contents of this file are subject to the terms of the
+# Common Development and Distribution License (the "License").
+# You may not use this file except in compliance with the License.
+#
+# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+# or http://www.opensolaris.org/os/licensing.
+# See the License for the specific language governing permissions
+# and limitations under the License.
+#
+# When distributing Covered Code, include this CDDL HEADER in each
+# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+# If applicable, add the following below this CDDL HEADER, with the
+# fields enclosed by brackets "[]" replaced with your own identifying
+# information: Portions Copyright [yyyy] [name of copyright owner]
+#
+# CDDL HEADER END
+#
+
+#
+# Copyright 2008 Sun Microsystems, Inc. All rights reserved.
+# Use is subject to license terms.
+#
+# ident "%Z%%M% %I% %E% SMI"
+#
+
+PROG= elfwrap
+
+include $(SRC)/cmd/Makefile.cmd
+include $(SRC)/cmd/sgs/Makefile.com
+
+COMOBJ = main.o
+
+MACHOBJ = machine.sparc.o machine.sparcv9.o \
+ machine.i386.o machine.amd64.o
+
+COMOBJ32 = elfwrap32.o
+
+COMOBJ64 = elfwrap64.o
+
+TOOLOBJS = alist.o
+
+BLTOBJ = msg.o
+
+OBJS= $(BLTOBJ) $(COMOBJ) $(MACHOBJ) $(COMOBJ32) $(COMOBJ64) \
+ $(TOOLOBJS)
+
+MAPFILES = $(MAPFILE.NGB)
+MAPOPTS = $(MAPFILES:%=-M%)
+
+CPPFLAGS = -I. -I../common -I../../include $(CPPFLAGS.master)
+LLDFLAGS =
+LLDFLAGS64 =
+LDFLAGS += $(VERSREF) $(USE_PROTO) $(MAPOPTS) $(LLDFLAGS) $(ZLAZYLOAD)
+LDLIBS += $(ELFLIBDIR) -lelf $(CONVLIBDIR) $(CONV_LIB)
+
+LINTFLAGS += -x
+LINTFLAGS64 += -x
+
+BLTDEFS = msg.h
+BLTDATA = msg.c
+BLTMESG = $(SGSMSGDIR)/elfwrap
+
+BLTFILES = $(BLTDEFS) $(BLTDATA) $(BLTMESG)
+
+SGSMSGCOM = ../common/elfwrap.msg
+SGSMSGTARG = $(SGSMSGCOM)
+SGSMSGALL = $(SGSMSGCOM)
+SGSMSGFLAGS += -h $(BLTDEFS) -d $(BLTDATA) -m $(BLTMESG) -n elfwrap_msg
+
+SRCS = $(COMOBJ:%.o=../common/%.c) ../common/machine.c \
+ $(COMOBJ32:%32.o=../common/%.c) \
+ $(TOOLOBJ:%.o=../../tools/common/%.c) $(BLTDATA)
+LINTSRCS = $(SRCS) ../common/lintsup.c
+
+CLEANFILES += $(LINTOUTS) $(BLTFILES)
diff --git a/usr/src/cmd/sgs/elfwrap/Makefile.targ b/usr/src/cmd/sgs/elfwrap/Makefile.targ
new file mode 100644
index 0000000000..26b0b71032
--- /dev/null
+++ b/usr/src/cmd/sgs/elfwrap/Makefile.targ
@@ -0,0 +1,93 @@
+#
+# CDDL HEADER START
+#
+# The contents of this file are subject to the terms of the
+# Common Development and Distribution License (the "License").
+# You may not use this file except in compliance with the License.
+#
+# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+# or http://www.opensolaris.org/os/licensing.
+# See the License for the specific language governing permissions
+# and limitations under the License.
+#
+# When distributing Covered Code, include this CDDL HEADER in each
+# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+# If applicable, add the following below this CDDL HEADER, with the
+# fields enclosed by brackets "[]" replaced with your own identifying
+# information: Portions Copyright [yyyy] [name of copyright owner]
+#
+# CDDL HEADER END
+#
+
+#
+# Copyright 2008 Sun Microsystems, Inc. All rights reserved.
+# Use is subject to license terms.
+#
+# ident "%Z%%M% %I% %E% SMI"
+#
+
+%.o: ../common/%.c
+ $(COMPILE.c) -o $@ $<
+ $(POST_PROCESS_O)
+
+%32.o: ../common/%.c
+ $(COMPILE.c) -o $@ $<
+ $(POST_PROCESS_O)
+
+%64.o: ../common/%.c
+ $(COMPILE.c) -D_ELF64 -o $@ $<
+ $(POST_PROCESS_O)
+
+%.o: $(SGSTOOLS)/common/%.c
+ $(COMPILE.c) -o $@ $<
+ $(POST_PROCESS_O)
+
+%.sparc.o: ../common/%.c
+ $(COMPILE.c) -DELFWRAP_SPARC -o $@ $<
+ $(POST_PROCESS_O)
+
+%.sparcv9.o: ../common/%.c
+ $(COMPILE.c) -DELFWRAP_SPARC -D_ELF64 -o $@ $<
+ $(POST_PROCESS_O)
+
+%.i386.o: ../common/%.c
+ $(COMPILE.c) -DELFWRAP_X86 -o $@ $<
+ $(POST_PROCESS_O)
+
+%.amd64.o: ../common/%.c
+ $(COMPILE.c) -DELFWRAP_X86 -D_ELF64 -o $@ $<
+ $(POST_PROCESS_O)
+
+$(PROG): $(OBJS) $(MAPFILES)
+ $(LINK.c) -o $@ $(OBJS) $(LDLIBS)
+ $(POST_PROCESS)
+
+all: $(PROG)
+
+clean:
+ $(RM) $(OBJS) $(CLEANFILES)
+
+delete:
+ $(RM) $(PROG)
+
+install \
+package: all $(ROOTPROG)
+
+.PARALLEL: $(LINTOUT32) $(LINTOUT64)
+
+lint: $(LINTOUT32) $(LINTOUT64) $(SGSLINTOUT)
+
+include $(SRC)/cmd/Makefile.targ
+include $(SRC)/cmd/sgs/Makefile.targ
+
+# Derived source and header files (messaging).
+
+catalog: $(BLTMESG)
+
+chkmsg: $(SRCS)
+ sh $(CHKMSG) $(CHKMSGFLAGS) $(SRCS)
+
+$(BLTDEFS) + \
+$(BLTDATA) + \
+$(BLTMESG): $(SGSMSGALL)
+ $(SGSMSG) $(SGSMSGFLAGS) $(SGSMSGALL)
diff --git a/usr/src/cmd/sgs/elfwrap/amd64/Makefile b/usr/src/cmd/sgs/elfwrap/amd64/Makefile
new file mode 100644
index 0000000000..0639219eef
--- /dev/null
+++ b/usr/src/cmd/sgs/elfwrap/amd64/Makefile
@@ -0,0 +1,43 @@
+#
+# CDDL HEADER START
+#
+# The contents of this file are subject to the terms of the
+# Common Development and Distribution License (the "License").
+# You may not use this file except in compliance with the License.
+#
+# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+# or http://www.opensolaris.org/os/licensing.
+# See the License for the specific language governing permissions
+# and limitations under the License.
+#
+# When distributing Covered Code, include this CDDL HEADER in each
+# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+# If applicable, add the following below this CDDL HEADER, with the
+# fields enclosed by brackets "[]" replaced with your own identifying
+# information: Portions Copyright [yyyy] [name of copyright owner]
+#
+# CDDL HEADER END
+#
+
+#
+# Copyright 2008 Sun Microsystems, Inc. All rights reserved.
+# Use is subject to license terms.
+#
+# ident "%Z%%M% %I% %E% SMI"
+#
+
+include ../Makefile.com
+
+.KEEP_STATE:
+
+ELFLIBDIR = $(ELFLIBDIR64)
+CONVLIBDIR = $(CONVLIBDIR64)
+LINTFLAGS64 += $(VAR_LINTFLAGS64)
+
+ROOTPROG= $(ROOTPROG64)
+
+include ../Makefile.targ
+include ../../Makefile.sub.64
+
+install \
+package: $(ROOTPROG64)
diff --git a/usr/src/cmd/sgs/elfwrap/common/_elfwrap.h b/usr/src/cmd/sgs/elfwrap/common/_elfwrap.h
new file mode 100644
index 0000000000..b53ffcf07a
--- /dev/null
+++ b/usr/src/cmd/sgs/elfwrap/common/_elfwrap.h
@@ -0,0 +1,112 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License (the "License").
+ * You may not use this file except in compliance with the License.
+ *
+ * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+ * or http://www.opensolaris.org/os/licensing.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+ * If applicable, add the following below this CDDL HEADER, with the
+ * fields enclosed by brackets "[]" replaced with your own identifying
+ * information: Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ */
+
+/*
+ * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
+ * Use is subject to license terms.
+ */
+
+#ifndef __ELFWRAP_H
+#define __ELFWRAP_H
+
+#pragma ident "%Z%%M% %I% %E% SMI"
+
+/*
+ * Local include file for elfwrap.
+ */
+#include <libelf.h>
+#include <alist.h>
+#include <_machelf.h>
+
+/*
+ * Define a target descriptor to hold target specific information.
+ */
+typedef struct {
+ uchar_t td_class; /* target class (32-bit/64-bit) */
+ uchar_t td_data; /* target data (LSB/MSB) */
+ ushort_t td_mach; /* target machine (sparc, i386, etc.) */
+ size_t td_align; /* target data buffer alignment */
+ size_t td_symsz; /* target symbol table entry size */
+} TargDesc_t;
+
+/*
+ * Define a descriptor for each ELF section being output to the new file.
+ */
+typedef struct {
+ const char *os_name; /* section name */
+ Word os_type; /* section type */
+ Xword os_flags; /* section flags */
+ size_t os_ndx; /* section index (input file) */
+ off_t os_size; /* section size (input file) */
+ void *os_addr; /* section address (input file) */
+ Shdr *os_shdr; /* section header (output file) */
+ Elf_Data *os_data; /* section data (output file) */
+} OutSec_t;
+
+#define AL_CNT_WOSECS 10 /* default number of input sections */
+
+/*
+ * Define a standard section descriptor.
+ */
+typedef struct {
+ const char *ss_name; /* section name */
+ Word ss_type; /* section type */
+ Xword ss_flags; /* section flags */
+} StdSec_t;
+
+/*
+ * Define a descriptor to maintain section information.
+ */
+typedef struct {
+ Alist *od_outsecs; /* list of output sections */
+ size_t od_symtabno; /* number of symbol table entries */
+ size_t od_strtabsz; /* string table size */
+ size_t od_shstrtabsz; /* section header string table size */
+} ObjDesc_t;
+
+/*
+ * Define all external interfaces.
+ */
+extern int input32(int, char **, const char *, const char *, ObjDesc_t *);
+extern int input64(int, char **, const char *, const char *, ObjDesc_t *);
+extern int output32(const char *, int, const char *, ushort_t,
+ ObjDesc_t *);
+extern int output64(const char *, int, const char *, ushort_t,
+ ObjDesc_t *);
+
+#if defined(lint)
+extern void target_init(TargDesc_t *);
+#else
+extern void target_init_sparc(TargDesc_t *);
+extern void target_init_sparcv9(TargDesc_t *);
+extern void target_init_i386(TargDesc_t *);
+extern void target_init_amd64(TargDesc_t *);
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __ELFWRAP_H */
diff --git a/usr/src/cmd/sgs/elfwrap/common/elfwrap.c b/usr/src/cmd/sgs/elfwrap/common/elfwrap.c
new file mode 100644
index 0000000000..ef5a482d60
--- /dev/null
+++ b/usr/src/cmd/sgs/elfwrap/common/elfwrap.c
@@ -0,0 +1,580 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License (the "License").
+ * You may not use this file except in compliance with the License.
+ *
+ * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+ * or http://www.opensolaris.org/os/licensing.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+ * If applicable, add the following below this CDDL HEADER, with the
+ * fields enclosed by brackets "[]" replaced with your own identifying
+ * information: Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ */
+
+/*
+ * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
+ * Use is subject to license terms.
+ */
+#pragma ident "%Z%%M% %I% %E% SMI"
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/mman.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <libgen.h>
+#include <errno.h>
+#include <libelf.h>
+#include <stdio.h>
+#include <strings.h>
+#include <msg.h>
+#include <machdep.h>
+#include <_libelf.h>
+#include <_elfwrap.h>
+
+/*
+ * This module is compiled to support 32-bit and 64-bit class objects. Define
+ * the necessary interfaces for these classes.
+ */
+#if defined(_ELF64)
+#define input input64
+#define output output64
+#else
+#define input input32
+#define output output32
+#endif
+
+static StdSec_t StdSecs[] = {
+ { MSG_ORIG(MSG_SCN_SYMTAB), SHT_SYMTAB, 0 },
+ { MSG_ORIG(MSG_SCN_STRTAB), SHT_STRTAB, SHF_STRINGS},
+ { MSG_ORIG(MSG_SCN_SHSTRTAB), SHT_STRTAB, SHF_STRINGS},
+ { NULL, 0, 0 }
+};
+
+/*
+ * Process all input files. These contain the data that will be assigned to a
+ * new ELF section.
+ */
+int
+input(int argc, char **argv, const char *prog, const char *ofile,
+ ObjDesc_t *odp)
+{
+ OutSec_t outsec;
+ StdSec_t *stdsecs;
+ size_t ndx, cnt;
+ int ret = 0, fd = -1;
+
+ /*
+ * Make sure we have access to read each input file, and prepare an
+ * output section descriptor for each. Note, we assign section indexes
+ * starting at 1, as section index 0 is special, and is created by
+ * libelf.
+ */
+ for (ndx = 1; argc; argc--, argv++, ndx++) {
+ char *file = *argv;
+ struct stat status;
+ size_t namesz;
+
+ /*
+ * Close any previously opened file.
+ */
+ if (fd != -1)
+ (void) close(fd);
+
+ /*
+ * Identify the section.
+ */
+ outsec.os_name = basename(file);
+ outsec.os_type = SHT_PROGBITS;
+ outsec.os_flags = SHF_ALLOC;
+ outsec.os_ndx = ndx;
+
+ if ((fd = open(file, O_RDONLY)) == -1) {
+ int err = errno;
+ (void) fprintf(stderr, MSG_INTL(MSG_ERR_OPEN),
+ prog, file, strerror(err));
+ ret = 1;
+ continue;
+ }
+ if (fstat(fd, &status) == -1) {
+ int err = errno;
+ (void) fprintf(stderr, MSG_INTL(MSG_ERR_FSTAT),
+ prog, file, strerror(err));
+ ret = 1;
+ continue;
+ }
+
+ if ((outsec.os_size = status.st_size) == 0) {
+ (void) fprintf(stderr, MSG_INTL(MSG_WARN_ZERO),
+ prog, file);
+ continue;
+ }
+
+ if ((outsec.os_addr = mmap(0, outsec.os_size, PROT_READ,
+ MAP_PRIVATE, fd, 0)) == MAP_FAILED) {
+ int err = errno;
+ (void) fprintf(stderr, MSG_INTL(MSG_ERR_MMAP),
+ prog, file, strerror(err));
+ ret = 1;
+ continue;
+ }
+
+ if (alist_append(&(odp->od_outsecs), &outsec, sizeof (OutSec_t),
+ AL_CNT_WOSECS) == 0) {
+ int err = errno;
+ (void) fprintf(stderr, MSG_INTL(MSG_ERR_ALLOC),
+ prog, file, strerror(err));
+ return (1);
+ }
+
+ /*
+ * Each data section contributes:
+ *
+ * i. its basename, prefixed with a "dot", to the .shstrtab.
+ * ii. a section symbol.
+ * iii. a data symbol, using the basename, with an
+ * appended "_data" string.
+ * iv. a data size symbol, using the basename with an
+ * appended "_size" string.
+ */
+ namesz = strlen(outsec.os_name) + 1;
+
+ odp->od_symtabno += 3;
+ odp->od_strtabsz += (namesz + MSG_STR_START_SIZE);
+ odp->od_strtabsz += (namesz + MSG_STR_END_SIZE);
+ odp->od_shstrtabsz += (namesz + MSG_STR_DOT_SIZE);
+ }
+
+ if (fd != -1)
+ (void) close(fd);
+
+ /*
+ * If an error occurred, or no input files contributed data, bail now.
+ */
+ if (ret || (odp->od_outsecs == NULL))
+ return (1);
+
+ /*
+ * Create section descriptors for .symtab, .strtab, and .shstrtab.
+ */
+ for (cnt = 0, stdsecs = &StdSecs[cnt]; stdsecs->ss_name; cnt++,
+ ndx++, stdsecs = &StdSecs[cnt]) {
+
+ /*
+ * Identify the section.
+ */
+ outsec.os_name = stdsecs->ss_name;
+ outsec.os_type = stdsecs->ss_type;
+ outsec.os_flags = stdsecs->ss_flags;
+ outsec.os_ndx = ndx;
+ outsec.os_size = 0;
+ outsec.os_addr = 0;
+
+ if (alist_append(&(odp->od_outsecs), &outsec, sizeof (OutSec_t),
+ AL_CNT_WOSECS) == 0) {
+ int err = errno;
+ (void) fprintf(stderr, MSG_INTL(MSG_ERR_ALLOC),
+ prog, outsec.os_name, strerror(err));
+ return (1);
+ }
+
+ /*
+ * Each standard section contributes:
+ *
+ * i. its section name to the .shstrtab.
+ * ii. a section symbol.
+ */
+ odp->od_symtabno++;
+ odp->od_shstrtabsz += (strlen(outsec.os_name) + 1);
+ }
+
+ /*
+ * The symbol table requires an initial NULL entry and a following
+ * FILE entry. Both string tables require an initial NULL byte.
+ * The .strtab requires room for the output file name (STT_FILE).
+ */
+ odp->od_symtabno += 2;
+ odp->od_strtabsz += strlen(ofile) + 2;
+ odp->od_shstrtabsz++;
+
+ return (0);
+}
+
+/*
+ * Having captured all input data, create the output file.
+ */
+int
+output(const char *prog, int fd, const char *ofile, ushort_t mach,
+ ObjDesc_t *odp)
+{
+ Aliste off;
+ Elf *melf, *oelf;
+ Ehdr *ehdr;
+ Sym *symtab, *secsymtabent, *glbsymtabent;
+ char *strtab, *strtabent, *shstrtab, *shstrtabent;
+ OutSec_t *outsec, *outsymtab, *outstrtab, *outshstrtab;
+ size_t len;
+ TargDesc_t tdesc;
+
+ /*
+ * Obtain any target specific ELF information.
+ */
+ if (mach == 0)
+ mach = M_MACH;
+
+ switch (mach) {
+#if !defined(lint)
+ case EM_SPARC:
+ target_init_sparc(&tdesc);
+ break;
+ case EM_SPARCV9:
+ target_init_sparcv9(&tdesc);
+ break;
+ case EM_386:
+ target_init_i386(&tdesc);
+ break;
+ case EM_AMD64:
+ target_init_amd64(&tdesc);
+ break;
+#else
+ default:
+ target_init(&tdesc);
+ break;
+#endif
+ }
+ /*
+ * Create a new ELF descriptor for the new output file.
+ */
+ if ((oelf = elf_begin(fd, ELF_C_WRITE, 0)) == NULL) {
+ (void) fprintf(stderr, MSG_INTL(MSG_ELF_BEGIN), prog,
+ elf_errmsg(elf_errno()));
+ return (1);
+ }
+
+ /*
+ * Create and initialize the new ELF header.
+ */
+ if ((ehdr = elf_newehdr(oelf)) == NULL) {
+ (void) fprintf(stderr, MSG_INTL(MSG_ELF_NEWEHDR), prog,
+ elf_errmsg(elf_errno()));
+ return (1);
+ }
+
+ /*
+ * Note, the ELF header is initialized to reflect the host running
+ * elfwrap(1) rather than the target. Using host byte order allows
+ * elfwrap(1) to create the object data. Prior to the final update,
+ * the output ELF header is modified to reflect the target, causing
+ * libelf to produce the output object using the correct byte order
+ * and other target information.
+ */
+ ehdr->e_ident[EI_DATA] = M_DATA;
+ ehdr->e_type = ET_REL;
+ ehdr->e_version = EV_CURRENT;
+
+ /*
+ * Create the required number of new sections, their associated section
+ * header, and an initial data buffer.
+ */
+ for (ALIST_TRAVERSE(odp->od_outsecs, off, outsec)) {
+ Elf_Scn *scn;
+ Elf_Data *data;
+ Shdr *shdr;
+
+ if ((scn = elf_newscn(oelf)) == NULL) {
+ (void) fprintf(stderr, MSG_INTL(MSG_ELF_NEWSCN),
+ prog, outsec->os_name, elf_errmsg(elf_errno()));
+ return (1);
+ }
+ if ((shdr = elf_getshdr(scn)) == NULL) {
+ (void) fprintf(stderr, MSG_INTL(MSG_ELF_GETSHDR),
+ prog, outsec->os_name, elf_errmsg(elf_errno()));
+ return (1);
+ }
+
+ /*
+ * Assign the section type and flags.
+ */
+ shdr->sh_type = outsec->os_type;
+ shdr->sh_flags = outsec->os_flags;
+
+ if ((data = elf_newdata(scn)) == NULL) {
+ (void) fprintf(stderr, MSG_INTL(MSG_ELF_NEWDATA),
+ prog, outsec->os_name, elf_errmsg(elf_errno()));
+ return (1);
+ }
+
+ switch (shdr->sh_type) {
+ case SHT_PROGBITS:
+ /*
+ * If this is a PROGBITS section, then the data
+ * originates from an input file. Assign the data
+ * buffer to this input file and provide a default
+ * alignment.
+ */
+ data->d_buf = outsec->os_addr;
+ data->d_type = ELF_T_BYTE;
+ data->d_size = outsec->os_size;
+ data->d_align = tdesc.td_align;
+ break;
+
+ case SHT_SYMTAB:
+ /*
+ * If this is the symbol table, use the symbol count to
+ * reserve sufficient space for the symbols we need.
+ */
+ data->d_buf = 0;
+ data->d_type = ELF_T_SYM;
+ data->d_size = (odp->od_symtabno * tdesc.td_symsz);
+ data->d_align = tdesc.td_align;
+ break;
+
+ case SHT_STRTAB:
+ /*
+ * If this is a string table, use the table size to
+ * reserve sufficient space for the strings we need.
+ */
+ data->d_buf = 0;
+ data->d_type = ELF_T_BYTE;
+ if (strcmp(outsec->os_name, MSG_ORIG(MSG_SCN_STRTAB)))
+ data->d_size = odp->od_shstrtabsz;
+ else
+ data->d_size = odp->od_strtabsz;
+ data->d_align = 1;
+ break;
+ }
+ }
+
+ /*
+ * Write the ELF data into a memory image.
+ */
+ if ((elf_update(oelf, ELF_C_WRIMAGE)) == -1) {
+ (void) fprintf(stderr, MSG_INTL(MSG_ELF_UPDATE), prog,
+ elf_errmsg(elf_errno()));
+ return (1);
+ }
+
+ /*
+ * Assign an ELF descriptor to the memory image.
+ */
+ if ((melf = elf_begin(0, ELF_C_IMAGE, oelf)) == NULL) {
+ (void) fprintf(stderr, MSG_INTL(MSG_ELF_BEGIN), prog,
+ elf_errmsg(elf_errno()));
+ return (1);
+ }
+
+ /*
+ * Get the ELF header from the memory image.
+ */
+ if ((ehdr = elf_getehdr(melf)) == NULL) {
+ (void) fprintf(stderr, MSG_INTL(MSG_ELF_GETEHDR), prog,
+ elf_errmsg(elf_errno()));
+ return (1);
+ }
+
+ /*
+ * Read the section header and data from the new sections of the
+ * memory image.
+ */
+ for (ALIST_TRAVERSE(odp->od_outsecs, off, outsec)) {
+ Elf_Scn *scn;
+ Shdr *shdr;
+
+ if ((scn = elf_getscn(melf, outsec->os_ndx)) == NULL) {
+ (void) fprintf(stderr, MSG_INTL(MSG_ELF_GETSCN),
+ prog, outsec->os_name, elf_errmsg(elf_errno()));
+ return (1);
+ }
+ if ((outsec->os_shdr = shdr = elf_getshdr(scn)) == NULL) {
+ (void) fprintf(stderr, MSG_INTL(MSG_ELF_GETSHDR),
+ prog, outsec->os_name, elf_errmsg(elf_errno()));
+ return (1);
+ }
+ if ((outsec->os_data = elf_getdata(scn, NULL)) == NULL) {
+ (void) fprintf(stderr, MSG_INTL(MSG_ELF_GETDATA),
+ prog, outsec->os_name, elf_errmsg(elf_errno()));
+ return (1);
+ }
+
+ if (shdr->sh_type == SHT_PROGBITS)
+ continue;
+
+ /*
+ * Remember the symbol table and string tables, so that they
+ * can be filled in later.
+ */
+ if (shdr->sh_type == SHT_SYMTAB) {
+ outsymtab = outsec;
+ symtab = (Sym *)outsec->os_data->d_buf;
+ } else if (shdr->sh_type == SHT_STRTAB) {
+ if (strcmp(outsec->os_name, MSG_ORIG(MSG_SCN_STRTAB))) {
+ outshstrtab = outsec;
+ shstrtab = (char *)outsec->os_data->d_buf;
+ } else {
+ outstrtab = outsec;
+ strtab = (char *)outsec->os_data->d_buf;
+ }
+ }
+ }
+
+ /*
+ * Update the ELF header with the .shstrtab index.
+ */
+ ehdr->e_shstrndx = outshstrtab->os_ndx;
+
+ /*
+ * Set up the string table entries, and skip the first byte.
+ */
+ strtabent = strtab;
+ strtabent++;
+
+ shstrtabent = shstrtab;
+ shstrtabent++;
+
+ /*
+ * Skip the first symbol table entry. Write a FILE entry, and set
+ * up for adding sections and data symbols. Associate the symbol
+ * table with the string table.
+ */
+ secsymtabent = symtab;
+ secsymtabent++;
+ secsymtabent->st_name = (strtabent - strtab);
+ secsymtabent->st_info = ELF_ST_INFO(STB_LOCAL, STT_NOTYPE);
+ secsymtabent->st_shndx = SHN_ABS;
+ secsymtabent++;
+
+ glbsymtabent = secsymtabent;
+ glbsymtabent += alist_nitems(odp->od_outsecs);
+
+ outsymtab->os_shdr->sh_link = outstrtab->os_ndx;
+
+ /*
+ * Write the output file name to the .strtab.
+ */
+ len = strlen(ofile) + 1;
+ (void) memcpy(strtabent, ofile, len);
+ strtabent += len;
+
+ /*
+ * Rescan all the new sections, adding symbols and strings as required.
+ */
+ for (ALIST_TRAVERSE(odp->od_outsecs, off, outsec)) {
+ size_t alen;
+
+ /*
+ * Create a section symbol.
+ */
+ secsymtabent->st_info = ELF_ST_INFO(STB_LOCAL, STT_SECTION);
+ secsymtabent->st_shndx = outsec->os_ndx;
+ secsymtabent++;
+
+ /*
+ * Store the section name, (with an appended "." if the section
+ * name is derived from the input file name), and point the
+ * section header to this name.
+ */
+ outsec->os_shdr->sh_name = (shstrtabent - shstrtab);
+
+ if (outsec->os_shdr->sh_type == SHT_PROGBITS) {
+ (void) memcpy(shstrtabent, MSG_ORIG(MSG_STR_DOT),
+ MSG_STR_DOT_SIZE);
+ shstrtabent += MSG_STR_DOT_SIZE;
+ }
+
+ len = strlen(outsec->os_name) + 1;
+ (void) memcpy(shstrtabent, outsec->os_name, len);
+ shstrtabent += len;
+
+ if (outsec->os_shdr->sh_type != SHT_PROGBITS)
+ continue;
+
+ /*
+ * Add a symbol pointing to this PROGBITS section. The value
+ * is the base offset of this section, which can only be 0.
+ * The size of the symbol can be taken straight from the section
+ * header information (that libelf generated).
+ */
+ glbsymtabent->st_name = (strtabent - strtab);
+ glbsymtabent->st_info = ELF_ST_INFO(STB_GLOBAL, STT_OBJECT);
+ glbsymtabent->st_shndx = outsec->os_ndx;
+ glbsymtabent->st_size = outsec->os_shdr->sh_size;
+ glbsymtabent++;
+
+ /*
+ * Store this symbol name (with an appended "_data") in the
+ * string table.
+ */
+ len--;
+ (void) memcpy(strtabent, outsec->os_name, len);
+ strtabent += len;
+ alen = (MSG_STR_START_SIZE + 1);
+ (void) memcpy(strtabent, MSG_ORIG(MSG_STR_START), alen);
+ strtabent += alen;
+
+ /*
+ * Add a symbol indicating the size of this PROGBITS section.
+ */
+ glbsymtabent->st_name = (strtabent - strtab);
+ glbsymtabent->st_info = ELF_ST_INFO(STB_GLOBAL, STT_OBJECT);
+ glbsymtabent->st_shndx = outsec->os_ndx;
+ glbsymtabent->st_value = outsec->os_shdr->sh_size;
+ glbsymtabent++;
+
+ /*
+ * Store this symbol name (with an appended "_end") in the
+ * string table.
+ */
+ (void) memcpy(strtabent, outsec->os_name, len);
+ strtabent += len;
+ alen = (MSG_STR_END_SIZE + 1);
+ (void) memcpy(strtabent, MSG_ORIG(MSG_STR_END), alen);
+ strtabent += alen;
+ }
+
+ /*
+ * Update the .symtab section header with the index of the first
+ * non-local symbol. The only locals written are the section symbols.
+ */
+ outsymtab->os_shdr->sh_info = (secsymtabent - symtab);
+
+ /*
+ * Having updated the image following the byte order of elfwrap(), seed
+ * the ELF header with the appropriate target information.
+ */
+ ehdr->e_ident[EI_CLASS] = tdesc.td_class;
+ ehdr->e_ident[EI_DATA] = tdesc.td_data;
+ ehdr->e_machine = tdesc.td_mach;
+
+ /*
+ * If the output relocatable object is targeted to a machine with a
+ * different byte order than the host running elfwrap(1), swap the data
+ * to the target byte order.
+ */
+ if ((_elf_sys_encoding() != ehdr->e_ident[EI_DATA]) &&
+ (_elf_swap_wrimage(melf) != 0)) {
+ (void) fprintf(stderr, MSG_INTL(MSG_ELF_SWAP_WRIMAGE), prog,
+ elf_errmsg(elf_errno()));
+ return (1);
+ }
+ (void) elf_end(melf);
+
+ /*
+ * Finally, write the updated memory image out to disc.
+ */
+ if ((elf_update(oelf, ELF_C_WRITE)) == -1) {
+ (void) fprintf(stderr, MSG_INTL(MSG_ELF_UPDATE), prog,
+ elf_errmsg(elf_errno()));
+ return (1);
+ }
+ (void) elf_end(oelf);
+
+ return (0);
+}
diff --git a/usr/src/cmd/sgs/elfwrap/common/elfwrap.msg b/usr/src/cmd/sgs/elfwrap/common/elfwrap.msg
new file mode 100644
index 0000000000..2d68fabbe3
--- /dev/null
+++ b/usr/src/cmd/sgs/elfwrap/common/elfwrap.msg
@@ -0,0 +1,88 @@
+#
+# CDDL HEADER START
+#
+# The contents of this file are subject to the terms of the
+# Common Development and Distribution License (the "License").
+# You may not use this file except in compliance with the License.
+#
+# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+# or http://www.opensolaris.org/os/licensing.
+# See the License for the specific language governing permissions
+# and limitations under the License.
+#
+# When distributing Covered Code, include this CDDL HEADER in each
+# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+# If applicable, add the following below this CDDL HEADER, with the
+# fields enclosed by brackets "[]" replaced with your own identifying
+# information: Portions Copyright [yyyy] [name of copyright owner]
+#
+# CDDL HEADER END
+#
+
+#
+# Copyright 2008 Sun Microsystems, Inc. All rights reserved.
+# Use is subject to license terms.
+#
+# ident "%Z%%M% %I% %E% SMI"
+#
+
+@ _START_
+
+# Message file for cmd/sgs/elfwrap.
+
+@ MSG_ID_ELFWRAP
+
+# Usage Messages
+
+@ MSG_USAGE_BRIEF "usage: %s [-64] [-o file] [-z target=platform] \
+ data, ...\n"
+
+@ MSG_ARG_BADTARG "%s: unknown target platform: %s\n"
+@ MSG_ARG_ILLEGAL "%s: option %s has illegal argument `%s'\n"
+
+@ MSG_ERR_LIBELF "%s: libelf: version not supported: %d\n"
+
+@ MSG_ERR_ALLOC "%s: %s: can't allocate memory: %s\n"
+@ MSG_ERR_FSTAT "%s: %s: can't fstat: %s\n"
+@ MSG_ERR_MMAP "%s: %s: can't mmap: %s\n"
+@ MSG_ERR_OPEN "%s: %s: can't open: %s\n"
+
+@ MSG_WARN_ZERO "%s: %s: has zero length - ignoring\n"
+
+@ MSG_ELF_BEGIN "%s: elf_begin failed: %s\n"
+@ MSG_ELF_GETEHDR "%s: elf_getehdr failed: %s\n"
+@ MSG_ELF_NEWEHDR "%s: elf_newehdr failed: %s\n"
+@ MSG_ELF_SWAP_WRIMAGE "%s: elf_swap_wrimage failed: %s\n"
+@ MSG_ELF_UPDATE "%s: elf_update failed: %s\n"
+
+@ MSG_ELF_GETDATA "%s: %s: elf_getdata failed: %s\n"
+@ MSG_ELF_GETSCN "%s: %s: elf_getscn failed: %s\n"
+@ MSG_ELF_GETSHDR "%s: %s: elf_getshdr failed: %s\n"
+@ MSG_ELF_NEWDATA "%s: %s: elf_newdata failed: %s\n"
+@ MSG_ELF_NEWSCN "%s: %s: elf_newscn failed: %s\n"
+
+@ _END_
+
+# The following strings represent reserved words, files, path names and symbols.
+# Reference to this strings is via the MSG_ORIG() macro, and thus no message
+# translation is required.
+
+@ MSG_SUNW_OST_SGS "SUNW_OST_SGS"
+
+@ MSG_ARG_6 "-6"
+@ MSG_ARG_OPTIONS "6:o:z:"
+@ MSG_ARG_TARGET "target="
+@ MSG_ARG_Z "-z"
+
+@ MSG_TARG_SPARC "sparc"
+@ MSG_TARG_X86 "x86"
+
+@ MSG_STR_AWRAPO "a.wrap.o"
+@ MSG_STR_EMPTY ""
+@ MSG_STR_START "_start"
+@ MSG_STR_END "_end"
+@ MSG_STR_DOT "."
+
+@ MSG_SCN_SYMTAB ".symtab"
+@ MSG_SCN_STRTAB ".strtab"
+@ MSG_SCN_SHSTRTAB ".shstrtab"
diff --git a/usr/src/cmd/sgs/elfwrap/common/lintsup.c b/usr/src/cmd/sgs/elfwrap/common/lintsup.c
new file mode 100644
index 0000000000..045b22d98f
--- /dev/null
+++ b/usr/src/cmd/sgs/elfwrap/common/lintsup.c
@@ -0,0 +1,70 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License (the "License").
+ * You may not use this file except in compliance with the License.
+ *
+ * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+ * or http://www.opensolaris.org/os/licensing.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+ * If applicable, add the following below this CDDL HEADER, with the
+ * fields enclosed by brackets "[]" replaced with your own identifying
+ * information: Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ */
+
+/*
+ * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
+ * Use is subject to license terms.
+ */
+/* LINTLIBRARY */
+/* PROTOLIB1 */
+
+#pragma ident "%Z%%M% %I% %E% SMI"
+
+/*
+ * Supplemental Pseudo-code to get lint to consider these symbols used.
+ */
+#include <msg.h>
+#include <_elfwrap.h>
+
+void
+foo()
+{
+ (void) _elfwrap_msg((Msg)&__elfwrap_msg[0]);
+}
+
+#if defined(_ELF64)
+int
+input32(int argc, char **argv, const char *prog, const char *ofile,
+ ObjDesc_t *odp)
+{
+ return (input64(argc, argv, prog, ofile, odp));
+}
+
+int
+output32(const char *prog, int fd, const char *ofile, ushort_t mach,
+ ObjDesc_t *odp)
+{
+ return (output64(prog, fd, ofile, mach, odp));
+}
+#else
+int
+input64(int argc, char **argv, const char *prog, const char *ofile,
+ ObjDesc_t *odp)
+{
+ return (input32(argc, argv, prog, ofile, odp));
+}
+int
+output64(const char *prog, int fd, const char *ofile, ushort_t mach,
+ ObjDesc_t *odp)
+{
+ return (output32(prog, fd, ofile, mach, odp));
+}
+#endif
diff --git a/usr/src/cmd/sgs/elfwrap/common/machine.c b/usr/src/cmd/sgs/elfwrap/common/machine.c
new file mode 100644
index 0000000000..73cc1fc5d6
--- /dev/null
+++ b/usr/src/cmd/sgs/elfwrap/common/machine.c
@@ -0,0 +1,75 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License (the "License").
+ * You may not use this file except in compliance with the License.
+ *
+ * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+ * or http://www.opensolaris.org/os/licensing.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+ * If applicable, add the following below this CDDL HEADER, with the
+ * fields enclosed by brackets "[]" replaced with your own identifying
+ * information: Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ */
+
+/*
+ * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
+ * Use is subject to license terms.
+ */
+#pragma ident "%Z%%M% %I% %E% SMI"
+
+#include <_elfwrap.h>
+
+#if defined(lint)
+#include <machdep.h>
+#else
+#if defined(ELFWRAP_X86)
+#include <i386/machdep_x86.h>
+#if defined(_ELF64)
+#define target_init target_init_amd64
+#else
+#define target_init target_init_i386
+#endif
+#endif
+#if defined(ELFWRAP_SPARC)
+#include <sparc/machdep_sparc.h>
+#if defined(_ELF64)
+#define target_init target_init_sparcv9
+#else
+#define target_init target_init_sparc
+#endif
+#endif
+#endif
+
+/*
+ * Establish any target specific data. This module is compiled using the
+ * defines shown above, to provide data for each target machine elfwrap(1)
+ * supports - each target module being assigned a unique interface name.
+ */
+void
+target_init(TargDesc_t *tdp)
+{
+ /*
+ * ELF header information.
+ */
+ tdp->td_class = M_CLASS; /* e_ident[EI_CLASS] */
+ tdp->td_data = M_DATA; /* e_ident[EI_DATA] */
+ tdp->td_mach = M_MACH; /* e_machine */
+
+ /*
+ * Default data buffer alignment.
+ */
+ tdp->td_align = M_WORD_ALIGN; /* d_align */
+
+ /*
+ * Symbol table entry size.
+ */
+ tdp->td_symsz = sizeof (Sym); /* d_size */
+}
diff --git a/usr/src/cmd/sgs/elfwrap/common/main.c b/usr/src/cmd/sgs/elfwrap/common/main.c
new file mode 100644
index 0000000000..cda080309a
--- /dev/null
+++ b/usr/src/cmd/sgs/elfwrap/common/main.c
@@ -0,0 +1,182 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License (the "License").
+ * You may not use this file except in compliance with the License.
+ *
+ * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+ * or http://www.opensolaris.org/os/licensing.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+ * If applicable, add the following below this CDDL HEADER, with the
+ * fields enclosed by brackets "[]" replaced with your own identifying
+ * information: Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ */
+
+/*
+ * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
+ * Use is subject to license terms.
+ */
+#pragma ident "%Z%%M% %I% %E% SMI"
+
+/*
+ * Wrap data in an elf file.
+ */
+#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 };
+
+ /*
+ * If we're on a 64-bit kernel, try to exec a full 64-bit version of
+ * the binary. If successful, conv_check_native() won't return.
+ */
+ (void) conv_check_native(argv, envp);
+
+ /*
+ * Establish locale.
+ */
+ (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': /* Create a 64-bit object */
+ 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': /* output file name */
+ ofile = optarg;
+ break;
+ case 'z': /* output file platform */
+ 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;
+ }
+ }
+
+ /*
+ * Verify that we have at least one input data file, and if no output
+ * file has been specified, provide a default. Update argc and argv
+ * for input() to continue processing any input files.
+ */
+ 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 the user specified a target, use it to determine the machine type
+ * for the output object. If no target is specified, we leave "mach" as
+ * EM_NONE. output() will replace EM_NONE with the appropriate machine
+ * code for the system running elfwrap(1).
+ */
+ 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);
+ }
+ }
+
+ /*
+ * Create the input information for the new image.
+ */
+ if (class == ELFCLASS64) {
+ if (input64(argc, argv, prog, ofile, &odesc) == 1)
+ return (1);
+ } else {
+ if (input32(argc, argv, prog, ofile, &odesc) == 1)
+ return (1);
+ }
+
+ /*
+ * Create and truncate the output file.
+ */
+ 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);
+ }
+
+ /*
+ * Initialize libelf, and create the new ELF file as the class dictates.
+ */
+ 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));
+}
diff --git a/usr/src/cmd/sgs/elfwrap/i386/Makefile b/usr/src/cmd/sgs/elfwrap/i386/Makefile
new file mode 100644
index 0000000000..6ac7ee0712
--- /dev/null
+++ b/usr/src/cmd/sgs/elfwrap/i386/Makefile
@@ -0,0 +1,33 @@
+#
+# CDDL HEADER START
+#
+# The contents of this file are subject to the terms of the
+# Common Development and Distribution License (the "License").
+# You may not use this file except in compliance with the License.
+#
+# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+# or http://www.opensolaris.org/os/licensing.
+# See the License for the specific language governing permissions
+# and limitations under the License.
+#
+# When distributing Covered Code, include this CDDL HEADER in each
+# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+# If applicable, add the following below this CDDL HEADER, with the
+# fields enclosed by brackets "[]" replaced with your own identifying
+# information: Portions Copyright [yyyy] [name of copyright owner]
+#
+# CDDL HEADER END
+#
+
+#
+# Copyright 2008 Sun Microsystems, Inc. All rights reserved.
+# Use is subject to license terms.
+#
+# ident "%Z%%M% %I% %E% SMI"
+#
+
+include ../Makefile.com
+
+.KEEP_STATE:
+
+include ../Makefile.targ
diff --git a/usr/src/cmd/sgs/elfwrap/sparc/Makefile b/usr/src/cmd/sgs/elfwrap/sparc/Makefile
new file mode 100644
index 0000000000..6ac7ee0712
--- /dev/null
+++ b/usr/src/cmd/sgs/elfwrap/sparc/Makefile
@@ -0,0 +1,33 @@
+#
+# CDDL HEADER START
+#
+# The contents of this file are subject to the terms of the
+# Common Development and Distribution License (the "License").
+# You may not use this file except in compliance with the License.
+#
+# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+# or http://www.opensolaris.org/os/licensing.
+# See the License for the specific language governing permissions
+# and limitations under the License.
+#
+# When distributing Covered Code, include this CDDL HEADER in each
+# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+# If applicable, add the following below this CDDL HEADER, with the
+# fields enclosed by brackets "[]" replaced with your own identifying
+# information: Portions Copyright [yyyy] [name of copyright owner]
+#
+# CDDL HEADER END
+#
+
+#
+# Copyright 2008 Sun Microsystems, Inc. All rights reserved.
+# Use is subject to license terms.
+#
+# ident "%Z%%M% %I% %E% SMI"
+#
+
+include ../Makefile.com
+
+.KEEP_STATE:
+
+include ../Makefile.targ
diff --git a/usr/src/cmd/sgs/elfwrap/sparcv9/Makefile b/usr/src/cmd/sgs/elfwrap/sparcv9/Makefile
new file mode 100644
index 0000000000..0639219eef
--- /dev/null
+++ b/usr/src/cmd/sgs/elfwrap/sparcv9/Makefile
@@ -0,0 +1,43 @@
+#
+# CDDL HEADER START
+#
+# The contents of this file are subject to the terms of the
+# Common Development and Distribution License (the "License").
+# You may not use this file except in compliance with the License.
+#
+# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+# or http://www.opensolaris.org/os/licensing.
+# See the License for the specific language governing permissions
+# and limitations under the License.
+#
+# When distributing Covered Code, include this CDDL HEADER in each
+# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+# If applicable, add the following below this CDDL HEADER, with the
+# fields enclosed by brackets "[]" replaced with your own identifying
+# information: Portions Copyright [yyyy] [name of copyright owner]
+#
+# CDDL HEADER END
+#
+
+#
+# Copyright 2008 Sun Microsystems, Inc. All rights reserved.
+# Use is subject to license terms.
+#
+# ident "%Z%%M% %I% %E% SMI"
+#
+
+include ../Makefile.com
+
+.KEEP_STATE:
+
+ELFLIBDIR = $(ELFLIBDIR64)
+CONVLIBDIR = $(CONVLIBDIR64)
+LINTFLAGS64 += $(VAR_LINTFLAGS64)
+
+ROOTPROG= $(ROOTPROG64)
+
+include ../Makefile.targ
+include ../../Makefile.sub.64
+
+install \
+package: $(ROOTPROG64)