Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RTLD]Adapt build env to shared libs #177

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from
6 changes: 6 additions & 0 deletions Makefile.common
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ PREFIX_O := $(BUILD_DIR)/$(CURR_SUFFIX)

# target install paths, can be provided exterally
PREFIX_A ?= $(PREFIX_BUILD)/lib/
PREFIX_SO ?= $(PREFIX_A)
PREFIX_H ?= $(PREFIX_BUILD)/include/
PREFIX_PROG ?= $(PREFIX_BUILD)/prog/
PREFIX_PROG_STRIPPED ?= $(PREFIX_BUILD)/prog.stripped/
Expand All @@ -74,11 +75,16 @@ include $(MAKES_PATH)/funcs.mk

# provide template files' paths to external makes
binary.mk := $(MAKES_PATH)/binary.mk
binary-dyn.mk := $(MAKES_PATH)/binary-dyn.mk
static-lib.mk := $(MAKES_PATH)/static-lib.mk
shared-lib.mk := $(MAKES_PATH)/shared-lib.mk

# default path for the programs to be installed in rootfs
DEFAULT_INSTALL_PATH := /bin

# default path for the shared-libraries to be installed in rootfs
DEFAULT_INSTALL_PATH_SO := /usr/lib

# do not clean and build in parallel
ifneq ($(filter %clean,$(MAKECMDGOALS)),)
$(info cleaning targets, make parallelism disabled)
Expand Down
3 changes: 3 additions & 0 deletions build-core-armv7a7-imx6ull.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ if [ "$LIBPHOENIX_DEVEL_MODE" = "y" ]; then

b_log "Building libphoenix"
make -C "libphoenix" all install

b_log "Building dynamic linker"
make -C "phoenix-rtos-utils" rtld rtld-install
fi

b_log "Building phoenix-rtos-corelibs"
Expand Down
3 changes: 3 additions & 0 deletions build-core-armv7a9-zynq7000.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ if [ "$LIBPHOENIX_DEVEL_MODE" = "y" ]; then

b_log "Building libphoenix"
make -C "libphoenix" all install

b_log "Building dynamic linker"
make -C "phoenix-rtos-utils" rtld rtld-install
fi

b_log "Building unity"
Expand Down
3 changes: 3 additions & 0 deletions build-core-ia32-generic.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ if [ "$LIBPHOENIX_DEVEL_MODE" = "y" ]; then

b_log "Building libphoenix"
make -C "libphoenix" all install

b_log "Building dynamic linker"
make -C "phoenix-rtos-utils" rtld rtld-install
fi

b_log "Building phoenix-rtos-corelibs"
Expand Down
3 changes: 3 additions & 0 deletions build-core-riscv64-generic.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ if [ "$LIBPHOENIX_DEVEL_MODE" = "y" ]; then

b_log "Building libphoenix"
make -C "libphoenix" all install

b_log "Building dynamic linker"
make -C "phoenix-rtos-utils" rtld rtld-install
fi

b_log "Building phoenix-rtos-corelibs"
Expand Down
3 changes: 3 additions & 0 deletions build-core-riscv64-noelv.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ if [ "$LIBPHOENIX_DEVEL_MODE" = "y" ]; then

b_log "Building libphoenix"
make -C "libphoenix" all install

b_log "Building dynamic linker"
make -C "phoenix-rtos-utils" rtld rtld-install
fi

b_log "Building phoenix-rtos-corelibs"
Expand Down
3 changes: 3 additions & 0 deletions build-core-sparcv8leon-generic.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ if [ "$LIBPHOENIX_DEVEL_MODE" = "y" ]; then

b_log "Building libphoenix"
make -C "libphoenix" all install

b_log "Building dynamic linker"
make -C "phoenix-rtos-utils" rtld rtld-install
fi

b_log "Building phoenix-rtos-corelibs"
Expand Down
3 changes: 3 additions & 0 deletions build-core-sparcv8leon-gr712rc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ if [ "$LIBPHOENIX_DEVEL_MODE" = "y" ]; then

b_log "Building libphoenix"
make -C "libphoenix" all install

