41
42
43
44
45
46
47
48
49
50
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
|
41
42
43
44
45
46
47
48
49
50
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
|
+
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
+
-
+
+
+
|
return -1;
}
struct ObjFWRTBase {
struct Library library;
void *seg_list;
bool initialized;
void *data_seg;
struct objc_libc libc;
};
extern uintptr_t __CTOR_LIST__[], __DTOR_LIST__[];
extern const void *_EH_FRAME_BEGINS__;
extern void *_EH_FRAME_OBJECTS__;
extern void *__a4_init;
struct ExecBase *SysBase;
#ifdef OF_MORPHOS
const ULONG __abox__ = 1;
#endif
struct objc_libc *libc;
FILE *stdout;
FILE *stderr;
__asm__ (
".text\n"
".globl ___restore_a4\n"
"___restore_a4:\n"
" movea.l 40(a6), a4\n"
" rts"
);
static OF_INLINE void *
get_data_seg(void)
{
void *data_seg;
__asm__ __volatile__ (
"lea.l ___a4_init, %0" : "=a"(data_seg)
);
return data_seg;
}
static struct Library *
lib_init(struct ExecBase *exec_base OBJC_M68K_REG("a6"),
lib_init(struct ExecBase *sys_base OBJC_M68K_REG("a6"),
void *seg_list OBJC_M68K_REG("a0"),
struct ObjFWRTBase *base OBJC_M68K_REG("d0"))
{
__asm__ __volatile__ (
SysBase = exec_base;
"move.l a6, _SysBase" :: "a"(sys_base)
);
base->seg_list = seg_list;
base->data_seg = get_data_seg();
return &base->library;
}
static struct Library *
OBJC_M68K_FUNC(lib_open, struct ObjFWRTBase *base OBJC_M68K_REG("a6"))
{
|
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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
-
+
-
+
+
-
+
-
+
-
+
+
-
+
-
-
+
+
+
-
+
+
+
-
+
+
+
-
+
+
+
-
+
+
+
-
+
+
-
+
+
+
-
+
+
+
-
+
+
+
-
+
+
+
-
+
+
+
-
+
+
+
-
+
+
+
-
+
+
+
-
+
+
+
-
+
+
+
-
+
+
+
-
+
+
+
-
+
+
+
-
+
+
+
-
+
|
Remove(&base->library.lib_Node);
FreeMem((char *)base - base->library.lib_NegSize,
base->library.lib_NegSize + base->library.lib_PosSize);
return seg_list;
}
static void *
static void *__saveds
OBJC_M68K_FUNC(lib_expunge, struct ObjFWRTBase *base OBJC_M68K_REG("a6"))
{
OBJC_M68K_ARG(struct ObjFWRTBase *, base, REG_A6)
return expunge(base);
}
static void *
static void *__saveds
OBJC_M68K_FUNC(lib_close, struct ObjFWRTBase *base OBJC_M68K_REG("a6"))
{
OBJC_M68K_ARG(struct ObjFWRTBase *, base, REG_A6)
if (base->initialized &&
(size_t)_EH_FRAME_BEGINS__ == (size_t)_EH_FRAME_OBJECTS__)
for (size_t i = 1; i <= (size_t)_EH_FRAME_BEGINS__; i++)
base->libc.__deregister_frame_info(
libc->__deregister_frame_info((&_EH_FRAME_BEGINS__)[i]);
(&_EH_FRAME_BEGINS__)[i]);
if (--base->library.lib_OpenCnt == 0 &&
(base->library.lib_Flags & LIBF_DELEXP))
return expunge(base);
return 0;
}
static void *
lib_null(void)
{
return 0;
}
static void
static void __saveds
objc_init(struct ObjFWRTBase *base OBJC_M68K_REG("a6"),
struct objc_libc *libc_ OBJC_M68K_REG("a0"))
struct objc_libc *libc OBJC_M68K_REG("a0"),
FILE *stdout_ OBJC_M68K_REG("a1"), FILE *stderr_ OBJC_M68K_REG("a2"))
{
uintptr_t *iter, *iter0;
libc = libc_;
memcpy(&base->libc, libc, sizeof(base->libc));
stdout = libc->stdout_;
stderr = libc->stderr_;
stdout = stdout_;
stderr = stderr_;
if ((size_t)_EH_FRAME_BEGINS__ == (size_t)_EH_FRAME_OBJECTS__)
for (size_t i = 1; i <= (size_t)_EH_FRAME_BEGINS__; i++)
base->libc.__register_frame_info(
libc->__register_frame_info((&_EH_FRAME_BEGINS__)[i],
(&_EH_FRAME_BEGINS__)[i],
(&_EH_FRAME_OBJECTS__)[i]);
iter0 = &__CTOR_LIST__[1];
for (iter = iter0; *iter != 0; iter++);
while (iter > iter0) {
void (*ctor)(void) = (void (*)(void))*--iter;
ctor();
}
base->initialized = true;
}
void *
malloc(size_t size)
{
register struct ObjFWRTBase *base OBJC_M68K_REG("a6");
return libc->malloc(size);
return base->libc.malloc(size);
}
void *
calloc(size_t count, size_t size)
{
register struct ObjFWRTBase *base OBJC_M68K_REG("a6");
return libc->calloc(count, size);
return base->libc.calloc(count, size);
}
void *
realloc(void *ptr, size_t size)
{
register struct ObjFWRTBase *base OBJC_M68K_REG("a6");
return libc->realloc(ptr, size);
return base->libc.realloc(ptr, size);
}
void
free(void *ptr)
{
register struct ObjFWRTBase *base OBJC_M68K_REG("a6");
libc->free(ptr);
base->libc.free(ptr);
}
int
fprintf(FILE *restrict stream, const char *restrict fmt, ...)
{
register struct ObjFWRTBase *base OBJC_M68K_REG("a6");
int ret;
va_list args;
va_start(args, fmt);
ret = libc->vfprintf(stream, fmt, args);
ret = base->libc.vfprintf(stream, fmt, args);
va_end(args);
return ret;
}
int
fputs(const char *restrict s, FILE *restrict stream)
{
register struct ObjFWRTBase *base OBJC_M68K_REG("a6");
return libc->fputs(s, stream);
return base->libc.fputs(s, stream);
}
void
exit(int status)
{
register struct ObjFWRTBase *base OBJC_M68K_REG("a6");
libc->exit(status);
base->libc.exit(status);
}
void
abort(void)
{
register struct ObjFWRTBase *base OBJC_M68K_REG("a6");
libc->abort();
base->libc.abort();
}
int
_Unwind_RaiseException(void *ex)
{
register struct ObjFWRTBase *base OBJC_M68K_REG("a6");
return libc->_Unwind_RaiseException(ex);
return base->libc._Unwind_RaiseException(ex);
}
void
_Unwind_DeleteException(void *ex)
{
register struct ObjFWRTBase *base OBJC_M68K_REG("a6");
libc->_Unwind_DeleteException(ex);
base->libc._Unwind_DeleteException(ex);
}
void *
_Unwind_GetLanguageSpecificData(void *ctx)
{
register struct ObjFWRTBase *base OBJC_M68K_REG("a6");
return libc->_Unwind_GetLanguageSpecificData(ctx);
return base->libc._Unwind_GetLanguageSpecificData(ctx);
}
uintptr_t
_Unwind_GetRegionStart(void *ctx)
{
register struct ObjFWRTBase *base OBJC_M68K_REG("a6");
return libc->_Unwind_GetRegionStart(ctx);
return base->libc._Unwind_GetRegionStart(ctx);
}
uintptr_t
_Unwind_GetDataRelBase(void *ctx)
{
register struct ObjFWRTBase *base OBJC_M68K_REG("a6");
return libc->_Unwind_GetDataRelBase(ctx);
return base->libc._Unwind_GetDataRelBase(ctx);
}
uintptr_t
_Unwind_GetTextRelBase(void *ctx)
{
register struct ObjFWRTBase *base OBJC_M68K_REG("a6");
return libc->_Unwind_GetTextRelBase(ctx);
return base->libc._Unwind_GetTextRelBase(ctx);
}
uintptr_t
_Unwind_GetIP(void *ctx)
{
register struct ObjFWRTBase *base OBJC_M68K_REG("a6");
return libc->_Unwind_GetIP(ctx);
return base->libc._Unwind_GetIP(ctx);
}
uintptr_t
_Unwind_GetGR(void *ctx, int gr)
{
register struct ObjFWRTBase *base OBJC_M68K_REG("a6");
return libc->_Unwind_GetGR(ctx, gr);
return base->libc._Unwind_GetGR(ctx, gr);
}
void
_Unwind_SetIP(void *ctx, uintptr_t ip)
{
register struct ObjFWRTBase *base OBJC_M68K_REG("a6");
libc->_Unwind_SetIP(ctx, ip);
base->libc._Unwind_SetIP(ctx, ip);
}
void
_Unwind_SetGR(void *ctx, int gr, uintptr_t value)
{
register struct ObjFWRTBase *base OBJC_M68K_REG("a6");
libc->_Unwind_SetGR(ctx, gr, value);
base->libc._Unwind_SetGR(ctx, gr, value);
}
void
_Unwind_Resume(void *ex)
{
register struct ObjFWRTBase *base OBJC_M68K_REG("a6");
libc->_Unwind_Resume(ex);
base->libc._Unwind_Resume(ex);
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
static CONST_APTR function_table[] = {
(CONST_APTR)lib_open,
(CONST_APTR)lib_close,
|