7obj.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * 7obj.c - identify and parse an alpha object file
  3. */
  4. #include <u.h>
  5. #include <libc.h>
  6. #include <bio.h>
  7. #include "7c/7.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. _is7(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. _read7(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 || as == ASIGNAME){
  37. if(as == ASIGNAME)
  38. skip(bp, 4); /* signature */
  39. p->kind = aName;
  40. p->type = type2char(Bgetc(bp)); /* type */
  41. p->sym = Bgetc(bp); /* sym */
  42. n = 0;
  43. for(;;) {
  44. as = Bgetc(bp);
  45. if(as < 0)
  46. return 0;
  47. n++;
  48. if(as == 0)
  49. break;
  50. }
  51. p->id = malloc(n);
  52. if(p->id == 0)
  53. return 0;
  54. Bseek(bp, -n, 1);
  55. if(Bread(bp, p->id, n) != n)
  56. return 0;
  57. return 1;
  58. }
  59. if(as == ATEXT)
  60. p->kind = aText;
  61. else if(as == AGLOBL)
  62. p->kind = aData;
  63. skip(bp, 5); /* reg(1), lineno(4) */
  64. a = addr(bp);
  65. addr(bp);
  66. if(a.type != D_OREG || a.name != D_STATIC && a.name != D_EXTERN)
  67. p->kind = aNone;
  68. p->sym = a.sym;
  69. return 1;
  70. }
  71. static Addr
  72. addr(Biobuf *bp)
  73. {
  74. Addr a;
  75. vlong off;
  76. a.type = Bgetc(bp); /* a.type */
  77. skip(bp,1); /* reg */
  78. a.sym = Bgetc(bp); /* sym index */
  79. a.name = Bgetc(bp); /* sym type */
  80. switch(a.type){
  81. default:
  82. case D_NONE: case D_REG: case D_FREG: case D_PREG:
  83. case D_FCREG: case D_PCC:
  84. break;
  85. case D_OREG:
  86. case D_CONST:
  87. case D_BRANCH:
  88. off = (uvlong)Bgetc(bp);
  89. off |= (uvlong)Bgetc(bp) << 8;
  90. off |= (uvlong)Bgetc(bp) << 16;
  91. off |= (uvlong)Bgetc(bp) << 24;
  92. off |= (uvlong)Bgetc(bp) << 32;
  93. off |= (uvlong)Bgetc(bp) << 40;
  94. off |= (uvlong)Bgetc(bp) << 48;
  95. off |= (uvlong)Bgetc(bp) << 56;
  96. if(off < 0)
  97. off = -off;
  98. if(a.sym && (a.name==D_PARAM || a.name==D_AUTO))
  99. _offset(a.sym, off);
  100. break;
  101. case D_SCONST:
  102. skip(bp, NSNAME);
  103. break;
  104. case D_FCONST:
  105. skip(bp, 8);
  106. break;
  107. }
  108. return a;
  109. }
  110. static char
  111. type2char(int t)
  112. {
  113. switch(t){
  114. case D_EXTERN: return 'U';
  115. case D_STATIC: return 'b';
  116. case D_AUTO: return 'a';
  117. case D_PARAM: return 'p';
  118. default: return UNKNOWN;
  119. }
  120. }
  121. static void
  122. skip(Biobuf *bp, int n)
  123. {
  124. while (n-- > 0)
  125. Bgetc(bp);
  126. }