commit 45e11bae6b983236fe35d9dd32f23a8474160404
parent f65270790ced5854725562d7b42dfc86a2d0b329
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Mon, 25 Sep 2023 15:56:49 +0200
Generate position-independent executables
Use the -fPIE compiler option when building executables and link them
with the -pie option and all other compiler flags used during
compilation. This follows the recommendation of the GCC manual for the
-pie option: "For predictable results, you must also specify the same
set of options used for compilation".
CFLAGS are now differentiated when compiling object files for a shared
object or for an executable. The former use the -fPIC flag, while the
latter require -fPIE. Similarly, the link editor flags are also
distinct. We therefore define 4 macros as follows: CFLAGS_SO,
CFLAGS_EXE, LDFLAGS_SO and LDFLAGS_EXE.
Diffstat:
2 files changed, 14 insertions(+), 12 deletions(-)
diff --git a/Makefile b/Makefile
@@ -47,7 +47,7 @@ build_program: .config src/les2htcp.d build_library
$(DEP) $(OBJ): config.mk
$(LIBNAME_SHARED): $(OBJ)
- $(CC) $(CFLAGS) $(RSYS_CFLAGS) -o $@ $(OBJ) $(LDFLAGS) $(SOFLAGS) $(RSYS_LIBS)
+ $(CC) $(CFLAGS_SO) $(RSYS_CFLAGS) -o $@ $(OBJ) $(LDFLAGS_SO) $(RSYS_LIBS)
$(LIBNAME_STATIC): $(OBJ)
$(AR) -rc $@ $?
@@ -63,7 +63,8 @@ src/les2htcp.h: src/les2htcp.h.in
src/les2htcp.h.in > $@
les2htcp: src/les2htcp.o
- $(CC) -o $@ src/les2htcp.o $(LDFLAGS) $(RSYS_LIBS) $(NETCDF_LIBS) -lm
+ $(CC) $(CFLAGS_EXE) $(RSYS_CFLAGS) $(NETCDF_CFLAGS) \
+ -o $@ src/les2htcp.o $(LDFLAGS_EXE) $(RSYS_LIBS) $(NETCDF_LIBS) -lm
.config: config.mk
@if ! $(PKG_CONFIG) --atleast-version $(RSYS_VERSION) rsys; then \
@@ -74,17 +75,17 @@ les2htcp: src/les2htcp.o
.SUFFIXES: .c .d .o
.c.d:
- @$(CC) $(CFLAGS) $(RSYS_CFLAGS) -MM -MT "$(@:.d=.o) $@" $< -MF $@
+ @$(CC) $(CFLAGS_SO) $(RSYS_CFLAGS) -MM -MT "$(@:.d=.o) $@" $< -MF $@
.c.o:
- $(CC) $(CFLAGS) $(RSYS_CFLAGS) -DHTCP_SHARED_BUILD -c $< -o $@
+ $(CC) $(CFLAGS_SO) $(RSYS_CFLAGS) -DHTCP_SHARED_BUILD -c $< -o $@
src/les2htcp.d: src/les2htcp.c
- @$(CC) $(CFLAGS) $(RSYS_CFLAGS) $(NETCDF_CFLAGS) -MM -MT "$(@:.d=.o) $@" \
+ @$(CC) $(CFLAGS_EXE) $(RSYS_CFLAGS) $(NETCDF_CFLAGS) -MM -MT "$(@:.d=.o) $@" \
src/les2htcp.c -MF $@
src/les2htcp.o: src/les2htcp.c
- $(CC) $(CFLAGS) $(RSYS_CFLAGS) $(NETCDF_CFLAGS) -c src/les2htcp.c -o $@
+ $(CC) $(CFLAGS_EXE) $(RSYS_CFLAGS) $(NETCDF_CFLAGS) -c src/les2htcp.c -o $@
################################################################################
# Installation
@@ -168,14 +169,14 @@ clean_test:
$(SHELL) make.sh clean_test $(TEST_SRC)
$(TEST_DEP): config.mk htcp-local.pc
- @$(CC) $(CFLAGS) $(RSYS_CFLAGS) $(HTCP_CFLAGS) \
+ @$(CC) $(CFLAGS_EXE) $(RSYS_CFLAGS) $(HTCP_CFLAGS) \
-MM -MT "$(@:.d=.o) $@" $(@:.d=.c) -MF $@
$(TEST_OBJ): config.mk htcp-local.pc
- $(CC) $(CFLAGS) $(RSYS_CFLAGS) $(HTCP_CFLAGS) -c $(@:.o=.c) -o $@
+ $(CC) $(CFLAGS_EXE) $(RSYS_CFLAGS) $(HTCP_CFLAGS) -c $(@:.o=.c) -o $@
test_htcp \
test_htcp_load \
test_htcp_load_from_file \
: config.mk htcp-local.pc
- $(CC) -o $@ src/$@.o $(RSYS_LIBS) $(HTCP_LIBS) -lm
+ $(CC) $(CFLAGS_EXE) -o $@ src/$@.o $(LDFLAGS_EXE) $(RSYS_LIBS) $(HTCP_LIBS) -lm
diff --git a/config.mk b/config.mk
@@ -48,20 +48,21 @@ WFLAGS =\
CFLAGS_COMMON =\
-std=c89\
-pedantic\
- -fPIC\
-fvisibility=hidden\
-fstrict-aliasing\
$(WFLAGS)
CFLAGS_RELEASE = -O2 -DNDEBUG $(CFLAGS_COMMON)
CFLAGS_DEBUG = -g $(CFLAGS_COMMON)
-CFLAGS = $(CFLAGS_$(BUILD_TYPE))
+CFLAGS_SO = $(CFLAGS_$(BUILD_TYPE)) -fPIC
+CFLAGS_EXE = $(CFLAGS_$(BUILD_TYPE)) -fPIE
################################################################################
# Linker options
################################################################################
-SOFLAGS = -shared -Wl,--no-undefined
LDFLAGS_DEBUG =
LDFLAGS_RELEASE = -s
LDFLAGS = $(LDFLAGS_$(BUILD_TYPE))
+LDFLAGS_SO = $(LDFLAGS_$(BUILD_TYPE)) -shared -Wl,--no-undefined
+LDFLAGS_EXE = $(LDFLAGS_$(BUILD_TYPE)) -pie