Skip to content

Commit

Permalink
chapters/hardware-software-interface: Add content for linking
Browse files Browse the repository at this point in the history
Add Reading and Tasks.
Modify config.yaml to render the laboratory.

Signed-off-by: Victor Alexe <[email protected]>
  • Loading branch information
victor1alexe committed Sep 30, 2024
1 parent 3dfe9fd commit 01ad898
Show file tree
Hide file tree
Showing 227 changed files with 2,939 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Fixing the Entry Point 1

Access the directory `drills/tasks/entry-fix-1/support/`.
We want to track issues with defining the `main()` function.

Go to the subdirectory `a-c/`.
Run the `make` command, interpret the encountered error, and resolve it by editing the `hello.c` file.

Go to the subdirectory `b-asm/`.
Run the `make` command, interpret the encountered error, and resolve it by editing the `hello.asm` file.

**Bonus**: In the subdirectories `c-extra-nolibc/` and `d-extra-libc/`, find solutions that do not modify the source code of `hello.c`.
These solutions instead modify the build system to use a different function, other than `main()`, as the program's entry point.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/hello
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-License-Identifier: BSD-3-Clause

#include <stdio.h>

static void hi(void)
{
puts("Hi!");
}

static void bye(void)
{
puts("Bye!");
}

int main(void)
{
hi();
bye();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/hello
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
; SPDX-License-Identifier: BSD-3-Clause

extern puts
extern main

section .rodata

hi_str db "Hi!", 0
bye_str db "Bye!", 0

section .text

hi:
push ebp
mov ebp, esp

push hi_str
call puts

leave
ret

bye:
push ebp
mov ebp, esp

push bye_str
call puts

leave
ret

main:
push ebp
mov ebp, esp

call hi
call bye

leave
ret
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/hello
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
CFLAGS = -fno-PIC -m32 -g
LDFLAGS = -nostdlib -nostdinc -no-pie -m32
AS = nasm
ASFLAGS = -f elf32

.DEFAULT_GOAL: all

.PHONY: all clean

all: hello

hello: hello.o start.o puts.o
$(CC) $(LDFLAGS) -o $@ $^

hello.o: hello.c

start.o: start.asm
$(AS) $(ASFLAGS) -o $@ $<

puts.o: puts.asm
$(AS) $(ASFLAGS) -o $@ $<

clean:
-rm -f hello hello.o start.o puts.o
-rm -f *~
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-License-Identifier: BSD-3-Clause

#include <stdio.h>

static void hi(void)
{
puts("Hi!\n");
}

static void bye(void)
{
puts("Bye!\n");
}

int my_main(void)
{
hi();
bye();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
; SPDX-License-Identifier: BSD-3-Clause

section .text

global puts

; /usr/include/x86_64-linux-gnu/asm/unistd_32.h
__NR_write equ 4

; Argument is message to be printed to standard output.
puts:
push ebp
mov ebp, esp

; Call __NR_write(1, message, message_len) (system call).
;
; Use x86 Linux system call convention.
; https://en.wikibooks.org/wiki/X86_Assembly/Interfacing_with_Linux#Making_a_system_call
;
; eax stores the system call id.
; ebx stores the first system call argument: 1 (standard output).
; ecx stores the second system call argument: message.
; edc stores the third system call argument: message length.

; Store the write system call id in eax.
mov eax, __NR_write

; Store standard output file descriptor (1) in ebx.
mov ebx, 1

; Store function argument (message) in ecx.
mov ecx, [ebp + 8]

; Compute message length in edx.
; Find NUL byte address of message using edi. Start from message address (ecx).
; Then edx <- edi - ecx.
mov edi, ecx
dec edi
again:
inc edi
cmp byte [edi], 0
jne again

mov edx, edi
sub edx, ecx

; Do system call.
int 0x80

leave
ret
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
; SPDX-License-Identifier: BSD-3-Clause

extern my_main

section .text

; /usr/include/x86_64-linux-gnu/asm/unistd_32.h
__NR_exit equ 1

global _start

_start:
call my_main

; Call __NR_exit(main_return_value) (system call).
;
; Use x86 Linux system call convention.
; https://en.wikibooks.org/wiki/X86_Assembly/Interfacing_with_Linux#Making_a_system_call
;
; ebx stores the first system call argument.
; eax stores the system call id.

; eax is main return value. Store it in ebx.
mov ebx, eax

; Store the exit system call id in rax.
mov eax, __NR_exit

; Do system call.
int 0x80
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/hello
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
CFLAGS = -fno-PIC -m32 -g
LDFLAGS = -no-pie -m32
AS = nasm
ASFLAGS = -f elf32

.DEFAULT_GOAL: all

.PHONY: all clean

all: hello

hello: hello.o start.o
ld -static -m elf_i386 -L/usr/lib/gcc/x86_64-linux-gnu/7/32/ -o $@ $^ /usr/lib32/crti.o --start-group -lc -lgcc -lgcc_eh --end-group /usr/lib32/crtn.o

hello.o: hello.c

start.o: start.asm
$(AS) $(ASFLAGS) -o $@ $<

clean:
-rm -f hello hello.o start.o
-rm -f *~
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-License-Identifier: BSD-3-Clause

#include <stdio.h>

static void hi(void)
{
puts("Hi!");
}

static void bye(void)
{
puts("Bye!");
}

int my_main(void)
{
hi();
bye();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
; SPDX-License-Identifier: BSD-3-Clause

extern __libc_csu_init
extern __libc_csu_fini
extern __libc_start_main
extern my_main

section .text

global _start
global _dl_relocate_static_pie

; Adapted from GNU Libc i386 start.S
; https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/i386/start.S;hb=HEAD
_start:
; Align stack.
xor ebp, ebp
pop esi
mov ecx, esp
and esp, 0xfffffff0

push eax
push esp
push edx

push __libc_csu_fini
push __libc_csu_init
push ecx
push esi
push my_main
call __libc_start_main

hlt

_dl_relocate_static_pie:
repz ret
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/hello
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
CFLAGS = -fno-PIC -m32 -g
LDFLAGS = -no-pie -m32

.DEFAULT_GOAL: all

.PHONY: all clean

all: hello

hello: hello.o
$(CC) $(LDFLAGS) -o $@ $^

hello.o: hello.c

clean:
-rm -f hello hello.o
-rm -f *~
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-License-Identifier: BSD-3-Clause

#include <stdio.h>

static void hi(void)
{
puts("Hi!");
}

static void bye(void)
{
puts("Bye!");
}

int my_main(void)
{
hi();
bye();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/hello
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
CFLAGS = -fno-PIC -m32 -g
LDFLAGS = -no-pie -m32
AS = nasm
ASFLAGS = -f elf32

.DEFAULT_GOAL: all

.PHONY: all clean

all: hello

hello: hello.o
$(CC) $(LDFLAGS) -o $@ $^

hello.o: hello.asm
$(AS) $(ASFLAGS) -o $@ $<

clean:
-rm -f hello hello.o
-rm -f *~
Loading

0 comments on commit 01ad898

Please sign in to comment.