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