# Makefile for uClibc
#
# Copyright (C) 2000-2008 Erik Andersen <andersen@uclibc.org>
#
# Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
#

subdirs += ldso/ldso/$(TARGET_ARCH)

CFLAGS-rtld := -DNOT_IN_libc -DIS_IN_rtld $(SSP_DISABLE_FLAGS)

ifneq ($(TARGET_ARCH),arc)
# This stuff will not work with -fomit-frame-pointer
CFLAGS-rtld += -fno-omit-frame-pointer
endif

ifeq ($(TARGET_ARCH),arm)
# This stuff will not work with -funwind-tables / -fasynchronous-unwind-tables
CFLAGS-rtld += -fno-unwind-tables -fno-asynchronous-unwind-tables
endif

CFLAGS-rtld += -I$(top_srcdir)ldso/ldso/$(TARGET_ARCH) -I$(top_srcdir)ldso/include -I$(top_srcdir)ldso/ldso
CFLAGS-rtld += -DUCLIBC_RUNTIME_PREFIX=\"$(RUNTIME_PREFIX)\" -DUCLIBC_LDSO=\"$(UCLIBC_LDSO)\"

# Not really much point in including debugging info, since gdb
# can't really debug ldso, since gdb requires help from ldso to
# debug things....
# On arm, gcc-4.3.x onwards -Os emits calls to libgcc, which calls _div0,
# which tries to call raise(). And raise comes from libc so a catch 22.
# Using -O2 instead. We could have use -fno-early-inlining with -Os too.
CFLAGS-$(DODEBUG)-ldso/ldso := -O2 -g

CFLAGS-ldso.c := -DLDSO_ELFINTERP=\"$(TARGET_ARCH)/elfinterp.c\"

# Search non-default multilib directories
ifneq ($(MULTILIB_DIR),lib)
CFLAGS-ldso.c	+= -DLDSO_MULTILIB_DIR=\"$(MULTILIB_DIR)\"
endif

ifeq ($(TARGET_ARCH),arc)
$(eval $(call check-gcc-var,-mno-long-calls))
CFLAGS-ldso.c += $(CFLAGS_-mno-long-calls)
endif

LDFLAGS-$(UCLIBC_FORMAT_DSBT_ELF)-$(UCLIBC_LDSO_NAME).so := -Wl,--dsbt-index=1
ifneq ($(SUPPORT_LD_DEBUG),y)
LDFLAGS-$(UCLIBC_LDSO_NAME).so := $(LDFLAGS)
else
LDFLAGS-$(UCLIBC_LDSO_NAME).so := $(LDFLAGS_NOSTRIP) -Wl,-z,defs
endif
LDFLAGS-$(UCLIBC_LDSO_NAME).so += -Wl,-e,$(SYMBOL_PREFIX)_start \
	-Wl,-z,now -Wl,-Bsymbolic \
	-Wl,--export-dynamic $(CFLAG_-Wl--sort-common) -Wl,--no-undefined

ifneq ($(DODEBUG),y)
	LDFLAGS-$(UCLIBC_LDSO_NAME).so +=  -Wl,--discard-locals $(CFLAG_-Wl--discard-all)
endif

LDFLAGS-$(LDSO_PRELINK_SUPPORT)-$(UCLIBC_LDSO_NAME).so += -Wl,-defsym=$(SYMBOL_PREFIX)_begin=0

ldso_FULL_NAME := $(UCLIBC_LDSO_NAME)-$(VERSION).so

$(UCLIBC_LDSO_NAME)_DIR := $(top_srcdir)ldso/ldso
$(UCLIBC_LDSO_NAME)_OUT := $(top_builddir)ldso/ldso

$(UCLIBC_LDSO_NAME)_CSRC := $($(UCLIBC_LDSO_NAME)_DIR)/ldso.c $($(UCLIBC_LDSO_NAME)_DIR)/dl-vdso.c
# prerequesites
$($(UCLIBC_LDSO_NAME)_OUT)/ldso.o $($(UCLIBC_LDSO_NAME)_OUT)/ldso.oS: \
	$($(UCLIBC_LDSO_NAME)_DIR)/dl-debug.c \
	$($(UCLIBC_LDSO_NAME)_DIR)/dl-startup.c \
	$($(UCLIBC_LDSO_NAME)_DIR)/dl-array.c \
	$($(UCLIBC_LDSO_NAME)_DIR)/dl-hash.c \
	$($(UCLIBC_LDSO_NAME)_DIR)/dl-elf.c \
	$($(UCLIBC_LDSO_NAME)_DIR)/dl-vdso.c
$(UCLIBC_LDSO_NAME)_COBJ := $(patsubst $($(UCLIBC_LDSO_NAME)_DIR)/%.c,$($(UCLIBC_LDSO_NAME)_OUT)/%.o,$($(UCLIBC_LDSO_NAME)_CSRC))

$(UCLIBC_LDSO_NAME)_SSRC := $(wildcard $($(UCLIBC_LDSO_NAME)_DIR)/$(TARGET_ARCH)/*.S)
$(UCLIBC_LDSO_NAME)_SOBJ := $(patsubst $($(UCLIBC_LDSO_NAME)_DIR)/$(TARGET_ARCH)/%.S,$($(UCLIBC_LDSO_NAME)_OUT)/$(TARGET_ARCH)/%.o,$($(UCLIBC_LDSO_NAME)_SSRC))

$(UCLIBC_LDSO_NAME)_OBJS := $($(UCLIBC_LDSO_NAME)_COBJ) $($(UCLIBC_LDSO_NAME)_SOBJ)

ldso-y := $($(UCLIBC_LDSO_NAME)_OBJS:.o=.oS)

lib-so-y += $(ldso)
objclean-y += CLEAN_ldso/ldso

$(ldso): $(ldso:.$(ABI_VERSION)=)
$(ldso:.$(ABI_VERSION)=): | $(top_builddir)lib
$(ldso:.$(ABI_VERSION)=): $($(UCLIBC_LDSO_NAME)_OUT)/$(UCLIBC_LDSO_NAME)_so.a
	$(call link.so,$(ldso_FULL_NAME),$(ABI_VERSION))
	# link for backward compatibility
	(cd $(top_builddir)lib; ln -sf $(UCLIBC_LDSO_NAME).so.$(ABI_VERSION) $(UCLIBC_LDSO_NAME).so.0 )
ifeq ($(CONFIG_MIPS_NAN_2008),y)
	(cd $(top_builddir)lib; ln -sf $(UCLIBC_LDSO_NAME).so.$(ABI_VERSION) $(UCLIBC_LDSO_NAME)-mipsn8.so.0)
endif

$($(UCLIBC_LDSO_NAME)_OUT)/$(UCLIBC_LDSO_NAME)_so.a: $(ldso-y)
	$(Q)$(RM) $@
	$(do_ar)

CLEAN_ldso/ldso:
	$(do_rm) $(addprefix $($(UCLIBC_LDSO_NAME)_OUT)/,$(foreach e, o os oS a,$(foreach d, *. */*.,$(d)$(e))))
