Skip to content

Commit

Permalink
Assert non-executable stack for assembly files
Browse files Browse the repository at this point in the history
Some GNU linkers do not set the stack as non-executable when the
`.note.GNU-stack` section is missing. Herein, we fix this.

Also remove some warnings when compiling assembly on Clang by removing
unneccessary/unused compiler options.

Also remove some warnings in source code.
  • Loading branch information
albinahlback committed Oct 3, 2024
1 parent 85b9c06 commit 76c90d5
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 15 deletions.
8 changes: 4 additions & 4 deletions Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ LIBS2:=-lflint $(LIBS)
PIC_FLAG:=@PIC_FLAG@

ifeq ($(WANT_ASSEMBLY),1)
ASM_OBJ_FLAGS:=$(filter-out -D%,$(CPPFLAGS) $(LIB_CPPFLAGS)) $(filter-out -funroll-loops -pedantic, $(CFLAGS))
ASM_LOBJ_FLAGS:=$(filter-out -D%,$(PIC_FLAG)) $(ASM_OBJ_FLAGS)
ASMFLAGS:=@ASMFLAGS@
ASM_PIC_FLAG:=$(filter-out -D%,$(PIC_FLAG))
endif

UNROLL_LOOPS:=@UNROLL_LOOPS@
Expand Down Expand Up @@ -580,7 +580,7 @@ endef
ifeq ($(WANT_ASSEMBLY),1)
%.o: %.s
@echo " CC $(<:$(BUILD_DIR)/%.s=%.asm)"
@$(CC) $(ASM_OBJ_FLAGS) -c $< -o $@
@$(CC) $(ASMFLAGS) -c $< -o $@
endif

$(foreach dir, $(DIRS), $(eval $(call xxx_OBJS_rule,$(dir))))
Expand All @@ -600,7 +600,7 @@ endef
ifeq ($(WANT_ASSEMBLY),1)
%.lo: %_pic.s
@echo " CC $(<:$(BUILD_DIR)/%.s=%.asm)"
@$(CC) $(ASM_LOBJ_FLAGS) -c $< -o $@
@$(CC) $(ASMFLAGS) $(ASM_PIC_FLAG) -c $< -o $@
endif

$(foreach dir, $(DIRS), $(eval $(call xxx_LOBJS_rule,$(dir))))
Expand Down
71 changes: 65 additions & 6 deletions acinclude.m4
Original file line number Diff line number Diff line change
Expand Up @@ -101,23 +101,45 @@ define(FAST_VROUNDPD_PATTERN,
[[znver[2-4]* | sandybridge* | ivybridge*]])


dnl FLINT_CLANG([action-if-true],[action-if-false])
dnl FLINT_CC_IS_GCC([action-if-true],[action-if-false])
dnl -----------------------
dnl Checks if compiler is GCC.

AC_DEFUN([FLINT_CC_IS_GCC],
[AC_CACHE_CHECK([if compiler is GCC],
flint_cv_cc_is_gcc,
[flint_cv_cc_is_gcc="no"
AC_PREPROC_IFELSE([AC_LANG_PROGRAM([
#if !(defined(__GNUC__) && !defined(__clang__))
#error
error
#endif
],[])],
[flint_cv_cc_is_gcc="yes"])
])
AS_VAR_IF([flint_cv_cc_is_gcc],"yes",
[m4_default([$1], :)],
[m4_default([$2], :)])
])


dnl FLINT_CC_IS_CLANG([action-if-true],[action-if-false])
dnl -----------------------
dnl Checks if compiler is clang.