b_log "Building dynamic linker"
make -C "phoenix-rtos-utils" rtld rtld-install
fi

b_log "Building phoenix-rtos-corelibs"
Expand Down
25 changes: 25 additions & 0 deletions makes/binary-dyn.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Makefile rules for compiling and linking dynamically linked binary file
# supported external variables, besides those stated in binary.mk:
# - DEP_LIBS_SHARED - shared libraries from current repo needed to be compiled/installed before this component (shortcut for putting something in LIBS and DEPS)
# - LIBS_SHARED - names of the shared libs to link the binary against (without .so suffix)

ifeq (${HAVE_SHLIB},n)
$(warning "binary-dyn.mk called on target not supporting dynamic linking!")
endif

RESOLVED_LIBS_SHARED := $(patsubst lib%,-l%, $(DEP_LIBS_SHARED) $(LIBS_SHARED))

DEPS += $(DEP_LIBS_SHARED)

# Add shared libraries directory search path
LD_FLAGS_DYN := -L$(PREFIX_SO)

LOCAL_LDFLAGS += $(LD_FLAGS_DYN)
LOCAL_LDLIBS += $(RESOLVED_LIBS_SHARED)

DYNAMIC_BINARY := y

include $(binary.mk)

DEP_LIBS_SHARED :=
LIBS_SHARED :=
8 changes: 8 additions & 0 deletions makes/binary.mk
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ RESOLVED_LIBS += $(patsubst %,$(PREFIX_A)%.a, $(LIBS))
DEPS += $(DEP_LIBS)
$(OBJS.$(NAME)): | $(DEPS)

ifneq ($(DYNAMIC_BINARY), y)
# FIXME: remove once hostutils use binary-dyn.mk
ifneq ($(TARGET_FAMILY), host)
LOCAL_LDFLAGS += -static
endif
endif

# potentially custom CFLAGS/CXXFLAGS/LDFLAGS for compilation and linking
# add ABS_HEADERS_DIR to CFLAGS/CXXFLAGS as a first -I path to build always using local headers instead of installed ones
$(OBJS.$(NAME)): CFLAGS:=-I"$(ABS_HEADERS_DIR)" $(CFLAGS) $(LOCAL_CFLAGS)
Expand Down Expand Up @@ -126,3 +133,4 @@ LOCAL_CXXFLAGS :=
LOCAL_LDFLAGS :=
LOCAL_LDLIBS :=
LOCAL_INSTALL_PATH :=
undefine DYNAMIC_BINARY
6 changes: 6 additions & 0 deletions makes/check-env.mk
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,9 @@ ifeq ($(HAVE_MMU), n)
$(error "KERNEL_PHADDR is not set for NOMMU target, please check project configuration")
endif
endif

ifeq ($(HAVE_SHLIB), n)
ifeq ($(LIBPHOENIX_SHARED), y)
$(error "LIBPHOENIX_SHARED is set for target without shared lib support, please check project configuration")
endif
endif
144 changes: 144 additions & 0 deletions makes/shared-lib.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
# Makefile rules for compiling a shared library
# supported external variables:
# - NAME - component/target binary name
# - LOCAL_SRCS - list of source files relative to current makefile
# - SRCS - list of source files relative to project root
# - LOCAL_HEADERS - headers to be installed (relative to current makefile)
# - LOCAL_HEADERS_DIR - headers tree be installed (relative to current makefile) - default "include"
# - HEADERS - headers to be installed (relative to project root)
# - LOCAL_LDFLAGS - additional LDFLAGS for current component linking
#
# - DEPS - list of components from current repo to be completed before starting this one
# - DEP_LIBS_SHARED - shared libraries from current repo needed to be compiled/installed before this component (shortcut for putting something in LIBS and DEPS)
# - LIBS_SHARED - names of the shared libs to link the binary against (without .so suffix)
# - LOCAL_LDLIBS - additional LDLIBS for current component linking
# - LOCAL_CFLAGS - additional CFLAGS for current component compilation
# - LOCAL_CXXFLAGS - additional CXXFLAGS for current component compilation
#
# - SONAME - library soname, defaults to $(NAME).so, to disable set to 'nothing'
# - LOCAL_VERSION_SCRIPT - version script relative to current makefile
#
# - LOCAL_INSTALL_PATH - custom rootfs dir for the shared library to be installed (if not provided - DEFAULT_INSTALL_PATH_SO)

