anm.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  3. * See the copyright notice in the ACK home directory, in the file "Copyright".
  4. */
  5. /* $Id$ */
  6. /*
  7. ** print symbol tables for
  8. ** ACK object files
  9. **
  10. ** anm [-gopruns] [name ...]
  11. */
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <ctype.h>
  16. #include "out.h"
  17. #include "arch.h"
  18. #include "ranlib.h"
  19. int numsort_flg;
  20. int sectsort_flg;
  21. int undef_flg;
  22. int revsort_flg = 1;
  23. int globl_flg;
  24. int nosort_flg;
  25. int arch_flg;
  26. int prep_flg;
  27. int read_error;
  28. struct outhead hbuf;
  29. struct outsect sbuf;
  30. long off;
  31. long s_base[S_MAX]; /* for specially encoded bases */
  32. char *filename;
  33. int narg;
  34. main(argc, argv)
  35. char **argv;
  36. {
  37. if (--argc>0 && argv[1][0]=='-' && argv[1][1]!=0) {
  38. argv++;
  39. while (*++*argv) switch (**argv) {
  40. case 'n': /* sort numerically */
  41. numsort_flg++;
  42. continue;
  43. case 's': /* sort in section order */
  44. sectsort_flg++;
  45. continue;
  46. case 'g': /* globl symbols only */
  47. globl_flg++;
  48. continue;
  49. case 'u': /* undefined symbols only */
  50. undef_flg++;
  51. continue;
  52. case 'r': /* sort in reverse order */
  53. revsort_flg = -1;
  54. continue;
  55. case 'p': /* don't sort -- symbol table order */
  56. nosort_flg++;
  57. continue;
  58. case 'o': /* prepend a name to each line */
  59. prep_flg++;
  60. continue;
  61. default: /* oops */
  62. fprintf(stderr, "anm: invalid argument -%c\n", *argv[0]);
  63. exit(1);
  64. }
  65. argc--;
  66. }
  67. if (argc == 0) {
  68. argc = 1;
  69. argv[1] = "a.out";
  70. }
  71. narg = argc;
  72. while(argc--) {
  73. int fd;
  74. filename = *++argv;
  75. if ((fd = open(filename, 0)) < 0) {
  76. fprintf(stderr, "anm: cannot open %s\n", filename);
  77. continue;
  78. }
  79. process(fd);
  80. close(fd);
  81. }
  82. exit(0);
  83. }
  84. extern int rd_unsigned2();
  85. process(fd)
  86. int fd;
  87. {
  88. unsigned int magic;
  89. long nextpos;
  90. struct ar_hdr archive_header;
  91. static char buf[sizeof(archive_header.ar_name)+1];
  92. if (narg > 1) printf("\n%s:\n", filename);
  93. magic = rd_unsigned2(fd);
  94. switch(magic) {
  95. case O_MAGIC:
  96. lseek(fd, 0L, 0);
  97. do_file(fd);
  98. break;
  99. case ARMAG:
  100. case AALMAG:
  101. while (rd_arhdr(fd, &archive_header)) {
  102. nextpos = lseek(fd, 0L, 1) + archive_header.ar_size;
  103. if (nextpos & 1) nextpos++;
  104. strncpy(buf,archive_header.ar_name,sizeof(archive_header.ar_name));
  105. filename = buf;
  106. if ( strcmp(filename, SYMDEF)) {
  107. printf("\n%s:\n", filename);
  108. do_file(fd);
  109. }
  110. lseek(fd, nextpos, 0);
  111. }
  112. break;
  113. default:
  114. fprintf(stderr, "anm: %s -- bad format\n", filename);
  115. break;
  116. }
  117. }
  118. do_file(fd)
  119. int fd;
  120. {
  121. struct outname *nbufp = NULL;
  122. struct outname nbuf;
  123. char *cbufp;
  124. long fi_to_co;
  125. long n;
  126. unsigned readcount;
  127. int i,j;
  128. int compare();
  129. read_error = 0;
  130. rd_fdopen(fd);
  131. rd_ohead(&hbuf);
  132. if (read_error) {
  133. return;
  134. }
  135. if (BADMAGIC(hbuf)) {
  136. return;
  137. }
  138. n = hbuf.oh_nname;
  139. if (n == 0) {
  140. fprintf(stderr, "anm: %s -- no name list\n", filename);
  141. return;
  142. }
  143. if (hbuf.oh_nchar == 0) {
  144. fprintf(stderr, "anm: %s -- no names\n", filename);
  145. return;
  146. }
  147. if ((readcount = hbuf.oh_nchar) != hbuf.oh_nchar) {
  148. fprintf(stderr, "anm: string area too big in %s\n", filename);
  149. exit(2);
  150. }
  151. /* store special section bases ??? */
  152. if (hbuf.oh_flags & HF_8086) {
  153. rd_sect(&sbuf, hbuf.oh_nsect);
  154. if (read_error) {
  155. return;
  156. }
  157. for (i=0; i<hbuf.oh_nsect; i++) {
  158. s_base[i+S_MIN] =
  159. (sbuf.os_base>>12) & 03777760;
  160. }
  161. }
  162. if ((cbufp = (char *)malloc(readcount)) == NULL) {
  163. fprintf(stderr, "anm: out of memory on %s\n", filename);
  164. exit(2);
  165. }
  166. rd_string(cbufp, hbuf.oh_nchar);
  167. if (read_error) {
  168. free(cbufp);
  169. return;
  170. }
  171. fi_to_co = (long) (cbufp - OFF_CHAR(hbuf));
  172. i = 0;
  173. while (--n >= 0) {
  174. rd_name(&nbuf, 1);
  175. if (read_error) {
  176. break;
  177. }
  178. if (globl_flg && (nbuf.on_type&S_EXT)==0)
  179. continue;
  180. if (undef_flg
  181. &&
  182. ((nbuf.on_type&S_TYP)!=S_UND || (nbuf.on_type&S_ETC)!=0))
  183. continue;
  184. if (nbuf.on_foff == 0) nbuf.on_mptr = 0;
  185. else nbuf.on_mptr = (char *) (nbuf.on_foff + fi_to_co);
  186. /* adjust value for specially encoded bases */
  187. if (hbuf.oh_flags & HF_8086) {
  188. if (((nbuf.on_type&S_ETC) == 0) ||
  189. ((nbuf.on_type&S_ETC) == S_SCT)) {
  190. j = nbuf.on_type&S_TYP;
  191. if ((j>=S_MIN) && (j<=S_MAX))
  192. nbuf.on_valu += s_base[j];
  193. }
  194. }
  195. if (nbufp == NULL)
  196. nbufp = (struct outname *)malloc(sizeof(struct outname));
  197. else
  198. nbufp = (struct outname *)realloc(nbufp, (i+1)*sizeof(struct outname));
  199. if (nbufp == NULL) {
  200. fprintf(stderr, "anm: out of memory on %s\n", filename);
  201. exit(2);
  202. }
  203. nbufp[i++] = nbuf;
  204. }
  205. if (nbufp && nosort_flg==0)
  206. qsort(nbufp, i, sizeof(struct outname), compare);
  207. for (n=0; n<i; n++) {
  208. char cs1[4];
  209. char cs2[4];
  210. if (prep_flg)
  211. printf("%s:", filename);
  212. switch(nbufp[n].on_type&S_ETC) {
  213. case S_SCT:
  214. sprintf(cs1, "%2d", (nbufp[n].on_type&S_TYP) - S_MIN);
  215. sprintf(cs2, " S");
  216. break;
  217. case S_FIL:
  218. sprintf(cs1, " -");
  219. sprintf(cs2, " F");
  220. break;
  221. case S_MOD:
  222. sprintf(cs1, " -");
  223. sprintf(cs2, " M");
  224. break;
  225. case S_COM:
  226. sprintf(cs1, " C");
  227. if (nbufp[n].on_type&S_EXT)
  228. sprintf(cs2, " E");
  229. else
  230. sprintf(cs2, " -");
  231. break;
  232. case 0:
  233. if (nbufp[n].on_type&S_EXT)
  234. sprintf(cs2, " E");
  235. else
  236. sprintf(cs2, " -");
  237. switch(nbufp[n].on_type&S_TYP) {
  238. case S_UND:
  239. sprintf(cs1, " U");
  240. break;
  241. case S_ABS:
  242. sprintf(cs1, " A");
  243. break;
  244. default:
  245. sprintf(cs1, "%2d", (nbufp[n].on_type&S_TYP) - S_MIN);
  246. }
  247. break;
  248. default:
  249. sprintf(cs1, "??");
  250. sprintf(cs2, " ?");
  251. }
  252. printf("%8lx %s %s %s\n",nbufp[n].on_valu,cs1,cs2,nbufp[n].on_mptr ? nbufp[n].on_mptr : "(NULL)");
  253. }
  254. if (nbufp)
  255. free((char *)nbufp);
  256. if (cbufp)
  257. free((char *)cbufp);
  258. }
  259. compare(p1, p2)
  260. struct outname *p1, *p2;
  261. {
  262. int i;
  263. if (sectsort_flg) {
  264. if ((p1->on_type&S_TYP) > (p2->on_type&S_TYP))
  265. return(revsort_flg);
  266. if ((p1->on_type&S_TYP) < (p2->on_type&S_TYP))
  267. return(-revsort_flg);
  268. }
  269. if (numsort_flg) {
  270. if (p1->on_valu > p2->on_valu)
  271. return(revsort_flg);
  272. if (p1->on_valu < p2->on_valu)
  273. return(-revsort_flg);
  274. }
  275. if (! p1->on_mptr) {
  276. if (! p2->on_mptr) return 0;
  277. return -revsort_flg;
  278. }
  279. if (! p2->on_mptr) return revsort_flg;
  280. i = strcmp(p1->on_mptr, p2->on_mptr);
  281. if (i > 0)
  282. return(revsort_flg);
  283. if (i < 0)
  284. return(-revsort_flg);
  285. return(0);
  286. }
  287. rd_fatal()
  288. {
  289. fprintf(stderr,"read error on %s\n", filename);
  290. read_error = 1;
  291. }