Topic: Attributes useful for shared objects (import, export,


Author: Antony Polukhin <antoshkka@gmail.com>
Date: Mon, 18 Jan 2016 22:37:11 +0300
Raw View
--94eb2c032e601208760529a0e19f
Content-Type: text/plain; charset=UTF-8

Hello,

There's a bunch of problems with dynamic loading entities defined in C++
shared libraries:
1) No portable way to define symbol as being imported/exported. All the C++
libraries are forced to reinvent BOOST_SYMBOL_EXPORT and
BOOST_SYMBOL_IMPORT like macro [1].
2) All modern binary formats store symbols in sections that are not
currently controlled from C++, but provide a rich set of useful
functionality. For example Linux kernel developers use sections to store
pointers to specific entry points (macro __init
http://lxr.free-electrons.com/source/include/linux/init.h), store data that
must be changed at load (https://lwn.net/Articles/164121/) and so on.


I'm proposing to add following attributes to C++ Standard:
* [[import]] - for importing symbol (same as BOOST_SYMBOL_IMPORT)
* [[export]] - for exporting symbol in default section with default C++
mangling (same as BOOST_SYMBOL_EXPORT)
* [[section "section name"]] - places the symbol into section with
specified name, constructs section if it does not exist(close to
BOOST_DLL_SECTION [2])

Here are some usage examples:

namespace boost {
    // exporting function with C++ mangled name.
    [[export]] void export_me(std::sting, variant<int, short>);

    // importing function with C++ mangled name
    [[import]] void import_me(variant<int, char>, int);

    extern "C" {
        [[export]] void export_me_c(std::sting v1, variant<int, short> v2) {
            export_me(v1, v2);
        }
        // Now we can load function from shared library using "export_me_c"
name:
        //   dlsym(dlopen(library), "export_me_c");


        // importing function with C mangled name
        [[import]] void import_me_c(variant<int, char>, int);
    }

    // Creates section named to_strip and places variable `data` into that
section
    [[section "to_strip"]] char data[] = "Some minor data that will be
stripped later";
}


////////////
// Now we can make nicer macro for header files that declare API, for
example:
#if EXPORTING
#   define API [[export]]
#else
#   define API [[import]]
#endif

#define PLUGIN extern "C" API [[section "plugin"]]

namespace my_namespace {
    // Shared object that contains "foo2" could be dynamically loaded or
dynamically linked.
    PLUGIN void foo2(std::sting);

    // Class that will be dynamically linked
    class API foo_class {
        // ...
    };
}

All the proposed attributes are either simple to implement or are already
supported by modern compilers (MSVC has dllimport, dllexport,
__pragma(section); GCC has dllimport, dllexport, section, visibility).

Any comments are wellcomed!


[1] Boost C++ libraries. Macros controlling shared library symbol visibility
http://www.boost.org/doc/libs/1_60_0/libs/config/doc/html/boost_config/boost_macro_reference.html#boost_config.boost_macro_reference.macros_for_libraries_with_separate_source_code.macros_controlling_shared_library_symbol_visibility
[2] Boost C++ libraries. Macro BOOST_DLL_SECTION
http://apolukhin.github.io/Boost.DLL/BOOST_DLL_SECTION.html

--
Best regards,
Antony Polukhin

--

---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-proposals/.

--94eb2c032e601208760529a0e19f
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div>Hello,</div><div><br></div><div>There&#39;s a bunch o=
f problems with dynamic loading entities defined in C++ shared libraries:</=
div><div>1) No portable way to define symbol as being imported/exported. Al=
l the C++ libraries are forced to reinvent BOOST_SYMBOL_EXPORT and BOOST_SY=
MBOL_IMPORT like macro [1].</div><div>2) All modern binary formats store sy=
mbols in sections that are not currently controlled from C++, but provide a=
 rich set of useful functionality. For example Linux kernel developers use =
sections to store pointers to specific entry points (macro __init <a href=
=3D"http://lxr.free-electrons.com/source/include/linux/init.h">http://lxr.f=
ree-electrons.com/source/include/linux/init.h</a>), store data that must be=
 changed at load (<a href=3D"https://lwn.net/Articles/164121/">https://lwn.=
net/Articles/164121/</a>) and so on.</div><div><br></div><div><br></div><di=
v>I&#39;m proposing to add following attributes to C++ Standard:</div><div>=
* [[import]] - for importing symbol (same as BOOST_SYMBOL_IMPORT)</div><div=
>* [[export]] - for exporting symbol in default section with default C++ ma=
ngling (same as BOOST_SYMBOL_EXPORT)</div><div>* [[section &quot;section na=
me&quot;]] - places the symbol into section with specified name, constructs=
 section if it does not exist(close to BOOST_DLL_SECTION [2])</div><div><br=
