anm.c 6.2 KB

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