Initial revision
[ssr] / StraySrc / Libraries / Core / AOF / c / aof
1 /*
2 * aof.c
3 *
4 * Creating AOF files
5 *
6 * © 1994-1998 Straylight
7 */
8
9 /*----- Licensing note ----------------------------------------------------*
10 *
11 * This file is part of Straylight's AOF library (aoflib).
12 *
13 * Aoflib 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 * Aoflib 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 Aoflib. 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
32 #include "aof.h"
33 #include "chunk.h"
34
35 extern void aof_error(void);
36
37 int aof_fillBlock(void *p,size_t offset,size_t l,aof_chunkinfo *i)
38 {
39 void *xp;
40 if (offset+l>i->size)
41 {
42 i->size=(offset+l+255)&~255;
43 xp=realloc(i->p,i->size);
44 if (!xp)
45 aof_error();
46 i->p=xp;
47 }
48 memcpy(i->p+offset,p,l);
49 return (offset);
50 }
51
52 int aof_addBlock(void *p,size_t l,aof_chunkinfo *i)
53 {
54 aof_fillBlock(p,i->next,l,i);
55 i->next+=l;
56 return (i->next-l);
57 }
58
59 int aof_branch(int from,int to)
60 {
61 return (0xEA000000 | (((to-from-8) & 0x03FFFFFC) >> 2));
62 }
63
64 void aof_reloc(char *name,int offset,int weak,aof_file *f)
65 {
66 aof_symbol sym={0};
67 aof_relocstr r={0};
68 sym.name=aof_string(name,f->strt);
69 sym.defined=0;
70 sym.export=1;
71 sym.weak=weak;
72 r.offset=offset;
73 r.t.type_1.symbol=aof_add(sym,f->symt)/sizeof(aof_symbol);
74 r.t.type_1.field=aof_FULLWORD;
75 r.t.type_1.type=aof_ADDITIVE;
76 r.t.type_1.symreloc=1;
77
78 aof_add(r,f->reloc);
79 }
80
81 void aof_reloc_b(char *name,int offset,aof_file *f)
82 {
83 aof_symbol sym={0};
84 aof_relocstr r={0};
85 sym.name=aof_string(name,f->strt);
86 sym.defined=0;
87 sym.export=1;
88 r.offset=offset;
89 r.t.type_1.symbol=aof_add(sym,f->symt)/sizeof(aof_symbol);
90 r.t.type_1.field=aof_FULLWORD;
91 r.t.type_1.type=aof_PCRELATIVE;
92 r.t.type_1.symreloc=1;
93 aof_add(r,f->reloc);
94 }
95
96 void aof_roff(int offset,aof_file *f)
97 {
98 aof_relocstr r={0};
99 r.offset=offset;
100 r.t.type_1.field=aof_FULLWORD;
101 r.t.type_1.type=aof_ADDITIVE;
102 r.t.type_1.symreloc=0;
103 aof_add(r,f->reloc);
104 }
105
106 void aof_addsym(char *name,int offset,int local,int area,aof_file *f)
107 {
108 aof_symbol sym={0};
109 sym.name=aof_string(name,f->strt);
110 sym.defined=1;
111 sym.export=!local;
112 sym.value=offset;
113 sym.area=area;
114 aof_add(sym,f->symt);
115 }