diff --git a/libavcodec/common.h b/libavcodec/common.h
index fba23063968dd2b302c465c23b421afc4fa56e6b..619719a6f354b47137a2adfa7996b85d7f7cb57e 100644
--- a/libavcodec/common.h
+++ b/libavcodec/common.h
@@ -224,11 +224,20 @@ static inline float floorf(float f) {
 
 #    include "bswap.h"
 
+// Use rip-relative addressing if compiling PIC code on x86-64.
 #    if defined(__MINGW32__) || defined(__CYGWIN__) || \
         defined(__OS2__) || (defined (__OpenBSD__) && !defined(__ELF__))
-#        define MANGLE(a) "_" #a
+#        if defined(ARCH_X86_64) && defined(PIC)
+#            define MANGLE(a) "_" #a"(%%rip)"
+#        else
+#            define MANGLE(a) "_" #a
+#        endif
 #    else
-#        define MANGLE(a) #a
+#        if defined(ARCH_X86_64) && defined(PIC)
+#            define MANGLE(a) #a"(%%rip)"
+#        else
+#            define MANGLE(a) #a
+#        endif
 #    endif
 
 /* debug stuff */
diff --git a/libavcodec/libpostproc/mangle.h b/libavcodec/libpostproc/mangle.h
index f3894cc332ff3ab9a949605a5cfde8a6cd314219..aa09cd6bf7e21b193e077916ee864fb972989610 100644
--- a/libavcodec/libpostproc/mangle.h
+++ b/libavcodec/libpostproc/mangle.h
@@ -8,12 +8,21 @@
 #define __MANGLE_H
 
 /* Feel free to add more to the list, eg. a.out IMO */
+/* Use rip-relative addressing if compiling PIC code on x86-64. */
 #if defined(__CYGWIN__) || defined(__MINGW32__) || defined(__OS2__) || \
    (defined(__OpenBSD__) && !defined(__ELF__))
+#if defined(ARCH_X86_64) && defined(PIC)
+#define MANGLE(a) "_" #a"(%%rip)"
+#else
 #define MANGLE(a) "_" #a
+#endif
+#else
+#if defined(ARCH_X86_64) && defined(PIC)
+#define MANGLE(a) #a"(%%rip)"
 #else
 #define MANGLE(a) #a
 #endif
+#endif
 
 #endif /* !__MANGLE_H */