Skip to contentSkip to conversation

gtest: add -fPIC

PATCH In review Martin Kelly 4 participants 10 messages Mar 28, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gtest: add -fPIC

From: Martin Kelly <[email protected]>

I tried turning on the Yocto security flags and hit a compilation error due to gtest not compiling. Adding -fPIC fixes the issue.

---
 meta-oe/recipes-test/gtest/gtest_1.8.0.bb | 9 +++++++++
 1 file changed, 9 insertions(+)
meta-oe/recipes-test/gtest/gtest_1.8.0.bb +9 -0
index 483dc1f..9ba4ee4 100644
@@ -19,6 +19,15 @@ S = "${WORKDIR}/googletest-release-${PV}"
 
 inherit cmake
 
+# -fPIC is needed to prevent relocation errors when we compile with Yocto
+# security flags. See this issue for more details:
+#
+# https://github.com/google/googletest/issues/854
+#
+# If that issue is fixed, we can probably remove the manual -fPIC flags here.
+OECMAKE_C_FLAGS += " -fPIC"
+OECMAKE_CXX_FLAGS += " -fPIC"
+
 ALLOW_EMPTY_${PN} = "1"
 ALLOW_EMPTY_${PN}-dbg = "1"
 
Signature · 1 line
2.1.4
#1 lore raw
Quoted 4 lines · Martin Kelly

From: Martin Kelly <[email protected]>

I tried turning on the Yocto security flags and hit a compilation error due to gtest not compiling. Adding -fPIC fixes the issue.

Since gtest only creates static libraries, compiling everything with -fPIC might not be the correct solution.

Does adding:

  SECURITY_CFLAGS_pn-gtest = "${SECURITY_NO_PIE_CFLAGS}"

to meta-oe/conf/distro/include/meta_oe_security_flags.inc help? If so, that might be the better approach.

Quoted 11 lines
---
 meta-oe/recipes-test/gtest/gtest_1.8.0.bb | 9 +++++++++
 1 file changed, 9 insertions(+)
meta-oe/recipes-test/gtest/gtest_1.8.0.bb +0 -0
index 483dc1f..9ba4ee4 100644
@@ -19,6 +19,15 @@ S = "${WORKDIR}/googletest-release-${PV}"
 

meta-oe/recipes-test/gtest/gtest_1.8.0.bb S = "${WORKDIR}/googletest-release-${PV}" · line 29

Quoted 10 lines
meta-oe/recipes-test/gtest/gtest_1.8.0.bb +8 -0
 inherit cmake
 
+# -fPIC is needed to prevent relocation errors when we compile with Yocto
+# security flags. See this issue for more details:
+#
+# https://github.com/google/googletest/issues/854
+#
+# If that issue is fixed, we can probably remove the manual -fPIC flags here.
+OECMAKE_C_FLAGS += " -fPIC"
+OECMAKE_CXX_FLAGS += " -fPIC"

No leading space is required with +=

Quoted 12 lines
meta-oe/recipes-test/gtest/gtest_1.8.0.bb +1 -1
+
 ALLOW_EMPTY_${PN} = "1"
 ALLOW_EMPTY_${PN}-dbg = "1"
 
--

2.1.4

--
_______________________________________________
Openembedded-devel mailing list
[email protected]
http://lists.openembedded.org/mailman/listinfo/openembedded-devel

#2 lore raw
Quoted 8 lines · Andre McCurdy
Quoted 4 lines · Martin Kelly

From: Martin Kelly <[email protected]>

I tried turning on the Yocto security flags and hit a compilation error due to gtest not compiling. Adding -fPIC fixes the issue.

Since gtest only creates static libraries, compiling everything with -fPIC might not be the correct solution.

it still is going to be linked with a PIE binary then it should be PIC

Quoted 44 lines

Does adding:

  SECURITY_CFLAGS_pn-gtest = "${SECURITY_NO_PIE_CFLAGS}"

to meta-oe/conf/distro/include/meta_oe_security_flags.inc help? If so, that might be the better approach.

Quoted 11 lines
---
 meta-oe/recipes-test/gtest/gtest_1.8.0.bb | 9 +++++++++
 1 file changed, 9 insertions(+)
