xref: /freebsd/share/mk/bsd.linker.mk (revision ff8f1e8332cd60abc0bc2b189d988c5a24d4650b)
1# $FreeBSD$
2
3# Setup variables for the linker.
4#
5# LINKER_TYPE is the major type of linker. Currently binutils and lld support
6# automatic detection.
7#
8# LINKER_VERSION is a numeric constant equal to:
9#     major * 10000 + minor * 100 + tiny
10# It too can be overridden on the command line.
11#
12# LINKER_FEATURES may contain one or more of the following, based on
13# linker support for that feature:
14#
15# - build-id:  support for generating a Build-ID note
16# - retpoline: support for generating PLT with retpoline speculative
17#              execution vulnerability mitigation
18#
19# These variables with an X_ prefix will also be provided if XLD is set.
20#
21# This file may be included multiple times, but only has effect the first time.
22#
23
24.if !target(__<bsd.linker.mk>__)
25__<bsd.linker.mk>__:
26
27.for ld X_ in LD $${_empty_var_} XLD X_
28.if ${ld} == "LD" || !empty(XLD)
29# Try to import LINKER_TYPE and LINKER_VERSION from parent make.
30# The value is only used/exported for the same environment that impacts
31# LD and LINKER_* settings here.
32_exported_vars=	${X_}LINKER_TYPE ${X_}LINKER_VERSION ${X_}LINKER_FEATURES
33${X_}_ld_hash=	${${ld}}${MACHINE}${PATH}
34${X_}_ld_hash:=	${${X_}_ld_hash:hash}
35# Only import if none of the vars are set somehow else.
36_can_export=	yes
37.for var in ${_exported_vars}
38.if defined(${var})
39_can_export=	no
40.endif
41.endfor
42.if ${_can_export} == yes
43.for var in ${_exported_vars}
44.if defined(${var}.${${X_}_ld_hash})
45${var}=	${${var}.${${X_}_ld_hash}}
46.endif
47.endfor
48.endif
49
50.if ${ld} == "LD" || (${ld} == "XLD" && ${XLD} != ${LD})
51.if !defined(${X_}LINKER_TYPE) || !defined(${X_}LINKER_VERSION)
52_ld_version!=	(${${ld}} --version || echo none) | head -n 1
53.if ${_ld_version} == "none"
54.warning Unable to determine linker type from ${ld}=${${ld}}
55.endif
56.if ${_ld_version:[1..2]} == "GNU ld"
57${X_}LINKER_TYPE=	bfd
58_v=	${_ld_version:M[1-9].[0-9]*:[1]}
59.elif ${_ld_version:[1]} == "LLD"
60${X_}LINKER_TYPE=	lld
61_v=	${_ld_version:[2]}
62.else
63.warning Unknown linker from ${ld}=${${ld}}: ${_ld_version}, defaulting to bfd
64${X_}LINKER_TYPE=	bfd
65_v=	2.17.50
66.endif
67${X_}LINKER_VERSION!=	echo "${_v:M[1-9].[0-9]*}" | \
68			  awk -F. '{print $$1 * 10000 + $$2 * 100 + $$3;}'
69.undef _ld_version
70.undef _v
71${X_}LINKER_FEATURES=
72.if ${${X_}LINKER_TYPE} != "bfd" || ${${X_}LINKER_VERSION} > 21750
73${X_}LINKER_FEATURES+=	build-id
74${X_}LINKER_FEATURES+=	ifunc
75.endif
76.if ${${X_}LINKER_TYPE} != "lld" || ${${X_}LINKER_VERSION} >= 50000
77${X_}LINKER_FEATURES+=	filter
78.endif
79.if ${${X_}LINKER_TYPE} == "lld" && ${${X_}LINKER_VERSION} >= 60000
80${X_}LINKER_FEATURES+=	retpoline
81.endif
82.endif
83.else
84# Use LD's values
85X_LINKER_TYPE=		${LINKER_TYPE}
86X_LINKER_VERSION=	${LINKER_VERSION}
87X_LINKER_FEATURES=	${LINKER_FEATURES}
88.endif	# ${ld} == "LD" || (${ld} == "XLD" && ${XLD} != ${LD})
89
90# Export the values so sub-makes don't have to look them up again, using the
91# hash key computed above.
92.for var in ${_exported_vars}
93${var}.${${X_}_ld_hash}:=	${${var}}
94.export-env ${var}.${${X_}_ld_hash}
95.undef ${var}.${${X_}_ld_hash}
96.endfor
97
98.endif	# ${ld} == "LD" || !empty(XLD)
99.endfor	# .for ld in LD XLD
100
101
102.endif	# !target(__<bsd.linker.mk>__)
103