title: Hardcoded byte array ends up as data when it exceeds 0xF in length
body: <p>My goal is to have my <code>.rdata</code> sections to contain as less stuff as possible and let the compiler use the text/code sections whenever it can. Right now I have a small issue which I hope someone could help me with. In clang and GCC, when you would compile the following C++ code (note the array is 15 bytes in length):</p>
  <pre><code>#include &lt;windows.h&gt;
  
  void _start() {
    unsigned char bytes[] = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF};
    MessageBoxA(nullptr, (char*)bytes, &quot;Hi&quot;, MB_OK);
  }
  </code></pre>
  <p>This compiles as I would want it to. All hardcoded data is nicely embedded into the code itself (it uses immediate movs) and so nothing is added to any data sections, no references to any data section is made. Here is the IDA PRO decompilation:
  <a href="https://i.stack.imgur.com/rmzPL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rmzPL.png" alt="enter image description here" /></a></p>
  <p>If you would add another byte to the array and have it exceed the 15 bytes in length (16 bytes in length totally now), like so:</p>
  <pre><code>#include &lt;windows.h&gt;
  
  void _start() {
    unsigned char bytes[] = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF, 0x10};
    MessageBoxA(nullptr, (char*)bytes, &quot;Hi&quot;, MB_OK);
  }
  </code></pre>
  <p>Then the compiler behaves in a way I want to get rid of. That is that it doesn't embed it anymore in the code, but instead moves everything inside the data section (the <code>unk_100402000</code>, which is inside <code>.rdata</code>):</p>
  <p><a href="https://i.stack.imgur.com/MB3nC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MB3nC.png" alt="enter image description here" /></a></p>
  <p>I tested this code against multiple compilers. MSVC embed these hardcoded byte arrays always nicely in the code itself, no matter how long it is. Since I would really like to keep using Clang and GCC, I hope there is a solution to this.</p>
  <p>The reason I need to limit usage of the <code>.rdata</code> section, is that I am doing an experimental project where any reference or access outside the text/code section will result in a major performance hit. Around 1000x slower per access outside text/code section. Because of this, I really need to have the compiler use the text/code section as much as possible.</p>
  <h3>Question</h3>
  <p>How can I make Clang/GCC compile 15+ long hardcoded byte arrays so that it uses immediate mov operations on the stack instead of using the <code>.rdata</code> section? If it is not possible through compiler options, could it be possible to change this behaviour through a compiler pass? I am also fine with any dirty hacks I need to apply to Clang in order to make this work.</p>
  <p>I know I could split my longer byte arrays up into smaller individual ones, but this is not the solution I am looking for.</p>
  <p>Thanks in advance for any help given!</p>
  <p>Clang version: 4.0.1</p>
  <h2>Best solution for now</h2>
  <p>As you can see in the following image. The big hardcoded byte array is compiled as multiple immediate mov instructions on the stack and so never references anything from a data section. This is exactly what I am looking for. Are there multiple ways to achieve this same behaviour?</p>
  <p><a href="https://i.stack.imgur.com/Tz0FC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Tz0FC.png" alt="enter image description here" /></a></p>