17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5c13de8f6Sab196087 * Common Development and Distribution License (the "License"). 6c13de8f6Sab196087 * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 227c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 237c478bd9Sstevel@tonic-gate 247c478bd9Sstevel@tonic-gate 257c478bd9Sstevel@tonic-gate /* Copyright (c) 1987, 1988 Microsoft Corporation */ 267c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate /* 29*9a411307Srie * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 307c478bd9Sstevel@tonic-gate * Use is subject to license terms. 317c478bd9Sstevel@tonic-gate */ 327c478bd9Sstevel@tonic-gate 337c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 347c478bd9Sstevel@tonic-gate 357c478bd9Sstevel@tonic-gate #define _LARGEFILE64_SOURCE 367c478bd9Sstevel@tonic-gate 37c13de8f6Sab196087 /* Get definitions for the relocation types supported. */ 38c13de8f6Sab196087 #define ELF_TARGET_ALL 39c13de8f6Sab196087 407c478bd9Sstevel@tonic-gate #include <ctype.h> 417c478bd9Sstevel@tonic-gate #include <unistd.h> 427c478bd9Sstevel@tonic-gate #include <fcntl.h> 437c478bd9Sstevel@tonic-gate #include <signal.h> 447c478bd9Sstevel@tonic-gate #include <stdio.h> 457c478bd9Sstevel@tonic-gate #include <libelf.h> 467c478bd9Sstevel@tonic-gate #include <stdlib.h> 477c478bd9Sstevel@tonic-gate #include <limits.h> 487c478bd9Sstevel@tonic-gate #include <locale.h> 497c478bd9Sstevel@tonic-gate #include <wctype.h> 507c478bd9Sstevel@tonic-gate #include <string.h> 517c478bd9Sstevel@tonic-gate #include <errno.h> 527c478bd9Sstevel@tonic-gate #include <door.h> 537c478bd9Sstevel@tonic-gate #include <sys/param.h> 547c478bd9Sstevel@tonic-gate #include <sys/types.h> 557c478bd9Sstevel@tonic-gate #include <sys/mkdev.h> 567c478bd9Sstevel@tonic-gate #include <sys/stat.h> 577c478bd9Sstevel@tonic-gate #include <sys/elf.h> 587c478bd9Sstevel@tonic-gate #include <procfs.h> 597c478bd9Sstevel@tonic-gate #include <sys/core.h> 607c478bd9Sstevel@tonic-gate #include <sys/dumphdr.h> 617c478bd9Sstevel@tonic-gate #include <netinet/in.h> 627c478bd9Sstevel@tonic-gate #include <gelf.h> 637c478bd9Sstevel@tonic-gate #include <elfcap.h> 64c13de8f6Sab196087 #include <sgsrtcid.h> 657c478bd9Sstevel@tonic-gate #include "file.h" 667c478bd9Sstevel@tonic-gate 677c478bd9Sstevel@tonic-gate typedef Elf64_Nhdr GElf_Nhdr; 687c478bd9Sstevel@tonic-gate 697c478bd9Sstevel@tonic-gate /* 707c478bd9Sstevel@tonic-gate * Misc 717c478bd9Sstevel@tonic-gate */ 727c478bd9Sstevel@tonic-gate 737c478bd9Sstevel@tonic-gate #define FBSZ 512 747c478bd9Sstevel@tonic-gate #define MLIST_SZ 12 757c478bd9Sstevel@tonic-gate 767c478bd9Sstevel@tonic-gate /* 777c478bd9Sstevel@tonic-gate * The 0x8FCA0102 magic string was used in crash dumps generated by releases 787c478bd9Sstevel@tonic-gate * prior to Solaris 7. 797c478bd9Sstevel@tonic-gate */ 807c478bd9Sstevel@tonic-gate #define OLD_DUMP_MAGIC 0x8FCA0102 817c478bd9Sstevel@tonic-gate 827c478bd9Sstevel@tonic-gate #if defined(__sparc) 837c478bd9Sstevel@tonic-gate #define NATIVE_ISA "SPARC" 847c478bd9Sstevel@tonic-gate #define OTHER_ISA "Intel" 857c478bd9Sstevel@tonic-gate #else 867c478bd9Sstevel@tonic-gate #define NATIVE_ISA "Intel" 877c478bd9Sstevel@tonic-gate #define OTHER_ISA "SPARC" 887c478bd9Sstevel@tonic-gate #endif 897c478bd9Sstevel@tonic-gate 907c478bd9Sstevel@tonic-gate /* Assembly language comment char */ 917c478bd9Sstevel@tonic-gate #ifdef pdp11 927c478bd9Sstevel@tonic-gate #define ASCOMCHAR '/' 937c478bd9Sstevel@tonic-gate #else 947c478bd9Sstevel@tonic-gate #define ASCOMCHAR '!' 957c478bd9Sstevel@tonic-gate #endif 967c478bd9Sstevel@tonic-gate 977c478bd9Sstevel@tonic-gate #pragma align 16(fbuf) 987c478bd9Sstevel@tonic-gate static char fbuf[FBSZ]; 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate /* 1017c478bd9Sstevel@tonic-gate * Magic file variables 1027c478bd9Sstevel@tonic-gate */ 1037c478bd9Sstevel@tonic-gate static intmax_t maxmagicoffset; 1047c478bd9Sstevel@tonic-gate static intmax_t tmpmax; 1057c478bd9Sstevel@tonic-gate static char *magicbuf; 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate static char *dfile; 1087c478bd9Sstevel@tonic-gate static char *troff[] = { /* new troff intermediate lang */ 1097c478bd9Sstevel@tonic-gate "x", "T", "res", "init", "font", "202", "V0", "p1", 0}; 1107c478bd9Sstevel@tonic-gate 1117c478bd9Sstevel@tonic-gate static char *fort[] = { /* FORTRAN */ 1127c478bd9Sstevel@tonic-gate "function", "subroutine", "common", "dimension", "block", 1137c478bd9Sstevel@tonic-gate "integer", "real", "data", "double", 1147c478bd9Sstevel@tonic-gate "FUNCTION", "SUBROUTINE", "COMMON", "DIMENSION", "BLOCK", 1157c478bd9Sstevel@tonic-gate "INTEGER", "REAL", "DATA", "DOUBLE", 0}; 1167c478bd9Sstevel@tonic-gate 1177c478bd9Sstevel@tonic-gate static char *asc[] = { /* Assembler Commands */ 1187c478bd9Sstevel@tonic-gate "sys", "mov", "tst", "clr", "jmp", "cmp", "set", "inc", 1197c478bd9Sstevel@tonic-gate "dec", 0}; 1207c478bd9Sstevel@tonic-gate 1217c478bd9Sstevel@tonic-gate static char *c[] = { /* C Language */ 1227c478bd9Sstevel@tonic-gate "int", "char", "float", "double", "short", "long", "unsigned", 1237c478bd9Sstevel@tonic-gate "register", "static", "struct", "extern", 0}; 1247c478bd9Sstevel@tonic-gate 1257c478bd9Sstevel@tonic-gate static char *as[] = { /* Assembler Pseudo Ops, prepended with '.' */ 1267c478bd9Sstevel@tonic-gate "globl", "global", "ident", "file", "byte", "even", 1277c478bd9Sstevel@tonic-gate "text", "data", "bss", "comm", 0}; 1287c478bd9Sstevel@tonic-gate 1297c478bd9Sstevel@tonic-gate /* 1307c478bd9Sstevel@tonic-gate * The line and debug section names are used by the strip command. 1317c478bd9Sstevel@tonic-gate * Any changes in the strip implementation need to be reflected here. 1327c478bd9Sstevel@tonic-gate */ 1337c478bd9Sstevel@tonic-gate static char *debug_sections[] = { /* Debug sections in a ELF file */ 1347c478bd9Sstevel@tonic-gate ".debug", ".stab", ".dwarf", ".line", NULL}; 1357c478bd9Sstevel@tonic-gate 1367c478bd9Sstevel@tonic-gate 1377c478bd9Sstevel@tonic-gate /* start for MB env */ 1387c478bd9Sstevel@tonic-gate static wchar_t wchar; 1397c478bd9Sstevel@tonic-gate static int length; 1407c478bd9Sstevel@tonic-gate static int IS_ascii; 1417c478bd9Sstevel@tonic-gate static int Max; 1427c478bd9Sstevel@tonic-gate /* end for MB env */ 1437c478bd9Sstevel@tonic-gate static int i; /* global index into first 'fbsz' bytes of file */ 1447c478bd9Sstevel@tonic-gate static int fbsz; 1457c478bd9Sstevel@tonic-gate static int ifd = -1; 1467c478bd9Sstevel@tonic-gate static int elffd = -1; 1477c478bd9Sstevel@tonic-gate static int tret; 1487c478bd9Sstevel@tonic-gate static int hflg; 1497c478bd9Sstevel@tonic-gate static int dflg; 1507c478bd9Sstevel@tonic-gate static int mflg; 1517c478bd9Sstevel@tonic-gate static int M_flg; 1527c478bd9Sstevel@tonic-gate static int iflg; 1537c478bd9Sstevel@tonic-gate static struct stat64 mbuf; 1547c478bd9Sstevel@tonic-gate 1557c478bd9Sstevel@tonic-gate static char **mlist1; /* 1st ordered list of magic files */ 1567c478bd9Sstevel@tonic-gate static char **mlist2; /* 2nd ordered list of magic files */ 1577c478bd9Sstevel@tonic-gate static size_t mlist1_sz; /* number of ptrs allocated for mlist1 */ 1587c478bd9Sstevel@tonic-gate static size_t mlist2_sz; /* number of ptrs allocated for mlist2 */ 1597c478bd9Sstevel@tonic-gate static char **mlist1p; /* next entry in mlist1 */ 1607c478bd9Sstevel@tonic-gate static char **mlist2p; /* next entry in mlist2 */ 1617c478bd9Sstevel@tonic-gate 1627c478bd9Sstevel@tonic-gate static ssize_t mread; 1637c478bd9Sstevel@tonic-gate 1647c478bd9Sstevel@tonic-gate static void is_stripped(Elf *elf); 1657c478bd9Sstevel@tonic-gate static Elf *is_elf_file(int elffd); 1667c478bd9Sstevel@tonic-gate static void ar_coff_or_aout(int ifd); 1677c478bd9Sstevel@tonic-gate static int type(char *file); 168*9a411307Srie static int def_position_tests(char *file); 1697c478bd9Sstevel@tonic-gate static void def_context_tests(void); 1707c478bd9Sstevel@tonic-gate static int troffint(char *bp, int n); 1717c478bd9Sstevel@tonic-gate static int lookup(char **tab); 1727c478bd9Sstevel@tonic-gate static int ccom(void); 1737c478bd9Sstevel@tonic-gate static int ascom(void); 1747c478bd9Sstevel@tonic-gate static int sccs(void); 1757c478bd9Sstevel@tonic-gate static int english(char *bp, int n); 1767c478bd9Sstevel@tonic-gate static int old_core(Elf *elf, GElf_Ehdr *ehdr, int format); 177*9a411307Srie static int core(Elf *elf, GElf_Ehdr *ehdr, int format, char *file); 1787c478bd9Sstevel@tonic-gate static int shellscript(char buf[], struct stat64 *sb); 179*9a411307Srie static int elf_check(Elf *elf, char *file); 1807c478bd9Sstevel@tonic-gate static int get_door_target(char *, char *, size_t); 1817c478bd9Sstevel@tonic-gate static int zipfile(char *, int); 1827c478bd9Sstevel@tonic-gate static int is_crash_dump(const char *, int); 1837c478bd9Sstevel@tonic-gate static void print_dumphdr(const int, const dumphdr_t *, uint32_t (*)(uint32_t), 1847c478bd9Sstevel@tonic-gate const char *); 1857c478bd9Sstevel@tonic-gate static uint32_t swap_uint32(uint32_t); 1867c478bd9Sstevel@tonic-gate static uint32_t return_uint32(uint32_t); 1877c478bd9Sstevel@tonic-gate static int is_in_list(char *[], char *); 1887c478bd9Sstevel@tonic-gate static void usage(void); 1897c478bd9Sstevel@tonic-gate static void default_magic(void); 1907c478bd9Sstevel@tonic-gate static void add_to_mlist(char *, int); 1917c478bd9Sstevel@tonic-gate static void fd_cleanup(void); 192c13de8f6Sab196087 static int is_rtld_config(void); 1937c478bd9Sstevel@tonic-gate 1947c478bd9Sstevel@tonic-gate #ifdef XPG4 1957c478bd9Sstevel@tonic-gate /* SUSv3 requires a single <space> after the colon */ 1967c478bd9Sstevel@tonic-gate #define prf(x) (void) printf("%s: ", x); 1977c478bd9Sstevel@tonic-gate #else /* !XPG4 */ 1987c478bd9Sstevel@tonic-gate #define prf(x) (void) printf("%s:%s", x, (int)strlen(x) > 6 ? "\t" : "\t\t"); 1997c478bd9Sstevel@tonic-gate #endif /* XPG4 */ 2007c478bd9Sstevel@tonic-gate 201*9a411307Srie /* 202*9a411307Srie * Static program identifier - used to prevent localization of the name "file" 203*9a411307Srie * within individual error messages. 204*9a411307Srie */ 205*9a411307Srie const char *File = "file"; 206*9a411307Srie 2077c478bd9Sstevel@tonic-gate int 2087c478bd9Sstevel@tonic-gate main(int argc, char **argv) 2097c478bd9Sstevel@tonic-gate { 2107c478bd9Sstevel@tonic-gate char *p; 2117c478bd9Sstevel@tonic-gate int ch; 2127c478bd9Sstevel@tonic-gate FILE *fl; 2137c478bd9Sstevel@tonic-gate int cflg = 0; 2147c478bd9Sstevel@tonic-gate int eflg = 0; 2157c478bd9Sstevel@tonic-gate int fflg = 0; 2167c478bd9Sstevel@tonic-gate char *ap = NULL; 2177c478bd9Sstevel@tonic-gate int pathlen; 2187c478bd9Sstevel@tonic-gate char **filep; 2197c478bd9Sstevel@tonic-gate 2207c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 2217c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */ 2227c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */ 2237c478bd9Sstevel@tonic-gate #endif 2247c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 2257c478bd9Sstevel@tonic-gate 2267c478bd9Sstevel@tonic-gate while ((ch = getopt(argc, argv, "M:cdf:him:")) != EOF) { 2277c478bd9Sstevel@tonic-gate switch (ch) { 2287c478bd9Sstevel@tonic-gate 2297c478bd9Sstevel@tonic-gate case 'M': 2307c478bd9Sstevel@tonic-gate add_to_mlist(optarg, !dflg); 2317c478bd9Sstevel@tonic-gate M_flg++; 2327c478bd9Sstevel@tonic-gate break; 2337c478bd9Sstevel@tonic-gate 2347c478bd9Sstevel@tonic-gate case 'c': 2357c478bd9Sstevel@tonic-gate cflg++; 2367c478bd9Sstevel@tonic-gate break; 2377c478bd9Sstevel@tonic-gate 2387c478bd9Sstevel@tonic-gate case 'd': 2397c478bd9Sstevel@tonic-gate if (!dflg) { 2407c478bd9Sstevel@tonic-gate default_magic(); 2417c478bd9Sstevel@tonic-gate add_to_mlist(dfile, 0); 2427c478bd9Sstevel@tonic-gate dflg++; 2437c478bd9Sstevel@tonic-gate } 2447c478bd9Sstevel@tonic-gate break; 2457c478bd9Sstevel@tonic-gate 2467c478bd9Sstevel@tonic-gate case 'f': 2477c478bd9Sstevel@tonic-gate fflg++; 248*9a411307Srie errno = 0; 2497c478bd9Sstevel@tonic-gate if ((fl = fopen(optarg, "r")) == NULL) { 250*9a411307Srie int err = errno; 251*9a411307Srie (void) fprintf(stderr, gettext("%s: cannot " 252*9a411307Srie "open file %s: %s\n"), File, optarg, 253*9a411307Srie err ? strerror(err) : ""); 2547c478bd9Sstevel@tonic-gate usage(); 2557c478bd9Sstevel@tonic-gate } 2567c478bd9Sstevel@tonic-gate pathlen = pathconf("/", _PC_PATH_MAX); 2577c478bd9Sstevel@tonic-gate if (pathlen == -1) { 258*9a411307Srie int err = errno; 259*9a411307Srie (void) fprintf(stderr, gettext("%s: cannot " 260*9a411307Srie "determine maximum path length: %s\n"), 261*9a411307Srie File, strerror(err)); 2627c478bd9Sstevel@tonic-gate exit(1); 2637c478bd9Sstevel@tonic-gate } 2647c478bd9Sstevel@tonic-gate pathlen += 2; /* for null and newline in fgets */ 265*9a411307Srie if ((ap = malloc(pathlen * sizeof (char))) == NULL) { 266*9a411307Srie int err = errno; 267*9a411307Srie (void) fprintf(stderr, gettext("%s: malloc " 268*9a411307Srie "failed: %s\n"), File, strerror(err)); 269*9a411307Srie exit(2); 2707c478bd9Sstevel@tonic-gate } 2717c478bd9Sstevel@tonic-gate break; 2727c478bd9Sstevel@tonic-gate 2737c478bd9Sstevel@tonic-gate case 'h': 2747c478bd9Sstevel@tonic-gate hflg++; 2757c478bd9Sstevel@tonic-gate break; 2767c478bd9Sstevel@tonic-gate 2777c478bd9Sstevel@tonic-gate case 'i': 2787c478bd9Sstevel@tonic-gate iflg++; 2797c478bd9Sstevel@tonic-gate break; 2807c478bd9Sstevel@tonic-gate 2817c478bd9Sstevel@tonic-gate case 'm': 2827c478bd9Sstevel@tonic-gate add_to_mlist(optarg, !dflg); 2837c478bd9Sstevel@tonic-gate mflg++; 2847c478bd9Sstevel@tonic-gate break; 2857c478bd9Sstevel@tonic-gate 2867c478bd9Sstevel@tonic-gate case '?': 2877c478bd9Sstevel@tonic-gate eflg++; 2887c478bd9Sstevel@tonic-gate break; 2897c478bd9Sstevel@tonic-gate } 2907c478bd9Sstevel@tonic-gate } 2917c478bd9Sstevel@tonic-gate if (!cflg && !fflg && (eflg || optind == argc)) 2927c478bd9Sstevel@tonic-gate usage(); 2937c478bd9Sstevel@tonic-gate if (iflg && (dflg || mflg || M_flg)) { 2947c478bd9Sstevel@tonic-gate usage(); 2957c478bd9Sstevel@tonic-gate } 2967c478bd9Sstevel@tonic-gate if (iflg && cflg) { 2977c478bd9Sstevel@tonic-gate usage(); 2987c478bd9Sstevel@tonic-gate } 2997c478bd9Sstevel@tonic-gate 3007c478bd9Sstevel@tonic-gate if (!dflg && !mflg && !M_flg && !iflg) { 3017c478bd9Sstevel@tonic-gate /* no -d, -m, nor -M option; also -i option doesn't need magic */ 3027c478bd9Sstevel@tonic-gate default_magic(); 3037c478bd9Sstevel@tonic-gate if (f_mkmtab(dfile, cflg, 0) == -1) { 3047c478bd9Sstevel@tonic-gate exit(2); 3057c478bd9Sstevel@tonic-gate } 3067c478bd9Sstevel@tonic-gate } 3077c478bd9Sstevel@tonic-gate 3087c478bd9Sstevel@tonic-gate else if (mflg && !M_flg && !dflg) { 3097c478bd9Sstevel@tonic-gate /* -m specified without -d nor -M */ 3107c478bd9Sstevel@tonic-gate 3117c478bd9Sstevel@tonic-gate #ifdef XPG4 /* For SUSv3 only */ 3127c478bd9Sstevel@tonic-gate 3137c478bd9Sstevel@tonic-gate /* 3147c478bd9Sstevel@tonic-gate * The default position-dependent magic file tests 3157c478bd9Sstevel@tonic-gate * in /etc/magic will follow all the -m magic tests. 3167c478bd9Sstevel@tonic-gate */ 3177c478bd9Sstevel@tonic-gate 3187c478bd9Sstevel@tonic-gate for (filep = mlist1; filep < mlist1p; filep++) { 3197c478bd9Sstevel@tonic-gate if (f_mkmtab(*filep, cflg, 1) == -1) { 3207c478bd9Sstevel@tonic-gate exit(2); 3217c478bd9Sstevel@tonic-gate } 3227c478bd9Sstevel@tonic-gate } 3237c478bd9Sstevel@tonic-gate default_magic(); 3247c478bd9Sstevel@tonic-gate if (f_mkmtab(dfile, cflg, 0) == -1) { 3257c478bd9Sstevel@tonic-gate exit(2); 3267c478bd9Sstevel@tonic-gate } 3277c478bd9Sstevel@tonic-gate #else /* !XPG4 */ 3287c478bd9Sstevel@tonic-gate /* 3297c478bd9Sstevel@tonic-gate * Retain Solaris file behavior for -m before SUSv3, 3307c478bd9Sstevel@tonic-gate * when the new -d and -M options are not specified. 3317c478bd9Sstevel@tonic-gate * Use the -m file specified in place of the default 3327c478bd9Sstevel@tonic-gate * /etc/magic file. Solaris file will 3337c478bd9Sstevel@tonic-gate * now allow more than one magic file to be specified 3347c478bd9Sstevel@tonic-gate * with multiple -m options, for consistency with 3357c478bd9Sstevel@tonic-gate * other behavior. 3367c478bd9Sstevel@tonic-gate * 3377c478bd9Sstevel@tonic-gate * Put the magic table(s) specified by -m into 3387c478bd9Sstevel@tonic-gate * the second magic table instead of the first 3397c478bd9Sstevel@tonic-gate * (as indicated by the last argument to f_mkmtab()), 3407c478bd9Sstevel@tonic-gate * since they replace the /etc/magic tests and 3417c478bd9Sstevel@tonic-gate * must be executed alongside the default 3427c478bd9Sstevel@tonic-gate * position-sensitive tests. 3437c478bd9Sstevel@tonic-gate */ 3447c478bd9Sstevel@tonic-gate 3457c478bd9Sstevel@tonic-gate for (filep = mlist1; filep < mlist1p; filep++) { 3467c478bd9Sstevel@tonic-gate if (f_mkmtab(*filep, cflg, 0) == -1) { 3477c478bd9Sstevel@tonic-gate exit(2); 3487c478bd9Sstevel@tonic-gate } 3497c478bd9Sstevel@tonic-gate } 3507c478bd9Sstevel@tonic-gate #endif /* XPG4 */ 3517c478bd9Sstevel@tonic-gate } else { 3527c478bd9Sstevel@tonic-gate /* 3537c478bd9Sstevel@tonic-gate * For any other combination of -d, -m, and -M, 3547c478bd9Sstevel@tonic-gate * use the magic files in command-line order. 3557c478bd9Sstevel@tonic-gate * Store the entries from the two separate lists of magic 3567c478bd9Sstevel@tonic-gate * files, if any, into two separate magic file tables. 3577c478bd9Sstevel@tonic-gate * mlist1: magic tests executed before default magic tests 3587c478bd9Sstevel@tonic-gate * mlist2: default magic tests and after 3597c478bd9Sstevel@tonic-gate */ 3607c478bd9Sstevel@tonic-gate for (filep = mlist1; filep && (filep < mlist1p); filep++) { 3617c478bd9Sstevel@tonic-gate if (f_mkmtab(*filep, cflg, 1) == -1) { 3627c478bd9Sstevel@tonic-gate exit(2); 3637c478bd9Sstevel@tonic-gate } 3647c478bd9Sstevel@tonic-gate } 3657c478bd9Sstevel@tonic-gate for (filep = mlist2; filep && (filep < mlist2p); filep++) { 3667c478bd9Sstevel@tonic-gate if (f_mkmtab(*filep, cflg, 0) == -1) { 3677c478bd9Sstevel@tonic-gate exit(2); 3687c478bd9Sstevel@tonic-gate } 3697c478bd9Sstevel@tonic-gate } 3707c478bd9Sstevel@tonic-gate } 3717c478bd9Sstevel@tonic-gate 3727c478bd9Sstevel@tonic-gate /* Initialize the magic file variables; check both magic tables */ 3737c478bd9Sstevel@tonic-gate tmpmax = f_getmaxoffset(1); 3747c478bd9Sstevel@tonic-gate maxmagicoffset = f_getmaxoffset(0); 3757c478bd9Sstevel@tonic-gate if (maxmagicoffset < tmpmax) { 3767c478bd9Sstevel@tonic-gate maxmagicoffset = tmpmax; 3777c478bd9Sstevel@tonic-gate } 3787c478bd9Sstevel@tonic-gate if (maxmagicoffset < (intmax_t)FBSZ) 3797c478bd9Sstevel@tonic-gate maxmagicoffset = (intmax_t)FBSZ; 380*9a411307Srie if ((magicbuf = malloc(maxmagicoffset)) == NULL) { 381*9a411307Srie int err = errno; 382*9a411307Srie (void) fprintf(stderr, gettext("%s: malloc failed: %s\n"), 383*9a411307Srie File, strerror(err)); 3847c478bd9Sstevel@tonic-gate exit(2); 3857c478bd9Sstevel@tonic-gate } 3867c478bd9Sstevel@tonic-gate 3877c478bd9Sstevel@tonic-gate if (cflg) { 3887c478bd9Sstevel@tonic-gate f_prtmtab(); 3897c478bd9Sstevel@tonic-gate if (ferror(stdout) != 0) { 390*9a411307Srie (void) fprintf(stderr, gettext("%s: error writing to " 391*9a411307Srie "stdout\n"), File); 3927c478bd9Sstevel@tonic-gate exit(1); 3937c478bd9Sstevel@tonic-gate } 3947c478bd9Sstevel@tonic-gate if (fclose(stdout) != 0) { 395*9a411307Srie int err = errno; 396*9a411307Srie (void) fprintf(stderr, gettext("%s: fclose " 397*9a411307Srie "failed: %s\n"), File, strerror(err)); 3987c478bd9Sstevel@tonic-gate exit(1); 3997c478bd9Sstevel@tonic-gate } 4007c478bd9Sstevel@tonic-gate exit(0); 4017c478bd9Sstevel@tonic-gate } 402*9a411307Srie 4037c478bd9Sstevel@tonic-gate for (; fflg || optind < argc; optind += !fflg) { 4047c478bd9Sstevel@tonic-gate register int l; 4057c478bd9Sstevel@tonic-gate 4067c478bd9Sstevel@tonic-gate if (fflg) { 4077c478bd9Sstevel@tonic-gate if ((p = fgets(ap, pathlen, fl)) == NULL) { 4087c478bd9Sstevel@tonic-gate fflg = 0; 4097c478bd9Sstevel@tonic-gate optind--; 4107c478bd9Sstevel@tonic-gate continue; 4117c478bd9Sstevel@tonic-gate } 4127c478bd9Sstevel@tonic-gate l = strlen(p); 4137c478bd9Sstevel@tonic-gate if (l > 0) 4147c478bd9Sstevel@tonic-gate p[l - 1] = '\0'; 4157c478bd9Sstevel@tonic-gate } else 4167c478bd9Sstevel@tonic-gate p = argv[optind]; 4177c478bd9Sstevel@tonic-gate prf(p); /* print "file_name:<tab>" */ 4187c478bd9Sstevel@tonic-gate 4197c478bd9Sstevel@tonic-gate if (type(p)) 4207c478bd9Sstevel@tonic-gate tret = 1; 4217c478bd9Sstevel@tonic-gate } 4227c478bd9Sstevel@tonic-gate if (ap != NULL) 4237c478bd9Sstevel@tonic-gate free(ap); 424*9a411307Srie if (tret != 0) 4257c478bd9Sstevel@tonic-gate exit(tret); 426*9a411307Srie 4277c478bd9Sstevel@tonic-gate if (ferror(stdout) != 0) { 428*9a411307Srie (void) fprintf(stderr, gettext("%s: error writing to " 429*9a411307Srie "stdout\n"), File); 4307c478bd9Sstevel@tonic-gate exit(1); 4317c478bd9Sstevel@tonic-gate } 4327c478bd9Sstevel@tonic-gate if (fclose(stdout) != 0) { 433*9a411307Srie int err = errno; 434*9a411307Srie (void) fprintf(stderr, gettext("%s: fclose failed: %s\n"), 435*9a411307Srie File, strerror(err)); 4367c478bd9Sstevel@tonic-gate exit(1); 4377c478bd9Sstevel@tonic-gate } 4387c478bd9Sstevel@tonic-gate return (0); 4397c478bd9Sstevel@tonic-gate } 4407c478bd9Sstevel@tonic-gate 4417c478bd9Sstevel@tonic-gate static int 4427c478bd9Sstevel@tonic-gate type(char *file) 4437c478bd9Sstevel@tonic-gate { 4447c478bd9Sstevel@tonic-gate int cc; 4457c478bd9Sstevel@tonic-gate char buf[BUFSIZ]; 4467c478bd9Sstevel@tonic-gate int (*statf)() = hflg ? lstat64 : stat64; 4477c478bd9Sstevel@tonic-gate 4487c478bd9Sstevel@tonic-gate i = 0; /* reset index to beginning of file */ 4497c478bd9Sstevel@tonic-gate ifd = -1; 4507c478bd9Sstevel@tonic-gate if ((*statf)(file, &mbuf) < 0) { 4517c478bd9Sstevel@tonic-gate if (statf == lstat64 || lstat64(file, &mbuf) < 0) { 452*9a411307Srie int err = errno; 4537c478bd9Sstevel@tonic-gate (void) printf(gettext("cannot open: %s\n"), 454*9a411307Srie strerror(err)); 4557c478bd9Sstevel@tonic-gate return (0); /* POSIX.2 */ 4567c478bd9Sstevel@tonic-gate } 4577c478bd9Sstevel@tonic-gate } 4587c478bd9Sstevel@tonic-gate switch (mbuf.st_mode & S_IFMT) { 4597c478bd9Sstevel@tonic-gate case S_IFREG: 4607c478bd9Sstevel@tonic-gate if (iflg) { 4617c478bd9Sstevel@tonic-gate (void) printf(gettext("regular file\n")); 4627c478bd9Sstevel@tonic-gate return (0); 4637c478bd9Sstevel@tonic-gate } 4647c478bd9Sstevel@tonic-gate break; 4657c478bd9Sstevel@tonic-gate case S_IFCHR: 4667c478bd9Sstevel@tonic-gate (void) printf(gettext("character")); 4677c478bd9Sstevel@tonic-gate goto spcl; 4687c478bd9Sstevel@tonic-gate 4697c478bd9Sstevel@tonic-gate case S_IFDIR: 4707c478bd9Sstevel@tonic-gate (void) printf(gettext("directory\n")); 4717c478bd9Sstevel@tonic-gate return (0); 4727c478bd9Sstevel@tonic-gate 4737c478bd9Sstevel@tonic-gate case S_IFIFO: 4747c478bd9Sstevel@tonic-gate (void) printf(gettext("fifo\n")); 4757c478bd9Sstevel@tonic-gate return (0); 4767c478bd9Sstevel@tonic-gate 4777c478bd9Sstevel@tonic-gate case S_IFLNK: 4787c478bd9Sstevel@tonic-gate if ((cc = readlink(file, buf, BUFSIZ)) < 0) { 479*9a411307Srie int err = errno; 4807c478bd9Sstevel@tonic-gate (void) printf(gettext("readlink error: %s\n"), 481*9a411307Srie strerror(err)); 4827c478bd9Sstevel@tonic-gate return (1); 4837c478bd9Sstevel@tonic-gate } 4847c478bd9Sstevel@tonic-gate buf[cc] = '\0'; 4857c478bd9Sstevel@tonic-gate (void) printf(gettext("symbolic link to %s\n"), buf); 4867c478bd9Sstevel@tonic-gate return (0); 4877c478bd9Sstevel@tonic-gate 4887c478bd9Sstevel@tonic-gate case S_IFBLK: 4897c478bd9Sstevel@tonic-gate (void) printf(gettext("block")); 4907c478bd9Sstevel@tonic-gate /* major and minor, see sys/mkdev.h */ 4917c478bd9Sstevel@tonic-gate spcl: 4927c478bd9Sstevel@tonic-gate (void) printf(gettext(" special (%d/%d)\n"), 4937c478bd9Sstevel@tonic-gate major(mbuf.st_rdev), minor(mbuf.st_rdev)); 4947c478bd9Sstevel@tonic-gate return (0); 4957c478bd9Sstevel@tonic-gate 4967c478bd9Sstevel@tonic-gate case S_IFSOCK: 4977c478bd9Sstevel@tonic-gate (void) printf("socket\n"); 4987c478bd9Sstevel@tonic-gate /* FIXME, should open and try to getsockname. */ 4997c478bd9Sstevel@tonic-gate return (0); 5007c478bd9Sstevel@tonic-gate 5017c478bd9Sstevel@tonic-gate case S_IFDOOR: 5027c478bd9Sstevel@tonic-gate if (get_door_target(file, buf, sizeof (buf)) == 0) 5037c478bd9Sstevel@tonic-gate (void) printf(gettext("door to %s\n"), buf); 5047c478bd9Sstevel@tonic-gate else 5057c478bd9Sstevel@tonic-gate (void) printf(gettext("door\n")); 5067c478bd9Sstevel@tonic-gate return (0); 5077c478bd9Sstevel@tonic-gate 5087c478bd9Sstevel@tonic-gate } 5097c478bd9Sstevel@tonic-gate 5107c478bd9Sstevel@tonic-gate if (elf_version(EV_CURRENT) == EV_NONE) { 5117c478bd9Sstevel@tonic-gate (void) printf(gettext("libelf is out of date\n")); 5127c478bd9Sstevel@tonic-gate return (1); 5137c478bd9Sstevel@tonic-gate } 5147c478bd9Sstevel@tonic-gate 5157c478bd9Sstevel@tonic-gate ifd = open64(file, O_RDONLY); 5167c478bd9Sstevel@tonic-gate if (ifd < 0) { 517*9a411307Srie int err = errno; 518*9a411307Srie (void) printf(gettext("cannot open: %s\n"), strerror(err)); 5197c478bd9Sstevel@tonic-gate return (0); /* POSIX.2 */ 5207c478bd9Sstevel@tonic-gate } 5217c478bd9Sstevel@tonic-gate 5227c478bd9Sstevel@tonic-gate /* need another fd for elf, since we might want to read the file too */ 5237c478bd9Sstevel@tonic-gate elffd = open64(file, O_RDONLY); 5247c478bd9Sstevel@tonic-gate if (elffd < 0) { 525*9a411307Srie int err = errno; 526*9a411307Srie (void) printf(gettext("cannot open: %s\n"), strerror(err)); 5277c478bd9Sstevel@tonic-gate (void) close(ifd); 5287c478bd9Sstevel@tonic-gate ifd = -1; 5297c478bd9Sstevel@tonic-gate return (0); /* POSIX.2 */ 5307c478bd9Sstevel@tonic-gate } 5317c478bd9Sstevel@tonic-gate if ((fbsz = read(ifd, fbuf, FBSZ)) == -1) { 532*9a411307Srie int err = errno; 533*9a411307Srie (void) printf(gettext("cannot read: %s\n"), strerror(err)); 5347c478bd9Sstevel@tonic-gate (void) close(ifd); 5357c478bd9Sstevel@tonic-gate ifd = -1; 5367c478bd9Sstevel@tonic-gate return (0); /* POSIX.2 */ 5377c478bd9Sstevel@tonic-gate } 5387c478bd9Sstevel@tonic-gate if (fbsz == 0) { 5397c478bd9Sstevel@tonic-gate (void) printf(gettext("empty file\n")); 5407c478bd9Sstevel@tonic-gate fd_cleanup(); 5417c478bd9Sstevel@tonic-gate return (0); 5427c478bd9Sstevel@tonic-gate } 5437c478bd9Sstevel@tonic-gate 5447c478bd9Sstevel@tonic-gate /* 5457c478bd9Sstevel@tonic-gate * First try user-specified position-dependent magic tests, if any, 5467c478bd9Sstevel@tonic-gate * which need to execute before the default tests. 5477c478bd9Sstevel@tonic-gate */ 5487c478bd9Sstevel@tonic-gate if ((mread = pread(ifd, (void*)magicbuf, (size_t)maxmagicoffset, 5497c478bd9Sstevel@tonic-gate (off_t)0)) == -1) { 550*9a411307Srie int err = errno; 551*9a411307Srie (void) printf(gettext("cannot read: %s\n"), strerror(err)); 5527c478bd9Sstevel@tonic-gate fd_cleanup(); 5537c478bd9Sstevel@tonic-gate return (0); 5547c478bd9Sstevel@tonic-gate } 5557c478bd9Sstevel@tonic-gate 5567c478bd9Sstevel@tonic-gate /* 5577c478bd9Sstevel@tonic-gate * ChecK against Magic Table entries. 5587c478bd9Sstevel@tonic-gate * Check first magic table for magic tests to be applied 5597c478bd9Sstevel@tonic-gate * before default tests. 5607c478bd9Sstevel@tonic-gate * If no default tests are to be applied, all magic tests 5617c478bd9Sstevel@tonic-gate * should occur in this magic table. 5627c478bd9Sstevel@tonic-gate */ 5637c478bd9Sstevel@tonic-gate switch (f_ckmtab(magicbuf, mread, 1)) { 5647c478bd9Sstevel@tonic-gate case -1: /* Error */ 5657c478bd9Sstevel@tonic-gate exit(2); 5667c478bd9Sstevel@tonic-gate break; 5677c478bd9Sstevel@tonic-gate case 0: /* Not magic */ 5687c478bd9Sstevel@tonic-gate break; 5697c478bd9Sstevel@tonic-gate default: /* Switch is magic index */ 5707c478bd9Sstevel@tonic-gate (void) putchar('\n'); 5717c478bd9Sstevel@tonic-gate fd_cleanup(); 5727c478bd9Sstevel@tonic-gate return (0); 5737c478bd9Sstevel@tonic-gate /* NOTREACHED */ 5747c478bd9Sstevel@tonic-gate break; 5757c478bd9Sstevel@tonic-gate } 5767c478bd9Sstevel@tonic-gate 5777c478bd9Sstevel@tonic-gate if (dflg || !M_flg) { 5787c478bd9Sstevel@tonic-gate /* 5797c478bd9Sstevel@tonic-gate * default position-dependent tests, 5807c478bd9Sstevel@tonic-gate * plus non-default magic tests, if any 5817c478bd9Sstevel@tonic-gate */ 582*9a411307Srie switch (def_position_tests(file)) { 5837c478bd9Sstevel@tonic-gate case -1: /* error */ 5847c478bd9Sstevel@tonic-gate fd_cleanup(); 5857c478bd9Sstevel@tonic-gate return (1); 5867c478bd9Sstevel@tonic-gate case 1: /* matching type found */ 5877c478bd9Sstevel@tonic-gate fd_cleanup(); 5887c478bd9Sstevel@tonic-gate return (0); 5897c478bd9Sstevel@tonic-gate /* NOTREACHED */ 5907c478bd9Sstevel@tonic-gate break; 5917c478bd9Sstevel@tonic-gate case 0: /* no matching type found */ 5927c478bd9Sstevel@tonic-gate break; 5937c478bd9Sstevel@tonic-gate } 5947c478bd9Sstevel@tonic-gate /* default context-sensitive tests */ 5957c478bd9Sstevel@tonic-gate def_context_tests(); 5967c478bd9Sstevel@tonic-gate } else { 5977c478bd9Sstevel@tonic-gate /* no more tests to apply; no match was found */ 5987c478bd9Sstevel@tonic-gate (void) printf(gettext("data\n")); 5997c478bd9Sstevel@tonic-gate } 6007c478bd9Sstevel@tonic-gate fd_cleanup(); 6017c478bd9Sstevel@tonic-gate return (0); 6027c478bd9Sstevel@tonic-gate } 6037c478bd9Sstevel@tonic-gate 6047c478bd9Sstevel@tonic-gate /* 6057c478bd9Sstevel@tonic-gate * def_position_tests() - applies default position-sensitive tests, 6067c478bd9Sstevel@tonic-gate * looking for values in specific positions in the file. 6077c478bd9Sstevel@tonic-gate * These are followed by default (followed by possibly some 6087c478bd9Sstevel@tonic-gate * non-default) magic file tests. 6097c478bd9Sstevel@tonic-gate * 6107c478bd9Sstevel@tonic-gate * All position-sensitive tests, default or otherwise, must 6117c478bd9Sstevel@tonic-gate * be applied before context-sensitive tests, to avoid 6127c478bd9Sstevel@tonic-gate * false context-sensitive matches. 6137c478bd9Sstevel@tonic-gate * 6147c478bd9Sstevel@tonic-gate * Returns -1 on error which should result in error (non-zero) 6157c478bd9Sstevel@tonic-gate * exit status for the file utility. 6167c478bd9Sstevel@tonic-gate * Returns 0 if no matching file type found. 6177c478bd9Sstevel@tonic-gate * Returns 1 if matching file type found. 6187c478bd9Sstevel@tonic-gate */ 6197c478bd9Sstevel@tonic-gate 6207c478bd9Sstevel@tonic-gate static int 621*9a411307Srie def_position_tests(char *file) 6227c478bd9Sstevel@tonic-gate { 6237c478bd9Sstevel@tonic-gate Elf *elf; 6247c478bd9Sstevel@tonic-gate 6257c478bd9Sstevel@tonic-gate if (sccs()) { /* look for "1hddddd" where d is a digit */ 6267c478bd9Sstevel@tonic-gate (void) printf("sccs \n"); 6277c478bd9Sstevel@tonic-gate return (1); 6287c478bd9Sstevel@tonic-gate } 6297c478bd9Sstevel@tonic-gate if (fbuf[0] == '#' && fbuf[1] == '!' && shellscript(fbuf+2, &mbuf)) 6307c478bd9Sstevel@tonic-gate return (1); 6317c478bd9Sstevel@tonic-gate if ((elf = is_elf_file(elffd)) != NULL) { 632*9a411307Srie (void) elf_check(elf, file); 6337c478bd9Sstevel@tonic-gate (void) elf_end(elf); 6347c478bd9Sstevel@tonic-gate (void) putchar('\n'); 6357c478bd9Sstevel@tonic-gate return (1); 6367c478bd9Sstevel@tonic-gate 637c13de8f6Sab196087 6387c478bd9Sstevel@tonic-gate /* LINTED: pointer cast may result in improper alignment */ 6397c478bd9Sstevel@tonic-gate } else if (*(int *)fbuf == CORE_MAGIC) { 6407c478bd9Sstevel@tonic-gate /* LINTED: pointer cast may result in improper alignment */ 6417c478bd9Sstevel@tonic-gate struct core *corep = (struct core *)fbuf; 6427c478bd9Sstevel@tonic-gate 6437c478bd9Sstevel@tonic-gate (void) printf("a.out core file"); 6447c478bd9Sstevel@tonic-gate 6457c478bd9Sstevel@tonic-gate if (*(corep->c_cmdname) != '\0') 6467c478bd9Sstevel@tonic-gate (void) printf(" from '%s'", corep->c_cmdname); 6477c478bd9Sstevel@tonic-gate (void) putchar('\n'); 6487c478bd9Sstevel@tonic-gate return (1); 6497c478bd9Sstevel@tonic-gate } 6507c478bd9Sstevel@tonic-gate 6517c478bd9Sstevel@tonic-gate /* 652c13de8f6Sab196087 * Runtime linker (ld.so.1) configuration file. 653c13de8f6Sab196087 */ 654c13de8f6Sab196087 if (is_rtld_config()) 655c13de8f6Sab196087 return (1); 656c13de8f6Sab196087 657c13de8f6Sab196087 /* 6587c478bd9Sstevel@tonic-gate * ZIP files, JAR files, and Java executables 6597c478bd9Sstevel@tonic-gate */ 6607c478bd9Sstevel@tonic-gate if (zipfile(fbuf, ifd)) 6617c478bd9Sstevel@tonic-gate return (1); 6627c478bd9Sstevel@tonic-gate 6637c478bd9Sstevel@tonic-gate if (is_crash_dump(fbuf, ifd)) 6647c478bd9Sstevel@tonic-gate return (1); 6657c478bd9Sstevel@tonic-gate 6667c478bd9Sstevel@tonic-gate /* 6677c478bd9Sstevel@tonic-gate * ChecK against Magic Table entries. 6687c478bd9Sstevel@tonic-gate * The magic entries checked here always start with default 6697c478bd9Sstevel@tonic-gate * magic tests and may be followed by other, non-default magic 6707c478bd9Sstevel@tonic-gate * tests. If no default tests are to be executed, all the 6717c478bd9Sstevel@tonic-gate * magic tests should have been in the first magic table. 6727c478bd9Sstevel@tonic-gate */ 6737c478bd9Sstevel@tonic-gate switch (f_ckmtab(magicbuf, mread, 0)) { 6747c478bd9Sstevel@tonic-gate case -1: /* Error */ 6757c478bd9Sstevel@tonic-gate exit(2); 6767c478bd9Sstevel@tonic-gate break; 6777c478bd9Sstevel@tonic-gate case 0: /* Not magic */ 6787c478bd9Sstevel@tonic-gate return (0); 6797c478bd9Sstevel@tonic-gate /* NOTREACHED */ 6807c478bd9Sstevel@tonic-gate break; 6817c478bd9Sstevel@tonic-gate default: /* Switch is magic index */ 6827c478bd9Sstevel@tonic-gate 6837c478bd9Sstevel@tonic-gate /* 6847c478bd9Sstevel@tonic-gate * f_ckmtab recognizes file type, 6857c478bd9Sstevel@tonic-gate * check if it is PostScript. 6867c478bd9Sstevel@tonic-gate * if not, check if elf or a.out 6877c478bd9Sstevel@tonic-gate */ 6887c478bd9Sstevel@tonic-gate if (magicbuf[0] == '%' && magicbuf[1] == '!') { 6897c478bd9Sstevel@tonic-gate (void) putchar('\n'); 6907c478bd9Sstevel@tonic-gate } else { 6917c478bd9Sstevel@tonic-gate 6927c478bd9Sstevel@tonic-gate /* 6937c478bd9Sstevel@tonic-gate * Check that the file is executable (dynamic 6947c478bd9Sstevel@tonic-gate * objects must be executable to be exec'ed, 6957c478bd9Sstevel@tonic-gate * shared objects need not be, but by convention 6967c478bd9Sstevel@tonic-gate * should be executable). 6977c478bd9Sstevel@tonic-gate * 6987c478bd9Sstevel@tonic-gate * Note that we should already have processed 6997c478bd9Sstevel@tonic-gate * the file if it was an ELF file. 7007c478bd9Sstevel@tonic-gate */ 7017c478bd9Sstevel@tonic-gate ar_coff_or_aout(elffd); 7027c478bd9Sstevel@tonic-gate (void) putchar('\n'); 7037c478bd9Sstevel@tonic-gate } 7047c478bd9Sstevel@tonic-gate return (1); 7057c478bd9Sstevel@tonic-gate /* NOTREACHED */ 7067c478bd9Sstevel@tonic-gate break; 7077c478bd9Sstevel@tonic-gate } 7087c478bd9Sstevel@tonic-gate 7097c478bd9Sstevel@tonic-gate return (0); /* file was not identified */ 7107c478bd9Sstevel@tonic-gate } 7117c478bd9Sstevel@tonic-gate 7127c478bd9Sstevel@tonic-gate /* 7137c478bd9Sstevel@tonic-gate * def_context_tests() - default context-sensitive tests. 7147c478bd9Sstevel@tonic-gate * These are the last tests to be applied. 7157c478bd9Sstevel@tonic-gate * If no match is found, prints out "data". 7167c478bd9Sstevel@tonic-gate */ 7177c478bd9Sstevel@tonic-gate 7187c478bd9Sstevel@tonic-gate static void 7197c478bd9Sstevel@tonic-gate def_context_tests(void) 7207c478bd9Sstevel@tonic-gate { 7217c478bd9Sstevel@tonic-gate int j; 7227c478bd9Sstevel@tonic-gate int nl; 7237c478bd9Sstevel@tonic-gate char ch; 7247c478bd9Sstevel@tonic-gate int len; 7257c478bd9Sstevel@tonic-gate 7267c478bd9Sstevel@tonic-gate if (ccom() == 0) 7277c478bd9Sstevel@tonic-gate goto notc; 7287c478bd9Sstevel@tonic-gate while (fbuf[i] == '#') { 7297c478bd9Sstevel@tonic-gate j = i; 7307c478bd9Sstevel@tonic-gate while (fbuf[i++] != '\n') { 7317c478bd9Sstevel@tonic-gate if (i - j > 255) { 7327c478bd9Sstevel@tonic-gate (void) printf(gettext("data\n")); 7337c478bd9Sstevel@tonic-gate return; 7347c478bd9Sstevel@tonic-gate } 7357c478bd9Sstevel@tonic-gate if (i >= fbsz) 7367c478bd9Sstevel@tonic-gate goto notc; 7377c478bd9Sstevel@tonic-gate } 7387c478bd9Sstevel@tonic-gate if (ccom() == 0) 7397c478bd9Sstevel@tonic-gate goto notc; 7407c478bd9Sstevel@tonic-gate } 7417c478bd9Sstevel@tonic-gate check: 7427c478bd9Sstevel@tonic-gate if (lookup(c) == 1) { 7437c478bd9Sstevel@tonic-gate while ((ch = fbuf[i]) != ';' && ch != '{') { 7447c478bd9Sstevel@tonic-gate if ((len = mblen(&fbuf[i], MB_CUR_MAX)) <= 0) 7457c478bd9Sstevel@tonic-gate len = 1; 7467c478bd9Sstevel@tonic-gate i += len; 7477c478bd9Sstevel@tonic-gate if (i >= fbsz) 7487c478bd9Sstevel@tonic-gate goto notc; 7497c478bd9Sstevel@tonic-gate } 7507c478bd9Sstevel@tonic-gate (void) printf(gettext("c program text")); 7517c478bd9Sstevel@tonic-gate goto outa; 7527c478bd9Sstevel@tonic-gate } 7537c478bd9Sstevel@tonic-gate nl = 0; 7547c478bd9Sstevel@tonic-gate while (fbuf[i] != '(') { 7557c478bd9Sstevel@tonic-gate if (fbuf[i] <= 0) 7567c478bd9Sstevel@tonic-gate goto notas; 7577c478bd9Sstevel@tonic-gate if (fbuf[i] == ';') { 7587c478bd9Sstevel@tonic-gate i++; 7597c478bd9Sstevel@tonic-gate goto check; 7607c478bd9Sstevel@tonic-gate } 7617c478bd9Sstevel@tonic-gate if (fbuf[i++] == '\n') 7627c478bd9Sstevel@tonic-gate if (nl++ > 6) 7637c478bd9Sstevel@tonic-gate goto notc; 7647c478bd9Sstevel@tonic-gate if (i >= fbsz) 7657c478bd9Sstevel@tonic-gate goto notc; 7667c478bd9Sstevel@tonic-gate } 7677c478bd9Sstevel@tonic-gate while (fbuf[i] != ')') { 7687c478bd9Sstevel@tonic-gate if (fbuf[i++] == '\n') 7697c478bd9Sstevel@tonic-gate if (nl++ > 6) 7707c478bd9Sstevel@tonic-gate goto notc; 7717c478bd9Sstevel@tonic-gate if (i >= fbsz) 7727c478bd9Sstevel@tonic-gate goto notc; 7737c478bd9Sstevel@tonic-gate } 7747c478bd9Sstevel@tonic-gate while (fbuf[i] != '{') { 7757c478bd9Sstevel@tonic-gate if ((len = mblen(&fbuf[i], MB_CUR_MAX)) <= 0) 7767c478bd9Sstevel@tonic-gate len = 1; 7777c478bd9Sstevel@tonic-gate if (fbuf[i] == '\n') 7787c478bd9Sstevel@tonic-gate if (nl++ > 6) 7797c478bd9Sstevel@tonic-gate goto notc; 7807c478bd9Sstevel@tonic-gate i += len; 7817c478bd9Sstevel@tonic-gate if (i >= fbsz) 7827c478bd9Sstevel@tonic-gate goto notc; 7837c478bd9Sstevel@tonic-gate } 7847c478bd9Sstevel@tonic-gate (void) printf(gettext("c program text")); 7857c478bd9Sstevel@tonic-gate goto outa; 7867c478bd9Sstevel@tonic-gate notc: 7877c478bd9Sstevel@tonic-gate i = 0; /* reset to begining of file again */ 7887c478bd9Sstevel@tonic-gate while (fbuf[i] == 'c' || fbuf[i] == 'C'|| fbuf[i] == '!' || 7897c478bd9Sstevel@tonic-gate fbuf[i] == '*' || fbuf[i] == '\n') { 7907c478bd9Sstevel@tonic-gate while (fbuf[i++] != '\n') 7917c478bd9Sstevel@tonic-gate if (i >= fbsz) 7927c478bd9Sstevel@tonic-gate goto notfort; 7937c478bd9Sstevel@tonic-gate } 7947c478bd9Sstevel@tonic-gate if (lookup(fort) == 1) { 7957c478bd9Sstevel@tonic-gate (void) printf(gettext("fortran program text")); 7967c478bd9Sstevel@tonic-gate goto outa; 7977c478bd9Sstevel@tonic-gate } 7987c478bd9Sstevel@tonic-gate notfort: /* looking for assembler program */ 7997c478bd9Sstevel@tonic-gate i = 0; /* reset to beginning of file again */ 8007c478bd9Sstevel@tonic-gate if (ccom() == 0) /* assembler programs may contain */ 8017c478bd9Sstevel@tonic-gate /* c-style comments */ 8027c478bd9Sstevel@tonic-gate goto notas; 8037c478bd9Sstevel@tonic-gate if (ascom() == 0) 8047c478bd9Sstevel@tonic-gate goto notas; 8057c478bd9Sstevel@tonic-gate j = i - 1; 8067c478bd9Sstevel@tonic-gate if (fbuf[i] == '.') { 8077c478bd9Sstevel@tonic-gate i++; 8087c478bd9Sstevel@tonic-gate if (lookup(as) == 1) { 8097c478bd9Sstevel@tonic-gate (void) printf(gettext("assembler program text")); 8107c478bd9Sstevel@tonic-gate goto outa; 8117c478bd9Sstevel@tonic-gate } else if (j != -1 && fbuf[j] == '\n' && isalpha(fbuf[j + 2])) { 8127c478bd9Sstevel@tonic-gate (void) printf( 8137c478bd9Sstevel@tonic-gate gettext("[nt]roff, tbl, or eqn input text")); 8147c478bd9Sstevel@tonic-gate goto outa; 8157c478bd9Sstevel@tonic-gate } 8167c478bd9Sstevel@tonic-gate } 8177c478bd9Sstevel@tonic-gate while (lookup(asc) == 0) { 8187c478bd9Sstevel@tonic-gate if (ccom() == 0) 8197c478bd9Sstevel@tonic-gate goto notas; 8207c478bd9Sstevel@tonic-gate if (ascom() == 0) 8217c478bd9Sstevel@tonic-gate goto notas; 8227c478bd9Sstevel@tonic-gate while (fbuf[i] != '\n' && fbuf[i++] != ':') { 8237c478bd9Sstevel@tonic-gate if (i >= fbsz) 8247c478bd9Sstevel@tonic-gate goto notas; 8257c478bd9Sstevel@tonic-gate } 8267c478bd9Sstevel@tonic-gate while (fbuf[i] == '\n' || fbuf[i] == ' ' || fbuf[i] == '\t') 8277c478bd9Sstevel@tonic-gate if (i++ >= fbsz) 8287c478bd9Sstevel@tonic-gate goto notas; 8297c478bd9Sstevel@tonic-gate j = i - 1; 8307c478bd9Sstevel@tonic-gate if (fbuf[i] == '.') { 8317c478bd9Sstevel@tonic-gate i++; 8327c478bd9Sstevel@tonic-gate if (lookup(as) == 1) { 8337c478bd9Sstevel@tonic-gate (void) printf( 8347c478bd9Sstevel@tonic-gate gettext("assembler program text")); 8357c478bd9Sstevel@tonic-gate goto outa; 8367c478bd9Sstevel@tonic-gate } else if (fbuf[j] == '\n' && isalpha(fbuf[j+2])) { 8377c478bd9Sstevel@tonic-gate (void) printf( 8387c478bd9Sstevel@tonic-gate gettext("[nt]roff, tbl, or eqn input " 8397c478bd9Sstevel@tonic-gate "text")); 8407c478bd9Sstevel@tonic-gate goto outa; 8417c478bd9Sstevel@tonic-gate } 8427c478bd9Sstevel@tonic-gate } 8437c478bd9Sstevel@tonic-gate } 8447c478bd9Sstevel@tonic-gate (void) printf(gettext("assembler program text")); 8457c478bd9Sstevel@tonic-gate goto outa; 8467c478bd9Sstevel@tonic-gate notas: 8477c478bd9Sstevel@tonic-gate /* start modification for multibyte env */ 8487c478bd9Sstevel@tonic-gate IS_ascii = 1; 8497c478bd9Sstevel@tonic-gate if (fbsz < FBSZ) 8507c478bd9Sstevel@tonic-gate Max = fbsz; 8517c478bd9Sstevel@tonic-gate else 8527c478bd9Sstevel@tonic-gate Max = FBSZ - MB_LEN_MAX; /* prevent cut of wchar read */ 8537c478bd9Sstevel@tonic-gate /* end modification for multibyte env */ 8547c478bd9Sstevel@tonic-gate 8557c478bd9Sstevel@tonic-gate for (i = 0; i < Max; /* null */) 8567c478bd9Sstevel@tonic-gate if (fbuf[i] & 0200) { 8577c478bd9Sstevel@tonic-gate IS_ascii = 0; 8587c478bd9Sstevel@tonic-gate if (fbuf[0] == '\100' && fbuf[1] == '\357') { 8597c478bd9Sstevel@tonic-gate (void) printf(gettext("troff output\n")); 8607c478bd9Sstevel@tonic-gate return; 8617c478bd9Sstevel@tonic-gate } 8627c478bd9Sstevel@tonic-gate /* start modification for multibyte env */ 8637c478bd9Sstevel@tonic-gate if ((length = mbtowc(&wchar, &fbuf[i], MB_CUR_MAX)) 8647c478bd9Sstevel@tonic-gate <= 0 || !iswprint(wchar)) { 8657c478bd9Sstevel@tonic-gate (void) printf(gettext("data\n")); 8667c478bd9Sstevel@tonic-gate return; 8677c478bd9Sstevel@tonic-gate } 8687c478bd9Sstevel@tonic-gate i += length; 8697c478bd9Sstevel@tonic-gate } 8707c478bd9Sstevel@tonic-gate else 8717c478bd9Sstevel@tonic-gate i++; 8727c478bd9Sstevel@tonic-gate i = fbsz; 8737c478bd9Sstevel@tonic-gate /* end modification for multibyte env */ 8747c478bd9Sstevel@tonic-gate if (mbuf.st_mode&(S_IXUSR|S_IXGRP|S_IXOTH)) 8757c478bd9Sstevel@tonic-gate (void) printf(gettext("commands text")); 8767c478bd9Sstevel@tonic-gate else if (troffint(fbuf, fbsz)) 8777c478bd9Sstevel@tonic-gate (void) printf(gettext("troff intermediate output text")); 8787c478bd9Sstevel@tonic-gate else if (english(fbuf, fbsz)) 8797c478bd9Sstevel@tonic-gate (void) printf(gettext("English text")); 8807c478bd9Sstevel@tonic-gate else if (IS_ascii) 8817c478bd9Sstevel@tonic-gate (void) printf(gettext("ascii text")); 8827c478bd9Sstevel@tonic-gate else 8837c478bd9Sstevel@tonic-gate (void) printf(gettext("text")); /* for multibyte env */ 8847c478bd9Sstevel@tonic-gate outa: 8857c478bd9Sstevel@tonic-gate /* 8867c478bd9Sstevel@tonic-gate * This code is to make sure that no MB char is cut in half 8877c478bd9Sstevel@tonic-gate * while still being used. 8887c478bd9Sstevel@tonic-gate */ 8897c478bd9Sstevel@tonic-gate fbsz = (fbsz < FBSZ ? fbsz : fbsz - MB_CUR_MAX + 1); 8907c478bd9Sstevel@tonic-gate while (i < fbsz) { 8917c478bd9Sstevel@tonic-gate if (isascii(fbuf[i])) { 8927c478bd9Sstevel@tonic-gate i++; 8937c478bd9Sstevel@tonic-gate continue; 8947c478bd9Sstevel@tonic-gate } else { 8957c478bd9Sstevel@tonic-gate if ((length = mbtowc(&wchar, &fbuf[i], MB_CUR_MAX)) 8967c478bd9Sstevel@tonic-gate <= 0 || !iswprint(wchar)) { 8977c478bd9Sstevel@tonic-gate (void) printf(gettext(" with garbage\n")); 8987c478bd9Sstevel@tonic-gate return; 8997c478bd9Sstevel@tonic-gate } 9007c478bd9Sstevel@tonic-gate i = i + length; 9017c478bd9Sstevel@tonic-gate } 9027c478bd9Sstevel@tonic-gate } 9037c478bd9Sstevel@tonic-gate (void) printf("\n"); 9047c478bd9Sstevel@tonic-gate } 9057c478bd9Sstevel@tonic-gate 9067c478bd9Sstevel@tonic-gate static int 9077c478bd9Sstevel@tonic-gate troffint(char *bp, int n) 9087c478bd9Sstevel@tonic-gate { 9097c478bd9Sstevel@tonic-gate int k; 9107c478bd9Sstevel@tonic-gate 9117c478bd9Sstevel@tonic-gate i = 0; 9127c478bd9Sstevel@tonic-gate for (k = 0; k < 6; k++) { 9137c478bd9Sstevel@tonic-gate if (lookup(troff) == 0) 9147c478bd9Sstevel@tonic-gate return (0); 9157c478bd9Sstevel@tonic-gate if (lookup(troff) == 0) 9167c478bd9Sstevel@tonic-gate return (0); 9177c478bd9Sstevel@tonic-gate while (i < n && bp[i] != '\n') 9187c478bd9Sstevel@tonic-gate i++; 9197c478bd9Sstevel@tonic-gate if (i++ >= n) 9207c478bd9Sstevel@tonic-gate return (0); 9217c478bd9Sstevel@tonic-gate } 9227c478bd9Sstevel@tonic-gate return (1); 9237c478bd9Sstevel@tonic-gate } 9247c478bd9Sstevel@tonic-gate 9257c478bd9Sstevel@tonic-gate /* 9267c478bd9Sstevel@tonic-gate * Determine if the passed descriptor describes an ELF file. 9277c478bd9Sstevel@tonic-gate * If so, return the Elf handle. 9287c478bd9Sstevel@tonic-gate */ 9297c478bd9Sstevel@tonic-gate static Elf * 9307c478bd9Sstevel@tonic-gate is_elf_file(int elffd) 9317c478bd9Sstevel@tonic-gate { 9327c478bd9Sstevel@tonic-gate Elf *elf; 9337c478bd9Sstevel@tonic-gate 9347c478bd9Sstevel@tonic-gate elf = elf_begin(elffd, ELF_C_READ, (Elf *)0); 9357c478bd9Sstevel@tonic-gate switch (elf_kind(elf)) { 9367c478bd9Sstevel@tonic-gate case ELF_K_ELF: 9377c478bd9Sstevel@tonic-gate break; 9387c478bd9Sstevel@tonic-gate default: 9397c478bd9Sstevel@tonic-gate (void) elf_end(elf); 9407c478bd9Sstevel@tonic-gate elf = NULL; 9417c478bd9Sstevel@tonic-gate break; 9427c478bd9Sstevel@tonic-gate } 9437c478bd9Sstevel@tonic-gate return (elf); 9447c478bd9Sstevel@tonic-gate } 9457c478bd9Sstevel@tonic-gate 9467c478bd9Sstevel@tonic-gate static void 9477c478bd9Sstevel@tonic-gate ar_coff_or_aout(int elffd) 9487c478bd9Sstevel@tonic-gate { 9497c478bd9Sstevel@tonic-gate Elf *elf; 9507c478bd9Sstevel@tonic-gate 9517c478bd9Sstevel@tonic-gate /* 9527c478bd9Sstevel@tonic-gate * Get the files elf descriptor and process it as an elf or 9537c478bd9Sstevel@tonic-gate * a.out (4.x) file. 9547c478bd9Sstevel@tonic-gate */ 9557c478bd9Sstevel@tonic-gate 9567c478bd9Sstevel@tonic-gate elf = elf_begin(elffd, ELF_C_READ, (Elf *)0); 9577c478bd9Sstevel@tonic-gate switch (elf_kind(elf)) { 9587c478bd9Sstevel@tonic-gate case ELF_K_AR : 9597c478bd9Sstevel@tonic-gate (void) printf(gettext(", not a dynamic executable " 9607c478bd9Sstevel@tonic-gate "or shared object")); 9617c478bd9Sstevel@tonic-gate break; 9627c478bd9Sstevel@tonic-gate case ELF_K_COFF: 9637c478bd9Sstevel@tonic-gate (void) printf(gettext(", unsupported or unknown " 9647c478bd9Sstevel@tonic-gate "file type")); 9657c478bd9Sstevel@tonic-gate break; 9667c478bd9Sstevel@tonic-gate default: 9677c478bd9Sstevel@tonic-gate /* 9687c478bd9Sstevel@tonic-gate * This is either an unknown file or an aout format 9697c478bd9Sstevel@tonic-gate * At this time, we don't print dynamic/stripped 9707c478bd9Sstevel@tonic-gate * info. on a.out or non-Elf binaries. 9717c478bd9Sstevel@tonic-gate */ 9727c478bd9Sstevel@tonic-gate break; 9737c478bd9Sstevel@tonic-gate } 9747c478bd9Sstevel@tonic-gate (void) elf_end(elf); 9757c478bd9Sstevel@tonic-gate } 9767c478bd9Sstevel@tonic-gate 9777c478bd9Sstevel@tonic-gate 9787c478bd9Sstevel@tonic-gate static void 9797c478bd9Sstevel@tonic-gate print_elf_type(Elf *elf, GElf_Ehdr *ehdr, int format) 9807c478bd9Sstevel@tonic-gate { 9817c478bd9Sstevel@tonic-gate switch (ehdr->e_type) { 9827c478bd9Sstevel@tonic-gate case ET_NONE: 9837c478bd9Sstevel@tonic-gate (void) printf(" %s", gettext("unknown type")); 9847c478bd9Sstevel@tonic-gate break; 9857c478bd9Sstevel@tonic-gate case ET_REL: 9867c478bd9Sstevel@tonic-gate (void) printf(" %s", gettext("relocatable")); 9877c478bd9Sstevel@tonic-gate break; 9887c478bd9Sstevel@tonic-gate case ET_EXEC: 9897c478bd9Sstevel@tonic-gate (void) printf(" %s", gettext("executable")); 9907c478bd9Sstevel@tonic-gate break; 9917c478bd9Sstevel@tonic-gate case ET_DYN: 9927c478bd9Sstevel@tonic-gate (void) printf(" %s", gettext("dynamic lib")); 9937c478bd9Sstevel@tonic-gate break; 9947c478bd9Sstevel@tonic-gate case ET_CORE: 9957c478bd9Sstevel@tonic-gate if (old_core(elf, ehdr, format)) 9967c478bd9Sstevel@tonic-gate (void) printf(" %s", gettext("pre-2.6 core file")); 9977c478bd9Sstevel@tonic-gate else 9987c478bd9Sstevel@tonic-gate (void) printf(" %s", gettext("core file")); 9997c478bd9Sstevel@tonic-gate break; 10007c478bd9Sstevel@tonic-gate default: 10017c478bd9Sstevel@tonic-gate break; 10027c478bd9Sstevel@tonic-gate } 10037c478bd9Sstevel@tonic-gate } 10047c478bd9Sstevel@tonic-gate 10057c478bd9Sstevel@tonic-gate static void 10067c478bd9Sstevel@tonic-gate print_elf_machine(int machine) 10077c478bd9Sstevel@tonic-gate { 1008ca1e0a81Sab196087 /* 1009ca1e0a81Sab196087 * This table must be kept in sync with the EM_ constants 1010ca1e0a81Sab196087 * in /usr/include/sys/elf.h. 1011ca1e0a81Sab196087 */ 1012ca1e0a81Sab196087 static const char *mach_str[EM_NUM] = { 1013ca1e0a81Sab196087 "unknown machine", /* 0 - EM_NONE */ 1014ca1e0a81Sab196087 "WE32100", /* 1 - EM_M32 */ 1015ca1e0a81Sab196087 "SPARC", /* 2 - EM_SPARC */ 1016ca1e0a81Sab196087 "80386", /* 3 - EM_386 */ 1017ca1e0a81Sab196087 "M68000", /* 4 - EM_68K */ 1018ca1e0a81Sab196087 "M88000", /* 5 - EM_88K */ 1019ca1e0a81Sab196087 "80486", /* 6 - EM_486 */ 1020ca1e0a81Sab196087 "i860", /* 7 - EM_860 */ 1021ca1e0a81Sab196087 "MIPS RS3000 Big-Endian", /* 8 - EM_MIPS */ 1022ca1e0a81Sab196087 "S/370", /* 9 - EM_S370 */ 1023ca1e0a81Sab196087 "MIPS RS3000 Little-Endian", /* 10 - EM_MIPS_RS3_LE */ 1024ca1e0a81Sab196087 "MIPS RS6000", /* 11 - EM_RS6000 */ 1025ca1e0a81Sab196087 NULL, /* 12 - EM_UNKNOWN12 */ 1026ca1e0a81Sab196087 NULL, /* 13 - EM_UNKNOWN13 */ 1027ca1e0a81Sab196087 NULL, /* 14 - EM_UNKNOWN14 */ 1028ca1e0a81Sab196087 "PA-RISC", /* 15 - EM_PA_RISC */ 1029ca1e0a81Sab196087 "nCUBE", /* 16 - EM_nCUBE */ 1030ca1e0a81Sab196087 "VPP500", /* 17 - EM_VPP500 */ 1031ca1e0a81Sab196087 "SPARC32PLUS", /* 18 - EM_SPARC32PLUS */ 1032ca1e0a81Sab196087 "i960", /* 19 - EM_960 */ 1033ca1e0a81Sab196087 "PowerPC", /* 20 - EM_PPC */ 1034ca1e0a81Sab196087 "PowerPC64", /* 21 - EM_PPC64 */ 10351638af81Sab196087 "S/390", /* 22 - EM_S390 */ 1036ca1e0a81Sab196087 NULL, /* 23 - EM_UNKNOWN23 */ 1037ca1e0a81Sab196087 NULL, /* 24 - EM_UNKNOWN24 */ 1038ca1e0a81Sab196087 NULL, /* 25 - EM_UNKNOWN25 */ 1039ca1e0a81Sab196087 NULL, /* 26 - EM_UNKNOWN26 */ 1040ca1e0a81Sab196087 NULL, /* 27 - EM_UNKNOWN27 */ 1041ca1e0a81Sab196087 NULL, /* 28 - EM_UNKNOWN28 */ 1042ca1e0a81Sab196087 NULL, /* 29 - EM_UNKNOWN29 */ 1043ca1e0a81Sab196087 NULL, /* 30 - EM_UNKNOWN30 */ 1044ca1e0a81Sab196087 NULL, /* 31 - EM_UNKNOWN31 */ 1045ca1e0a81Sab196087 NULL, /* 32 - EM_UNKNOWN32 */ 1046ca1e0a81Sab196087 NULL, /* 33 - EM_UNKNOWN33 */ 1047ca1e0a81Sab196087 NULL, /* 34 - EM_UNKNOWN34 */ 1048ca1e0a81Sab196087 NULL, /* 35 - EM_UNKNOWN35 */ 1049ca1e0a81Sab196087 "V800", /* 36 - EM_V800 */ 1050ca1e0a81Sab196087 "FR20", /* 37 - EM_FR20 */ 1051ca1e0a81Sab196087 "RH32", /* 38 - EM_RH32 */ 1052ca1e0a81Sab196087 "RCE", /* 39 - EM_RCE */ 1053ca1e0a81Sab196087 "ARM", /* 40 - EM_ARM */ 1054ca1e0a81Sab196087 "Alpha", /* 41 - EM_ALPHA */ 1055ca1e0a81Sab196087 "S/390", /* 42 - EM_SH */ 1056ca1e0a81Sab196087 "SPARCV9", /* 43 - EM_SPARCV9 */ 1057ca1e0a81Sab196087 "Tricore", /* 44 - EM_TRICORE */ 1058ca1e0a81Sab196087 "ARC", /* 45 - EM_ARC */ 1059ca1e0a81Sab196087 "H8/300", /* 46 - EM_H8_300 */ 1060ca1e0a81Sab196087 "H8/300H", /* 47 - EM_H8_300H */ 1061ca1e0a81Sab196087 "H8S", /* 48 - EM_H8S */ 1062ca1e0a81Sab196087 "H8/500", /* 49 - EM_H8_500 */ 1063ca1e0a81Sab196087 "IA64", /* 50 - EM_IA_64 */ 1064ca1e0a81Sab196087 "MIPS-X", /* 51 - EM_MIPS_X */ 1065ca1e0a81Sab196087 "Coldfire", /* 52 - EM_COLDFIRE */ 1066ca1e0a81Sab196087 "M68HC12", /* 53 - EM_68HC12 */ 1067ca1e0a81Sab196087 "MMA", /* 54 - EM_MMA */ 1068ca1e0a81Sab196087 "PCP", /* 55 - EM_PCP */ 1069ca1e0a81Sab196087 "nCPU", /* 56 - EM_NCPU */ 1070ca1e0a81Sab196087 "NDR1", /* 57 - EM_NDR1 */ 1071ca1e0a81Sab196087 "Starcore", /* 58 - EM_STARCORE */ 1072ca1e0a81Sab196087 "ME16", /* 59 - EM_ME16 */ 1073ca1e0a81Sab196087 "ST100", /* 60 - EM_ST100 */ 1074ca1e0a81Sab196087 "TINYJ", /* 61 - EM_TINYJ */ 1075ca1e0a81Sab196087 "AMD64", /* 62 - EM_AMD64 */ 1076ca1e0a81Sab196087 "PDSP", /* 63 - EM_PDSP */ 1077ca1e0a81Sab196087 NULL, /* 64 - EM_UNKNOWN64 */ 1078ca1e0a81Sab196087 NULL, /* 65 - EM_UNKNOWN65 */ 1079ca1e0a81Sab196087 "FX66", /* 66 - EM_FX66 */ 1080ca1e0a81Sab196087 "ST9 PLUS", /* 67 - EM_ST9PLUS */ 1081ca1e0a81Sab196087 "ST7", /* 68 - EM_ST7 */ 1082ca1e0a81Sab196087 "68HC16", /* 69 - EM_68HC16 */ 1083ca1e0a81Sab196087 "68HC11", /* 70 - EM_68HC11 */ 1084ca1e0a81Sab196087 "68H08", /* 71 - EM_68HC08 */ 1085ca1e0a81Sab196087 "68HC05", /* 72 - EM_68HC05 */ 1086ca1e0a81Sab196087 "SVX", /* 73 - EM_SVX */ 1087ca1e0a81Sab196087 "ST19", /* 74 - EM_ST19 */ 1088ca1e0a81Sab196087 "VAX", /* 75 - EM_VAX */ 1089ca1e0a81Sab196087 "CRIS", /* 76 - EM_CRIS */ 1090ca1e0a81Sab196087 "Javelin", /* 77 - EM_JAVELIN */ 1091ca1e0a81Sab196087 "Firepath", /* 78 - EM_FIREPATH */ 1092ca1e0a81Sab196087 "ZSP", /* 79 - EM_ZSP */ 1093ca1e0a81Sab196087 "MMIX", /* 80 - EM_MMIX */ 1094ca1e0a81Sab196087 "HUANY", /* 81 - EM_HUANY */ 1095ca1e0a81Sab196087 "Prism", /* 82 - EM_PRISM */ 1096ca1e0a81Sab196087 "AVR", /* 83 - EM_AVR */ 1097ca1e0a81Sab196087 "FR30", /* 84 - EM_FR30 */ 1098ca1e0a81Sab196087 "D10V", /* 85 - EM_D10V */ 1099ca1e0a81Sab196087 "D30V", /* 86 - EM_D30V */ 1100ca1e0a81Sab196087 "V850", /* 87 - EM_V850 */ 1101ca1e0a81Sab196087 "M32R", /* 88 - EM_M32R */ 1102ca1e0a81Sab196087 "MN10300", /* 89 - EM_MN10300 */ 1103ca1e0a81Sab196087 "MN10200", /* 90 - EM_MN10200 */ 1104ca1e0a81Sab196087 "picoJava", /* 91 - EM_PJ */ 1105ca1e0a81Sab196087 "OpenRISC", /* 92 - EM_OPENRISC */ 1106ca1e0a81Sab196087 "Tangent-A5", /* 93 - EM_ARC_A5 */ 1107ca1e0a81Sab196087 "Xtensa" /* 94 - EM_XTENSA */ 1108ca1e0a81Sab196087 }; 1109ca1e0a81Sab196087 /* If new machine is added, refuse to compile until we're updated */ 1110ca1e0a81Sab196087 #if EM_NUM != 95 1111ca1e0a81Sab196087 #error "Number of known ELF machine constants has changed" 1112ca1e0a81Sab196087 #endif 1113ca1e0a81Sab196087 1114ca1e0a81Sab196087 const char *str; 1115ca1e0a81Sab196087 1116ca1e0a81Sab196087 if ((machine < EM_NONE) || (machine >= EM_NUM)) 1117ca1e0a81Sab196087 machine = EM_NONE; 1118ca1e0a81Sab196087 1119ca1e0a81Sab196087 str = mach_str[machine]; 1120ca1e0a81Sab196087 if (str) 1121ca1e0a81Sab196087 (void) printf(" %s", str); 11227c478bd9Sstevel@tonic-gate } 11237c478bd9Sstevel@tonic-gate 11247c478bd9Sstevel@tonic-gate static void 11257c478bd9Sstevel@tonic-gate print_elf_datatype(int datatype) 11267c478bd9Sstevel@tonic-gate { 11277c478bd9Sstevel@tonic-gate switch (datatype) { 11287c478bd9Sstevel@tonic-gate case ELFDATA2LSB: 1129ca1e0a81Sab196087 (void) printf(" LSB"); 11307c478bd9Sstevel@tonic-gate break; 11317c478bd9Sstevel@tonic-gate case ELFDATA2MSB: 1132ca1e0a81Sab196087 (void) printf(" MSB"); 11337c478bd9Sstevel@tonic-gate break; 11347c478bd9Sstevel@tonic-gate default: 11357c478bd9Sstevel@tonic-gate break; 11367c478bd9Sstevel@tonic-gate } 11377c478bd9Sstevel@tonic-gate } 11387c478bd9Sstevel@tonic-gate 11397c478bd9Sstevel@tonic-gate static void 11407c478bd9Sstevel@tonic-gate print_elf_class(int class) 11417c478bd9Sstevel@tonic-gate { 11427c478bd9Sstevel@tonic-gate switch (class) { 11437c478bd9Sstevel@tonic-gate case ELFCLASS32: 11447c478bd9Sstevel@tonic-gate (void) printf(" %s", gettext("32-bit")); 11457c478bd9Sstevel@tonic-gate break; 11467c478bd9Sstevel@tonic-gate case ELFCLASS64: 11477c478bd9Sstevel@tonic-gate (void) printf(" %s", gettext("64-bit")); 11487c478bd9Sstevel@tonic-gate break; 11497c478bd9Sstevel@tonic-gate default: 11507c478bd9Sstevel@tonic-gate break; 11517c478bd9Sstevel@tonic-gate } 11527c478bd9Sstevel@tonic-gate } 11537c478bd9Sstevel@tonic-gate 11547c478bd9Sstevel@tonic-gate static void 11557c478bd9Sstevel@tonic-gate print_elf_flags(int machine, unsigned int flags) 11567c478bd9Sstevel@tonic-gate { 11577c478bd9Sstevel@tonic-gate switch (machine) { 11587c478bd9Sstevel@tonic-gate case EM_SPARCV9: 11597c478bd9Sstevel@tonic-gate if (flags & EF_SPARC_EXT_MASK) { 11607c478bd9Sstevel@tonic-gate if (flags & EF_SPARC_SUN_US3) { 11617c478bd9Sstevel@tonic-gate (void) printf("%s", gettext( 11627c478bd9Sstevel@tonic-gate ", UltraSPARC3 Extensions Required")); 11637c478bd9Sstevel@tonic-gate } else if (flags & EF_SPARC_SUN_US1) { 11647c478bd9Sstevel@tonic-gate (void) printf("%s", gettext( 11657c478bd9Sstevel@tonic-gate ", UltraSPARC1 Extensions Required")); 11667c478bd9Sstevel@tonic-gate } 11677c478bd9Sstevel@tonic-gate if (flags & EF_SPARC_HAL_R1) 11687c478bd9Sstevel@tonic-gate (void) printf("%s", gettext( 11697c478bd9Sstevel@tonic-gate ", HaL R1 Extensions Required")); 11707c478bd9Sstevel@tonic-gate } 11717c478bd9Sstevel@tonic-gate break; 11727c478bd9Sstevel@tonic-gate case EM_SPARC32PLUS: 11737c478bd9Sstevel@tonic-gate if (flags & EF_SPARC_32PLUS) 11747c478bd9Sstevel@tonic-gate (void) printf("%s", gettext(", V8+ Required")); 11757c478bd9Sstevel@tonic-gate if (flags & EF_SPARC_SUN_US3) { 11767c478bd9Sstevel@tonic-gate (void) printf("%s", 11777c478bd9Sstevel@tonic-gate gettext(", UltraSPARC3 Extensions Required")); 11787c478bd9Sstevel@tonic-gate } else if (flags & EF_SPARC_SUN_US1) { 11797c478bd9Sstevel@tonic-gate (void) printf("%s", 11807c478bd9Sstevel@tonic-gate gettext(", UltraSPARC1 Extensions Required")); 11817c478bd9Sstevel@tonic-gate } 11827c478bd9Sstevel@tonic-gate if (flags & EF_SPARC_HAL_R1) 11837c478bd9Sstevel@tonic-gate (void) printf("%s", 11847c478bd9Sstevel@tonic-gate gettext(", HaL R1 Extensions Required")); 11857c478bd9Sstevel@tonic-gate break; 11867c478bd9Sstevel@tonic-gate default: 11877c478bd9Sstevel@tonic-gate break; 11887c478bd9Sstevel@tonic-gate } 11897c478bd9Sstevel@tonic-gate } 11907c478bd9Sstevel@tonic-gate 11917c478bd9Sstevel@tonic-gate static int 1192*9a411307Srie print_cap(Elf *elf, GElf_Ehdr *ehdr, char *file) 11937c478bd9Sstevel@tonic-gate { 11947c478bd9Sstevel@tonic-gate Elf_Scn *scn = 0; 11957c478bd9Sstevel@tonic-gate 11967c478bd9Sstevel@tonic-gate /* 11977c478bd9Sstevel@tonic-gate * Traverse the files sections to see if any software/hardware 11987c478bd9Sstevel@tonic-gate * capabilities are available. 11997c478bd9Sstevel@tonic-gate */ 12007c478bd9Sstevel@tonic-gate while ((scn = elf_nextscn(elf, scn)) != 0) { 12017c478bd9Sstevel@tonic-gate GElf_Word ndx, capn; 12027c478bd9Sstevel@tonic-gate GElf_Shdr shdr; 12037c478bd9Sstevel@tonic-gate Elf_Data *data; 12047c478bd9Sstevel@tonic-gate 12057c478bd9Sstevel@tonic-gate if (gelf_getshdr(scn, &shdr) == 0) { 1206*9a411307Srie (void) fprintf(stderr, gettext("%s: %s: can't read " 1207*9a411307Srie "ELF section header - ELF capabilities ignored\n"), 1208*9a411307Srie File, file); 12097c478bd9Sstevel@tonic-gate return (1); 12107c478bd9Sstevel@tonic-gate } 12117c478bd9Sstevel@tonic-gate if (shdr.sh_type != SHT_SUNW_cap) 12127c478bd9Sstevel@tonic-gate continue; 12137c478bd9Sstevel@tonic-gate 12147c478bd9Sstevel@tonic-gate /* 12157c478bd9Sstevel@tonic-gate * Get the data associated with the .cap section. 12167c478bd9Sstevel@tonic-gate */ 12177c478bd9Sstevel@tonic-gate if ((data = elf_getdata(scn, 0)) == 0) { 1218*9a411307Srie (void) fprintf(stderr, gettext("%s: %s: can't read " 1219*9a411307Srie "ELF section data - ELF capabilities ignored\n"), 1220*9a411307Srie File, file); 12217c478bd9Sstevel@tonic-gate return (1); 12227c478bd9Sstevel@tonic-gate } 12237c478bd9Sstevel@tonic-gate 1224*9a411307Srie if ((shdr.sh_size == 0) || (shdr.sh_entsize == 0)) { 1225*9a411307Srie (void) fprintf(stderr, gettext("%s: %s zero size or " 1226*9a411307Srie "zero entry ELF section - ELF capabilities " 1227*9a411307Srie "ignored\n"), File, file); 1228*9a411307Srie return (1); 1229*9a411307Srie } 12307c478bd9Sstevel@tonic-gate capn = (GElf_Word)(shdr.sh_size / shdr.sh_entsize); 1231*9a411307Srie 12327c478bd9Sstevel@tonic-gate for (ndx = 0; ndx < capn; ndx++) { 12337c478bd9Sstevel@tonic-gate char str[100]; 12347c478bd9Sstevel@tonic-gate GElf_Cap cap; 12357c478bd9Sstevel@tonic-gate 12367c478bd9Sstevel@tonic-gate if (gelf_getcap(data, ndx, &cap) == NULL) { 1237*9a411307Srie (void) fprintf(stderr, gettext("%s: %s: can't " 1238*9a411307Srie "read ELF capabilities data - ELF " 1239*9a411307Srie "capabilities ignored\n"), File, file); 12407c478bd9Sstevel@tonic-gate return (1); 12417c478bd9Sstevel@tonic-gate } 12427c478bd9Sstevel@tonic-gate if (cap.c_tag != CA_SUNW_NULL) { 12437c478bd9Sstevel@tonic-gate (void) cap_val2str(cap.c_tag, cap.c_un.c_val, 12447c478bd9Sstevel@tonic-gate str, sizeof (str), 0, ehdr->e_machine); 12457c478bd9Sstevel@tonic-gate (void) printf(" [%s]", str); 12467c478bd9Sstevel@tonic-gate } 12477c478bd9Sstevel@tonic-gate } 12487c478bd9Sstevel@tonic-gate } 12497c478bd9Sstevel@tonic-gate return (0); 12507c478bd9Sstevel@tonic-gate } 12517c478bd9Sstevel@tonic-gate 12527c478bd9Sstevel@tonic-gate static int 1253*9a411307Srie elf_check(Elf *elf, char *file) 12547c478bd9Sstevel@tonic-gate { 12557c478bd9Sstevel@tonic-gate GElf_Ehdr ehdr; 12567c478bd9Sstevel@tonic-gate GElf_Phdr phdr; 12577c478bd9Sstevel@tonic-gate int dynamic, cnt; 12587c478bd9Sstevel@tonic-gate char *ident; 12597c478bd9Sstevel@tonic-gate size_t size; 12607c478bd9Sstevel@tonic-gate 12617c478bd9Sstevel@tonic-gate /* 1262*9a411307Srie * Verify information in file header. 12637c478bd9Sstevel@tonic-gate */ 12647c478bd9Sstevel@tonic-gate if (gelf_getehdr(elf, &ehdr) == (GElf_Ehdr *)0) { 1265*9a411307Srie (void) fprintf(stderr, gettext("%s: %s: can't read ELF " 1266*9a411307Srie "header\n"), File, file); 12677c478bd9Sstevel@tonic-gate return (1); 12687c478bd9Sstevel@tonic-gate } 12697c478bd9Sstevel@tonic-gate ident = elf_getident(elf, &size); 12707c478bd9Sstevel@tonic-gate (void) printf("%s", gettext("ELF")); 12717c478bd9Sstevel@tonic-gate print_elf_class(ident[EI_CLASS]); 12727c478bd9Sstevel@tonic-gate print_elf_datatype(ident[EI_DATA]); 12737c478bd9Sstevel@tonic-gate print_elf_type(elf, &ehdr, ident[EI_DATA]); 12747c478bd9Sstevel@tonic-gate print_elf_machine(ehdr.e_machine); 12757c478bd9Sstevel@tonic-gate if (ehdr.e_version == 1) 12767c478bd9Sstevel@tonic-gate (void) printf(" %s %d", 12777c478bd9Sstevel@tonic-gate gettext("Version"), (int)ehdr.e_version); 12787c478bd9Sstevel@tonic-gate print_elf_flags(ehdr.e_machine, ehdr.e_flags); 12797c478bd9Sstevel@tonic-gate 1280*9a411307Srie if (core(elf, &ehdr, ident[EI_DATA], file)) /* check for core file */ 12817c478bd9Sstevel@tonic-gate return (0); 12827c478bd9Sstevel@tonic-gate 1283*9a411307Srie if (print_cap(elf, &ehdr, file)) 12847c478bd9Sstevel@tonic-gate return (1); 12857c478bd9Sstevel@tonic-gate 12867c478bd9Sstevel@tonic-gate /* 1287*9a411307Srie * Check type. 12887c478bd9Sstevel@tonic-gate */ 12897c478bd9Sstevel@tonic-gate if ((ehdr.e_type != ET_EXEC) && (ehdr.e_type != ET_DYN)) 12907c478bd9Sstevel@tonic-gate return (1); 12917c478bd9Sstevel@tonic-gate 12927c478bd9Sstevel@tonic-gate /* 1293*9a411307Srie * Read program header and check for dynamic section. 12947c478bd9Sstevel@tonic-gate */ 12957c478bd9Sstevel@tonic-gate if (ehdr.e_phnum == 0) { 1296*9a411307Srie (void) fprintf(stderr, gettext("%s: %s: no ELF program headers " 1297*9a411307Srie "exist\n"), File, file); 12987c478bd9Sstevel@tonic-gate return (1); 12997c478bd9Sstevel@tonic-gate } 13007c478bd9Sstevel@tonic-gate 13017c478bd9Sstevel@tonic-gate for (dynamic = 0, cnt = 0; cnt < (int)ehdr.e_phnum; cnt++) { 13027c478bd9Sstevel@tonic-gate if (gelf_getphdr(elf, cnt, &phdr) == NULL) { 1303*9a411307Srie (void) fprintf(stderr, gettext("%s: %s: can't read " 1304*9a411307Srie "ELF program header\n"), File, file); 13057c478bd9Sstevel@tonic-gate return (1); 13067c478bd9Sstevel@tonic-gate } 13077c478bd9Sstevel@tonic-gate if (phdr.p_type == PT_DYNAMIC) { 13087c478bd9Sstevel@tonic-gate dynamic = 1; 13097c478bd9Sstevel@tonic-gate break; 13107c478bd9Sstevel@tonic-gate } 13117c478bd9Sstevel@tonic-gate } 13127c478bd9Sstevel@tonic-gate if (dynamic) 13137c478bd9Sstevel@tonic-gate (void) printf(gettext(", dynamically linked")); 13147c478bd9Sstevel@tonic-gate else 13157c478bd9Sstevel@tonic-gate (void) printf(gettext(", statically linked")); 13167c478bd9Sstevel@tonic-gate 13177c478bd9Sstevel@tonic-gate is_stripped(elf); 13187c478bd9Sstevel@tonic-gate return (0); 13197c478bd9Sstevel@tonic-gate } 13207c478bd9Sstevel@tonic-gate 13217c478bd9Sstevel@tonic-gate /* 13227c478bd9Sstevel@tonic-gate * is_stripped prints information on whether the executable has 13237c478bd9Sstevel@tonic-gate * been stripped. 13247c478bd9Sstevel@tonic-gate */ 13257c478bd9Sstevel@tonic-gate static void 13267c478bd9Sstevel@tonic-gate is_stripped(Elf *elf) 13277c478bd9Sstevel@tonic-gate { 13287c478bd9Sstevel@tonic-gate GElf_Shdr shdr; 13297c478bd9Sstevel@tonic-gate GElf_Ehdr ehdr; 13307c478bd9Sstevel@tonic-gate Elf_Scn *scn, *nextscn; 13317c478bd9Sstevel@tonic-gate char *section_name; 13327c478bd9Sstevel@tonic-gate int symtab = 0; 13337c478bd9Sstevel@tonic-gate int debuginfo = 0; 13347c478bd9Sstevel@tonic-gate 13357c478bd9Sstevel@tonic-gate 13367c478bd9Sstevel@tonic-gate if (gelf_getehdr(elf, &ehdr) == NULL) { 13377c478bd9Sstevel@tonic-gate return; 13387c478bd9Sstevel@tonic-gate } 13397c478bd9Sstevel@tonic-gate 13407c478bd9Sstevel@tonic-gate /* 13417c478bd9Sstevel@tonic-gate * Definition time: 13427c478bd9Sstevel@tonic-gate * - "not stripped" means that an executable file 13437c478bd9Sstevel@tonic-gate * contains a Symbol Table (.symtab) 13447c478bd9Sstevel@tonic-gate * - "stripped" means that an executable file 13457c478bd9Sstevel@tonic-gate * does not contain a Symbol Table. 13467c478bd9Sstevel@tonic-gate * When strip -l or strip -x is run, it strips the 13477c478bd9Sstevel@tonic-gate * debugging information (.line section name (strip -l), 13487c478bd9Sstevel@tonic-gate * .line, .debug*, .stabs*, .dwarf* section names 13497c478bd9Sstevel@tonic-gate * and SHT_SUNW_DEBUGSTR and SHT_SUNW_DEBUG 13507c478bd9Sstevel@tonic-gate * section types (strip -x), however the Symbol 13517c478bd9Sstevel@tonic-gate * Table will still be present. 13527c478bd9Sstevel@tonic-gate * Therefore, if 13537c478bd9Sstevel@tonic-gate * - No Symbol Table present, then report 13547c478bd9Sstevel@tonic-gate * "stripped" 13557c478bd9Sstevel@tonic-gate * - Symbol Table present with debugging 13567c478bd9Sstevel@tonic-gate * information (line number or debug section names, 13577c478bd9Sstevel@tonic-gate * or SHT_SUNW_DEBUGSTR or SHT_SUNW_DEBUG section 13587c478bd9Sstevel@tonic-gate * types) then report: 13597c478bd9Sstevel@tonic-gate * "not stripped" 13607c478bd9Sstevel@tonic-gate * - Symbol Table present with no debugging 13617c478bd9Sstevel@tonic-gate * information (line number or debug section names, 13627c478bd9Sstevel@tonic-gate * or SHT_SUNW_DEBUGSTR or SHT_SUNW_DEBUG section 13637c478bd9Sstevel@tonic-gate * types) then report: 13647c478bd9Sstevel@tonic-gate * "not stripped, no debugging information 13657c478bd9Sstevel@tonic-gate * available" 13667c478bd9Sstevel@tonic-gate */ 13677c478bd9Sstevel@tonic-gate scn = NULL; 13687c478bd9Sstevel@tonic-gate while ((nextscn = elf_nextscn(elf, scn)) != NULL) { 13697c478bd9Sstevel@tonic-gate if (symtab && debuginfo) { 13707c478bd9Sstevel@tonic-gate break; 13717c478bd9Sstevel@tonic-gate } 13727c478bd9Sstevel@tonic-gate 13737c478bd9Sstevel@tonic-gate scn = nextscn; 13747c478bd9Sstevel@tonic-gate if (gelf_getshdr(scn, &shdr) == NULL) { 13757c478bd9Sstevel@tonic-gate continue; 13767c478bd9Sstevel@tonic-gate } 13777c478bd9Sstevel@tonic-gate 13787c478bd9Sstevel@tonic-gate if (!symtab && (shdr.sh_type == SHT_SYMTAB)) { 13797c478bd9Sstevel@tonic-gate symtab++; 13807c478bd9Sstevel@tonic-gate continue; 13817c478bd9Sstevel@tonic-gate } 13827c478bd9Sstevel@tonic-gate 13837c478bd9Sstevel@tonic-gate if (!debuginfo && 13847c478bd9Sstevel@tonic-gate ((shdr.sh_type == SHT_SUNW_DEBUG) || 13857c478bd9Sstevel@tonic-gate (shdr.sh_type == SHT_SUNW_DEBUGSTR) || 13867c478bd9Sstevel@tonic-gate (((section_name = elf_strptr(elf, ehdr.e_shstrndx, 13877c478bd9Sstevel@tonic-gate (size_t)shdr.sh_name)) != NULL) && 13887c478bd9Sstevel@tonic-gate (is_in_list(debug_sections, section_name))))) { 13897c478bd9Sstevel@tonic-gate debuginfo++; 13907c478bd9Sstevel@tonic-gate } 13917c478bd9Sstevel@tonic-gate } 13927c478bd9Sstevel@tonic-gate 13937c478bd9Sstevel@tonic-gate /* 13947c478bd9Sstevel@tonic-gate * Now that we've scanned all sections, print out the appropriate 13957c478bd9Sstevel@tonic-gate * diagnostic. 13967c478bd9Sstevel@tonic-gate */ 13977c478bd9Sstevel@tonic-gate if (symtab) { 13987c478bd9Sstevel@tonic-gate (void) printf(gettext(", not stripped")); 13997c478bd9Sstevel@tonic-gate if (!debuginfo) { 14007c478bd9Sstevel@tonic-gate (void) printf(gettext( 14017c478bd9Sstevel@tonic-gate ", no debugging information available")); 14027c478bd9Sstevel@tonic-gate } 14037c478bd9Sstevel@tonic-gate } else { 14047c478bd9Sstevel@tonic-gate (void) printf(gettext(", stripped")); 14057c478bd9Sstevel@tonic-gate } 14067c478bd9Sstevel@tonic-gate } 14077c478bd9Sstevel@tonic-gate 14087c478bd9Sstevel@tonic-gate /* 1409c13de8f6Sab196087 * is_rtld_config - If file is a runtime linker config file, prints 1410c13de8f6Sab196087 * the description and returns True (1). Otherwise, silently returns 1411c13de8f6Sab196087 * False (0). 1412c13de8f6Sab196087 */ 1413c13de8f6Sab196087 int 1414c13de8f6Sab196087 is_rtld_config(void) 1415c13de8f6Sab196087 { 1416c13de8f6Sab196087 Rtc_id *id; 1417c13de8f6Sab196087 1418c13de8f6Sab196087 if ((fbsz >= sizeof (*id)) && RTC_ID_TEST(fbuf)) { 1419c13de8f6Sab196087 (void) printf(gettext("Runtime Linking Configuration")); 1420c13de8f6Sab196087 id = (Rtc_id *) fbuf; 1421c13de8f6Sab196087 print_elf_class(id->id_class); 1422c13de8f6Sab196087 print_elf_datatype(id->id_data); 1423c13de8f6Sab196087 print_elf_machine(id->id_machine); 1424c13de8f6Sab196087 (void) printf("\n"); 1425c13de8f6Sab196087 return (1); 1426c13de8f6Sab196087 } 1427c13de8f6Sab196087 1428c13de8f6Sab196087 return (0); 1429c13de8f6Sab196087 } 1430c13de8f6Sab196087 1431c13de8f6Sab196087 /* 14327c478bd9Sstevel@tonic-gate * lookup - 14337c478bd9Sstevel@tonic-gate * Attempts to match one of the strings from a list, 'tab', 14347c478bd9Sstevel@tonic-gate * with what is in the file, starting at the current index position 'i'. 14357c478bd9Sstevel@tonic-gate * Looks past any initial whitespace and expects whitespace or other 14367c478bd9Sstevel@tonic-gate * delimiting characters to follow the matched string. 14377c478bd9Sstevel@tonic-gate * A match identifies the file as being 'assembler', 'fortran', 'c', etc. 14387c478bd9Sstevel@tonic-gate * Returns 1 for a successful match, 0 otherwise. 14397c478bd9Sstevel@tonic-gate */ 14407c478bd9Sstevel@tonic-gate static int 14417c478bd9Sstevel@tonic-gate lookup(char **tab) 14427c478bd9Sstevel@tonic-gate { 14437c478bd9Sstevel@tonic-gate register char r; 14447c478bd9Sstevel@tonic-gate register int k, j, l; 14457c478bd9Sstevel@tonic-gate 14467c478bd9Sstevel@tonic-gate while (fbuf[i] == ' ' || fbuf[i] == '\t' || fbuf[i] == '\n') 14477c478bd9Sstevel@tonic-gate i++; 14487c478bd9Sstevel@tonic-gate for (j = 0; tab[j] != 0; j++) { 14497c478bd9Sstevel@tonic-gate l = 0; 14507c478bd9Sstevel@tonic-gate for (k = i; ((r = tab[j][l++]) == fbuf[k] && r != '\0'); k++); 14517c478bd9Sstevel@tonic-gate if (r == '\0') 14527c478bd9Sstevel@tonic-gate if (fbuf[k] == ' ' || fbuf[k] == '\n' || 14537c478bd9Sstevel@tonic-gate fbuf[k] == '\t' || fbuf[k] == '{' || 14547c478bd9Sstevel@tonic-gate fbuf[k] == '/') { 14557c478bd9Sstevel@tonic-gate i = k; 14567c478bd9Sstevel@tonic-gate return (1); 14577c478bd9Sstevel@tonic-gate } 14587c478bd9Sstevel@tonic-gate } 14597c478bd9Sstevel@tonic-gate return (0); 14607c478bd9Sstevel@tonic-gate } 14617c478bd9Sstevel@tonic-gate 14627c478bd9Sstevel@tonic-gate /* 14637c478bd9Sstevel@tonic-gate * ccom - 14647c478bd9Sstevel@tonic-gate * Increments the current index 'i' into the file buffer 'fbuf' past any 14657c478bd9Sstevel@tonic-gate * whitespace lines and C-style comments found, starting at the current 14667c478bd9Sstevel@tonic-gate * position of 'i'. Returns 1 as long as we don't increment i past the 14677c478bd9Sstevel@tonic-gate * size of fbuf (fbsz). Otherwise, returns 0. 14687c478bd9Sstevel@tonic-gate */ 14697c478bd9Sstevel@tonic-gate 14707c478bd9Sstevel@tonic-gate static int 14717c478bd9Sstevel@tonic-gate ccom(void) 14727c478bd9Sstevel@tonic-gate { 14737c478bd9Sstevel@tonic-gate register char cc; 14747c478bd9Sstevel@tonic-gate int len; 14757c478bd9Sstevel@tonic-gate 14767c478bd9Sstevel@tonic-gate while ((cc = fbuf[i]) == ' ' || cc == '\t' || cc == '\n') 14777c478bd9Sstevel@tonic-gate if (i++ >= fbsz) 14787c478bd9Sstevel@tonic-gate return (0); 14797c478bd9Sstevel@tonic-gate if (fbuf[i] == '/' && fbuf[i+1] == '*') { 14807c478bd9Sstevel@tonic-gate i += 2; 14817c478bd9Sstevel@tonic-gate while (fbuf[i] != '*' || fbuf[i+1] != '/') { 14827c478bd9Sstevel@tonic-gate if (fbuf[i] == '\\') 14837c478bd9Sstevel@tonic-gate i++; 14847c478bd9Sstevel@tonic-gate if ((len = mblen(&fbuf[i], MB_CUR_MAX)) <= 0) 14857c478bd9Sstevel@tonic-gate len = 1; 14867c478bd9Sstevel@tonic-gate i += len; 14877c478bd9Sstevel@tonic-gate if (i >= fbsz) 14887c478bd9Sstevel@tonic-gate return (0); 14897c478bd9Sstevel@tonic-gate } 14907c478bd9Sstevel@tonic-gate if ((i += 2) >= fbsz) 14917c478bd9Sstevel@tonic-gate return (0); 14927c478bd9Sstevel@tonic-gate } 14937c478bd9Sstevel@tonic-gate if (fbuf[i] == '\n') 14947c478bd9Sstevel@tonic-gate if (ccom() == 0) 14957c478bd9Sstevel@tonic-gate return (0); 14967c478bd9Sstevel@tonic-gate return (1); 14977c478bd9Sstevel@tonic-gate } 14987c478bd9Sstevel@tonic-gate 14997c478bd9Sstevel@tonic-gate /* 15007c478bd9Sstevel@tonic-gate * ascom - 15017c478bd9Sstevel@tonic-gate * Increments the current index 'i' into the file buffer 'fbuf' past 15027c478bd9Sstevel@tonic-gate * consecutive assembler program comment lines starting with ASCOMCHAR, 15037c478bd9Sstevel@tonic-gate * starting at the current position of 'i'. 15047c478bd9Sstevel@tonic-gate * Returns 1 as long as we don't increment i past the 15057c478bd9Sstevel@tonic-gate * size of fbuf (fbsz). Otherwise returns 0. 15067c478bd9Sstevel@tonic-gate */ 15077c478bd9Sstevel@tonic-gate 15087c478bd9Sstevel@tonic-gate static int 15097c478bd9Sstevel@tonic-gate ascom(void) 15107c478bd9Sstevel@tonic-gate { 15117c478bd9Sstevel@tonic-gate while (fbuf[i] == ASCOMCHAR) { 15127c478bd9Sstevel@tonic-gate i++; 15137c478bd9Sstevel@tonic-gate while (fbuf[i++] != '\n') 15147c478bd9Sstevel@tonic-gate if (i >= fbsz) 15157c478bd9Sstevel@tonic-gate return (0); 15167c478bd9Sstevel@tonic-gate while (fbuf[i] == '\n') 15177c478bd9Sstevel@tonic-gate if (i++ >= fbsz) 15187c478bd9Sstevel@tonic-gate return (0); 15197c478bd9Sstevel@tonic-gate } 15207c478bd9Sstevel@tonic-gate return (1); 15217c478bd9Sstevel@tonic-gate } 15227c478bd9Sstevel@tonic-gate 15237c478bd9Sstevel@tonic-gate static int 15247c478bd9Sstevel@tonic-gate sccs(void) 15257c478bd9Sstevel@tonic-gate { /* look for "1hddddd" where d is a digit */ 15267c478bd9Sstevel@tonic-gate register int j; 15277c478bd9Sstevel@tonic-gate 15287c478bd9Sstevel@tonic-gate if (fbuf[0] == 1 && fbuf[1] == 'h') { 15297c478bd9Sstevel@tonic-gate for (j = 2; j <= 6; j++) { 15307c478bd9Sstevel@tonic-gate if (isdigit(fbuf[j])) 15317c478bd9Sstevel@tonic-gate continue; 15327c478bd9Sstevel@tonic-gate else 15337c478bd9Sstevel@tonic-gate return (0); 15347c478bd9Sstevel@tonic-gate } 15357c478bd9Sstevel@tonic-gate } else { 15367c478bd9Sstevel@tonic-gate return (0); 15377c478bd9Sstevel@tonic-gate } 15387c478bd9Sstevel@tonic-gate return (1); 15397c478bd9Sstevel@tonic-gate } 15407c478bd9Sstevel@tonic-gate 15417c478bd9Sstevel@tonic-gate static int 15427c478bd9Sstevel@tonic-gate english(char *bp, int n) 15437c478bd9Sstevel@tonic-gate { 15447c478bd9Sstevel@tonic-gate #define NASC 128 /* number of ascii char ?? */ 15457c478bd9Sstevel@tonic-gate register int j, vow, freq, rare, len; 15467c478bd9Sstevel@tonic-gate register int badpun = 0, punct = 0; 15477c478bd9Sstevel@tonic-gate int ct[NASC]; 15487c478bd9Sstevel@tonic-gate 15497c478bd9Sstevel@tonic-gate if (n < 50) 15507c478bd9Sstevel@tonic-gate return (0); /* no point in statistics on squibs */ 15517c478bd9Sstevel@tonic-gate for (j = 0; j < NASC; j++) 15527c478bd9Sstevel@tonic-gate ct[j] = 0; 15537c478bd9Sstevel@tonic-gate for (j = 0; j < n; j += len) { 15547c478bd9Sstevel@tonic-gate if ((unsigned char)bp[j] < NASC) 15557c478bd9Sstevel@tonic-gate ct[bp[j]|040]++; 15567c478bd9Sstevel@tonic-gate switch (bp[j]) { 15577c478bd9Sstevel@tonic-gate case '.': 15587c478bd9Sstevel@tonic-gate case ',': 15597c478bd9Sstevel@tonic-gate case ')': 15607c478bd9Sstevel@tonic-gate case '%': 15617c478bd9Sstevel@tonic-gate case ';': 15627c478bd9Sstevel@tonic-gate case ':': 15637c478bd9Sstevel@tonic-gate case '?': 15647c478bd9Sstevel@tonic-gate punct++; 15657c478bd9Sstevel@tonic-gate if (j < n-1 && bp[j+1] != ' ' && bp[j+1] != '\n') 15667c478bd9Sstevel@tonic-gate badpun++; 15677c478bd9Sstevel@tonic-gate } 15687c478bd9Sstevel@tonic-gate if ((len = mblen(&bp[j], MB_CUR_MAX)) <= 0) 15697c478bd9Sstevel@tonic-gate len = 1; 15707c478bd9Sstevel@tonic-gate } 15717c478bd9Sstevel@tonic-gate if (badpun*5 > punct) 15727c478bd9Sstevel@tonic-gate return (0); 15737c478bd9Sstevel@tonic-gate vow = ct['a'] + ct['e'] + ct['i'] + ct['o'] + ct['u']; 15747c478bd9Sstevel@tonic-gate freq = ct['e'] + ct['t'] + ct['a'] + ct['i'] + ct['o'] + ct['n']; 15757c478bd9Sstevel@tonic-gate rare = ct['v'] + ct['j'] + ct['k'] + ct['q'] + ct['x'] + ct['z']; 15767c478bd9Sstevel@tonic-gate if (2*ct[';'] > ct['e']) 15777c478bd9Sstevel@tonic-gate return (0); 15787c478bd9Sstevel@tonic-gate if ((ct['>'] + ct['<'] + ct['/']) > ct['e']) 15797c478bd9Sstevel@tonic-gate return (0); /* shell file test */ 15807c478bd9Sstevel@tonic-gate return (vow * 5 >= n - ct[' '] && freq >= 10 * rare); 15817c478bd9Sstevel@tonic-gate } 15827c478bd9Sstevel@tonic-gate 15837c478bd9Sstevel@tonic-gate /* 15847c478bd9Sstevel@tonic-gate * Convert a word from an elf file to native format. 15857c478bd9Sstevel@tonic-gate * This is needed because there's no elf routine to 15867c478bd9Sstevel@tonic-gate * get and decode a Note section header. 15877c478bd9Sstevel@tonic-gate */ 15887c478bd9Sstevel@tonic-gate static void 15897c478bd9Sstevel@tonic-gate convert_gelf_word(Elf *elf, GElf_Word *data, int version, int format) 15907c478bd9Sstevel@tonic-gate { 15917c478bd9Sstevel@tonic-gate Elf_Data src, dst; 15927c478bd9Sstevel@tonic-gate 15937c478bd9Sstevel@tonic-gate dst.d_buf = data; 15947c478bd9Sstevel@tonic-gate dst.d_version = version; 15957c478bd9Sstevel@tonic-gate dst.d_size = sizeof (GElf_Word); 15967c478bd9Sstevel@tonic-gate dst.d_type = ELF_T_WORD; 15977c478bd9Sstevel@tonic-gate src.d_buf = data; 15987c478bd9Sstevel@tonic-gate src.d_version = version; 15997c478bd9Sstevel@tonic-gate src.d_size = sizeof (GElf_Word); 16007c478bd9Sstevel@tonic-gate src.d_type = ELF_T_WORD; 16017c478bd9Sstevel@tonic-gate (void) gelf_xlatetom(elf, &dst, &src, format); 16027c478bd9Sstevel@tonic-gate } 16037c478bd9Sstevel@tonic-gate 16047c478bd9Sstevel@tonic-gate static void 16057c478bd9Sstevel@tonic-gate convert_gelf_nhdr(Elf *elf, GElf_Nhdr *nhdr, GElf_Word version, int format) 16067c478bd9Sstevel@tonic-gate { 16077c478bd9Sstevel@tonic-gate convert_gelf_word(elf, &nhdr->n_namesz, version, format); 16087c478bd9Sstevel@tonic-gate convert_gelf_word(elf, &nhdr->n_descsz, version, format); 16097c478bd9Sstevel@tonic-gate convert_gelf_word(elf, &nhdr->n_type, version, format); 16107c478bd9Sstevel@tonic-gate } 16117c478bd9Sstevel@tonic-gate 16127c478bd9Sstevel@tonic-gate /* 16137c478bd9Sstevel@tonic-gate * Return true if it is an old (pre-restructured /proc) core file. 16147c478bd9Sstevel@tonic-gate */ 16157c478bd9Sstevel@tonic-gate static int 16167c478bd9Sstevel@tonic-gate old_core(Elf *elf, GElf_Ehdr *ehdr, int format) 16177c478bd9Sstevel@tonic-gate { 16187c478bd9Sstevel@tonic-gate register int inx; 16197c478bd9Sstevel@tonic-gate GElf_Phdr phdr; 16207c478bd9Sstevel@tonic-gate GElf_Phdr nphdr; 16217c478bd9Sstevel@tonic-gate GElf_Nhdr nhdr; 16227c478bd9Sstevel@tonic-gate off_t offset; 16237c478bd9Sstevel@tonic-gate 16247c478bd9Sstevel@tonic-gate if (ehdr->e_type != ET_CORE) 16257c478bd9Sstevel@tonic-gate return (0); 16267c478bd9Sstevel@tonic-gate for (inx = 0; inx < (int)ehdr->e_phnum; inx++) { 16277c478bd9Sstevel@tonic-gate if (gelf_getphdr(elf, inx, &phdr) == NULL) { 16287c478bd9Sstevel@tonic-gate return (0); 16297c478bd9Sstevel@tonic-gate } 16307c478bd9Sstevel@tonic-gate if (phdr.p_type == PT_NOTE) { 16317c478bd9Sstevel@tonic-gate /* 16327c478bd9Sstevel@tonic-gate * If the next segment is also a note, use it instead. 16337c478bd9Sstevel@tonic-gate */ 16347c478bd9Sstevel@tonic-gate if (gelf_getphdr(elf, inx+1, &nphdr) == NULL) { 16357c478bd9Sstevel@tonic-gate return (0); 16367c478bd9Sstevel@tonic-gate } 16377c478bd9Sstevel@tonic-gate if (nphdr.p_type == PT_NOTE) 16387c478bd9Sstevel@tonic-gate phdr = nphdr; 16397c478bd9Sstevel@tonic-gate offset = (off_t)phdr.p_offset; 16407c478bd9Sstevel@tonic-gate (void) pread(ifd, &nhdr, sizeof (GElf_Nhdr), offset); 16417c478bd9Sstevel@tonic-gate convert_gelf_nhdr(elf, &nhdr, ehdr->e_version, format); 16427c478bd9Sstevel@tonic-gate /* 16437c478bd9Sstevel@tonic-gate * Old core files have type NT_PRPSINFO. 16447c478bd9Sstevel@tonic-gate */ 16457c478bd9Sstevel@tonic-gate if (nhdr.n_type == NT_PRPSINFO) 16467c478bd9Sstevel@tonic-gate return (1); 16477c478bd9Sstevel@tonic-gate return (0); 16487c478bd9Sstevel@tonic-gate } 16497c478bd9Sstevel@tonic-gate } 16507c478bd9Sstevel@tonic-gate return (0); 16517c478bd9Sstevel@tonic-gate } 16527c478bd9Sstevel@tonic-gate 16537c478bd9Sstevel@tonic-gate /* 16547c478bd9Sstevel@tonic-gate * If it's a core file, print out the name of the file that dumped core. 16557c478bd9Sstevel@tonic-gate */ 16567c478bd9Sstevel@tonic-gate static int 1657*9a411307Srie core(Elf *elf, GElf_Ehdr *ehdr, int format, char *file) 16587c478bd9Sstevel@tonic-gate { 16597c478bd9Sstevel@tonic-gate register int inx; 16607c478bd9Sstevel@tonic-gate char *psinfo; 16617c478bd9Sstevel@tonic-gate GElf_Phdr phdr; 16627c478bd9Sstevel@tonic-gate GElf_Phdr nphdr; 16637c478bd9Sstevel@tonic-gate GElf_Nhdr nhdr; 16647c478bd9Sstevel@tonic-gate off_t offset; 16657c478bd9Sstevel@tonic-gate 16667c478bd9Sstevel@tonic-gate if (ehdr->e_type != ET_CORE) 16677c478bd9Sstevel@tonic-gate return (0); 16687c478bd9Sstevel@tonic-gate for (inx = 0; inx < (int)ehdr->e_phnum; inx++) { 16697c478bd9Sstevel@tonic-gate if (gelf_getphdr(elf, inx, &phdr) == NULL) { 1670*9a411307Srie (void) fprintf(stderr, gettext("%s: %s: can't read " 1671*9a411307Srie "ELF program header\n"), File, file); 16727c478bd9Sstevel@tonic-gate return (0); 16737c478bd9Sstevel@tonic-gate } 16747c478bd9Sstevel@tonic-gate if (phdr.p_type == PT_NOTE) { 16757c478bd9Sstevel@tonic-gate char *fname; 16767c478bd9Sstevel@tonic-gate size_t size; 1677*9a411307Srie 16787c478bd9Sstevel@tonic-gate /* 16797c478bd9Sstevel@tonic-gate * If the next segment is also a note, use it instead. 16807c478bd9Sstevel@tonic-gate */ 16817c478bd9Sstevel@tonic-gate if (gelf_getphdr(elf, inx+1, &nphdr) == NULL) { 1682*9a411307Srie (void) fprintf(stderr, gettext("%s: %s: can't " 1683*9a411307Srie "read ELF program header\n"), File, file); 16847c478bd9Sstevel@tonic-gate return (0); 16857c478bd9Sstevel@tonic-gate } 16867c478bd9Sstevel@tonic-gate if (nphdr.p_type == PT_NOTE) 16877c478bd9Sstevel@tonic-gate phdr = nphdr; 16887c478bd9Sstevel@tonic-gate offset = (off_t)phdr.p_offset; 16897c478bd9Sstevel@tonic-gate (void) pread(ifd, &nhdr, sizeof (GElf_Nhdr), offset); 16907c478bd9Sstevel@tonic-gate convert_gelf_nhdr(elf, &nhdr, ehdr->e_version, format); 1691*9a411307Srie 16927c478bd9Sstevel@tonic-gate /* 16937c478bd9Sstevel@tonic-gate * Note: the ABI states that n_namesz must 16947c478bd9Sstevel@tonic-gate * be rounded up to a 4 byte boundary. 16957c478bd9Sstevel@tonic-gate */ 16967c478bd9Sstevel@tonic-gate offset += sizeof (GElf_Nhdr) + 16977c478bd9Sstevel@tonic-gate ((nhdr.n_namesz + 0x03) & ~0x3); 16987c478bd9Sstevel@tonic-gate size = nhdr.n_descsz; 1699*9a411307Srie if ((psinfo = malloc(size)) == NULL) { 1700*9a411307Srie int err = errno; 1701*9a411307Srie (void) fprintf(stderr, gettext("%s: malloc " 1702*9a411307Srie "failed: %s\n"), File, strerror(err)); 1703*9a411307Srie exit(2); 1704*9a411307Srie } 17057c478bd9Sstevel@tonic-gate (void) pread(ifd, psinfo, size, offset); 1706*9a411307Srie 17077c478bd9Sstevel@tonic-gate /* 17087c478bd9Sstevel@tonic-gate * We want to print the string contained 17097c478bd9Sstevel@tonic-gate * in psinfo->pr_fname[], where 'psinfo' 17107c478bd9Sstevel@tonic-gate * is either an old NT_PRPSINFO structure 17117c478bd9Sstevel@tonic-gate * or a new NT_PSINFO structure. 17127c478bd9Sstevel@tonic-gate * 17137c478bd9Sstevel@tonic-gate * Old core files have only type NT_PRPSINFO. 17147c478bd9Sstevel@tonic-gate * New core files have type NT_PSINFO. 17157c478bd9Sstevel@tonic-gate * 17167c478bd9Sstevel@tonic-gate * These structures are also different by 17177c478bd9Sstevel@tonic-gate * virtue of being contained in a core file 17187c478bd9Sstevel@tonic-gate * of either 32-bit or 64-bit type. 17197c478bd9Sstevel@tonic-gate * 17207c478bd9Sstevel@tonic-gate * To further complicate matters, we ourself 17217c478bd9Sstevel@tonic-gate * might be compiled either 32-bit or 64-bit. 17227c478bd9Sstevel@tonic-gate * 17237c478bd9Sstevel@tonic-gate * For these reason, we just *know* the offsets of 17247c478bd9Sstevel@tonic-gate * pr_fname[] into the four different structures 17257c478bd9Sstevel@tonic-gate * here, regardless of how we are compiled. 17267c478bd9Sstevel@tonic-gate */ 17277c478bd9Sstevel@tonic-gate if (gelf_getclass(elf) == ELFCLASS32) { 17287c478bd9Sstevel@tonic-gate /* 32-bit core file, 32-bit structures */ 17297c478bd9Sstevel@tonic-gate if (nhdr.n_type == NT_PSINFO) 17307c478bd9Sstevel@tonic-gate fname = psinfo + 88; 17317c478bd9Sstevel@tonic-gate else /* old: NT_PRPSINFO */ 17327c478bd9Sstevel@tonic-gate fname = psinfo + 84; 17337c478bd9Sstevel@tonic-gate } else if (gelf_getclass(elf) == ELFCLASS64) { 17347c478bd9Sstevel@tonic-gate /* 64-bit core file, 64-bit structures */ 17357c478bd9Sstevel@tonic-gate if (nhdr.n_type == NT_PSINFO) 17367c478bd9Sstevel@tonic-gate fname = psinfo + 136; 17377c478bd9Sstevel@tonic-gate else /* old: NT_PRPSINFO */ 17387c478bd9Sstevel@tonic-gate fname = psinfo + 120; 17397c478bd9Sstevel@tonic-gate } else { 17407c478bd9Sstevel@tonic-gate free(psinfo); 17417c478bd9Sstevel@tonic-gate break; 17427c478bd9Sstevel@tonic-gate } 17437c478bd9Sstevel@tonic-gate (void) printf(gettext(", from '%s'"), fname); 17447c478bd9Sstevel@tonic-gate free(psinfo); 17457c478bd9Sstevel@tonic-gate break; 17467c478bd9Sstevel@tonic-gate } 17477c478bd9Sstevel@tonic-gate } 17487c478bd9Sstevel@tonic-gate return (1); 17497c478bd9Sstevel@tonic-gate } 17507c478bd9Sstevel@tonic-gate 17517c478bd9Sstevel@tonic-gate static int 17527c478bd9Sstevel@tonic-gate shellscript(char buf[], struct stat64 *sb) 17537c478bd9Sstevel@tonic-gate { 17547c478bd9Sstevel@tonic-gate char *tp, *cp, *xp, *up, *gp; 17557c478bd9Sstevel@tonic-gate 17567c478bd9Sstevel@tonic-gate cp = strchr(buf, '\n'); 17577c478bd9Sstevel@tonic-gate if (cp == NULL || cp - fbuf > fbsz) 17587c478bd9Sstevel@tonic-gate return (0); 17597c478bd9Sstevel@tonic-gate for (tp = buf; tp != cp && isspace((unsigned char)*tp); tp++) 17607c478bd9Sstevel@tonic-gate if (!isascii(*tp)) 17617c478bd9Sstevel@tonic-gate return (0); 17627c478bd9Sstevel@tonic-gate for (xp = tp; tp != cp && !isspace((unsigned char)*tp); tp++) 17637c478bd9Sstevel@tonic-gate if (!isascii(*tp)) 17647c478bd9Sstevel@tonic-gate return (0); 17657c478bd9Sstevel@tonic-gate if (tp == xp) 17667c478bd9Sstevel@tonic-gate return (0); 17677c478bd9Sstevel@tonic-gate if (sb->st_mode & S_ISUID) 17687c478bd9Sstevel@tonic-gate up = gettext("set-uid "); 17697c478bd9Sstevel@tonic-gate else 17707c478bd9Sstevel@tonic-gate up = ""; 17717c478bd9Sstevel@tonic-gate 17727c478bd9Sstevel@tonic-gate if (sb->st_mode & S_ISGID) 17737c478bd9Sstevel@tonic-gate gp = gettext("set-gid "); 17747c478bd9Sstevel@tonic-gate else 17757c478bd9Sstevel@tonic-gate gp = ""; 17767c478bd9Sstevel@tonic-gate 17777c478bd9Sstevel@tonic-gate if (strncmp(xp, "/bin/sh", tp - xp) == 0) 17787c478bd9Sstevel@tonic-gate xp = gettext("shell"); 17797c478bd9Sstevel@tonic-gate else if (strncmp(xp, "/bin/csh", tp - xp) == 0) 17807c478bd9Sstevel@tonic-gate xp = gettext("c-shell"); 17817c478bd9Sstevel@tonic-gate else if (strncmp(xp, "/usr/sbin/dtrace", tp - xp) == 0) 17827c478bd9Sstevel@tonic-gate xp = gettext("DTrace"); 17837c478bd9Sstevel@tonic-gate else 17847c478bd9Sstevel@tonic-gate *tp = '\0'; 17857c478bd9Sstevel@tonic-gate /* 17867c478bd9Sstevel@tonic-gate * TRANSLATION_NOTE 17877c478bd9Sstevel@tonic-gate * This message is printed by file command for shell scripts. 17887c478bd9Sstevel@tonic-gate * The first %s is for the translation for "set-uid " (if the script 17897c478bd9Sstevel@tonic-gate * has the set-uid bit set), or is for an empty string (if the 17907c478bd9Sstevel@tonic-gate * script does not have the set-uid bit set). 17917c478bd9Sstevel@tonic-gate * Similarly, the second %s is for the translation for "set-gid ", 17927c478bd9Sstevel@tonic-gate * or is for an empty string. 17937c478bd9Sstevel@tonic-gate * The third %s is for the translation for either: "shell", "c-shell", 17947c478bd9Sstevel@tonic-gate * or "DTrace", or is for the pathname of the program the script 17957c478bd9Sstevel@tonic-gate * executes. 17967c478bd9Sstevel@tonic-gate */ 17977c478bd9Sstevel@tonic-gate (void) printf(gettext("%s%sexecutable %s script\n"), up, gp, xp); 17987c478bd9Sstevel@tonic-gate return (1); 17997c478bd9Sstevel@tonic-gate } 18007c478bd9Sstevel@tonic-gate 18017c478bd9Sstevel@tonic-gate static int 18027c478bd9Sstevel@tonic-gate get_door_target(char *file, char *buf, size_t bufsize) 18037c478bd9Sstevel@tonic-gate { 18047c478bd9Sstevel@tonic-gate int fd; 18057c478bd9Sstevel@tonic-gate door_info_t di; 18067c478bd9Sstevel@tonic-gate psinfo_t psinfo; 18077c478bd9Sstevel@tonic-gate 18087c478bd9Sstevel@tonic-gate if ((fd = open64(file, O_RDONLY)) < 0 || 18097c478bd9Sstevel@tonic-gate door_info(fd, &di) != 0) { 18107c478bd9Sstevel@tonic-gate if (fd >= 0) 18117c478bd9Sstevel@tonic-gate (void) close(fd); 18127c478bd9Sstevel@tonic-gate return (-1); 18137c478bd9Sstevel@tonic-gate } 18147c478bd9Sstevel@tonic-gate (void) close(fd); 18157c478bd9Sstevel@tonic-gate 18167c478bd9Sstevel@tonic-gate (void) sprintf(buf, "/proc/%ld/psinfo", di.di_target); 18177c478bd9Sstevel@tonic-gate if ((fd = open64(buf, O_RDONLY)) < 0 || 18187c478bd9Sstevel@tonic-gate read(fd, &psinfo, sizeof (psinfo)) != sizeof (psinfo)) { 18197c478bd9Sstevel@tonic-gate if (fd >= 0) 18207c478bd9Sstevel@tonic-gate (void) close(fd); 18217c478bd9Sstevel@tonic-gate return (-1); 18227c478bd9Sstevel@tonic-gate } 18237c478bd9Sstevel@tonic-gate (void) close(fd); 18247c478bd9Sstevel@tonic-gate 18257c478bd9Sstevel@tonic-gate (void) snprintf(buf, bufsize, "%s[%ld]", psinfo.pr_fname, di.di_target); 18267c478bd9Sstevel@tonic-gate return (0); 18277c478bd9Sstevel@tonic-gate } 18287c478bd9Sstevel@tonic-gate 18297c478bd9Sstevel@tonic-gate /* 18307c478bd9Sstevel@tonic-gate * ZIP file header information 18317c478bd9Sstevel@tonic-gate */ 18327c478bd9Sstevel@tonic-gate #define SIGSIZ 4 18337c478bd9Sstevel@tonic-gate #define LOCSIG "PK\003\004" 18347c478bd9Sstevel@tonic-gate #define LOCHDRSIZ 30 18357c478bd9Sstevel@tonic-gate 18367c478bd9Sstevel@tonic-gate #define CH(b, n) (((unsigned char *)(b))[n]) 18377c478bd9Sstevel@tonic-gate #define SH(b, n) (CH(b, n) | (CH(b, n+1) << 8)) 18387c478bd9Sstevel@tonic-gate #define LG(b, n) (SH(b, n) | (SH(b, n+2) << 16)) 18397c478bd9Sstevel@tonic-gate 18407c478bd9Sstevel@tonic-gate #define LOCNAM(b) (SH(b, 26)) /* filename size */ 18417c478bd9Sstevel@tonic-gate #define LOCEXT(b) (SH(b, 28)) /* extra field size */ 18427c478bd9Sstevel@tonic-gate 18437c478bd9Sstevel@tonic-gate #define XFHSIZ 4 /* header id, data size */ 18447c478bd9Sstevel@tonic-gate #define XFHID(b) (SH(b, 0)) /* extract field header id */ 18457c478bd9Sstevel@tonic-gate #define XFDATASIZ(b) (SH(b, 2)) /* extract field data size */ 18467c478bd9Sstevel@tonic-gate #define XFJAVASIG 0xcafe /* java executables */ 18477c478bd9Sstevel@tonic-gate 18487c478bd9Sstevel@tonic-gate static int 18497c478bd9Sstevel@tonic-gate zipfile(char *fbuf, int fd) 18507c478bd9Sstevel@tonic-gate { 18517c478bd9Sstevel@tonic-gate off_t xoff, xoff_end; 18527c478bd9Sstevel@tonic-gate 18537c478bd9Sstevel@tonic-gate if (strncmp(fbuf, LOCSIG, SIGSIZ) != 0) 18547c478bd9Sstevel@tonic-gate return (0); 18557c478bd9Sstevel@tonic-gate 18567c478bd9Sstevel@tonic-gate xoff = LOCHDRSIZ + LOCNAM(fbuf); 18577c478bd9Sstevel@tonic-gate xoff_end = xoff + LOCEXT(fbuf); 18587c478bd9Sstevel@tonic-gate 18597c478bd9Sstevel@tonic-gate while (xoff < xoff_end) { 18607c478bd9Sstevel@tonic-gate char xfhdr[XFHSIZ]; 18617c478bd9Sstevel@tonic-gate 18627c478bd9Sstevel@tonic-gate if (pread(fd, xfhdr, XFHSIZ, xoff) != XFHSIZ) 18637c478bd9Sstevel@tonic-gate break; 18647c478bd9Sstevel@tonic-gate 18657c478bd9Sstevel@tonic-gate if (XFHID(xfhdr) == XFJAVASIG) { 18667c478bd9Sstevel@tonic-gate (void) printf("%s\n", gettext("java program")); 18677c478bd9Sstevel@tonic-gate return (1); 18687c478bd9Sstevel@tonic-gate } 18697c478bd9Sstevel@tonic-gate xoff += sizeof (xfhdr) + XFDATASIZ(xfhdr); 18707c478bd9Sstevel@tonic-gate } 18717c478bd9Sstevel@tonic-gate 18727c478bd9Sstevel@tonic-gate /* 18737c478bd9Sstevel@tonic-gate * We could just print "ZIP archive" here. 18747c478bd9Sstevel@tonic-gate * 18757c478bd9Sstevel@tonic-gate * However, customers may be using their own entries in 18767c478bd9Sstevel@tonic-gate * /etc/magic to distinguish one kind of ZIP file from another, so 18777c478bd9Sstevel@tonic-gate * let's defer the printing of "ZIP archive" to there. 18787c478bd9Sstevel@tonic-gate */ 18797c478bd9Sstevel@tonic-gate return (0); 18807c478bd9Sstevel@tonic-gate } 18817c478bd9Sstevel@tonic-gate 18827c478bd9Sstevel@tonic-gate static int 18837c478bd9Sstevel@tonic-gate is_crash_dump(const char *buf, int fd) 18847c478bd9Sstevel@tonic-gate { 18857c478bd9Sstevel@tonic-gate /* LINTED: pointer cast may result in improper alignment */ 18867c478bd9Sstevel@tonic-gate const dumphdr_t *dhp = (const dumphdr_t *)buf; 18877c478bd9Sstevel@tonic-gate 18887c478bd9Sstevel@tonic-gate /* 18897c478bd9Sstevel@tonic-gate * The current DUMP_MAGIC string covers Solaris 7 and later releases. 18907c478bd9Sstevel@tonic-gate * The utsname struct is only present in dumphdr_t's with dump_version 18917c478bd9Sstevel@tonic-gate * greater than or equal to 9. 18927c478bd9Sstevel@tonic-gate */ 18937c478bd9Sstevel@tonic-gate if (dhp->dump_magic == DUMP_MAGIC) { 18947c478bd9Sstevel@tonic-gate print_dumphdr(fd, dhp, return_uint32, NATIVE_ISA); 18957c478bd9Sstevel@tonic-gate 18967c478bd9Sstevel@tonic-gate } else if (dhp->dump_magic == swap_uint32(DUMP_MAGIC)) { 18977c478bd9Sstevel@tonic-gate print_dumphdr(fd, dhp, swap_uint32, OTHER_ISA); 18987c478bd9Sstevel@tonic-gate 18997c478bd9Sstevel@tonic-gate } else if (dhp->dump_magic == OLD_DUMP_MAGIC || 19007c478bd9Sstevel@tonic-gate dhp->dump_magic == swap_uint32(OLD_DUMP_MAGIC)) { 19017c478bd9Sstevel@tonic-gate char *isa = (dhp->dump_magic == OLD_DUMP_MAGIC ? 19027c478bd9Sstevel@tonic-gate NATIVE_ISA : OTHER_ISA); 19037c478bd9Sstevel@tonic-gate (void) printf(gettext("SunOS 32-bit %s crash dump\n"), isa); 19047c478bd9Sstevel@tonic-gate 19057c478bd9Sstevel@tonic-gate } else { 19067c478bd9Sstevel@tonic-gate return (0); 19077c478bd9Sstevel@tonic-gate } 19087c478bd9Sstevel@tonic-gate 19097c478bd9Sstevel@tonic-gate return (1); 19107c478bd9Sstevel@tonic-gate } 19117c478bd9Sstevel@tonic-gate 19127c478bd9Sstevel@tonic-gate static void 19137c478bd9Sstevel@tonic-gate print_dumphdr(const int fd, const dumphdr_t *dhp, uint32_t (*swap)(uint32_t), 19147c478bd9Sstevel@tonic-gate const char *isa) 19157c478bd9Sstevel@tonic-gate { 19167c478bd9Sstevel@tonic-gate dumphdr_t dh; 19177c478bd9Sstevel@tonic-gate 19187c478bd9Sstevel@tonic-gate /* 19197c478bd9Sstevel@tonic-gate * A dumphdr_t is bigger than FBSZ, so we have to manually read the 19207c478bd9Sstevel@tonic-gate * rest of it. 19217c478bd9Sstevel@tonic-gate */ 19227c478bd9Sstevel@tonic-gate if (swap(dhp->dump_version) > 8 && pread(fd, &dh, sizeof (dumphdr_t), 19237c478bd9Sstevel@tonic-gate (off_t)0) == sizeof (dumphdr_t)) { 19247c478bd9Sstevel@tonic-gate (void) printf(gettext( 19257c478bd9Sstevel@tonic-gate "%s %s %s %u-bit %s crash dump from '%s'\n"), 19267c478bd9Sstevel@tonic-gate dh.dump_utsname.sysname, dh.dump_utsname.release, 19277c478bd9Sstevel@tonic-gate dh.dump_utsname.version, swap(dh.dump_wordsize), isa, 19287c478bd9Sstevel@tonic-gate dh.dump_utsname.nodename); 19297c478bd9Sstevel@tonic-gate } else { 19307c478bd9Sstevel@tonic-gate (void) printf(gettext("SunOS %u-bit %s crash dump\n"), 19317c478bd9Sstevel@tonic-gate swap(dhp->dump_wordsize), isa); 19327c478bd9Sstevel@tonic-gate } 19337c478bd9Sstevel@tonic-gate } 19347c478bd9Sstevel@tonic-gate 19357c478bd9Sstevel@tonic-gate static void 19367c478bd9Sstevel@tonic-gate usage(void) 19377c478bd9Sstevel@tonic-gate { 19387c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 19397c478bd9Sstevel@tonic-gate "usage: file [-dh] [-M mfile] [-m mfile] [-f ffile] file ...\n" 19407c478bd9Sstevel@tonic-gate " file [-dh] [-M mfile] [-m mfile] -f ffile\n" 19417c478bd9Sstevel@tonic-gate " file -i [-h] [-f ffile] file ...\n" 19427c478bd9Sstevel@tonic-gate " file -i [-h] -f ffile\n" 19437c478bd9Sstevel@tonic-gate " file -c [-d] [-M mfile] [-m mfile]\n")); 19447c478bd9Sstevel@tonic-gate exit(2); 19457c478bd9Sstevel@tonic-gate } 19467c478bd9Sstevel@tonic-gate 19477c478bd9Sstevel@tonic-gate static uint32_t 19487c478bd9Sstevel@tonic-gate swap_uint32(uint32_t in) 19497c478bd9Sstevel@tonic-gate { 19507c478bd9Sstevel@tonic-gate uint32_t out; 19517c478bd9Sstevel@tonic-gate 19527c478bd9Sstevel@tonic-gate out = (in & 0x000000ff) << 24; 19537c478bd9Sstevel@tonic-gate out |= (in & 0x0000ff00) << 8; /* >> 8 << 16 */ 19547c478bd9Sstevel@tonic-gate out |= (in & 0x00ff0000) >> 8; /* >> 16 << 8 */ 19557c478bd9Sstevel@tonic-gate out |= (in & 0xff000000) >> 24; 19567c478bd9Sstevel@tonic-gate 19577c478bd9Sstevel@tonic-gate return (out); 19587c478bd9Sstevel@tonic-gate } 19597c478bd9Sstevel@tonic-gate 19607c478bd9Sstevel@tonic-gate static uint32_t 19617c478bd9Sstevel@tonic-gate return_uint32(uint32_t in) 19627c478bd9Sstevel@tonic-gate { 19637c478bd9Sstevel@tonic-gate return (in); 19647c478bd9Sstevel@tonic-gate } 19657c478bd9Sstevel@tonic-gate 19667c478bd9Sstevel@tonic-gate /* 19677c478bd9Sstevel@tonic-gate * Check if str is in the string list str_list. 19687c478bd9Sstevel@tonic-gate */ 19697c478bd9Sstevel@tonic-gate static int 19707c478bd9Sstevel@tonic-gate is_in_list(char *str_list[], char *str) 19717c478bd9Sstevel@tonic-gate { 19727c478bd9Sstevel@tonic-gate int i; 19737c478bd9Sstevel@tonic-gate 19747c478bd9Sstevel@tonic-gate /* 19757c478bd9Sstevel@tonic-gate * Only need to compare the strlen(str_list[i]) bytes. 19767c478bd9Sstevel@tonic-gate * That way .stab will match on .stab* sections, and 19777c478bd9Sstevel@tonic-gate * .debug will match on .debug* sections. 19787c478bd9Sstevel@tonic-gate */ 19797c478bd9Sstevel@tonic-gate for (i = 0; str_list[i] != NULL; i++) { 19807c478bd9Sstevel@tonic-gate if (strncmp(str_list[i], str, strlen(str_list[i])) == 0) { 19817c478bd9Sstevel@tonic-gate return (1); 19827c478bd9Sstevel@tonic-gate } 19837c478bd9Sstevel@tonic-gate } 19847c478bd9Sstevel@tonic-gate return (0); 19857c478bd9Sstevel@tonic-gate } 19867c478bd9Sstevel@tonic-gate 19877c478bd9Sstevel@tonic-gate /* 19887c478bd9Sstevel@tonic-gate * default_magic - 19897c478bd9Sstevel@tonic-gate * allocate space for and create the default magic file 19907c478bd9Sstevel@tonic-gate * name string. 19917c478bd9Sstevel@tonic-gate */ 19927c478bd9Sstevel@tonic-gate 19937c478bd9Sstevel@tonic-gate static void 19947c478bd9Sstevel@tonic-gate default_magic(void) 19957c478bd9Sstevel@tonic-gate { 19967c478bd9Sstevel@tonic-gate const char *msg_locale = setlocale(LC_MESSAGES, NULL); 19977c478bd9Sstevel@tonic-gate struct stat statbuf; 19987c478bd9Sstevel@tonic-gate 1999*9a411307Srie if ((dfile = malloc(strlen(msg_locale) + 35)) == NULL) { 2000*9a411307Srie int err = errno; 2001*9a411307Srie (void) fprintf(stderr, gettext("%s: malloc failed: %s\n"), 2002*9a411307Srie File, strerror(err)); 20037c478bd9Sstevel@tonic-gate exit(2); 20047c478bd9Sstevel@tonic-gate } 20057c478bd9Sstevel@tonic-gate (void) snprintf(dfile, strlen(msg_locale) + 35, 20067c478bd9Sstevel@tonic-gate "/usr/lib/locale/%s/LC_MESSAGES/magic", msg_locale); 20077c478bd9Sstevel@tonic-gate if (stat(dfile, &statbuf) != 0) { 20087c478bd9Sstevel@tonic-gate (void) strcpy(dfile, "/etc/magic"); 20097c478bd9Sstevel@tonic-gate } 20107c478bd9Sstevel@tonic-gate } 20117c478bd9Sstevel@tonic-gate 20127c478bd9Sstevel@tonic-gate /* 20137c478bd9Sstevel@tonic-gate * add_to_mlist - 20147c478bd9Sstevel@tonic-gate * Add the given magic_file filename string to the list of magic 20157c478bd9Sstevel@tonic-gate * files (mlist). This list of files will later be examined, and 20167c478bd9Sstevel@tonic-gate * each magic file's entries will be added in order to 20177c478bd9Sstevel@tonic-gate * the mtab table. 20187c478bd9Sstevel@tonic-gate * 20197c478bd9Sstevel@tonic-gate * The first flag is set to 1 to add to the first list, mlist1. 20207c478bd9Sstevel@tonic-gate * The first flag is set to 0 to add to the second list, mlist2. 20217c478bd9Sstevel@tonic-gate */ 20227c478bd9Sstevel@tonic-gate 20237c478bd9Sstevel@tonic-gate static void 20247c478bd9Sstevel@tonic-gate add_to_mlist(char *magic_file, int first) 20257c478bd9Sstevel@tonic-gate { 20267c478bd9Sstevel@tonic-gate char **mlist; /* ordered list of magic files */ 20277c478bd9Sstevel@tonic-gate size_t mlist_sz; /* number of pointers allocated for mlist */ 20287c478bd9Sstevel@tonic-gate char **mlistp; /* next entry in mlist */ 20297c478bd9Sstevel@tonic-gate size_t mlistp_off; 20307c478bd9Sstevel@tonic-gate 20317c478bd9Sstevel@tonic-gate if (first) { 20327c478bd9Sstevel@tonic-gate mlist = mlist1; 20337c478bd9Sstevel@tonic-gate mlist_sz = mlist1_sz; 20347c478bd9Sstevel@tonic-gate mlistp = mlist1p; 20357c478bd9Sstevel@tonic-gate } else { 20367c478bd9Sstevel@tonic-gate mlist = mlist2; 20377c478bd9Sstevel@tonic-gate mlist_sz = mlist2_sz; 20387c478bd9Sstevel@tonic-gate mlistp = mlist2p; 20397c478bd9Sstevel@tonic-gate } 20407c478bd9Sstevel@tonic-gate 20417c478bd9Sstevel@tonic-gate if (mlist == NULL) { /* initial mlist allocation */ 2042*9a411307Srie if ((mlist = calloc(MLIST_SZ, sizeof (char *))) == NULL) { 2043*9a411307Srie int err = errno; 2044*9a411307Srie (void) fprintf(stderr, gettext("%s: malloc " 2045*9a411307Srie "failed: %s\n"), File, strerror(err)); 20467c478bd9Sstevel@tonic-gate exit(2); 20477c478bd9Sstevel@tonic-gate } 20487c478bd9Sstevel@tonic-gate mlist_sz = MLIST_SZ; 20497c478bd9Sstevel@tonic-gate mlistp = mlist; 20507c478bd9Sstevel@tonic-gate } 20517c478bd9Sstevel@tonic-gate if ((mlistp - mlist) >= mlist_sz) { 20527c478bd9Sstevel@tonic-gate mlistp_off = mlistp - mlist; 20537c478bd9Sstevel@tonic-gate mlist_sz *= 2; 2054*9a411307Srie if ((mlist = realloc(mlist, 20557c478bd9Sstevel@tonic-gate mlist_sz * sizeof (char *))) == NULL) { 2056*9a411307Srie int err = errno; 2057*9a411307Srie (void) fprintf(stderr, gettext("%s: malloc " 2058*9a411307Srie "failed: %s\n"), File, strerror(err)); 20597c478bd9Sstevel@tonic-gate exit(2); 20607c478bd9Sstevel@tonic-gate } 20617c478bd9Sstevel@tonic-gate mlistp = mlist + mlistp_off; 20627c478bd9Sstevel@tonic-gate } 20637c478bd9Sstevel@tonic-gate /* 20647c478bd9Sstevel@tonic-gate * now allocate memory for and copy the 20657c478bd9Sstevel@tonic-gate * magic file name string 20667c478bd9Sstevel@tonic-gate */ 20677c478bd9Sstevel@tonic-gate if ((*mlistp = malloc(strlen(magic_file) + 1)) == NULL) { 2068*9a411307Srie int err = errno; 2069*9a411307Srie (void) fprintf(stderr, gettext("%s: malloc failed: %s\n"), 2070*9a411307Srie File, strerror(err)); 20717c478bd9Sstevel@tonic-gate exit(2); 20727c478bd9Sstevel@tonic-gate } 20737c478bd9Sstevel@tonic-gate (void) strlcpy(*mlistp, magic_file, strlen(magic_file) + 1); 20747c478bd9Sstevel@tonic-gate mlistp++; 20757c478bd9Sstevel@tonic-gate 20767c478bd9Sstevel@tonic-gate if (first) { 20777c478bd9Sstevel@tonic-gate mlist1 = mlist; 20787c478bd9Sstevel@tonic-gate mlist1_sz = mlist_sz; 20797c478bd9Sstevel@tonic-gate mlist1p = mlistp; 20807c478bd9Sstevel@tonic-gate } else { 20817c478bd9Sstevel@tonic-gate mlist2 = mlist; 20827c478bd9Sstevel@tonic-gate mlist2_sz = mlist_sz; 20837c478bd9Sstevel@tonic-gate mlist2p = mlistp; 20847c478bd9Sstevel@tonic-gate } 20857c478bd9Sstevel@tonic-gate } 20867c478bd9Sstevel@tonic-gate 20877c478bd9Sstevel@tonic-gate static void 20887c478bd9Sstevel@tonic-gate fd_cleanup(void) 20897c478bd9Sstevel@tonic-gate { 20907c478bd9Sstevel@tonic-gate if (ifd != -1) { 20917c478bd9Sstevel@tonic-gate (void) close(ifd); 20927c478bd9Sstevel@tonic-gate ifd = -1; 20937c478bd9Sstevel@tonic-gate } 20947c478bd9Sstevel@tonic-gate if (elffd != -1) { 20957c478bd9Sstevel@tonic-gate (void) close(elffd); 20967c478bd9Sstevel@tonic-gate elffd = -1; 20977c478bd9Sstevel@tonic-gate } 20987c478bd9Sstevel@tonic-gate } 2099