10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*1951Sjohnlev * Common Development and Distribution License (the "License").
6*1951Sjohnlev * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*1951Sjohnlev * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
270Sstevel@tonic-gate
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate * Given a file containing sections with stabs data, convert the stabs data to
300Sstevel@tonic-gate * CTF data, and replace the stabs sections with a CTF section.
310Sstevel@tonic-gate */
320Sstevel@tonic-gate
330Sstevel@tonic-gate #include <stdio.h>
340Sstevel@tonic-gate #include <stdlib.h>
350Sstevel@tonic-gate #include <unistd.h>
360Sstevel@tonic-gate #include <signal.h>
370Sstevel@tonic-gate #include <string.h>
380Sstevel@tonic-gate #include <fcntl.h>
390Sstevel@tonic-gate #include <libgen.h>
400Sstevel@tonic-gate #include <errno.h>
410Sstevel@tonic-gate #include <assert.h>
420Sstevel@tonic-gate
430Sstevel@tonic-gate #include "ctftools.h"
440Sstevel@tonic-gate #include "memory.h"
450Sstevel@tonic-gate
460Sstevel@tonic-gate const char *progname;
470Sstevel@tonic-gate int debug_level = DEBUG_LEVEL;
480Sstevel@tonic-gate
490Sstevel@tonic-gate static const char *infile = NULL;
500Sstevel@tonic-gate static const char *outfile = NULL;
510Sstevel@tonic-gate static int dynsym;
520Sstevel@tonic-gate
530Sstevel@tonic-gate static void
usage(void)540Sstevel@tonic-gate usage(void)
550Sstevel@tonic-gate {
560Sstevel@tonic-gate (void) fprintf(stderr,
570Sstevel@tonic-gate "Usage: %s [-gis] -l label | -L labelenv [-o outfile] object_file\n"
580Sstevel@tonic-gate "\n"
590Sstevel@tonic-gate " Note: if -L labelenv is specified and labelenv is not set in\n"
600Sstevel@tonic-gate " the environment, a default value is used.\n",
610Sstevel@tonic-gate progname);
620Sstevel@tonic-gate }
630Sstevel@tonic-gate
640Sstevel@tonic-gate static void
terminate_cleanup(void)650Sstevel@tonic-gate terminate_cleanup(void)
660Sstevel@tonic-gate {
670Sstevel@tonic-gate if (!outfile) {
680Sstevel@tonic-gate fprintf(stderr, "Removing %s\n", infile);
690Sstevel@tonic-gate unlink(infile);
700Sstevel@tonic-gate }
710Sstevel@tonic-gate }
720Sstevel@tonic-gate
730Sstevel@tonic-gate static void
handle_sig(int sig)740Sstevel@tonic-gate handle_sig(int sig)
750Sstevel@tonic-gate {
760Sstevel@tonic-gate terminate("Caught signal %d - exiting\n", sig);
770Sstevel@tonic-gate }
780Sstevel@tonic-gate
790Sstevel@tonic-gate static int
file_read(tdata_t * td,const char * filename,int ignore_non_c)800Sstevel@tonic-gate file_read(tdata_t *td, const char *filename, int ignore_non_c)
810Sstevel@tonic-gate {
820Sstevel@tonic-gate typedef int (*reader_f)(tdata_t *, Elf *, const char *);
830Sstevel@tonic-gate static const reader_f readers[] = {
840Sstevel@tonic-gate stabs_read,
850Sstevel@tonic-gate dw_read,
860Sstevel@tonic-gate NULL
870Sstevel@tonic-gate };
880Sstevel@tonic-gate
890Sstevel@tonic-gate source_types_t source_types;
900Sstevel@tonic-gate Elf *elf;
910Sstevel@tonic-gate int i, rc, fd;
920Sstevel@tonic-gate
930Sstevel@tonic-gate if ((fd = open(filename, O_RDONLY)) < 0)
940Sstevel@tonic-gate terminate("failed to open %s", filename);
950Sstevel@tonic-gate
960Sstevel@tonic-gate (void) elf_version(EV_CURRENT);
970Sstevel@tonic-gate
980Sstevel@tonic-gate if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
990Sstevel@tonic-gate close(fd);
1000Sstevel@tonic-gate terminate("failed to read %s: %s\n", filename,
101*1951Sjohnlev elf_errmsg(-1));
1020Sstevel@tonic-gate }
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate source_types = built_source_types(elf, filename);
1050Sstevel@tonic-gate
1060Sstevel@tonic-gate if ((source_types == SOURCE_NONE || (source_types & SOURCE_UNKNOWN)) &&
1070Sstevel@tonic-gate ignore_non_c) {
1080Sstevel@tonic-gate debug(1, "Ignoring file %s from unknown sources\n", filename);
1090Sstevel@tonic-gate exit(0);
1100Sstevel@tonic-gate }
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate for (i = 0; readers[i] != NULL; i++) {
1130Sstevel@tonic-gate if ((rc = readers[i](td, elf, filename)) == 0)
1140Sstevel@tonic-gate break;
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate assert(rc < 0 && errno == ENOENT);
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate if (readers[i] == NULL) {
1200Sstevel@tonic-gate /*
1210Sstevel@tonic-gate * None of the readers found compatible type data.
1220Sstevel@tonic-gate */
1230Sstevel@tonic-gate
124*1951Sjohnlev if (findelfsecidx(elf, filename, ".debug") >= 0) {
125*1951Sjohnlev terminate("%s: DWARF version 1 is not supported\n",
126*1951Sjohnlev filename);
127*1951Sjohnlev }
1280Sstevel@tonic-gate
1290Sstevel@tonic-gate if (!(source_types & SOURCE_C) && ignore_non_c) {
1300Sstevel@tonic-gate debug(1, "Ignoring file %s not built from C sources\n",
1310Sstevel@tonic-gate filename);
1320Sstevel@tonic-gate exit(0);
1330Sstevel@tonic-gate }
1340Sstevel@tonic-gate
1350Sstevel@tonic-gate rc = 0;
1360Sstevel@tonic-gate } else {
1370Sstevel@tonic-gate rc = 1;
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate
1400Sstevel@tonic-gate (void) elf_end(elf);
1410Sstevel@tonic-gate (void) close(fd);
1420Sstevel@tonic-gate
1430Sstevel@tonic-gate return (rc);
1440Sstevel@tonic-gate }
1450Sstevel@tonic-gate
1460Sstevel@tonic-gate int
main(int argc,char ** argv)1470Sstevel@tonic-gate main(int argc, char **argv)
1480Sstevel@tonic-gate {
1490Sstevel@tonic-gate tdata_t *filetd, *mstrtd;
1500Sstevel@tonic-gate char *label = NULL;
1510Sstevel@tonic-gate int verbose = 0;
1520Sstevel@tonic-gate int ignore_non_c = 0;
1530Sstevel@tonic-gate int keep_stabs = 0;
1540Sstevel@tonic-gate int c;
1550Sstevel@tonic-gate
1560Sstevel@tonic-gate sighold(SIGINT);
1570Sstevel@tonic-gate sighold(SIGQUIT);
1580Sstevel@tonic-gate sighold(SIGTERM);
1590Sstevel@tonic-gate
1600Sstevel@tonic-gate progname = basename(argv[0]);
1610Sstevel@tonic-gate
1620Sstevel@tonic-gate if (getenv("CTFCONVERT_DEBUG_LEVEL"))
1630Sstevel@tonic-gate debug_level = atoi(getenv("CTFCONVERT_DEBUG_LEVEL"));
1640Sstevel@tonic-gate if (getenv("CTFCONVERT_DEBUG_PARSE"))
1650Sstevel@tonic-gate debug_parse = atoi(getenv("CTFCONVERT_DEBUG_PARSE"));
1660Sstevel@tonic-gate
1670Sstevel@tonic-gate while ((c = getopt(argc, argv, ":l:L:o:givs")) != EOF) {
1680Sstevel@tonic-gate switch (c) {
1690Sstevel@tonic-gate case 'l':
1700Sstevel@tonic-gate label = optarg;
1710Sstevel@tonic-gate break;
1720Sstevel@tonic-gate case 'L':
1730Sstevel@tonic-gate if ((label = getenv(optarg)) == NULL)
1740Sstevel@tonic-gate label = CTF_DEFAULT_LABEL;
1750Sstevel@tonic-gate break;
1760Sstevel@tonic-gate case 'o':
1770Sstevel@tonic-gate outfile = optarg;
1780Sstevel@tonic-gate break;
1790Sstevel@tonic-gate case 's':
1800Sstevel@tonic-gate dynsym = CTF_USE_DYNSYM;
1810Sstevel@tonic-gate break;
1820Sstevel@tonic-gate case 'i':
1830Sstevel@tonic-gate ignore_non_c = 1;
1840Sstevel@tonic-gate break;
1850Sstevel@tonic-gate case 'g':
1860Sstevel@tonic-gate keep_stabs = CTF_KEEP_STABS;
1870Sstevel@tonic-gate break;
1880Sstevel@tonic-gate case 'v':
1890Sstevel@tonic-gate verbose = 1;
1900Sstevel@tonic-gate break;
1910Sstevel@tonic-gate default:
1920Sstevel@tonic-gate usage();
1930Sstevel@tonic-gate exit(2);
1940Sstevel@tonic-gate }
1950Sstevel@tonic-gate }
1960Sstevel@tonic-gate
1970Sstevel@tonic-gate if (getenv("STRIPSTABS_KEEP_STABS") != NULL)
1980Sstevel@tonic-gate keep_stabs = CTF_KEEP_STABS;
1990Sstevel@tonic-gate
2000Sstevel@tonic-gate if (argc - optind != 1 || label == NULL) {
2010Sstevel@tonic-gate usage();
2020Sstevel@tonic-gate exit(2);
2030Sstevel@tonic-gate }
2040Sstevel@tonic-gate
2050Sstevel@tonic-gate infile = argv[optind];
2060Sstevel@tonic-gate if (access(infile, R_OK) != 0)
2070Sstevel@tonic-gate terminate("Can't access %s", infile);
2080Sstevel@tonic-gate
2090Sstevel@tonic-gate /*
2100Sstevel@tonic-gate * Upon receipt of a signal, we want to clean up and exit. Our
2110Sstevel@tonic-gate * primary goal during cleanup is to restore the system to a state
2120Sstevel@tonic-gate * such that a subsequent make will eventually cause this command to
2130Sstevel@tonic-gate * be re-run. If we remove the input file (which we do if we get a
2140Sstevel@tonic-gate * signal and the user didn't specify a separate output file), make
2150Sstevel@tonic-gate * will need to rebuild the input file, and will then need to re-run
2160Sstevel@tonic-gate * ctfconvert, which is what we want.
2170Sstevel@tonic-gate */
2180Sstevel@tonic-gate set_terminate_cleanup(terminate_cleanup);
2190Sstevel@tonic-gate
2200Sstevel@tonic-gate sigset(SIGINT, handle_sig);
2210Sstevel@tonic-gate sigset(SIGQUIT, handle_sig);
2220Sstevel@tonic-gate sigset(SIGTERM, handle_sig);
2230Sstevel@tonic-gate
2240Sstevel@tonic-gate filetd = tdata_new();
2250Sstevel@tonic-gate
2260Sstevel@tonic-gate if (!file_read(filetd, infile, ignore_non_c))
2270Sstevel@tonic-gate terminate("%s doesn't have type data to convert\n", infile);
2280Sstevel@tonic-gate
2290Sstevel@tonic-gate if (verbose)
2300Sstevel@tonic-gate iidesc_stats(filetd->td_iihash);
2310Sstevel@tonic-gate
2320Sstevel@tonic-gate mstrtd = tdata_new();
2330Sstevel@tonic-gate merge_into_master(filetd, mstrtd, NULL, 1);
2340Sstevel@tonic-gate
2350Sstevel@tonic-gate tdata_label_add(mstrtd, label, CTF_LABEL_LASTIDX);
2360Sstevel@tonic-gate
2370Sstevel@tonic-gate /*
2380Sstevel@tonic-gate * If the user supplied an output file that is different from the
2390Sstevel@tonic-gate * input file, write directly to the output file. Otherwise, write
2400Sstevel@tonic-gate * to a temporary file, and replace the input file when we're done.
2410Sstevel@tonic-gate */
2420Sstevel@tonic-gate if (outfile && strcmp(infile, outfile) != 0) {
2430Sstevel@tonic-gate write_ctf(mstrtd, infile, outfile, dynsym | keep_stabs);
2440Sstevel@tonic-gate } else {
2450Sstevel@tonic-gate char *tmpname = mktmpname(infile, ".ctf");
2460Sstevel@tonic-gate
2470Sstevel@tonic-gate write_ctf(mstrtd, infile, tmpname, dynsym | keep_stabs);
2480Sstevel@tonic-gate if (rename(tmpname, infile) != 0)
2490Sstevel@tonic-gate terminate("Couldn't rename temp file %s", tmpname);
2500Sstevel@tonic-gate free(tmpname);
2510Sstevel@tonic-gate }
2520Sstevel@tonic-gate
2530Sstevel@tonic-gate return (0);
2540Sstevel@tonic-gate }
255