c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * CDDL HEADER START
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * The contents of this file are subject to the terms of the
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * Common Development and Distribution License (the "License").
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * You may not use this file except in compliance with the License.
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * See the License for the specific language governing permissions
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * and limitations under the License.
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * When distributing Covered Code, include this CDDL HEADER in each
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * If applicable, add the following below this CDDL HEADER, with the
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * fields enclosed by brackets "[]" replaced with your own identifying
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * information: Portions Copyright [yyyy] [name of copyright owner]
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * CDDL HEADER END
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny/* All Rights Reserved */
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny/* Copyright (c) 1987, 1988 Microsoft Corporation */
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny/* All Rights Reserved */
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * Use is subject to license terms.
0fe5e696eb581c66085be2cec9a0bf65978ba6d6ab * ELF files can exceed 2GB in size. A standard 32-bit program
0fe5e696eb581c66085be2cec9a0bf65978ba6d6ab * like 'file' cannot read past 2GB, and will be unable to see
0fe5e696eb581c66085be2cec9a0bf65978ba6d6ab * the ELF section headers that typically are at the end of the
0fe5e696eb581c66085be2cec9a0bf65978ba6d6ab * object. The simplest solution to this problem would be to make
0fe5e696eb581c66085be2cec9a0bf65978ba6d6ab * the 'file' command a 64-bit application. However, as a matter of
0fe5e696eb581c66085be2cec9a0bf65978ba6d6ab * policy, we do not want to require this. A simple command like
0fe5e696eb581c66085be2cec9a0bf65978ba6d6ab * 'file' should not carry such a requirement, especially as we
0fe5e696eb581c66085be2cec9a0bf65978ba6d6ab * support 32-bit only hardware.
0fe5e696eb581c66085be2cec9a0bf65978ba6d6ab * An alternative solution is to build this code as 32-bit
0fe5e696eb581c66085be2cec9a0bf65978ba6d6ab * large file aware. The usual way to do this is to define a pair
0fe5e696eb581c66085be2cec9a0bf65978ba6d6ab * of preprocessor definitions:
0fe5e696eb581c66085be2cec9a0bf65978ba6d6ab * _LARGEFILE64_SOURCE
0fe5e696eb581c66085be2cec9a0bf65978ba6d6ab * Map standard I/O routines to their largefile aware versions.
0fe5e696eb581c66085be2cec9a0bf65978ba6d6ab * _FILE_OFFSET_BITS=64
0fe5e696eb581c66085be2cec9a0bf65978ba6d6ab * Map off_t to off64_t
0fe5e696eb581c66085be2cec9a0bf65978ba6d6ab * The problem with this solution is that libelf is not large file capable,
0fe5e696eb581c66085be2cec9a0bf65978ba6d6ab * and the libelf header file will prevent compilation if
0fe5e696eb581c66085be2cec9a0bf65978ba6d6ab * _FILE_OFFSET_BITS is set to 64.
0fe5e696eb581c66085be2cec9a0bf65978ba6d6ab * So, the solution used in this code is to define _LARGEFILE64_SOURCE
0fe5e696eb581c66085be2cec9a0bf65978ba6d6ab * to get access to the 64-bit APIs, not to define _FILE_OFFSET_BITS, and to
0fe5e696eb581c66085be2cec9a0bf65978ba6d6ab * use our own types in place of off_t, and size_t. We read all the file
0fe5e696eb581c66085be2cec9a0bf65978ba6d6ab * data directly using pread64(), and avoid the use of libelf for anything
0fe5e696eb581c66085be2cec9a0bf65978ba6d6ab * other than the xlate functionality.
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6nyextern const char *File;
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6nystatic int get_class(void);
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6nystatic int get_version(void);
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6nystatic int get_format(void);
97cca09008b0f5f24287c17660e644b0294c9ba2abstatic Elf_Word EI_Ehdr_shstrndx; /* Index of section hdr string table */
97cca09008b0f5f24287c17660e644b0294c9ba2abstatic Elf_Shdr EI_Shdr; /* recent Elf_Shdr to be stored */
97cca09008b0f5f24287c17660e644b0294c9ba2abstatic Elf_Phdr EI_Phdr; /* recent Elf_Phdr to be stored */
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny /* do as what libelf:_elf_config() does */
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * file_xlatetom: translate different headers from file
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * representation to memory representaion.
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny /* will convert only these types */
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * xlatetom_nhdr: There is no routine to convert Note header
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * so we convert each field of this header.
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny return (r);
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * elf_read: reads elf header, program, section headers to
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * collect all information needed for file(1)
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * output and stores them in Elf_Info.
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny if (file_xlatetom(ELF_T_EHDR, (char *)ehdr) == ELF_READ_FAIL)
97cca09008b0f5f24287c17660e644b0294c9ba2ab * Extended section or program indexes in use? If so, special
97cca09008b0f5f24287c17660e644b0294c9ba2ab * values in the ELF header redirect us to get the real values
97cca09008b0f5f24287c17660e644b0294c9ba2ab * from shdr[0].
97cca09008b0f5f24287c17660e644b0294c9ba2ab if (((EI_Ehdr_shnum == 0) || (EI_Ehdr_phnum == PN_XNUM)) &&
97cca09008b0f5f24287c17660e644b0294c9ba2ab if ((EI_Ehdr_phnum == PN_XNUM) && (EI_Shdr.sh_info != 0))
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny if (ret == 0) {
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny /* We don't need section info for core files */
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * get_phdr: reads program header of specified index.
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny if (pread64(EI->elffd, (void *)&EI_Phdr, size, off) != size)
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny if (file_xlatetom(ELF_T_PHDR, (char *)&EI_Phdr) == ELF_READ_FAIL)
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * get_shdr: reads section header of specified index.
97cca09008b0f5f24287c17660e644b0294c9ba2ab * Prevent access to non-existent section headers.
97cca09008b0f5f24287c17660e644b0294c9ba2ab * A value of 0 for e_shoff means that there is no section header
97cca09008b0f5f24287c17660e644b0294c9ba2ab * array in the file. A value of 0 for e_shndx does not necessarily
97cca09008b0f5f24287c17660e644b0294c9ba2ab * mean this - there can still be a 1-element section header array
97cca09008b0f5f24287c17660e644b0294c9ba2ab * to support extended section or program header indexes that
97cca09008b0f5f24287c17660e644b0294c9ba2ab * exceed the 16-bit fields used in the ELF header to represent them.
97cca09008b0f5f24287c17660e644b0294c9ba2ab if ((EI_Ehdr.e_shoff == 0) || ((inx > 0) && (inx >= EI_Ehdr_shnum)))
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny if (pread64(EI->elffd, (void *)&EI_Shdr, size, off) != size)
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny if (file_xlatetom(ELF_T_SHDR, (char *)&EI_Shdr) == ELF_READ_FAIL)
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * process_phdr: Read Program Headers and see if it is a core
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * file of either new or (pre-restructured /proc)
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * type, read the name of the file that dumped this
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * core, else see if this is a dynamically linked.
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny /* read the note if it is a core */
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * If the next segment is also a note, use it instead.
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny /* read the first phdr back */
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny /* Translate the ELF note header */
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * We want to print the string contained
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * in psinfo->pr_fname[], where 'psinfo'
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * is either an old NT_PRPSINFO structure
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * or a new NT_PSINFO structure.
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * Old core files have only type NT_PRPSINFO.
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * New core files have type NT_PSINFO.
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * These structures are also different by
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * virtue of being contained in a core file
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * of either 32-bit or 64-bit type.
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * To further complicate matters, we ourself
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * might be compiled either 32-bit or 64-bit.
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * For these reason, we just *know* the offsets of
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * pr_fname[] into the four different structures
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * here, regardless of how we are compiled.
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny /* 32-bit core file, 32-bit structures */
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny else /* old: NT_PRPSINFO */
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny /* 64-bit core file, 64-bit structures */
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny else /* old: NT_PRPSINFO */
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * process_shdr: Read Section Headers to attempt to get HW/SW
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * capabilities by looking at the SUNW_cap
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * section and set string in Elf_Info.
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * Also look for symbol tables and debug
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * information sections. Set the "stripped" field
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * in Elf_Info with corresponding flags.
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny /* if there are no sections, return success anyway */
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny /* read section names from String Section */
91600d919baafe4e4d8bdee4168878036351c556Richard Lowe if (pread64(EI->elffd, strtab, shdr->sh_size, shdr->sh_offset)
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny /* read all the sections and process them */
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny for (j = 0; j < capn; j++) {
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * read cap and xlate the values
3d6a6d03a2b1d567b03fb49347aee6b5223261cbRichard Lowe * Each capatibility group is terminated with
3d6a6d03a2b1d567b03fb49347aee6b5223261cbRichard Lowe * CA_SUNW_NULL. Groups other than the first
3d6a6d03a2b1d567b03fb49347aee6b5223261cbRichard Lowe * represent symbol capabilities, and aren't
3d6a6d03a2b1d567b03fb49347aee6b5223261cbRichard Lowe * interesting here.
3d6a6d03a2b1d567b03fb49347aee6b5223261cbRichard Lowe if ((*EI->cap_str != '\0') && (*capstr != '\0'))
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * Definition time:
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * - "not stripped" means that an executable file
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * contains a Symbol Table (.symtab)
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * - "stripped" means that an executable file
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * does not contain a Symbol Table.
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * When strip -l or strip -x is run, it strips the
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * debugging information (.line section name (strip -l),
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * .line, .debug*, .stabs*, .dwarf* section names
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * and SHT_SUNW_DEBUGSTR and SHT_SUNW_DEBUG
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * section types (strip -x), however the Symbol
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * Table will still be present.
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * Therefore, if
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * - No Symbol Table present, then report
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * "stripped"
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * - Symbol Table present with debugging
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * information (line number or debug section names,
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * or SHT_SUNW_DEBUGSTR or SHT_SUNW_DEBUG section
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * types) then report:
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * "not stripped"
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * - Symbol Table present with no debugging
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * information (line number or debug section names,
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * or SHT_SUNW_DEBUGSTR or SHT_SUNW_DEBUG section
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * types) then report:
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * "not stripped, no debugging information
c2c65e21c1fd025c3fc6103cc713dd5343fc9ad6ny * available"