xref: /freebsd/share/mk/bsd.linker.mk (revision a7d84af8903353c75350105366bed5c80182a66f)
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.for ld X_ in LD $${_empty_var_} XLD X_
30.if ${ld} == "LD" || !empty(XLD)
31# Try to import LINKER_TYPE and LINKER_VERSION from parent make.
32# The value is only used/exported for the same environment that impacts
33# LD and LINKER_* settings here.
34_exported_vars=	${X_}LINKER_TYPE ${X_}LINKER_VERSION ${X_}LINKER_FEATURES \
35		${X_}LINKER_FREEBSD_VERSION
36${X_}_ld_hash=	${${ld}}${MACHINE}${PATH}
37${X_}_ld_hash:=	${${X_}_ld_hash:hash}
38# Only import if none of the vars are set somehow else.
39_can_export=	yes
40.for var in ${_exported_vars}
41.if defined(${var})
42_can_export=	no
43.endif
44.endfor
45.if ${_can_export} == yes
46.for var in ${_exported_vars}
47.if defined(${var}.${${X_}_ld_hash})
48${var}=	${${var}.${${X_}_ld_hash}}
49.endif
50.endfor
51.endif
52
53.if ${ld} == "LD" || (${ld} == "XLD" && ${XLD} != ${LD})
54.if !defined(${X_}LINKER_TYPE) || !defined(${X_}LINKER_VERSION)
55_ld_version!=	(${${ld}} --version || echo none) | sed -n 1p
56.if ${_ld_version} == "none"
57.warning Unable to determine linker type from ${ld}=${${ld}}
58.endif
59.if ${_ld_version:[1..2]} == "GNU ld"
60${X_}LINKER_TYPE=	bfd
61${X_}LINKER_FREEBSD_VERSION=	0
62_v=	${_ld_version:M[1-9].[0-9]*:[1]}
63.elif ${_ld_version:[1]} == "LLD"
64${X_}LINKER_TYPE=	lld
65_v=	${_ld_version:[2]}
66${X_}LINKER_FREEBSD_VERSION!= \
67	${${ld}} --version | \
68	awk '$$3 ~ /FreeBSD/ {print substr($$4, 1, length($$4)-1)}'
69.else
70.warning Unknown linker from ${ld}=${${ld}}: ${_ld_version}, defaulting to bfd
71${X_}LINKER_TYPE=	bfd
72_v=	2.17.50
73.endif
74${X_}LINKER_VERSION!=	echo "${_v:M[1-9].[0-9]*}" | \
75			  awk -F. '{print $$1 * 10000 + $$2 * 100 + $$3;}'
76.undef _ld_version
77.undef _v
78${X_}LINKER_FEATURES=
79.if ${${X_}LINKER_TYPE} != "bfd" || ${${X_}LINKER_VERSION} > 21750
80${X_}LINKER_FEATURES+=	build-id
81${X_}LINKER_FEATURES+=	ifunc
82.endif
83.if ${${X_}LINKER_TYPE} != "lld" || ${${X_}LINKER_VERSION} >= 50000
84${X_}LINKER_FEATURES+=	filter
85.endif
86.if ${${X_}LINKER_TYPE} == "lld" && ${${X_}LINKER_VERSION} >= 60000
87${X_}LINKER_FEATURES+=	retpoline
88.endif
89.endif
90.else
91# Use LD's values
92X_LINKER_TYPE=		${LINKER_TYPE}
93X_LINKER_VERSION=	${LINKER_VERSION}
94X_LINKER_FEATURES=	${LINKER_FEATURES}
95X_LINKER_FREEBSD_VERSION= ${LINKER_FREEBSD_VERSION}
96.endif	# ${ld} == "LD" || (${ld} == "XLD" && ${XLD} != ${LD})
97
98# Export the values so sub-makes don't have to look them up again, using the
99# hash key computed above.
100.for var in ${_exported_vars}
101${var}.${${X_}_ld_hash}:=	${${var}}
102.export-env ${var}.${${X_}_ld_hash}
103.undef ${var}.${${X_}_ld_hash}
104.endfor
105
106.endif	# ${ld} == "LD" || !empty(XLD)
107.endfor	# .for ld in LD XLD
108
109
110.endif	# !target(__<bsd.linker.mk>__)
111