[virglrenderer-devel] [PATCH] fix symbol `util_cpu_caps' can not be used when making a shared object

Hongxu Jia hongxu.jia at windriver.com
Sun May 19 07:00:01 UTC 2019


Extern global variable `util_cpu_caps' to multiple C source files is
not a good habit, it caused linking failure when making a shard object
in some cross compiling circumstance
...
|ld: gallium/auxiliary/.libs/libgallium.a(u_cpu_detect.o): relocation
R_386_GOTOFF against undefined symbol `util_cpu_caps' can not be used
when making a shared object
|ld: final link failed: bad value
...

Covert global variable to static and assign it only in one C source file,
provide get function for other C source files

Signed-off-by: Hongxu Jia <hongxu.jia at windriver.com>
---
 src/gallium/auxiliary/util/u_cpu_detect.c | 6 +++++-
 src/gallium/auxiliary/util/u_cpu_detect.h | 3 +--
 src/gallium/auxiliary/util/u_math.c       | 8 ++++----
 3 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/src/gallium/auxiliary/util/u_cpu_detect.c b/src/gallium/auxiliary/util/u_cpu_detect.c
index 0b4b83a..be0cedd 100644
--- a/src/gallium/auxiliary/util/u_cpu_detect.c
+++ b/src/gallium/auxiliary/util/u_cpu_detect.c
@@ -78,7 +78,11 @@ DEBUG_GET_ONCE_BOOL_OPTION(dump_cpu, "GALLIUM_DUMP_CPU", FALSE)
 #endif
 
 
-struct util_cpu_caps util_cpu_caps;
+static struct util_cpu_caps util_cpu_caps;
+struct util_cpu_caps* get_util_cpu_caps(void)
+{
+    return &util_cpu_caps;
+}
 
 #if defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64)
 static int has_cpuid(void);
diff --git a/src/gallium/auxiliary/util/u_cpu_detect.h b/src/gallium/auxiliary/util/u_cpu_detect.h
index 01f3896..58134ab 100644
--- a/src/gallium/auxiliary/util/u_cpu_detect.h
+++ b/src/gallium/auxiliary/util/u_cpu_detect.h
@@ -73,8 +73,7 @@ struct util_cpu_caps {
    unsigned has_daz:1;
 };
 
-extern struct util_cpu_caps
-util_cpu_caps;
+struct util_cpu_caps* get_util_cpu_caps(void);
 
 void util_cpu_detect(void);
 
diff --git a/src/gallium/auxiliary/util/u_math.c b/src/gallium/auxiliary/util/u_math.c
index e574153..c08ed3b 100644
--- a/src/gallium/auxiliary/util/u_math.c
+++ b/src/gallium/auxiliary/util/u_math.c
@@ -90,7 +90,7 @@ util_fpstate_get(void)
    unsigned mxcsr = 0;
 
 #if defined(PIPE_ARCH_SSE)
-   if (util_cpu_caps.has_sse) {
+   if (get_util_cpu_caps()->has_sse) {
       mxcsr = _mm_getcsr();
    }
 #endif
@@ -108,10 +108,10 @@ unsigned
 util_fpstate_set_denorms_to_zero(unsigned current_mxcsr)
 {
 #if defined(PIPE_ARCH_SSE)
-   if (util_cpu_caps.has_sse) {
+   if (get_util_cpu_caps()->has_sse) {
       /* Enable flush to zero mode */
       current_mxcsr |= _MM_FLUSH_ZERO_MASK;
-      if (util_cpu_caps.has_daz) {
+      if (get_util_cpu_caps()->has_daz) {
          /* Enable denormals are zero mode */
          current_mxcsr |= _MM_DENORMALS_ZERO_MASK;
       }
@@ -130,7 +130,7 @@ void
 util_fpstate_set(unsigned mxcsr)
 {
 #if defined(PIPE_ARCH_SSE)
-   if (util_cpu_caps.has_sse) {
+   if (get_util_cpu_caps()->has_sse) {
       _mm_setcsr(mxcsr);
    }
 #endif
-- 
2.8.1



More information about the virglrenderer-devel mailing list