i386 removal, part 25/x: Remove i386 parts from kgdb, libkvm, crunchide. master
authorSascha Wildner <saw@online.de>
Wed, 29 Jul 2015 17:06:58 +0000 (19:06 +0200)
committerSascha Wildner <saw@online.de>
Wed, 29 Jul 2015 17:06:58 +0000 (19:06 +0200)
gnu/usr.bin/gdb/kgdb/trgt_i386.c [deleted file]
lib/libkvm/kvm_i386.c [deleted file]
lib/libkvm/kvm_minidump_i386.c [deleted file]
usr.bin/crunch/crunchide/Makefile
usr.bin/crunch/crunchide/exec_elf32.c [deleted file]
usr.bin/crunch/crunchide/exec_elf64.c

diff --git a/gnu/usr.bin/gdb/kgdb/trgt_i386.c b/gnu/usr.bin/gdb/kgdb/trgt_i386.c
deleted file mode 100644 (file)
index 2034e7b..0000000
+++ /dev/null
@@ -1,425 +0,0 @@
-/*
- * Copyright (c) 2004 Marcel Moolenaar
- * 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 AUTHORS ``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 AUTHORS 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.
- *
- * $FreeBSD: src/gnu/usr.bin/gdb/kgdb/trgt_i386.c,v 1.13 2008/09/27 15:58:37 kib Exp $
- */
-
-#include <sys/cdefs.h>
-
-#include <sys/types.h>
-#include <machine/thread.h>
-#include <sys/thread.h>
-#include <machine/globaldata.h>
-#include <machine/pcb.h>
-#include <machine/frame.h>
-#include <machine/segments.h>
-#include <machine/tss.h>
-#include <err.h>
-#include <kvm.h>
-#include <string.h>
-
-#include <defs.h>
-#include <target.h>
-#include <gdbthread.h>
-#include <inferior.h>
-#include <regcache.h>
-#include <frame-unwind.h>
-#include <i386-tdep.h>
-
-#include "kgdb.h"
-
-static int
-kgdb_trgt_trapframe_sniffer(const struct frame_unwind *self,
-                           struct frame_info *next_frame,
-                           void **this_prologue_cache);
-
-void
-kgdb_trgt_fetch_registers(struct target_ops *target_ops, struct regcache *regcache, int regno)
-{
-       struct kthr *kt;
-       struct pcb pcb;
-
-       kt = kgdb_thr_lookup_tid(ptid_get_tid(inferior_ptid));
-       if (kt == NULL) {
-               regcache_raw_supply(regcache, regno, NULL);
-               return;
-       }
-
-       /*
-        * kt->pcb == 0 is a marker for "non-dumping kernel thread".
-        */
-       if (kt->pcb == 0) {
-               uintptr_t regs[5];
-               uintptr_t addr;
-               uintptr_t sp;
-
-               addr = kt->kaddr + offsetof(struct thread, td_sp);
-               kvm_read(kvm, addr, &sp, sizeof(sp));
-               /*
-                * Stack is:
-                * -2 ret
-                * -1 popfl
-                * 0 popl %edi
-                * 1 popl %esi
-                * 2 popl %ebx
-                * 3 popl %ebp
-                * 4 ret
-                */
-               if (kvm_read(kvm, sp + 2 * sizeof(regs[0]), regs, sizeof(regs)) != sizeof(regs)) {
-                       warnx("kvm_read: %s", kvm_geterr(kvm));
-                       memset(regs, 0, sizeof(regs));
-               }
-               regcache_raw_supply(regcache, I386_EDI_REGNUM, &regs[0]);
-               regcache_raw_supply(regcache, I386_ESI_REGNUM, &regs[1]);
-               regcache_raw_supply(regcache, I386_EBX_REGNUM, &regs[2]);
-               regcache_raw_supply(regcache, I386_EBP_REGNUM, &regs[3]);
-               regcache_raw_supply(regcache, I386_EIP_REGNUM, &regs[4]);
-               sp += 7 * sizeof(regs[0]);
-               regcache_raw_supply(regcache, I386_ESP_REGNUM, &sp);
-               return;
-       }
-
-       if (kvm_read(kvm, kt->pcb, &pcb, sizeof(pcb)) != sizeof(pcb)) {
-               warnx("kvm_read: %s", kvm_geterr(kvm));
-               memset(&pcb, 0, sizeof(pcb));
-       }
-       regcache_raw_supply(regcache, I386_EBX_REGNUM, (char *)&pcb.pcb_ebx);
-       regcache_raw_supply(regcache, I386_ESP_REGNUM, (char *)&pcb.pcb_esp);
-       regcache_raw_supply(regcache, I386_EBP_REGNUM, (char *)&pcb.pcb_ebp);
-       regcache_raw_supply(regcache, I386_ESI_REGNUM, (char *)&pcb.pcb_esi);
-       regcache_raw_supply(regcache, I386_EDI_REGNUM, (char *)&pcb.pcb_edi);
-       regcache_raw_supply(regcache, I386_EIP_REGNUM, (char *)&pcb.pcb_eip);
-}
-
-struct kgdb_tss_cache {
-       CORE_ADDR       pc;
-       CORE_ADDR       sp;
-       CORE_ADDR       tss;
-};
-
-static int kgdb_trgt_tss_offset[15] = {
-       offsetof(struct i386tss, tss_eax),
-       offsetof(struct i386tss, tss_ecx),
-       offsetof(struct i386tss, tss_edx),
-       offsetof(struct i386tss, tss_ebx),
-       offsetof(struct i386tss, tss_esp),
-       offsetof(struct i386tss, tss_ebp),
-       offsetof(struct i386tss, tss_esi),
-       offsetof(struct i386tss, tss_edi),
-       offsetof(struct i386tss, tss_eip),
-       offsetof(struct i386tss, tss_eflags),
-       offsetof(struct i386tss, tss_cs),
-       offsetof(struct i386tss, tss_ss),
-       offsetof(struct i386tss, tss_ds),
-       offsetof(struct i386tss, tss_es),
-       offsetof(struct i386tss, tss_fs)
-};
-
-/*
- * If the current thread is executing on a CPU, fetch the common_tss
- * for that CPU.
- *
- * This is painful because 'struct pcpu' is variant sized, so we can't
- * use it.  Instead, we lookup the GDT selector for this CPU and
- * extract the base of the TSS from there.
- */
-static CORE_ADDR
-kgdb_trgt_fetch_tss(void)
-{
-       struct kthr *kt;
-       struct segment_descriptor sd;
-       uintptr_t addr, tss;
-
-       kt = kgdb_thr_lookup_tid(ptid_get_tid(inferior_ptid));
-       if (kt == NULL || kt->gd == 0)
-               return (0);
-
-       addr = kt->gd + offsetof(struct mdglobaldata, gd_common_tssd);
-       if (kvm_read(kvm, addr, &sd, sizeof(sd)) != sizeof(sd)) {
-               warnx("kvm_read: %s", kvm_geterr(kvm));
-               return (0);
-       }
-       if (sd.sd_type != SDT_SYS386BSY) {
-               warnx("descriptor is not a busy TSS");
-               return (0);
-       }
-       tss = kt->gd + offsetof(struct mdglobaldata, gd_common_tss);
-
-       return ((CORE_ADDR)tss);
-}
-
-static struct kgdb_tss_cache *
-kgdb_trgt_tss_cache(struct frame_info *next_frame, void **this_cache)
-{
-       struct gdbarch *gdbarch = get_frame_arch(next_frame);
-       enum bfd_endian byte_order = gdbarch_byte_order(gdbarch);
-       char buf[MAX_REGISTER_SIZE];
-       struct kgdb_tss_cache *cache;
-
-       cache = *this_cache;
-       if (cache == NULL) {
-               cache = FRAME_OBSTACK_ZALLOC(struct kgdb_tss_cache);
-               *this_cache = cache;
-               cache->pc = get_frame_address_in_block(next_frame);
-               frame_unwind_register(next_frame, I386_ESP_REGNUM, buf);
-               cache->sp = extract_unsigned_integer(buf,
-                   register_size(gdbarch, I386_ESP_REGNUM),
-                   byte_order);
-               cache->tss = kgdb_trgt_fetch_tss();
-       }
-       return (cache);
-}
-
-static void
-kgdb_trgt_dblfault_this_id(struct frame_info *next_frame, void **this_cache,
-    struct frame_id *this_id)
-{
-       struct kgdb_tss_cache *cache;
-
-       cache = kgdb_trgt_tss_cache(next_frame, this_cache);
-       *this_id = frame_id_build(cache->sp, cache->pc);
-}
-
-static struct value *
-kgdb_trgt_dblfault_prev_register(struct frame_info *next_frame,
-    void **this_cache, int regnum)
-{
-       CORE_ADDR addrp;
-       struct kgdb_tss_cache *cache;
-       int ofs;
-
-       if (regnum < I386_EAX_REGNUM || regnum > I386_FS_REGNUM)
-               return frame_unwind_got_register(next_frame, regnum, regnum);
-
-       ofs = kgdb_trgt_tss_offset[regnum];
-
-       cache = kgdb_trgt_tss_cache(next_frame, this_cache);
-       if (cache->tss == 0)
-               return frame_unwind_got_register(next_frame, regnum, regnum);
-
-       addrp = cache->tss + ofs;
-       return frame_unwind_got_memory(next_frame, regnum, addrp);
-}
-
-static enum unwind_stop_reason
-kgdb_trgt_dblfault_unwind_reason(struct frame_info *next_frame,
-    void **this_cache)
-{
-    /* XXX marino : populate logic to determine unwind stoppage */
-    return UNWIND_NO_REASON;
-}
-
-static const struct frame_unwind kgdb_trgt_dblfault_unwind = {
-        NORMAL_FRAME,
-        &kgdb_trgt_dblfault_unwind_reason,
-        &kgdb_trgt_dblfault_this_id,
-        &kgdb_trgt_dblfault_prev_register,
-       .sniffer = kgdb_trgt_trapframe_sniffer
-};
-
-struct kgdb_frame_cache {
-       int             frame_type;
-       CORE_ADDR       pc;
-       CORE_ADDR       sp;
-};
-#define        FT_NORMAL               1
-#define        FT_INTRFRAME            2
-/*#define      FT_INTRTRAPFRAME        3*/
-#define        FT_TIMERFRAME           4
-#define        FT_CALLTRAP             5
-
-static int kgdb_trgt_frame_offset[15] = {
-       offsetof(struct trapframe, tf_eax),
-       offsetof(struct trapframe, tf_ecx),
-       offsetof(struct trapframe, tf_edx),
-       offsetof(struct trapframe, tf_ebx),
-       offsetof(struct trapframe, tf_esp),
-       offsetof(struct trapframe, tf_ebp),
-       offsetof(struct trapframe, tf_esi),
-       offsetof(struct trapframe, tf_edi),
-       offsetof(struct trapframe, tf_eip),
-       offsetof(struct trapframe, tf_eflags),
-       offsetof(struct trapframe, tf_cs),
-       offsetof(struct trapframe, tf_ss),
-       offsetof(struct trapframe, tf_ds),
-       offsetof(struct trapframe, tf_es),
-       offsetof(struct trapframe, tf_fs)
-};
-
-static struct kgdb_frame_cache *
-kgdb_trgt_frame_cache(struct frame_info *next_frame, void **this_cache)
-{
-       struct kgdb_frame_cache *cache;
-       const char *pname;
-
-       cache = *this_cache;
-       if (cache == NULL) {
-               cache = FRAME_OBSTACK_ZALLOC(struct kgdb_frame_cache);
-               *this_cache = cache;
-               cache->pc = get_frame_address_in_block(next_frame);
-               cache->sp = get_frame_sp(next_frame);
-               find_pc_partial_function(cache->pc, &pname, NULL, NULL);
-
-               if (strcmp(pname, "calltrap") == 0)
-                       cache->frame_type = FT_CALLTRAP;
-               else if (pname[0] != 'X')
-                       cache->frame_type = FT_NORMAL;
-               else if (strcmp(pname, "Xtimerint") == 0)
-                       cache->frame_type = FT_TIMERFRAME;
-               /* else if (strcmp(pname, "Xcpustop") == 0 ||
-                   strcmp(pname, "Xrendezvous") == 0 ||
-                   strcmp(pname, "Xipi_intr_bitmap_handler") == 0 ||
-                   strcmp(pname, "Xlazypmap") == 0)
-                       cache->frame_type = FT_INTRTRAPFRAME;
-                       */
-               else
-                       cache->frame_type = FT_INTRFRAME;
-       }
-       return (cache);
-}
-
-static void
-kgdb_trgt_trapframe_this_id(struct frame_info *next_frame, void **this_cache,
-    struct frame_id *this_id)
-{
-       struct kgdb_frame_cache *cache;
-
-       cache = kgdb_trgt_frame_cache(next_frame, this_cache);
-       *this_id = frame_id_build(cache->sp, cache->pc);
-}
-
-static struct value *
-kgdb_trgt_trapframe_prev_register(struct frame_info *next_frame,
-    void **this_cache, int regnum)
-{
-       CORE_ADDR addrp;
-       struct kgdb_frame_cache *cache;
-       int ofs;
-
-       if (regnum < I386_EAX_REGNUM || regnum > I386_FS_REGNUM)
-               return frame_unwind_got_register(next_frame, regnum, regnum);
-
-       ofs = kgdb_trgt_frame_offset[regnum] + 4;
-
-       cache = kgdb_trgt_frame_cache(next_frame, this_cache);
-
-       switch (cache->frame_type) {
-       case FT_NORMAL:
-               break;
-       case FT_INTRFRAME:
-               ofs += 4;
-               break;
-       case FT_TIMERFRAME:
-               break;
-               /*
-       case FT_INTRTRAPFRAME:
-               ofs -= ofs_fix;
-               break;
-               */
-       case FT_CALLTRAP:
-               ofs += 0;
-               break;
-       default:
-               fprintf_unfiltered(gdb_stderr, "Correct FT_XXX frame offsets "
-                  "for %d\n", cache->frame_type);
-               break;
-       }
-
-       addrp = cache->sp + ofs;
-
-#if 0
-       /*
-        * If we are in the kernel, we don't have esp stored in the
-        * trapframe, but we can calculate it simply by subtracting
-        * the size of the frame.
-        */
-       if (regnum == I386_ESP_REGNUM) {
-               char buf[4];
-
-               frame_unwind_register(next_frame, I386_CS_REGNUM, buf);
-               if (extract_unsigned_integer(buf, 4, byte_order) != SEL_UPL)
-                       return frame_unwind_got_address(next_frame, regnum, addrp);
-               /* else FALLTHROUGH */
-       }
-#endif
-
-       return frame_unwind_got_memory(next_frame, regnum, addrp);
-}
-
-static enum unwind_stop_reason
-kgdb_trgt_trapframe_unwind_reason(struct frame_info *next_frame,
-    void **this_cache)
-{
-    /* XXX marino : populate logic to determine unwind stoppage */
-    return UNWIND_NO_REASON;
-}
-
-const struct frame_unwind kgdb_trgt_trapframe_unwind = {
-        NORMAL_FRAME,
-        &kgdb_trgt_trapframe_unwind_reason,
-        &kgdb_trgt_trapframe_this_id,
-        &kgdb_trgt_trapframe_prev_register,
-       .sniffer = kgdb_trgt_trapframe_sniffer
-};
-
-static int
-kgdb_trgt_trapframe_sniffer(const struct frame_unwind *self,
-                           struct frame_info *next_frame,
-                           void **this_prologue_cache)
-{
-       const char *pname;
-       CORE_ADDR pc;
-
-       pc = get_frame_address_in_block(next_frame);
-       pname = NULL;
-       find_pc_partial_function(pc, &pname, NULL, NULL);
-       if (pname == NULL)
-               return (0);
-
-       /*
-        * This is a combined sniffer, since only the
-        * function names change.
-        */
-
-       /*
-        * If we're the sniffer for a trapframe, deal with
-        * all these function names.
-        */
-       if (self == &kgdb_trgt_trapframe_unwind &&
-           (strcmp(pname, "calltrap") == 0 ||
-            (pname[0] == 'X' && pname[1] != '_')))
-               return (1);
-
-       /*
-        * If we're a double fault sniffer, only look for
-        * the double fault name.
-        */
-       if(self == &kgdb_trgt_dblfault_unwind &&
-          strcmp(pname, "dblfault_handler") == 0)
-               return (1);
-
-       /* printf("%s: %llx =%s\n", __func__, pc, pname); */
-       return (0);
-}
diff --git a/lib/libkvm/kvm_i386.c b/lib/libkvm/kvm_i386.c
deleted file mode 100644 (file)
index d47707e..0000000
+++ /dev/null
@@ -1,339 +0,0 @@
-/*-
- * Copyright (c) 1989, 1992, 1993
- *     The Regents of the University of California.  All rights reserved.
- *
- * This code is derived from software developed by the Computer Systems
- * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
- * BG 91-66 and contributed to Berkeley.
- *
- * 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.
- * 3. Neither the name of the University nor the names of its contributors
- *    may be used to endorse or promote products derived from this software
- *    without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
- *
- * @(#)kvm_hp300.c     8.1 (Berkeley) 6/4/93
- * $FreeBSD: src/lib/libkvm/kvm_i386.c,v 1.11.2.1 2001/09/21 04:01:51 peter Exp $
- */
-
-/*
- * i386 machine dependent routines for kvm.  Hopefully, the forthcoming
- * vm code will one day obsolete this module.
- */
-
-#include <sys/user.h>  /* MUST BE FIRST */
-#include <sys/param.h>
-#include <sys/proc.h>
-#include <sys/stat.h>
-#include <sys/mman.h>
-#include <sys/elf_common.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-#include <nlist.h>
-#include <kvm.h>
-
-#include <vm/vm.h>
-#include <vm/vm_param.h>
-
-#include <machine/elf.h>
-
-#include <limits.h>
-
-#include "kvm_private.h"
-
-#ifndef btop
-#define        btop(x)         (i386_btop(x))
-#define        ptob(x)         (i386_ptob(x))
-#endif
-
-/* minidump must be the first item! */
-struct vmstate {
-       int             minidump;       /* 1 = minidump mode */
-       void            *mmapbase;
-       size_t          mmapsize;
-       void            *PTD;
-};
-
-/*
- * Map the ELF headers into the process' address space. We do this in two
- * steps: first the ELF header itself and using that information the whole
- * set of headers. (Taken from kvm_ia64.c)
- */
-static int
-_kvm_maphdrs(kvm_t *kd, size_t sz)
-{
-       struct vmstate *vm = kd->vmst;
-
-       if (kd->vmst->minidump) {
-               _kvm_minidump_freevtop(kd);
-               return (0);
-       }
-
-       /* munmap() previous mmap(). */
-       if (vm->mmapbase != NULL) {
-               munmap(vm->mmapbase, vm->mmapsize);
-               vm->mmapbase = NULL;
-       }
-
-       vm->mmapsize = sz;
-       vm->mmapbase = mmap(NULL, sz, PROT_READ, MAP_PRIVATE, kd->pmfd, 0);
-       if (vm->mmapbase == MAP_FAILED) {
-               _kvm_err(kd, kd->program, "cannot mmap corefile");
-               return (-1);
-       }
-       return (0);
-}
-
-/*
- * Translate a physical memory address to a file-offset in the crash-dump.
- * (Taken from kvm_ia64.c)
- */
-static size_t
-_kvm_pa2off(kvm_t *kd, uint64_t pa, off_t *ofs)
-{
-       Elf_Ehdr *e = kd->vmst->mmapbase;
-       Elf_Phdr *p;
-       int n;
-
-       if (kd->rawdump) {
-               *ofs = pa;
-               return (PAGE_SIZE - ((size_t)pa & PAGE_MASK));
-       }
-
-       p = (Elf_Phdr*)((char*)e + e->e_phoff);
-       n = e->e_phnum;
-
-       while (n && (pa < p->p_paddr || pa >= p->p_paddr + p->p_memsz))
-               p++, n--;
-       if (n == 0)
-               return (0);
-       *ofs = (pa - p->p_paddr) + p->p_offset;
-       return (PAGE_SIZE - ((size_t)pa & PAGE_MASK));
-}
-
-void
-_kvm_freevtop(kvm_t *kd)
-{
-       struct vmstate  *vm = kd->vmst;
-
-       if (kd->vmst->minidump) {
-               _kvm_minidump_freevtop(kd);
-               return;
-       }
-
-       if (vm->mmapbase != NULL)
-               munmap(vm->mmapbase, vm->mmapsize);
-       if (vm->PTD)
-               free(vm->PTD);
-       free(vm);
-       kd->vmst = NULL;
-}
-
-int
-_kvm_initvtop(kvm_t *kd)
-{
-       struct nlist nlist[2];
-       u_long pa;
-       u_long kernbase;
-       char            *PTD;
-       Elf_Ehdr        *ehdr;
-       size_t          hdrsz;
-       char            minihdr[8];
-       struct pcb      dumppcb;
-
-       if (pread(kd->pmfd, &minihdr, 8, 0) == 8)
-               if (memcmp(&minihdr, "minidump", 8) == 0)
-                       return (_kvm_minidump_initvtop(kd));
-
-       kd->vmst = (struct vmstate *)_kvm_malloc(kd, sizeof(*kd->vmst));
-       if (kd->vmst == 0) {
-               _kvm_err(kd, kd->program, "cannot allocate vm");
-               return (-1);
-       }
-       kd->vmst->PTD = 0;
-
-       if (_kvm_maphdrs(kd, sizeof(Elf_Ehdr)) == -1)
-               return (-1);
-       /*
-        * Check if this is indeed an ELF header. If not, assume old style dump or
-        * memory layout.
-        */
-       ehdr = kd->vmst->mmapbase;
-       if (!IS_ELF(*ehdr)) {
-               kd->rawdump = 1;
-               munmap(kd->vmst->mmapbase, kd->vmst->mmapsize);
-               kd->vmst->mmapbase = NULL;
-       } else {
-               hdrsz = ehdr->e_phoff + ehdr->e_phentsize * ehdr->e_phnum;
-               if (_kvm_maphdrs(kd, hdrsz) == -1)
-                       return (-1);
-       }
-
-       nlist[0].n_name = "_kernbase";
-       nlist[1].n_name = 0;
-
-       if (kvm_nlist(kd, nlist) != 0)
-               kernbase = KERNBASE;    /* for old kernels */
-       else
-               kernbase = nlist[0].n_value;
-
-       nlist[0].n_name = "_dumppcb";
-       nlist[1].n_name = 0;
-
-       if (kvm_nlist(kd, nlist) != 0) {
-               _kvm_err(kd, kd->program, "bad namelist");
-               return (-1);
-       }
-
-       if (kvm_read(kd, (nlist[0].n_value - kernbase), &dumppcb,
-                    sizeof(dumppcb)) != sizeof(dumppcb)) {
-               _kvm_err(kd, kd->program, "cannot read dumppcb");
-               return (-1);
-       }
-       pa = dumppcb.pcb_cr3 & PG_FRAME;
-
-       PTD = _kvm_malloc(kd, PAGE_SIZE);
-       if (kvm_read(kd, pa, PTD, PAGE_SIZE) != PAGE_SIZE) {
-               _kvm_err(kd, kd->program, "cannot read PTD");
-               return (-1);
-       }
-       kd->vmst->PTD = PTD;
-       return (0);
-}
-
-static int
-_kvm_vatop(kvm_t *kd, u_long va, off_t *pa)
-{
-       struct vmstate *vm;
-       u_long offset;
-       u_long pte_pa;
-       u_long pde_pa;
-       pd_entry_t pde;
-       pt_entry_t pte;
-       u_long pdeindex;
-       u_long pteindex;
-       size_t  s;
-       u_long  a;
-       off_t   ofs;
-       uint32_t *PTD;
-
-
-       vm = kd->vmst;
-       PTD = (uint32_t *)vm->PTD;
-       offset = va & (PAGE_SIZE - 1);
-
-       /*
-        * If we are initializing (kernel page table descriptor pointer
-        * not yet set) then return pa == va to avoid infinite recursion.
-        */
-       if (PTD == NULL) {
-               s = _kvm_pa2off(kd, va, pa);
-               if (s == 0) {
-                       _kvm_err(kd, kd->program,
-                           "_kvm_vatop: bootstrap data not in dump");
-                       goto invalid;
-               } else {
-                       return (PAGE_SIZE - offset);
-               }
-       }
-
-       pdeindex = va >> PDRSHIFT;
-       pde = PTD[pdeindex];
-
-       if (((u_long)pde & PG_V) == 0) {
-               _kvm_err(kd, kd->program, "_kvm_vatop: pde not valid");
-               goto invalid;
-       }
-
-       if ((u_long)pde & PG_PS) {
-             /*
-              * No second-level page table; ptd describes one 4MB page.
-              * (We assume that the kernel wouldn't set PG_PS without enabling
-              * it cr0, and that the kernel doesn't support 36-bit physical
-              * addresses).
-              */
-#define        PAGE4M_MASK     (NBPDR - 1)
-#define        PG_FRAME4M      (~PAGE4M_MASK)
-#if 0
-               *pa = ((u_long)pde & PG_FRAME4M) + (va & PAGE4M_MASK);
-#endif
-               pde_pa = ((u_long)pde & PG_FRAME4M) + (va & PAGE4M_MASK);
-               s = _kvm_pa2off(kd, pde_pa, &ofs);
-               if (s < sizeof pde) {
-                       _kvm_syserr(kd, kd->program,
-                           "_kvm_vatop: pde_pa not found");
-                       goto invalid;
-               }
-               *pa = ofs;
-
-               return (NBPDR - (va & PAGE4M_MASK));
-       }
-
-       pteindex = (va >> PAGE_SHIFT) & (NPTEPG-1);
-       pte_pa = ((u_long)pde & PG_FRAME) + (pteindex * sizeof(pde));
-
-       s = _kvm_pa2off(kd, pte_pa, &ofs);
-       if (s < sizeof pte) {
-               _kvm_err(kd, kd->program, "_kvm_vatop: pdpe_pa not found");
-               goto invalid;
-       }
-
-       /* XXX This has to be a physical address read, kvm_read is virtual */
-       if (lseek(kd->pmfd, ofs, 0) == -1) {
-               _kvm_syserr(kd, kd->program, "_kvm_vatop: lseek");
-               goto invalid;
-       }
-       if (read(kd->pmfd, &pte, sizeof pte) != sizeof pte) {
-               _kvm_syserr(kd, kd->program, "_kvm_vatop: read");
-               goto invalid;
-       }
-       if (((u_long)pte & PG_V) == 0) {
-               _kvm_err(kd, kd->program, "_kvm_kvatop: pte not valid");
-               goto invalid;
-       }
-
-       a = ((u_long)pte & PG_FRAME) + offset;
-       s =_kvm_pa2off(kd, a, pa);
-       if (s == 0) {
-               _kvm_err(kd, kd->program, "_kvm_vatop: address not in dump");
-               goto invalid;
-       } else
-               return (PAGE_SIZE - offset);
-
-invalid:
-       _kvm_err(kd, 0, "invalid address (0x%lx)", va);
-       return (0);
-}
-
-int
-_kvm_kvatop(kvm_t *kd, u_long va, off_t *pa)
-{
-       if (kd->vmst->minidump)
-               return (_kvm_minidump_kvatop(kd, va, pa));
-
-       if (kvm_ishost(kd)) {
-               _kvm_err(kd, 0, "vatop called in live kernel!");
-               return (0);
-       }
-
-       return (_kvm_vatop(kd, va, pa));
-}
diff --git a/lib/libkvm/kvm_minidump_i386.c b/lib/libkvm/kvm_minidump_i386.c
deleted file mode 100644 (file)
index 7a59376..0000000
+++ /dev/null
@@ -1,247 +0,0 @@
-/*-
- * Copyright (c) 2006 Peter Wemm
- *
- * 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.
- */
-
-/*
- * AMD64 machine dependent routines for kvm and minidumps.
- */
-#include <sys/user.h>
-#include <sys/param.h>
-#include <sys/proc.h>
-#include <sys/stat.h>
-#include <sys/mman.h>
-#include <sys/fnv_hash.h>
-#include <stdlib.h>
-#include <string.h>
-#include <strings.h>
-#include <unistd.h>
-#include <nlist.h>
-#include <kvm.h>
-
-#include <vm/vm.h>
-#include <vm/vm_param.h>
-
-#include <machine/elf.h>
-#include <machine/cpufunc.h>
-#include <machine/minidump.h>
-
-#include <limits.h>
-
-#include "kvm_private.h"
-
-#define PG_FRAME_PAE   (~((uint64_t)PAGE_MASK))
-
-struct hpte {
-       struct hpte *next;
-       uint64_t pa;
-       int64_t off;
-};
-
-#define HPT_SIZE 1024
-
-/* minidump must be the first item! */
-struct vmstate {
-       int minidump;           /* 1 = minidump mode */
-       struct minidumphdr hdr;
-       void *hpt_head[HPT_SIZE];
-       uint32_t *bitmap;
-       void *ptemap;
-};
-
-static void
-hpt_insert(kvm_t *kd, uint64_t pa, int64_t off)
-{
-       struct hpte *hpte;
-       uint32_t fnv = FNV1_32_INIT;
-
-       fnv = fnv_32_buf(&pa, sizeof(pa), fnv);
-       fnv &= (HPT_SIZE - 1);
-       hpte = malloc(sizeof(*hpte));
-       hpte->pa = pa;
-       hpte->off = off;
-       hpte->next = kd->vmst->hpt_head[fnv];
-       kd->vmst->hpt_head[fnv] = hpte;
-}
-
-static int64_t
-hpt_find(kvm_t *kd, uint64_t pa)
-{
-       struct hpte *hpte;
-       uint32_t fnv = FNV1_32_INIT;
-
-       fnv = fnv_32_buf(&pa, sizeof(pa), fnv);
-       fnv &= (HPT_SIZE - 1);
-       for (hpte = kd->vmst->hpt_head[fnv]; hpte != NULL; hpte = hpte->next) {
-               if (pa == hpte->pa)
-                       return (hpte->off);
-       }
-       return (-1);
-}
-
-static int
-inithash(kvm_t *kd, uint32_t *base, int len, off_t off)
-{
-       uint64_t idx;
-       uint32_t bit, bits;
-       uint64_t pa;
-
-       for (idx = 0; idx < len / sizeof(*base); idx++) {
-               bits = base[idx];
-               while (bits) {
-                       bit = bsfl(bits);
-                       bits &= ~(1ul << bit);
-                       pa = (idx * sizeof(*base) * NBBY + bit) * PAGE_SIZE;
-                       hpt_insert(kd, pa, off);
-                       off += PAGE_SIZE;
-               }
-       }
-       return (off);
-}
-
-void
-_kvm_minidump_freevtop(kvm_t *kd)
-{
-       struct vmstate *vm = kd->vmst;
-
-       if (vm->bitmap)
-               free(vm->bitmap);
-       if (vm->ptemap)
-               free(vm->ptemap);
-       free(vm);
-       kd->vmst = NULL;
-}
-
-int
-_kvm_minidump_initvtop(kvm_t *kd)
-{
-       struct vmstate *vmst;
-       off_t off;
-
-       vmst = _kvm_malloc(kd, sizeof(*vmst));
-       if (vmst == NULL) {
-               _kvm_err(kd, kd->program, "cannot allocate vm");
-               return (-1);
-       }
-       kd->vmst = vmst;
-       bzero(vmst, sizeof(*vmst));
-       vmst->minidump = 1;
-       if (pread(kd->pmfd, &vmst->hdr, sizeof(vmst->hdr), 0) !=
-           sizeof(vmst->hdr)) {
-               _kvm_err(kd, kd->program, "cannot read dump header");
-               return (-1);
-       }
-       if (strncmp(MINIDUMP_MAGIC, vmst->hdr.magic, sizeof(vmst->hdr.magic)) != 0) {
-               _kvm_err(kd, kd->program, "not a minidump for this platform");
-               return (-1);
-       }
-       if (vmst->hdr.version != MINIDUMP_VERSION) {
-               _kvm_err(kd, kd->program, "wrong minidump version. expected %d got %d",
-                   MINIDUMP_VERSION, vmst->hdr.version);
-               return (-1);
-       }
-
-       /* Skip header and msgbuf */
-       off = PAGE_SIZE + round_page(vmst->hdr.msgbufsize);
-
-       vmst->bitmap = _kvm_malloc(kd, vmst->hdr.bitmapsize);
-       if (vmst->bitmap == NULL) {
-               _kvm_err(kd, kd->program, "cannot allocate %d bytes for bitmap", vmst->hdr.bitmapsize);
-               return (-1);
-       }
-       if (pread(kd->pmfd, vmst->bitmap, vmst->hdr.bitmapsize, off) !=
-           vmst->hdr.bitmapsize) {
-               _kvm_err(kd, kd->program, "cannot read %d bytes for page bitmap", vmst->hdr.bitmapsize);
-               return (-1);
-       }
-       off += round_page(vmst->hdr.bitmapsize);
-
-       vmst->ptemap = _kvm_malloc(kd, vmst->hdr.ptesize);
-       if (vmst->ptemap == NULL) {
-               _kvm_err(kd, kd->program, "cannot allocate %d bytes for ptemap", vmst->hdr.ptesize);
-               return (-1);
-       }
-       if (pread(kd->pmfd, vmst->ptemap, vmst->hdr.ptesize, off) !=
-           vmst->hdr.ptesize) {
-               _kvm_err(kd, kd->program, "cannot read %d bytes for ptemap", vmst->hdr.ptesize);
-               return (-1);
-       }
-       off += vmst->hdr.ptesize;
-
-       /* build physical address hash table for sparse pages */
-       inithash(kd, vmst->bitmap, vmst->hdr.bitmapsize, off);
-
-       return (0);
-}
-
-static int
-_kvm_minidump_vatop(kvm_t *kd, u_long va, off_t *pa)
-{
-       struct vmstate *vm;
-       u_long offset;
-       pt_entry_t pte;
-       u_long pteindex;
-       u_long a;
-       off_t ofs;
-       uint32_t *ptemap;
-
-       vm = kd->vmst;
-       ptemap = vm->ptemap;
-       offset = va & (PAGE_SIZE - 1);
-
-       if (va >= vm->hdr.kernbase) {
-               pteindex = (va - vm->hdr.kernbase) >> PAGE_SHIFT;
-               pte = ptemap[pteindex];
-               if ((pte & PG_V) == 0) {
-                       _kvm_err(kd, kd->program, "_kvm_vatop: pte not valid");
-                       goto invalid;
-               }
-               a = pte & PG_FRAME;
-               ofs = hpt_find(kd, a);
-               if (ofs == -1) {
-                       _kvm_err(kd, kd->program, "_kvm_vatop: physical address 0x%lx not in minidump", a);
-                       goto invalid;
-               }
-               *pa = ofs + offset;
-               return (PAGE_SIZE - offset);
-       } else {
-               _kvm_err(kd, kd->program, "_kvm_vatop: virtual address 0x%lx not minidumped", va);
-               goto invalid;
-       }
-
-invalid:
-       _kvm_err(kd, 0, "invalid address (0x%lx)", va);
-       return (0);
-}
-
-int
-_kvm_minidump_kvatop(kvm_t *kd, u_long va, off_t *pa)
-{
-
-       if (kvm_ishost(kd)) {
-               _kvm_err(kd, 0, "vatop called in live kernel!");
-               return (0);
-       }
-
-       return (_kvm_minidump_vatop(kd, va, pa));
-}
index 66cef35..9f7cf7f 100644 (file)
@@ -1,17 +1,11 @@
 # $FreeBSD: head/usr.sbin/crunch/crunchide/Makefile 232911 2012-03-13 05:21:14Z jmallett $
 
