xref: /freebsd/share/mk/bsd.linker.mk (revision 01c4f3a763ed9e6cc1ebc3beca76f29b9b6ebfb3)
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}} -v 2>&1 || 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.elif ${_ld_version:[1]} == "@(\#)PROGRAM:ld"
78# bootstrap linker on MacOS
79${X_}LINKER_TYPE=        mac
80_v=        ${_ld_version:[2]:S/PROJECT:ld64-//}
81# Convert version 409.12 to 409.12.0 so that the echo + awk below works
82.if empty(_v:M[1-9]*.[0-9]*.[0-9]*) && !empty(_v:M[1-9]*.[0-9]*)
83_v:=${_v}.0
84.else
85# Some versions do not contain a minor version so we need to append .0.0 there
86_v:=${_v}.0.0
87.endif
88.else
89.warning Unknown linker from ${ld}=${${ld}}: ${_ld_version}, defaulting to bfd
90${X_}LINKER_TYPE=	bfd
91_v=	2.17.50
92.endif
93${X_}LINKER_VERSION!=	echo "${_v:M[1-9]*.[0-9]*}" | \
94			  awk -F. '{print $$1 * 10000 + $$2 * 100 + $$3;}'
95.undef _ld_version
96.undef _v
97${X_}LINKER_FEATURES=
98.if ${${X_}LINKER_TYPE} != "bfd" || ${${X_}LINKER_VERSION} > 21750
99${X_}LINKER_FEATURES+=	build-id
100${X_}LINKER_FEATURES+=	ifunc
101.endif
102.if ${${X_}LINKER_TYPE} == "bfd" && ${${X_}LINKER_VERSION} > 21750
103${X_}LINKER_FEATURES+=	riscv-relaxations
104.endif
105.if ${${X_}LINKER_TYPE} == "lld" && ${${X_}LINKER_VERSION} >= 60000
106${X_}LINKER_FEATURES+=	retpoline
107.endif
108.if ${${X_}LINKER_TYPE} == "lld" && ${${X_}LINKER_VERSION} >= 90000
109${X_}LINKER_FEATURES+=	ifunc-noplt
110.endif
111.endif
112.else
113# Use LD's values
114X_LINKER_TYPE=		${LINKER_TYPE}
115X_LINKER_VERSION=	${LINKER_VERSION}
116X_LINKER_FEATURES=	${LINKER_FEATURES}
117X_LINKER_FREEBSD_VERSION= ${LINKER_FREEBSD_VERSION}
118.endif	# ${ld} == "LD" || (${ld} == "XLD" && ${XLD} != ${LD})
119
120# Export the values so sub-makes don't have to look them up again, using the
121# hash key computed above.
122.for var in ${_exported_vars}
123${var}__${${X_}_ld_hash}:=	${${var}}
124.export-env ${var}__${${X_}_ld_hash}
125.undef ${var}__${${X_}_ld_hash}
126.endfor
127
128.endif	# ${ld} == "LD" || !empty(XLD)
129.endfor	# .for ld in LD XLD
130
131
132.endif	# !target(__<bsd.linker.mk>__)
133