obj.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /*
  10. * obj.c
  11. * routines universal to all object files
  12. */
  13. #include <u.h>
  14. #include <libc.h>
  15. #include <bio.h>
  16. #include <ar.h>
  17. #include <mach.h>
  18. #include "obj.h"
  19. #define islocal(t) ((t)=='a' || (t)=='p')
  20. enum
  21. {
  22. NNAMES = 50,
  23. MAXIS = 8, /* max length to determine if a file is a .? file */
  24. MAXOFF = 0x7fffffff, /* larger than any possible local offset */
  25. NHASH = 1024, /* must be power of two */
  26. HASHMUL = 79L,
  27. };
  28. int _is2(char*), /* in [$OS].c */
  29. _is5(char*),
  30. _is6(char*),
  31. _is7(char*),
  32. _is8(char*),
  33. _is9(char*),
  34. _isk(char*),
  35. _isq(char*),
  36. _isv(char*),
  37. _isu(char*),
  38. _read2(Biobuf*, Prog*),
  39. _read5(Biobuf*, Prog*),
  40. _read6(Biobuf*, Prog*),
  41. _read7(Biobuf*, Prog*),
  42. _read8(Biobuf*, Prog*),
  43. _read9(Biobuf*, Prog*),
  44. _readk(Biobuf*, Prog*),
  45. _readq(Biobuf*, Prog*),
  46. _readv(Biobuf*, Prog*),
  47. _readu(Biobuf*, Prog*);
  48. typedef struct Obj Obj;
  49. typedef struct Symtab Symtab;
  50. struct Obj /* functions to handle each intermediate (.$O) file */
  51. {
  52. char *name; /* name of each $O file */
  53. int (*is)(char*); /* test for each type of $O file */
  54. int (*read)(Biobuf*, Prog*); /* read for each type of $O file*/
  55. };
  56. static Obj obj[] =
  57. { /* functions to identify and parse each type of obj */
  58. [Maxobjtype ]= {0, 0, 0},
  59. };
  60. struct Symtab
  61. {
  62. struct Sym s;
  63. struct Symtab *next;
  64. };
  65. static Symtab *hash[NHASH];
  66. static Sym *names[NNAMES]; /* working set of active names */
  67. static int processprog(Prog*,int); /* decode each symbol reference */
  68. static void objreset(void);
  69. static void objlookup(int, char *, int, uint);
  70. static void objupdate(int, int);
  71. int
  72. objtype(Biobuf *bp, char **name)
  73. {
  74. int i;
  75. char buf[MAXIS];
  76. if(Bread(bp, buf, MAXIS) < MAXIS)
  77. return -1;
  78. Bseek(bp, -MAXIS, 1);
  79. for (i = 0; i < Maxobjtype; i++) {
  80. if (obj[i].is && (*obj[i].is)(buf)) {
  81. if (name)
  82. *name = obj[i].name;
  83. return i;
  84. }
  85. }
  86. return -1;
  87. }
  88. int
  89. isar(Biobuf *bp)
  90. {
  91. int n;
  92. char magbuf[SARMAG];
  93. n = Bread(bp, magbuf, SARMAG);
  94. if(n == SARMAG && strncmp(magbuf, ARMAG, SARMAG) == 0)
  95. return 1;
  96. return 0;
  97. }
  98. /*
  99. * determine what kind of object file this is and process it.
  100. * return whether or not this was a recognized intermediate file.
  101. */
  102. int
  103. readobj(Biobuf *bp, int objtype)
  104. {
  105. Prog p;
  106. if (objtype < 0 || objtype >= Maxobjtype || obj[objtype].is == 0)
  107. return 1;
  108. objreset();
  109. while ((*obj[objtype].read)(bp, &p))
  110. if (!processprog(&p, 1))
  111. return 0;
  112. return 1;
  113. }
  114. int
  115. readar(Biobuf *bp, int objtype, int64_t end, int doautos)
  116. {
  117. Prog p;
  118. if (objtype < 0 || objtype >= Maxobjtype || obj[objtype].is == 0)
  119. return 1;
  120. objreset();
  121. while ((*obj[objtype].read)(bp, &p) && Boffset(bp) < end)
  122. if (!processprog(&p, doautos))
  123. return 0;
  124. return 1;
  125. }
  126. /*
  127. * decode a symbol reference or definition
  128. */
  129. static int
  130. processprog(Prog *p, int doautos)
  131. {
  132. if(p->kind == aNone)
  133. return 1;
  134. if(p->sym < 0 || p->sym >= NNAMES)
  135. return 0;
  136. switch(p->kind)
  137. {
  138. case aName:
  139. if (!doautos)
  140. if(p->type != 'U' && p->type != 'b')
  141. break;
  142. objlookup(p->sym, p->id, p->type, p->sig);
  143. break;
  144. case aText:
  145. objupdate(p->sym, 'T');
  146. break;
  147. case aData:
  148. objupdate(p->sym, 'D');
  149. break;
  150. default:
  151. break;
  152. }
  153. return 1;
  154. }
  155. /*
  156. * find the entry for s in the symbol array.
  157. * make a new entry if it is not already there.
  158. */
  159. static void
  160. objlookup(int id, char *name, int type, uint sig)
  161. {
  162. int32_t h;
  163. char *cp;
  164. Sym *s;
  165. Symtab *sp;
  166. s = names[id];
  167. if(s && strcmp(s->name, name) == 0) {
  168. s->type = type;
  169. s->sig = sig;
  170. return;
  171. }
  172. h = *name;
  173. for(cp = name+1; *cp; h += *cp++)
  174. h *= HASHMUL;
  175. if(h < 0)
  176. h = ~h;
  177. h &= (NHASH-1);
  178. if (type == 'U' || type == 'b' || islocal(type)) {
  179. for(sp = hash[h]; sp; sp = sp->next)
  180. if(strcmp(sp->s.name, name) == 0) {
  181. switch(sp->s.type) {
  182. case 'T':
  183. case 'D':
  184. case 'U':
  185. if (type == 'U') {
  186. names[id] = &sp->s;
  187. return;
  188. }
  189. break;
  190. case 't':
  191. case 'd':
  192. case 'b':
  193. if (type == 'b') {
  194. names[id] = &sp->s;
  195. return;
  196. }
  197. break;
  198. case 'a':
  199. case 'p':
  200. if (islocal(type)) {
  201. names[id] = &sp->s;
  202. return;
  203. }
  204. break;
  205. default:
  206. break;
  207. }
  208. }
  209. }
  210. sp = malloc(sizeof(Symtab));
  211. sp->s.name = name;
  212. sp->s.type = type;
  213. sp->s.sig = sig;
  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, int64_t 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. /*
  259. * look for the next file in an archive
  260. */
  261. int
  262. nextar(Biobuf *bp, int offset, char *buf)
  263. {
  264. struct ar_hdr a;
  265. int i, r;
  266. int32_t arsize;
  267. if (offset&01)
  268. offset++;
  269. Bseek(bp, offset, 0);
  270. r = Bread(bp, &a, SAR_HDR);
  271. if(r != SAR_HDR)
  272. return 0;
  273. if(strncmp(a.fmag, ARFMAG, sizeof(a.fmag)))
  274. return -1;
  275. for(i=0; i<sizeof(a.name) && i<SARNAME && a.name[i] != ' '; i++)
  276. buf[i] = a.name[i];
  277. buf[i] = 0;
  278. arsize = strtol(a.size, 0, 0);
  279. if (arsize&1)
  280. arsize++;
  281. return arsize + SAR_HDR;
  282. }
  283. static void
  284. objreset(void)
  285. {
  286. int i;
  287. Symtab *s, *n;
  288. for(i = 0; i < NHASH; i++) {
  289. for(s = hash[i]; s; s = n) {
  290. n = s->next;
  291. free(s->s.name);
  292. free(s);
  293. }
  294. hash[i] = 0;
  295. }
  296. memset(names, 0, sizeof names);
  297. }