xref: /freebsd/share/mk/bsd.linker.mk (revision 63a938566d524836885917d95bd491aa4400b181)
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.endif
75.if ${${X_}LINKER_TYPE} != "lld" || ${${X_}LINKER_VERSION} >= 50000
76${X_}LINKER_FEATURES+=	filter
77.endif
78.if ${${X_}LINKER_TYPE} == "lld" && ${${X_}LINKER_VERSION} >= 60000
79${X_}LINKER_FEATURES+=	retpoline
80.endif
81.endif
82.else
83# Use LD's values
84X_LINKER_TYPE=		${LINKER_TYPE}
85X_LINKER_VERSION=	${LINKER_VERSION}
86X_LINKER_FEATURES=	${LINKER_FEATURES}
87.endif	# ${ld} == "LD" || (${ld} == "XLD" && ${XLD} != ${LD})
88
89# Export the values so sub-makes don't have to look them up again, using the
90# hash key computed above.
91.for var in ${_exported_vars}
92${var}.${${X_}_ld_hash}:=	${${var}}
93.export-env ${var}.${${X_}_ld_hash}
94.undef ${var}.${${X_}_ld_hash}
95.endfor
96
97.endif	# ${ld} == "LD" || !empty(XLD)
98.endfor	# .for ld in LD XLD
99
100
101.endif	# !target(__<bsd.linker.mk>__)
102