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 632F84D96D
	for <ffmpegdev@gitmailbox.com>; Wed, 26 Feb 2025 19:44:57 +0000 (UTC)
Received: from [127.0.1.1] (localhost [127.0.0.1])
	by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 5E56968C8AC;
	Wed, 26 Feb 2025 21:44:53 +0200 (EET)
Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com
 [209.85.128.202])
 by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id F289F68C2ED
 for <ffmpeg-devel@ffmpeg.org>; Wed, 26 Feb 2025 21:44:46 +0200 (EET)
Received: by mail-yw1-f202.google.com with SMTP id
 00721157ae682-6f3ff1ff117so3602887b3.0
 for <ffmpeg-devel@ffmpeg.org>; Wed, 26 Feb 2025 11:44:46 -0800 (PST)
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20230601; t=1740599085; x=1741203885;
 h=cc:to:from:subject:message-id:references:mime-version:in-reply-to
 :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to;
 bh=g1xG2BwUHwAVwGs3/fulHYF0J1EyFJsKdQHYIUF6QgI=;
 b=bD8TISltddpeK1N6bJuZ69aDrDCBTbzVtboZ+UqIPYgFrwrO6Z4BkdZGzULr8tcILh
 faioO0IOdygmtdrndBRVMUnKSrCybxtNonUMrhFU8FOzFkJyzoNp3a7gPC+aesn3E5o2
 ChjP4f+6QjsGeZCveajhYf0646XU/VazAhqILo93YMFMvy65sr1EQWN9ftb+YntJgTL/
 ENDKkizjMagBxQ5st9liYhqkJ+jepzLd6f87hnMWz7k/W/f6y1K8aOXBVl8HzdA+kaLx
 GxhykijWk2x983dHsOUCsIref3fT+lp0MlxM0AxxrcShP/zHOo6xGB1rHTB1io1Wvcu0
 GNyg==
X-Gm-Message-State: AOJu0YxHlLF4cFOknmu/daqF/5D8WbuK3KtmRhThWqoBTlti36kJDJaN
 SKZcMkQFc/hb5wf+vzytUC+aupifHJTEHuufh1mSI6BfK5nutpelH1kgJ20ojpd/+bG+pxKHnNJ
 1O68jNuK7aFeBohCejRJ3OvLuxyLuqDBOKs9+1tmyohnqo+NFxDE7kxTGPJ+LNF8Z2qjPBjokX/
 hFBUornsj2kKj5XxoJtoioijU=
X-Google-Smtp-Source: AGHT+IEek5qrSrAKX8nQf7WDfizgjWWiBySVPkJTNrI2dB4NSKqshTeWEg5mTy8GdmuAiAAxXLRv73tG
X-Received: from ywbjd12.prod.google.com
 ([2002:a05:690c:6f0c:b0:6f9:4a3e:e1d1])
 (user=prka job=prod-delivery.src-stubby-dispatcher) by
 2002:a05:690c:3585:b0:6fb:9389:3cde
 with SMTP id 00721157ae682-6fd109aad0cmr76394117b3.3.1740599085307; Wed, 26
 Feb 2025 11:44:45 -0800 (PST)
Date: Wed, 26 Feb 2025 19:44:37 +0000
In-Reply-To: <20250225213717.3373852-1-prka@google.com>
Mime-Version: 1.0
References: <20250225213717.3373852-1-prka@google.com>
X-Mailer: git-send-email 2.48.1.658.g4767266eb4-goog
Message-ID: <20250226194437.121075-1-prka@google.com>
To: ffmpeg-devel@ffmpeg.org
Subject: [FFmpeg-devel] [PATCH] Mark C globals with small code model
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>
From: Pranav Kant via ffmpeg-devel <ffmpeg-devel@ffmpeg.org>
Reply-To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org>
Cc: Pranav Kant <prka@google.com>
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/20250226194437.121075-1-prka@google.com/>
List-Archive: <https://master.gitmailbox.com/ffmpegdev/>
List-Post: <mailto:ffmpegdev@gitmailbox.com>

