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.
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.
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]
22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
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.
47 int debug_level = DEBUG_LEVEL;
49 static char *infile = NULL;
50 static const char *outfile = NULL;
56 (void) fprintf(stderr,
57 "Usage: %s [-gis] -l label | -L labelenv [-o outfile] object_file\n"
59 " Note: if -L labelenv is specified and labelenv is not set in\n"
60 " the environment, a default value is used.\n",
65 terminate_cleanup(void)
67 #if !defined(__FreeBSD__)
69 fprintf(stderr, "Removing %s\n", infile);
78 terminate("Caught signal %d - exiting\n", sig);
82 file_read(tdata_t *td, char *filename, int ignore_non_c)
84 typedef int (*reader_f)(tdata_t *, Elf *, char *);
85 static reader_f readers[] = {
91 source_types_t source_types;
95 if ((fd = open(filename, O_RDONLY)) < 0)
96 terminate("failed to open %s", filename);
98 (void) elf_version(EV_CURRENT);
100 if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
102 terminate("failed to read %s: %s\n", filename,
106 source_types = built_source_types(elf, filename);
108 if ((source_types == SOURCE_NONE || (source_types & SOURCE_UNKNOWN)) &&
110 debug(1, "Ignoring file %s from unknown sources\n", filename);
114 for (i = 0; readers[i] != NULL; i++) {
115 if ((rc = readers[i](td, elf, filename)) == 0)
118 assert(rc < 0 && errno == ENOENT);
121 if (readers[i] == NULL) {
123 * None of the readers found compatible type data.
126 if (findelfsecidx(elf, filename, ".debug") >= 0) {
127 terminate("%s: DWARF version 1 is not supported\n",
131 if (!(source_types & SOURCE_C) && ignore_non_c) {
132 debug(1, "Ignoring file %s not built from C sources\n",
149 main(int argc, char **argv)
151 tdata_t *filetd, *mstrtd;
152 const char *label = NULL;
154 int ignore_non_c = 0;
164 progname = basename(argv[0]);
166 if (getenv("CTFCONVERT_DEBUG_LEVEL"))
167 debug_level = atoi(getenv("CTFCONVERT_DEBUG_LEVEL"));
168 if (getenv("CTFCONVERT_DEBUG_PARSE"))
169 debug_parse = atoi(getenv("CTFCONVERT_DEBUG_PARSE"));
171 while ((c = getopt(argc, argv, ":l:L:o:givs")) != EOF) {
177 if ((label = getenv(optarg)) == NULL)
178 label = CTF_DEFAULT_LABEL;
184 dynsym = CTF_USE_DYNSYM;
190 keep_stabs = CTF_KEEP_STABS;
201 if (getenv("STRIPSTABS_KEEP_STABS") != NULL)
202 keep_stabs = CTF_KEEP_STABS;
204 if (argc - optind != 1 || label == NULL) {
209 infile = argv[optind];
210 if (access(infile, R_OK) != 0)
211 terminate("Can't access %s", infile);
214 * Upon receipt of a signal, we want to clean up and exit. Our
215 * primary goal during cleanup is to restore the system to a state
216 * such that a subsequent make will eventually cause this command to
217 * be re-run. If we remove the input file (which we do if we get a
218 * signal and the user didn't specify a separate output file), make
219 * will need to rebuild the input file, and will then need to re-run
220 * ctfconvert, which is what we want.
222 set_terminate_cleanup(terminate_cleanup);
225 sigset(SIGINT, handle_sig);
226 sigset(SIGQUIT, handle_sig);
227 sigset(SIGTERM, handle_sig);
229 signal(SIGINT, handle_sig);
230 signal(SIGQUIT, handle_sig);
231 signal(SIGTERM, handle_sig);
234 filetd = tdata_new();
236 if (!file_read(filetd, infile, ignore_non_c))
237 terminate("%s doesn't have type data to convert\n", infile);
240 iidesc_stats(filetd->td_iihash);
242 mstrtd = tdata_new();
243 merge_into_master(filetd, mstrtd, NULL, 1);
245 tdata_label_add(mstrtd, label, CTF_LABEL_LASTIDX);
248 * If the user supplied an output file that is different from the
249 * input file, write directly to the output file. Otherwise, write
250 * to a temporary file, and replace the input file when we're done.
252 if (outfile && strcmp(infile, outfile) != 0) {
253 write_ctf(mstrtd, infile, outfile, dynsym | keep_stabs);
255 char *tmpname = mktmpname(infile, ".ctf");
256 write_ctf(mstrtd, infile, tmpname, dynsym | keep_stabs);
257 if (rename(tmpname, infile) != 0)
258 terminate("Couldn't rename temp file %s", tmpname);