Print this page
[mq]: file
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/file/file.c
+++ new/usr/src/cmd/file/file.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 2009 Sun Microsystems, Inc. All rights reserved.
30 30 * Use is subject to license terms.
31 31 */
32 32
33 +/*
34 + * Copyright 2012, Josef 'Jeff' Sipek <jeffpc@31bits.net>. All rights reserved.
35 + */
36 +
33 37 #define _LARGEFILE64_SOURCE
34 38
35 39 /* Get definitions for the relocation types supported. */
36 40 #define ELF_TARGET_ALL
37 41
38 42 #include <ctype.h>
39 43 #include <unistd.h>
40 44 #include <fcntl.h>
41 45 #include <signal.h>
42 46 #include <stdio.h>
43 47 #include <libelf.h>
44 48 #include <stdlib.h>
45 49 #include <limits.h>
46 50 #include <locale.h>
47 51 #include <wctype.h>
48 52 #include <string.h>
49 53 #include <errno.h>
50 54 #include <door.h>
51 55 #include <sys/param.h>
52 56 #include <sys/types.h>
53 57 #include <sys/mkdev.h>
54 58 #include <sys/stat.h>
55 59 #include <sys/elf.h>
56 60 #include <procfs.h>
57 61 #include <sys/core.h>
58 62 #include <sys/dumphdr.h>
59 63 #include <netinet/in.h>
60 64 #include <gelf.h>
61 65 #include <elfcap.h>
62 66 #include <sgsrtcid.h>
63 67 #include "file.h"
64 68 #include "elf_read.h"
65 69
66 70 /*
67 71 * Misc
68 72 */
69 73
70 74 #define FBSZ 512
71 75 #define MLIST_SZ 12
72 76
73 77 /*
74 78 * The 0x8FCA0102 magic string was used in crash dumps generated by releases
75 79 * prior to Solaris 7.
76 80 */
77 81 #define OLD_DUMP_MAGIC 0x8FCA0102
78 82
79 83 #if defined(__sparc)
80 84 #define NATIVE_ISA "SPARC"
81 85 #define OTHER_ISA "Intel"
82 86 #else
83 87 #define NATIVE_ISA "Intel"
84 88 #define OTHER_ISA "SPARC"
85 89 #endif
86 90
87 91 /* Assembly language comment char */
88 92 #ifdef pdp11
89 93 #define ASCOMCHAR '/'
90 94 #else
91 95 #define ASCOMCHAR '!'
92 96 #endif
93 97
94 98 #pragma align 16(fbuf)
95 99 static char fbuf[FBSZ];
96 100
97 101 /*
98 102 * Magic file variables
99 103 */
100 104 static intmax_t maxmagicoffset;
101 105 static intmax_t tmpmax;
102 106 static char *magicbuf;
103 107
104 108 static char *dfile;
105 109 static char *troff[] = { /* new troff intermediate lang */
106 110 "x", "T", "res", "init", "font", "202", "V0", "p1", 0};
107 111
108 112 static char *fort[] = { /* FORTRAN */
109 113 "function", "subroutine", "common", "dimension", "block",
110 114 "integer", "real", "data", "double",
111 115 "FUNCTION", "SUBROUTINE", "COMMON", "DIMENSION", "BLOCK",
112 116 "INTEGER", "REAL", "DATA", "DOUBLE", 0};
113 117
114 118 static char *asc[] = { /* Assembler Commands */
115 119 "sys", "mov", "tst", "clr", "jmp", "cmp", "set", "inc",
116 120 "dec", 0};
117 121
118 122 static char *c[] = { /* C Language */
119 123 "int", "char", "float", "double", "short", "long", "unsigned",
120 124 "register", "static", "struct", "extern", 0};
121 125
122 126 static char *as[] = { /* Assembler Pseudo Ops, prepended with '.' */
123 127 "globl", "global", "ident", "file", "byte", "even",
124 128 "text", "data", "bss", "comm", 0};
125 129
126 130 /*
127 131 * The line and debug section names are used by the strip command.
128 132 * Any changes in the strip implementation need to be reflected here.
129 133 */
130 134 static char *debug_sections[] = { /* Debug sections in a ELF file */
131 135 ".debug", ".stab", ".dwarf", ".line", NULL};
132 136
133 137 /* start for MB env */
↓ open down ↓ |
91 lines elided |
↑ open up ↑ |
134 138 static wchar_t wchar;
135 139 static int length;
136 140 static int IS_ascii;
137 141 static int Max;
138 142 /* end for MB env */
139 143 static int i; /* global index into first 'fbsz' bytes of file */
140 144 static int fbsz;
141 145 static int ifd = -1;
142 146 static int elffd = -1;
143 147 static int tret;
148 +static int sflg;
144 149 static int hflg;
145 150 static int dflg;
146 151 static int mflg;
147 152 static int M_flg;
148 153 static int iflg;
149 154 static struct stat64 mbuf;
150 155
151 156 static char **mlist1; /* 1st ordered list of magic files */
152 157 static char **mlist2; /* 2nd ordered list of magic files */
153 158 static size_t mlist1_sz; /* number of ptrs allocated for mlist1 */
154 159 static size_t mlist2_sz; /* number of ptrs allocated for mlist2 */
155 160 static char **mlist1p; /* next entry in mlist1 */
156 161 static char **mlist2p; /* next entry in mlist2 */
157 162
158 163 static ssize_t mread;
159 164
160 165 static void ar_coff_or_aout(int ifd);
161 166 static int type(char *file);
162 167 static int def_position_tests(char *file);
163 168 static void def_context_tests(void);
164 169 static int troffint(char *bp, int n);
165 170 static int lookup(char **tab);
166 171 static int ccom(void);
167 172 static int ascom(void);
168 173 static int sccs(void);
169 174 static int english(char *bp, int n);
170 175 static int shellscript(char buf[], struct stat64 *sb);
171 176 static int elf_check(char *file);
172 177 static int get_door_target(char *, char *, size_t);
173 178 static int zipfile(char *, int);
174 179 static int is_crash_dump(const char *, int);
175 180 static void print_dumphdr(const int, const dumphdr_t *, uint32_t (*)(uint32_t),
176 181 const char *);
177 182 static uint32_t swap_uint32(uint32_t);
178 183 static uint32_t return_uint32(uint32_t);
179 184 static void usage(void);
180 185 static void default_magic(void);
181 186 static void add_to_mlist(char *, int);
182 187 static void fd_cleanup(void);
183 188 static int is_rtld_config(void);
184 189
185 190 /* from elf_read.c */
186 191 int elf_read32(int elffd, Elf_Info *EInfo);
187 192 int elf_read64(int elffd, Elf_Info *EInfo);
188 193
189 194 #ifdef XPG4
190 195 /* SUSv3 requires a single <space> after the colon */
191 196 #define prf(x) (void) printf("%s: ", x);
192 197 #else /* !XPG4 */
193 198 #define prf(x) (void) printf("%s:%s", x, (int)strlen(x) > 6 ? "\t" : "\t\t");
194 199 #endif /* XPG4 */
195 200
196 201 /*
197 202 * Static program identifier - used to prevent localization of the name "file"
198 203 * within individual error messages.
199 204 */
200 205 const char *File = "file";
201 206
202 207 int
203 208 main(int argc, char **argv)
204 209 {
205 210 char *p;
206 211 int ch;
207 212 FILE *fl;
208 213 int cflg = 0;
209 214 int eflg = 0;
210 215 int fflg = 0;
↓ open down ↓ |
57 lines elided |
↑ open up ↑ |
211 216 char *ap = NULL;
212 217 int pathlen;
213 218 char **filep;
214 219
215 220 (void) setlocale(LC_ALL, "");
216 221 #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
217 222 #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
218 223 #endif
219 224 (void) textdomain(TEXT_DOMAIN);
220 225
221 - while ((ch = getopt(argc, argv, "M:cdf:him:")) != EOF) {
226 + while ((ch = getopt(argc, argv, "M:cdsf:him:")) != EOF) {
222 227 switch (ch) {
223 228
224 229 case 'M':
225 230 add_to_mlist(optarg, !dflg);
226 231 M_flg++;
227 232 break;
228 233
229 234 case 'c':
230 235 cflg++;
231 236 break;
232 237
233 238 case 'd':
234 239 if (!dflg) {
235 240 default_magic();
236 241 add_to_mlist(dfile, 0);
237 242 dflg++;
238 243 }
239 244 break;
240 245
246 + case 's':
247 + sflg++;
248 + break;
249 +
241 250 case 'f':
242 251 fflg++;
243 252 errno = 0;
244 253 if ((fl = fopen(optarg, "r")) == NULL) {
245 254 int err = errno;
246 255 (void) fprintf(stderr, gettext("%s: cannot "
247 256 "open file %s: %s\n"), File, optarg,
248 257 err ? strerror(err) : "");
249 258 usage();
250 259 }
251 260 pathlen = pathconf("/", _PC_PATH_MAX);
252 261 if (pathlen == -1) {
253 262 int err = errno;
254 263 (void) fprintf(stderr, gettext("%s: cannot "
255 264 "determine maximum path length: %s\n"),
256 265 File, strerror(err));
257 266 exit(1);
258 267 }
259 268 pathlen += 2; /* for null and newline in fgets */
260 269 if ((ap = malloc(pathlen * sizeof (char))) == NULL) {
261 270 int err = errno;
262 271 (void) fprintf(stderr, gettext("%s: malloc "
263 272 "failed: %s\n"), File, strerror(err));
264 273 exit(2);
265 274 }
266 275 break;
267 276
268 277 case 'h':
269 278 hflg++;
270 279 break;
271 280
272 281 case 'i':
273 282 iflg++;
274 283 break;
275 284
276 285 case 'm':
277 286 add_to_mlist(optarg, !dflg);
278 287 mflg++;
279 288 break;
280 289
281 290 case '?':
282 291 eflg++;
283 292 break;
↓ open down ↓ |
33 lines elided |
↑ open up ↑ |
284 293 }
285 294 }
286 295 if (!cflg && !fflg && (eflg || optind == argc))
287 296 usage();
288 297 if (iflg && (dflg || mflg || M_flg)) {
289 298 usage();
290 299 }
291 300 if (iflg && cflg) {
292 301 usage();
293 302 }
303 + if (sflg && (iflg || cflg))
304 + usage();
294 305
295 306 if (!dflg && !mflg && !M_flg && !iflg) {
296 307 /* no -d, -m, nor -M option; also -i option doesn't need magic */
297 308 default_magic();
298 309 if (f_mkmtab(dfile, cflg, 0) == -1) {
299 310 exit(2);
300 311 }
301 312 }
302 313
303 314 else if (mflg && !M_flg && !dflg) {
304 315 /* -m specified without -d nor -M */
305 316
306 317 #ifdef XPG4 /* For SUSv3 only */
307 318
308 319 /*
309 320 * The default position-dependent magic file tests
310 321 * in /etc/magic will follow all the -m magic tests.
311 322 */
312 323
313 324 for (filep = mlist1; filep < mlist1p; filep++) {
314 325 if (f_mkmtab(*filep, cflg, 1) == -1) {
315 326 exit(2);
316 327 }
317 328 }
318 329 default_magic();
319 330 if (f_mkmtab(dfile, cflg, 0) == -1) {
320 331 exit(2);
321 332 }
322 333 #else /* !XPG4 */
323 334 /*
324 335 * Retain Solaris file behavior for -m before SUSv3,
325 336 * when the new -d and -M options are not specified.
326 337 * Use the -m file specified in place of the default
327 338 * /etc/magic file. Solaris file will
328 339 * now allow more than one magic file to be specified
329 340 * with multiple -m options, for consistency with
330 341 * other behavior.
331 342 *
332 343 * Put the magic table(s) specified by -m into
333 344 * the second magic table instead of the first
334 345 * (as indicated by the last argument to f_mkmtab()),
335 346 * since they replace the /etc/magic tests and
336 347 * must be executed alongside the default
337 348 * position-sensitive tests.
338 349 */
339 350
340 351 for (filep = mlist1; filep < mlist1p; filep++) {
341 352 if (f_mkmtab(*filep, cflg, 0) == -1) {
342 353 exit(2);
343 354 }
344 355 }
345 356 #endif /* XPG4 */
346 357 } else {
347 358 /*
348 359 * For any other combination of -d, -m, and -M,
349 360 * use the magic files in command-line order.
350 361 * Store the entries from the two separate lists of magic
351 362 * files, if any, into two separate magic file tables.
352 363 * mlist1: magic tests executed before default magic tests
353 364 * mlist2: default magic tests and after
354 365 */
355 366 for (filep = mlist1; filep && (filep < mlist1p); filep++) {
356 367 if (f_mkmtab(*filep, cflg, 1) == -1) {
357 368 exit(2);
358 369 }
359 370 }
360 371 for (filep = mlist2; filep && (filep < mlist2p); filep++) {
361 372 if (f_mkmtab(*filep, cflg, 0) == -1) {
362 373 exit(2);
363 374 }
364 375 }
365 376 }
366 377
367 378 /* Initialize the magic file variables; check both magic tables */
368 379 tmpmax = f_getmaxoffset(1);
369 380 maxmagicoffset = f_getmaxoffset(0);
370 381 if (maxmagicoffset < tmpmax) {
371 382 maxmagicoffset = tmpmax;
372 383 }
373 384 if (maxmagicoffset < (intmax_t)FBSZ)
374 385 maxmagicoffset = (intmax_t)FBSZ;
375 386 if ((magicbuf = malloc(maxmagicoffset)) == NULL) {
376 387 int err = errno;
377 388 (void) fprintf(stderr, gettext("%s: malloc failed: %s\n"),
378 389 File, strerror(err));
379 390 exit(2);
380 391 }
381 392
382 393 if (cflg) {
383 394 f_prtmtab();
384 395 if (ferror(stdout) != 0) {
385 396 (void) fprintf(stderr, gettext("%s: error writing to "
386 397 "stdout\n"), File);
387 398 exit(1);
388 399 }
389 400 if (fclose(stdout) != 0) {
390 401 int err = errno;
391 402 (void) fprintf(stderr, gettext("%s: fclose "
392 403 "failed: %s\n"), File, strerror(err));
393 404 exit(1);
394 405 }
395 406 exit(0);
396 407 }
397 408
398 409 for (; fflg || optind < argc; optind += !fflg) {
399 410 register int l;
400 411
401 412 if (fflg) {
402 413 if ((p = fgets(ap, pathlen, fl)) == NULL) {
403 414 fflg = 0;
404 415 optind--;
405 416 continue;
406 417 }
407 418 l = strlen(p);
408 419 if (l > 0)
409 420 p[l - 1] = '\0';
410 421 } else
411 422 p = argv[optind];
412 423 prf(p); /* print "file_name:<tab>" */
413 424
414 425 if (type(p))
415 426 tret = 1;
416 427 }
417 428 if (ap != NULL)
418 429 free(ap);
419 430 if (tret != 0)
420 431 exit(tret);
421 432
422 433 if (ferror(stdout) != 0) {
423 434 (void) fprintf(stderr, gettext("%s: error writing to "
424 435 "stdout\n"), File);
425 436 exit(1);
426 437 }
427 438 if (fclose(stdout) != 0) {
428 439 int err = errno;
429 440 (void) fprintf(stderr, gettext("%s: fclose failed: %s\n"),
430 441 File, strerror(err));
431 442 exit(1);
432 443 }
433 444 return (0);
434 445 }
435 446
436 447 static int
437 448 type(char *file)
438 449 {
439 450 int cc;
440 451 char buf[BUFSIZ];
441 452 int (*statf)() = hflg ? lstat64 : stat64;
442 453
443 454 i = 0; /* reset index to beginning of file */
444 455 ifd = -1;
445 456 if ((*statf)(file, &mbuf) < 0) {
446 457 if (statf == lstat64 || lstat64(file, &mbuf) < 0) {
447 458 int err = errno;
448 459 (void) printf(gettext("cannot open: %s\n"),
449 460 strerror(err));
450 461 return (0); /* POSIX.2 */
↓ open down ↓ |
147 lines elided |
↑ open up ↑ |
451 462 }
452 463 }
453 464 switch (mbuf.st_mode & S_IFMT) {
454 465 case S_IFREG:
455 466 if (iflg) {
456 467 (void) printf(gettext("regular file\n"));
457 468 return (0);
458 469 }
459 470 break;
460 471 case S_IFCHR:
472 + if (sflg)
473 + break;
461 474 (void) printf(gettext("character"));
462 475 goto spcl;
463 476
464 477 case S_IFDIR:
465 478 (void) printf(gettext("directory\n"));
466 479 return (0);
467 480
468 481 case S_IFIFO:
469 482 (void) printf(gettext("fifo\n"));
470 483 return (0);
471 484
472 485 case S_IFLNK:
473 486 if ((cc = readlink(file, buf, BUFSIZ)) < 0) {
↓ open down ↓ |
3 lines elided |
↑ open up ↑ |
474 487 int err = errno;
475 488 (void) printf(gettext("readlink error: %s\n"),
476 489 strerror(err));
477 490 return (1);
478 491 }
479 492 buf[cc] = '\0';
480 493 (void) printf(gettext("symbolic link to %s\n"), buf);
481 494 return (0);
482 495
483 496 case S_IFBLK:
497 + if (sflg)
498 + break;
484 499 (void) printf(gettext("block"));
485 500 /* major and minor, see sys/mkdev.h */
486 501 spcl:
487 502 (void) printf(gettext(" special (%d/%d)\n"),
488 503 major(mbuf.st_rdev), minor(mbuf.st_rdev));
489 504 return (0);
490 505
491 506 case S_IFSOCK:
492 507 (void) printf("socket\n");
493 508 /* FIXME, should open and try to getsockname. */
494 509 return (0);
495 510
496 511 case S_IFDOOR:
497 512 if (get_door_target(file, buf, sizeof (buf)) == 0)
498 513 (void) printf(gettext("door to %s\n"), buf);
499 514 else
500 515 (void) printf(gettext("door\n"));
501 516 return (0);
502 517
503 518 }
504 519
505 520 if (elf_version(EV_CURRENT) == EV_NONE) {
506 521 (void) printf(gettext("libelf is out of date\n"));
507 522 return (1);
508 523 }
509 524
510 525 ifd = open64(file, O_RDONLY);
511 526 if (ifd < 0) {
512 527 int err = errno;
513 528 (void) printf(gettext("cannot open: %s\n"), strerror(err));
514 529 return (0); /* POSIX.2 */
515 530 }
516 531
517 532 /* need another fd for elf, since we might want to read the file too */
518 533 elffd = open64(file, O_RDONLY);
519 534 if (elffd < 0) {
520 535 int err = errno;
521 536 (void) printf(gettext("cannot open: %s\n"), strerror(err));
522 537 (void) close(ifd);
523 538 ifd = -1;
524 539 return (0); /* POSIX.2 */
525 540 }
526 541 if ((fbsz = read(ifd, fbuf, FBSZ)) == -1) {
527 542 int err = errno;
528 543 (void) printf(gettext("cannot read: %s\n"), strerror(err));
529 544 (void) close(ifd);
530 545 ifd = -1;
531 546 return (0); /* POSIX.2 */
532 547 }
533 548 if (fbsz == 0) {
534 549 (void) printf(gettext("empty file\n"));
535 550 fd_cleanup();
536 551 return (0);
537 552 }
538 553
539 554 /*
540 555 * First try user-specified position-dependent magic tests, if any,
541 556 * which need to execute before the default tests.
542 557 */
543 558 if ((mread = pread(ifd, (void*)magicbuf, (size_t)maxmagicoffset,
544 559 (off_t)0)) == -1) {
545 560 int err = errno;
546 561 (void) printf(gettext("cannot read: %s\n"), strerror(err));
547 562 fd_cleanup();
548 563 return (0);
549 564 }
550 565
551 566 /*
552 567 * ChecK against Magic Table entries.
553 568 * Check first magic table for magic tests to be applied
554 569 * before default tests.
555 570 * If no default tests are to be applied, all magic tests
556 571 * should occur in this magic table.
557 572 */
558 573 switch (f_ckmtab(magicbuf, mread, 1)) {
559 574 case -1: /* Error */
560 575 exit(2);
561 576 break;
562 577 case 0: /* Not magic */
563 578 break;
564 579 default: /* Switch is magic index */
565 580 (void) putchar('\n');
566 581 fd_cleanup();
567 582 return (0);
568 583 /* NOTREACHED */
569 584 break;
570 585 }
571 586
572 587 if (dflg || !M_flg) {
573 588 /*
574 589 * default position-dependent tests,
575 590 * plus non-default magic tests, if any
576 591 */
577 592 switch (def_position_tests(file)) {
578 593 case -1: /* error */
579 594 fd_cleanup();
580 595 return (1);
581 596 case 1: /* matching type found */
582 597 fd_cleanup();
583 598 return (0);
584 599 /* NOTREACHED */
585 600 break;
586 601 case 0: /* no matching type found */
587 602 break;
588 603 }
589 604 /* default context-sensitive tests */
590 605 def_context_tests();
591 606 } else {
592 607 /* no more tests to apply; no match was found */
593 608 (void) printf(gettext("data\n"));
594 609 }
595 610 fd_cleanup();
596 611 return (0);
597 612 }
598 613
599 614 /*
600 615 * def_position_tests() - applies default position-sensitive tests,
601 616 * looking for values in specific positions in the file.
602 617 * These are followed by default (followed by possibly some
603 618 * non-default) magic file tests.
604 619 *
605 620 * All position-sensitive tests, default or otherwise, must
606 621 * be applied before context-sensitive tests, to avoid
607 622 * false context-sensitive matches.
608 623 *
609 624 * Returns -1 on error which should result in error (non-zero)
610 625 * exit status for the file utility.
611 626 * Returns 0 if no matching file type found.
612 627 * Returns 1 if matching file type found.
613 628 */
614 629
615 630 static int
616 631 def_position_tests(char *file)
617 632 {
618 633 if (sccs()) { /* look for "1hddddd" where d is a digit */
619 634 (void) printf("sccs \n");
620 635 return (1);
621 636 }
622 637 if (fbuf[0] == '#' && fbuf[1] == '!' && shellscript(fbuf+2, &mbuf))
623 638 return (1);
624 639
625 640 if (elf_check(file) == 0) {
626 641 (void) putchar('\n');
627 642 return (1);
628 643 /* LINTED: pointer cast may result in improper alignment */
629 644 } else if (*(int *)fbuf == CORE_MAGIC) {
630 645 /* LINTED: pointer cast may result in improper alignment */
631 646 struct core *corep = (struct core *)fbuf;
632 647
633 648 (void) printf("a.out core file");
634 649
635 650 if (*(corep->c_cmdname) != '\0')
636 651 (void) printf(" from '%s'", corep->c_cmdname);
637 652 (void) putchar('\n');
638 653 return (1);
639 654 }
640 655
641 656 /*
642 657 * Runtime linker (ld.so.1) configuration file.
643 658 */
644 659 if (is_rtld_config())
645 660 return (1);
646 661
647 662 /*
648 663 * ZIP files, JAR files, and Java executables
649 664 */
650 665 if (zipfile(fbuf, ifd))
651 666 return (1);
652 667
653 668 if (is_crash_dump(fbuf, ifd))
654 669 return (1);
655 670
656 671 /*
657 672 * ChecK against Magic Table entries.
658 673 * The magic entries checked here always start with default
659 674 * magic tests and may be followed by other, non-default magic
660 675 * tests. If no default tests are to be executed, all the
661 676 * magic tests should have been in the first magic table.
662 677 */
663 678 switch (f_ckmtab(magicbuf, mread, 0)) {
664 679 case -1: /* Error */
665 680 exit(2);
666 681 break;
667 682 case 0: /* Not magic */
668 683 return (0);
669 684 /* NOTREACHED */
670 685 break;
671 686 default: /* Switch is magic index */
672 687
673 688 /*
674 689 * f_ckmtab recognizes file type,
675 690 * check if it is PostScript.
676 691 * if not, check if elf or a.out
677 692 */
678 693 if (magicbuf[0] == '%' && magicbuf[1] == '!') {
679 694 (void) putchar('\n');
680 695 } else {
681 696
682 697 /*
683 698 * Check that the file is executable (dynamic
684 699 * objects must be executable to be exec'ed,
685 700 * shared objects need not be, but by convention
686 701 * should be executable).
687 702 *
688 703 * Note that we should already have processed
689 704 * the file if it was an ELF file.
690 705 */
691 706 ar_coff_or_aout(elffd);
692 707 (void) putchar('\n');
693 708 }
694 709 return (1);
695 710 /* NOTREACHED */
696 711 break;
697 712 }
698 713
699 714 return (0); /* file was not identified */
700 715 }
701 716
702 717 /*
703 718 * def_context_tests() - default context-sensitive tests.
704 719 * These are the last tests to be applied.
705 720 * If no match is found, prints out "data".
706 721 */
707 722
708 723 static void
709 724 def_context_tests(void)
710 725 {
711 726 int j;
712 727 int nl;
713 728 char ch;
714 729 int len;
715 730
716 731 if (ccom() == 0)
717 732 goto notc;
718 733 while (fbuf[i] == '#') {
719 734 j = i;
720 735 while (fbuf[i++] != '\n') {
721 736 if (i - j > 255) {
722 737 (void) printf(gettext("data\n"));
723 738 return;
724 739 }
725 740 if (i >= fbsz)
726 741 goto notc;
727 742 }
728 743 if (ccom() == 0)
729 744 goto notc;
730 745 }
731 746 check:
732 747 if (lookup(c) == 1) {
733 748 while ((ch = fbuf[i]) != ';' && ch != '{') {
734 749 if ((len = mblen(&fbuf[i], MB_CUR_MAX)) <= 0)
735 750 len = 1;
736 751 i += len;
737 752 if (i >= fbsz)
738 753 goto notc;
739 754 }
740 755 (void) printf(gettext("c program text"));
741 756 goto outa;
742 757 }
743 758 nl = 0;
744 759 while (fbuf[i] != '(') {
745 760 if (fbuf[i] <= 0)
746 761 goto notas;
747 762 if (fbuf[i] == ';') {
748 763 i++;
749 764 goto check;
750 765 }
751 766 if (fbuf[i++] == '\n')
752 767 if (nl++ > 6)
753 768 goto notc;
754 769 if (i >= fbsz)
755 770 goto notc;
756 771 }
757 772 while (fbuf[i] != ')') {
758 773 if (fbuf[i++] == '\n')
759 774 if (nl++ > 6)
760 775 goto notc;
761 776 if (i >= fbsz)
762 777 goto notc;
763 778 }
764 779 while (fbuf[i] != '{') {
765 780 if ((len = mblen(&fbuf[i], MB_CUR_MAX)) <= 0)
766 781 len = 1;
767 782 if (fbuf[i] == '\n')
768 783 if (nl++ > 6)
769 784 goto notc;
770 785 i += len;
771 786 if (i >= fbsz)
772 787 goto notc;
773 788 }
774 789 (void) printf(gettext("c program text"));
775 790 goto outa;
776 791 notc:
777 792 i = 0; /* reset to begining of file again */
778 793 while (fbuf[i] == 'c' || fbuf[i] == 'C'|| fbuf[i] == '!' ||
779 794 fbuf[i] == '*' || fbuf[i] == '\n') {
780 795 while (fbuf[i++] != '\n')
781 796 if (i >= fbsz)
782 797 goto notfort;
783 798 }
784 799 if (lookup(fort) == 1) {
785 800 (void) printf(gettext("fortran program text"));
786 801 goto outa;
787 802 }
788 803 notfort: /* looking for assembler program */
789 804 i = 0; /* reset to beginning of file again */
790 805 if (ccom() == 0) /* assembler programs may contain */
791 806 /* c-style comments */
792 807 goto notas;
793 808 if (ascom() == 0)
794 809 goto notas;
795 810 j = i - 1;
796 811 if (fbuf[i] == '.') {
797 812 i++;
798 813 if (lookup(as) == 1) {
799 814 (void) printf(gettext("assembler program text"));
800 815 goto outa;
801 816 } else if (j != -1 && fbuf[j] == '\n' && isalpha(fbuf[j + 2])) {
802 817 (void) printf(
803 818 gettext("[nt]roff, tbl, or eqn input text"));
804 819 goto outa;
805 820 }
806 821 }
807 822 while (lookup(asc) == 0) {
808 823 if (ccom() == 0)
809 824 goto notas;
810 825 if (ascom() == 0)
811 826 goto notas;
812 827 while (fbuf[i] != '\n' && fbuf[i++] != ':') {
813 828 if (i >= fbsz)
814 829 goto notas;
815 830 }
816 831 while (fbuf[i] == '\n' || fbuf[i] == ' ' || fbuf[i] == '\t')
817 832 if (i++ >= fbsz)
818 833 goto notas;
819 834 j = i - 1;
820 835 if (fbuf[i] == '.') {
821 836 i++;
822 837 if (lookup(as) == 1) {
823 838 (void) printf(
824 839 gettext("assembler program text"));
825 840 goto outa;
826 841 } else if (fbuf[j] == '\n' && isalpha(fbuf[j+2])) {
827 842 (void) printf(
828 843 gettext("[nt]roff, tbl, or eqn input "
829 844 "text"));
830 845 goto outa;
831 846 }
832 847 }
833 848 }
834 849 (void) printf(gettext("assembler program text"));
835 850 goto outa;
836 851 notas:
837 852 /* start modification for multibyte env */
838 853 IS_ascii = 1;
839 854 if (fbsz < FBSZ)
840 855 Max = fbsz;
841 856 else
842 857 Max = FBSZ - MB_LEN_MAX; /* prevent cut of wchar read */
843 858 /* end modification for multibyte env */
844 859
845 860 for (i = 0; i < Max; /* null */)
846 861 if (fbuf[i] & 0200) {
847 862 IS_ascii = 0;
848 863 if (fbuf[0] == '\100' && fbuf[1] == '\357') {
849 864 (void) printf(gettext("troff output\n"));
850 865 return;
851 866 }
852 867 /* start modification for multibyte env */
853 868 if ((length = mbtowc(&wchar, &fbuf[i], MB_CUR_MAX))
854 869 <= 0 || !iswprint(wchar)) {
855 870 (void) printf(gettext("data\n"));
856 871 return;
857 872 }
858 873 i += length;
859 874 }
860 875 else
861 876 i++;
862 877 i = fbsz;
863 878 /* end modification for multibyte env */
864 879 if (mbuf.st_mode&(S_IXUSR|S_IXGRP|S_IXOTH))
865 880 (void) printf(gettext("commands text"));
866 881 else if (troffint(fbuf, fbsz))
867 882 (void) printf(gettext("troff intermediate output text"));
868 883 else if (english(fbuf, fbsz))
869 884 (void) printf(gettext("English text"));
870 885 else if (IS_ascii)
871 886 (void) printf(gettext("ascii text"));
872 887 else
873 888 (void) printf(gettext("text")); /* for multibyte env */
874 889 outa:
875 890 /*
876 891 * This code is to make sure that no MB char is cut in half
877 892 * while still being used.
878 893 */
879 894 fbsz = (fbsz < FBSZ ? fbsz : fbsz - MB_CUR_MAX + 1);
880 895 while (i < fbsz) {
881 896 if (isascii(fbuf[i])) {
882 897 i++;
883 898 continue;
884 899 } else {
885 900 if ((length = mbtowc(&wchar, &fbuf[i], MB_CUR_MAX))
886 901 <= 0 || !iswprint(wchar)) {
887 902 (void) printf(gettext(" with garbage\n"));
888 903 return;
889 904 }
890 905 i = i + length;
891 906 }
892 907 }
893 908 (void) printf("\n");
894 909 }
895 910
896 911 static int
897 912 troffint(char *bp, int n)
898 913 {
899 914 int k;
900 915
901 916 i = 0;
902 917 for (k = 0; k < 6; k++) {
903 918 if (lookup(troff) == 0)
904 919 return (0);
905 920 if (lookup(troff) == 0)
906 921 return (0);
907 922 while (i < n && bp[i] != '\n')
908 923 i++;
909 924 if (i++ >= n)
910 925 return (0);
911 926 }
912 927 return (1);
913 928 }
914 929
915 930 static void
916 931 ar_coff_or_aout(int elffd)
917 932 {
918 933 Elf *elf;
919 934
920 935 /*
921 936 * Get the files elf descriptor and process it as an elf or
922 937 * a.out (4.x) file.
923 938 */
924 939
925 940 elf = elf_begin(elffd, ELF_C_READ, (Elf *)0);
926 941 switch (elf_kind(elf)) {
927 942 case ELF_K_AR :
928 943 (void) printf(gettext(", not a dynamic executable "
929 944 "or shared object"));
930 945 break;
931 946 case ELF_K_COFF:
932 947 (void) printf(gettext(", unsupported or unknown "
933 948 "file type"));
934 949 break;
935 950 default:
936 951 /*
937 952 * This is either an unknown file or an aout format
938 953 * At this time, we don't print dynamic/stripped
939 954 * info. on a.out or non-Elf binaries.
940 955 */
941 956 break;
942 957 }
943 958 (void) elf_end(elf);
944 959 }
945 960
946 961
947 962 static void
948 963 print_elf_type(Elf_Info EI)
949 964 {
950 965 switch (EI.type) {
951 966 case ET_NONE:
952 967 (void) printf(" %s", gettext("unknown type"));
953 968 break;
954 969 case ET_REL:
955 970 (void) printf(" %s", gettext("relocatable"));
956 971 break;
957 972 case ET_EXEC:
958 973 (void) printf(" %s", gettext("executable"));
959 974 break;
960 975 case ET_DYN:
961 976 (void) printf(" %s", gettext("dynamic lib"));
962 977 break;
963 978 default:
964 979 break;
965 980 }
966 981 }
967 982
968 983 static void
969 984 print_elf_machine(int machine)
970 985 {
971 986 /*
972 987 * This table must be kept in sync with the EM_ constants
973 988 * in /usr/include/sys/elf.h.
974 989 */
975 990 static const char *mach_str[EM_NUM] = {
976 991 "unknown machine", /* 0 - EM_NONE */
977 992 "WE32100", /* 1 - EM_M32 */
978 993 "SPARC", /* 2 - EM_SPARC */
979 994 "80386", /* 3 - EM_386 */
980 995 "M68000", /* 4 - EM_68K */
981 996 "M88000", /* 5 - EM_88K */
982 997 "80486", /* 6 - EM_486 */
983 998 "i860", /* 7 - EM_860 */
984 999 "MIPS RS3000 Big-Endian", /* 8 - EM_MIPS */
985 1000 "S/370", /* 9 - EM_S370 */
986 1001 "MIPS RS3000 Little-Endian", /* 10 - EM_MIPS_RS3_LE */
987 1002 "MIPS RS6000", /* 11 - EM_RS6000 */
988 1003 NULL, /* 12 - EM_UNKNOWN12 */
989 1004 NULL, /* 13 - EM_UNKNOWN13 */
990 1005 NULL, /* 14 - EM_UNKNOWN14 */
991 1006 "PA-RISC", /* 15 - EM_PA_RISC */
992 1007 "nCUBE", /* 16 - EM_nCUBE */
993 1008 "VPP500", /* 17 - EM_VPP500 */
994 1009 "SPARC32PLUS", /* 18 - EM_SPARC32PLUS */
995 1010 "i960", /* 19 - EM_960 */
996 1011 "PowerPC", /* 20 - EM_PPC */
997 1012 "PowerPC64", /* 21 - EM_PPC64 */
998 1013 "S/390", /* 22 - EM_S390 */
999 1014 NULL, /* 23 - EM_UNKNOWN23 */
1000 1015 NULL, /* 24 - EM_UNKNOWN24 */
1001 1016 NULL, /* 25 - EM_UNKNOWN25 */
1002 1017 NULL, /* 26 - EM_UNKNOWN26 */
1003 1018 NULL, /* 27 - EM_UNKNOWN27 */
1004 1019 NULL, /* 28 - EM_UNKNOWN28 */
1005 1020 NULL, /* 29 - EM_UNKNOWN29 */
1006 1021 NULL, /* 30 - EM_UNKNOWN30 */
1007 1022 NULL, /* 31 - EM_UNKNOWN31 */
1008 1023 NULL, /* 32 - EM_UNKNOWN32 */
1009 1024 NULL, /* 33 - EM_UNKNOWN33 */
1010 1025 NULL, /* 34 - EM_UNKNOWN34 */
1011 1026 NULL, /* 35 - EM_UNKNOWN35 */
1012 1027 "V800", /* 36 - EM_V800 */
1013 1028 "FR20", /* 37 - EM_FR20 */
1014 1029 "RH32", /* 38 - EM_RH32 */
1015 1030 "RCE", /* 39 - EM_RCE */
1016 1031 "ARM", /* 40 - EM_ARM */
1017 1032 "Alpha", /* 41 - EM_ALPHA */
1018 1033 "S/390", /* 42 - EM_SH */
1019 1034 "SPARCV9", /* 43 - EM_SPARCV9 */
1020 1035 "Tricore", /* 44 - EM_TRICORE */
1021 1036 "ARC", /* 45 - EM_ARC */
1022 1037 "H8/300", /* 46 - EM_H8_300 */
1023 1038 "H8/300H", /* 47 - EM_H8_300H */
1024 1039 "H8S", /* 48 - EM_H8S */
1025 1040 "H8/500", /* 49 - EM_H8_500 */
1026 1041 "IA64", /* 50 - EM_IA_64 */
1027 1042 "MIPS-X", /* 51 - EM_MIPS_X */
1028 1043 "Coldfire", /* 52 - EM_COLDFIRE */
1029 1044 "M68HC12", /* 53 - EM_68HC12 */
1030 1045 "MMA", /* 54 - EM_MMA */
1031 1046 "PCP", /* 55 - EM_PCP */
1032 1047 "nCPU", /* 56 - EM_NCPU */
1033 1048 "NDR1", /* 57 - EM_NDR1 */
1034 1049 "Starcore", /* 58 - EM_STARCORE */
1035 1050 "ME16", /* 59 - EM_ME16 */
1036 1051 "ST100", /* 60 - EM_ST100 */
1037 1052 "TINYJ", /* 61 - EM_TINYJ */
1038 1053 "AMD64", /* 62 - EM_AMD64 */
1039 1054 "PDSP", /* 63 - EM_PDSP */
1040 1055 NULL, /* 64 - EM_UNKNOWN64 */
1041 1056 NULL, /* 65 - EM_UNKNOWN65 */
1042 1057 "FX66", /* 66 - EM_FX66 */
1043 1058 "ST9 PLUS", /* 67 - EM_ST9PLUS */
1044 1059 "ST7", /* 68 - EM_ST7 */
1045 1060 "68HC16", /* 69 - EM_68HC16 */
1046 1061 "68HC11", /* 70 - EM_68HC11 */
1047 1062 "68H08", /* 71 - EM_68HC08 */
1048 1063 "68HC05", /* 72 - EM_68HC05 */
1049 1064 "SVX", /* 73 - EM_SVX */
1050 1065 "ST19", /* 74 - EM_ST19 */
1051 1066 "VAX", /* 75 - EM_VAX */
1052 1067 "CRIS", /* 76 - EM_CRIS */
1053 1068 "Javelin", /* 77 - EM_JAVELIN */
1054 1069 "Firepath", /* 78 - EM_FIREPATH */
1055 1070 "ZSP", /* 79 - EM_ZSP */
1056 1071 "MMIX", /* 80 - EM_MMIX */
1057 1072 "HUANY", /* 81 - EM_HUANY */
1058 1073 "Prism", /* 82 - EM_PRISM */
1059 1074 "AVR", /* 83 - EM_AVR */
1060 1075 "FR30", /* 84 - EM_FR30 */
1061 1076 "D10V", /* 85 - EM_D10V */
1062 1077 "D30V", /* 86 - EM_D30V */
1063 1078 "V850", /* 87 - EM_V850 */
1064 1079 "M32R", /* 88 - EM_M32R */
1065 1080 "MN10300", /* 89 - EM_MN10300 */
1066 1081 "MN10200", /* 90 - EM_MN10200 */
1067 1082 "picoJava", /* 91 - EM_PJ */
1068 1083 "OpenRISC", /* 92 - EM_OPENRISC */
1069 1084 "Tangent-A5", /* 93 - EM_ARC_A5 */
1070 1085 "Xtensa" /* 94 - EM_XTENSA */
1071 1086 };
1072 1087 /* If new machine is added, refuse to compile until we're updated */
1073 1088 #if EM_NUM != 95
1074 1089 #error "Number of known ELF machine constants has changed"
1075 1090 #endif
1076 1091
1077 1092 const char *str;
1078 1093
1079 1094 if ((machine < EM_NONE) || (machine >= EM_NUM))
1080 1095 machine = EM_NONE;
1081 1096
1082 1097 str = mach_str[machine];
1083 1098 if (str)
1084 1099 (void) printf(" %s", str);
1085 1100 }
1086 1101
1087 1102 static void
1088 1103 print_elf_datatype(int datatype)
1089 1104 {
1090 1105 switch (datatype) {
1091 1106 case ELFDATA2LSB:
1092 1107 (void) printf(" LSB");
1093 1108 break;
1094 1109 case ELFDATA2MSB:
1095 1110 (void) printf(" MSB");
1096 1111 break;
1097 1112 default:
1098 1113 break;
1099 1114 }
1100 1115 }
1101 1116
1102 1117 static void
1103 1118 print_elf_class(int class)
1104 1119 {
1105 1120 switch (class) {
1106 1121 case ELFCLASS32:
1107 1122 (void) printf(" %s", gettext("32-bit"));
1108 1123 break;
1109 1124 case ELFCLASS64:
1110 1125 (void) printf(" %s", gettext("64-bit"));
1111 1126 break;
1112 1127 default:
1113 1128 break;
1114 1129 }
1115 1130 }
1116 1131
1117 1132 static void
1118 1133 print_elf_flags(Elf_Info EI)
1119 1134 {
1120 1135 unsigned int flags;
1121 1136
1122 1137 flags = EI.flags;
1123 1138 switch (EI.machine) {
1124 1139 case EM_SPARCV9:
1125 1140 if (flags & EF_SPARC_EXT_MASK) {
1126 1141 if (flags & EF_SPARC_SUN_US3) {
1127 1142 (void) printf("%s", gettext(
1128 1143 ", UltraSPARC3 Extensions Required"));
1129 1144 } else if (flags & EF_SPARC_SUN_US1) {
1130 1145 (void) printf("%s", gettext(
1131 1146 ", UltraSPARC1 Extensions Required"));
1132 1147 }
1133 1148 if (flags & EF_SPARC_HAL_R1)
1134 1149 (void) printf("%s", gettext(
1135 1150 ", HaL R1 Extensions Required"));
1136 1151 }
1137 1152 break;
1138 1153 case EM_SPARC32PLUS:
1139 1154 if (flags & EF_SPARC_32PLUS)
1140 1155 (void) printf("%s", gettext(", V8+ Required"));
1141 1156 if (flags & EF_SPARC_SUN_US3) {
1142 1157 (void) printf("%s",
1143 1158 gettext(", UltraSPARC3 Extensions Required"));
1144 1159 } else if (flags & EF_SPARC_SUN_US1) {
1145 1160 (void) printf("%s",
1146 1161 gettext(", UltraSPARC1 Extensions Required"));
1147 1162 }
1148 1163 if (flags & EF_SPARC_HAL_R1)
1149 1164 (void) printf("%s",
1150 1165 gettext(", HaL R1 Extensions Required"));
1151 1166 break;
1152 1167 default:
1153 1168 break;
1154 1169 }
1155 1170 }
1156 1171
1157 1172 /*
1158 1173 * check_ident: checks the ident field of the presumeably
1159 1174 * elf file. If check fails, this is not an
1160 1175 * elf file.
1161 1176 */
1162 1177 static int
1163 1178 check_ident(unsigned char *ident, int fd)
1164 1179 {
1165 1180 int class;
1166 1181 if (pread64(fd, ident, EI_NIDENT, 0) != EI_NIDENT)
1167 1182 return (ELF_READ_FAIL);
1168 1183 class = ident[EI_CLASS];
1169 1184 if (class != ELFCLASS32 && class != ELFCLASS64)
1170 1185 return (ELF_READ_FAIL);
1171 1186 if (ident[EI_MAG0] != ELFMAG0 || ident[EI_MAG1] != ELFMAG1 ||
1172 1187 ident[EI_MAG2] != ELFMAG2 || ident[EI_MAG3] != ELFMAG3)
1173 1188 return (ELF_READ_FAIL);
1174 1189
1175 1190 return (ELF_READ_OKAY);
1176 1191 }
1177 1192
1178 1193 static int
1179 1194 elf_check(char *file)
1180 1195 {
1181 1196 Elf_Info EInfo;
1182 1197 int class, version, format;
1183 1198 unsigned char ident[EI_NIDENT];
1184 1199
1185 1200 (void) memset(&EInfo, 0, sizeof (Elf_Info));
1186 1201 EInfo.file = file;
1187 1202
1188 1203 /*
1189 1204 * Verify information in file indentifier.
1190 1205 * Return quietly if not elf; Different type of file.
1191 1206 */
1192 1207 if (check_ident(ident, elffd) == ELF_READ_FAIL)
1193 1208 return (1);
1194 1209
1195 1210 /*
1196 1211 * Read the elf headers for processing and get the
1197 1212 * get the needed information in Elf_Info struct.
1198 1213 */
1199 1214 class = ident[EI_CLASS];
1200 1215 if (class == ELFCLASS32) {
1201 1216 if (elf_read32(elffd, &EInfo) == ELF_READ_FAIL) {
1202 1217 (void) fprintf(stderr, gettext("%s: %s: can't "
1203 1218 "read ELF header\n"), File, file);
1204 1219 return (1);
1205 1220 }
1206 1221 } else if (class == ELFCLASS64) {
1207 1222 if (elf_read64(elffd, &EInfo) == ELF_READ_FAIL) {
1208 1223 (void) fprintf(stderr, gettext("%s: %s: can't "
1209 1224 "read ELF header\n"), File, file);
1210 1225 return (1);
1211 1226 }
1212 1227 } else {
1213 1228 /* something wrong */
1214 1229 return (1);
1215 1230 }
1216 1231
1217 1232 /* version not in ident then 1 */
1218 1233 version = ident[EI_VERSION] ? ident[EI_VERSION] : 1;
1219 1234
1220 1235 format = ident[EI_DATA];
1221 1236 (void) printf("%s", gettext("ELF"));
1222 1237 print_elf_class(class);
1223 1238 print_elf_datatype(format);
1224 1239 print_elf_type(EInfo);
1225 1240
1226 1241 if (EInfo.core_type != EC_NOTCORE) {
1227 1242 /* Print what kind of core is this */
1228 1243 if (EInfo.core_type == EC_OLDCORE)
1229 1244 (void) printf(" %s", gettext("pre-2.6 core file"));
1230 1245 else
1231 1246 (void) printf(" %s", gettext("core file"));
1232 1247 }
1233 1248
1234 1249 /* Print machine info */
1235 1250 print_elf_machine(EInfo.machine);
1236 1251
1237 1252 /* Print Version */
1238 1253 if (version == 1)
1239 1254 (void) printf(" %s %d", gettext("Version"), version);
1240 1255
1241 1256 /* Print Flags */
1242 1257 print_elf_flags(EInfo);
1243 1258
1244 1259 /* Last bit, if it is a core */
1245 1260 if (EInfo.core_type != EC_NOTCORE) {
1246 1261 /* Print the program name that dumped this core */
1247 1262 (void) printf(gettext(", from '%s'"), EInfo.fname);
1248 1263 return (0);
1249 1264 }
1250 1265
1251 1266 /* Print Capabilities */
1252 1267 if (EInfo.cap_str[0] != '\0')
1253 1268 (void) printf(" [%s]", EInfo.cap_str);
1254 1269
1255 1270 if ((EInfo.type != ET_EXEC) && (EInfo.type != ET_DYN))
1256 1271 return (0);
1257 1272
1258 1273 /* Print if it is dynamically linked */
1259 1274 if (EInfo.dynamic)
1260 1275 (void) printf(gettext(", dynamically linked"));
1261 1276 else
1262 1277 (void) printf(gettext(", statically linked"));
1263 1278
1264 1279 /* Printf it it is stripped */
1265 1280 if (EInfo.stripped & E_SYMTAB) {
1266 1281 (void) printf(gettext(", not stripped"));
1267 1282 if (!(EInfo.stripped & E_DBGINF)) {
1268 1283 (void) printf(gettext(
1269 1284 ", no debugging information available"));
1270 1285 }
1271 1286 } else {
1272 1287 (void) printf(gettext(", stripped"));
1273 1288 }
1274 1289
1275 1290 return (0);
1276 1291 }
1277 1292
1278 1293 /*
1279 1294 * is_rtld_config - If file is a runtime linker config file, prints
1280 1295 * the description and returns True (1). Otherwise, silently returns
1281 1296 * False (0).
1282 1297 */
1283 1298 int
1284 1299 is_rtld_config(void)
1285 1300 {
1286 1301 Rtc_id *id;
1287 1302
1288 1303 if ((fbsz >= sizeof (*id)) && RTC_ID_TEST(fbuf)) {
1289 1304 (void) printf(gettext("Runtime Linking Configuration"));
1290 1305 id = (Rtc_id *) fbuf;
1291 1306 print_elf_class(id->id_class);
1292 1307 print_elf_datatype(id->id_data);
1293 1308 print_elf_machine(id->id_machine);
1294 1309 (void) printf("\n");
1295 1310 return (1);
1296 1311 }
1297 1312
1298 1313 return (0);
1299 1314 }
1300 1315
1301 1316 /*
1302 1317 * lookup -
1303 1318 * Attempts to match one of the strings from a list, 'tab',
1304 1319 * with what is in the file, starting at the current index position 'i'.
1305 1320 * Looks past any initial whitespace and expects whitespace or other
1306 1321 * delimiting characters to follow the matched string.
1307 1322 * A match identifies the file as being 'assembler', 'fortran', 'c', etc.
1308 1323 * Returns 1 for a successful match, 0 otherwise.
1309 1324 */
1310 1325 static int
1311 1326 lookup(char **tab)
1312 1327 {
1313 1328 register char r;
1314 1329 register int k, j, l;
1315 1330
1316 1331 while (fbuf[i] == ' ' || fbuf[i] == '\t' || fbuf[i] == '\n')
1317 1332 i++;
1318 1333 for (j = 0; tab[j] != 0; j++) {
1319 1334 l = 0;
1320 1335 for (k = i; ((r = tab[j][l++]) == fbuf[k] && r != '\0'); k++)
1321 1336 ;
1322 1337 if (r == '\0')
1323 1338 if (fbuf[k] == ' ' || fbuf[k] == '\n' ||
1324 1339 fbuf[k] == '\t' || fbuf[k] == '{' ||
1325 1340 fbuf[k] == '/') {
1326 1341 i = k;
1327 1342 return (1);
1328 1343 }
1329 1344 }
1330 1345 return (0);
1331 1346 }
1332 1347
1333 1348 /*
1334 1349 * ccom -
1335 1350 * Increments the current index 'i' into the file buffer 'fbuf' past any
1336 1351 * whitespace lines and C-style comments found, starting at the current
1337 1352 * position of 'i'. Returns 1 as long as we don't increment i past the
1338 1353 * size of fbuf (fbsz). Otherwise, returns 0.
1339 1354 */
1340 1355
1341 1356 static int
1342 1357 ccom(void)
1343 1358 {
1344 1359 register char cc;
1345 1360 int len;
1346 1361
1347 1362 while ((cc = fbuf[i]) == ' ' || cc == '\t' || cc == '\n')
1348 1363 if (i++ >= fbsz)
1349 1364 return (0);
1350 1365 if (fbuf[i] == '/' && fbuf[i+1] == '*') {
1351 1366 i += 2;
1352 1367 while (fbuf[i] != '*' || fbuf[i+1] != '/') {
1353 1368 if (fbuf[i] == '\\')
1354 1369 i++;
1355 1370 if ((len = mblen(&fbuf[i], MB_CUR_MAX)) <= 0)
1356 1371 len = 1;
1357 1372 i += len;
1358 1373 if (i >= fbsz)
1359 1374 return (0);
1360 1375 }
1361 1376 if ((i += 2) >= fbsz)
1362 1377 return (0);
1363 1378 }
1364 1379 if (fbuf[i] == '\n')
1365 1380 if (ccom() == 0)
1366 1381 return (0);
1367 1382 return (1);
1368 1383 }
1369 1384
1370 1385 /*
1371 1386 * ascom -
1372 1387 * Increments the current index 'i' into the file buffer 'fbuf' past
1373 1388 * consecutive assembler program comment lines starting with ASCOMCHAR,
1374 1389 * starting at the current position of 'i'.
1375 1390 * Returns 1 as long as we don't increment i past the
1376 1391 * size of fbuf (fbsz). Otherwise returns 0.
1377 1392 */
1378 1393
1379 1394 static int
1380 1395 ascom(void)
1381 1396 {
1382 1397 while (fbuf[i] == ASCOMCHAR) {
1383 1398 i++;
1384 1399 while (fbuf[i++] != '\n')
1385 1400 if (i >= fbsz)
1386 1401 return (0);
1387 1402 while (fbuf[i] == '\n')
1388 1403 if (i++ >= fbsz)
1389 1404 return (0);
1390 1405 }
1391 1406 return (1);
1392 1407 }
1393 1408
1394 1409 static int
1395 1410 sccs(void)
1396 1411 { /* look for "1hddddd" where d is a digit */
1397 1412 register int j;
1398 1413
1399 1414 if (fbuf[0] == 1 && fbuf[1] == 'h') {
1400 1415 for (j = 2; j <= 6; j++) {
1401 1416 if (isdigit(fbuf[j]))
1402 1417 continue;
1403 1418 else
1404 1419 return (0);
1405 1420 }
1406 1421 } else {
1407 1422 return (0);
1408 1423 }
1409 1424 return (1);
1410 1425 }
1411 1426
1412 1427 static int
1413 1428 english(char *bp, int n)
1414 1429 {
1415 1430 #define NASC 128 /* number of ascii char ?? */
1416 1431 register int j, vow, freq, rare, len;
1417 1432 register int badpun = 0, punct = 0;
1418 1433 int ct[NASC];
1419 1434
1420 1435 if (n < 50)
1421 1436 return (0); /* no point in statistics on squibs */
1422 1437 for (j = 0; j < NASC; j++)
1423 1438 ct[j] = 0;
1424 1439 for (j = 0; j < n; j += len) {
1425 1440 if ((unsigned char)bp[j] < NASC)
1426 1441 ct[bp[j]|040]++;
1427 1442 switch (bp[j]) {
1428 1443 case '.':
1429 1444 case ',':
1430 1445 case ')':
1431 1446 case '%':
1432 1447 case ';':
1433 1448 case ':':
1434 1449 case '?':
1435 1450 punct++;
1436 1451 if (j < n-1 && bp[j+1] != ' ' && bp[j+1] != '\n')
1437 1452 badpun++;
1438 1453 }
1439 1454 if ((len = mblen(&bp[j], MB_CUR_MAX)) <= 0)
1440 1455 len = 1;
1441 1456 }
1442 1457 if (badpun*5 > punct)
1443 1458 return (0);
1444 1459 vow = ct['a'] + ct['e'] + ct['i'] + ct['o'] + ct['u'];
1445 1460 freq = ct['e'] + ct['t'] + ct['a'] + ct['i'] + ct['o'] + ct['n'];
1446 1461 rare = ct['v'] + ct['j'] + ct['k'] + ct['q'] + ct['x'] + ct['z'];
1447 1462 if (2*ct[';'] > ct['e'])
1448 1463 return (0);
1449 1464 if ((ct['>'] + ct['<'] + ct['/']) > ct['e'])
1450 1465 return (0); /* shell file test */
1451 1466 return (vow * 5 >= n - ct[' '] && freq >= 10 * rare);
1452 1467 }
1453 1468
1454 1469
1455 1470 static int
1456 1471 shellscript(char buf[], struct stat64 *sb)
1457 1472 {
1458 1473 char *tp, *cp, *xp, *up, *gp;
1459 1474
1460 1475 cp = strchr(buf, '\n');
1461 1476 if (cp == NULL || cp - fbuf > fbsz)
1462 1477 return (0);
1463 1478 for (tp = buf; tp != cp && isspace((unsigned char)*tp); tp++)
1464 1479 if (!isascii(*tp))
1465 1480 return (0);
1466 1481 for (xp = tp; tp != cp && !isspace((unsigned char)*tp); tp++)
1467 1482 if (!isascii(*tp))
1468 1483 return (0);
1469 1484 if (tp == xp)
1470 1485 return (0);
1471 1486 if (sb->st_mode & S_ISUID)
1472 1487 up = gettext("set-uid ");
1473 1488 else
1474 1489 up = "";
1475 1490
1476 1491 if (sb->st_mode & S_ISGID)
1477 1492 gp = gettext("set-gid ");
1478 1493 else
1479 1494 gp = "";
1480 1495
1481 1496 if (strncmp(xp, "/bin/sh", tp - xp) == 0)
1482 1497 xp = gettext("shell");
1483 1498 else if (strncmp(xp, "/bin/csh", tp - xp) == 0)
1484 1499 xp = gettext("c-shell");
1485 1500 else if (strncmp(xp, "/usr/sbin/dtrace", tp - xp) == 0)
1486 1501 xp = gettext("DTrace");
1487 1502 else
1488 1503 *tp = '\0';
1489 1504 /*
1490 1505 * TRANSLATION_NOTE
1491 1506 * This message is printed by file command for shell scripts.
1492 1507 * The first %s is for the translation for "set-uid " (if the script
1493 1508 * has the set-uid bit set), or is for an empty string (if the
1494 1509 * script does not have the set-uid bit set).
1495 1510 * Similarly, the second %s is for the translation for "set-gid ",
1496 1511 * or is for an empty string.
1497 1512 * The third %s is for the translation for either: "shell", "c-shell",
1498 1513 * or "DTrace", or is for the pathname of the program the script
1499 1514 * executes.
1500 1515 */
1501 1516 (void) printf(gettext("%s%sexecutable %s script\n"), up, gp, xp);
1502 1517 return (1);
1503 1518 }
1504 1519
1505 1520 static int
1506 1521 get_door_target(char *file, char *buf, size_t bufsize)
1507 1522 {
1508 1523 int fd;
1509 1524 door_info_t di;
1510 1525 psinfo_t psinfo;
1511 1526
1512 1527 if ((fd = open64(file, O_RDONLY)) < 0 ||
1513 1528 door_info(fd, &di) != 0) {
1514 1529 if (fd >= 0)
1515 1530 (void) close(fd);
1516 1531 return (-1);
1517 1532 }
1518 1533 (void) close(fd);
1519 1534
1520 1535 (void) sprintf(buf, "/proc/%ld/psinfo", di.di_target);
1521 1536 if ((fd = open64(buf, O_RDONLY)) < 0 ||
1522 1537 read(fd, &psinfo, sizeof (psinfo)) != sizeof (psinfo)) {
1523 1538 if (fd >= 0)
1524 1539 (void) close(fd);
1525 1540 return (-1);
1526 1541 }
1527 1542 (void) close(fd);
1528 1543
1529 1544 (void) snprintf(buf, bufsize, "%s[%ld]", psinfo.pr_fname, di.di_target);
1530 1545 return (0);
1531 1546 }
1532 1547
1533 1548 /*
1534 1549 * ZIP file header information
1535 1550 */
1536 1551 #define SIGSIZ 4
1537 1552 #define LOCSIG "PK\003\004"
1538 1553 #define LOCHDRSIZ 30
1539 1554
1540 1555 #define CH(b, n) (((unsigned char *)(b))[n])
1541 1556 #define SH(b, n) (CH(b, n) | (CH(b, n+1) << 8))
1542 1557 #define LG(b, n) (SH(b, n) | (SH(b, n+2) << 16))
1543 1558
1544 1559 #define LOCNAM(b) (SH(b, 26)) /* filename size */
1545 1560 #define LOCEXT(b) (SH(b, 28)) /* extra field size */
1546 1561
1547 1562 #define XFHSIZ 4 /* header id, data size */
1548 1563 #define XFHID(b) (SH(b, 0)) /* extract field header id */
1549 1564 #define XFDATASIZ(b) (SH(b, 2)) /* extract field data size */
1550 1565 #define XFJAVASIG 0xcafe /* java executables */
1551 1566
1552 1567 static int
1553 1568 zipfile(char *fbuf, int fd)
1554 1569 {
1555 1570 off_t xoff, xoff_end;
1556 1571
1557 1572 if (strncmp(fbuf, LOCSIG, SIGSIZ) != 0)
1558 1573 return (0);
1559 1574
1560 1575 xoff = LOCHDRSIZ + LOCNAM(fbuf);
1561 1576 xoff_end = xoff + LOCEXT(fbuf);
1562 1577
1563 1578 while (xoff < xoff_end) {
1564 1579 char xfhdr[XFHSIZ];
1565 1580
1566 1581 if (pread(fd, xfhdr, XFHSIZ, xoff) != XFHSIZ)
1567 1582 break;
1568 1583
1569 1584 if (XFHID(xfhdr) == XFJAVASIG) {
1570 1585 (void) printf("%s\n", gettext("java archive file"));
1571 1586 return (1);
1572 1587 }
1573 1588 xoff += sizeof (xfhdr) + XFDATASIZ(xfhdr);
1574 1589 }
1575 1590
1576 1591 /*
1577 1592 * We could just print "ZIP archive" here.
1578 1593 *
1579 1594 * However, customers may be using their own entries in
1580 1595 * /etc/magic to distinguish one kind of ZIP file from another, so
1581 1596 * let's defer the printing of "ZIP archive" to there.
1582 1597 */
1583 1598 return (0);
1584 1599 }
1585 1600
1586 1601 static int
1587 1602 is_crash_dump(const char *buf, int fd)
1588 1603 {
1589 1604 /* LINTED: pointer cast may result in improper alignment */
1590 1605 const dumphdr_t *dhp = (const dumphdr_t *)buf;
1591 1606
1592 1607 /*
1593 1608 * The current DUMP_MAGIC string covers Solaris 7 and later releases.
1594 1609 * The utsname struct is only present in dumphdr_t's with dump_version
1595 1610 * greater than or equal to 9.
1596 1611 */
1597 1612 if (dhp->dump_magic == DUMP_MAGIC) {
1598 1613 print_dumphdr(fd, dhp, return_uint32, NATIVE_ISA);
1599 1614
1600 1615 } else if (dhp->dump_magic == swap_uint32(DUMP_MAGIC)) {
1601 1616 print_dumphdr(fd, dhp, swap_uint32, OTHER_ISA);
1602 1617
1603 1618 } else if (dhp->dump_magic == OLD_DUMP_MAGIC ||
1604 1619 dhp->dump_magic == swap_uint32(OLD_DUMP_MAGIC)) {
1605 1620 char *isa = (dhp->dump_magic == OLD_DUMP_MAGIC ?
1606 1621 NATIVE_ISA : OTHER_ISA);
1607 1622 (void) printf(gettext("SunOS 32-bit %s crash dump\n"), isa);
1608 1623
1609 1624 } else {
1610 1625 return (0);
1611 1626 }
1612 1627
1613 1628 return (1);
1614 1629 }
1615 1630
1616 1631 static void
1617 1632 print_dumphdr(const int fd, const dumphdr_t *dhp, uint32_t (*swap)(uint32_t),
1618 1633 const char *isa)
1619 1634 {
1620 1635 dumphdr_t dh;
1621 1636
1622 1637 /*
1623 1638 * A dumphdr_t is bigger than FBSZ, so we have to manually read the
1624 1639 * rest of it.
1625 1640 */
1626 1641 if (swap(dhp->dump_version) > 8 && pread(fd, &dh, sizeof (dumphdr_t),
1627 1642 (off_t)0) == sizeof (dumphdr_t)) {
1628 1643 const char *c = swap(dh.dump_flags) & DF_COMPRESSED ?
1629 1644 "compressed " : "";
1630 1645 const char *l = swap(dh.dump_flags) & DF_LIVE ?
1631 1646 "live" : "crash";
1632 1647
1633 1648 (void) printf(gettext(
1634 1649 "%s %s %s %u-bit %s %s%s dump from '%s'\n"),
1635 1650 dh.dump_utsname.sysname, dh.dump_utsname.release,
1636 1651 dh.dump_utsname.version, swap(dh.dump_wordsize), isa,
1637 1652 c, l, dh.dump_utsname.nodename);
↓ open down ↓ |
1144 lines elided |
↑ open up ↑ |
1638 1653 } else {
1639 1654 (void) printf(gettext("SunOS %u-bit %s crash dump\n"),
1640 1655 swap(dhp->dump_wordsize), isa);
1641 1656 }
1642 1657 }
1643 1658
1644 1659 static void
1645 1660 usage(void)
1646 1661 {
1647 1662 (void) fprintf(stderr, gettext(
1648 - "usage: file [-dh] [-M mfile] [-m mfile] [-f ffile] file ...\n"
1649 - " file [-dh] [-M mfile] [-m mfile] -f ffile\n"
1663 + "usage: file [-dhs] [-M mfile] [-m mfile] [-f ffile] file ...\n"
1664 + " file [-dhs] [-M mfile] [-m mfile] -f ffile\n"
1650 1665 " file -i [-h] [-f ffile] file ...\n"
1651 1666 " file -i [-h] -f ffile\n"
1652 1667 " file -c [-d] [-M mfile] [-m mfile]\n"));
1653 1668 exit(2);
1654 1669 }
1655 1670
1656 1671 static uint32_t
1657 1672 swap_uint32(uint32_t in)
1658 1673 {
1659 1674 uint32_t out;
1660 1675
1661 1676 out = (in & 0x000000ff) << 24;
1662 1677 out |= (in & 0x0000ff00) << 8; /* >> 8 << 16 */
1663 1678 out |= (in & 0x00ff0000) >> 8; /* >> 16 << 8 */
1664 1679 out |= (in & 0xff000000) >> 24;
1665 1680
1666 1681 return (out);
1667 1682 }
1668 1683
1669 1684 static uint32_t
1670 1685 return_uint32(uint32_t in)
1671 1686 {
1672 1687 return (in);
1673 1688 }
1674 1689
1675 1690 /*
1676 1691 * Check if str is in the string list str_list.
1677 1692 */
1678 1693 int
1679 1694 is_in_list(char *str)
1680 1695 {
1681 1696 int i;
1682 1697
1683 1698 /*
1684 1699 * Only need to compare the strlen(str_list[i]) bytes.
1685 1700 * That way .stab will match on .stab* sections, and
1686 1701 * .debug will match on .debug* sections.
1687 1702 */
1688 1703 for (i = 0; debug_sections[i] != NULL; i++) {
1689 1704 if (strncmp(debug_sections[i], str,
1690 1705 strlen(debug_sections[i])) == 0) {
1691 1706 return (1);
1692 1707 }
1693 1708 }
1694 1709 return (0);
1695 1710 }
1696 1711
1697 1712 /*
1698 1713 * default_magic -
1699 1714 * allocate space for and create the default magic file
1700 1715 * name string.
1701 1716 */
1702 1717
1703 1718 static void
1704 1719 default_magic(void)
1705 1720 {
1706 1721 const char *msg_locale = setlocale(LC_MESSAGES, NULL);
1707 1722 struct stat statbuf;
1708 1723
1709 1724 if ((dfile = malloc(strlen(msg_locale) + 35)) == NULL) {
1710 1725 int err = errno;
1711 1726 (void) fprintf(stderr, gettext("%s: malloc failed: %s\n"),
1712 1727 File, strerror(err));
1713 1728 exit(2);
1714 1729 }
1715 1730 (void) snprintf(dfile, strlen(msg_locale) + 35,
1716 1731 "/usr/lib/locale/%s/LC_MESSAGES/magic", msg_locale);
1717 1732 if (stat(dfile, &statbuf) != 0) {
1718 1733 (void) strcpy(dfile, "/etc/magic");
1719 1734 }
1720 1735 }
1721 1736
1722 1737 /*
1723 1738 * add_to_mlist -
1724 1739 * Add the given magic_file filename string to the list of magic
1725 1740 * files (mlist). This list of files will later be examined, and
1726 1741 * each magic file's entries will be added in order to
1727 1742 * the mtab table.
1728 1743 *
1729 1744 * The first flag is set to 1 to add to the first list, mlist1.
1730 1745 * The first flag is set to 0 to add to the second list, mlist2.
1731 1746 */
1732 1747
1733 1748 static void
1734 1749 add_to_mlist(char *magic_file, int first)
1735 1750 {
1736 1751 char **mlist; /* ordered list of magic files */
1737 1752 size_t mlist_sz; /* number of pointers allocated for mlist */
1738 1753 char **mlistp; /* next entry in mlist */
1739 1754 size_t mlistp_off;
1740 1755
1741 1756 if (first) {
1742 1757 mlist = mlist1;
1743 1758 mlist_sz = mlist1_sz;
1744 1759 mlistp = mlist1p;
1745 1760 } else {
1746 1761 mlist = mlist2;
1747 1762 mlist_sz = mlist2_sz;
1748 1763 mlistp = mlist2p;
1749 1764 }
1750 1765
1751 1766 if (mlist == NULL) { /* initial mlist allocation */
1752 1767 if ((mlist = calloc(MLIST_SZ, sizeof (char *))) == NULL) {
1753 1768 int err = errno;
1754 1769 (void) fprintf(stderr, gettext("%s: malloc "
1755 1770 "failed: %s\n"), File, strerror(err));
1756 1771 exit(2);
1757 1772 }
1758 1773 mlist_sz = MLIST_SZ;
1759 1774 mlistp = mlist;
1760 1775 }
1761 1776 if ((mlistp - mlist) >= mlist_sz) {
1762 1777 mlistp_off = mlistp - mlist;
1763 1778 mlist_sz *= 2;
1764 1779 if ((mlist = realloc(mlist,
1765 1780 mlist_sz * sizeof (char *))) == NULL) {
1766 1781 int err = errno;
1767 1782 (void) fprintf(stderr, gettext("%s: malloc "
1768 1783 "failed: %s\n"), File, strerror(err));
1769 1784 exit(2);
1770 1785 }
1771 1786 mlistp = mlist + mlistp_off;
1772 1787 }
1773 1788 /*
1774 1789 * now allocate memory for and copy the
1775 1790 * magic file name string
1776 1791 */
1777 1792 if ((*mlistp = malloc(strlen(magic_file) + 1)) == NULL) {
1778 1793 int err = errno;
1779 1794 (void) fprintf(stderr, gettext("%s: malloc failed: %s\n"),
1780 1795 File, strerror(err));
1781 1796 exit(2);
1782 1797 }
1783 1798 (void) strlcpy(*mlistp, magic_file, strlen(magic_file) + 1);
1784 1799 mlistp++;
1785 1800
1786 1801 if (first) {
1787 1802 mlist1 = mlist;
1788 1803 mlist1_sz = mlist_sz;
1789 1804 mlist1p = mlistp;
1790 1805 } else {
1791 1806 mlist2 = mlist;
1792 1807 mlist2_sz = mlist_sz;
1793 1808 mlist2p = mlistp;
1794 1809 }
1795 1810 }
1796 1811
1797 1812 static void
1798 1813 fd_cleanup(void)
1799 1814 {
1800 1815 if (ifd != -1) {
1801 1816 (void) close(ifd);
1802 1817 ifd = -1;
1803 1818 }
1804 1819 if (elffd != -1) {
1805 1820 (void) close(elffd);
1806 1821 elffd = -1;
1807 1822 }
1808 1823 }
↓ open down ↓ |
149 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX