anm.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. /* $Header$ */
  6. /*
  7. ** print symbol tables for
  8. ** ACK object files
  9. **
  10. ** anm [-gopruns] [name ...]
  11. */
  12. #include "out.h"
  13. #include "arch.h"
  14. #include "ranlib.h"
  15. #include <stdio.h>
  16. #include <ctype.h>
  17. int numsort_flg;
  18. int sectsort_flg;
  19. int undef_flg;
  20. int revsort_flg = 1;
  21. int globl_flg;
  22. int nosort_flg;
  23. int arch_flg;
  24. int prep_flg;
  25. int read_error;
  26. struct outhead hbuf;
  27. struct outsect sbuf;
  28. long off;
  29. char *malloc();
  30. char *realloc();
  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. extern long lseek();
  86. extern char *strncpy();
  87. process(fd)
  88. int fd;
  89. {
  90. unsigned int magic;
  91. long nextpos;
  92. struct ar_hdr archive_header;
  93. static char buf[sizeof(archive_header.ar_name)+1];
  94. if (narg > 1) printf("\n%s:\n", filename);
  95. magic = rd_unsigned2(fd);
  96. switch(magic) {
  97. case O_MAGIC:
  98. lseek(fd, 0L, 0);
  99. do_file(fd);
  100. break;
  101. case ARMAG:
  102. case AALMAG:
  103. while (rd_arhdr(fd, &archive_header)) {
  104. nextpos = lseek(fd, 0L, 1) + archive_header.ar_size;
  105. if (nextpos & 1) nextpos++;
  106. strncpy(buf,archive_header.ar_name,sizeof(archive_header.ar_name));
  107. filename = buf;
  108. if ( strcmp(filename, SYMDEF)) {
  109. printf("\n%s:\n", filename);
  110. do_file(fd);
  111. }
  112. lseek(fd, nextpos, 0);
  113. }
  114. break;
  115. default:
  116. fprintf(stderr, "anm: %s -- bad format\n", filename);
  117. break;
  118. }
  119. }
  120. do_file(fd)
  121. int fd;
  122. {
  123. struct outname *nbufp = NULL;
  124. struct outname nbuf;
  125. char *cbufp;
  126. long fi_to_co;
  127. long n;
  128. unsigned readcount;
  129. int i,j;
  130. int compare();
  131. read_error = 0;
  132. rd_fdopen(fd);
  133. rd_ohead(&hbuf);
  134. if (read_error) {
  135. return;
  136. }
  137. if (BADMAGIC(hbuf)) {
  138. return;
  139. }
  140. n = hbuf.oh_nname;
  141. if (n == 0) {
  142. fprintf(stderr, "anm: %s -- no name list\n", filename);
  143. return;
  144. }
  145. if (hbuf.oh_nchar == 0) {
  146. fprintf(stderr, "anm: %s -- no names\n", filename);
  147. return;
  148. }
  149. if ((readcount = hbuf.oh_nchar) != hbuf.oh_nchar) {
  150. fprintf(stderr, "anm: string area too big in %s\n", filename);
  151. exit(2);
  152. }
  153. /* store special section bases ??? */
  154. if (hbuf.oh_flags & HF_8086) {
  155. rd_sect(&sbuf, hbuf.oh_nsect);
  156. if (read_error) {
  157. return;
  158. }
  159. for (i=0; i<hbuf.oh_nsect; i++) {
  160. s_base[i+S_MIN] =
  161. (sbuf.os_base>>12) & 03777760;
  162. }
  163. }
  164. if ((cbufp = (char *)malloc(readcount)) == NULL) {
  165. fprintf(stderr, "anm: out of memory on %s\n", filename);
  166. exit(2);
  167. }
  168. rd_string(cbufp, hbuf.oh_nchar);
  169. if (read_error) {
  170. free(cbufp);
  171. return;
  172. }
  173. fi_to_co = (long) (cbufp - OFF_CHAR(hbuf));
  174. i = 0;
  175. while (--n >= 0) {
  176. rd_name(&nbuf, 1);
  177. if (read_error) {
  178. break;
  179. }
  180. if (globl_flg && (nbuf.on_type&S_EXT)==0)
  181. continue;
  182. if (undef_flg
  183. &&
  184. ((nbuf.on_type&S_TYP)!=S_UND || (nbuf.on_type&S_ETC)!=0))
  185. continue;
  186. if (nbuf.on_foff == 0) nbuf.on_mptr = 0;
  187. else nbuf.on_mptr = (char *) (nbuf.on_foff + fi_to_co);
  188. /* adjust value for specially encoded bases */
  189. if (hbuf.oh_flags & HF_8086) {
  190. if (((nbuf.on_type&S_ETC) == 0) ||
  191. ((nbuf.on_type&S_ETC) == S_SCT)) {
  192. j = nbuf.on_type&S_TYP;
  193. if ((j>=S_MIN) && (j<=S_MAX))
  194. nbuf.on_valu += s_base[j];
  195. }
  196. }
  197. if (nbufp == NULL)
  198. nbufp = (struct outname *)malloc(sizeof(struct outname));
  199. else
  200. nbufp = (struct outname *)realloc(nbufp, (i+1)*sizeof(struct outname));
  201. if (nbufp == NULL) {
  202. fprintf(stderr, "anm: out of memory on %s\n", filename);
  203. exit(2);
  204. }
  205. nbufp[i++] = nbuf;
  206. }
  207. if (nbufp && nosort_flg==0)
  208. qsort(nbufp, i, sizeof(struct outname), compare);
  209. for (n=0; n<i; n++) {
  210. char cs1[4];
  211. char cs2[4];
  212. if (prep_flg)
  213. printf("%s:", filename);
  214. switch(nbufp[n].on_type&S_ETC) {
  215. case S_SCT:
  216. sprintf(cs1, "%2d", (nbufp[n].on_type&S_TYP) - S_MIN);
  217. sprintf(cs2, " S");
  218. break;
  219. case S_FIL:
  220. sprintf(cs1, " -");
  221. sprintf(cs2, " F");
  222. break;
  223. case S_MOD:
  224. sprintf(cs1, " -");
  225. sprintf(cs2, " M");
  226. break;
  227. case S_COM:
  228. sprintf(cs1, " C");
  229. if (nbufp[n].on_type&S_EXT)
  230. sprintf(cs2, " E");
  231. else
  232. sprintf(cs2, " -");
  233. break;
  234. case 0:
  235. if (nbufp[n].on_type&S_EXT)
  236. sprintf(cs2, " E");
  237. else
  238. sprintf(cs2, " -");
  239. switch(nbufp[n].on_type&S_TYP) {
  240. case S_UND:
  241. sprintf(cs1, " U");
  242. break;
  243. case S_ABS:
  244. sprintf(cs1, " A");
  245. break;
  246. default:
  247. sprintf(cs1, "%2d", (nbufp[n].on_type&S_TYP) - S_MIN);
  248. }
  249. break;
  250. default:
  251. sprintf(cs1, "??");
  252. sprintf(cs2, " ?");
  253. }
  254. printf("%8lx %s %s %s\n",nbufp[n].on_valu,cs1,cs2,nbufp[n].on_mptr ? nbufp[n].on_mptr : "(NULL)");
  255. }
  256. if (nbufp)
  257. free((char *)nbufp);
  258. if (cbufp)
  259. free((char *)cbufp);
  260. }
  261. compare(p1, p2)
  262. struct outname *p1, *p2;
  263. {
  264. int i;
  265. if (sectsort_flg) {
  266. if ((p1->on_type&S_TYP) > (p2->on_type&S_TYP))
  267. return(revsort_flg);
  268. if ((p1->on_type&S_TYP) < (p2->on_type&S_TYP))
  269. return(-revsort_flg);
  270. }
  271. if (numsort_flg) {
  272. if (p1->on_valu > p2->on_valu)
  273. return(revsort_flg);
  274. if (p1->on_valu < p2->on_valu)
  275. return(-revsort_flg);
  276. }
  277. if (! p1->on_mptr) {
  278. if (! p2->on_mptr) return 0;
  279. return -revsort_flg;
  280. }
  281. if (! p2->on_mptr) return revsort_flg;
  282. i = strcmp(p1->on_mptr, p2->on_mptr);
  283. if (i > 0)
  284. return(revsort_flg);
  285. if (i < 0)
  286. return(-revsort_flg);
  287. return(0);
  288. }
  289. rd_fatal()
  290. {
  291. fprintf(stderr,"read error on %s\n", filename);
  292. read_error = 1;
  293. }