xref: /freebsd/share/mk/bsd.linker.mk (revision 99ac8154ffa847ae709a193d014bd66d8cdfabb9)
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
16cdd89b98SEd Maste#
1708154765SBryan Drewery# These variables with an X_ prefix will also be provided if XLD is set.
1808154765SBryan Drewery#
19125902b6SBryan Drewery# This file may be included multiple times, but only has effect the first time.
20125902b6SBryan Drewery#
21125902b6SBryan Drewery
22125902b6SBryan Drewery.if !target(__<bsd.linker.mk>__)
23125902b6SBryan Drewery__<bsd.linker.mk>__:
24125902b6SBryan Drewery
2508154765SBryan Drewery.for ld X_ in LD $${_empty_var_} XLD X_
2608154765SBryan Drewery.if ${ld} == "LD" || !empty(XLD)
2719fad655SBryan Drewery# Try to import LINKER_TYPE and LINKER_VERSION from parent make.
2819fad655SBryan Drewery# The value is only used/exported for the same environment that impacts
2919fad655SBryan Drewery# LD and LINKER_* settings here.
30cdd89b98SEd Maste_exported_vars=	${X_}LINKER_TYPE ${X_}LINKER_VERSION ${X_}LINKER_FEATURES
3119fad655SBryan Drewery${X_}_ld_hash=	${${ld}}${MACHINE}${PATH}
3219fad655SBryan Drewery${X_}_ld_hash:=	${${X_}_ld_hash:hash}
3319fad655SBryan Drewery# Only import if none of the vars are set somehow else.
3419fad655SBryan Drewery_can_export=	yes
3519fad655SBryan Drewery.for var in ${_exported_vars}
3619fad655SBryan Drewery.if defined(${var})
3719fad655SBryan Drewery_can_export=	no
3819fad655SBryan Drewery.endif
3919fad655SBryan Drewery.endfor
4019fad655SBryan Drewery.if ${_can_export} == yes
4119fad655SBryan Drewery.for var in ${_exported_vars}
4219fad655SBryan Drewery.if defined(${var}.${${X_}_ld_hash})
4319fad655SBryan Drewery${var}=	${${var}.${${X_}_ld_hash}}
4419fad655SBryan Drewery.endif
4519fad655SBryan Drewery.endfor
4619fad655SBryan Drewery.endif
4719fad655SBryan Drewery
4808154765SBryan Drewery.if ${ld} == "LD" || (${ld} == "XLD" && ${XLD} != ${LD})
49ec39013eSBryan Drewery.if !defined(${X_}LINKER_TYPE) || !defined(${X_}LINKER_VERSION)
50d786d510SEd Maste_ld_version!=	(${${ld}} --version || echo none) | head -n 1
51125902b6SBryan Drewery.if ${_ld_version} == "none"
52d786d510SEd Maste.warning Unable to determine linker type from ${ld}=${${ld}}
53125902b6SBryan Drewery.endif
54125902b6SBryan Drewery.if ${_ld_version:[1..2]} == "GNU ld"
5583cff1f8SEd Maste${X_}LINKER_TYPE=	bfd
5689bf31fdSBryan Drewery_v=	${_ld_version:M[1-9].[0-9]*:[1]}
57125902b6SBryan Drewery.elif ${_ld_version:[1]} == "LLD"
5808154765SBryan Drewery${X_}LINKER_TYPE=	lld
59125902b6SBryan Drewery_v=	${_ld_version:[2]}
60125902b6SBryan Drewery.else
61d786d510SEd Maste.warning Unknown linker from ${ld}=${${ld}}: ${_ld_version}, defaulting to bfd
62d786d510SEd Maste${X_}LINKER_TYPE=	bfd
63d786d510SEd Maste_v=	2.17.50
64125902b6SBryan Drewery.endif
6508154765SBryan Drewery${X_}LINKER_VERSION!=	echo "${_v:M[1-9].[0-9]*}" | \
6608154765SBryan Drewery			  awk -F. '{print $$1 * 10000 + $$2 * 100 + $$3;}'
6708154765SBryan Drewery.undef _ld_version
6808154765SBryan Drewery.undef _v
69cdd89b98SEd Maste${X_}LINKER_FEATURES=
70cdd89b98SEd Maste.if ${${X_}LINKER_TYPE} != "bfd" || ${${X_}LINKER_VERSION} > 21750
71cdd89b98SEd Maste${X_}LINKER_FEATURES+=	build-id
72cdd89b98SEd Maste.endif
73*99ac8154SKonstantin Belousov.if ${${X_}LINKER_TYPE} == "bfd"
74*99ac8154SKonstantin Belousov${X_}LINKER_FEATURES+=	filter
75*99ac8154SKonstantin Belousov.endif
76ec39013eSBryan Drewery.endif
776f81bd02SBryan Drewery.else
786f81bd02SBryan Drewery# Use LD's values
796f81bd02SBryan DreweryX_LINKER_TYPE=		${LINKER_TYPE}
806f81bd02SBryan DreweryX_LINKER_VERSION=	${LINKER_VERSION}
81cdd89b98SEd MasteX_LINKER_FEATURES=	${LINKER_FEATURES}
8208154765SBryan Drewery.endif	# ${ld} == "LD" || (${ld} == "XLD" && ${XLD} != ${LD})
8308154765SBryan Drewery
8419fad655SBryan Drewery# Export the values so sub-makes don't have to look them up again, using the
8519fad655SBryan Drewery# hash key computed above.
8619fad655SBryan Drewery.for var in ${_exported_vars}
8719fad655SBryan Drewery${var}.${${X_}_ld_hash}:=	${${var}}
8819fad655SBryan Drewery.export-env ${var}.${${X_}_ld_hash}
8919fad655SBryan Drewery.undef ${var}.${${X_}_ld_hash}
9019fad655SBryan Drewery.endfor
9119fad655SBryan Drewery
9208154765SBryan Drewery.endif	# ${ld} == "LD" || !empty(XLD)
9308154765SBryan Drewery.endfor	# .for ld in LD XLD
9408154765SBryan Drewery
95125902b6SBryan Drewery
96125902b6SBryan Drewery.endif	# !target(__<bsd.linker.mk>__)
97