From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <ffmpeg-devel-bounces@ffmpeg.org>
Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100])
	by master.gitmailbox.com (Postfix) with ESMTPS id BB20C4D329
	for <ffmpegdev@gitmailbox.com>; Wed, 16 Apr 2025 22:01:40 +0000 (UTC)
Received: from [127.0.1.1] (localhost [127.0.0.1])
	by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 40B6C68A0C3;
	Thu, 17 Apr 2025 01:01:37 +0300 (EEST)
Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com
 [209.85.214.175])
 by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 854C5687DD1
 for <ffmpeg-devel@ffmpeg.org>; Thu, 17 Apr 2025 01:01:30 +0300 (EEST)
Received: by mail-pl1-f175.google.com with SMTP id
 d9443c01a7336-224341bbc1dso1576725ad.3
 for <ffmpeg-devel@ffmpeg.org>; Wed, 16 Apr 2025 15:01:30 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=gmail.com; s=20230601; t=1744840888; x=1745445688; darn=ffmpeg.org;
 h=content-transfer-encoding:mime-version:message-id:date:subject:to
 :from:from:to:cc:subject:date:message-id:reply-to;
 bh=1buh892j2MPQ15c8I8F0uPdLugTMbhWlPEHoxljxfU8=;
 b=mdjeA2rkGU/IMWooB+MyHQ311aP4r4oBmj1DklxwCgPBlM2J1MuzPp9SAlQRm5Cjqp
 e4eZuxYtqb/+8++lWWNdBZtFpI2KWBE/kxIFuRUQtZRn6K2rRNkfdFdIiKVepA4qYdik
 p2V/K14XIx+v82DUYGAS5FgekzQ+PInE2Emx9Vp4s0rAsobZJC29jwD2yW70Qtkms1JC
 gx1eTAcCZAXEWotq49dgfQPTj7wOnehuXSmOxpScq0CwkK7ljbsEzoNbxG/zghBTqh4o
 Csut2mrSxbCUHOaSjAUTETninE9aGqu81O+d8/5CM/+tdzrUIT4rFqCRDF8T8ZDOJXfE
 cZdQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20230601; t=1744840888; x=1745445688;
 h=content-transfer-encoding:mime-version:message-id:date:subject:to
 :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to;
 bh=1buh892j2MPQ15c8I8F0uPdLugTMbhWlPEHoxljxfU8=;
 b=Hps76g3uYr5rbXZR1xm3Q39vDJ9GUDxfmvX1ZGsPCcKTdpODzG2xQhgVRHWZIjWN0z
 R6ALhyDzUSoYqxwNB/8Sm8ktUBnZT6hUI6BwhKom2Q0szpOPfHfcYbv1W6DojHSU1SsS
 GVe6aAUWPrQiorW5EWaxKhbfuC3XIpuPvKewJWo1nStFqKvuBuh0XtEexAg/0rnBJbYP
 UUMEBkNDH55GSJrbNIxQe48PmtU7yPfW524B2GHiEE47HLmkK1o0w0xZWLYGYgImlYRP
 Y5bemeG/hWpnii8mA+Qy1epY4Q2WxKaK4vRLjyMRBNIs8tZ4jGxZrkPFG2FqU0HrVP3X
 zdkg==
X-Gm-Message-State: AOJu0YwteV59ayqDmbtdQfLbDP5FkncmIeThXQK9spV3RjtsQx63Uojc
 r1HI9anOYBNZex/z+MomM6LTuQ3lntycEoHClTCSNxIIb685i8d6oPOxeg==
X-Gm-Gg: ASbGncvUNbbnWquJTTdJJpW/+IW4OxK/jmDWd3X7tWmvpCV2I5+gVTFsgZziLpctMMr
 atv+xmMcjKOMKURJwW2qyKOpcl56ttusDIiYoH80N+J5jOWpX8bUm7jWLMRAAQ6kXLMxWzOalSI
 UDo9ScWi4slYJO98LOmFLxBRqmMYsW/QvCZRULIDuIkWGCHInyYiZ9trB4cGY/pKaMBDl8j46Z9
 7+4tZJg0u5S7oirSSNiZSL4eTixcQQeH7Dyhi7rPZtqw1aS4noePVorW8tOs8hOdzvKLL/iAFN0
 7ed10/ZMzu8QwR4/2KqY4Fd/Zlw/Db4o1D+4Y3YyTFGFc+aDOCwKQGEGg0p2
X-Google-Smtp-Source: AGHT+IFzu+r5T7j2bR7WNEsvHLysUcl1/ypTMkyQbKlCMBvUOmeuCeTrVpqOylYgOLa46kZxNOKW3A==
X-Received: by 2002:a17:902:ce8c:b0:224:1943:c65 with SMTP id
 d9443c01a7336-22c358d9654mr47334315ad.14.1744840887509; 
 Wed, 16 Apr 2025 15:01:27 -0700 (PDT)
Received: from localhost.localdomain ([2800:2121:b000:82e:d416:2fb5:ab74:baba])
 by smtp.gmail.com with ESMTPSA id
 d9443c01a7336-22c33f1cd7csm19642545ad.93.2025.04.16.15.01.26
 for <ffmpeg-devel@ffmpeg.org>
 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
 Wed, 16 Apr 2025 15:01:26 -0700 (PDT)
