Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a .def file equivalent on Linux for controlling exported function names in a shared library?

I am building a shared library on Ubuntu 9.10. I want to export only a subset of my functions from the library. On the Windows platform, this would be done using a module definition (.def) file which would contain a list of the external and internal names of the functions exported from the library.

I have the following questions:

  1. How can I restrict the exported functions of a shared library to those I want (i.e. a .def file equivalent)

  2. Using .def files as an example, you can give a function an external name that is different from its internal name (useful for prevent name collisions and also redecorating mangled names etc)

  3. On windows I can use the EXPORT command (IIRC) to check the list of exported functions and addresses, what is the equivalent way to do this on Linux?

like image 227
morpheous Avatar asked May 15 '10 08:05

morpheous


People also ask

What is a DEF file in Python?

See also. A module-definition or DEF file (*.def) is a text file containing one or more module statements that describe various attributes of a DLL. If you are not using the __declspec (dllexport) keyword to export the DLL's functions, the DLL requires a DEF file.

What is the use of Def module in linker?

Module-definition (.def) files provide the linker with information about exports, attributes, and other information about the program to be linked. A .def file is most useful when building a DLL.

How to export functions in a c++ file?

If you are exporting functions in a C++ file, you have to either place the decorated names in the DEF file or define your exported functions with standard C linkage by using extern "C". If you need to place the decorated names in the DEF file, you can obtain them by using the DUMPBIN tool or by using the linker /MAP option.

What are the sections of a DEF file?

A standard DEF file contains mainly following sections and order of statement is also important. Here we will take a sample DEF file to describe the various sections of the file. In the header part, the version of DEF, Design name, Technology name, Units and Dia area are mentioned.


4 Answers

The most common way to only make certain symbols visible in a shared object on linux is to pass the -fvisibility=hidden to gcc and then decorate the symbols that you want to be visible with __attribute__((visibility("default"))).

If your looking for an export file like solution you might want to look at the linker option --retain-symbols-file=FILENAME which may do what you are looking for.

I don't know an easy way of exporting a function with a different name from its function name, but it is probably possible with an elf editor. Edit: I think you can use a linker script (have a look at the man page for ld) to assign values to symbols in the link step, hence giving an alternative name to a given function. Note, I haven't ever actually tried this.

To view the visible symbols in a shared object you can use the readelf command. readelf -Ds if I remember correctly.

like image 158
CB Bailey Avatar answered Oct 22 '22 21:10

CB Bailey


How can I restrict the exported functions of a shared library to those I want (i.e. a .def file equivalent)

Perhaps you're looking for GNU Export Maps or Symbol Versioning

g++ -shared spaceship.cpp -o libspaceship.so.1 -Wl,-soname=libspaceship.so.1 -Wl, --version-script=spaceship.expmap

like image 31
Dmitry Yudakov Avatar answered Oct 22 '22 23:10

Dmitry Yudakov


gcc also supports the VC syntax of __declspec(dllexport). See this.

like image 23
Alex Budovski Avatar answered Oct 22 '22 23:10

Alex Budovski


Another option is to use the strip command with this way:

strip --keep-symbol=symbol_to_export1 --keep-symbol=symbol_to_export2 ... \
     libtotrip.so -o libout.so
like image 45
Sam Liao Avatar answered Oct 22 '22 21:10

Sam Liao