Accessing section attributes from assembly (or C)

Christopher Bahns chris@bahns.com
Wed Jul 5 02:26:00 GMT 2000


How can I access information about a section from within my C or
assembly code? Specifically, I need to know the starting address and
size (or ending address) of a section. Now, I already know how to do
this as a C-language variable, but I think I need access to these values
as literals. Hold on a sec and I'll show why...

Here is an excerpt from my linker script:
__________________________________________________________________
  .my_tables : {
        __tables_start = .;
        *(.my_tables)
        __tables_end = .;
        __tables_size = __tables_end - __tables_start;
  } > rom
__________________________________________________________________

And here is some assembly code (inside a C file):
__________________________________________________________________
__asm__(
"   .extern __tables_size\n"
"DATA_Table:\n"
"            DC.B  0x01\n"                  /* UBYTE table format
version */
"            DC.B  0x00\n"                  /* UBYTE unused_a for
alignment */
"            DC.L  __tables_size\n"
)
__________________________________________________________________

Note that the linker would have to put the final (literal) value of
"__tables_size" directly at this location at link time. I can't really
use a variable to refer to "__tables_size" and copy the value to this
location at run time (at least not easily). The above code generates an
error in the assembler: "Error: undefined symbol __tables_size in
operation".

It is possible to do this with MRI (I am converting from MRI to GNU), so
I really hope GNU has an easy way to do this as well. I've looked
through some of the documentation but not thoroughly. If you can point
me to the right part of the documentation please do. I *really* don't
want to use MRI-compatibility mode. I've already gotten most of it
converted to GNU and would like to move totally from the old syntax. If
GNU simply cannot do some of the nice things that MRI can do, then I'll
be sorely disappointed.

Here is the corresponding MRI-compatible assembly code:
__________________________________________________________________
#pragma asm
   XDEF   _DATA_Table
_DATA_Table:
            DC.B  $01                  /* UBYTE table format version */
            DC.B  $00                  /* UBYTE unused_a for alignment
*/
            DC.L  .SIZEOF.(.my_tables)
#pragma endasm
__________________________________________________________________

As you can see with MRI it is easy. I can have the linker just stick the
size of the ".my_tables" section directly at the desired location, using
the ".SIZEOF." directive. MRI also has a ".STARTOF." directive for
obtaining the start address of a section.


Can anyone help? (Thanks to those that helped me with other stuff...
almost there now.)
Chris


More information about the crossgcc mailing list