xref: /freebsd/share/mk/bsd.linker.mk (revision f8147dad44f1036160a413bb00b4e8b41f7a4a84)
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}
44489377c0SAlex Richardson# Only import if none of the vars are set differently somehow else.
4519fad655SBryan Drewery_can_export=	yes
4619fad655SBryan Drewery.for var in ${_exported_vars}
47489377c0SAlex Richardson.if defined(${var}) && (!defined(${var}__${${X_}_ld_hash}) || ${${var}__${${X_}_ld_hash}} != ${${var}})
48489377c0SAlex Richardson.if defined(${var}__${${X_}_ld_hash})
49489377c0SAlex Richardson.info "Cannot import ${X_}LINKER variables since cached ${var} is different: ${${var}__${${X_}_ld_hash}} != ${${var}}"
50489377c0SAlex Richardson.endif
5119fad655SBryan Drewery_can_export=	no
5219fad655SBryan Drewery.endif
5319fad655SBryan Drewery.endfor
5419fad655SBryan Drewery.if ${_can_export} == yes
5519fad655SBryan Drewery.for var in ${_exported_vars}
56dc3c6ad6SAlex Richardson.if defined(${var}__${${X_}_ld_hash})
57dc3c6ad6SAlex Richardson${var}=	${${var}__${${X_}_ld_hash}}
5819fad655SBryan Drewery.endif
5919fad655SBryan Drewery.endfor
6019fad655SBryan Drewery.endif
6119fad655SBryan Drewery
6208154765SBryan Drewery.if ${ld} == "LD" || (${ld} == "XLD" && ${XLD} != ${LD})
63ec39013eSBryan Drewery.if !defined(${X_}LINKER_TYPE) || !defined(${X_}LINKER_VERSION)
6401c4f3a7SAlex Richardson_ld_version!=	(${${ld}} -v 2>&1 || echo none) | sed -n 1p
65125902b6SBryan Drewery.if ${_ld_version} == "none"
66d786d510SEd Maste.warning Unable to determine linker type from ${ld}=${${ld}}
67125902b6SBryan Drewery.endif
68125902b6SBryan Drewery.if ${_ld_version:[1..2]} == "GNU ld"
6983cff1f8SEd Maste${X_}LINKER_TYPE=	bfd
70a7d84af8SBryan Drewery${X_}LINKER_FREEBSD_VERSION=	0
71d54b0ffeSDimitry Andric_v=	${_ld_version:M[1-9]*.[0-9]*:[1]}
72*f8147dadSAlex Richardson.elif ${_ld_version:MLLD}
73*f8147dadSAlex Richardson# Strip any leading PACKAGE_VENDOR string (e.g. "Homebrew")
74*f8147dadSAlex Richardson_ld_version:=${_ld_version:[*]:C/^.* LLD /LLD /:[@]}
7508154765SBryan Drewery${X_}LINKER_TYPE=	lld
76125902b6SBryan Drewery_v=	${_ld_version:[2]}
77c246b393SDimitry Andric.if ${_ld_version:[3]} == "(FreeBSD"
789caf7b36SDimitry Andric${X_}LINKER_FREEBSD_VERSION:=	${_ld_version:[4]:C/.*-([^-]*)\)/\1/}
79c246b393SDimitry Andric.else
80c246b393SDimitry Andric${X_}LINKER_FREEBSD_VERSION=	0
81c246b393SDimitry Andric.endif
8201c4f3a7SAlex Richardson.elif ${_ld_version:[1]} == "@(\#)PROGRAM:ld"
8301c4f3a7SAlex Richardson# bootstrap linker on MacOS
8401c4f3a7SAlex Richardson${X_}LINKER_TYPE=        mac
8501c4f3a7SAlex Richardson_v=        ${_ld_version:[2]:S/PROJECT:ld64-//}
8601c4f3a7SAlex Richardson# Convert version 409.12 to 409.12.0 so that the echo + awk below works
8701c4f3a7SAlex Richardson.if empty(_v:M[1-9]*.[0-9]*.[0-9]*) && !empty(_v:M[1-9]*.[0-9]*)
8801c4f3a7SAlex Richardson_v:=${_v}.0
8901c4f3a7SAlex Richardson.else
9001c4f3a7SAlex Richardson# Some versions do not contain a minor version so we need to append .0.0 there
9101c4f3a7SAlex Richardson_v:=${_v}.0.0
9201c4f3a7SAlex Richardson.endif
93125902b6SBryan Drewery.else
94d786d510SEd Maste.warning Unknown linker from ${ld}=${${ld}}: ${_ld_version}, defaulting to bfd
95d786d510SEd Maste${X_}LINKER_TYPE=	bfd
96d786d510SEd Maste_v=	2.17.50
97125902b6SBryan Drewery.endif
98d54b0ffeSDimitry Andric${X_}LINKER_VERSION!=	echo "${_v:M[1-9]*.[0-9]*}" | \
9908154765SBryan Drewery			  awk -F. '{print $$1 * 10000 + $$2 * 100 + $$3;}'
10008154765SBryan Drewery.undef _ld_version
10108154765SBryan Drewery.undef _v
102cdd89b98SEd Maste${X_}LINKER_FEATURES=
103cdd89b98SEd Maste.if ${${X_}LINKER_TYPE} != "bfd" || ${${X_}LINKER_VERSION} > 21750
104cdd89b98SEd Maste${X_}LINKER_FEATURES+=	build-id
105ff8f1e83SEd Maste${X_}LINKER_FEATURES+=	ifunc
106cdd89b98SEd Maste.endif
1070869dcebSJohn Baldwin.if ${${X_}LINKER_TYPE} == "bfd" && ${${X_}LINKER_VERSION} > 21750
1080869dcebSJohn Baldwin${X_}LINKER_FEATURES+=	riscv-relaxations
1090869dcebSJohn Baldwin.endif
11048164080SEd Maste.if ${${X_}LINKER_TYPE} == "lld" && ${${X_}LINKER_VERSION} >= 60000
11148164080SEd Maste${X_}LINKER_FEATURES+=	retpoline
11248164080SEd Maste.endif
11391b31c10SAlex Richardson.if ${${X_}LINKER_TYPE} == "lld" && ${${X_}LINKER_VERSION} >= 90000
11491b31c10SAlex Richardson${X_}LINKER_FEATURES+=	ifunc-noplt
11591b31c10SAlex Richardson.endif
116ec39013eSBryan Drewery.endif
1176f81bd02SBryan Drewery.else
1186f81bd02SBryan Drewery# Use LD's values
1196f81bd02SBryan DreweryX_LINKER_TYPE=		${LINKER_TYPE}
1206f81bd02SBryan DreweryX_LINKER_VERSION=	${LINKER_VERSION}
121cdd89b98SEd MasteX_LINKER_FEATURES=	${LINKER_FEATURES}
122a7d84af8SBryan DreweryX_LINKER_FREEBSD_VERSION= ${LINKER_FREEBSD_VERSION}
12308154765SBryan Drewery.endif	# ${ld} == "LD" || (${ld} == "XLD" && ${XLD} != ${LD})
12408154765SBryan Drewery
12519fad655SBryan Drewery# Export the values so sub-makes don't have to look them up again, using the
12619fad655SBryan Drewery# hash key computed above.
12719fad655SBryan Drewery.for var in ${_exported_vars}
128dc3c6ad6SAlex Richardson${var}__${${X_}_ld_hash}:=	${${var}}
129dc3c6ad6SAlex Richardson.export-env ${var}__${${X_}_ld_hash}
130dc3c6ad6SAlex Richardson.undef ${var}__${${X_}_ld_hash}
13119fad655SBryan Drewery.endfor
13219fad655SBryan Drewery
13308154765SBryan Drewery.endif	# ${ld} == "LD" || !empty(XLD)
13408154765SBryan Drewery.endfor	# .for ld in LD XLD
13508154765SBryan Drewery
136125902b6SBryan Drewery
137125902b6SBryan Drewery.endif	# !target(__<bsd.linker.mk>__)
138