xref: /freebsd/share/mk/bsd.linker.mk (revision dc3c6ad63c9b9f23cea1cd047288972bfcf6049b)
1125902b6SBryan Drewery# $FreeBSD$
2125902b6SBryan Drewery
3125902b6SBryan Drewery# Setup variables for the linker.
4125902b6SBryan Drewery#
5125902b6SBryan Drewery# LINKER_TYPE is the major type of linker. Currently binutils and lld support
6125902b6SBryan Drewery# automatic detection.
7125902b6SBryan Drewery#
8125902b6SBryan Drewery# LINKER_VERSION is a numeric constant equal to:
9125902b6SBryan Drewery#     major * 10000 + minor * 100 + tiny
10125902b6SBryan Drewery# It too can be overridden on the command line.
11125902b6SBryan Drewery#
12cdd89b98SEd Maste# LINKER_FEATURES may contain one or more of the following, based on
13cdd89b98SEd Maste# linker support for that feature:
14cdd89b98SEd Maste#
15cdd89b98SEd Maste# - build-id:  support for generating a Build-ID note
1648164080SEd Maste# - retpoline: support for generating PLT with retpoline speculative
1748164080SEd Maste#              execution vulnerability mitigation
18cdd89b98SEd Maste#
19a7d84af8SBryan Drewery# LINKER_FREEBSD_VERSION is the linker's internal source version.
20a7d84af8SBryan Drewery#
2108154765SBryan Drewery# These variables with an X_ prefix will also be provided if XLD is set.
2208154765SBryan Drewery#
23125902b6SBryan Drewery# This file may be included multiple times, but only has effect the first time.
24125902b6SBryan Drewery#
25125902b6SBryan Drewery
26125902b6SBryan Drewery.if !target(__<bsd.linker.mk>__)
27125902b6SBryan Drewery__<bsd.linker.mk>__:
28125902b6SBryan Drewery
2974f65486SAlex Richardson_ld_vars=LD $${_empty_var_}
3074f65486SAlex Richardson.if !empty(_WANT_TOOLCHAIN_CROSS_VARS)
3174f65486SAlex Richardson# Only the toplevel makefile needs to compute the X_LINKER_* variables.
3274f65486SAlex Richardson_ld_vars+=XLD X_
3374f65486SAlex Richardson.endif
3474f65486SAlex Richardson
3574f65486SAlex Richardson.for ld X_ in ${_ld_vars}
3608154765SBryan Drewery.if ${ld} == "LD" || !empty(XLD)
3719fad655SBryan Drewery# Try to import LINKER_TYPE and LINKER_VERSION from parent make.
3819fad655SBryan Drewery# The value is only used/exported for the same environment that impacts
3919fad655SBryan Drewery# LD and LINKER_* settings here.
40a7d84af8SBryan Drewery_exported_vars=	${X_}LINKER_TYPE ${X_}LINKER_VERSION ${X_}LINKER_FEATURES \
41a7d84af8SBryan Drewery		${X_}LINKER_FREEBSD_VERSION
4219fad655SBryan Drewery${X_}_ld_hash=	${${ld}}${MACHINE}${PATH}
4319fad655SBryan Drewery${X_}_ld_hash:=	${${X_}_ld_hash:hash}
4419fad655SBryan Drewery# Only import if none of the vars are set somehow else.
4519fad655SBryan Drewery_can_export=	yes
4619fad655SBryan Drewery.for var in ${_exported_vars}
4719fad655SBryan Drewery.if defined(${var})
4819fad655SBryan Drewery_can_export=	no
4919fad655SBryan Drewery.endif
5019fad655SBryan Drewery.endfor
5119fad655SBryan Drewery.if ${_can_export} == yes
5219fad655SBryan Drewery.for var in ${_exported_vars}
53*dc3c6ad6SAlex Richardson.if defined(${var}__${${X_}_ld_hash})
54*dc3c6ad6SAlex Richardson${var}=	${${var}__${${X_}_ld_hash}}
5519fad655SBryan Drewery.endif
5619fad655SBryan Drewery.endfor
5719fad655SBryan Drewery.endif
5819fad655SBryan Drewery
5908154765SBryan Drewery.if ${ld} == "LD" || (${ld} == "XLD" && ${XLD} != ${LD})
60ec39013eSBryan Drewery.if !defined(${X_}LINKER_TYPE) || !defined(${X_}LINKER_VERSION)
617ccdc068SBryan Drewery_ld_version!=	(${${ld}} --version || echo none) | sed -n 1p
62125902b6SBryan Drewery.if ${_ld_version} == "none"
63d786d510SEd Maste.warning Unable to determine linker type from ${ld}=${${ld}}
64125902b6SBryan Drewery.endif
65125902b6SBryan Drewery.if ${_ld_version:[1..2]} == "GNU ld"
6683cff1f8SEd Maste${X_}LINKER_TYPE=	bfd
67a7d84af8SBryan Drewery${X_}LINKER_FREEBSD_VERSION=	0
6889bf31fdSBryan Drewery_v=	${_ld_version:M[1-9].[0-9]*:[1]}
69125902b6SBryan Drewery.elif ${_ld_version:[1]} == "LLD"
7008154765SBryan Drewery${X_}LINKER_TYPE=	lld
71125902b6SBryan Drewery_v=	${_ld_version:[2]}
72a7d84af8SBryan Drewery${X_}LINKER_FREEBSD_VERSION!= \
73a7d84af8SBryan Drewery	${${ld}} --version | \
74a7d84af8SBryan Drewery	awk '$$3 ~ /FreeBSD/ {print substr($$4, 1, length($$4)-1)}'
75125902b6SBryan Drewery.else
76d786d510SEd Maste.warning Unknown linker from ${ld}=${${ld}}: ${_ld_version}, defaulting to bfd
77d786d510SEd Maste${X_}LINKER_TYPE=	bfd
78d786d510SEd Maste_v=	2.17.50
79125902b6SBryan Drewery.endif
8008154765SBryan Drewery${X_}LINKER_VERSION!=	echo "${_v:M[1-9].[0-9]*}" | \
8108154765SBryan Drewery			  awk -F. '{print $$1 * 10000 + $$2 * 100 + $$3;}'
8208154765SBryan Drewery.undef _ld_version
8308154765SBryan Drewery.undef _v
84cdd89b98SEd Maste${X_}LINKER_FEATURES=
85cdd89b98SEd Maste.if ${${X_}LINKER_TYPE} != "bfd" || ${${X_}LINKER_VERSION} > 21750
86cdd89b98SEd Maste${X_}LINKER_FEATURES+=	build-id
87ff8f1e83SEd Maste${X_}LINKER_FEATURES+=	ifunc
88cdd89b98SEd Maste.endif
8948164080SEd Maste.if ${${X_}LINKER_TYPE} == "lld" && ${${X_}LINKER_VERSION} >= 60000
9048164080SEd Maste${X_}LINKER_FEATURES+=	retpoline
9148164080SEd Maste.endif
92ec39013eSBryan Drewery.endif
936f81bd02SBryan Drewery.else
946f81bd02SBryan Drewery# Use LD's values
956f81bd02SBryan DreweryX_LINKER_TYPE=		${LINKER_TYPE}
966f81bd02SBryan DreweryX_LINKER_VERSION=	${LINKER_VERSION}
97cdd89b98SEd MasteX_LINKER_FEATURES=	${LINKER_FEATURES}
98a7d84af8SBryan DreweryX_LINKER_FREEBSD_VERSION= ${LINKER_FREEBSD_VERSION}
9908154765SBryan Drewery.endif	# ${ld} == "LD" || (${ld} == "XLD" && ${XLD} != ${LD})
10008154765SBryan Drewery
10119fad655SBryan Drewery# Export the values so sub-makes don't have to look them up again, using the
10219fad655SBryan Drewery# hash key computed above.
10319fad655SBryan Drewery.for var in ${_exported_vars}
104*dc3c6ad6SAlex Richardson${var}__${${X_}_ld_hash}:=	${${var}}
105*dc3c6ad6SAlex Richardson.export-env ${var}__${${X_}_ld_hash}
106*dc3c6ad6SAlex Richardson.undef ${var}__${${X_}_ld_hash}
10719fad655SBryan Drewery.endfor
10819fad655SBryan Drewery
10908154765SBryan Drewery.endif	# ${ld} == "LD" || !empty(XLD)
11008154765SBryan Drewery.endfor	# .for ld in LD XLD
11108154765SBryan Drewery
112125902b6SBryan Drewery
113125902b6SBryan Drewery.endif	# !target(__<bsd.linker.mk>__)
114