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 54d232658Sjohnlev * Common Development and Distribution License (the "License"). 64d232658Sjohnlev * 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 /* 224d232658Sjohnlev * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate /* 297c478bd9Sstevel@tonic-gate * Utility functions 307c478bd9Sstevel@tonic-gate */ 317c478bd9Sstevel@tonic-gate 327c478bd9Sstevel@tonic-gate #include <stdio.h> 337c478bd9Sstevel@tonic-gate #include <stdlib.h> 347c478bd9Sstevel@tonic-gate #include <string.h> 357c478bd9Sstevel@tonic-gate #include <libelf.h> 367c478bd9Sstevel@tonic-gate #include <gelf.h> 377c478bd9Sstevel@tonic-gate #include <errno.h> 387c478bd9Sstevel@tonic-gate #include <stdarg.h> 397c478bd9Sstevel@tonic-gate #include <pthread.h> 407c478bd9Sstevel@tonic-gate #include <unistd.h> 417c478bd9Sstevel@tonic-gate #include <sys/param.h> 427c478bd9Sstevel@tonic-gate 437c478bd9Sstevel@tonic-gate #include "ctftools.h" 447c478bd9Sstevel@tonic-gate #include "memory.h" 457c478bd9Sstevel@tonic-gate 467c478bd9Sstevel@tonic-gate static void (*terminate_cleanup)() = NULL; 477c478bd9Sstevel@tonic-gate 487c478bd9Sstevel@tonic-gate /* returns 1 if s1 == s2, 0 otherwise */ 497c478bd9Sstevel@tonic-gate int 50*e824d57fSjohnlev streq(const char *s1, const char *s2) 517c478bd9Sstevel@tonic-gate { 527c478bd9Sstevel@tonic-gate if (s1 == NULL) { 537c478bd9Sstevel@tonic-gate if (s2 != NULL) 547c478bd9Sstevel@tonic-gate return (0); 557c478bd9Sstevel@tonic-gate } else if (s2 == NULL) 567c478bd9Sstevel@tonic-gate return (0); 577c478bd9Sstevel@tonic-gate else if (strcmp(s1, s2) != 0) 587c478bd9Sstevel@tonic-gate return (0); 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate return (1); 617c478bd9Sstevel@tonic-gate } 627c478bd9Sstevel@tonic-gate 637c478bd9Sstevel@tonic-gate int 64c168da27Sjohnlev findelfsecidx(Elf *elf, const char *file, const char *tofind) 657c478bd9Sstevel@tonic-gate { 667c478bd9Sstevel@tonic-gate Elf_Scn *scn = NULL; 677c478bd9Sstevel@tonic-gate GElf_Ehdr ehdr; 687c478bd9Sstevel@tonic-gate GElf_Shdr shdr; 697c478bd9Sstevel@tonic-gate 70c168da27Sjohnlev if (gelf_getehdr(elf, &ehdr) == NULL) 71c168da27Sjohnlev elfterminate(file, "Couldn't read ehdr"); 727c478bd9Sstevel@tonic-gate 737c478bd9Sstevel@tonic-gate while ((scn = elf_nextscn(elf, scn)) != NULL) { 747c478bd9Sstevel@tonic-gate char *name; 757c478bd9Sstevel@tonic-gate 76c168da27Sjohnlev if (gelf_getshdr(scn, &shdr) == NULL) { 77c168da27Sjohnlev elfterminate(file, 78c168da27Sjohnlev "Couldn't read header for section %d", 79c168da27Sjohnlev elf_ndxscn(scn)); 80c168da27Sjohnlev } 81c168da27Sjohnlev 82c168da27Sjohnlev if ((name = elf_strptr(elf, ehdr.e_shstrndx, 837c478bd9Sstevel@tonic-gate (size_t)shdr.sh_name)) == NULL) { 84c168da27Sjohnlev elfterminate(file, 85c168da27Sjohnlev "Couldn't get name for section %d", 86c168da27Sjohnlev elf_ndxscn(scn)); 877c478bd9Sstevel@tonic-gate } 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate if (strcmp(name, tofind) == 0) 907c478bd9Sstevel@tonic-gate return (elf_ndxscn(scn)); 917c478bd9Sstevel@tonic-gate } 927c478bd9Sstevel@tonic-gate 937c478bd9Sstevel@tonic-gate return (-1); 947c478bd9Sstevel@tonic-gate } 957c478bd9Sstevel@tonic-gate 96*e824d57fSjohnlev size_t 97*e824d57fSjohnlev elf_ptrsz(Elf *elf) 98*e824d57fSjohnlev { 99*e824d57fSjohnlev GElf_Ehdr ehdr; 100*e824d57fSjohnlev 101*e824d57fSjohnlev if (gelf_getehdr(elf, &ehdr) == NULL) { 102*e824d57fSjohnlev terminate("failed to read ELF header: %s\n", 103*e824d57fSjohnlev elf_errmsg(-1)); 104*e824d57fSjohnlev } 105*e824d57fSjohnlev 106*e824d57fSjohnlev if (ehdr.e_ident[EI_CLASS] == ELFCLASS32) 107*e824d57fSjohnlev return (4); 108*e824d57fSjohnlev else if (ehdr.e_ident[EI_CLASS] == ELFCLASS64) 109*e824d57fSjohnlev return (8); 110*e824d57fSjohnlev else 111*e824d57fSjohnlev terminate("unknown ELF class %d\n", ehdr.e_ident[EI_CLASS]); 112*e824d57fSjohnlev 113*e824d57fSjohnlev /*NOTREACHED*/ 114*e824d57fSjohnlev return (0); 115*e824d57fSjohnlev } 116*e824d57fSjohnlev 1177c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/ 1187c478bd9Sstevel@tonic-gate static void 1197c478bd9Sstevel@tonic-gate whine(char *type, char *format, va_list ap) 1207c478bd9Sstevel@tonic-gate { 1217c478bd9Sstevel@tonic-gate int error = errno; 1227c478bd9Sstevel@tonic-gate 1237c478bd9Sstevel@tonic-gate fprintf(stderr, "%s: %s: ", type, progname); 1247c478bd9Sstevel@tonic-gate vfprintf(stderr, format, ap); 1257c478bd9Sstevel@tonic-gate 1267c478bd9Sstevel@tonic-gate if (format[strlen(format) - 1] != '\n') 1277c478bd9Sstevel@tonic-gate fprintf(stderr, ": %s\n", strerror(error)); 1287c478bd9Sstevel@tonic-gate } 1297c478bd9Sstevel@tonic-gate 1307c478bd9Sstevel@tonic-gate void 1317c478bd9Sstevel@tonic-gate set_terminate_cleanup(void (*cleanup)()) 1327c478bd9Sstevel@tonic-gate { 1337c478bd9Sstevel@tonic-gate terminate_cleanup = cleanup; 1347c478bd9Sstevel@tonic-gate } 1357c478bd9Sstevel@tonic-gate 1367c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/ 1377c478bd9Sstevel@tonic-gate void 1387c478bd9Sstevel@tonic-gate terminate(char *format, ...) 1397c478bd9Sstevel@tonic-gate { 1407c478bd9Sstevel@tonic-gate va_list ap; 1417c478bd9Sstevel@tonic-gate 1427c478bd9Sstevel@tonic-gate va_start(ap, format); 1437c478bd9Sstevel@tonic-gate whine("ERROR", format, ap); 1447c478bd9Sstevel@tonic-gate va_end(ap); 1457c478bd9Sstevel@tonic-gate 1467c478bd9Sstevel@tonic-gate if (terminate_cleanup) 1477c478bd9Sstevel@tonic-gate terminate_cleanup(); 1487c478bd9Sstevel@tonic-gate 149c168da27Sjohnlev if (getenv("CTF_ABORT_ON_TERMINATE") != NULL) 150c168da27Sjohnlev abort(); 1517c478bd9Sstevel@tonic-gate exit(1); 1527c478bd9Sstevel@tonic-gate } 1537c478bd9Sstevel@tonic-gate 1547c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/ 1557c478bd9Sstevel@tonic-gate void 156c168da27Sjohnlev aborterr(char *format, ...) 157c168da27Sjohnlev { 158c168da27Sjohnlev va_list ap; 159c168da27Sjohnlev 160c168da27Sjohnlev va_start(ap, format); 161c168da27Sjohnlev whine("ERROR", format, ap); 162c168da27Sjohnlev va_end(ap); 163c168da27Sjohnlev 164c168da27Sjohnlev abort(); 165c168da27Sjohnlev } 166c168da27Sjohnlev 167c168da27Sjohnlev /*PRINTFLIKE1*/ 168c168da27Sjohnlev void 1697c478bd9Sstevel@tonic-gate warning(char *format, ...) 1707c478bd9Sstevel@tonic-gate { 1717c478bd9Sstevel@tonic-gate va_list ap; 1727c478bd9Sstevel@tonic-gate 1737c478bd9Sstevel@tonic-gate va_start(ap, format); 1747c478bd9Sstevel@tonic-gate whine("WARNING", format, ap); 1757c478bd9Sstevel@tonic-gate va_end(ap); 1767c478bd9Sstevel@tonic-gate 1777c478bd9Sstevel@tonic-gate if (debug_level >= 3) 1787c478bd9Sstevel@tonic-gate terminate("Termination due to warning\n"); 1797c478bd9Sstevel@tonic-gate } 1807c478bd9Sstevel@tonic-gate 1817c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/ 1827c478bd9Sstevel@tonic-gate void 1837c478bd9Sstevel@tonic-gate vadebug(int level, char *format, va_list ap) 1847c478bd9Sstevel@tonic-gate { 1857c478bd9Sstevel@tonic-gate if (level > debug_level) 1867c478bd9Sstevel@tonic-gate return; 1877c478bd9Sstevel@tonic-gate 1887c478bd9Sstevel@tonic-gate (void) fprintf(DEBUG_STREAM, "DEBUG: "); 1897c478bd9Sstevel@tonic-gate (void) vfprintf(DEBUG_STREAM, format, ap); 1907c478bd9Sstevel@tonic-gate fflush(DEBUG_STREAM); 1917c478bd9Sstevel@tonic-gate } 1927c478bd9Sstevel@tonic-gate 1937c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/ 1947c478bd9Sstevel@tonic-gate void 1957c478bd9Sstevel@tonic-gate debug(int level, char *format, ...) 1967c478bd9Sstevel@tonic-gate { 1977c478bd9Sstevel@tonic-gate va_list ap; 1987c478bd9Sstevel@tonic-gate 1997c478bd9Sstevel@tonic-gate if (level > debug_level) 2007c478bd9Sstevel@tonic-gate return; 2017c478bd9Sstevel@tonic-gate 2027c478bd9Sstevel@tonic-gate va_start(ap, format); 2037c478bd9Sstevel@tonic-gate (void) vadebug(level, format, ap); 2047c478bd9Sstevel@tonic-gate va_end(ap); 2057c478bd9Sstevel@tonic-gate } 2067c478bd9Sstevel@tonic-gate 2077c478bd9Sstevel@tonic-gate char * 2087c478bd9Sstevel@tonic-gate mktmpname(const char *origname, const char *suffix) 2097c478bd9Sstevel@tonic-gate { 2107c478bd9Sstevel@tonic-gate char *newname; 2117c478bd9Sstevel@tonic-gate 2124d232658Sjohnlev newname = xmalloc(strlen(origname) + strlen(suffix) + 1); 2134d232658Sjohnlev (void) strcpy(newname, origname); 2144d232658Sjohnlev (void) strcat(newname, suffix); 2157c478bd9Sstevel@tonic-gate return (newname); 2167c478bd9Sstevel@tonic-gate } 2177c478bd9Sstevel@tonic-gate 2187c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/ 2197c478bd9Sstevel@tonic-gate void 2207c478bd9Sstevel@tonic-gate elfterminate(const char *file, const char *fmt, ...) 2217c478bd9Sstevel@tonic-gate { 2227c478bd9Sstevel@tonic-gate static char msgbuf[BUFSIZ]; 2237c478bd9Sstevel@tonic-gate va_list ap; 2247c478bd9Sstevel@tonic-gate 2257c478bd9Sstevel@tonic-gate va_start(ap, fmt); 2267c478bd9Sstevel@tonic-gate vsnprintf(msgbuf, sizeof (msgbuf), fmt, ap); 2277c478bd9Sstevel@tonic-gate va_end(ap); 2287c478bd9Sstevel@tonic-gate 229c168da27Sjohnlev terminate("%s: %s: %s\n", file, msgbuf, elf_errmsg(-1)); 2307c478bd9Sstevel@tonic-gate } 2314d232658Sjohnlev 2324d232658Sjohnlev const char * 2334d232658Sjohnlev tdesc_name(tdesc_t *tdp) 2344d232658Sjohnlev { 2354d232658Sjohnlev return (tdp->t_name == NULL ? "(anon)" : tdp->t_name); 2364d232658Sjohnlev } 237