6obj.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * 6obj.c - identify and parse an I960 object file
  3. */
  4. #include <u.h>
  5. #include <libc.h>
  6. #include <bio.h>
  7. #include "6c/6.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. _is6(char *t)
  20. {
  21. uchar *s = (uchar*)t;
  22. return s[0] == ANAME /* aslo = ANAME */
  23. && s[1] == D_FILE /* type */
  24. && s[2] == 1 /* sym */
  25. && s[3] == '<'; /* name of file */
  26. }
  27. int
  28. _read6(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. if(as == AGLOBL)
  60. p->kind = aData;
  61. skip(bp, 5); /* lineno(4 bytes), reg(1 byte)*/
  62. a = addr(bp);
  63. addr(bp);
  64. if(!(a.flags & T_SYM))
  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. int t;
  74. long off;
  75. off = 0;
  76. a.sym = -1;
  77. a.flags = Bgetc(bp); /* flags */
  78. if(a.flags & T_INDEX)
  79. skip(bp, 2);
  80. if(a.flags & T_OFFSET){
  81. off = Bgetc(bp);
  82. off |= Bgetc(bp) << 8;
  83. off |= Bgetc(bp) << 16;
  84. off |= Bgetc(bp) << 24;
  85. if(off < 0)
  86. off = -off;
  87. }
  88. if(a.flags & T_SYM)
  89. a.sym = Bgetc(bp);
  90. if(a.flags & T_FCONST)
  91. skip(bp, 8);
  92. else
  93. if(a.flags & T_SCONST)
  94. skip(bp, NSNAME);
  95. if(a.flags & T_TYPE) {
  96. t = Bgetc(bp);
  97. if(a.sym > 0 && (t==D_PARAM || t==D_AUTO))
  98. _offset(a.sym, off);
  99. }
  100. return a;
  101. }
  102. static char
  103. type2char(int t)
  104. {
  105. switch(t){
  106. case D_EXTERN: return 'U';
  107. case D_STATIC: return 'b';
  108. case D_AUTO: return 'a';
  109. case D_PARAM: return 'p';
  110. default: return UNKNOWN;
  111. }
  112. }
  113. static void
  114. skip(Biobuf *bp, int n)
  115. {
  116. while (n-- > 0)
  117. Bgetc(bp);
  118. }