2722387f30847b828b57ba9ca59d2b47ee9244ecrie * CDDL HEADER START
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * The contents of this file are subject to the terms of the
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * Common Development and Distribution License (the "License").
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * You may not use this file except in compliance with the License.
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * See the License for the specific language governing permissions
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * and limitations under the License.
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * When distributing Covered Code, include this CDDL HEADER in each
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * If applicable, add the following below this CDDL HEADER, with the
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * fields enclosed by brackets "[]" replaced with your own identifying
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * information: Portions Copyright [yyyy] [name of copyright owner]
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * CDDL HEADER END
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * Use is subject to license terms.
2722387f30847b828b57ba9ca59d2b47ee9244ecrie#pragma ident "%Z%%M% %I% %E% SMI"
2722387f30847b828b57ba9ca59d2b47ee9244ecrie#include <_libelf.h>
2722387f30847b828b57ba9ca59d2b47ee9244ecrie#include <_elfwrap.h>
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * This module is compiled to support 32-bit and 64-bit class objects. Define
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * the necessary interfaces for these classes.
2722387f30847b828b57ba9ca59d2b47ee9244ecrie#if defined(_ELF64)
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * Process all input files. These contain the data that will be assigned to a
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * new ELF section.
2722387f30847b828b57ba9ca59d2b47ee9244ecrieinput(int argc, char **argv, const char *prog, const char *ofile,
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * Make sure we have access to read each input file, and prepare an
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * output section descriptor for each. Note, we assign section indexes
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * starting at 1, as section index 0 is special, and is created by
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * Close any previously opened file.
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * Identify the section.
2722387f30847b828b57ba9ca59d2b47ee9244ecrie if ((outsec.os_addr = mmap(0, outsec.os_size, PROT_READ,
2722387f30847b828b57ba9ca59d2b47ee9244ecrie if (alist_append(&(odp->od_outsecs), &outsec, sizeof (OutSec_t),
2722387f30847b828b57ba9ca59d2b47ee9244ecrie return (1);
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * Each data section contributes:
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * i. its basename, prefixed with a "dot", to the .shstrtab.
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * ii. a section symbol.
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * iii. a data symbol, using the basename, with an
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * appended "_data" string.
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * iv. a data size symbol, using the basename with an
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * appended "_size" string.
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * If an error occurred, or no input files contributed data, bail now.
2722387f30847b828b57ba9ca59d2b47ee9244ecrie return (1);
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * Create section descriptors for .symtab, .strtab, and .shstrtab.
2722387f30847b828b57ba9ca59d2b47ee9244ecrie for (cnt = 0, stdsecs = &StdSecs[cnt]; stdsecs->ss_name; cnt++,
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * Identify the section.
2722387f30847b828b57ba9ca59d2b47ee9244ecrie if (alist_append(&(odp->od_outsecs), &outsec, sizeof (OutSec_t),
2722387f30847b828b57ba9ca59d2b47ee9244ecrie return (1);
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * Each standard section contributes:
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * i. its section name to the .shstrtab.
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * ii. a section symbol.
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * The symbol table requires an initial NULL entry and a following
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * FILE entry. Both string tables require an initial NULL byte.
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * The .strtab requires room for the output file name (STT_FILE).
2722387f30847b828b57ba9ca59d2b47ee9244ecrie return (0);
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * Having captured all input data, create the output file.
2722387f30847b828b57ba9ca59d2b47ee9244ecrieoutput(const char *prog, int fd, const char *ofile, ushort_t mach,
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * Obtain any target specific ELF information.
2722387f30847b828b57ba9ca59d2b47ee9244ecrie switch (mach) {
2722387f30847b828b57ba9ca59d2b47ee9244ecrie#if !defined(lint)
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * Create a new ELF descriptor for the new output file.
2722387f30847b828b57ba9ca59d2b47ee9244ecrie return (1);
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * Create and initialize the new ELF header.
2722387f30847b828b57ba9ca59d2b47ee9244ecrie return (1);
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * Note, the ELF header is initialized to reflect the host running
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * elfwrap(1) rather than the target. Using host byte order allows
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * elfwrap(1) to create the object data. Prior to the final update,
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * the output ELF header is modified to reflect the target, causing
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * libelf to produce the output object using the correct byte order
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * and other target information.
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * Create the required number of new sections, their associated section
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * header, and an initial data buffer.
2722387f30847b828b57ba9ca59d2b47ee9244ecrie return (1);
2722387f30847b828b57ba9ca59d2b47ee9244ecrie return (1);
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * Assign the section type and flags.
2722387f30847b828b57ba9ca59d2b47ee9244ecrie return (1);
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * If this is a PROGBITS section, then the data
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * originates from an input file. Assign the data
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * buffer to this input file and provide a default
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * alignment.
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * If this is the symbol table, use the symbol count to
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * reserve sufficient space for the symbols we need.
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * If this is a string table, use the table size to
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * reserve sufficient space for the strings we need.
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * Write the ELF data into a memory image.
2722387f30847b828b57ba9ca59d2b47ee9244ecrie return (1);
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * Assign an ELF descriptor to the memory image.
2722387f30847b828b57ba9ca59d2b47ee9244ecrie return (1);
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * Get the ELF header from the memory image.
2722387f30847b828b57ba9ca59d2b47ee9244ecrie return (1);
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * Read the section header and data from the new sections of the
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * memory image.
2722387f30847b828b57ba9ca59d2b47ee9244ecrie return (1);
2722387f30847b828b57ba9ca59d2b47ee9244ecrie if ((outsec->os_shdr = shdr = elf_getshdr(scn)) == NULL) {
2722387f30847b828b57ba9ca59d2b47ee9244ecrie return (1);
2722387f30847b828b57ba9ca59d2b47ee9244ecrie if ((outsec->os_data = elf_getdata(scn, NULL)) == NULL) {
2722387f30847b828b57ba9ca59d2b47ee9244ecrie return (1);
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * Remember the symbol table and string tables, so that they
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * can be filled in later.
2722387f30847b828b57ba9ca59d2b47ee9244ecrie if (strcmp(outsec->os_name, MSG_ORIG(MSG_SCN_STRTAB))) {
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * Update the ELF header with the .shstrtab index.
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * Set up the string table entries, and skip the first byte.
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * Skip the first symbol table entry. Write a FILE entry, and set
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * up for adding sections and data symbols. Associate the symbol
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * table with the string table.
2722387f30847b828b57ba9ca59d2b47ee9244ecrie secsymtabent->st_info = ELF_ST_INFO(STB_LOCAL, STT_NOTYPE);
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * Write the output file name to the .strtab.
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * Rescan all the new sections, adding symbols and strings as required.
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * Create a section symbol.
2722387f30847b828b57ba9ca59d2b47ee9244ecrie secsymtabent->st_info = ELF_ST_INFO(STB_LOCAL, STT_SECTION);
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * Store the section name, (with an appended "." if the section
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * name is derived from the input file name), and point the
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * section header to this name.
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * Add a symbol pointing to this PROGBITS section. The value
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * is the base offset of this section, which can only be 0.
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * The size of the symbol can be taken straight from the section
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * header information (that libelf generated).
2722387f30847b828b57ba9ca59d2b47ee9244ecrie glbsymtabent->st_info = ELF_ST_INFO(STB_GLOBAL, STT_OBJECT);
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * Store this symbol name (with an appended "_data") in the
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * string table.
2722387f30847b828b57ba9ca59d2b47ee9244ecrie (void) memcpy(strtabent, MSG_ORIG(MSG_STR_START), alen);
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * Add a symbol indicating the size of this PROGBITS section.
2722387f30847b828b57ba9ca59d2b47ee9244ecrie glbsymtabent->st_info = ELF_ST_INFO(STB_GLOBAL, STT_OBJECT);
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * Store this symbol name (with an appended "_end") in the
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * string table.
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * Update the .symtab section header with the index of the first
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * non-local symbol. The only locals written are the section symbols.
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * Having updated the image following the byte order of elfwrap(), seed
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * the ELF header with the appropriate target information.
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * If the output relocatable object is targeted to a machine with a
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * different byte order than the host running elfwrap(1), swap the data
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * to the target byte order.
2722387f30847b828b57ba9ca59d2b47ee9244ecrie (void) fprintf(stderr, MSG_INTL(MSG_ELF_SWAP_WRIMAGE), prog,
2722387f30847b828b57ba9ca59d2b47ee9244ecrie return (1);
2722387f30847b828b57ba9ca59d2b47ee9244ecrie * Finally, write the updated memory image out to disc.
2722387f30847b828b57ba9ca59d2b47ee9244ecrie return (1);
2722387f30847b828b57ba9ca59d2b47ee9244ecrie return (0);