xref: /onnv-gate/usr/src/tools/ctf/cvt/util.c (revision 1951:668d8ad6b241)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51882Sjohnlev  * Common Development and Distribution License (the "License").
61882Sjohnlev  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
221882Sjohnlev  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  * Utility functions
300Sstevel@tonic-gate  */
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <stdio.h>
330Sstevel@tonic-gate #include <stdlib.h>
340Sstevel@tonic-gate #include <string.h>
350Sstevel@tonic-gate #include <libelf.h>
360Sstevel@tonic-gate #include <gelf.h>
370Sstevel@tonic-gate #include <errno.h>
380Sstevel@tonic-gate #include <stdarg.h>
390Sstevel@tonic-gate #include <pthread.h>
400Sstevel@tonic-gate #include <unistd.h>
410Sstevel@tonic-gate #include <sys/param.h>
420Sstevel@tonic-gate 
430Sstevel@tonic-gate #include "ctftools.h"
440Sstevel@tonic-gate #include "memory.h"
450Sstevel@tonic-gate 
460Sstevel@tonic-gate static void (*terminate_cleanup)() = NULL;
470Sstevel@tonic-gate 
480Sstevel@tonic-gate /* returns 1 if s1 == s2, 0 otherwise */
490Sstevel@tonic-gate int
500Sstevel@tonic-gate streq(char *s1, char *s2)
510Sstevel@tonic-gate {
520Sstevel@tonic-gate 	if (s1 == NULL) {
530Sstevel@tonic-gate 		if (s2 != NULL)
540Sstevel@tonic-gate 			return (0);
550Sstevel@tonic-gate 	} else if (s2 == NULL)
560Sstevel@tonic-gate 		return (0);
570Sstevel@tonic-gate 	else if (strcmp(s1, s2) != 0)
580Sstevel@tonic-gate 		return (0);
590Sstevel@tonic-gate 
600Sstevel@tonic-gate 	return (1);
610Sstevel@tonic-gate }
620Sstevel@tonic-gate 
630Sstevel@tonic-gate int
64*1951Sjohnlev findelfsecidx(Elf *elf, const char *file, const char *tofind)
650Sstevel@tonic-gate {
660Sstevel@tonic-gate 	Elf_Scn *scn = NULL;
670Sstevel@tonic-gate 	GElf_Ehdr ehdr;
680Sstevel@tonic-gate 	GElf_Shdr shdr;
690Sstevel@tonic-gate 
70*1951Sjohnlev 	if (gelf_getehdr(elf, &ehdr) == NULL)
71*1951Sjohnlev 		elfterminate(file, "Couldn't read ehdr");
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 	while ((scn = elf_nextscn(elf, scn)) != NULL) {
740Sstevel@tonic-gate 		char *name;
750Sstevel@tonic-gate 
76*1951Sjohnlev 		if (gelf_getshdr(scn, &shdr) == NULL) {
77*1951Sjohnlev 			elfterminate(file,
78*1951Sjohnlev 			    "Couldn't read header for section %d",
79*1951Sjohnlev 			    elf_ndxscn(scn));
80*1951Sjohnlev 		}
81*1951Sjohnlev 
82*1951Sjohnlev 		if ((name = elf_strptr(elf, ehdr.e_shstrndx,
830Sstevel@tonic-gate 		    (size_t)shdr.sh_name)) == NULL) {
84*1951Sjohnlev 			elfterminate(file,
85*1951Sjohnlev 			    "Couldn't get name for section %d",
86*1951Sjohnlev 			    elf_ndxscn(scn));
870Sstevel@tonic-gate 		}
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 		if (strcmp(name, tofind) == 0)
900Sstevel@tonic-gate 			return (elf_ndxscn(scn));
910Sstevel@tonic-gate 	}
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 	return (-1);
940Sstevel@tonic-gate }
950Sstevel@tonic-gate 
960Sstevel@tonic-gate /*PRINTFLIKE2*/
970Sstevel@tonic-gate static void
980Sstevel@tonic-gate whine(char *type, char *format, va_list ap)
990Sstevel@tonic-gate {
1000Sstevel@tonic-gate 	int error = errno;
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 	fprintf(stderr, "%s: %s: ", type, progname);
1030Sstevel@tonic-gate 	vfprintf(stderr, format, ap);
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 	if (format[strlen(format) - 1] != '\n')
1060Sstevel@tonic-gate 		fprintf(stderr, ": %s\n", strerror(error));
1070Sstevel@tonic-gate }
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate void
1100Sstevel@tonic-gate set_terminate_cleanup(void (*cleanup)())
1110Sstevel@tonic-gate {
1120Sstevel@tonic-gate 	terminate_cleanup = cleanup;
1130Sstevel@tonic-gate }
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate /*PRINTFLIKE1*/
1160Sstevel@tonic-gate void
1170Sstevel@tonic-gate terminate(char *format, ...)
1180Sstevel@tonic-gate {
119*1951Sjohnlev 	va_list ap;
1200Sstevel@tonic-gate 
121*1951Sjohnlev 	va_start(ap, format);
122*1951Sjohnlev 	whine("ERROR", format, ap);
123*1951Sjohnlev 	va_end(ap);
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 	if (terminate_cleanup)
1260Sstevel@tonic-gate 		terminate_cleanup();
1270Sstevel@tonic-gate 
128*1951Sjohnlev 	if (getenv("CTF_ABORT_ON_TERMINATE") != NULL)
129*1951Sjohnlev 		abort();
1300Sstevel@tonic-gate 	exit(1);
1310Sstevel@tonic-gate }
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate /*PRINTFLIKE1*/
1340Sstevel@tonic-gate void
135*1951Sjohnlev aborterr(char *format, ...)
136*1951Sjohnlev {
137*1951Sjohnlev 	va_list ap;
138*1951Sjohnlev 
139*1951Sjohnlev 	va_start(ap, format);
140*1951Sjohnlev 	whine("ERROR", format, ap);
141*1951Sjohnlev 	va_end(ap);
142*1951Sjohnlev 
143*1951Sjohnlev 	abort();
144*1951Sjohnlev }
145*1951Sjohnlev 
146*1951Sjohnlev /*PRINTFLIKE1*/
147*1951Sjohnlev void
1480Sstevel@tonic-gate warning(char *format, ...)
1490Sstevel@tonic-gate {
1500Sstevel@tonic-gate 	va_list ap;
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	va_start(ap, format);
1530Sstevel@tonic-gate 	whine("WARNING", format, ap);
1540Sstevel@tonic-gate 	va_end(ap);
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	if (debug_level >= 3)
1570Sstevel@tonic-gate 		terminate("Termination due to warning\n");
1580Sstevel@tonic-gate }
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate /*PRINTFLIKE2*/
1610Sstevel@tonic-gate void
1620Sstevel@tonic-gate vadebug(int level, char *format, va_list ap)
1630Sstevel@tonic-gate {
1640Sstevel@tonic-gate 	if (level > debug_level)
1650Sstevel@tonic-gate 		return;
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	(void) fprintf(DEBUG_STREAM, "DEBUG: ");
1680Sstevel@tonic-gate 	(void) vfprintf(DEBUG_STREAM, format, ap);
1690Sstevel@tonic-gate 	fflush(DEBUG_STREAM);
1700Sstevel@tonic-gate }
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate /*PRINTFLIKE2*/
1730Sstevel@tonic-gate void
1740Sstevel@tonic-gate debug(int level, char *format, ...)
1750Sstevel@tonic-gate {
1760Sstevel@tonic-gate 	va_list ap;
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 	if (level > debug_level)
1790Sstevel@tonic-gate 		return;
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	va_start(ap, format);
1820Sstevel@tonic-gate 	(void) vadebug(level, format, ap);
1830Sstevel@tonic-gate 	va_end(ap);
1840Sstevel@tonic-gate }
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate char *
1870Sstevel@tonic-gate mktmpname(const char *origname, const char *suffix)
1880Sstevel@tonic-gate {
1890Sstevel@tonic-gate 	char *newname;
1900Sstevel@tonic-gate 
1911882Sjohnlev 	newname = xmalloc(strlen(origname) + strlen(suffix) + 1);
1921882Sjohnlev 	(void) strcpy(newname, origname);
1931882Sjohnlev 	(void) strcat(newname, suffix);
1940Sstevel@tonic-gate 	return (newname);
1950Sstevel@tonic-gate }
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate /*PRINTFLIKE2*/
1980Sstevel@tonic-gate void
1990Sstevel@tonic-gate elfterminate(const char *file, const char *fmt, ...)
2000Sstevel@tonic-gate {
2010Sstevel@tonic-gate 	static char msgbuf[BUFSIZ];
2020Sstevel@tonic-gate 	va_list ap;
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 	va_start(ap, fmt);
2050Sstevel@tonic-gate 	vsnprintf(msgbuf, sizeof (msgbuf), fmt, ap);
2060Sstevel@tonic-gate 	va_end(ap);
2070Sstevel@tonic-gate 
208*1951Sjohnlev 	terminate("%s: %s: %s\n", file, msgbuf, elf_errmsg(-1));
2090Sstevel@tonic-gate }
2101882Sjohnlev 
2111882Sjohnlev const char *
2121882Sjohnlev tdesc_name(tdesc_t *tdp)
2131882Sjohnlev {
2141882Sjohnlev 	return (tdp->t_name == NULL ? "(anon)" : tdp->t_name);
2151882Sjohnlev }
216