title: Get section length in linker script
body: <p>I have a makefile which compiles modules written in assembler into a binary file to be loaded by another software. The interesting part of the makefile looks like this: </p>
  
  <pre><code>$(BUILD_ALL)/mod.elf.ld.inc:
      $(LOG)
      $Qecho "#define MOD(id, addr, ...) . = ALIGN(4); .rodata.id : { id ## _start = .; *(.text.id); id ## _size = ABSOLUTE(. - id ## _start); }" &gt; $@
      ...
  
  $(BUILD_ALL)/mod.data.o.inc:
      $(LOG)
      $Qecho "#define MOD(id, addr, ...) .int addr; .int id ## _start; .int id ## _size; .int 0; " &gt; $@
      ...
  
  $(BUILD_ALL)/mod.text.elf.ld.inc:
      $(LOG)
      $Qecho "#define MOD(id, addr, ...) .text.id addr : { *(.text.id); } " &gt; $@
      ...
  
  $(BUILD_ALL)/mod.text.o.inc:
      $(LOG)
      $Qecho "#define MOD(id, addr, ...) .section .text.id; __VA_ARGS__" &gt; $@
      ...
  </code></pre>
  
  <p>What this does is it lets me define a module looking like this: </p>
  
  <pre><code>MOD(unique-id, 0x80001234, 
      li r3, 0; 
      li r4, 0; 
      ...
  )
  </code></pre>
  
  <p>This then places the actual code inside the text section of the ELF file (see rule for mod.text.o.inc), will tell the linker to load it to the address I entered (rule for mod.text.elf.ld.inc), add loading address, size, file address to the data section of the ELF (mod.data.o.inc), and mod.elf.ld.inc is responsible to determine the size of the code. </p>
  
  <p>The output for the Data section basically looks like this: </p>
  
  <pre><code>.int 0x80001234
   .int unique-id_start
   .int unique-id_size
   .int 0
  
  .int 0x80003864
   .int mod1_start
   .int mod1_size
   .int 0
  
  .int 0x80003ea4
   .int mod2_start
   .int mod2_size
   .int 0
  </code></pre>
  
  <p>I am now trying to modify this "build environment" so I have another command "IMOD" which I can use to "set" a destination address and make the  compiler place any other "DMOD" assignments not at an address I need to enter (like 0x80001234 in my example), but automatically at the end. </p>
  
  <p>I tried copying MOD to IMOD and DMOD like this: </p>
  
  <pre><code>$(BUILD_ALL)/mod.elf.ld.inc:
      $(LOG)
      $Qecho "#define MOD(id, addr, ...) . = ALIGN(4); .rodata.id : { id ## _start = .; *(.text.id); id ## _size = ABSOLUTE(. - id ## _start); }" &gt; $@
      $Qecho "#define IMOD(id, addr, ...) . = ALIGN(4); .rodata.id : { id ## _start = .; *(.text.id); id ## _size = ABSOLUTE(. - id ## _start); last_size = addr + SIZEOF(.rodata.id); }" &gt;&gt; $@
      $Qecho "#define DMOD(id, ...) . = ALIGN(4); .rodata.id : { id ## _start = .; *(.text.id); id ## _size = ABSOLUTE(. - id ## _start); last_size = last_size + SIZEOF(.rodata.id); }" &gt;&gt; $@
      ...
  </code></pre>
  
  <p>so that it adds the section size to a counter, and then added this: </p>
  
  <pre><code>$(BUILD_ALL)/mod.data.o.inc:
      $(LOG)
      $Qecho "#define MOD(id, addr, ...) .int addr; .int id ## _start; .int id ## _size; .int 0; " &gt; $@
      $Qecho "#define IMOD(id, addr, ...) .int addr; .int id ## _start; .int id ## _size; .int 0; " &gt; $@
      $Qecho "#define DMOD(id, ...) .int last_size; .int id ## _start; .int id ## _size; .int 0; " &gt; $@
      ...
  </code></pre>
  
  <p>However, the resulting section (generated from mod.data.o.inc) does not have increasing addresses for ".int last_size", not even a wrong address, but the (section) length of the whole ELF Data section - not just the one section I want it to. </p>
  
  <p>Basically, if I call IMOD(test, 0x80001000, nop;) and then DMOD(test2, nop;), I want the address 0x80001004 written to the ELF data section. Instead, the code I (tried to) write writes the ELF data section length instead. </p>
  
  <p>I am sorry if my explanation is shit - English is not my mother tongue, the build environment hasn't been written by me (so I don't exactly know what everything does) and I don't have much experience with how a linker works. </p>
  
  <p>I can give <a href="https://github.com/Chadderz121/wii-ct-code/blob/master/bad1/bad1Data/mod/makefile.mk" rel="nofollow noreferrer">a link to the build environment / code</a> but that is very specific to that system / software so I don't know if this is useful. </p>