xref: /onnv-gate/usr/src/tools/ctf/stabs/common/ctfstabs.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * This program has two modes.
31*0Sstevel@tonic-gate  *
32*0Sstevel@tonic-gate  * In the first, or genassym, mode, it generates a header file containing
33*0Sstevel@tonic-gate  * #define'd values for offsets and other information about requested
34*0Sstevel@tonic-gate  * structures and arrays.  This header file can then be used by assembly
35*0Sstevel@tonic-gate  * source files to access those structures without having to hard-code the
36*0Sstevel@tonic-gate  * offsets.  The offsets and values in the header file are derived from the
37*0Sstevel@tonic-gate  * CTF data in a provided object file.
38*0Sstevel@tonic-gate  *
39*0Sstevel@tonic-gate  * The second mode creates forthdebug macros for specified structures and
40*0Sstevel@tonic-gate  * members from an object file.  The macros are created using the CTF data in
41*0Sstevel@tonic-gate  * the object file.
42*0Sstevel@tonic-gate  *
43*0Sstevel@tonic-gate  * Forthdebug macros and offsets header files are generated using the same
44*0Sstevel@tonic-gate  * tool for historical reasons.
45*0Sstevel@tonic-gate  *
46*0Sstevel@tonic-gate  * The input and output files, and their interaction with the tool are
47*0Sstevel@tonic-gate  * shown below:
48*0Sstevel@tonic-gate  *
49*0Sstevel@tonic-gate  *  ---------------        -----------   cc -c -g    ------------------
50*0Sstevel@tonic-gate  *  |#includes    | -----> |#includes| ------------> |object file with|
51*0Sstevel@tonic-gate  *  |mode-specific|        -----------  ctfconvert   |    CTF data    |
52*0Sstevel@tonic-gate  *  |   directives|				     ------------------
53*0Sstevel@tonic-gate  *  ---------------					      |
54*0Sstevel@tonic-gate  *	   |						      | obj_file
55*0Sstevel@tonic-gate  *	   |						      V
56*0Sstevel@tonic-gate  *	   |		   ------------			 ----------
57*0Sstevel@tonic-gate  *	   \-------------> |directives| ---------------> |ctfstabs|
58*0Sstevel@tonic-gate  *			   ------------  input_template  ----------
59*0Sstevel@tonic-gate  *							      |
60*0Sstevel@tonic-gate  *							      V
61*0Sstevel@tonic-gate  *							---------------
62*0Sstevel@tonic-gate  *	Mode-specific input and output formats are	|mode-specific|
63*0Sstevel@tonic-gate  *	described in forth.c and genassym.c		|   output    |
64*0Sstevel@tonic-gate  *							---------------
65*0Sstevel@tonic-gate  */
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate #include <stdio.h>
68*0Sstevel@tonic-gate #include <stdlib.h>
69*0Sstevel@tonic-gate #include <stdarg.h>
70*0Sstevel@tonic-gate #include <errno.h>
71*0Sstevel@tonic-gate #include <fcntl.h>
72*0Sstevel@tonic-gate #include <unistd.h>
73*0Sstevel@tonic-gate #include <libgen.h>
74*0Sstevel@tonic-gate #include <string.h>
75*0Sstevel@tonic-gate #include <ctype.h>
76*0Sstevel@tonic-gate #include <sys/types.h>
77*0Sstevel@tonic-gate #include <sys/stat.h>
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate #include "ctf_headers.h"
80*0Sstevel@tonic-gate #include "utils.h"
81*0Sstevel@tonic-gate #include "memory.h"
82*0Sstevel@tonic-gate #include "ctfstabs.h"
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate #define	WORD_LEN	256
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate static int lineno;
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate FILE *out;
89*0Sstevel@tonic-gate ctf_file_t *ctf;
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate static void
usage(void)92*0Sstevel@tonic-gate usage(void)
93*0Sstevel@tonic-gate {
94*0Sstevel@tonic-gate 	(void) fprintf(stderr, "Usage: %s -t genassym [-m model] "
95*0Sstevel@tonic-gate 	    "[-i input_template] [-o output] obj_file\n", getpname());
96*0Sstevel@tonic-gate 	(void) fprintf(stderr, "       %s -t forth [-m model] "
97*0Sstevel@tonic-gate 	    "[-i input_template] [-o output] obj_file\n", getpname());
98*0Sstevel@tonic-gate 	exit(2);
99*0Sstevel@tonic-gate }
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate /*PRINTFLIKE1*/
102*0Sstevel@tonic-gate int
parse_warn(char * format,...)103*0Sstevel@tonic-gate parse_warn(char *format, ...)
104*0Sstevel@tonic-gate {
105*0Sstevel@tonic-gate 	va_list alist;
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: Line %d: ", getpname(), lineno);
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate 	va_start(alist, format);
110*0Sstevel@tonic-gate 	(void) vfprintf(stderr, format, alist);
111*0Sstevel@tonic-gate 	va_end(alist);
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate 	(void) fprintf(stderr, "\n");
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate 	return (-1);
116*0Sstevel@tonic-gate }
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate #define	READLINE_BUF_INCR	2
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate /*
121*0Sstevel@tonic-gate  * Read a line of input into a statically-allocated buffer.  If the line
122*0Sstevel@tonic-gate  * is larger than the buffer, the buffer will be dynamically resized.
123*0Sstevel@tonic-gate  * Subsequent calls will overwrite the buffer.
124*0Sstevel@tonic-gate  */
125*0Sstevel@tonic-gate static char *
readline(FILE * fp)126*0Sstevel@tonic-gate readline(FILE *fp)
127*0Sstevel@tonic-gate {
128*0Sstevel@tonic-gate 	static char *buf, *bptr;
129*0Sstevel@tonic-gate 	static int buflen;
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate 	if (buflen == 0) {
132*0Sstevel@tonic-gate 		buf = xmalloc(READLINE_BUF_INCR);
133*0Sstevel@tonic-gate 		buflen = READLINE_BUF_INCR;
134*0Sstevel@tonic-gate 	}
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate 	bptr = buf;
137*0Sstevel@tonic-gate 	for (;;) {
138*0Sstevel@tonic-gate 		size_t len, off;
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate 		if (fgets(bptr, buflen - (size_t)(bptr - buf), fp) == NULL)
141*0Sstevel@tonic-gate 			return (NULL);
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate 		len = strlen(bptr);
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate 		if (bptr[len - 1] == '\n')
146*0Sstevel@tonic-gate 			return (buf);
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 		off = (size_t)((bptr + len) - buf);
149*0Sstevel@tonic-gate 		buflen += READLINE_BUF_INCR;
150*0Sstevel@tonic-gate 		buf = xrealloc(buf, buflen);
151*0Sstevel@tonic-gate 		bptr = buf + off;
152*0Sstevel@tonic-gate 	}
153*0Sstevel@tonic-gate }
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate /*
156*0Sstevel@tonic-gate  * We're only given a type name.  Even if it's a struct or a union, we
157*0Sstevel@tonic-gate  * still only get the struct or union name.  We therefore iterate through
158*0Sstevel@tonic-gate  * the possible prefixes, trying to find the right type.
159*0Sstevel@tonic-gate  */
160*0Sstevel@tonic-gate ctf_id_t
find_type(char * name)161*0Sstevel@tonic-gate find_type(char *name)
162*0Sstevel@tonic-gate {
163*0Sstevel@tonic-gate 	char fullname[WORD_LEN];
164*0Sstevel@tonic-gate 	ctf_id_t id;
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate 	if ((id = ctf_lookup_by_name(ctf, name)) != CTF_ERR)
167*0Sstevel@tonic-gate 		return (id);
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate 	(void) snprintf(fullname, WORD_LEN, "struct %s", name);
170*0Sstevel@tonic-gate 	if ((id = ctf_lookup_by_name(ctf, fullname)) != CTF_ERR)
171*0Sstevel@tonic-gate 		return (id);
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate 	(void) snprintf(fullname, WORD_LEN, "union %s", name);
174*0Sstevel@tonic-gate 	if ((id = ctf_lookup_by_name(ctf, fullname)) != CTF_ERR)
175*0Sstevel@tonic-gate 		return (id);
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate 	(void) snprintf(fullname, WORD_LEN, "enum %s", name);
178*0Sstevel@tonic-gate 	if ((id = ctf_lookup_by_name(ctf, fullname)) != CTF_ERR)
179*0Sstevel@tonic-gate 		return (id);
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate 	return (CTF_ERR);
182*0Sstevel@tonic-gate }
183*0Sstevel@tonic-gate 
184*0Sstevel@tonic-gate static int
process_ifile(FILE * tmpl,proc_ops_t * ops)185*0Sstevel@tonic-gate process_ifile(FILE *tmpl, proc_ops_t *ops)
186*0Sstevel@tonic-gate {
187*0Sstevel@tonic-gate 	char *line;
188*0Sstevel@tonic-gate 	int skipping;
189*0Sstevel@tonic-gate 	size_t len;
190*0Sstevel@tonic-gate 	int err = 0;
191*0Sstevel@tonic-gate 
192*0Sstevel@tonic-gate 	for (lineno = skipping = 0; (line = readline(tmpl)) != NULL; lineno++) {
193*0Sstevel@tonic-gate 		len = strlen(line) - 1;
194*0Sstevel@tonic-gate 		line[len] = '\0';
195*0Sstevel@tonic-gate 
196*0Sstevel@tonic-gate 		if (len == 0)
197*0Sstevel@tonic-gate 			skipping = 0;
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate 		if (skipping == 1)
200*0Sstevel@tonic-gate 			continue;
201*0Sstevel@tonic-gate 
202*0Sstevel@tonic-gate 		if (ops->po_line(line) < 0) {
203*0Sstevel@tonic-gate 			(void) parse_warn("Error found: skipping to the next "
204*0Sstevel@tonic-gate 			    "blank line");
205*0Sstevel@tonic-gate 			err++;
206*0Sstevel@tonic-gate 			skipping = 1;
207*0Sstevel@tonic-gate 			continue;
208*0Sstevel@tonic-gate 		}
209*0Sstevel@tonic-gate 	}
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate 	return (err > 0 ? -1 : 0);
212*0Sstevel@tonic-gate }
213*0Sstevel@tonic-gate 
214*0Sstevel@tonic-gate static char *
get_model(ctf_file_t * ctf)215*0Sstevel@tonic-gate get_model(ctf_file_t *ctf)
216*0Sstevel@tonic-gate {
217*0Sstevel@tonic-gate 	ssize_t lsz;
218*0Sstevel@tonic-gate 	ctf_id_t lid;
219*0Sstevel@tonic-gate 
220*0Sstevel@tonic-gate 	/* Neither of these should fail */
221*0Sstevel@tonic-gate 	if ((lid = ctf_lookup_by_name(ctf, "long")) == CTF_ERR ||
222*0Sstevel@tonic-gate 	    (lsz = ctf_type_size(ctf, lid)) == CTF_ERR)
223*0Sstevel@tonic-gate 		die("Couldn't get size of long in object file");
224*0Sstevel@tonic-gate 
225*0Sstevel@tonic-gate 	if (lsz == 8)
226*0Sstevel@tonic-gate 		return ("lp64");
227*0Sstevel@tonic-gate 	else if (lsz == 4)
228*0Sstevel@tonic-gate 		return ("ilp32");
229*0Sstevel@tonic-gate 	else
230*0Sstevel@tonic-gate 		die("Unexpected size of long: %d bytes\n", lsz);
231*0Sstevel@tonic-gate 
232*0Sstevel@tonic-gate 	return (NULL);
233*0Sstevel@tonic-gate }
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate int
main(int argc,char ** argv)236*0Sstevel@tonic-gate main(int argc, char **argv)
237*0Sstevel@tonic-gate {
238*0Sstevel@tonic-gate 	char *model = NULL, *objfile = NULL, *outfile = NULL, *tmplfile = NULL;
239*0Sstevel@tonic-gate 	proc_ops_t *ops = &ga_ops;
240*0Sstevel@tonic-gate 	FILE *tmpl;
241*0Sstevel@tonic-gate 	int ctferr, c;
242*0Sstevel@tonic-gate 
243*0Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "i:m:o:t:")) != EOF) {
244*0Sstevel@tonic-gate 		switch (c) {
245*0Sstevel@tonic-gate 		case 'i':
246*0Sstevel@tonic-gate 			tmplfile = optarg;
247*0Sstevel@tonic-gate 			break;
248*0Sstevel@tonic-gate 		case 'm':
249*0Sstevel@tonic-gate 			model = optarg;
250*0Sstevel@tonic-gate 			break;
251*0Sstevel@tonic-gate 		case 't':
252*0Sstevel@tonic-gate 			if (strcmp(optarg, "genassym") == 0)
253*0Sstevel@tonic-gate 				ops = &ga_ops;
254*0Sstevel@tonic-gate 			else if (strcmp(optarg, "forth") == 0)
255*0Sstevel@tonic-gate 				ops = &fth_ops;
256*0Sstevel@tonic-gate 			else
257*0Sstevel@tonic-gate 				usage();
258*0Sstevel@tonic-gate 			break;
259*0Sstevel@tonic-gate 
260*0Sstevel@tonic-gate 		case 'o':
261*0Sstevel@tonic-gate 			outfile = optarg;
262*0Sstevel@tonic-gate 			break;
263*0Sstevel@tonic-gate 		default:
264*0Sstevel@tonic-gate 			usage();
265*0Sstevel@tonic-gate 		}
266*0Sstevel@tonic-gate 	}
267*0Sstevel@tonic-gate 
268*0Sstevel@tonic-gate 	if (argc - optind != 1)
269*0Sstevel@tonic-gate 		usage();
270*0Sstevel@tonic-gate 	objfile = argv[optind];
271*0Sstevel@tonic-gate 
272*0Sstevel@tonic-gate 	if (tmplfile == NULL || strcmp(tmplfile, "-") == 0)
273*0Sstevel@tonic-gate 		tmpl = stdin;
274*0Sstevel@tonic-gate 	else if ((tmpl = fopen(tmplfile, "r")) == NULL)
275*0Sstevel@tonic-gate 		die("Couldn't open template file %s", tmplfile);
276*0Sstevel@tonic-gate 
277*0Sstevel@tonic-gate 	/*
278*0Sstevel@tonic-gate 	 * this can fail if ENOENT or if there's no CTF data in the file.
279*0Sstevel@tonic-gate 	 */
280*0Sstevel@tonic-gate 	if ((ctf = ctf_open(objfile, &ctferr)) == NULL) {
281*0Sstevel@tonic-gate 		die("Couldn't open object file %s: %s\n", objfile,
282*0Sstevel@tonic-gate 		    ctf_errmsg(ctferr));
283*0Sstevel@tonic-gate 	}
284*0Sstevel@tonic-gate 
285*0Sstevel@tonic-gate 	if (model == NULL)
286*0Sstevel@tonic-gate 		model = get_model(ctf);
287*0Sstevel@tonic-gate 	else if (strcmp(model, get_model(ctf)) != 0)
288*0Sstevel@tonic-gate 		die("Model argument %s doesn't match the object file\n", model);
289*0Sstevel@tonic-gate 
290*0Sstevel@tonic-gate 	if (outfile == NULL || strcmp(outfile, "-") == 0)
291*0Sstevel@tonic-gate 		out = stdout;
292*0Sstevel@tonic-gate 	else if ((out = fopen(outfile, "w")) == NULL)
293*0Sstevel@tonic-gate 		die("Couldn't open output file %s for writing", outfile);
294*0Sstevel@tonic-gate 
295*0Sstevel@tonic-gate 	if ((ops->po_init != NULL && ops->po_init(model) < 0) ||
296*0Sstevel@tonic-gate 	    (process_ifile(tmpl, ops) < 0) ||
297*0Sstevel@tonic-gate 	    (ops->po_fini != NULL && ops->po_fini() < 0)) {
298*0Sstevel@tonic-gate 		(void) fclose(out);
299*0Sstevel@tonic-gate 		(void) unlink(outfile);
300*0Sstevel@tonic-gate 		return (1);
301*0Sstevel@tonic-gate 	}
302*0Sstevel@tonic-gate 
303*0Sstevel@tonic-gate 	return (0);
304*0Sstevel@tonic-gate }
305