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
494cc75139SJohn Birrell static char *infile = NULL;
501673e404SJohn Birrell static const char *outfile = NULL;
511673e404SJohn Birrell static int dynsym;
521673e404SJohn Birrell
531673e404SJohn Birrell static void
usage(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
terminate_cleanup(void)651673e404SJohn Birrell terminate_cleanup(void)
661673e404SJohn Birrell {
674cc75139SJohn Birrell #if !defined(__FreeBSD__)
681673e404SJohn Birrell if (!outfile) {
691673e404SJohn Birrell fprintf(stderr, "Removing %s\n", infile);
701673e404SJohn Birrell unlink(infile);
711673e404SJohn Birrell }
724cc75139SJohn Birrell #endif
731673e404SJohn Birrell }
741673e404SJohn Birrell
751673e404SJohn Birrell static void
handle_sig(int sig)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
file_read(tdata_t * td,char * filename,int ignore_non_c)824cc75139SJohn Birrell file_read(tdata_t *td, char *filename, int ignore_non_c)
831673e404SJohn Birrell {
844cc75139SJohn Birrell typedef int (*reader_f)(tdata_t *, Elf *, char *);
854cc75139SJohn Birrell static reader_f readers[] = {
861673e404SJohn Birrell dw_read,
871673e404SJohn Birrell NULL
881673e404SJohn Birrell };
891673e404SJohn Birrell
901673e404SJohn Birrell source_types_t source_types;
911673e404SJohn Birrell Elf *elf;
921673e404SJohn Birrell int i, rc, fd;
931673e404SJohn Birrell
941673e404SJohn Birrell if ((fd = open(filename, O_RDONLY)) < 0)
951673e404SJohn Birrell terminate("failed to open %s", filename);
961673e404SJohn Birrell
971673e404SJohn Birrell (void) elf_version(EV_CURRENT);
981673e404SJohn Birrell
991673e404SJohn Birrell if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
1001673e404SJohn Birrell close(fd);
1011673e404SJohn Birrell terminate("failed to read %s: %s\n", filename,
1021673e404SJohn Birrell elf_errmsg(-1));
1031673e404SJohn Birrell }
1041673e404SJohn Birrell
1051673e404SJohn Birrell source_types = built_source_types(elf, filename);
1061673e404SJohn Birrell
1071673e404SJohn Birrell if ((source_types == SOURCE_NONE || (source_types & SOURCE_UNKNOWN)) &&
1081673e404SJohn Birrell ignore_non_c) {
1091673e404SJohn Birrell debug(1, "Ignoring file %s from unknown sources\n", filename);
1101673e404SJohn Birrell exit(0);
1111673e404SJohn Birrell }
1121673e404SJohn Birrell
1131673e404SJohn Birrell for (i = 0; readers[i] != NULL; i++) {
1141673e404SJohn Birrell if ((rc = readers[i](td, elf, filename)) == 0)
1151673e404SJohn Birrell break;
1161673e404SJohn Birrell
1171673e404SJohn Birrell assert(rc < 0 && errno == ENOENT);
1181673e404SJohn Birrell }
1191673e404SJohn Birrell
1201673e404SJohn Birrell if (readers[i] == NULL) {
1211673e404SJohn Birrell /*
1221673e404SJohn Birrell * None of the readers found compatible type data.
1231673e404SJohn Birrell */
1241673e404SJohn Birrell
1251673e404SJohn Birrell if (findelfsecidx(elf, filename, ".debug") >= 0) {
1261673e404SJohn Birrell terminate("%s: DWARF version 1 is not supported\n",
1271673e404SJohn Birrell filename);
1281673e404SJohn Birrell }
1291673e404SJohn Birrell
1301673e404SJohn Birrell if (!(source_types & SOURCE_C) && ignore_non_c) {
1311673e404SJohn Birrell debug(1, "Ignoring file %s not built from C sources\n",
1321673e404SJohn Birrell filename);
1331673e404SJohn Birrell exit(0);
1341673e404SJohn Birrell }
1351673e404SJohn Birrell
1361673e404SJohn Birrell rc = 0;
1371673e404SJohn Birrell } else {
1381673e404SJohn Birrell rc = 1;
1391673e404SJohn Birrell }
1401673e404SJohn Birrell
1411673e404SJohn Birrell (void) elf_end(elf);
1421673e404SJohn Birrell (void) close(fd);
1431673e404SJohn Birrell
1441673e404SJohn Birrell return (rc);
1451673e404SJohn Birrell }
1461673e404SJohn Birrell
1471673e404SJohn Birrell int
main(int argc,char ** argv)1481673e404SJohn Birrell main(int argc, char **argv)
1491673e404SJohn Birrell {
1501673e404SJohn Birrell tdata_t *filetd, *mstrtd;
1514cc75139SJohn Birrell const char *label = NULL;
1521673e404SJohn Birrell int verbose = 0;
1531673e404SJohn Birrell int ignore_non_c = 0;
1541673e404SJohn Birrell int keep_stabs = 0;
1551673e404SJohn Birrell int c;
1561673e404SJohn Birrell
157*bc96366cSSteven Hartland #ifdef illumos
1581673e404SJohn Birrell sighold(SIGINT);
1591673e404SJohn Birrell sighold(SIGQUIT);
1601673e404SJohn Birrell sighold(SIGTERM);
1614cc75139SJohn Birrell #endif
1621673e404SJohn Birrell
1631673e404SJohn Birrell progname = basename(argv[0]);
1641673e404SJohn Birrell
1651673e404SJohn Birrell if (getenv("CTFCONVERT_DEBUG_LEVEL"))
1661673e404SJohn Birrell debug_level = atoi(getenv("CTFCONVERT_DEBUG_LEVEL"));
1671673e404SJohn Birrell
1681673e404SJohn Birrell while ((c = getopt(argc, argv, ":l:L:o:givs")) != EOF) {
1691673e404SJohn Birrell switch (c) {
1701673e404SJohn Birrell case 'l':
1711673e404SJohn Birrell label = optarg;
1721673e404SJohn Birrell break;
1731673e404SJohn Birrell case 'L':
1741673e404SJohn Birrell if ((label = getenv(optarg)) == NULL)
1751673e404SJohn Birrell label = CTF_DEFAULT_LABEL;
1761673e404SJohn Birrell break;
1771673e404SJohn Birrell case 'o':
1781673e404SJohn Birrell outfile = optarg;
1791673e404SJohn Birrell break;
1801673e404SJohn Birrell case 's':
1811673e404SJohn Birrell dynsym = CTF_USE_DYNSYM;
1821673e404SJohn Birrell break;
1831673e404SJohn Birrell case 'i':
1841673e404SJohn Birrell ignore_non_c = 1;
1851673e404SJohn Birrell break;
1861673e404SJohn Birrell case 'g':
1871673e404SJohn Birrell keep_stabs = CTF_KEEP_STABS;
1881673e404SJohn Birrell break;
1891673e404SJohn Birrell case 'v':
1901673e404SJohn Birrell verbose = 1;
1911673e404SJohn Birrell break;
1921673e404SJohn Birrell default:
1931673e404SJohn Birrell usage();
1941673e404SJohn Birrell exit(2);
1951673e404SJohn Birrell }
1961673e404SJohn Birrell }
1971673e404SJohn Birrell
1981673e404SJohn Birrell if (getenv("STRIPSTABS_KEEP_STABS") != NULL)
1991673e404SJohn Birrell keep_stabs = CTF_KEEP_STABS;
2001673e404SJohn Birrell
2011673e404SJohn Birrell if (argc - optind != 1 || label == NULL) {
2021673e404SJohn Birrell usage();
2031673e404SJohn Birrell exit(2);
2041673e404SJohn Birrell }
2051673e404SJohn Birrell
2061673e404SJohn Birrell infile = argv[optind];
2071673e404SJohn Birrell if (access(infile, R_OK) != 0)
2081673e404SJohn Birrell terminate("Can't access %s", infile);
2091673e404SJohn Birrell
2101673e404SJohn Birrell /*
2111673e404SJohn Birrell * Upon receipt of a signal, we want to clean up and exit. Our
2121673e404SJohn Birrell * primary goal during cleanup is to restore the system to a state
2131673e404SJohn Birrell * such that a subsequent make will eventually cause this command to
2141673e404SJohn Birrell * be re-run. If we remove the input file (which we do if we get a
2151673e404SJohn Birrell * signal and the user didn't specify a separate output file), make
2161673e404SJohn Birrell * will need to rebuild the input file, and will then need to re-run
2171673e404SJohn Birrell * ctfconvert, which is what we want.
2181673e404SJohn Birrell */
2191673e404SJohn Birrell set_terminate_cleanup(terminate_cleanup);
2201673e404SJohn Birrell
221*bc96366cSSteven Hartland #ifdef illumos
2221673e404SJohn Birrell sigset(SIGINT, handle_sig);
2231673e404SJohn Birrell sigset(SIGQUIT, handle_sig);
2241673e404SJohn Birrell sigset(SIGTERM, handle_sig);
2254cc75139SJohn Birrell #else
2264cc75139SJohn Birrell signal(SIGINT, handle_sig);
2274cc75139SJohn Birrell signal(SIGQUIT, handle_sig);
2284cc75139SJohn Birrell signal(SIGTERM, handle_sig);
2294cc75139SJohn Birrell #endif
2301673e404SJohn Birrell
2311673e404SJohn Birrell filetd = tdata_new();
2321673e404SJohn Birrell
2331673e404SJohn Birrell if (!file_read(filetd, infile, ignore_non_c))
2341673e404SJohn Birrell terminate("%s doesn't have type data to convert\n", infile);
2351673e404SJohn Birrell
2361673e404SJohn Birrell if (verbose)
2371673e404SJohn Birrell iidesc_stats(filetd->td_iihash);
2381673e404SJohn Birrell
2391673e404SJohn Birrell mstrtd = tdata_new();
2401673e404SJohn Birrell merge_into_master(filetd, mstrtd, NULL, 1);
2411673e404SJohn Birrell
2421673e404SJohn Birrell tdata_label_add(mstrtd, label, CTF_LABEL_LASTIDX);
2431673e404SJohn Birrell
2441673e404SJohn Birrell /*
2451673e404SJohn Birrell * If the user supplied an output file that is different from the
2461673e404SJohn Birrell * input file, write directly to the output file. Otherwise, write
2471673e404SJohn Birrell * to a temporary file, and replace the input file when we're done.
2481673e404SJohn Birrell */
2491673e404SJohn Birrell if (outfile && strcmp(infile, outfile) != 0) {
2501673e404SJohn Birrell write_ctf(mstrtd, infile, outfile, dynsym | keep_stabs);
2511673e404SJohn Birrell } else {
2521673e404SJohn Birrell char *tmpname = mktmpname(infile, ".ctf");
2531673e404SJohn Birrell write_ctf(mstrtd, infile, tmpname, dynsym | keep_stabs);
2541673e404SJohn Birrell if (rename(tmpname, infile) != 0)
2551673e404SJohn Birrell terminate("Couldn't rename temp file %s", tmpname);
2561673e404SJohn Birrell free(tmpname);
2571673e404SJohn Birrell }
2581673e404SJohn Birrell
2591673e404SJohn Birrell return (0);
2601673e404SJohn Birrell }
261