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