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

    <tr>
        <th>Summary</th>
        <td>
            lld doesn't protect the last RELRO page on Asahi Linux
        </td>
    </tr>

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

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

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

<pre>
    I installed Asahi Linux on my M1 Mac Mini. Note that Asahi Linux is a 16 KiB page system.

```
#include <stdio.h>

extern char _GLOBAL_OFFSET_TABLE_;

int main() {
 printf("Hello world\n");
  _GLOBAL_OFFSET_TABLE_ = 3;
}
```

If I run the above program, it should crash because it writes to `.got` and `.got` should be protected by GNU_RELRO. However, it doesn't actually crash.

```
$ clang -fuse-ld=/home/ruiu/llvm-project/build/bin/ld.lld -o hello hello.c
$ ./hello
Hello world
```

I believe it is because RELRO does not end at a page boundary and therefore the last page in the RELRO isn't protected. As you can see, the RELRO in the executable ends at 0x21000, not at 0x30000.

```
$ readelf --segments hello

Elf file type is DYN (Position-Independent Executable file)
Entry point 0x10780
There are 11 program headers, starting at offset 64

Program Headers:
 Type           Offset   VirtAddr           PhysAddr           FileSiz  MemSiz Flg Align
  PHDR           0x000040 0x0000000000000040 0x0000000000000040 0x000268 0x000268 R   0x8
  INTERP         0x0002a8 0x00000000000002a8 0x00000000000002a8 0x00001b 0x00001b R   0x1
 [Requesting program interpreter: /lib/ld-linux-aarch64.so.1]
  LOAD 0x000000 0x0000000000000000 0x0000000000000000 0x000750 0x000750 R 0x10000
  LOAD           0x000780 0x0000000000010780 0x0000000000010780 0x0001d0 0x0001d0 R E 0x10000
  LOAD           0x000950 0x0000000000020950 0x0000000000020950 0x0001f0 0x0001f0 RW  0x10000
  LOAD           0x000b40 0x0000000000030b40 0x0000000000030b40 0x000048 0x000049 RW  0x10000
 DYNAMIC        0x000960 0x0000000000020960 0x0000000000020960 0x0001b0 0x0001b0 RW  0x8
  GNU_RELRO      0x000950 0x0000000000020950 0x0000000000020950 0x0001f0 0x0006b0 R   0x1
  GNU_EH_FRAME   0x000644 0x0000000000000644 0x0000000000000644 0x00003c 0x00003c R   0x4
 GNU_STACK      0x000000 0x0000000000000000 0x0000000000000000 0x000000 0x000000 RW  0
  NOTE           0x0002c4 0x00000000000002c4 0x00000000000002c4 0x000038 0x000038 R   0x4
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVl1v6rgW_TXmZYvIcT6ABx5oIafV6Zc43Ht1nion2SG-MjZjO22ZXz9yEiClR8wZaRAKmxV77eXl7Q9urdgqxDlJbkiyHPHG1drMTSOiMB7lujzM70Eo67iUWMLC8lrAg1DNB2gFuwM8hvDIC3gUSgTwpB2Cq7n71FBY4BCm8F3cwJ5vEezBOtwFhC4JXfTPlPbf7i-LhCpkUyKQ6Na6UuigJtFq2AU_HBoFRc0NvH57eL5ZPLw-Z9mP1eZ1s7h5WL2S6GbYXigHOy4UYVPCZkAm_VvYG6Fc1cLsDqXU8K6NLElyqwhjhM1ORPDrRECiJUTndJPlrwfVPu8ruAfTKHA1As_1G8Le6K3hO8JuQTiwtW5kCYXhtoYcC95Y9Pi7EQ4tOA0kpcFWO5JS4Koc_u375i2nw8JhCfkBvj3953W9elg_B3Cn3_ENTZ-r1GgVYRMHvHANl_LQ5b0-NzEUkqstjKvG4liWJFoSltV6h4RlphENYZmUb7vx3uj_Y-EIy_JGyNL_ev8zWQZSljDWULd-t8-gOPMHns-DHTSclSvOQo5S4FvrlrAn79qRt0MFpR2gKoE74F0t5rpRJTeH1klXo8FKG2wnR3Lrukaim62OSPSOnRwOYGHhoBsouAKL6L0dNO_64gcWjeO5RC_AegX0g4WUUt_c62qRiFJK_859g7xEWcF4bHG7Q-UsDMzqnitZQSUkgjvs0bux_PkEhE1ftBVOaDW-VyXuUZWoHKzO4nwfX_IdiXLmAHvtlw79COlk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