Skip to contentSkip to conversation

Accessing static variable from outside

DISCUSSION Rajat Jain 4 participants 4 messages Jun 16, 2007 – Jun 17, 2007

lore
Share & tools
lore

Rajat Jain

author
#0 lore raw

Hi,

I understand that this is off-topic, but still if some one has any ideas ...

I have a global variable that is defined static in an object file (say 1.o). Now I want to be able to get the value of this variable in ANOTHER object file (say 2.o) without changing the source of 1.c.

Any ideas, anyone? I know that as per C, this is not possible. But may be some clever hack?

Thanks,

Rajat
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

#1 lore raw
Quoted 11 lines · Rajat Jain

Hi,

I understand that this is off-topic, but still if some one has any ideas
...

I have a global variable that is defined static in an object file (say 1.o). Now I want to be able to get the value of this variable in ANOTHER object file (say 2.o) without changing the source of 1.c.

Any ideas, anyone? I know that as per C, this is not possible. But may be some clever hack?

Depending on how the variable gets used the compiler might even completely optimize it away.

Quoted 3 lines

Thanks,

Rajat

cu
Adrian

Signature · 10 lines
       "Is there not promise of rain?" Ling Tan asked suddenly out
        of the darkness. There had been need of rain for many days.
       "Only a promise," Lao Er said.
                                       Pearl S. Buck - Dragon Seed

-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
#2 lore raw
In reply to Rajat Jain

AFAIK one way to be able to access a static variable is using dynamic linking lodader. u can read man pages of dlopen() and family. and here is example of its usage http://www.slimy.com/~jleonard/src/dlopen.html

/tejas

Quoted 23 lines · Rajat Jain

Hi,

I understand that this is off-topic, but still if some one has any ideas
...

I have a global variable that is defined static in an object file (say 1.o). Now I want to be able to get the value of this variable in ANOTHER object file (say 2.o) without changing the source of 1.c.

Any ideas, anyone? I know that as per C, this is not possible. But may be some clever hack?

Thanks,

Rajat

--
To unsubscribe from this list: send an email with "unsubscribe kernelnewbies" to [email protected] Please read the FAQ at http://kernelnewbies.org/FAQ

#3 lore raw

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

A: http://en.wikipedia.org/wiki/Top_post
Q: Were do I find info about this thing called top-posting?
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing in e-mail?

A: No.
Q: Should I leave quotations after my reply?

Quoted 3 lines · tejas khatiwala

AFAIK one way to be able to access a static variable is using dynamic linking lodader. u can read man pages of dlopen() and family. and here is example of its usage http://www.slimy.com/~jleonard/src/dlopen.html

The example doesn't apply:

- - dlopen() is a userspace function and can't be used in kernel
- - dynamic linking can only be used to link exported (i.e. non-static) symbols

Like Adrian Bunk already explained: there are dirty tricks to get the address of a static variable, but they will all fail when the compiler decides to optimise the variable away (which it is completely free to do because the "static" keyword *guarantees* that the variable will not be used outside that particular compilation unit).

Erik

- --
They're all fools. Don't worry. Darwin may be slow, but he'll eventually get them. -- Matthew Lammers in alt.sysadmin.recovery -----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFGdQrn/PlVHJtIto0RAsWkAJ0Qk9txqXYxwLMEeioj8z4T+74h5QCgiH51 7yl9an3I/1EPWWkw4eFqOrA=
=bEkd
-----END PGP SIGNATURE-----
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs