@@ -439,24 +439,12 @@ typedef struct
439
439
{
440
440
mach_header ; uint32_t reserved ;
441
441
} mach_header_64;
442
- typedef struct {
443
- uint32_t cmd , cmdsize ;
444
- char segname [16 ];
445
- uint32_t vmaddr , vmsize , fileoff , filesize ;
446
- uint32_t maxprot , initprot , nsects , flags ;
447
- } mach_segment_command;
448
442
typedef struct {
449
443
uint32_t cmd , cmdsize ;
450
444
char segname [16 ];
451
445
uint64_t vmaddr , vmsize , fileoff , filesize ;
452
446
uint32_t maxprot , initprot , nsects , flags ;
453
447
} mach_segment_command_64;
454
- typedef struct {
455
- char sectname [16 ], segname [16 ];
456
- uint32_t addr , size ;
457
- uint32_t offset , align , reloff , nreloc , flags ;
458
- uint32_t reserved1 , reserved2 ;
459
- } mach_section;
460
448
typedef struct {
461
449
char sectname [16 ], segname [16 ];
462
450
uint64_t addr , size ;
@@ -466,133 +454,58 @@ typedef struct {
466
454
typedef struct {
467
455
uint32_t cmd , cmdsize , symoff , nsyms , stroff , strsize ;
468
456
} mach_symtab_command;
469
- typedef struct {
470
- int32_t strx ;
471
- uint8_t type , sect ;
472
- int16_t desc ;
473
- uint32_t value ;
474
- } mach_nlist;
475
457
typedef struct {
476
458
int32_t strx ;
477
459
uint8_t type , sect ;
478
460
uint16_t desc ;
479
461
uint64_t value ;
480
462
} mach_nlist_64;
481
- typedef struct
482
- {
483
- int32_t magic , nfat_arch ;
484
- } mach_fat_header;
485
- typedef struct
486
- {
487
- int32_t cputype , cpusubtype , offset , size , align ;
488
- } mach_fat_arch;
489
- typedef struct {
490
- struct {
491
- mach_header hdr ;
492
- mach_segment_command seg ;
493
- mach_section sec ;
494
- mach_symtab_command sym ;
495
- } arch [1 ];
496
- mach_nlist sym_entry ;
497
- uint8_t space [4096 ];
498
- } mach_obj;
499
463
typedef struct {
500
- struct {
501
- mach_header_64 hdr ;
502
- mach_segment_command_64 seg ;
503
- mach_section_64 sec ;
504
- mach_symtab_command sym ;
505
- } arch [1 ];
464
+ mach_header_64 hdr ;
465
+ mach_segment_command_64 seg ;
466
+ mach_section_64 sec ;
467
+ mach_symtab_command sym ;
506
468
mach_nlist_64 sym_entry ;
507
469
uint8_t space [4096 ];
508
470
} mach_obj_64;
509
- typedef struct {
510
- mach_fat_header fat ;
511
- mach_fat_arch fat_arch [2 ];
512
- struct {
513
- mach_header hdr ;
514
- mach_segment_command seg ;
515
- mach_section sec ;
516
- mach_symtab_command sym ;
517
- } arch [2 ];
518
- mach_nlist sym_entry ;
519
- uint8_t space [4096 ];
520
- } mach_fat_obj;
521
- typedef struct {
522
- mach_fat_header fat ;
523
- mach_fat_arch fat_arch [2 ];
524
- struct {
525
- mach_header_64 hdr ;
526
- mach_segment_command_64 seg ;
527
- mach_section_64 sec ;
528
- mach_symtab_command sym ;
529
- } arch [2 ];
530
- mach_nlist_64 sym_entry ;
531
- uint8_t space [4096 ];
532
- } mach_fat_obj_64;
533
471
]]
534
472
local symname = ' _' .. LJBC_PREFIX .. ctx .modname
535
- local isfat , is64 , align , mobj = false , false , 4 , " mach_obj"
536
- if ctx .arch == " x64" then
537
- is64 , align , mobj = true , 8 , " mach_obj_64"
538
- elseif ctx .arch == " arm" then
539
- isfat , mobj = true , " mach_fat_obj"
540
- elseif ctx .arch == " arm64" then
541
- is64 , align , isfat , mobj = true , 8 , true , " mach_fat_obj_64"
542
- else
543
- check (ctx .arch == " x86" , " unsupported architecture for OSX" )
473
+ local cputype , cpusubtype = 0x01000007 , 3
474
+ if ctx .arch ~= " x64" then
475
+ check (ctx .arch == " arm64" , " unsupported architecture for OSX" )
476
+ cputype , cpusubtype = 0x0100000c , 0
544
477
end
545
478
local function aligned (v , a ) return bit .band (v + a - 1 , - a ) end
546
- local be32 = bit .bswap -- Mach-O FAT is BE, supported archs are LE.
547
479
548
480
-- Create Mach-O object and fill in header.
549
- local o = ffi .new (mobj )
550
- local mach_size = aligned (ffi .offsetof (o , " space" )+# symname + 2 , align )
551
- local cputype = ({ x86 = {7 }, x64 = {0x01000007 }, arm = {7 ,12 }, arm64 = {0x01000007 ,0x0100000c } })[ctx .arch ]
552
- local cpusubtype = ({ x86 = {3 }, x64 = {3 }, arm = {3 ,9 }, arm64 = {3 ,0 } })[ctx .arch ]
553
- if isfat then
554
- o .fat .magic = be32 (0xcafebabe )
555
- o .fat .nfat_arch = be32 (# cpusubtype )
556
- end
481
+ local o = ffi .new (" mach_obj_64" )
482
+ local mach_size = aligned (ffi .offsetof (o , " space" )+# symname + 2 , 8 )
557
483
558
484
-- Fill in sections and symbols.
559
- for i = 0 ,# cpusubtype - 1 do
560
- local ofs = 0
561
- if isfat then
562
- local a = o .fat_arch [i ]
563
- a .cputype = be32 (cputype [i + 1 ])
564
- a .cpusubtype = be32 (cpusubtype [i + 1 ])
565
- -- Subsequent slices overlap each other to share data.
566
- ofs = ffi .offsetof (o , " arch" ) + i * ffi .sizeof (o .arch [0 ])
567
- a .offset = be32 (ofs )
568
- a .size = be32 (mach_size - ofs +# s )
569
- end
570
- local a = o .arch [i ]
571
- a .hdr .magic = is64 and 0xfeedfacf or 0xfeedface
572
- a .hdr .cputype = cputype [i + 1 ]
573
- a .hdr .cpusubtype = cpusubtype [i + 1 ]
574
- a .hdr .filetype = 1
575
- a .hdr .ncmds = 2
576
- a .hdr .sizeofcmds = ffi .sizeof (a .seg )+ ffi .sizeof (a .sec )+ ffi .sizeof (a .sym )
577
- a .seg .cmd = is64 and 0x19 or 0x1
578
- a .seg .cmdsize = ffi .sizeof (a .seg )+ ffi .sizeof (a .sec )
579
- a .seg .vmsize = # s
580
- a .seg .fileoff = mach_size - ofs
581
- a .seg .filesize = # s
582
- a .seg .maxprot = 1
583
- a .seg .initprot = 1
584
- a .seg .nsects = 1
585
- ffi .copy (a .sec .sectname , " __data" )
586
- ffi .copy (a .sec .segname , " __DATA" )
587
- a .sec .size = # s
588
- a .sec .offset = mach_size - ofs
589
- a .sym .cmd = 2
590
- a .sym .cmdsize = ffi .sizeof (a .sym )
591
- a .sym .symoff = ffi .offsetof (o , " sym_entry" )- ofs
592
- a .sym .nsyms = 1
593
- a .sym .stroff = ffi .offsetof (o , " sym_entry" )+ ffi .sizeof (o .sym_entry )- ofs
594
- a .sym .strsize = aligned (# symname + 2 , align )
595
- end
485
+ o .hdr .magic = 0xfeedfacf
486
+ o .hdr .cputype = cputype
487
+ o .hdr .cpusubtype = cpusubtype
488
+ o .hdr .filetype = 1
489
+ o .hdr .ncmds = 2
490
+ o .hdr .sizeofcmds = ffi .sizeof (o .seg )+ ffi .sizeof (o .sec )+ ffi .sizeof (o .sym )
491
+ o .seg .cmd = 0x19
492
+ o .seg .cmdsize = ffi .sizeof (o .seg )+ ffi .sizeof (o .sec )
493
+ o .seg .vmsize = # s
494
+ o .seg .fileoff = mach_size
495
+ o .seg .filesize = # s
496
+ o .seg .maxprot = 1
497
+ o .seg .initprot = 1
498
+ o .seg .nsects = 1
499
+ ffi .copy (o .sec .sectname , " __data" )
500
+ ffi .copy (o .sec .segname , " __DATA" )
501
+ o .sec .size = # s
502
+ o .sec .offset = mach_size
503
+ o .sym .cmd = 2
504
+ o .sym .cmdsize = ffi .sizeof (o .sym )
505
+ o .sym .symoff = ffi .offsetof (o , " sym_entry" )
506
+ o .sym .nsyms = 1
507
+ o .sym .stroff = ffi .offsetof (o , " sym_entry" )+ ffi .sizeof (o .sym_entry )
508
+ o .sym .strsize = aligned (# symname + 2 , 8 )
596
509
o .sym_entry .type = 0xf
597
510
o .sym_entry .sect = 1
598
511
o .sym_entry .strx = 1
0 commit comments