obj.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. [ObjAmd64] = {"amd64 .6", _is6, _read6},
  59. [Maxobjtype] = {0, 0, 0},
  60. };
  61. struct Symtab
  62. {
  63. struct Sym s;
  64. struct Symtab *next;
  65. };
  66. static Symtab *hash[NHASH];
  67. static Sym *names[NNAMES]; /* working set of active names */
  68. static int processprog(Prog*,int); /* decode each symbol reference */
  69. static void objreset(void);
  70. static void objlookup(int, char *, int, uint);
  71. static void objupdate(int, int);
  72. int
  73. objtype(Biobuf *bp, char **name)
  74. {
  75. int i;
  76. char buf[MAXIS];
  77. if(Bread(bp, buf, MAXIS) < MAXIS)
  78. return -1;
  79. Bseek(bp, -MAXIS, 1);
  80. for (i = 0; i < Maxobjtype; i++) {
  81. if (obj[i].is && (*obj[i].is)(buf)) {
  82. if (name)
  83. *name = obj[i].name;
  84. return i;
  85. }
  86. }
  87. return -1;
  88. }
  89. int
  90. isar(Biobuf *bp)
  91. {
  92. int n;
  93. char magbuf[SARMAG];
  94. n = Bread(bp, magbuf, SARMAG);
  95. if(n == SARMAG && strncmp(magbuf, ARMAG, SARMAG) == 0)
  96. return 1;
  97. return 0;
  98. }
  99. /*
  100. * determine what kind of object file this is and process it.
  101. * return whether or not this was a recognized intermediate file.
  102. */
  103. int
  104. readobj(Biobuf *bp, int objtype)
  105. {
  106. Prog p;
  107. if (objtype < 0 || objtype >= Maxobjtype || obj[objtype].is == 0)
  108. return 1;
  109. objreset();
  110. while ((*obj[objtype].read)(bp, &p))
  111. if (!processprog(&p, 1))
  112. return 0;
  113. return 1;
  114. }
  115. int
  116. readar(Biobuf *bp, int objtype, int64_t end, int doautos)
  117. {
  118. Prog p;
  119. if (objtype < 0 || objtype >= Maxobjtype || obj[objtype].is == 0)
  120. return 1;
  121. objreset();
  122. while ((*obj[objtype].read)(bp, &p) && Boffset(bp) < end)
  123. if (!processprog(&p, doautos))
  124. return 0;
  125. return 1;
  126. }
  127. /*
  128. * decode a symbol reference or definition
  129. */
  130. static int
  131. processprog(Prog *p, int doautos)
  132. {
  133. if(p->kind == aNone)
  134. return 1;
  135. if(p->sym < 0 || p->sym >= NNAMES)
  136. return 0;
  137. switch(p->kind)
  138. {
  139. case aName:
  140. if (!doautos)
  141. if(p->type != 'U' && p->type != 'b')
  142. break;
  143. objlookup(p->sym, p->id, p->type, p->sig);
  144. break;
  145. case aText:
  146. objupdate(p->sym, 'T');
  147. break;
  148. case aData:
  149. objupdate(p->sym, 'D');
  150. break;
  151. default:
  152. break;
  153. }
  154. return 1;
  155. }
  156. /*
  157. * find the entry for s in the symbol array.
  158. * make a new entry if it is not already there.
  159. */
  160. static void
  161. objlookup(int id, char *name, int type, uint sig)
  162. {
  163. int32_t h;
  164. char *cp;
  165. Sym *s;
  166. Symtab *sp;
  167. s = names[id];
  168. if(s && strcmp(s->name, name) == 0) {
  169. s->type = type;
  170. s->sig = sig;
  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.sig = sig;
  215. sp->s.value = islocal(type) ? MAXOFF : 0;
  216. names[id] = &sp->s;
  217. sp->next = hash[h];
  218. hash[h] = sp;
  219. return;
  220. }
  221. /*
  222. * traverse the symbol lists
  223. */
  224. void
  225. objtraverse(void (*fn)(Sym*, void*), void *pointer)
  226. {
  227. int i;
  228. Symtab *s;
  229. for(i = 0; i < NHASH; i++)
  230. for(s = hash[i]; s; s = s->next)
  231. (*fn)(&s->s, pointer);
  232. }
  233. /*
  234. * update the offset information for a 'a' or 'p' symbol in an intermediate file
  235. */
  236. void
  237. _offset(int id, int64_t off)
  238. {
  239. Sym *s;
  240. s = names[id];
  241. if (s && s->name[0] && islocal(s->type) && s->value > off)
  242. s->value = off;
  243. }
  244. /*
  245. * update the type of a global text or data symbol
  246. */
  247. static void
  248. objupdate(int id, int type)
  249. {
  250. Sym *s;
  251. s = names[id];
  252. if (s && s->name[0]) {
  253. if (s->type == 'U')
  254. s->type = type;
  255. else if (s->type == 'b')
  256. s->type = tolower(type);
  257. }
  258. }
  259. /*
  260. * look for the next file in an archive
  261. */
  262. int
  263. nextar(Biobuf *bp, int offset, char *buf)
  264. {
  265. struct ar_hdr a;
  266. int i, r;
  267. int32_t arsize;
  268. if (offset&01)
  269. offset++;
  270. Bseek(bp, offset, 0);
  271. r = Bread(bp, &a, SAR_HDR);
  272. if(r != SAR_HDR)
  273. return 0;
  274. if(strncmp(a.fmag, ARFMAG, sizeof(a.fmag)))
  275. return -1;
  276. for(i=0; i<sizeof(a.name) && i<SARNAME && a.name[i] != ' '; i++)
  277. buf[i] = a.name[i];
  278. buf[i] = 0;
  279. arsize = strtol(a.size, 0, 0);
  280. if (arsize&1)
  281. arsize++;
  282. return arsize + SAR_HDR;
  283. }
  284. static void
  285. objreset(void)
  286. {
  287. int i;
  288. Symtab *s, *n;
  289. for(i = 0; i < NHASH; i++) {
  290. for(s = hash[i]; s; s = n) {
  291. n = s->next;
  292. free(s->s.name);
  293. free(s);
  294. }
  295. hash[i] = 0;
  296. }
  297. memset(names, 0, sizeof names);
  298. }