ld.conf 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. LDFLAGS+=$USER_LDFLAGS $_LD_FLAGS
  2. LDFLAGS_GLOBAL+=
  3. _START_GROUP=
  4. _END_GROUP=
  5. BEFORE_PEERS=
  6. AFTER_PEERS=
  7. START_WHOLE_ARCHIVE_VALUE=
  8. END_WHOLE_ARCHIVE_VALUE=
  9. LD_STRIP_FLAG=
  10. _SONAME_OPTION=
  11. _LD_SRCS_GLOBALS=--start-wa ${rootrel;ext=.a:SRCS_GLOBAL} --end-wa ${rootrel;ext=.o:SRCS_GLOBAL} ${rootrel;ext=.supp:SRCS_GLOBAL}
  12. when ($OS_LINUX == "yes" || $OS_ANDROID == "yes" || $OS_CYGWIN == "yes" || $OS_NONE == "yes") {
  13. _START_GROUP=-Wl,--start-group
  14. _END_GROUP=-Wl,--end-group
  15. START_WHOLE_ARCHIVE_VALUE=-Wl,--whole-archive
  16. END_WHOLE_ARCHIVE_VALUE=-Wl,--no-whole-archive
  17. LD_STRIP_FLAG=-s
  18. _SONAME_OPTION=-soname
  19. _LD_SRCS_GLOBALS=-Wl,--whole-archive --ya-start-command-file ${rootrel;ext=.a:SRCS_GLOBAL} --ya-end-command-file -Wl,--no-whole-archive ${rootrel;ext=.o:SRCS_GLOBAL} ${rootrel;ext=.supp:SRCS_GLOBAL}
  20. }
  21. when ($_LD_WHOLE_ARCHIVE == "yes") {
  22. }
  23. when ($OS_DARWIN == "yes" || $OS_IOS == "yes") {
  24. _SONAME_OPTION=-install_name
  25. }
  26. _LD_LINKER_OUTPUT=
  27. _LD_LINKER_OUTPUT_FLAG=--linker-output ${output;pre=$MODULE_PREFIX;suf=$MODULE_SUFFIX$MODULE_VERSION.linker.txt:REALPRJNAME}
  28. # Enable section-level DCE (dead code elimination):
  29. # remove whole unused code and data sections
  30. # (needs `-ffunction-sections` and `-fdata-sections` to be useful)
  31. #
  32. # NOTE: CGO linker doesn't seem to support DCE, but shares common LDFLAGS
  33. _LD_DCE_FLAG_GC_SECTIONS=
  34. _LD_DCE_FLAG_PRINT_SECTIONS=
  35. when ($OS_DARWIN == "yes" || $OS_IOS == "yes" || $OS_IOSSIM == "yes") {
  36. _LD_DCE_FLAG_GC_SECTIONS=-Wl,-dead_strip
  37. }
  38. elsewhen ($OS_LINUX == "yes" || $OS_ANDROID == "yes" || $OS_NONE == "yes") {
  39. _LD_DCE_FLAG_GC_SECTIONS=-Wl,--gc-sections
  40. when ($LINKER_DCE_PRINT_SECTIONS == "yes") {
  41. _LD_DCE_FLAG_PRINT_SECTIONS=-Wl,--print-gc-sections
  42. _LD_LINKER_OUTPUT=$_LD_LINKER_OUTPUT_FLAG
  43. }
  44. }
  45. LD_DCE_FLAG =
  46. LD_DCE_FLAG+=$_LD_DCE_FLAG_GC_SECTIONS
  47. LD_DCE_FLAG+=$_LD_DCE_FLAG_PRINT_SECTIONS
  48. DCE_FLAG=
  49. _LD_ICF_FLAG=
  50. _LD_ICF_FLAG_PRINT_SECTIONS=
  51. when ($_DEFAULT_LINKER_ID == "lld") {
  52. # Enable ICF (identical code folding pass) in safe mode
  53. # https://research.google/pubs/pub36912/
  54. _LD_ICF_FLAG=-Wl,--icf=safe
  55. when ($LINKER_ICF_PRINT_SECTIONS == "yes") {
  56. _LD_ICF_FLAG_PRINT_SECTIONS=-Wl,--print-icf-sections
  57. _LD_LINKER_OUTPUT=$_LD_LINKER_OUTPUT_FLAG
  58. }
  59. }
  60. LD_ICF_FLAG =
  61. LD_ICF_FLAG+=$_LD_ICF_FLAG
  62. LD_ICF_FLAG+=$_LD_ICF_FLAG_PRINT_SECTIONS
  63. ICF_FLAG=
  64. STRIP_FLAG=
  65. C_LIBRARY_PATH=
  66. C_SYSTEM_LIBRARIES_INTERCEPT=
  67. when ($USEMPROF == "yes" || $USE_MPROF == "yes") {
  68. C_SYSTEM_LIBRARIES_INTERCEPT+=-ldmalloc
  69. }
  70. _LD_THREAD_LIBRARY=
  71. when ($OS_LINUX == "yes" || $OS_DARWIN == "yes") {
  72. _LD_THREAD_LIBRARY=-lpthread
  73. }
  74. _LD_USE_STDLIB=
  75. when ($_BUILD_COVERAGE == "yes" || $GCOV_COVERAGE == "yes" || $CLANG_COVERAGE == "yes" || $_BUILD_SANITIZED == "yes") {
  76. _LD_USE_STDLIB=
  77. }
  78. elsewhen ($OS_ANDROID == "yes") {
  79. when ($_ANDROID_NDK_VERSION_LESS_THEN_23 == "yes") {
  80. # Preserve old behaviour: specify runtime libs manually
  81. _LD_USE_STDLIB=-nodefaultlibs
  82. }
  83. otherwise {
  84. # Use toolchain defaults to link with libunwind/clang_rt.builtins
  85. _LD_USE_STDLIB=-nostdlib++
  86. }
  87. }
  88. elsewhen ($OS_LINUX == "yes" || $OS_DARWIN == "yes" || $OS_IOS == "yes") {
  89. _LD_USE_STDLIB=-nodefaultlibs
  90. }
  91. _C_SYSTEM_LIBRARIES=$_LD_USE_STDLIB $_LD_THREAD_LIBRARY $_LD_SYS_LIB -lc
  92. when ($MUSL == "yes") {
  93. _C_SYSTEM_LIBRARIES=-nostdlib
  94. }
  95. when ($OS_EMSCRIPTEN == "yes") {
  96. _C_SYSTEM_LIBRARIES=-nostdlib
  97. }
  98. C_SYSTEM_LIBRARIES=$_C_SYSTEM_LIBRARIES
  99. OBJADDE=
  100. LD_EXPORT_ALL_DYNAMIC_SYMBOLS_FLAG=
  101. when ($OS_LINUX == "yes" || $OS_ANDROID == "yes") {
  102. LD_EXPORT_ALL_DYNAMIC_SYMBOLS_FLAG=-rdynamic
  103. }
  104. NO_EXPORT_DYNAMIC_SYMBOLS=
  105. EXPORTS_VALUE=$LD_EXPORT_ALL_DYNAMIC_SYMBOLS_FLAG
  106. when ($EXPORTS_FILE) {
  107. EXPORTS_VALUE=$LD_EXPORT_ALL_DYNAMIC_SYMBOLS_FLAG -Wl,--version-script=${input:EXPORTS_FILE}
  108. }
  109. when ($NO_EXPORT_DYNAMIC_SYMBOLS == "yes") {
  110. EXPORTS_VALUE=
  111. }
  112. LINKER_SCRIPT_VALUE=${ext=.ld;pre=-T:SRCS_GLOBAL}
  113. _LINKER_TIME_TRACE_GRANULARITY=500
  114. when ($TIME_TRACE_GRANULARITY && $TIME_TRACE_GRANULARITY != "") {
  115. _LINKER_TIME_TRACE_GRANULARITY=$TIME_TRACE_GRANULARITY
  116. }
  117. _LINKER_TIME_TRACE_FLAG=
  118. when (($TIME_TRACE == "yes" || $LINKER_TIME_TRACE == "yes") && $_DEFAULT_LINKER_ID == "lld") {
  119. _LINKER_TIME_TRACE_FLAG=\
  120. -Wl,--time-trace \
  121. -Wl,--time-trace-granularity=$_LINKER_TIME_TRACE_GRANULARITY \
  122. -Wl,--time-trace-file=${output;rootrel;pre=$MODULE_PREFIX;suf=$MODULE_SUFFIX.time_trace.json:REALPRJNAME}
  123. }
  124. _EXE_FLAGS=\
  125. $C_FLAGS_PLATFORM \
  126. $BEFORE_PEERS \
  127. $_START_GROUP \
  128. ${rootrel:PEERS} \
  129. $_END_GROUP \
  130. $AFTER_PEERS \
  131. $EXPORTS_VALUE \
  132. $LINKER_SCRIPT_VALUE \
  133. $LDFLAGS \
  134. $LDFLAGS_GLOBAL \
  135. $OBJADDE \
  136. $OBJADDE_LIB \
  137. $OBJADDE_LIB_GLOBAL \
  138. $C_LIBRARY_PATH \
  139. $C_SYSTEM_LIBRARIES_INTERCEPT \
  140. $C_SYSTEM_LIBRARIES \
  141. $STRIP_FLAG \
  142. $DCE_FLAG \
  143. $ICF_FLAG \
  144. $_LINKER_TIME_TRACE_FLAG
  145. _SONAME_FLAG=-Wl,$_SONAME_OPTION,$_SONAME
  146. when ($OS_EMSCRIPTEN == "yes") {
  147. _SONAME_FLAG=
  148. }
  149. _SHARED_FLAG=-shared
  150. when ($OS_EMSCRIPTEN == "yes") {
  151. _SHARED_FLAG=-Wl,-shared -Wl,--no-entry -Wl,--export-all -Wl,--import-memory -Wl,--import-undefined
  152. }
  153. _EXEC_SHARED_FLAG=
  154. when ($OS_LINUX == "yes") {
  155. _EXEC_SHARED_FLAG=-pie -fPIE -Wl,--unresolved-symbols=ignore-all -rdynamic
  156. }
  157. _LD_ENV_STYLE=${cwd:ARCADIA_BUILD_ROOT} $TOOLCHAIN_ENV ${hide;kv:"p LD"} ${hide;kv:"pc light-blue"} ${hide;kv:"show_out"}
  158. # Program
  159. GENERATE_MF_CMD=\
  160. $YMAKE_PYTHON ${input:"build/scripts/generate_mf.py"} ${input;hide:"build/scripts/process_command_files.py"} \
  161. --build-root $ARCADIA_BUILD_ROOT \
  162. --module-name $REALPRJNAME \
  163. -o ${output;rootrel;pre=$MODULE_PREFIX;suf=$MODULE_SUFFIX.mf:REALPRJNAME} \
  164. -t $MODULE_TYPE \
  165. --ya-start-command-file \
  166. -Ya,lics $LICENSE_NAMES \
  167. -Ya,peers ${rootrel:PEERS} \
  168. -Ya,credits ${input:CREDITS_TEXTS_FILE} $CREDITS_FLAGS \
  169. --ya-end-command-file
  170. LINK_SCRIPT_EXE_FLAGS=
  171. REAL_LINK_EXE_CMDLINE =\
  172. $YMAKE_PYTHON ${input:"build/scripts/link_exe.py"} ${hide;input:"build/scripts/fix_py2_protobuf.py"} \
  173. --source-root $ARCADIA_ROOT
  174. REAL_LINK_EXE_CMDLINE+=--clang-ver $CLANG_VER
  175. REAL_LINK_EXE_CMDLINE+=$_LD_LINKER_OUTPUT
  176. REAL_LINK_EXE_CMDLINE+=\
  177. ${pre=--whole-archive-peers :WHOLE_ARCHIVE_PEERS} \
  178. ${pre=--whole-archive-libs :_WHOLE_ARCHIVE_LIBS_VALUE_GLOBAL} \
  179. $_LD_ARCH_FLAG \
  180. $LINK_SCRIPT_EXE_FLAGS \
  181. $CXX_COMPILER_OLD \
  182. $_LD_SRCS_GLOBALS \
  183. $VCS_C_OBJ $_EXTRA_OBJS $AUTO_INPUT -o $TARGET \
  184. $_EXE_FLAGS \
  185. $_PROCESS_WHOLE_ARCHIVE_SCRIPT \
  186. $_LD_ENV_STYLE
  187. REAL_LINK_EXE_TIDY=\
  188. $YMAKE_PYTHON ${input:"build/scripts/clang_tidy_arch.py"} \
  189. --build-root $ARCADIA_BUILD_ROOT \
  190. --source-root $ARCADIA_ROOT \
  191. --output-file $TARGET \
  192. $AUTO_INPUT \
  193. $SRCS_GLOBAL \
  194. $_LD_ENV_STYLE
  195. # Executable Shared Library
  196. REAL_LINK_EXEC_DYN_LIB_CMDLINE =\
  197. $YMAKE_PYTHON ${input:"build/scripts/link_dyn_lib.py"} ${hide;input:"build/scripts/fix_py2_protobuf.py"}\
  198. --target $TARGET
  199. REAL_LINK_EXEC_DYN_LIB_CMDLINE+=$_LD_LINKER_OUTPUT
  200. REAL_LINK_EXEC_DYN_LIB_CMDLINE+=\
  201. ${pre=--whole-archive-peers :WHOLE_ARCHIVE_PEERS} \
  202. ${pre=--whole-archive-libs :_WHOLE_ARCHIVE_LIBS_VALUE_GLOBAL} \
  203. $_LD_ARCH_FLAG \
  204. $LINK_DYN_LIB_FLAGS \
  205. $CXX_COMPILER_OLD \
  206. $_LD_SRCS_GLOBALS \
  207. $VCS_C_OBJ $_EXTRA_OBJS $AUTO_INPUT -o $TARGET \
  208. $_EXEC_SHARED_FLAG \
  209. $_SONAME_FLAG \
  210. $_EXE_FLAGS \
  211. $_PROCESS_WHOLE_ARCHIVE_SCRIPT \
  212. $_LD_ENV_STYLE
  213. REAL_LINK_EXEC_DYN_LIB=$REAL_LINK_EXEC_DYN_LIB_IMPL($_WHOLE_ARCHIVE_PEERS_VALUE)
  214. # Shared Library
  215. LINK_DYN_LIB_FLAGS=
  216. REAL_LINK_DYN_LIB_CMDLINE =\
  217. $YMAKE_PYTHON ${input:"build/scripts/link_dyn_lib.py"} ${hide;input:"build/scripts/fix_py2_protobuf.py"} \
  218. --target $TARGET
  219. REAL_LINK_DYN_LIB_CMDLINE+=$_LD_LINKER_OUTPUT
  220. REAL_LINK_DYN_LIB_CMDLINE+=\
  221. ${pre=--whole-archive-peers :WHOLE_ARCHIVE_PEERS} \
  222. ${pre=--whole-archive-libs :_WHOLE_ARCHIVE_LIBS_VALUE_GLOBAL} \
  223. $_LD_ARCH_FLAG \
  224. $LINK_DYN_LIB_FLAGS \
  225. $CXX_COMPILER_OLD \
  226. $_LD_SRCS_GLOBALS \
  227. $VCS_C_OBJ $_EXTRA_OBJS $AUTO_INPUT -o $TARGET \
  228. $_SHARED_FLAG \
  229. $_SONAME_FLAG \
  230. $_EXE_FLAGS \
  231. $_PROCESS_WHOLE_ARCHIVE_SCRIPT \
  232. $_LD_ENV_STYLE
  233. REAL_LINK_DYN_LIB=$REAL_LINK_DYN_LIB_IMPL($_WHOLE_ARCHIVE_PEERS_VALUE)
  234. _DWARF_COMMAND=
  235. when (($OS_DARWIN == "yes" || $OS_IOS || $OS_IOSSIM) && $NO_DEBUGINFO != "yes" && $SPLIT_DWARF_VALUE != "no" && $NO_SPLIT_DWARF != "yes") {
  236. _DWARF_COMMAND=$DWARF_TOOL $TARGET -o ${output;pre=$MODULE_PREFIX$REALPRJNAME.dSYM/Contents/Resources/DWARF/$MODULE_PREFIX:REALPRJNAME} $_LD_ENV_STYLE
  237. }
  238. elsewhen ($OS_DARWIN == "yes" && $NO_DEBUGINFO != "yes") {
  239. _EXE_FLAGS+=$DEBUG_INFO_FLAGS
  240. }
  241. otherwise {
  242. _DWARF_COMMAND=
  243. }
  244. DWARF_COMMAND=$_DWARF_COMMAND
  245. _REAL_LINK_EXE=$REAL_LINK_EXE_IMPL($_WHOLE_ARCHIVE_PEERS_VALUE)
  246. _EXTRA_OBJS=
  247. _GENERATE_EXTRA_OBJS=
  248. _LINK_EXE=
  249. _LINK_EXE+=$GENERATE_MF
  250. _LINK_EXE+=&& $GENERATE_VCS_C_INFO_NODEP
  251. _LINK_EXE+=&& $_GENERATE_EXTRA_OBJS
  252. _LINK_EXE+=&& $COPY_PROFILE_RUNTIME
  253. _LINK_EXE+=&& $REAL_LINK_EXE
  254. _LINK_EXE+=&& $DWARF_COMMAND
  255. _LINK_EXE+=&& $LINK_ADDITIONAL_SECTIONS_COMMAND
  256. _LINK_EXE+=&& $PACK_IOS_CMD
  257. _LINK_DYN_LIB=
  258. _LINK_DYN_LIB+=$GENERATE_MF
  259. _LINK_DYN_LIB+=&& $GENERATE_VCS_C_INFO_NODEP
  260. _LINK_DYN_LIB+=&& $_GENERATE_EXTRA_OBJS
  261. _LINK_DYN_LIB+=&& $COPY_PROFILE_RUNTIME
  262. _LINK_DYN_LIB+=&& $REAL_LINK_DYN_LIB
  263. _LINK_DYN_LIB+=&& $DWARF_COMMAND
  264. _LINK_DYN_LIB+=&& $LINK_ADDITIONAL_SECTIONS_COMMAND
  265. when ($TIDY == "yes") {
  266. _REAL_LINK_EXE=$REAL_LINK_EXE_TIDY
  267. _LINK_EXE=$REAL_LINK_EXE
  268. _LINK_DYN_LIB=$REAL_LINK_EXE
  269. }
  270. REAL_LINK_EXE=$_REAL_LINK_EXE
  271. LINK_EXE=$_LINK_EXE
  272. LINK_DYN_LIB=$_LINK_DYN_LIB
  273. LINK_EXEC_DYN_LIB=\
  274. $GENERATE_MF && \
  275. $GENERATE_VCS_C_INFO_NODEP && \
  276. $_GENERATE_EXTRA_OBJS && \
  277. $REAL_LINK_EXEC_DYN_LIB && \
  278. $DWARF_COMMAND && \
  279. $LINK_ADDITIONAL_SECTIONS_COMMAND
  280. SWIG_DLL_JAR_CMD=\
  281. $GENERATE_MF && \
  282. $GENERATE_VCS_C_INFO_NODEP && \
  283. $REAL_SWIG_DLL_JAR_CMD && \
  284. $DWARF_COMMAND
  285. _LD_TAIL_LINK_LIB=$AUTO_INPUT ${hide;kv:"p AR"} $TOOLCHAIN_ENV ${hide;kv:"pc light-red"} ${hide;kv:"show_out"}
  286. _LD_ARCHIVER=$YMAKE_PYTHON ${input:"build/scripts/link_lib.py"} ${quo:AR_TOOL} $AR_TYPE $_LD_LLVM_AR_FORMAT $ARCADIA_BUILD_ROOT $_LD_AR_PLUGIN
  287. _LD_LIB_GENERATE_MF=$GENERATE_MF &&
  288. when ($TIDY == "yes") {
  289. _LD_ARCHIVER=$YMAKE_PYTHON ${input:"build/scripts/clang_tidy_arch.py"} --source-root $ARCADIA_ROOT --build-root $ARCADIA_BUILD_ROOT --output-file
  290. _LD_LIB_GENERATE_MF=
  291. }
  292. LINK_LIB=$_LD_LIB_GENERATE_MF $_LD_ARCHIVER $TARGET $_LD_TAIL_LINK_LIB
  293. GLOBAL_LINK_LIB=$_LD_ARCHIVER $GLOBAL_TARGET $_LD_TAIL_LINK_LIB
  294. # "Fat Object" : pre-linked global objects and static library with all dependencies
  295. _LD_LINK_FAT_PREFIX=$GENERATE_MF && $GENERATE_VCS_C_INFO_NODEP && $YMAKE_PYTHON ${input:"build/scripts/link_fat_obj.py"} --build-root $ARCADIA_BUILD_ROOT $_PROCESS_WHOLE_ARCHIVE_SCRIPT
  296. _LD_LINK_FAT_SRCS_GLOBALS_WA=$_LD_SRCS_GLOBALS
  297. _LD_LINK_FAT_SRCS_GLOBALS_NO_WA=${rootrel;ext=.a:SRCS_GLOBAL} ${rootrel;ext=.o:SRCS_GLOBAL}
  298. macro _FAT_OBJECT_ARGS_BASE(Flag, Lib) {
  299. .CMD=$Flag=$Lib
  300. }
  301. # FIXME: pre=lib here is weird legacy
  302. _FAT_OBJECT_ARGS=--obj=${pre=lib;output:REALPRJNAME.o}
  303. _LD_LINK_FAT_SRCS_GLOBALS=$_LD_LINK_FAT_SRCS_GLOBALS_WA
  304. macro PACK_GLOBALS_IN_LIBRARY() {
  305. SET(_FAT_OBJECT_ARGS $_FAT_OBJECT_ARGS_BASE(--globals-lib, \${output;pre=$MODULE_PREFIX:REALPRJNAME.globals.a}))
  306. }
  307. when ($OS_ANDROID == "yes") {
  308. _FAT_OBJECT_ARGS=
  309. _LD_LINK_FAT_SRCS_GLOBALS=$_LD_LINK_FAT_SRCS_GLOBALS_NO_WA
  310. }
  311. _LD_LINK_FAT_SUFFIX1=$_LD_ARCH_FLAG \
  312. -Ya,input $AUTO_INPUT $VCS_C_OBJ \
  313. -Ya,global_srcs
  314. _LD_LINK_FAT_SUFFIX2=-Ya,peers $PEERS \
  315. -Ya,linker $CXX_COMPILER_OLD \
  316. $LDFLAGS_GLOBAL $C_FLAGS_PLATFORM $LD_SDK_VERSION \
  317. -Ya,archiver $_LD_ARCHIVER \
  318. $TOOLCHAIN_ENV ${hide;kv:"p LD"} ${hide;kv:"pc light-blue"} ${hide;kv:"show_out"}
  319. LINK_FAT_OBJECT=\
  320. $_LD_LINK_FAT_PREFIX \
  321. --lib=$TARGET $_FAT_OBJECT_ARGS \
  322. $_LD_LINK_FAT_SUFFIX1 \
  323. $_LD_LINK_FAT_SRCS_GLOBALS \
  324. $_LD_LINK_FAT_SUFFIX2
  325. LINK_RECURSIVE_LIBRARY=\
  326. $_LD_LINK_FAT_PREFIX \
  327. --lib=$TARGET --with-own-obj --with-global-srcs \
  328. $_LD_LINK_FAT_SUFFIX1 \
  329. $_LD_LINK_FAT_SRCS_GLOBALS_NO_WA \
  330. $_LD_LINK_FAT_SUFFIX2
  331. LIBRT=-lrt
  332. LIBRESOLV=
  333. when ($OS_LINUX || $OS_DARWIN || $OS_ANDROID) {
  334. LIBRESOLV=-lresolv
  335. }
  336. PROFFLAG=-pg