8obj.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * 8obj.c - identify and parse a 386 object file
  3. */
  4. #include <u.h>
  5. #include <libc.h>
  6. #include <bio.h>
  7. #include <mach.h>
  8. #include "8c/8.out.h"
  9. #include "obj.h"
  10. typedef struct Addr Addr;
  11. struct Addr
  12. {
  13. char sym;
  14. char flags;
  15. };
  16. static Addr addr(Biobuf*);
  17. static char type2char(int);
  18. static void skip(Biobuf*, int);
  19. int
  20. _is8(char *t)
  21. {
  22. uchar *s = (uchar*)t;
  23. return s[0] == (ANAME&0xff) /* aslo = ANAME */
  24. && s[1] == ((ANAME>>8)&0xff)
  25. && s[2] == D_FILE /* type */
  26. && s[3] == 1 /* sym */
  27. && s[4] == '<'; /* name of file */
  28. }
  29. int
  30. _read8(Biobuf *bp, Prog* p)
  31. {
  32. int as, n, c;
  33. Addr a;
  34. as = Bgetc(bp); /* as(low) */
  35. if(as < 0)
  36. return 0;
  37. c = Bgetc(bp); /* as(high) */
  38. if(c < 0)
  39. return 0;
  40. as |= ((c & 0xff) << 8);
  41. p->kind = aNone;
  42. p->sig = 0;
  43. if(as == ANAME || as == ASIGNAME){
  44. if(as == ASIGNAME){
  45. Bread(bp, &p->sig, 4);
  46. p->sig = leswal(p->sig);
  47. }
  48. p->kind = aName;
  49. p->type = type2char(Bgetc(bp)); /* type */
  50. p->sym = Bgetc(bp); /* sym */
  51. n = 0;
  52. for(;;) {
  53. as = Bgetc(bp);
  54. if(as < 0)
  55. return 0;
  56. n++;
  57. if(as == 0)
  58. break;
  59. }
  60. p->id = malloc(n);
  61. if(p->id == 0)
  62. return 0;
  63. Bseek(bp, -n, 1);
  64. if(Bread(bp, p->id, n) != n)
  65. return 0;
  66. return 1;
  67. }
  68. if(as == ATEXT)
  69. p->kind = aText;
  70. if(as == AGLOBL)
  71. p->kind = aData;
  72. skip(bp, 4); /* lineno(4) */
  73. a = addr(bp);
  74. addr(bp);
  75. if(!(a.flags & T_SYM))
  76. p->kind = aNone;
  77. p->sym = a.sym;
  78. return 1;
  79. }
  80. static Addr
  81. addr(Biobuf *bp)
  82. {
  83. Addr a;
  84. int t;
  85. long off;
  86. off = 0;
  87. a.sym = -1;
  88. a.flags = Bgetc(bp); /* flags */
  89. if(a.flags & T_INDEX)
  90. skip(bp, 2);
  91. if(a.flags & T_OFFSET){
  92. off = Bgetc(bp);
  93. off |= Bgetc(bp) << 8;
  94. off |= Bgetc(bp) << 16;
  95. off |= Bgetc(bp) << 24;
  96. if(off < 0)
  97. off = -off;
  98. }
  99. if(a.flags & T_SYM)
  100. a.sym = Bgetc(bp);
  101. if(a.flags & T_FCONST)
  102. skip(bp, 8);
  103. else
  104. if(a.flags & T_SCONST)
  105. skip(bp, NSNAME);
  106. if(a.flags & T_TYPE) {
  107. t = Bgetc(bp);
  108. if(a.sym > 0 && (t==D_PARAM || t==D_AUTO))
  109. _offset(a.sym, off);
  110. }
  111. return a;
  112. }
  113. static char
  114. type2char(int t)
  115. {
  116. switch(t){
  117. case D_EXTERN: return 'U';
  118. case D_STATIC: return 'b';
  119. case D_AUTO: return 'a';
  120. case D_PARAM: return 'p';
  121. default: return UNKNOWN;
  122. }
  123. }
  124. static void
  125. skip(Biobuf *bp, int n)
  126. {
  127. while (n-- > 0)
  128. Bgetc(bp);
  129. }