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 507c478bd9Sstevel@tonic-gate streq(char *s1, 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 64*c168da27Sjohnlev 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 70*c168da27Sjohnlev if (gelf_getehdr(elf, &ehdr) == NULL) 71*c168da27Sjohnlev 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 76*c168da27Sjohnlev if (gelf_getshdr(scn, &shdr) == NULL) { 77*c168da27Sjohnlev elfterminate(file, 78*c168da27Sjohnlev "Couldn't read header for section %d", 79*c168da27Sjohnlev elf_ndxscn(scn)); 80*c168da27Sjohnlev } 81*c168da27Sjohnlev 82*c168da27Sjohnlev if ((name = elf_strptr(elf, ehdr.e_shstrndx, 837c478bd9Sstevel@tonic-gate (size_t)shdr.sh_name)) == NULL) { 84*c168da27Sjohnlev elfterminate(file, 85*c168da27Sjohnlev "Couldn't get name for section %d", 86*c168da27Sjohnlev 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 967c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/ 977c478bd9Sstevel@tonic-gate static void 987c478bd9Sstevel@tonic-gate whine(char *type, char *format, va_list ap) 997c478bd9Sstevel@tonic-gate { 1007c478bd9Sstevel@tonic-gate int error = errno; 1017c478bd9Sstevel@tonic-gate 1027c478bd9Sstevel@tonic-gate fprintf(stderr, "%s: %s: ", type, progname); 1037c478bd9Sstevel@tonic-gate vfprintf(stderr, format, ap); 1047c478bd9Sstevel@tonic-gate 1057c478bd9Sstevel@tonic-gate if (format[strlen(format) - 1] != '\n') 1067c478bd9Sstevel@tonic-gate fprintf(stderr, ": %s\n", strerror(error)); 1077c478bd9Sstevel@tonic-gate } 1087c478bd9Sstevel@tonic-gate 1097c478bd9Sstevel@tonic-gate void 1107c478bd9Sstevel@tonic-gate set_terminate_cleanup(void (*cleanup)()) 1117c478bd9Sstevel@tonic-gate { 1127c478bd9Sstevel@tonic-gate terminate_cleanup = cleanup; 1137c478bd9Sstevel@tonic-gate } 1147c478bd9Sstevel@tonic-gate 1157c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/ 1167c478bd9Sstevel@tonic-gate void 1177c478bd9Sstevel@tonic-gate terminate(char *format, ...) 1187c478bd9Sstevel@tonic-gate { 1197c478bd9Sstevel@tonic-gate va_list ap; 1207c478bd9Sstevel@tonic-gate 1217c478bd9Sstevel@tonic-gate va_start(ap, format); 1227c478bd9Sstevel@tonic-gate whine("ERROR", format, ap); 1237c478bd9Sstevel@tonic-gate va_end(ap); 1247c478bd9Sstevel@tonic-gate 1257c478bd9Sstevel@tonic-gate if (terminate_cleanup) 1267c478bd9Sstevel@tonic-gate terminate_cleanup(); 1277c478bd9Sstevel@tonic-gate 128*c168da27Sjohnlev if (getenv("CTF_ABORT_ON_TERMINATE") != NULL) 129*c168da27Sjohnlev abort(); 1307c478bd9Sstevel@tonic-gate exit(1); 1317c478bd9Sstevel@tonic-gate } 1327c478bd9Sstevel@tonic-gate 1337c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/ 1347c478bd9Sstevel@tonic-gate void 135*c168da27Sjohnlev aborterr(char *format, ...) 136*c168da27Sjohnlev { 137*c168da27Sjohnlev va_list ap; 138*c168da27Sjohnlev 139*c168da27Sjohnlev va_start(ap, format); 140*c168da27Sjohnlev whine("ERROR", format, ap); 141*c168da27Sjohnlev va_end(ap); 142*c168da27Sjohnlev 143*c168da27Sjohnlev abort(); 144*c168da27Sjohnlev } 145*c168da27Sjohnlev 146*c168da27Sjohnlev /*PRINTFLIKE1*/ 147*c168da27Sjohnlev void 1487c478bd9Sstevel@tonic-gate warning(char *format, ...) 1497c478bd9Sstevel@tonic-gate { 1507c478bd9Sstevel@tonic-gate va_list ap; 1517c478bd9Sstevel@tonic-gate 1527c478bd9Sstevel@tonic-gate va_start(ap, format); 1537c478bd9Sstevel@tonic-gate whine("WARNING", format, ap); 1547c478bd9Sstevel@tonic-gate va_end(ap); 1557c478bd9Sstevel@tonic-gate 1567c478bd9Sstevel@tonic-gate if (debug_level >= 3) 1577c478bd9Sstevel@tonic-gate terminate("Termination due to warning\n"); 1587c478bd9Sstevel@tonic-gate } 1597c478bd9Sstevel@tonic-gate 1607c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/ 1617c478bd9Sstevel@tonic-gate void 1627c478bd9Sstevel@tonic-gate vadebug(int level, char *format, va_list ap) 1637c478bd9Sstevel@tonic-gate { 1647c478bd9Sstevel@tonic-gate if (level > debug_level) 1657c478bd9Sstevel@tonic-gate return; 1667c478bd9Sstevel@tonic-gate 1677c478bd9Sstevel@tonic-gate (void) fprintf(DEBUG_STREAM, "DEBUG: "); 1687c478bd9Sstevel@tonic-gate (void) vfprintf(DEBUG_STREAM, format, ap); 1697c478bd9Sstevel@tonic-gate fflush(DEBUG_STREAM); 1707c478bd9Sstevel@tonic-gate } 1717c478bd9Sstevel@tonic-gate 1727c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/ 1737c478bd9Sstevel@tonic-gate void 1747c478bd9Sstevel@tonic-gate debug(int level, char *format, ...) 1757c478bd9Sstevel@tonic-gate { 1767c478bd9Sstevel@tonic-gate va_list ap; 1777c478bd9Sstevel@tonic-gate 1787c478bd9Sstevel@tonic-gate if (level > debug_level) 1797c478bd9Sstevel@tonic-gate return; 1807c478bd9Sstevel@tonic-gate 1817c478bd9Sstevel@tonic-gate va_start(ap, format); 1827c478bd9Sstevel@tonic-gate (void) vadebug(level, format, ap); 1837c478bd9Sstevel@tonic-gate va_end(ap); 1847c478bd9Sstevel@tonic-gate } 1857c478bd9Sstevel@tonic-gate 1867c478bd9Sstevel@tonic-gate char * 1877c478bd9Sstevel@tonic-gate mktmpname(const char *origname, const char *suffix) 1887c478bd9Sstevel@tonic-gate { 1897c478bd9Sstevel@tonic-gate char *newname; 1907c478bd9Sstevel@tonic-gate 1914d232658Sjohnlev newname = xmalloc(strlen(origname) + strlen(suffix) + 1); 1924d232658Sjohnlev (void) strcpy(newname, origname); 1934d232658Sjohnlev (void) strcat(newname, suffix); 1947c478bd9Sstevel@tonic-gate return (newname); 1957c478bd9Sstevel@tonic-gate } 1967c478bd9Sstevel@tonic-gate 1977c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/ 1987c478bd9Sstevel@tonic-gate void 1997c478bd9Sstevel@tonic-gate elfterminate(const char *file, const char *fmt, ...) 2007c478bd9Sstevel@tonic-gate { 2017c478bd9Sstevel@tonic-gate static char msgbuf[BUFSIZ]; 2027c478bd9Sstevel@tonic-gate va_list ap; 2037c478bd9Sstevel@tonic-gate 2047c478bd9Sstevel@tonic-gate va_start(ap, fmt); 2057c478bd9Sstevel@tonic-gate vsnprintf(msgbuf, sizeof (msgbuf), fmt, ap); 2067c478bd9Sstevel@tonic-gate va_end(ap); 2077c478bd9Sstevel@tonic-gate 208*c168da27Sjohnlev terminate("%s: %s: %s\n", file, msgbuf, elf_errmsg(-1)); 2097c478bd9Sstevel@tonic-gate } 2104d232658Sjohnlev 2114d232658Sjohnlev const char * 2124d232658Sjohnlev tdesc_name(tdesc_t *tdp) 2134d232658Sjohnlev { 2144d232658Sjohnlev return (tdp->t_name == NULL ? "(anon)" : tdp->t_name); 2154d232658Sjohnlev } 216