annotate dynodump/dynodump.c @ 0:376386a54a3c r19-14

Import from CVS: tag r19-14
author cvs
date Mon, 13 Aug 2007 08:45:50 +0200
parents
children ac2d302a0011
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 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 #pragma ident "@(#) $Id: dynodump.c,v 1.1.1.1 1996/12/18 03:37:22 steve Exp $ - SMI"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 #include <sys/param.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 #include <sys/procfs.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 #include <fcntl.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 #include <stdio.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 #include <libelf.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 #include <link.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 #include <stdlib.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 #include <string.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 #include <unistd.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 #include <errno.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 #include <malloc.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 #include "machdep.h"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 #include "_dynodump.h"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 * Generic elf error message generator
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 elferr(const char * str)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 fprintf(stderr, "%s: %s\n", str, elf_errmsg(elf_errno()));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 return (1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 dynodump(const char * file)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 Elf *ielf, *oelf;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 Ehdr *iehdr, *oehdr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 Phdr *iphdr, *ophdr, *data_phdr = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 Cache *icache, *ocache, *_icache, *_ocache;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 Cache *data_cache = 0, *shstr_cache = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 Cache *heap_cache = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 Word heap_sz = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 Elf_Scn *scn;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 Shdr *shdr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 Elf_Data *data, rundata;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 Half ndx, _ndx;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 int fd, _fd;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 Addr edata, _addr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 char *istrs, *ostrs, *_ostrs, proc[16];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 const char heap[] = ".heap";
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120 prstatus_t pstat;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 /* make a call to the processor specific un-init stuff */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 dynodump_uninit();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 * Obtain a file descriptor for this process,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 * for the executable and get a prstatus_t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 * structure.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 sprintf(proc, "/proc/%ld", getpid());
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 if (((_fd = open(proc, O_RDONLY, 0)) == -1) ||
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 ((fd = ioctl(_fd, PIOCOPENM, (void *)0)) == -1) ||
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 (ioctl(_fd, PIOCSTATUS, &pstat) == -1)) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 fprintf(stderr, "/proc: initialization error: %s\n",
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 strerror(errno));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 close(_fd);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 return (1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 close(_fd);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 * Initialize with the ELF library and make sure this is an executable
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 * ELF file we're dealing with.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 elf_version(EV_CURRENT);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 if ((ielf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 close(fd);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 return (elferr("elf_begin"));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150 close(fd);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152 if ((elf_kind(ielf) != ELF_K_ELF) ||
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 ((iehdr = elf_getehdr(ielf)) == NULL) ||
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154 (iehdr->e_type != ET_EXEC)) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 fprintf(stderr, "image is not an ELF executable\n");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156 elf_end(ielf);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157 return (1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 * Elf_elf_header(iehdr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164 * Create the new output file.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166 if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC, 0777)) == -1) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167 fprintf(stderr, "%s: open failed: %s\n", file,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168 strerror(errno));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169 elf_end(ielf);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170 return (1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 if ((oelf = elf_begin(fd, ELF_C_WRITE, NULL)) == NULL) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173 elf_end(ielf);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174 close(fd);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175 return (elferr("elf_begin"));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179 * Obtain the input program headers. Remember the data segments
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180 * program header entry as this will be updated later to reflect the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181 * new .heap sections size.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
183 if ((iphdr = elf_getphdr(ielf)) == NULL)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184 return (elferr("elf_getphdr"));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186 for (ndx = 0, ophdr = iphdr; ndx != iehdr->e_phnum; ndx++, ophdr++) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
187 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188 * Save the program header that contains the NOBITS section, or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
189 * the last loadable program header if no NOBITS exists.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190 * A NOBITS section translates to a memory size requirement that
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191 * is greater than the file data it is mapped from.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
193 if (ophdr->p_type == PT_LOAD) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194 if (ophdr->p_filesz != ophdr->p_memsz)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
195 data_phdr = ophdr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196 else if (data_phdr) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197 if (data_phdr->p_vaddr < ophdr->p_vaddr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198 data_phdr = ophdr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199 } else
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 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
202 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203 if (data_phdr == 0) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
204 fprintf(stderr, "no data segment found!\n");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205 return (0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
207
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209 * Obtain the input files section header string table.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211 if ((scn = elf_getscn(ielf, iehdr->e_shstrndx)) == NULL)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212 return (elferr("elf_getscn"));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213 if ((data = elf_getdata(scn, NULL)) == NULL)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214 return (elferr("elf_getdata"));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
215 istrs = data->d_buf;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218 * Construct a cache to maintain the input files section information.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220 if ((icache = (Cache *) malloc(iehdr->e_shnum * sizeof (Cache))) == 0) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221 fprintf(stderr, "malloc failed: %s\n", strerror(errno));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
222 return (1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
223 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
224 _icache = icache;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225 _icache++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
227 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
228 * Traverse each section from the input file.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
229 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
230 for (ndx = 1, scn = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
231 _icache->c_scn = elf_nextscn(ielf, scn);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
232 ndx++, scn = _icache->c_scn, _icache++) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
233
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
234 if ((_icache->c_shdr = shdr = elf_getshdr(_icache->c_scn)) == NULL)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
235 return (elferr("elf_getshdr"));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
236
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
237 if ((_icache->c_data = elf_getdata(_icache->c_scn, NULL)) == NULL)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
238 return (elferr("elf_getdata"));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
239
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
240 _icache->c_name = istrs + (size_t)(shdr->sh_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
241
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
242 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
243 * For each section that has a virtual address reestablish the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
244 * data buffer to point to the memory image.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
245 *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
246 * if (shdr->sh_addr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
247 * _icache->c_data->d_buf = (void *)shdr->sh_addr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
248 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
249
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
250 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
251 * Remember the last section of the data segment, the new .heap
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
252 * section will be added after this section.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
253 * If we already have one, then set data_cache to the previous
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
254 * section and set heap_cache to this one.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
255 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
256 if ((shdr->sh_addr + shdr->sh_size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
257 == (data_phdr->p_vaddr + data_phdr->p_memsz)) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
258 if (strcmp(_icache->c_name, heap) == 0) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
259 #ifdef DEBUG
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
260 printf("Found a previous .heap section\n");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
261 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
262 data_cache = _icache - 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
263 heap_cache = _icache;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
264 heap_sz = shdr->sh_size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
265 } else {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
266 data_cache = _icache;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
267 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
268 }
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 * Remember the section header string table as this will be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
272 * rewritten with the new .heap name.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
273 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
274 if ((shdr->sh_type == SHT_STRTAB) &&
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
275 ((strcmp(_icache->c_name, ".shstrtab")) == 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
276 shstr_cache = _icache;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
277 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
278 if (data_cache == 0) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
279 fprintf(stderr, "final data section not found!\n");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
280 return (0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
281 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
282
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
283 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
284 * Determine the new .heap section to create.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
285 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
286 rundata.d_buf = (void *)(data_cache->c_shdr->sh_addr +
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
287 data_cache->c_shdr->sh_size);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
288 rundata.d_size = (int)sbrk(0) - (int)rundata.d_buf;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
289 rundata.d_type = ELF_T_BYTE;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
290 rundata.d_off = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
291 rundata.d_align = 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
292 rundata.d_version = EV_CURRENT;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
293
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
294 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
295 * From the new data buffer determine the new value for _end and _edata.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
296 * This will also be used to update the data segment program header.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
297 *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
298 * 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
299 * headers notion of data size. Because we're only going to output one
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
300 * heap section (ignoring the one in the running binary) we need to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
301 * subract the size of that which we're ignoring.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
302 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
303 if (heap_cache) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
304 edata = S_ROUND((data_phdr->p_vaddr
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
305 + data_phdr->p_memsz
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
306 - heap_sz), rundata.d_align) + rundata.d_size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
307 } else {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
308 edata = S_ROUND((data_phdr->p_vaddr + data_phdr->p_memsz),
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
309 rundata.d_align) + rundata.d_size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
310 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
311
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
312 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
313 * We're now ready to construct the new elf image.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
314 *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
315 * Obtain a new elf header and initialize it with any basic information
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
316 * that isn't calculated as part of elf_update(). Bump the section
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
317 * header string table index to account for the .heap section we'll be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
318 * adding.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
319 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
320 if ((oehdr = elf_newehdr(oelf)) == NULL)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
321 return (elferr("elf_newehdr"));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
322
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
323 oehdr->e_entry = iehdr->e_entry;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
324 oehdr->e_machine = iehdr->e_machine;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
325 oehdr->e_type = iehdr->e_type;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
326 oehdr->e_flags = iehdr->e_flags;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
327 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
328 * If we already have a heap section, we don't need any adjustment
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 (heap_cache)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
331 oehdr->e_shstrndx = iehdr->e_shstrndx;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
332 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
333 oehdr->e_shstrndx = iehdr->e_shstrndx + 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
334
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
335 #ifdef DEBUG
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
336 printf("iehdr->e_flags = %x\n", iehdr->e_flags);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
337 printf("iehdr->e_entry = %x\n", iehdr->e_entry);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
338 printf("iehdr->e_shstrndx= %d\n", iehdr->e_shstrndx);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
339 printf("iehdr->e_machine = %d\n", iehdr->e_machine);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
340 printf("iehdr->e_type = 0x%x\n", iehdr->e_type);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
341 printf("oehdr->e_machine = %d\n", oehdr->e_machine);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
342 printf("oehdr->e_type = 0x%x\n", oehdr->e_type);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
343 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
344
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
345 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
346 * Obtain a new set of program headers. Initialize these with the same
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
347 * information as the input program headers and update the data segment
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
348 * to reflect the new .heap section.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
349 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
350 if ((ophdr = elf_newphdr(oelf, iehdr->e_phnum)) == NULL)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
351 return (elferr("elf_newphdr"));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
352
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
353 for (ndx = 0; ndx != iehdr->e_phnum; ndx++, iphdr++, ophdr++) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
354 *ophdr = *iphdr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
355 if (data_phdr == iphdr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
356 ophdr->p_filesz = ophdr->p_memsz = edata - ophdr->p_vaddr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
357 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
358
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
359 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
360 * Obtain a new set of sections.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
361 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
362 _icache = icache;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
363 _icache++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
364 for (ndx = 1; ndx != iehdr->e_shnum; ndx++, _icache++) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
365 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
366 * Skip the heap section of the running executable
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
367 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
368 if (_icache == heap_cache)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
369 continue;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
370 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
371 * Create a matching section header in the output file.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
372 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
373 if ((scn = elf_newscn(oelf)) == NULL)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
374 return (elferr("elf_newscn"));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
375 if ((shdr = elf_getshdr(scn)) == NULL)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
376 return (elferr("elf_getshdr"));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
377 *shdr = *_icache->c_shdr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
378
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
379 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
380 * Create a matching data buffer for this section.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
381 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
382 if ((data = elf_newdata(scn)) == NULL)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
383 return (elferr("elf_newdata"));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
384 *data = *_icache->c_data;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
385
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
386 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
387 * For each section that has a virtual address reestablish the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
388 * data buffer to point to the memory image. Note, we skip
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
389 * the plt section.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
390 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
391 if ((shdr->sh_addr) && (!((shdr->sh_type == SHT_PROGBITS)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
392 && (strcmp(_icache->c_name, ".plt") == 0))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
393 data->d_buf = (void *)shdr->sh_addr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
394
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
395 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
396 * Update any NOBITS section to indicate that it now contains
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
397 * data.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
398 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
399 if (shdr->sh_type == SHT_NOBITS)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
400 shdr->sh_type = SHT_PROGBITS;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
401
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
402 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
403 * Add the new .heap section after the last section of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
404 * present data segment. If we had a heap section, then
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
405 * this is the section preceding it.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
406 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
407 if (data_cache == _icache) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
408 if ((scn = elf_newscn(oelf)) == NULL)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
409 return (elferr("elf_newscn"));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
410 if ((shdr = elf_getshdr(scn)) == NULL)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
411 return (elferr("elf_getshdr"));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
412 shdr->sh_type = SHT_PROGBITS;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
413 shdr->sh_flags = SHF_ALLOC | SHF_WRITE;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
414
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
415 if ((data = elf_newdata(scn)) == NULL)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
416 return (elferr("elf_newdata"));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
417 *data = rundata;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
418 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
419
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
420 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
421 * Update the section header string table size to reflect the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
422 * new section name (only if we didn't already have a heap).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
423 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
424 if (!heap_cache) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
425 if (shstr_cache && (shstr_cache == _icache)) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
426 data->d_size += sizeof (heap);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
427 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
428 }
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 * Write out the new image, and obtain a new elf descriptor that will
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
433 * allow us to write to the new image.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
434 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
435 if (elf_update(oelf, ELF_C_WRITE) == -1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
436 return (elferr("elf_update"));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
437 elf_end(oelf);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
438 if ((oelf = elf_begin(fd, ELF_C_RDWR, NULL)) == NULL)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
439 return (elferr("elf_begin"));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
440 if ((oehdr = elf_getehdr(oelf)) == NULL)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
441 return (elferr("elf_getehdr"));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
442
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
443 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
444 * Obtain the output files section header string table.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
445 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
446 if ((scn = elf_getscn(oelf, oehdr->e_shstrndx)) == NULL)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
447 return (elferr("elf_getscn"));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
448 if ((data = elf_getdata(scn, NULL)) == NULL)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
449 return (elferr("elf_getdata"));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
450 ostrs = _ostrs = data->d_buf;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
451 *_ostrs++ = '\0';
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
452
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
453 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
454 * Construct a cache to maintain the output files section information.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
455 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
456 if ((ocache = (Cache *)malloc(oehdr->e_shnum * sizeof (Cache))) == 0) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
457 fprintf(stderr, "malloc failed: %s\n", strerror(errno));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
458 return (1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
459 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
460 _ocache = ocache;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
461 _ocache++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
462 _icache = icache;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
463 _icache++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
464
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
465 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
466 * Traverse each section from the input file rebuilding the section
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
467 * header string table as we go.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
468 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
469 _ndx = _addr = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
470 for (ndx = 1, scn = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
471 _ocache->c_scn = elf_nextscn(oelf, scn);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
472 ndx++, scn = _ocache->c_scn, _ocache++, _icache++) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
473
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
474 const char *strs;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
475
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
476 if (_icache == heap_cache) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
477 #ifdef DEBUG
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
478 printf("ignoring .heap section in input\n");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
479 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
480 _icache++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
481 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
482
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
483 if ((_ocache->c_shdr = shdr =
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
484 elf_getshdr(_ocache->c_scn)) == NULL)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
485 return (elferr("elf_getshdr"));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
486 if ((_ocache->c_data =
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
487 elf_getdata(_ocache->c_scn, NULL)) == NULL)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
488 return (elferr("elf_getdata"));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
489
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
490 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
491 * If were inserting the new .heap section, insert the new
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
492 * section name and initialize it's virtual address.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
493 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
494 if (_addr) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
495 strs = heap;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
496 shdr->sh_addr = S_ROUND(_addr, shdr->sh_addralign);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
497 _addr = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
498 } else {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
499 strs = istrs + (size_t)(_icache->c_shdr->sh_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
500 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
501
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
502 strcpy(_ostrs, strs);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
503 shdr->sh_name = _ostrs - ostrs;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
504 _ocache->c_name = _ostrs;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
505 _ostrs += strlen(strs) + 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
506
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
507 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
508 * If we've inserted a new section any later section may need
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
509 * their sh_link fields updated.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
510 * If we already had a heap section, then this is not required.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
511 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
512 if (!heap_cache) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
513 if (_ndx) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
514 if (_ocache->c_shdr->sh_link >= _ndx)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
515 _ocache->c_shdr->sh_link++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
516 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
517 }
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 * If this is the last section of the original data segment
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
521 * determine sufficient information to initialize the new .heap
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
522 * section which will be obtained next.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
523 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
524 if (data_cache == _icache) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
525 _ndx = ndx + 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
526 _addr = shdr->sh_addr + shdr->sh_size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
527 _icache--;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
528 data_cache = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
529 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
530 }
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 * Now that we have a complete description of the new image update any
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
534 * sections that are required.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
535 *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
536 * o update the value of _edata and _end.
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 reset any relocation entries if necessary.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
539 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
540 _ocache = &ocache[1];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
541 _icache = &icache[1];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
542 for (ndx = 1; ndx < oehdr->e_shnum; ndx++, _ocache++, _icache++) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
543 if ((_ocache->c_shdr->sh_type == SHT_SYMTAB) ||
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
544 (_ocache->c_shdr->sh_type == SHT_DYNSYM))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
545 update_sym(ocache, _ocache, edata);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
546
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
547 if (_ocache->c_shdr->sh_type == M_REL_SHT_TYPE)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
548 update_reloc(ocache, _ocache, icache, _icache, oehdr->e_shnum);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
549 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
550
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
551 if (elf_update(oelf, ELF_C_WRITE) == -1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
552 return (elferr("elf_update"));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
553
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
554 elf_end(oelf);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
555 elf_end(ielf);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
556 return (0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
557 }