Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chapters/hardware-software-interface: Add content for linking #26

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# 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.

If you're having difficulties solving this exercise, go through [this](../../../reading/README.md) reading material.
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

Check failure on line 24 in chapters/hardware-software-interface/linking/drills/tasks/entry-fix-1/solution/c-extra-nolibc/Makefile

View workflow job for this annotation

GitHub Actions / Checkpatch

WARNING:REPEATED_WORD: Possible repeated word: 'hello'
-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

Check failure on line 21 in chapters/hardware-software-interface/linking/drills/tasks/entry-fix-1/solution/d-extra-libc/Makefile

View workflow job for this annotation

GitHub Actions / Checkpatch

WARNING:REPEATED_WORD: Possible repeated word: 'hello'
-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

Check failure on line 16 in chapters/hardware-software-interface/linking/drills/tasks/entry-fix-1/support/a-c/Makefile

View workflow job for this annotation

GitHub Actions / Checkpatch

WARNING:REPEATED_WORD: Possible repeated word: 'hello'
-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

Check failure on line 19 in chapters/hardware-software-interface/linking/drills/tasks/entry-fix-1/support/b-asm/Makefile

View workflow job for this annotation

GitHub Actions / Checkpatch

WARNING:REPEATED_WORD: Possible repeated word: 'hello'
-rm -f *~
Loading
Loading