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