10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*1882Sjohnlev * Common Development and Distribution License (the "License"). 6*1882Sjohnlev * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 22*1882Sjohnlev * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 270Sstevel@tonic-gate 280Sstevel@tonic-gate /* 290Sstevel@tonic-gate * Utility functions 300Sstevel@tonic-gate */ 310Sstevel@tonic-gate 320Sstevel@tonic-gate #include <stdio.h> 330Sstevel@tonic-gate #include <stdlib.h> 340Sstevel@tonic-gate #include <string.h> 350Sstevel@tonic-gate #include <libelf.h> 360Sstevel@tonic-gate #include <gelf.h> 370Sstevel@tonic-gate #include <errno.h> 380Sstevel@tonic-gate #include <stdarg.h> 390Sstevel@tonic-gate #include <pthread.h> 400Sstevel@tonic-gate #include <unistd.h> 410Sstevel@tonic-gate #include <sys/param.h> 420Sstevel@tonic-gate 430Sstevel@tonic-gate #include "ctftools.h" 440Sstevel@tonic-gate #include "memory.h" 450Sstevel@tonic-gate 460Sstevel@tonic-gate static void (*terminate_cleanup)() = NULL; 470Sstevel@tonic-gate 480Sstevel@tonic-gate /* returns 1 if s1 == s2, 0 otherwise */ 490Sstevel@tonic-gate int 500Sstevel@tonic-gate streq(char *s1, char *s2) 510Sstevel@tonic-gate { 520Sstevel@tonic-gate if (s1 == NULL) { 530Sstevel@tonic-gate if (s2 != NULL) 540Sstevel@tonic-gate return (0); 550Sstevel@tonic-gate } else if (s2 == NULL) 560Sstevel@tonic-gate return (0); 570Sstevel@tonic-gate else if (strcmp(s1, s2) != 0) 580Sstevel@tonic-gate return (0); 590Sstevel@tonic-gate 600Sstevel@tonic-gate return (1); 610Sstevel@tonic-gate } 620Sstevel@tonic-gate 630Sstevel@tonic-gate int 640Sstevel@tonic-gate findelfsecidx(Elf *elf, char *tofind) 650Sstevel@tonic-gate { 660Sstevel@tonic-gate Elf_Scn *scn = NULL; 670Sstevel@tonic-gate GElf_Ehdr ehdr; 680Sstevel@tonic-gate GElf_Shdr shdr; 690Sstevel@tonic-gate 700Sstevel@tonic-gate if (gelf_getehdr(elf, &ehdr) == NULL) { 710Sstevel@tonic-gate terminate("gelf_getehdr: %s\n", elf_errmsg(elf_errno())); 720Sstevel@tonic-gate } 730Sstevel@tonic-gate 740Sstevel@tonic-gate while ((scn = elf_nextscn(elf, scn)) != NULL) { 750Sstevel@tonic-gate char *name; 760Sstevel@tonic-gate 770Sstevel@tonic-gate if (gelf_getshdr(scn, &shdr) == NULL || 780Sstevel@tonic-gate (name = elf_strptr(elf, ehdr.e_shstrndx, 790Sstevel@tonic-gate (size_t)shdr.sh_name)) == NULL) { 800Sstevel@tonic-gate terminate("gelf_getehdr: %s\n", 810Sstevel@tonic-gate elf_errmsg(elf_errno())); 820Sstevel@tonic-gate } 830Sstevel@tonic-gate 840Sstevel@tonic-gate if (strcmp(name, tofind) == 0) 850Sstevel@tonic-gate return (elf_ndxscn(scn)); 860Sstevel@tonic-gate } 870Sstevel@tonic-gate 880Sstevel@tonic-gate return (-1); 890Sstevel@tonic-gate } 900Sstevel@tonic-gate 910Sstevel@tonic-gate /*PRINTFLIKE2*/ 920Sstevel@tonic-gate static void 930Sstevel@tonic-gate whine(char *type, char *format, va_list ap) 940Sstevel@tonic-gate { 950Sstevel@tonic-gate int error = errno; 960Sstevel@tonic-gate 970Sstevel@tonic-gate fprintf(stderr, "%s: %s: ", type, progname); 980Sstevel@tonic-gate vfprintf(stderr, format, ap); 990Sstevel@tonic-gate 1000Sstevel@tonic-gate if (format[strlen(format) - 1] != '\n') 1010Sstevel@tonic-gate fprintf(stderr, ": %s\n", strerror(error)); 1020Sstevel@tonic-gate } 1030Sstevel@tonic-gate 1040Sstevel@tonic-gate void 1050Sstevel@tonic-gate vaterminate(char *format, va_list ap) 1060Sstevel@tonic-gate { 1070Sstevel@tonic-gate whine("ERROR", format, ap); 1080Sstevel@tonic-gate } 1090Sstevel@tonic-gate 1100Sstevel@tonic-gate void 1110Sstevel@tonic-gate set_terminate_cleanup(void (*cleanup)()) 1120Sstevel@tonic-gate { 1130Sstevel@tonic-gate terminate_cleanup = cleanup; 1140Sstevel@tonic-gate } 1150Sstevel@tonic-gate 1160Sstevel@tonic-gate /*PRINTFLIKE1*/ 1170Sstevel@tonic-gate void 1180Sstevel@tonic-gate terminate(char *format, ...) 1190Sstevel@tonic-gate { 1200Sstevel@tonic-gate if (format) { 1210Sstevel@tonic-gate va_list ap; 1220Sstevel@tonic-gate 1230Sstevel@tonic-gate va_start(ap, format); 1240Sstevel@tonic-gate whine("ERROR", format, ap); 1250Sstevel@tonic-gate va_end(ap); 1260Sstevel@tonic-gate } 1270Sstevel@tonic-gate 1280Sstevel@tonic-gate if (terminate_cleanup) 1290Sstevel@tonic-gate terminate_cleanup(); 1300Sstevel@tonic-gate 1310Sstevel@tonic-gate exit(1); 1320Sstevel@tonic-gate } 1330Sstevel@tonic-gate 1340Sstevel@tonic-gate /*PRINTFLIKE1*/ 1350Sstevel@tonic-gate void 1360Sstevel@tonic-gate warning(char *format, ...) 1370Sstevel@tonic-gate { 1380Sstevel@tonic-gate va_list ap; 1390Sstevel@tonic-gate 1400Sstevel@tonic-gate va_start(ap, format); 1410Sstevel@tonic-gate whine("WARNING", format, ap); 1420Sstevel@tonic-gate va_end(ap); 1430Sstevel@tonic-gate 1440Sstevel@tonic-gate if (debug_level >= 3) 1450Sstevel@tonic-gate terminate("Termination due to warning\n"); 1460Sstevel@tonic-gate } 1470Sstevel@tonic-gate 1480Sstevel@tonic-gate /*PRINTFLIKE2*/ 1490Sstevel@tonic-gate void 1500Sstevel@tonic-gate vadebug(int level, char *format, va_list ap) 1510Sstevel@tonic-gate { 1520Sstevel@tonic-gate if (level > debug_level) 1530Sstevel@tonic-gate return; 1540Sstevel@tonic-gate 1550Sstevel@tonic-gate (void) fprintf(DEBUG_STREAM, "DEBUG: "); 1560Sstevel@tonic-gate (void) vfprintf(DEBUG_STREAM, format, ap); 1570Sstevel@tonic-gate fflush(DEBUG_STREAM); 1580Sstevel@tonic-gate } 1590Sstevel@tonic-gate 1600Sstevel@tonic-gate /*PRINTFLIKE2*/ 1610Sstevel@tonic-gate void 1620Sstevel@tonic-gate debug(int level, char *format, ...) 1630Sstevel@tonic-gate { 1640Sstevel@tonic-gate va_list ap; 1650Sstevel@tonic-gate 1660Sstevel@tonic-gate if (level > debug_level) 1670Sstevel@tonic-gate return; 1680Sstevel@tonic-gate 1690Sstevel@tonic-gate va_start(ap, format); 1700Sstevel@tonic-gate (void) vadebug(level, format, ap); 1710Sstevel@tonic-gate va_end(ap); 1720Sstevel@tonic-gate } 1730Sstevel@tonic-gate 1740Sstevel@tonic-gate char * 1750Sstevel@tonic-gate mktmpname(const char *origname, const char *suffix) 1760Sstevel@tonic-gate { 1770Sstevel@tonic-gate char *newname; 1780Sstevel@tonic-gate 179*1882Sjohnlev newname = xmalloc(strlen(origname) + strlen(suffix) + 1); 180*1882Sjohnlev (void) strcpy(newname, origname); 181*1882Sjohnlev (void) strcat(newname, suffix); 1820Sstevel@tonic-gate return (newname); 1830Sstevel@tonic-gate } 1840Sstevel@tonic-gate 1850Sstevel@tonic-gate /*PRINTFLIKE2*/ 1860Sstevel@tonic-gate void 1870Sstevel@tonic-gate elfterminate(const char *file, const char *fmt, ...) 1880Sstevel@tonic-gate { 1890Sstevel@tonic-gate static char msgbuf[BUFSIZ]; 1900Sstevel@tonic-gate va_list ap; 1910Sstevel@tonic-gate 1920Sstevel@tonic-gate va_start(ap, fmt); 1930Sstevel@tonic-gate vsnprintf(msgbuf, sizeof (msgbuf), fmt, ap); 1940Sstevel@tonic-gate va_end(ap); 1950Sstevel@tonic-gate 1960Sstevel@tonic-gate terminate("%s: %s: %s\n", file, msgbuf, elf_errmsg(elf_errno())); 1970Sstevel@tonic-gate } 198*1882Sjohnlev 199*1882Sjohnlev const char * 200*1882Sjohnlev tdesc_name(tdesc_t *tdp) 201*1882Sjohnlev { 202*1882Sjohnlev return (tdp->t_name == NULL ? "(anon)" : tdp->t_name); 203*1882Sjohnlev } 204