2obj.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * 2obj.c - identify and parse a 68020 object file
  3. */
  4. #include <u.h>
  5. #include <libc.h>
  6. #include <bio.h>
  7. #include <mach.h>
  8. #include "2c/2.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. _is2(char *t)
  21. {
  22. uchar *s = (uchar *)t;
  23. return s[0] == (ANAME&0xff) /* aslo = ANAME */
  24. && s[1] == ((ANAME>>8)&0xff) /* ashi = ANAME */
  25. && s[2] == D_FILE /* type */
  26. && s[3] == 1 /* sym */
  27. && s[4] == '<'; /* name of file */
  28. }
  29. int
  30. _read2(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 = beswal(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. else if(as == AGLOBL)
  71. p->kind = aData;
  72. skip(bp, 4); /*lineno: low, high, lowhigh, highigh*/
  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. a.flags = Bgetc(bp); /* flags */
  87. a.sym = -1;
  88. off = 0;
  89. if(a.flags & T_FIELD)
  90. skip(bp, 2);
  91. if(a.flags & T_INDEX)
  92. skip(bp, 7);
  93. if(a.flags & T_OFFSET){
  94. off = Bgetc(bp);
  95. off |= Bgetc(bp) << 8;
  96. off |= Bgetc(bp) << 16;
  97. off |= Bgetc(bp) << 24;
  98. if(off < 0)
  99. off = -off;
  100. }
  101. if(a.flags & T_SYM)
  102. a.sym = Bgetc(bp);
  103. if(a.flags & T_FCONST)
  104. skip(bp, 8);
  105. else if(a.flags & T_SCONST)
  106. skip(bp, NSNAME);
  107. else{
  108. t = Bgetc(bp);
  109. if(a.flags & T_TYPE)
  110. t |= Bgetc(bp) << 8;
  111. t &= D_MASK;
  112. if(a.sym > 0 && (t==D_PARAM || t==D_AUTO))
  113. _offset(a.sym, off);
  114. }
  115. return a;
  116. }
  117. static char
  118. type2char(int t)
  119. {
  120. switch(t){
  121. case D_EXTERN: return 'U';
  122. case D_STATIC: return 'b';
  123. case D_AUTO: return 'a';
  124. case D_PARAM: return 'p';
  125. default: return UNKNOWN;
  126. }
  127. }
  128. static void
  129. skip(Biobuf *bp, int n)
  130. {
  131. while (n-- > 0)
  132. Bgetc(bp);
  133. }