title: How linker place symbols in a memory section
body: <p>I'm trying to figuring out how memory layout is generated by linker. I have basic knowledge about memory sections and their attributes. My question is how linker place symbols inside a specific section?</p>
  <p>For example; a C-like array elements are guaranteed to place consecutively in the <code>data</code> section.</p>
  <pre><code>+---------+
  |   A[0]  | 0x0000 &lt;- An arbitrary memory address
  +---------+
  |   A[1]  | 0x0004
  +---------+
  |   A[2]  | 0x0008
  +---------+
  |         |
  |   ...   |
  |         |
  +---------+
  </code></pre>
  <p>Apart from the local variables that placed in stack, how other variables are placed by the linker?</p>
  <p>Let's assume I have two separate module <code>foo</code> and <code>bar</code>, and all of their variables will be placed to the memory without discarding by optimizations.</p>
  <pre><code>Module foo:
      var a;
      var b;
  
  
  Module bar:
      var a;
      var b;
  </code></pre>
  <p>Is there any rule or convention for this case such as; same module variables are placed consecutively or symbol names generated by the compiler are placed alphabetically?</p>
  <pre><code>+-----------+
  |   a(foo)  | \ 
  +-----------+  -&gt; Same module variables placed consecutively
  |   b(foo)  | /
  +-----------+
  |   a(bar)  | \
  +-----------+  -&gt; Same module variables placed consecutively
  |   b(bar)  | /
  +-----------+
  |           |
  |    ...    |
  |           |
  +-----------+
  
  
  +-----------+
  |   a(foo)  | \ 
  +-----------+  -&gt; Variables placed alphabetically
  |   a(bar)  | /
  +-----------+
  |   b(foo)  | \
  +-----------+  -&gt; Variables placed alphabetically
  |   b(bar)  | /
  +-----------+
  |           |
  |    ...    |
  |           |
  +-----------+
  </code></pre>
  <p>I've checked both GCC and LLVM documentations but couldn't find any information about this topic. Is there any place where I can look for more detailed information about address assignments or memory layout optimizations?</p>