obj.c 5.8 KB

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