view dynodump/dynodump.c @ 814:a634e3b7acc8

[xemacs-hg @ 2002-04-14 12:41:59 by ben] latest changes TODO.ben-mule-21-5: Update. make-docfile.c: Add basic support for handling ISO 2022 doc strings -- we parse the basic charset designation sequences so we know whether we're in ASCII and have to pay attention to end quotes and such. Reformat code according to coding standards. abbrev.el: Add `global-abbrev-mode', which turns on or off abbrev-mode in all buffers. Added `defining-abbrev-turns-on-abbrev-mode' -- if non-nil, defining an abbrev through an interactive function will automatically turn on abbrev-mode, either globally or locally depending on the command. This is the "what you'd expect" behavior. indent.el: general function for indenting a balanced expression in a mode-correct way. Works similar to indent-region in that a mode can specify a specific command to do the whole operation; if not, figure out the region using forward-sexp and indent each line using indent-according-to-mode. keydefs.el: Removed. Modify M-C-backslash to do indent-region-or-balanced-expression. Make S-Tab just insert a TAB char, like it's meant to do. make-docfile.el: Now that we're using the call-process-in-lisp, we need to load an extra file win32-native.el because we're running a bare temacs. menubar-items.el: Totally redo the Cmds menu so that most used commands appear directly on the menu and less used commands appear in submenus. The old way may have been very pretty, but rather impractical. process.el: Under Windows, don't ever use old-call-process-internal, even in batch mode. We can do processes in batch mode. subr.el: Someone recoded truncate-string-to-width, saying "the FSF version is too complicated and does lots of hard-to-understand stuff" but the resulting recoded version was *totally* wrong! it misunderstood the basic point of this function, which is work in *columns* not chars. i dumped ours and copied the version from FSF 21.1. Also added truncate-string-with-continuation-dots, since this idiom is used often. config.inc.samp, xemacs.mak: Separate out debug and optimize flags. Remove all vestiges of USE_MINIMAL_TAGBITS, USE_INDEXED_LRECORD_IMPLEMENTATION, and GUNG_HO, since those ifdefs have long been removed. Make error-checking support actually work. Some rearrangement of config.inc.samp to make it more logical. Remove callproc.c and ntproc.c from xemacs.mak, no longer used. Make pdump the default. lisp.h: Add support for strong type-checking of Bytecount, Bytebpos, Charcount, Charbpos, and others, by making them classes, overloading the operators to provide integer-like operation and carefully controlling what operations are allowed. Not currently enabled in C++ builds because there are still a number of compile errors, and it won't really work till we merge in my "8-bit-Mule" workspace, in which I make use of the new types Charxpos, Bytexpos, Memxpos, representing a "position" either in a buffer or a string. (This is especially important in the extent code.) abbrev.c, alloc.c, eval.c, buffer.c, buffer.h, editfns.c, fns.c, text.h: Warning fixes, some of them related to new C++ strict type checking of Bytecount, Charbpos, etc. dired.c: Caught an actual error due to strong type checking -- char len being passed when should be byte len. alloc.c, backtrace.h, bytecode.c, bytecode.h, eval.c, sysdep.c: Further optimize Ffuncall: -- process arg list at compiled-function creation time, converting into an array for extra-quick access at funcall time. -- rewrite funcall_compiled_function to use it, and inline this function. -- change the order of check for magic stuff in SPECBIND_FAST_UNSAFE to be faster. -- move the check for need to garbage collect into the allocation code, so only a single flag needs to be checked in funcall. buffer.c, symbols.c: add debug funs to check on mule optimization info in buffers and strings. eval.c, emacs.c, text.c, regex.c, scrollbar-msw.c, search.c: Fix evil crashes due to eistrings not properly reinitialized under pdump. Redo a bit some of the init routines; convert some complex_vars_of() into simple vars_of(), because they didn't need complex processing. callproc.c, emacs.c, event-stream.c, nt.c, process.c, process.h, sysdep.c, sysdep.h, syssignal.h, syswindows.h, ntproc.c: Delete. Hallelujah, praise the Lord, there is no god but Allah!!! fix so that processes can be invoked in bare temacs -- thereby eliminating any need for callproc.c. (currently only eliminated under NT.) remove all crufty and unnecessary old process code in ntproc.c and elsewhere. move non-callproc-specific stuff (mostly environment) into process.c, so callproc.c can be left out under NT. console-tty.c, doc.c, file-coding.c, file-coding.h, lstream.c, lstream.h: fix doc string handling so it works with Japanese, etc docs. change handling of "character mode" so callers don't have to manually set it (quite error-prone). event-msw.c: spacing fixes. lread.c: eliminate unused crufty vintage-19 "FSF defun hack" code. lrecord.h: improve pdump description docs. buffer.c, ntheap.c, unexnt.c, win32.c, emacs.c: Mule-ize some unexec and startup code. It was pseudo-Mule-ized before by simply always calling the ...A versions of functions, but that won't cut it -- eventually we want to be able to run properly even if XEmacs has been installed in a Japanese directory. (The current problem is the timing of the loading of the Unicode tables; this will eventually be fixed.) Go through and fix various other places where the code was not Mule-clean. Provide a function mswindows_get_module_file_name() to get our own name without resort to PATH_MAX and such. Add a big comment in main() about the problem with Unicode table load timing that I just alluded to. emacs.c: When error-checking is enabled (interpreted as "user is developing XEmacs"), don't ask user to "pause to read messages" when a fatal error has occurred, because it will wedge if we are in an inner modal loop (typically when a menu is popped up) and make us unable to get a useful stack trace in the debugger. text.c: Correct update_entirely_ascii_p_flag to actually work. lisp.h, symsinit.h: declarations for above changes.
author ben
date Sun, 14 Apr 2002 12:43:31 +0000
parents 20ae8821c23d
children
line wrap: on
line source

/*
 *	Copyright (c) 1995 by Sun Microsystems, Inc.
 *	All rights reserved.
 *
 * This source code is a product of Sun Microsystems, Inc. and is provided
 * for unrestricted use provided that this legend is included on all tape
 * media and as a part of the software program in whole or part.  Users
 * may copy or modify this source code without charge, but are not authorized
 * to license or distribute it to anyone else except as part of a product or
 * program developed by the user.
 *
 * THIS PROGRAM CONTAINS SOURCE CODE COPYRIGHTED BY SUN MICROSYSTEMS, INC.
 * SUN MICROSYSTEMS, INC., MAKES NO REPRESENTATIONS ABOUT THE SUITABLITY
 * OF SUCH SOURCE CODE FOR ANY PURPOSE.  IT IS PROVIDED "AS IS" WITHOUT
 * EXPRESS OR IMPLIED WARRANTY OF ANY KIND.  SUN MICROSYSTEMS, INC. DISCLAIMS
 * ALL WARRANTIES WITH REGARD TO SUCH SOURCE CODE, INCLUDING ALL IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  IN
 * NO EVENT SHALL SUN MICROSYSTEMS, INC. BE LIABLE FOR ANY SPECIAL, INDIRECT,
 * INCIDENTAL, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
 * FROM USE OF SUCH SOURCE CODE, REGARDLESS OF THE THEORY OF LIABILITY.
 *
 * This source code is provided with no support and without any obligation on
 * the part of Sun Microsystems, Inc. to assist in its use, correction,
 * modification or enhancement.
 *
 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS
 * SOURCE CODE OR ANY PART THEREOF.
 *
 * Sun Microsystems, Inc.
 * 2550 Garcia Avenue
 * Mountain View, California 94043
 */

/*
 * dynodump(3x) dumps a running executable into a specified ELF file.  The new
 * file consists of the memory contents of the original file together with any
 * heap.  This heap is assigned to a new `.heap' section within the new file.
 *
 * The new file may be re-executed, and will contain any data modifications
 * made to the original image up until the time dynodump(3x) was called.
 *
 * The original image may have undergone relocations (performed by ld.so.1)
 * prior to control being transferred to the image.  These relocations will
 * reside as the data copied from the image.  To prevent subsequent executions
 * of the new image from undergoing the same relocations, any relocation entries
 * (besides copy or jump slot relocations) are nulled out.  Note that copy
 * relocations such as required for __iob must be reinitialized each time the
 * process starts, so it is not sufficient to simply null out the .dynamic
 * sections relocation information.  The effect of this is that if the new
 * image was bound to definitions in any shared object dependencies, then these
 * dependencies *must* reside in the same location as when dynodump(3x) was
 * called.  Any changes to the shared object dependencies of the new image, or
 * uses of such things as LD_PRELOAD, may result in the bindings encoded in the
 * image becoming invalid.
 *
 * The following flags modify the data of the image created:
 *
 *  RTLD_SAVREL	save the original relocation data.  Under this option any
 *		relocation offset is reset to contain the same data as was
 *		found in the images original file.
 *
 *		This option allows relocation information to be retained in the
 *		new image so that it may be re-executed when the new image is
 *		run.  This allows far greater flexibility as the new image can
 *		now take advantage of new shared objects.
 *
 *		Note. under this mechanism, any data item that undergoes
 *		relocation and is then further modified during the execution of
 *		the image before dynodump(3x) is called will lose the
 *		modification that occurred during the applications execution.
 *
 * N.B. The above commentary is not quite correct in the flags have been hardwired
 *      to RTLD_SAVREL.
 */
