anm.c 6.3 KB

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