xref: /freebsd/share/mk/bsd.linker.mk (revision e25152834cdf3b353892835a4f3b157e066a8ed4)
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 differently somehow else.
45_can_export=	yes
46.for var in ${_exported_vars}
47.if defined(${var}) && (!defined(${var}__${${X_}_ld_hash}) || ${${var}__${${X_}_ld_hash}} != ${${var}})
48.if defined(${var}__${${X_}_ld_hash})
49.info "Cannot import ${X_}LINKER variables since cached ${var} is different: ${${var}__${${X_}_ld_hash}} != ${${var}}"
50.endif
51_can_export=	no
52.endif
53.endfor
54.if ${_can_export} == yes
55.for var in ${_exported_vars}
56.if defined(${var}__${${X_}_ld_hash})
57${var}=	${${var}__${${X_}_ld_hash}}
58.endif
59.endfor
60.endif
61
62.if ${ld} == "LD" || (${ld} == "XLD" && ${XLD} != ${LD})
63.if !defined(${X_}LINKER_TYPE) || !defined(${X_}LINKER_VERSION)
64_ld_version!=	(${${ld}} -v 2>&1 || echo none) | sed -n 1p
65.if ${_ld_version} == "none"
66.warning Unable to determine linker type from ${ld}=${${ld}}
67.endif
68.if ${_ld_version:[1..2]} == "GNU ld"
69${X_}LINKER_TYPE=	bfd
70${X_}LINKER_FREEBSD_VERSION=	0
71_v=	${_ld_version:M[1-9]*.[0-9]*:[1]}
72.elif ${_ld_version:[1]} == "LLD"
73${X_}LINKER_TYPE=	lld
74_v=	${_ld_version:[2]}
75.if ${_ld_version:[3]} == "(FreeBSD"
76${X_}LINKER_FREEBSD_VERSION:=	${_ld_version:[4]:C/.*-([^-]*)\)/\1/}
77.else
78${X_}LINKER_FREEBSD_VERSION=	0
79.endif
80.elif ${_ld_version:[1]} == "@(\#)PROGRAM:ld"
81# bootstrap linker on MacOS
82${X_}LINKER_TYPE=        mac
83_v=        ${_ld_version:[2]:S/PROJECT:ld64-//}
84# Convert version 409.12 to 409.12.0 so that the echo + awk below works
85.if empty(_v:M[1-9]*.[0-9]*.[0-9]*) && !empty(_v:M[1-9]*.[0-9]*)
86_v:=${_v}.0
87.else
88# Some versions do not contain a minor version so we need to append .0.0 there
89_v:=${_v}.0.0
90.endif
91.else
92.warning Unknown linker from ${ld}=${${ld}}: ${_ld_version}, defaulting to bfd
93${X_}LINKER_TYPE=	bfd
94_v=	2.17.50
95.endif
96${X_}LINKER_VERSION!=	echo "${_v:M[1-9]*.[0-9]*}" | \
97			  awk -F. '{print $$1 * 10000 + $$2 * 100 + $$3;}'
98.undef _ld_version
99.undef _v
100${X_}LINKER_FEATURES=
101.if ${${X_}LINKER_TYPE} != "bfd" || ${${X_}LINKER_VERSION} > 21750
102${X_}LINKER_FEATURES+=	build-id
103${X_}LINKER_FEATURES+=	ifunc
104.endif
105.if ${${X_}LINKER_TYPE} == "bfd" && ${${X_}LINKER_VERSION} > 21750
106${X_}LINKER_FEATURES+=	riscv-relaxations
107.endif
108.if ${${X_}LINKER_TYPE} == "lld" && ${${X_}LINKER_VERSION} >= 60000
109${X_}LINKER_FEATURES+=	retpoline
110.endif
111.if ${${X_}LINKER_TYPE} == "lld" && ${${X_}LINKER_VERSION} >= 90000
112${X_}LINKER_FEATURES+=	ifunc-noplt
113.endif
114.endif
115.else
116# Use LD's values
117X_LINKER_TYPE=		${LINKER_TYPE}
118X_LINKER_VERSION=	${LINKER_VERSION}
119X_LINKER_FEATURES=	${LINKER_FEATURES}
120X_LINKER_FREEBSD_VERSION= ${LINKER_FREEBSD_VERSION}
121.endif	# ${ld} == "LD" || (${ld} == "XLD" && ${XLD} != ${LD})
122
123# Export the values so sub-makes don't have to look them up again, using the
124# hash key computed above.
125.for var in ${_exported_vars}
126${var}__${${X_}_ld_hash}:=	${${var}}
127.export-env ${var}__${${X_}_ld_hash}
128.undef ${var}__${${X_}_ld_hash}
129.endfor
130
131.endif	# ${ld} == "LD" || !empty(XLD)
132.endfor	# .for ld in LD XLD
133
134
135.endif	# !target(__<bsd.linker.mk>__)
136