anm.c 6.3 KB

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