miaou — le blogv1.0.3

cat-lie.hex2

← Le code · src/jonction/cat-lie.hex2 · 349 lignes

# cat-lie.hex2 : deuxième binaire lié dynamiquement produit à la main.
# Un cat minimal : lit l'entrée standard par blocs de 4 Kio et la réécrit sur
# la sortie standard, jusqu'à EOF. Utilise trois symboles de libSystem :
# _read, _write, _exit. Buffer sur la pile (4 Kio).
# Structure identique à premier-lie.hex2 : 13 load commands, PAGEZERO + TEXT
# + DATA_CONST + LINKEDIT, DYLD_CHAINED_PTR_64, signature ad hoc.
# Épreuve J1 enrichie : plusieurs imports, boucle, buffer sur la pile.
 
# ---------------------------------------------------------------------------
# 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
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)
:pagezero
19 00 00 00 48 00 00 00  # cmd, cmdsize 72
5f 5f 50 41 47 45 5a 45 52 4f 00 00 00 00 00 00
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 (152 octets, 1 section __text) : code seul, pas de chaîne
:texte
19 00 00 00 98 00 00 00  # cmd, cmdsize 152
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  # r-x
01 00 00 00 00 00 00 00  # nsects 1, 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
&fin_code>code 00 00 00 00  # size
&code                    # offset
02 00 00 00              # align 2^2
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
 
# ---------------------------------------------------------------------------
# LC_SEGMENT_64 __DATA_CONST (152 octets, 1 section __got) : 3 entrées
:data_const
19 00 00 00 98 00 00 00  # cmd, 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
18 00 00 00 00 00 00 00  # filesize 24 (trois pointeurs)
03 00 00 00 03 00 00 00  # rw-
01 00 00 00 10 00 00 00  # nsects 1, flags SG_READ_ONLY
 
# 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
18 00 00 00 00 00 00 00  # size 24
00 40 00 00              # offset 0x4000
03 00 00 00              # align 2^3
00 00 00 00 00 00 00 00  # reloff/nreloc
06 00 00 00              # flags S_NON_LAZY_SYMBOL_POINTERS
00 00 00 00              # reserved1
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, cmdsize 72
5f 5f 4c 49 4e 4b 45 44 49 54 00 00 00 00 00 00
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  # r--
00 00 00 00 00 00 00 00
 
# ---------------------------------------------------------------------------
# LC_DYLD_CHAINED_FIXUPS (16 octets)
34 00 00 80 10 00 00 00
&chained_fixups          # dataoff uint32
&fin_chained>chained_fixups  # datasize uint32
 
# ---------------------------------------------------------------------------
# LC_SYMTAB (24 octets)
02 00 00 00 18 00 00 00
&symtab                  # symoff uint32
03 00 00 00              # nsyms 3
&strtab                  # stroff uint32
&fin_strtab>strtab       # strsize uint32
 
# ---------------------------------------------------------------------------
# LC_DYSYMTAB (80 octets)
0b 00 00 00 50 00 00 00
00 00 00 00 00 00 00 00  # ilocalsym/nlocalsym
00 00 00 00 00 00 00 00  # iextdefsym/nextdefsym
00 00 00 00 03 00 00 00  # iundefsym 0, nundefsym 3
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
 
# ---------------------------------------------------------------------------
# LC_LOAD_DYLINKER (32 octets)
0e 00 00 00 20 00 00 00
0c 00 00 00
2f 75 73 72 2f 6c 69 62 2f 64 79 6c 64 00 00 00
00 00 00 00
 
# ---------------------------------------------------------------------------
# LC_UUID (24 octets)
1b 00 00 00 18 00 00 00
6d 69 61 6f 75 5f 6a 31
00 00 00 00 00 00 00 02
 
# ---------------------------------------------------------------------------
# LC_BUILD_VERSION (24 octets)
32 00 00 00 18 00 00 00
01 00 00 00              # platform MACOS
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
&code 00 00 00 00        # entryoff
00 00 00 00 00 00 00 00  # stacksize
 
# ---------------------------------------------------------------------------
# LC_LOAD_DYLIB (56 octets)
0c 00 00 00 38 00 00 00
18 00 00 00              # name offset 24
02 00 00 00              # time stamp
00 00 01 00              # current version
00 00 01 00              # compatibility version
2f 75 73 72 2f 6c 69 62 2f 6c 69 62 53 79 73 74
65 6d 2e 42 2e 64 79 6c 69 62 00 00 00 00 00 00
 
# ---------------------------------------------------------------------------
# LC_CODE_SIGNATURE (16 octets)
1d 00 00 00 10 00 00 00
&signature               # dataoff uint32
&fin_signature>signature # datasize uint32
:fin_commandes
 
# ---------------------------------------------------------------------------
# __text : le code (23 instructions, 92 octets)
:code
# Prologue : réserve 4096 octets sur la pile pour le buffer
ff 43 00 d1              # sub sp, sp, #0x1000
 
:boucle
# _read(0, sp, 4096)
00 00 80 d2              # mov x0, #0
e1 03 00 91              # mov x1, sp         (= add x1, sp, #0)
02 00 82 d2              # mov x2, #0x1000
08 00 00 10 %got_read    # adr x8, got_read
08 01 40 f9              # ldr x8, [x8]
00 01 3f d6              # blr x8
# si x0 == 0 (EOF), aller à fin
00 00 00 b4 @fin         # cbz x0, fin
 
