obj.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * obj.c
  3. * routines universal to all object files
  4. */
  5. #include <u.h>
  6. #include <libc.h>
  7. #include <bio.h>
  8. #include <ar.h>
  9. #include <mach.h>
  10. #include "obj.h"
  11. #define islocal(t) ((t)=='a' || (t)=='p')
  12. enum
  13. {
  14. NNAMES = 50,
  15. MAXIS = 8, /* max length to determine if a file is a .? file */
  16. MAXOFF = 0x7fffffff, /* larger than any possible local offset */
  17. NHASH = 1024, /* must be power of two */
  18. HASHMUL = 79L,
  19. };
  20. int _is2(char*), /* in [$OS].c */
  21. _is5(char*),
  22. _is6(char*),
  23. _is7(char*),
  24. _is8(char*),
  25. _is9(char*),
  26. _isk(char*),
  27. _isq(char*),
  28. _isv(char*),
  29. _isx(char*),
  30. _read2(Biobuf*, Prog*),
  31. _read5(Biobuf*, Prog*),
  32. _read6(Biobuf*, Prog*),
  33. _read7(Biobuf*, Prog*),
  34. _read8(Biobuf*, Prog*),
  35. _read9(Biobuf*, Prog*),
  36. _readk(Biobuf*, Prog*),
  37. _readq(Biobuf*, Prog*),
  38. _readv(Biobuf*, Prog*),
  39. _readx(Biobuf*, Prog*);
  40. typedef struct Obj Obj;
  41. typedef struct Symtab Symtab;
  42. struct Obj /* functions to handle each intermediate (.$O) file */
  43. {
  44. char *name; /* name of each $O file */
  45. int (*is)(char*); /* test for each type of $O file */
  46. int (*read)(Biobuf*, Prog*); /* read for each type of $O file*/
  47. };
  48. static Obj obj[] =
  49. { /* functions to identify and parse each type of obj */
  50. [Obj68020] "68020 .2", _is2, _read2,
  51. [ObjSparc] "sparc .k", _isk, _readk,
  52. [ObjMips] "mips .v", _isv, _readv,
  53. [Obj386] "386 .8", _is8, _read8,
  54. [ObjArm] "arm .5", _is5, _read5,
  55. [ObjPower] "power .q", _isq, _readq,
  56. [Obj960] "960 .6", _is6, _read6,
  57. [Obj3210] "3210 .x", _isx, _readx,
  58. [Obj29000] "29000 .9", _is9, _read9,
  59. [ObjAlpha] "alpha .7", _is7, _read7,
  60. [Maxobjtype] 0, 0
  61. };
  62. struct Symtab
  63. {
  64. struct Sym s;
  65. struct Symtab *next;
  66. };
  67. static Symtab *hash[NHASH];
  68. static Sym *names[NNAMES]; /* working set of active names */
  69. static int processprog(Prog*,int); /* decode each symbol reference */
  70. static void objreset(void);
  71. static void objlookup(int, char *, int );
  72. static void objupdate(int, int);
  73. int
  74. objtype(Biobuf *bp, char **name)
  75. {
  76. int i;
  77. char buf[MAXIS];
  78. if(Bread(bp, buf, MAXIS) < MAXIS)
  79. return -1;
  80. Bseek(bp, -MAXIS, 1);
  81. for (i = 0; i < Maxobjtype; i++) {
  82. if (obj[i].is && (*obj[i].is)(buf)) {
  83. if (name)
  84. *name = obj[i].name;
  85. return i;
  86. }
  87. }
  88. return -1;
  89. }
  90. int
  91. isar(Biobuf *bp)
  92. {
  93. int n;
  94. char magbuf[SARMAG];
  95. n = Bread(bp, magbuf, SARMAG);
  96. if(n == SARMAG && strncmp(magbuf, ARMAG, SARMAG) == 0)
  97. return 1;
  98. return 0;
  99. }
  100. /*
  101. * determine what kind of object file this is and process it.
  102. * return whether or not this was a recognized intermediate file.
  103. */
  104. int
  105. readobj(Biobuf *bp, int objtype)
  106. {
  107. Prog p;
  108. if (objtype < 0 || objtype >= Maxobjtype || obj[objtype].is == 0)
  109. return 1;
  110. objreset();
  111. while ((*obj[objtype].read)(bp, &p))
  112. if (!processprog(&p, 1))
  113. return 0;
  114. return 1;
  115. }
  116. int
  117. readar(Biobuf *bp, int objtype, int end, int doautos)
  118. {
  119. Prog p;
  120. if (objtype < 0 || objtype >= Maxobjtype || obj[objtype].is == 0)
  121. return 1;
  122. objreset();
  123. while ((*obj[objtype].read)(bp, &p) && Boffset(bp) < end)
  124. if (!processprog(&p, doautos))
  125. return 0;
  126. return 1;
  127. }
  128. /*
  129. * decode a symbol reference or definition
  130. */
  131. static int
  132. processprog(Prog *p, int doautos)
  133. {
  134. if(p->kind == aNone)
  135. return 1;
  136. if(p->sym < 0 || p->sym >= NNAMES)
  137. return 0;
  138. switch(p->kind)
  139. {
  140. case aName:
  141. if (!doautos)
  142. if(p->type != 'U' && p->type != 'b')
  143. break;
  144. objlookup(p->sym, p->id, p->type);
  145. break;
  146. case aText:
  147. objupdate(p->sym, 'T');
  148. break;
  149. case aData:
  150. objupdate(p->sym, 'D');
  151. break;
  152. default:
  153. break;
  154. }
  155. return 1;
  156. }
  157. /*
  158. * find the entry for s in the symbol array.
  159. * make a new entry if it is not already there.
  160. */
  161. static void
  162. objlookup(int id, char *name, int type)
  163. {
  164. long h;
  165. char *cp;
  166. Sym *s;
  167. Symtab *sp;
  168. s = names[id];
  169. if(s && strcmp(s->name, name) == 0) {
  170. s->type = type;
  171. return;
  172. }
  173. h = *name;
  174. for(cp = name+1; *cp; h += *cp++)
  175. h *= HASHMUL;
  176. if(h < 0)
  177. h = ~h;
  178. h &= (NHASH-1);
  179. if (type == 'U' || type == 'b' || islocal(type)) {
  180. for(sp = hash[h]; sp; sp = sp->next)
  181. if(strcmp(sp->s.name, name) == 0) {
  182. switch(sp->s.type) {
  183. case 'T':
  184. case 'D':
  185. case 'U':
  186. if (type == 'U') {
  187. names[id] = &sp->s;
  188. return;
  189. }
  190. break;
  191. case 't':
  192. case 'd':
  193. case 'b':
  194. if (type == 'b') {
  195. names[id] = &sp->s;
  196. return;
  197. }
  198. break;
  199. case 'a':
  200. case 'p':
  201. if (islocal(type)) {
  202. names[id] = &sp->s;
  203. return;
  204. }
  205. break;
  206. default:
  207. break;
  208. }
  209. }
  210. }
  211. sp = malloc(sizeof(Symtab));
  212. sp->s.name = name;
  213. sp->s.type = type;
  214. sp->s.value = islocal(type) ? MAXOFF : 0;
  215. names[id] = &sp->s;
  216. sp->next = hash[h];
  217. hash[h] = sp;
  218. return;
  219. }
  220. /*
  221. * traverse the symbol lists
  222. */
  223. void
  224. objtraverse(void (*fn)(Sym*, void*), void *pointer)
  225. {
  226. int i;
  227. Symtab *s;
  228. for(i = 0; i < NHASH; i++)
  229. for(s = hash[i]; s; s = s->next)
  230. (*fn)(&s->s, pointer);
  231. }
  232. /*
  233. * update the offset information for a 'a' or 'p' symbol in an intermediate file
  234. */
  235. void
  236. _offset(int id, long off)
  237. {
  238. Sym *s;
  239. s = names[id];
  240. if (s && s->name[0] && islocal(s->type) && s->value > off)
  241. s->value = off;
  242. }
  243. /*
  244. * update the type of a global text or data symbol
  245. */
  246. static void
  247. objupdate(int id, int type)
  248. {
  249. Sym *s;
  250. s = names[id];
  251. if (s && s->name[0])
  252. if (s->type == 'U')
  253. s->type = type;
  254. else if (s->type == 'b')
  255. s->type = tolower(type);
  256. }
  257. /*
  258. * look for the next file in an archive
  259. */
  260. int
  261. nextar(Biobuf *bp, int offset, char *buf)
  262. {
  263. struct ar_hdr a;
  264. int i, r;
  265. long arsize;
  266. if (offset&01)
  267. offset++;
  268. Bseek(bp, offset, 0);
  269. r = Bread(bp, &a, SAR_HDR);
  270. if(r != SAR_HDR)
  271. return 0;
  272. if(strncmp(a.fmag, ARFMAG, sizeof(a.fmag)))
  273. return -1;
  274. for(i=0; i<sizeof(a.name) && i<SARNAME && a.name[i] != ' '; i++)
  275. buf[i] = a.name[i];
  276. buf[i] = 0;
  277. arsize = atol(a.size);
  278. if (arsize&1)
  279. arsize++;
  280. return arsize + SAR_HDR;
  281. }
  282. static void
  283. objreset(void)
  284. {
  285. int i;
  286. Symtab *s, *n;
  287. for(i = 0; i < NHASH; i++) {
  288. for(s = hash[i]; s; s = n) {
  289. n = s->next;
  290. free(s->s.name);
  291. free(s);
  292. }
  293. hash[i] = 0;
  294. }
  295. memset(names, 0, sizeof names);
  296. }