1 /* $Id $ */
2 /** @file
3  * LX structures, types and defines.
4  */
5 
6 /*
7  * Copyright (c) 2006-2007 Knut St. Osmundsen <bird-kStuff-spamix@anduin.net>
8  *
9  * Permission is hereby granted, free of charge, to any person
10  * obtaining a copy of this software and associated documentation
11  * files (the "Software"), to deal in the Software without
12  * restriction, including without limitation the rights to use,
13  * copy, modify, merge, publish, distribute, sublicense, and/or sell
14  * copies of the Software, and to permit persons to whom the
15  * Software is furnished to do so, subject to the following
16  * conditions:
17  *
18  * The above copyright notice and this permission notice shall be
19  * included in all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
23  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28  * OTHER DEALINGS IN THE SOFTWARE.
29  */
30 
31 #ifndef ___k_kLdrFmts_lx_h___
32 #define ___k_kLdrFmts_lx_h___
33 
34 #include <k/kDefs.h>
35 #include <k/kTypes.h>
36 
37 
38 #ifndef IMAGE_OS2_SIGNATURE_LX
39 /** LX signature ("LX") */
40 # define IMAGE_LX_SIGNATURE  K_LE2H_U16('L' | ('X' << 8))
41 #endif
42 
43 #pragma pack(1)
44 
45 /**
46  * Linear eXecutable header.
47  * This structure is exactly 196 bytes long.
48  */
49 struct e32_exe
50 {
51     KU8                 e32_magic[2];
52     KU8                 e32_border;
53     KU8                 e32_worder;
54     KU32                e32_level;
55     KU16                e32_cpu;
56     KU16                e32_os;
57     KU32                e32_ver;
58     KU32                e32_mflags;
59     KU32                e32_mpages;
60     KU32                e32_startobj;
61     KU32                e32_eip;
62     KU32                e32_stackobj;
63     KU32                e32_esp;
64     KU32                e32_pagesize;
65     KU32                e32_pageshift;
66     /** The size of the fixup section.
67      * The fixup section consists of the fixup page table, the fixup record table,
68      * the import module table, and the import procedure name table.
69      */
70     KU32                e32_fixupsize;
71     KU32                e32_fixupsum;
72     /** The size of the resident loader section.
73      * This includes the object table, the object page map table, the resource table, the resident name table,
74      * the entry table, the module format directives table, and the page checksum table (?). */
75     KU32                e32_ldrsize;
76     /** The checksum of the loader section. 0 if not calculated. */
77     KU32                e32_ldrsum;
78     /** The offset of the object table relative to this structure. */
79     KU32                e32_objtab;
80     /** Count of objects. */
81     KU32                e32_objcnt;
82     /** The offset of the object page map table relative to this structure. */
83     KU32                e32_objmap;
84     /** The offset of the object iterated pages (whatever this is used for) relative to the start of the file. */
85     KU32                e32_itermap;
86     /** The offset of the resource table relative to this structure. */
87     KU32                e32_rsrctab;
88     /** The number of entries in the resource table. */
89     KU32                e32_rsrccnt;
90     /** The offset of the resident name table relative to this structure. */
91     KU32                e32_restab;
92     /** The offset of the entry (export) table relative to this structure. */
93     KU32                e32_enttab;
94     /** The offset of the module format directives table relative to this structure. */
95     KU32                e32_dirtab;
96     /** The number of entries in the module format directives table. */
97     KU32                e32_dircnt;
98     /** The offset of the fixup page table relative to this structure. */
99     KU32                e32_fpagetab;
100     /** The offset of the fixup record table relative to this structure. */
101     KU32                e32_frectab;
102     /** The offset of the import module name table relative to this structure. */
103     KU32                e32_impmod;
104     /** The number of entries in the import module name table. */
105     KU32                e32_impmodcnt;
106     /** The offset of the import procedure name table relative to this structure. */
107     KU32                e32_impproc;
108     /** The offset of the page checksum table relative to this structure. */
109     KU32                e32_pagesum;
110     /** The offset of the data pages relative to the start of the file. */
111     KU32                e32_datapage;
112     /** The number of preload pages (ignored). */
113     KU32                e32_preload;
114     /** The offset of the non-resident name table relative to the start of the file. */
115     KU32                e32_nrestab;
116     /** The size of the non-resident name table. */
117     KU32                e32_cbnrestab;
118     KU32                e32_nressum;
119     KU32                e32_autodata;
120     KU32                e32_debuginfo;
121     KU32                e32_debuglen;
122     KU32                e32_instpreload;
123     KU32                e32_instdemand;
124     KU32                e32_heapsize;
125     KU32                e32_stacksize;
126     KU8                 e32_res3[20];
127 };
128 
129 /** e32_magic[0] */
130 #define E32MAGIC1       'L'
131 /** e32_magic[1] */
132 #define E32MAGIC2       'X'
133 /** MAKEWORD(e32_magic[0], e32_magic[1]) */
134 #define E32MAGIC        0x584c
135 /** e32_border - little endian */
136 #define E32LEBO         0
137 /** e32_border - big endian */
138 #define E32BEBO         1
139 /** e32_worder - little endian */
140 #define E32LEWO         0
141 /** e32_worder - big endian */
142 #define E32BEWO         1
143 /** e32_level */
144 #define E32LEVEL        KU32_C(0)
145 /** e32_cpu - 80286 */
146 #define E32CPU286       1
147 /** e32_cpu - 80386 */
148 #define E32CPU386       2
149 /** e32_cpu - 80486 */
150 #define E32CPU486       3
151 /** e32_pagesize */
152 #define OBJPAGELEN      KU32_C(0x1000)
153 
154 
155 /** @name e32_mflags
156  * @{ */
157 /** App Type: Fullscreen only. */
158 #define E32NOPMW         KU32_C(0x00000100)
159 /** App Type: PM API. */
160 #define E32PMAPI         KU32_C(0x00000300)
161 /** App Type: PM VIO compatible. */
162 #define E32PMW           KU32_C(0x00000200)
163 /** Application type mask. */
164 #define E32APPMASK       KU32_C(0x00000300)
165 /** Executable module. */
166 #define E32MODEXE        KU32_C(0x00000000)
167 /** Dynamic link library (DLL / library) module. */
168 #define E32MODDLL        KU32_C(0x00008000)
169 /** Protected memory DLL. */
170 #define E32PROTDLL       KU32_C(0x00010000)
171 /** Physical Device Driver. */
172 #define E32MODPDEV       KU32_C(0x00020000)
173 /** Virtual Device Driver. */
174 #define E32MODVDEV       KU32_C(0x00028000)
175 /** Device driver */
176 #define E32DEVICE        E32MODPDEV
177 /** Dynamic link library (DLL / library) module. */
178 #define E32NOTP          E32MODDLL
179 /** Protected memory DLL. */
180 #define E32MODPROTDLL    (E32MODDLL | E32PROTDLL)
181 /** Module Type mask. */
182 #define E32MODMASK       KU32_C(0x00038000)
183 /** Not loadable (linker error). */
184 #define E32NOLOAD        KU32_C(0x00002000)
185 /** No internal fixups. */
186 #define E32NOINTFIX      KU32_C(0x00000010)
187 /** No external fixups (i.e. imports). */
188 #define E32NOEXTFIX      KU32_C(0x00000020)
189 /** System DLL, no internal fixups. */
190 #define E32SYSDLL        KU32_C(0x00000008)
191 /** Global (set) or per instance (cleared) library initialization. */
192 #define E32LIBINIT       KU32_C(0x00000004)
193 /** Global (set) or per instance (cleared) library termination. */
194 #define E32LIBTERM       KU32_C(0x40000000)
195 /** Indicates when set in an executable that the process isn't SMP safe. */
196 #define E32NOTMPSAFE     KU32_C(0x00080000)
197 /** @} */
198 
199 /** @name Relocations (aka Fixups).
200  * @{ */
201 typedef union _offset
202 {
203     KU16                offset16;
204     KU32                offset32;
205 } offset;
206 
207 /** A relocation.
208  * @remark this structure isn't very usable since LX relocations comes in too many size variations.
209  */
210 struct r32_rlc
211 {
212     KU8                 nr_stype;
213     KU8                 nr_flags;
214     KI16                r32_soff;
215     KU16                r32_objmod;
216 
217     union targetid
218     {
219         offset          intref;
220         union extfixup
221         {
222             offset      proc;
223             KU32        ord;
224         } extref;
225         struct addfixup
226         {
227             KU16        entry;
228             offset      addval;
229         } addfix;
230     } r32_target;
231     KU16                r32_srccount;
232     KU16                r32_chain;
233 };
234 
235 /** @name Some attempt at size constanstants.
236  * @{
237  */
238 #define RINTSIZE16      8
239 #define RINTSIZE32      10
240 #define RORDSIZE        8
241 #define RNAMSIZE16      8
242 #define RNAMSIZE32      10
243 #define RADDSIZE16      10
244 #define RADDSIZE32      12
245 /** @} */
246 
247 /** @name nr_stype (source flags)
248  * @{ */
249 #define NRSBYT          0x00
250 #define NRSSEG          0x02
251 #define NRSPTR          0x03
252 #define NRSOFF          0x05
253 #define NRPTR48         0x06
254 #define NROFF32         0x07
255 #define NRSOFF32        0x08
256 #define NRSTYP          0x0f
257 #define NRSRCMASK       0x0f
258 #define NRALIAS         0x10
259 #define NRCHAIN         0x20
260 /** @} */
261 
262 /** @name nr_flags (target flags)
263  * @{ */
264 #define NRRINT          0x00
265 #define NRRORD          0x01
266 #define NRRNAM          0x02
267 #define NRRENT          0x03
268 #define NRRTYP          0x03
269 #define NRADD           0x04
270 #define NRICHAIN        0x08
271 #define NR32BITOFF      0x10
272 #define NR32BITADD      0x20
273 #define NR16OBJMOD      0x40
274 #define NR8BITORD       0x80
275 /** @} */
276 
277 /** @} */
278 
279 
280 /** @name The Object Table (aka segment table)
281  * @{ */
282 
283 /** The Object Table Entry. */
284 struct o32_obj
285 {
286     /** The size of the object. */
287     KU32                o32_size;
288     /** The base address of the object. */
289     KU32                o32_base;
290     /** Object flags. */
291     KU32                o32_flags;
292     /** Page map index. */
293     KU32                o32_pagemap;
294     /** Page map size. (doesn't need to be o32_size >> page shift). */
295     KU32                o32_mapsize;
296     /** Reserved */
297     KU32                o32_reserved;
298 };
299 
300 /** @name o32_flags
301  * @{ */
302 /** Read access. */
303 #define OBJREAD         KU32_C(0x00000001)
304 /** Write access. */
305 #define OBJWRITE        KU32_C(0x00000002)
306 /** Execute access. */
307 #define OBJEXEC         KU32_C(0x00000004)
308 /** Resource object. */
309 #define OBJRSRC         KU32_C(0x00000008)
310 /** The object is discarable (i.e. don't swap, just load in pages from the executable).
311  * This overlaps a bit with object type. */
312 #define OBJDISCARD      KU32_C(0x00000010)
313 /** The object is shared. */
314 #define OBJSHARED       KU32_C(0x00000020)
315 /** The object has preload pages. */
316 #define OBJPRELOAD      KU32_C(0x00000040)
317 /** The object has invalid pages. */
318 #define OBJINVALID      KU32_C(0x00000080)
319 /** Non-permanent, link386 bug. */
320 #define LNKNONPERM      KU32_C(0x00000600)
321 /** Non-permanent, correct 'value'. */
322 #define OBJNONPERM      KU32_C(0x00000000)
323 /** Obj Type: The object is permanent and swappable. */
324 #define OBJPERM         KU32_C(0x00000100)
325 /** Obj Type: The object is permanent and resident (i.e. not swappable). */
326 #define OBJRESIDENT     KU32_C(0x00000200)
327 /** Obj Type: The object is resident and contigious. */
328 #define OBJCONTIG       KU32_C(0x00000300)
329 /** Obj Type: The object is permanent and long locable. */
330 #define OBJDYNAMIC      KU32_C(0x00000400)
331 /** Object type mask. */
332 #define OBJTYPEMASK     KU32_C(0x00000700)
333 /** x86: The object require an 16:16 alias. */
334 #define OBJALIAS16      KU32_C(0x00001000)
335 /** x86: Big/Default selector setting, i.e. toggle 32-bit or 16-bit. */
336 #define OBJBIGDEF       KU32_C(0x00002000)
337 /** x86: conforming selector setting (weird stuff). */
338 #define OBJCONFORM      KU32_C(0x00004000)
339 /** x86: IOPL. */
340 #define OBJIOPL         KU32_C(0x00008000)
341 /** @} */
342 
343 /** A Object Page Map Entry. */
344 struct o32_map
345 {
346     /** The file offset of the page. */
347     KU32                o32_pagedataoffset;
348     /** The number of bytes of raw page data. */
349     KU16                o32_pagesize;
350     /** Per page flags describing how the page is encoded in the file. */
351     KU16                o32_pageflags;
352 };
353 
354 /** @name o32 o32_pageflags
355  * @{
356  */
357 /** Raw page (uncompressed) in the file. */
358 #define VALID           KU16_C(0x0000)
359 /** RLE encoded page in file. */
360 #define ITERDATA        KU16_C(0x0001)
361 /** Invalid page, nothing in the file. */
362 #define INVALID         KU16_C(0x0002)
363 /** Zero page, nothing in file. */
364 #define ZEROED          KU16_C(0x0003)
365 /** range of pages (what is this?) */
366 #define RANGE           KU16_C(0x0004)
367 /** Compressed page in file. */
368 #define ITERDATA2       KU16_C(0x0005)
369 /** @} */
370 
371 
372 /** Iteration Record format (RLE compressed page). */
373 struct LX_Iter
374 {
375     /** Number of iterations. */
376     KU16                LX_nIter;
377     /** The number of bytes that's being iterated. */
378     KU16                LX_nBytes;
379     /** The bytes. */
380     KU8                 LX_Iterdata;
381 };
382 
383 /** @} */
384 
385 
386 /** A Resource Table Entry */
387 struct rsrc32
388 {
389     /** Resource Type. */
390     KU16                type;
391     /** Resource ID. */
392     KU16                name;
393     /** Resource size in bytes. */
394     KU32                cb;
395     /** The index of the object containing the resource. */
396     KU16                obj;
397     /** Offset of the resource that within the object. */
398     KU32                offset;
399 };
400 
401 
402 /** @name The Entry Table (aka Export Table)
403  * @{ */
404 
405 /** Entry bundle.
406  * Header descripting up to 255 entries that follows immediatly after this structure. */
407 struct b32_bundle
408 {
409     /** The number of entries. */
410     KU8                 b32_cnt;
411     /** The type of bundle. */
412     KU8                 b32_type;
413     /** The index of the object containing these entry points. */
414     KU16                b32_obj;
415 };
416 
417 /** @name b32_type
418  * @{ */
419 /** Empty bundle, filling up unused ranges of ordinals. */
420 #define EMPTY           0x00
421 /** 16-bit offset entry point. */
422 #define ENTRY16         0x01
423 /** 16-bit callgate entry point. */
424 #define GATE16          0x02
425 /** 32-bit offset entry point. */
426 #define ENTRY32         0x03
427 /** Forwarder entry point. */
428 #define ENTRYFWD        0x04
429 /** Typing information present indicator. */
430 #define TYPEINFO        0x80
431 /** @} */
432 
433 
434 /** Entry point. */
435 struct e32_entry
436 {
437     /** Entry point flags */
438     KU8                 e32_flags;      /* Entry point flags */
439     union entrykind
440     {
441         /** ENTRY16 or ENTRY32. */
442         offset          e32_offset;
443         /** GATE16 */
444         struct callgate
445         {
446             /** Offset into segment. */
447             KU16        offset;
448             /** The callgate selector */
449             KU16        callgate;
450         } e32_callgate;
451         /** ENTRYFWD */
452         struct fwd
453         {
454             /** Module ordinal number (i.e. into the import module table). */
455             KU16        modord;
456             /** Procedure name or ordinal number. */
457             KU32        value;
458         } e32_fwd;
459     } e32_variant;
460 };
461 
462 /** @name e32_flags
463  * @{ */
464 /** Exported entry (set) or private entry (clear). */
465 #define E32EXPORT       0x01
466 /** Uses shared data. */
467 #define E32SHARED       0x02
468 /** Parameter word count mask. */
469 #define E32PARAMS       0xf8
470 /** ENTRYFWD: Imported by ordinal (set) or by name (clear). */
471 #define FWD_ORDINAL     0x01
472 /** @} */
473 
474 /** @name dunno
475  * @{ */
476 #define FIXENT16        3
477 #define FIXENT32        5
478 #define GATEENT16       5
479 #define FWDENT          7
480 /** @} */
481 
482 #pragma pack()
483 
484 #endif
485 
486