title: How to use subsections in a c program?
body: <p>Code that belongs to the same section but different subsections has its order of placement defined by the subsection number.  I need to use this feature in a c program - i.e. I need two functions to be in the same section and in a particular order.  GCC re-orders functions in the same section as it pleases, so that is why I need subsections.  Here is the syntax for sections - I can't figure out how to specify subsections using the __attribute__ syntax.</p>
  
  <pre><code>void func1() __attribute__ ((section ("mysection")));
  </code></pre>
  
  <p>See Jester's comment below for assembly syntax.  I am using gcc, so I am assuming gas assembler?</p>
  
  <p>Here is a long explanation of why I have gotten to the point of needing subsections.  Maybe one of my conclusions along the way was incorrect and you can help me avoid this.</p>
  
  <p>Q: Why not create separate sections and load them contiguously?</p>
  
  <p>A: I have a separate problem where I need to be able to figure out the exact beginning address of my functions ahead of time.</p>
  
  <p>Q: Why do you need to know the address?</p>
  
  <p>A: I want to align some code in my functions (not the function itself) to a particular alignment</p>
  
  <p>Q: Why not use .align?</p>
  
  <p>A: I have found that using .align inside a c function for some reason forces that function itself to be aligned to that value, and I do not want that - so I have come up with an ugly macro alternative to the .align directive:</p>
  
  <pre><code>b 1f
  
  . = . + (1 &lt;&lt; #alignment") - (("#section_start" + .) &amp; ((1 &lt;&lt; "#alignment") - 1))
  
  1:
  </code></pre>
  
  <p>Q: Why not use labels to calculate your current location? Or a label in the loader file?</p>
  
  <p>A: Assembler doesn't let me - I have to use the dot operator.</p>
  
  <p>Q: Tell me again why you need section_start here?</p>
  
  <p>A: The dot operator is relative to the start of the section, it is not the absolute address</p>
  
  <p>Q: Why are you trying this low level stuff in C this is dumb</p>
  
  <p>A: I agree this is dumb, but play along.</p>