xref: /freebsd/share/mk/bsd.linker.mk (revision ebacd8013fe5f7fdf9f6a5b286f6680dd2891036)
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 differently somehow else.
45_can_export=	yes
46.for var in ${_exported_vars}
47.if defined(${var}) && (!defined(${var}__${${X_}_ld_hash}) || ${${var}__${${X_}_ld_hash}} != ${${var}})
48.if defined(${var}__${${X_}_ld_hash})
49.info Cannot import ${X_}LINKER variables since cached ${var} is different: ${${var}__${${X_}_ld_hash}} != ${${var}}
50.endif
51_can_export=	no
52.endif
53.endfor
54.if ${_can_export} == yes
55.for var in ${_exported_vars}
56.if defined(${var}__${${X_}_ld_hash})
57${var}=	${${var}__${${X_}_ld_hash}}
58.endif
59.endfor
60.endif
61
62.if ${ld} == "LD" || (${ld} == "XLD" && ${XLD} != ${LD})
63.if !defined(${X_}LINKER_TYPE) || !defined(${X_}LINKER_VERSION)
64_ld_version!=	(${${ld}} -v 2>&1 || echo none) | sed -n 1p
65.if ${_ld_version} == "none"
66.warning Unable to determine linker type from ${ld}=${${ld}}
67.endif
68.if ${_ld_version:[1..2]} == "GNU ld"
69${X_}LINKER_TYPE=	bfd
70${X_}LINKER_FREEBSD_VERSION=	0
71_v=	${_ld_version:M[1-9]*.[0-9]*:[1]}
72.elif ${_ld_version:MLLD}
73# Strip any leading PACKAGE_VENDOR string (e.g. "Homebrew")
74_ld_version:=${_ld_version:[*]:C/^.* LLD /LLD /:[@]}
75${X_}LINKER_TYPE=	lld
76_v=	${_ld_version:[2]}
77.if ${_ld_version:[3]} == "(FreeBSD"
78${X_}LINKER_FREEBSD_VERSION:=	${_ld_version:[4]:C/.*-([^-]*)\)/\1/}
79.else
80${X_}LINKER_FREEBSD_VERSION=	0
81.endif
82.elif ${_ld_version:[1]} == "@(\#)PROGRAM:ld"
83# bootstrap linker on MacOS
84${X_}LINKER_TYPE=        mac
85_v=        ${_ld_version:[2]:S/PROJECT:ld64-//}
86# Convert version 409.12 to 409.12.0 so that the echo + awk below works
87.if empty(_v:M[1-9]*.[0-9]*.[0-9]*) && !empty(_v:M[1-9]*.[0-9]*)
88_v:=${_v}.0
89.else
90# Some versions do not contain a minor version so we need to append .0.0 there
91_v:=${_v}.0.0
92.endif
93.else
94.warning Unknown linker from ${ld}=${${ld}}: ${_ld_version}, defaulting to bfd
95${X_}LINKER_TYPE=	bfd
96_v=	2.17.50
97.endif
98${X_}LINKER_VERSION!=	echo "${_v:M[1-9]*.[0-9]*}" | \
99			  awk -F. '{print $$1 * 10000 + $$2 * 100 + $$3;}'
100.undef _ld_version
101.undef _v
102${X_}LINKER_FEATURES=
103.if ${${X_}LINKER_TYPE} != "bfd" || ${${X_}LINKER_VERSION} > 21750
104${X_}LINKER_FEATURES+=	build-id
105${X_}LINKER_FEATURES+=	ifunc
106.endif
107.if ${${X_}LINKER_TYPE} == "bfd" && ${${X_}LINKER_VERSION} > 21750
108${X_}LINKER_FEATURES+=	riscv-relaxations
109.endif
110.if ${${X_}LINKER_TYPE} == "lld" && ${${X_}LINKER_VERSION} >= 60000
111${X_}LINKER_FEATURES+=	retpoline
112.endif
113.if ${${X_}LINKER_TYPE} == "lld" && ${${X_}LINKER_VERSION} >= 90000
114${X_}LINKER_FEATURES+=	ifunc-noplt
115.endif
116.endif
117.else
118# Use LD's values
119X_LINKER_TYPE=		${LINKER_TYPE}
120X_LINKER_VERSION=	${LINKER_VERSION}
121X_LINKER_FEATURES=	${LINKER_FEATURES}
122X_LINKER_FREEBSD_VERSION= ${LINKER_FREEBSD_VERSION}
123.endif	# ${ld} == "LD" || (${ld} == "XLD" && ${XLD} != ${LD})
124
125# Export the values so sub-makes don't have to look them up again, using the
126# hash key computed above.
127.for var in ${_exported_vars}
128${var}__${${X_}_ld_hash}:=	${${var}}
129.export-env ${var}__${${X_}_ld_hash}
130.undef ${var}__${${X_}_ld_hash}
131.endfor
132
133.endif	# ${ld} == "LD" || !empty(XLD)
134.endfor	# .for ld in LD XLD
135
136
137.endif	# !target(__<bsd.linker.mk>__)
138