Skip to contentSkip to conversation

pkt-line: initialize packet_buffer to avoid macOS linker warning

PATCH In review Harald Nordgren via GitGitGadget 3 participants 8 messages May 27 – Jun 2

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 pkt-line: initialize packet_buffer to avoid macOS linker warning

From: Harald Nordgren <[email protected]>

---

    pkt-line: initialize packet_buffer to avoid macOS linker warning
    
    Removes this warning:
    
    $ make -s -j8
    GIT_VERSION=2.54.0.380.gc69baaf57b
    ld: warning: reducing alignment of section __DATA,__common from 0x8000 to 0x4000 because it exceeds segment maximum alignment
    ld: warning: reducing alignment of section __DATA,__common from 0x8000 to 0x4000 because it exceeds segment maximum alignment
    ld: warning: reducing alignment of section __DATA,__common from 0x8000 to 0x4000 because it exceeds segment maximum alignment
    ld: warning: reducing alignment of section __DATA,__common from 0x8000 to 0x4000 because it exceeds segment maximum alignment
    ld: warning: reducing alignment of section __DATA,__common from 0x8000 to 0x4000 because it exceeds segment maximum alignment
    ld: warning: reducing alignment of section __DATA,__common from 0x8000 to 0x4000 because it exceeds segment maximum alignment
    ld: warning: reducing alignment of section __DATA,__common from 0x8000 to 0x4000 because it exceeds segment maximum alignment
    ld: warning: reducing alignment of section __DATA,__common from 0x8000 to 0x4000 because it exceeds segment maximum alignment

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2313%2FHaraldNordgren%2Fpkt-line-init-buffer-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2313/HaraldNordgren/pkt-line-init-buffer-v1
Pull-Request: https://github.com/git/git/pull/2313

 pkt-line.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
pkt-line.c +1 -1
index 3fc3e9ea70..cfd2799677 100644
@@ -8,7 +8,7 @@
 #include "trace.h"
 #include "write-or-die.h"
 
-char packet_buffer[LARGE_PACKET_MAX];
+char packet_buffer[LARGE_PACKET_MAX] = {0};
 static const char *packet_trace_prefix = "git";
 static struct trace_key trace_packet = TRACE_KEY_INIT(PACKET);
 static struct trace_key trace_pack = TRACE_KEY_INIT(PACKFILE);

base-commit: c69baaf57ba26cf117c2b6793802877f19738b0d

Signature · 1 line
gitgitgadget
#1 lore raw
Quoted 11 lines · Harald Nordgren via GitGitGadget

From: Harald Nordgren <[email protected]>

---

    pkt-line: initialize packet_buffer to avoid macOS linker warning
    
    Removes this warning:
    
    $ make -s -j8
    GIT_VERSION=2.54.0.380.gc69baaf57b
    ld: warning: reducing alignment of section __DATA,__common from 0x8000 to 0x4000 because it exceeds segment maximum alignment

This sounds nuts.

Not complaining at you, but we are talking about char[]; what alignment constraints are they talking about?

pkt-line.c line 11

Quoted 10 lines
pkt-line.c +1 -1
index 3fc3e9ea70..cfd2799677 100644
@@ -8,7 +8,7 @@
 #include "trace.h"
 #include "write-or-die.h"
 
-char packet_buffer[LARGE_PACKET_MAX];
+char packet_buffer[LARGE_PACKET_MAX] = {0};

I do not like this; it sounds more like a workaround for broken linker (and compiler to certain degree).

This, compiled with a stupid compiler that is too faithful to the source text, may make the resulting object file on disk larger by 64kB, since the original said "I need 64kB area in BSS with its starting address recorded as 'packet_buffer'" (which costs almost nothing) and the updated says "Here is a 64kB of literal data" (which would record the literal data, even if its bytes happen to be all NUL). Luckily both versions of GCC and Clang I have notices that the literal data is all NUL and still keeps the area in BSS with no change in the object file size or output from "size packet-line.o", so to me and others on similar systems as I use, this probably is a benign no-op, but not everywhere.

Are there different versions of C compiler available on macOS for you to try? I am hoping that even though vendor compilers tend to lag a bit behind from the public upstream, the problems may have already been fixed in more fresher versions.

    ... goes and looks ...

According to Internet, Xcode 16.3 or newer introduced this insanity, it seems. How about adding -fno-common to your CFLAGS? If it solves the issue, then we can think about teaching config.mak.uname to detect macOS with problematic versions of compilers and add the flag as workaround.

Quoted 5 lines
pkt-line.c +0 -0
 static const char *packet_trace_prefix = "git";
 static struct trace_key trace_packet = TRACE_KEY_INIT(PACKET);
 static struct trace_key trace_pack = TRACE_KEY_INIT(PACKFILE);

base-commit: c69baaf57ba26cf117c2b6793802877f19738b0d

#2 lore raw
Quoted 5 lines

