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