ifeq (${HAVE_SHLIB},n)
$(warning "shared-lib.mk called on target not supporting shared libraries!")
endif

# directory with current Makefile - relative to the repository root
# filter-out all Makefiles outside of TOPDIR
# WARNING: LOCAL_DIR computation would fail if any Makefile include would be done before including this file
# if necessary set LOCAL_DIR := $(call my-dir) at the beginning of the Makefile
ifeq ($(origin LOCAL_DIR), undefined)
CLIENT_MAKES := $(filter $(TOPDIR)/%,$(abspath $(MAKEFILE_LIST)))
LOCAL_DIR := $(patsubst $(TOPDIR)/%,%,$(dir $(lastword $(CLIENT_MAKES))))
endif

# external headers - by default "include" dir - to disable functionality set "LOCAL_HEADERS_DIR := nothing"
LOCAL_HEADERS_DIR ?= include
ABS_HEADERS_DIR := $(abspath ./$(LOCAL_DIR)/$(LOCAL_HEADERS_DIR))

SRCS += $(addprefix $(LOCAL_DIR), $(LOCAL_SRCS))
HEADERS += $(addprefix $(LOCAL_DIR), $(LOCAL_HEADERS))

# removing all files with unsupported extensions
SRCS := $(filter $(LANGUAGE_EXTENSIONS), $(SRCS))

# linking prerequisites
OBJS.$(NAME) := $(patsubst %,$(PREFIX_O)%.o,$(basename $(SRCS)))

# Add libs
RESOLVED_LIBS_SHARED := $(patsubst lib%,-l%, $(DEP_LIBS_SHARED) $(LIBS_SHARED))
LOCAL_LDLIBS += $(RESOLVED_LIBS_SHARED)

DEPS += $(DEP_LIBS_SHARED)

# compilation prerequisites - component order-only dependency
$(OBJS.$(NAME)): | $(DEPS)

# Shared lib flags
SHARED_LIB_LD_FLAGS := $(TARGET_PIC_FLAG) -shared -z text -L$(PREFIX_SO)

SONAME ?= $(NAME).so

ifneq ($(SONAME), nothing)
SHARED_LIB_LD_FLAGS += $(LDFLAGS_PREFIX)-soname,$(SONAME)
endif

ifneq ($(LOCAL_VERSION_SCRIPT),)
SHARED_LIB_LD_FLAGS += $(LDFLAGS_PREFIX)--version-script=$(LOCAL_DIR)/$(LOCAL_VERSION_SCRIPT)
endif

# potentially custom CFLAGS/CXXFLAGS/LDFLAGS for compilation and linking
# add ABS_HEADERS_DIR to CFLAGS/CXXFLAGS to build always using local headers instead of installed ones
$(OBJS.$(NAME)): CFLAGS:=-I"$(ABS_HEADERS_DIR)" $(CFLAGS) $(LOCAL_CFLAGS) $(TARGET_PIC_FLAG)
$(OBJS.$(NAME)): CXXFLAGS:=-I"$(ABS_HEADERS_DIR)" $(CXXFLAGS) $(LOCAL_CXXFLAGS) $(TARGET_PIC_FLAG)

$(PREFIX_SO)$(NAME).so: LDFLAGS:=$(LDFLAGS) $(SHARED_LIB_LD_FLAGS) $(LOCAL_LDFLAGS)
$(PREFIX_SO)$(NAME).so: LDLIBS:=$(LDLIBS) $(LOCAL_LDLIBS)

# dynamically generated dependencies (file-to-file dependencies)
DEPS.$(NAME) := $(patsubst %,$(PREFIX_O)%.d,$(SRCS))
-include $(DEPS.$(NAME))

