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	"@(#)util.c	1.11	06/08/22 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
46static void (*terminate_cleanup)() = NULL;
47
48/* returns 1 if s1 == s2, 0 otherwise */
49int
50streq(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
63int
64findelfsecidx(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
96size_t
97elf_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*/
118static void
119whine(char *type, 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
130void
131set_terminate_cleanup(void (*cleanup)())
132{
133	terminate_cleanup = cleanup;
134}
135
136/*PRINTFLIKE1*/
137void
138terminate(char *format, ...)
139{
140	va_list ap;
141
142	va_start(ap, format);
143#if !defined(__APPLE__)
144	whine("ERROR", format, ap);
145#else
146    /*
147     * Supress the error message if the format is empty
148     */
149    if (format[0] != 0) {
150        whine("ERROR", format, ap);
151    }
152#endif
153	va_end(ap);
154
155	if (terminate_cleanup)
156		terminate_cleanup();
157
158	if (getenv("CTF_ABORT_ON_TERMINATE") != NULL)
159		abort();
160	exit(1);
161}
162
163/*PRINTFLIKE1*/
164void
165aborterr(char *format, ...)
166{
167	va_list ap;
168
169	va_start(ap, format);
170	whine("ERROR", format, ap);
171	va_end(ap);
172
173#if !defined(__APPLE__)
174	abort();
175#else
176	/*
177	 * On Mac OS X, unhandled SIGABRT raised by abort() invokes the CrashReporter
178	 * mechanism. That's way more heavyweight than needed. And as there are no SIGABRT
179	 * signal handlers in ctfconvert and friends, we may as well just exit().
180	 */
181	exit(1);
182#endif /* __APPLE__ */
183}
184
185/*PRINTFLIKE1*/
186void
187warning(char *format, ...)
188{
189	va_list ap;
190
191	va_start(ap, format);
192	whine("WARNING", format, ap);
193	va_end(ap);
194
195	if (debug_level >= 3)
196		terminate("Termination due to warning\n");
197}
198
199/*PRINTFLIKE2*/
200void
201vadebug(int level, char *format, va_list ap)
202{
203	if (level > debug_level)
204		return;
205
206	(void) fprintf(DEBUG_STREAM, "DEBUG: ");
207	(void) vfprintf(DEBUG_STREAM, format, ap);
208	fflush(DEBUG_STREAM);
209}
210
211/*PRINTFLIKE2*/
212void
213debug(int level, char *format, ...)
214{
215	va_list ap;
216
217	if (level > debug_level)
218		return;
219
220	va_start(ap, format);
221	(void) vadebug(level, format, ap);
222	va_end(ap);
223}
224
225char *
226mktmpname(const char *origname, const char *suffix)
227{
228	char *newname;
229
230	newname = xmalloc(strlen(origname) + strlen(suffix) + 1);
231	(void) strcpy(newname, origname);
232	(void) strcat(newname, suffix);
233	return (newname);
234}
235
236/*PRINTFLIKE2*/
237void
238elfterminate(const char *file, const char *fmt, ...)
239{
240	static char msgbuf[BUFSIZ];
241	va_list ap;
242
243	va_start(ap, fmt);
244	vsnprintf(msgbuf, sizeof (msgbuf), fmt, ap);
245	va_end(ap);
246
247	terminate("%s: %s: %s\n", file, msgbuf, elf_errmsg(-1));
248}
249
250const char *
251tdesc_name(tdesc_t *tdp)
252{
253	return (tdp->t_name == NULL ? "(anon)" : tdp->t_name);
254}
255