1 | /*****************************************************************************/ |
2 | /* */ |
3 | /* o65.h */ |
4 | /* */ |
5 | /* Definitions and code for the o65 file format */ |
6 | /* */ |
7 | /* */ |
8 | /* */ |
9 | /* (C) 2002-2003 Ullrich von Bassewitz */ |
10 | /* Roemerstrasse 52 */ |
11 | /* D-70794 Filderstadt */ |
12 | /* EMail: uz@cc65.org */ |
13 | /* */ |
14 | /* */ |
15 | /* This software is provided 'as-is', without any expressed or implied */ |
16 | /* warranty. In no event will the authors be held liable for any damages */ |
17 | /* arising from the use of this software. */ |
18 | /* */ |
19 | /* Permission is granted to anyone to use this software for any purpose, */ |
20 | /* including commercial applications, and to alter it and redistribute it */ |
21 | /* freely, subject to the following restrictions: */ |
22 | /* */ |
23 | /* 1. The origin of this software must not be misrepresented; you must not */ |
24 | /* claim that you wrote the original software. If you use this software */ |
25 | /* in a product, an acknowledgment in the product documentation would be */ |
26 | /* appreciated but is not required. */ |
27 | /* 2. Altered source versions must be plainly marked as such, and must not */ |
28 | /* be misrepresented as being the original software. */ |
29 | /* 3. This notice may not be removed or altered from any source */ |
30 | /* distribution. */ |
31 | /* */ |
32 | /*****************************************************************************/ |
33 | |
34 | |
35 | |
36 | /* This files exports structures and constants to handle the o65 relocatable |
37 | ** file format as defined by Andre Fachat. See the original document under |
38 | ** |
39 | ** http://www.6502.org/users/andre/o65/fileformat.html |
40 | ** |
41 | ** for more information. |
42 | */ |
43 | |
44 | |
45 | |
46 | #ifndef _O65_H |
47 | #define _O65_H |
48 | |
49 | |
50 | |
51 | /* common */ |
52 | #include "coll.h" |
53 | |
54 | |
55 | |
56 | /*****************************************************************************/ |
57 | /* Defines */ |
58 | /*****************************************************************************/ |
59 | |
60 | |
61 | |
62 | /* Define a structure for the o65 file header */ |
63 | typedef struct O65Header ; |
64 | struct { |
65 | char [2]; /* Non-C64 marker */ |
66 | char [3]; /* o65 magic */ |
67 | char ; /* Version number */ |
68 | unsigned ; /* Mode word */ |
69 | unsigned long ; /* Original text (code) segment address */ |
70 | unsigned long ; /* Size of text (code) segment */ |
71 | unsigned long ; /* Original data segment address */ |
72 | unsigned long ; /* Size of data segment */ |
73 | unsigned long ; /* Original bss segment address */ |
74 | unsigned long ; /* Size of bss segment */ |
75 | unsigned long ; /* Original zp segment address */ |
76 | unsigned long ; /* Size of zp segment */ |
77 | unsigned long ; /* Stacksize needed */ |
78 | }; |
79 | |
80 | /* o65 option */ |
81 | typedef struct O65Option O65Option; |
82 | struct O65Option { |
83 | unsigned char Len; /* Option length */ |
84 | unsigned char Type; /* Option type */ |
85 | unsigned char Data[1]; /* Option data (dynamically allocated) */ |
86 | }; |
87 | |
88 | /* o65 relocation entry */ |
89 | typedef struct O65Reloc O65Reloc; |
90 | struct O65Reloc { |
91 | unsigned long Offs; /* Offset in segment */ |
92 | unsigned char Type; /* Relocation type */ |
93 | unsigned char SegID; /* Segment ID */ |
94 | unsigned Val; /* Any offset value needed for relocation */ |
95 | unsigned long SymIdx; /* Index into list of imported symbols */ |
96 | }; |
97 | |
98 | /* o65 import */ |
99 | typedef struct O65Import O65Import; |
100 | struct O65Import { |
101 | char Name[1]; /* Name of the import (dynamically allocated) */ |
102 | }; |
103 | |
104 | /* o65 export */ |
105 | typedef struct O65Export O65Export; |
106 | struct O65Export { |
107 | unsigned char SegID; /* Segment ID */ |
108 | unsigned long Val; /* Relocation value */ |
109 | char Name[1]; /* Name of the export (dynamically allocated) */ |
110 | }; |
111 | |
112 | /* Complete o65 file data */ |
113 | typedef struct O65Data O65Data; |
114 | struct O65Data { |
115 | O65Header ; /* File header */ |
116 | Collection Options; /* O65 options */ |
117 | unsigned char* Text; /* Text segment data (unrelocated) */ |
118 | unsigned char* Data; /* Data segment data (unrelocated) */ |
119 | Collection TextReloc; /* Relocation entries for the text segment */ |
120 | Collection DataReloc; /* Relocation entries for the data segment */ |
121 | Collection Imports; /* Imported symbols */ |
122 | Collection Exports; /* Exported symbols */ |
123 | }; |
124 | |
125 | |
126 | |
127 | /* Marker, magic and version number */ |
128 | #define O65_MARKER_0 0x01 |
129 | #define O65_MARKER_1 0x00 |
130 | #define O65_MAGIC_0 0x6F /* 'o' */ |
131 | #define O65_MAGIC_1 0x36 /* '6' */ |
132 | #define O65_MAGIC_2 0x35 /* '5' */ |
133 | #define O65_VERSION 0x00 |
134 | |
135 | /* Defines for the mode word */ |
136 | #define O65_CPU_65816 0x8000 /* Executable is for 65816 */ |
137 | #define O65_CPU_6502 0x0000 /* Executable is for the 6502 */ |
138 | #define O65_CPU_MASK 0x8000 /* Mask to extract CPU type */ |
139 | |
140 | #define O65_RELOC_PAGE 0x4000 /* Page wise relocation */ |
141 | #define O65_RELOC_BYTE 0x0000 /* Byte wise relocation */ |
142 | #define O65_RELOC_MASK 0x4000 /* Mask to extract relocation type */ |
143 | |
144 | #define O65_SIZE_32BIT 0x2000 /* All size words are 32bit */ |
145 | #define O65_SIZE_16BIT 0x0000 /* All size words are 16bit */ |
146 | #define O65_SIZE_MASK 0x2000 /* Mask to extract size */ |
147 | |
148 | #define O65_FTYPE_OBJ 0x1000 /* Object file */ |
149 | #define O65_FTYPE_EXE 0x0000 /* Executable file */ |
150 | #define O65_FTYPE_MASK 0x1000 /* Mask to extract type */ |
151 | |
152 | #define O65_ADDR_SIMPLE 0x0800 /* Simple addressing */ |
153 | #define O65_ADDR_DEFAULT 0x0000 /* Default addressing */ |
154 | #define O65_ADDR_MASK 0x0800 /* Mask to extract addressing */ |
155 | |
156 | #define O65_ALIGN_1 0x0000 /* Bytewise alignment */ |
157 | #define O65_ALIGN_2 0x0001 /* Align words */ |
158 | #define O65_ALIGN_4 0x0002 /* Align longwords */ |
159 | #define O65_ALIGN_256 0x0003 /* Align pages (256 bytes) */ |
160 | #define O65_ALIGN_MASK 0x0003 /* Mask to extract alignment */ |
161 | |
162 | /* The mode word as generated by the ld65 linker */ |
163 | #define O65_MODE_CC65 (O65_CPU_6502 | \ |
164 | O65_RELOC_BYTE | \ |
165 | O65_SIZE_16BIT | \ |
166 | O65_FTYPE_EXE | \ |
167 | O65_ADDR_SIMPLE | \ |
168 | O65_ALIGN_1) |
169 | |
170 | /* The four o65 segment types. */ |
171 | #define O65_SEGID_UNDEF 0x00 |
172 | #define O65_SEGID_ABS 0x01 |
173 | #define O65_SEGID_TEXT 0x02 |
174 | #define O65_SEGID_DATA 0x03 |
175 | #define O65_SEGID_BSS 0x04 |
176 | #define O65_SEGID_ZP 0x05 |
177 | #define O65_SEGID_MASK 0x07 |
178 | |
179 | /* Relocation type codes */ |
180 | #define O65_RTYPE_WORD 0x80 |
181 | #define O65_RTYPE_HIGH 0x40 |
182 | #define O65_RTYPE_LOW 0x20 |
183 | #define O65_RTYPE_SEGADDR 0xC0 |
184 | #define O65_RTYPE_SEG 0xA0 |
185 | #define O65_RTYPE_MASK 0xE0 |
186 | |
187 | /* Segment IDs */ |
188 | #define O65_SEGID_UNDEF 0x00 |
189 | #define O65_SEGID_ABS 0x01 |
190 | #define O65_SEGID_TEXT 0x02 |
191 | #define O65_SEGID_DATA 0x03 |
192 | #define O65_SEGID_BSS 0x04 |
193 | #define O65_SEGID_ZP 0x05 |
194 | #define O65_SEGID_MASK 0x07 |
195 | |
196 | /* Option tags */ |
197 | #define O65_OPT_FILENAME 0 |
198 | #define O65_OPT_OS 1 |
199 | #define O65_OPT_ASM 2 |
200 | #define O65_OPT_AUTHOR 3 |
201 | #define O65_OPT_TIMESTAMP 4 |
202 | |
203 | /* Operating system codes for O65_OPT_OS */ |
204 | #define O65_OS_OSA65 1 |
205 | #define O65_OS_LUNIX 2 |
206 | #define O65_OS_CC65_MODULE 3 |
207 | |
208 | |
209 | |
210 | /*****************************************************************************/ |
211 | /* Code */ |
212 | /*****************************************************************************/ |
213 | |
214 | |
215 | |
216 | O65Data* ReadO65File (const char* Name); |
217 | /* Read a complete o65 file into dynamically allocated memory and return the |
218 | ** created O65Data struct. |
219 | */ |
220 | |
221 | const char* GetO65OSName (unsigned char OS); |
222 | /* Return the name of the operating system given by OS */ |
223 | |
224 | const char* GetO65OptionText (const O65Option* O); |
225 | /* Return the data of the given option as a readable text. The function returns |
226 | ** a pointer to a static buffer that is reused on the next call, so if in doubt, |
227 | ** make a copy (and no, the function is not thread safe). |
228 | */ |
229 | |
230 | |
231 | |
232 | /* End of o65.h */ |
233 | |
234 | #endif |
235 | |