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 (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 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. 12 * 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] 18 * 19 * CDDL HEADER END 20 */ 21/* 22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26#pragma ident "%Z%%M% %I% %E% SMI" 27 28/* 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. 31 */ 32 33#if HAVE_NBTOOL_CONFIG_H 34# include "nbtool_config.h" 35#endif 36 37#include <stdio.h> 38#include <stdlib.h> 39#include <unistd.h> 40#include <signal.h> 41#include <string.h> 42#include <fcntl.h> 43#include <libgen.h> 44#include <errno.h> 45#include <assert.h> 46 47#include "ctftools.h" 48#include "memory.h" 49 50const char *progname; 51int debug_level = DEBUG_LEVEL; 52 53static char *infile = NULL; 54static const char *outfile = NULL; 55static int dynsym; 56 57static void 58usage(void) 59{ 60 (void) fprintf(stderr, 61 "Usage: %s [-gis] -l label | -L labelenv [-o outfile] object_file\n" 62 "\n" 63 " Note: if -L labelenv is specified and labelenv is not set in\n" 64 " the environment, a default value is used.\n", 65 progname); 66} 67 68static void 69terminate_cleanup(void) 70{ 71#if !defined(__FreeBSD__) 72 if (!outfile) { 73 fprintf(stderr, "Removing %s\n", infile); 74 unlink(infile); 75 } 76#endif 77} 78 79static void 80handle_sig(int sig) 81{ 82 terminate("Caught signal %d - exiting\n", sig); 83} 84 85static int 86file_read(tdata_t *td, char *filename, int ignore_non_c) 87{ 88 typedef int (*reader_f)(tdata_t *, Elf *, char *); 89 static reader_f readers[] = { 90 stabs_read, 91 dw_read, 92 NULL 93 }; 94 95 source_types_t source_types; 96 Elf *elf; 97 int i, rc, fd; 98 99 if ((fd = open(filename, O_RDONLY)) < 0) 100 terminate("failed to open %s", filename); 101 102 (void) elf_version(EV_CURRENT); 103 104 if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) { 105 close(fd); 106 terminate("failed to read %s: %s\n", filename, 107 elf_errmsg(-1)); 108 } 109 110 source_types = built_source_types(elf, filename); 111 112 if ((source_types == SOURCE_NONE || (source_types & SOURCE_UNKNOWN)) && 113 ignore_non_c) { 114 debug(1, "Ignoring file %s from unknown sources\n", filename); 115 exit(0); 116 } 117 118 for (i = 0; readers[i] != NULL; i++) { 119 if ((rc = readers[i](td, elf, filename)) == 0) 120 break; 121 122 assert(rc < 0 && errno == ENOENT); 123 } 124 125 if (readers[i] == NULL) { 126 /* 127 * None of the readers found compatible type data. 128 */ 129 130 if (findelfsecidx(elf, filename, ".debug") >= 0) { 131 terminate("%s: DWARF version 1 is not supported\n", 132 filename); 133 } 134 135 if (!(source_types & SOURCE_C) && ignore_non_c) { 136 debug(1, "Ignoring file %s not built from C sources\n", 137 filename); 138 exit(0); 139 } 140 141 rc = 0; 142 } else { 143 rc = 1; 144 } 145 146 (void) elf_end(elf); 147 (void) close(fd); 148 149 return (rc); 150} 151 152int 153main(int argc, char **argv) 154{ 155 tdata_t *filetd, *mstrtd; 156 const char *label = NULL; 157 int verbose = 0; 158 int ignore_non_c = 0; 159 int keep_stabs = 0; 160 int c; 161 162#if defined(sun) 163 sighold(SIGINT); 164 sighold(SIGQUIT); 165 sighold(SIGTERM); 166#endif 167 168 progname = basename(argv[0]); 169 170 if (getenv("CTFCONVERT_DEBUG_LEVEL")) 171 debug_level = atoi(getenv("CTFCONVERT_DEBUG_LEVEL")); 172 if (getenv("CTFCONVERT_DEBUG_PARSE")) 173 debug_parse = atoi(getenv("CTFCONVERT_DEBUG_PARSE")); 174 175 while ((c = getopt(argc, argv, ":l:L:o:givs")) != EOF) { 176 switch (c) { 177 case 'l': 178 label = optarg; 179 break; 180 case 'L': 181 if ((label = getenv(optarg)) == NULL) 182 label = CTF_DEFAULT_LABEL; 183 break; 184 case 'o': 185 outfile = optarg; 186 break; 187 case 's': 188 dynsym = CTF_USE_DYNSYM; 189 break; 190 case 'i': 191 ignore_non_c = 1; 192 break; 193 case 'g': 194 keep_stabs = CTF_KEEP_STABS; 195 break; 196 case 'v': 197 verbose = 1; 198 break; 199 default: 200 usage(); 201 exit(2); 202 } 203 } 204 205 if (getenv("STRIPSTABS_KEEP_STABS") != NULL) 206 keep_stabs = CTF_KEEP_STABS; 207 208 if (argc - optind != 1 || label == NULL) { 209 usage(); 210 exit(2); 211 } 212 213 infile = argv[optind]; 214 if (access(infile, R_OK) != 0) 215 terminate("Can't access %s", infile); 216 217 /* 218 * Upon receipt of a signal, we want to clean up and exit. Our 219 * primary goal during cleanup is to restore the system to a state 220 * such that a subsequent make will eventually cause this command to 221 * be re-run. If we remove the input file (which we do if we get a 222 * signal and the user didn't specify a separate output file), make 223 * will need to rebuild the input file, and will then need to re-run 224 * ctfconvert, which is what we want. 225 */ 226 set_terminate_cleanup(terminate_cleanup); 227 228#if defined(sun) 229 sigset(SIGINT, handle_sig); 230 sigset(SIGQUIT, handle_sig); 231 sigset(SIGTERM, handle_sig); 232#else 233 signal(SIGINT, handle_sig); 234 signal(SIGQUIT, handle_sig); 235 signal(SIGTERM, handle_sig); 236#endif 237 238 filetd = tdata_new(); 239 240 if (!file_read(filetd, infile, ignore_non_c)) 241 terminate("%s doesn't have type data to convert\n", infile); 242 243 if (verbose) 244 iidesc_stats(filetd->td_iihash); 245 246 mstrtd = tdata_new(); 247 merge_into_master(filetd, mstrtd, NULL, 1); 248 249 tdata_label_add(mstrtd, label, CTF_LABEL_LASTIDX); 250 251 /* 252 * If the user supplied an output file that is different from the 253 * input file, write directly to the output file. Otherwise, write 254 * to a temporary file, and replace the input file when we're done. 255 */ 256 if (outfile && strcmp(infile, outfile) != 0) { 257 write_ctf(mstrtd, infile, outfile, dynsym | keep_stabs); 258 } else { 259 char *tmpname = mktmpname(infile, ".ctf"); 260 write_ctf(mstrtd, infile, tmpname, dynsym | keep_stabs); 261 if (rename(tmpname, infile) != 0) 262 terminate("Couldn't rename temp file %s", tmpname); 263 free(tmpname); 264 } 265 266 return (0); 267} 268