Print this page
3299 file should only care about object capabilities
3300 file should care about all object capabilities
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/file/elf_read.c
+++ new/usr/src/cmd/file/elf_read.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
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
22 22 /* All Rights Reserved */
↓ open down ↓ |
22 lines elided |
↑ open up ↑ |
23 23
24 24
25 25 /* Copyright (c) 1987, 1988 Microsoft Corporation */
26 26 /* All Rights Reserved */
27 27
28 28 /*
29 29 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
30 30 * Use is subject to license terms.
31 31 */
32 32
33 -#pragma ident "%Z%%M% %I% %E% SMI"
34 -
35 33 /*
36 34 * ELF files can exceed 2GB in size. A standard 32-bit program
37 35 * like 'file' cannot read past 2GB, and will be unable to see
38 36 * the ELF section headers that typically are at the end of the
39 37 * object. The simplest solution to this problem would be to make
40 38 * the 'file' command a 64-bit application. However, as a matter of
41 39 * policy, we do not want to require this. A simple command like
42 40 * 'file' should not carry such a requirement, especially as we
43 41 * support 32-bit only hardware.
44 42 *
45 43 * An alternative solution is to build this code as 32-bit
46 44 * large file aware. The usual way to do this is to define a pair
47 45 * of preprocessor definitions:
48 46 *
49 47 * _LARGEFILE64_SOURCE
50 48 * Map standard I/O routines to their largefile aware versions.
51 49 *
52 50 * _FILE_OFFSET_BITS=64
53 51 * Map off_t to off64_t
54 52 *
55 53 * The problem with this solution is that libelf is not large file capable,
56 54 * and the libelf header file will prevent compilation if
57 55 * _FILE_OFFSET_BITS is set to 64.
58 56 *
59 57 * So, the solution used in this code is to define _LARGEFILE64_SOURCE
60 58 * to get access to the 64-bit APIs, not to define _FILE_OFFSET_BITS, and to
61 59 * use our own types in place of off_t, and size_t. We read all the file
62 60 * data directly using pread64(), and avoid the use of libelf for anything
63 61 * other than the xlate functionality.
64 62 */
65 63 #define _LARGEFILE64_SOURCE
66 64 #define FILE_ELF_OFF_T off64_t
67 65 #define FILE_ELF_SIZE_T uint64_t
68 66
69 67 #include <ctype.h>
70 68 #include <unistd.h>
71 69 #include <fcntl.h>
72 70 #include <stdio.h>
73 71 #include <libelf.h>
74 72 #include <stdlib.h>
75 73 #include <limits.h>
76 74 #include <locale.h>
77 75 #include <string.h>
78 76 #include <errno.h>
79 77 #include <procfs.h>
80 78 #include <sys/param.h>
81 79 #include <sys/types.h>
82 80 #include <sys/stat.h>
83 81 #include <sys/elf.h>
84 82 #include <elfcap.h>
85 83 #include "file.h"
86 84 #include "elf_read.h"
87 85
88 86 extern const char *File;
89 87
90 88 static int get_class(void);
91 89 static int get_version(void);
92 90 static int get_format(void);
93 91 static int process_shdr(Elf_Info *);
94 92 static int process_phdr(Elf_Info *);
95 93 static int file_xlatetom(Elf_Type, char *);
96 94 static int xlatetom_nhdr(Elf_Nhdr *);
97 95 static int get_phdr(Elf_Info *, int);
98 96 static int get_shdr(Elf_Info *, int);
99 97
100 98 static Elf_Ehdr EI_Ehdr; /* Elf_Ehdr to be stored */
101 99 static Elf_Word EI_Ehdr_shnum; /* # section headers */
102 100 static Elf_Word EI_Ehdr_phnum; /* # program headers */
103 101 static Elf_Word EI_Ehdr_shstrndx; /* Index of section hdr string table */
104 102 static Elf_Shdr EI_Shdr; /* recent Elf_Shdr to be stored */
105 103 static Elf_Phdr EI_Phdr; /* recent Elf_Phdr to be stored */
106 104
107 105
108 106 static int
109 107 get_class(void)
110 108 {
111 109 return (EI_Ehdr.e_ident[EI_CLASS]);
112 110 }
113 111
114 112 static int
115 113 get_version(void)
116 114 {
117 115 /* do as what libelf:_elf_config() does */
118 116 return (EI_Ehdr.e_ident[EI_VERSION] ?
119 117 EI_Ehdr.e_ident[EI_VERSION] : 1);
120 118 }
121 119
122 120 static int
123 121 get_format(void)
124 122 {
125 123 return (EI_Ehdr.e_ident[EI_DATA]);
126 124 }
127 125
128 126 /*
129 127 * file_xlatetom: translate different headers from file
130 128 * representation to memory representaion.
131 129 */
132 130 #define HDRSZ 512
133 131 static int
134 132 file_xlatetom(Elf_Type type, char *hdr)
135 133 {
136 134 Elf_Data src, dst;
137 135 char *hbuf[HDRSZ];
138 136 int version, format;
139 137
140 138 version = get_version();
141 139 format = get_format();
142 140
143 141 /* will convert only these types */
144 142 if (type != ELF_T_EHDR && type != ELF_T_PHDR &&
145 143 type != ELF_T_SHDR && type != ELF_T_WORD &&
146 144 type != ELF_T_CAP)
147 145 return (ELF_READ_FAIL);
148 146
149 147 src.d_buf = (Elf_Void *)hdr;
150 148 src.d_type = type;
151 149 src.d_version = version;
152 150
153 151 dst.d_buf = (Elf_Void *)&hbuf;
154 152 dst.d_version = EV_CURRENT;
155 153
156 154 src.d_size = elf_fsize(type, 1, version);
157 155 dst.d_size = elf_fsize(type, 1, EV_CURRENT);
158 156 if (elf_xlatetom(&dst, &src, format) == NULL)
159 157 return (ELF_READ_FAIL);
160 158
161 159 (void) memcpy(hdr, &hbuf, dst.d_size);
162 160 return (ELF_READ_OKAY);
163 161 }
164 162
165 163 /*
166 164 * xlatetom_nhdr: There is no routine to convert Note header
167 165 * so we convert each field of this header.
168 166 */
169 167 static int
170 168 xlatetom_nhdr(Elf_Nhdr *nhdr)
171 169 {
172 170 int r = ELF_READ_FAIL;
173 171
174 172 r |= file_xlatetom(ELF_T_WORD, (char *)&nhdr->n_namesz);
175 173 r |= file_xlatetom(ELF_T_WORD, (char *)&nhdr->n_descsz);
176 174 r |= file_xlatetom(ELF_T_WORD, (char *)&nhdr->n_type);
177 175 return (r);
178 176 }
179 177
180 178 /*
181 179 * elf_read: reads elf header, program, section headers to
182 180 * collect all information needed for file(1)
183 181 * output and stores them in Elf_Info.
184 182 */
185 183 int
186 184 elf_read(int fd, Elf_Info *EI)
187 185 {
188 186 FILE_ELF_SIZE_T size;
189 187 int ret = 1;
190 188
191 189 Elf_Ehdr *ehdr = &EI_Ehdr;
192 190
193 191 EI->elffd = fd;
194 192 size = sizeof (Elf_Ehdr);
195 193
196 194 if (pread64(EI->elffd, (void*)ehdr, size, 0) != size)
197 195 ret = 0;
198 196
199 197
200 198 if (file_xlatetom(ELF_T_EHDR, (char *)ehdr) == ELF_READ_FAIL)
201 199 ret = 0;
202 200
203 201 if (EI->file == NULL)
204 202 return (ELF_READ_FAIL);
205 203
206 204 /*
207 205 * Extended section or program indexes in use? If so, special
208 206 * values in the ELF header redirect us to get the real values
209 207 * from shdr[0].
210 208 */
211 209 EI_Ehdr_shnum = EI_Ehdr.e_shnum;
212 210 EI_Ehdr_phnum = EI_Ehdr.e_phnum;
213 211 EI_Ehdr_shstrndx = EI_Ehdr.e_shstrndx;
214 212 if (((EI_Ehdr_shnum == 0) || (EI_Ehdr_phnum == PN_XNUM)) &&
215 213 (EI_Ehdr.e_shoff != 0)) {
216 214 if (get_shdr(EI, 0) == ELF_READ_FAIL)
217 215 return (ELF_READ_FAIL);
218 216 if (EI_Ehdr_shnum == 0)
219 217 EI_Ehdr_shnum = EI_Shdr.sh_size;
220 218 if ((EI_Ehdr_phnum == PN_XNUM) && (EI_Shdr.sh_info != 0))
221 219 EI_Ehdr_phnum = EI_Shdr.sh_info;
222 220 if (EI_Ehdr_shstrndx == SHN_XINDEX)
223 221 EI_Ehdr_shstrndx = EI_Shdr.sh_link;
224 222 }
225 223
226 224 EI->type = ehdr->e_type;
227 225 EI->machine = ehdr->e_machine;
228 226 EI->flags = ehdr->e_flags;
229 227
230 228 if (ret == 0) {
231 229 (void) fprintf(stderr, gettext("%s: %s: can't "
232 230 "read ELF header\n"), File, EI->file);
233 231 return (ELF_READ_FAIL);
234 232 }
235 233 if (process_phdr(EI) == ELF_READ_FAIL)
236 234 return (ELF_READ_FAIL);
237 235
238 236 /* We don't need section info for core files */
239 237 if (ehdr->e_type != ET_CORE)
240 238 if (process_shdr(EI) == ELF_READ_FAIL)
241 239 return (ELF_READ_FAIL);
242 240
243 241 return (ELF_READ_OKAY);
244 242 }
245 243
246 244 /*
247 245 * get_phdr: reads program header of specified index.
248 246 */
249 247 static int
250 248 get_phdr(Elf_Info *EI, int inx)
251 249 {
252 250 FILE_ELF_OFF_T off = 0;
253 251 FILE_ELF_SIZE_T size;
254 252
255 253 if (inx >= EI_Ehdr_phnum)
256 254 return (ELF_READ_FAIL);
257 255
258 256 size = sizeof (Elf_Phdr);
259 257 off = (FILE_ELF_OFF_T)EI_Ehdr.e_phoff + (inx * size);
260 258 if (pread64(EI->elffd, (void *)&EI_Phdr, size, off) != size)
261 259 return (ELF_READ_FAIL);
262 260
263 261 if (file_xlatetom(ELF_T_PHDR, (char *)&EI_Phdr) == ELF_READ_FAIL)
264 262 return (ELF_READ_FAIL);
265 263
266 264 return (ELF_READ_OKAY);
267 265 }
268 266
269 267 /*
270 268 * get_shdr: reads section header of specified index.
271 269 */
272 270 static int
273 271 get_shdr(Elf_Info *EI, int inx)
274 272 {
275 273 FILE_ELF_OFF_T off = 0;
276 274 FILE_ELF_SIZE_T size;
277 275
278 276 /*
279 277 * Prevent access to non-existent section headers.
280 278 *
281 279 * A value of 0 for e_shoff means that there is no section header
282 280 * array in the file. A value of 0 for e_shndx does not necessarily
283 281 * mean this - there can still be a 1-element section header array
284 282 * to support extended section or program header indexes that
285 283 * exceed the 16-bit fields used in the ELF header to represent them.
286 284 */
287 285 if ((EI_Ehdr.e_shoff == 0) || ((inx > 0) && (inx >= EI_Ehdr_shnum)))
288 286 return (ELF_READ_FAIL);
289 287
290 288 size = sizeof (Elf_Shdr);
291 289 off = (FILE_ELF_OFF_T)EI_Ehdr.e_shoff + (inx * size);
292 290
293 291 if (pread64(EI->elffd, (void *)&EI_Shdr, size, off) != size)
294 292 return (ELF_READ_FAIL);
295 293
296 294 if (file_xlatetom(ELF_T_SHDR, (char *)&EI_Shdr) == ELF_READ_FAIL)
297 295 return (ELF_READ_FAIL);
298 296
299 297 return (ELF_READ_OKAY);
300 298 }
301 299
302 300 /*
303 301 * process_phdr: Read Program Headers and see if it is a core
304 302 * file of either new or (pre-restructured /proc)
305 303 * type, read the name of the file that dumped this
306 304 * core, else see if this is a dynamically linked.
307 305 */
308 306 static int
309 307 process_phdr(Elf_Info *EI)
310 308 {
311 309 register int inx;
312 310
313 311 Elf_Nhdr Nhdr, *nhdr; /* note header just read */
314 312 Elf_Phdr *phdr = &EI_Phdr;
315 313
316 314 FILE_ELF_SIZE_T nsz, nmsz, dsz;
317 315 FILE_ELF_OFF_T offset;
318 316 int class;
319 317 int ntype;
320 318 char *psinfo, *fname;
321 319
322 320 nsz = sizeof (Elf_Nhdr);
323 321 nhdr = &Nhdr;
324 322 class = get_class();
325 323 for (inx = 0; inx < EI_Ehdr_phnum; inx++) {
326 324 if (get_phdr(EI, inx) == ELF_READ_FAIL)
327 325 return (ELF_READ_FAIL);
328 326
329 327 /* read the note if it is a core */
330 328 if (phdr->p_type == PT_NOTE &&
331 329 EI_Ehdr.e_type == ET_CORE) {
332 330 /*
333 331 * If the next segment is also a note, use it instead.
334 332 */
335 333 if (get_phdr(EI, inx+1) == ELF_READ_FAIL)
336 334 return (ELF_READ_FAIL);
337 335 if (phdr->p_type != PT_NOTE) {
338 336 /* read the first phdr back */
339 337 if (get_phdr(EI, inx) == ELF_READ_FAIL)
340 338 return (ELF_READ_FAIL);
341 339 }
342 340 offset = phdr->p_offset;
343 341 if (pread64(EI->elffd, (void *)nhdr, nsz, offset)
344 342 != nsz)
345 343 return (ELF_READ_FAIL);
346 344
347 345 /* Translate the ELF note header */
348 346 if (xlatetom_nhdr(nhdr) == ELF_READ_FAIL)
349 347 return (ELF_READ_FAIL);
350 348
351 349 ntype = nhdr->n_type;
352 350 nmsz = nhdr->n_namesz;
353 351 dsz = nhdr->n_descsz;
354 352
355 353 offset += nsz + ((nmsz + 0x03) & ~0x3);
356 354 if ((psinfo = malloc(dsz)) == NULL) {
357 355 int err = errno;
358 356 (void) fprintf(stderr, gettext("%s: malloc "
359 357 "failed: %s\n"), File, strerror(err));
360 358 exit(1);
361 359 }
362 360 if (pread64(EI->elffd, psinfo, dsz, offset) != dsz)
363 361 return (ELF_READ_FAIL);
364 362 /*
365 363 * We want to print the string contained
366 364 * in psinfo->pr_fname[], where 'psinfo'
367 365 * is either an old NT_PRPSINFO structure
368 366 * or a new NT_PSINFO structure.
369 367 *
370 368 * Old core files have only type NT_PRPSINFO.
371 369 * New core files have type NT_PSINFO.
372 370 *
373 371 * These structures are also different by
374 372 * virtue of being contained in a core file
375 373 * of either 32-bit or 64-bit type.
376 374 *
377 375 * To further complicate matters, we ourself
378 376 * might be compiled either 32-bit or 64-bit.
379 377 *
380 378 * For these reason, we just *know* the offsets of
381 379 * pr_fname[] into the four different structures
382 380 * here, regardless of how we are compiled.
383 381 */
384 382 if (class == ELFCLASS32) {
385 383 /* 32-bit core file, 32-bit structures */
386 384 if (ntype == NT_PSINFO)
387 385 fname = psinfo + 88;
388 386 else /* old: NT_PRPSINFO */
389 387 fname = psinfo + 84;
390 388 } else if (class == ELFCLASS64) {
391 389 /* 64-bit core file, 64-bit structures */
392 390 if (ntype == NT_PSINFO)
393 391 fname = psinfo + 136;
394 392 else /* old: NT_PRPSINFO */
395 393 fname = psinfo + 120;
396 394 }
397 395 EI->core_type = (ntype == NT_PRPSINFO)?
398 396 EC_OLDCORE : EC_NEWCORE;
399 397 (void) memcpy(EI->fname, fname, strlen(fname));
400 398 free(psinfo);
401 399 }
402 400 if (phdr->p_type == PT_DYNAMIC) {
403 401 EI->dynamic = B_TRUE;
404 402 }
405 403 }
406 404 return (ELF_READ_OKAY);
407 405 }
408 406
409 407 /*
410 408 * process_shdr: Read Section Headers to attempt to get HW/SW
411 409 * capabilities by looking at the SUNW_cap
412 410 * section and set string in Elf_Info.
413 411 * Also look for symbol tables and debug
414 412 * information sections. Set the "stripped" field
415 413 * in Elf_Info with corresponding flags.
416 414 */
417 415 static int
418 416 process_shdr(Elf_Info *EI)
419 417 {
420 418 int capn, mac;
421 419 int i, j, idx;
422 420 FILE_ELF_OFF_T cap_off;
423 421 FILE_ELF_SIZE_T csize;
424 422 char *section_name;
425 423 Elf_Cap Chdr;
426 424 Elf_Shdr *shdr = &EI_Shdr;
427 425
428 426
429 427 csize = sizeof (Elf_Cap);
430 428 mac = EI_Ehdr.e_machine;
431 429
432 430 /* if there are no sections, return success anyway */
433 431 if (EI_Ehdr.e_shoff == 0 && EI_Ehdr_shnum == 0)
434 432 return (ELF_READ_OKAY);
435 433
436 434 /* read section names from String Section */
437 435 if (get_shdr(EI, EI_Ehdr_shstrndx) == ELF_READ_FAIL)
438 436 return (ELF_READ_FAIL);
439 437
440 438 if ((section_name = malloc(shdr->sh_size)) == NULL)
441 439 return (ELF_READ_FAIL);
442 440
443 441 if (pread64(EI->elffd, section_name, shdr->sh_size, shdr->sh_offset)
444 442 != shdr->sh_size)
445 443 return (ELF_READ_FAIL);
446 444
447 445 /* read all the sections and process them */
448 446 for (idx = 1, i = 0; i < EI_Ehdr_shnum; idx++, i++) {
449 447 char *str;
450 448
↓ open down ↓ |
406 lines elided |
↑ open up ↑ |
451 449 if (get_shdr(EI, i) == ELF_READ_FAIL)
452 450 return (ELF_READ_FAIL);
453 451
454 452 if (shdr->sh_type == SHT_NULL) {
455 453 idx--;
456 454 continue;
457 455 }
458 456
459 457 cap_off = shdr->sh_offset;
460 458 if (shdr->sh_type == SHT_SUNW_cap) {
459 + char capstr[128];
460 +
461 461 if (shdr->sh_size == 0 || shdr->sh_entsize == 0) {
462 462 (void) fprintf(stderr, ELF_ERR_ELFCAP1,
463 463 File, EI->file);
464 464 return (ELF_READ_FAIL);
465 465 }
466 466 capn = (shdr->sh_size / shdr->sh_entsize);
467 467 for (j = 0; j < capn; j++) {
468 468 /*
469 469 * read cap and xlate the values
470 470 */
471 471 if (pread64(EI->elffd, &Chdr, csize, cap_off)
472 472 != csize ||
473 473 file_xlatetom(ELF_T_CAP, (char *)&Chdr)
474 474 == 0) {
475 475 (void) fprintf(stderr, ELF_ERR_ELFCAP2,
476 476 File, EI->file);
477 477 return (ELF_READ_FAIL);
478 478 }
479 479
480 - if (Chdr.c_tag != CA_SUNW_NULL) {
481 - (void) elfcap_tag_to_str(
482 - ELFCAP_STYLE_UC, Chdr.c_tag,
483 - Chdr.c_un.c_val, EI->cap_str,
484 - sizeof (EI->cap_str),
485 - ELFCAP_FMT_SNGSPACE, mac);
486 - }
487 480 cap_off += csize;
481 +
482 + /*
483 + * Each capatibility group is terminated with
484 + * CA_SUNW_NULL. Groups other than the first
485 + * represent symbol capabilities, and aren't
486 + * interesting here.
487 + */
488 + if (Chdr.c_tag == CA_SUNW_NULL)
489 + break;
490 +
491 + (void) elfcap_tag_to_str(ELFCAP_STYLE_UC,
492 + Chdr.c_tag, Chdr.c_un.c_val, capstr,
493 + sizeof (capstr), ELFCAP_FMT_SNGSPACE,
494 + mac);
495 +
496 + if ((*EI->cap_str != '\0') && (*capstr != '\0'))
497 + (void) strlcat(EI->cap_str, " ",
498 + sizeof (EI->cap_str));
499 +
500 + (void) strlcat(EI->cap_str, capstr,
501 + sizeof (EI->cap_str));
488 502 }
489 503 }
490 504
491 505 /*
492 506 * Definition time:
493 507 * - "not stripped" means that an executable file
494 508 * contains a Symbol Table (.symtab)
495 509 * - "stripped" means that an executable file
496 510 * does not contain a Symbol Table.
497 511 * When strip -l or strip -x is run, it strips the
498 512 * debugging information (.line section name (strip -l),
499 513 * .line, .debug*, .stabs*, .dwarf* section names
500 514 * and SHT_SUNW_DEBUGSTR and SHT_SUNW_DEBUG
501 515 * section types (strip -x), however the Symbol
502 516 * Table will still be present.
503 517 * Therefore, if
504 518 * - No Symbol Table present, then report
505 519 * "stripped"
506 520 * - Symbol Table present with debugging
507 521 * information (line number or debug section names,
508 522 * or SHT_SUNW_DEBUGSTR or SHT_SUNW_DEBUG section
509 523 * types) then report:
510 524 * "not stripped"
511 525 * - Symbol Table present with no debugging
512 526 * information (line number or debug section names,
513 527 * or SHT_SUNW_DEBUGSTR or SHT_SUNW_DEBUG section
514 528 * types) then report:
515 529 * "not stripped, no debugging information
516 530 * available"
517 531 */
518 532 if ((EI->stripped & E_NOSTRIP) == E_NOSTRIP)
519 533 continue;
520 534
521 535 if (!(EI->stripped & E_SYMTAB) &&
522 536 (shdr->sh_type == SHT_SYMTAB)) {
523 537 EI->stripped |= E_SYMTAB;
524 538 continue;
525 539 }
526 540
527 541 str = §ion_name[shdr->sh_name];
528 542
529 543 if (!(EI->stripped & E_DBGINF) &&
530 544 ((shdr->sh_type == SHT_SUNW_DEBUG) ||
531 545 (shdr->sh_type == SHT_SUNW_DEBUGSTR) ||
532 546 (is_in_list(str)))) {
533 547 EI->stripped |= E_DBGINF;
534 548 }
535 549 }
536 550 free(section_name);
537 551
538 552 return (ELF_READ_OKAY);
539 553 }
↓ open down ↓ |
42 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX