8obj.c 2.1 KB

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