#pragma ident	"@(#) $Id: dynodump.c,v 1.9 2001/04/13 09:11:20 michaels Exp $ - SMI"

#define __EXTENSIONS__ 1

#include	<sys/param.h>
#include	<sys/procfs.h>
#include	<fcntl.h>
#include	<stdio.h>
#include	<libelf.h>
#include	<link.h>
#include	<stdlib.h>
#include	<string.h>
#include	<unistd.h>
#include	<errno.h>
#include	<malloc.h>
#include	"machdep.h"
#include	"_dynodump.h"

/*
 * Generic elf error message generator
 */
static int
elferr(const char * str)
{
    fprintf(stderr, "%s: %s\n", str, elf_errmsg(elf_errno()));
    return (1);
}

int dynodump (const char * file);
int
dynodump(const char * file)
{
    Elf		*ielf, *oelf;
    Ehdr	*iehdr, *oehdr;
    Phdr	*iphdr, *ophdr, *data_phdr = 0;
    Cache	*icache, *ocache, *_icache, *_ocache;
    Cache	*data_cache = 0, *shstr_cache = 0;
    Cache	*heap_cache = 0;
    Word	heap_sz = 0;
    Elf_Scn	*scn;
    Shdr	*shdr;
    Elf_Data	*data, rundata;
    Half	ndx, _ndx;
    int		fd, _fd;
    Addr	edata, _addr;
    char	*istrs, *ostrs, *_ostrs, proc[16];
    const char 	heap[] = ".heap";
    prstatus_t	pstat;

    /* make a call to the processor specific un-init stuff */
    dynodump_uninit();

    /*
     * Obtain a file descriptor for this process,
     * for the executable and get a prstatus_t
     * structure.
     */
    sprintf(proc, "/proc/%ld", getpid());
    if (((_fd = open(proc, O_RDONLY, 0)) == -1) ||
	((fd = ioctl(_fd, PIOCOPENM, (void *)0)) == -1) ||
	(ioctl(_fd, PIOCSTATUS, &pstat) == -1)) {
	fprintf(stderr, "/proc: initialization error: %s\n",
		strerror(errno));
	close(_fd);
	return (1);
    }
    close(_fd);

    /*
     * Initialize with the ELF library and make sure this is an executable
     * ELF file we're dealing with.
     */
    elf_version(EV_CURRENT);
    if ((ielf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
	close(fd);
	return (elferr("elf_begin"));
    }
    close(fd);

    if ((elf_kind(ielf) != ELF_K_ELF) ||
	((iehdr = elf_getehdr(ielf)) == NULL) ||
	(iehdr->e_type != ET_EXEC)) {
	fprintf(stderr, "image is not an ELF executable\n");
	elf_end(ielf);
	return (1);
    }
    /*
     * Elf_elf_header(iehdr);
     */

    /*
     * Create the new output file.
     */
    if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC, 0777)) == -1) {
	fprintf(stderr, "%s: open failed: %s\n", file,
		       strerror(errno));
	elf_end(ielf);
	return (1);
    }
    if ((oelf = elf_begin(fd, ELF_C_WRITE, NULL)) == NULL) {
	elf_end(ielf);
	close(fd);
	return (elferr("elf_begin"));
    }

    /*
     * Obtain the input program headers.  Remember the data segments
     * program header entry as this will be updated later to reflect the
     * new .heap sections size.
     */
    if ((iphdr = elf_getphdr(ielf)) == NULL)
	return (elferr("elf_getphdr"));

    for (ndx = 0, ophdr = iphdr; ndx != iehdr->e_phnum; ndx++, ophdr++) {
	/*
	 * Save the program header that contains the NOBITS section, or
	 * the last loadable program header if no NOBITS exists.
	 * A NOBITS section translates to a memory size requirement that
	 * is greater than the file data it is mapped from.
	 */
	if (ophdr->p_type == PT_LOAD) {
	    if (ophdr->p_filesz != ophdr->p_memsz)
		data_phdr = ophdr;
	    else if (data_phdr) {
		if (data_phdr->p_vaddr < ophdr->p_vaddr)
		    data_phdr = ophdr;
	    } else
		data_phdr = ophdr;
	}
    }
    if (data_phdr == 0) {
	fprintf(stderr, "no data segment found!\n");
	return (0);
    }

    /*
     * Obtain the input files section header string table.
     */
    if ((scn = elf_getscn(ielf, iehdr->e_shstrndx)) == NULL)
	return (elferr("elf_getscn"));
    if ((data = elf_getdata(scn, NULL)) == NULL)
	return (elferr("elf_getdata"));
    istrs = (char *) data->d_buf;

    /*
     * Construct a cache to maintain the input files section information.
     */
    if ((icache = (Cache *) malloc(iehdr->e_shnum * sizeof (Cache))) == 0) {
	fprintf(stderr, "malloc failed: %s\n", strerror(errno));
	return (1);
    }
    _icache = icache;
    _icache++;

    /*
     * Traverse each section from the input file.
     */
    for (ndx = 1, scn = 0;
	 (_icache->c_scn = elf_nextscn(ielf, scn));
	 ndx++, scn = _icache->c_scn, _icache++) {

	if ((_icache->c_shdr = shdr = elf_getshdr(_icache->c_scn)) == NULL)
	    return (elferr("elf_getshdr"));

	if ((_icache->c_data = elf_getdata(_icache->c_scn, NULL)) == NULL)
	    return (elferr("elf_getdata"));

	_icache->c_name = istrs + (size_t)(shdr->sh_name);

	/*
	 * For each section that has a virtual address reestablish the
	 * data buffer to point to the memory image.
	 *
	 * if (shdr->sh_addr)
	 *     _icache->c_data->d_buf = (void *)shdr->sh_addr;
	 */

	/*
	 * Remember the last section of the data segment, the new .heap
	 * section will be added after this section.
	 * If we already have one, then set data_cache to the previous
	 * section and set heap_cache to this one.
	 */
	if ((shdr->sh_addr + shdr->sh_size)
	    == (data_phdr->p_vaddr + data_phdr->p_memsz)) {
	    if (strcmp(_icache->c_name, heap) == 0) {
#ifdef DEBUG
		printf("Found a previous .heap section\n");
#endif
		data_cache = _icache - 1;
		heap_cache = _icache;
		heap_sz = shdr->sh_size;
	    } else {
		data_cache = _icache;
	    }
	}

	/*
	 * Remember the section header string table as this will be
	 * rewritten with the new .heap name.
	 */
	if ((shdr->sh_type == SHT_STRTAB) &&
	    ((strcmp(_icache->c_name, ".shstrtab")) == 0))
	    shstr_cache = _icache;
    }
    if (data_cache == 0) {
	fprintf(stderr, "final data section not found!\n");
	return (0);
    }

    /*
     * Determine the new .heap section to create.
     */
    rundata.d_buf = (void *)(data_cache->c_shdr->sh_addr +
			     data_cache->c_shdr->sh_size);
    rundata.d_size = (int)sbrk(0) - (int)rundata.d_buf;
    rundata.d_type = ELF_T_BYTE;
    rundata.d_off = 0;
    rundata.d_align = 1;
    rundata.d_version = EV_CURRENT;

    /*
     * From the new data buffer determine the new value for _end and _edata.
     * This will also be used to update the data segment program header.
     *
     * If we had a .heap section, then its size is part of the program
     * headers notion of data size.  Because we're only going to output one
     * heap section (ignoring the one in the running binary) we need to
     * subtract the size of that which we're ignoring.
     */
    if (heap_cache) {
	edata = S_ROUND((data_phdr->p_vaddr
			 + data_phdr->p_memsz
			 - heap_sz), rundata.d_align) + rundata.d_size;
    } else {
	edata = S_ROUND((data_phdr->p_vaddr + data_phdr->p_memsz),
			rundata.d_align) + rundata.d_size;
    }

    /*
     * We're now ready to construct the new elf image.
     *
     * Obtain a new elf header and initialize it with any basic information
     * that isn't calculated as part of elf_update().  Bump the section
     * header string table index to account for the .heap section we'll be
     * adding.
     */
    if ((oehdr = elf_newehdr(oelf)) == NULL)
	return (elferr("elf_newehdr"));

    oehdr->e_entry = iehdr->e_entry;
    oehdr->e_machine = iehdr->e_machine;
    oehdr->e_type = iehdr->e_type;
    oehdr->e_flags = iehdr->e_flags;
    /*
     * If we already have a heap section, we don't need any adjustment
     */
    if (heap_cache)
	oehdr->e_shstrndx = iehdr->e_shstrndx;
    else
	oehdr->e_shstrndx = iehdr->e_shstrndx + 1;

