Bug 1545795 - Port bug 1079662: Always enable PIE. r=mkmelin,aceman
authorRichard Marti <richard.marti@gmail.com>
Fri, 19 Apr 2019 19:11:50 +0200
changeset 26466 d9429bddf7f53cbb78e0e77a640b4bd710fa317c
parent 26465 f07df723ca483e7abef13fa9d1eae4118e30e9b9
child 26467 c3ebd6f3872c19a0b939f9b5d465f019c360b44d
push id15842
push usermozilla@jorgk.com
push dateFri, 26 Apr 2019 19:11:17 +0000
treeherdercomm-central@fb9349e31a40 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmkmelin, aceman
bugs1545795, 1079662
Bug 1545795 - Port bug 1079662: Always enable PIE. r=mkmelin,aceman
mail/app/Makefile.in
mail/app/moz.build
mail/app/no-pie/NoPie.c
mail/app/no-pie/moz.build
mail/moz.configure
--- a/mail/app/Makefile.in
+++ b/mail/app/Makefile.in
@@ -60,18 +60,22 @@ include $(moztopsrcdir)/config/rules.mk
 
 ifeq ($(OS_ARCH),WINNT)
 
 $(RESFILE): embedded-icons
 
 endif
 
 ifneq ($(OS_ARCH),WINNT)
+ifdef COMPILE_ENVIRONMENT
+ifndef MOZ_NO_PIE_COMPAT
 libs::
 	cp -p $(DIST)/bin/$(MOZ_APP_NAME)$(BIN_SUFFIX) $(DIST)/bin/$(MOZ_APP_NAME)-bin$(BIN_SUFFIX)
+endif
+endif
 
 GARBAGE += $(addprefix $(DIST)/bin/defaults/pref/, all.js all-thunderbird.js mailnews.js)
 endif # ! WinNT
 
 ifneq (,$(filter windows,$(MOZ_WIDGET_TOOLKIT)))
 ICON_SUFFIX=.ico
 
 DESKTOP_ICONS = \
--- a/mail/app/moz.build
+++ b/mail/app/moz.build
@@ -1,16 +1,22 @@
 # vim: set filetype=python:
 # This Source Code Form is subject to the terms of the Mozilla Public
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 DIRS += ['profile']
 
-GeckoProgram(CONFIG['MOZ_APP_NAME'])
+if CONFIG['MOZ_NO_PIE_COMPAT']:
+    GeckoProgram(CONFIG['MOZ_APP_NAME'] + '-bin')
+
+    DIRS += ['no-pie']
+else:
+    GeckoProgram(CONFIG['MOZ_APP_NAME'])
+
 USE_LIBS += ['mozglue']
 SOURCES += ['nsMailApp.cpp']
 LOCAL_INCLUDES += [
     '!/build',
     '/%s/ipc/contentproc/' % CONFIG['mozreltopsrcdir'],
     '/%s/toolkit/xre' % CONFIG['mozreltopsrcdir'],
     '/%s/xpcom/base' % CONFIG['mozreltopsrcdir'],
     '/%s/xpcom/build' % CONFIG['mozreltopsrcdir'],
new file mode 100644
--- /dev/null
+++ b/mail/app/no-pie/NoPie.c
@@ -0,0 +1,26 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include <errno.h>
+#include <limits.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+int main(int argc, char* argv[]) {
+  // Ideally, we'd use mozilla::BinaryPath, but that pulls in stdc++compat,
+  // and further causes trouble linking with LTO.
+  char path[PATH_MAX + 4];
+  ssize_t len = readlink("/proc/self/exe", path, PATH_MAX - 1);
+  if (len < 0) {
+    fprintf(stderr, "Couldn't find the application directory.\n");
+    return 255;
+  }
+  strcpy(path + len, "-bin");
+  execv(path, argv);
+  // execv never returns. If it did, there was an error.
+  fprintf(stderr, "Exec failed with error: %s\n", strerror(errno));
+  return 255;
+}
new file mode 100644
--- /dev/null
+++ b/mail/app/no-pie/moz.build
@@ -0,0 +1,28 @@
+# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
+# vim: set filetype=python:
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+Program(CONFIG['MOZ_APP_NAME'])
+
+SOURCES += [
+    'NoPie.c',
+]
+
+# For some reason, LTO messes things up. We don't care anyways.
+CFLAGS += [
+    '-fno-lto',
+]
+
+# Use OS_LIBS instead of LDFLAGS to "force" the flag to come after -pie
+# from MOZ_PROGRAM_LDFLAGS.
+if CONFIG['CC_TYPE'] == 'clang':
+    # clang < 5.0 doesn't support -no-pie.
+    OS_LIBS += [
+        '-nopie'
+    ]
+else:
+    OS_LIBS += [
+        '-no-pie'
+    ]
--- a/mail/moz.configure
+++ b/mail/moz.configure
@@ -69,10 +69,17 @@ set_defconf('THUNDERBIRD_VERSION_DISPLAY
 
 set_define('MOZ_SEPARATE_MANIFEST_FOR_THEME_OVERRIDES', True)
 
 imply_option('MOZ_PLACES', True)
 imply_option('MOZ_ALLOW_LEGACY_EXTENSIONS', True)
 imply_option('MOZ_DEDICATED_PROFILES', True)
 imply_option('MOZ_BLOCK_PROFILE_DOWNGRADE', True)
 
+with only_when(target_is_linux & compile_environment):
+    option(env='MOZ_NO_PIE_COMPAT',
+           help='Enable non-PIE wrapper')
+
+    set_config('MOZ_NO_PIE_COMPAT',
+               depends_if('MOZ_NO_PIE_COMPAT')(lambda _: True))
+
 include('../mailnews/moz.configure')
 include('../../toolkit/moz.configure')