ObjFW  Diff

Differences From Artifact [4eb80f8cc9]:

To Artifact [fdc1473cd0]:


18
19
20
21
22
23
24

25
26
27
28
29
30
31
32
33
34
35
36
37
#include <exec/libraries.h>
#include <exec/nodes.h>
#include <exec/resident.h>
#include <proto/exec.h>

#import "OFDNSResourceRecord.h"
#import "OFHTTPRequest.h"

#import "OFStdIOStream.h"
#import "OFString.h"

#import "amiga-library.h"
#import "macros.h"
#import "socket.h"

#define CONCAT_VERSION2(major, minor) #major "." #minor
#define CONCAT_VERSION(major, minor) CONCAT_VERSION2(major, minor)
#define VERSION_STRING CONCAT_VERSION(OBJFW_LIB_MAJOR, OBJFW_LIB_MINOR)

#if defined(OF_AMIGAOS_M68K)
# define DATA_OFFSET 0x7FFE







>





<







18
19
20
21
22
23
24
25
26
27
28
29
30

31
32
33
34
35
36
37
#include <exec/libraries.h>
#include <exec/nodes.h>
#include <exec/resident.h>
#include <proto/exec.h>

#import "OFDNSResourceRecord.h"
#import "OFHTTPRequest.h"
#import "OFSocket.h"
#import "OFStdIOStream.h"
#import "OFString.h"

#import "amiga-library.h"
#import "macros.h"


#define CONCAT_VERSION2(major, minor) #major "." #minor
#define CONCAT_VERSION(major, minor) CONCAT_VERSION2(major, minor)
#define VERSION_STRING CONCAT_VERSION(OBJFW_LIB_MAJOR, OBJFW_LIB_MINOR)

#if defined(OF_AMIGAOS_M68K)
# define DATA_OFFSET 0x7FFE
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

#ifdef OF_AMIGAOS_M68K
extern uintptr_t __CTOR_LIST__[];
extern const void *_EH_FRAME_BEGINS__;
extern void *_EH_FRAME_OBJECTS__;
#endif

extern bool glue_of_init(void);
extern void *glue_of_alloc(void);
extern void *glue_of_alloc_zeroed(void);
extern void *glue_of_realloc(void);
extern uint32_t *glue_of_hash_seed_ref(void);
extern OFStdIOStream **glue_of_stdin_ref(void);
extern OFStdIOStream **glue_of_stdout_ref(void);
extern OFStdIOStream **glue_of_stderr_ref(void);
extern void glue_of_logv(void);
extern int glue_of_application_main(void);
extern const char *glue_of_http_request_method_to_string(void);
extern of_http_request_method_t glue_of_http_request_method_from_string(void);
extern OFString *glue_of_http_status_code_to_string(void);
extern size_t glue_of_sizeof_type_encoding(void);
extern size_t glue_of_alignof_type_encoding(void);
extern of_string_encoding_t glue_of_string_parse_encoding(void);
extern OFString *glue_of_string_name_of_encoding(void);
extern size_t glue_of_string_utf8_encode(void);
extern ssize_t glue_of_string_utf8_decode(void);
extern size_t glue_of_string_utf16_length(void);
extern size_t glue_of_string_utf32_length(void);
extern OFString *glue_of_zip_archive_entry_version_to_string(void);
extern OFString *glue_of_zip_archive_entry_compression_method_to_string(void);
extern size_t glue_of_zip_archive_entry_extra_field_find(void);
extern void glue_of_pbkdf2(void);
extern void glue_of_salsa20_8_core(void);
extern void glue_of_scrypt_block_mix(void);
extern void glue_of_scrypt_romix(void);
extern void glue_of_scrypt(void);
extern const char *glue_of_strptime(void);
extern of_socket_address_t glue_of_socket_address_parse_ip(void);
extern of_socket_address_t glue_of_socket_address_parse_ipv4(void);
extern of_socket_address_t glue_of_socket_address_parse_ipv6(void);
extern of_socket_address_t glue_of_socket_address_ipx(void);
extern bool glue_of_socket_address_equal(void);
extern unsigned long glue_of_socket_address_hash(void);
extern OFString *glue_of_socket_address_ip_string(void);
extern void glue_of_socket_address_set_port(void);
extern uint16_t glue_of_socket_address_get_port(void);
extern void glue_of_socket_address_set_ipx_network(void);
extern uint32_t glue_of_socket_address_get_ipx_network(void);
extern void glue_of_socket_address_set_ipx_node(void);
extern void glue_of_socket_address_get_ipx_node(void);
extern OFString *glue_of_dns_class_to_string(void);
extern OFString *glue_of_dns_record_type_to_string(void);
extern of_dns_class_t glue_of_dns_class_parse(void);
extern of_dns_record_type_t glue_of_dns_record_type_parse(void);

