obj.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. // [Obj68020] "68020 .2", _is2, _read2,
  59. [ObjAmd64] "amd64 .6", _is6, _read6,
  60. // [ObjArm] "arm .5", _is5, _read5,
  61. // [ObjAlpha] "alpha .7", _is7, _read7,
  62. // [Obj386] "386 .8", _is8, _read8,
  63. // [ObjSparc] "sparc .k", _isk, _readk,
  64. // [ObjPower] "power .q", _isq, _readq,
  65. // [ObjMips] "mips .v", _isv, _readv,
  66. // [ObjSparc64] "sparc64 .u", _isu, _readu,
  67. // [ObjPower64] "power64 .9", _is9, _read9,
  68. [Maxobjtype] 0, 0
  69. };
  70. struct Symtab
  71. {
  72. struct Sym s;
  73. struct Symtab *next;
  74. };
  75. static Symtab *hash[NHASH];
  76. static Sym *names[NNAMES]; /* working set of active names */
  77. static int processprog(Prog*,int); /* decode each symbol reference */
  78. static void objreset(void);
  79. static void objlookup(int, char *, int, uint);
  80. static void objupdate(int, int);
  81. int
  82. objtype(Biobuf *bp, char **name)
  83. {
  84. int i;
  85. char buf[MAXIS];
  86. if(Bread(bp, buf, MAXIS) < MAXIS)
  87. return -1;
  88. Bseek(bp, -MAXIS, 1);
  89. for (i = 0; i < Maxobjtype; i++) {
  90. if (obj[i].is && (*obj[i].is)(buf)) {
  91. if (name)
  92. *name = obj[i].name;
  93. return i;
  94. }
  95. }
  96. return -1;
  97. }
  98. int
  99. isar(Biobuf *bp)
  100. {
  101. int n;
  102. char magbuf[SARMAG];
  103. n = Bread(bp, magbuf, SARMAG);
  104. if(n == SARMAG && strncmp(magbuf, ARMAG, SARMAG) == 0)
  105. return 1;
  106. return 0;
  107. }
  108. /*
  109. * determine what kind of object file this is and process it.
  110. * return whether or not this was a recognized intermediate file.
  111. */
  112. int
  113. readobj(Biobuf *bp, int objtype)
  114. {
  115. Prog p;
  116. if (objtype < 0 || objtype >= Maxobjtype || obj[objtype].is == 0)
  117. return 1;
  118. objreset();
  119. while ((*obj[objtype].read)(bp, &p))
  120. if (!processprog(&p, 1))
  121. return 0;
  122. return 1;
  123. }
  124. int
  125. readar(Biobuf *bp, int objtype, int64_t end, int doautos)
  126. {
  127. Prog p;
  128. if (objtype < 0 || objtype >= Maxobjtype || obj[objtype].is == 0)
  129. return 1;
  130. objreset();
  131. while ((*obj[objtype].read)(bp, &p) && Boffset(bp) < end)
  132. if (!processprog(&p, doautos))
  133. return 0;
  134. return 1;
  135. }
  136. /*
  137. * decode a symbol reference or definition
  138. */
  139. static int
  140. processprog(Prog *p, int doautos)
  141. {
  142. if(p->kind == aNone)
  143. return 1;
  144. if(p->sym < 0 || p->sym >= NNAMES)
  145. return 0;
  146. switch(p->kind)
  147. {
  148. case aName:
  149. if (!doautos)
  150. if(p->type != 'U' && p->type != 'b')
  151. break;
  152. objlookup(p->sym, p->id, p->type, p->sig);
  153. break;
  154. case aText:
  155. objupdate(p->sym, 'T');
  156. break;
  157. case aData:
  158. objupdate(p->sym, 'D');
  159. break;
  160. default:
  161. break;
  162. }
  163. return 1;
  164. }
  165. /*
  166. * find the entry for s in the symbol array.
  167. * make a new entry if it is not already there.
  168. */
  169. static void
  170. objlookup(int id, char *name, int type, uint sig)
  171. {
  172. int32_t h;
  173. char *cp;
  174. Sym *s;
  175. Symtab *sp;
  176. s = names[id];
  177. if(s && strcmp(s->name, name) == 0) {
  178. s->type = type;
  179. s->sig = sig;
  180. return;
  181. }
  182. h = *name;
  183. for(cp = name+1; *cp; h += *cp++)
  184. h *= HASHMUL;
  185. if(h < 0)
  186. h = ~h;
  187. h &= (NHASH-1);
  188. if (type == 'U' || type == 'b' || islocal(type)) {
  189. for(sp = hash[h]; sp; sp = sp->next)
  190. if(strcmp(sp->s.name, name) == 0) {
  191. switch(sp->s.type) {
  192. case 'T':
  193. case 'D':
  194. case 'U':
  195. if (type == 'U') {
  196. names[id] = &sp->s;
  197. return;
  198. }
  199. break;
  200. case 't':
  201. case 'd':
  202. case 'b':
  203. if (type == 'b') {
  204. names[id] = &sp->s;
  205. return;
  206. }
  207. break;
  208. case 'a':
  209. case 'p':
  210. if (islocal(type)) {
  211. names[id] = &sp->s;
  212. return;
  213. }
  214. break;
  215. default:
  216. break;
  217. }
  218. }
  219. }
  220. sp = malloc(sizeof(Symtab));
  221. sp->s.name = name;
  222. sp->s.type = type;
  223. sp->s.sig = sig;
  224. sp->s.value = islocal(type) ? MAXOFF : 0;
  225. names[id] = &sp->s;
  226. sp->next = hash[h];
  227. hash[h] = sp;
  228. return;
  229. }
  230. /*
  231. * traverse the symbol lists
  232. */
  233. void
  234. objtraverse(void (*fn)(Sym*, void*), void *pointer)
  235. {
  236. int i;
  237. Symtab *s;
  238. for(i = 0; i < NHASH; i++)
  239. for(s = hash[i]; s; s = s->next)
  240. (*fn)(&s->s, pointer);
  241. }
  242. /*
  243. * update the offset information for a 'a' or 'p' symbol in an intermediate file
  244. */
  245. void
  246. _offset(int id, int64_t off)
  247. {
  248. Sym *s;
  249. s = names[id];
  250. if (s && s->name[0] && islocal(s->type) && s->value > off)
  251. s->value = off;
  252. }
  253. /*
  254. * update the type of a global text or data symbol
  255. */
  256. static void
  257. objupdate(int id, int type)
  258. {
  259. Sym *s;
  260. s = names[id];
  261. if (s && s->name[0])
  262. if (s->type == 'U')
  263. s->type = type;
  264. else if (s->type == 'b')
  265. s->type = tolower(type);
  266. }
  267. /*
  268. * look for the next file in an archive
  269. */
  270. int
  271. nextar(Biobuf *bp, int offset, char *buf)
  272. {
  273. struct ar_hdr a;
  274. int i, r;
  275. int32_t arsize;
  276. if (offset&01)
  277. offset++;
  278. Bseek(bp, offset, 0);
  279. r = Bread(bp, &a, SAR_HDR);
  280. if(r != SAR_HDR)
  281. return 0;
  282. if(strncmp(a.fmag, ARFMAG, sizeof(a.fmag)))
  283. return -1;
  284. for(i=0; i<sizeof(a.name) && i<SARNAME && a.name[i] != ' '; i++)
  285. buf[i] = a.name[i];
  286. buf[i] = 0;
  287. arsize = strtol(a.size, 0, 0);
  288. if (arsize&1)
  289. arsize++;
  290. return arsize + SAR_HDR;
  291. }
  292. static void
  293. objreset(void)
  294. {
  295. int i;
  296. Symtab *s, *n;
  297. for(i = 0; i < NHASH; i++) {
  298. for(s = hash[i]; s; s = n) {
  299. n = s->next;
  300. free(s->s.name);
  301. free(s);
  302. }
  303. hash[i] = 0;
  304. }
  305. memset(names, 0, sizeof names);
  306. }