4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
30 * This program has two modes.
32 * In the first, or genassym, mode, it generates a header file containing
33 * #define'd values for offsets and other information about requested
34 * structures and arrays. This header file can then be used by assembly
35 * source files to access those structures without having to hard-code the
36 * offsets. The offsets and values in the header file are derived from the
37 * CTF data in a provided object file.
39 * The second mode creates forthdebug macros for specified structures and
40 * members from an object file. The macros are created using the CTF data in
43 * Forthdebug macros and offsets header files are generated using the same
44 * tool for historical reasons.
46 * The input and output files, and their interaction with the tool are
49 * --------------- ----------- cc -c -g ------------------
50 * |#includes | -----> |#includes| ------------> |object file with|
51 * |mode-specific| ----------- ctfconvert | CTF data |
52 * | directives| ------------------
56 * | ------------ ----------
57 * \-------------> |directives| ---------------> |ctfstabs|
58 * ------------ input_template ----------
62 * Mode-specific input and output formats are |mode-specific|
63 * described in forth.c and genassym.c | output |
76 #include <sys/types.h>
79 #include "ctf_headers.h"
94 (void) fprintf(stderr
, "Usage: %s -t genassym [-m model] "
95 "[-i input_template] [-o output] obj_file\n", getpname());
96 (void) fprintf(stderr
, " %s -t forth [-m model] "
97 "[-i input_template] [-o output] obj_file\n", getpname());
103 parse_warn(char *format
, ...)
107 (void) fprintf(stderr
, "%s: Line %d: ", getpname(), lineno
);
109 va_start(alist
, format
);
110 (void) vfprintf(stderr
, format
, alist
);
113 (void) fprintf(stderr
, "\n");
118 #define READLINE_BUF_INCR 2
121 * Read a line of input into a statically-allocated buffer. If the line
122 * is larger than the buffer, the buffer will be dynamically resized.
123 * Subsequent calls will overwrite the buffer.
128 static char *buf
, *bptr
;
132 buf
= xmalloc(READLINE_BUF_INCR
);
133 buflen
= READLINE_BUF_INCR
;
140 if (fgets(bptr
, buflen
- (size_t)(bptr
- buf
), fp
) == NULL
)
145 if (bptr
[len
- 1] == '\n')
148 off
= (size_t)((bptr
+ len
) - buf
);
149 buflen
+= READLINE_BUF_INCR
;
150 buf
= xrealloc(buf
, buflen
);
156 * We're only given a type name. Even if it's a struct or a union, we
157 * still only get the struct or union name. We therefore iterate through
158 * the possible prefixes, trying to find the right type.
161 find_type(char *name
)
163 char fullname
[WORD_LEN
];
166 if ((id
= ctf_lookup_by_name(ctf
, name
)) != CTF_ERR
)
169 (void) snprintf(fullname
, WORD_LEN
, "struct %s", name
);
170 if ((id
= ctf_lookup_by_name(ctf
, fullname
)) != CTF_ERR
)
173 (void) snprintf(fullname
, WORD_LEN
, "union %s", name
);
174 if ((id
= ctf_lookup_by_name(ctf
, fullname
)) != CTF_ERR
)
177 (void) snprintf(fullname
, WORD_LEN
, "enum %s", name
);
178 if ((id
= ctf_lookup_by_name(ctf
, fullname
)) != CTF_ERR
)
185 process_ifile(FILE *tmpl
, proc_ops_t
*ops
)
192 for (lineno
= skipping
= 0; (line
= readline(tmpl
)) != NULL
; lineno
++) {
193 len
= strlen(line
) - 1;
202 if (ops
->po_line(line
) < 0) {
203 (void) parse_warn("Error found: skipping to the next "
211 return (err
> 0 ? -1 : 0);
215 get_model(ctf_file_t
*ctf
)
220 /* Neither of these should fail */
221 if ((lid
= ctf_lookup_by_name(ctf
, "long")) == CTF_ERR
||
222 (lsz
= ctf_type_size(ctf
, lid
)) == CTF_ERR
)
223 die("Couldn't get size of long in object file");
230 die("Unexpected size of long: %d bytes\n", lsz
);
236 main(int argc
, char **argv
)
238 char *model
= NULL
, *objfile
= NULL
, *outfile
= NULL
, *tmplfile
= NULL
;
239 proc_ops_t
*ops
= &ga_ops
;
243 while ((c
= getopt(argc
, argv
, "i:m:o:t:")) != EOF
) {
252 if (strcmp(optarg
, "genassym") == 0)
254 else if (strcmp(optarg
, "forth") == 0)
268 if (argc
- optind
!= 1)
270 objfile
= argv
[optind
];
272 if (tmplfile
== NULL
|| strcmp(tmplfile
, "-") == 0)
274 else if ((tmpl
= fopen(tmplfile
, "r")) == NULL
)
275 die("Couldn't open template file %s", tmplfile
);
278 * this can fail if ENOENT or if there's no CTF data in the file.
280 if ((ctf
= ctf_open(objfile
, &ctferr
)) == NULL
) {
281 die("Couldn't open object file %s: %s\n", objfile
,
286 model
= get_model(ctf
);
287 else if (strcmp(model
, get_model(ctf
)) != 0)
288 die("Model argument %s doesn't match the object file\n", model
);
290 if (outfile
== NULL
|| strcmp(outfile
, "-") == 0)
292 else if ((out
= fopen(outfile
, "w")) == NULL
)
293 die("Couldn't open output file %s for writing", outfile
);
295 if ((ops
->po_init
!= NULL
&& ops
->po_init(model
) < 0) ||
296 (process_ifile(tmpl
, ops
) < 0) ||
297 (ops
->po_fini
!= NULL
&& ops
->po_fini() < 0)) {
299 (void) unlink(outfile
);