<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/54638>54638</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Apple clang generates incorrect x86_64 executable when .zerofill is used to create a large segment
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          JayReynoldsFreeman
      </td>
    </tr>
</table>

<pre>
    This is a duplicate of a bug I have filed with Apple developer feedback: It concerns the version of clang that Apple provides with Xcode 13.2.1. When I input "clang -- version", I get 

Apple clang version 13.0.0 (clang-1300.0.29.30)
Target: x86_64-apple-darwin20.6.0
Thread model: posix

The essence of the problem is that when targeting the x86_64 architecture, using ".zerofill" in a .s file to specify a segment, clang will not properly create a segment larger than (approximately) 1 Gbye, and provides no warning that it has failed to do so. I am using .zerofill directives of the form:

.zerofill FooSegment, FooSector, FooVariable, 0x40000000

and am using objdump -h to investigate sector layout in the compiled executable. Specifically, clang creates a section of length zero instead of the length specified in the .zerofill directive.

The problem does not occur if I target the arm64 architecture.

I attach two short files which can be compiled to demonstrate the problem. See detailed description of what to do with the files, and what results I have obtained, in file "BugSegment.c++".

Files: Here is "BugSegment.s":

`
.zerofill FooSegment, FooSector, FooVariable, 0x40000000
//.zerofill FooSegment, FooSector, FooVariable, 0x80000000
`

And here is "BugSegment.c++":

`
/****************************************************************
*****************************************************************

                          INSTRUCTIONS

The code is way at the bottom -- literally a one-liner. The purpose of
this test is to demonstrate an anomaly in binary file organization
when using the ".zerofill" directive to create a large segment. The
idea is to use "objdump.h" to inspect the executable file. File
"BugSegment.s" contains two .zerofill directives, one specifying
a segment size of 0x40000000 and one specifying 0x80000000. The idea
is to comment out one or the other directive, to see what happens.
The compiler invocation uses the -segaddr linker flag to position the
segment at a particular address.

The problem occurs only when I am compiling with a target architecture
of Intel Silicon. When I target Apple silicon, the binary looks fine.

Let's be clear: There are four cases of interest:

Target architecture  Architecture of development       Does it work?
                     machine (where I am compiling)
===================  ================================  =============
Intel processor      Intel machine (MacPro7,1)              NO
Intel processor      Apple machine (Macmini9,1)             NO
Apple silicon        Intel machine (MacPro7,1)              YES
Apple silicon        Apple machine (Macmini9,1)             YES


E.g.:

On an Intel Mac (MacPro7,1), running Big Sur 11.6.5 and with Xcode
13.2.1 (and its compilers) installed, and using .zerofill with
0x40000000, I compiled with

clang BugSegment.c++ BugSegment.s -o BugSegment -segaddr FooSegment 0x10000000000

Then I changed "BugSegment.s" so that the size was 0x80000000 instead of
0x40000000, and tried again.

In the first case (0x40000000), objdump produced a reasonable
result. The sector showed up at the expected address with the expected
size:

#### SNIP ####

> objdump -h BugSegment

BugSegment: file format mach-o 64-bit x86-64

Sections:
Idx Name            Size     VMA              Type
  0 __text          00000025 0000000100003f30 TEXT
  1 __stubs         00000006 0000000100003f56 TEXT
  2 __stub_helper   0000001a 0000000100003f5c TEXT
  3 __cstring       00000042 0000000100003f76 DATA
  4 __unwind_info   00000048 0000000100003fb8 DATA
  5 __got           00000008 0000000100004000 DATA
  6 __la_symbol_ptr 00000008 0000000100008000 DATA
  7 __data          00000008 0000000100008008 DATA
  8 FooSector       40000000 0000010000000000 BSS

#### UNSNIP ####


BUT, on changing to a size of 0x80000000, things got weird -- a
"__huge" sector was created with the expected size, but at an
unexpected address, and the sector named in ".zerofill" was at
the right address but had size zero:


#### SNIP ####

BugSegment: file format mach-o 64-bit x86-64

Sections:
Idx Name            Size     VMA              Type
  0 __text          00000025 0000000100003f30 TEXT
  1 __stubs         00000006 0000000100003f56 TEXT
  2 __stub_helper   0000001a 0000000100003f5c TEXT
  3 __cstring       00000042 0000000100003f76 DATA
  4 __unwind_info   00000048 0000000100003fb8 DATA
  5 __got           00000008 0000000100004000 DATA
  6 __la_symbol_ptr 00000008 0000000100008000 DATA
  7 __data          00000008 0000000100008008 DATA
  8 __huge          80000000 0000000100008010 BSS  <==ANOMALY
  9 FooSector       00000000 0000010000000000 BSS  <==ANOMALY

#### UNSNIP ####


Next I went back to the MacPro7,1, and compiled using the
"-target" flag to specify Apple Silicon.

clang BugSegment.c++ BugSegment.s -o BugSegment -segaddr FooSegment 0x10000000000 -target arm64-apple-macos11.1

The executable looked fine when I examined it with objdump -h.

Et cetera ...


Thus whatever this problem is, it has to do with file-format
generation for Intel silicon. (And my project, from which this
simple test case was distilled, cannot work the correctly on
both architectures.)

****************************************************************
****************************************************************/

#include <stdio.h>

int main( int argc, char *argv[] )
{
    printf("Use objdump -h on the executable to see the segments, et cetera.\n");
}
`


</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJztWd9z2zgO_mucF441shQ79oMfkqa5y8023Vm7e7tPGUqiJW4l0kNScdy_fgFQv5P0pp3ew-6sx5PWMgECIPDhA53o7LzdF9IyeHOW1cdSptwJpg_wMalzds8K_iTYQZYiYyfpCnZ9PJaCZeJJlPooDDsIkSU8_TyLr9m9Y6lWqTDKMlcI9iSMlVqhurTkKoeH3DUajkY_yUxYr_W3VGeCLeIgChYB-28hFGwt1bF2bBZFXng-bxXCo1n0DlbkAr4Pb2fhtf_rVfvl7eagNAxCULOm5_NFHMLnINoEcTiLNl5wzw3oQh-e16vH1eWco6Z5xs1JqigMVkHYLCyM4BmrwNwSlx-1lc9DE_bgt7BWQBjQbwwDuJqUosIgUwBO6J6jHSUFRTS7Mm7SQjqRutoI9LC2uADcDb4Io-EUSvg_xAVOJ7B0KsxpZo8ilYczPLQir4RyKOqDcAIRprRDG-C0yjNLwX444W4tK9EQg5YpDBI4bvSzrGBReYb4sAX7V3Ima7jK-mNTmp24UbI9VekgVcAmTqkCVmVgmA7gkHjV-NE5wTJpwEn5BHqaGB20qSCew0j2y--03vWe0afUadN8-JUbySHA-DF8vgz9a6gJDe-s0MkfWV0d2bxAK6UCI5zMMSaWtEJAzhryDqKMhqW6OpJL4lmktcONArajiEOtlBijNtg-tJZiC975xC-FyiHB0RdQaR1m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