-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathamx.c
4643 lines (4422 loc) · 122 KB
/
amx.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
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
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Pawn Abstract Machine (for the Pawn language)
*
* Copyright (c) ITB CompuPhase, 1997-2006
*
* This software is provided "as-is", without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from
* the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software in
* a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*
* Version: $Id: amx.c 3662 2006-11-07 08:44:33Z thiadmer $
*/
#if BUILD_PLATFORM == WINDOWS && BUILD_TYPE == RELEASE && BUILD_COMPILER == MSVC && PAWN_CELL_SIZE == 64
/* bad bad workaround but we have to prevent a compiler crash :/ */
#pragma optimize("g",off)
#endif
#define WIN32_LEAN_AND_MEAN
#if defined _UNICODE || defined __UNICODE__ || defined UNICODE
# if !defined UNICODE /* for Windows API */
# define UNICODE
# endif
# if !defined _UNICODE /* for C library */
# define _UNICODE
# endif
#endif
#include <assert.h>
#include <stdarg.h>
#include <stddef.h> /* for wchar_t */
#include <stdlib.h> /* for getenv() */
#include <string.h>
#include "osdefs.h"
#if defined LINUX || defined __FreeBSD__ || defined __OpenBSD__
#include <sclinux.h>
#if !defined AMX_NODYNALOAD
#include <dlfcn.h>
#endif
#if defined JIT
#include <sys/types.h>
#include <sys/mman.h>
#endif
#endif
#if defined __LCC__ || defined LINUX
#include <wchar.h> /* for wcslen() */
#endif
#include "amx.h"
#if (defined _Windows && !defined AMX_NODYNALOAD) || (defined JIT && __WIN32__)
#include <windows.h>
#endif
/* When one or more of the AMX_funcname macris are defined, we want
* to compile only those functions. However, when none of these macros
* is present, we want to compile everything.
*/
#if defined AMX_ALIGN || defined AMX_ALLOT || defined AMX_CLEANUP
#define AMX_EXPLIT_FUNCTIONS
#endif
#if defined AMX_CLONE || defined AMX_DEFCALLBACK || defined AMX_EXEC
#define AMX_EXPLIT_FUNCTIONS
#endif
#if defined AMX_FLAGS || defined AMX_GETADDR || defined AMX_INIT
#define AMX_EXPLIT_FUNCTIONS
#endif
#if defined AMX_MEMINFO || defined AMX_NAMELENGTH || defined AMX_NATIVEINFO
#define AMX_EXPLIT_FUNCTIONS
#endif
#if defined AMX_PUSHXXX || defined AMX_RAISEERROR || defined AMX_REGISTER
#define AMX_EXPLIT_FUNCTIONS
#endif
#if defined AMX_SETCALLBACK || defined AMX_SETDEBUGHOOK || defined AMX_XXXNATIVES
#define AMX_EXPLIT_FUNCTIONS
#endif
#if defined AMX_XXXPUBLICS || defined AMX_XXXPUBVARS || defined AMX_XXXSTRING
#define AMX_EXPLIT_FUNCTIONS
#endif
#if defined AMX_XXXTAGS || defined AMX_XXXUSERDATA || defined AMX_UTF8XXX
#define AMX_EXPLIT_FUNCTIONS
#endif
#if !defined AMX_EXPLIT_FUNCTIONS
/* no constant set, set them all */
#define AMX_ALIGN /* amx_Align16(), amx_Align32() and amx_Align64() */
#define AMX_ALLOT /* amx_Allot() and amx_Release() */
#define AMX_DEFCALLBACK /* amx_Callback() */
#define AMX_CLEANUP /* amx_Cleanup() */
#define AMX_CLONE /* amx_Clone() */
#define AMX_EXEC /* amx_Exec() */
#define AMX_FLAGS /* amx_Flags() */
#define AMX_GETADDR /* amx_GetAddr() */
#define AMX_INIT /* amx_Init() and amx_InitJIT() */
#define AMX_MEMINFO /* amx_MemInfo() */
#define AMX_NAMELENGTH /* amx_NameLength() */
#define AMX_NATIVEINFO /* amx_NativeInfo() */
#define AMX_PUSHXXX /* amx_Push(), amx_PushArray() and amx_PushString() */
#define AMX_RAISEERROR /* amx_RaiseError() */
#define AMX_REGISTER /* amx_Register() */
#define AMX_SETCALLBACK /* amx_SetCallback() */
#define AMX_SETDEBUGHOOK /* amx_SetDebugHook() */
#define AMX_XXXNATIVES /* amx_NumNatives(), amx_GetNative() and amx_FindNative() */
#define AMX_XXXPUBLICS /* amx_NumPublics(), amx_GetPublic() and amx_FindPublic() */
#define AMX_XXXPUBVARS /* amx_NumPubVars(), amx_GetPubVar() and amx_FindPubVar() */
#define AMX_XXXSTRING /* amx_StrLen(), amx_GetString() and amx_SetString() */
#define AMX_XXXTAGS /* amx_NumTags(), amx_GetTag() and amx_FindTagId() */
#define AMX_XXXUSERDATA /* amx_GetUserData() and amx_SetUserData() */
#define AMX_UTF8XXX /* amx_UTF8Get(), amx_UTF8Put(), amx_UTF8Check() */
#endif
#undef AMX_EXPLIT_FUNCTIONS
#if defined AMX_ANSIONLY
#undef AMX_UTF8XXX /* no UTF-8 support in ANSI/ASCII-only version */
#endif
#if defined AMX_NO_NATIVEINFO
#undef AMX_NATIVEINFO
#endif
#if AMX_USERNUM <= 0
#undef AMX_XXXUSERDATA
#endif
#if defined JIT
#define AMX_NO_MACRO_INSTR /* JIT is incompatible with macro instructions */
#endif
typedef enum {
OP_NONE, /* invalid opcode */
OP_LOAD_PRI,
OP_LOAD_ALT,
OP_LOAD_S_PRI,
OP_LOAD_S_ALT,
OP_LREF_PRI,
OP_LREF_ALT,
OP_LREF_S_PRI,
OP_LREF_S_ALT,
OP_LOAD_I,
OP_LODB_I,
OP_CONST_PRI,
OP_CONST_ALT,
OP_ADDR_PRI,
OP_ADDR_ALT,
OP_STOR_PRI,
OP_STOR_ALT,
OP_STOR_S_PRI,
OP_STOR_S_ALT,
OP_SREF_PRI,
OP_SREF_ALT,
OP_SREF_S_PRI,
OP_SREF_S_ALT,
OP_STOR_I,
OP_STRB_I,
OP_LIDX,
OP_LIDX_B,
OP_IDXADDR,
OP_IDXADDR_B,
OP_ALIGN_PRI,
OP_ALIGN_ALT,
OP_LCTRL,
OP_SCTRL,
OP_MOVE_PRI,
OP_MOVE_ALT,
OP_XCHG,
OP_PUSH_PRI,
OP_PUSH_ALT,
OP_PUSH_R,
OP_PUSH_C,
OP_PUSH,
OP_PUSH_S,
OP_POP_PRI,
OP_POP_ALT,
OP_STACK,
OP_HEAP,
OP_PROC,
OP_RET,
OP_RETN,
OP_CALL,
OP_CALL_PRI,
OP_JUMP,
OP_JREL,
OP_JZER,
OP_JNZ,
OP_JEQ,
OP_JNEQ,
OP_JLESS,
OP_JLEQ,
OP_JGRTR,
OP_JGEQ,
OP_JSLESS,
OP_JSLEQ,
OP_JSGRTR,
OP_JSGEQ,
OP_SHL,
OP_SHR,
OP_SSHR,
OP_SHL_C_PRI,
OP_SHL_C_ALT,
OP_SHR_C_PRI,
OP_SHR_C_ALT,
OP_SMUL,
OP_SDIV,
OP_SDIV_ALT,
OP_UMUL,
OP_UDIV,
OP_UDIV_ALT,
OP_ADD,
OP_SUB,
OP_SUB_ALT,
OP_AND,
OP_OR,
OP_XOR,
OP_NOT,
OP_NEG,
OP_INVERT,
OP_ADD_C,
OP_SMUL_C,
OP_ZERO_PRI,
OP_ZERO_ALT,
OP_ZERO,
OP_ZERO_S,
OP_SIGN_PRI,
OP_SIGN_ALT,
OP_EQ,
OP_NEQ,
OP_LESS,
OP_LEQ,
OP_GRTR,
OP_GEQ,
OP_SLESS,
OP_SLEQ,
OP_SGRTR,
OP_SGEQ,
OP_EQ_C_PRI,
OP_EQ_C_ALT,
OP_INC_PRI,
OP_INC_ALT,
OP_INC,
OP_INC_S,
OP_INC_I,
OP_DEC_PRI,
OP_DEC_ALT,
OP_DEC,
OP_DEC_S,
OP_DEC_I,
OP_MOVS,
OP_CMPS,
OP_FILL,
OP_HALT,
OP_BOUNDS,
OP_SYSREQ_PRI,
OP_SYSREQ_C,
OP_FILE, /* obsolete */
OP_LINE, /* obsolete */
OP_SYMBOL, /* obsolete */
OP_SRANGE, /* obsolete */
OP_JUMP_PRI,
OP_SWITCH,
OP_CASETBL,
OP_SWAP_PRI,
OP_SWAP_ALT,
OP_PUSH_ADR,
OP_NOP,
OP_SYSREQ_N,
OP_SYMTAG, /* obsolete */
OP_BREAK,
OP_PUSH2_C,
OP_PUSH2,
OP_PUSH2_S,
OP_PUSH2_ADR,
OP_PUSH3_C,
OP_PUSH3,
OP_PUSH3_S,
OP_PUSH3_ADR,
OP_PUSH4_C,
OP_PUSH4,
OP_PUSH4_S,
OP_PUSH4_ADR,
OP_PUSH5_C,
OP_PUSH5,
OP_PUSH5_S,
OP_PUSH5_ADR,
OP_LOAD_BOTH,
OP_LOAD_S_BOTH,
OP_CONST,
OP_CONST_S,
/* ----- */
OP_SYSREQ_D,
OP_SYSREQ_ND,
/* ----- */
OP_NUM_OPCODES
} OPCODE;
#define USENAMETABLE(hdr) \
((hdr)->defsize==sizeof(AMX_FUNCSTUBNT))
#define NUMENTRIES(hdr,field,nextfield) \
(unsigned)(((hdr)->nextfield - (hdr)->field) / (hdr)->defsize)
#define GETENTRY(hdr,table,index) \
(AMX_FUNCSTUB *)((unsigned char*)(hdr) + (unsigned)(hdr)->table + (unsigned)index*(hdr)->defsize)
#define GETENTRYNAME(hdr,entry) \
( USENAMETABLE(hdr) \
? (char *)((unsigned char*)(hdr) + (unsigned)((AMX_FUNCSTUBNT*)(entry))->nameofs) \
: ((AMX_FUNCSTUB*)(entry))->name )
#if !defined NDEBUG
static int check_endian(void)
{
uint16_t val=0x00ff;
unsigned char *ptr=(unsigned char *)&val;
/* "ptr" points to the starting address of "val". If that address
* holds the byte "0xff", the computer stored the low byte of "val"
* at the lower address, and so the memory lay out is Little Endian.
*/
assert(*ptr==0xff || *ptr==0x00);
#if BYTE_ORDER==BIG_ENDIAN
return *ptr==0x00; /* return "true" if big endian */
#else
return *ptr==0xff; /* return "true" if little endian */
#endif
}
#endif
#if BYTE_ORDER==BIG_ENDIAN || PAWN_CELL_SIZE==16
static void swap16(uint16_t *v)
{
unsigned char *s = (unsigned char *)v;
unsigned char t;
assert(sizeof(*v)==2);
/* swap two bytes */
t=s[0];
s[0]=s[1];
s[1]=t;
}
#endif
#if BYTE_ORDER==BIG_ENDIAN || PAWN_CELL_SIZE==32
static void swap32(uint32_t *v)
{
unsigned char *s = (unsigned char *)v;
unsigned char t;
assert_static(sizeof(*v)==4);
/* swap outer two bytes */
t=s[0];
s[0]=s[3];
s[3]=t;
/* swap inner two bytes */
t=s[1];
s[1]=s[2];
s[2]=t;
}
#endif
#if (BYTE_ORDER==BIG_ENDIAN || PAWN_CELL_SIZE==64) && (defined _I64_MAX || defined HAVE_I64)
static void swap64(uint64_t *v)
{
unsigned char *s = (unsigned char *)v;
unsigned char t;
assert(sizeof(*v)==8);
t=s[0];
s[0]=s[7];
s[7]=t;
t=s[1];
s[1]=s[6];
s[6]=t;
t=s[2];
s[2]=s[5];
s[5]=t;
t=s[3];
s[3]=s[4];
s[4]=t;
}
#endif
#if defined AMX_ALIGN || defined AMX_INIT
uint16_t * AMXAPI amx_Align16(uint16_t *v)
{
assert_static(sizeof(*v)==2);
assert(check_endian());
#if BYTE_ORDER==BIG_ENDIAN
swap16(v);
#endif
return v;
}
uint32_t * AMXAPI amx_Align32(uint32_t *v)
{
assert_static(sizeof(*v)==4);
assert(check_endian());
#if BYTE_ORDER==BIG_ENDIAN
swap32(v);
#endif
return v;
}
#if defined _I64_MAX || defined HAVE_I64
uint64_t * AMXAPI amx_Align64(uint64_t *v)
{
assert(sizeof(*v)==8);
assert(check_endian());
#if BYTE_ORDER==BIG_ENDIAN
swap64(v);
#endif
return v;
}
#endif /* _I64_MAX || HAVE_I64 */
#endif /* AMX_ALIGN || AMX_INIT */
#if PAWN_CELL_SIZE==16
#define swapcell swap16
#elif PAWN_CELL_SIZE==32
#define swapcell swap32
#elif PAWN_CELL_SIZE==64 && (defined _I64_MAX || defined HAVE_I64)
#define swapcell swap64
#else
#error Unsupported cell size
#endif
#if defined AMX_FLAGS
int AMXAPI amx_Flags(AMX *amx,uint16_t *flags)
{
AMX_HEADER *hdr;
*flags=0;
if (amx==NULL)
return AMX_ERR_FORMAT;
hdr=(AMX_HEADER *)amx->base;
if (hdr->magic!=AMX_MAGIC)
return AMX_ERR_FORMAT;
if (hdr->file_version>CUR_FILE_VERSION || hdr->amx_version<MIN_FILE_VERSION)
return AMX_ERR_VERSION;
*flags=hdr->flags;
return AMX_ERR_NONE;
}
#endif /* AMX_FLAGS */
#if defined AMX_DEFCALLBACK
int AMXAPI amx_Callback(AMX *amx, cell index, cell *result, const cell *params)
{
#if defined AMX_NATIVETABLE
extern AMX_NATIVE const AMX_NATIVETABLE[];
#endif
AMX_HEADER *hdr;
AMX_FUNCSTUB *func;
AMX_NATIVE f;
assert(amx!=NULL);
hdr=(AMX_HEADER *)amx->base;
assert(hdr!=NULL);
assert(hdr->magic==AMX_MAGIC);
assert(hdr->natives<=hdr->libraries);
#if defined AMX_NATIVETABLE
if (index<0) {
/* size of AMX_NATIVETABLE is unknown here, so we cannot verify index */
f=(AMX_NATIVETABLE)[-(index+1)];
} else {
#endif
assert(index>=0 && index<(cell)NUMENTRIES(hdr,natives,libraries));
func=GETENTRY(hdr,natives,index);
f=(AMX_NATIVE)func->address;
#if defined AMX_NATIVETABLE
} /* if */
#endif
assert(f!=NULL);
/* Now that we have found the function, patch the program so that any
* subsequent call will call the function directly (bypassing this
* callback).
* This trick cannot work in the JIT, because the program would need to
* be re-JIT-compiled after patching a P-code instruction.
*/
#if defined JIT && !defined NDEBUG
if ((amx->flags & AMX_FLAG_JITC)!=0)
assert(amx->sysreq_d==0);
#endif
if (amx->sysreq_d!=0) {
/* at the point of the call, the CIP pseudo-register points directly
* behind the SYSREQ instruction and its parameter(s)
*/
unsigned char *code=amx->base+(int)hdr->cod+(int)amx->cip-sizeof(cell);
assert(amx->cip >= 4 && amx->cip < (hdr->dat - hdr->cod));
assert_static(sizeof(f)<=sizeof(cell)); /* function pointer must fit in a cell */
if (amx->flags & AMX_FLAG_SYSREQN) /* SYSREQ.N has 2 parameters */
code-=sizeof(cell);
#if defined __GNUC__ || defined __ICC || defined ASM32
if (*(cell*)code==index) {
#else
if (*(cell*)code!=OP_SYSREQ_PRI) {
assert(*(cell*)(code-sizeof(cell))==OP_SYSREQ_C || *(cell*)(code-sizeof(cell))==OP_SYSREQ_N);
assert(*(cell*)code==index);
#endif
*(cell*)(code-sizeof(cell))=amx->sysreq_d;
*(cell*)code=(cell)f;
} /* if */
} /* if */
/* Note:
* params[0] == number of bytes for the additional parameters passed to the native function
* params[1] == first argument
* etc.
*/
amx->error=AMX_ERR_NONE;
*result = f(amx,params);
return amx->error;
}
#endif /* defined AMX_DEFCALLBACK */
#if defined JIT
extern int AMXAPI getMaxCodeSize(void);
extern int AMXAPI asm_runJIT(void *sourceAMXbase, void *jumparray, void *compiledAMXbase);
#endif
#if PAWN_CELL_SIZE==16 || defined AMX_DONT_RELOCATE
#define JUMPABS(base,ip) ((cell *)((base) + *(ip)))
#define RELOC_ABS(base, off)
#define RELOC_VALUE(base, v)
#else
#define JUMPABS(base, ip) ((cell *)*(ip))
#define RELOC_ABS(base, off) (*(ucell *)((base)+(int)(off)) += (ucell)(base))
#define RELOC_VALUE(base, v) ((v)+((ucell)(base)))
#endif
#define DBGPARAM(v) ( (v)=*(cell *)(code+(int)cip), cip+=sizeof(cell) )
#if defined AMX_INIT
static int amx_BrowseRelocate(AMX *amx)
{
AMX_HEADER *hdr;
unsigned char *code;
cell cip;
long codesize;
OPCODE op;
int sysreq_flg;
#if defined __GNUC__ || defined __ICC || defined ASM32 || defined JIT
cell *opcode_list;
#endif
#if defined JIT
int opcode_count = 0;
int reloc_count = 0;
#endif
assert(amx!=NULL);
hdr=(AMX_HEADER *)amx->base;
assert(hdr!=NULL);
assert(hdr->magic==AMX_MAGIC);
code=amx->base+(int)hdr->cod;
codesize=hdr->dat - hdr->cod;
amx->flags|=AMX_FLAG_BROWSE;
/* sanity checks */
assert_static(OP_PUSH_PRI==36);
assert_static(OP_PROC==46);
assert_static(OP_SHL==65);
assert_static(OP_SMUL==72);
assert_static(OP_EQ==95);
assert_static(OP_INC_PRI==107);
assert_static(OP_MOVS==117);
assert_static(OP_SYMBOL==126);
assert_static(OP_LOAD_BOTH==154);
amx->sysreq_d=0; /* preset */
sysreq_flg=0;
#if defined __GNUC__ || defined __ICC || defined ASM32 || defined JIT
amx_Exec(amx, (cell*)(void*)&opcode_list, 0);
#endif
/* start browsing code */
for (cip=0; cip<codesize; ) {
op=(OPCODE) *(ucell *)(code+(int)cip);
if ((unsigned)op>=OP_NUM_OPCODES) {
amx->flags &= ~AMX_FLAG_BROWSE;
return AMX_ERR_INVINSTR;
} /* if */
#if defined __GNUC__ || defined __ICC || defined ASM32 || defined JIT
/* relocate opcode (only works if the size of an opcode is at least
* as big as the size of a pointer (jump address); so basically we
* rely on the opcode and a pointer being 32-bit
*/
*(cell *)(code+(int)cip) = opcode_list[op];
#endif
#if defined JIT
opcode_count++;
#endif
cip+=sizeof(cell);
switch (op) {
#if !defined AMX_NO_MACRO_INSTR
case OP_PUSH5_C: /* instructions with 5 parameters */
case OP_PUSH5:
case OP_PUSH5_S:
case OP_PUSH5_ADR:
cip+=sizeof(cell)*5;
break;
case OP_PUSH4_C: /* instructions with 4 parameters */
case OP_PUSH4:
case OP_PUSH4_S:
case OP_PUSH4_ADR:
#if defined AMX_NO_MACRO_INSTR
amx->flags &= ~AMX_FLAG_BROWSE;
return AMX_ERR_INVINSTR;
#endif
cip+=sizeof(cell)*4;
break;
case OP_PUSH3_C: /* instructions with 3 parameters */
case OP_PUSH3:
case OP_PUSH3_S:
case OP_PUSH3_ADR:
#if defined AMX_NO_MACRO_INSTR
amx->flags &= ~AMX_FLAG_BROWSE;
return AMX_ERR_INVINSTR;
#endif
cip+=sizeof(cell)*3;
break;
case OP_PUSH2_C: /* instructions with 2 parameters */
case OP_PUSH2:
case OP_PUSH2_S:
case OP_PUSH2_ADR:
case OP_LOAD_BOTH:
case OP_LOAD_S_BOTH:
case OP_CONST:
case OP_CONST_S:
#if defined AMX_NO_MACRO_INSTR
amx->flags &= ~AMX_FLAG_BROWSE;
return AMX_ERR_INVINSTR;
#endif
cip+=sizeof(cell)*2;
break;
#endif /* !defined AMX_NO_MACRO_INSTR */
case OP_LOAD_PRI: /* instructions with 1 parameter */
case OP_LOAD_ALT:
case OP_LOAD_S_PRI:
case OP_LOAD_S_ALT:
case OP_LREF_PRI:
case OP_LREF_ALT:
case OP_LREF_S_PRI:
case OP_LREF_S_ALT:
case OP_LODB_I:
case OP_CONST_PRI:
case OP_CONST_ALT:
case OP_ADDR_PRI:
case OP_ADDR_ALT:
case OP_STOR_PRI:
case OP_STOR_ALT:
case OP_STOR_S_PRI:
case OP_STOR_S_ALT:
case OP_SREF_PRI:
case OP_SREF_ALT:
case OP_SREF_S_PRI:
case OP_SREF_S_ALT:
case OP_STRB_I:
case OP_LIDX_B:
case OP_IDXADDR_B:
case OP_ALIGN_PRI:
case OP_ALIGN_ALT:
case OP_LCTRL:
case OP_SCTRL:
case OP_PUSH_R:
case OP_PUSH_C:
case OP_PUSH:
case OP_PUSH_S:
case OP_STACK:
case OP_HEAP:
case OP_JREL:
case OP_SHL_C_PRI:
case OP_SHL_C_ALT:
case OP_SHR_C_PRI:
case OP_SHR_C_ALT:
case OP_ADD_C:
case OP_SMUL_C:
case OP_ZERO:
case OP_ZERO_S:
case OP_EQ_C_PRI:
case OP_EQ_C_ALT:
case OP_INC:
case OP_INC_S:
case OP_DEC:
case OP_DEC_S:
case OP_MOVS:
case OP_CMPS:
case OP_FILL:
case OP_HALT:
case OP_BOUNDS:
case OP_PUSH_ADR:
cip+=sizeof(cell);
break;
case OP_LOAD_I: /* instructions without parameters */
case OP_STOR_I:
case OP_LIDX:
case OP_IDXADDR:
case OP_MOVE_PRI:
case OP_MOVE_ALT:
case OP_XCHG:
case OP_PUSH_PRI:
case OP_PUSH_ALT:
case OP_POP_PRI:
case OP_POP_ALT:
case OP_PROC:
case OP_RET:
case OP_RETN:
case OP_CALL_PRI:
case OP_SHL:
case OP_SHR:
case OP_SSHR:
case OP_SMUL:
case OP_SDIV:
case OP_SDIV_ALT:
case OP_UMUL:
case OP_UDIV:
case OP_UDIV_ALT:
case OP_ADD:
case OP_SUB:
case OP_SUB_ALT:
case OP_AND:
case OP_OR:
case OP_XOR:
case OP_NOT:
case OP_NEG:
case OP_INVERT:
case OP_ZERO_PRI:
case OP_ZERO_ALT:
case OP_SIGN_PRI:
case OP_SIGN_ALT:
case OP_EQ:
case OP_NEQ:
case OP_LESS:
case OP_LEQ:
case OP_GRTR:
case OP_GEQ:
case OP_SLESS:
case OP_SLEQ:
case OP_SGRTR:
case OP_SGEQ:
case OP_INC_PRI:
case OP_INC_ALT:
case OP_INC_I:
case OP_DEC_PRI:
case OP_DEC_ALT:
case OP_DEC_I:
case OP_SYSREQ_PRI:
case OP_JUMP_PRI:
case OP_SWAP_PRI:
case OP_SWAP_ALT:
case OP_NOP:
case OP_BREAK:
break;
case OP_CALL: /* opcodes that need relocation */
case OP_JUMP:
case OP_JZER:
case OP_JNZ:
case OP_JEQ:
case OP_JNEQ:
case OP_JLESS:
case OP_JLEQ:
case OP_JGRTR:
case OP_JGEQ:
case OP_JSLESS:
case OP_JSLEQ:
case OP_JSGRTR:
case OP_JSGEQ:
case OP_SWITCH:
#if defined JIT
reloc_count++;
#endif
RELOC_ABS(code, cip);
cip+=sizeof(cell);
break;
case OP_SYSREQ_C:
cip+=sizeof(cell);
sysreq_flg|=0x01; /* mark SYSREQ.C found */
break;
#if !defined AMX_NO_MACRO_INSTR
case OP_SYSREQ_N:
cip+=sizeof(cell)*2;
sysreq_flg|=0x02; /* mark SYSREQ.N found */
break;
#endif
case OP_FILE:
case OP_SYMBOL: {
cell num;
DBGPARAM(num);
cip+=num;
break;
} /* case */
case OP_LINE:
case OP_SRANGE:
cip+=2*sizeof(cell);
break;
case OP_SYMTAG:
cip+=sizeof(cell);
break;
case OP_CASETBL: {
cell num;
int i;
DBGPARAM(num); /* number of records follows the opcode */
for (i=0; i<=num; i++) {
RELOC_ABS(code, cip+2*i*sizeof(cell));
#if defined JIT
reloc_count++;
#endif
} /* for */
cip+=(2*num + 1)*sizeof(cell);
break;
} /* case */
default:
amx->flags &= ~AMX_FLAG_BROWSE;
return AMX_ERR_INVINSTR;
} /* switch */
} /* for */
assert(sysreq_flg==0 || sysreq_flg==0x01 || sysreq_flg==0x02);
#if !defined AMX_DONT_RELOCATE
if (sysreq_flg==0x01 || sysreq_flg==0x02) {
/* only either type of system request opcode should be found (otherwise,
* we probably have a non-conforming compiler
*/
#if (defined __GNUC__ || defined __ICC || defined ASM32 || defined JIT) && !defined __64BIT__
/* to use direct system requests, a function pointer must fit in a cell;
* because the native function's address will be stored as the parameter
* of SYSREQ.D
*/
if ((amx->flags & AMX_FLAG_JITC)==0 && sizeof(AMX_NATIVE)<=sizeof(cell))
amx->sysreq_d=(sysreq_flg==0x01) ? opcode_list[OP_SYSREQ_D] : opcode_list[OP_SYSREQ_ND];
#else
/* ANSI C
* to use direct system requests, a function pointer must fit in a cell;
* see the comment above
*/
if (sizeof(AMX_NATIVE)<=sizeof(cell))
amx->sysreq_d=(sysreq_flg==0x01) ? OP_SYSREQ_D : OP_SYSREQ_ND;
#endif
} /* if */
#endif
#if defined JIT
amx->code_size = getMaxCodeSize()*opcode_count + hdr->cod
+ (hdr->stp - hdr->dat);
amx->reloc_size = 2*sizeof(cell)*reloc_count;
#endif
amx->flags &= ~AMX_FLAG_BROWSE;
amx->flags |= AMX_FLAG_RELOC;
if (sysreq_flg & 0x02)
amx->flags |= AMX_FLAG_SYSREQN;
return AMX_ERR_NONE;
}
#if AMX_COMPACTMARGIN > 2
static void expand(unsigned char *code, long codesize, long memsize)
{
ucell c;
struct {
long memloc;
ucell c;
} spare[AMX_COMPACTMARGIN];
int sh=0,st=0,sc=0;
int shift;
/* for in-place expansion, move from the end backward */
assert(memsize % sizeof(cell) == 0);
while (codesize>0) {
c=0;
shift=0;
do {
codesize--;
/* no input byte should be shifted out completely */
assert(shift<8*sizeof(cell));
/* we work from the end of a sequence backwards; the final code in
* a sequence may not have the continuation bit set */
assert(shift>0 || (code[(size_t)codesize] & 0x80)==0);
c|=(ucell)(code[(size_t)codesize] & 0x7f) << shift;
shift+=7;
} while (codesize>0 && (code[(size_t)codesize-1] & 0x80)!=0);
/* sign expand */
if ((code[(size_t)codesize] & 0x40)!=0) {
while (shift < (int)(8*sizeof(cell))) {
c|=(ucell)0xff << shift;
shift+=8;
} /* while */
} /* if */
/* store */
while (sc && (spare[sh].memloc>codesize)) {
*(ucell *)(code+(int)spare[sh].memloc)=spare[sh].c;
sh=(sh+1)%AMX_COMPACTMARGIN;
sc--;
} /* while */
memsize -= sizeof(cell);
assert(memsize>=0);
if ((memsize>codesize)||((memsize==codesize)&&(memsize==0))) {
*(ucell *)(code+(size_t)memsize)=c;
} else {
assert(sc<AMX_COMPACTMARGIN);
spare[st].memloc=memsize;
spare[st].c=c;
st=(st+1)%AMX_COMPACTMARGIN;
sc++;
} /* if */
} /* while */
/* when all bytes have been expanded, the complete memory block should be done */
assert(memsize==0);
}
#endif /* AMX_COMPACTMARGIN > 2 */
int AMXAPI amx_Init(AMX *amx,void *program)
{
AMX_HEADER *hdr;
int err;
unsigned char *data;
#if (defined _Windows || defined LINUX || defined __FreeBSD__ || defined __OpenBSD__) && !defined AMX_NODYNALOAD
#if defined _Windows
char libname[sNAMEMAX+8]; /* +1 for '\0', +3 for 'amx' prefix, +4 for extension */
typedef int (FAR WINAPI *AMX_ENTRY)(AMX _FAR *amx);
HINSTANCE hlib;
#elif defined LINUX || defined __FreeBSD__ || defined __OpenBSD__
char libname[_MAX_PATH];
char *root;
typedef int (*AMX_ENTRY)(AMX *amx);
void *hlib;
#if !defined AMX_LIBPATH
#define AMX_LIBPATH "AMXLIB"
#endif
#endif
int numlibraries,i;
AMX_FUNCSTUB *lib;
AMX_ENTRY libinit;
#endif
if ((amx->flags & AMX_FLAG_RELOC)!=0)
return AMX_ERR_INIT; /* already initialized (may not do so twice) */
hdr=(AMX_HEADER *)program;
/* the header is in Little Endian, on a Big Endian machine, swap all
* multi-byte words
*/
assert(check_endian());
#if BYTE_ORDER==BIG_ENDIAN
amx_Align32((uint32_t*)&hdr->size);
amx_Align16(&hdr->magic);
amx_Align16((uint16_t*)&hdr->flags);
amx_Align16((uint16_t*)&hdr->defsize);
amx_Align32((uint32_t*)&hdr->cod);
amx_Align32((uint32_t*)&hdr->dat);
amx_Align32((uint32_t*)&hdr->hea);
amx_Align32((uint32_t*)&hdr->stp);
amx_Align32((uint32_t*)&hdr->cip);
amx_Align32((uint32_t*)&hdr->publics);
amx_Align32((uint32_t*)&hdr->natives);
amx_Align32((uint32_t*)&hdr->libraries);
amx_Align32((uint32_t*)&hdr->pubvars);
amx_Align32((uint32_t*)&hdr->tags);
#endif
if (hdr->magic!=AMX_MAGIC)
return AMX_ERR_FORMAT;
if (hdr->file_version>CUR_FILE_VERSION || hdr->amx_version<MIN_FILE_VERSION)
return AMX_ERR_VERSION;
if (hdr->defsize!=sizeof(AMX_FUNCSTUB) && hdr->defsize!=sizeof(AMX_FUNCSTUBNT))
return AMX_ERR_FORMAT;
if (USENAMETABLE(hdr)) {
uint16_t *namelength;
/* when there is a separate name table, check the maximum name length
* in that table
*/
amx_Align32((uint32_t*)&hdr->nametable);
namelength=(uint16_t*)((unsigned char*)program + (unsigned)hdr->nametable);
amx_Align16(namelength);
if (*namelength>sNAMEMAX)
return AMX_ERR_FORMAT;
} /* if */
if (hdr->stp<=0)
return AMX_ERR_FORMAT;
#if BYTE_ORDER==BIG_ENDIAN
if ((hdr->flags & AMX_FLAG_COMPACT)==0) {
ucell *code=(ucell *)((unsigned char *)program+(int)hdr->cod);
while (code<(ucell *)((unsigned char *)program+(int)hdr->hea))
swapcell(code++);
} /* if */
#endif
assert((hdr->flags & AMX_FLAG_COMPACT)!=0 || hdr->hea == hdr->size);
if ((hdr->flags & AMX_FLAG_COMPACT)!=0) {