Home | History | Annotate | Line # | Download | only in hello
      1  1.1  ryo /*	$NetBSD: x86_64.s,v 1.1 2022/10/14 19:41:18 ryo Exp $	*/
      2  1.1  ryo 
      3  1.1  ryo /*-
      4  1.1  ryo  * Copyright (c) 2022 The NetBSD Foundation, Inc.
      5  1.1  ryo  * All rights reserved.
      6  1.1  ryo  *
      7  1.1  ryo  * Redistribution and use in source and binary forms, with or without
      8  1.1  ryo  * modification, are permitted provided that the following conditions
      9  1.1  ryo  * are met:
     10  1.1  ryo  * 1. Redistributions of source code must retain the above copyright
     11  1.1  ryo  *    notice, this list of conditions and the following disclaimer.
     12  1.1  ryo  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  ryo  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  ryo  *    documentation and/or other materials provided with the distribution.
     15  1.1  ryo  *
     16  1.1  ryo  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.1  ryo  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1  ryo  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1  ryo  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1  ryo  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1  ryo  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1  ryo  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1  ryo  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1  ryo  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1  ryo  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1  ryo  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1  ryo  */
     28  1.1  ryo 
     29  1.1  ryo /*
     30  1.1  ryo  * assemble & link:
     31  1.1  ryo  *  as -o x86_64.o x86_64.s
     32  1.1  ryo  *  ld -o hello -e _start x86_64.o
     33  1.1  ryo  */
     34  1.1  ryo 
     35  1.1  ryo 	/* ------------------------------------------------------------------ */
     36  1.1  ryo 
     37  1.1  ryo 	/*
     38  1.1  ryo 	 * This ELF section is used by the kernel to determine, among other
     39  1.1  ryo 	 * things, the system call interface used by the binary.
     40  1.1  ryo 	 *
     41  1.1  ryo 	 * Normally, /usr/lib/crti.o is linked, but here it is written manually.
     42  1.1  ryo 	 *
     43  1.1  ryo 	 * SEE ALSO:
     44  1.1  ryo 	 * - http://www.netbsd.org/docs/kernel/elf-notes.html
     45  1.1  ryo 	 * - src/sys/sys/exec_elf.h
     46  1.1  ryo 	 * - src/lib/csu/common/sysident.S
     47  1.1  ryo 	 * - src/lib/csu/arch/x86_64/crti.S
     48  1.1  ryo 	 */
     49  1.1  ryo 	.section ".note.netbsd.ident", "a"
     50  1.1  ryo 	.p2align 2
     51  1.1  ryo 	.long	7		/* ELF_NOTE_NETBSD_NAMESZ */
     52  1.1  ryo 	.long	4		/* ELF_NOTE_NETBSD_DESCSZ */
     53  1.1  ryo 	.long	1		/* ELF_NOTE_TYPE_NETBSD_TAG */
     54  1.1  ryo 	.ascii	"NetBSD\0\0"	/* ELF_NOTE_NETBSD_NAME */
     55  1.1  ryo 	.long	999010000	/* __NetBSD_Version__ (sys/sys/param.h) */
     56  1.1  ryo 
     57  1.1  ryo 
     58  1.1  ryo 	/* ------------------------------------------------------------------ */
     59  1.1  ryo 
     60  1.1  ryo 	.section ".rodata"
     61  1.1  ryo message:
     62  1.1  ryo 	.ascii "Hello, world!\n"
     63  1.1  ryo 	.set MESSAGE_SIZE, . - message
     64  1.1  ryo 
     65  1.1  ryo 
     66  1.1  ryo 	/* ------------------------------------------------------------------ */
     67  1.1  ryo 
     68  1.1  ryo 	.section ".text"
     69  1.1  ryo 	.p2align 2
     70  1.1  ryo 
     71  1.1  ryo 	.global _start
     72  1.1  ryo 	.type _start, %function
     73  1.1  ryo _start:
     74  1.1  ryo 	/* write(STDOUT_FILENO, message, MESSAGE_SIZE) */
     75  1.1  ryo 	movl	$1, %edi		/* %edi: fd = STDOUT_FILENO */
     76  1.1  ryo 	leaq	message(%rip), %rsi	/* %rsi: buf = message */
     77  1.1  ryo 	movl	$MESSAGE_SIZE, %edx	/* %edx: nbytes = MESSAGE_SIZE */
     78  1.1  ryo 
     79  1.1  ryo 	movl	$4, %eax		/* SYS_write */
     80  1.1  ryo 	movq	%rcx, %r10
     81  1.1  ryo 	syscall
     82  1.1  ryo 
     83  1.1  ryo 	/* exit(0) */
     84  1.1  ryo 	xorl	%edi, %edi		/* %rdi: status = 0 */
     85  1.1  ryo 	mov	$1, %eax		/* SYS_exit */
     86  1.1  ryo 	movq	%rcx, %r10
     87  1.1  ryo 	syscall
     88  1.1  ryo 
     89  1.1  ryo 	.size _start, . - _start
     90