meta-oe/recipes-test/gtest/gtest_1.8.0.bb +0 -0
index 483dc1f..9ba4ee4 100644
@@ -19,6 +19,15 @@ S = "${WORKDIR}/googletest-release-${PV}"
 

meta-oe/recipes-test/gtest/gtest_1.8.0.bb S = "${WORKDIR}/googletest-release-${PV}" · line 29

Quoted 10 lines
meta-oe/recipes-test/gtest/gtest_1.8.0.bb +8 -0
 inherit cmake
 
+# -fPIC is needed to prevent relocation errors when we compile with Yocto
+# security flags. See this issue for more details:
+#
+# https://github.com/google/googletest/issues/854
+#
+# If that issue is fixed, we can probably remove the manual -fPIC flags here.
+OECMAKE_C_FLAGS += " -fPIC"
+OECMAKE_CXX_FLAGS += " -fPIC"

No leading space is required with +=

Quoted 12 lines
meta-oe/recipes-test/gtest/gtest_1.8.0.bb +1 -1
+
 ALLOW_EMPTY_${PN} = "1"
 ALLOW_EMPTY_${PN}-dbg = "1"
 
--

2.1.4

--
_______________________________________________
Openembedded-devel mailing list
[email protected]
http://lists.openembedded.org/mailman/listinfo/openembedded-devel

#3 lore raw
Quoted 6 lines · Khem Raj
Quoted 3 lines · Andre McCurdy
Quoted 2 lines · Martin Kelly

From: Martin Kelly <[email protected]>

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

I tried turning on the Yocto security flags and hit a compilation error due to gtest not compiling. Adding -fPIC fixes the issue.

Since gtest only creates static libraries, compiling everything with -fPIC might not be the correct solution.

it still is going to be linked with a PIE binary then it should be PIC

Yes, I should probably clarify that gtest itself compiles just fine, but the failure occurs when you try to link against it. As an example from the AGL project, dlt-daemon fails to link with security flags turned on, with an error message indicating that -fPIC should be added.

More context here:
https://gerrit.automotivelinux.org/gerrit/#/c/8935/

#4 lore raw
Quoted 6 lines · Martin Kelly
Quoted 5 lines · Khem Raj
Quoted 4 lines · Andre McCurdy
Quoted 3 lines · Martin Kelly

From: Martin Kelly <[email protected]>

Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

I tried turning on the Yocto security flags and hit a compilation error due to gtest not compiling. Adding -fPIC fixes the issue.

Since gtest only creates static libraries, compiling everything with -fPIC might not be the correct solution.

it still is going to be linked with a PIE binary then it should be PIC

Wouldn't compiling all gtest objects with -fpie work in that case too?

Quoted 7 lines

Yes, I should probably clarify that gtest itself compiles just fine, but the failure occurs when you try to link against it. As an example from the AGL project, dlt-daemon fails to link with security flags turned on, with an error message indicating that -fPIC should be added.

More context here:
https://gerrit.automotivelinux.org/gerrit/#/c/8935/

#5 lore raw
Quoted 11 lines · Andre McCurdy
Quoted 10 lines · Martin Kelly
Quoted 9 lines · Khem Raj
Quoted 8 lines · Andre McCurdy
Quoted 5 lines · Martin Kelly

From: Martin Kelly <[email protected]>

I tried turning on the Yocto security flags and hit a compilation error due to gtest not compiling. Adding -fPIC fixes the issue.

Quoted 7 lines
Quoted 4 lines
Quoted 4 lines
Quoted 2 lines

Since gtest only creates static libraries, compiling everything with -fPIC might not be the correct solution.

it still is going to be linked with a PIE binary then it should be PIC

Wouldn't compiling all gtest objects with -fpie work in that case too?

I am no expert in this, but from the gcc manpage, it looks like -fPIE will work only when linking with executables, while -fPIC will work in either case. Thus I suppose it depends on whether we want to support linking gtest against a library.

