How to reference a C symbol from an assembly file

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

I'm trying desperately to access the address of a C structure in a assembly file.

E.g. if I have the .c file:

typedef struct {
    uint8_t a;
    uint8_t b;
} status_t;
volatile status_t status;

and the .S file

.extern status

    .global TWI_vect
TWI_vect:
    push r30
    push r31

    ldi r30, lo8(status)
    ldi r31, hi8(status)
...
    pop r31
    pop r30
    reti

then I get the linker error (undefined symbol 'status').
But the symbol is defined; if I list the symbols with avr-nm, then I get:

Quote:
008000ae b status.2474

I'm aware of the trick to reserve a register to be used only in the ISR, but I don't want to limit the number of registers in the C code.

I have also tried to define the structure as:

volatile status_t status asm("ASMSTRUCT");

but neither ASMSTRUCT was defined.

How is it possible to get the base address of my structure in a .S file?

Cheers,
Thomas

pycrc -- a free CRC calculator and C source code generator

Last Edited: Thu. Jun 19, 2008 - 01:16 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I just copied your two snippets into a .c and a .S in an AVR Studio project and it built and linked without error?

Build started 18.6.2008 at 17:27:05
avr-gcc.exe  -mmcu=atmega88 -Wall -gdwarf-2  -Os -fsigned-char -MD -MP -MT ctest.o -MF dep/ctest.o.d  -c  ../ctest.c
avr-gcc.exe  -mmcu=atmega88 -mmcu=atmega88 -Wall -gdwarf-2  -Os -fsigned-char -MD -MP -MT test.o -MF dep/test.o.d  -x assembler-with-cpp -Wa,-gdwarf2 -c  ../test.S
avr-gcc.exe -mmcu=atmega88 -Wl,-Map=ctest.map ctest.o test.o     -o ctest.elf

and also

C:\Documents and Settings\CLawson\My Documents\ctest\default>avr-nm test.o
00000000 T TWI_vect
         U status

C:\Documents and Settings\CLawson\My Documents\ctest\default>avr-nm ctest.o
0000003e a __SP_H__
0000003d a __SP_L__
0000003f a __SREG__
         U __addsf3
         U __do_clear_bss
         U __do_copy_data
         U __floatunsisf
         U __mulsf3
00000000 a __tmp_reg__
00000001 a __zero_reg__
00000000 T main
00000002 C status

I notice the symbol type in my case is 'C' and the symbol is 'status' while in your case it is 'b' and the symbol is listed as 'status.2474'

Are you using C++ rather than C - I'm just wondering why the symbol name appears to have effectively been "mangled". I'd also wonder what happens if you refer to it in the .S as status.2474 ?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

tpircher wrote:
But the symbol is defined; if I list the symbols with avr-nm, then I get the symbol:
Quote:
008000ae b status.2474
The lower case b indicates a local symbol in the bss section.
Would status be static?

Iluvatar is the better part of Valar.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thanks for the replies.

status is volatile and has external linkage. I'm not compiling in C++, but I think I have figured out what triggers the name mangling.

I did not mention in my first post that I was compiling with -fwhole-program --combine. Turning these options off, I can access the symbol by its name.

I'm rather intrigued by this. Why does GCC mangle names in this particular case?

Cheers,
Thomas

pycrc -- a free CRC calculator and C source code generator

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

tpircher wrote:
status is volatile and has external linkage. I'm not compiling in C++, but I think I have figured out what triggers the name mangling.

I did not mention in my first post that I was compiling with -fwhole-program --combine. Turning these options off, I can access the symbol by its name.

I'm rather intrigued by this. Why does GCC mangle names in this particular case?

You have effectively made all your variables static.
There is the possibility that it will gcc will be processing
distinct static variables with the same name.
Tacking on a number allows gcc to distinguish them.

Since the variable are all local,
none of them are visible to assembly.

Iluvatar is the better part of Valar.