annotate dynodump/dynodump.c @ 119:d101af7320b8

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