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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
|
/* Size of rel file utility (`size') for GNU.
Copyright (C) 1986 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 1, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include <stdio.h>
#include <ar.h>
#include <sys/types.h>
#include <sys/file.h>
#if !defined(A_OUT) && !defined(MACH_O)
#define A_OUT
#endif
#ifdef A_OUT
#ifdef COFF_ENCAPSULATE
#include "a.out.encap.h"
#else
/* On native BSD systems, use the system's own a.out.h. */
#include <a.out.h>
#endif
#endif
#ifdef MACH_O
#include <sys/loader.h>
#endif
#ifdef USG
#include <fcntl.h>
#include <string.h>
#else
#include <strings.h>
#endif
/* Number of input file names specified. */
int number_of_files;
/* Current file's name */
char *input_name;
/* Current member's name, or 0 if processing a non-library file. */
char *input_member;
/* Offset within archive of the current member,
if we are processing an archive. */
int member_offset;
/* The name this program was run with. */
char *program_name;
char *malloc ();
void do_one_file ();
void do_one_rel_file ();
void error_with_file ();
void fatal ();
void perror_name ();
void print_file_name ();
char *xmalloc ();
char *concat ();
void
main (argc, argv)
char **argv;
int argc;
{
int i;
program_name = argv[0];
number_of_files = argc - 1;
printf ("text\tdata\tbss\tdec\thex\n");
/* Now scan and describe the files. */
if (number_of_files == 0)
do_one_file ("a.out");
else
for (i = 1; i < argc; i++)
do_one_file (argv[i]);
exit (0);
}
/* Print the filename of the current file on 'outfile' (a stdio stream). */
void
print_file_name (outfile)
FILE *outfile;
{
fprintf (outfile, "%s", input_name);
if (input_member)
fprintf (outfile, "(%s)", input_member);
}
/* process one input file */
void scan_library ();
void
do_one_file (name)
char *name;
{
int desc;
char armag[SARMAG];
desc = open (name, O_RDONLY, 0);
if (desc < 0)
{
perror_name (name);
return;
}
input_name = name;
input_member = 0;
if (SARMAG != read (desc, armag, SARMAG) || strncmp (armag, ARMAG, SARMAG))
do_one_rel_file (desc, 0L);
else
scan_library (desc);
close (desc);
}
/* Read in the archive data about one member.
SUBFILE_OFFSET is the address within the archive of the start of that data.
Return the length of the member's contents, which does
not include the archive data about the member.
If there are no more valid members, return zero. */
int
decode_library_subfile (desc, subfile_offset)
int desc;
int subfile_offset;
{
int bytes_read;
int namelen;
int member_length;
char *name;
struct ar_hdr hdr1;
lseek (desc, subfile_offset, 0);
bytes_read = read (desc, &hdr1, sizeof hdr1);
if (!bytes_read)
; /* end of archive */
else if (sizeof hdr1 != bytes_read)
error_with_file ("malformed library archive");
else if (sscanf (hdr1.ar_size, "%d", &member_length) != 1)
error_with_file ("malformatted header of archive member");
else
{
for (namelen = 0;
hdr1.ar_name[namelen] != 0
&& hdr1.ar_name[namelen] != ' '
&& hdr1.ar_name[namelen] != '/';
namelen++)
;
name = (char *) xmalloc (namelen+1);
strncpy (name, hdr1.ar_name, namelen);
*(name + namelen) = 0;
input_member = name;
return member_length;
}
return 0; /* tell caller to exit loop */
}
/* Scan a library and describe each member. */
void
scan_library (desc)
int desc;
{
int this_subfile_offset = SARMAG;
int member_length;
while (member_length = decode_library_subfile (desc, this_subfile_offset))
{
/* describe every member except the ranlib data if any */
if (strcmp (input_member, "__.SYMDEF"))
do_one_rel_file (desc, this_subfile_offset + sizeof (struct ar_hdr));
this_subfile_offset += ((member_length + sizeof (struct ar_hdr)) + 1) & -2;
}
}
/* Read a file's header and fill in various pieces of information.
Return 0 on failure, 1 on success. */
int
read_header_info (desc, offset, tsize, dsize, bsize)
int desc;
long int offset;
unsigned int *tsize;
unsigned int *dsize;
unsigned int *bsize;
{
int len;
#ifdef A_OUT
{
struct exec hdr;
lseek (desc, offset, 0);
#ifdef HEADER_SEEK_FD
/* Skip the headers that encapsulate our data in some other format
such as COFF. */
HEADER_SEEK_FD (desc);
#endif
len = read (desc, (char *) &hdr, sizeof (struct exec));
if (len == sizeof (struct exec) && !N_BADMAG (hdr))
{
*tsize = hdr.a_text;
*dsize = hdr.a_data;
*bsize = hdr.a_bss;
return 1;
}
}
#endif
#ifdef MACH_O
{
struct mach_header mach_header;
char *hdrbuf;
struct load_command *load_command;
struct segment_command *segment_command;
struct section *section;
int len, cmd, seg;
lseek (desc, offset, 0);
len = read (desc, (char *) &mach_header, sizeof (struct mach_header));
if (len == sizeof (struct mach_header) && mach_header.magic == MH_MAGIC)
{
hdrbuf = xmalloc (mach_header.sizeofcmds);
len = read (desc, hdrbuf, mach_header.sizeofcmds);
if (len != mach_header.sizeofcmds)
{
error_with_file ("failure reading Mach-O load commands");
return 0;
}
load_command = (struct load_command *) hdrbuf;
for (cmd = 0; cmd < mach_header.ncmds; ++cmd)
{
if (load_command->cmd == LC_SEGMENT)
{
segment_command = (struct segment_command *) load_command;
section = (struct section *) ((char *) (segment_command + 1));
for (seg = 0; seg < segment_command->nsects; ++seg, ++section)
{
if (!strncmp(SECT_TEXT, section->sectname, sizeof section->sectname))
*tsize = section->size;
else if (!strncmp(SECT_DATA, section->sectname, sizeof section->sectname))
*dsize = section->size;
else if (!strncmp(SECT_BSS, section->sectname, sizeof section->sectname))
*bsize = section->size;
}
}
load_command = (struct load_command *)
((char *) load_command + load_command->cmdsize);
}
free (hdrbuf);
return 1;
}
}
#endif
return 0;
}
void
do_one_rel_file (desc, offset)
int desc;
long int offset;
{
unsigned int tsize, dsize, bsize, total;
if (read_header_info (desc, offset, &tsize, &dsize, &bsize))
{
total = tsize + dsize + bsize;
printf ("%u\t%u\t%u\t%u\t%x", tsize, dsize, bsize, total, total);
}
else
{
error_with_file ("malformed input file (not rel or archive)");
return;
}
if (number_of_files > 1 || input_member)
{
printf ("\t");
print_file_name (stdout);
}
printf ("\n");
}
/* Report a fatal error.
STRING is a printf format string and ARG is one arg for it. */
void
fatal (string, arg)
char *string, *arg;
{
fprintf (stderr, "%s: ", program_name);
fprintf (stderr, string, arg);
fprintf (stderr, "\n");
exit (1);
}
/* Report a nonfatal error.
STRING is a printf format string and ARG is one arg for it. */
void
error (string, arg)
char *string, *arg;
{
fprintf (stderr, "%s: ", program_name);
fprintf (stderr, string, arg);
fprintf (stderr, "\n");
}
/* Report a nonfatal error.
STRING is printed, followed by the current file name. */
void
error_with_file (string)
char *string;
{
fprintf (stderr, "%s: ", program_name);
print_file_name (stderr);
fprintf (stderr, ": ");
fprintf (stderr, string);
fprintf (stderr, "\n");
}
/* Report an error using the message for the last failed system call,
followed by the string NAME. */
void
perror_name (name)
char *name;
{
extern int errno, sys_nerr;
extern char *sys_errlist[];
char *s;
if (errno < sys_nerr)
s = concat (name, ": ", sys_errlist[errno]);
else
s = concat (name, ": ", "unknown error");
error (s, name);
}
/* Like malloc but get fatal error if memory is exhausted. */
char *
xmalloc (size)
unsigned size;
{
char *result = malloc (size);
if (!result)
fatal ("virtual memory exhausted", 0);
return result;
}
/* Return a newly-allocated string
whose contents concatenate those of S1, S2, S3. */
char *
concat (s1, s2, s3)
char *s1, *s2, *s3;
{
int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
strcpy (result, s1);
strcpy (result + len1, s2);
strcpy (result + len1 + len2, s3);
result[len1 + len2 + len3] = 0;
return result;
}
|