# rule for installing headers
INSTALLED_HEADERS.$(NAME) := $(patsubst $(LOCAL_DIR)%.h, $(PREFIX_H)%.h, $(HEADERS))

# external headers dir support (install whole subtree)
INSTALLED_HEADERS_TREE.$(NAME) := $(patsubst $(ABS_HEADERS_DIR)/%,$(PREFIX_H)%,$(shell find $(ABS_HEADERS_DIR) -type f -name '*.h' 2>/dev/null))

ifneq ($(filter-out $(PREFIX_H)%, $(INSTALLED_HEADERS.$(NAME)) $(INSTALLED_HEADERS_TREE.$(NAME))),)
$(error $(NAME): Installing headers outside of PREFIX_H, check Your makefile: $(INSTALLED_HEADERS.$(NAME) $(INSTALLED_HEADERS_TREE.$(NAME))))
endif

$(INSTALLED_HEADERS.$(NAME)): $(PREFIX_H)%.h: $(LOCAL_DIR)%.h
$(HEADER)

$(INSTALLED_HEADERS_TREE.$(NAME)): $(PREFIX_H)%.h: $(ABS_HEADERS_DIR)/%.h
$(HEADER)

# rule for linking shared lib
$(PREFIX_SO)$(NAME).so: $(OBJS.$(NAME))
$(LINK)

# create component phony targets
.PHONY: $(NAME) $(NAME)-headers $(NAME)-clean

$(NAME)-headers: $(INSTALLED_HEADERS.$(NAME)) $(INSTALLED_HEADERS_TREE.$(NAME))

$(NAME): $(NAME)-headers $(PREFIX_SO)$(NAME).so

$(NAME)-clean:
@echo "cleaning $(NAME)"
@rm -rf $(OBJS.$(NAME)) $(DEPS.$(NAME)) $(INSTALLED_HEADERS.$(NAME)) $(INSTALLED_HEADERS_TREE.$(NAME)) $(PREFIX_SO)$(NAME).so

# install into the root filesystem
LOCAL_INSTALL_PATH := $(or $(LOCAL_INSTALL_PATH),$(DEFAULT_INSTALL_PATH_SO))

$(NAME)-install: $(NAME) $(PREFIX_ROOTFS)$(LOCAL_INSTALL_PATH)/$(NAME).so
$(PREFIX_ROOTFS)$(LOCAL_INSTALL_PATH)/$(NAME).so: $(PREFIX_SO)$(NAME).so
$(INSTALL_FS)

# necessary for NAME variable to be correctly set in recipes
$(NAME) $(NAME)-clean: NAME:=$(NAME)

ALL_COMPONENTS += $(NAME)

# cleaning vars to avoid strange errors
NAME :=
LOCAL_SRCS :=
undefine LOCAL_DIR # need to treat LOCAL_DIR="" as a valid (set-extenally) value
LOCAL_HEADERS :=
undefine LOCAL_HEADERS_DIR # undefine needed for default value to work in next component
DEPS :=
SRCS :=
HEADERS :=
DEP_LIBS_SHARED :=
LIBS_SHARED :=
LOCAL_LDFLAGS :=
LOCAL_CFLAGS :=
LOCAL_CXXFLAGS :=
LOCAL_LDLIBS :=
undefine SONAME
LOCAL_VERSION_SCRIPT :=
undefine LOCAL_INSTALL_PATH

2 changes: 1 addition & 1 deletion makes/static-lib.mk
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Makefile rules for compiling and static library
# Makefile rules for compiling a static library
# supported external variables:
# - NAME - component/target binary name
# - LOCAL_SRCS - list of source files relative to current makefile
Expand Down
8 changes: 8 additions & 0 deletions target/armv7a.mk
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ else
$(error Incorrect TARGET.)
endif

TARGET_PIC_FLAG = -fpic
TARGET_PIE_FLAG = -fpie

CFLAGS += -mcpu=$(cpu) -mtune=$(cpu) -mthumb -fomit-frame-pointer -mno-unaligned-access
CXXFLAGS := $(CFLAGS)

Expand All @@ -40,4 +43,9 @@ STRIP := $(CROSS)strip

