xref: /freebsd/share/mk/bsd.linker.mk (revision 440cec3faf778469b36b998bb52aab7fbc43eae3)
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# LINKER_FREEBSD_VERSION is the linker's internal source version.
20#
21# These variables with an X_ prefix will also be provided if XLD is set.
22#
23# This file may be included multiple times, but only has effect the first time.
24#
25
26.if !target(__<bsd.linker.mk>__)
27__<bsd.linker.mk>__:
28
29_ld_vars=LD $${_empty_var_}
30.if !empty(_WANT_TOOLCHAIN_CROSS_VARS)
31# Only the toplevel makefile needs to compute the X_LINKER_* variables.
32_ld_vars+=XLD X_
33.endif
34
35.for ld X_ in ${_ld_vars}
36.if ${ld} == "LD" || !empty(XLD)
37# Try to import LINKER_TYPE and LINKER_VERSION from parent make.
38# The value is only used/exported for the same environment that impacts
39# LD and LINKER_* settings here.
40_exported_vars=	${X_}LINKER_TYPE ${X_}LINKER_VERSION ${X_}LINKER_FEATURES \
41		${X_}LINKER_FREEBSD_VERSION
42${X_}_ld_hash=	${${ld}}${MACHINE}${PATH}
43${X_}_ld_hash:=	${${X_}_ld_hash:hash}
44# Only import if none of the vars are set somehow else.
45_can_export=	yes
46.for var in ${_exported_vars}
47.if defined(${var})
48_can_export=	no
49.endif
50.endfor
51.if ${_can_export} == yes
52.for var in ${_exported_vars}
53.if defined(${var}__${${X_}_ld_hash})
54${var}=	${${var}__${${X_}_ld_hash}}
55.endif
56.endfor
57.endif
58
59.if ${ld} == "LD" || (${ld} == "XLD" && ${XLD} != ${LD})
60.if !defined(${X_}LINKER_TYPE) || !defined(${X_}LINKER_VERSION)
61_ld_version!=	(${${ld}} --version || echo none) | sed -n 1p
62.if ${_ld_version} == "none"
63.warning Unable to determine linker type from ${ld}=${${ld}}
64.endif
65.if ${_ld_version:[1..2]} == "GNU ld"
66${X_}LINKER_TYPE=	bfd
67${X_}LINKER_FREEBSD_VERSION=	0
68_v=	${_ld_version:M[1-9]*.[0-9]*:[1]}
69.elif ${_ld_version:[1]} == "LLD"
70${X_}LINKER_TYPE=	lld
71_v=	${_ld_version:[2]}
72.if ${_ld_version:[3]} == "(FreeBSD"
73${X_}LINKER_FREEBSD_VERSION:=	${_ld_version:[4]:C/.*-([^-]*)\)/\1/}
74.else
75${X_}LINKER_FREEBSD_VERSION=	0
76.endif
77.else
78.warning Unknown linker from ${ld}=${${ld}}: ${_ld_version}, defaulting to bfd
79${X_}LINKER_TYPE=	bfd
80_v=	2.17.50
81.endif
82${X_}LINKER_VERSION!=	echo "${_v:M[1-9]*.[0-9]*}" | \
83			  awk -F. '{print $$1 * 10000 + $$2 * 100 + $$3;}'
84.undef _ld_version
85.undef _v
86${X_}LINKER_FEATURES=
87.if ${${X_}LINKER_TYPE} != "bfd" || ${${X_}LINKER_VERSION} > 21750
88${X_}LINKER_FEATURES+=	build-id
89${X_}LINKER_FEATURES+=	ifunc
90.endif
91.if ${${X_}LINKER_TYPE} == "bfd" && ${${X_}LINKER_VERSION} > 21750
92${X_}LINKER_FEATURES+=	riscv-relaxations
93.endif
94.if ${${X_}LINKER_TYPE} == "lld" && ${${X_}LINKER_VERSION} >= 60000
95${X_}LINKER_FEATURES+=	retpoline
96.endif
97.if ${${X_}LINKER_TYPE} == "lld" && ${${X_}LINKER_VERSION} >= 90000
98${X_}LINKER_FEATURES+=	ifunc-noplt
99.endif
100.endif
101.else
102# Use LD's values
103X_LINKER_TYPE=		${LINKER_TYPE}
104X_LINKER_VERSION=	${LINKER_VERSION}
105X_LINKER_FEATURES=	${LINKER_FEATURES}
106X_LINKER_FREEBSD_VERSION= ${LINKER_FREEBSD_VERSION}
107.endif	# ${ld} == "LD" || (${ld} == "XLD" && ${XLD} != ${LD})
108
109# Export the values so sub-makes don't have to look them up again, using the
110# hash key computed above.
111.for var in ${_exported_vars}
112${var}__${${X_}_ld_hash}:=	${${var}}
113.export-env ${var}__${${X_}_ld_hash}
114.undef ${var}__${${X_}_ld_hash}
115.endfor
116
117.endif	# ${ld} == "LD" || !empty(XLD)
118.endfor	# .for ld in LD XLD
119
120
121.endif	# !target(__<bsd.linker.mk>__)
122