></div><div>Here are some usage examples:</div><div><br></div><div>namespac=
e boost {</div><div>=C2=A0 =C2=A0 // exporting function with C++ mangled na=
me.</div><div>=C2=A0 =C2=A0 [[export]] void export_me(std::sting, variant&l=
t;int, short&gt;);</div><div><br></div><div>=C2=A0 =C2=A0 // importing func=
tion with C++ mangled name</div><div>=C2=A0 =C2=A0 [[import]] void import_m=
e(variant&lt;int, char&gt;, int);</div><div><br></div><div>=C2=A0 =C2=A0 ex=
tern &quot;C&quot; {</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 [[export]] void =
export_me_c(std::sting v1, variant&lt;int, short&gt; v2) {</div><div>=C2=A0=
 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 export_me(v1, v2);</div><div>=C2=A0 =C2=
=A0 =C2=A0 =C2=A0 }</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 // Now we can loa=
d function from shared library using &quot;export_me_c&quot; name:</div><di=
v>=C2=A0 =C2=A0 =C2=A0 =C2=A0 // =C2=A0 dlsym(dlopen(library), &quot;export=
_me_c&quot;);</div><div><br></div><div><br></div><div>=C2=A0 =C2=A0 =C2=A0 =
=C2=A0 // importing function with C mangled name</div><div>=C2=A0 =C2=A0 =
=C2=A0 =C2=A0 [[import]] void import_me_c(variant&lt;int, char&gt;, int);</=
div><div>=C2=A0 =C2=A0 }</div><div><br></div><div>=C2=A0 =C2=A0 // Creates =
section named to_strip and places variable `data` into that section</div><d=
iv>=C2=A0 =C2=A0 [[section &quot;to_strip&quot;]] char data[] =3D &quot;Som=
e minor data that will be stripped later&quot;;</div><div>}</div><div><br><=
/div><div><br></div><div>////////////</div><div>// Now we can make nicer ma=
cro for header files that declare API, for example:</div><div>#if EXPORTING=
</div><div># =C2=A0 define API [[export]]</div><div>#else</div><div># =C2=
=A0 define API [[import]]</div><div>#endif</div><div><br></div><div>#define=
 PLUGIN extern &quot;C&quot; API [[section &quot;plugin&quot;]]</div><div><=
br></div><div>namespace my_namespace {</div><div>=C2=A0 =C2=A0 // Shared ob=
ject that contains &quot;foo2&quot; could be dynamically loaded or dynamica=
lly linked.</div><div>=C2=A0 =C2=A0 PLUGIN void foo2(std::sting);</div><div=
><br></div><div>=C2=A0 =C2=A0 // Class that will be dynamically linked</div=
><div>=C2=A0 =C2=A0 class API foo_class {</div><div>=C2=A0 =C2=A0 =C2=A0 =
=C2=A0 // ...</div><div>=C2=A0 =C2=A0 };</div><div>}</div><div><br></div><d=
iv>All the proposed attributes are either simple to implement or are alread=
y supported by modern compilers (MSVC has dllimport, dllexport, __pragma(se=
ction); GCC has dllimport, dllexport, section, visibility).</div><div><br><=
/div><div>Any comments are wellcomed!</div><div><br></div><br>[1] Boost C++=
 libraries. Macros controlling shared library symbol visibility<div><a href=
=3D"http://www.boost.org/doc/libs/1_60_0/libs/config/doc/html/boost_config/=
boost_macro_reference.html#boost_config.boost_macro_reference.macros_for_li=
braries_with_separate_source_code.macros_controlling_shared_library_symbol_=
visibility">http://www.boost.org/doc/libs/1_60_0/libs/config/doc/html/boost=
_config/boost_macro_reference.html#boost_config.boost_macro_reference.macro=
s_for_libraries_with_separate_source_code.macros_controlling_shared_library=
_symbol_visibility</a></div><div>[2] Boost C++ libraries.=C2=A0Macro BOOST_=
DLL_SECTION </div><div><a href=3D"http://apolukhin.github.io/Boost.DLL/BOOS=
T_DLL_SECTION.html">http://apolukhin.github.io/Boost.DLL/BOOST_DLL_SECTION.=
html</a></div><div><br></div>-- <br><div class=3D"gmail_signature">Best reg=
ards,<br>Antony Polukhin</div>
</div>

<p></p>

-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"https://groups.google.com/a/isocpp.org/group=
/std-proposals/">https://groups.google.com/a/isocpp.org/group/std-proposals=
/</a>.<br />

--94eb2c032e601208760529a0e19f--

.