1# $OpenBSD: Makefile,v 1.80 2019/12/02 17:19:25 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=__syscall close exit fstat getdents getentropy getthrid issetugid \ 31 mprotect munmap msyscall open pledge read __realpath sendsyslog \ 32 __set_tcb sysctl thrkill utrace write 33 34GEN_PREFIX=\t.file "${@:R}.c"\n\#include "SYS.h" 35.for _i in ${syscall} 36OBJS+=dl_${_i}.o 37dl_${_i}.o: SYS.h 38 printf '${GEN_PREFIX}\nDL_SYSCALL(${_i})\n' | \ 39 ${CC} ${DEBUG} -c -fPIC ${CFLAGS:M-[ID]*} ${CFLAGS:M-pipe} ${AINC} -P \ 40 -x assembler-with-cpp - ${DFLAGS} -MF ${.TARGET:R}.d -o ${.TARGET} 41.endfor 42 43.if (${MACHINE_ARCH} == "i386") 44SRCS+= library_mquery.c 45.else 46SRCS+= library.c 47.endif 48 49.include "${.CURDIR}/${MACHINE_CPU}/Makefile.inc" 50.PATH: ${.CURDIR}/${MACHINE_CPU} 51 52.ifdef LD_SCRIPT 53 ELF_LDFLAGS+=-T ${LD_SCRIPT} 54 CFLAGS += -DDO_CLEAN_BOOT 55.endif 56 57DEBUG?= -g 58CFLAGS += -fno-builtin 59CFLAGS += -Wall -Werror 60CFLAGS += -I${.CURDIR} -I${.CURDIR}/${MACHINE_CPU} \ 61 -D'DEF_WEAK(x)=asm("")' -D'DEF_STRONG(x)=asm("")' \ 62 -Dstrsep=_dl_strsep -Dstrlcat=_dl_strlcat -Dstrlen=_dl_strlen 63CDIAGFLAGS= 64INSTALL_STRIP= 65 66ELF_LDFLAGS+=--shared -Bsymbolic --no-undefined 67 68.ifdef RELATIVE_RELOC 69CHECK_LDSO=c() { \ 70 ! readelf -Wr $$1 | \ 71 egrep -qv '^($$|[ R])| (${RELATIVE_RELOC}) '; \ 72 }; c 73.endif 74 75test_prog= test-$(PROG) 76CLEANFILES+= test-$(PROG) 77candidate= $(PROG).test 78CLEANFILES+= ${candidate} 79 80$(test_prog): 81 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' | \ 82 $(CC) -P -x c - -Wl,-dynamic-linker,./$(candidate) -o $@ -lpthread 83 84$(PROG): $(test_prog) ${VERSION_SCRIPT} $(OBJS) ${LD_SCRIPT} 85.if defined(SYSPATCH_PATH) 86 $(LD) -e _dl_start $(ELF_LDFLAGS) -o $(candidate) \ 87 `readelf -Ws ${SYSPATCH_PATH}/usr/libexec/${.TARGET} | \ 88 awk '/ FILE/{sub(".*/", "", $$NF); gsub(/\..*/, ".o", $$NF); print $$NF}' | \ 89 awk '!x[$$0]++'` 90.else 91 $(LD) -e _dl_start $(ELF_LDFLAGS) -o $(candidate) \ 92 `echo ${OBJS} | tr " " "\n" | sort -R` 93.endif 94.ifdef CHECK_LDSO 95 ${CHECK_LDSO} $(candidate) 96.endif 97.ifndef CROSSDIR 98 ulimit -c 0; [ "`${.OBJDIR}/$(test_prog) ok`" = "${.OBJDIR}/$(test_prog): ok!" ] 99.endif 100 cp $(candidate) $@ 101.endif 102 103.include <bsd.prog.mk> 104 105${BOOT:R}.o: ${BOOT} 106 ${COMPILE.c} -fno-stack-protector ${.IMPSRC} 107 108CLEANFILES+= ld.so.a 109 110all: ld.so.a 111 112ld.so.a: ${OBJS} ${.CURDIR}/Symbols.map ${test_prog} ${LD_SCRIPT} 113 ar cqD $@ $? 114 115afterinstall: ld.so.a 116 install -d -o root -g wheel -m 755 \ 117 ${DESTDIR}/usr/share/relink/usr/libexec 118 install -o ${BINOWN} -g ${BINGRP} -m ${NONBINMODE} \ 119 ld.so.a ${DESTDIR}/usr/share/relink/usr/libexec/ld.so.a 120