xref: /freebsd/share/mk/bsd.linker.mk (revision 390dc369efaaeca2802baf168ddbd7a40e3afcc8)
1
2# Setup variables for the linker.
3#
4# LINKER_TYPE is the major type of linker. Currently binutils and lld support
5# automatic detection.
6#
7# LINKER_VERSION is a numeric constant equal to:
8#     major * 10000 + minor * 100 + tiny
9# It too can be overridden on the command line.
10#
11# LINKER_FEATURES may contain one or more of the following, based on
12# linker support for that feature:
13#
14# - build-id:   support for generating a Build-ID note
15# - retpoline:  support for generating PLT with retpoline speculative
16#               execution vulnerability mitigation
17# - bti-report: support for specifying how to report the missing
18#               Branch Target Identification (BTI) property (AArch64)
19#
20# LINKER_FREEBSD_VERSION is the linker's internal source version.
21#
22# These variables with an X_ prefix will also be provided if XLD is set.
23#
24# This file may be included multiple times, but only has effect the first time.
25#
26
27.if !target(__<bsd.linker.mk>__)
28__<bsd.linker.mk>__:
29
30_ld_vars=LD $${_empty_var_}
31.if !empty(_WANT_TOOLCHAIN_CROSS_VARS)
32# Only the toplevel makefile needs to compute the X_LINKER_* variables.
33_ld_vars+=XLD X_
34.endif
35
36.for ld X_ in ${_ld_vars}
37.if ${ld} == "LD" || !empty(XLD)
38# Try to import LINKER_TYPE and LINKER_VERSION from parent make.
39# The value is only used/exported for the same environment that impacts
40# LD and LINKER_* settings here.
41_exported_vars=	${X_}LINKER_TYPE ${X_}LINKER_VERSION ${X_}LINKER_FEATURES \
42		${X_}LINKER_FREEBSD_VERSION
43${X_}_ld_hash=	${${ld}}${MACHINE}${PATH}
44${X_}_ld_hash:=	${${X_}_ld_hash:hash}
45# Only import if none of the vars are set differently somehow else.
46_can_export=	yes
47.for var in ${_exported_vars}
48.if defined(${var}) && (!defined(${var}__${${X_}_ld_hash}) || ${${var}__${${X_}_ld_hash}} != ${${var}})
49.if defined(${var}__${${X_}_ld_hash})
50.info Cannot import ${X_}LINKER variables since cached ${var} is different: ${${var}__${${X_}_ld_hash}} != ${${var}}
51.endif
52_can_export=	no
53.endif
54.endfor
55.if ${_can_export} == yes
56.for var in ${_exported_vars}
57.if defined(${var}__${${X_}_ld_hash})
58${var}=	${${var}__${${X_}_ld_hash}}
59.endif
60.endfor
61.endif
62
63.if ${ld} == "LD" || (${ld} == "XLD" && ${XLD} != ${LD})
64.if !defined(${X_}LINKER_TYPE) || !defined(${X_}LINKER_VERSION)
65_ld_version!=	(${${ld}} -v 2>&1 || echo none) | sed -n 1p
66.if ${_ld_version} == "none"
67.warning Unable to determine linker type from ${ld}=${${ld}}
68.endif
69.if ${_ld_version:[1..2]} == "GNU ld"
70${X_}LINKER_TYPE=	bfd
71${X_}LINKER_FREEBSD_VERSION=	0
72_v=	${_ld_version:M[1-9]*.[0-9]*:[1]}
73.elif ${_ld_version:MLLD}
74# Strip any leading PACKAGE_VENDOR string (e.g. "Homebrew")
75_ld_version:=${_ld_version:[*]:C/^.* LLD /LLD /:[@]}
76${X_}LINKER_TYPE=	lld
77_v=	${_ld_version:[2]}
78.if ${_ld_version:[3]} == "(FreeBSD"
79${X_}LINKER_FREEBSD_VERSION:=	${_ld_version:[4]:C/.*-([^-]*)\)/\1/}
80.else
81${X_}LINKER_FREEBSD_VERSION=	0
82.endif
83.elif ${_ld_version:[1]:S/-classic$//} == "@(\#)PROGRAM:ld"
84# bootstrap linker on MacOS
85${X_}LINKER_TYPE=        mac
86_v=        ${_ld_version:[2]:C/PROJECT:(ld64|dyld)-//}
87# Convert version 409.12 to 409.12.0 so that the echo + awk below works
88.if empty(_v:M[1-9]*.[0-9]*.[0-9]*) && !empty(_v:M[1-9]*.[0-9]*)
89_v:=${_v}.0
90.else
91# Some versions do not contain a minor version so we need to append .0.0 there
92_v:=${_v}.0.0
93.endif
94.else
95.warning Unknown linker from ${ld}=${${ld}}: ${_ld_version}, defaulting to bfd
96${X_}LINKER_TYPE=	bfd
97_v=	2.17.50
98.endif
99${X_}LINKER_VERSION!=	echo "${_v:M[1-9]*.[0-9]*}" | \
100			  awk -F. '{print $$1 * 10000 + $$2 * 100 + $$3;}'
101.undef _ld_version
102.undef _v
103${X_}LINKER_FEATURES=
104.if ${${X_}LINKER_TYPE} != "bfd" || ${${X_}LINKER_VERSION} > 21750
105${X_}LINKER_FEATURES+=	build-id
106${X_}LINKER_FEATURES+=	ifunc
107.endif
108.if ${${X_}LINKER_TYPE} == "bfd" && ${${X_}LINKER_VERSION} > 21750
109${X_}LINKER_FEATURES+=	riscv-relaxations
110.endif
111.if ${${X_}LINKER_TYPE} == "lld" && ${${X_}LINKER_VERSION} >= 60000
112${X_}LINKER_FEATURES+=	retpoline
113.endif
114.if ${${X_}LINKER_TYPE} == "lld" && ${${X_}LINKER_VERSION} >= 90000
115${X_}LINKER_FEATURES+=	ifunc-noplt
116.endif
117.if ${${X_}LINKER_TYPE} == "lld" && ${${X_}LINKER_VERSION} >= 140000
118${X_}LINKER_FEATURES+=	bti-report
119.endif
120.endif
121.else
122# Use LD's values
123X_LINKER_TYPE=		${LINKER_TYPE}
124X_LINKER_VERSION=	${LINKER_VERSION}
125X_LINKER_FEATURES=	${LINKER_FEATURES}
126X_LINKER_FREEBSD_VERSION= ${LINKER_FREEBSD_VERSION}
127.endif	# ${ld} == "LD" || (${ld} == "XLD" && ${XLD} != ${LD})
128
129# Export the values so sub-makes don't have to look them up again, using the
130# hash key computed above.
131.for var in ${_exported_vars}
132${var}__${${X_}_ld_hash}:=	${${var}}
133.export-env ${var}__${${X_}_ld_hash}
134.undef ${var}__${${X_}_ld_hash}
135.endfor
136
137.endif	# ${ld} == "LD" || !empty(XLD)
138.endfor	# .for ld in LD XLD
139
140
141.endif	# !target(__<bsd.linker.mk>__)
142