title: Is it possible to force GCC to pad string constants in .rodata
body: <p>I'm working on porting some code to an environment with more strict alignment requirements than x86 has, but I'm changing/testing on an x86 Linux machine for the time being due to this being easier for hardware access reasons, among other things.</p>
  
  <p>I've distilled the first problem that I've run into into the following   concise example:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  
  #define BUFFER_SIZE 1024
  #define DMQUOTE_LOG "DMQUOTELOG"
  
  void aFunction (const char *configPath)
  {
      char LogFilename[BUFFER_SIZE] __attribute ((aligned));
  
  //     printf ("A\n");
      strcpy (LogFilename, configPath);
      strcat (LogFilename, DMQUOTE_LOG);
  
      printf ("Log: %s\n", LogFilename);
  }
  
  int main (int argc, char **argv)
  {
      __asm__("pushf\n"  
              "orl $0x40000, (%esp)\n"  
              "popf");  
  
      aFunction ("");
  
      return 0;
  }
  </code></pre>
  
  <p>Running this code as is provides the expected output. However, uncommenting the other printf causes a bus error to trigger on the strcat line.</p>
  
  <p>It looks to me as if the reason for this is that by introducing a second string constant, the constant from the define is shifted so that it's not aligned. This is upheld by noticing that if the string constant is changed from "A\n" to "AAA\n", everything works again (and magically gcc replaces the call to printf with a call to puts and drops the \n from the constant).</p>
  
  <p>Is there some nice way to make gcc insert extra padding between all of the string constants that it's inserting into the .rodata section so that things align properly? </p>
  
  <p>[EDIT]  </p>
  
  <p>As mentioned by fucanchik below, here's what the .rodata section of the above is (with the extra printf enabled):</p>
  
  <pre><code>    .file   "sample.c"
      .section    .rodata
  .LC0:
      .string "A"
  .LC1:
      .string "DMQUOTELOG"
  .LC2:
      .string "Log: %s\n"
      .text
  .globl aFunction
  ...
  </code></pre>
  
  <p>There is no alignment forced, which makes sense because I'm compiling under x86, which doesn't strictly require it. Naturally, modifying the assembler to this has the desired effect. However, I can't see a way to get gcc to apply this on its own on the fly. This may of course be moot if glibc itself can't handle being run in this mode in the general case, though.</p>
  
  <pre><code>    .file   "sample.c"
      .section    .rodata
  .LC0:
      .string "A"
      .align 4,0
  .LC1:
      .string "DMQUOTELOG"
  .LC2:
      .string "Log: %s\n"
      .text
  .globl aFunction
  ...
  </code></pre>