hotspot/make/linux/makefiles/gcc.make

Print this page
rev 5915 : 8032045: Enable compiler and linker safety switches for debug builds
Reviewed-by: duke

@@ -102,11 +102,11 @@
     # There are some files which don't like precompiled headers
     # The following files are build with 'OPT_CFLAGS/NOOPT' (-O0) in the opt build.
     # But Clang doesn't support a precompiled header which was compiled with -O3
     # to be used in a compilation unit which uses '-O0'. We could also prepare an
     # extra '-O0' PCH file for the opt build and use it here, but it's probably
-    # not worth the effoert as long as only two files need this special handling.
+    # not worth the effort as long as only two files need this special handling.
     PCH_FLAG/loopTransform.o = $(PCH_FLAG/NO_PCH)
     PCH_FLAG/sharedRuntimeTrig.o = $(PCH_FLAG/NO_PCH)
     PCH_FLAG/sharedRuntimeTrans.o = $(PCH_FLAG/NO_PCH)
 
   endif

@@ -223,24 +223,33 @@
     WARNING_FLAGS += -Wconversion
   endif
 endif
 
 CFLAGS_WARN/DEFAULT = $(WARNINGS_ARE_ERRORS) $(WARNING_FLAGS)
+
 # Special cases
 CFLAGS_WARN/BYFILE = $(CFLAGS_WARN/$@)$(CFLAGS_WARN/DEFAULT$(CFLAGS_WARN/$@)) 
 
-# The flags to use for an Optimized g++ build
+# optimization control flags (Used by fastdebug and release variants)
+OPT_CFLAGS/NOOPT=-O0
+ifeq "$(shell expr \( $(CC_VER_MAJOR) \> 4 \) \| \( \( $(CC_VER_MAJOR) = 4 \) \& \( $(CC_VER_MINOR) \>= 8 \) \))" "1"
+  # Allow basic optimizations which don't distrupt debugging. (Principally dead code elimination)
+  OPT_CFLAGS/DEBUG=-Og
+else
+  # Allow no optimizations.
+  OPT_CFLAGS/DEBUG=-O0
+endif
 OPT_CFLAGS/SIZE=-Os
 OPT_CFLAGS/SPEED=-O3
 
+OPT_CFLAGS_DEFAULT ?= SPEED
+
 # Hotspot uses very unstrict aliasing turn this optimization off
 # This option is added to CFLAGS rather than OPT_CFLAGS
 # so that OPT_CFLAGS overrides get this option too.
 CFLAGS += -fno-strict-aliasing 
 
-OPT_CFLAGS_DEFAULT ?= SPEED
-
 ifdef OPT_CFLAGS
   ifneq ("$(origin OPT_CFLAGS)", "command line")
     $(error " Use OPT_EXTRAS instead of OPT_CFLAGS to add extra flags to OPT_CFLAGS.")
   endif
 endif

@@ -251,12 +260,10 @@
 # if we use expensive-optimizations
 ifeq ($(BUILDARCH), ia64)
 OPT_CFLAGS += -fno-expensive-optimizations
 endif
 
-OPT_CFLAGS/NOOPT=-O0
-
 # Work around some compiler bugs.
 ifeq ($(USE_CLANG), true)
   ifeq ($(shell expr $(CC_VER_MAJOR) = 4 \& $(CC_VER_MINOR) = 2), 1)
     OPT_CFLAGS/loopTransform.o += $(OPT_CFLAGS/NOOPT)
   endif

@@ -268,11 +275,11 @@
 endif
 
 # Flags for generating make dependency flags.
 DEPFLAGS = -MMD -MP -MF $(DEP_DIR)/$(@:%=%.d)
 ifeq ($(USE_CLANG),)
-  ifneq ("${CC_VER_MAJOR}", "2")
+  ifneq ($(CC_VER_MAJOR), 2)
     DEPFLAGS += -fpch-deps
   endif
 endif
 
 #------------------------------------------------------------------------