# _write(1, sp, x0)
e2 03 00 aa              # mov x2, x0         (= orr x2, xzr, x0)
20 00 80 d2              # mov x0, #1
e1 03 00 91              # mov x1, sp
08 00 00 10 %got_write   # adr x8, got_write
08 01 40 f9              # ldr x8, [x8]
00 01 3f d6              # blr x8
 
# retour au début de la boucle
00 00 00 14 @boucle      # b boucle
 
:fin
# Épilogue : rend la pile
ff 43 00 91              # add sp, sp, #0x1000
# _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
00 00 20 d4              # brk #0
:fin_code
 
# ---------------------------------------------------------------------------
# Bourrage jusqu'à __DATA_CONST à 0x4000
!4000
:data_const_debut
 
# ---------------------------------------------------------------------------
# __got : trois entrées encodées dyld_chained_ptr_64_bind
# Format : ordinal(24) addend(8) reserved(19) next(12) bind(1)
# bind=1 => bit 63. next en strides de 4 octets.
# Entrée 0 (_read) : ordinal=0, next=2 (=8 octets jusqu'à l'entrée suivante), bind=1
#   val = (2 << 51) | (1 << 63) = 0x8010000000000000
:got_read
00 00 00 00 00 00 10 80
# Entrée 1 (_write) : ordinal=1, next=2, bind=1
#   val = 1 | (2 << 51) | (1 << 63) = 0x8010000000000001
:got_write
01 00 00 00 00 00 10 80
# Entrée 2 (_exit) : ordinal=2, next=0 (fin), bind=1
#   val = 2 | (1 << 63) = 0x8000000000000002
:got_exit
02 00 00 00 00 00 00 80
 
# ---------------------------------------------------------------------------
# Bourrage jusqu'à __LINKEDIT à 0x8000
!4000
:linkedit
 
# ---------------------------------------------------------------------------
# dyld_chained_fixups payload
:chained_fixups
00 00 00 00              # fixups_version 0
20 00 00 00              # starts_offset 32
50 00 00 00              # imports_offset 80
5c 00 00 00              # symbols_offset 92
03 00 00 00              # imports_count 3
01 00 00 00              # imports_format DYLD_CHAINED_IMPORT
00 00 00 00              # symbols_format 0
 
# padding
00 00 00 00
 
# dyld_chained_starts_in_image
04 00 00 00              # seg_count 4
00 00 00 00              # seg_info_offset[0] PAGEZERO 0
00 00 00 00              # seg_info_offset[1] TEXT 0
18 00 00 00              # seg_info_offset[2] DATA_CONST 24
00 00 00 00              # seg_info_offset[3] LINKEDIT 0
 
# padding pour starts_in_segment à offset 56
00 00 00 00
 
# dyld_chained_starts_in_segment (24 octets)
18 00 00 00              # size 24
00 40                    # page_size 0x4000
02 00                    # pointer_format 2 (DYLD_CHAINED_PTR_64)
00 40 00 00 00 00 00 00  # segment_offset 0x4000
00 00 00 00              # max_valid_pointer 0
01 00                    # page_count 1
00 00                    # page_start[0] 0
 
# imports (offset 80) : 3 entrées de 4 octets
# _read : lib=1, name=1  -> val = (1<<9)|1 = 0x201
01 02 00 00
# _write : lib=1, name=7 -> val = (7<<9)|1 = 0xE01
01 0e 00 00
# _exit : lib=1, name=14 -> val = (14<<9)|1 = 0x1C01
01 1c 00 00
 
# symbols (offset 92) : "\0_read\0_write\0_exit\0" (20 octets)
:symbols_chained
00
5f 72 65 61 64 00        # _read\0  (positions 1..6)
5f 77 72 69 74 65 00     # _write\0 (positions 7..13)
5f 65 78 69 74 00        # _exit\0  (positions 14..19)
:fin_chained
 
# padding à 8 (déjà aligné : 112 = 8*14)
 
# ---------------------------------------------------------------------------
# LC_SYMTAB : 3 nlist_64 de 16 octets, puis strtab
:symtab
01 00 00 00              # n_strx 1 (_read)
01 00 00 01
00 00 00 00 00 00 00 00
07 00 00 00              # n_strx 7 (_write)
01 00 00 01
00 00 00 00 00 00 00 00
0e 00 00 00              # n_strx 14 (_exit)
01 00 00 01
00 00 00 00 00 00 00 00
:strtab
00
5f 72 65 61 64 00
5f 77 72 69 74 65 00
5f 65 78 69 74 00
:fin_strtab
 
# padding à 16 pour la signature
!10
:signature
 
# ---------------------------------------------------------------------------
# Signature ad hoc. codeLimit = &signature. 9 pages de 4 Kio (dernière partielle).
# Taille : 20 (SuperBlob+index) + 88 (CD header) + 4 (ident "cat\0") + 9*32 = 400.
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 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)
00 00 00 58              # identOffset 88
00 00 00 00              # nSpecialSlots 0
00 00 00 09              # nCodeSlots 9
00 00 80 c0              # codeLimit (gros-boutiste, 9 pages couvrent 0x80c0)
20 02 00 0c              # hashSize 32, hashType SHA-256, 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
00 00 00 00 00 00 40 00  # execSegLimit 0x4000
00 00 00 00 00 00 00 01  # execSegFlags MAIN_BINARY
# ident "cat\0" (4 octets)
63 61 74 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
 
!10
:fin_fichier