#6 lore raw
Quoted 13 lines · Martin Kelly
Quoted 12 lines · Andre McCurdy
Quoted 11 lines · Martin Kelly
Quoted 10 lines · Khem Raj
Quoted 8 lines · Andre McCurdy
Quoted 4 lines · Martin Kelly

From: Martin Kelly <[email protected]>

I tried turning on the Yocto security flags and hit a compilation error due to gtest not compiling. Adding -fPIC fixes the issue.

Since gtest only creates static libraries, compiling everything with -fPIC might not be the correct solution.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line

it still is going to be linked with a PIE binary then it should be PIC

Wouldn't compiling all gtest objects with -fpie work in that case too?

I am no expert in this, but from the gcc manpage, it looks like -fPIE will work only when linking with executables, while -fPIC will work in either case.

Right. So the question is if security flags are enabled (and therefore -fpie is passed into the gtest build via CFLAGS) then why do you see an issue?

Quoted 2 lines

Thus I suppose it depends on whether we want to support linking gtest against a library.

If gtest needs to support linking against an .so then it would need -fPIC, regardless of any security flags. That seems like a separate question.

#7 lore raw
Quoted 5 lines · Andre McCurdy

Right. So the question is if security flags are enabled (and therefore -fpie is passed into the gtest build via CFLAGS) then why do you see an issue?

Looking at this more, I see that I hit the issue in dlt-daemon because it was trying to compile a shared version of gtest, resulting in this error:

command:
[snip]-g++ -fPIC [snip] -shared -o libgtest.so [snip]

out:
[snip]/ld: CMakeFiles/gtest.dir/src/gtest-all.cc.o: relocation R_X86_64_PC32 against symbol `_ZTVN7testing8internal17TestEventRepeaterE' can not be used when making a shared object; recompile with -fPIC

However, looking at the gtest version in meta-oe, I see only static libraries, as you say. Thus unless we start building shared versions of gtest, it looks like this patch is applicable to dlt-daemon but not gtest, so we can drop it. Thanks!

#8 lore raw
Quoted 20 lines · Martin Kelly
Quoted 5 lines · Andre McCurdy

Right. So the question is if security flags are enabled (and therefore -fpie is passed into the gtest build via CFLAGS) then why do you see an issue?

Looking at this more, I see that I hit the issue in dlt-daemon because it was trying to compile a shared version of gtest, resulting in this error:

command:
[snip]-g++ -fPIC [snip] -shared -o libgtest.so [snip]

out:
[snip]/ld: CMakeFiles/gtest.dir/src/gtest-all.cc.o: relocation R_X86_64_PC32 against symbol `_ZTVN7testing8internal17TestEventRepeaterE' can not be used when making a shared object; recompile with -fPIC

Quoted 4 lines

However, looking at the gtest version in meta-oe, I see only static libraries, as you say. Thus unless we start building shared versions of gtest, it looks like this patch is applicable to dlt-daemon but not gtest, so we can drop it. Thanks!

Is there any reason not to build shared version though ?

Quoted 2 lines
#9 lore raw
Quoted 23 lines · Khem Raj
Quoted 21 lines · Martin Kelly
Quoted 5 lines · Andre McCurdy

Right. So the question is if security flags are enabled (and therefore -fpie is passed into the gtest build via CFLAGS) then why do you see an issue?

Looking at this more, I see that I hit the issue in dlt-daemon because it was trying to compile a shared version of gtest, resulting in this error:

command:
[snip]-g++ -fPIC [snip] -shared -o libgtest.so [snip]

out:
[snip]/ld: CMakeFiles/gtest.dir/src/gtest-all.cc.o: relocation R_X86_64_PC32 against symbol `_ZTVN7testing8internal17TestEventRepeaterE' can not be used when making a shared object; recompile with -fPIC

Quoted 7 lines
Quoted 4 lines

However, looking at the gtest version in meta-oe, I see only static libraries, as you say. Thus unless we start building shared versions of gtest, it looks like this patch is applicable to dlt-daemon but not gtest, so we can drop it. Thanks!

Is there any reason not to build shared version though ?

The cmake build defaults to static libs but support for shared libs seems to be there too. Maybe just try it and see...

Quoted 3 lines
Quoted 2 lines