By default, all globals in C/C++ compiled by clang are allocated
in non-large data sections. See [1] for background on code models.
For PIC (Position independent code), this is fine as long as binary is
small but as binary size increases, users maybe want to use medium/large
code models (-mcmodel=medium) which moves data in to large sections.
As data in these large sections cannot be accessed using PIC code
anymore (as it may be too far away), compiler ends up using a different
instruction sequence when building C/C++ code -- using GOT to access
these globals (which can be relaxed by linker at link time if binary
ends up being smaller). However, assembly files continue to access these
globals defined in C/C++ files using older (and invalid instruction
sequence). So, we mark all such globals with an attribute that forces
them to be allocated in small sections allowing them to validly be
accessed from the assembly code.

This patch should not have any affect on builds that use small code
model, which is the default mode.

[1] https://eli.thegreenplace.net/2012/01/03/understanding-the-x64-code-models

Signed-off-by: Pranav Kant <prka@google.com>
---
 libavcodec/ac3dsp.c             |  2 ++
 libavcodec/cabac.c              |  2 ++
 libavcodec/x86/constants.c      |  8 ++++++++
 libavutil/attributes_internal.h | 15 +++++++++++++++
 4 files changed, 27 insertions(+)

diff --git a/libavcodec/ac3dsp.c b/libavcodec/ac3dsp.c
index 730fa70fff..d16b6c24c3 100644
--- a/libavcodec/ac3dsp.c
+++ b/libavcodec/ac3dsp.c
@@ -25,6 +25,7 @@
 
 #include "config.h"
 #include "libavutil/attributes.h"
+#include "libavutil/attributes_internal.h"
 #include "libavutil/common.h"
 #include "libavutil/intmath.h"
 #include "libavutil/mem_internal.h"
@@ -104,6 +105,7 @@ static void ac3_update_bap_counts_c(uint16_t mant_cnt[16], uint8_t *bap,
         mant_cnt[bap[len]]++;
 }
 
+attribute_mcmodel_small
 DECLARE_ALIGNED(16, const uint16_t, ff_ac3_bap_bits)[16] = {
     0,  0,  0,  3,  0,  4,  5,  6,  7,  8,  9, 10, 11, 12, 14, 16
 };
diff --git a/libavcodec/cabac.c b/libavcodec/cabac.c
index 7d41cd2ae6..b8c6db29a2 100644
--- a/libavcodec/cabac.c
+++ b/libavcodec/cabac.c
@@ -24,11 +24,13 @@
  * Context Adaptive Binary Arithmetic Coder.
  */
 
+#include "libavutil/attributes_internal.h"
 #include "libavutil/error.h"
 #include "libavutil/mem_internal.h"
 
 #include "cabac.h"
 
+attribute_mcmodel_small
 DECLARE_ASM_ALIGNED(1, const uint8_t, ff_h264_cabac_tables)[512 + 4*2*64 + 4*64 + 63] = {
     9,8,7,7,6,6,6,6,5,5,5,5,5,5,5,5,
     4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
diff --git a/libavcodec/x86/constants.c b/libavcodec/x86/constants.c
index bc7f2b17b8..347b7dd1d3 100644
--- a/libavcodec/x86/constants.c
+++ b/libavcodec/x86/constants.c
@@ -18,17 +18,21 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/attributes_internal.h"
 #include "libavutil/mem_internal.h"
 #include "libavutil/x86/asm.h" // for xmm_reg
 #include "constants.h"
 
+attribute_mcmodel_small
 DECLARE_ALIGNED(32, const ymm_reg,  ff_pw_1)    = { 0x0001000100010001ULL, 0x0001000100010001ULL,
                                                     0x0001000100010001ULL, 0x0001000100010001ULL };
 DECLARE_ALIGNED(32, const ymm_reg,  ff_pw_2)    = { 0x0002000200020002ULL, 0x0002000200020002ULL,
                                                     0x0002000200020002ULL, 0x0002000200020002ULL };
 DECLARE_ASM_ALIGNED(16, const xmm_reg,  ff_pw_3)    = { 0x0003000300030003ULL, 0x0003000300030003ULL };
+attribute_mcmodel_small
 DECLARE_ASM_ALIGNED(32, const ymm_reg,  ff_pw_4)    = { 0x0004000400040004ULL, 0x0004000400040004ULL,
                                                     0x0004000400040004ULL, 0x0004000400040004ULL };
+attribute_mcmodel_small
 DECLARE_ASM_ALIGNED(16, const xmm_reg,  ff_pw_5)    = { 0x0005000500050005ULL, 0x0005000500050005ULL };
 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_8)    = { 0x0008000800080008ULL, 0x0008000800080008ULL };
 DECLARE_ASM_ALIGNED(16, const xmm_reg,  ff_pw_9)    = { 0x0009000900090009ULL, 0x0009000900090009ULL };