From: James Almer <jamrial@gmail.com>
To: ffmpeg-devel@ffmpeg.org
Date: Wed, 16 Apr 2025 19:01:17 -0300
Message-ID: <20250416220117.2192-1-jamrial@gmail.com>
X-Mailer: git-send-email 2.49.0
MIME-Version: 1.0
Subject: [FFmpeg-devel] [PATCH] avutil/x86: Improve ELF PIC support for
 external function calls
X-BeenThere: ffmpeg-devel@ffmpeg.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: FFmpeg development discussions and patches <ffmpeg-devel.ffmpeg.org>
List-Unsubscribe: <https://ffmpeg.org/mailman/options/ffmpeg-devel>,
 <mailto:ffmpeg-devel-request@ffmpeg.org?subject=unsubscribe>
List-Archive: <https://ffmpeg.org/pipermail/ffmpeg-devel>
List-Post: <mailto:ffmpeg-devel@ffmpeg.org>
List-Help: <mailto:ffmpeg-devel-request@ffmpeg.org?subject=help>
List-Subscribe: <https://ffmpeg.org/mailman/listinfo/ffmpeg-devel>,
 <mailto:ffmpeg-devel-request@ffmpeg.org?subject=subscribe>
Reply-To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org>
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Errors-To: ffmpeg-devel-bounces@ffmpeg.org
Sender: "ffmpeg-devel" <ffmpeg-devel-bounces@ffmpeg.org>
Archived-At: <https://master.gitmailbox.com/ffmpegdev/20250416220117.2192-1-jamrial@gmail.com/>
List-Archive: <https://master.gitmailbox.com/ffmpegdev/>
List-Post: <mailto:ffmpegdev@gitmailbox.com>

From: Henrik Gramner <henrik@gramner.com>

PLT/GOT indirections are required in some cases. Most commonly when
calling functions from other shared libraries, but also in some
scenarios when calling functions with default symbol visibility
even within the same component on certain elf64 platforms.

On elf64 we can simply use PLT relocations for all calls to external
functions. Since the linker is able to eliminate unnecessary PLT
indirections with the final output binary being identical to non-PLT
relocations there isn't really any downside to doing so. This mimics
what regular compilers normally do for calls to external functions.

On elf32 with PIC we can use a function pointer from the GOT when
calling external functions, similar to what regular compilers do when
using -fno-plt. Since this both introduces overhead and clobbers one
register, which could potentially have been used for custom calling
conventions when calling other asm functions within the same library,
it's only performed for functions declared using 'cextern_naked'.

Signed-off-by: James Almer <jamrial@gmail.com>
---
 libavutil/x86/x86inc.asm | 25 ++++++++++++++++++++-----
 1 file changed, 20 insertions(+), 5 deletions(-)

diff --git a/libavutil/x86/x86inc.asm b/libavutil/x86/x86inc.asm
index e61d924bc1..40d2e16d84 100644
--- a/libavutil/x86/x86inc.asm
+++ b/libavutil/x86/x86inc.asm
@@ -242,7 +242,7 @@ DECLARE_REG_TMP_SIZE 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14
 %elif PIC
     call $+5 ; special-cased to not affect the RSB on most CPU:s
     pop %1
-    add %1, (%2)-$+1
+    add %1, -$+1+%2
 %else
     mov %1, %2
 %endif
@@ -874,16 +874,16 @@ BRANCH_INSTR jz, je, jnz, jne, jl, jle, jnl, jnle, jg, jge, jng, jnge, ja, jae,
 
 %macro cextern 1
     %xdefine %1 mangle(private_prefix %+ _ %+ %1)
-    CAT_XDEFINE cglobaled_, %1, 1
+    CAT_XDEFINE cglobaled_, %1, 2
     extern %1
 %endmacro
 
-; like cextern, but without the prefix
+; Like cextern, but without the prefix. This should be used for symbols from external libraries.
 %macro cextern_naked 1
     %ifdef PREFIX
         %xdefine %1 mangle(%1)
     %endif
-    CAT_XDEFINE cglobaled_, %1, 1
+    CAT_XDEFINE cglobaled_, %1, 3
     extern %1
 %endmacro
 
@@ -1278,12 +1278,27 @@ INIT_XMM
 %endmacro
 %macro call_internal 2
     %xdefine %%i %2
+    %define %%j %%i
     %ifndef cglobaled_%2
         %ifdef cglobaled_%1
             %xdefine %%i %1
         %endif
+    %elif FORMAT_ELF
+        %if ARCH_X86_64
+            %if cglobaled_%2 >= 2
+                ; Always emit PLT relocations when calling external functions,
+                ; the linker will eliminate unnecessary PLT indirections anyway.
+                %define %%j %%i wrt ..plt
+            %endif
+        %elif PIC && cglobaled_%2 == 3
+            ; Go through the GOT for functions declared using cextern_naked with
+            ; PIC, as such functions presumably exists in external libraries.
+            extern _GLOBAL_OFFSET_TABLE_
+            LEA eax, $$+_GLOBAL_OFFSET_TABLE_ wrt ..gotpc
+            %define %%j [eax+%%i wrt ..got]
+        %endif
     %endif
-    call %%i
+    call %%j
     LOAD_MM_PERMUTATION %%i
 %endmacro
 
-- 
2.49.0

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".