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 "@(#)ctfconvert.c 1.12 06/05/03 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 46const char *progname; 47int debug_level = DEBUG_LEVEL; 48 49static const char *infile = NULL; 50static const char *outfile = NULL; 51static int dynsym; 52 53static void 54usage(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 64static void 65terminate_cleanup(void) 66{ 67 if (!outfile) { 68 fprintf(stderr, "Removing %s\n", infile); 69 unlink(infile); 70 } 71} 72 73static void 74handle_sig(int sig) 75{ 76 terminate("Caught signal %d - exiting\n", sig); 77} 78 79static int 80file_read(tdata_t *td, const char *filename, int ignore_non_c) 81{ 82 typedef int (*reader_f)(tdata_t *, Elf *, const char *); 83 static const reader_f readers[] = { 84#if !defined(__APPLE__) 85 stabs_read, 86#endif /* __APPLE__ */ 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#if !defined(__APPLE__) 107 source_types = built_source_types(elf, filename); 108#else 109 source_types = SOURCE_C | SOURCE_S; 110#endif /* __APPLE__ */ 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 char *label = NULL; 157 int verbose = 0; 158 int ignore_non_c = 0; 159 int keep_stabs = 0; 160 int c; 161 162 sighold(SIGINT); 163 sighold(SIGQUIT); 164 sighold(SIGTERM); 165 166 progname = basename(argv[0]); 167 168 if (getenv("CTFCONVERT_DEBUG_LEVEL")) 169 debug_level = atoi(getenv("CTFCONVERT_DEBUG_LEVEL")); 170 if (getenv("CTFCONVERT_DEBUG_PARSE")) 171 debug_parse = atoi(getenv("CTFCONVERT_DEBUG_PARSE")); 172 173 while ((c = getopt(argc, argv, ":l:L:o:givs")) != EOF) { 174 switch (c) { 175 case 'l': 176 label = optarg; 177 break; 178 case 'L': 179 if ((label = getenv(optarg)) == NULL) 180 label = CTF_DEFAULT_LABEL; 181 break; 182 case 'o': 183 outfile = optarg; 184 break; 185 case 's': 186 dynsym = CTF_USE_DYNSYM; 187 break; 188 case 'i': 189 ignore_non_c = 1; 190 break; 191 case 'g': 192 keep_stabs = CTF_KEEP_STABS; 193 break; 194 case 'v': 195 verbose = 1; 196 break; 197 default: 198 usage(); 199 exit(2); 200 } 201 } 202 203 if (getenv("STRIPSTABS_KEEP_STABS") != NULL) 204 keep_stabs = CTF_KEEP_STABS; 205 206 if (argc - optind != 1 || label == NULL) { 207 usage(); 208 exit(2); 209 } 210 211 infile = argv[optind]; 212 if (access(infile, R_OK) != 0) 213 terminate("Can't access %s", infile); 214 215 /* 216 * Upon receipt of a signal, we want to clean up and exit. Our 217 * primary goal during cleanup is to restore the system to a state 218 * such that a subsequent make will eventually cause this command to 219 * be re-run. If we remove the input file (which we do if we get a 220 * signal and the user didn't specify a separate output file), make 221 * will need to rebuild the input file, and will then need to re-run 222 * ctfconvert, which is what we want. 223 */ 224 set_terminate_cleanup(terminate_cleanup); 225 226 sigset(SIGINT, handle_sig); 227 sigset(SIGQUIT, handle_sig); 228 sigset(SIGTERM, handle_sig); 229 230 filetd = tdata_new(); 231 232 if (!file_read(filetd, infile, ignore_non_c)) 233 terminate("%s doesn't have type data to convert\n", infile); 234 235 if (verbose) 236 iidesc_stats(filetd->td_iihash); 237 238 mstrtd = tdata_new(); 239 merge_into_master(filetd, mstrtd, NULL, 1); 240 241 tdata_label_add(mstrtd, label, CTF_LABEL_LASTIDX); 242 243 /* 244 * If the user supplied an output file that is different from the 245 * input file, write directly to the output file. Otherwise, write 246 * to a temporary file, and replace the input file when we're done. 247 */ 248 249 if (outfile && strcmp(infile, outfile) != 0) { 250 write_ctf(mstrtd, infile, outfile, dynsym | keep_stabs); 251 } else { 252 char *tmpname = mktmpname(infile, ".ctf"); 253 254 write_ctf(mstrtd, infile, tmpname, dynsym | keep_stabs); 255 if (rename(tmpname, infile) != 0) 256 terminate("Couldn't rename temp file %s", tmpname); 257 free(tmpname); 258 } 259 260 return (0); 261} 262