Skip to content

Commit

Permalink
test util library extracted to global tinylibc library
Browse files Browse the repository at this point in the history
  • Loading branch information
mmamayka committed Aug 18, 2023
1 parent 68e8cd3 commit 95f1614
Show file tree
Hide file tree
Showing 16 changed files with 56 additions and 29 deletions.
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ project(dynamic-linker LANGUAGES C ASM)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)



add_subdirectory(lib)



set(DYNAMIC_LINKER_SO ld-dynamic.so)

set(DYNAMIC_LINKER_CFLAGS -O2 -Wall -Wextra -g -fno-stack-protector
Expand Down Expand Up @@ -35,6 +41,8 @@ target_compile_options(${DYNAMIC_LINKER_SO} PRIVATE
$<$<COMPILE_LANGUAGE:ASM>:${DYNAMIC_LINKER_ASMFLAGS}>
)

target_link_libraries(${DYNAMIC_LINKER_SO} PRIVATE tinylibc)

target_link_options(${DYNAMIC_LINKER_SO} PRIVATE ${DYNAMIC_LINKER_LDFLAGS})

target_include_directories(${DYNAMIC_LINKER_SO}
Expand Down
3 changes: 3 additions & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
cmake_minimum_required(VERSION 3.22.1)

add_subdirectory(tinylibc)
17 changes: 17 additions & 0 deletions lib/tinylibc/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
cmake_minimum_required(VERSION 3.22.1)

set(TINYLIBC_CFLAGS -O2 -Wall -Wextra -g -fno-stack-protector)
set(TINYLIBC_ASFLAGS -x assembler-with-cpp ${TINYLIBC_CFLAGS})

add_library(tinylibc STATIC)

target_compile_options(tinylibc PRIVATE
$<$<COMPILE_LANGUAGE:C>:${TINYLIBC_CFLAGS}>
$<$<COMPILE_LANGUAGE:ASM>:${TINYLIBC_ASFLAGS}>
)

target_include_directories(tinylibc PUBLIC include)

file(GLOB TINYLIBC_SRC "./*.[cs]")

target_sources(tinylibc PRIVATE ${TINYLIBC_SRC})
8 changes: 1 addition & 7 deletions src/asmutil.s → lib/tinylibc/_getchar.s
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@


#include <asm/unistd.h>
#include <syscall.h>

.section .text, "ax", @progbits

.global exit
exit:
mov $1, %rdi
syscall

.global _putchar
_putchar:
push %rdi
Expand Down
2 changes: 1 addition & 1 deletion t/util/exit.s → lib/tinylibc/exit.s
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

#include <syscall.h>
#include <asm/unistd.h>

.section .text, "ax", @progbits

Expand Down
1 change: 1 addition & 0 deletions lib/tinylibc/include/printf.h
18 changes: 18 additions & 0 deletions lib/tinylibc/include/tinylibc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef MY1STLD_LIB_TINYLIBC_H
#define MY1STLD_LIB_TINYLIBC_H

#include "printf.h"

void exit(int exit_code);

#define ASSERT_EXIT_CODE 666
#define assert(condition) \
do { \
if(!condition) { \
printf("assetion %s failed at %s:%i\n", \
#condition, __FILE__, __LINE__); \
exit(ASSERT_EXIT_CODE); \
} \
} while(0) \

#endif /* MY1STLD_LIB_TINYLIBC_H */
1 change: 1 addition & 0 deletions lib/tinylibc/printf.c
2 changes: 1 addition & 1 deletion src/entry.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <linux/auxvec.h>
#include <linux/elf.h>

#include "printf.h"
#include "tinylibc.h"

#ifndef NDEBUG
# define DEBUG_LOG(...) \
Expand Down
1 change: 0 additions & 1 deletion src/include/printf.h

This file was deleted.

1 change: 0 additions & 1 deletion src/printf.c

This file was deleted.

7 changes: 0 additions & 7 deletions t/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@ set(TEST_LDFLAGS -nostartfiles -nodefaultlibs
)
set(TEST_ASMFLAGS -x assembler-with-cpp)

add_library(test_util STATIC)
target_compile_options(test_util PRIVATE
$<$<COMPILE_LANGUAGE:C>:${TEST_CFLAGS}>
$<$<COMPILE_LANGUAGE:ASM>:${TEST_ASMFLAGS}>
)
target_sources(test_util PRIVATE util/exit.s)

# startup test

add_executable(startup_test)
Expand Down
4 changes: 2 additions & 2 deletions t/got_linking/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ target_sources(got_linking_lib PRIVATE lib.c)

add_executable(got_linking_test)

target_include_directories(got_linking_test PRIVATE ../util/include)
#target_include_directories(got_linking_test PRIVATE ../../lib/tinylibc/include)

target_compile_options(got_linking_test PRIVATE
$<$<COMPILE_LANGUAGE:C>:${TEST_CFLAGS}>
Expand All @@ -28,7 +28,7 @@ target_link_options(got_linking_test PRIVATE
${TEST_LDFLAGS}
)

target_link_libraries(got_linking_test PRIVATE got_linking_lib test_util)
target_link_libraries(got_linking_test PRIVATE got_linking_lib tinylibc)

target_sources(got_linking_test PRIVATE startup.c)

Expand Down
2 changes: 1 addition & 1 deletion t/got_linking/lib.c
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@

int so_variable = 0;
int so_variable = 1;
4 changes: 2 additions & 2 deletions t/got_linking/startup.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "util.h"
#include "lib.h"
#include "tinylibc.h"

void _start() {
so_variable++;
assert(so_variable == 1);
exit(0);
}
6 changes: 0 additions & 6 deletions t/util/include/util.h

This file was deleted.

0 comments on commit 95f1614

Please sign in to comment.