title: One or two weird linking errors with some inline assembly
body: <p>I have the following short application (The original is taken from: <a href="https://www.aldeid.com/wiki/X86-assembly/Instructions/str" rel="nofollow noreferrer">https://www.aldeid.com/wiki/X86-assembly/Instructions/str</a>) I just modified it (tried to...) to make it compilable on linux too.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  unsigned char mema[4] = {0, 0, 0, 0};
  
  void test4 (void)
  {
  
      asm (
          "str mema\n"
      );
  
      printf ("\n[+] Test 4: STR\n");
      printf ("STR base: 0x%02x%02x%02x%02x\n", mema[0], mema[1], mema[2], mema[3]);
  
      if ((mema[0] == 0x00) &amp;&amp; (mema[1] == 0x40))
          printf ("Result  : VMware detected\n\n");
      else
          printf ("Result  : Native OS\n\n");
  }
  
  int main () {
  
      test4();
  }
  </code></pre>
  
  <p>and when I try to compile it:</p>
  
  <pre><code>$ gcc -ggdb -O0 -fPIC -x c ./pill.c -o pill                                                                                                       
  </code></pre>
  
  <p>it gives me the wird error:</p>
  
  <pre><code>/usr/bin/ld: /tmp/ccDhGo05.o: relocation R_X86_64_32S against symbol `mema' can not be used when making a PIE object; recompile with -fPIC
  /usr/bin/ld: final link failed: Nonrepresentable section on output
  collect2: error: ld returned 1 exit status
  
  </code></pre>
  
  <p>However the same thing works on an online compiler: <a href="https://gcc.godbolt.org/z/RYWjy8" rel="nofollow noreferrer">https://gcc.godbolt.org/z/RYWjy8</a> </p>
  
  <p>... except if I move the variable <code>mema</code> to be a local variable in the function <code>test4</code> when it complains both places about </p>
  
  <pre><code>undefined symbol `mema' 
  </code></pre>
  
  <p>And another weirdity: same stuff works when compiled with clang.</p>
  
  <p>Any idea why this happens? (Not considering the required privilege level for <code>STR</code>, and other thingies, I am just interested in the linking error and why does it work on one system / compiler and not on another.</p>