title: Using .reloc from assembly
body: <p>This is a cut-down version of the problem I'm facing on AArch64:</p>
  
  <p>I've this macro that keeps dumping some data into a section.</p>
  
  <pre>
  #define GEN_DATA(_type) \
      .pushsection .mydata_##_type, "aw"; \
      .ifndef start; \
      start:; \
      .endif; \
      .word 1; \
      .reloc end_loc, R_AARCH64_ABS64, .; \
      .popsection
  </pre>
  
  <p>I eventually wanted to capture the start and end of similar types in a structure like this:</p>
  
  <pre>
      .pushsection .head, "aw"
      .quad start
      end_loc:
      .quad 0
      .popsection
  </pre>
  
  <p>I can track where the section starts with the <code>start</code> symbol. I don't know in advance how many invocations of <code>GEN_DATA()</code> will be there in the build, so I can't define an <code>end</code>. I don't know how many <code>_type</code>s of section will be used either, so can't place guard symbol script. Therefore I decided to leave a relocation entry for <code>end_loc</code> so that linker will eventually fixup where the whole section ends. Yes, there will be multiple relocation entries for the same <code>end_loc</code>, but because they're absolute relocations, I reckon they don't conflict.</p>
  
  <p>I had hunch, but in the final binary, <code>end_loc</code> is getting fixed up with a wrong address. I'll blame it on multiple relocation entries, but the weird thing is that, everything is OK if I also add a dummy, extra relocation entry too -- I.e. I modify the structures above to:</p>
  
  <pre>
  #define GEN_DATA(_type) \
      .pushsection .mydata_##_type, "aw"; \
      .ifndef start; \
      start:; \
      .endif; \
      .word 1; \
      .reloc end_loc, R_AARCH64_ABS64, .; \
      <b>.reloc dummy_loc, R_AARCH64_ABS64, .; \</b>
      .popsection
  </pre>
  
  <p>and:</p>
  
  <pre>
      .pushsection .head, "aw"
      .quad start
      end_loc:
      .quad 0
      <b>dummy_loc:
      .quad 0</b>
      .popsection
  </pre>
  
  <p>So I'm wondering:</p>
  
  <ul>
  <li><p>Why is <code>end_loc</code> getting fixed up wrongly? What's wrong with multiple absolute relocation entries, if at all? Isn't the linker expected to go through them in order, and the last one falls into effect?</p></li>
  <li><p>Why would simply adding a dummy relocation make everything right?</p></li>
  </ul>
  
  <p>Basically, what's going on?!</p>
  
  <p>And finally, are there any alternatives approaches that I could try?</p>
  
  <p>EDIT: I've now pushed the sample code to a <a href="https://bitbucket.org/jeenuv/duplicate_reloc/" rel="nofollow noreferrer">Git repository</a>. Use <code>make</code> and <code>make broken=1</code> to view the disassembly. Needs <a href="https://releases.linaro.org/components/toolchain/binaries/latest/aarch64-linux-gnu/gcc-linaro-6.2.1-2016.11-x86_64_aarch64-linux-gnu.tar.xz" rel="nofollow noreferrer">Linaro AArch64 tool chain</a> in <code>$PATH</code>.</p>