According to Internet, Xcode 16.3 or newer introduced this insanity, it seems. How about adding -fno-common to your CFLAGS? If it solves the issue, then we can think about teaching config.mak.uname to detect macOS with problematic versions of compilers and add the flag as workaround.

Yes, this works:

```
make -s CFLAGS_APPEND="-fno-common"
```

Harald

#3 lore raw

So maybe we can do something like this then?

```
+       # Silence Xcode 16.3+ linker warning about __DATA,__common alignment.
+       LD_MAJOR_VERSION = $(shell ld -v 2>&1 | sed -n
's/.*PROJECT:ld-\([0-9]*\).*/\1/p')
+        ifeq ($(shell test "$(LD_MAJOR_VERSION)" -ge 1167 && echo 1),1)
+               BASIC_CFLAGS += -fno-common
+        endif
```

Harald

Quoted 15 lines · Harald Nordgren
Quoted 6 lines

According to Internet, Xcode 16.3 or newer introduced this insanity, it seems. How about adding -fno-common to your CFLAGS? If it solves the issue, then we can think about teaching config.mak.uname to detect macOS with problematic versions of compilers and add the flag as workaround.

Yes, this works:

```
make -s CFLAGS_APPEND="-fno-common"
```

Harald

#4 lore raw
Quoted 11 lines · Harald Nordgren

So maybe we can do something like this then?

```

(quoted diff) +2 -0
+       # Silence Xcode 16.3+ linker warning about __DATA,__common alignment.
+       LD_MAJOR_VERSION = $(shell ld -v 2>&1 | sed -n

's/.*PROJECT:ld-\([0-9]*\).*/\1/p')

(quoted diff) +3 -0
+        ifeq ($(shell test "$(LD_MAJOR_VERSION)" -ge 1167 && echo 1),1)
+               BASIC_CFLAGS += -fno-common
+        endif

```

Quoted 1 line

Harald

I do not exactly know where these magic numbers and patterns for "ld" comes from, but yes, something like that in macOS specific section would be what I had in mind.

Thanks.

#5 lore raw

PATCH v2 1/1 config.mak.uname: avoid macOS linker warning on Xcode 16.3+

In reply to Harald Nordgren via GitGitGadget

From: Harald Nordgren <[email protected]>

Building on macOS with Xcode 16.3 or newer emits:

ld: warning: reducing alignment of section __DATA,__common from 0x8000 to 0x4000 because it exceeds segment maximum alignment

Pass -fno-common when "ld -v" reports ld-1167 or newer, so tentative definitions of large arrays go into BSS instead of __DATA,__common.

---
pkt-line: initialize packet_buffer to avoid macOS linker warning

