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