xref: /freebsd/share/mk/bsd.linker.mk (revision dc3c6ad63c9b9f23cea1cd047288972bfcf6049b)
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}} --version || 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${X_}LINKER_FREEBSD_VERSION!= \
73	${${ld}} --version | \
74	awk '$$3 ~ /FreeBSD/ {print substr($$4, 1, length($$4)-1)}'
75.else
76.warning Unknown linker from ${ld}=${${ld}}: ${_ld_version}, defaulting to bfd
77${X_}LINKER_TYPE=	bfd
78_v=	2.17.50
79.endif
80${X_}LINKER_VERSION!=	echo "${_v:M[1-9].[0-9]*}" | \
81			  awk -F. '{print $$1 * 10000 + $$2 * 100 + $$3;}'
82.undef _ld_version
83.undef _v
84${X_}LINKER_FEATURES=
85.if ${${X_}LINKER_TYPE} != "bfd" || ${${X_}LINKER_VERSION} > 21750
86${X_}LINKER_FEATURES+=	build-id
87${X_}LINKER_FEATURES+=	ifunc
88.endif
89.if ${${X_}LINKER_TYPE} == "lld" && ${${X_}LINKER_VERSION} >= 60000
90${X_}LINKER_FEATURES+=	retpoline
91.endif
92.endif
93.else
94# Use LD's values
95X_LINKER_TYPE=		${LINKER_TYPE}
96X_LINKER_VERSION=	${LINKER_VERSION}
97X_LINKER_FEATURES=	${LINKER_FEATURES}
98X_LINKER_FREEBSD_VERSION= ${LINKER_FREEBSD_VERSION}
99.endif	# ${ld} == "LD" || (${ld} == "XLD" && ${XLD} != ${LD})
100
101# Export the values so sub-makes don't have to look them up again, using the
102# hash key computed above.
103.for var in ${_exported_vars}
104${var}__${${X_}_ld_hash}:=	${${var}}
105.export-env ${var}__${${X_}_ld_hash}
106.undef ${var}__${${X_}_ld_hash}
107.endfor
108
109.endif	# ${ld} == "LD" || !empty(XLD)
110.endfor	# .for ld in LD XLD
111
112
113.endif	# !target(__<bsd.linker.mk>__)
114