* Check MacOS ld version instead
(https://en.wikipedia.org/wiki/Xcode#Xcode_15.0_-_16.x_(since_visionOS_support)_2)

Parsing output of

❯ ld -v
@(#)PROGRAM:ld PROJECT:ld-1267
BUILD 18:30:29 Apr 22 2026
configured to support archs: armv6 armv7 armv7s arm64 arm64e arm64_32 i386 x86_64 x86_64h armv6m armv7k armv7m armv7em armv8m.main armv8.1m.main will use ld-classic for: armv6 armv7 armv7s i386 armv6m armv7k armv7m armv7em
LTO support using: LLVM version 21.0.0 (static support for 30, runtime is 30)
TAPI support using: Apple TAPI version 21.0.0 (tapi-2100.0.2.6)

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2313%2FHaraldNordgren%2Fpkt-line-init-buffer-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2313/HaraldNordgren/pkt-line-init-buffer-v2
Pull-Request: https://github.com/git/git/pull/2313

Range-diff vs v1:

 1:  1c1c66d85b < -:  ---------- pkt-line: initialize packet_buffer to avoid macOS linker warning
 -:  ---------- > 1:  0e660a346e config.mak.uname: avoid macOS linker warning on Xcode 16.3+
 config.mak.uname | 6 ++++++
 1 file changed, 6 insertions(+)
config.mak.uname +6 -0
index ce5e7de779..d4d55cb324 100644
@@ -163,6 +163,12 @@ ifeq ($(uname_S),Darwin)
 		NEEDS_GOOD_LIBICONV = UnfortunatelyYes
         endif
 
+	# Silence Xcode 16.3+ linker warning about __DATA,__common alignment.
+	LD_MAJOR_VERSION = $(shell ld -v 2>&1 | sed -n 's/.*PROJECT:ld-\([0-9]*\).*/\1/p')
+        ifeq ($(shell test "$(LD_MAJOR_VERSION)" -ge 1167 && echo 1),1)
+		BASIC_CFLAGS += -fno-common
+        endif
+
 	# The builtin FSMonitor on MacOS builds upon Simple-IPC.  Both require
 	# Unix domain sockets and PThreads.
         ifndef NO_PTHREADS

base-commit: c69baaf57ba26cf117c2b6793802877f19738b0d

Signature · 1 line
gitgitgadget
#6 lore raw

Re: [PATCH v2] config.mak.uname: avoid macOS linker warning on Xcode 16.3+

config.mak.uname ifeq ($(uname_S),Darwin) · line 167

Quoted 10 lines · Harald Nordgren via GitGitGadget
config.mak.uname +2 -0
index ce5e7de779..d4d55cb324 100644
@@ -163,6 +163,12 @@ ifeq ($(uname_S),Darwin)
 		NEEDS_GOOD_LIBICONV = UnfortunatelyYes
         endif
 
+	# Silence Xcode 16.3+ linker warning about __DATA,__common alignment.
+	LD_MAJOR_VERSION = $(shell ld -v 2>&1 | sed -n 's/.*PROJECT:ld-\([0-9]*\).*/\1/p')

If LD_MAJOR_VERSION ever ends up being an empty string, then ...

config.mak.uname ifeq ($(uname_S),Darwin) · line 168

Quoted 1 line
config.mak.uname +1 -0
+        ifeq ($(shell test "$(LD_MAJOR_VERSION)" -ge 1167 && echo 1),1)

... this "test" would see

	test "" -ge 1167

that would result in errors like

/bin/bash: line 1: test: : integer expected
dash: 1: test: Illegal number:

While this won't break the build (the ifeq will just evaluate to false), it can be noisy.

Perhaps

	test -n "$(LD_MAJOR_VERSION)" &&

in front? We know from your "sed" invocation that we won't see anything other than an empty string that is not a number, so I do not think we have to worry about feeding a non-number to the comparison against 1167 except for the "ah, no match, empty string" case.

Other than that, looking good. Thanks.

Quoted 8 lines
config.mak.uname +3 -0
+		BASIC_CFLAGS += -fno-common
+        endif
+
 	# The builtin FSMonitor on MacOS builds upon Simple-IPC.  Both require
 	# Unix domain sockets and PThreads.
         ifndef NO_PTHREADS

base-commit: c69baaf57ba26cf117c2b6793802877f19738b0d

#7 lore raw

PATCH v3 1/1 config.mak.uname: avoid macOS linker warning on Xcode 16.3+

In reply to Harald Nordgren via GitGitGadget

From: Harald Nordgren <[email protected]>

Building on macOS with Xcode 16.3 or newer emits:

ld: warning: reducing alignment of section __DATA,__common from 0x8000 to 0x4000 because it exceeds segment maximum alignment

Pass -fno-common when "ld -v" reports ld-1167 or newer, so tentative definitions of large arrays go into BSS instead of __DATA,__common.

---
fix macOS linker warning

Check for empty LD_MAJOR_VERSION.

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2313%2FHaraldNordgren%2Fpkt-line-init-buffer-v3
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2313/HaraldNordgren/pkt-line-init-buffer-v3
Pull-Request: https://github.com/git/git/pull/2313

Range-diff vs v2:

1: 0e660a346e ! 1: f864912c53 config.mak.uname: avoid macOS linker warning on Xcode 16.3+

     @@ config.mak.uname: ifeq ($(uname_S),Darwin)
       
      +	# Silence Xcode 16.3+ linker warning about __DATA,__common alignment.
      +	LD_MAJOR_VERSION = $(shell ld -v 2>&1 | sed -n 's/.*PROJECT:ld-\([0-9]*\).*/\1/p')
     -+        ifeq ($(shell test "$(LD_MAJOR_VERSION)" -ge 1167 && echo 1),1)
     ++        ifeq ($(shell test -n "$(LD_MAJOR_VERSION)" && test "$(LD_MAJOR_VERSION)" -ge 1167 && echo 1),1)
      +		BASIC_CFLAGS += -fno-common
      +        endif
      +
 config.mak.uname | 6 ++++++
 1 file changed, 6 insertions(+)
config.mak.uname +6 -0
index f9a5ad9720..8719e09f66 100644
@@ -173,6 +173,12 @@ ifeq ($(uname_S),Darwin)
 		NEEDS_GOOD_LIBICONV = UnfortunatelyYes
         endif
 
+	# Silence Xcode 16.3+ linker warning about __DATA,__common alignment.
+	LD_MAJOR_VERSION = $(shell ld -v 2>&1 | sed -n 's/.*PROJECT:ld-\([0-9]*\).*/\1/p')
+        ifeq ($(shell test -n "$(LD_MAJOR_VERSION)" && test "$(LD_MAJOR_VERSION)" -ge 1167 && echo 1),1)
+		BASIC_CFLAGS += -fno-common
+        endif
+
 	# The builtin FSMonitor on MacOS builds upon Simple-IPC.  Both require
 	# Unix domain sockets and PThreads.
         ifndef NO_PTHREADS

base-commit: 1666c1265231b0bc5f613fbbf3f0a9896cdef76e

Signature · 1 line
gitgitgadget