@@ -281,23 +288,31 @@
 # statically link libstdc++.so, work with gcc but ignored by g++
 STATIC_STDCXX = -Wl,-Bstatic -lstdc++ -Wl,-Bdynamic
 
 ifeq ($(USE_CLANG),)
   # statically link libgcc and/or libgcc_s, libgcc does not exist before gcc-3.x.
-  ifneq ("${CC_VER_MAJOR}", "2")
+  ifneq ($(CC_VER_MAJOR), 2)
     STATIC_LIBGCC += -static-libgcc
   endif
 
+  # Enable linker optimization
+  LFLAGS += -Xlinker -O1
+
+  ifeq ($(VARIANT), DBG)
+    # for relocations read-only
+    LFLAGS += -Wl,-z,relro
+
+    ifeq ($(FASTDEBUG), false)
+      # disable incremental relocations linking
+      LFLAGS += -Wl,-z,now
+    endif
+  endif
+
   ifeq ($(BUILDARCH), ia64)
     LFLAGS += -Wl,-relax
   endif
-endif
-
-# Enable linker optimization
-LFLAGS += -Xlinker -O1
 
-ifeq ($(USE_CLANG),)
   # If this is a --hash-style=gnu system, use --hash-style=both
   #   The gnu .hash section won't work on some Linux systems like SuSE 10.
   _HAS_HASH_STYLE_GNU:=$(shell $(CC) -dumpspecs | grep -- '--hash-style=gnu')
   ifneq ($(_HAS_HASH_STYLE_GNU),)
     LDFLAGS_HASH_STYLE = -Wl,--hash-style=both

@@ -330,10 +345,18 @@
   # too big object files and speed the build up a little bit
   # (see http://llvm.org/bugs/show_bug.cgi?id=7554)
   CFLAGS += -flimit-debug-info
 endif
 
+ifeq "$(shell expr \( $(CC_VER_MAJOR) \> 4 \) \| \( \( $(CC_VER_MAJOR) = 4 \) \& \( $(CC_VER_MINOR) \>= 8 \) \))" "1"
+  # Allow basic optimizations which don't distrupt debugging. (Principally dead code elimination)
+  DEBUG_CFLAGS=-Og
+else
+  # Allow no optimizations.
+  DEBUG_CFLAGS=-O0
+endif
+
 # DEBUG_BINARIES uses full -g debug information for all configs
 ifeq ($(DEBUG_BINARIES), true)
   CFLAGS += -g
 else
   # Use the stabs format for debugging information (this is the default

@@ -359,11 +382,11 @@
   ifeq ($(ENABLE_FULL_DEBUG_SYMBOLS),1)
     FASTDEBUG_CFLAGS/ia64  = -g
     FASTDEBUG_CFLAGS/amd64 = -g
     FASTDEBUG_CFLAGS/arm   = -g
     FASTDEBUG_CFLAGS/ppc   = -g
-    FASTDEBUG_CFLAGS += $(DEBUG_CFLAGS/$(BUILDARCH))
+    FASTDEBUG_CFLAGS += $(FASTDEBUG_CFLAGS/$(BUILDARCH))
     ifeq ($(FASTDEBUG_CFLAGS/$(BUILDARCH)),)
       ifeq ($(USE_CLANG), true)
         # Clang doesn't understand -gstabs
         FASTDEBUG_CFLAGS += -g
       else

@@ -385,10 +408,24 @@
       endif
     endif
   endif
 endif
 
+# Enable bounds checking.
+ifeq ($(VARIANT), DBG)
+  # _FORTIFY_SOURCE appears in GCC 4.0+
+  ifeq "$(shell expr \( $(CC_VER_MAJOR) \> 3 \) )" "0"
+    ifeq ($(FASTDEBUG), true)
+      # compile time size bounds checks
+      CFLAGS += -D_FORTIFY_SOURCE=1
+    else
+      # and runtime size bounds checks and paranoid stack smashing checks.
+      CFLAGS += -D_FORTIFY_SOURCE=2 -fstack-protector-all --param ssp-buffer-size=1
+    endif
+  endif
+endif
+
 # If we are building HEADLESS, pass on to VM
 # so it can set the java.awt.headless property
 ifdef HEADLESS
 CFLAGS += -DHEADLESS
 endif