export a function with a different name?

export a function with a different name?

Post by Jeremy D. Mulli » Fri, 27 Sep 2002 07:17:43



Is it possible to export a function from an executable (a function named
"foo", for example), but export it with a different symbol name (like
"bar")?

Basically, I would like for a shared object to be able to call the
function "bar" (for which there is no real code, only a symbol that
points to the "foo" function in the executable that loaded the shared
object).

  TIA,
   J.D.

 
 
 

export a function with a different name?

Post by M?ns Rullg? » Fri, 27 Sep 2002 09:42:00



Quote:> Is it possible to export a function from an executable (a function named
> "foo", for example), but export it with a different symbol name (like
> "bar")?

With gcc you can create aliases for symbols.  It might require ELF,
I'm not sure.

--
M?ns Rullg?rd


 
 
 

export a function with a different name?

Post by Jeremy D. Mulli » Fri, 27 Sep 2002 22:36:28


Thanks. Do you know how, or where I could start looking? I've read the
ld documentation and didn't see anything.




> > Is it possible to export a function from an executable (a function named
> > "foo", for example), but export it with a different symbol name (like
> > "bar")?

> With gcc you can create aliases for symbols.  It might require ELF,
> I'm not sure.

 
 
 

export a function with a different name?

Post by Kevin Easto » Fri, 27 Sep 2002 23:44:34




>> Is it possible to export a function from an executable (a function named
>> "foo", for example), but export it with a different symbol name (like
>> "bar")?

> With gcc you can create aliases for symbols.  It might require ELF,
> I'm not sure.

 
 
 

export a function with a different name?

Post by Kevin Easto » Fri, 27 Sep 2002 23:45:57



Quote:> Is it possible to export a function from an executable (a function named
> "foo", for example), but export it with a different symbol name (like
> "bar")?

> Basically, I would like for a shared object to be able to call the
> function "bar" (for which there is no real code, only a symbol that
> points to the "foo" function in the executable that loaded the shared
> object).

If foo is declared as int foo(int, char *), for example:

int (*bar)(int, char *) = foo;

 
 
 

export a function with a different name?

Post by Kasper Dupon » Sat, 28 Sep 2002 00:44:09




> > Is it possible to export a function from an executable (a function named
> > "foo", for example), but export it with a different symbol name (like
> > "bar")?

> > Basically, I would like for a shared object to be able to call the
> > function "bar" (for which there is no real code, only a symbol that
> > points to the "foo" function in the executable that loaded the shared
> > object).

> If foo is declared as int foo(int, char *), for example:

> int (*bar)(int, char *) = foo;

That does not declare bar to be a function, but rather a
pointer pointing to a function.

--
Kasper Dupont -- der bruger for meget tid p? usenet.


 
 
 

export a function with a different name?

Post by Kevin Easto » Sat, 28 Sep 2002 00:50:22





>> > Is it possible to export a function from an executable (a function named
>> > "foo", for example), but export it with a different symbol name (like
>> > "bar")?

>> > Basically, I would like for a shared object to be able to call the
>> > function "bar" (for which there is no real code, only a symbol that
>> > points to the "foo" function in the executable that loaded the shared
>> > object).

>> If foo is declared as int foo(int, char *), for example:

>> int (*bar)(int, char *) = foo;

> That does not declare bar to be a function, but rather a
> pointer pointing to a function.

This is true, but when you call foo, you are also dereferencing a
function pointer (in something like a = foo(10, "blah");, the foo is
evaluating to a pointer to the function foo, which is then implicitly
dereferenced by the action of the () operator).

So the net effect is very similar, with the exception that 'bar' can be
assigned to, whilst 'foo' cannot, and the action of the sizeof and &
operators.

        - Kevin.

 
 
 

export a function with a different name?

Post by Kasper Dupon » Sat, 28 Sep 2002 05:01:10






> >> > Is it possible to export a function from an executable (a function named
> >> > "foo", for example), but export it with a different symbol name (like
> >> > "bar")?

> >> > Basically, I would like for a shared object to be able to call the
> >> > function "bar" (for which there is no real code, only a symbol that
> >> > points to the "foo" function in the executable that loaded the shared
> >> > object).

> >> If foo is declared as int foo(int, char *), for example:

> >> int (*bar)(int, char *) = foo;

> > That does not declare bar to be a function, but rather a
> > pointer pointing to a function.

> This is true, but when you call foo, you are also dereferencing a
> function pointer (in something like a = foo(10, "blah");, the foo is
> evaluating to a pointer to the function foo, which is then implicitly
> dereferenced by the action of the () operator).

> So the net effect is very similar, with the exception that 'bar' can be
> assigned to, whilst 'foo' cannot, and the action of the sizeof and &
> operators.

Yes, but that only works if the caller has defined bar as a pointer
in the included header files. If the caller has defined bar as a
function, you will get into big trouble by your approach. And you
can btw expect the direct function call to be slightly more
efficient.

--
Kasper Dupont -- der bruger for meget tid p? usenet.


 
 
 

export a function with a different name?

Post by M?ns Rullg? » Sat, 28 Sep 2002 05:12:02



Quote:> Thanks. Do you know how, or where I could start looking? I've read the
> ld documentation and didn't see anything.

Quoting GCC info page:

`alias ("TARGET")'
     The `alias' attribute causes the declaration to be emitted as an
     alias for another symbol, which must be specified.  For instance,

          void __f () { /* do something */; }
          void f () __attribute__ ((weak, alias ("__f")));

     declares `f' to be a weak alias for `__f'.  In C++, the mangled
     name for the target must be used.

     Not all target machines support this attribute.

--
M?ns Rullg?rd

 
 
 

1. Different Functions of Same Name in Different Libs

I'm trying to architect a program to eliminate unresolved symbols.  I'm
doing so in the following manner:

//  Header file
class Interface {
public:
    static void myFunc();
//  End Header file

//  Library 1
Interface::myFunc() {assert (0);}

if (a) {   //  'a' would *ONLY* be set in a program that also linked library
2.
    Interface::myFunc();
else {
    otherFunc();
//  End Library 1

//  Library 2
Interface::myFunc() { /* real code */ }
//  End Library 2

As you can see, myFunc should *NEVER* be called from a program that just
uses library 1.  That is correct operation.

However, in some programs, I want to set 'a' and call the myFunc() in
library 2.  Obviously I can't remove the myFunc() dummy declaration from
library 1 or I will get an unresolved symbol problem when linking programs
that only use library 1.

But, I'm afraid that the linker may arbitrarily choose one of the myFunc()'s
and sometimes choose the wrong one.  Is every linker's choice deterministic?
I mean, can I always link library 2 before library 1 and guarantee that the
second myFunc() is chosen when it is linked?  Is there some other linker or
compiler switch I can give to be sure that the library 2 implementation is
used when it is linked?  Or, is making a program like the above guaranteed
to cause cross-platform havoc?

Thanks in advance,
Scott

2. /tmp + swap

3. different file names under different shells

4. NATD configuration with dynamic (tun0) interface

5. Using different IP Names for different Unix apps.

6. SLIP Server Setup

7. Is it possible for a file has different names under different shells

8. 3com905b-fx doesn't connect

9. Resolving different domain names to different pages?

10. Name of the function/script that called the actual function?

11. How to setup named to let two names relate to different ports on thew same machine

12. UUCP name different than HOST name

13. bash [ function name same as command name ]