xref: /freebsd/cddl/contrib/opensolaris/tools/ctf/cvt/ctfconvert.c (revision 1673e4046da975ab0e2bf67d45499930ebab0dbe)
11673e404SJohn Birrell /*
21673e404SJohn Birrell  * CDDL HEADER START
31673e404SJohn Birrell  *
41673e404SJohn Birrell  * The contents of this file are subject to the terms of the
51673e404SJohn Birrell  * Common Development and Distribution License (the "License").
61673e404SJohn Birrell  * You may not use this file except in compliance with the License.
71673e404SJohn Birrell  *
81673e404SJohn Birrell  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
91673e404SJohn Birrell  * or http://www.opensolaris.org/os/licensing.
101673e404SJohn Birrell  * See the License for the specific language governing permissions
111673e404SJohn Birrell  * and limitations under the License.
121673e404SJohn Birrell  *
131673e404SJohn Birrell  * When distributing Covered Code, include this CDDL HEADER in each
141673e404SJohn Birrell  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
151673e404SJohn Birrell  * If applicable, add the following below this CDDL HEADER, with the
161673e404SJohn Birrell  * fields enclosed by brackets "[]" replaced with your own identifying
171673e404SJohn Birrell  * information: Portions Copyright [yyyy] [name of copyright owner]
181673e404SJohn Birrell  *
191673e404SJohn Birrell  * CDDL HEADER END
201673e404SJohn Birrell  */
211673e404SJohn Birrell /*
221673e404SJohn Birrell  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
231673e404SJohn Birrell  * Use is subject to license terms.
241673e404SJohn Birrell  */
251673e404SJohn Birrell 
261673e404SJohn Birrell #pragma ident	"%Z%%M%	%I%	%E% SMI"
271673e404SJohn Birrell 
281673e404SJohn Birrell /*
291673e404SJohn Birrell  * Given a file containing sections with stabs data, convert the stabs data to
301673e404SJohn Birrell  * CTF data, and replace the stabs sections with a CTF section.
311673e404SJohn Birrell  */
321673e404SJohn Birrell 
331673e404SJohn Birrell #include <stdio.h>
341673e404SJohn Birrell #include <stdlib.h>
351673e404SJohn Birrell #include <unistd.h>
361673e404SJohn Birrell #include <signal.h>
371673e404SJohn Birrell #include <string.h>
381673e404SJohn Birrell #include <fcntl.h>
391673e404SJohn Birrell #include <libgen.h>
401673e404SJohn Birrell #include <errno.h>
411673e404SJohn Birrell #include <assert.h>
421673e404SJohn Birrell 
431673e404SJohn Birrell #include "ctftools.h"
441673e404SJohn Birrell #include "memory.h"
451673e404SJohn Birrell 
461673e404SJohn Birrell const  char *progname;
471673e404SJohn Birrell int debug_level = DEBUG_LEVEL;
481673e404SJohn Birrell 
491673e404SJohn Birrell static char *infile = NULL;
501673e404SJohn Birrell static const char *outfile = NULL;
511673e404SJohn Birrell static int dynsym;
521673e404SJohn Birrell 
531673e404SJohn Birrell static void
541673e404SJohn Birrell usage(void)
551673e404SJohn Birrell {
561673e404SJohn Birrell 	(void) fprintf(stderr,
571673e404SJohn Birrell 	    "Usage: %s [-gis] -l label | -L labelenv [-o outfile] object_file\n"
581673e404SJohn Birrell 	    "\n"
591673e404SJohn Birrell 	    "  Note: if -L labelenv is specified and labelenv is not set in\n"
601673e404SJohn Birrell 	    "  the environment, a default value is used.\n",
611673e404SJohn Birrell 	    progname);
621673e404SJohn Birrell }
631673e404SJohn Birrell 
641673e404SJohn Birrell static void
651673e404SJohn Birrell terminate_cleanup(void)
661673e404SJohn Birrell {
671673e404SJohn Birrell #if !defined(__FreeBSD__)
681673e404SJohn Birrell 	if (!outfile) {
691673e404SJohn Birrell 		fprintf(stderr, "Removing %s\n", infile);
701673e404SJohn Birrell 		unlink(infile);
711673e404SJohn Birrell 	}
721673e404SJohn Birrell #endif
731673e404SJohn Birrell }
741673e404SJohn Birrell 
751673e404SJohn Birrell static void
761673e404SJohn Birrell handle_sig(int sig)
771673e404SJohn Birrell {
781673e404SJohn Birrell 	terminate("Caught signal %d - exiting\n", sig);
791673e404SJohn Birrell }
801673e404SJohn Birrell 
811673e404SJohn Birrell static int
821673e404SJohn Birrell file_read(tdata_t *td, char *filename, int ignore_non_c)
831673e404SJohn Birrell {
841673e404SJohn Birrell 	typedef int (*reader_f)(tdata_t *, Elf *, char *);
851673e404SJohn Birrell 	static reader_f readers[] = {
861673e404SJohn Birrell 		stabs_read,
871673e404SJohn Birrell 		dw_read,
881673e404SJohn Birrell 		NULL
891673e404SJohn Birrell 	};
901673e404SJohn Birrell 
911673e404SJohn Birrell 	source_types_t source_types;
921673e404SJohn Birrell 	Elf *elf;
931673e404SJohn Birrell 	int i, rc, fd;
941673e404SJohn Birrell 
951673e404SJohn Birrell 	if ((fd = open(filename, O_RDONLY)) < 0)
961673e404SJohn Birrell 		terminate("failed to open %s", filename);
971673e404SJohn Birrell 
981673e404SJohn Birrell 	(void) elf_version(EV_CURRENT);
991673e404SJohn Birrell 
1001673e404SJohn Birrell 	if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
1011673e404SJohn Birrell 		close(fd);
1021673e404SJohn Birrell 		terminate("failed to read %s: %s\n", filename,
1031673e404SJohn Birrell 		    elf_errmsg(-1));
1041673e404SJohn Birrell 	}
1051673e404SJohn Birrell 
1061673e404SJohn Birrell 	source_types = built_source_types(elf, filename);
1071673e404SJohn Birrell 
1081673e404SJohn Birrell 	if ((source_types == SOURCE_NONE || (source_types & SOURCE_UNKNOWN)) &&
1091673e404SJohn Birrell 	    ignore_non_c) {
1101673e404SJohn Birrell 		debug(1, "Ignoring file %s from unknown sources\n", filename);
1111673e404SJohn Birrell 		exit(0);
1121673e404SJohn Birrell 	}
1131673e404SJohn Birrell 
1141673e404SJohn Birrell 	for (i = 0; readers[i] != NULL; i++) {
1151673e404SJohn Birrell 		if ((rc = readers[i](td, elf, filename)) == 0)
1161673e404SJohn Birrell 			break;
1171673e404SJohn Birrell 
1181673e404SJohn Birrell 		assert(rc < 0 && errno == ENOENT);
1191673e404SJohn Birrell 	}
1201673e404SJohn Birrell 
1211673e404SJohn Birrell 	if (readers[i] == NULL) {
1221673e404SJohn Birrell 		/*
1231673e404SJohn Birrell 		 * None of the readers found compatible type data.
1241673e404SJohn Birrell 		 */
1251673e404SJohn Birrell 
1261673e404SJohn Birrell 		if (findelfsecidx(elf, filename, ".debug") >= 0) {
1271673e404SJohn Birrell 			terminate("%s: DWARF version 1 is not supported\n",
1281673e404SJohn Birrell 			    filename);
1291673e404SJohn Birrell 		}
1301673e404SJohn Birrell 
1311673e404SJohn Birrell 		if (!(source_types & SOURCE_C) && ignore_non_c) {
1321673e404SJohn Birrell 			debug(1, "Ignoring file %s not built from C sources\n",
1331673e404SJohn Birrell 			    filename);
1341673e404SJohn Birrell 			exit(0);
1351673e404SJohn Birrell 		}
1361673e404SJohn Birrell 
1371673e404SJohn Birrell 		rc = 0;
1381673e404SJohn Birrell 	} else {
1391673e404SJohn Birrell 		rc = 1;
1401673e404SJohn Birrell 	}
1411673e404SJohn Birrell 
1421673e404SJohn Birrell 	(void) elf_end(elf);
1431673e404SJohn Birrell 	(void) close(fd);
1441673e404SJohn Birrell 
1451673e404SJohn Birrell 	return (rc);
1461673e404SJohn Birrell }
1471673e404SJohn Birrell 
1481673e404SJohn Birrell int
1491673e404SJohn Birrell main(int argc, char **argv)
1501673e404SJohn Birrell {
1511673e404SJohn Birrell 	tdata_t *filetd, *mstrtd;
1521673e404SJohn Birrell 	const char *label = NULL;
1531673e404SJohn Birrell 	int verbose = 0;
1541673e404SJohn Birrell 	int ignore_non_c = 0;
1551673e404SJohn Birrell 	int keep_stabs = 0;
1561673e404SJohn Birrell 	int c;
1571673e404SJohn Birrell 
1581673e404SJohn Birrell #if defined(sun)
1591673e404SJohn Birrell 	sighold(SIGINT);
1601673e404SJohn Birrell 	sighold(SIGQUIT);
1611673e404SJohn Birrell 	sighold(SIGTERM);
1621673e404SJohn Birrell #endif
1631673e404SJohn Birrell 
1641673e404SJohn Birrell 	progname = basename(argv[0]);
1651673e404SJohn Birrell 
1661673e404SJohn Birrell 	if (getenv("CTFCONVERT_DEBUG_LEVEL"))
1671673e404SJohn Birrell 		debug_level = atoi(getenv("CTFCONVERT_DEBUG_LEVEL"));
1681673e404SJohn Birrell 	if (getenv("CTFCONVERT_DEBUG_PARSE"))
1691673e404SJohn Birrell 		debug_parse = atoi(getenv("CTFCONVERT_DEBUG_PARSE"));
1701673e404SJohn Birrell 
1711673e404SJohn Birrell 	while ((c = getopt(argc, argv, ":l:L:o:givs")) != EOF) {
1721673e404SJohn Birrell 		switch (c) {
1731673e404SJohn Birrell 		case 'l':
1741673e404SJohn Birrell 			label = optarg;
1751673e404SJohn Birrell 			break;
1761673e404SJohn Birrell 		case 'L':
1771673e404SJohn Birrell 			if ((label = getenv(optarg)) == NULL)
1781673e404SJohn Birrell 				label = CTF_DEFAULT_LABEL;
1791673e404SJohn Birrell 			break;
1801673e404SJohn Birrell 		case 'o':
1811673e404SJohn Birrell 			outfile = optarg;
1821673e404SJohn Birrell 			break;
1831673e404SJohn Birrell 		case 's':
1841673e404SJohn Birrell 			dynsym = CTF_USE_DYNSYM;
1851673e404SJohn Birrell 			break;
1861673e404SJohn Birrell 		case 'i':
1871673e404SJohn Birrell 			ignore_non_c = 1;
1881673e404SJohn Birrell 			break;
1891673e404SJohn Birrell 		case 'g':
1901673e404SJohn Birrell 			keep_stabs = CTF_KEEP_STABS;
1911673e404SJohn Birrell 			break;
1921673e404SJohn Birrell 		case 'v':
1931673e404SJohn Birrell 			verbose = 1;
1941673e404SJohn Birrell 			break;
1951673e404SJohn Birrell 		default:
1961673e404SJohn Birrell 			usage();
1971673e404SJohn Birrell 			exit(2);
1981673e404SJohn Birrell 		}
1991673e404SJohn Birrell 	}
2001673e404SJohn Birrell 
2011673e404SJohn Birrell 	if (getenv("STRIPSTABS_KEEP_STABS") != NULL)
2021673e404SJohn Birrell 		keep_stabs = CTF_KEEP_STABS;
2031673e404SJohn Birrell 
2041673e404SJohn Birrell 	if (argc - optind != 1 || label == NULL) {
2051673e404SJohn Birrell 		usage();
2061673e404SJohn Birrell 		exit(2);
2071673e404SJohn Birrell 	}
2081673e404SJohn Birrell 
2091673e404SJohn Birrell 	infile = argv[optind];
2101673e404SJohn Birrell 	if (access(infile, R_OK) != 0)
2111673e404SJohn Birrell 		terminate("Can't access %s", infile);
2121673e404SJohn Birrell 
2131673e404SJohn Birrell 	/*
2141673e404SJohn Birrell 	 * Upon receipt of a signal, we want to clean up and exit.  Our
2151673e404SJohn Birrell 	 * primary goal during cleanup is to restore the system to a state
2161673e404SJohn Birrell 	 * such that a subsequent make will eventually cause this command to
2171673e404SJohn Birrell 	 * be re-run.  If we remove the input file (which we do if we get a
2181673e404SJohn Birrell 	 * signal and the user didn't specify a separate output file), make
2191673e404SJohn Birrell 	 * will need to rebuild the input file, and will then need to re-run
2201673e404SJohn Birrell 	 * ctfconvert, which is what we want.
2211673e404SJohn Birrell 	 */
2221673e404SJohn Birrell 	set_terminate_cleanup(terminate_cleanup);
2231673e404SJohn Birrell 
2241673e404SJohn Birrell #if defined(sun)
2251673e404SJohn Birrell 	sigset(SIGINT, handle_sig);
2261673e404SJohn Birrell 	sigset(SIGQUIT, handle_sig);
2271673e404SJohn Birrell 	sigset(SIGTERM, handle_sig);
2281673e404SJohn Birrell #else
2291673e404SJohn Birrell 	signal(SIGINT, handle_sig);
2301673e404SJohn Birrell 	signal(SIGQUIT, handle_sig);
2311673e404SJohn Birrell 	signal(SIGTERM, handle_sig);
2321673e404SJohn Birrell #endif
2331673e404SJohn Birrell 
2341673e404SJohn Birrell 	filetd = tdata_new();
2351673e404SJohn Birrell 
2361673e404SJohn Birrell 	if (!file_read(filetd, infile, ignore_non_c))
2371673e404SJohn Birrell 		terminate("%s doesn't have type data to convert\n", infile);
2381673e404SJohn Birrell 
2391673e404SJohn Birrell 	if (verbose)
2401673e404SJohn Birrell 		iidesc_stats(filetd->td_iihash);
2411673e404SJohn Birrell 
2421673e404SJohn Birrell 	mstrtd = tdata_new();
2431673e404SJohn Birrell 	merge_into_master(filetd, mstrtd, NULL, 1);
2441673e404SJohn Birrell 
2451673e404SJohn Birrell 	tdata_label_add(mstrtd, label, CTF_LABEL_LASTIDX);
2461673e404SJohn Birrell 
2471673e404SJohn Birrell 	/*
2481673e404SJohn Birrell 	 * If the user supplied an output file that is different from the
2491673e404SJohn Birrell 	 * input file, write directly to the output file.  Otherwise, write
2501673e404SJohn Birrell 	 * to a temporary file, and replace the input file when we're done.
2511673e404SJohn Birrell 	 */
2521673e404SJohn Birrell 	if (outfile && strcmp(infile, outfile) != 0) {
2531673e404SJohn Birrell 		write_ctf(mstrtd, infile, outfile, dynsym | keep_stabs);
2541673e404SJohn Birrell 	} else {
2551673e404SJohn Birrell 		char *tmpname = mktmpname(infile, ".ctf");
2561673e404SJohn Birrell 		write_ctf(mstrtd, infile, tmpname, dynsym | keep_stabs);
2571673e404SJohn Birrell 		if (rename(tmpname, infile) != 0)
2581673e404SJohn Birrell 			terminate("Couldn't rename temp file %s", tmpname);
2591673e404SJohn Birrell 		free(tmpname);
2601673e404SJohn Birrell 	}
2611673e404SJohn Birrell 
2621673e404SJohn Birrell 	return (0);
2631673e404SJohn Birrell }
264