VADDR_KERNEL_INIT := 0xc0000000


LIBPHOENIX_PIC ?= y
LIBPHOENIX_SHARED ?= y

HAVE_MMU := y
HAVE_SHLIB := y
6 changes: 5 additions & 1 deletion target/armv7m.mk
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,17 @@ endif

VADDR_KERNEL_INIT := $(KERNEL_PHADDR)

TARGET_PIC_FLAG = -fpic
TARGET_PIE_FLAG = -fpie

LDFLAGS := -Wl,-z,max-page-size=0x10

ifeq ($(KERNEL), 1)
CFLAGS += -ffixed-r9
LDFLAGS += -Tbss=20000000 -Tdata=20000000
STRIP := $(CROSS)strip
else
CFLAGS += -fpic -fpie -msingle-pic-base -mno-pic-data-is-text-relative
CFLAGS += $(TARGET_PIC_FLAG) $(TARGET_PIE_FLAG) -msingle-pic-base -mno-pic-data-is-text-relative
# output .rel.* sections to make ELF position-independent
LDFLAGS += -Wl,-q
STRIP := $(PREFIX_PROJECT)/phoenix-rtos-build/scripts/strip.py $(CROSS)strip --strip-unneeded -R .rel.text
Expand All @@ -50,3 +53,4 @@ OBJCOPY := $(CROSS)objcopy
OBJDUMP := $(CROSS)objdump

HAVE_MMU := n
HAVE_SHLIB := n
6 changes: 5 additions & 1 deletion target/armv8m.mk
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@ CFLAGS += -mcpu=cortex-m33 -mfloat-abi=soft -fstack-usage

VADDR_KERNEL_INIT := $(KERNEL_PHADDR)

TARGET_PIC_FLAG = -fpic
TARGET_PIE_FLAG = -fpie

LDFLAGS := -Wl,-z,max-page-size=0x10

ifeq ($(KERNEL), 1)
CFLAGS += -ffixed-r9
LDFLAGS += -Tbss=20000000 -Tdata=20000000
STRIP := $(CROSS)strip
else
CFLAGS += -fpic -fpie -msingle-pic-base -mno-pic-data-is-text-relative
CFLAGS += $(TARGET_PIC_FLAG) $(TARGET_PIE_FLAG) -msingle-pic-base -mno-pic-data-is-text-relative
# output .rel.* sections to make ELF position-independent
LDFLAGS += -Wl,-q
STRIP := $(PREFIX_PROJECT)/phoenix-rtos-build/scripts/strip.py $(CROSS)strip --strip-unneeded -R .rel.text
Expand All @@ -45,3 +48,4 @@ OBJCOPY := $(CROSS)objcopy
OBJDUMP := $(CROSS)objdump

HAVE_MMU := n
HAVE_SHLIB := n
6 changes: 5 additions & 1 deletion target/armv8r.mk
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@ endif

VADDR_KERNEL_INIT := $(KERNEL_PHADDR)

TARGET_PIC_FLAG = -fpic
TARGET_PIE_FLAG = -fpie

LDFLAGS := -Wl,-z,max-page-size=0x10

ifeq ($(KERNEL), 1)
CFLAGS += -ffixed-r9
LDFLAGS += -Tbss=10014000 -Tdata=10014000
STRIP := $(CROSS)strip
else
CFLAGS += -fpic -fpie -msingle-pic-base -mno-pic-data-is-text-relative
CFLAGS += $(TARGET_PIC_FLAG) $(TARGET_PIE_FLAG) -msingle-pic-base -mno-pic-data-is-text-relative
# output .rel.* sections to make ELF position-independent
LDFLAGS += -Wl,-q
STRIP := $(PREFIX_PROJECT)/phoenix-rtos-build/scripts/strip.py $(CROSS)strip --strip-unneeded -R .rel.text
Expand All @@ -46,3 +49,4 @@ OBJCOPY := $(CROSS)objcopy
OBJDUMP := $(CROSS)objdump

HAVE_MMU := n
HAVE_SHLIB := n
Loading
Loading