Skip to content

Latest commit

 

History

History
335 lines (296 loc) · 8.43 KB

lib2ine.h

File metadata and controls

335 lines (296 loc) · 8.43 KB
 
Feb 26, 2018
Feb 26, 2018
1
2
3
4
5
6
7
8
/**
* 2ine; an OS/2 emulator for Linux.
*
* Please see the file LICENSE.txt in the source's root directory.
*
* This file written by Ryan C. Gordon.
*/
Feb 28, 2018
Feb 28, 2018
9
10
11
12
13
/* lib2ine is support code and data that is common between various native
reimplementations of the OS/2 API, and the OS/2 binary loader. */
#ifndef _INCL_LIB2INE_H_
#define _INCL_LIB2INE_H_ 1
14
Oct 6, 2016
Oct 6, 2016
15
16
17
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
18
Sep 21, 2016
Sep 21, 2016
19
20
#include <stdio.h>
#include <stdlib.h>
21
#include <stdint.h>
Sep 21, 2016
Sep 21, 2016
22
#include <string.h>
23
24
25
26
typedef uint8_t uint8;
typedef uint16_t uint16;
typedef uint32_t uint32;
Nov 2, 2016
Nov 2, 2016
27
typedef uint64_t uint64;
28
Sep 25, 2016
Sep 25, 2016
29
30
31
typedef int8_t sint8;
typedef int16_t sint16;
typedef int32_t sint32;
Nov 2, 2016
Nov 2, 2016
32
typedef int64_t sint64;
Sep 25, 2016
Sep 25, 2016
33
Sep 30, 2016
Sep 30, 2016
34
35
typedef unsigned int uint;
Oct 6, 2016
Oct 6, 2016
36
37
38
39
40
41
42
43
44
45
#ifndef _STUBBED
#define _STUBBED(x, what) do { \
static int seen_this = 0; \
if (!seen_this) { \
seen_this = 1; \
fprintf(stderr, "2INE " what ": %s at %s (%s:%d)\n", x, __FUNCTION__, __FILE__, __LINE__); \
} \
} while (0)
#endif
Oct 14, 2016
Oct 14, 2016
46
#if 0
Oct 6, 2016
Oct 6, 2016
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#ifndef STUBBED
#define STUBBED(x) _STUBBED(x, "STUBBED")
#endif
#ifndef FIXME
#define FIXME(x) _STUBBED(x, "FIXME")
#endif
#endif
#ifndef STUBBED
#define STUBBED(x) do {} while (0)
#endif
#ifndef FIXME
#define FIXME(x) do {} while (0)
#endif
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#pragma pack(push, 1)
typedef struct LxHeader
{
uint8 magic_l;
uint8 magic_x;
uint8 byte_order;
uint8 word_order;
uint32 lx_version;
uint16 cpu_type;
uint16 os_type;
uint32 module_version;
uint32 module_flags;
uint32 module_num_pages;
uint32 eip_object;
uint32 eip;
uint32 esp_object;
uint32 esp;
uint32 page_size;
uint32 page_offset_shift;
uint32 fixup_section_size;
uint32 fixup_section_checksum;
uint32 loader_section_size;
uint32 loader_section_checksum;
uint32 object_table_offset;
uint32 module_num_objects;
uint32 object_page_table_offset;
uint32 object_iter_pages_offset;
uint32 resource_table_offset;
uint32 num_resource_table_entries;
uint32 resident_name_table_offset;
uint32 entry_table_offset;
uint32 module_directives_offset;
uint32 num_module_directives;
uint32 fixup_page_table_offset;
uint32 fixup_record_table_offset;
uint32 import_module_table_offset;
uint32 num_import_mod_entries;
uint32 import_proc_table_offset;
uint32 per_page_checksum_offset;
uint32 data_pages_offset;
uint32 num_preload_pages;
uint32 non_resident_name_table_offset;
uint32 non_resident_name_table_len;
uint32 non_resident_name_table_checksum;
uint32 auto_ds_object_num;
uint32 debug_info_offset;
uint32 debug_info_len;
uint32 num_instance_preload;
uint32 num_instance_demand;
uint32 heapsize;
} LxHeader;
typedef struct LxObjectTableEntry
{
uint32 virtual_size;
uint32 reloc_base_addr;
uint32 object_flags;
uint32 page_table_index;
uint32 num_page_table_entries;
uint32 reserved;
} LxObjectTableEntry;
typedef struct LxObjectPageTableEntry
{
uint32 page_data_offset;
uint16 data_size;
uint16 flags;
} LxObjectPageTableEntry;
typedef struct LxResourceTableEntry
{
uint16 type_id;
uint16 name_id;
uint32 resource_size;
uint16 object;
uint32 offset;
} LxResourceTableEntry;
Mar 7, 2018
Mar 7, 2018
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
typedef struct NeHeader
{
uint8 magic_n;
uint8 magic_e;
uint8 linker_version;
uint8 linker_revision;
uint16 entry_table_offset;
uint16 entry_table_size;
uint32 crc32;
uint16 module_flags;
uint16 auto_data_segment;
uint16 dynamic_heap_size;
uint16 stack_size;
uint16 reg_ip;
uint16 reg_cs;
uint16 reg_sp;
uint16 reg_ss;
uint16 num_segment_table_entries;
uint16 num_module_ref_table_entries;
uint16 non_resident_name_table_size;
uint16 segment_table_offset;
uint16 resource_table_offset;
uint16 resident_name_table_offset;
uint16 module_reference_table_offset;
uint16 imported_names_table_offset;
uint32 non_resident_name_table_offset;
uint16 num_movable_entries;
uint16 sector_alignment_shift_count;
uint16 num_resource_entries;
uint8 exe_type;
Jun 13, 2018
Jun 13, 2018
170
171
uint8 os2_exe_flags;
uint8 reserved[8];
Mar 7, 2018
Mar 7, 2018
172
173
174
175
176
177
178
179
180
181
} NeHeader;
typedef struct NeSegmentTableEntry
{
uint16 offset;
uint16 size;
uint16 segment_flags;
uint16 minimum_allocation;
} NeSegmentTableEntry;
182
183
#pragma pack(pop)
Sep 21, 2016
Sep 21, 2016
184
185
186
typedef struct LxMmaps
{
Oct 27, 2016
Oct 27, 2016
187
void *mapped;
Sep 21, 2016
Sep 21, 2016
188
189
void *addr;
size_t size;
Jan 4, 2018
Jan 4, 2018
190
int prot;
Oct 27, 2016
Oct 27, 2016
191
uint16 alias; // 16:16 alias, if one.
Sep 21, 2016
Sep 21, 2016
192
193
} LxMmaps;
Oct 2, 2016
Oct 2, 2016
194
typedef struct LxExport
Sep 21, 2016
Sep 21, 2016
195
196
{
uint32 ordinal;
Oct 2, 2016
Oct 2, 2016
197
198
const char *name; // can be NULL
void *addr;
Oct 27, 2016
Oct 27, 2016
199
const LxMmaps *object;
Oct 2, 2016
Oct 2, 2016
200
} LxExport;
Sep 21, 2016
Sep 21, 2016
201
202
203
204
205
206
struct LxModule;
typedef struct LxModule LxModule;
struct LxModule
{
uint32 refcount;
Jun 6, 2018
Jun 6, 2018
207
208
209
210
211
212
213
int is_lx; // 1 if an LX module, 0 if NE.
union
{
LxHeader lx;
NeHeader ne;
} header;
Sep 21, 2016
Sep 21, 2016
214
char name[256]; // !!! FIXME: allocate this.
Jun 6, 2018
Jun 6, 2018
215
Sep 21, 2016
Sep 21, 2016
216
LxModule **dependencies;
Jun 6, 2018
Jun 6, 2018
217
218
uint32 num_dependencies;
Sep 21, 2016
Sep 21, 2016
219
LxMmaps *mmaps;
Jun 6, 2018
Jun 6, 2018
220
uint32 num_mmaps;
Oct 27, 2016
Oct 27, 2016
221
Oct 2, 2016
Oct 2, 2016
222
223
const LxExport *exports;
uint32 num_exports;
Sep 21, 2016
Sep 21, 2016
224
225
226
void *nativelib;
uint32 eip;
uint32 esp;
Jun 6, 2018
Jun 6, 2018
227
uint16 autodatasize; // only used for NE binaries.
Sep 26, 2016
Sep 26, 2016
228
int initialized;
Sep 30, 2016
Sep 30, 2016
229
char *os2path; // absolute path to module, in OS/2 format
Oct 2, 2016
Oct 2, 2016
230
// !!! FIXME: put this elsewhere?
Sep 30, 2016
Sep 30, 2016
231
uint32 signal_exception_focus_count;
Sep 21, 2016
Sep 21, 2016
232
233
234
235
LxModule *prev; // all loaded modules are in a doubly-linked list.
LxModule *next; // all loaded modules are in a doubly-linked list.
};
Oct 8, 2016
Oct 8, 2016
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#pragma pack(push, 1)
typedef struct LxTIB2
{
uint32 tib2_ultid;
uint32 tib2_ulpri;
uint32 tib2_version;
uint16 tib2_usMCCount;
uint16 tib2_fMCForceFlag;
} LxTIB2;
typedef struct LxTIB
{
void *tib_pexchain;
void *tib_pstack;
void *tib_pstacklimit;
LxTIB2 *tib_ptib2;
uint32 tib_version;
uint32 tib_ordinal;
} LxTIB;
Oct 17, 2016
Oct 17, 2016
255
256
257
258
259
260
261
262
263
264
265
266
typedef struct LxPIB
{
uint32 pib_ulpid;
uint32 pib_ulppid;
void *pib_hmte;
char *pib_pchcmd;
char *pib_pchenv;
uint32 pib_flstatus;
uint32 pib_ultype;
} LxPIB;
Oct 14, 2016
Oct 14, 2016
267
#pragma pack(pop)
Oct 8, 2016
Oct 8, 2016
268
Jan 15, 2018
Jan 15, 2018
269
270
271
272
273
274
275
276
// We put the 128 bytes of TLS slots (etc) after the TIB structs.
typedef struct LxPostTIB
{
uint32 tls[32];
void *anchor_block;
} LxPostTIB;
#define LXTIBSIZE (sizeof (LxTIB) + sizeof (LxTIB2) + sizeof (LxPostTIB))
Oct 16, 2016
Oct 16, 2016
277
Feb 28, 2018
Feb 28, 2018
278
279
#define LX_MAX_LDT_SLOTS 8192
Oct 8, 2016
Oct 8, 2016
280
typedef struct LxLoaderState
Sep 30, 2016
Sep 30, 2016
281
282
283
{
LxModule *loaded_modules;
LxModule *main_module;
Oct 27, 2016
Oct 27, 2016
284
uint8 main_tibspace[LXTIBSIZE];
Oct 17, 2016
Oct 17, 2016
285
LxPIB pib;
Feb 28, 2018
Feb 28, 2018
286
int using_lx_loader;
Oct 14, 2016
Oct 14, 2016
287
int subprocess;
Oct 17, 2016
Oct 17, 2016
288
int running;
Oct 27, 2016
Oct 27, 2016
289
int trace_native;
Jan 15, 2018
Jan 15, 2018
290
int trace_events;
Jul 10, 2018
Jul 10, 2018
291
292
293
294
char *disks[26]; // mount points, A: through Z: ... NULL if unmounted.
char *current_dir[26]; // current directory, per-disk, A: through Z: ... NULL if unmounted.
int current_disk; // 1==A:\\, 2==B:\\, etc.
uint32 diskmap; // 1<<0==drive A mounted, 1<<1==drive B mounted, etc.
Feb 28, 2018
Feb 28, 2018
295
uint8 main_tib_selector;
Jun 8, 2018
Jun 8, 2018
296
uint32 mainstacksize;
Oct 27, 2016
Oct 27, 2016
297
298
uint16 original_cs;
uint16 original_ds;
Nov 2, 2016
Nov 2, 2016
299
300
uint16 original_es;
uint16 original_ss;
Feb 28, 2018
Feb 28, 2018
301
uint32 *ldt; //[LX_MAX_LDT_SLOTS];
Oct 27, 2016
Oct 27, 2016
302
303
char *libpath;
uint32 libpathlen;
Oct 19, 2016
Oct 19, 2016
304
305
306
uint32 *tlspage;
uint32 tlsmask; // one bit for each TLS slot in use.
uint8 tlsallocs[32]; // number of TLS slots allocated in one block, indexed by starting block (zero if not the starting block).
Oct 28, 2016
Oct 28, 2016
307
void (*dosExit)(uint32 action, uint32 result);
Feb 28, 2018
Feb 28, 2018
308
309
310
void (*initOs2Tib)(uint8 *tibspace, void *_topOfStack, const size_t stacklen, const uint32 tid);
uint16 (*setOs2Tib)(uint8 *tibspace);
LxTIB *(*getOs2Tib)(void);
Oct 14, 2016
Oct 14, 2016
311
void (*deinitOs2Tib)(const uint16 selector);
Jun 8, 2018
Jun 8, 2018
312
313
void *(*allocSegment)(uint16 *selector, const int iscode);
void (*freeSegment)(const uint16 selector);
Jan 4, 2018
Jan 4, 2018
314
int (*findSelector)(const uint32 addr, uint16 *outselector, uint16 *outoffset, int iscode);
Oct 27, 2016
Oct 27, 2016
315
316
void (*freeSelector)(const uint16 selector);
void *(*convert1616to32)(const uint32 addr1616);
Oct 29, 2016
Oct 29, 2016
317
uint32 (*convert32to1616)(void *addr32);
Oct 17, 2016
Oct 17, 2016
318
LxModule *(*loadModule)(const char *modname);
Oct 18, 2016
Oct 18, 2016
319
char *(*makeUnixPath)(const char *os2path, uint32 *err);
Oct 28, 2016
Oct 28, 2016
320
void __attribute__((noreturn)) (*terminate)(const uint32 exitcode);
Sep 30, 2016
Sep 30, 2016
321
322
} LxLoaderState;
Feb 28, 2018
Feb 28, 2018
323
typedef const LxExport *(*LxNativeModuleInitEntryPoint)(uint32 *lx_num_exports);
Oct 2, 2016
Oct 2, 2016
324
typedef void (*LxNativeModuleDeinitEntryPoint)(void);
Sep 21, 2016
Sep 21, 2016
325
Feb 28, 2018
Feb 28, 2018
326
327
328
329
330
331
// !!! FIXME: need to change this symbol name.
extern __attribute__((visibility("default"))) LxLoaderState GLoaderState;
#define LX_NATIVE_CONSTRUCTOR(modname) void __attribute__((constructor)) lxNativeConstructor_##modname(void)
#define LX_NATIVE_DESTRUCTOR(modname) void __attribute__((destructor)) lxNativeDestructor_##modname(void)
332
333
#endif
Feb 28, 2018
Feb 28, 2018
334
// end of lib2ine.h ...