size.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /* Size of rel file utility (`size') for GNU.
  2. Copyright (C) 1986 Free Software Foundation, Inc.
  3. NO WARRANTY
  4. BECAUSE THIS PROGRAM IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
  5. NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW. EXCEPT
  6. WHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC,
  7. RICHARD M. STALLMAN AND/OR OTHER PARTIES PROVIDE THIS PROGRAM "AS IS"
  8. WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
  9. BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  10. FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY
  11. AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE
  12. DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
  13. CORRECTION.
  14. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
  15. STALLMAN, THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTY
  16. WHO MAY MODIFY AND REDISTRIBUTE THIS PROGRAM AS PERMITTED BELOW, BE
  17. LIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR
  18. OTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
  19. USE OR INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR
  20. DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR
  21. A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) THIS
  22. PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
  23. DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
  24. GENERAL PUBLIC LICENSE TO COPY
  25. 1. You may copy and distribute verbatim copies of this source file
  26. as you receive it, in any medium, provided that you conspicuously
  27. and appropriately publish on each copy a valid copyright notice
  28. "Copyright (C) 1986 Free Software Foundation, Inc.", and include
  29. following the copyright notice a verbatim copy of the above disclaimer
  30. of warranty and of this License.
  31. 2. You may modify your copy or copies of this source file or
  32. any portion of it, and copy and distribute such modifications under
  33. the terms of Paragraph 1 above, provided that you also do the following:
  34. a) cause the modified files to carry prominent notices stating
  35. that you changed the files and the date of any change; and
  36. b) cause the whole of any work that you distribute or publish,
  37. that in whole or in part contains or is a derivative of this
  38. program or any part thereof, to be licensed at no charge to all
  39. third parties on terms identical to those contained in this
  40. License Agreement (except that you may choose to grant more extensive
  41. warranty protection to some or all third parties, at your option).
  42. c) You may charge a distribution fee for the physical act of
  43. transferring a copy, and you may at your option offer warranty
  44. protection in exchange for a fee.
  45. Mere aggregation of another unrelated program with this program (or its
  46. derivative) on a volume of a storage or distribution medium does not bring
  47. the other program under the scope of these terms.
  48. 3. You may copy and distribute this program (or a portion or derivative
  49. of it, under Paragraph 2) in object code or executable form under the terms
  50. of Paragraphs 1 and 2 above provided that you also do one of the following:
  51. a) accompany it with the complete corresponding machine-readable
  52. source code, which must be distributed under the terms of
  53. Paragraphs 1 and 2 above; or,
  54. b) accompany it with a written offer, valid for at least three
  55. years, to give any third party free (except for a nominal
  56. shipping charge) a complete machine-readable copy of the
  57. corresponding source code, to be distributed under the terms of
  58. Paragraphs 1 and 2 above; or,
  59. c) accompany it with the information you received as to where the
  60. corresponding source code may be obtained. (This alternative is
  61. allowed only for noncommercial distribution and only if you
  62. received the program in object code or executable form alone.)
  63. For an executable file, complete source code means all the source code for
  64. all modules it contains; but, as a special exception, it need not include
  65. source code for modules which are standard libraries that accompany the
  66. operating system on which the executable file runs.
  67. 4. You may not copy, sublicense, distribute or transfer this program
  68. except as expressly provided under this License Agreement. Any attempt
  69. otherwise to copy, sublicense, distribute or transfer this program is void and
  70. your rights to use the program under this License agreement shall be
  71. automatically terminated. However, parties who have received computer
  72. software programs from you with this License Agreement will not have
  73. their licenses terminated so long as such parties remain in full compliance.
  74. 5. If you wish to incorporate parts of this program into other free
  75. programs whose distribution conditions are different, write to the Free
  76. Software Foundation at 675 Mass Ave, Cambridge, MA 02139. We have not yet
  77. worked out a simple rule that can be stated here, but we will often permit
  78. this. We will be guided by the two goals of preserving the free status of
  79. all derivatives of our free software and of promoting the sharing and reuse of
  80. software.
  81. In other words, you are welcome to use, share and improve this program.
  82. You are forbidden to forbid anyone else to use, share and improve
  83. what you give them. Help stamp out software-hoarding! */
  84. #include <stdio.h>
  85. #include <ar.h>
  86. #include <sys/types.h>
  87. #include <sys/file.h>
  88. #ifdef COFF_ENCAPSULATE
  89. #include "a.out.encap.h"
  90. #else
  91. /* On native BSD systems, use the system's own a.out.h. */
  92. #include <a.out.h>
  93. #endif
  94. #ifdef USG
  95. #include <fcntl.h>
  96. #include <string.h>
  97. #else
  98. #include <strings.h>
  99. #endif
  100. /* Struct or union for header of object file. */
  101. #define HEADER_TYPE struct exec
  102. /* Number of input file names specified. */
  103. int number_of_files;
  104. /* Current file's name */
  105. char *input_name;
  106. /* Current member's name, or 0 if processing a non-library file. */
  107. char *input_member;
  108. /* Offset within archive of the current member,
  109. if we are processing an archive. */
  110. int member_offset;
  111. void do_one_file (), do_one_rel_file ();
  112. char *concat ();
  113. main (argc, argv)
  114. char **argv;
  115. int argc;
  116. {
  117. int i;
  118. number_of_files = argc - 1;
  119. printf ("text\tdata\tbss\tdec\thex\n");
  120. /* Now scan and describe the files. */
  121. if (number_of_files == 0)
  122. do_one_file ("a.out");
  123. else
  124. for (i = 1; i < argc; i++)
  125. do_one_file (argv[i]);
  126. }
  127. /* Print the filename of the current file on 'outfile' (a stdio stream). */
  128. print_file_name (outfile)
  129. FILE *outfile;
  130. {
  131. fprintf (outfile, "%s", input_name);
  132. if (input_member)
  133. fprintf (outfile, "(%s)", input_member);
  134. }
  135. /* process one input file */
  136. void scan_library ();
  137. void
  138. do_one_file (name)
  139. char *name;
  140. {
  141. int len, magicnum, desc;
  142. desc = open (name, O_RDONLY, 0);
  143. if (desc < 0)
  144. {
  145. perror_name (name);
  146. return;
  147. }
  148. input_name = name;
  149. input_member = 0;
  150. #ifdef HEADER_SEEK_FD
  151. /* Skip the headers that encapsulate our data in some other format
  152. such as COFF. */
  153. HEADER_SEEK_FD (desc);
  154. #endif
  155. len = read (desc, &magicnum, sizeof magicnum);
  156. if (len != sizeof magicnum)
  157. error_with_file ("failure reading header");
  158. else if (!N_BADMAG (*((struct exec *)&magicnum)))
  159. do_one_rel_file (desc, 0);
  160. else
  161. {
  162. char armag[SARMAG];
  163. lseek (desc, 0, 0);
  164. if (SARMAG != read (desc, armag, SARMAG) || strncmp (armag, ARMAG, SARMAG))
  165. error_with_file ("malformed input file (not rel or archive)");
  166. else
  167. scan_library (desc);
  168. }
  169. close (desc);
  170. }
  171. /* Read in the archive data about one member.
  172. subfile_offset is the address within the archive of the start of that data.
  173. The value returned is the length of the member's contents, which does
  174. not include the archive data about the member.
  175. If there are no more valid members, zero is returned. */
  176. int
  177. decode_library_subfile (desc, subfile_offset)
  178. int desc;
  179. int subfile_offset;
  180. {
  181. int bytes_read;
  182. int namelen;
  183. int member_length;
  184. char *name;
  185. struct ar_hdr hdr1;
  186. lseek (desc, subfile_offset, 0);
  187. bytes_read = read (desc, &hdr1, sizeof hdr1);
  188. if (!bytes_read)
  189. ; /* end of archive */
  190. else if (sizeof hdr1 != bytes_read)
  191. error_with_file ("malformed library archive");
  192. else if (sscanf (hdr1.ar_size, "%d", &member_length) != 1)
  193. error_with_file ("malformatted header of archive member");
  194. else
  195. {
  196. for (namelen = 0;
  197. hdr1.ar_name[namelen] != 0
  198. && hdr1.ar_name[namelen] != ' '
  199. && hdr1.ar_name[namelen] != '/';
  200. namelen++)
  201. ;
  202. name = (char *) xmalloc (namelen+1);
  203. strncpy (name, hdr1.ar_name, namelen);
  204. *(name + namelen) = 0;
  205. input_member = name;
  206. return member_length;
  207. }
  208. return 0; /* tell caller to exit loop */
  209. }
  210. /* Scan a library and describe each member. */
  211. void
  212. scan_library (desc)
  213. int desc;
  214. {
  215. int this_subfile_offset = SARMAG;
  216. int member_length;
  217. while (member_length = decode_library_subfile (desc, this_subfile_offset))
  218. {
  219. /* describe every member except the ranlib data if any */
  220. if (strcmp (input_member, "__.SYMDEF"))
  221. do_one_rel_file (desc, this_subfile_offset + sizeof (struct ar_hdr));
  222. this_subfile_offset += ((member_length + sizeof (struct ar_hdr)) + 1) & -2;
  223. }
  224. }
  225. void read_header ();
  226. void
  227. do_one_rel_file (desc, offset)
  228. int desc;
  229. int offset;
  230. {
  231. HEADER_TYPE header; /* file header read in here */
  232. int total;
  233. header.a_magic = 0;
  234. read_header (desc, &header, offset);
  235. if (N_BADMAG (header))
  236. {
  237. error_with_file ("bad magic number");
  238. return;
  239. }
  240. {
  241. int tsize, dsize, bsize;
  242. tsize = header.a_text;
  243. dsize = header.a_data;
  244. bsize = header.a_bss;
  245. total = tsize + dsize + bsize;
  246. printf ("%d\t%d\t%d\t%d\t%x", tsize, dsize, bsize, total, total);
  247. }
  248. if (number_of_files > 1 || input_member)
  249. {
  250. printf ("\t");
  251. print_file_name (stdout);
  252. }
  253. printf ("\n");
  254. }
  255. /* read a file's header */
  256. void
  257. read_header (desc, loc, offset)
  258. int desc;
  259. HEADER_TYPE *loc;
  260. int offset;
  261. {
  262. int len;
  263. lseek (desc, offset, 0);
  264. #ifdef HEADER_SEEK_FD
  265. /* Skip the headers that encapsulate our data in some other format
  266. such as COFF. */
  267. HEADER_SEEK_FD (desc);
  268. #endif
  269. len = read (desc, loc, sizeof (struct exec));
  270. if (len != sizeof (struct exec))
  271. error_with_file ("failure reading header");
  272. }
  273. /* Report a fatal error.
  274. STRING is a printf format string and ARG is one arg for it. */
  275. fatal (string, arg)
  276. char *string, *arg;
  277. {
  278. fprintf (stderr, "size: ");
  279. fprintf (stderr, string, arg);
  280. fprintf (stderr, "\n");
  281. exit (1);
  282. }
  283. /* Report a nonfatal error.
  284. STRING is a printf format string and ARG is one arg for it. */
  285. error (string, arg)
  286. char *string, *arg;
  287. {
  288. fprintf (stderr, "size: ");
  289. fprintf (stderr, string, arg);
  290. fprintf (stderr, "\n");
  291. }
  292. /* Report a nonfatal error.
  293. STRING is printed, followed by the current file name. */
  294. error_with_file (string)
  295. char *string;
  296. {
  297. fprintf (stderr, "size: ");
  298. print_file_name (stderr);
  299. fprintf (stderr, ": ");
  300. fprintf (stderr, string);
  301. fprintf (stderr, "\n");
  302. }
  303. /* Report an error using the message for the last failed system call,
  304. followed by the string NAME. */
  305. perror_name (name)
  306. char *name;
  307. {
  308. extern int errno, sys_nerr;
  309. extern char *sys_errlist[];
  310. char *s;
  311. if (errno < sys_nerr)
  312. s = concat (name, ": ", sys_errlist[errno]);
  313. else
  314. s = concat (name, ": ", "unknown error");
  315. error (s, name);
  316. }
  317. /* Like malloc but get fatal error if memory is exhausted. */
  318. int
  319. xmalloc (size)
  320. int size;
  321. {
  322. int result = malloc (size);
  323. if (!result)
  324. fatal ("virtual memory exhausted", 0);
  325. return result;
  326. }
  327. /* Return a newly-allocated string
  328. whose contents concatenate those of S1, S2, S3. */
  329. char *
  330. concat (s1, s2, s3)
  331. char *s1, *s2, *s3;
  332. {
  333. int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
  334. char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
  335. strcpy (result, s1);
  336. strcpy (result + len1, s2);
  337. strcpy (result + len1 + len2, s3);
  338. *(result + len1 + len2 + len3) = 0;
  339. return result;
  340. }