#ifdef OF_AMIGAOS_M68K
void
__init_eh(void)
{
	/* Taken care of by of_init() */
}
#endif

#ifdef OF_MORPHOS
const ULONG __abox__ = 1;
#endif
struct ExecBase *SysBase;
struct of_libc libc;
FILE **__sF;

#if defined(OF_AMIGAOS_M68K)
__asm__ (
    ".text\n"
    ".globl ___restore_a4\n"
    ".align 1\n"







|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<





|







|







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

#ifdef OF_AMIGAOS_M68K
extern uintptr_t __CTOR_LIST__[];
extern const void *_EH_FRAME_BEGINS__;
extern void *_EH_FRAME_OBJECTS__;
#endif

#include "amiga-glue.h"















































#ifdef OF_AMIGAOS_M68K
void
__init_eh(void)
{
	/* Taken care of by OFInit() */
}
#endif

#ifdef OF_MORPHOS
const ULONG __abox__ = 1;
#endif
struct ExecBase *SysBase;
struct OFLibC libC;
FILE **__sF;

#if defined(OF_AMIGAOS_M68K)
__asm__ (
    ".text\n"
    ".globl ___restore_a4\n"
    ".align 1\n"
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
	);
#endif

	return dataDataRelocs;
}

static struct Library *
lib_init(struct ObjFWBase *base OF_M68K_REG(d0), void *segList OF_M68K_REG(a0),
    struct ExecBase *sysBase OF_M68K_REG(a6))
{
#if defined(OF_AMIGAOS_M68K)
	__asm__ __volatile__ (
	    "move.l	a6, _SysBase"
	    :: "a"(sysBase)
	);







|







169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
	);
#endif

	return dataDataRelocs;
}

static struct Library *
libInit(struct ObjFWBase *base OF_M68K_REG(d0), void *segList OF_M68K_REG(a0),
    struct ExecBase *sysBase OF_M68K_REG(a6))
{
#if defined(OF_AMIGAOS_M68K)
	__asm__ __volatile__ (
	    "move.l	a6, _SysBase"
	    :: "a"(sysBase)
	);
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
	base->parent = NULL;
	base->dataSeg = getDataSeg();

	return &base->library;
}

struct Library *__saveds
lib_open(void)
{
	OF_M68K_ARG(struct ObjFWBase *, base, a6)

	struct ObjFWBase *child;
	size_t dataSize, *dataDataRelocs;
	ptrdiff_t displacement;

	if (base->parent != NULL)
		return NULL;

	base->library.lib_OpenCnt++;
	base->library.lib_Flags &= ~LIBF_DELEXP;

	/*
	 * We cannot use malloc here, as that depends on the libc passed from
	 * the application.
	 */
	if ((child = AllocMem(base->library.lib_NegSize +
	    base->library.lib_PosSize, MEMF_ANY)) == NULL) {
		base->library.lib_OpenCnt--;
		return NULL;
	}







|














|







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
	base->parent = NULL;
	base->dataSeg = getDataSeg();

	return &base->library;
}

struct Library *__saveds
libOpen(void)
{
	OF_M68K_ARG(struct ObjFWBase *, base, a6)

	struct ObjFWBase *child;
	size_t dataSize, *dataDataRelocs;
	ptrdiff_t displacement;

	if (base->parent != NULL)
		return NULL;

	base->library.lib_OpenCnt++;
	base->library.lib_Flags &= ~LIBF_DELEXP;

	/*
	 * We cannot use malloc here, as that depends on the libC passed from
	 * the application.
	 */
	if ((child = AllocMem(base->library.lib_NegSize +
	    base->library.lib_PosSize, MEMF_ANY)) == NULL) {
		base->library.lib_OpenCnt--;
		return NULL;
	}
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
353
354
355
356
357
358
	    base->library.lib_NegSize + base->library.lib_PosSize);

	return segList;
