-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathm68.cxx
More file actions
10600 lines (9414 loc) · 422 KB
/
m68.cxx
File metadata and controls
10600 lines (9414 loc) · 422 KB
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
/*
#ifdef ARMOS
This emulates an extemely simple ARM64 Linux-like OS.
It can load and execute ARM64 apps built with Gnu tools in .elf files targeting Linux.
Written by David Lee in October 2024
#elif defined( RVOS )
This emulates an extemely simple RISC-V OS.
Per https://riscv.org/wp-content/uploads/2017/05/riscv-privileged-v1.10.pdf
It's an AEE (Application Execution Environment) that exposes an ABI (Application Binary Inferface) for an Application.
It runs in RISC-V M mode, similar to embedded systems.
It can load and execute 64-bit RISC-V apps built with Gnu tools in .elf files targeting Linux.
Written by David Lee in February 2023
Useful: https://github.com/jart/cosmopolitan/blob/1.0/libc/sysv/consts.sh
#elif defined( M68 )
This emulates 68000 machines running Linux, CP/M 68k, and the 68000 Visual Simulator Trap 0x15
how to build a cross compiler for 68000 on Windows:
http://www.aaldert.com/outrun/gcc-auto.html#:~:text=I've%20made%20the%2068000%20cross%20compiler%20build,have%20MinGW/MSYS%20installed%2C%20and%20have%20an%20internet
It also runs some cp/m 68k v1.3 binaries. Tested with the C compiler, assembler, and linker. The apps they produce also run
#elif defined( SPARCOS )
This emulates a SPARC V8 32-bit CPU running un usermode on Linux.
build a cross-compiler from here: https://gitlab.com/buildroot.org/buildroot/
#elif defined( X64OS )
This emulates an AMD64 CPU in Long Mode + 64-bit mode. No other modes are supported
#elif defined( X32OS )
This emulates an Intel x86 CPU in 32-bit real mode.
#else
#error one of ARMOS, RVOS, M68, SPARCOS, X64OS, or X32OS must be defined
#endif
*/
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <ctype.h>
#include <errno.h>
#include <vector>
#include <chrono>
#include <locale.h>
#include <cstddef>
#include <djl_os.hxx>
#include <linuxem.h>
#ifdef _WIN32
#include <io.h>
struct tms
{
uint64_t tms_utime;
uint64_t tms_stime;
uint64_t tms_cutime;
uint64_t tms_cstime;
};
#ifndef _SIZE_T_DEFINED
typedef SSIZE_T ssize_t;
#endif
#else
#include <unistd.h>
#include <sys/wait.h>
#include <termios.h>
#include <sys/ioctl.h>
#ifdef __mc68000__
#include <time.h>
#else
#include <sys/random.h>
#include <sys/uio.h>
#endif
#include <dirent.h>
#include <sys/times.h>
#include <sys/resource.h>
#if !defined( __APPLE__ ) && !defined( __mc68000__ )
#include <sys/sysinfo.h>
#endif
#ifdef __mc68000__
DIR * fdopendir( int fd );
extern "C" int lstat( const char * path, struct stat * statbuf );
extern "C" int pipe2( int pipefd[2], int flags );
extern "C" int wait4( pid_t pid, int * wstatus, int options, struct rusage * ru );
extern "C" int waitpid( int pid, int * wstatus, int options );
extern "C" int execve( const char * path, char * const argv[], char * const envp[] );
extern "C" char * realpath( const char *__restrict path, char *__restrict resolved_path );
#endif
struct LINUX_FIND_DATA
{
char cFileName[ EMULATOR_MAX_PATH ];
};
#ifndef PATH_MAX
#define PATH_MAX 4096
#endif
#endif
#include <djltrace.hxx>
#include <djl_con.hxx>
#include <djl_mmap.hxx>
using namespace std;
using namespace std::chrono;
#ifdef ARMOS
#include "arm64.hxx"
#define CPUClass Arm64
#define ELF_MACHINE_ISA 0xb7
#define APP_NAME "ARMOS"
#define LOGFILE_NAME "armos.log"
#define REG_FORMAT "%lld"
#define REG_TYPE uint64_t
#define SIGNED_REG_TYPE int64_t
#define ACCESS_REG( x ) cpu.regs[ x ]
#define CPU_IS_LITTLE_ENDIAN true
#define AT_PLATFORM_VALUE "aarch64"
#define REG_PROGRAM_COUNTER cpu.pc
#define REG_SYSCALL 8
#define REG_RESULT 0
#define REG_ARG0 0
#define REG_ARG1 1
#define REG_ARG2 2
#define REG_ARG3 3
#define REG_ARG4 4
#define REG_ARG5 5
#elif defined( RVOS )
#include "riscv.hxx"
#define CPUClass RiscV
#define ELF_MACHINE_ISA 0xf3
#define APP_NAME "RVOS"
#define LOGFILE_NAME "rvos.log"
#define REG_FORMAT "%lld"
#define REG_TYPE uint64_t
#define SIGNED_REG_TYPE int64_t
#define ACCESS_REG( x ) cpu.regs[ x ]
#define CPU_IS_LITTLE_ENDIAN true
#define AT_PLATFORM_VALUE "riscv"
#define REG_PROGRAM_COUNTER cpu.pc
#define REG_SYSCALL RiscV::a7
#define REG_RESULT RiscV::a0
#define REG_ARG0 RiscV::a0
#define REG_ARG1 RiscV::a1
#define REG_ARG2 RiscV::a2
#define REG_ARG3 RiscV::a3
#define REG_ARG4 RiscV::a4
#define REG_ARG5 RiscV::a5
#elif defined( M68 )
#include "m68000.hxx"
#define CPUClass m68000
#define ELF_MACHINE_ISA 4
#define APP_NAME "M68"
#define LOGFILE_NAME "m68.log"
#define REG_FORMAT "%d"
#define REG_TYPE uint32_t
#define SIGNED_REG_TYPE int32_t
#define ACCESS_REG( x ) cpu.dregs[ x ].l
#define CPU_IS_LITTLE_ENDIAN false
#define AT_PLATFORM_VALUE "m68knommu"
#define REG_PROGRAM_COUNTER cpu.pc
#define REG_SYSCALL 0
#define REG_RESULT 0
#define REG_ARG0 1
#define REG_ARG1 2
#define REG_ARG2 3
#define REG_ARG3 4
#define REG_ARG4 5
#define REG_ARG5 6
#elif defined( SPARCOS )
#include "sparc.hxx"
#define CPUClass Sparc
#define ELF_MACHINE_ISA 2
#define APP_NAME "SPARCOS"
#define LOGFILE_NAME "sparcos.log"
#define REG_FORMAT "%d"
#define REG_TYPE uint32_t
#define SIGNED_REG_TYPE int32_t
#define ACCESS_REG( x ) cpu.Sparc_reg( x )
#define CPU_IS_LITTLE_ENDIAN false
#define AT_PLATFORM_VALUE "sparc"
#define REG_PROGRAM_COUNTER cpu.pc
#define REG_SYSCALL 1 // g1
#define REG_RESULT 8 // o0
#define REG_ARG0 8 // o0..o5
#define REG_ARG1 9
#define REG_ARG2 10
#define REG_ARG3 11
#define REG_ARG4 12
#define REG_ARG5 13
#elif defined( X64OS )
#include "x64.hxx"
#define CPUClass x64
#define ELF_MACHINE_ISA 0x3e
#define APP_NAME "X64OS"
#define LOGFILE_NAME "x64os.log"
#define REG_FORMAT "%lld"
#define REG_TYPE uint64_t
#define SIGNED_REG_TYPE int64_t
#define ACCESS_REG( x ) cpu.regs[ x ].q
#define CPU_IS_LITTLE_ENDIAN true
#define AT_PLATFORM_VALUE "x86_64"
#define REG_PROGRAM_COUNTER cpu.rip
#define REG_SYSCALL x64::rax
#define REG_RESULT x64::rax
#define REG_ARG0 x64::rdi
#define REG_ARG1 x64::rsi
#define REG_ARG2 x64::rdx
#define REG_ARG3 x64::r10
#define REG_ARG4 x64::r8
#define REG_ARG5 x64::r9
#elif defined( X32OS )
#include "x64.hxx"
#define CPUClass x64
#define ELF_MACHINE_ISA 3
#define APP_NAME "X32OS"
#define LOGFILE_NAME "x32os.log"
#define REG_FORMAT "%d"
#define REG_TYPE uint32_t
#define SIGNED_REG_TYPE int32_t
#define ACCESS_REG( x ) cpu.regs[ x ].d
#define CPU_IS_LITTLE_ENDIAN true
#define AT_PLATFORM_VALUE "i686"
#define REG_PROGRAM_COUNTER ( (uint32_t) cpu.rip )
#define REG_SYSCALL x64::rax
#define REG_RESULT x64::rax
#define REG_ARG0 x64::rbx
#define REG_ARG1 x64::rcx
#define REG_ARG2 x64::rdx
#define REG_ARG3 x64::rsi
#define REG_ARG4 x64::rdi
#define REG_ARG5 x64::rbp
#else
#error "One of ARMOS, RVOS, M68, SPARCOS, X64OS, or X32OS must be defined for compilation"
#endif
#define CONCATENATE(e1, e2) e1 ## e2
#define PREFIX_L(s) CONCATENATE(L, s)
CDJLTrace tracer;
ConsoleConfiguration g_consoleConfig;
bool g_compressed_rvc = false; // is the app compressed risc-v?
const REG_TYPE g_arg_data_commit = 1024; // storage spot for command-line arguments and environment variables
REG_TYPE g_stack_commit = 128 * 1024; // RAM to allocate for the fixed stack. the top of this has argv data
REG_TYPE g_brk_commit = 40 * 1024 * 1024; // RAM to reserve if the app calls brk to allocate space. 40 meg default
REG_TYPE g_mmap_commit = 40 * 1024 * 1024; // RAM to reserve if the app calls mmap to allocate space. 40 meg default
bool g_terminate = false; // has the app asked to shut down?
int g_exit_code = 0; // exit code of the app in the vm
vector<uint8_t> memory; // RAM for the vm
REG_TYPE g_base_address = 0; // vm address of start of memory
REG_TYPE g_execution_address = 0; // where the program counter starts
REG_TYPE g_brk_offset = 0; // offset of brk, initially g_end_of_data
REG_TYPE g_mmap_offset = 0; // offset of where mmap allocations start
REG_TYPE g_highwater_brk = 0; // highest brk seen during app; peak dynamically-allocated RAM
REG_TYPE g_end_of_data = 0; // official end of the loaded app
REG_TYPE g_bottom_of_stack = 0; // just beyond where brk might move
REG_TYPE g_top_of_stack = 0; // argc, argv, penv, aux records sit above this
CMMap g_mmap; // for mmap and munmap system calls
bool g_addCRBeforeLF = true; // on Windows, a command-line argument can make this false so the emulated app acts like Linux
bool g_addTimeZoneToEnv = true; // on Windows, a command-line argument to control if the TZ is added to the environment
char * g_penvironment = 0; // 0 or initial environment variables from the command-line
char g_acLoadedApp[ EMULATOR_MAX_PATH ]; // path of the app being emulated
struct local_kernel_termios g_termios; // current state of stdin/stdout/stderr for those not redirected
// fake descriptors.
// /etc/timezone is not implemented, so apps running in the emulator on Windows assume UTC
const uint64_t findFirstDescriptor = 3000;
const uint64_t timebaseFrequencyDescriptor = 3001;
const uint64_t osreleaseDescriptor = 3002;
#if defined( __mc68000 ) || defined( sparc )
#define HOST_IS_LITTLE_ENDIAN false
#elif defined( __riscv ) || defined( __amd64__ ) || defined( __aarch64__ ) || defined( __i386__ ) || defined( _M_AMD64 ) || defined( _M_ARM64 ) || defined( _M_IX86 ) || defined( __ARM_32BIT_STATE )
#define HOST_IS_LITTLE_ENDIAN true
#else
#error "update this code to include the endianness of the ISA you are targeting"
#endif
uint64_t swap_endian64( uint64_t x )
{
if ( CPU_IS_LITTLE_ENDIAN != HOST_IS_LITTLE_ENDIAN )
return flip_endian64( x );
return x;
} //swap_endian64
uint32_t swap_endian32( uint32_t x )
{
if ( CPU_IS_LITTLE_ENDIAN != HOST_IS_LITTLE_ENDIAN )
return flip_endian32( x );
return x;
} //swap_endian32
uint16_t swap_endian16( uint16_t x )
{
if ( CPU_IS_LITTLE_ENDIAN != HOST_IS_LITTLE_ENDIAN )
return flip_endian16( x );
return x;
} //swap_endian16
#pragma pack( push, 1 )
struct ElfHeader64
{
uint32_t magic;
uint8_t bit_width;
uint8_t endianness;
uint8_t elf_version;
uint8_t os_abi;
uint8_t os_avi_version;
uint8_t padding[ 7 ];
uint16_t type;
uint16_t machine;
uint32_t version;
uint64_t entry_point;
uint64_t program_header_table;
uint64_t section_header_table;
uint32_t flags;
uint16_t header_size;
uint16_t program_header_table_size;
uint16_t program_header_table_entries;
uint16_t section_header_table_size;
uint16_t section_header_table_entries;
uint16_t section_with_section_names;
void swap_endianness()
{
type = swap_endian16( type );
machine = swap_endian16( machine );
version = swap_endian32( version );
entry_point = swap_endian64( entry_point );
program_header_table = swap_endian64( program_header_table );
section_header_table = swap_endian64( section_header_table );
flags = swap_endian32( flags );
header_size = swap_endian16( header_size );
program_header_table_size = swap_endian16( program_header_table_size );
program_header_table_entries = swap_endian16( program_header_table_entries );
section_header_table_size = swap_endian16( section_header_table_size );
section_header_table_entries = swap_endian16( section_header_table_entries );
section_with_section_names = swap_endian16( section_with_section_names );
}
};
struct ElfHeader32
{
uint32_t magic;
uint8_t bit_width;
uint8_t endianness;
uint8_t elf_version;
uint8_t os_abi;
uint8_t os_avi_version;
uint8_t padding[ 7 ];
uint16_t type;
uint16_t machine;
uint32_t version;
uint32_t entry_point;
uint32_t program_header_table;
uint32_t section_header_table;
uint32_t flags;
uint16_t header_size;
uint16_t program_header_table_size;
uint16_t program_header_table_entries;
uint16_t section_header_table_size;
uint16_t section_header_table_entries;
uint16_t section_with_section_names;
void swap_endianness()
{
type = swap_endian16( type );
machine = swap_endian16( machine );
version = swap_endian32( version );
entry_point = swap_endian32( entry_point );
program_header_table = swap_endian32( program_header_table );
section_header_table = swap_endian32( section_header_table );
flags = swap_endian32( flags );
header_size = swap_endian16( header_size );
program_header_table_size = swap_endian16( program_header_table_size );
program_header_table_entries = swap_endian16( program_header_table_entries );
section_header_table_size = swap_endian16( section_header_table_size );
section_header_table_entries = swap_endian16( section_header_table_entries );
section_with_section_names = swap_endian16( section_with_section_names );
}
void trace()
{
printf( "bit width %d\n", bit_width );
printf( "type %d\n", type );
printf( "machine %d\n", machine );
}
};
struct ElfSymbol64
{
uint32_t name; // index into the symbol string table
uint8_t info; // value of the symbol
uint8_t other;
uint16_t shndx;
uint64_t value; // address where the symbol resides in memory
uint64_t size; // length in memory of the symbol
const char * show_info() const
{
if ( 0 == info )
return "local";
if ( 1 == info )
return "global";
if ( 2 == info )
return "weak";
if ( 3 == info )
return "num";
if ( 4 == info )
return "file";
if ( 5 == info )
return "common";
if ( 6 == info )
return "tls";
if ( 7 == info )
return "num";
if ( 10 == info )
return "loos / gnu_ifunc";
if ( 12 == info )
return "hios";
if ( 13 == info )
return "loproc";
if ( 15 == info )
return "hiproc";
return "unknown";
} //show_info
void swap_endianness()
{
name = swap_endian32( name );
shndx = swap_endian16( shndx );
value = swap_endian64( value );
size = swap_endian64( size );
} //swap_endianness
const char * show_other() const
{
if ( 0 == other )
return "default";
if ( 1 == other )
return "internal";
if ( 2 == other )
return "hidden";
if ( 3 == other )
return "protected";
return "unknown";
}
};
struct ElfSymbol32
{
uint32_t name; // index into the symbol string table
uint32_t value; // address where the symbol resides in memory
uint32_t size; // length in memory of the symbol
uint8_t info; // value of the symbol
uint8_t other;
uint16_t shndx;
void swap_endianness()
{
name = swap_endian32( name );
shndx = swap_endian16( shndx );
value = swap_endian32( value );
size = swap_endian32( size );
} //swap_endianness
const char * show_info() const
{
if ( 0 == info )
return "local";
if ( 1 == info )
return "global";
if ( 2 == info )
return "weak";
if ( 3 == info )
return "num";
if ( 4 == info )
return "file";
if ( 5 == info )
return "common";
if ( 6 == info )
return "tls";
if ( 7 == info )
return "num";
if ( 10 == info )
return "loos / gnu_ifunc";
if ( 12 == info )
return "hios";
if ( 13 == info )
return "loproc";
if ( 15 == info )
return "hiproc";
return "unknown";
} //show_info
const char * show_other() const
{
if ( 0 == other )
return "default";
if ( 1 == other )
return "internal";
if ( 2 == other )
return "hidden";
if ( 3 == other )
return "protected";
return "unknown";
}
};
struct ElfProgramHeader64
{
uint32_t type;
uint32_t flags;
uint64_t offset_in_image;
uint64_t virtual_address;
uint64_t physical_address;
uint64_t file_size;
uint64_t memory_size;
uint64_t alignment;
void swap_endianness()
{
type = swap_endian32( type );
flags = swap_endian32( flags );
offset_in_image = swap_endian64( offset_in_image );
virtual_address = swap_endian64( virtual_address );
physical_address = swap_endian64( physical_address );
file_size = swap_endian64( file_size );
memory_size = swap_endian64( memory_size );
alignment = swap_endian64( alignment );
} //swap_endianness
const char * show_type() const
{
uint32_t basetype = ( type & 0xf );
if ( 0 == basetype )
return "unused";
if ( 1 == basetype )
return "load";
if ( 2 == basetype )
return "dynamic";
if ( 3 == basetype )
return "interp";
if ( 4 == basetype )
return "note";
if ( 5 == basetype )
return "shlib";
if ( 6 == basetype )
return "phdr";
if ( 7 == basetype )
return "tls";
if ( 8 == basetype )
return "num";
return "unknown";
}
const char * show_flags()
{
if ( 7 == flags )
return "rwe";
if ( 6 == flags )
return "rw";
if ( 5 == flags )
return "rx";
if ( 4 == flags )
return "r";
if ( 3 == flags )
return "wx";
if ( 2 == flags )
return "w";
if ( 1 == flags )
return "x";
return "";
}
};
struct ElfProgramHeader32
{
uint32_t type;
uint32_t offset_in_image;
uint32_t virtual_address;
uint32_t physical_address;
uint32_t file_size;
uint32_t memory_size;
uint32_t flags;
uint32_t alignment;
void swap_endianness()
{
type = swap_endian32( type );
offset_in_image = swap_endian32( offset_in_image );
virtual_address = swap_endian32( virtual_address );
physical_address = swap_endian32( physical_address );
file_size = swap_endian32( file_size );
flags = swap_endian32( flags );
memory_size = swap_endian32( memory_size );
alignment = swap_endian32( alignment );
} //swap_endianness
const char * show_type() const
{
uint32_t basetype = ( type & 0xf );
if ( 0 == basetype )
return "unused";
if ( 1 == basetype )
return "load";
if ( 2 == basetype )
return "dynamic";
if ( 3 == basetype )
return "interp";
if ( 4 == basetype )
return "note";
if ( 5 == basetype )
return "shlib";
if ( 6 == basetype )
return "phdr";
if ( 7 == basetype )
return "tls";
if ( 8 == basetype )
return "num";
return "unknown";
}
const char * show_flags()
{
if ( 7 == flags )
return "rwe";
if ( 6 == flags )
return "rw";
if ( 5 == flags )
return "rx";
if ( 4 == flags )
return "r";
if ( 3 == flags )
return "wx";
if ( 2 == flags )
return "w";
if ( 1 == flags )
return "x";
return "";
}
};
struct ElfSectionHeader64
{
uint32_t name_offset;
uint32_t type;
uint64_t flags;
uint64_t address;
uint64_t offset;
uint64_t size;
uint32_t link;
uint32_t info;
uint64_t address_alignment;
uint64_t entry_size;
void swap_endianness()
{
name_offset = swap_endian32( name_offset );
type = swap_endian32( type );
flags = swap_endian64( flags );
address = swap_endian64( address );
offset = swap_endian64( offset );
size = swap_endian64( size );
link = swap_endian32( link );
info = swap_endian32( info );
address_alignment = swap_endian64( address_alignment );
entry_size = swap_endian64( entry_size );
} //swap_endianness
const char * show_type() const
{
uint32_t basetype = ( type & 0xf );
if ( 0 == basetype )
return "unused";
if ( 1 == basetype )
return "program data";
if ( 2 == basetype )
return "symbol table";
if ( 3 == basetype )
return "string table";
if ( 4 == basetype )
return "relocation entries with addends";
if ( 5 == basetype )
return "symbol hash table";
if ( 6 == basetype )
return "dynamic";
if ( 7 == basetype )
return "note";
if ( 8 == basetype )
return "nobits";
if ( 9 == basetype )
return "relocation entries without addends";
if ( 10 == basetype )
return "shlib";
if ( 11 == basetype )
return "dynsym";
if ( 12 == basetype )
return "num";
if ( 14 == basetype )
return "initialization functions";
if ( 15 == basetype )
return "termination functions";
return "unknown";
}
const char * show_flags() const
{
static char ac[ 80 ];
ac[0] = 0;
if ( flags & 0x1 )
strcat( ac, "write, " );
if ( flags & 0x2 )
strcat( ac, "alloc, " );
if ( flags & 0x4 )
strcat( ac, "executable, " );
if ( flags & 0x10 )
strcat( ac, "merge, " );
if ( flags & 0x20 )
strcat( ac, "asciz strings, " );
return ac;
}
};
struct ElfSectionHeader32
{
uint32_t name_offset;
uint32_t type;
uint32_t flags;
uint32_t address;
uint32_t offset;
uint32_t size;
uint32_t link;
uint32_t info;
uint32_t address_alignment;
uint32_t entry_size;
void swap_endianness()
{
name_offset = swap_endian32( name_offset );
type = swap_endian32( type );
flags = swap_endian32( flags );
address = swap_endian32( address );
offset = swap_endian32( offset );
size = swap_endian32( size );
link = swap_endian32( link );
info = swap_endian32( info );
address_alignment = swap_endian32( address_alignment );
entry_size = swap_endian32( entry_size );
} //swap_endianness
const char * show_type() const
{
uint32_t basetype = ( type & 0xf );
if ( 0 == basetype )
return "unused";
if ( 1 == basetype )
return "program data";
if ( 2 == basetype )
return "symbol table";
if ( 3 == basetype )
return "string table";
if ( 4 == basetype )
return "relocation entries with addends";
if ( 5 == basetype )
return "symbol hash table";
if ( 6 == basetype )
return "dynamic";
if ( 7 == basetype )
return "note";
if ( 8 == basetype )
return "nobits";
if ( 9 == basetype )
return "relocation entries without addends";
if ( 10 == basetype )
return "shlib";
if ( 11 == basetype )
return "dynsym";
if ( 12 == basetype )
return "num";
if ( 14 == basetype )
return "initialization functions";
if ( 15 == basetype )
return "termination functions";
return "unknown";
}
const char * show_flags() const
{
static char ac[ 80 ];
ac[0] = 0;
if ( flags & 0x1 )
strcat( ac, "write, " );
if ( flags & 0x2 )
strcat( ac, "alloc, " );
if ( flags & 0x4 )
strcat( ac, "executable, " );
if ( flags & 0x10 )
strcat( ac, "merge, " );
if ( flags & 0x20 )
strcat( ac, "asciz strings, " );
return ac;
}
};
#pragma pack(pop)
static void usage( char const * perror = 0 )
{
g_consoleConfig.RestoreConsole( false );
if ( 0 != perror )
printf( "error: %s\n", perror );
printf( "usage: %s <%s arguments> <executable> <app arguments>\n", APP_NAME, APP_NAME );
printf( " arguments: -e environment. semicolon-separated list of name=value pairs\n" );
#ifdef RVOS
printf( " -g (internal) generate rcvtable.txt then exit\n" );
#endif
printf( " -h:X # of meg for the heap (brk space). 0..1024 are valid. default is 40\n" );
printf( " -i if -t is set, also enables instruction tracing with symbols\n" );
#ifdef _WIN32
printf( " -l don't let Windows translate LF (10) to CR (13) / LF (10)\n" );
#endif
printf( " -m:X # of meg for mmap space. 0..1024 are valid. default is 40.\n" );
printf( " -n just show information about the elf executable; don't actually run it\n" );
printf( " -p shows performance information at app exit\n" );
printf( " -s:X # of KB for stack space. 1..1024 are valid. default is 128.\n" );
printf( " -t enable debug tracing to %s\n", LOGFILE_NAME );
printf( " -v used with -e shows verbose information (e.g. symbols)\n" );
#ifdef _WIN32
printf( " -z on Windows, don't add time zone to environment at startup\n" );
#endif
printf( " %s\n", build_string() );
exit( 1 );
} //usage
#ifdef _WIN32
int map_win32_to_errno( DWORD win_error )
{
switch (win_error)
{
case ERROR_SUCCESS: return 0;
// File and Path errors
case ERROR_FILE_NOT_FOUND:
case ERROR_PATH_NOT_FOUND: return ENOENT; // No such file/dir
case ERROR_TOO_MANY_OPEN_FILES: return EMFILE; // Too many open files
case ERROR_ACCESS_DENIED:
case ERROR_SHARING_VIOLATION:
case ERROR_LOCK_VIOLATION: return EACCES; // Permission denied
case ERROR_FILE_EXISTS:
case ERROR_ALREADY_EXISTS: return EEXIST; // File exists (Error 17)
case ERROR_DIR_NOT_EMPTY: return ENOTEMPTY;
// Resource and Memory errors
case ERROR_OUTOFMEMORY:
case ERROR_NOT_ENOUGH_MEMORY: return ENOMEM; // Out of memory
case ERROR_DISK_FULL:
case ERROR_HANDLE_DISK_FULL: return ENOSPC; // No space left
// Argument and Handle errors
case ERROR_INVALID_HANDLE: return EBADF; // Bad file descriptor
case ERROR_INVALID_PARAMETER:
case ERROR_INVALID_NAME: return EINVAL; // Invalid argument
// Process/Thread errors
case ERROR_BROKEN_PIPE: return EPIPE; // Broken pipe
case ERROR_BUSY: return EBUSY; // Resource busy
case ERROR_NOT_SUPPORTED: return ENOSYS; // Function not implemented
default: return EINVAL; // Generic mapping
}
} //map_win32_to_errno
#endif
static uint64_t rand64()
{
uint64_t r = 0;
for ( int i = 0; i < 7; i++ )
r = ( r << 15 ) | ( rand() & 0x7FFF );
return r;
} //rand64
static void backslash_to_slash( char * p )
{
while ( *p )
{
if ( '\\' == *p )
*p = '/';
p++;
}
} //backslash_to_slash
#ifdef __mc68000__
extern "C" bool _setbinarymode( bool binmode );
#endif
class SetBinaryMode
{
int prevmodeout, prevmodeerr;
bool modeset;
public:
SetBinaryMode( bool set ) : modeset( false ), prevmodeout( 0 ), prevmodeerr( 0 )
{
#if defined( _WIN32 )
if ( set && !g_addCRBeforeLF )
{
fflush( stdout );
fflush( stderr );
_flushall();
prevmodeout = _setmode( _fileno( stdout ), _O_BINARY ); // don't convert LF (10) to CR LF (13 10)
prevmodeerr = _setmode( _fileno( stderr ), _O_BINARY ); // don't convert LF (10) to CR LF (13 10)
modeset = true;
}
#endif
#ifdef __mc68000__
if ( set && !g_addCRBeforeLF )
{
prevmodeout = _setbinarymode( true );
modeset = true;
}
#endif
}
~SetBinaryMode()
{
#if defined( _WIN32 )
if ( modeset )
{
fflush( stdout );
fflush( stderr );
_flushall();
_setmode( _fileno( stdout ), prevmodeout ); // likely back in text mode
_setmode( _fileno( stderr ), prevmodeerr ); // likely back in text mode
}
#endif
#ifdef __mc68000__
if ( modeset )
_setbinarymode( prevmodeout );
#endif
}
};
/*
what was their childhood trauma?
DOS & Windows Linux RISC-V64, AMD64, X32 Linux Arm32 & Arm64 macOS Linux sparc newlib 68000 Watcom Manx CP/M
------------- -------------------------- ------------------- ----- ----------- ------------ ------ ---------
0x0 O_RDONLY O_RDONLY O_RDONLY O_RDONLY O_RDONLY O_RDONLY O_RDONLY O_RDONLY
0x1 O_WRONLY O_WRONLY O_WRONLY O_WRONLY O_WRONLY O_WRONLY O_WRONLY O_WRONLY
0x2 O_RDRW O_RDRW O_RDRW O_RDRW O_RDRW O_RDRW O_RDRW O_RDRW
0x8 O_APPEND O_APPEND O_APPEND O_APPEND
0x10 O_RANDOM O_SHLOCK O_SHLOCK O_APPEND