boot2

Playing with the boostrap
git clone https://git.ryansepassi.com/git/boot2.git
Log | Files | Refs | README

catm.hex2 (5468B)


      1 # SPDX-FileCopyrightText: 2019 Jeremiah Orians <jeremiah@pdp10.guru>
      2 #
      3 # SPDX-License-Identifier: GPL-3.0-or-later
      4 
      5 ## ELF Header
      6 
      7 :ELF_base
      8 7F 45 4C 46        ## e_ident[EI_MAG0-3] ELF's magic number
      9 
     10 02                 ## e_ident[EI_CLASS] Indicating 64 bit
     11 01                 ## e_ident[EI_DATA] Indicating little endianness
     12 01                 ## e_ident[EI_VERSION] Indicating original elf
     13 
     14 03                 ## e_ident[EI_OSABI] Set at 3 because FreeBSD is strict
     15 00                 ## e_ident[EI_ABIVERSION] Set at 0 because none cares
     16 
     17 00 00 00 00 00 00 00 ## e_ident[EI_PAD]
     18 02 00              ## e_type Indicating Executable
     19 3E 00              ## e_machine Indicating AMD64
     20 01 00 00 00        ## e_version Indicating original elf
     21 
     22 &_start 00 00 00 00 ## e_entry Address of the entry point (Number of bytes this header is + Base Address)
     23 %ELF_program_headers>ELF_base 00 00 00 00 ## e_phoff Address of program header table
     24 00 00 00 00 00 00 00 00 ## e_shoff Address of section header table
     25 
     26 00 00 00 00        ## e_flags
     27 40 00              ## e_ehsize Indicating our 64 Byte header
     28 
     29 38 00              ## e_phentsize size of a program header table
     30 01 00              ## e_phnum number of entries in program table
     31 
     32 00 00              ## e_shentsize size of a section header table
     33 00 00              ## e_shnum number of entries in section table
     34 
     35 00 00              ## e_shstrndx index of the section names
     36 
     37 ## Program Header
     38 :ELF_program_headers
     39 01 00 00 00             ## p_type
     40 07 00 00 00             ## ph_flags: PF-X|PF-W|PF-R = 7
     41 00 00 00 00 00 00 00 00 ## p_offset
     42 
     43 &ELF_base 00 00 00 00 ## p_vaddr
     44 &ELF_base 00 00 00 00 ## p_physaddr
     45 
     46 %ELF_end>ELF_base 00 00 00 00 ## p_filesz
     47 %ELF_end>ELF_base 00 00 00 00 ## p_memsz
     48 
     49 01 00 00 00 00 00 00 00 ## Required alignment
     50 
     51 :ELF_text
     52 
     53 :_start
     54 	58                          ; pop_rax                     # Get the number of arguments
     55 	5F                          ; pop_rdi                     # Get the program name
     56 	5F                          ; pop_rdi                     # Get the actual output name
     57 	48C7C6 41020000             ; mov_rsi, %577               # Prepare file as O_WRONLY|O_CREAT|O_TRUNC
     58 	48C7C2 80010000             ; mov_rdx, %384               # Prepare file as RW for owner only (600 in octal)
     59 	48C7C0 02000000             ; mov_rax, %2                 # the syscall number for open()
     60 	0F05                        ; syscall                     # Now open that file
     61 	4989C7                      ; mov_r15,rax                 # Preserve the file pointer we were given
     62 
     63 	48C7C0 0C000000             ; mov_rax, %12                # the Syscall # for SYS_BRK
     64 	48C7C7 00000000             ; mov_rdi, %0                 # Get current brk
     65 	0F05                        ; syscall                     # Let the kernel do the work
     66 	4989C6                      ; mov_r14,rax                 # Set our malloc pointer
     67 
     68 	48C7C0 0C000000             ; mov_rax, %12                # the Syscall # for SYS_BRK
     69 	4C89F7                      ; mov_r14,rax                 # Using current pointer
     70 	4881C7 00001000             ; add_rdi, %0x100000          # Allocate 1MB
     71 	0F05                        ; syscall                     # Let the kernel do the work
     72 
     73 :core
     74 	5F                          ; pop_rdi                     # Get the actual input name
     75 	4883FF 00                   ; cmp_rdi, !0                 # Check for null string
     76 	0F84 %done                  ; je %done                    # Hit null be done
     77 
     78 	48C7C6 00000000             ; mov_rsi, %0                 # prepare read_only
     79 	48C7C2 00000000             ; mov_rdx, %0                 # prevent any interactions
     80 	48C7C0 02000000             ; mov_rax, %2                 # the syscall number for open()
     81 	0F05                        ; syscall                     # Now open that damn file
     82 	4989C5                      ; mov_r13,rax                 # Protect INPUT
     83 :keep
     84 	48C7C2 00001000             ; mov_rdx, %0x100000          # set the size of chars we want
     85 	4C89F6                      ; mov_rsi,r14                 # Where to put it
     86 	4C89EF                      ; mov_rdi,r13                 # Where are we reading from
     87 	48C7C0 00000000             ; mov_rax, %0                 # the syscall number for read
     88 	0F05                        ; syscall                     # call the Kernel
     89 	50                          ; push_rax                    # Protect the number of bytes read
     90 
     91 	4889C2                      ; mov_rdx,rax                 # Number of bytes to write
     92 	4C89F6                      ; mov_rsi,r14                 # What we are writing
     93 	4C89FF                      ; mov_rdi,r15                 # Write to target file
     94 	48C7C0 01000000             ; mov_rax, %1                 # the syscall number for write
     95 	0F05                        ; syscall                     # call the Kernel
     96 
     97 	58                          ; pop_rax                     # Get bytes read
     98 	483D 00001000               ; cmp_rax, %0x100000          # Check if buffer was fully used
     99 	0F84 %keep                  ; je %keep                    # Keep looping if was full
    100 	E9 %core                    ; jmp %core                   # Otherwise move to next file
    101 
    102 :done
    103 	# program completed Successfully
    104 	48C7C7 00000000             ; mov_rdi, %0                 # All is well
    105 	48C7C0 3C000000             ; mov_rax, %0x3C              # put the exit syscall number in eax
    106 	0F05                        ; syscall                     # Call it a good day
    107 
    108 :ELF_end