8obj.c 2.6 KB

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