2 * dissect an aof file into areas
5 /*----- Licensing note ----------------------------------------------------*
7 * This file is part of Straylight's Dynamic Linking System (SDLS)
9 * SDLS is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2, or (at your option)
14 * SDLS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with SDLS. If not, write to the Free Software Foundation,
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include "aof/chunk.h"
26 #define aof_RELOC (0xC5E2D080ul) /* Relocatable object type */
30 unsigned long type; /* Type of object file */
31 int version; /* Version number of the format */
32 int areas; /* Number of AREAs defined */
33 int symbols; /* Number of symbols in table */
34 int entryArea; /* Index of AREA with ENTRY att */
35 int entryOff; /* Offset in AREA for ENTRY */
41 int name; /* AREA name (OBJ_STRT offset) */
43 int alignment :8; /* AREA alignment (must be 2) */
45 int :1; /* Reserved bit */
46 int code :1; /* AREA contains code */
47 int common :1; /* Common AREA definition */
48 int commonRef :1; /* Reference to common AREA */
49 int zinit :1; /* AREA is zero-initialised */
50 int readonly :1; /* AREA is (sort-of) readonly */
51 int :1; /* Reserved bit */
52 int debug :1; /* AREA contains debug tables */
53 int :16; /* Reserved shortword */
55 int size; /* Size of this AREA */
56 int relocs; /* Number of relocations */
57 int :32; /* Reserved word */
63 aof_fixedHeader hdr; /* The fixed header info */
64 aof_areaEntry table[1]; /* AREA table (unsized array) */
70 int name; /* Name string table entry */
72 int defined :1; /* Symbol defined in file */
73 int export :1; /* Symbol is exported globally */
74 int absolute :1; /* Symbol not relative to AREA */
75 int ignoreCase :1; /* Symbol is not case sensitive */
76 int weak :1; /* Symbol is weak external ref */
77 int strong :1; /* Symbol is strong global */
78 int common :1; /* Symbol is in a common AREA */
79 int :32-7; /* Pad out to integer boundary */
81 int value; /* Value of the symbol */
82 int area; /* Offset of AREA name */
88 int offset; /* Offset of word to relocate */
93 int symbol :16; /* Symbol to relocate by/to */
94 int field :2; /* Field size to alter */
95 int type :1; /* Relocation type */
96 int symreloc :1; /* Relocation is symbol-relative*/
97 int :12; /* Reserved bits */
99 type_1; /* Type 1 relocation directive */
103 int symbol :24; /* Symbol to relocate by/to */
104 int field :2; /* Field size to alter */
105 int type :1; /* Relocation type */
106 int symreloc :1; /* Relocation is symbol-relative*/
107 int :3; /* Reserved bits */
108 int set_me :1; /* Set this bit for type 2 */
110 type_2; /* Type 2 relocation directive */
135 static char *nicely(char *name)
149 static int oscli(char *cmd,...)
154 vsprintf(buffer,cmd,ap);
156 return (system(buffer));
159 static int findChunk(chunk_header *c,char *name)
162 for (i=0;i<c->hdr.maxChunks;i++)
164 if (c->table[i].offset && !memcmp(c->table[i].chunkName,name,8))
170 static void get(void *p,FILE *fp,size_t size,size_t offset)
178 static int dissect(char *aof,char *dir)
182 chunk_fixedHeader ch={0,0,0};
194 oscli("cdir %s",dir);
198 fprintf(stderr,"couldn't open '%s'\n",aof);
201 fread(&ch,sizeof(ch),1,fp);
202 if (ch.id!=chunk_MAGIC)
204 fprintf(stderr,"bad aof '%s'\n",aof);
207 chf=malloc(sizeof(chunk_fixedHeader)+
208 ch.maxChunks*sizeof(chunk_tableEntry));
211 fprintf(stderr,"no memory\n");
215 fread(chf->table,sizeof(chunk_tableEntry),ch.maxChunks,fp);
217 h=findChunk(chf,"OBJ_HEAD");
218 a=findChunk(chf,"OBJ_AREA");
219 s=findChunk(chf,"OBJ_STRT");
220 if (h==-1 || a==-1 || s==-1)
222 fprintf(stderr,"bad aof '%s'\n",aof);
226 header=malloc(chf->table[h].size);
227 area=malloc(chf->table[a].size);
228 string=malloc(chf->table[s].size);
229 if (!header || !area || !string)
231 fprintf(stderr,"no memory\n");
235 get(header,fp,chf->table[h].size,chf->table[h].offset);
236 get(area,fp,chf->table[a].size,chf->table[a].offset);
237 get(string,fp,chf->table[s].size,chf->table[s].offset);
239 for (i=0;i<header->hdr.areas;i++)
241 if (header->table[i].zinit)
243 sprintf(name,"%s.%s",dir,nicely(string+header->table[i].name));
244 out=fopen(name,"wb");
247 fprintf(stderr,"couldn't write '%s'\n",name);
250 fwrite(area+bit,1,header->table[i].size,out);
251 bit+=header->table[i].size+header->table[i].relocs*8;
257 int main(int argc,char *argv[])
261 fprintf(stderr,"dissect <aof> <dir>\n");
264 return (dissect(argv[1],argv[2]));