kobj.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * kobj.c - identify and parse a sparc object file
  3. */
  4. #include <u.h>
  5. #include <libc.h>
  6. #include <bio.h>
  7. #include <mach.h>
  8. #include "kc/k.out.h"
  9. #include "obj.h"
  10. typedef struct Addr Addr;
  11. struct Addr
  12. {
  13. char type;
  14. char sym;
  15. char name;
  16. };
  17. static Addr addr(Biobuf*);
  18. static char type2char(int);
  19. static void skip(Biobuf*, int);
  20. int
  21. _isk(char *s)
  22. {
  23. return s[0] == ANAME /* ANAME */
  24. && s[1] == D_FILE /* type */
  25. && s[2] == 1 /* sym */
  26. && s[3] == '<'; /* name of file */
  27. }
  28. int
  29. _readk(Biobuf *bp, Prog *p)
  30. {
  31. int as, n;
  32. Addr a;
  33. as = Bgetc(bp); /* as */
  34. if(as < 0)
  35. return 0;
  36. p->kind = aNone;
  37. p->sig = 0;
  38. if(as == ANAME || as == ASIGNAME){
  39. if(as == ASIGNAME){
  40. Bread(bp, &p->sig, 4);
  41. p->sig = beswal(p->sig);
  42. }
  43. p->kind = aName;
  44. p->type = type2char(Bgetc(bp)); /* type */
  45. p->sym = Bgetc(bp); /* sym */
  46. n = 0;
  47. for(;;) {
  48. as = Bgetc(bp);
  49. if(as < 0)
  50. return 0;
  51. n++;
  52. if(as == 0)
  53. break;
  54. }
  55. p->id = malloc(n);
  56. if(p->id == 0)
  57. return 0;
  58. Bseek(bp, -n, 1);
  59. if(Bread(bp, p->id, n) != n)
  60. return 0;
  61. return 1;
  62. }
  63. if(as == ATEXT)
  64. p->kind = aText;
  65. else if(as == AGLOBL)
  66. p->kind = aData;
  67. skip(bp, 5); /* reg (1 byte); lineno (4 bytes) */
  68. a = addr(bp);
  69. addr(bp);
  70. if(a.type != D_OREG || 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: case D_PREG:
  87. break;
  88. case D_BRANCH:
  89. case D_OREG:
  90. case D_ASI:
  91. case D_CONST:
  92. off = Bgetc(bp);
  93. off |= Bgetc(bp) << 8;
  94. off |= Bgetc(bp) << 16;
  95. off |= Bgetc(bp) << 24;
  96. if(off < 0)
  97. off = -off;
  98. if(a.sym!=0 && (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. }