-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
75 lines (59 loc) · 1.95 KB
/
CMakeLists.txt
File metadata and controls
75 lines (59 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
cmake_minimum_required(VERSION 3.16)
project(toy_dynamic_linker)
# Set clang as the default compiler
set(CMAKE_C_COMPILER clang)
set(CMAKE_CXX_COMPILER clang++)
# Set output directories
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
# Create the toy_linker shared library
add_library(toy_linker SHARED src/toy_linker.c src/auxv.h src/util.h src/program_header.h src/dynamic_section.h src/library_types.h src/loader.h src/symbol.h src/relocation.h src/hash_table.h src/init_libs.h)
# Set include directories
target_include_directories(toy_linker PRIVATE src)
# Set the output name to toy_linker.so
set_target_properties(toy_linker PROPERTIES OUTPUT_NAME "toy_linker")
# Set compilation flags
target_compile_options(toy_linker PRIVATE
-nostdlib
-fPIC
-g
)
# Set linker flags
target_link_options(toy_linker PRIVATE
-nostdlib
-shared
-Wl,-e,_start,--no-undefined
)
# Create a simple shared library with no external dependencies
add_library(simple_lib SHARED example/simple_lib.c)
target_compile_options(simple_lib PRIVATE
-g
-fPIC
-fno-builtin
-nostdlib
-fno-stack-protector
)
target_link_options(simple_lib PRIVATE
-nostdlib
-shared
)
# Create a simpler C program that uses our simple_lib
add_executable(simple_program example/simple_program.c)
# Add debug information to simple_program and disable standard library
target_compile_options(simple_program PRIVATE
-g
-fno-builtin
-nostdlib
-fno-stack-protector
)
# Link against our simple_lib
target_link_libraries(simple_program PRIVATE simple_lib)
# Use our toy_linker as the dynamic linker
target_link_options(simple_program PRIVATE
-Wl,--dynamic-linker=${CMAKE_BINARY_DIR}/lib/libtoy_linker.so
-nostdlib
-nostartfiles
-nodefaultlibs
)
# Make sure simple_program depends on toy_linker and simple_lib so they're built first
add_dependencies(simple_program toy_linker simple_lib)