uobj.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * uobj.c - identify and parse a sparc64 object file
  3. */
  4. #include <u.h>
  5. #include <libc.h>
  6. #include <bio.h>
  7. #include "uc/u.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. _isu(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. _readu(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 byte); lineno (4 bytes) */
  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. long 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_CREG: case D_PREG:
  83. break;
  84. case D_BRANCH:
  85. case D_OREG:
  86. case D_ASI:
  87. case D_CONST:
  88. off = Bgetc(bp);
  89. off |= Bgetc(bp) << 8;
  90. off |= Bgetc(bp) << 16;
  91. off |= Bgetc(bp) << 24;
  92. if(off < 0)
  93. off = -off;
  94. if(a.sym!=0 && (a.name==D_PARAM || a.name==D_AUTO))
  95. _offset(a.sym, off);
  96. break;
  97. case D_SCONST:
  98. skip(bp, NSNAME);
  99. break;
  100. case D_FCONST:
  101. skip(bp, 8);
  102. break;
  103. }
  104. return a;
  105. }
  106. static char
  107. type2char(int t)
  108. {
  109. switch(t){
  110. case D_EXTERN: return 'U';
  111. case D_STATIC: return 'b';
  112. case D_AUTO: return 'a';
  113. case D_PARAM: return 'p';
  114. default: return UNKNOWN;
  115. }
  116. }
  117. static void
  118. skip(Biobuf *bp, int n)
  119. {
  120. while (n-- > 0)
  121. Bgetc(bp);
  122. }