Print this page
10075 make usr/src/tools smatch clean
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/tools/elfextract/elfextract.c
+++ new/usr/src/tools/elfextract/elfextract.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
↓ open down ↓ |
16 lines elided |
↑ open up ↑ |
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24 24 * Use is subject to license terms.
25 25 */
26 26
27 -#pragma ident "%Z%%M% %I% %E% SMI"
27 +/*
28 + * Copyright (c) 2018, Joyent, Inc.
29 + */
28 30
31 +#include <unistd.h>
29 32 #include <stdlib.h>
30 33 #include <fcntl.h>
31 34 #include <strings.h>
32 35 #include <stdio.h>
33 36 #include <errno.h>
34 37 #include <sys/types.h>
35 38 #include <sys/inttypes.h>
36 39 #include <sys/elf.h>
37 40 #include <sys/elf_notes.h>
38 41 #include <sys/mman.h>
39 42 #include <sys/stat.h>
40 43 #include <sys/statvfs.h>
41 44
42 45 static char *pname;
43 46 static char *fname;
44 47 static char *image; /* pointer to the ELF file in memory */
45 48
46 49 #define ELFSEEK(offset) ((void *)(image + offset))
47 50
48 51 /*
49 52 * Extract the PT_LOAD bits and format them into a .s
50 53 */
51 54 static void
52 55 extract32(Elf32_Ehdr *eh)
53 56 {
54 57 Elf32_Phdr *phdr;
55 58 caddr_t allphdrs;
56 59 int i;
57 60 int c;
58 61 unsigned char *bytes;
59 62 uint_t cnt = 10;
60 63
61 64 allphdrs = NULL;
62 65
63 66 if (eh->e_type != ET_EXEC) {
64 67 (void) fprintf(stderr, "%s: not ET_EXEC, e_type = 0x%x\n",
65 68 pname, eh->e_type);
66 69 exit(1);
67 70 }
68 71 if (eh->e_phnum == 0 || eh->e_phoff == 0) {
69 72 (void) fprintf(stderr, "%s: no program headers\n", pname);
70 73 exit(1);
71 74 }
72 75
73 76 /*
74 77 * Get the program headers.
75 78 */
76 79 allphdrs = ELFSEEK(eh->e_phoff);
77 80 if (allphdrs == NULL) {
78 81 (void) fprintf(stderr, "%s: Failed to get %d program hdrs\n",
79 82 pname, eh->e_phnum);
80 83 exit(1);
81 84 }
82 85
83 86 /*
84 87 * Find the PT_LOAD section
85 88 */
86 89 for (i = 0; i < eh->e_phnum; i++) {
87 90 /*LINTED [ELF program header alignment]*/
88 91 phdr = (Elf32_Phdr *)(allphdrs + eh->e_phentsize * i);
89 92
90 93 if (phdr->p_type != PT_LOAD)
91 94 continue;
92 95
93 96 if (phdr->p_memsz == 0)
94 97 continue;
95 98
96 99 bytes = ELFSEEK(phdr->p_offset);
97 100 for (c = 0; c < phdr->p_filesz; ++c) {
98 101 if (c % cnt == 0)
99 102 (void) printf("\n .byte ");
100 103 else
101 104 (void) printf(",");
102 105 (void) printf("0x%x", bytes[c]);
103 106 }
104 107 for (; c < phdr->p_memsz; ++c) {
105 108 if (c % cnt == 0) {
106 109 (void) printf("\n .byte ");
107 110 cnt = 20;
108 111 } else {
109 112 (void) printf(", ");
110 113 }
111 114 (void) printf("0");
112 115 }
113 116 (void) printf("\n");
114 117 return;
115 118 }
116 119
117 120 (void) fprintf(stderr, "%s: Failed finding PT_LOAD section\n", pname);
118 121 exit(1);
119 122 }
120 123
121 124 static void
122 125 extract64(Elf64_Ehdr *eh)
123 126 {
124 127 Elf64_Phdr *phdr;
125 128 caddr_t allphdrs;
126 129 int i;
127 130 int c;
128 131 unsigned char *bytes;
129 132 uint_t cnt = 10;
130 133
131 134 allphdrs = NULL;
132 135
133 136 if (eh->e_type != ET_EXEC) {
134 137 (void) fprintf(stderr, "%s: not ET_EXEC, e_type = 0x%x\n",
135 138 pname, eh->e_type);
136 139 exit(1);
137 140 }
138 141 if (eh->e_phnum == 0 || eh->e_phoff == 0) {
139 142 (void) fprintf(stderr, "%s: no program headers\n", pname);
140 143 exit(1);
141 144 }
142 145
143 146 /*
144 147 * Get the program headers.
145 148 */
146 149 allphdrs = ELFSEEK(eh->e_phoff);
147 150 if (allphdrs == NULL) {
148 151 (void) fprintf(stderr, "%s: Failed to get %d program hdrs\n",
149 152 pname, eh->e_phnum);
150 153 exit(1);
151 154 }
152 155
153 156 /*
154 157 * Find the PT_LOAD section
155 158 */
156 159 for (i = 0; i < eh->e_phnum; i++) {
157 160 /*LINTED [ELF program header alignment]*/
158 161 phdr = (Elf64_Phdr *)(allphdrs + eh->e_phentsize * i);
159 162
160 163 if (phdr->p_type != PT_LOAD)
161 164 continue;
162 165
163 166 if (phdr->p_memsz == 0)
164 167 continue;
165 168
166 169 bytes = ELFSEEK(phdr->p_offset);
167 170 for (c = 0; c < phdr->p_filesz; ++c) {
168 171 if (c % cnt == 0)
169 172 (void) printf("\n .byte ");
170 173 else
171 174 (void) printf(",");
172 175 (void) printf("0x%x", bytes[c]);
173 176 }
174 177 for (; c < phdr->p_memsz; ++c) {
175 178 if (c % cnt == 0) {
176 179 (void) printf("\n .byte ");
177 180 cnt = 20;
178 181 } else {
179 182 (void) printf(", ");
180 183 }
181 184 (void) printf("0");
182 185 }
183 186 (void) printf("\n");
184 187 return;
185 188 }
186 189
187 190 (void) fprintf(stderr, "%s: Failed finding PT_LOAD section\n", pname);
188 191 exit(1);
189 192 }
190 193
191 194 int
192 195 main(int argc, char **argv)
193 196 {
194 197 int fd;
195 198 uchar_t *ident;
196 199 void *hdr = NULL;
197 200 struct stat stats;
198 201 ssize_t r;
199 202 size_t pgsz;
200 203 uint_t len;
201 204
202 205 /*
203 206 * we expect one argument -- the elf file
204 207 */
205 208 if (argc != 2) {
206 209 (void) fprintf(stderr, "usage: %s <unix-elf-file>\n", argv[0]);
207 210 exit(1);
208 211 }
209 212
210 213 pname = strrchr(argv[0], '/');
211 214 if (pname == NULL)
212 215 pname = argv[0];
213 216 else
214 217 ++pname;
215 218
216 219 fname = argv[1];
217 220 fd = open(fname, O_RDONLY);
218 221 if (fd < 0) {
219 222 (void) fprintf(stderr, "%s: open(%s, O_RDONLY) failed, %s\n",
220 223 pname, fname, strerror(errno));
221 224 exit(1);
222 225 }
223 226
224 227 if (stat(fname, &stats) < 0) {
225 228 (void) fprintf(stderr, "%s: stat(%s, ...) failed, %s\n",
226 229 pname, fname, strerror(errno));
227 230 exit(1);
228 231 }
229 232
230 233 pgsz = getpagesize();
231 234 len = (stats.st_size + (pgsz - 1)) & (~(pgsz - 1));
232 235
233 236 /*
234 237 * mmap the file
235 238 */
236 239 image = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
237 240 if (image == MAP_FAILED) {
238 241 (void) fprintf(stderr, "%s: mmap() of %s failed, %s\n",
239 242 pname, fname, strerror(errno));
240 243 exit(1);
241 244 }
242 245
243 246 ident = ELFSEEK(0);
244 247 if (ident[EI_MAG0] != ELFMAG0 || ident[EI_MAG1] != ELFMAG1 ||
245 248 ident[EI_MAG2] != ELFMAG2 || ident[EI_MAG3] != ELFMAG3) {
246 249 (void) fprintf(stderr, "%s: not an ELF file!\n", pname);
247 250 exit(1);
248 251 }
249 252
250 253 if (ident[EI_CLASS] == ELFCLASS32) {
251 254 hdr = ELFSEEK(0);
252 255 extract32(hdr);
253 256 } else if (ident[EI_CLASS] == ELFCLASS64) {
254 257 hdr = ELFSEEK(0);
255 258 extract64(hdr);
256 259 } else {
257 260 (void) fprintf(stderr, "%s: Wrong ELF class 0x%x\n", pname,
258 261 ident[EI_CLASS]);
259 262 exit(1);
260 263 }
261 264 return (0);
262 265 }
↓ open down ↓ |
224 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX