obj.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. _isu(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. _readu(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. [ObjAmd64] "amd64 .6", _is6, _read6,
  52. [ObjArm] "arm .5", _is5, _read5,
  53. [ObjAlpha] "alpha .7", _is7, _read7,
  54. [Obj386] "386 .8", _is8, _read8,
  55. [ObjSparc] "sparc .k", _isk, _readk,
  56. [ObjPower] "power .q", _isq, _readq,
  57. [ObjMips] "mips .v", _isv, _readv,
  58. [ObjSparc64] "sparc64 .u", _isu, _readu,
  59. [ObjPower64] "power64 .9", _is9, _read9,
  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, uint);
  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, vlong 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, p->sig);
  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, uint sig)
  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. s->sig = sig;
  172. return;
  173. }
  174. h = *name;
  175. for(cp = name+1; *cp; h += *cp++)
  176. h *= HASHMUL;
  177. if(h < 0)
  178. h = ~h;
  179. h &= (NHASH-1);
  180. if (type == 'U' || type == 'b' || islocal(type)) {
  181. for(sp = hash[h]; sp; sp = sp->next)
  182. if(strcmp(sp->s.name, name) == 0) {
  183. switch(sp->s.type) {
  184. case 'T':
  185. case 'D':
  186. case 'U':
  187. if (type == 'U') {
  188. names[id] = &sp->s;
  189. return;
  190. }
  191. break;
  192. case 't':
  193. case 'd':
  194. case 'b':
  195. if (type == 'b') {
  196. names[id] = &sp->s;
  197. return;
  198. }
  199. break;
  200. case 'a':
  201. case 'p':
  202. if (islocal(type)) {
  203. names[id] = &sp->s;
  204. return;
  205. }
  206. break;
  207. default:
  208. break;
  209. }
  210. }
  211. }
  212. sp = malloc(sizeof(Symtab));
  213. sp->s.name = name;
  214. sp->s.type = type;
  215. sp->s.sig = sig;
  216. sp->s.value = islocal(type) ? MAXOFF : 0;
  217. names[id] = &sp->s;
  218. sp->next = hash[h];
  219. hash[h] = sp;
  220. return;
  221. }
  222. /*
  223. * traverse the symbol lists
  224. */
  225. void
  226. objtraverse(void (*fn)(Sym*, void*), void *pointer)
  227. {
  228. int i;
  229. Symtab *s;
  230. for(i = 0; i < NHASH; i++)
  231. for(s = hash[i]; s; s = s->next)
  232. (*fn)(&s->s, pointer);
  233. }
  234. /*
  235. * update the offset information for a 'a' or 'p' symbol in an intermediate file
  236. */
  237. void
  238. _offset(int id, vlong off)
  239. {
  240. Sym *s;
  241. s = names[id];
  242. if (s && s->name[0] && islocal(s->type) && s->value > off)
  243. s->value = off;
  244. }
  245. /*
  246. * update the type of a global text or data symbol
  247. */
  248. static void
  249. objupdate(int id, int type)
  250. {
  251. Sym *s;
  252. s = names[id];
  253. if (s && s->name[0])
  254. if (s->type == 'U')
  255. s->type = type;
  256. else if (s->type == 'b')
  257. s->type = tolower(type);
  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. long 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. }