-PROG=   crunchide
+PROG=  crunchide
 SRCS=  crunchide.c
 
-TARGET_ARCH?=  ${MACHINE_ARCH}
-
-.if ${TARGET_ARCH} == x86_64
+.if ${MACHINE_ARCH} == "x86_64"
 CFLAGS+=-DNLIST_ELF64
 SRCS+= exec_elf64.c
-exec_elf64.o: exec_elf32.c
-.else
-CFLAGS+=-DNLIST_ELF32
-SRCS+= exec_elf32.c
 .endif
 
 .include <bsd.prog.mk>
diff --git a/usr.bin/crunch/crunchide/exec_elf32.c b/usr.bin/crunch/crunchide/exec_elf32.c
deleted file mode 100644 (file)
index f63d09e..0000000
+++ /dev/null
@@ -1,490 +0,0 @@
-/*
- * Copyright (c) 1997 Christopher G. Demetriou.  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.
- * 3. All advertising materials mentioning features or use of this software
- *    must display the following acknowledgement:
- *      This product includes software developed by Christopher G. Demetriou
- *     for the NetBSD Project.
- * 4. The name of the author may not be used to endorse or promote products
- *    derived from this software without specific prior written permission
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 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.
- */
-
-#ifndef ELFSIZE
-#define ELFSIZE         32
-#endif
-
-#include <sys/types.h>
-#include <sys/endian.h>
-#include <sys/stat.h>
-
-#include <errno.h>
-#include <limits.h>
-#include <stddef.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-
-#include "extern.h"
-
-#if (defined(NLIST_ELF32) && (ELFSIZE == 32)) || \
-    (defined(NLIST_ELF64) && (ELFSIZE == 64))
-
-#define        __ELF_WORD_SIZE ELFSIZE
-#if (ELFSIZE == 32)
-#include <sys/elf32.h>
-#define        xewtoh(x)       ((data == ELFDATA2MSB) ? be32toh(x) : le32toh(x))
-#define        htoxew(x)       ((data == ELFDATA2MSB) ? htobe32(x) : htole32(x))
-#define        wewtoh(x)       ((data == ELFDATA2MSB) ? be32toh(x) : le32toh(x))
-#define        htowew(x)       ((data == ELFDATA2MSB) ? htobe32(x) : htole32(x))
-#elif (ELFSIZE == 64)
-#include <sys/elf64.h>
-#define        xewtoh(x)       ((data == ELFDATA2MSB) ? be64toh(x) : le64toh(x))
-#define        htoxew(x)       ((data == ELFDATA2MSB) ? htobe64(x) : htole64(x))
-/* elf64 Elf64_Word are 32 bits */
-#define        wewtoh(x)       ((data == ELFDATA2MSB) ? be32toh(x) : le32toh(x))
-#define        htowew(x)       ((data == ELFDATA2MSB) ? htobe32(x) : htole32(x))
-#endif
-#include <sys/elf_generic.h>
-
-#define CONCAT(x,y)     __CONCAT(x,y)
-#define ELFNAME(x)      CONCAT(elf,CONCAT(ELFSIZE,CONCAT(_,x)))
-#define ELFNAME2(x,y)   CONCAT(x,CONCAT(_elf,CONCAT(ELFSIZE,CONCAT(_,y))))
-#define ELFNAMEEND(x)   CONCAT(x,CONCAT(_elf,ELFSIZE))
-#define ELFDEFNNAME(x)  CONCAT(ELF,CONCAT(ELFSIZE,CONCAT(_,x)))
-
-#define        xe16toh(x)      ((data == ELFDATA2MSB) ? be16toh(x) : le16toh(x))
-#define        xe32toh(x)      ((data == ELFDATA2MSB) ? be32toh(x) : le32toh(x))
-#define        htoxe32(x)      ((data == ELFDATA2MSB) ? htobe32(x) : htole32(x))
-
-struct shlayout {
-       Elf_Shdr *shdr;
-       void *bufp;
-};
-
-static ssize_t
-xreadatoff(int fd, void *buf, off_t off, size_t size, const char *fn)
-{
-       ssize_t rv;
-
-       if (lseek(fd, off, SEEK_SET) != off) {
-               perror(fn);
-               return -1;
-       }
-       if ((size_t)(rv = read(fd, buf, size)) != size) {
-               fprintf(stderr, "%s: read error: %s\n", fn,
-                   rv == -1 ? strerror(errno) : "short read");
-               return -1;
-       }
-       return size;
-}
-
-static ssize_t
-xwriteatoff(int fd, void *buf, off_t off, size_t size, const char *fn)
-{
-       ssize_t rv;
-
-       if (lseek(fd, off, SEEK_SET) != off) {
-               perror(fn);
-               return -1;
-       }
-       if ((size_t)(rv = write(fd, buf, size)) != size) {
-               fprintf(stderr, "%s: write error: %s\n", fn,
-                   rv == -1 ? strerror(errno) : "short write");
-               return -1;
-       }
-       return size;
-}
-
-static void *
-xmalloc(size_t size, const char *fn, const char *use)
-{
-       void *rv;
-
-       rv = malloc(size);
-       if (rv == NULL)
-               fprintf(stderr, "%s: out of memory (allocating for %s)\n",
-                   fn, use);
-       return (rv);
-}
-
-static void *
-xrealloc(void *ptr, size_t size, const char *fn, const char *use)
-{
-       void *rv;
-               
-       rv = realloc(ptr, size);
-       if (rv == NULL) {
-               free(ptr);
-               fprintf(stderr, "%s: out of memory (reallocating for %s)\n",
-                   fn, use);
-       }
-       return (rv);
-} 
-
-int
-ELFNAMEEND(check)(int fd, const char *fn)
-{
-       Elf_Ehdr eh;
-       struct stat sb;
-       unsigned char data;
-
-       /*
-        * Check the header to maek sure it's an ELF file (of the
-        * appropriate size).
-        */
-       if (fstat(fd, &sb) == -1)
-               return 0;
-       if (sb.st_size < (off_t)(sizeof eh))
-               return 0;
-       if (read(fd, &eh, sizeof eh) != sizeof eh)
-               return 0;
-
-       if (IS_ELF(eh) == 0)
-                return 0;
-
-       data = eh.e_ident[EI_DATA];
-
-       switch (xe16toh(eh.e_machine)) {
-       case EM_386: break;
-       case EM_ALPHA: break;
-#ifndef EM_AARCH64
-#define        EM_AARCH64      183
-#endif
-       case EM_AARCH64: break;
-#ifndef EM_ARM
-#define EM_ARM         40
-#endif
-       case EM_ARM: break;
-#ifndef EM_MIPS
-#define EM_MIPS                8
-#endif
-#ifndef EM_MIPS_RS4_BE         /* same as EM_MIPS_RS3_LE */
-#define EM_MIPS_RS4_BE 10
-#endif
-       case EM_MIPS: break;
-       case /* EM_MIPS_RS3_LE */ EM_MIPS_RS4_BE: break;
-#ifndef EM_PPC
-#define        EM_PPC          20
-#endif
-       case EM_PPC: break;
-#ifndef EM_PPC64
-#define        EM_PPC64        21
-#endif
-       case EM_PPC64: break;
-#ifndef EM_SPARCV9
-#define        EM_SPARCV9      43
-#endif
-       case EM_SPARCV9: break;
-#ifndef EM_X86_64
-#define        EM_X86_64       62
-#endif
-       case EM_X86_64: break;
-/*        ELFDEFNNAME(MACHDEP_ID_CASES) */
-
-        default:
-                return 0;
-        }
-
-       return 1;
-}
-
-/*
- * This function 'hides' (some of) ELF executable file's symbols.
- * It hides them by renaming them to "_$$hide$$ <filename> <symbolname>".
- * Symbols in the global keep list, or which are marked as being undefined,
- * are left alone.
- *
- * An old version of this code shuffled various tables around, turning
- * global symbols to be hidden into local symbols.  That lost on the
- * mips, because CALL16 relocs must reference global symbols, and, if
- * those symbols were being hidden, they were no longer global.
- *
- * The new renaming behaviour doesn't take global symbols out of the
- * namespace.  However, it's ... unlikely that there will ever be
- * any collisions in practice because of the new method.
- */
-int
-ELFNAMEEND(hide)(int fd, const char *fn)
-{
-       Elf_Ehdr ehdr;
-       struct shlayout *layoutp = NULL;
-       Elf_Shdr *shdrp = NULL, *symtabshdr, *strtabshdr, *shstrtabshdr;
-       Elf_Shdr shdrshdr;
-       Elf_Sym *symtabp = NULL;
-       char *shstrtabp = NULL, *strtabp = NULL;
-       Elf_Size nsyms, ewi;
-       Elf_Off off;
-       ssize_t shdrsize;
-       int rv, i, weird, l, m, r, strtabidx;
-       size_t nstrtab_size, nstrtab_nextoff, fn_size, size;
-       char *nstrtabp = NULL;
-       unsigned char data;
-       const char *weirdreason = NULL;
-       void *buf;
-       Elf_Half shnum;
-
-       rv = 0;
-       if (xreadatoff(fd, &ehdr, 0, sizeof ehdr, fn) != sizeof ehdr)
-               goto bad;
-
-       data = ehdr.e_ident[EI_DATA];
-       shnum = xe16toh(ehdr.e_shnum);
-
-       shdrsize = shnum * xe16toh(ehdr.e_shentsize);
-       if ((shdrp = xmalloc(shdrsize, fn, "section header table")) == NULL)
-               goto bad;
-       if (xreadatoff(fd, shdrp, xewtoh(ehdr.e_shoff), shdrsize, fn) !=
-           shdrsize)
-               goto bad;
-
-       symtabshdr = strtabshdr = shstrtabshdr = NULL;
-       weird = 0;
-       for (i = 0; i < shnum; i++) {
-               switch (xe32toh(shdrp[i].sh_type)) {
-               case SHT_SYMTAB:
-                       if (symtabshdr != NULL) {
-                               weird = 1;
-                               weirdreason = "multiple symbol tables";
-                       }
-                       symtabshdr = &shdrp[i];
-                       strtabshdr = &shdrp[xe32toh(shdrp[i].sh_link)];
-                       break;
-               case SHT_STRTAB:
-                       if (i == xe16toh(ehdr.e_shstrndx))
-                               shstrtabshdr = &shdrp[i];
-                       break;
-               }
-       }
-       if (symtabshdr == NULL)
-               goto out;
-       if (strtabshdr == NULL) {
-               weird = 1;
-               weirdreason = "string table does not exist";
-       }
-       if (shstrtabshdr == NULL) {
-               weird = 1;
-               weirdreason = "section header string table does not exist";
-       }
-       if (weirdreason == NULL)
-               weirdreason = "unsupported";
-       if (weird) {
-               fprintf(stderr, "%s: weird executable (%s)\n", fn, weirdreason);
-               goto bad;
-       }
-
-       /*
-        * sort section layout table by offset
-        */
-       layoutp = xmalloc((shnum + 1) * sizeof(struct shlayout),
-           fn, "layout table");
-       if (layoutp == NULL)
-               goto bad;
-
-       /* add a pseudo entry to represent the section header table */
-       shdrshdr.sh_offset = ehdr.e_shoff;
-       shdrshdr.sh_size = htoxew(shdrsize);
-       shdrshdr.sh_addralign = htoxew(ELFSIZE / 8);
-       layoutp[shnum].shdr = &shdrshdr;
-
-       /* insert and sort normal section headers */
-       for (i = shnum; i-- != 0;) {
-               l = i + 1;
-               r = shnum;
-               while (l <= r) {
-                       m = ( l + r) / 2;
-                       if (xewtoh(shdrp[i].sh_offset) >
-                           xewtoh(layoutp[m].shdr->sh_offset))
-                               l = m + 1;
-                       else
-                               r = m - 1;
-               }
-
-               if (r != i) {
-                       memmove(&layoutp[i], &layoutp[i + 1],
-                           sizeof(struct shlayout) * (r - i));
-               }
-
-               layoutp[r].shdr = &shdrp[i];
-               layoutp[r].bufp = NULL;
-       }
-       ++shnum;
-
-       /*
-        * load up everything we need
-        */
-
-       /* load section string table for debug use */
-       if ((shstrtabp = xmalloc(xewtoh(shstrtabshdr->sh_size), fn,
-           "section string table")) == NULL)
-               goto bad;
-       if ((size_t)xreadatoff(fd, shstrtabp, xewtoh(shstrtabshdr->sh_offset),
-           xewtoh(shstrtabshdr->sh_size), fn) != xewtoh(shstrtabshdr->sh_size))
-               goto bad;
-
-       /* we need symtab, strtab, and everything behind strtab */
-       strtabidx = INT_MAX;
-       for (i = 0; i < shnum; i++) {
-               if (layoutp[i].shdr == &shdrshdr) {
-                       /* not load section header again */
-                       layoutp[i].bufp = shdrp;
-                       continue;
-               }
-               if (layoutp[i].shdr == shstrtabshdr) {
-                       /* not load section string table again */
-                       layoutp[i].bufp = shstrtabp;
-                       continue;
-               }
-
-               if (layoutp[i].shdr == strtabshdr)
-                       strtabidx = i;
-               if (layoutp[i].shdr == symtabshdr || i >= strtabidx) {
-                       off = xewtoh(layoutp[i].shdr->sh_offset);
-                       size = xewtoh(layoutp[i].shdr->sh_size);
-                       layoutp[i].bufp = xmalloc(size, fn,
-                           shstrtabp + xewtoh(layoutp[i].shdr->sh_name));
-                       if (layoutp[i].bufp == NULL)
-                               goto bad;
-                       if ((size_t)xreadatoff(fd, layoutp[i].bufp, off, size, fn) !=
-                           size)
-                               goto bad;
-
-                       /* set symbol table and string table */
-                       if (layoutp[i].shdr == symtabshdr)
-                               symtabp = layoutp[i].bufp;
-                       else if (layoutp[i].shdr == strtabshdr)
-                               strtabp = layoutp[i].bufp;
-               }
-       }
-
-       nstrtab_size = 256;
-       nstrtabp = xmalloc(nstrtab_size, fn, "new string table");
-       if (nstrtabp == NULL)
-               goto bad;
-       nstrtab_nextoff = 0;
-
-       fn_size = strlen(fn);
-
-       /* Prepare data structures for symbol movement. */
-       nsyms = xewtoh(symtabshdr->sh_size) / xewtoh(symtabshdr->sh_entsize);
-
-       /* move symbols, making them local */
-       for (ewi = 0; ewi < nsyms; ewi++) {
-               Elf_Sym *sp = &symtabp[ewi];
-               const char *symname = strtabp + xe32toh(sp->st_name);
-               size_t newent_len;
-               /*
-                * make sure there's size for the next entry, even if it's
-                * as large as it can be.
-                *
-                * "_$$hide$$ <filename> <symname><NUL>" ->
-                *    9 + 3 + sizes of fn and sym name
-                */
-               while ((nstrtab_size - nstrtab_nextoff) <
-                   strlen(symname) + fn_size + 12) {
-                       nstrtab_size *= 2;
-                       nstrtabp = xrealloc(nstrtabp, nstrtab_size, fn,
-                           "new string table");
-                       if (nstrtabp == NULL)
-                               goto bad;
-               }
-
-               sp->st_name = htowew(nstrtab_nextoff);
-
-               /* if it's a keeper or is undefined, don't rename it. */
-               if (in_keep_list(symname) ||
-                   (xe16toh(sp->st_shndx) == SHN_UNDEF)) {
-                       newent_len = sprintf(nstrtabp + nstrtab_nextoff,
-                           "%s", symname) + 1;
-               } else {
-                       newent_len = sprintf(nstrtabp + nstrtab_nextoff,
-                           "_$$hide$$ %s %s", fn, symname) + 1;
-               }
-               nstrtab_nextoff += newent_len;
-       }
-       strtabshdr->sh_size = htoxew(nstrtab_nextoff);
-
-       /*
-        * update section header table in ascending order of offset
-        */
-       for (i = strtabidx + 1; i < shnum; i++) {
-               Elf_Off off, align;
-               off = xewtoh(layoutp[i - 1].shdr->sh_offset) +
-                   xewtoh(layoutp[i - 1].shdr->sh_size);
-               align = xewtoh(layoutp[i].shdr->sh_addralign);
-               off = (off + (align - 1)) & ~(align - 1);
-               layoutp[i].shdr->sh_offset = htoxew(off);
-       }
-
-       /*
-        * write data to the file in descending order of offset
-        */
-       for (i = shnum; i-- != 0;) {
-               if (layoutp[i].shdr == strtabshdr) {
-                       /* new string table */
-                       buf = nstrtabp;
-               } else
-                       buf = layoutp[i].bufp;
-
-               if (layoutp[i].shdr == &shdrshdr ||
-                   layoutp[i].shdr == symtabshdr || i >= strtabidx) {
-                       if (buf == NULL)
-                               goto bad;
-
-                       /*
-                        * update the offset of section header table in elf
-                        * header if needed.
-                        */
-                       if (layoutp[i].shdr == &shdrshdr &&
-                           ehdr.e_shoff != shdrshdr.sh_offset) {
-                               ehdr.e_shoff = shdrshdr.sh_offset;
-                               off = offsetof(Elf_Ehdr, e_shoff);
-                               size = sizeof(Elf_Off);
-                               if ((size_t)xwriteatoff(fd, &ehdr.e_shoff, off, size,
-                                   fn) != size)
-                                       goto bad;
-                       }
-
-                       off = xewtoh(layoutp[i].shdr->sh_offset);
-                       size = xewtoh(layoutp[i].shdr->sh_size);
-                       if ((size_t)xwriteatoff(fd, buf, off, size, fn) != size)
-                               goto bad;
-               }
-       }
-
-out:
-       if (layoutp != NULL) {
-               for (i = 0; i < shnum; i++) {
-                       if (layoutp[i].bufp != NULL)
-                               free(layoutp[i].bufp);
-               }
-               free(layoutp);
-       }
-       free(nstrtabp);
-       return (rv);
-
-bad:
-       rv = 1;
-       goto out;
-}
-
-#endif /* include this size of ELF */
index 179e45d..4bd252b 100644 (file)
@@ -1,5 +1,3 @@
-/*     $NetBSD: exec_elf64.c,v 1.2 1997/08/02 21:30:19 perry Exp $     */
-
 /*
  * Copyright (c) 1997 Christopher G. Demetriou.  All rights reserved.
  *
  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+#ifdef __x86_64__
 #define        ELFSIZE         64
+#endif
+
+#include <sys/types.h>
+#include <sys/endian.h>
+#include <sys/stat.h>
+
+#include <errno.h>
+#include <limits.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "extern.h"
+
+#if (defined(NLIST_ELF32) && (ELFSIZE == 32)) || \
+    (defined(NLIST_ELF64) && (ELFSIZE == 64))
+
+#define        __ELF_WORD_SIZE ELFSIZE
+#if (ELFSIZE == 32)
+#include <sys/elf32.h>
+#define        xewtoh(x)       ((data == ELFDATA2MSB) ? be32toh(x) : le32toh(x))
+#define        htoxew(x)       ((data == ELFDATA2MSB) ? htobe32(x) : htole32(x))
+#define        wewtoh(x)       ((data == ELFDATA2MSB) ? be32toh(x) : le32toh(x))
+#define        htowew(x)       ((data == ELFDATA2MSB) ? htobe32(x) : htole32(x))
+#elif (ELFSIZE == 64)
+#include <sys/elf64.h>
+#define        xewtoh(x)       ((data == ELFDATA2MSB) ? be64toh(x) : le64toh(x))
+#define        htoxew(x)       ((data == ELFDATA2MSB) ? htobe64(x) : htole64(x))
+/* elf64 Elf64_Word are 32 bits */
+#define        wewtoh(x)       ((data == ELFDATA2MSB) ? be32toh(x) : le32toh(x))
+#define        htowew(x)       ((data == ELFDATA2MSB) ? htobe32(x) : htole32(x))
+#endif
+#include <sys/elf_generic.h>
+
+#define CONCAT(x,y)     __CONCAT(x,y)
+#define ELFNAME(x)      CONCAT(elf,CONCAT(ELFSIZE,CONCAT(_,x)))
+#define ELFNAME2(x,y)   CONCAT(x,CONCAT(_elf,CONCAT(ELFSIZE,CONCAT(_,y))))
+#define ELFNAMEEND(x)   CONCAT(x,CONCAT(_elf,ELFSIZE))
+#define ELFDEFNNAME(x)  CONCAT(ELF,CONCAT(ELFSIZE,CONCAT(_,x)))
+
+#define        xe16toh(x)      ((data == ELFDATA2MSB) ? be16toh(x) : le16toh(x))
+#define        xe32toh(x)      ((data == ELFDATA2MSB) ? be32toh(x) : le32toh(x))
+#define        htoxe32(x)      ((data == ELFDATA2MSB) ? htobe32(x) : htole32(x))
+
+struct shlayout {
+       Elf_Shdr *shdr;
+       void *bufp;
+};
+
+static ssize_t
+xreadatoff(int fd, void *buf, off_t off, size_t size, const char *fn)
+{
+       ssize_t rv;
+
+       if (lseek(fd, off, SEEK_SET) != off) {
+               perror(fn);
+               return -1;
+       }
+       if ((size_t)(rv = read(fd, buf, size)) != size) {
+               fprintf(stderr, "%s: read error: %s\n", fn,
+                   rv == -1 ? strerror(errno) : "short read");
+               return -1;
+       }
+       return size;
+}
+
+static ssize_t
+xwriteatoff(int fd, void *buf, off_t off, size_t size, const char *fn)
+{
+       ssize_t rv;
+
+       if (lseek(fd, off, SEEK_SET) != off) {
+               perror(fn);
+               return -1;
+       }
+       if ((size_t)(rv = write(fd, buf, size)) != size) {
+               fprintf(stderr, "%s: write error: %s\n", fn,
+                   rv == -1 ? strerror(errno) : "short write");
+               return -1;
+       }
+       return size;
+}
+
+static void *
+xmalloc(size_t size, const char *fn, const char *use)
+{
+       void *rv;
+
+       rv = malloc(size);
+       if (rv == NULL)
+               fprintf(stderr, "%s: out of memory (allocating for %s)\n",
+                   fn, use);
+       return (rv);
+}
+
+static void *
+xrealloc(void *ptr, size_t size, const char *fn, const char *use)
+{
+       void *rv;
+               
+       rv = realloc(ptr, size);
+       if (rv == NULL) {
+               free(ptr);
+               fprintf(stderr, "%s: out of memory (reallocating for %s)\n",
+                   fn, use);
+       }
+       return (rv);
+} 
+
+int
+ELFNAMEEND(check)(int fd, const char *fn)
+{
+       Elf_Ehdr eh;
+       struct stat sb;
+       unsigned char data;
+
+       /*
+        * Check the header to maek sure it's an ELF file (of the
+        * appropriate size).
+        */
+       if (fstat(fd, &sb) == -1)
+               return 0;
+       if (sb.st_size < (off_t)(sizeof eh))
+               return 0;
+       if (read(fd, &eh, sizeof eh) != sizeof eh)
+               return 0;
+
+       if (IS_ELF(eh) == 0)
+                return 0;
+
+       data = eh.e_ident[EI_DATA];
+
+       switch (xe16toh(eh.e_machine)) {
+       case EM_386: break;
+       case EM_ALPHA: break;
+#ifndef EM_AARCH64
+#define        EM_AARCH64      183
+#endif
+       case EM_AARCH64: break;
+#ifndef EM_ARM
+#define EM_ARM         40
+#endif
+       case EM_ARM: break;
+#ifndef EM_MIPS
+#define EM_MIPS                8
+#endif
+#ifndef EM_MIPS_RS4_BE         /* same as EM_MIPS_RS3_LE */
+#define EM_MIPS_RS4_BE 10
+#endif
+       case EM_MIPS: break;
+       case /* EM_MIPS_RS3_LE */ EM_MIPS_RS4_BE: break;
+#ifndef EM_PPC
+#define        EM_PPC          20
+#endif
+       case EM_PPC: break;
+#ifndef EM_PPC64
+#define        EM_PPC64        21
+#endif
+       case EM_PPC64: break;
+#ifndef EM_SPARCV9
+#define        EM_SPARCV9      43
+#endif
+       case EM_SPARCV9: break;
+#ifndef EM_X86_64
+#define        EM_X86_64       62
+#endif
+       case EM_X86_64: break;
+/*        ELFDEFNNAME(MACHDEP_ID_CASES) */
+
+        default:
+                return 0;
+        }
+
+       return 1;
+}
+
+/*
+ * This function 'hides' (some of) ELF executable file's symbols.
+ * It hides them by renaming them to "_$$hide$$ <filename> <symbolname>".
+ * Symbols in the global keep list, or which are marked as being undefined,
+ * are left alone.
+ *
+ * An old version of this code shuffled various tables around, turning
+ * global symbols to be hidden into local symbols.  That lost on the
+ * mips, because CALL16 relocs must reference global symbols, and, if
+ * those symbols were being hidden, they were no longer global.
+ *
+ * The new renaming behaviour doesn't take global symbols out of the
+ * namespace.  However, it's ... unlikely that there will ever be
+ * any collisions in practice because of the new method.
+ */
+int
+ELFNAMEEND(hide)(int fd, const char *fn)
+{
+       Elf_Ehdr ehdr;
+       struct shlayout *layoutp = NULL;
+       Elf_Shdr *shdrp = NULL, *symtabshdr, *strtabshdr, *shstrtabshdr;
+       Elf_Shdr shdrshdr;
+       Elf_Sym *symtabp = NULL;
+       char *shstrtabp = NULL, *strtabp = NULL;
+       Elf_Size nsyms, ewi;
+       Elf_Off off;
+       ssize_t shdrsize;
+       int rv, i, weird, l, m, r, strtabidx;
+       size_t nstrtab_size, nstrtab_nextoff, fn_size, size;
+       char *nstrtabp = NULL;
+       unsigned char data;
+       const char *weirdreason = NULL;
+       void *buf;
+       Elf_Half shnum;
+
+       rv = 0;
+       if (xreadatoff(fd, &ehdr, 0, sizeof ehdr, fn) != sizeof ehdr)
+               goto bad;
+
+       data = ehdr.e_ident[EI_DATA];
+       shnum = xe16toh(ehdr.e_shnum);
+
+       shdrsize = shnum * xe16toh(ehdr.e_shentsize);
+       if ((shdrp = xmalloc(shdrsize, fn, "section header table")) == NULL)
+               goto bad;
+       if (xreadatoff(fd, shdrp, xewtoh(ehdr.e_shoff), shdrsize, fn) !=
+           shdrsize)
+               goto bad;
+
+       symtabshdr = strtabshdr = shstrtabshdr = NULL;
+       weird = 0;
+       for (i = 0; i < shnum; i++) {
+               switch (xe32toh(shdrp[i].sh_type)) {
+               case SHT_SYMTAB:
+                       if (symtabshdr != NULL) {
+                               weird = 1;
+                               weirdreason = "multiple symbol tables";
+                       }
+                       symtabshdr = &shdrp[i];
+                       strtabshdr = &shdrp[xe32toh(shdrp[i].sh_link)];
+                       break;
+               case SHT_STRTAB:
+                       if (i == xe16toh(ehdr.e_shstrndx))
+                               shstrtabshdr = &shdrp[i];
+                       break;
+               }
+       }
+       if (symtabshdr == NULL)
+               goto out;
+       if (strtabshdr == NULL) {
+               weird = 1;
+               weirdreason = "string table does not exist";
+       }
+       if (shstrtabshdr == NULL) {
+               weird = 1;
+               weirdreason = "section header string table does not exist";
+       }
+       if (weirdreason == NULL)
+               weirdreason = "unsupported";
+       if (weird) {
+               fprintf(stderr, "%s: weird executable (%s)\n", fn, weirdreason);
+               goto bad;
+       }
+
+       /*
+        * sort section layout table by offset
+        */
+       layoutp = xmalloc((shnum + 1) * sizeof(struct shlayout),
+           fn, "layout table");
+       if (layoutp == NULL)
+               goto bad;
+
+       /* add a pseudo entry to represent the section header table */
+       shdrshdr.sh_offset = ehdr.e_shoff;
+       shdrshdr.sh_size = htoxew(shdrsize);
+       shdrshdr.sh_addralign = htoxew(ELFSIZE / 8);
+       layoutp[shnum].shdr = &shdrshdr;
+
+       /* insert and sort normal section headers */
+       for (i = shnum; i-- != 0;) {
+               l = i + 1;
+               r = shnum;
+               while (l <= r) {
+                       m = ( l + r) / 2;
+                       if (xewtoh(shdrp[i].sh_offset) >
+                           xewtoh(layoutp[m].shdr->sh_offset))
+                               l = m + 1;
+                       else
+                               r = m - 1;
+               }
+
+               if (r != i) {
+                       memmove(&layoutp[i], &layoutp[i + 1],
+                           sizeof(struct shlayout) * (r - i));
+               }
+
+               layoutp[r].shdr = &shdrp[i];
+               layoutp[r].bufp = NULL;
+       }
+       ++shnum;
+
+       /*
+        * load up everything we need
+        */
+
+       /* load section string table for debug use */
+       if ((shstrtabp = xmalloc(xewtoh(shstrtabshdr->sh_size), fn,
+           "section string table")) == NULL)
+               goto bad;
+       if ((size_t)xreadatoff(fd, shstrtabp, xewtoh(shstrtabshdr->sh_offset),
+           xewtoh(shstrtabshdr->sh_size), fn) != xewtoh(shstrtabshdr->sh_size))
+               goto bad;
+
+       /* we need symtab, strtab, and everything behind strtab */
+       strtabidx = INT_MAX;
+       for (i = 0; i < shnum; i++) {
+               if (layoutp[i].shdr == &shdrshdr) {
+                       /* not load section header again */
+                       layoutp[i].bufp = shdrp;
+                       continue;
+               }
+               if (layoutp[i].shdr == shstrtabshdr) {
+                       /* not load section string table again */
+                       layoutp[i].bufp = shstrtabp;
+                       continue;
+               }
+
+               if (layoutp[i].shdr == strtabshdr)
+                       strtabidx = i;
+               if (layoutp[i].shdr == symtabshdr || i >= strtabidx) {
+                       off = xewtoh(layoutp[i].shdr->sh_offset);
+                       size = xewtoh(layoutp[i].shdr->sh_size);
+                       layoutp[i].bufp = xmalloc(size, fn,
+                           shstrtabp + xewtoh(layoutp[i].shdr->sh_name));
+                       if (layoutp[i].bufp == NULL)
+                               goto bad;
+                       if ((size_t)xreadatoff(fd, layoutp[i].bufp, off, size, fn) !=
+                           size)
+                               goto bad;
+
+                       /* set symbol table and string table */
+                       if (layoutp[i].shdr == symtabshdr)
+                               symtabp = layoutp[i].bufp;
+                       else if (layoutp[i].shdr == strtabshdr)
+                               strtabp = layoutp[i].bufp;
+               }
+       }
+
+       nstrtab_size = 256;
+       nstrtabp = xmalloc(nstrtab_size, fn, "new string table");
+       if (nstrtabp == NULL)
+               goto bad;
+       nstrtab_nextoff = 0;
+
+       fn_size = strlen(fn);
+
+       /* Prepare data structures for symbol movement. */
+       nsyms = xewtoh(symtabshdr->sh_size) / xewtoh(symtabshdr->sh_entsize);
+
+       /* move symbols, making them local */
+       for (ewi = 0; ewi < nsyms; ewi++) {
+               Elf_Sym *sp = &symtabp[ewi];
+               const char *symname = strtabp + xe32toh(sp->st_name);
+               size_t newent_len;
+               /*
+                * make sure there's size for the next entry, even if it's
+                * as large as it can be.
+                *
+                * "_$$hide$$ <filename> <symname><NUL>" ->
+                *    9 + 3 + sizes of fn and sym name
+                */
+               while ((nstrtab_size - nstrtab_nextoff) <
+                   strlen(symname) + fn_size + 12) {
+                       nstrtab_size *= 2;
+                       nstrtabp = xrealloc(nstrtabp, nstrtab_size, fn,
+                           "new string table");
+                       if (nstrtabp == NULL)
+                               goto bad;
+               }
+
+               sp->st_name = htowew(nstrtab_nextoff);
+
+               /* if it's a keeper or is undefined, don't rename it. */
+               if (in_keep_list(symname) ||
+                   (xe16toh(sp->st_shndx) == SHN_UNDEF)) {
+                       newent_len = sprintf(nstrtabp + nstrtab_nextoff,
+                           "%s", symname) + 1;
+               } else {
+                       newent_len = sprintf(nstrtabp + nstrtab_nextoff,
+                           "_$$hide$$ %s %s", fn, symname) + 1;
+               }
+               nstrtab_nextoff += newent_len;
+       }
+       strtabshdr->sh_size = htoxew(nstrtab_nextoff);
+
+       /*
+        * update section header table in ascending order of offset
+        */
+       for (i = strtabidx + 1; i < shnum; i++) {
+               Elf_Off off, align;
+               off = xewtoh(layoutp[i - 1].shdr->sh_offset) +
+                   xewtoh(layoutp[i - 1].shdr->sh_size);
+               align = xewtoh(layoutp[i].shdr->sh_addralign);
+               off = (off + (align - 1)) & ~(align - 1);
+               layoutp[i].shdr->sh_offset = htoxew(off);
+       }
+
+       /*
+        * write data to the file in descending order of offset
+        */
+       for (i = shnum; i-- != 0;) {
+               if (layoutp[i].shdr == strtabshdr) {
+                       /* new string table */
+                       buf = nstrtabp;
+               } else
+                       buf = layoutp[i].bufp;
+
+               if (layoutp[i].shdr == &shdrshdr ||
+                   layoutp[i].shdr == symtabshdr || i >= strtabidx) {
+                       if (buf == NULL)
+                               goto bad;
+
+                       /*
+                        * update the offset of section header table in elf
+                        * header if needed.
+                        */
+                       if (layoutp[i].shdr == &shdrshdr &&
+                           ehdr.e_shoff != shdrshdr.sh_offset) {
+                               ehdr.e_shoff = shdrshdr.sh_offset;
+                               off = offsetof(Elf_Ehdr, e_shoff);
+                               size = sizeof(Elf_Off);
+                               if ((size_t)xwriteatoff(fd, &ehdr.e_shoff, off, size,
+                                   fn) != size)
+                                       goto bad;
+                       }
+
+                       off = xewtoh(layoutp[i].shdr->sh_offset);
+                       size = xewtoh(layoutp[i].shdr->sh_size);
+                       if ((size_t)xwriteatoff(fd, buf, off, size, fn) != size)
+                               goto bad;
+               }
+       }
+
+out:
+       if (layoutp != NULL) {
+               for (i = 0; i < shnum; i++) {
+                       if (layoutp[i].bufp != NULL)
+                               free(layoutp[i].bufp);
+               }
+               free(layoutp);
+       }
+       free(nstrtabp);
+       return (rv);
+
+bad:
+       rv = 1;
+       goto out;
+}
 
-#include "exec_elf32.c"
+#endif /* include this size of ELF */