xref: /openbsd-src/libexec/ld.so/Makefile (revision f7413c6a41ec258d5b922a501ffe84b626e09079)
1#	$OpenBSD: Makefile,v 1.88 2024/04/05 13:51:47 deraadt Exp $
2
3SUBDIR=ldconfig ldd
4MAN=	ld.so.1
5
6.include <bsd.own.mk>
7
8.if defined(NOPIC)
9NOPROG=
10.else
11PROG=	ld.so
12BINMODE=444
13
14VERSION_SCRIPT=	${.CURDIR}/Symbols.map
15ELF_LDFLAGS+=--version-script=${VERSION_SCRIPT}
16
17VPATH=${.CURDIR}/../../lib/libc/string
18
19.if exists(${.CURDIR}/${MACHINE_CPU}/boot_md.c)
20BOOT = boot_md.c
21.else
22BOOT = boot.c
23.endif
24
25SRCS=	ldasm.S ${BOOT} loader.c resolve.c dlfcn.c dl_printf.c rtld_machine.c
26SRCS+=	path.c util.c sod.c strsep.c strtol.c dir.c library_subr.c
27SRCS+=	dl_uname.c dl_dirname.c strlcat.c strlen.c trace.c
28SRCS+=	malloc.c reallocarray.c tib.c ffs.c
29
30syscall=close exit fstat getdents getentropy getthrid issetugid kbind \
31	mimmutable mmap mprotect munmap open pledge \
32	pinsyscalls read __realpath sendsyslog __set_tcb sysctl thrkill \
33	utrace write
34
35.if (${MACHINE_ARCH} == "i386")
36syscall+=mquery
37SRCS+=	library_mquery.c
38.else
39SRCS+=	library.c
40.endif
41
42GEN_PREFIX=\t.file "${@:R}.c"\n\#include "SYS.h"
43.for _i in ${syscall}
44OBJS+=dl_${_i}.o
45dl_${_i}.o: SYS.h
46	printf '${GEN_PREFIX}\nDL_SYSCALL(${_i})\n' | \
47	    ${CC} ${DEBUG} -c -fPIC ${CFLAGS:M-[ID]*} ${CFLAGS:M-pipe} \
48	    ${AFLAGS} -P -x assembler-with-cpp - ${DFLAGS} -MF \
49	    ${.TARGET:R}.d -o ${.TARGET}
50.endfor
51
52.include "${.CURDIR}/${MACHINE_CPU}/Makefile.inc"
53.PATH:	${.CURDIR}/${MACHINE_CPU}
54
55.ifdef LD_SCRIPT
56 ELF_LDFLAGS+=-T ${LD_SCRIPT}
57 CFLAGS	+= -DDO_CLEAN_BOOT
58.endif
59
60DEBUG?=	-g
61CFLAGS += -fno-builtin
62CFLAGS += -Wall -Werror
63CFLAGS += -I${.CURDIR} -I${.CURDIR}/${MACHINE_CPU} \
64	-D'DEF_WEAK(x)=asm("")' -D'DEF_STRONG(x)=asm("")' \
65	-Dstrsep=_dl_strsep -Dstrlcat=_dl_strlcat -Dstrlen=_dl_strlen
66CDIAGFLAGS=
67INSTALL_STRIP=
68
69ELF_LDFLAGS+=--shared -Bsymbolic --no-undefined
70
71.ifdef	RELATIVE_RELOC
72CHECK_LDSO=c() {					\
73	! readelf -Wr $$1 |				\
74	  egrep -qv '^($$|[ R])| (${RELATIVE_RELOC}) ';	\
75	}; c
76.endif
77
78test_prog=	test-$(PROG)
79CLEANFILES+=	test-$(PROG)
80candidate=	$(PROG).test
81CLEANFILES+=	${candidate}
82
83$(test_prog):
84	printf '#include <stdio.h>\n#include <pthread.h>\nint main(int argc, char **argv){ pthread_attr_t attr; printf("%%s: ", argv[0]); pthread_attr_init(&attr); printf("%%s!\\n", argv[1] ? argv[1] : "foo"); }\n' | \
85	$(CC) -P -x c - -Wl,-dynamic-linker,./$(candidate) -o $@ -lpthread
86
87$(PROG): $(test_prog) ${VERSION_SCRIPT} $(OBJS) ${LD_SCRIPT}
88.if defined(SYSPATCH_PATH)
89	$(LD) -e _dl_start $(ELF_LDFLAGS) -o $(candidate) \
90            `readelf -Ws ${SYSPATCH_PATH}/usr/libexec/${.TARGET} | \
91            awk '/ FILE/{sub(".*/", "", $$NF); gsub(/\..*/, ".o", $$NF); print $$NF}' | \
92            awk '!x[$$0]++'`
93.else
94	$(LD) -e _dl_start $(ELF_LDFLAGS) -o $(candidate) \
95	    `echo ${OBJS} | tr " " "\n" | sort -R`
96.endif
97.ifdef	CHECK_LDSO
98	${CHECK_LDSO} $(candidate)
99.endif
100.ifndef CROSSDIR
101	ulimit -c 0; [ "`${.OBJDIR}/$(test_prog) ok`" = "${.OBJDIR}/$(test_prog): ok!" ]
102.endif
103	cp $(candidate) $@
104.endif
105
106.include <bsd.prog.mk>
107
108${BOOT:R}.o: ${BOOT}
109	${COMPILE.c} -fno-stack-protector ${.IMPSRC}
110
111CLEANFILES+=	ld.so.a
112
113all: ld.so.a
114
115ld.so.a: ${OBJS} ${.CURDIR}/Symbols.map ${test_prog} ${LD_SCRIPT}
116	ar cqD $@ $?
117
118afterinstall: ld.so.a
119	install -d -o root -g wheel -m 755 \
120	    ${DESTDIR}/usr/share/relink/usr/libexec
121	install -o ${BINOWN} -g ${BINGRP} -m ${NONBINMODE} \
122	    ld.so.a ${DESTDIR}/usr/share/relink/usr/libexec/ld.so.a
123