Blame SOURCES/0001_use_PIE_objects_for_static_libraries.patch

25b4e3
From a57d1aaafe47dadaa5fad8b73ec9e2e31bb5f123 Mon Sep 17 00:00:00 2001
25b4e3
From: Paolo Bonzini <pbonzini@redhat.com>
25b4e3
Date: Thu, 8 Oct 2020 15:30:24 +0200
25b4e3
Subject: [PATCH] build: use PIE objects for static libraries if
25b4e3
 b_staticpic=false but b_pie=true
25b4e3
25b4e3
If static_library is used as a convenience library (e.g. for link_whole)
25b4e3
it should in principle not need position independent code.
25b4e3
However, if the executables that the libraries is linked to are PIE,
25b4e3
the non-PIC objects in the static library will cause linker errors.
25b4e3
To avoid this, obey b_pie for static libraries if either b_staticpic=false
25b4e3
or they use "pic: false".
25b4e3
25b4e3
Without this patch, QEMU cannot use b_staticpic, which causes a slowdown
25b4e3
on some QEMU benchmarks up to 20%.
25b4e3
25b4e3
(cherry picked from commit 021d242f9ce8e6cd804af0c1eb4179b8c83fd470)
25b4e3
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
25b4e3
---
25b4e3
 mesonbuild/backend/backends.py |  2 +-
25b4e3
 mesonbuild/build.py            | 18 +++++++++++-------
25b4e3
 2 files changed, 12 insertions(+), 8 deletions(-)
25b4e3
25b4e3
diff --git a/mesonbuild/backend/backends.py b/mesonbuild/backend/backends.py
25b4e3
index c84bb7534..f67efe370 100644
25b4e3
--- a/mesonbuild/backend/backends.py
25b4e3
+++ b/mesonbuild/backend/backends.py
25b4e3
@@ -694,7 +694,7 @@ class Backend:
25b4e3
         # Set -fPIC for static libraries by default unless explicitly disabled
25b4e3
         if isinstance(target, build.StaticLibrary) and target.pic:
25b4e3
             commands += compiler.get_pic_args()
25b4e3
-        if isinstance(target, build.Executable) and target.pie:
25b4e3
+        elif isinstance(target, (build.StaticLibrary, build.Executable)) and target.pie:
25b4e3
             commands += compiler.get_pie_args()
25b4e3
         # Add compile args needed to find external dependencies. Link args are
25b4e3
         # added while generating the link command.
25b4e3
diff --git a/mesonbuild/build.py b/mesonbuild/build.py
25b4e3
index a06979cea..90cbd1aa6 100644
25b4e3
--- a/mesonbuild/build.py
25b4e3
+++ b/mesonbuild/build.py
25b4e3
@@ -972,13 +972,13 @@ This will become a hard error in a future Meson release.''')
25b4e3
             if m.is_darwin() or m.is_windows():
25b4e3
                 self.pic = True
25b4e3
             else:
25b4e3
-                self.pic = self._extract_pic_pie(kwargs, 'pic')
25b4e3
-        if isinstance(self, Executable):
25b4e3
+                self.pic = self._extract_pic_pie(kwargs, 'pic', environment, 'b_staticpic')
25b4e3
+        if isinstance(self, Executable) or (isinstance(self, StaticLibrary) and not self.pic):
25b4e3
             # Executables must be PIE on Android
25b4e3
             if self.environment.machines[self.for_machine].is_android():
25b4e3
                 self.pie = True
25b4e3
             else:
25b4e3
-                self.pie = self._extract_pic_pie(kwargs, 'pie')
25b4e3
+                self.pie = self._extract_pic_pie(kwargs, 'pie', environment, 'b_pie')
25b4e3
         self.implicit_include_directories = kwargs.get('implicit_include_directories', True)
25b4e3
         if not isinstance(self.implicit_include_directories, bool):
25b4e3
             raise InvalidArguments('Implicit_include_directories must be a boolean.')
25b4e3
@@ -990,14 +990,20 @@ This will become a hard error in a future Meson release.''')
25b4e3
             if self.gnu_symbol_visibility not in permitted:
25b4e3
                 raise InvalidArguments('GNU symbol visibility arg {} not one of: {}'.format(self.symbol_visibility, ', '.join(permitted)))
25b4e3
 
25b4e3
-    def _extract_pic_pie(self, kwargs, arg):
25b4e3
+    def _extract_pic_pie(self, kwargs, arg, environment, option):
25b4e3
         # Check if we have -fPIC, -fpic, -fPIE, or -fpie in cflags
25b4e3
         all_flags = self.extra_args['c'] + self.extra_args['cpp']
25b4e3
         if '-f' + arg.lower() in all_flags or '-f' + arg.upper() in all_flags:
25b4e3
             mlog.warning("Use the '{}' kwarg instead of passing '{}' manually to {!r}".format(arg, '-f' + arg, self.name))
25b4e3
             return True
25b4e3
 
25b4e3
-        val = kwargs.get(arg, False)
25b4e3
+        if arg in kwargs:
25b4e3
+            val = kwargs[arg]
25b4e3
+        elif option in environment.coredata.base_options:
25b4e3
+            val = environment.coredata.base_options[option].value
25b4e3
+        else:
25b4e3
+            val = False
25b4e3
+
25b4e3
         if not isinstance(val, bool):
25b4e3
             raise InvalidArguments('Argument {} to {!r} must be boolean'.format(arg, self.name))
25b4e3
         return val
25b4e3
@@ -1611,8 +1617,6 @@ class StaticLibrary(BuildTarget):
25b4e3
 
25b4e3
     def __init__(self, name, subdir, subproject, for_machine: MachineChoice, sources, objects, environment, kwargs):
25b4e3
         self.typename = 'static library'
25b4e3
-        if 'pic' not in kwargs and 'b_staticpic' in environment.coredata.base_options:
25b4e3
-            kwargs['pic'] = environment.coredata.base_options['b_staticpic'].value
25b4e3
         super().__init__(name, subdir, subproject, for_machine, sources, objects, environment, kwargs)
25b4e3
         if 'cs' in self.compilers:
25b4e3
             raise InvalidArguments('Static libraries not supported for C#.')
25b4e3
-- 
25b4e3
2.27.0
25b4e3