xref: /titanic_44/usr/src/tools/ctf/cvt/ctfconvert.c (revision 8eea8e29cc4374d1ee24c25a07f45af132db3499)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * Given a file containing sections with stabs data, convert the stabs data to
31  * CTF data, and replace the stabs sections with a CTF section.
32  */
33 
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <signal.h>
38 #include <string.h>
39 #include <fcntl.h>
40 #include <libgen.h>
41 #include <errno.h>
42 #include <assert.h>
43 
44 #include "ctftools.h"
45 #include "memory.h"
46 
47 const  char *progname;
48 int debug_level = DEBUG_LEVEL;
49 
50 static const char *infile = NULL;
51 static const char *outfile = NULL;
52 static int dynsym;
53 
54 static void
55 usage(void)
56 {
57 	(void) fprintf(stderr,
58 	    "Usage: %s [-gis] -l label | -L labelenv [-o outfile] object_file\n"
59 	    "\n"
60 	    "  Note: if -L labelenv is specified and labelenv is not set in\n"
61 	    "  the environment, a default value is used.\n",
62 	    progname);
63 }
64 
65 static void
66 terminate_cleanup(void)
67 {
68 	if (!outfile) {
69 		fprintf(stderr, "Removing %s\n", infile);
70 		unlink(infile);
71 	}
72 }
73 
74 static void
75 handle_sig(int sig)
76 {
77 	terminate("Caught signal %d - exiting\n", sig);
78 }
79 
80 static int
81 file_read(tdata_t *td, const char *filename, int ignore_non_c)
82 {
83 	typedef int (*reader_f)(tdata_t *, Elf *, const char *);
84 	static const reader_f readers[] = {
85 		stabs_read,
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(elf_errno()));
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, ".debug") >= 0)
126 			terminate("DWARF version 1 is not supported\n");
127 
128 		if (!(source_types & SOURCE_C) && ignore_non_c) {
129 			debug(1, "Ignoring file %s not built from C sources\n",
130 			    filename);
131 			exit(0);
132 		}
133 
134 		rc = 0;
135 	} else {
136 		rc = 1;
137 	}
138 
139 	(void) elf_end(elf);
140 	(void) close(fd);
141 
142 	return (rc);
143 }
144 
145 int
146 main(int argc, char **argv)
147 {
148 	tdata_t *filetd, *mstrtd;
149 	char *label = NULL;
150 	int verbose = 0;
151 	int ignore_non_c = 0;
152 	int keep_stabs = 0;
153 	int c;
154 
155 	sighold(SIGINT);
156 	sighold(SIGQUIT);
157 	sighold(SIGTERM);
158 
159 	progname = basename(argv[0]);
160 
161 	if (getenv("CTFCONVERT_DEBUG_LEVEL"))
162 		debug_level = atoi(getenv("CTFCONVERT_DEBUG_LEVEL"));
163 	if (getenv("CTFCONVERT_DEBUG_PARSE"))
164 		debug_parse = atoi(getenv("CTFCONVERT_DEBUG_PARSE"));
165 
166 	while ((c = getopt(argc, argv, ":l:L:o:givs")) != EOF) {
167 		switch (c) {
168 		case 'l':
169 			label = optarg;
170 			break;
171 		case 'L':
172 			if ((label = getenv(optarg)) == NULL)
173 				label = CTF_DEFAULT_LABEL;
174 			break;
175 		case 'o':
176 			outfile = optarg;
177 			break;
178 		case 's':
179 			dynsym = CTF_USE_DYNSYM;
180 			break;
181 		case 'i':
182 			ignore_non_c = 1;
183 			break;
184 		case 'g':
185 			keep_stabs = CTF_KEEP_STABS;
186 			break;
187 		case 'v':
188 			verbose = 1;
189 			break;
190 		default:
191 			usage();
192 			exit(2);
193 		}
194 	}
195 
196 	if (getenv("STRIPSTABS_KEEP_STABS") != NULL)
197 		keep_stabs = CTF_KEEP_STABS;
198 
199 	if (argc - optind != 1 || label == NULL) {
200 		usage();
201 		exit(2);
202 	}
203 
204 	infile = argv[optind];
205 	if (access(infile, R_OK) != 0)
206 		terminate("Can't access %s", infile);
207 
208 	/*
209 	 * Upon receipt of a signal, we want to clean up and exit.  Our
210 	 * primary goal during cleanup is to restore the system to a state
211 	 * such that a subsequent make will eventually cause this command to
212 	 * be re-run.  If we remove the input file (which we do if we get a
213 	 * signal and the user didn't specify a separate output file), make
214 	 * will need to rebuild the input file, and will then need to re-run
215 	 * ctfconvert, which is what we want.
216 	 */
217 	set_terminate_cleanup(terminate_cleanup);
218 
219 	sigset(SIGINT, handle_sig);
220 	sigset(SIGQUIT, handle_sig);
221 	sigset(SIGTERM, handle_sig);
222 
223 	filetd = tdata_new();
224 
225 	if (!file_read(filetd, infile, ignore_non_c))
226 		terminate("%s doesn't have type data to convert\n", infile);
227 
228 	if (verbose)
229 		iidesc_stats(filetd->td_iihash);
230 
231 	mstrtd = tdata_new();
232 	merge_into_master(filetd, mstrtd, NULL, 1);
233 
234 	tdata_label_add(mstrtd, label, CTF_LABEL_LASTIDX);
235 
236 	/*
237 	 * If the user supplied an output file that is different from the
238 	 * input file, write directly to the output file.  Otherwise, write
239 	 * to a temporary file, and replace the input file when we're done.
240 	 */
241 	if (outfile && strcmp(infile, outfile) != 0) {
242 		write_ctf(mstrtd, infile, outfile, dynsym | keep_stabs);
243 	} else {
244 		char *tmpname = mktmpname(infile, ".ctf");
245 
246 		write_ctf(mstrtd, infile, tmpname, dynsym | keep_stabs);
247 		if (rename(tmpname, infile) != 0)
248 			terminate("Couldn't rename temp file %s", tmpname);
249 		free(tmpname);
250 	}
251 
252 	return (0);
253 }
254