-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrvos.cxx
5109 lines (4451 loc) · 187 KB
/
rvos.cxx
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
#else
#error one of ARMOS, RVOS, or M68 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 <djl_os.hxx>
#ifdef _WIN32
#include <io.h>
struct iovec
{
void * iov_base; /* Starting address */
size_t iov_len; /* Length in bytes */
};
struct tms
{
uint64_t tms_utime;
uint64_t tms_stime;
uint64_t tms_cutime;
uint64_t tms_cstime;
};
typedef SSIZE_T ssize_t;
#else
#include <unistd.h>
#ifndef OLDGCC // the several-years-old Gnu C compilers for the RISC-V development boards
#ifndef M68K
#include <termios.h>
#include <sys/random.h>
#endif
#ifdef M68K
#include <time.h>
#else
#include <sys/uio.h>
#include <dirent.h>
#endif
#include <sys/times.h>
#include <sys/resource.h>
#if !defined( __APPLE__ ) && !defined( M68K )
#include <sys/sysinfo.h>
#endif
#ifndef M68K
// this structure is smaller than the usermode version. I don't know of a cross-platform header with it.
#define local_KERNEL_NCCS 19
struct local_kernel_termios
{
tcflag_t c_iflag; /* input mode flags */
tcflag_t c_oflag; /* output mode flags */
tcflag_t c_cflag; /* control mode flags */
tcflag_t c_lflag; /* local mode flags */
cc_t c_line; /* line discipline */
cc_t c_cc[local_KERNEL_NCCS]; /* control characters */
};
#endif
#endif
struct LINUX_FIND_DATA
{
char cFileName[ MAX_PATH ];
};
#endif
#if defined( _WIN32) || defined( M68K )
#define local_KERNEL_NCCS 19
struct local_kernel_termios
{
uint32_t c_iflag; /* input mode flags */
uint32_t c_oflag; /* output mode flags */
uint32_t c_cflag; /* control mode flags */
uint32_t c_lflag; /* local mode flags */
uint8_t c_line; /* line discipline */
uint8_t c_cc[local_KERNEL_NCCS]; /* control characters */
};
#endif
#include <djltrace.hxx>
#include <djl_con.hxx>
#include <djl_mmap.hxx>
using namespace std;
using namespace std::chrono;
#include "linuxem.h"
#ifdef ARMOS
#include "arm64.hxx"
#define CPUClass Arm64
#define ELF_MACHINE_ISA 0xb7
#define APP_NAME "ARMOS"
#define LOGFILE_NAME L"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 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 L"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 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 0x04
#define APP_NAME "M68"
#define LOGFILE_NAME L"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 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
#else
#error "One of ARMOS, RVOS, or M68 must be defined for compilation"
#endif
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
const REG_TYPE g_stack_commit = 128 * 1024; // RAM to allocate for the fixed stack. the top of this has argv data
#ifdef M68
REG_TYPE g_brk_commit = 10 * 1024 * 1024; // RAM to reserve if the app calls brk to allocate space. 10 meg default
REG_TYPE g_mmap_commit = 10 * 1024 * 1024; // RAM to reserve if the app mmap to allocate space. 10 meg default
#else
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 mmap to allocate space. 40 meg default
#endif
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_hostIsLittleEndian = true; // is the host little endian?
// 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;
#pragma warning(disable: 4200) // 0-sized array
struct linux_dirent64_syscall {
uint64_t d_ino; /* Inode number */
uint64_t d_off; /* Offset to next linux_dirent */
uint16_t d_reclen; /* Length of this linux_dirent */
uint8_t d_type; /* DT_DIR (4) if a dir, DT_REG (8) if a regular file */
char d_name[];
/* optional and not implemented. must be 0-filled
char pad
char d_type
*/
};
struct linux_timeval
{
uint64_t tv_sec; // time_t
uint64_t tv_usec; // suseconds_t
};
#pragma pack( push, 1 )
struct linux_timeval32
{
uint64_t tv_sec; // time_t
uint32_t tv_usec;
};
#pragma pack(pop)
struct linux_tms_syscall
{
uint64_t tms_utime;
uint64_t tms_stime;
uint64_t tms_cutime;
uint64_t tms_cstime;
};
struct linux_rusage_syscall {
struct linux_timeval ru_utime; /* user CPU time used */
struct linux_timeval ru_stime; /* system CPU time used */
long ru_maxrss; /* maximum resident set size */
long ru_ixrss; /* integral shared memory size */
long ru_idrss; /* integral unshared data size */
long ru_isrss; /* integral unshared stack size */
long ru_minflt; /* page reclaims (soft page faults) */
long ru_majflt; /* page faults (hard page faults) */
long ru_nswap; /* swaps */
long ru_inblock; /* block input operations */
long ru_oublock; /* block output operations */
long ru_msgsnd; /* IPC messages sent */
long ru_msgrcv; /* IPC messages received */
long ru_nsignals; /* signals received */
long ru_nvcsw; /* voluntary context switches */
long ru_nivcsw; /* involuntary context switches */
};
struct pollfd_syscall {
int fd;
short events;
short revents;
};
struct timespec_syscall {
uint64_t tv_sec;
uint64_t tv_nsec;
};
#define SYS_NMLN 65 // appears to be true for Arm64.
struct utsname_syscall {
/** The information returned by uname(). */
/** The OS name. "Linux" on Android. */
char sysname[SYS_NMLN];
/** The name on the network. Typically "localhost" on Android. */
char nodename[SYS_NMLN];
/** The OS release. Typically something like "4.4.115-g442ad7fba0d" on Android. */
char release[SYS_NMLN];
/** The OS version. Typically something like "#1 SMP PREEMPT" on Android. */
char version[SYS_NMLN];
/** The hardware architecture. Typically "aarch64" on Android. */
char machine[SYS_NMLN];
/** The domain name set by setdomainname(). Typically "localdomain" on Android. */
char domainname[SYS_NMLN];
};
struct stat_linux_syscall {
/*
struct stat info run on a 64-bit RISC-V system
sizeof s: 128
offset size field
0 8 st_dev
8 8 st_ino
16 4 st_mode
20 4 st_nlink
24 4 st_uid
28 4 st_gid
32 8 st_rdev
48 8 st_size
56 4 st_blksize
64 8 st_blocks
72 16 st_atim
88 16 st_mtim
104 16 st_ctim
*/
uint64_t st_dev; /* ID of device containing file */
uint64_t st_ino; /* Inode number */
uint32_t st_mode; /* File type and mode */
uint32_t st_nlink; /* Number of hard links */
uint32_t st_uid; /* User ID of owner */
uint32_t st_gid; /* Group ID of owner */
uint64_t st_rdev; /* Device ID (if special file) */
uint64_t st_mystery_spot;
uint64_t st_size; /* Total size, in bytes */
uint32_t st_blksize; /* Block size for filesystem I/O */
uint64_t st_blocks; /* Number of 512 B blocks allocated */
/* Since POSIX.1-2008, this structure supports nanosecond
precision for the following timestamp fields.
For the details before POSIX.1-2008, see VERSIONS. */
struct timespec st_atim; /* Time of last access */
struct timespec st_mtim; /* Time of last modification */
struct timespec st_ctim; /* Time of last status change */
#ifndef st_atime
#if !defined( OLDGCC ) && !defined( M68K )
#define st_atime st_atim.tv_sec /* Backward compatibility */
#define st_mtine st_mtim.tv_sec
#define st_ctime st_ctim.tv_sec
#endif
#endif
uint64_t st_mystery_spot_2;
};
uint64_t swap_endian64( uint64_t x )
{
if ( CPU_IS_LITTLE_ENDIAN != g_hostIsLittleEndian )
return flip_endian64( x );
return x;
} //swap_endian64
uint32_t swap_endian32( uint32_t x )
{
if ( CPU_IS_LITTLE_ENDIAN != g_hostIsLittleEndian )
return flip_endian32( x );
return x;
} //swap_endian32
uint16_t swap_endian16( uint16_t x )
{
if ( CPU_IS_LITTLE_ENDIAN != g_hostIsLittleEndian )
return flip_endian16( x );
return x;
} //swap_endian16
#pragma pack( push, 1 )
struct AuxProcessStart
{
uint64_t a_type; // AT_xxx ID from elf.h
union
{
uint64_t a_val;
void * a_ptr;
void ( * a_fcn )();
} a_un;
void swap_endianness()
{
a_type = swap_endian64( a_type );
a_un.a_val = swap_endian64( a_un.a_val );
}
};
struct AuxProcessStart32
{
uint32_t a_type; // AT_xxx ID from elf.h
union
{
uint32_t a_val;
void * a_ptr;
void ( * a_fcn )();
} a_un;
void swap_endianness()
{
a_type = swap_endian32( a_type );
a_un.a_val = swap_endian32( a_un.a_val );
}
};
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;
};
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
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;
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;
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> <elf_executable> <app arguments>\n", APP_NAME, APP_NAME );
printf( " arguments: -e just show information about the elf executable; don't actually run it\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\n" );
printf( " -m:X # of meg for mmap space. 0..1024 are valid. default is 40\n" );
printf( " -p shows performance information at app exit\n" );
printf( " -t enable debug tracing to %ls\n", LOGFILE_NAME );
printf( " -v used with -e shows verbose information (e.g. symbols)\n" );
printf( " %s\n", build_string() );
exit( 1 );
} //usage
static int gettimeofday( linux_timeval * tp )
{
// the older GCC chrono implementations are built on time(), which calls this but
// has only second resolution. So the microseconds returned here are lost.
// All other C++ implementations do the right thing.
namespace sc = std::chrono;
sc::system_clock::duration d = sc::system_clock::now().time_since_epoch();
sc::seconds s = sc::duration_cast<sc::seconds>( d );
tp->tv_sec = s.count();
tp->tv_usec = sc::duration_cast<sc::microseconds>( d - s ).count();
return 0;
} //gettimeofday
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 _WIN32
static void slash_to_backslash( char * p )
{
while ( *p )
{
if ( '/' == *p )
*p = '\\';
p++;
}
} //slash_to_backslash
static int windows_translate_flags( int flags )
{
// Translate open() flags from Linux to Windows
// Microsoft C uses different constants for flags than linux:
// msft (win+dos) linux 68000 newlib
// -------------- ----- ------------
// 0 O_RDONLY O_RDONLY O_RDONLY
// 1 O_WRONLY O_WRONLY O_WRONLY
// 2 O_RDRW O_RDWR O_RDWR
// 0x8 O_APPEND n/a O_APPEND
// 0x10 O_RANDOM _FMARK / O_SHLOCK / _FSHLOCK / O_SYNC
// 0x20 O_SEQUENTIAL _FDEFER / O_EXLOCK / _FEXLOCK
// 0x40 O_TEMPORARY O_CREAT
// 0x80 O_NOINHERIT O_EXCL
// 0x100 O_CREAT n/a
// 0x200 O_TRUNC O_TRUNC O_CREAT
// 0x400 O_EXCL O_APPEND O_TRUNC
// 0x800 O_EXCL
// 0x2000 O_OBTAINDIR O_ASYNC O_SYNC
// 0x4000 O_TEXT n/a O_NONBLOCK
// 0x8000 O_BINARY n/a
// 0x10100 n/a O_FSYNC, O_SYNC
int f = flags & 3; // copy rd/wr/rdrw
f |= O_BINARY; // this is assumed on Linux systems
#ifdef M68
if ( 0x200 & flags )
f |= O_CREAT;
if ( 0x800 & flags )
f |= O_EXCL;
if ( 0x400 & flags )
f |= O_TRUNC;
if ( 0x8 & flags )
f |= O_APPEND;
#else // MacOS + Linux