1
2# Use the following command to build local debug version of dynamic
3# linker:
4# make DEBUG_FLAGS=-g WITHOUT_TESTS=yes all
5
6RTLD_ELF_DIR:=	${.PARSEDIR}
7
8.include <src.opts.mk>
9PACKAGE=	clibs
10MK_PIE=		no # Always position independent using local rules
11# Not compatible with sanitizer instrumentation or SSP.
12MK_ASAN=	no
13MK_SSP=		no
14MK_UBSAN=	no
15
16.include <bsd.compat.pre.mk>
17
18# SSP forced off already implies FORTIFY_SOURCE=0, but we must make sure that
19# one cannot turn it back on.
20FORTIFY_SOURCE=	0
21
22.if !defined(NEED_COMPAT)
23CONFS=		libmap.conf
24.endif
25PROG?=		ld-elf.so.1
26.for _libcompat in ${_ALL_libcompats}
27.if ${PROG:M*ld-elf${_libcompat}[-.]*} != ""
28TAGS+=		lib${_libcompat}
29.endif
30.endfor
31SRCS= \
32	crtbrand.S \
33	rtld_start.S \
34	reloc.c \
35	rtld.c \
36	rtld_lock.c \
37	rtld_malloc.c \
38	rtld_printf.c \
39	map_object.c \
40	xmalloc.c \
41	debug.c \
42	libmap.c
43MAN?=		rtld.1
44ACFLAGS+=	-DLOCORE
45CFLAGS+=	-Wall -DIN_RTLD -ffreestanding
46CFLAGS+=	-I${SRCTOP}/lib/csu/common
47.if exists(${RTLD_ELF_DIR}/${MACHINE_ARCH:S/powerpc64le/powerpc64/})
48RTLD_ARCH=	${MACHINE_ARCH:S/powerpc64le/powerpc64/}
49.else
50RTLD_ARCH=	${MACHINE_CPUARCH}
51.endif
52CFLAGS+=	-I${RTLD_ELF_DIR}/${RTLD_ARCH} -I${RTLD_ELF_DIR}
53
54NO_WCAST_ALIGN=	yes
55INSTALLFLAGS=	-C -b
56PRECIOUSPROG=
57BINDIR=		/libexec
58SYMLINKS=	../..${BINDIR}/${PROG} ${LIBEXECDIR}/${PROG}
59MLINKS?=	rtld.1 ld-elf.so.1.1 \
60		rtld.1 ld.so.1
61
62CFLAGS+=	-fpic -DPIC $(DEBUG)
63
64LDFLAGS+=	-shared -Wl,-Bsymbolic -Wl,-z,defs -nostdlib -e ${RTLD_ENTRY}
65# Pull in the dependencies that we use from libc
66.include "rtld-libc/Makefile.inc"
67
68VERSION_DEF=	${LIBCSRCDIR}/Versions.def
69SYMBOL_MAPS=	${RTLD_ELF_DIR}/Symbol.map
70VERSION_MAP=	Version.map
71LDFLAGS+=	-Wl,--version-script=${VERSION_MAP}
72
73.if exists(${RTLD_ELF_DIR}/${RTLD_ARCH}/Symbol.map)
74SYMBOL_MAPS+=	${RTLD_ELF_DIR}/${RTLD_ARCH}/Symbol.map
75.endif
76
77.sinclude "${RTLD_ELF_DIR}/${RTLD_ARCH}/Makefile.inc"
78RTLD_ENTRY?=	.rtld_start
79
80# Always produce the map file so that may be inspected to confirm
81# undesired code is not linked from libsys/libc.
82MAPFILE=	ld-elf.so.1.map
83LDFLAGS+=	-Wl,-Map=${MAPFILE} -Wl,--cref
84CLEANFILES+=	${MAPFILE}
85
86afterbuild:
87	@if grep __libsys_interposing ${MAPFILE} >/dev/null ; then \
88		echo "libsys_interposing leaked" 1>&2 ; \
89		exit 1 ; \
90	fi
91	@if grep __libc_interposing ${MAPFILE} >/dev/null ; then \
92		echo "libc_interposing leaked" 1>&2 ; \
93		exit 1 ; \
94	fi
95	@if grep xlocale ${MAPFILE} >/dev/null ; then \
96		echo "xlocale leaked" 1>&2 ; \
97		exit 1 ; \
98	fi
99	@if grep fprintf ${MAPFILE} >/dev/null ; then \
100		echo "stdio leaked" 1>&2 ; \
101		exit 1 ; \
102	fi
103
104
105# Since moving rtld-elf to /libexec, we need to create a symlink.
106# Fixup the existing binary that's there so we can symlink over it.
107beforeinstall:
108.if exists(${DESTDIR}/usr/libexec/${PROG}) && ${MK_STAGING} == "no"
109	-chflags -h noschg ${DESTDIR}/usr/libexec/${PROG}
110.endif
111
112.PATH: ${RTLD_ELF_DIR}/${RTLD_ARCH} ${SRCTOP}/lib/csu/common
113
114.if ${.CURDIR} == ${RTLD_ELF_DIR}
115HAS_TESTS=
116SUBDIR.${MK_TESTS}+= tests
117.endif
118
119# Some of the required math functions (div & mod) are implemented in
120# libcompiler_rt on some architectures.
121LIBADD+=	compiler_rt
122
123.include <bsd.prog.mk>
124${PROG_FULL}:	${VERSION_MAP}
125.include <bsd.symver.mk>
126
127.if ${COMPILER_TYPE} == "gcc"
128# GCC warns about redeclarations even though they have __exported
129# and are therefore not identical to the ones from the system headers.
130CFLAGS+=	-Wno-redundant-decls
131.endif
132