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