/
libheader.c
530 lines (432 loc) · 13.5 KB
/
libheader.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
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
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
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
/**
* Startup code for AmigaOS's Shared Libraries.
* (heavily based on mccheader.c)
*
* $Id: libheader.c,v 0.1 2006/07/29 10:08:28 diegocr Exp $
*/
/******************************************************************************/
/* Includes */
/******************************************************************************/
/* MorphOS relevant includes... */
#ifdef __MORPHOS__
#include <emul/emulinterface.h>
#include <emul/emulregs.h>
#endif
/* a few other includes... */
#include <exec/types.h>
#include <exec/memory.h>
#include <exec/libraries.h>
#include <exec/semaphores.h>
#include <exec/resident.h>
#include <exec/execbase.h>
#include <dos/dos.h>
#include <proto/exec.h>
#include <proto/utility.h>
//#include <proto/intuition.h>
#include <SDI_lib.h>
#include "libheader.h"
/* Entry point, if the user try to run me I'll return to the shell nicely */
#ifndef __amigaos4__
STATIC CONST UWORD __rts = 0x4E75;
#else
int _start( void )
{
return 0;
}
#endif
/* We need a pointer to this string in our ROMTag (see below). */
STATIC CONST UBYTE UserLibID[] = LIBID;
STATIC CONST UBYTE UserLibName[] = LIBNAME;
/* Here's our global data, described above. */
#if defined(__amigaos4__)
#include <inline4/exec.h>
struct Library *SysBase = NULL;
struct Library *UtilityBase = NULL;
//struct Library *IntuitionBase = NULL;
struct ExecIFace *IExec = NULL;
struct UtilityIFace *IUtility = NULL;
//struct IntuitionIFace *IIntuition = NULL;
#else
struct ExecBase *SysBase = NULL;
//struct DosLibrary *DOSBase = NULL;
//struct IntuitionBase *IntuitionBase = NULL;
#endif
/* Our library structure, consisting of a struct Library, a segment pointer */
/* and a semaphore. We need the semaphore to protect init/exit stuff in our */
/* open/close functions */
struct LibraryHeader
{
struct Library lh_Library;
UWORD lh_Pad1;
BPTR lh_Segment;
struct SignalSemaphore lh_Semaphore;
UWORD lh_Pad2;
};
/******************************************************************************/
/* Local Structures & Prototypes */
/******************************************************************************/
#if defined(__amigaos4__)
struct LibraryHeader * LIBFUNC LibInit (struct LibraryHeader *base, BPTR librarySegment, struct ExecIFace *pIExec);
BPTR LIBFUNC LibExpunge(struct LibraryManagerInterface *Self);
struct LibraryHeader * LIBFUNC LibOpen (struct LibraryManagerInterface *Self, ULONG version);
BPTR LIBFUNC LibClose (struct LibraryManagerInterface *Self);
ULONG LIBFUNC MCC_Query (UNUSED struct Interface *self, REG(d0, LONG which));
#elif defined(__MORPHOS__)
struct LibraryHeader * LIBFUNC LibInit (struct LibraryHeader *base, BPTR Segment, struct ExecBase *SysBase);
BPTR LIBFUNC LibExpunge(void);
struct LibraryHeader * LIBFUNC LibOpen (void);
BPTR LIBFUNC LibClose (void);
ULONG LIBFUNC MCC_Query (void);
#else
struct LibraryHeader * LIBFUNC LibInit (REG(a0, BPTR Segment), REG(a6, struct ExecBase *SysBase));
BPTR LIBFUNC LibExpunge(REG(a6, struct LibraryHeader *base));
struct LibraryHeader * LIBFUNC LibOpen (REG(a6, struct LibraryHeader *base));
BPTR LIBFUNC LibClose (REG(a6, struct LibraryHeader *base));
ULONG LIBFUNC MCC_Query (REG(d0, LONG which));
#endif
/*****************************************************************************/
/*****************************************************************************/
#define CrossOpenLib( BASE, IFACE, NAME, VERSION ) \
((BASE = OpenLibrary( NAME,VERSION)) && GETINTERFACE( IFACE, BASE ))
#define CrossCloseLib( BASE, IFACE ) \
DROPINTERFACE( IFACE ); CloseLibrary( BASE ); BASE = NULL
INLINE BOOL UserLibOpen(struct Library *base)
{
BOOL UserLibInitFunc(struct Library *base);
if(base->lib_OpenCnt != 1 )
return(TRUE);
// if(CrossOpenLib( IntuitionBase, IIntuition, "intuition.library", 39))
{
if(CrossOpenLib( UtilityBase, IUtility, "utility.library", 39))
{
#ifdef UserLibInit
if(UserLibInitFunc( base ))
#endif
return(TRUE);
CrossCloseLib( UtilityBase, IUtility );
}
// CrossCloseLib( IntuitionBase, IIntuition );
}
return(FALSE);
}
/*****************************************************************************/
/*****************************************************************************/
INLINE BOOL UserLibClose(struct Library *base)
{
BOOL UserLibExitFunc(struct Library *base);
if(base->lib_OpenCnt == 1 )
{
#ifdef UserLibExit
UserLibExitFunc( base );
#endif
CrossCloseLib( UtilityBase, IUtility );
// CrossCloseLib( IntuitionBase, IIntuition );
}
return(TRUE);
}
/*****************************************************************************/
/*****************************************************************************/
/******************************************************************************/
/* Local data structures */
/******************************************************************************/
#if defined(__amigaos4__)
/* ------------------- OS4 Manager Interface ------------------------ */
STATIC ULONG LibObtain(struct LibraryManagerInterface *Self)
{
return(Self->Data.RefCount++);
}
STATIC ULONG LibRelease(struct LibraryManagerInterface *Self)
{
return(Self->Data.RefCount--);
}
STATIC CONST APTR LibManagerVectors[] =
{
(APTR)LibObtain,
(APTR)LibRelease,
(APTR)NULL,
(APTR)NULL,
(APTR)LibOpen,
(APTR)LibClose,
(APTR)LibExpunge,
(APTR)NULL,
(APTR)-1
};
STATIC CONST struct TagItem LibManagerTags[] =
{
{MIT_Name, (ULONG)"__library"},
{MIT_VectorTable, (ULONG)LibManagerVectors},
{MIT_Version, 1},
{TAG_DONE, 0}
};
/* ------------------- Library Interface(s) ------------------------ */
STATIC CONST APTR LibVectors[] =
{
(APTR)LibObtain,
(APTR)LibRelease,
(APTR)NULL,
(APTR)NULL,
LIBRARY_API,
(APTR)-1
};
STATIC CONST struct TagItem MainTags[] =
{
{MIT_Name, (ULONG)"main"},
{MIT_VectorTable, (ULONG)LibVectors},
{MIT_Version, 1},
{TAG_DONE, 0}
};
STATIC CONST ULONG LibInterfaces[] =
{
(ULONG)LibManagerTags,
(ULONG)MainTags,
(ULONG)0
};
// Out libraries always have to carry a 68k jump table with it, so
// lets define it here as extern, as we are going to link it to
// our binary here.
#ifndef NO_VECTABLE68K
extern const APTR VecTable68K[];
#endif
STATIC CONST struct TagItem LibCreateTags[] =
{
{CLT_DataSize, (ULONG)(sizeof(struct LibraryHeader))},
{CLT_InitFunc, (ULONG)LibInit},
{CLT_Interfaces, (ULONG)LibInterfaces},
#ifndef NO_VECTABLE68K
{CLT_Vector68K, (ULONG)VecTable68K},
#endif
{TAG_DONE, 0}
};
#else
LONG LIBFUNC LibNull(VOID) {
return(0);
}
static const APTR LibVectors[] =
{
#ifdef __MORPHOS__
(APTR)FUNCARRAY_32BIT_NATIVE,
#endif
(APTR)LibOpen,
(APTR)LibClose,
(APTR)LibExpunge,
(APTR)LibNull,
LIBRARY_API,
(APTR)-1
};
#endif
/* ------------------- ROM Tag ------------------------ */
static const USED_VAR struct Resident ROMTag =
{
RTC_MATCHWORD,
(struct Resident *)&ROMTag,
(struct Resident *)&ROMTag + 1,
#if defined(__amigaos4__)
RTF_AUTOINIT|RTF_NATIVE, // The Library should be set up according to the given table.
#elif defined(__MORPHOS__)
RTF_PPC,
#else
0,
#endif
VERSION,
NT_LIBRARY,
0,
(char *)UserLibName,
(char *)UserLibID+5,
#if defined(__amigaos4__)
(APTR)LibCreateTags, // This table is for initializing the Library.
#else
(APTR)LibInit,
#endif
#if defined(__MORPHOS__)
REVISION,
0
#endif
};
#if defined(__MORPHOS__)
/*
* To tell the loader that this is a new emulppc elf and not
* one for the ppc.library.
* ** IMPORTANT **
*/
const USED_VAR ULONG __amigappc__ = 1;
const USED_VAR ULONG __abox__ = 1;
#undef USE_SEMAPHORE
#else
#ifndef USE_SEMAPHORE
#define USE_SEMAPHORE
#endif
#endif /* __MORPHOS */
#ifndef __amigaos4__
#define DeleteLibrary(LIB) \
FreeMem((STRPTR)(LIB)-(LIB)->lib_NegSize, (ULONG)((LIB)->lib_NegSize+(LIB)->lib_PosSize))
#endif
/******************************************************************************/
/* Standard Library Functions, all of them are called in Forbid() state. */
/******************************************************************************/
#if defined(__amigaos4__)
struct LibraryHeader * ASM SAVEDS LibInit(struct LibraryHeader *base, BPTR librarySegment, struct ExecIFace *pIExec)
{
struct ExecBase *sb = (struct ExecBase *)pIExec->Data.LibBase;
IExec = pIExec;
SysBase = (struct Library *)sb;
base->lh_Library.lib_Node.ln_Type = NT_LIBRARY;
base->lh_Library.lib_Node.ln_Pri = 0;
base->lh_Library.lib_Node.ln_Name = (char *)UserLibName;
base->lh_Library.lib_Flags = LIBF_CHANGED | LIBF_SUMUSED;
base->lh_Library.lib_Version = VERSION;
base->lh_Library.lib_Revision = REVISION;
base->lh_Library.lib_IdString = (char *)UserLibID;
base->lh_Segment = librarySegment;
InitSemaphore(&base->lh_Semaphore);
/*
if(!UserLibInit((struct Library *)base))
{
DeleteLibrary((struct Library *)base);
return(NULL);
}
*/
return(base);
}
#else
#ifdef __MORPHOS__
#undef CLASS_STACKSWAP
struct LibraryHeader *LibInit(struct LibraryHeader *base, BPTR Segment, struct ExecBase *sb)
{
#else
struct LibraryHeader * ASM SAVEDS LibInit(REG(a0, BPTR Segment), REG(a6, struct ExecBase *sb))
{
#endif
#if !defined(__MORPHOS__)
struct LibraryHeader *base;
#endif
SysBase = sb;
#if defined(__HAVE_68881__)
if(!(SysBase->AttnFlags & AFF_68881))
return(NULL);
#endif
#if defined(_M68060) || defined(mc68060) || defined(__mc68060)
if(!(SysBase->AttnFlags & AFF_68060))
return(NULL);
#elif defined(_M68040) || defined(mc68040) || defined(__mc68040)
if(!(SysBase->AttnFlags & AFF_68040))
return(NULL);
#elif defined(_M68030) || defined(mc68030) || defined(__mc68030)
if(!(SysBase->AttnFlags & AFF_68030))
return(NULL);
#elif defined(_M68020) || defined(mc68020) || defined(__mc68020)
if(!(SysBase->AttnFlags & AFF_68020))
return(NULL);
#endif
if((base = (struct LibraryHeader *)MakeLibrary((APTR)LibVectors,NULL,NULL,sizeof(struct LibraryHeader),NULL)))
{
base->lh_Library.lib_Node.ln_Type = NT_LIBRARY;
base->lh_Library.lib_Node.ln_Name = (char *)UserLibName;
base->lh_Library.lib_Flags = LIBF_CHANGED | LIBF_SUMUSED;
base->lh_Library.lib_Version = VERSION;
base->lh_Library.lib_Revision = REVISION;
base->lh_Library.lib_IdString = (char *)UserLibID;
base->lh_Segment = Segment;
#if defined(USE_SEMAPHORE)
InitSemaphore(&base->lh_Semaphore);
#endif
AddLibrary((struct Library *)base);
}
return(base);
}
#endif
/*****************************************************************************/
/*****************************************************************************/
#ifdef __amigaos4__
BPTR LibExpunge(struct LibraryManagerInterface *Self)
{
struct LibraryHeader *base = (struct LibraryHeader *)Self->Data.LibBase;
#elif __MORPHOS__
BPTR LibExpunge(void)
{
struct LibraryHeader *base = (void *)REG_A6;
#else
BPTR ASM SAVEDS LibExpunge(REG(a6, struct LibraryHeader *base))
{
#endif
BPTR rc;
if(base->lh_Library.lib_OpenCnt > 0)
{
base->lh_Library.lib_Flags |= LIBF_DELEXP;
return(0);
}
Remove((struct Node *)base);
rc = base->lh_Segment;
DeleteLibrary(&base->lh_Library);
return(rc);
}
/*****************************************************************************/
/*****************************************************************************/
#ifdef __amigaos4__
struct LibraryHeader *LibOpen(struct LibraryManagerInterface *Self, UNUSED ULONG version)
{
struct LibraryHeader *base = (struct LibraryHeader *)Self->Data.LibBase;
#elif __MORPHOS__
struct LibraryHeader *LibOpen( void )
{
struct LibraryHeader *base = (void *)REG_A6;
#else
struct LibraryHeader * ASM SAVEDS LibOpen(REG(a6, struct LibraryHeader *base))
{
#endif
struct LibraryHeader *rc;
/* Kill the Delayed Expunge flag since we are opened again */
base->lh_Library.lib_Flags &= ~LIBF_DELEXP;
#ifdef USE_SEMAPHORE
ObtainSemaphore(&base->lh_Semaphore);
#endif
base->lh_Library.lib_OpenCnt++;
if(UserLibOpen(&base->lh_Library))
{
rc = base;
}
else
{
rc = NULL;
base->lh_Library.lib_OpenCnt--;
}
#ifdef USE_SEMAPHORE
ReleaseSemaphore(&base->lh_Semaphore);
#endif
return(rc);
}
/*****************************************************************************/
/*****************************************************************************/
#ifdef __amigaos4__
BPTR LibClose(struct LibraryManagerInterface *Self)
{
struct LibraryHeader *base = (struct LibraryHeader *)Self->Data.LibBase;
#elif __MORPHOS__
BPTR LibClose(void)
{
struct LibraryHeader *base = (struct LibraryHeader *)REG_A6;
#else
BPTR ASM SAVEDS LibClose(REG(a6, struct LibraryHeader *base))
{
#endif
BPTR rc = 0;
#ifdef USE_SEMAPHORE
ObtainSemaphore(&base->lh_Semaphore);
#endif
UserLibClose((struct Library *)base);
base->lh_Library.lib_OpenCnt--;
#ifdef USE_SEMAPHORE
ReleaseSemaphore(&base->lh_Semaphore);
#endif
if(base->lh_Library.lib_OpenCnt == 0 &&
base->lh_Library.lib_Flags & LIBF_DELEXP)
{
#ifdef __amigaos4__
rc = LibExpunge(Self);
#elif __MORPHOS__
rc = LibExpunge();
#else
rc = LibExpunge(base);
#endif
}
return(rc);
}