vobj.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * vobj.c - identify and parse a mips object file
  3. */
  4. #include <u.h>
  5. #include <libc.h>
  6. #include <bio.h>
  7. #include "vc/v.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. _isv(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. _readv(Biobuf *bp, Prog *p)
  29. {
  30. int as, n;
  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. skip(bp, 5); /* reg(1), lineno(4) */
  62. a = addr(bp);
  63. addr(bp);
  64. if(a.type != D_OREG || a.name != D_STATIC && a.name != D_EXTERN)
  65. p->kind = aNone;
  66. p->sym = a.sym;
  67. return 1;
  68. }
  69. static Addr
  70. addr(Biobuf *bp)
  71. {
  72. Addr a;
  73. long off;
  74. a.type = Bgetc(bp); /* a.type */
  75. skip(bp,1); /* reg */
  76. a.sym = Bgetc(bp); /* sym index */
  77. a.name = Bgetc(bp); /* sym type */
  78. switch(a.type){
  79. default:
  80. case D_NONE: case D_REG: case D_FREG: case D_MREG:
  81. case D_FCREG: case D_LO: case D_HI:
  82. break;
  83. case D_OREG:
  84. case D_CONST:
  85. case D_BRANCH:
  86. off = Bgetc(bp);
  87. off |= Bgetc(bp) << 8;
  88. off |= Bgetc(bp) << 16;
  89. off |= Bgetc(bp) << 24;
  90. if(off < 0)
  91. off = -off;
  92. if(a.sym && (a.name==D_PARAM || a.name==D_AUTO))
  93. _offset(a.sym, off);
  94. break;
  95. case D_SCONST:
  96. skip(bp, NSNAME);
  97. break;
  98. case D_FCONST:
  99. skip(bp, 8);
  100. break;
  101. }
  102. return a;
  103. }
  104. static char
  105. type2char(int t)
  106. {
  107. switch(t){
  108. case D_EXTERN: return 'U';
  109. case D_STATIC: return 'b';
  110. case D_AUTO: return 'a';
  111. case D_PARAM: return 'p';
  112. default: return UNKNOWN;
  113. }
  114. }
  115. static void
  116. skip(Biobuf *bp, int n)
  117. {
  118. while (n-- > 0)
  119. Bgetc(bp);
  120. }