AC_DEFUN([FLINT_CLANG],
AC_DEFUN([FLINT_CC_IS_CLANG],
[AC_CACHE_CHECK([if compiler is Clang],
flint_cv_clang,
[flint_cv_clang="no"
flint_cv_cc_is_clang,
[flint_cv_cc_is_clang="no"
AC_PREPROC_IFELSE([AC_LANG_PROGRAM([
#ifndef __clang__
#error
error
#endif
],[])],
[flint_cv_clang="yes"])
[flint_cv_cc_is_clang="yes"])
])
AS_VAR_IF([flint_cv_clang],"yes",
AS_VAR_IF([flint_cv_cc_is_clang],"yes",
[m4_default([$1], :)],
[m4_default([$2], :)])
])
Expand Down Expand Up @@ -695,6 +717,43 @@ rm -f conftest*
])
dnl CL_ASM_NOEXECSTACK
dnl -------------------
dnl
dnl Checks whether the stack can be marked nonexecutable by passing an option
dnl to the C-compiler when acting on .s files. Appends that option to ASMFLAGS.
dnl This macro is adapted from one found in GLIBC-2.3.5.
dnl
dnl FIXME: This test looks broken. It tests that a file with
dnl .note.GNU-stack... can be compiled/assembled with -Wa,--noexecstack. It
dnl does not determine if that command-line option has any effect on general
dnl asm code.
AC_DEFUN([CL_ASM_NOEXECSTACK],
[AC_REQUIRE([AC_PROG_CC])
AC_CACHE_CHECK([whether assembler supports --noexecstack option],
cl_cv_asm_noexecstack,
[dnl
cat > conftest.c <<EOF
void foo() {}
EOF
if AC_TRY_COMMAND([${CC} $CFLAGS $CPPFLAGS
-S -o conftest.s conftest.c >/dev/null]) \
&& grep .note.GNU-stack conftest.s >/dev/null \
&& AC_TRY_COMMAND([${CC} $CFLAGS $CPPFLAGS -Wa,--noexecstack
-c -o conftest.o conftest.s >/dev/null])
then
cl_cv_asm_noexecstack=yes
else
cl_cv_asm_noexecstack=no
fi
rm -f conftest*])
if test "$cl_cv_asm_noexecstack" = yes; then
ASMFLAGS="$ASMFLAGS -Wa,--noexecstack"
fi
AC_SUBST(ASMFLAGS)
])
dnl GMP_ASM_LABEL_SUFFIX
dnl --------------------
dnl : - is usual.
Expand Down
13 changes: 10 additions & 3 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,9 @@ dnl AC_PROG_CC

AC_PROG_MKDIR_P

FLINT_CC_IS_GCC
FLINT_CC_IS_CLANG

################################################################################
# environment variables
################################################################################
Expand Down Expand Up @@ -1178,9 +1181,12 @@ AC_CHECK_FUNCS([aligned_alloc _aligned_malloc])
# CFLAGS
################################################################################

# The following is needed for Clang to check for unknown options.
FLINT_CLANG([AX_CHECK_COMPILE_FLAG([-Werror=unknown-warning-option],
[CFLAGS="-Werror=unknown-warning-option"])])
if test "$flint_cv_cc_is_clang" = "yes";
then
# The following is needed for Clang to check for unknown options.
AX_CHECK_COMPILE_FLAG([-Werror=unknown-warning-option],
[CFLAGS="-Werror=unknown-warning-option"])
fi

if test "$enable_coverage" = "yes";
then
Expand Down Expand Up @@ -1322,6 +1328,7 @@ then
GMP_PROG_M4
GMP_PATH_NM

CL_ASM_NOEXECSTACK
GMP_ASM_TEXT
GMP_ASM_DATA
GMP_ASM_LABEL_SUFFIX
Expand Down
2 changes: 1 addition & 1 deletion src/machine_vectors.h
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ FLINT_FORCE_INLINE vec4n CAT6(vec4n, permute, i0, i1, i2, i3)(vec4n a) { \
DEFINE_IT(3,2,1,0)
#undef DEFINE_IT

FLINT_FORCE_INLINE vec4n vec4n_zero()
FLINT_FORCE_INLINE vec4n vec4n_zero(void)
{
return _mm256_setzero_si256();
}
Expand Down
2 changes: 1 addition & 1 deletion src/mpn_extras/get_str.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ _flint_mpn_get_str(mp_srcptr x, mp_size_t n)
s = fmpz_get_str(NULL, 10, t);
fmpz_clear(t);
return s;
}
}

0 comments on commit 76c90d5

Please sign in to comment.