XEmacs 21.2.14.
[chise/xemacs-chise.git.1] / dynodump / i386 / _relocate.c
1 /*
2  *      Copyright (c) 1995 by Sun Microsystems, Inc.
3  *      All rights reserved.
4  *
5  * This source code is a product of Sun Microsystems, Inc. and is provided
6  * for unrestricted use provided that this legend is included on all tape
7  * media and as a part of the software program in whole or part.  Users
8  * may copy or modify this source code without charge, but are not authorized
9  * to license or distribute it to anyone else except as part of a product or
10  * program developed by the user.
11  *
12  * THIS PROGRAM CONTAINS SOURCE CODE COPYRIGHTED BY SUN MICROSYSTEMS, INC.
13  * SUN MICROSYSTEMS, INC., MAKES NO REPRESENTATIONS ABOUT THE SUITABLITY
14  * OF SUCH SOURCE CODE FOR ANY PURPOSE.  IT IS PROVIDED "AS IS" WITHOUT
15  * EXPRESS OR IMPLIED WARRANTY OF ANY KIND.  SUN MICROSYSTEMS, INC. DISCLAIMS
16  * ALL WARRANTIES WITH REGARD TO SUCH SOURCE CODE, INCLUDING ALL IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  IN
18  * NO EVENT SHALL SUN MICROSYSTEMS, INC. BE LIABLE FOR ANY SPECIAL, INDIRECT,
19  * INCIDENTAL, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
20  * FROM USE OF SUCH SOURCE CODE, REGARDLESS OF THE THEORY OF LIABILITY.
21  * 
22  * This source code is provided with no support and without any obligation on
23  * the part of Sun Microsystems, Inc. to assist in its use, correction, 
24  * modification or enhancement.
25  *
26  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
27  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS
28  * SOURCE CODE OR ANY PART THEREOF.
29  *
30  * Sun Microsystems, Inc.
31  * 2550 Garcia Avenue
32  * Mountain View, California 94043
33  */
34
35 #pragma ident   "@(#) $Id: _relocate.c,v 1.4 1995/06/26 20:12:41 georgn Exp $ - SMI"
36
37 /* LINTLIBRARY */
38
39 #include        <libelf.h>
40 #include        <string.h>
41 #include        <machdep.h>
42 #include        "_dynodump.h"
43
44 void
45 update_reloc(Cache *ocache, Cache *_ocache,
46              Cache *icache, Cache *_icache,
47              Half shnum)
48 {
49     Shdr *shdr;
50     Rel *rels;
51     int reln, cnt;
52     Cache *orcache, *ircache;
53
54     /*
55      * Set up to readh the output relocation table.
56      */
57     shdr = _ocache->c_shdr;
58     rels = (Rel *) _ocache->c_data->d_buf;
59     reln = shdr->sh_size / shdr->sh_entsize;
60
61     /*
62      * Determine the section that is being relocated.
63      */
64     orcache = &ocache[shdr->sh_info];
65     shdr = _icache->c_shdr;
66     ircache = &icache[shdr->sh_info];
67
68     /*
69      * Determine the section that is being relocated.  Note that for this
70      * stupid architecture the .rel.plt actually contains offsets into the
71      * .got.
72      */
73     if (strcmp(_ocache->c_name, ".rel.plt")) {
74         orcache = &ocache[shdr->sh_info];
75         shdr = _icache->c_shdr;
76         ircache = &icache[shdr->sh_info];
77     } else {
78         Half    ndx;
79         Cache * __ocache = ocache;
80
81         for (__ocache++, ndx = 1; ndx != shnum; ndx++, __ocache++) {
82             if (strcmp(__ocache->c_name, ".got") == 0) {
83                 orcache = __ocache;
84                 ircache = &icache[ndx];
85                 break;
86             }
87         }
88     }
89
90     /*
91      * Loop through the relocation table.
92      */
93     for (cnt = 0; cnt < reln; cnt++, rels++) {
94         unsigned char *iaddr, *oaddr;
95         Addr off;
96         unsigned char type = ELF_R_TYPE(rels->r_info);
97
98         /*
99          * Ignore some relocations as these can be safely carried out
100          * twice (they simply override any existing data).  In fact,
101          * some relocations like __iob's copy relocation must be carried
102          * out each time the process restarts, otherwise stdio blows up.
103          */
104         if ((type == R_386_COPY) || (type == R_386_NONE))
105             continue;
106
107         /*
108          * If we are required to restore the relocation location
109          * to its value prior to relocation, then read the
110          * location's original contents from the input image and
111          * copy it to the output image.
112          */
113         off = rels->r_offset - ircache->c_shdr->sh_addr;
114         iaddr = (unsigned char *) ircache->c_data->d_buf + off;
115         oaddr = (unsigned char *) orcache->c_data->d_buf + off;
116         *(unsigned long *) oaddr = *(unsigned long *) iaddr;
117     }
118 }