premier-lie.hex2
← Le code · src/jonction/premier-lie.hex2 · 353 lignes
# premier-lie.hex2 : premier Mach-O lié dynamiquement produit à la main.
# Appelle _write(1, "miaou\n", 6) puis _exit(0). Symboles importés de
# /usr/lib/libSystem.B.dylib via le GOT résolu par dyld au chargement.
# Assemblé par src/amorce/adresses.macos-arm64, signé par signeur.macos-arm64.
# Épreuve J1 du plan de la jonction ; carnet src/jonction/etude-j1.md.
# ---------------------------------------------------------------------------
# En-tête Mach-O (32 octets)
:entete
cf fa ed fe # magic MH_MAGIC_64
0c 00 00 01 # cputype ARM64
00 00 00 00 # cpusubtype ALL (arm64 ordinaire, pas arm64e)
02 00 00 00 # filetype MH_EXECUTE
0d 00 00 00 # ncmds = 13
&fin_commandes>commandes # sizeofcmds
85 00 20 00 # flags NOUNDEFS DYLDLINK TWOLEVEL PIE
00 00 00 00 # reserved
:commandes
# ---------------------------------------------------------------------------
# LC_SEGMENT_64 __PAGEZERO (72 octets) : 4 Gio sans accès
:pagezero
19 00 00 00 48 00 00 00 # cmd LC_SEGMENT_64, cmdsize 72
5f 5f 50 41 47 45 5a 45 52 4f 00 00 00 00 00 00 # "__PAGEZERO"
00 00 00 00 00 00 00 00 # vmaddr 0
00 00 00 00 01 00 00 00 # vmsize 0x100000000
00 00 00 00 00 00 00 00 # fileoff 0
00 00 00 00 00 00 00 00 # filesize 0
00 00 00 00 00 00 00 00 # maxprot/initprot 0
00 00 00 00 00 00 00 00 # nsects 0, flags 0
# ---------------------------------------------------------------------------
# LC_SEGMENT_64 __TEXT (232 octets, 2 sections) : code + chaîne
:texte
19 00 00 00 e8 00 00 00 # cmd LC_SEGMENT_64, cmdsize 232
5f 5f 54 45 58 54 00 00 00 00 00 00 00 00 00 00 # "__TEXT"
00 00 00 00 01 00 00 00 # vmaddr 0x100000000
00 40 00 00 00 00 00 00 # vmsize 0x4000
00 00 00 00 00 00 00 00 # fileoff 0
00 40 00 00 00 00 00 00 # filesize 0x4000
05 00 00 00 05 00 00 00 # maxprot/initprot r-x
02 00 00 00 00 00 00 00 # nsects 2, flags 0
# section __text (80 octets)
5f 5f 74 65 78 74 00 00 00 00 00 00 00 00 00 00 # "__text"
5f 5f 54 45 58 54 00 00 00 00 00 00 00 00 00 00 # "__TEXT"
&code 01 00 00 00 # addr = 0x100000000 + offset(code)
&fin_code>code 00 00 00 00 # size
&code # offset(code) uint32
02 00 00 00 # align 2^2 = 4
00 00 00 00 00 00 00 00 # reloff 0, nreloc 0
00 04 00 80 # flags PURE_INSTRUCTIONS SOME_INSTRUCTIONS
00 00 00 00 # reserved1
00 00 00 00 # reserved2
00 00 00 00 # reserved3
# section __cstring (80 octets)
5f 5f 63 73 74 72 69 6e 67 00 00 00 00 00 00 00 # "__cstring"
5f 5f 54 45 58 54 00 00 00 00 00 00 00 00 00 00 # "__TEXT"
&chaine 01 00 00 00 # addr = 0x100000000 + offset(chaine)
&fin_chaine>chaine 00 00 00 00 # size
&chaine # offset(chaine) uint32
00 00 00 00 # align 2^0 = 1
00 00 00 00 00 00 00 00 # reloff 0, nreloc 0
02 00 00 00 # flags S_CSTRING_LITERALS
00 00 00 00 # reserved1
00 00 00 00 # reserved2
00 00 00 00 # reserved3
# ---------------------------------------------------------------------------
# LC_SEGMENT_64 __DATA_CONST (152 octets, 1 section __got) : pointeurs liés
:data_const
19 00 00 00 98 00 00 00 # cmd LC_SEGMENT_64, cmdsize 152
5f 5f 44 41 54 41 5f 43 4f 4e 53 54 00 00 00 00 # "__DATA_CONST"
00 40 00 00 01 00 00 00 # vmaddr 0x100004000
00 40 00 00 00 00 00 00 # vmsize 0x4000
00 40 00 00 00 00 00 00 # fileoff 0x4000
10 00 00 00 00 00 00 00 # filesize 16 (deux pointeurs de 8 octets)
03 00 00 00 03 00 00 00 # maxprot/initprot rw-
01 00 00 00 10 00 00 00 # nsects 1, flags SG_READ_ONLY (0x10)
# section __got (80 octets)
5f 5f 67 6f 74 00 00 00 00 00 00 00 00 00 00 00 # "__got"
5f 5f 44 41 54 41 5f 43 4f 4e 53 54 00 00 00 00 # "__DATA_CONST"
00 40 00 00 01 00 00 00 # addr 0x100004000
10 00 00 00 00 00 00 00 # size 16
00 40 00 00 # offset 0x4000
03 00 00 00 # align 2^3 = 8
00 00 00 00 00 00 00 00 # reloff 0, nreloc 0
06 00 00 00 # flags S_NON_LAZY_SYMBOL_POINTERS
00 00 00 00 # reserved1 : index 0 dans indirect symbol table
00 00 00 00 # reserved2
00 00 00 00 # reserved3
# ---------------------------------------------------------------------------
# LC_SEGMENT_64 __LINKEDIT (72 octets)
:linkedit_cmd
19 00 00 00 48 00 00 00 # cmd LC_SEGMENT_64, cmdsize 72
5f 5f 4c 49 4e 4b 45 44 49 54 00 00 00 00 00 00 # "__LINKEDIT"
00 80 00 00 01 00 00 00 # vmaddr 0x100008000
00 40 00 00 00 00 00 00 # vmsize 0x4000
00 80 00 00 00 00 00 00 # fileoff 0x8000
&fin_fichier>linkedit 00 00 00 00 # filesize
01 00 00 00 01 00 00 00 # maxprot/initprot r--
00 00 00 00 00 00 00 00 # nsects 0, flags 0
# ---------------------------------------------------------------------------
# LC_DYLD_CHAINED_FIXUPS (16 octets)
34 00 00 80 10 00 00 00 # cmd 0x80000034, cmdsize 16
&chained_fixups # dataoff uint32
&fin_chained>chained_fixups # datasize uint32
# ---------------------------------------------------------------------------
# LC_SYMTAB (24 octets)
02 00 00 00 18 00 00 00 # cmd, cmdsize
&symtab # symoff uint32
02 00 00 00 # nsyms 2
&strtab # stroff uint32
&fin_strtab>strtab # strsize uint32
# ---------------------------------------------------------------------------
# LC_DYSYMTAB (80 octets)
0b 00 00 00 50 00 00 00 # cmd, cmdsize
00 00 00 00 00 00 00 00 # ilocalsym 0, nlocalsym 0
00 00 00 00 00 00 00 00 # iextdefsym 0, nextdefsym 0
00 00 00 00 02 00 00 00 # iundefsym 0, nundefsym 2
00 00 00 00 00 00 00 00 # tocoff 0, ntoc 0
00 00 00 00 00 00 00 00 # modtaboff 0, nmodtab 0
00 00 00 00 00 00 00 00 # extrefsymoff 0, nextrefsyms 0
00 00 00 00 00 00 00 00 # indirectsymoff 0, nindirectsyms 0
00 00 00 00 00 00 00 00 # extreloff 0, nextrel 0
00 00 00 00 00 00 00 00 # locreloff 0, nlocrel 0
# ---------------------------------------------------------------------------
# LC_LOAD_DYLINKER (32 octets) : /usr/lib/dyld
0e 00 00 00 20 00 00 00 # cmd, cmdsize 32
0c 00 00 00 # name offset 12
2f 75 73 72 2f 6c 69 62 2f 64 79 6c 64 00 00 00 # "/usr/lib/dyld\0\0\0"
00 00 00 00 # remplissage jusqu'à 32
# ---------------------------------------------------------------------------
# LC_UUID (24 octets) : identifiant fixe
1b 00 00 00 18 00 00 00 # cmd, cmdsize 24
6d 69 61 6f 75 5f 6a 31 # "miaou_j1"
00 00 00 00 00 00 00 01 # ...suite de l'uuid
# ---------------------------------------------------------------------------
# LC_BUILD_VERSION (24 octets) : MACOS 11.0.0, pas d'outil
32 00 00 00 18 00 00 00 # cmd, cmdsize 24
01 00 00 00 # platform MACOS (1)
00 00 0b 00 # minos 11.0.0
00 00 0b 00 # sdk 11.0.0
00 00 00 00 # ntools 0
# ---------------------------------------------------------------------------
# LC_MAIN (24 octets)
28 00 00 80 18 00 00 00 # cmd 0x80000028, cmdsize 24
&code 00 00 00 00 # entryoff = offset(code) dans le fichier
00 00 00 00 00 00 00 00 # stacksize 0
# ---------------------------------------------------------------------------
# LC_LOAD_DYLIB (56 octets) : /usr/lib/libSystem.B.dylib
0c 00 00 00 38 00 00 00 # cmd, cmdsize 56
18 00 00 00 # name offset 24
02 00 00 00 # time stamp 2
00 00 01 00 # current version 1.0.0
00 00 01 00 # compatibility version 1.0.0
2f 75 73 72 2f 6c 69 62 2f 6c 69 62 53 79 73 74 # "/usr/lib/libSyst"
65 6d 2e 42 2e 64 79 6c 69 62 00 00 00 00 00 00 # "em.B.dylib\0\0\0\0\0\0"
# ---------------------------------------------------------------------------
# LC_CODE_SIGNATURE (16 octets)
1d 00 00 00 10 00 00 00 # cmd, cmdsize 16
&signature # dataoff uint32
&fin_signature>signature # datasize uint32
:fin_commandes
# ---------------------------------------------------------------------------
# __text : le code (11 instructions, 44 octets)
:code
# _write(1, chaine, 6)
20 00 80 d2 # mov x0, #1 (fd stdout)
01 00 00 10 %chaine # adr x1, chaine
c2 00 80 d2 # mov x2, #6 (count)
08 00 00 10 %got_write # adr x8, got_write
08 01 40 f9 # ldr x8, [x8]
00 01 3f d6 # blr x8
# _exit(0)
00 00 80 d2 # mov x0, #0
08 00 00 10 %got_exit # adr x8, got_exit
08 01 40 f9 # ldr x8, [x8]
00 01 3f d6 # blr x8
# garde-fou : brk si on revient de _exit
00 00 20 d4 # brk #0
:fin_code
# ---------------------------------------------------------------------------
# __cstring : la chaîne littérale
:chaine
6d 69 61 6f 75 0a # "miaou\n"
:fin_chaine
# ---------------------------------------------------------------------------
# Bourrage jusqu'à __DATA_CONST à 0x4000
!4000
:data_const_debut
# ---------------------------------------------------------------------------
# __got : deux entrées de 8 octets, encodées comme dyld_chained_ptr_64_bind.
# Chaque entrée : ordinal(24) addend(8) reserved(19) next(12) bind(1)
# bind=1 va en bit 63. next en stride de 4 octets vers l'entrée suivante.
# Entrée 0 (_write) : ordinal=0, next=2 (= 8 octets = 2 strides de 4), bind=1
# dyld_chained_ptr_64_bind : ordinal(24) addend(8) reserved(19) next(12) bind(1)
# bits 0-23 = ordinal = 0, bits 51-62 = next = 2 (bit 52 set), bit 63 = bind = 1
# valeur u64 = (2 << 51) | (1 << 63) = 0x8010000000000000
# octets little-endian : 00 00 00 00 00 00 10 80
:got_write
00 00 00 00 00 00 10 80
# Entrée 1 (_exit) : ordinal=1, next=0 (fin de chaîne), bind=1
# valeur u64 = 1 | (1 << 63) = 0x8000000000000001
# octets little-endian : 01 00 00 00 00 00 00 80
:got_exit
01 00 00 00 00 00 00 80
# ---------------------------------------------------------------------------
# Bourrage jusqu'à __LINKEDIT à 0x8000
!4000
:linkedit
# ---------------------------------------------------------------------------
# dyld_chained_fixups payload
:chained_fixups
# dyld_chained_fixups_header (28 octets)
00 00 00 00 # fixups_version 0
20 00 00 00 # starts_offset 32 (dyld_chained_starts_in_image)
50 00 00 00 # imports_offset 80
58 00 00 00 # symbols_offset 88
02 00 00 00 # imports_count 2
01 00 00 00 # imports_format DYLD_CHAINED_IMPORT
00 00 00 00 # symbols_format 0 (uncompressed)
# padding de 4 octets
00 00 00 00 # pad jusqu'à offset 32
# dyld_chained_starts_in_image (offset 32)
04 00 00 00 # seg_count 4 (PAGEZERO, TEXT, DATA_CONST, LINKEDIT)
00 00 00 00 # seg_info_offset[0] PAGEZERO : 0 (pas de fixup)
00 00 00 00 # seg_info_offset[1] TEXT : 0
18 00 00 00 # seg_info_offset[2] DATA_CONST : 24 (relatif à starts_in_image)
00 00 00 00 # seg_info_offset[3] LINKEDIT : 0
# padding de 4 octets pour aligner starts_in_segment à offset 56 (= 32+24)
00 00 00 00
# dyld_chained_starts_in_segment (offset 32+24=56, taille 24)
18 00 00 00 # size 24
00 40 # page_size 0x4000 (16 Kio)
02 00 # pointer_format 2 (DYLD_CHAINED_PTR_64)
00 40 00 00 00 00 00 00 # segment_offset 0x4000 (offset VM du __DATA_CONST, relatif à imagebase)
00 00 00 00 # max_valid_pointer 0 (64-bit)
01 00 # page_count 1
00 00 # page_start[0] 0 (début de chaîne au premier octet)
# imports (offset 80)
# DYLD_CHAINED_IMPORT : lib_ordinal(8) weak(1) name_offset(23)
# _write : ordinal=1 (libSystem), name_offset=1 (après le \0 initial)
01 02 00 00 # 0x00000201 : lib=1, weak=0, name_offset=1
# _exit : ordinal=1, name_offset=8 (après "\0_write\0")
01 10 00 00 # 0x00001001 : lib=1, weak=0, name_offset=8
# symbols table "\0_write\0_exit\0" (offset 88)
:symbols_chained
00 # \0 initial
5f 77 72 69 74 65 00 # "_write\0"
5f 65 78 69 74 00 # "_exit\0"
:fin_chained
# padding jusqu'à multiple de 8 (fin à 102, padder à 104)
00 00
# ---------------------------------------------------------------------------
# LC_SYMTAB : table des symboles au format nlist_64
:symtab
# nlist_64 _write : n_strx=1, n_type=0x01 (N_UNDF|N_EXT), n_sect=0, n_desc=0x0100 (lib ord 1), n_value=0
01 00 00 00 # n_strx 1
01 # n_type (N_UNDF=0x00 | N_EXT=0x01)
00 # n_sect 0
00 01 # n_desc 0x0100 (library ordinal 1, dans bits 8-15)
00 00 00 00 00 00 00 00 # n_value 0
# nlist_64 _exit : n_strx=8, n_type=0x01, n_sect=0, n_desc=0x0100, n_value=0
08 00 00 00 # n_strx 8
01 # n_type
00 # n_sect
00 01 # n_desc 0x0100
00 00 00 00 00 00 00 00 # n_value 0
# strtab "\0_write\0_exit\0"
:strtab
00 # \0 initial (n_strx=0)
5f 77 72 69 74 65 00 # "_write\0" (n_strx=1)
5f 65 78 69 74 00 # "_exit\0" (n_strx=8)
:fin_strtab
# ---------------------------------------------------------------------------
# Padding jusqu'à la signature (multiple de 16)
!10
:signature
# ---------------------------------------------------------------------------
# Signature ad hoc (gros-boutiste), SuperBlob avec un CodeDirectory.
# codeLimit = &signature (position de la signature, tout ce qui précède).
# 9 pages de 4 Kio (0x0000 à 0x8000 par pas de 0x1000, dernière de 0 à 0x80A0).
#
# Taille totale : 20 (SuperBlob+index) + 88 (CDheader) + 4 (ident "lie\0") + 9*32 (hashes) = 400 octets.
fa de 0c c0 00 00 01 90 # magic EMBEDDED_SIGNATURE, length 400
00 00 00 01 # count 1
00 00 00 00 00 00 00 14 # type CODEDIRECTORY (0), offset 20
# CodeDirectory (380 octets)
fa de 0c 02 00 00 01 7c # magic CODEDIRECTORY, length 380
00 02 04 00 # version 0x20400
00 02 00 02 # flags ADHOC | LINKER_SIGNED
00 00 00 5c # hashOffset 92 (= 88 + 4 bytes ident)
00 00 00 58 # identOffset 88
00 00 00 00 # nSpecialSlots 0
00 00 00 09 # nCodeSlots 9
00 00 80 a0 # codeLimit : signeur vérifie qu'il vaut dataoff
20 02 00 0c # hashSize 32, hashType SHA-256 (2), platform 0, pageSize 2^12
00 00 00 00 # spare2
00 00 00 00 # scatterOffset
00 00 00 00 # teamOffset
00 00 00 00 # spare3
00 00 00 00 00 00 00 00 # codeLimit64
00 00 00 00 00 00 00 00 # execSegBase 0
00 00 00 00 00 00 40 00 # execSegLimit 0x4000
00 00 00 00 00 00 00 01 # execSegFlags MAIN_BINARY
# ident "lie\0" (4 octets)
6c 69 65 00
# 9 cases SHA-256, remplies par le signeur
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
:fin_signature
# Padding à 16 octets pour la fin
!10
:fin_fichier