xref: /titanic_52/usr/src/tools/ctf/cvt/util.c (revision 4d232658ec6831daceb12672726ce4cabe270c23)
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
5*4d232658Sjohnlev  * Common Development and Distribution License (the "License").
6*4d232658Sjohnlev  * 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 /*
22*4d232658Sjohnlev  * 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
647c478bd9Sstevel@tonic-gate findelfsecidx(Elf *elf, 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 
707c478bd9Sstevel@tonic-gate 	if (gelf_getehdr(elf, &ehdr) == NULL) {
717c478bd9Sstevel@tonic-gate 		terminate("gelf_getehdr: %s\n", elf_errmsg(elf_errno()));
727c478bd9Sstevel@tonic-gate 	}
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	while ((scn = elf_nextscn(elf, scn)) != NULL) {
757c478bd9Sstevel@tonic-gate 		char *name;
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 		if (gelf_getshdr(scn, &shdr) == NULL ||
787c478bd9Sstevel@tonic-gate 		    (name = elf_strptr(elf, ehdr.e_shstrndx,
797c478bd9Sstevel@tonic-gate 		    (size_t)shdr.sh_name)) == NULL) {
807c478bd9Sstevel@tonic-gate 			terminate("gelf_getehdr: %s\n",
817c478bd9Sstevel@tonic-gate 			    elf_errmsg(elf_errno()));
827c478bd9Sstevel@tonic-gate 		}
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 		if (strcmp(name, tofind) == 0)
857c478bd9Sstevel@tonic-gate 			return (elf_ndxscn(scn));
867c478bd9Sstevel@tonic-gate 	}
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	return (-1);
897c478bd9Sstevel@tonic-gate }
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/
927c478bd9Sstevel@tonic-gate static void
937c478bd9Sstevel@tonic-gate whine(char *type, char *format, va_list ap)
947c478bd9Sstevel@tonic-gate {
957c478bd9Sstevel@tonic-gate 	int error = errno;
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	fprintf(stderr, "%s: %s: ", type, progname);
987c478bd9Sstevel@tonic-gate 	vfprintf(stderr, format, ap);
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	if (format[strlen(format) - 1] != '\n')
1017c478bd9Sstevel@tonic-gate 		fprintf(stderr, ": %s\n", strerror(error));
1027c478bd9Sstevel@tonic-gate }
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate void
1057c478bd9Sstevel@tonic-gate vaterminate(char *format, va_list ap)
1067c478bd9Sstevel@tonic-gate {
1077c478bd9Sstevel@tonic-gate 	whine("ERROR", format, ap);
1087c478bd9Sstevel@tonic-gate }
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate void
1117c478bd9Sstevel@tonic-gate set_terminate_cleanup(void (*cleanup)())
1127c478bd9Sstevel@tonic-gate {
1137c478bd9Sstevel@tonic-gate 	terminate_cleanup = cleanup;
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
1177c478bd9Sstevel@tonic-gate void
1187c478bd9Sstevel@tonic-gate terminate(char *format, ...)
1197c478bd9Sstevel@tonic-gate {
1207c478bd9Sstevel@tonic-gate 	if (format) {
1217c478bd9Sstevel@tonic-gate 		va_list ap;
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 		va_start(ap, format);
1247c478bd9Sstevel@tonic-gate 		whine("ERROR", format, ap);
1257c478bd9Sstevel@tonic-gate 		va_end(ap);
1267c478bd9Sstevel@tonic-gate 	}
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	if (terminate_cleanup)
1297c478bd9Sstevel@tonic-gate 		terminate_cleanup();
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	exit(1);
1327c478bd9Sstevel@tonic-gate }
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
1357c478bd9Sstevel@tonic-gate void
1367c478bd9Sstevel@tonic-gate warning(char *format, ...)
1377c478bd9Sstevel@tonic-gate {
1387c478bd9Sstevel@tonic-gate 	va_list ap;
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	va_start(ap, format);
1417c478bd9Sstevel@tonic-gate 	whine("WARNING", format, ap);
1427c478bd9Sstevel@tonic-gate 	va_end(ap);
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	if (debug_level >= 3)
1457c478bd9Sstevel@tonic-gate 		terminate("Termination due to warning\n");
1467c478bd9Sstevel@tonic-gate }
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/
1497c478bd9Sstevel@tonic-gate void
1507c478bd9Sstevel@tonic-gate vadebug(int level, char *format, va_list ap)
1517c478bd9Sstevel@tonic-gate {
1527c478bd9Sstevel@tonic-gate 	if (level > debug_level)
1537c478bd9Sstevel@tonic-gate 		return;
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	(void) fprintf(DEBUG_STREAM, "DEBUG: ");
1567c478bd9Sstevel@tonic-gate 	(void) vfprintf(DEBUG_STREAM, format, ap);
1577c478bd9Sstevel@tonic-gate 	fflush(DEBUG_STREAM);
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/
1617c478bd9Sstevel@tonic-gate void
1627c478bd9Sstevel@tonic-gate debug(int level, char *format, ...)
1637c478bd9Sstevel@tonic-gate {
1647c478bd9Sstevel@tonic-gate 	va_list ap;
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	if (level > debug_level)
1677c478bd9Sstevel@tonic-gate 		return;
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	va_start(ap, format);
1707c478bd9Sstevel@tonic-gate 	(void) vadebug(level, format, ap);
1717c478bd9Sstevel@tonic-gate 	va_end(ap);
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate char *
1757c478bd9Sstevel@tonic-gate mktmpname(const char *origname, const char *suffix)
1767c478bd9Sstevel@tonic-gate {
1777c478bd9Sstevel@tonic-gate 	char *newname;
1787c478bd9Sstevel@tonic-gate 
179*4d232658Sjohnlev 	newname = xmalloc(strlen(origname) + strlen(suffix) + 1);
180*4d232658Sjohnlev 	(void) strcpy(newname, origname);
181*4d232658Sjohnlev 	(void) strcat(newname, suffix);
1827c478bd9Sstevel@tonic-gate 	return (newname);
1837c478bd9Sstevel@tonic-gate }
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/
1867c478bd9Sstevel@tonic-gate void
1877c478bd9Sstevel@tonic-gate elfterminate(const char *file, const char *fmt, ...)
1887c478bd9Sstevel@tonic-gate {
1897c478bd9Sstevel@tonic-gate 	static char msgbuf[BUFSIZ];
1907c478bd9Sstevel@tonic-gate 	va_list ap;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
1937c478bd9Sstevel@tonic-gate 	vsnprintf(msgbuf, sizeof (msgbuf), fmt, ap);
1947c478bd9Sstevel@tonic-gate 	va_end(ap);
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	terminate("%s: %s: %s\n", file, msgbuf, elf_errmsg(elf_errno()));
1977c478bd9Sstevel@tonic-gate }
198*4d232658Sjohnlev 
199*4d232658Sjohnlev const char *
200*4d232658Sjohnlev tdesc_name(tdesc_t *tdp)
201*4d232658Sjohnlev {
202*4d232658Sjohnlev 	return (tdp->t_name == NULL ? "(anon)" : tdp->t_name);
203*4d232658Sjohnlev }
204