annotate dynodump/dynodump.c @ 351:1057b58c392a r21-1-5

Import from CVS: tag r21-1-5
author cvs
date Mon, 13 Aug 2007 10:54:53 +0200
parents c5d627a313b1
children a4f53d9b3154
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
153
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
1 /*
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
2 * Copyright (c) 1995 by Sun Microsystems, Inc.
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
3 * All rights reserved.
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
4 *
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
5 * This source code is a product of Sun Microsystems, Inc. and is provided
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
6 * for unrestricted use provided that this legend is included on all tape
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
7 * media and as a part of the software program in whole or part. Users
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
8 * may copy or modify this source code without charge, but are not authorized
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
9 * to license or distribute it to anyone else except as part of a product or
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
10 * program developed by the user.
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
11 *
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
12 * THIS PROGRAM CONTAINS SOURCE CODE COPYRIGHTED BY SUN MICROSYSTEMS, INC.
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
13 * SUN MICROSYSTEMS, INC., MAKES NO REPRESENTATIONS ABOUT THE SUITABLITY
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
14 * OF SUCH SOURCE CODE FOR ANY PURPOSE. IT IS PROVIDED "AS IS" WITHOUT
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
15 * EXPRESS OR IMPLIED WARRANTY OF ANY KIND. SUN MICROSYSTEMS, INC. DISCLAIMS
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
16 * ALL WARRANTIES WITH REGARD TO SUCH SOURCE CODE, INCLUDING ALL IMPLIED
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
18 * NO EVENT SHALL SUN MICROSYSTEMS, INC. BE LIABLE FOR ANY SPECIAL, INDIRECT,
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
19 * INCIDENTAL, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
20 * FROM USE OF SUCH SOURCE CODE, REGARDLESS OF THE THEORY OF LIABILITY.
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 185
diff changeset
21 *
153
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
22 * This source code is provided with no support and without any obligation on
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 185
diff changeset
23 * the part of Sun Microsystems, Inc. to assist in its use, correction,
153
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
24 * modification or enhancement.
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
25 *
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
26 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
27 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
28 * SOURCE CODE OR ANY PART THEREOF.
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
29 *
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
30 * Sun Microsystems, Inc.
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
31 * 2550 Garcia Avenue
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
32 * Mountain View, California 94043
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
33 */
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
34
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
35 /*
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
36 * dynodump(3x) dumps a running executable into a specified ELF file. The new
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
37 * file consists of the memory contents of the original file together with any
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
38 * heap. This heap is assigned to a new `.heap' section within the new file.
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
39 *
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
40 * The new file may be re-executed, and will contain any data modifications
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
41 * made to the original image up until the time dynodump(3x) was called.
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
42 *
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
43 * The original image may have undergone relocations (performed by ld.so.1)
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
44 * prior to control being transferred to the image. These relocations will
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
45 * reside as the data copied from the image. To prevent subsequent executions
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
46 * of the new image from undergoing the same relocations, any relocation entries
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
47 * (besides copy or jump slot relocations) are nulled out. Note that copy
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
48 * relocations such as required for __iob must be reinitialized each time the
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
49 * process starts, so it is not sufficient to simply null out the .dynamic
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
50 * sections relocation information. The effect of this is that if the new
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
51 * image was bound to definitions in any shared object dependencies, then these
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
52 * dependencies *must* reside in the same location as when dynodump(3x) was
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
53 * called. Any changes to the shared object dependencies of the new image, or
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
54 * uses of such things as LD_PRELOAD, may result in the bindings encoded in the
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
55 * image becoming invalid.
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
56 *
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
57 * The following flags modify the data of the image created:
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
58 *
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
59 * RTLD_SAVREL save the original relocation data. Under this option any
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
60 * relocation offset is reset to contain the same data as was
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
61 * found in the images original file.
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
62 *
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
63 * This option allows relocation information to be retained in the
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
64 * new image so that it may be re-executed when the new image is
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
65 * run. This allows far greater flexibility as the new image can
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
66 * now take advantage of new shared objects.
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
67 *
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
68 * Note. under this mechanism, any data item that undergoes
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
69 * relocation and is then further modified during the execution of
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
70 * the image before dynodump(3x) is called will lose the
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
71 * modification that occured during the applications execution.
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
72 *
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
73 * N.B. The above commentary is not quite correct in the flags have been hardwired
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
74 * to RTLD_SAVREL.
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
75 */
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 185
diff changeset
76 #pragma ident "@(#) $Id: dynodump.c,v 1.6 1998/03/31 20:10:55 steve Exp $ - SMI"
173
8eaf7971accc Import from CVS: tag r20-3b13
cvs
parents: 153
diff changeset
77
8eaf7971accc Import from CVS: tag r20-3b13
cvs
parents: 153
diff changeset
78 #define __EXTENSIONS__ 1
153
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
79
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
80 #include <sys/param.h>
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
81 #include <sys/procfs.h>
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
82 #include <fcntl.h>
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
83 #include <stdio.h>
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
84 #include <libelf.h>
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
85 #include <link.h>
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
86 #include <stdlib.h>
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
87 #include <string.h>
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
88 #include <unistd.h>
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
89 #include <errno.h>
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
90 #include <malloc.h>
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
91 #include "machdep.h"
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
92 #include "_dynodump.h"
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
93
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
94 /*
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
95 * Generic elf error message generator
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
96 */
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
97 static int
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
98 elferr(const char * str)
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
99 {
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
100 fprintf(stderr, "%s: %s\n", str, elf_errmsg(elf_errno()));
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
101 return (1);
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
102 }
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
103
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
104 int dynodump (const char * file);
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
105 int
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
106 dynodump(const char * file)
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
107 {
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
108 Elf *ielf, *oelf;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
109 Ehdr *iehdr, *oehdr;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
110 Phdr *iphdr, *ophdr, *data_phdr = 0;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
111 Cache *icache, *ocache, *_icache, *_ocache;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
112 Cache *data_cache = 0, *shstr_cache = 0;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
113 Cache *heap_cache = 0;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
114 Word heap_sz = 0;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
115 Elf_Scn *scn;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
116 Shdr *shdr;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
117 Elf_Data *data, rundata;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
118 Half ndx, _ndx;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
119 int fd, _fd;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
120 Addr edata, _addr;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
121 char *istrs, *ostrs, *_ostrs, proc[16];
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
122 const char heap[] = ".heap";
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
123 prstatus_t pstat;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
124
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
125 /* make a call to the processor specific un-init stuff */
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
126 dynodump_uninit();
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
127
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
128 /*
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
129 * Obtain a file descriptor for this process,
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
130 * for the executable and get a prstatus_t
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
131 * structure.
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
132 */
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
133 sprintf(proc, "/proc/%ld", getpid());
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
134 if (((_fd = open(proc, O_RDONLY, 0)) == -1) ||
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
135 ((fd = ioctl(_fd, PIOCOPENM, (void *)0)) == -1) ||
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
136 (ioctl(_fd, PIOCSTATUS, &pstat) == -1)) {
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
137 fprintf(stderr, "/proc: initialization error: %s\n",
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
138 strerror(errno));
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
139 close(_fd);
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
140 return (1);
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
141 }
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
142 close(_fd);
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
143
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
144 /*
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
145 * Initialize with the ELF library and make sure this is an executable
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
146 * ELF file we're dealing with.
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
147 */
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
148 elf_version(EV_CURRENT);
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
149 if ((ielf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
150 close(fd);
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
151 return (elferr("elf_begin"));
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
152 }
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
153 close(fd);
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
154
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
155 if ((elf_kind(ielf) != ELF_K_ELF) ||
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
156 ((iehdr = elf_getehdr(ielf)) == NULL) ||
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
157 (iehdr->e_type != ET_EXEC)) {
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
158 fprintf(stderr, "image is not an ELF executable\n");
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
159 elf_end(ielf);
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
160 return (1);
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
161 }
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
162 /*
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
163 * Elf_elf_header(iehdr);
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
164 */
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
165
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
166 /*
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
167 * Create the new output file.
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
168 */
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
169 if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC, 0777)) == -1) {
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
170 fprintf(stderr, "%s: open failed: %s\n", file,
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
171 strerror(errno));
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
172 elf_end(ielf);
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
173 return (1);
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
174 }
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
175 if ((oelf = elf_begin(fd, ELF_C_WRITE, NULL)) == NULL) {
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
176 elf_end(ielf);
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
177 close(fd);
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
178 return (elferr("elf_begin"));
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
179 }
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
180
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
181 /*
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
182 * Obtain the input program headers. Remember the data segments
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
183 * program header entry as this will be updated later to reflect the
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
184 * new .heap sections size.
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
185 */
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
186 if ((iphdr = elf_getphdr(ielf)) == NULL)
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
187 return (elferr("elf_getphdr"));
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
188
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
189 for (ndx = 0, ophdr = iphdr; ndx != iehdr->e_phnum; ndx++, ophdr++) {
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
190 /*
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
191 * Save the program header that contains the NOBITS section, or
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
192 * the last loadable program header if no NOBITS exists.
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
193 * A NOBITS section translates to a memory size requirement that
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
194 * is greater than the file data it is mapped from.
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
195 */
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
196 if (ophdr->p_type == PT_LOAD) {
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
197 if (ophdr->p_filesz != ophdr->p_memsz)
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
198 data_phdr = ophdr;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
199 else if (data_phdr) {
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
200 if (data_phdr->p_vaddr < ophdr->p_vaddr)
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
201 data_phdr = ophdr;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
202 } else
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
203 data_phdr = ophdr;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
204 }
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
205 }
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
206 if (data_phdr == 0) {
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
207 fprintf(stderr, "no data segment found!\n");
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
208 return (0);
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
209 }
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
210
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
211 /*
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
212 * Obtain the input files section header string table.
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
213 */
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
214 if ((scn = elf_getscn(ielf, iehdr->e_shstrndx)) == NULL)
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
215 return (elferr("elf_getscn"));
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
216 if ((data = elf_getdata(scn, NULL)) == NULL)
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
217 return (elferr("elf_getdata"));
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 173
diff changeset
218 istrs = (char *) data->d_buf;
153
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
219
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
220 /*
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
221 * Construct a cache to maintain the input files section information.
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
222 */
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
223 if ((icache = (Cache *) malloc(iehdr->e_shnum * sizeof (Cache))) == 0) {
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
224 fprintf(stderr, "malloc failed: %s\n", strerror(errno));
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
225 return (1);
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
226 }
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
227 _icache = icache;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
228 _icache++;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
229
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
230 /*
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
231 * Traverse each section from the input file.
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
232 */
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
233 for (ndx = 1, scn = 0;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
234 (_icache->c_scn = elf_nextscn(ielf, scn));
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
235 ndx++, scn = _icache->c_scn, _icache++) {
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
236
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
237 if ((_icache->c_shdr = shdr = elf_getshdr(_icache->c_scn)) == NULL)
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
238 return (elferr("elf_getshdr"));
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
239
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
240 if ((_icache->c_data = elf_getdata(_icache->c_scn, NULL)) == NULL)
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
241 return (elferr("elf_getdata"));
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
242
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
243 _icache->c_name = istrs + (size_t)(shdr->sh_name);
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
244
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
245 /*
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
246 * For each section that has a virtual address reestablish the
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
247 * data buffer to point to the memory image.
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
248 *
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
249 * if (shdr->sh_addr)
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
250 * _icache->c_data->d_buf = (void *)shdr->sh_addr;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
251 */
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
252
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
253 /*
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
254 * Remember the last section of the data segment, the new .heap
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
255 * section will be added after this section.
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
256 * If we already have one, then set data_cache to the previous
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
257 * section and set heap_cache to this one.
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
258 */
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
259 if ((shdr->sh_addr + shdr->sh_size)
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
260 == (data_phdr->p_vaddr + data_phdr->p_memsz)) {
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
261 if (strcmp(_icache->c_name, heap) == 0) {
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
262 #ifdef DEBUG
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
263 printf("Found a previous .heap section\n");
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
264 #endif
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
265 data_cache = _icache - 1;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
266 heap_cache = _icache;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
267 heap_sz = shdr->sh_size;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
268 } else {
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
269 data_cache = _icache;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
270 }
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
271 }
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
272
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
273 /*
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
274 * Remember the section header string table as this will be
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
275 * rewritten with the new .heap name.
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
276 */
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
277 if ((shdr->sh_type == SHT_STRTAB) &&
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
278 ((strcmp(_icache->c_name, ".shstrtab")) == 0))
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
279 shstr_cache = _icache;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
280 }
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
281 if (data_cache == 0) {
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
282 fprintf(stderr, "final data section not found!\n");
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
283 return (0);
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
284 }
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
285
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
286 /*
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
287 * Determine the new .heap section to create.
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
288 */
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
289 rundata.d_buf = (void *)(data_cache->c_shdr->sh_addr +
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
290 data_cache->c_shdr->sh_size);
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
291 rundata.d_size = (int)sbrk(0) - (int)rundata.d_buf;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
292 rundata.d_type = ELF_T_BYTE;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
293 rundata.d_off = 0;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
294 rundata.d_align = 1;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
295 rundata.d_version = EV_CURRENT;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
296
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
297 /*
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
298 * From the new data buffer determine the new value for _end and _edata.
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
299 * This will also be used to update the data segment program header.
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
300 *
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
301 * If we had a .heap section, then its size is part of the program
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
302 * headers notion of data size. Because we're only going to output one
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
303 * heap section (ignoring the one in the running binary) we need to
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
304 * subract the size of that which we're ignoring.
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
305 */
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
306 if (heap_cache) {
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
307 edata = S_ROUND((data_phdr->p_vaddr
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
308 + data_phdr->p_memsz
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
309 - heap_sz), rundata.d_align) + rundata.d_size;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
310 } else {
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
311 edata = S_ROUND((data_phdr->p_vaddr + data_phdr->p_memsz),
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
312 rundata.d_align) + rundata.d_size;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
313 }
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
314
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
315 /*
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
316 * We're now ready to construct the new elf image.
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
317 *
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
318 * Obtain a new elf header and initialize it with any basic information
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
319 * that isn't calculated as part of elf_update(). Bump the section
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
320 * header string table index to account for the .heap section we'll be
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
321 * adding.
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
322 */
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
323 if ((oehdr = elf_newehdr(oelf)) == NULL)
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
324 return (elferr("elf_newehdr"));
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
325
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
326 oehdr->e_entry = iehdr->e_entry;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
327 oehdr->e_machine = iehdr->e_machine;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
328 oehdr->e_type = iehdr->e_type;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
329 oehdr->e_flags = iehdr->e_flags;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
330 /*
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
331 * If we already have a heap section, we don't need any adjustment
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
332 */
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
333 if (heap_cache)
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
334 oehdr->e_shstrndx = iehdr->e_shstrndx;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
335 else
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
336 oehdr->e_shstrndx = iehdr->e_shstrndx + 1;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
337
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
338 #ifdef DEBUG
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
339 printf("iehdr->e_flags = %x\n", iehdr->e_flags);
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
340 printf("iehdr->e_entry = %x\n", iehdr->e_entry);
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
341 printf("iehdr->e_shstrndx= %d\n", iehdr->e_shstrndx);
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
342 printf("iehdr->e_machine = %d\n", iehdr->e_machine);
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
343 printf("iehdr->e_type = 0x%x\n", iehdr->e_type);
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
344 printf("oehdr->e_machine = %d\n", oehdr->e_machine);
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
345 printf("oehdr->e_type = 0x%x\n", oehdr->e_type);
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
346 #endif
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
347
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
348 /*
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
349 * Obtain a new set of program headers. Initialize these with the same
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
350 * information as the input program headers and update the data segment
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
351 * to reflect the new .heap section.
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
352 */
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
353 if ((ophdr = elf_newphdr(oelf, iehdr->e_phnum)) == NULL)
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
354 return (elferr("elf_newphdr"));
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
355
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
356 for (ndx = 0; ndx != iehdr->e_phnum; ndx++, iphdr++, ophdr++) {
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
357 *ophdr = *iphdr;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
358 if (data_phdr == iphdr)
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
359 ophdr->p_filesz = ophdr->p_memsz = edata - ophdr->p_vaddr;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
360 }
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
361
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
362 /*
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
363 * Obtain a new set of sections.
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
364 */
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
365 _icache = icache;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
366 _icache++;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
367 for (ndx = 1; ndx != iehdr->e_shnum; ndx++, _icache++) {
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
368 /*
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
369 * Skip the heap section of the running executable
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
370 */
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
371 if (_icache == heap_cache)
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
372 continue;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
373 /*
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
374 * Create a matching section header in the output file.
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
375 */
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
376 if ((scn = elf_newscn(oelf)) == NULL)
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
377 return (elferr("elf_newscn"));
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
378 if ((shdr = elf_getshdr(scn)) == NULL)
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
379 return (elferr("elf_getshdr"));
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
380 *shdr = *_icache->c_shdr;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
381
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
382 /*
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
383 * Create a matching data buffer for this section.
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
384 */
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
385 if ((data = elf_newdata(scn)) == NULL)
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
386 return (elferr("elf_newdata"));
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
387 *data = *_icache->c_data;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
388
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
389 /*
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
390 * For each section that has a virtual address reestablish the
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
391 * data buffer to point to the memory image. Note, we skip
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
392 * the plt section.
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
393 */
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
394 if ((shdr->sh_addr) && (!((shdr->sh_type == SHT_PROGBITS)
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
395 && (strcmp(_icache->c_name, ".plt") == 0))))
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
396 data->d_buf = (void *)shdr->sh_addr;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
397
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
398 /*
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
399 * Update any NOBITS section to indicate that it now contains
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
400 * data.
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
401 */
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
402 if (shdr->sh_type == SHT_NOBITS)
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
403 shdr->sh_type = SHT_PROGBITS;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
404
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
405 /*
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
406 * Add the new .heap section after the last section of the
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
407 * present data segment. If we had a heap section, then
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
408 * this is the section preceding it.
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
409 */
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
410 if (data_cache == _icache) {
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
411 if ((scn = elf_newscn(oelf)) == NULL)
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
412 return (elferr("elf_newscn"));
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
413 if ((shdr = elf_getshdr(scn)) == NULL)
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
414 return (elferr("elf_getshdr"));
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
415 shdr->sh_type = SHT_PROGBITS;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
416 shdr->sh_flags = SHF_ALLOC | SHF_WRITE;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
417
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
418 if ((data = elf_newdata(scn)) == NULL)
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
419 return (elferr("elf_newdata"));
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
420 *data = rundata;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
421 }
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
422
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
423 /*
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
424 * Update the section header string table size to reflect the
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
425 * new section name (only if we didn't already have a heap).
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
426 */
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
427 if (!heap_cache) {
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
428 if (shstr_cache && (shstr_cache == _icache)) {
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
429 data->d_size += sizeof (heap);
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
430 }
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
431 }
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
432 }
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
433
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
434 /*
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
435 * Write out the new image, and obtain a new elf descriptor that will
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
436 * allow us to write to the new image.
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
437 */
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
438 if (elf_update(oelf, ELF_C_WRITE) == -1)
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
439 return (elferr("elf_update"));
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
440 elf_end(oelf);
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
441 if ((oelf = elf_begin(fd, ELF_C_RDWR, NULL)) == NULL)
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
442 return (elferr("elf_begin"));
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
443 if ((oehdr = elf_getehdr(oelf)) == NULL)
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
444 return (elferr("elf_getehdr"));
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
445
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
446 /*
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
447 * Obtain the output files section header string table.
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
448 */
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
449 if ((scn = elf_getscn(oelf, oehdr->e_shstrndx)) == NULL)
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
450 return (elferr("elf_getscn"));
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
451 if ((data = elf_getdata(scn, NULL)) == NULL)
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
452 return (elferr("elf_getdata"));
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 173
diff changeset
453 ostrs = _ostrs = (char *) data->d_buf;
153
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
454 *_ostrs++ = '\0';
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
455
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
456 /*
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
457 * Construct a cache to maintain the output files section information.
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
458 */
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
459 if ((ocache = (Cache *)malloc(oehdr->e_shnum * sizeof (Cache))) == 0) {
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
460 fprintf(stderr, "malloc failed: %s\n", strerror(errno));
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
461 return (1);
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
462 }
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
463 _ocache = ocache;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
464 _ocache++;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
465 _icache = icache;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
466 _icache++;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
467
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
468 /*
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
469 * Traverse each section from the input file rebuilding the section
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
470 * header string table as we go.
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
471 */
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
472 _ndx = _addr = 0;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
473 for (ndx = 1, scn = 0;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
474 (_ocache->c_scn = elf_nextscn(oelf, scn));
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
475 ndx++, scn = _ocache->c_scn, _ocache++, _icache++) {
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
476
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
477 const char *strs;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
478
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
479 if (_icache == heap_cache) {
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
480 #ifdef DEBUG
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
481 printf("ignoring .heap section in input\n");
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
482 #endif
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
483 _icache++;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
484 }
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
485
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
486 if ((_ocache->c_shdr = shdr =
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
487 elf_getshdr(_ocache->c_scn)) == NULL)
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
488 return (elferr("elf_getshdr"));
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
489 if ((_ocache->c_data =
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
490 elf_getdata(_ocache->c_scn, NULL)) == NULL)
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
491 return (elferr("elf_getdata"));
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
492
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
493 /*
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
494 * If were inserting the new .heap section, insert the new
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 185
diff changeset
495 * section name and initialize its virtual address.
153
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
496 */
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
497 if (_addr) {
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
498 strs = heap;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
499 shdr->sh_addr = S_ROUND(_addr, shdr->sh_addralign);
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
500 _addr = 0;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
501 } else {
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
502 strs = istrs + (size_t)(_icache->c_shdr->sh_name);
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
503 }
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
504
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
505 strcpy(_ostrs, strs);
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
506 shdr->sh_name = _ostrs - ostrs;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
507 _ocache->c_name = _ostrs;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
508 _ostrs += strlen(strs) + 1;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
509
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
510 /*
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
511 * If we've inserted a new section any later section may need
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
512 * their sh_link fields updated.
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
513 * If we already had a heap section, then this is not required.
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
514 */
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
515 if (!heap_cache) {
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
516 if (_ndx) {
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
517 if (_ocache->c_shdr->sh_link >= _ndx)
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
518 _ocache->c_shdr->sh_link++;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
519 }
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
520 }
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
521
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
522 /*
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
523 * If this is the last section of the original data segment
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
524 * determine sufficient information to initialize the new .heap
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
525 * section which will be obtained next.
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
526 */
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
527 if (data_cache == _icache) {
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
528 _ndx = ndx + 1;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
529 _addr = shdr->sh_addr + shdr->sh_size;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
530 _icache--;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
531 data_cache = 0;
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
532 }
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
533 }
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
534
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
535 /*
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
536 * Now that we have a complete description of the new image update any
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
537 * sections that are required.
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
538 *
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
539 * o update the value of _edata and _end.
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
540 *
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
541 * o reset any relocation entries if necessary.
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
542 */
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
543 _ocache = &ocache[1];
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
544 _icache = &icache[1];
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
545 for (ndx = 1; ndx < oehdr->e_shnum; ndx++, _ocache++, _icache++) {
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
546 if ((_ocache->c_shdr->sh_type == SHT_SYMTAB) ||
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
547 (_ocache->c_shdr->sh_type == SHT_DYNSYM))
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
548 update_sym(ocache, _ocache, edata);
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
549
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
550 if (_ocache->c_shdr->sh_type == M_REL_SHT_TYPE)
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
551 update_reloc(ocache, _ocache, icache, _icache, oehdr->e_shnum);
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
552 }
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
553
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
554 if (elf_update(oelf, ELF_C_WRITE) == -1)
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
555 return (elferr("elf_update"));
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
556
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
557 elf_end(oelf);
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
558 elf_end(ielf);
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
559 return (0);
25f70ba0133c Import from CVS: tag r20-3b3
cvs
parents:
diff changeset
560 }