/
lx_loader.h
246 lines (218 loc) · 5.92 KB
1
2
3
#ifndef _INCL_LX_LOADER_H_
#define _INCL_LX_LOADER_H_ 1
4
5
6
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
7
8
9
#include <stdio.h>
#include <stdlib.h>
10
#include <stdint.h>
11
#include <string.h>
12
13
14
15
typedef uint8_t uint8;
typedef uint16_t uint16;
typedef uint32_t uint32;
16
typedef uint64_t uint64;
17
18
19
20
typedef int8_t sint8;
typedef int16_t sint16;
typedef int32_t sint32;
21
typedef int64_t sint64;
22
23
24
typedef unsigned int uint;
25
26
27
28
29
30
31
32
33
34
#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
35
#if 0
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#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
51
52
53
54
55
56
57
58
59
60
61
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
#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;
#pragma pack(pop)
131
132
133
typedef struct LxMmaps
{
134
void *mapped;
135
136
void *addr;
size_t size;
137
uint16 alias; // 16:16 alias, if one.
138
139
} LxMmaps;
140
typedef struct LxExport
141
142
{
uint32 ordinal;
143
144
const char *name; // can be NULL
void *addr;
145
const LxMmaps *object;
146
} LxExport;
147
148
149
150
151
152
153
154
155
156
struct LxModule;
typedef struct LxModule LxModule;
struct LxModule
{
LxHeader lx;
uint32 refcount;
char name[256]; // !!! FIXME: allocate this.
LxModule **dependencies;
LxMmaps *mmaps;
157
158
159
const LxExport *exports;
uint32 num_exports;
160
161
162
void *nativelib;
uint32 eip;
uint32 esp;
163
int initialized;
164
char *os2path; // absolute path to module, in OS/2 format
165
// !!! FIXME: put this elsewhere?
166
uint32 signal_exception_focus_count;
167
168
169
170
LxModule *prev; // all loaded modules are in a doubly-linked list.
LxModule *next; // all loaded modules are in a doubly-linked list.
};
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#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;
190
191
192
193
194
195
196
197
198
199
200
201
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;
202
#pragma pack(pop)
203
204
205
// We put the 128 bytes of TLS slots after the TIB structs.
#define LXTIBSIZE (sizeof (LxTIB) + sizeof (LxTIB2) + 128)
206
207
typedef struct LxLoaderState
208
209
210
{
LxModule *loaded_modules;
LxModule *main_module;
211
uint8 main_tibspace[LXTIBSIZE];
212
LxPIB pib;
213
int subprocess;
214
int running;
215
216
217
int trace_native;
uint16 original_cs;
uint16 original_ds;
218
219
uint16 original_es;
uint16 original_ss;
220
uint32 ldt[8192];
221
222
char *libpath;
uint32 libpathlen;
223
224
225
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).
226
void (*dosExit)(uint32 action, uint32 result);
227
uint16 (*initOs2Tib)(uint8 *tibspace, void *_topOfStack, const size_t stacklen, const uint32 tid);
228
void (*deinitOs2Tib)(const uint16 selector);
229
230
231
int (*findSelector)(const uint32 addr, uint16 *outselector, uint16 *outoffset);
void (*freeSelector)(const uint16 selector);
void *(*convert1616to32)(const uint32 addr1616);
232
uint32 (*convert32to1616)(void *addr32);
233
LxModule *(*loadModule)(const char *modname);
234
235
236
int (*locatePathCaseInsensitive)(char *buf);
char *(*makeUnixPath)(const char *os2path, uint32 *err);
char *(*makeOS2Path)(const char *fname);
237
void __attribute__((noreturn)) (*terminate)(const uint32 exitcode);
238
239
} LxLoaderState;
240
241
typedef const LxExport *(*LxNativeModuleInitEntryPoint)(LxLoaderState *lx_state, uint32 *lx_num_exports);
typedef void (*LxNativeModuleDeinitEntryPoint)(void);
242
243
244
245
#endif
// end of lx_loader.h ...