3 * routines universal to all object files
12 #define islocal(t) ((t)=='a' || (t)=='p')
17 MAXIS = 8, /* max length to determine if a file is a .? file */
18 MAXOFF = 0x7fffffff, /* larger than any possible local offset */
19 NHASH = 1024, /* must be power of two */
23 int _is2(char*), /* in [$OS].c */
33 _read2(Biobuf*, Prog*),
34 _read5(Biobuf*, Prog*),
35 _read6(Biobuf*, Prog*),
36 _read7(Biobuf*, Prog*),
37 _read8(Biobuf*, Prog*),
38 _read9(Biobuf*, Prog*),
39 _readk(Biobuf*, Prog*),
40 _readq(Biobuf*, Prog*),
41 _readv(Biobuf*, Prog*),
42 _readu(Biobuf*, Prog*);
44 typedef struct Obj Obj;
45 typedef struct Symtab Symtab;
47 struct Obj /* functions to handle each intermediate (.$O) file */
49 char *name; /* name of each $O file */
50 int (*is)(char*); /* test for each type of $O file */
51 int (*read)(Biobuf*, Prog*); /* read for each type of $O file*/
55 { /* functions to identify and parse each type of obj */
56 [Obj68020] "68020 .2", _is2, _read2,
57 [ObjAmd64] "amd64 .6", _is6, _read6,
58 [ObjArm] "arm .5", _is5, _read5,
59 [ObjArm64] "arm64 .7", _is7, _read7,
60 [Obj386] "386 .8", _is8, _read8,
61 [ObjSparc] "sparc .k", _isk, _readk,
62 [ObjPower] "power .q", _isq, _readq,
63 [ObjMips] "mips .v", _isv, _readv,
64 [ObjSparc64] "sparc64 .u", _isu, _readu,
65 [ObjPower64] "power64 .9", _is9, _read9,
75 static Symtab *hash[NHASH];
76 static Sym *names[NNAMES]; /* working set of active names */
78 static int processprog(Prog*,int); /* decode each symbol reference */
79 static void objreset(void);
80 static void objlookup(int, char *, int, uint);
81 static void objupdate(int, int);
84 objtype(Biobuf *bp, char **name)
89 if(Bread(bp, buf, MAXIS) < MAXIS)
92 for (i = 0; i < Maxobjtype; i++) {
93 if (obj[i].is && (*obj[i].is)(buf)) {
108 n = Bread(bp, magbuf, SARMAG);
109 if(n == SARMAG && strncmp(magbuf, ARMAG, SARMAG) == 0)
115 * determine what kind of object file this is and process it.
116 * return whether or not this was a recognized intermediate file.
119 readobj(Biobuf *bp, int objtype)
123 if (objtype < 0 || objtype >= Maxobjtype || obj[objtype].is == 0)
126 while ((*obj[objtype].read)(bp, &p))
127 if (!processprog(&p, 1))
133 readar(Biobuf *bp, int objtype, vlong end, int doautos)
137 if (objtype < 0 || objtype >= Maxobjtype || obj[objtype].is == 0)
140 while ((*obj[objtype].read)(bp, &p) && Boffset(bp) < end)
141 if (!processprog(&p, doautos))
147 * decode a symbol reference or definition
150 processprog(Prog *p, int doautos)
154 if(p->sym < 0 || p->sym >= NNAMES)
160 if(p->type != 'U' && p->type != 'b')
162 objlookup(p->sym, p->id, p->type, p->sig);
165 objupdate(p->sym, 'T');
168 objupdate(p->sym, 'D');
177 * find the entry for s in the symbol array.
178 * make a new entry if it is not already there.
181 objlookup(int id, char *name, int type, uint sig)
189 if(s && strcmp(s->name, name) == 0) {
196 for(cp = name+1; *cp; h += *cp++)
201 if (type == 'U' || type == 'b' || islocal(type)) {
202 for(sp = hash[h]; sp; sp = sp->next)
203 if(strcmp(sp->s.name, name) == 0) {
233 sp = malloc(sizeof(Symtab));
237 sp->s.value = islocal(type) ? MAXOFF : 0;
244 * traverse the symbol lists
247 objtraverse(void (*fn)(Sym*, void*), void *pointer)
252 for(i = 0; i < NHASH; i++)
253 for(s = hash[i]; s; s = s->next)
254 (*fn)(&s->s, pointer);
258 * update the offset information for a 'a' or 'p' symbol in an intermediate file
261 _offset(int id, vlong off)
266 if (s && s->name[0] && islocal(s->type) && s->value > off)
271 * update the type of a global text or data symbol
274 objupdate(int id, int type)
282 else if (s->type == 'b')
283 s->type = tolower(type);
287 * look for the next file in an archive
290 nextar(Biobuf *bp, int offset, char *buf)
298 Bseek(bp, offset, 0);
299 r = Bread(bp, &a, SAR_HDR);
302 if(strncmp(a.fmag, ARFMAG, sizeof(a.fmag)))
304 for(i=0; i<sizeof(a.name) && i<SARNAME && a.name[i] != ' '; i++)
307 arsize = strtol(a.size, 0, 0);
310 return arsize + SAR_HDR;
319 for(i = 0; i < NHASH; i++) {
320 for(s = hash[i]; s; s = n) {
327 memset(names, 0, sizeof names);