@@ -49,6 +53,7 @@ DECLARE_ALIGNED(32, const ymm_reg,  ff_pw_256)  = { 0x0100010001000100ULL, 0x010
 DECLARE_ALIGNED(32, const ymm_reg,  ff_pw_512)  = { 0x0200020002000200ULL, 0x0200020002000200ULL,
                                                     0x0200020002000200ULL, 0x0200020002000200ULL };
 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_1019) = { 0x03FB03FB03FB03FBULL, 0x03FB03FB03FB03FBULL };
+attribute_mcmodel_small
 DECLARE_ALIGNED(32, const ymm_reg,  ff_pw_1023) = { 0x03ff03ff03ff03ffULL, 0x03ff03ff03ff03ffULL,
                                                     0x03ff03ff03ff03ffULL, 0x03ff03ff03ff03ffULL};
 DECLARE_ALIGNED(32, const ymm_reg,  ff_pw_1024) = { 0x0400040004000400ULL, 0x0400040004000400ULL,
@@ -66,13 +71,16 @@ DECLARE_ALIGNED(32, const ymm_reg,  ff_pw_m1)   = { 0xFFFFFFFFFFFFFFFFULL, 0xFFF
 
 DECLARE_ALIGNED(32, const ymm_reg,  ff_pb_0)    = { 0x0000000000000000ULL, 0x0000000000000000ULL,
                                                     0x0000000000000000ULL, 0x0000000000000000ULL };
+attribute_mcmodel_small
 DECLARE_ALIGNED(32, const ymm_reg,  ff_pb_1)    = { 0x0101010101010101ULL, 0x0101010101010101ULL,
                                                     0x0101010101010101ULL, 0x0101010101010101ULL };
 DECLARE_ALIGNED(32, const ymm_reg,  ff_pb_2)    = { 0x0202020202020202ULL, 0x0202020202020202ULL,
                                                     0x0202020202020202ULL, 0x0202020202020202ULL };
+attribute_mcmodel_small
 DECLARE_ALIGNED(32, const ymm_reg,  ff_pb_3)    = { 0x0303030303030303ULL, 0x0303030303030303ULL,
                                                     0x0303030303030303ULL, 0x0303030303030303ULL };
 DECLARE_ALIGNED(32, const xmm_reg,  ff_pb_15)   = { 0x0F0F0F0F0F0F0F0FULL, 0x0F0F0F0F0F0F0F0FULL };
+attribute_mcmodel_small
 DECLARE_ALIGNED(32, const ymm_reg,  ff_pb_80)   = { 0x8080808080808080ULL, 0x8080808080808080ULL,
                                                     0x8080808080808080ULL, 0x8080808080808080ULL };
 DECLARE_ALIGNED(32, const ymm_reg,  ff_pb_FE)   = { 0xFEFEFEFEFEFEFEFEULL, 0xFEFEFEFEFEFEFEFEULL,
diff --git a/libavutil/attributes_internal.h b/libavutil/attributes_internal.h
index 3df1ee6af3..0c40e6461f 100644
--- a/libavutil/attributes_internal.h
+++ b/libavutil/attributes_internal.h
@@ -31,4 +31,19 @@
 #    define FF_VISIBILITY_POP_HIDDEN
 #endif
 
+/**
+ * Some globals defined in C files are used from hardcoded asm that assumes small
+ * code model (that is, accessing these globals without GOT). This is a problem
+ * when FFMpeg is built with medium code model (-mcmodel=medium) which allocates
+ * all globals in a data section that's unreachable with PC relative instructions
+ * (small code model instruction sequence). We mark all such globals with this
+ * attribute_mcmodel_small to ensure assembly accessible globals continue to be
+ * allocated in sections reachable from PC relative instructions.
+ */
+#if ARCH_X86_64 && defined(__ELF__) && __has_attribute(model)
+#    define attribute_mcmodel_small __attribute__(model("small"))
+#else
+#    define attribute_mcmodel_small
+#endif
+
 #endif /* AVUTIL_ATTRIBUTES_INTERNAL_H */
-- 
2.48.1.658.g4767266eb4-goog

_______________________________________________
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".