diff --git a/contrib/elftoolchain/elfcopy/archive.c b/contrib/elftoolchain/elfcopy/archive.c index 9e23b831fdca..e57caad58e35 100644 --- a/contrib/elftoolchain/elfcopy/archive.c +++ b/contrib/elftoolchain/elfcopy/archive.c @@ -1,526 +1,541 @@ /*- * Copyright (c) 2007-2009 Kai Wang * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include #include #include #include #include #include #ifndef LIBELF_AR #include #include #endif /* ! LIBELF_AR */ #include "elfcopy.h" ELFTC_VCSID("$Id: archive.c 3490 2016-08-31 00:12:22Z emaste $"); #define _ARMAG_LEN 8 /* length of ar magic string */ #define _ARHDR_LEN 60 /* length of ar header */ #define _INIT_AS_CAP 128 /* initial archive string table size */ #define _INIT_SYMOFF_CAP (256*(sizeof(uint32_t))) /* initial so table size */ #define _INIT_SYMNAME_CAP 1024 /* initial sn table size */ #define _MAXNAMELEN_SVR4 15 /* max member name length in svr4 variant */ #ifndef LIBELF_AR static void ac_read_objs(struct elfcopy *ecp, int ifd); static void ac_write_cleanup(struct elfcopy *ecp); static void ac_write_data(struct archive *a, const void *buf, size_t s); static void ac_write_objs(struct elfcopy *ecp, int ofd); #endif /* ! LIBELF_AR */ static void add_to_ar_str_table(struct elfcopy *elfcopy, const char *name); static void add_to_ar_sym_table(struct elfcopy *ecp, const char *name); static void extract_arsym(struct elfcopy *ecp); static void process_ar_obj(struct elfcopy *ecp, struct ar_obj *obj); static void sync_ar(struct elfcopy *ecp); static void process_ar_obj(struct elfcopy *ecp, struct ar_obj *obj) { struct stat sb; char *tempfile; int fd; /* Output to a temporary file. */ create_tempfile(NULL, &tempfile, &fd); - if ((ecp->eout = elf_begin(fd, ELF_C_WRITE, NULL)) == NULL) + if ((ecp->eout = elf_begin(fd, ELF_C_WRITE, NULL)) == NULL) { + cleanup_tempfile(tempfile); errx(EXIT_FAILURE, "elf_begin() failed: %s", elf_errmsg(-1)); + } elf_flagelf(ecp->eout, ELF_C_SET, ELF_F_LAYOUT); create_elf(ecp); elf_end(ecp->ein); elf_end(ecp->eout); free(obj->buf); obj->buf = NULL; /* Extract archive symbols. */ - if (lseek(fd, 0, SEEK_SET) < 0) + if (lseek(fd, 0, SEEK_SET) < 0) { + cleanup_tempfile(tempfile); err(EXIT_FAILURE, "lseek failed for '%s'", tempfile); - if ((ecp->eout = elf_begin(fd, ELF_C_READ, NULL)) == NULL) + } + if ((ecp->eout = elf_begin(fd, ELF_C_READ, NULL)) == NULL) { + cleanup_tempfile(tempfile); errx(EXIT_FAILURE, "elf_begin() failed: %s", elf_errmsg(-1)); + } extract_arsym(ecp); elf_end(ecp->eout); - if (fstat(fd, &sb) == -1) + if (fstat(fd, &sb) == -1) { + cleanup_tempfile(tempfile); err(EXIT_FAILURE, "fstat %s failed", tempfile); - if (lseek(fd, 0, SEEK_SET) < 0) + } + if (lseek(fd, 0, SEEK_SET) < 0) { + cleanup_tempfile(tempfile); err(EXIT_FAILURE, "lseek %s failed", tempfile); + } obj->size = sb.st_size; - if ((obj->maddr = malloc(obj->size)) == NULL) + if ((obj->maddr = malloc(obj->size)) == NULL) { + cleanup_tempfile(tempfile); err(EXIT_FAILURE, "memory allocation failed for '%s'", tempfile); - if ((size_t) read(fd, obj->maddr, obj->size) != obj->size) + } + if ((size_t) read(fd, obj->maddr, obj->size) != obj->size) { + cleanup_tempfile(tempfile); err(EXIT_FAILURE, "read failed for '%s'", tempfile); - if (unlink(tempfile)) + } + if (cleanup_tempfile(tempfile) < 0) err(EXIT_FAILURE, "unlink %s failed", tempfile); free(tempfile); + tempfile = NULL; close(fd); if (strlen(obj->name) > _MAXNAMELEN_SVR4) add_to_ar_str_table(ecp, obj->name); ecp->rela_off += _ARHDR_LEN + obj->size + obj->size % 2; STAILQ_INSERT_TAIL(&ecp->v_arobj, obj, objs); } /* * Append to the archive string table buffer. */ static void add_to_ar_str_table(struct elfcopy *ecp, const char *name) { if (ecp->as == NULL) { ecp->as_cap = _INIT_AS_CAP; ecp->as_sz = 0; if ((ecp->as = malloc(ecp->as_cap)) == NULL) err(EXIT_FAILURE, "malloc failed"); } /* * The space required for holding one member name in as table includes: * strlen(name) + (1 for '/') + (1 for '\n') + (possibly 1 for padding). */ while (ecp->as_sz + strlen(name) + 3 > ecp->as_cap) { ecp->as_cap *= 2; ecp->as = realloc(ecp->as, ecp->as_cap); if (ecp->as == NULL) err(EXIT_FAILURE, "realloc failed"); } strncpy(&ecp->as[ecp->as_sz], name, strlen(name)); ecp->as_sz += strlen(name); ecp->as[ecp->as_sz++] = '/'; ecp->as[ecp->as_sz++] = '\n'; } /* * Append to the archive symbol table buffer. */ static void add_to_ar_sym_table(struct elfcopy *ecp, const char *name) { if (ecp->s_so == NULL) { if ((ecp->s_so = malloc(_INIT_SYMOFF_CAP)) == NULL) err(EXIT_FAILURE, "malloc failed"); ecp->s_so_cap = _INIT_SYMOFF_CAP; ecp->s_cnt = 0; } if (ecp->s_sn == NULL) { if ((ecp->s_sn = malloc(_INIT_SYMNAME_CAP)) == NULL) err(EXIT_FAILURE, "malloc failed"); ecp->s_sn_cap = _INIT_SYMNAME_CAP; ecp->s_sn_sz = 0; } if (ecp->s_cnt * sizeof(uint32_t) >= ecp->s_so_cap) { ecp->s_so_cap *= 2; ecp->s_so = realloc(ecp->s_so, ecp->s_so_cap); if (ecp->s_so == NULL) err(EXIT_FAILURE, "realloc failed"); } ecp->s_so[ecp->s_cnt] = ecp->rela_off; ecp->s_cnt++; /* * The space required for holding one symbol name in sn table includes: * strlen(name) + (1 for '\n') + (possibly 1 for padding). */ while (ecp->s_sn_sz + strlen(name) + 2 > ecp->s_sn_cap) { ecp->s_sn_cap *= 2; ecp->s_sn = realloc(ecp->s_sn, ecp->s_sn_cap); if (ecp->s_sn == NULL) err(EXIT_FAILURE, "realloc failed"); } strncpy(&ecp->s_sn[ecp->s_sn_sz], name, strlen(name)); ecp->s_sn_sz += strlen(name); ecp->s_sn[ecp->s_sn_sz++] = '\0'; } static void sync_ar(struct elfcopy *ecp) { size_t s_sz; /* size of archive symbol table. */ size_t pm_sz; /* size of pseudo members */ int i; /* * Pad the symbol name string table. It is treated specially because * symbol name table should be padded by a '\0', not the common '\n' * for other members. The size of sn table includes the pad bit. */ if (ecp->s_cnt != 0 && ecp->s_sn_sz % 2 != 0) ecp->s_sn[ecp->s_sn_sz++] = '\0'; /* * Archive string table is padded by a "\n" as the normal members. * The difference is that the size of archive string table counts * in the pad bit, while normal members' size fileds do not. */ if (ecp->as != NULL && ecp->as_sz % 2 != 0) ecp->as[ecp->as_sz++] = '\n'; /* * If there is a symbol table, calculate the size of pseudo members, * convert previously stored relative offsets to absolute ones, and * then make them Big Endian. * * absolute_offset = htobe32(relative_offset + size_of_pseudo_members) */ if (ecp->s_cnt != 0) { s_sz = (ecp->s_cnt + 1) * sizeof(uint32_t) + ecp->s_sn_sz; pm_sz = _ARMAG_LEN + (_ARHDR_LEN + s_sz); if (ecp->as != NULL) pm_sz += _ARHDR_LEN + ecp->as_sz; for (i = 0; (size_t)i < ecp->s_cnt; i++) *(ecp->s_so + i) = htobe32(*(ecp->s_so + i) + pm_sz); } } /* * Extract global symbols from archive members. */ static void extract_arsym(struct elfcopy *ecp) { Elf_Scn *scn; GElf_Shdr shdr; GElf_Sym sym; Elf_Data *data; char *name; size_t n, shstrndx; int elferr, tabndx, len, i; if (elf_kind(ecp->eout) != ELF_K_ELF) { warnx("internal: cannot extract symbols from non-elf object"); return; } if (elf_getshstrndx(ecp->eout, &shstrndx) == 0) { warnx("elf_getshstrndx failed: %s", elf_errmsg(-1)); return; } tabndx = -1; scn = NULL; while ((scn = elf_nextscn(ecp->eout, scn)) != NULL) { if (gelf_getshdr(scn, &shdr) != &shdr) { warnx("elf_getshdr failed: %s", elf_errmsg(-1)); continue; } if ((name = elf_strptr(ecp->eout, shstrndx, shdr.sh_name)) == NULL) { warnx("elf_strptr failed: %s", elf_errmsg(-1)); continue; } if (strcmp(name, ".strtab") == 0) { tabndx = elf_ndxscn(scn); break; } } elferr = elf_errno(); if (elferr != 0) warnx("elf_nextscn failed: %s", elf_errmsg(elferr)); /* Ignore members without symbol table. */ if (tabndx == -1) return; scn = NULL; while ((scn = elf_nextscn(ecp->eout, scn)) != NULL) { if (gelf_getshdr(scn, &shdr) != &shdr) { warnx("elf_getshdr failed: %s", elf_errmsg(-1)); continue; } if (shdr.sh_type != SHT_SYMTAB) continue; data = NULL; n = 0; while (n < shdr.sh_size && (data = elf_getdata(scn, data)) != NULL) { len = data->d_size / shdr.sh_entsize; for (i = 0; i < len; i++) { if (gelf_getsym(data, i, &sym) != &sym) { warnx("gelf_getsym failed: %s", elf_errmsg(-1)); continue; } /* keep only global or weak symbols */ if (GELF_ST_BIND(sym.st_info) != STB_GLOBAL && GELF_ST_BIND(sym.st_info) != STB_WEAK) continue; /* keep only defined symbols */ if (sym.st_shndx == SHN_UNDEF) continue; if ((name = elf_strptr(ecp->eout, tabndx, sym.st_name)) == NULL) { warnx("elf_strptr failed: %s", elf_errmsg(-1)); continue; } add_to_ar_sym_table(ecp, name); } } } elferr = elf_errno(); if (elferr != 0) warnx("elf_nextscn failed: %s", elf_errmsg(elferr)); } #ifndef LIBELF_AR /* * Convenient wrapper for general libarchive error handling. */ #define AC(CALL) do { \ if ((CALL)) \ errx(EXIT_FAILURE, "%s", archive_error_string(a)); \ } while (0) /* Earlier versions of libarchive had some functions that returned 'void'. */ #if ARCHIVE_VERSION_NUMBER >= 2000000 #define ACV(CALL) AC(CALL) #else #define ACV(CALL) do { \ (CALL); \ } while (0) #endif int ac_detect_ar(int ifd) { struct archive *a; struct archive_entry *entry; int r; r = -1; if ((a = archive_read_new()) == NULL) return (0); archive_read_support_format_ar(a); if (archive_read_open_fd(a, ifd, 10240) == ARCHIVE_OK) r = archive_read_next_header(a, &entry); archive_read_close(a); archive_read_free(a); return (r == ARCHIVE_OK); } void ac_create_ar(struct elfcopy *ecp, int ifd, int ofd) { ac_read_objs(ecp, ifd); sync_ar(ecp); ac_write_objs(ecp, ofd); ac_write_cleanup(ecp); } static void ac_read_objs(struct elfcopy *ecp, int ifd) { struct archive *a; struct archive_entry *entry; struct ar_obj *obj; const char *name; char *buff; size_t size; int r; ecp->rela_off = 0; if (lseek(ifd, 0, SEEK_SET) == -1) err(EXIT_FAILURE, "lseek failed"); if ((a = archive_read_new()) == NULL) errx(EXIT_FAILURE, "archive_read_new failed"); archive_read_support_format_ar(a); AC(archive_read_open_fd(a, ifd, 10240)); for(;;) { r = archive_read_next_header(a, &entry); if (r == ARCHIVE_FATAL) errx(EXIT_FAILURE, "%s", archive_error_string(a)); if (r == ARCHIVE_EOF) break; if (r == ARCHIVE_WARN || r == ARCHIVE_RETRY) warnx("%s", archive_error_string(a)); if (r == ARCHIVE_RETRY) continue; name = archive_entry_pathname(entry); /* skip pseudo members. */ if (strcmp(name, "/") == 0 || strcmp(name, "//") == 0) continue; size = archive_entry_size(entry); if (size > 0) { if ((buff = malloc(size)) == NULL) err(EXIT_FAILURE, "malloc failed"); if (archive_read_data(a, buff, size) != (ssize_t)size) { warnx("%s", archive_error_string(a)); free(buff); continue; } if ((obj = malloc(sizeof(*obj))) == NULL) err(EXIT_FAILURE, "malloc failed"); if ((obj->name = strdup(name)) == NULL) err(EXIT_FAILURE, "strdup failed"); obj->buf = buff; obj->uid = archive_entry_uid(entry); obj->gid = archive_entry_gid(entry); obj->md = archive_entry_mode(entry); obj->mtime = archive_entry_mtime(entry); if ((ecp->ein = elf_memory(buff, size)) == NULL) errx(EXIT_FAILURE, "elf_memory() failed: %s", elf_errmsg(-1)); if (elf_kind(ecp->ein) != ELF_K_ELF) errx(EXIT_FAILURE, "file format not recognized"); process_ar_obj(ecp, obj); } } AC(archive_read_close(a)); ACV(archive_read_free(a)); } static void ac_write_objs(struct elfcopy *ecp, int ofd) { struct archive *a; struct archive_entry *entry; struct ar_obj *obj; time_t timestamp; int nr; if ((a = archive_write_new()) == NULL) errx(EXIT_FAILURE, "archive_write_new failed"); archive_write_set_format_ar_svr4(a); AC(archive_write_open_fd(a, ofd)); /* Write the archive symbol table, even if it's empty. */ entry = archive_entry_new(); archive_entry_copy_pathname(entry, "/"); if (elftc_timestamp(×tamp) != 0) err(EXIT_FAILURE, "elftc_timestamp"); archive_entry_set_mtime(entry, timestamp, 0); archive_entry_set_size(entry, (ecp->s_cnt + 1) * sizeof(uint32_t) + ecp->s_sn_sz); AC(archive_write_header(a, entry)); nr = htobe32(ecp->s_cnt); ac_write_data(a, &nr, sizeof(uint32_t)); ac_write_data(a, ecp->s_so, sizeof(uint32_t) * ecp->s_cnt); ac_write_data(a, ecp->s_sn, ecp->s_sn_sz); archive_entry_free(entry); /* Write the archive string table, if exist. */ if (ecp->as != NULL) { entry = archive_entry_new(); archive_entry_copy_pathname(entry, "//"); archive_entry_set_size(entry, ecp->as_sz); AC(archive_write_header(a, entry)); ac_write_data(a, ecp->as, ecp->as_sz); archive_entry_free(entry); } /* Write normal members. */ STAILQ_FOREACH(obj, &ecp->v_arobj, objs) { entry = archive_entry_new(); archive_entry_copy_pathname(entry, obj->name); archive_entry_set_uid(entry, obj->uid); archive_entry_set_gid(entry, obj->gid); archive_entry_set_mode(entry, obj->md); archive_entry_set_size(entry, obj->size); archive_entry_set_mtime(entry, obj->mtime, 0); archive_entry_set_filetype(entry, AE_IFREG); AC(archive_write_header(a, entry)); ac_write_data(a, obj->maddr, obj->size); archive_entry_free(entry); } AC(archive_write_close(a)); ACV(archive_write_free(a)); } static void ac_write_cleanup(struct elfcopy *ecp) { struct ar_obj *obj, *obj_temp; STAILQ_FOREACH_SAFE(obj, &ecp->v_arobj, objs, obj_temp) { STAILQ_REMOVE(&ecp->v_arobj, obj, ar_obj, objs); if (obj->maddr != NULL) free(obj->maddr); free(obj->name); free(obj); } free(ecp->as); free(ecp->s_so); free(ecp->s_sn); ecp->as = NULL; ecp->s_so = NULL; ecp->s_sn = NULL; } /* * Wrapper for archive_write_data(). */ static void ac_write_data(struct archive *a, const void *buf, size_t s) { if (archive_write_data(a, buf, s) != (ssize_t)s) errx(EXIT_FAILURE, "%s", archive_error_string(a)); } #endif /* ! LIBELF_AR */ diff --git a/contrib/elftoolchain/elfcopy/elfcopy.h b/contrib/elftoolchain/elfcopy/elfcopy.h index b8845a574428..17d8b803156d 100644 --- a/contrib/elftoolchain/elfcopy/elfcopy.h +++ b/contrib/elftoolchain/elfcopy/elfcopy.h @@ -1,324 +1,325 @@ /*- * Copyright (c) 2007-2013 Kai Wang * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $Id: elfcopy.h 3757 2019-06-28 01:15:28Z emaste $ */ #include #include #include #include "_elftc.h" /* * User specified symbol operation (strip, keep, localize, globalize, * weaken, rename, etc). */ struct symop { const char *name; const char *newname; #define SYMOP_KEEP 0x0001U #define SYMOP_STRIP 0x0002U #define SYMOP_GLOBALIZE 0x0004U #define SYMOP_LOCALIZE 0x0008U #define SYMOP_KEEPG 0x0010U #define SYMOP_WEAKEN 0x0020U #define SYMOP_REDEF 0x0040U unsigned int op; STAILQ_ENTRY(symop) symop_list; }; /* File containing symbol list. */ struct symfile { dev_t dev; ino_t ino; size_t size; char *data; unsigned int op; STAILQ_ENTRY(symfile) symfile_list; }; /* Sections to copy/remove/rename/... */ struct sec_action { const char *name; const char *addopt; const char *newname; const char *string; uint64_t lma; uint64_t vma; int64_t lma_adjust; int64_t vma_adjust; #define SF_ALLOC 0x0001U #define SF_LOAD 0x0002U #define SF_NOLOAD 0x0004U #define SF_READONLY 0x0008U #define SF_DEBUG 0x0010U #define SF_CODE 0x0020U #define SF_DATA 0x0040U #define SF_ROM 0x0080U #define SF_SHARED 0X0100U #define SF_CONTENTS 0x0200U int flags; int add; int append; int compress; int copy; int print; int remove; int rename; int setflags; int setlma; int setvma; STAILQ_ENTRY(sec_action) sac_list; }; /* Sections to add from file. */ struct sec_add { char *name; char *content; size_t size; STAILQ_ENTRY(sec_add) sadd_list; }; struct segment; /* Internal data structure for sections. */ struct section { struct segment *seg; /* containing segment */ struct segment *seg_tls; /* tls segment */ const char *name; /* section name */ char *newname; /* new section name */ Elf_Scn *is; /* input scn */ Elf_Scn *os; /* output scn */ void *buf; /* section content */ uint8_t *pad; /* section padding */ uint64_t off; /* section offset */ uint64_t sz; /* section size */ uint64_t cap; /* section capacity */ uint64_t align; /* section alignment */ uint64_t type; /* section type */ uint64_t flags; /* section flags */ uint64_t vma; /* section virtual addr */ uint64_t lma; /* section load addr */ uint64_t pad_sz;/* section padding size */ int loadable; /* whether loadable */ int pseudo; int nocopy; Elftc_String_Table *strtab; TAILQ_ENTRY(section) sec_list; /* next section */ }; TAILQ_HEAD(sectionlist, section); /* Internal data structure for segments. */ struct segment { uint64_t vaddr; /* virtual addr (VMA) */ uint64_t paddr; /* physical addr (LMA) */ uint64_t off; /* file offset */ uint64_t fsz; /* file size */ uint64_t msz; /* memory size */ uint64_t type; /* segment type */ int remove; /* whether remove */ int nsec; /* number of sections contained */ struct section **v_sec; /* list of sections contained */ STAILQ_ENTRY(segment) seg_list; /* next segment */ }; /* * In-memory representation of ar(1) archive member(object). */ struct ar_obj { char *name; /* member name */ char *buf; /* member content */ void *maddr; /* mmap start address */ uid_t uid; /* user id */ gid_t gid; /* group id */ mode_t md; /* octal file permissions */ size_t size; /* member size */ time_t mtime; /* modification time */ STAILQ_ENTRY(ar_obj) objs; }; /* * Structure encapsulates the "global" data for "elfcopy" program. */ struct elfcopy { const char *progname; /* program name */ int iec; /* elfclass of input object */ Elftc_Bfd_Target_Flavor itf; /* flavour of input object */ Elftc_Bfd_Target_Flavor otf; /* flavour of output object */ const char *otgt; /* output target name */ int oec; /* elfclass of output object */ unsigned char oed; /* endianness of output object */ int oem; /* EM_XXX of output object */ int abi; /* OSABI of output object */ Elf *ein; /* ELF descriptor of input object */ Elf *eout; /* ELF descriptor of output object */ int iphnum; /* num. of input object phdr entries */ int ophnum; /* num. of output object phdr entries */ int nos; /* num. of output object sections */ enum { STRIP_NONE = 0, STRIP_ALL, STRIP_DEBUG, STRIP_DWO, STRIP_NONDEBUG, STRIP_NONDWO, STRIP_UNNEEDED } strip; #define EXECUTABLE 0x00000001U #define DYNAMIC 0x00000002U #define RELOCATABLE 0x00000004U #define SYMTAB_EXIST 0x00000010U #define SYMTAB_INTACT 0x00000020U #define KEEP_GLOBAL 0x00000040U #define DISCARD_LOCAL 0x00000080U #define WEAKEN_ALL 0x00000100U #define PRESERVE_DATE 0x00001000U #define SREC_FORCE_S3 0x00002000U #define SREC_FORCE_LEN 0x00004000U #define SET_START 0x00008000U #define GAP_FILL 0x00010000U #define WILDCARD 0x00020000U #define NO_CHANGE_WARN 0x00040000U #define SEC_ADD 0x00080000U #define SEC_APPEND 0x00100000U #define SEC_COMPRESS 0x00200000U #define SEC_PRINT 0x00400000U #define SEC_REMOVE 0x00800000U #define SEC_COPY 0x01000000U #define DISCARD_LLABEL 0x02000000U #define LOCALIZE_HIDDEN 0x04000000U int flags; /* elfcopy run control flags. */ int64_t change_addr; /* Section address adjustment. */ int64_t change_start; /* Entry point adjustment. */ uint64_t set_start; /* Entry point value. */ unsigned long srec_len; /* S-Record length. */ uint64_t pad_to; /* load address padding. */ uint8_t fill; /* gap fill value. */ char *prefix_sec; /* section prefix. */ char *prefix_alloc; /* alloc section prefix. */ char *prefix_sym; /* symbol prefix. */ char *debuglink; /* GNU debuglink file. */ struct section *symtab; /* .symtab section. */ struct section *strtab; /* .strtab section. */ struct section *shstrtab; /* .shstrtab section. */ uint64_t *secndx; /* section index map. */ uint64_t *symndx; /* symbol index map. */ unsigned char *v_rel; /* symbols needed by relocation. */ unsigned char *v_grp; /* symbols referred by section group. */ unsigned char *v_secsym; /* sections with section symbol. */ STAILQ_HEAD(, segment) v_seg; /* list of segments. */ STAILQ_HEAD(, sec_action) v_sac;/* list of section operations. */ STAILQ_HEAD(, sec_add) v_sadd; /* list of sections to add. */ STAILQ_HEAD(, symop) v_symop; /* list of symbols operations. */ STAILQ_HEAD(, symfile) v_symfile; /* list of symlist files. */ TAILQ_HEAD(, section) v_sec; /* list of sections. */ /* * Fields for the ar(1) archive. */ char *as; /* buffer for archive string table. */ size_t as_sz; /* current size of as table. */ size_t as_cap; /* capacity of as table buffer. */ uint32_t s_cnt; /* current number of symbols. */ uint32_t *s_so; /* symbol offset table. */ size_t s_so_cap; /* capacity of so table buffer. */ char *s_sn; /* symbol name table */ size_t s_sn_cap; /* capacity of sn table buffer. */ size_t s_sn_sz; /* current size of sn table. */ off_t rela_off; /* offset relative to pseudo members. */ STAILQ_HEAD(, ar_obj) v_arobj; /* archive object(member) list. */ }; void add_section(struct elfcopy *_ecp, const char *_optarg); void add_to_shstrtab(struct elfcopy *_ecp, const char *_name); void add_to_symop_list(struct elfcopy *_ecp, const char *_name, const char *_newname, unsigned int _op); void add_to_symtab(struct elfcopy *_ecp, const char *_name, uint64_t _st_value, uint64_t _st_size, uint16_t _st_shndx, unsigned char _st_info, unsigned char _st_other, int _ndx_known); int add_to_inseg_list(struct elfcopy *_ecp, struct section *_sec); void adjust_addr(struct elfcopy *_ecp); +int cleanup_tempfile(char *_fn); void copy_content(struct elfcopy *_ecp); void copy_data(struct section *_s); void copy_phdr(struct elfcopy *_ecp); void copy_shdr(struct elfcopy *_ecp, struct section *_s, const char *_name, int _copy, int _sec_flags); void create_binary(int _ifd, int _ofd); void create_elf(struct elfcopy *_ecp); void create_elf_from_binary(struct elfcopy *_ecp, int _ifd, const char *ifn); void create_elf_from_ihex(struct elfcopy *_ecp, int _ifd); void create_elf_from_srec(struct elfcopy *_ecp, int _ifd); struct section *create_external_section(struct elfcopy *_ecp, const char *_name, char *_newname, void *_buf, uint64_t _size, uint64_t _off, uint64_t _stype, Elf_Type _dtype, uint64_t flags, uint64_t _align, uint64_t _vma, int _loadable); void create_external_symtab(struct elfcopy *_ecp); void create_ihex(int _ifd, int _ofd); void create_pe(struct elfcopy *_ecp, int _ifd, int _ofd); void create_scn(struct elfcopy *_ecp); void create_srec(struct elfcopy *_ecp, int _ifd, int _ofd, const char *_ofn); void create_symtab(struct elfcopy *_ecp); void create_symtab_data(struct elfcopy *_ecp); void create_tempfile(const char *_src, char **_fn, int *_fd); void finalize_external_symtab(struct elfcopy *_ecp); void free_elf(struct elfcopy *_ecp); void free_sec_act(struct elfcopy *_ecp); void free_sec_add(struct elfcopy *_ecp); void free_symtab(struct elfcopy *_ecp); void init_shstrtab(struct elfcopy *_ecp); void insert_to_sec_list(struct elfcopy *_ecp, struct section *_sec, int _tail); struct section *insert_shtab(struct elfcopy *_ecp, int tail); int is_remove_reloc_sec(struct elfcopy *_ecp, uint32_t _sh_info); int is_remove_section(struct elfcopy *_ecp, const char *_name); struct sec_action *lookup_sec_act(struct elfcopy *_ecp, const char *_name, int _add); struct symop *lookup_symop_list(struct elfcopy *_ecp, const char *_name, unsigned int _op); void resync_sections(struct elfcopy *_ecp); void setup_phdr(struct elfcopy *_ecp); void update_shdr(struct elfcopy *_ecp, int _update_link); #ifndef LIBELF_AR int ac_detect_ar(int _ifd); void ac_create_ar(struct elfcopy *_ecp, int _ifd, int _ofd); #endif /* ! LIBELF_AR */ diff --git a/contrib/elftoolchain/elfcopy/main.c b/contrib/elftoolchain/elfcopy/main.c index c02bb296c4a3..964d3358de60 100644 --- a/contrib/elftoolchain/elfcopy/main.c +++ b/contrib/elftoolchain/elfcopy/main.c @@ -1,1637 +1,1684 @@ /*- * Copyright (c) 2007-2013 Kai Wang * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include #include #include #include #include #include #include #include #include #include #include #include "elfcopy.h" ELFTC_VCSID("$Id: main.c 3757 2019-06-28 01:15:28Z emaste $"); enum options { ECP_ADD_GNU_DEBUGLINK, ECP_ADD_SECTION, ECP_CHANGE_ADDR, ECP_CHANGE_SEC_ADDR, ECP_CHANGE_SEC_LMA, ECP_CHANGE_SEC_VMA, ECP_CHANGE_START, ECP_CHANGE_WARN, ECP_GAP_FILL, ECP_GLOBALIZE_SYMBOL, ECP_GLOBALIZE_SYMBOLS, ECP_KEEP_SYMBOLS, ECP_KEEP_GLOBAL_SYMBOLS, ECP_LOCALIZE_HIDDEN, ECP_LOCALIZE_SYMBOLS, ECP_NO_CHANGE_WARN, ECP_ONLY_DEBUG, ECP_ONLY_DWO, ECP_PAD_TO, ECP_PREFIX_ALLOC, ECP_PREFIX_SEC, ECP_PREFIX_SYM, ECP_REDEF_SYMBOL, ECP_REDEF_SYMBOLS, ECP_RENAME_SECTION, ECP_SET_OSABI, ECP_SET_SEC_FLAGS, ECP_SET_START, ECP_SREC_FORCE_S3, ECP_SREC_LEN, ECP_STRIP_DWO, ECP_STRIP_SYMBOLS, ECP_STRIP_UNNEEDED, ECP_WEAKEN_ALL, ECP_WEAKEN_SYMBOLS }; static struct option mcs_longopts[] = { { "help", no_argument, NULL, 'h' }, { "version", no_argument, NULL, 'V' }, { NULL, 0, NULL, 0 } }; static struct option strip_longopts[] = { {"discard-all", no_argument, NULL, 'x'}, {"discard-locals", no_argument, NULL, 'X'}, {"help", no_argument, NULL, 'h'}, {"input-target", required_argument, NULL, 'I'}, {"keep-symbol", required_argument, NULL, 'K'}, {"only-keep-debug", no_argument, NULL, ECP_ONLY_DEBUG}, {"output-file", required_argument, NULL, 'o'}, {"output-target", required_argument, NULL, 'O'}, {"preserve-dates", no_argument, NULL, 'p'}, {"remove-section", required_argument, NULL, 'R'}, {"strip-all", no_argument, NULL, 's'}, {"strip-debug", no_argument, NULL, 'S'}, {"strip-symbol", required_argument, NULL, 'N'}, {"strip-unneeded", no_argument, NULL, ECP_STRIP_UNNEEDED}, {"version", no_argument, NULL, 'V'}, {"wildcard", no_argument, NULL, 'w'}, {NULL, 0, NULL, 0} }; static struct option elfcopy_longopts[] = { {"add-gnu-debuglink", required_argument, NULL, ECP_ADD_GNU_DEBUGLINK}, {"add-section", required_argument, NULL, ECP_ADD_SECTION}, {"adjust-section-vma", required_argument, NULL, ECP_CHANGE_SEC_ADDR}, {"adjust-vma", required_argument, NULL, ECP_CHANGE_ADDR}, {"adjust-start", required_argument, NULL, ECP_CHANGE_START}, {"adjust-warnings", no_argument, NULL, ECP_CHANGE_WARN}, {"binary-architecture", required_argument, NULL, 'B'}, {"change-addresses", required_argument, NULL, ECP_CHANGE_ADDR}, {"change-section-address", required_argument, NULL, ECP_CHANGE_SEC_ADDR}, {"change-section-lma", required_argument, NULL, ECP_CHANGE_SEC_LMA}, {"change-section-vma", required_argument, NULL, ECP_CHANGE_SEC_VMA}, {"change-start", required_argument, NULL, ECP_CHANGE_START}, {"change-warnings", no_argument, NULL, ECP_CHANGE_WARN}, {"discard-all", no_argument, NULL, 'x'}, {"discard-locals", no_argument, NULL, 'X'}, {"extract-dwo", no_argument, NULL, ECP_ONLY_DWO}, {"gap-fill", required_argument, NULL, ECP_GAP_FILL}, {"globalize-symbol", required_argument, NULL, ECP_GLOBALIZE_SYMBOL}, {"globalize-symbols", required_argument, NULL, ECP_GLOBALIZE_SYMBOLS}, {"help", no_argument, NULL, 'h'}, {"input-target", required_argument, NULL, 'I'}, {"keep-symbol", required_argument, NULL, 'K'}, {"keep-symbols", required_argument, NULL, ECP_KEEP_SYMBOLS}, {"keep-global-symbol", required_argument, NULL, 'G'}, {"keep-global-symbols", required_argument, NULL, ECP_KEEP_GLOBAL_SYMBOLS}, {"localize-hidden", no_argument, NULL, ECP_LOCALIZE_HIDDEN}, {"localize-symbol", required_argument, NULL, 'L'}, {"localize-symbols", required_argument, NULL, ECP_LOCALIZE_SYMBOLS}, {"no-adjust-warnings", no_argument, NULL, ECP_NO_CHANGE_WARN}, {"no-change-warnings", no_argument, NULL, ECP_NO_CHANGE_WARN}, {"only-keep-debug", no_argument, NULL, ECP_ONLY_DEBUG}, {"only-section", required_argument, NULL, 'j'}, {"osabi", required_argument, NULL, ECP_SET_OSABI}, {"output-target", required_argument, NULL, 'O'}, {"pad-to", required_argument, NULL, ECP_PAD_TO}, {"preserve-dates", no_argument, NULL, 'p'}, {"prefix-alloc-sections", required_argument, NULL, ECP_PREFIX_ALLOC}, {"prefix-sections", required_argument, NULL, ECP_PREFIX_SEC}, {"prefix-symbols", required_argument, NULL, ECP_PREFIX_SYM}, {"redefine-sym", required_argument, NULL, ECP_REDEF_SYMBOL}, {"redefine-syms", required_argument, NULL, ECP_REDEF_SYMBOLS}, {"remove-section", required_argument, NULL, 'R'}, {"rename-section", required_argument, NULL, ECP_RENAME_SECTION}, {"set-section-flags", required_argument, NULL, ECP_SET_SEC_FLAGS}, {"set-start", required_argument, NULL, ECP_SET_START}, {"srec-forceS3", no_argument, NULL, ECP_SREC_FORCE_S3}, {"srec-len", required_argument, NULL, ECP_SREC_LEN}, {"strip-all", no_argument, NULL, 'S'}, {"strip-debug", no_argument, 0, 'g'}, {"strip-dwo", no_argument, NULL, ECP_STRIP_DWO}, {"strip-symbol", required_argument, NULL, 'N'}, {"strip-symbols", required_argument, NULL, ECP_STRIP_SYMBOLS}, {"strip-unneeded", no_argument, NULL, ECP_STRIP_UNNEEDED}, {"version", no_argument, NULL, 'V'}, {"weaken", no_argument, NULL, ECP_WEAKEN_ALL}, {"weaken-symbol", required_argument, NULL, 'W'}, {"weaken-symbols", required_argument, NULL, ECP_WEAKEN_SYMBOLS}, {"wildcard", no_argument, NULL, 'w'}, {NULL, 0, NULL, 0} }; static struct { const char *name; int value; } sec_flags[] = { {"alloc", SF_ALLOC}, {"load", SF_LOAD}, {"noload", SF_NOLOAD}, {"readonly", SF_READONLY}, {"debug", SF_DEBUG}, {"code", SF_CODE}, {"data", SF_DATA}, {"rom", SF_ROM}, {"share", SF_SHARED}, {"contents", SF_CONTENTS}, {NULL, 0} }; static struct { const char *name; int abi; } osabis[] = { {"sysv", ELFOSABI_SYSV}, {"hpus", ELFOSABI_HPUX}, {"netbsd", ELFOSABI_NETBSD}, {"linux", ELFOSABI_LINUX}, {"hurd", ELFOSABI_HURD}, {"86open", ELFOSABI_86OPEN}, {"solaris", ELFOSABI_SOLARIS}, {"aix", ELFOSABI_AIX}, {"irix", ELFOSABI_IRIX}, {"freebsd", ELFOSABI_FREEBSD}, {"tru64", ELFOSABI_TRU64}, {"modesto", ELFOSABI_MODESTO}, {"openbsd", ELFOSABI_OPENBSD}, {"openvms", ELFOSABI_OPENVMS}, {"nsk", ELFOSABI_NSK}, {"cloudabi", ELFOSABI_CLOUDABI}, {"arm", ELFOSABI_ARM}, {"standalone", ELFOSABI_STANDALONE}, {NULL, 0} }; static int copy_from_tempfile(const char *src, const char *dst, int infd, int *outfd, int in_place); static void create_file(struct elfcopy *ecp, const char *src, const char *dst); static void elfcopy_main(struct elfcopy *ecp, int argc, char **argv); static void elfcopy_usage(void); static void mcs_main(struct elfcopy *ecp, int argc, char **argv); static void mcs_usage(void); static void parse_sec_address_op(struct elfcopy *ecp, int optnum, const char *optname, char *s); static void parse_sec_flags(struct sec_action *sac, char *s); static void parse_symlist_file(struct elfcopy *ecp, const char *fn, unsigned int op); static void print_version(void); static void set_input_target(struct elfcopy *ecp, const char *target_name); static void set_osabi(struct elfcopy *ecp, const char *abi); static void set_output_target(struct elfcopy *ecp, const char *target_name); static void strip_main(struct elfcopy *ecp, int argc, char **argv); static void strip_usage(void); /* * An ELF object usually has a structure described by the * diagram below. * _____________ * | | * | NULL | <- always a SHT_NULL section * |_____________| * | | * | .interp | * |_____________| * | | * | ... | * |_____________| * | | * | .text | * |_____________| * | | * | ... | * |_____________| * | | * | .comment | <- above(include) this: normal sections * |_____________| * | | * | add sections| <- unloadable sections added by --add-section * |_____________| * | | * | .shstrtab | <- section name string table * |_____________| * | | * | shdrs | <- section header table * |_____________| * | | * | .symtab | <- symbol table, if any * |_____________| * | | * | .strtab | <- symbol name string table, if any * |_____________| * | | * | .rel.text | <- relocation info for .o files. * |_____________| */ void create_elf(struct elfcopy *ecp) { struct section *shtab; GElf_Ehdr ieh; GElf_Ehdr oeh; size_t ishnum; ecp->flags |= SYMTAB_INTACT; ecp->flags &= ~SYMTAB_EXIST; /* Create EHDR. */ if (gelf_getehdr(ecp->ein, &ieh) == NULL) errx(EXIT_FAILURE, "gelf_getehdr() failed: %s", elf_errmsg(-1)); if ((ecp->iec = gelf_getclass(ecp->ein)) == ELFCLASSNONE) errx(EXIT_FAILURE, "getclass() failed: %s", elf_errmsg(-1)); if (ecp->oec == ELFCLASSNONE) ecp->oec = ecp->iec; if (ecp->oed == ELFDATANONE) ecp->oed = ieh.e_ident[EI_DATA]; if (gelf_newehdr(ecp->eout, ecp->oec) == NULL) errx(EXIT_FAILURE, "gelf_newehdr failed: %s", elf_errmsg(-1)); if (gelf_getehdr(ecp->eout, &oeh) == NULL) errx(EXIT_FAILURE, "gelf_getehdr() failed: %s", elf_errmsg(-1)); memcpy(oeh.e_ident, ieh.e_ident, sizeof(ieh.e_ident)); oeh.e_ident[EI_CLASS] = ecp->oec; oeh.e_ident[EI_DATA] = ecp->oed; if (ecp->abi != -1) oeh.e_ident[EI_OSABI] = ecp->abi; oeh.e_flags = ieh.e_flags; oeh.e_machine = ieh.e_machine; oeh.e_type = ieh.e_type; oeh.e_entry = ieh.e_entry; oeh.e_version = ieh.e_version; ecp->flags &= ~(EXECUTABLE | DYNAMIC | RELOCATABLE); if (ieh.e_type == ET_EXEC) ecp->flags |= EXECUTABLE; else if (ieh.e_type == ET_DYN) ecp->flags |= DYNAMIC; else if (ieh.e_type == ET_REL) ecp->flags |= RELOCATABLE; else errx(EXIT_FAILURE, "unsupported e_type"); if (!elf_getshnum(ecp->ein, &ishnum)) errx(EXIT_FAILURE, "elf_getshnum failed: %s", elf_errmsg(-1)); if (ishnum > 0 && (ecp->secndx = calloc(ishnum, sizeof(*ecp->secndx))) == NULL) err(EXIT_FAILURE, "calloc failed"); /* Read input object program header. */ setup_phdr(ecp); /* * Scan of input sections: we iterate through sections from input * object, skip sections need to be stripped, allot Elf_Scn and * create internal section structure for sections we want. * (i.e., determine output sections) */ create_scn(ecp); /* Apply section address changes, if any. */ adjust_addr(ecp); /* * Determine if the symbol table needs to be changed based on * command line options. */ if (ecp->strip == STRIP_DEBUG || ecp->strip == STRIP_UNNEEDED || ecp->flags & WEAKEN_ALL || ecp->flags & LOCALIZE_HIDDEN || ecp->flags & DISCARD_LOCAL || ecp->flags & DISCARD_LLABEL || ecp->prefix_sym != NULL || !STAILQ_EMPTY(&ecp->v_symop)) ecp->flags &= ~SYMTAB_INTACT; /* * Create symbol table. Symbols are filtered or stripped according to * command line args specified by user, and later updated for the new * layout of sections in the output object. */ if ((ecp->flags & SYMTAB_EXIST) != 0) create_symtab(ecp); /* * Write the underlying ehdr. Note that it should be called * before elf_setshstrndx() since it will overwrite e->e_shstrndx. */ if (gelf_update_ehdr(ecp->eout, &oeh) == 0) errx(EXIT_FAILURE, "gelf_update_ehdr() failed: %s", elf_errmsg(-1)); /* * First processing of output sections: at this stage we copy the * content of each section from input to output object. Section * content will be modified and printed (mcs) if need. Also content of * relocation section probably will be filtered and updated according * to symbol table changes. */ copy_content(ecp); /* * Second processing of output sections: Update section headers. * At this stage we set name string index, update st_link and st_info * for output sections. */ update_shdr(ecp, 1); /* Renew oeh to get the updated e_shstrndx. */ if (gelf_getehdr(ecp->eout, &oeh) == NULL) errx(EXIT_FAILURE, "gelf_getehdr() failed: %s", elf_errmsg(-1)); /* * Insert SHDR table into the internal section list as a "pseudo" * section, so later it will get sorted and resynced just as "normal" * sections. * * Under FreeBSD, Binutils objcopy always put the section header * at the end of all the sections. We want to do the same here. * * However, note that the behaviour is still different with Binutils: * elfcopy checks the FreeBSD OSABI tag to tell whether it needs to * move the section headers, while Binutils is probably configured * this way when it's compiled on FreeBSD. */ if (oeh.e_ident[EI_OSABI] == ELFOSABI_FREEBSD) shtab = insert_shtab(ecp, 1); else shtab = insert_shtab(ecp, 0); /* * Resync section offsets in the output object. This is needed * because probably sections are modified or new sections are added, * as a result overlap/gap might appears. */ resync_sections(ecp); /* Store SHDR offset in EHDR. */ oeh.e_shoff = shtab->off; /* Put program header table immediately after the Elf header. */ if (ecp->ophnum > 0) { oeh.e_phoff = gelf_fsize(ecp->eout, ELF_T_EHDR, 1, EV_CURRENT); if (oeh.e_phoff == 0) errx(EXIT_FAILURE, "gelf_fsize() failed: %s", elf_errmsg(-1)); } /* * Update ELF object entry point if requested. */ if (ecp->change_addr != 0) oeh.e_entry += ecp->change_addr; if (ecp->flags & SET_START) oeh.e_entry = ecp->set_start; if (ecp->change_start != 0) oeh.e_entry += ecp->change_start; /* * Update ehdr again before we call elf_update(), since we * modified e_shoff and e_phoff. */ if (gelf_update_ehdr(ecp->eout, &oeh) == 0) errx(EXIT_FAILURE, "gelf_update_ehdr() failed: %s", elf_errmsg(-1)); if (ecp->ophnum > 0) copy_phdr(ecp); /* Write out the output elf object. */ if (elf_update(ecp->eout, ELF_C_WRITE) < 0) errx(EXIT_FAILURE, "elf_update() failed: %s", elf_errmsg(-1)); /* Release allocated resource. */ free_elf(ecp); } void free_elf(struct elfcopy *ecp) { struct segment *seg, *seg_temp; struct section *sec, *sec_temp; /* Free internal segment list. */ if (!STAILQ_EMPTY(&ecp->v_seg)) { STAILQ_FOREACH_SAFE(seg, &ecp->v_seg, seg_list, seg_temp) { STAILQ_REMOVE(&ecp->v_seg, seg, segment, seg_list); free(seg); } } /* Free symbol table buffers. */ free_symtab(ecp); /* Free section name string table. */ elftc_string_table_destroy(ecp->shstrtab->strtab); /* Free internal section list. */ if (!TAILQ_EMPTY(&ecp->v_sec)) { TAILQ_FOREACH_SAFE(sec, &ecp->v_sec, sec_list, sec_temp) { TAILQ_REMOVE(&ecp->v_sec, sec, sec_list); if (sec->buf != NULL) free(sec->buf); if (sec->newname != NULL) free(sec->newname); if (sec->pad != NULL) free(sec->pad); free(sec); } } ecp->symtab = NULL; ecp->strtab = NULL; ecp->shstrtab = NULL; if (ecp->secndx != NULL) { free(ecp->secndx); ecp->secndx = NULL; } } +/* + * Remove a temporary file, without freeing its filename. + * + * Safe to pass NULL, will just ignore it. + */ +int +cleanup_tempfile(char *fn) +{ + int errno_save, retval; + + if (fn == NULL) + return 0; + errno_save = errno; + if ((retval = unlink(fn)) < 0) { + warn("unlink tempfile %s failed", fn); + errno = errno_save; + return retval; + } + return 0; +} + /* Create a temporary file. */ void create_tempfile(const char *src, char **fn, int *fd) { static const char _TEMPDIR[] = "/tmp/"; static const char _TEMPFILE[] = "ecp.XXXXXXXX"; const char *tmpdir; char *tmpf; size_t tlen, slen, plen; if (fn == NULL || fd == NULL) return; for (;;) { if (src == NULL) { /* Respect TMPDIR environment variable. */ tmpdir = getenv("TMPDIR"); if (tmpdir == NULL || *tmpdir == '\0') tmpdir = _TEMPDIR; tlen = strlen(tmpdir); slen = tmpdir[tlen - 1] == '/' ? 0 : 1; } else { /* Create temporary file relative to source file. */ if ((tmpdir = strrchr(src, '/')) == NULL) { /* No path, only use a template filename. */ tlen = 0; } else { /* Append the template after the slash. */ tlen = ++tmpdir - src; tmpdir = src; } slen = 0; } plen = strlen(_TEMPFILE) + 1; tmpf = malloc(tlen + slen + plen); if (tmpf == NULL) err(EXIT_FAILURE, "malloc failed"); if (tlen > 0) memcpy(tmpf, tmpdir, tlen); if (slen > 0) tmpf[tlen] = '/'; /* Copy template filename including NUL terminator. */ memcpy(tmpf + tlen + slen, _TEMPFILE, plen); if ((*fd = mkstemp(tmpf)) != -1) break; if (errno != EACCES || src == NULL) err(EXIT_FAILURE, "mkstemp %s failed", tmpf); /* Permission denied, try again using TMPDIR or /tmp. */ free(tmpf); src = NULL; } if (fchmod(*fd, 0644) == -1) err(EXIT_FAILURE, "fchmod %s failed", tmpf); *fn = tmpf; } /* * Copy temporary file with path src and file descriptor infd to path dst. * If in_place is set act as if editing the file in place, avoiding rename() * to preserve hard and symbolic links. Output file remains open, with file * descriptor returned in outfd. */ static int copy_from_tempfile(const char *src, const char *dst, int infd, int *outfd, int in_place) { int tmpfd; /* * First, check if we can use rename(). */ if (in_place == 0) { if (rename(src, dst) >= 0) { *outfd = infd; return (0); } else if (errno != EXDEV && errno != EACCES) return (-1); /* * If the rename() failed due to 'src' and 'dst' residing in * two different file systems, invoke a helper function in * libelftc to do the copy. */ if (errno != EACCES && unlink(dst) < 0) return (-1); } if ((tmpfd = open(dst, O_CREAT | O_TRUNC | O_WRONLY, 0755)) < 0) return (-1); if (elftc_copyfile(infd, tmpfd) < 0) { (void) close(tmpfd); return (-1); } /* * Remove the temporary file from the file system * namespace, and close its file descriptor. */ if (unlink(src) < 0) { (void) close(tmpfd); return (-1); } (void) close(infd); /* * Return the file descriptor for the destination. */ *outfd = tmpfd; return (0); } static void create_file(struct elfcopy *ecp, const char *src, const char *dst) { struct stat sb; char *tempfile, *elftemp; int efd, ifd, ofd, ofd0, tfd; int in_place; tempfile = NULL; if (src == NULL) errx(EXIT_FAILURE, "internal: src == NULL"); if ((ifd = open(src, O_RDONLY)) == -1) err(EXIT_FAILURE, "open %s failed", src); if (fstat(ifd, &sb) == -1) err(EXIT_FAILURE, "fstat %s failed", src); if (dst == NULL) create_tempfile(src, &tempfile, &ofd); else if ((ofd = open(dst, O_RDWR|O_CREAT, 0755)) == -1) err(EXIT_FAILURE, "open %s failed", dst); #ifndef LIBELF_AR /* Detect and process ar(1) archive using libarchive. */ if (ac_detect_ar(ifd)) { ac_create_ar(ecp, ifd, ofd); goto copy_done; } #endif - if (lseek(ifd, 0, SEEK_SET) < 0) + if (lseek(ifd, 0, SEEK_SET) < 0) { + cleanup_tempfile(tempfile); err(EXIT_FAILURE, "lseek failed"); + } /* * If input object is not ELF file, convert it to an intermediate * ELF object before processing. */ if (ecp->itf != ETF_ELF) { /* * If the output object is not an ELF file, choose an arbitrary * ELF format for the intermediate file. srec, ihex and binary * formats are independent of class, endianness and machine * type so these choices do not affect the output. */ if (ecp->otf != ETF_ELF) { if (ecp->oec == ELFCLASSNONE) ecp->oec = ELFCLASS64; if (ecp->oed == ELFDATANONE) ecp->oed = ELFDATA2LSB; } create_tempfile(src, &elftemp, &efd); - if ((ecp->eout = elf_begin(efd, ELF_C_WRITE, NULL)) == NULL) + if ((ecp->eout = elf_begin(efd, ELF_C_WRITE, NULL)) == NULL) { + cleanup_tempfile(elftemp); + cleanup_tempfile(tempfile); errx(EXIT_FAILURE, "elf_begin() failed: %s", elf_errmsg(-1)); + } elf_flagelf(ecp->eout, ELF_C_SET, ELF_F_LAYOUT); if (ecp->itf == ETF_BINARY) create_elf_from_binary(ecp, ifd, src); else if (ecp->itf == ETF_IHEX) create_elf_from_ihex(ecp, ifd); else if (ecp->itf == ETF_SREC) create_elf_from_srec(ecp, ifd); - else + else { + cleanup_tempfile(elftemp); + cleanup_tempfile(tempfile); errx(EXIT_FAILURE, "Internal: invalid target flavour"); + } elf_end(ecp->eout); /* Open intermediate ELF object as new input object. */ close(ifd); - if ((ifd = open(elftemp, O_RDONLY)) == -1) + if ((ifd = open(elftemp, O_RDONLY)) == -1) { + cleanup_tempfile(elftemp); + cleanup_tempfile(tempfile); err(EXIT_FAILURE, "open %s failed", src); + } close(efd); - if (unlink(elftemp) < 0) + if (cleanup_tempfile(elftemp) < 0) { + cleanup_tempfile(tempfile); err(EXIT_FAILURE, "unlink %s failed", elftemp); + } free(elftemp); + elftemp = NULL; } - if ((ecp->ein = elf_begin(ifd, ELF_C_READ, NULL)) == NULL) + if ((ecp->ein = elf_begin(ifd, ELF_C_READ, NULL)) == NULL) { + cleanup_tempfile(tempfile); errx(EXIT_FAILURE, "elf_begin() failed: %s", elf_errmsg(-1)); + } switch (elf_kind(ecp->ein)) { case ELF_K_NONE: + cleanup_tempfile(tempfile); errx(EXIT_FAILURE, "file format not recognized"); case ELF_K_ELF: - if ((ecp->eout = elf_begin(ofd, ELF_C_WRITE, NULL)) == NULL) + if ((ecp->eout = elf_begin(ofd, ELF_C_WRITE, NULL)) == NULL) { + cleanup_tempfile(tempfile); errx(EXIT_FAILURE, "elf_begin() failed: %s", elf_errmsg(-1)); + } /* elfcopy(1) manage ELF layout by itself. */ elf_flagelf(ecp->eout, ELF_C_SET, ELF_F_LAYOUT); /* * Create output ELF object. */ create_elf(ecp); elf_end(ecp->eout); /* * Convert the output ELF object to binary/srec/ihex if need. */ if (ecp->otf != ETF_ELF) { /* * Create (another) tempfile for binary/srec/ihex * output object. */ - if (tempfile != NULL) { - if (unlink(tempfile) < 0) - err(EXIT_FAILURE, "unlink %s failed", - tempfile); - free(tempfile); - } + if (cleanup_tempfile(tempfile) < 0) + errx(EXIT_FAILURE, "unlink %s failed", + tempfile); + free(tempfile); create_tempfile(src, &tempfile, &ofd0); /* * Rewind the file descriptor being processed. */ - if (lseek(ofd, 0, SEEK_SET) < 0) + if (lseek(ofd, 0, SEEK_SET) < 0) { + cleanup_tempfile(tempfile); err(EXIT_FAILURE, "lseek failed for the output object"); + } /* * Call flavour-specific conversion routine. */ switch (ecp->otf) { case ETF_BINARY: create_binary(ofd, ofd0); break; case ETF_IHEX: create_ihex(ofd, ofd0); break; case ETF_SREC: create_srec(ecp, ofd, ofd0, dst != NULL ? dst : src); break; case ETF_PE: case ETF_EFI: #if WITH_PE create_pe(ecp, ofd, ofd0); #else + cleanup_tempfile(tempfile); errx(EXIT_FAILURE, "PE/EFI support not enabled" " at compile time"); #endif break; default: + cleanup_tempfile(tempfile); errx(EXIT_FAILURE, "Internal: unsupported" " output flavour %d", ecp->oec); } close(ofd); ofd = ofd0; } break; case ELF_K_AR: /* XXX: Not yet supported. */ break; default: + cleanup_tempfile(tempfile); errx(EXIT_FAILURE, "file format not supported"); } elf_end(ecp->ein); #ifndef LIBELF_AR copy_done: #endif if (tempfile != NULL) { in_place = 0; if (dst == NULL) { dst = src; if (lstat(dst, &sb) != -1 && (sb.st_nlink > 1 || S_ISLNK(sb.st_mode))) in_place = 1; } - if (copy_from_tempfile(tempfile, dst, ofd, &tfd, in_place) < 0) + if (copy_from_tempfile(tempfile, dst, ofd, + &tfd, in_place) < 0) { + cleanup_tempfile(tempfile); err(EXIT_FAILURE, "creation of %s failed", dst); + } + /* 'tempfile' has been removed by copy_from_tempfile(). */ free(tempfile); tempfile = NULL; ofd = tfd; } if (strcmp(dst, "/dev/null") && fchmod(ofd, sb.st_mode) == -1) err(EXIT_FAILURE, "fchmod %s failed", dst); if ((ecp->flags & PRESERVE_DATE) && elftc_set_timestamps(dst, &sb) < 0) err(EXIT_FAILURE, "setting timestamps failed"); close(ifd); close(ofd); } static void elfcopy_main(struct elfcopy *ecp, int argc, char **argv) { struct sec_action *sac; const char *infile, *outfile; char *fn, *s; int opt; while ((opt = getopt_long(argc, argv, "dB:gG:I:j:K:L:N:O:pR:s:SwW:xXV", elfcopy_longopts, NULL)) != -1) { switch(opt) { case 'B': /* ignored */ break; case 'R': sac = lookup_sec_act(ecp, optarg, 1); if (sac->copy != 0) errx(EXIT_FAILURE, "both copy and remove specified"); sac->remove = 1; ecp->flags |= SEC_REMOVE; break; case 'S': ecp->strip = STRIP_ALL; break; case 'g': ecp->strip = STRIP_DEBUG; break; case 'G': ecp->flags |= KEEP_GLOBAL; add_to_symop_list(ecp, optarg, NULL, SYMOP_KEEPG); break; case 'I': case 's': set_input_target(ecp, optarg); break; case 'j': sac = lookup_sec_act(ecp, optarg, 1); if (sac->remove != 0) errx(EXIT_FAILURE, "both copy and remove specified"); sac->copy = 1; ecp->flags |= SEC_COPY; break; case 'K': add_to_symop_list(ecp, optarg, NULL, SYMOP_KEEP); break; case 'L': add_to_symop_list(ecp, optarg, NULL, SYMOP_LOCALIZE); break; case 'N': add_to_symop_list(ecp, optarg, NULL, SYMOP_STRIP); break; case 'O': set_output_target(ecp, optarg); break; case 'p': ecp->flags |= PRESERVE_DATE; break; case 'V': print_version(); break; case 'w': ecp->flags |= WILDCARD; break; case 'W': add_to_symop_list(ecp, optarg, NULL, SYMOP_WEAKEN); break; case 'x': ecp->flags |= DISCARD_LOCAL; break; case 'X': ecp->flags |= DISCARD_LLABEL; break; case ECP_ADD_GNU_DEBUGLINK: ecp->debuglink = optarg; break; case ECP_ADD_SECTION: add_section(ecp, optarg); break; case ECP_CHANGE_ADDR: ecp->change_addr = (int64_t) strtoll(optarg, NULL, 0); break; case ECP_CHANGE_SEC_ADDR: parse_sec_address_op(ecp, opt, "--change-section-addr", optarg); break; case ECP_CHANGE_SEC_LMA: parse_sec_address_op(ecp, opt, "--change-section-lma", optarg); break; case ECP_CHANGE_SEC_VMA: parse_sec_address_op(ecp, opt, "--change-section-vma", optarg); break; case ECP_CHANGE_START: ecp->change_start = (int64_t) strtoll(optarg, NULL, 0); break; case ECP_CHANGE_WARN: /* default */ break; case ECP_GAP_FILL: ecp->fill = (uint8_t) strtoul(optarg, NULL, 0); ecp->flags |= GAP_FILL; break; case ECP_GLOBALIZE_SYMBOL: add_to_symop_list(ecp, optarg, NULL, SYMOP_GLOBALIZE); break; case ECP_GLOBALIZE_SYMBOLS: parse_symlist_file(ecp, optarg, SYMOP_GLOBALIZE); break; case ECP_KEEP_SYMBOLS: parse_symlist_file(ecp, optarg, SYMOP_KEEP); break; case ECP_KEEP_GLOBAL_SYMBOLS: parse_symlist_file(ecp, optarg, SYMOP_KEEPG); break; case ECP_LOCALIZE_HIDDEN: ecp->flags |= LOCALIZE_HIDDEN; break; case ECP_LOCALIZE_SYMBOLS: parse_symlist_file(ecp, optarg, SYMOP_LOCALIZE); break; case ECP_NO_CHANGE_WARN: ecp->flags |= NO_CHANGE_WARN; break; case ECP_ONLY_DEBUG: ecp->strip = STRIP_NONDEBUG; break; case ECP_ONLY_DWO: ecp->strip = STRIP_NONDWO; break; case ECP_PAD_TO: ecp->pad_to = (uint64_t) strtoull(optarg, NULL, 0); break; case ECP_PREFIX_ALLOC: ecp->prefix_alloc = optarg; break; case ECP_PREFIX_SEC: ecp->prefix_sec = optarg; break; case ECP_PREFIX_SYM: ecp->prefix_sym = optarg; break; case ECP_REDEF_SYMBOL: if ((s = strchr(optarg, '=')) == NULL) errx(EXIT_FAILURE, "illegal format for --redefine-sym"); *s++ = '\0'; add_to_symop_list(ecp, optarg, s, SYMOP_REDEF); break; case ECP_REDEF_SYMBOLS: parse_symlist_file(ecp, optarg, SYMOP_REDEF); break; case ECP_RENAME_SECTION: if ((fn = strchr(optarg, '=')) == NULL) errx(EXIT_FAILURE, "illegal format for --rename-section"); *fn++ = '\0'; /* Check for optional flags. */ if ((s = strchr(fn, ',')) != NULL) *s++ = '\0'; sac = lookup_sec_act(ecp, optarg, 1); sac->rename = 1; sac->newname = fn; if (s != NULL) parse_sec_flags(sac, s); break; case ECP_SET_OSABI: set_osabi(ecp, optarg); break; case ECP_SET_SEC_FLAGS: if ((s = strchr(optarg, '=')) == NULL) errx(EXIT_FAILURE, "illegal format for --set-section-flags"); *s++ = '\0'; sac = lookup_sec_act(ecp, optarg, 1); parse_sec_flags(sac, s); break; case ECP_SET_START: ecp->flags |= SET_START; ecp->set_start = (uint64_t) strtoull(optarg, NULL, 0); break; case ECP_SREC_FORCE_S3: ecp->flags |= SREC_FORCE_S3; break; case ECP_SREC_LEN: ecp->flags |= SREC_FORCE_LEN; ecp->srec_len = strtoul(optarg, NULL, 0); break; case ECP_STRIP_DWO: ecp->strip = STRIP_DWO; break; case ECP_STRIP_SYMBOLS: parse_symlist_file(ecp, optarg, SYMOP_STRIP); break; case ECP_STRIP_UNNEEDED: ecp->strip = STRIP_UNNEEDED; break; case ECP_WEAKEN_ALL: ecp->flags |= WEAKEN_ALL; break; case ECP_WEAKEN_SYMBOLS: parse_symlist_file(ecp, optarg, SYMOP_WEAKEN); break; default: elfcopy_usage(); } } argc -= optind; argv += optind; if (argc == 0 || argc > 2) elfcopy_usage(); infile = argv[0]; outfile = NULL; if (argc > 1) outfile = argv[1]; create_file(ecp, infile, outfile); } static void mcs_main(struct elfcopy *ecp, int argc, char **argv) { struct sec_action *sac; const char *string; int append, delete, compress, name, print; int opt, i; append = delete = compress = name = print = 0; string = NULL; while ((opt = getopt_long(argc, argv, "a:cdhn:pV", mcs_longopts, NULL)) != -1) { switch(opt) { case 'a': append = 1; string = optarg; /* XXX multiple -a not supported */ break; case 'c': compress = 1; break; case 'd': delete = 1; break; case 'n': name = 1; (void)lookup_sec_act(ecp, optarg, 1); break; case 'p': print = 1; break; case 'V': print_version(); break; case 'h': default: mcs_usage(); } } argc -= optind; argv += optind; if (argc == 0) mcs_usage(); /* Must specify one operation at least. */ if (!append && !compress && !delete && !print) mcs_usage(); /* * If we are going to delete, ignore other operations. This is * different from the Solaris implementation, which can print * and delete a section at the same time, for example. Also, this * implementation do not respect the order between operations that * user specified, i.e., "mcs -pc a.out" equals to "mcs -cp a.out". */ if (delete) { append = compress = print = 0; ecp->flags |= SEC_REMOVE; } if (append) ecp->flags |= SEC_APPEND; if (compress) ecp->flags |= SEC_COMPRESS; if (print) ecp->flags |= SEC_PRINT; /* .comment is the default section to operate on. */ if (!name) (void)lookup_sec_act(ecp, ".comment", 1); STAILQ_FOREACH(sac, &ecp->v_sac, sac_list) { sac->append = append; sac->compress = compress; sac->print = print; sac->remove = delete; sac->string = string; } for (i = 0; i < argc; i++) { /* If only -p is specified, output to /dev/null */ if (print && !append && !compress && !delete) create_file(ecp, argv[i], "/dev/null"); else create_file(ecp, argv[i], NULL); } } static void strip_main(struct elfcopy *ecp, int argc, char **argv) { struct sec_action *sac; const char *outfile; int opt; int i; outfile = NULL; while ((opt = getopt_long(argc, argv, "hI:K:N:o:O:pR:sSdgVxXw", strip_longopts, NULL)) != -1) { switch(opt) { case 'R': sac = lookup_sec_act(ecp, optarg, 1); sac->remove = 1; ecp->flags |= SEC_REMOVE; break; case 's': ecp->strip = STRIP_ALL; break; case 'S': case 'g': case 'd': ecp->strip = STRIP_DEBUG; break; case 'I': /* ignored */ break; case 'K': add_to_symop_list(ecp, optarg, NULL, SYMOP_KEEP); break; case 'N': add_to_symop_list(ecp, optarg, NULL, SYMOP_STRIP); break; case 'o': outfile = optarg; break; case 'O': set_output_target(ecp, optarg); break; case 'p': ecp->flags |= PRESERVE_DATE; break; case 'V': print_version(); break; case 'w': ecp->flags |= WILDCARD; break; case 'x': ecp->flags |= DISCARD_LOCAL; break; case 'X': ecp->flags |= DISCARD_LLABEL; break; case ECP_ONLY_DEBUG: ecp->strip = STRIP_NONDEBUG; break; case ECP_STRIP_UNNEEDED: ecp->strip = STRIP_UNNEEDED; break; case 'h': default: strip_usage(); } } argc -= optind; argv += optind; if (ecp->strip == 0 && ((ecp->flags & DISCARD_LOCAL) == 0) && ((ecp->flags & DISCARD_LLABEL) == 0) && lookup_symop_list(ecp, NULL, SYMOP_STRIP) == NULL) ecp->strip = STRIP_ALL; if (argc == 0) strip_usage(); /* * Only accept a single input file if an output file had been * specified. */ if (outfile != NULL && argc != 1) strip_usage(); for (i = 0; i < argc; i++) create_file(ecp, argv[i], outfile); } static void parse_sec_flags(struct sec_action *sac, char *s) { const char *flag; int found, i; for (flag = strtok(s, ","); flag; flag = strtok(NULL, ",")) { found = 0; for (i = 0; sec_flags[i].name != NULL; i++) if (strcasecmp(sec_flags[i].name, flag) == 0) { sac->flags |= sec_flags[i].value; found = 1; break; } if (!found) errx(EXIT_FAILURE, "unrecognized section flag %s", flag); } } static void parse_sec_address_op(struct elfcopy *ecp, int optnum, const char *optname, char *s) { struct sec_action *sac; const char *name; char *v; char op; name = v = s; do { v++; } while (*v != '\0' && *v != '=' && *v != '+' && *v != '-'); if (*v == '\0' || *(v + 1) == '\0') errx(EXIT_FAILURE, "invalid format for %s", optname); op = *v; *v++ = '\0'; sac = lookup_sec_act(ecp, name, 1); switch (op) { case '=': if (optnum == ECP_CHANGE_SEC_LMA || optnum == ECP_CHANGE_SEC_ADDR) { sac->setlma = 1; sac->lma = (uint64_t) strtoull(v, NULL, 0); } if (optnum == ECP_CHANGE_SEC_VMA || optnum == ECP_CHANGE_SEC_ADDR) { sac->setvma = 1; sac->vma = (uint64_t) strtoull(v, NULL, 0); } break; case '+': if (optnum == ECP_CHANGE_SEC_LMA || optnum == ECP_CHANGE_SEC_ADDR) sac->lma_adjust = (int64_t) strtoll(v, NULL, 0); if (optnum == ECP_CHANGE_SEC_VMA || optnum == ECP_CHANGE_SEC_ADDR) sac->vma_adjust = (int64_t) strtoll(v, NULL, 0); break; case '-': if (optnum == ECP_CHANGE_SEC_LMA || optnum == ECP_CHANGE_SEC_ADDR) sac->lma_adjust = (int64_t) -strtoll(v, NULL, 0); if (optnum == ECP_CHANGE_SEC_VMA || optnum == ECP_CHANGE_SEC_ADDR) sac->vma_adjust = (int64_t) -strtoll(v, NULL, 0); break; default: break; } } static void parse_symlist_file(struct elfcopy *ecp, const char *fn, unsigned int op) { struct symfile *sf; struct stat sb; FILE *fp; char *data, *p, *line, *end, *e, *n; if (stat(fn, &sb) == -1) err(EXIT_FAILURE, "stat %s failed", fn); /* Check if we already read and processed this file. */ STAILQ_FOREACH(sf, &ecp->v_symfile, symfile_list) { if (sf->dev == sb.st_dev && sf->ino == sb.st_ino) goto process_symfile; } if ((fp = fopen(fn, "r")) == NULL) err(EXIT_FAILURE, "can not open %s", fn); if ((data = malloc(sb.st_size + 1)) == NULL) err(EXIT_FAILURE, "malloc failed"); if (sb.st_size > 0) if (fread(data, sb.st_size, 1, fp) != 1) err(EXIT_FAILURE, "fread failed"); fclose(fp); data[sb.st_size] = '\0'; if ((sf = calloc(1, sizeof(*sf))) == NULL) err(EXIT_FAILURE, "malloc failed"); sf->dev = sb.st_dev; sf->ino = sb.st_ino; sf->size = sb.st_size + 1; sf->data = data; process_symfile: /* * Basically what we do here is to convert EOL to '\0', and remove * leading and trailing whitespaces for each line. */ end = sf->data + sf->size; line = NULL; for(p = sf->data; p < end; p++) { if ((*p == '\t' || *p == ' ') && line == NULL) continue; if (*p == '\r' || *p == '\n' || *p == '\0') { *p = '\0'; if (line == NULL) continue; /* Skip comment. */ if (*line == '#') { line = NULL; continue; } e = p - 1; while(e != line && (*e == '\t' || *e == ' ')) *e-- = '\0'; if (op != SYMOP_REDEF) add_to_symop_list(ecp, line, NULL, op); else { if (strlen(line) < 3) errx(EXIT_FAILURE, "illegal format for" " --redefine-sym"); for(n = line + 1; n < e; n++) { if (*n == ' ' || *n == '\t') { while(*n == ' ' || *n == '\t') *n++ = '\0'; break; } } if (n >= e) errx(EXIT_FAILURE, "illegal format for" " --redefine-sym"); add_to_symop_list(ecp, line, n, op); } line = NULL; continue; } if (line == NULL) line = p; } } static void set_input_target(struct elfcopy *ecp, const char *target_name) { Elftc_Bfd_Target *tgt; if ((tgt = elftc_bfd_find_target(target_name)) == NULL) errx(EXIT_FAILURE, "%s: invalid target name", target_name); ecp->itf = elftc_bfd_target_flavor(tgt); } static void set_output_target(struct elfcopy *ecp, const char *target_name) { Elftc_Bfd_Target *tgt; if ((tgt = elftc_bfd_find_target(target_name)) == NULL) errx(EXIT_FAILURE, "%s: invalid target name", target_name); ecp->otf = elftc_bfd_target_flavor(tgt); if (ecp->otf == ETF_ELF) { ecp->oec = elftc_bfd_target_class(tgt); ecp->oed = elftc_bfd_target_byteorder(tgt); ecp->oem = elftc_bfd_target_machine(tgt); ecp->abi = elftc_bfd_target_osabi(tgt); } if (ecp->otf == ETF_EFI || ecp->otf == ETF_PE) ecp->oem = elftc_bfd_target_machine(tgt); ecp->otgt = target_name; } static void set_osabi(struct elfcopy *ecp, const char *abi) { int i, found; found = 0; for (i = 0; osabis[i].name != NULL; i++) if (strcasecmp(osabis[i].name, abi) == 0) { ecp->abi = osabis[i].abi; found = 1; break; } if (!found) errx(EXIT_FAILURE, "unrecognized OSABI %s", abi); } #define ELFCOPY_USAGE_MESSAGE "\ Usage: %s [options] infile [outfile]\n\ Transform object files.\n\n\ Options:\n\ -d | -g | --strip-debug Remove debugging information from the output.\n\ -j SECTION | --only-section=SECTION\n\ Copy only the named section to the output.\n\ -p | --preserve-dates Preserve access and modification times.\n\ -w | --wildcard Use shell-style patterns to name symbols.\n\ -x | --discard-all Do not copy non-globals to the output.\n\ -I FORMAT | --input-target=FORMAT\n\ Specify object format for the input file.\n\ -K SYM | --keep-symbol=SYM Copy symbol SYM to the output.\n\ -L SYM | --localize-symbol=SYM\n\ Make symbol SYM local to the output file.\n\ -N SYM | --strip-symbol=SYM Do not copy symbol SYM to the output.\n\ -O FORMAT | --output-target=FORMAT\n\ Specify object format for the output file.\n\ FORMAT should be a target name understood by\n\ elftc_bfd_find_target(3).\n\ -R NAME | --remove-section=NAME\n\ Remove the named section.\n\ -S | --strip-all Remove all symbol and relocation information\n\ from the output.\n\ -V | --version Print a version identifier and exit.\n\ -W SYM | --weaken-symbol=SYM Mark symbol SYM as weak in the output.\n\ -X | --discard-locals Do not copy compiler generated symbols to\n\ the output.\n\ --add-section NAME=FILE Add the contents of FILE to the ELF object as\n\ a new section named NAME.\n\ --adjust-section-vma SECTION{=,+,-}VAL | \\\n\ --change-section-address SECTION{=,+,-}VAL\n\ Set or adjust the VMA and the LMA of the\n\ named section by VAL.\n\ --adjust-start=INCR | --change-start=INCR\n\ Add INCR to the start address for the ELF\n\ object.\n\ --adjust-vma=INCR | --change-addresses=INCR\n\ Increase the VMA and LMA of all sections by\n\ INCR.\n\ --adjust-warning | --change-warnings\n\ Issue warnings for non-existent sections.\n\ --change-section-lma SECTION{=,+,-}VAL\n\ Set or adjust the LMA address of the named\n\ section by VAL.\n\ --change-section-vma SECTION{=,+,-}VAL\n\ Set or adjust the VMA address of the named\n\ section by VAL.\n\ --gap-fill=VAL Fill the gaps between sections with bytes\n\ of value VAL.\n\ --localize-hidden Make all hidden symbols local to the output\n\ file.\n\ --no-adjust-warning| --no-change-warnings\n\ Do not issue warnings for non-existent\n\ sections.\n\ --only-keep-debug Copy only debugging information.\n\ --output-target=FORMAT Use the specified format for the output.\n\ --pad-to=ADDRESS Pad the output object up to the given address.\n\ --prefix-alloc-sections=STRING\n\ Prefix the section names of all the allocated\n\ sections with STRING.\n\ --prefix-sections=STRING Prefix the section names of all the sections\n\ with STRING.\n\ --prefix-symbols=STRING Prefix the symbol names of all the symbols\n\ with STRING.\n\ --rename-section OLDNAME=NEWNAME[,FLAGS]\n\ Rename and optionally change section flags.\n\ --set-section-flags SECTION=FLAGS\n\ Set section flags for the named section.\n\ Supported flags are: 'alloc', 'code',\n\ 'contents', 'data', 'debug', 'load',\n\ 'noload', 'readonly', 'rom', and 'shared'.\n\ --set-start=ADDRESS Set the start address of the ELF object.\n\ --srec-forceS3 Only generate S3 S-Records.\n\ --srec-len=LEN Set the maximum length of a S-Record line.\n\ --strip-unneeded Do not copy relocation information.\n" static void elfcopy_usage(void) { (void) fprintf(stderr, ELFCOPY_USAGE_MESSAGE, ELFTC_GETPROGNAME()); exit(EXIT_FAILURE); } #define MCS_USAGE_MESSAGE "\ Usage: %s [options] file...\n\ Manipulate the comment section in an ELF object.\n\n\ Options:\n\ -a STRING Append 'STRING' to the comment section.\n\ -c Remove duplicate entries from the comment section.\n\ -d Delete the comment section.\n\ -h | --help Print a help message and exit.\n\ -n NAME Operate on the ELF section with name 'NAME'.\n\ -p Print the contents of the comment section.\n\ -V | --version Print a version identifier and exit.\n" static void mcs_usage(void) { (void) fprintf(stderr, MCS_USAGE_MESSAGE, ELFTC_GETPROGNAME()); exit(EXIT_FAILURE); } #define STRIP_USAGE_MESSAGE "\ Usage: %s [options] file...\n\ Discard information from ELF objects.\n\n\ Options:\n\ -d | -g | -S | --strip-debug Remove debugging symbols.\n\ -h | --help Print a help message.\n\ -o FILE | --output-file FILE Write output to FILE.\n\ --only-keep-debug Keep debugging information only.\n\ -p | --preserve-dates Preserve access and modification times.\n\ -s | --strip-all Remove all symbols.\n\ --strip-unneeded Remove symbols not needed for relocation\n\ processing.\n\ -w | --wildcard Use shell-style patterns to name symbols.\n\ -x | --discard-all Discard all non-global symbols.\n\ -I TGT| --input-target=TGT (Accepted, but ignored).\n\ -K SYM | --keep-symbol=SYM Keep symbol 'SYM' in the output.\n\ -N SYM | --strip-symbol=SYM Remove symbol 'SYM' from the output.\n\ -O TGT | --output-target=TGT Set the output file format to 'TGT'.\n\ -R SEC | --remove-section=SEC Remove the section named 'SEC'.\n\ -V | --version Print a version identifier and exit.\n\ -X | --discard-locals Remove compiler-generated local symbols.\n" static void strip_usage(void) { (void) fprintf(stderr, STRIP_USAGE_MESSAGE, ELFTC_GETPROGNAME()); exit(EXIT_FAILURE); } static void print_version(void) { (void) printf("%s (%s)\n", ELFTC_GETPROGNAME(), elftc_version()); exit(EXIT_SUCCESS); } /* * Compare the ending of s with end. */ static int strrcmp(const char *s, const char *end) { size_t endlen, slen; slen = strlen(s); endlen = strlen(end); if (slen >= endlen) s += slen - endlen; return (strcmp(s, end)); } int main(int argc, char **argv) { struct elfcopy *ecp; if (elf_version(EV_CURRENT) == EV_NONE) errx(EXIT_FAILURE, "ELF library initialization failed: %s", elf_errmsg(-1)); ecp = calloc(1, sizeof(*ecp)); if (ecp == NULL) err(EXIT_FAILURE, "calloc failed"); ecp->itf = ecp->otf = ETF_ELF; ecp->iec = ecp->oec = ELFCLASSNONE; ecp->oed = ELFDATANONE; ecp->abi = -1; /* There is always an empty section. */ ecp->nos = 1; ecp->fill = 0; STAILQ_INIT(&ecp->v_seg); STAILQ_INIT(&ecp->v_sac); STAILQ_INIT(&ecp->v_sadd); STAILQ_INIT(&ecp->v_symop); STAILQ_INIT(&ecp->v_symfile); STAILQ_INIT(&ecp->v_arobj); TAILQ_INIT(&ecp->v_sec); if ((ecp->progname = ELFTC_GETPROGNAME()) == NULL) ecp->progname = "elfcopy"; if (strrcmp(ecp->progname, "strip") == 0) strip_main(ecp, argc, argv); else if (strrcmp(ecp->progname, "mcs") == 0) mcs_main(ecp, argc, argv); else { if (strrcmp(ecp->progname, "elfcopy") != 0 && strrcmp(ecp->progname, "objcopy") != 0) warnx("program mode not known, defaulting to elfcopy"); elfcopy_main(ecp, argc, argv); } free_sec_add(ecp); free_sec_act(ecp); free(ecp); exit(EXIT_SUCCESS); }