2obj.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 "2c/2.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. _is2(char *t)
  20. {
  21. uchar *s = (uchar *)t;
  22. return s[0] == (ANAME&0xff) /* aslo = ANAME */
  23. && s[1] == ((ANAME>>8)&0xff) /* ashi = ANAME */
  24. && s[2] == D_FILE /* type */
  25. && s[3] == 1 /* sym */
  26. && s[4] == '<'; /* name of file */
  27. }
  28. int
  29. _read2(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){
  42. p->kind = aName;
  43. p->type = type2char(Bgetc(bp)); /* type */
  44. p->sym = Bgetc(bp); /* sym */
  45. n = 0;
  46. for(;;) {
  47. as = Bgetc(bp);
  48. if(as < 0)
  49. return 0;
  50. n++;
  51. if(as == 0)
  52. break;
  53. }
  54. p->id = malloc(n);
  55. if(p->id == 0)
  56. return 0;
  57. Bseek(bp, -n, 1);
  58. if(Bread(bp, p->id, n) != n)
  59. return 0;
  60. return 1;
  61. }
  62. if(as == ATEXT)
  63. p->kind = aText;
  64. else if(as == AGLOBL)
  65. p->kind = aData;
  66. skip(bp, 4); /*lineno: low, high, lowhigh, highigh*/
  67. a = addr(bp);
  68. addr(bp);
  69. if(!(a.flags & T_SYM))
  70. p->kind = aNone;
  71. p->sym = a.sym;
  72. return 1;
  73. }
  74. static Addr
  75. addr(Biobuf *bp)
  76. {
  77. Addr a;
  78. int t;
  79. long off;
  80. a.flags = Bgetc(bp); /* flags */
  81. a.sym = -1;
  82. off = 0;
  83. if(a.flags & T_FIELD)
  84. skip(bp, 2);
  85. if(a.flags & T_INDEX)
  86. skip(bp, 7);
  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 if(a.flags & T_SCONST)
  100. skip(bp, NSNAME);
  101. else{
  102. t = Bgetc(bp);
  103. if(a.flags & T_TYPE)
  104. t |= Bgetc(bp) << 8;
  105. t &= D_MASK;
  106. if(a.sym > 0 && (t==D_PARAM || t==D_AUTO))
  107. _offset(a.sym, off);
  108. }
  109. return a;
  110. }
  111. static char
  112. type2char(int t)
  113. {
  114. switch(t){
  115. case D_EXTERN: return 'U';
  116. case D_STATIC: return 'b';
  117. case D_AUTO: return 'a';
  118. case D_PARAM: return 'p';
  119. default: return UNKNOWN;
  120. }
  121. }
  122. static void
  123. skip(Biobuf *bp, int n)
  124. {
  125. while (n-- > 0)
  126. Bgetc(bp);
  127. }