|
|
1.1 root 1: /*
2: * 2obj.c - identify and parse a 68020 object file
3: */
4: #include <u.h>
5: #include <libc.h>
6: #include <bio.h>
7: #include "2c/2.out.h"
8: #include "obj.h"
9:
10: typedef struct Addr Addr;
11: struct Addr
12: {
13: char sym;
14: char flags;
15: };
16: static Addr addr(Biobuf *);
17: static char type2char(int);
18: static void skip(Biobuf*, int);
19:
20: int
21: _is2(char *t)
22: {
23: uchar *s = (uchar *)t;
24:
25: return s[0] == (ANAME&0xff) /* aslo = ANAME */
26: && s[1] == ((ANAME>>8)&0xff) /* ashi = ANAME */
27: && s[2] == D_FILE /* type */
28: && s[3] == 1 /* sym */
29: && s[4] == '<'; /* name of file */
30: }
31:
32: int
33: _read2(Biobuf *bp, Prog *p)
34: {
35: int as, n, c;
36: Addr a;
37:
38: as = Bgetc(bp); /* as(low) */
39: if(as < 0)
40: return 0;
41: c = Bgetc(bp); /* as(high) */
42: if(c < 0)
43: return 0;
44: as |= ((c & 0xff) << 8);
45: p->kind = aNone;
46: if(as == ANAME){
47: p->kind = aName;
48: p->type = type2char(Bgetc(bp)); /* type */
49: p->sym = Bgetc(bp); /* sym */
50: n = 0;
51: for(;;) {
52: as = Bgetc(bp);
53: if(as < 0)
54: return 0;
55: n++;
56: if(as == 0)
57: break;
58: }
59: p->id = malloc(n);
60: if(p->id == 0)
61: return 0;
62: Bseek(bp, -n, 1);
63: if(Bread(bp, p->id, n) != n)
64: return 0;
65: return 1;
66: }
67: if(as == ATEXT)
68: p->kind = aText;
69: else if(as == AGLOBL)
70: p->kind = aData;
71: skip(bp, 4); /*lineno: low, high, lowhigh, highigh*/
72: a = addr(bp);
73: addr(bp);
74: if(!(a.flags & T_SYM))
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: int t;
85: long off;
86:
87: a.flags = Bgetc(bp); /* flags */
88: a.sym = -1;
89: off = 0;
90: if(a.flags & T_FIELD)
91: skip(bp, 2);
92: if(a.flags & T_INDEX)
93: skip(bp, 7);
94: if(a.flags & T_OFFSET){
95: off = Bgetc(bp);
96: off |= Bgetc(bp) << 8;
97: off |= Bgetc(bp) << 16;
98: off |= Bgetc(bp) << 24;
99: if(off < 0)
100: off = -off;
101: }
102: if(a.flags & T_SYM)
103: a.sym = Bgetc(bp);
104: if(a.flags & T_FCONST)
105: skip(bp, 8);
106: else if(a.flags & T_SCONST)
107: skip(bp, NSNAME);
108: else{
109: t = Bgetc(bp);
110: if(a.flags & T_TYPE)
111: t |= Bgetc(bp) << 8;
112: t &= D_MASK;
113: if(a.sym > 0 && (t==D_PARAM || t==D_AUTO))
114: _offset(a.sym, off);
115: }
116: return a;
117: }
118:
119: static char
120: type2char(int t)
121: {
122: switch(t){
123: case D_EXTERN: return 'U';
124: case D_STATIC: return 'b';
125: case D_AUTO: return 'a';
126: case D_PARAM: return 'p';
127: default: return UNKNOWN;
128: }
129: }
130:
131: static void
132: skip(Biobuf *bp, int n)
133: {
134: while (n-- > 0)
135: Bgetc(bp);
136: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.