1 # This function merges multiple objects into a single relocatable object
2 # cc -r obj1.o obj2.o -o obj.o
3 # A relocatable object is an object file that is not fully linked into an
4 # executable or a shared library. It is an intermediate file format that can
5 # be passed into the linker.
6 # A crt object has arch-specific code and arch-agnostic code. To reduce code
7 # duplication, the implementation is split into multiple units. As a result,
8 # we need to merge them into a single relocatable object.
9 # See also: https://maskray.me/blog/2022-11-21-relocatable-linking
10 function(merge_relocatable_object name)
12 set(fq_link_libraries "")
13 get_fq_deps_list(fq_dep_list ${ARGN})
14 foreach(target IN LISTS fq_dep_list)
15 list(APPEND obj_list "$<TARGET_OBJECTS:${target}>")
16 get_target_property(libs ${target} DEPS)
17 list(APPEND fq_link_libraries "${libs}")
19 list(REMOVE_DUPLICATES obj_list)
20 list(REMOVE_DUPLICATES fq_link_libraries)
21 get_fq_target_name(${name} fq_name)
22 set(relocatable_target "${fq_name}.__relocatable__")
27 # Pass -r to the driver is much cleaner than passing -Wl,-r: the compiler knows it is
28 # a relocatable linking and will not pass other irrelevant flags to the linker.
29 target_link_options(${relocatable_target} PRIVATE -r -nostdlib)
30 set_target_properties(
33 RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
36 add_library(${fq_name} OBJECT IMPORTED GLOBAL)
37 add_dependencies(${fq_name} ${relocatable_target})
38 target_link_libraries(${fq_name} INTERFACE ${fq_link_libraries})
39 set_target_properties(
43 IMPORTED_OBJECTS ${CMAKE_CURRENT_BINARY_DIR}/${name}.o
44 TARGET_TYPE ${OBJECT_LIBRARY_TARGET_TYPE}
45 DEPS "${fq_link_libraries}"
49 function(add_startup_object name)
50 cmake_parse_arguments(
53 "SRC" # Single value arguments
54 "DEPENDS;COMPILE_OPTIONS" # Multi value arguments
58 get_fq_target_name(${name} fq_target_name)
62 SRCS ${ADD_STARTUP_OBJECT_SRC}
63 DEPENDS ${ADD_STARTUP_OBJECT_DEPENDS}
64 COMPILE_OPTIONS ${ADD_STARTUP_OBJECT_COMPILE_OPTIONS}
66 set_target_properties(
73 check_cxx_compiler_flag("-r" LIBC_LINKER_SUPPORTS_RELOCATABLE)
75 if(NOT LIBC_LINKER_SUPPORTS_RELOCATABLE)
76 message(STATUS "Skipping startup for target architecture ${LIBC_TARGET_ARCHITECTURE}: linker does not support -r")
80 if(NOT (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_ARCHITECTURE}))
81 message(STATUS "Skipping startup for target architecture ${LIBC_TARGET_ARCHITECTURE}")
85 add_subdirectory(${LIBC_TARGET_ARCHITECTURE})
94 libc.config.linux.app_h
96 libc.include.sys_syscall
97 libc.src.__support.threads.thread
98 libc.src.__support.OSUtil.osutil
100 libc.src.stdlib.atexit
101 libc.src.unistd.environ
103 -ffreestanding # To avoid compiler warnings about calling the main function.
104 -fno-builtin # avoid emit unexpected calls
105 -fno-stack-protector # stack protect canary is not available yet.
108 # TODO: factor out crt1 into multiple objects
109 merge_relocatable_object(
111 .${LIBC_TARGET_ARCHITECTURE}.start
112 .${LIBC_TARGET_ARCHITECTURE}.tls
128 add_custom_target(libc-startup)
129 set(startup_components crt1 crti crtn)
130 foreach(target IN LISTS startup_components)
131 set(fq_target_name libc.startup.linux.${target})
132 add_dependencies(libc-startup ${fq_target_name})
133 install(FILES $<TARGET_OBJECTS:${fq_target_name}>
134 DESTINATION ${CMAKE_INSTALL_LIBDIR}
135 RENAME $<TARGET_PROPERTY:${fq_target_name},OUTPUT_NAME>