xobj.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * xobj.c - identify and parse a 3210 object file
  3. */
  4. #include <u.h>
  5. #include <libc.h>
  6. #include <bio.h>
  7. #include "xc/x.out.h"
  8. #include "obj.h"
  9. typedef struct Addr Addr;
  10. struct Addr
  11. {
  12. char type;
  13. char sym;
  14. char name;
  15. };
  16. static Addr addr(Biobuf*);
  17. static char type2char(int);
  18. static void skip(Biobuf*, int);
  19. int
  20. _isx(char *s)
  21. {
  22. return s[0] == ANAME /* ANAME */
  23. && s[1] == D_FILE /* type */
  24. && s[2] == 1 /* sym */
  25. && s[3] == '<'; /* name of file */
  26. }
  27. int
  28. _readx(Biobuf *bp, Prog *p)
  29. {
  30. int as, n, isf;
  31. Addr a;
  32. as = Bgetc(bp); /* as */
  33. if(as < 0)
  34. return 0;
  35. p->kind = aNone;
  36. if(as == ANAME){
  37. p->kind = aName;
  38. p->type = type2char(Bgetc(bp)); /* type */
  39. p->sym = Bgetc(bp); /* sym */
  40. n = 0;
  41. for(;;) {
  42. as = Bgetc(bp);
  43. if(as < 0)
  44. return 0;
  45. n++;
  46. if(as == 0)
  47. break;
  48. }
  49. p->id = malloc(n);
  50. if(p->id == 0)
  51. return 0;
  52. Bseek(bp, -n, 1);
  53. if(Bread(bp, p->id, n) != n)
  54. return 0;
  55. return 1;
  56. }
  57. if(as == ATEXT)
  58. p->kind = aText;
  59. else if(as == AGLOBL)
  60. p->kind = aData;
  61. Bgetc(bp); /* reg */
  62. isf = Bgetc(bp) >> 6; /* cc & flt */
  63. skip(bp, 4); /* lineno */
  64. a = addr(bp);
  65. addr(bp);
  66. if(isf > 1)
  67. addr(bp);
  68. if(isf > 2)
  69. addr(bp);
  70. if(a.type != D_NAME || a.name != D_STATIC && a.name != D_EXTERN)
  71. p->kind = aNone;
  72. p->sym = a.sym;
  73. return 1;
  74. }
  75. static Addr
  76. addr(Biobuf *bp)
  77. {
  78. Addr a;
  79. long off;
  80. a.type = Bgetc(bp); /* a.type */
  81. skip(bp, 1); /* reg */
  82. a.sym = Bgetc(bp); /* sym index */
  83. a.name = Bgetc(bp); /* sym type */
  84. switch(a.type) {
  85. default:
  86. case D_NONE: case D_REG: case D_FREG: case D_CREG:
  87. case D_INDREG: case D_INC: case D_DEC:
  88. break;
  89. case D_INCREG:
  90. Bgetc(bp);
  91. break;
  92. case D_BRANCH:
  93. case D_OREG:
  94. case D_NAME:
  95. case D_CONST:
  96. off = Bgetc(bp);
  97. off |= Bgetc(bp) << 8;
  98. off |= Bgetc(bp) << 16;
  99. off |= Bgetc(bp) << 24;
  100. if(off < 0)
  101. off = -off;
  102. if(a.sym!=0 && (a.name==D_PARAM || a.name==D_AUTO))
  103. _offset(a.sym, off);
  104. break;
  105. case D_SCONST:
  106. skip(bp, NSNAME);
  107. break;
  108. case D_FCONST:
  109. case D_AFCONST:
  110. skip(bp, 4);
  111. break;
  112. }
  113. return a;
  114. }
  115. static char
  116. type2char(int t)
  117. {
  118. switch(t){
  119. case D_EXTERN: return 'U';
  120. case D_STATIC: return 'b';
  121. case D_AUTO: return 'a';
  122. case D_PARAM: return 'p';
  123. default: return UNKNOWN;
  124. }
  125. }
  126. static void
  127. skip(Biobuf *bp, int n)
  128. {
  129. while (n-- > 0)
  130. Bgetc(bp);
  131. }