<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - RISC-V binary does not respect page alignment"
   href="https://bugs.llvm.org/show_bug.cgi?id=46112">46112</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>RISC-V binary does not respect page alignment
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>lld
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>unspecified
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>Other
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>other
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>ELF
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>xxuejie@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, smithp352@googlemail.com
          </td>
        </tr></table>
      <p>
        <div>
        <pre>A sample code to reproduce this bug can be found at here:
<a href="https://gist.github.com/xxuejie/2825d4d0526c09f742d1ec4491deeaff">https://gist.github.com/xxuejie/2825d4d0526c09f742d1ec4491deeaff</a>, it just
contains some sillylogic so as to make sure the binary contains an executable
section, a writable section and a readable section.

First, let's compile the code:

clang --target=riscv64 -mno-relax -c -o test.o test.c

Then let's try link the code(I'm testing this on Linux but I would assume the
same bug applies elsewhere):

ld.lld test.o -o test

If we check the ELF generated here, it contains the following sections:

  LOAD           0x0000000000000000 0x0000000000010000 0x0000000000010000
                 0x00000000000001d8 0x00000000000001d8  R      0x1000
  LOAD           0x00000000000001d8 0x00000000000111d8 0x00000000000111d8
                 0x000000000000011e 0x000000000000011e  R E    0x1000
  LOAD           0x00000000000002f8 0x00000000000122f8 0x00000000000122f8
                 0x0000000000000000 0x0000000000000004  RW     0x1000

We can see that latter sections start at some offset in the designated pages,
not the beginning of the page. This result won't change if we use --no-nmagic
which I suppose should fix the problem. I tried both LLVM 10 and the latest git
HEAD, they both have this behavior.

So am I understanding page alignment rule wrong here? Or is there anyway we can
make sure in each section, the generated code starts from the beginning of the
page?

Many thanks for the help</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>