#undef SysBase
}

static void *__saveds
lib_expunge(void)
{
	OF_M68K_ARG(struct ObjFWBase *, base, a6)

	return expunge(base, SysBase);
}

static void *__saveds
lib_close(void)
{
	/*
	 * SysBase becomes invalid during this function, so we store it in
	 * sysBase and add a define to make the inlines use the right one.
	 */
	struct ExecBase *sysBase = SysBase;
#define SysBase sysBase

	OF_M68K_ARG(struct ObjFWBase *, base, a6)

	if (base->parent != NULL) {
		struct ObjFWBase *parent;

#ifdef OF_AMIGAOS_M68K
		if (base->initialized)
			for (size_t i = 1; i <= (size_t)_EH_FRAME_BEGINS__; i++)
				libc.__deregister_frame_info(
				    (&_EH_FRAME_BEGINS__)[i]);
#endif

		parent = base->parent;

		FreeMem(base->dataSeg - DATA_OFFSET, getDataSize());
		FreeMem((char *)base - base->library.lib_NegSize,







|







|
















|







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
	    base->library.lib_NegSize + base->library.lib_PosSize);

	return segList;
#undef SysBase
}

static void *__saveds
libExpunge(void)
{
	OF_M68K_ARG(struct ObjFWBase *, base, a6)

	return expunge(base, SysBase);
}

static void *__saveds
libClose(void)
{
	/*
	 * SysBase becomes invalid during this function, so we store it in
	 * sysBase and add a define to make the inlines use the right one.
	 */
	struct ExecBase *sysBase = SysBase;
#define SysBase sysBase

	OF_M68K_ARG(struct ObjFWBase *, base, a6)

	if (base->parent != NULL) {
		struct ObjFWBase *parent;

#ifdef OF_AMIGAOS_M68K
		if (base->initialized)
			for (size_t i = 1; i <= (size_t)_EH_FRAME_BEGINS__; i++)
				libC.__deregister_frame_info(
				    (&_EH_FRAME_BEGINS__)[i]);
#endif

		parent = base->parent;

		FreeMem(base->dataSeg - DATA_OFFSET, getDataSize());
		FreeMem((char *)base - base->library.lib_NegSize,
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
		return expunge(base, sysBase);

	return NULL;
#undef SysBase
}

static void *
lib_null(void)
{
	return NULL;
}

bool __saveds
of_init(unsigned int version, struct of_libc *libc_, FILE **sF)
{
#ifdef OF_AMIGAOS_M68K
	OF_M68K_ARG(struct ObjFWBase *, base, a6)
#else
	register struct ObjFWBase *r12 __asm__("r12");
	struct ObjFWBase *base = r12;
#endif
#ifdef OF_MORPHOS
	void *frame;
#endif
	uintptr_t *iter, *iter0;

	if (version > 1)
		return false;

	if (base->initialized)
		return true;

	memcpy(&libc, libc_, sizeof(libc));
	__sF = sF;

#ifdef OF_AMIGAOS_M68K
	if ((size_t)_EH_FRAME_BEGINS__ != (size_t)_EH_FRAME_OBJECTS__)
		return false;

	for (size_t i = 1; i <= (size_t)_EH_FRAME_BEGINS__; i++)
		libc.__register_frame_info((&_EH_FRAME_BEGINS__)[i],
		    (&_EH_FRAME_OBJECTS__)[i]);

	iter0 = &__CTOR_LIST__[1];
#elif defined(OF_MORPHOS)
	__asm__ (
	    "lis	%0, __EH_FRAME_BEGIN__@ha\n\t"
	    "la		%0, __EH_FRAME_BEGIN__@l(%0)\n\t"
	    "lis	%1, __CTOR_LIST__@ha\n\t"
	    "la		%1, __CTOR_LIST__@l(%1)\n\t"
	    : "=r"(frame), "=r"(iter0)
	);

	libc.__register_frame(frame);
#endif

	for (iter = iter0; *iter != 0; iter++);

	while (iter > iter0) {
		void (*ctor)(void) = (void (*)(void))*--iter;
		ctor();
	}

	base->initialized = true;

	return true;
}

void *
malloc(size_t size)
{
	return libc.malloc(size);
}

void *
calloc(size_t count, size_t size)
{
	return libc.calloc(count, size);
}

void *
realloc(void *ptr, size_t size)
{
	return libc.realloc(ptr, size);
}

void
free(void *ptr)
{
	libc.free(ptr);
}

int
fprintf(FILE *restrict stream, const char *restrict fmt, ...)
{
	int ret;
	va_list args;

	va_start(args, fmt);
	ret = libc.vfprintf(stream, fmt, args);
	va_end(args);

	return ret;
}

int
vfprintf(FILE *restrict stream, const char *restrict fmt, va_list args)
{
	return libc.vfprintf(stream, fmt, args);
}

int
fflush(FILE *restrict stream)
{
	return libc.fflush(stream);
}

void
abort(void)
{
	libc.abort();

	OF_UNREACHABLE
}

#ifdef HAVE_SJLJ_EXCEPTIONS
int
_Unwind_SjLj_RaiseException(void *ex)
{
	return libc._Unwind_SjLj_RaiseException(ex);
}
#else
int
_Unwind_RaiseException(void *ex)
{
	return libc._Unwind_RaiseException(ex);
}
#endif

void
_Unwind_DeleteException(void *ex)
{
	libc._Unwind_DeleteException(ex);
}

void *
_Unwind_GetLanguageSpecificData(void *ctx)
{
	return libc._Unwind_GetLanguageSpecificData(ctx);
}

uintptr_t
_Unwind_GetRegionStart(void *ctx)
{
	return libc._Unwind_GetRegionStart(ctx);
}

uintptr_t
_Unwind_GetDataRelBase(void *ctx)
{
	return libc._Unwind_GetDataRelBase(ctx);
}

uintptr_t
_Unwind_GetTextRelBase(void *ctx)
{
	return libc._Unwind_GetTextRelBase(ctx);
}

uintptr_t
_Unwind_GetIP(void *ctx)
{
	return libc._Unwind_GetIP(ctx);
}

uintptr_t
_Unwind_GetGR(void *ctx, int gr)
{
	return libc._Unwind_GetGR(ctx, gr);
}

void
_Unwind_SetIP(void *ctx, uintptr_t ip)
{
	libc._Unwind_SetIP(ctx, ip);
}

void
_Unwind_SetGR(void *ctx, int gr, uintptr_t value)
{
	libc._Unwind_SetGR(ctx, gr, value);
}

#ifdef HAVE_SJLJ_EXCEPTIONS
void
_Unwind_SjLj_Resume(void *ex)
{
	libc._Unwind_SjLj_Resume(ex);
}
#else
void
_Unwind_Resume(void *ex)
{
	libc._Unwind_Resume(ex);
}
#endif

#ifdef OF_AMIGAOS_M68K
void
__register_frame_info(const void *begin, void *object)
{
	libc.__register_frame_info(begin, object);
}

void
*__deregister_frame_info(const void *begin)
{
	return libc.__deregister_frame_info(begin);
}
#endif

#ifdef OF_MORPHOS
void __register_frame(void *frame)
{
	libc.__register_frame(frame);
}

void __deregister_frame(void *frame)
{
	libc.__deregister_frame(frame);
}
#endif

int *
of_get_errno(void)
{
	return libc.get_errno();
}

int
vsnprintf(char *restrict str, size_t size, const char *restrict fmt,
    va_list args)
{
	return libc.vsnprintf(str, size, fmt, args);
}

#ifdef OF_AMIGAOS_M68K
int
sscanf(const char *restrict str, const char *restrict fmt, ...)
{
	int ret;
	va_list args;

	va_start(args, fmt);
	ret = libc.vsscanf(str, fmt, args);
	va_end(args);

	return ret;
}
#endif

void
exit(int status)
{
	libc.exit(status);

	OF_UNREACHABLE
}

int
atexit(void (*function)(void))
{
	return libc.atexit(function);
}

of_sig_t
signal(int sig, of_sig_t func)
{
	return libc.signal(sig, func);
}

char *
setlocale(int category, const char *locale)
{
	return libc.setlocale(category, locale);
}

int
_Unwind_Backtrace(int (*callback)(void *, void *), void *data)
{
	return libc._Unwind_Backtrace(callback, data);
}

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
static CONST_APTR functionTable[] = {
#ifdef OF_MORPHOS
	(CONST_APTR)FUNCARRAY_BEGIN,
	(CONST_APTR)FUNCARRAY_32BIT_NATIVE,
#endif
	(CONST_APTR)lib_open,
	(CONST_APTR)lib_close,
	(CONST_APTR)lib_expunge,
	(CONST_APTR)lib_null,
#ifdef OF_MORPHOS
	(CONST_APTR)-1,
	(CONST_APTR)FUNCARRAY_32BIT_SYSTEMV,
#endif
	(CONST_APTR)glue_of_init,
	(CONST_APTR)glue_of_alloc,
	(CONST_APTR)glue_of_alloc_zeroed,
	(CONST_APTR)glue_of_realloc,
	(CONST_APTR)glue_of_hash_seed_ref,
	(CONST_APTR)glue_of_stdin_ref,
	(CONST_APTR)glue_of_stdout_ref,
	(CONST_APTR)glue_of_stderr_ref,
	(CONST_APTR)glue_of_logv,
	(CONST_APTR)glue_of_application_main,
	(CONST_APTR)glue_of_http_request_method_to_string,
	(CONST_APTR)glue_of_http_request_method_from_string,
	(CONST_APTR)glue_of_http_status_code_to_string,
	(CONST_APTR)glue_of_sizeof_type_encoding,
	(CONST_APTR)glue_of_alignof_type_encoding,
	(CONST_APTR)glue_of_string_parse_encoding,
	(CONST_APTR)glue_of_string_name_of_encoding,
	(CONST_APTR)glue_of_string_utf8_encode,
	(CONST_APTR)glue_of_string_utf8_decode,
	(CONST_APTR)glue_of_string_utf16_length,
	(CONST_APTR)glue_of_string_utf32_length,
	(CONST_APTR)glue_of_zip_archive_entry_version_to_string,
	(CONST_APTR)glue_of_zip_archive_entry_compression_method_to_string,
	(CONST_APTR)glue_of_zip_archive_entry_extra_field_find,
	(CONST_APTR)glue_of_pbkdf2,
	(CONST_APTR)glue_of_salsa20_8_core,
	(CONST_APTR)glue_of_scrypt_block_mix,
	(CONST_APTR)glue_of_scrypt_romix,
	(CONST_APTR)glue_of_scrypt,
	(CONST_APTR)glue_of_strptime,
	(CONST_APTR)glue_of_socket_address_parse_ip,
	(CONST_APTR)glue_of_socket_address_parse_ipv4,
	(CONST_APTR)glue_of_socket_address_parse_ipv6,
	(CONST_APTR)glue_of_socket_address_ipx,
	(CONST_APTR)glue_of_socket_address_equal,
	(CONST_APTR)glue_of_socket_address_hash,
	(CONST_APTR)glue_of_socket_address_ip_string,
	(CONST_APTR)glue_of_socket_address_set_port,
	(CONST_APTR)glue_of_socket_address_get_port,
	(CONST_APTR)glue_of_socket_address_set_ipx_network,
	(CONST_APTR)glue_of_socket_address_get_ipx_network,
	(CONST_APTR)glue_of_socket_address_set_ipx_node,
	(CONST_APTR)glue_of_socket_address_get_ipx_node,
	(CONST_APTR)glue_of_dns_class_to_string,
	(CONST_APTR)glue_of_dns_record_type_to_string,
	(CONST_APTR)glue_of_dns_class_parse,
	(CONST_APTR)glue_of_dns_record_type_parse,
	(CONST_APTR)-1,
#ifdef OF_MORPHOS
	(CONST_APTR)FUNCARRAY_END
#endif
};
#pragma GCC diagnostic pop

static struct {
	ULONG dataSize;
	CONST_APTR *functionTable;
	ULONG *dataTable;
	struct Library *(*initFunc)(
	    struct ObjFWBase *base OF_M68K_REG(d0),
	    void *segList OF_M68K_REG(a0),
	    struct ExecBase *execBase OF_M68K_REG(a6));
} init_table = {
	sizeof(struct ObjFWBase),
	functionTable,
	NULL,
	lib_init
};

struct Resident resident = {
	.rt_MatchWord = RTC_MATCHWORD,
	.rt_MatchTag = &resident,
	.rt_EndSkip = &resident + 1,
	.rt_Flags = RTF_AUTOINIT







|





|


















|







|












|

















|





|





|





|









|








|





|





|








|





|






|





|





|





|





|





|





|





|





|






|





|







|





|






|




|




|

|






|










|









|







|


|
|

|





|





|









|
|
|
|




|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<



















|







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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628














































629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
		return expunge(base, sysBase);

	return NULL;
#undef SysBase
}

static void *
libNull(void)
{
	return NULL;
}

bool __saveds
OFInit(unsigned int version, struct OFLibC *libC_, FILE **sF)
{
#ifdef OF_AMIGAOS_M68K
	OF_M68K_ARG(struct ObjFWBase *, base, a6)
#else
	register struct ObjFWBase *r12 __asm__("r12");
	struct ObjFWBase *base = r12;
#endif
#ifdef OF_MORPHOS
	void *frame;
#endif
	uintptr_t *iter, *iter0;

	if (version > 1)
		return false;

	if (base->initialized)
		return true;

	memcpy(&libC, libC_, sizeof(libC));
	__sF = sF;

#ifdef OF_AMIGAOS_M68K
	if ((size_t)_EH_FRAME_BEGINS__ != (size_t)_EH_FRAME_OBJECTS__)
		return false;

	for (size_t i = 1; i <= (size_t)_EH_FRAME_BEGINS__; i++)
		libC.__register_frame_info((&_EH_FRAME_BEGINS__)[i],
		    (&_EH_FRAME_OBJECTS__)[i]);

	iter0 = &__CTOR_LIST__[1];
#elif defined(OF_MORPHOS)
	__asm__ (
	    "lis	%0, __EH_FRAME_BEGIN__@ha\n\t"
	    "la		%0, __EH_FRAME_BEGIN__@l(%0)\n\t"
	    "lis	%1, __CTOR_LIST__@ha\n\t"
	    "la		%1, __CTOR_LIST__@l(%1)\n\t"
	    : "=r"(frame), "=r"(iter0)
	);

	libC.__register_frame(frame);
#endif

	for (iter = iter0; *iter != 0; iter++);

	while (iter > iter0) {
		void (*ctor)(void) = (void (*)(void))*--iter;
		ctor();
	}

	base->initialized = true;

	return true;
}

void *
malloc(size_t size)
{
	return libC.malloc(size);
}

void *
calloc(size_t count, size_t size)
{
	return libC.calloc(count, size);
}

void *
realloc(void *ptr, size_t size)
{
	return libC.realloc(ptr, size);
}

void
free(void *ptr)
{
	libC.free(ptr);
}

int
fprintf(FILE *restrict stream, const char *restrict fmt, ...)
{
	int ret;
	va_list args;

	va_start(args, fmt);
	ret = libC.vfprintf(stream, fmt, args);
	va_end(args);

	return ret;
}

int
vfprintf(FILE *restrict stream, const char *restrict fmt, va_list args)
{
	return libC.vfprintf(stream, fmt, args);
}

int
fflush(FILE *restrict stream)
{
	return libC.fflush(stream);
}

void
abort(void)
{
	libC.abort();

	OF_UNREACHABLE
}

#ifdef HAVE_SJLJ_EXCEPTIONS
int
_Unwind_SjLj_RaiseException(void *ex)
{
	return libC._Unwind_SjLj_RaiseException(ex);
}
#else
int
_Unwind_RaiseException(void *ex)
{
	return libC._Unwind_RaiseException(ex);
}
#endif

void
_Unwind_DeleteException(void *ex)
{
	libC._Unwind_DeleteException(ex);
}

void *
_Unwind_GetLanguageSpecificData(void *ctx)
{
	return libC._Unwind_GetLanguageSpecificData(ctx);
}

uintptr_t
_Unwind_GetRegionStart(void *ctx)
{
	return libC._Unwind_GetRegionStart(ctx);
}

uintptr_t
_Unwind_GetDataRelBase(void *ctx)
{
	return libC._Unwind_GetDataRelBase(ctx);
}

uintptr_t
_Unwind_GetTextRelBase(void *ctx)
{
	return libC._Unwind_GetTextRelBase(ctx);
}

uintptr_t
_Unwind_GetIP(void *ctx)
{
	return libC._Unwind_GetIP(ctx);
}

uintptr_t
_Unwind_GetGR(void *ctx, int gr)
{
	return libC._Unwind_GetGR(ctx, gr);
}

void
_Unwind_SetIP(void *ctx, uintptr_t ip)
{
	libC._Unwind_SetIP(ctx, ip);
}

void
_Unwind_SetGR(void *ctx, int gr, uintptr_t value)
{
	libC._Unwind_SetGR(ctx, gr, value);
}

#ifdef HAVE_SJLJ_EXCEPTIONS
void
_Unwind_SjLj_Resume(void *ex)
{
	libC._Unwind_SjLj_Resume(ex);
}
#else
void
_Unwind_Resume(void *ex)
{
	libC._Unwind_Resume(ex);
}
#endif

#ifdef OF_AMIGAOS_M68K
void
__register_frame_info(const void *begin, void *object)
{
	libC.__register_frame_info(begin, object);
}

void
*__deregister_frame_info(const void *begin)
{
	return libC.__deregister_frame_info(begin);
}
#endif

#ifdef OF_MORPHOS
void __register_frame(void *frame)
{
	libC.__register_frame(frame);
}

void __deregister_frame(void *frame)
{
	libC.__deregister_frame(frame);
}
#endif

int *
OFErrNo(void)
{
	return libC.errNo();
}

int
vsnprintf(char *restrict str, size_t size, const char *restrict fmt,
    va_list args)
{
	return libC.vsnprintf(str, size, fmt, args);
}

#ifdef OF_AMIGAOS_M68K
int
sscanf(const char *restrict str, const char *restrict fmt, ...)
{
	int ret;
	va_list args;

	va_start(args, fmt);
	ret = libC.vsscanf(str, fmt, args);
	va_end(args);

	return ret;
}
#endif

void
exit(int status)
{
	libC.exit(status);

	OF_UNREACHABLE
}

int
atexit(void (*function)(void))
{
	return libC.atexit(function);
}

OFSignalHandler
signal(int sig, OFSignalHandler func)
{
	return libC.signal(sig, func);
}

char *
setlocale(int category, const char *locale)
{
	return libC.setlocale(category, locale);
}

int
_Unwind_Backtrace(int (*callback)(void *, void *), void *data)
{
	return libC._Unwind_Backtrace(callback, data);
}

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
static CONST_APTR functionTable[] = {
#ifdef OF_MORPHOS
	(CONST_APTR)FUNCARRAY_BEGIN,
	(CONST_APTR)FUNCARRAY_32BIT_NATIVE,
#endif
	(CONST_APTR)libOpen,
	(CONST_APTR)libClose,
	(CONST_APTR)libExpunge,
	(CONST_APTR)libNull,
#ifdef OF_MORPHOS
	(CONST_APTR)-1,
	(CONST_APTR)FUNCARRAY_32BIT_SYSTEMV,
#endif
#include "amiga-funcarray.inc"














































	(CONST_APTR)-1,
#ifdef OF_MORPHOS
	(CONST_APTR)FUNCARRAY_END
#endif
};
#pragma GCC diagnostic pop

static struct {
	ULONG dataSize;
	CONST_APTR *functionTable;
	ULONG *dataTable;
	struct Library *(*initFunc)(
	    struct ObjFWBase *base OF_M68K_REG(d0),
	    void *segList OF_M68K_REG(a0),
	    struct ExecBase *execBase OF_M68K_REG(a6));
} init_table = {
	sizeof(struct ObjFWBase),
	functionTable,
	NULL,
	libInit
};

struct Resident resident = {
	.rt_MatchWord = RTC_MATCHWORD,
	.rt_MatchTag = &resident,
	.rt_EndSkip = &resident + 1,
	.rt_Flags = RTF_AUTOINIT