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