Thread: Extern variables in GAS?

  1. #1
    Registered User Kernel Sanders's Avatar
    Join Date
    Aug 2008
    Posts
    61

    Extern variables in GAS?

    For my embedded systems course we are writing assembly wrappers for syscalls. When in error we need to set the global errno (defined in the class's own files not the normal libc) as expected. How do you reference external variables in .S files?

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I always found it easier to declare them in the C files, but it shouldn't be anything special. Just declare it in the S file, and call it as an extern from the C file. I have done it using MASM before.

  3. #3
    Registered User Kernel Sanders's Avatar
    Join Date
    Aug 2008
    Posts
    61
    Quote Originally Posted by master5001 View Post
    I always found it easier to declare them in the C files, but it shouldn't be anything special. Just declare it in the S file, and call it as an extern from the C file. I have done it using MASM before.
    I unfortunately need to work within the confines of the lab. There are errno.h and errno.c which declare and initialize errno, we just have to write read.S, which must set the already declared errno. Do you know to declare the extern var in the .S file?

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Don't you just use the extern keyword? This is Mats chance to shine. Off hand I don't know.

  5. #5
    Registered User Kernel Sanders's Avatar
    Join Date
    Aug 2008
    Posts
    61
    Quote Originally Posted by master5001 View Post
    Don't you just use the extern keyword? This is Mats chance to shine. Off hand I don't know.
    Well it's assembly so you don't declare variables as such. We've tried

    Code:
    EXTERN errno
    EXTERN errno
    extern errno
    Google dug all three up. All gave the error

    Code:
    bad instruction 'extern errno'
    or similar

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Psh... You obviously only have ever used one assembler before:

    GNU Assembler (gas)
    GAS previously had "extern" functioning similar to NASM. My quess is that the authors realized this problem, and changed its behavior, but I am just speculating.

    Currently, any undefined symbol that occurs in source is automatically declared as external. This means there are no extern declarations needed in header, but it also means that typos in names are not checked during compilation. In the case of typos, you get confusing error messages during linking, or even bad executable if you are unlucky.

    However, gas is more a back-end assembler than "human" assembler so such behavior may be appropriate from this point of view.
    Cool. So just don't define it and it should be assumed to be an external variable. Problem solved.

  7. #7
    Registered User Kernel Sanders's Avatar
    Join Date
    Aug 2008
    Posts
    61
    Finally found a solution. There needs to be a

    Code:
    _errno
         .word     errno
    block. _errno is then synonymous with the address of the external symbol errno. It worked as described, you just can't shove the external symbol into an instruction. There needs to be a label to reference it
    Last edited by Kernel Sanders; 10-20-2008 at 10:15 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. extern const?Please help
    By huwan in forum C++ Programming
    Replies: 10
    Last Post: 08-12-2008, 04:53 AM
  2. need help with extern pointers.
    By broli86 in forum C Programming
    Replies: 17
    Last Post: 04-11-2008, 09:16 AM
  3. Declaring an variable number of variables
    By Decrypt in forum C++ Programming
    Replies: 8
    Last Post: 02-27-2005, 04:46 PM
  4. Global variables keep resetting
    By earnshaw in forum C Programming
    Replies: 6
    Last Post: 01-19-2005, 10:35 AM
  5. functions to return 2 variables?
    By tim in forum C Programming
    Replies: 5
    Last Post: 02-18-2002, 02:39 PM