1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 /* 29 * Utility functions 30 */ 31 32 #include <stdio.h> 33 #include <stdlib.h> 34 #include <string.h> 35 #include <libelf.h> 36 #include <gelf.h> 37 #include <errno.h> 38 #include <stdarg.h> 39 #include <pthread.h> 40 #include <unistd.h> 41 #include <sys/param.h> 42 43 #include "ctftools.h" 44 #include "memory.h" 45 46 static void (*terminate_cleanup)(void) = NULL; 47 48 /* returns 1 if s1 == s2, 0 otherwise */ 49 int 50 streq(const char *s1, const char *s2) 51 { 52 if (s1 == NULL) { 53 if (s2 != NULL) 54 return (0); 55 } else if (s2 == NULL) 56 return (0); 57 else if (strcmp(s1, s2) != 0) 58 return (0); 59 60 return (1); 61 } 62 63 int 64 findelfsecidx(Elf *elf, const char *file, const char *tofind) 65 { 66 Elf_Scn *scn = NULL; 67 GElf_Ehdr ehdr; 68 GElf_Shdr shdr; 69 70 if (gelf_getehdr(elf, &ehdr) == NULL) 71 elfterminate(file, "Couldn't read ehdr"); 72 73 while ((scn = elf_nextscn(elf, scn)) != NULL) { 74 char *name; 75 76 if (gelf_getshdr(scn, &shdr) == NULL) { 77 elfterminate(file, 78 "Couldn't read header for section %d", 79 elf_ndxscn(scn)); 80 } 81 82 if ((name = elf_strptr(elf, ehdr.e_shstrndx, 83 (size_t)shdr.sh_name)) == NULL) { 84 elfterminate(file, 85 "Couldn't get name for section %d", 86 elf_ndxscn(scn)); 87 } 88 89 if (strcmp(name, tofind) == 0) 90 return (elf_ndxscn(scn)); 91 } 92 93 return (-1); 94 } 95 96 size_t 97 elf_ptrsz(Elf *elf) 98 { 99 GElf_Ehdr ehdr; 100 101 if (gelf_getehdr(elf, &ehdr) == NULL) { 102 terminate("failed to read ELF header: %s\n", 103 elf_errmsg(-1)); 104 } 105 106 if (ehdr.e_ident[EI_CLASS] == ELFCLASS32) 107 return (4); 108 else if (ehdr.e_ident[EI_CLASS] == ELFCLASS64) 109 return (8); 110 else 111 terminate("unknown ELF class %d\n", ehdr.e_ident[EI_CLASS]); 112 113 /*NOTREACHED*/ 114 return (0); 115 } 116 117 /*PRINTFLIKE2*/ 118 static void 119 whine(const char *type, const char *format, va_list ap) 120 { 121 int error = errno; 122 123 fprintf(stderr, "%s: %s: ", type, progname); 124 vfprintf(stderr, format, ap); 125 126 if (format[strlen(format) - 1] != '\n') 127 fprintf(stderr, ": %s\n", strerror(error)); 128 } 129 130 void 131 set_terminate_cleanup(void (*cleanup)(void)) 132 { 133 terminate_cleanup = cleanup; 134 } 135 136 /*PRINTFLIKE1*/ 137 void 138 terminate(const char *format, ...) 139 { 140 va_list ap; 141 142 va_start(ap, format); 143 whine("ERROR", format, ap); 144 va_end(ap); 145 146 if (terminate_cleanup) 147 terminate_cleanup(); 148 149 if (getenv("CTF_ABORT_ON_TERMINATE") != NULL) 150 abort(); 151 exit(1); 152 } 153 154 /*PRINTFLIKE1*/ 155 void 156 aborterr(const char *format, ...) 157 { 158 va_list ap; 159 160 va_start(ap, format); 161 whine("ERROR", format, ap); 162 va_end(ap); 163 164 #ifdef illumos 165 abort(); 166 #else 167 exit(0); 168 #endif 169 } 170 171 /*PRINTFLIKE1*/ 172 void 173 warning(const char *format, ...) 174 { 175 va_list ap; 176 177 va_start(ap, format); 178 whine("WARNING", format, ap); 179 va_end(ap); 180 181 if (debug_level >= 3) 182 terminate("Termination due to warning\n"); 183 } 184 185 /*PRINTFLIKE2*/ 186 void 187 vadebug(int level, const char *format, va_list ap) 188 { 189 if (level > debug_level) 190 return; 191 192 (void) fprintf(DEBUG_STREAM, "DEBUG: "); 193 (void) vfprintf(DEBUG_STREAM, format, ap); 194 fflush(DEBUG_STREAM); 195 } 196 197 /*PRINTFLIKE2*/ 198 void 199 debug(int level, const char *format, ...) 200 { 201 va_list ap; 202 203 if (level > debug_level) 204 return; 205 206 va_start(ap, format); 207 (void) vadebug(level, format, ap); 208 va_end(ap); 209 } 210 211 char * 212 mktmpname(const char *origname, const char *suffix) 213 { 214 char *newname; 215 216 newname = xmalloc(strlen(origname) + strlen(suffix) + 1); 217 (void) strcpy(newname, origname); 218 (void) strcat(newname, suffix); 219 return (newname); 220 } 221 222 /*PRINTFLIKE2*/ 223 void 224 elfterminate(const char *file, const char *fmt, ...) 225 { 226 static char msgbuf[BUFSIZ]; 227 va_list ap; 228 229 va_start(ap, fmt); 230 vsnprintf(msgbuf, sizeof (msgbuf), fmt, ap); 231 va_end(ap); 232 233 terminate("%s: %s: %s\n", file, msgbuf, elf_errmsg(-1)); 234 } 235 236 const char * 237 tdesc_name(tdesc_t *tdp) 238 { 239 return (tdp->t_name == NULL ? "(anon)" : tdp->t_name); 240 } 241 242 static char *watch_address = NULL; 243 static int watch_length = 0; 244 245 void 246 watch_set(void *addr, int len) 247 { 248 watch_address = addr; 249 watch_length = len; 250 } 251 252 void 253 watch_dump(int v) 254 { 255 char *p = watch_address; 256 int i; 257 258 if (watch_address == NULL || watch_length == 0) 259 return; 260 261 printf("%d: watch %p len %d\n",v,watch_address,watch_length); 262 for (i = 0; i < watch_length; i++) { 263 if (*p >= 0x20 && *p < 0x7f) { 264 printf(" %c",*p++ & 0xff); 265 } else { 266 printf(" %02x",*p++ & 0xff); 267 } 268 } 269 printf("\n"); 270 271 } 272 273 274