#ifdef DEBUG
    printf("iehdr->e_flags   = %x\n", iehdr->e_flags);
    printf("iehdr->e_entry   = %x\n", iehdr->e_entry);
    printf("iehdr->e_shstrndx= %d\n", iehdr->e_shstrndx);
    printf("iehdr->e_machine = %d\n", iehdr->e_machine);
    printf("iehdr->e_type    = 0x%x\n", iehdr->e_type);
    printf("oehdr->e_machine = %d\n", oehdr->e_machine);
    printf("oehdr->e_type    = 0x%x\n", oehdr->e_type);
#endif

    /*
     * Obtain a new set of program headers.  Initialize these with the same
     * information as the input program headers and update the data segment
     * to reflect the new .heap section.
     */
    if ((ophdr = elf_newphdr(oelf, iehdr->e_phnum)) == NULL)
	return (elferr("elf_newphdr"));

    for (ndx = 0; ndx != iehdr->e_phnum; ndx++, iphdr++, ophdr++) {
	*ophdr = *iphdr;
	if (data_phdr == iphdr)
	    ophdr->p_filesz = ophdr->p_memsz = edata - ophdr->p_vaddr;
    }

    /*
     * Obtain a new set of sections.
     */
    _icache = icache;
    _icache++;
    for (ndx = 1; ndx != iehdr->e_shnum; ndx++, _icache++) {
	/*
	 * Skip the heap section of the running executable
	 */
	if (_icache == heap_cache)
	    continue;
	/*
	 * Create a matching section header in the output file.
	 */
	if ((scn = elf_newscn(oelf)) == NULL)
	    return (elferr("elf_newscn"));
	if ((shdr = elf_getshdr(scn)) == NULL)
	    return (elferr("elf_getshdr"));
	*shdr = *_icache->c_shdr;

	/*
	 * Create a matching data buffer for this section.
	 */
	if ((data = elf_newdata(scn)) == NULL)
	    return (elferr("elf_newdata"));
	*data = *_icache->c_data;

	/*
	 * For each section that has a virtual address reestablish the
	 * data buffer to point to the memory image.  Note, we skip
	 * the plt section.
	 */
	if ((shdr->sh_addr) && (!((shdr->sh_type == SHT_PROGBITS)
				  && (strcmp(_icache->c_name, ".plt") == 0))))
	    data->d_buf = (void *)shdr->sh_addr;

	/*
	 * Update any NOBITS section to indicate that it now contains
	 * data.
	 */
	if (shdr->sh_type == SHT_NOBITS)
	    shdr->sh_type = SHT_PROGBITS;

	/*
	 * Add the new .heap section after the last section of the
	 * present data segment.  If we had a heap section, then
	 * this is the section preceding it.
	 */
	if (data_cache == _icache) {
	    if ((scn = elf_newscn(oelf)) == NULL)
		return (elferr("elf_newscn"));
	    if ((shdr = elf_getshdr(scn)) == NULL)
		return (elferr("elf_getshdr"));
	    shdr->sh_type = SHT_PROGBITS;
	    shdr->sh_flags = SHF_ALLOC | SHF_WRITE;

	    if ((data = elf_newdata(scn)) == NULL)
		return (elferr("elf_newdata"));
	    *data = rundata;
	}

	/*
	 * Update the section header string table size to reflect the
	 * new section name (only if we didn't already have a heap).
	 */
	if (!heap_cache) {
	    if (shstr_cache && (shstr_cache == _icache)) {
		data->d_size += sizeof (heap);
	    }
	}
    }

    /*
     * Write out the new image, and obtain a new elf descriptor that will
     * allow us to write to the new image.
     */
    if (elf_update(oelf, ELF_C_WRITE) == -1)
	return (elferr("elf_update"));
    elf_end(oelf);
    if ((oelf = elf_begin(fd, ELF_C_RDWR, NULL)) == NULL)
	return (elferr("elf_begin"));
    if ((oehdr = elf_getehdr(oelf)) == NULL)
	return (elferr("elf_getehdr"));

    /*
     * Obtain the output files section header string table.
     */
    if ((scn = elf_getscn(oelf, oehdr->e_shstrndx)) == NULL)
	return (elferr("elf_getscn"));
    if ((data = elf_getdata(scn, NULL)) == NULL)
	return (elferr("elf_getdata"));
    ostrs = _ostrs = (char *) data->d_buf;
    *_ostrs++ = '\0';

    /*
     * Construct a cache to maintain the output files section information.
     */
    if ((ocache = (Cache *)malloc(oehdr->e_shnum * sizeof (Cache))) == 0) {
	fprintf(stderr, "malloc failed: %s\n", strerror(errno));
	return (1);
    }
    _ocache = ocache;
    _ocache++;
    _icache = icache;
    _icache++;

    /*
     * Traverse each section from the input file rebuilding the section
     * header string table as we go.
     */
    _ndx = _addr = 0;
    for (ndx = 1, scn = 0;
	 (_ocache->c_scn = elf_nextscn(oelf, scn));
	 ndx++, scn = _ocache->c_scn, _ocache++, _icache++) {

	const char *strs;

	if (_icache == heap_cache) {
#ifdef DEBUG
	    printf("ignoring .heap section in input\n");
#endif
	    _icache++;
	}

	if ((_ocache->c_shdr = shdr =
	     elf_getshdr(_ocache->c_scn)) == NULL)
	    return (elferr("elf_getshdr"));
	if ((_ocache->c_data =
	     elf_getdata(_ocache->c_scn, NULL)) == NULL)
	    return (elferr("elf_getdata"));

	/*
	 * If were inserting the new .heap section, insert the new
	 * section name and initialize its virtual address.
	 */
	if (_addr) {
	    strs = heap;
	    shdr->sh_addr = S_ROUND(_addr, shdr->sh_addralign);
	    _addr = 0;
	} else {
	    strs = istrs + (size_t)(_icache->c_shdr->sh_name);
	}

	strcpy(_ostrs, strs);
	shdr->sh_name = _ostrs - ostrs;
	_ocache->c_name = _ostrs;
	_ostrs += strlen(strs) + 1;

	/*
	 * If we've inserted a new section any later section may need
	 * their sh_link fields updated.
	 * If we already had a heap section, then this is not required.
	 */
	if (!heap_cache) {
	    if (_ndx) {
		if (_ocache->c_shdr->sh_link >= _ndx)
		    _ocache->c_shdr->sh_link++;
	    }
	}

	/*
	 * If this is the last section of the original data segment
	 * determine sufficient information to initialize the new .heap
	 * section which will be obtained next.
	 */
	if (data_cache == _icache) {
	    _ndx = ndx + 1;
	    _addr = shdr->sh_addr + shdr->sh_size;
	    _icache--;
	    data_cache = 0;
	}
    }

    /*
     * Now that we have a complete description of the new image update any
     * sections that are required.
     *
     *  o	update the value of _edata and _end.
     *
     *  o	reset any relocation entries if necessary.
     */
    _ocache = &ocache[1];
    _icache = &icache[1];
    for (ndx = 1; ndx < oehdr->e_shnum; ndx++, _ocache++, _icache++) {
	if ((_ocache->c_shdr->sh_type == SHT_SYMTAB) ||
	    (_ocache->c_shdr->sh_type == SHT_DYNSYM))
	    update_sym(ocache, _ocache, edata);

	if (_ocache->c_shdr->sh_type == M_REL_SHT_TYPE)
	    update_reloc(ocache, _ocache, icache, _icache, oehdr->e_shnum);
    }

    if (elf_update(oelf, ELF_C_WRITE) == -1)
	return (elferr("elf_update"));

    elf_end(oelf);
    elf_end(ielf);
    return (0);
}