xref: /freebsd/cddl/contrib/opensolaris/tools/ctf/cvt/ctfconvert.c (revision e6bfd18d21b225af6a0ed67ceeaf1293b7b9eba5)
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  * Given a file containing sections with stabs data, convert the stabs data to
30  * CTF data, and replace the stabs sections with a CTF section.
31  */
32 
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <signal.h>
37 #include <string.h>
38 #include <fcntl.h>
39 #include <libgen.h>
40 #include <errno.h>
41 #include <assert.h>
42 
43 #include "ctftools.h"
44 #include "memory.h"
45 
46 const  char *progname;
47 int debug_level = DEBUG_LEVEL;
48 
49 static char *infile = NULL;
50 static const char *outfile = NULL;
51 static int dynsym;
52 
53 static void
54 usage(void)
55 {
56 	(void) fprintf(stderr,
57 	    "Usage: %s [-gis] -l label | -L labelenv [-o outfile] object_file\n"
58 	    "\n"
59 	    "  Note: if -L labelenv is specified and labelenv is not set in\n"
60 	    "  the environment, a default value is used.\n",
61 	    progname);
62 }
63 
64 static void
65 terminate_cleanup(void)
66 {
67 #if !defined(__FreeBSD__)
68 	if (!outfile) {
69 		fprintf(stderr, "Removing %s\n", infile);
70 		unlink(infile);
71 	}
72 #endif
73 }
74 
75 static void
76 handle_sig(int sig)
77 {
78 	terminate("Caught signal %d - exiting\n", sig);
79 }
80 
81 static int
82 file_read(tdata_t *td, char *filename, int ignore_non_c)
83 {
84 	typedef int (*reader_f)(tdata_t *, Elf *, char *);
85 	static reader_f readers[] = {
86 		dw_read,
87 		NULL
88 	};
89 
90 	source_types_t source_types;
91 	Elf *elf;
92 	int i, rc, fd;
93 
94 	if ((fd = open(filename, O_RDONLY)) < 0)
95 		terminate("failed to open %s", filename);
96 
97 	(void) elf_version(EV_CURRENT);
98 
99 	if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
100 		close(fd);
101 		terminate("failed to read %s: %s\n", filename,
102 		    elf_errmsg(-1));
103 	}
104 
105 	source_types = built_source_types(elf, filename);
106 
107 	if ((source_types == SOURCE_NONE || (source_types & SOURCE_UNKNOWN)) &&
108 	    ignore_non_c) {
109 		debug(1, "Ignoring file %s from unknown sources\n", filename);
110 		exit(0);
111 	}
112 
113 	for (i = 0; readers[i] != NULL; i++) {
114 		if ((rc = readers[i](td, elf, filename)) == 0)
115 			break;
116 
117 		assert(rc < 0 && errno == ENOENT);
118 	}
119 
120 	if (readers[i] == NULL) {
121 		/*
122 		 * None of the readers found compatible type data.
123 		 */
124 
125 		if (findelfsecidx(elf, filename, ".debug") >= 0) {
126 			terminate("%s: DWARF version 1 is not supported\n",
127 			    filename);
128 		}
129 
130 		if (!(source_types & SOURCE_C) && ignore_non_c) {
131 			debug(1, "Ignoring file %s not built from C sources\n",
132 			    filename);
133 			exit(0);
134 		}
135 
136 		rc = 0;
137 	} else {
138 		rc = 1;
139 	}
140 
141 	(void) elf_end(elf);
142 	(void) close(fd);
143 
144 	return (rc);
145 }
146 
147 int
148 main(int argc, char **argv)
149 {
150 	tdata_t *filetd, *mstrtd;
151 	const char *label = NULL;
152 	int verbose = 0;
153 	int ignore_non_c = 0;
154 	int keep_stabs = 0;
155 	int c;
156 
157 #ifdef illumos
158 	sighold(SIGINT);
159 	sighold(SIGQUIT);
160 	sighold(SIGTERM);
161 #endif
162 
163 	progname = basename(argv[0]);
164 
165 	if (getenv("CTFCONVERT_DEBUG_LEVEL"))
166 		debug_level = atoi(getenv("CTFCONVERT_DEBUG_LEVEL"));
167 
168 	while ((c = getopt(argc, argv, ":l:L:o:givs")) != EOF) {
169 		switch (c) {
170 		case 'l':
171 			label = optarg;
172 			break;
173 		case 'L':
174 			if ((label = getenv(optarg)) == NULL)
175 				label = CTF_DEFAULT_LABEL;
176 			break;
177 		case 'o':
178 			outfile = optarg;
179 			break;
180 		case 's':
181 			dynsym = CTF_USE_DYNSYM;
182 			break;
183 		case 'i':
184 			ignore_non_c = 1;
185 			break;
186 		case 'g':
187 			keep_stabs = CTF_KEEP_STABS;
188 			break;
189 		case 'v':
190 			verbose = 1;
191 			break;
192 		default:
193 			usage();
194 			exit(2);
195 		}
196 	}
197 
198 	if (getenv("STRIPSTABS_KEEP_STABS") != NULL)
199 		keep_stabs = CTF_KEEP_STABS;
200 
201 	if (argc - optind != 1 || label == NULL) {
202 		usage();
203 		exit(2);
204 	}
205 
206 	infile = argv[optind];
207 	if (access(infile, R_OK) != 0)
208 		terminate("Can't access %s", infile);
209 
210 	/*
211 	 * Upon receipt of a signal, we want to clean up and exit.  Our
212 	 * primary goal during cleanup is to restore the system to a state
213 	 * such that a subsequent make will eventually cause this command to
214 	 * be re-run.  If we remove the input file (which we do if we get a
215 	 * signal and the user didn't specify a separate output file), make
216 	 * will need to rebuild the input file, and will then need to re-run
217 	 * ctfconvert, which is what we want.
218 	 */
219 	set_terminate_cleanup(terminate_cleanup);
220 
221 #ifdef illumos
222 	sigset(SIGINT, handle_sig);
223 	sigset(SIGQUIT, handle_sig);
224 	sigset(SIGTERM, handle_sig);
225 #else
226 	signal(SIGINT, handle_sig);
227 	signal(SIGQUIT, handle_sig);
228 	signal(SIGTERM, handle_sig);
229 #endif
230 
231 	filetd = tdata_new();
232 
233 	if (!file_read(filetd, infile, ignore_non_c))
234 		terminate("%s doesn't have type data to convert\n", infile);
235 
236 	if (verbose)
237 		iidesc_stats(filetd->td_iihash);
238 
239 	mstrtd = tdata_new();
240 	merge_into_master(filetd, mstrtd, NULL, 1);
241 
242 	tdata_label_add(mstrtd, label, CTF_LABEL_LASTIDX);
243 
244 	/*
245 	 * If the user supplied an output file that is different from the
246 	 * input file, write directly to the output file.  Otherwise, write
247 	 * to a temporary file, and replace the input file when we're done.
248 	 */
249 	if (outfile && strcmp(infile, outfile) != 0) {
250 		write_ctf(mstrtd, infile, outfile, dynsym | keep_stabs);
251 	} else {
252 		char *tmpname = mktmpname(infile, ".ctf");
253 		write_ctf(mstrtd, infile, tmpname, dynsym | keep_stabs);
254 		if (rename(tmpname, infile) != 0)
255 			terminate("Couldn't rename temp file %s", tmpname);
256 		free(tmpname);
257 	}
258 
259 	return (0);
260 }
261