Force -fPIE on ELF if Qt is built with reduced relocations
authorOlivier Goffart <ogoffart@woboq.com>
Sun, 29 Jan 2012 12:54:19 +0000 (13:54 +0100)
committerQt by Nokia <qt-info@nokia.com>
Fri, 10 Feb 2012 10:42:31 +0000 (11:42 +0100)
Put in qconfig.h whether qt is compiled with reduced relocations.

When using -Bsymbolic-functions (enabled by default on Qt)
but not -fPIE, the comparison of the function pointers fail
because the addresses are different in Qt, and in the executable.

Hence we now enable -fPIE by default on qmake, and force a compilation
error when it is not enabled and built with reduced relocations.

Done-with: Sune Vuorela <sune@vuorela.dk>
Change-Id: Ib3fdba06fab6e8a93b75b4c6cf16cc973ab335db
Reviewed-by: Bradley T. Hughes <bradley.hughes@nokia.com>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
configure
mkspecs/common/gcc-base.conf
src/corelib/global/qglobal.h
tests/auto/corelib/kernel/qobject/tst_qobject.cpp
tests/auto/tools/moc/tst_moc.cpp

index a3b94e1..d497c2f 100755 (executable)
--- a/configure
+++ b/configure
@@ -7066,6 +7066,11 @@ else
     echo "#define QT_POINTER_SIZE $?" >>"$outpath/src/corelib/global/qconfig.h.new"
 fi
 
+#REDUCE_RELOCATIONS is a elf/unix only thing, so not in windows configure.exe
+if [ "$CFG_REDUCE_RELOCATIONS" = "yes" ]; then
+    echo "#define QT_REDUCE_RELOCATIONS" >>"$outpath/src/corelib/global/qconfig.h.new"
+fi
+
 
 echo "" >>"$outpath/src/corelib/global/qconfig.h.new"
 
index 0e90666..28e3b87 100644 (file)
@@ -39,6 +39,7 @@ QMAKE_CFLAGS_RELEASE        += -O2
 QMAKE_CFLAGS_DEBUG          += -g
 QMAKE_CFLAGS_SHLIB          += -fPIC
 QMAKE_CFLAGS_STATIC_LIB     += -fPIC
+QMAKE_CFLAGS_APP            += -fPIE
 QMAKE_CFLAGS_YACC           += -Wno-unused -Wno-parentheses
 QMAKE_CFLAGS_HIDESYMS       += -fvisibility=hidden
 
@@ -50,6 +51,7 @@ QMAKE_CXXFLAGS_RELEASE    += $$QMAKE_CFLAGS_RELEASE
 QMAKE_CXXFLAGS_DEBUG      += $$QMAKE_CFLAGS_DEBUG
 QMAKE_CXXFLAGS_SHLIB      += $$QMAKE_CFLAGS_SHLIB
 QMAKE_CXXFLAGS_STATIC_LIB += $$QMAKE_CFLAGS_STATIC_LIB
+QMAKE_CXXFLAGS_APP        += $$QMAKE_CFLAGS_APP
 QMAKE_CXXFLAGS_YACC       += $$QMAKE_CFLAGS_YACC
 QMAKE_CXXFLAGS_HIDESYMS   += $$QMAKE_CFLAGS_HIDESYMS -fvisibility-inlines-hidden
 
index 33b5b8b..ffeb8a2 100644 (file)
@@ -62,6 +62,7 @@
 #include <QtCore/qconfig.h>
 #endif
 
+
 #include <QtCore/qsystemdetection.h>
 #include <QtCore/qcompilerdetection.h>
 #include <QtCore/qprocessordetection.h>
@@ -1783,6 +1784,11 @@ Q_CORE_EXPORT int qrand();
 #  endif
 #endif
 
+#if defined(QT_REDUCE_RELOCATIONS) && defined(__ELF__) && !defined(__PIC__)
+#  error "You must build your code with position independent code if Qt was built with -reduce-relocations. "\
+         "Compile your code with -fPIC or -fPIE."
+#endif
+
 namespace QtPrivate {
 //like std::enable_if
 template <bool B, typename T = void> struct QEnableIf;
index 91b93e4..8f4203a 100644 (file)
@@ -4152,9 +4152,6 @@ void tst_QObject::pointerConnect()
     QVERIFY( connect( s, &SenderObject::signal1 , r2, &ReceiverObject::slot1 ) );
     QVERIFY( connect( s, &SenderObject::signal1 , r1, &ReceiverObject::slot3 ) );
     QVERIFY( connect( s, &SenderObject::signal3 , r1, &ReceiverObject::slot3 ) );
-#if defined(Q_CC_GNU) && defined(Q_OS_UNIX)
-    QEXPECT_FAIL("", "Test may fail due to failing comparison of pointers to member functions caused by problems with -reduce-relocations on this platform.", Continue);
-#endif
     QVERIFY2( connect( &timer, &QTimer::timeout, r1, &ReceiverObject::deleteLater ),
              "Signal connection failed most likely due to failing comparison of pointers to member functions caused by problems with -reduce-relocations on this platform.");
 
index 7c47d60..0aa1a68 100644 (file)
@@ -614,7 +614,7 @@ void tst_Moc::oldStyleCasts()
 
     QStringList args;
     args << "-c" << "-x" << "c++" << "-Wold-style-cast" << "-I" << "."
-         << "-I" << qtIncludePath << "-o" << "/dev/null" << "-";
+         << "-I" << qtIncludePath << "-o" << "/dev/null" << "-fPIE" << "-";
     proc.start("gcc", args);
     QVERIFY(proc.waitForStarted());
     proc.write(mocOut);
@@ -683,7 +683,7 @@ void tst_Moc::inputFileNameWithDotsButNoExtension()
 
     QStringList args;
     args << "-c" << "-x" << "c++" << "-I" << ".."
-         << "-I" << qtIncludePath << "-o" << "/dev/null" << "-";
+         << "-I" << qtIncludePath << "-o" << "/dev/null" << "-fPIE" <<  "-";
     proc.start("gcc", args);
     QVERIFY(proc.waitForStarted());
     proc.write(mocOut);