2obj.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 || 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. else if(as == AGLOBL)
  67. p->kind = aData;
  68. skip(bp, 4); /*lineno: low, high, lowhigh, highigh*/
  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. a.flags = Bgetc(bp); /* flags */
  83. a.sym = -1;
  84. off = 0;
  85. if(a.flags & T_FIELD)
  86. skip(bp, 2);
  87. if(a.flags & T_INDEX)
  88. skip(bp, 7);
  89. if(a.flags & T_OFFSET){
  90. off = Bgetc(bp);
  91. off |= Bgetc(bp) << 8;
  92. off |= Bgetc(bp) << 16;
  93. off |= Bgetc(bp) << 24;
  94. if(off < 0)
  95. off = -off;
  96. }
  97. if(a.flags & T_SYM)
  98. a.sym = Bgetc(bp);
  99. if(a.flags & T_FCONST)
  100. skip(bp, 8);
  101. else if(a.flags & T_SCONST)
  102. skip(bp, NSNAME);
  103. else{
  104. t = Bgetc(bp);
  105. if(a.flags & T_TYPE)
  106. t |= Bgetc(bp) << 8;
  107. t &= D_MASK;
  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. }