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 #include <stdio.h> 34 #include <stdlib.h> 35 #include <unistd.h> 36 #include <signal.h> 37 #include <string.h> 38 #include <fcntl.h> 39 #include <libgen.h> 40 #include <errno.h> 41 #include <assert.h> 42 43 #include "ctftools.h" 44 #include "memory.h" 45 46 const char *progname; 47 int debug_level = DEBUG_LEVEL; 48 49 static char *infile = NULL; 50 static const char *outfile = NULL; 51 static int dynsym; 52 53 static void 54 usage(void) 55 { 56 (void) fprintf(stderr, 57 "Usage: %s [-gis] -l label | -L labelenv [-o outfile] object_file\n" 58 "\n" 59 " Note: if -L labelenv is specified and labelenv is not set in\n" 60 " the environment, a default value is used.\n", 61 progname); 62 } 63 64 static void 65 terminate_cleanup(void) 66 { 67 #if !defined(__FreeBSD__) 68 if (!outfile) { 69 fprintf(stderr, "Removing %s\n", infile); 70 unlink(infile); 71 } 72 #endif 73 } 74 75 static void 76 handle_sig(int sig) 77 { 78 terminate("Caught signal %d - exiting\n", sig); 79 } 80 81 static int 82 file_read(tdata_t *td, char *filename, int ignore_non_c) 83 { 84 typedef int (*reader_f)(tdata_t *, Elf *, char *); 85 static reader_f readers[] = { 86 stabs_read, 87 dw_read, 88 NULL 89 }; 90 91 source_types_t source_types; 92 Elf *elf; 93 int i, rc, fd; 94 95 if ((fd = open(filename, O_RDONLY)) < 0) 96 terminate("failed to open %s", filename); 97 98 (void) elf_version(EV_CURRENT); 99 100 if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) { 101 close(fd); 102 terminate("failed to read %s: %s\n", filename, 103 elf_errmsg(-1)); 104 } 105 106 source_types = built_source_types(elf, filename); 107 108 if ((source_types == SOURCE_NONE || (source_types & SOURCE_UNKNOWN)) && 109 ignore_non_c) { 110 debug(1, "Ignoring file %s from unknown sources\n", filename); 111 exit(0); 112 } 113 114 for (i = 0; readers[i] != NULL; i++) { 115 if ((rc = readers[i](td, elf, filename)) == 0) 116 break; 117 118 assert(rc < 0 && errno == ENOENT); 119 } 120 121 if (readers[i] == NULL) { 122 /* 123 * None of the readers found compatible type data. 124 */ 125 126 if (findelfsecidx(elf, filename, ".debug") >= 0) { 127 terminate("%s: DWARF version 1 is not supported\n", 128 filename); 129 } 130 131 if (!(source_types & SOURCE_C) && ignore_non_c) { 132 debug(1, "Ignoring file %s not built from C sources\n", 133 filename); 134 exit(0); 135 } 136 137 rc = 0; 138 } else { 139 rc = 1; 140 } 141 142 (void) elf_end(elf); 143 (void) close(fd); 144 145 return (rc); 146 } 147 148 int 149 main(int argc, char **argv) 150 { 151 tdata_t *filetd, *mstrtd; 152 const char *label = NULL; 153 int verbose = 0; 154 int ignore_non_c = 0; 155 int keep_stabs = 0; 156 int c; 157 158 #if defined(sun) 159 sighold(SIGINT); 160 sighold(SIGQUIT); 161 sighold(SIGTERM); 162 #endif 163 164 progname = basename(argv[0]); 165 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")); 170 171 while ((c = getopt(argc, argv, ":l:L:o:givs")) != EOF) { 172 switch (c) { 173 case 'l': 174 label = optarg; 175 break; 176 case 'L': 177 if ((label = getenv(optarg)) == NULL) 178 label = CTF_DEFAULT_LABEL; 179 break; 180 case 'o': 181 outfile = optarg; 182 break; 183 case 's': 184 dynsym = CTF_USE_DYNSYM; 185 break; 186 case 'i': 187 ignore_non_c = 1; 188 break; 189 case 'g': 190 keep_stabs = CTF_KEEP_STABS; 191 break; 192 case 'v': 193 verbose = 1; 194 break; 195 default: 196 usage(); 197 exit(2); 198 } 199 } 200 201 if (getenv("STRIPSTABS_KEEP_STABS") != NULL) 202 keep_stabs = CTF_KEEP_STABS; 203 204 if (argc - optind != 1 || label == NULL) { 205 usage(); 206 exit(2); 207 } 208 209 infile = argv[optind]; 210 if (access(infile, R_OK) != 0) 211 terminate("Can't access %s", infile); 212 213 /* 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. 221 */ 222 set_terminate_cleanup(terminate_cleanup); 223 224 #if defined(sun) 225 sigset(SIGINT, handle_sig); 226 sigset(SIGQUIT, handle_sig); 227 sigset(SIGTERM, handle_sig); 228 #else 229 signal(SIGINT, handle_sig); 230 signal(SIGQUIT, handle_sig); 231 signal(SIGTERM, handle_sig); 232 #endif 233 234 filetd = tdata_new(); 235 236 if (!file_read(filetd, infile, ignore_non_c)) 237 terminate("%s doesn't have type data to convert\n", infile); 238 239 if (verbose) 240 iidesc_stats(filetd->td_iihash); 241 242 mstrtd = tdata_new(); 243 merge_into_master(filetd, mstrtd, NULL, 1); 244 245 tdata_label_add(mstrtd, label, CTF_LABEL_LASTIDX); 246 247 /* 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. 251 */ 252 if (outfile && strcmp(infile, outfile) != 0) { 253 write_ctf(mstrtd, infile, outfile, dynsym | keep_stabs); 254 } else { 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); 259 free(tmpname); 260 } 261 262 return (0); 263 } 264