forked from open-education-hub/hardware-software-interface
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chapters/hardware-software-interface: Add content for linking
Add Reading and Tasks. Modify config.yaml to render the laboratory. Signed-off-by: Victor Alexe <[email protected]>
- Loading branch information
1 parent
3dfe9fd
commit 5146107
Showing
233 changed files
with
3,020 additions
and
0 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
chapters/hardware-software-interface/linking/drills/tasks/entry-fix-1/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
1 change: 1 addition & 0 deletions
1
...ters/hardware-software-interface/linking/drills/tasks/entry-fix-1/solution/a-c/.gitignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/hello |
1 change: 1 addition & 0 deletions
1
chapters/hardware-software-interface/linking/drills/tasks/entry-fix-1/solution/a-c/Makefile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../support/a-c/Makefile |
19 changes: 19 additions & 0 deletions
19
chapters/hardware-software-interface/linking/drills/tasks/entry-fix-1/solution/a-c/hello.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
1 change: 1 addition & 0 deletions
1
...rs/hardware-software-interface/linking/drills/tasks/entry-fix-1/solution/b-asm/.gitignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/hello |
1 change: 1 addition & 0 deletions
1
...ters/hardware-software-interface/linking/drills/tasks/entry-fix-1/solution/b-asm/Makefile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../support/b-asm/Makefile |
41 changes: 41 additions & 0 deletions
41
...ers/hardware-software-interface/linking/drills/tasks/entry-fix-1/solution/b-asm/hello.asm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
1 change: 1 addition & 0 deletions
1
...re-software-interface/linking/drills/tasks/entry-fix-1/solution/c-extra-nolibc/.gitignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/hello |
25 changes: 25 additions & 0 deletions
25
...ware-software-interface/linking/drills/tasks/entry-fix-1/solution/c-extra-nolibc/Makefile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 *~ |
19 changes: 19 additions & 0 deletions
19
...dware-software-interface/linking/drills/tasks/entry-fix-1/solution/c-extra-nolibc/hello.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
51 changes: 51 additions & 0 deletions
51
...ware-software-interface/linking/drills/tasks/entry-fix-1/solution/c-extra-nolibc/puts.asm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
30 changes: 30 additions & 0 deletions
30
...are-software-interface/linking/drills/tasks/entry-fix-1/solution/c-extra-nolibc/start.asm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
1 change: 1 addition & 0 deletions
1
...ware-software-interface/linking/drills/tasks/entry-fix-1/solution/d-extra-libc/.gitignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/hello |
22 changes: 22 additions & 0 deletions
22
...rdware-software-interface/linking/drills/tasks/entry-fix-1/solution/d-extra-libc/Makefile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 *~ |
19 changes: 19 additions & 0 deletions
19
...ardware-software-interface/linking/drills/tasks/entry-fix-1/solution/d-extra-libc/hello.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
36 changes: 36 additions & 0 deletions
36
...dware-software-interface/linking/drills/tasks/entry-fix-1/solution/d-extra-libc/start.asm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
1 change: 1 addition & 0 deletions
1
chapters/hardware-software-interface/linking/drills/tasks/entry-fix-1/support/a-c/.gitignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/hello |
17 changes: 17 additions & 0 deletions
17
chapters/hardware-software-interface/linking/drills/tasks/entry-fix-1/support/a-c/Makefile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 *~ |
19 changes: 19 additions & 0 deletions
19
chapters/hardware-software-interface/linking/drills/tasks/entry-fix-1/support/a-c/hello.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
1 change: 1 addition & 0 deletions
1
...ers/hardware-software-interface/linking/drills/tasks/entry-fix-1/support/b-asm/.gitignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/hello |
20 changes: 20 additions & 0 deletions
20
chapters/hardware-software-interface/linking/drills/tasks/entry-fix-1/support/b-asm/Makefile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 *~ |
Oops, something went wrong.