1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate /* 30*7c478bd9Sstevel@tonic-gate * Utility functions 31*7c478bd9Sstevel@tonic-gate */ 32*7c478bd9Sstevel@tonic-gate 33*7c478bd9Sstevel@tonic-gate #include <stdio.h> 34*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 35*7c478bd9Sstevel@tonic-gate #include <string.h> 36*7c478bd9Sstevel@tonic-gate #include <libelf.h> 37*7c478bd9Sstevel@tonic-gate #include <gelf.h> 38*7c478bd9Sstevel@tonic-gate #include <errno.h> 39*7c478bd9Sstevel@tonic-gate #include <stdarg.h> 40*7c478bd9Sstevel@tonic-gate #include <pthread.h> 41*7c478bd9Sstevel@tonic-gate #include <unistd.h> 42*7c478bd9Sstevel@tonic-gate #include <sys/param.h> 43*7c478bd9Sstevel@tonic-gate 44*7c478bd9Sstevel@tonic-gate #include "ctftools.h" 45*7c478bd9Sstevel@tonic-gate #include "memory.h" 46*7c478bd9Sstevel@tonic-gate 47*7c478bd9Sstevel@tonic-gate static void (*terminate_cleanup)() = NULL; 48*7c478bd9Sstevel@tonic-gate 49*7c478bd9Sstevel@tonic-gate /* returns 1 if s1 == s2, 0 otherwise */ 50*7c478bd9Sstevel@tonic-gate int 51*7c478bd9Sstevel@tonic-gate streq(char *s1, char *s2) 52*7c478bd9Sstevel@tonic-gate { 53*7c478bd9Sstevel@tonic-gate if (s1 == NULL) { 54*7c478bd9Sstevel@tonic-gate if (s2 != NULL) 55*7c478bd9Sstevel@tonic-gate return (0); 56*7c478bd9Sstevel@tonic-gate } else if (s2 == NULL) 57*7c478bd9Sstevel@tonic-gate return (0); 58*7c478bd9Sstevel@tonic-gate else if (strcmp(s1, s2) != 0) 59*7c478bd9Sstevel@tonic-gate return (0); 60*7c478bd9Sstevel@tonic-gate 61*7c478bd9Sstevel@tonic-gate return (1); 62*7c478bd9Sstevel@tonic-gate } 63*7c478bd9Sstevel@tonic-gate 64*7c478bd9Sstevel@tonic-gate int 65*7c478bd9Sstevel@tonic-gate findelfsecidx(Elf *elf, char *tofind) 66*7c478bd9Sstevel@tonic-gate { 67*7c478bd9Sstevel@tonic-gate Elf_Scn *scn = NULL; 68*7c478bd9Sstevel@tonic-gate GElf_Ehdr ehdr; 69*7c478bd9Sstevel@tonic-gate GElf_Shdr shdr; 70*7c478bd9Sstevel@tonic-gate 71*7c478bd9Sstevel@tonic-gate if (gelf_getehdr(elf, &ehdr) == NULL) { 72*7c478bd9Sstevel@tonic-gate terminate("gelf_getehdr: %s\n", elf_errmsg(elf_errno())); 73*7c478bd9Sstevel@tonic-gate } 74*7c478bd9Sstevel@tonic-gate 75*7c478bd9Sstevel@tonic-gate while ((scn = elf_nextscn(elf, scn)) != NULL) { 76*7c478bd9Sstevel@tonic-gate char *name; 77*7c478bd9Sstevel@tonic-gate 78*7c478bd9Sstevel@tonic-gate if (gelf_getshdr(scn, &shdr) == NULL || 79*7c478bd9Sstevel@tonic-gate (name = elf_strptr(elf, ehdr.e_shstrndx, 80*7c478bd9Sstevel@tonic-gate (size_t)shdr.sh_name)) == NULL) { 81*7c478bd9Sstevel@tonic-gate terminate("gelf_getehdr: %s\n", 82*7c478bd9Sstevel@tonic-gate elf_errmsg(elf_errno())); 83*7c478bd9Sstevel@tonic-gate } 84*7c478bd9Sstevel@tonic-gate 85*7c478bd9Sstevel@tonic-gate if (strcmp(name, tofind) == 0) 86*7c478bd9Sstevel@tonic-gate return (elf_ndxscn(scn)); 87*7c478bd9Sstevel@tonic-gate } 88*7c478bd9Sstevel@tonic-gate 89*7c478bd9Sstevel@tonic-gate return (-1); 90*7c478bd9Sstevel@tonic-gate } 91*7c478bd9Sstevel@tonic-gate 92*7c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/ 93*7c478bd9Sstevel@tonic-gate static void 94*7c478bd9Sstevel@tonic-gate whine(char *type, char *format, va_list ap) 95*7c478bd9Sstevel@tonic-gate { 96*7c478bd9Sstevel@tonic-gate int error = errno; 97*7c478bd9Sstevel@tonic-gate 98*7c478bd9Sstevel@tonic-gate fprintf(stderr, "%s: %s: ", type, progname); 99*7c478bd9Sstevel@tonic-gate vfprintf(stderr, format, ap); 100*7c478bd9Sstevel@tonic-gate 101*7c478bd9Sstevel@tonic-gate if (format[strlen(format) - 1] != '\n') 102*7c478bd9Sstevel@tonic-gate fprintf(stderr, ": %s\n", strerror(error)); 103*7c478bd9Sstevel@tonic-gate } 104*7c478bd9Sstevel@tonic-gate 105*7c478bd9Sstevel@tonic-gate void 106*7c478bd9Sstevel@tonic-gate vaterminate(char *format, va_list ap) 107*7c478bd9Sstevel@tonic-gate { 108*7c478bd9Sstevel@tonic-gate whine("ERROR", format, ap); 109*7c478bd9Sstevel@tonic-gate } 110*7c478bd9Sstevel@tonic-gate 111*7c478bd9Sstevel@tonic-gate void 112*7c478bd9Sstevel@tonic-gate set_terminate_cleanup(void (*cleanup)()) 113*7c478bd9Sstevel@tonic-gate { 114*7c478bd9Sstevel@tonic-gate terminate_cleanup = cleanup; 115*7c478bd9Sstevel@tonic-gate } 116*7c478bd9Sstevel@tonic-gate 117*7c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/ 118*7c478bd9Sstevel@tonic-gate void 119*7c478bd9Sstevel@tonic-gate terminate(char *format, ...) 120*7c478bd9Sstevel@tonic-gate { 121*7c478bd9Sstevel@tonic-gate if (format) { 122*7c478bd9Sstevel@tonic-gate va_list ap; 123*7c478bd9Sstevel@tonic-gate 124*7c478bd9Sstevel@tonic-gate va_start(ap, format); 125*7c478bd9Sstevel@tonic-gate whine("ERROR", format, ap); 126*7c478bd9Sstevel@tonic-gate va_end(ap); 127*7c478bd9Sstevel@tonic-gate } 128*7c478bd9Sstevel@tonic-gate 129*7c478bd9Sstevel@tonic-gate if (terminate_cleanup) 130*7c478bd9Sstevel@tonic-gate terminate_cleanup(); 131*7c478bd9Sstevel@tonic-gate 132*7c478bd9Sstevel@tonic-gate exit(1); 133*7c478bd9Sstevel@tonic-gate } 134*7c478bd9Sstevel@tonic-gate 135*7c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/ 136*7c478bd9Sstevel@tonic-gate void 137*7c478bd9Sstevel@tonic-gate warning(char *format, ...) 138*7c478bd9Sstevel@tonic-gate { 139*7c478bd9Sstevel@tonic-gate va_list ap; 140*7c478bd9Sstevel@tonic-gate 141*7c478bd9Sstevel@tonic-gate va_start(ap, format); 142*7c478bd9Sstevel@tonic-gate whine("WARNING", format, ap); 143*7c478bd9Sstevel@tonic-gate va_end(ap); 144*7c478bd9Sstevel@tonic-gate 145*7c478bd9Sstevel@tonic-gate if (debug_level >= 3) 146*7c478bd9Sstevel@tonic-gate terminate("Termination due to warning\n"); 147*7c478bd9Sstevel@tonic-gate } 148*7c478bd9Sstevel@tonic-gate 149*7c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/ 150*7c478bd9Sstevel@tonic-gate void 151*7c478bd9Sstevel@tonic-gate vadebug(int level, char *format, va_list ap) 152*7c478bd9Sstevel@tonic-gate { 153*7c478bd9Sstevel@tonic-gate if (level > debug_level) 154*7c478bd9Sstevel@tonic-gate return; 155*7c478bd9Sstevel@tonic-gate 156*7c478bd9Sstevel@tonic-gate (void) fprintf(DEBUG_STREAM, "DEBUG: "); 157*7c478bd9Sstevel@tonic-gate (void) vfprintf(DEBUG_STREAM, format, ap); 158*7c478bd9Sstevel@tonic-gate fflush(DEBUG_STREAM); 159*7c478bd9Sstevel@tonic-gate } 160*7c478bd9Sstevel@tonic-gate 161*7c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/ 162*7c478bd9Sstevel@tonic-gate void 163*7c478bd9Sstevel@tonic-gate debug(int level, char *format, ...) 164*7c478bd9Sstevel@tonic-gate { 165*7c478bd9Sstevel@tonic-gate va_list ap; 166*7c478bd9Sstevel@tonic-gate 167*7c478bd9Sstevel@tonic-gate if (level > debug_level) 168*7c478bd9Sstevel@tonic-gate return; 169*7c478bd9Sstevel@tonic-gate 170*7c478bd9Sstevel@tonic-gate va_start(ap, format); 171*7c478bd9Sstevel@tonic-gate (void) vadebug(level, format, ap); 172*7c478bd9Sstevel@tonic-gate va_end(ap); 173*7c478bd9Sstevel@tonic-gate } 174*7c478bd9Sstevel@tonic-gate 175*7c478bd9Sstevel@tonic-gate char * 176*7c478bd9Sstevel@tonic-gate mktmpname(const char *origname, const char *suffix) 177*7c478bd9Sstevel@tonic-gate { 178*7c478bd9Sstevel@tonic-gate const char *dot; 179*7c478bd9Sstevel@tonic-gate char *newname; 180*7c478bd9Sstevel@tonic-gate size_t newlen; 181*7c478bd9Sstevel@tonic-gate 182*7c478bd9Sstevel@tonic-gate if (!(dot = strrchr(origname, '.'))) 183*7c478bd9Sstevel@tonic-gate dot = origname + strlen(origname); 184*7c478bd9Sstevel@tonic-gate 185*7c478bd9Sstevel@tonic-gate newlen = dot - origname + strlen(suffix) + 1; 186*7c478bd9Sstevel@tonic-gate newname = xmalloc(newlen); 187*7c478bd9Sstevel@tonic-gate snprintf(newname, newlen, "%*.*s%s", dot - origname, dot - origname, 188*7c478bd9Sstevel@tonic-gate origname, suffix); 189*7c478bd9Sstevel@tonic-gate 190*7c478bd9Sstevel@tonic-gate return (newname); 191*7c478bd9Sstevel@tonic-gate } 192*7c478bd9Sstevel@tonic-gate 193*7c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/ 194*7c478bd9Sstevel@tonic-gate void 195*7c478bd9Sstevel@tonic-gate elfterminate(const char *file, const char *fmt, ...) 196*7c478bd9Sstevel@tonic-gate { 197*7c478bd9Sstevel@tonic-gate static char msgbuf[BUFSIZ]; 198*7c478bd9Sstevel@tonic-gate va_list ap; 199*7c478bd9Sstevel@tonic-gate 200*7c478bd9Sstevel@tonic-gate va_start(ap, fmt); 201*7c478bd9Sstevel@tonic-gate vsnprintf(msgbuf, sizeof (msgbuf), fmt, ap); 202*7c478bd9Sstevel@tonic-gate va_end(ap); 203*7c478bd9Sstevel@tonic-gate 204*7c478bd9Sstevel@tonic-gate terminate("%s: %s: %s\n", file, msgbuf, elf_errmsg(elf_errno())); 205*7c478bd9Sstevel@tonic-gate } 206