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 "kc/k.out.h" 8 #include "obj.h" 9 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 21 22 int 23 _isk(char *s) 24 { 25 return s[0] == ANAME /* ANAME */ 26 && s[1] == D_FILE /* type */ 27 && s[2] == 1 /* sym */ 28 && s[3] == '<'; /* name of file */ 29 } 30 31 32 int 33 _readk(Biobuf *bp, Prog *p) 34 { 35 int as, n; 36 Addr a; 37 38 as = Bgetc(bp); /* as */ 39 if(as < 0) 40 return 0; 41 p->kind = aNone; 42 if(as == ANAME){ 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 76 static Addr 77 addr(Biobuf *bp) 78 { 79 Addr a; 80 long off; 81 82 a.type = Bgetc(bp); /* a.type */ 83 skip(bp, 1); /* reg */ 84 a.sym = Bgetc(bp); /* sym index */ 85 a.name = Bgetc(bp); /* sym type */ 86 switch(a.type) { 87 default: 88 case D_NONE: case D_REG: case D_FREG: case D_CREG: case D_PREG: 89 break; 90 case D_BRANCH: 91 case D_OREG: 92 case D_ASI: 93 case D_CONST: 94 off = Bgetc(bp); 95 off |= Bgetc(bp) << 8; 96 off |= Bgetc(bp) << 16; 97 off |= Bgetc(bp) << 24; 98 if(off < 0) 99 off = -off; 100 if(a.sym!=0 && (a.name==D_PARAM || a.name==D_AUTO)) 101 _offset(a.sym, off); 102 break; 103 case D_SCONST: 104 skip(bp, NSNAME); 105 break; 106 case D_FCONST: 107 skip(bp, 8); 108 break; 109 } 110 return a; 111 } 112 113 114 static char 115 type2char(int t) 116 { 117 switch(t){ 118 case D_EXTERN: return 'U'; 119 case D_STATIC: return 'b'; 120 case D_AUTO: return 'a'; 121 case D_PARAM: return 'p'; 122 default: return UNKNOWN; 123 } 124 } 125 126 static void 127 skip(Biobuf *bp, int n) 128 { 129 while (n-- > 0) 130 Bgetc(bp); 131 } 132