chiark / gitweb /
Initial revision
[ssr] / StraySrc / SDLS / cdll / c / extentry
1 /*
2  * extentry.c
3  *
4  * Create DLL header and things
5  *
6  * © 1994-1998 Straylight
7  */
8
9 /*----- Licensing note ----------------------------------------------------*
10  *
11  * This file is part of Straylight's Dynamic Linking System (SDLS)
12  *
13  * SDLS is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2, or (at your option)
16  * any later version.
17  *
18  * SDLS is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with SDLS.  If not, write to the Free Software Foundation,
25  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26  */
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stddef.h>
32
33 #include "extentry.h"
34 #include "readdef.h"
35 #include "aof/aof.h"
36 #include "aof/chunk.h"
37 #include "hashtable.h"
38 #include "error.h"
39
40 typedef struct
41 {
42   aof_file f;
43   hashtable osym;
44   int symbol;
45 }
46 ext__thing;
47
48 static void ext__entries(char *p,void *handle)
49 {
50   ext__thing *t=handle;
51   aof_relocstr r;
52   static int entry[4]={0xE92D0007,              /* STMFD   sp!,{a1-a3}  */
53                        0xE59FC000,              /* LDR     ip,=address  */
54                        0xEA000000};             /* B       _dll_extentry */
55   char buf[256];
56   if (hash_find(t->osym,p))
57   {
58     r.offset=t->f.area->next+8;
59     r.t.type_1.symbol=t->symbol;
60     r.t.type_1.field=aof_FULLWORD;
61     r.t.type_1.type=aof_PCRELATIVE;
62     r.t.type_1.symreloc=1;
63     aof_add(r,t->f.reloc);
64
65     aof_reloc(p,t->f.area->next+12,0,&t->f);
66     sprintf(buf,"_extEntry_%s",p);
67     aof_addsym(buf,t->f.area->next,0,4,&t->f);
68     aof_add(entry,t->f.area);
69   }
70 }
71
72 void extentry(char *name,readdef_info *inf,hashtable osym)
73 {
74   FILE *fp;
75
76   fp=fopen(name,"wb");                  /* Open the output file         */
77   if (!fp)                              /* If the file wasn't opened    */
78     error(NOOPENOUT,name);              /* Better give an error         */
79   else
80   {
81     char _buf[sizeof(chunk_fixedHeader)+5*sizeof(chunk_tableEntry)];
82     chunk_header *h=(chunk_header *)&_buf; /* Allocate memory easily :-) */
83
84     aof_chunkinfo obj_idfn={0};
85     aof_chunkinfo obj_head={0};
86     aof_chunkinfo obj_area={0};
87     aof_chunkinfo obj_symt={0};
88     aof_chunkinfo obj_strt={0};
89     aof_chunkinfo reloc={0};
90     ext__thing t;
91
92     int o;
93
94     h->hdr.id=chunk_MAGIC;              /* Fill in magic thingy         */
95     h->hdr.maxChunks=h->hdr.numChunks=5; /* And number of chunks        */
96
97     t.f.area=&obj_area;
98     t.osym=osym;
99     t.f.symt=&obj_symt;
100     t.f.strt=&obj_strt;
101     t.f.reloc=&reloc;
102
103     aof_string("Straylight Dynamic Link Library building system 1.01",
104                  &obj_idfn);
105     aof_align(obj_idfn);
106
107     aof_int(0,&obj_strt);
108     aof_string("DLL$$ExtEntry",&obj_strt);
109
110     {
111       aof_symbol sym={0};
112       sym.name=aof_string("_dll_extentry",&obj_strt);
113       sym.defined=0;
114       sym.export=1;
115       t.symbol=aof_add(sym,&obj_symt)/sizeof(aof_symbol);
116     }
117
118     hash_enumerate(inf->vsym,ext__entries,&t);
119
120     aof_int((int)aof_RELOC,&obj_head);
121     aof_int(150,&obj_head);
122     aof_int(1,&obj_head);
123     aof_int(obj_symt.next/sizeof(aof_symbol),&obj_head);
124     aof_int(0,&obj_head);
125     aof_int(0,&obj_head);
126
127     aof_int(4,&obj_head);
128     aof_int(0x00002202,&obj_head);
129     aof_int(obj_area.next,&obj_head);
130     aof_int(reloc.next/8,&obj_head);
131     aof_int(0,&obj_head);
132
133     aof_addBlock(reloc.p,reloc.next,&obj_area);
134
135     aof_fill(obj_strt.next,0,&obj_strt);
136     aof_align(obj_strt);
137
138     memcpy(h->table[0].chunkName,"OBJ_IDFN",8);
139     memcpy(h->table[1].chunkName,"OBJ_HEAD",8);
140     memcpy(h->table[2].chunkName,"OBJ_AREA",8);
141     memcpy(h->table[3].chunkName,"OBJ_SYMT",8);
142     memcpy(h->table[4].chunkName,"OBJ_STRT",8);
143
144     o=sizeof(chunk_fixedHeader)+5*sizeof(chunk_tableEntry);
145
146     h->table[0].offset=o;
147     h->table[0].size=obj_idfn.next;
148     o+=obj_idfn.next;
149
150     h->table[1].offset=o;
151     h->table[1].size=obj_head.next;
152     o+=obj_head.next;
153
154     h->table[2].offset=o;
155     h->table[2].size=obj_area.next;
156     o+=obj_area.next;
157
158     h->table[3].offset=o;
159     h->table[3].size=obj_symt.next;
160     o+=obj_symt.next;
161
162     h->table[4].offset=o;
163     h->table[4].size=obj_strt.next;
164     o+=obj_strt.next;
165
166     fwrite(h,sizeof(chunk_fixedHeader)+5*sizeof(chunk_tableEntry),1,fp);
167     fwrite(obj_idfn.p,obj_idfn.next,1,fp);
168     fwrite(obj_head.p,obj_head.next,1,fp);
169     fwrite(obj_area.p,obj_area.next,1,fp);
170     fwrite(obj_symt.p,obj_symt.next,1,fp);
171     fwrite(obj_strt.p,obj_strt.next,1,fp);
172     fclose(fp);
173
174     free(obj_idfn.p);
175     free(obj_area.p);
176     free(obj_strt.p);
177     free(obj_head.p);
178     free(obj_symt.p);
179   }
180 }