1 /* 2 * qobj.c - identify and parse a PowerPC object file 3 * forsyth@terzarima.net 4 */ 5 #include <u.h> 6 #include <libc.h> 7 #include <bio.h> 8 #include "qc/q.out.h" 9 #include "obj.h" 10 11 typedef struct Addr Addr; 12 struct Addr 13 { 14 char type; 15 char sym; 16 char name; 17 }; 18 static Addr addr(Biobuf*); 19 static char type2char(int); 20 static void skip(Biobuf*, int); 21 22 int 23 _isq(char *s) 24 { 25 return (s[0]&0377) == ANAME /* ANAME */ 26 && s[1] == D_FILE /* type */ 27 && s[2] == 1 /* sym */ 28 && s[3] == '<'; /* name of file */ 29 } 30 31 int 32 _readq(Biobuf *bp, Prog *p) 33 { 34 int as, n; 35 Addr a; 36 37 as = Bgetc(bp); /* as */ 38 if(as < 0) 39 return 0; 40 p->kind = aNone; 41 if(as == ANAME || as == ASIGNAME){ 42 if(as == ASIGNAME) 43 skip(bp, 4); /* signature */ 44 p->kind = aName; 45 p->type = type2char(Bgetc(bp)); /* type */ 46 p->sym = Bgetc(bp); /* sym */ 47 n = 0; 48 for(;;) { 49 as = Bgetc(bp); 50 if(as < 0) 51 return 0; 52 n++; 53 if(as == 0) 54 break; 55 } 56 p->id = malloc(n); 57 if(p->id == 0) 58 return 0; 59 Bseek(bp, -n, 1); 60 if(Bread(bp, p->id, n) != n) 61 return 0; 62 return 1; 63 } 64 if(as == ATEXT) 65 p->kind = aText; 66 else if(as == AGLOBL) 67 p->kind = aData; 68 n = Bgetc(bp); /* reg and flag */ 69 skip(bp, 4); /* lineno(4) */ 70 a = addr(bp); 71 if(n & 0x40) 72 addr(bp); 73 addr(bp); 74 if(a.type != D_OREG || a.name != D_STATIC && a.name != D_EXTERN) 75 p->kind = aNone; 76 p->sym = a.sym; 77 return 1; 78 } 79 80 static Addr 81 addr(Biobuf *bp) 82 { 83 Addr a; 84 long off; 85 86 a.type = Bgetc(bp); /* a.type */ 87 skip(bp,1); /* reg */ 88 a.sym = Bgetc(bp); /* sym index */ 89 a.name = Bgetc(bp); /* sym type */ 90 switch(a.type){ 91 default: 92 case D_NONE: case D_REG: case D_FREG: case D_CREG: 93 case D_FPSCR: case D_MSR: case D_SREG: 94 break; 95 case D_SPR: 96 case D_OREG: 97 case D_CONST: 98 case D_BRANCH: 99 off = Bgetc(bp); 100 off |= Bgetc(bp) << 8; 101 off |= Bgetc(bp) << 16; 102 off |= Bgetc(bp) << 24; 103 if(off < 0) 104 off = -off; 105 if(a.sym && (a.name==D_PARAM || a.name==D_AUTO)) 106 _offset(a.sym, off); 107 break; 108 case D_SCONST: 109 skip(bp, NSNAME); 110 break; 111 case D_FCONST: 112 skip(bp, 8); 113 break; 114 } 115 return a; 116 } 117 118 static char 119 type2char(int t) 120 { 121 switch(t){ 122 case D_EXTERN: return 'U'; 123 case D_STATIC: return 'b'; 124 case D_AUTO: return 'a'; 125 case D_PARAM: return 'p'; 126 default: return UNKNOWN; 127 } 128 } 129 130 static void 131 skip(Biobuf *bp, int n) 132 { 133 while (n-- > 0) 134 Bgetc(bp); 135 } 136