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