Skip to content

Farzy/fant-asm

Repository files navigation

Fant-asm

Learning x86 assembly and making stupid puns.

Note: Some texts have been copied from other sites, full references are provided at the end of this document.

Usage

Compiling

make all

Cleaning

make clean

Helper tool

There is a helper tool written in Rust in the helper directory.

In order to build it, install a Rust toolchain and run make helper.

Sample usage:

bin/helper help
Fant-asm helper 1.0
Farzad FARID <farzy@farzy.org>
Helper functions for learning assembly

USAGE:
    helper [SUBCOMMAND]

FLAGS:
    -h, --help       Prints help information
    -V, --version    Prints version information

SUBCOMMANDS:
    help       Prints this message or the help of the given subcommand(s)
    ieee754    Floating point conversion
bin/helper ieee754 0xC1440000
C1440000 = 11000001010001000000000000000000
Sign: 1, exponent: 3, fraction = 0b1.10001000000000000000000

Learnings

32 bits vs 64 bits

Most of the ASM samples I found so far were written for 32 bits systems. I replaced the registers with the 64 bits versions and it worked so far… For example eax becomes rax.

Be careful not to clobber memory by using a too big register when a 16 or 32 bits write is enough!

Linux vs macOS

  • The system call numbers are different!
  • For write and exit at least, the same arguments are passed in the same registers

The reason why they use the same register is because both OS on 64 bits architectures adopted the System V AMD64 ABI reference calling convention.

macOS version for the linker

Linking for macOS min version 10.7 needs the entry point to be named start and no dynamic library linking.

In order to compile & link for macOS min version 10.12 you need to:

  • Remplace the symbol start with _main
  • Link to the System dynamic library with -lSystem

Finding the XNU version corresponding to an OS X version

For example, for macOS 10.15.6 we have xnu-6153.141.1.

Finding system call tables

  • Find the XNU version
  • Navigate to the xnu source code
  • Find the file bsd/kern/syscalls.master

For example: https://opensource.apple.com/source/xnu/xnu-6153.141.1/bsd/kern/syscalls.master

System calls on X86-64

  • Arguments are passed on the registers rdi, rsi, rdx, r10, r8 and r9
  • Syscall number is in the rax register
  • The call is done via the syscall instruction

Relative versus absolute addressing

Most sample codes that I found use absolute memory addressing, apparently because it's how it works on 32 bits systems.

64 bits macOS uses relative memory addressing by default in order to activate PIC ("position-independent code"), also called PIE ("position-independent executable").

If the compiled .o file contains absolute adresssing, ld complains with the following message but compiles anyway:

ld: warning: PIE disabled. Absolute addressing (perhaps -mdynamic-no-pic) not allowed in code signed PIE, but used in _main from hello_macos.o. To fix this warning, don't compile with -mdynamic-no-pic or link with -Wl,-no_pie

In order to convert to PIE style addressing, add this line at the top of the source code:

        default   rel

Then convert these calls:

        mov       rsi, message            ; address of string to output

to:

        lea       rsi, [message]      ; address of string to output

Licence

Copyright 2020 Farzad FARID farzy@farzy.org

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

References

Starters

Full courses

Reference manuals