[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Tinycc-devel] Shared libraries: startup and cleanup

From: Jared Maddox
Subject: Re: [Tinycc-devel] Shared libraries: startup and cleanup
Date: Wed, 9 Sep 2015 23:40:44 -0500

> Date: Fri, 4 Sep 2015 01:30:14 +0300
> From: Dmitry Selyutin <address@hidden>
> To: address@hidden
> Subject: [Tinycc-devel] Shared libraries: startup and cleanup
> Message-ID:
>         <address@hidden>
> Content-Type: text/plain; charset="utf-8"
> Hello guys,
> first of all I'd like to thank everyone who takes part in tcc
> development: it's amazing tool and actually the fastest and
> smallest compiler I know about! I'm glad that I've found it
> some years ago and especially glad that it is being developed
> even now.
> The question I have is that I write an open-source project, a big
> library which provides some Unicode functionality as well as
> portable filesystem functions and various I/O stuff. I write in C89
> with the POSIX standard in mind, but Woe32 is supported as well.
> The only difficulty I met is that I need to find some analogue for
> GCC's `__attribute__((constructor))' and `__attribute__((destructor))'.
> This is the absolutely necessary evil, since I want to get path to
> current directory right before `main()' is called.
> These attributes work under gcc and clang; they also seem to work
> with pcc, though I prefer to use `_Pragma("init")' and `_Pragma("fini")'
> instead. I even found the way to achieve the same goal in MSVC.
> However, tcc seems to ignore `__attribute__', so this approach
> doesn't help.
> A quick search approves my own investigations. It is a pity that tcc
> lacks this feature; I know it is a non-standard but this is the only
> reliable way to make an automatic library startup/cleanup without
> forcing user to call some `weird_lib_init()' and `weird_lib_fini'
> functions. Is this feature hard to implement? I don't know an
> assembler, I only heard that this functionality is usually achieved via
> some kind of `.ctors/.dtors' or `.init_array/.fini_array', where pointers
> to functions (or their code?) are stored (well, for ELF). But probably
> there may be a simpler way.
> I'd also like to help if this problem can be solved without assembler
> code. If there is anything I can do I'd like to be useful, so please
> don't hesitate to ask me! Could you please think about implementing
> this feature?
> Thank you for your help!

Haven't done this myself, but link: http://bellard.org/tcc/tcc-doc.html#SEC8

tl;dr: __attribute__ should be supported, but you'll need to use
section() in conjunction with it. This makes the entire subject more
platform specific, so you'll need to research what exactly you need to
do in C for each target platform. Basically you're looking for a way
to register your function(s) as C++ -style constructors/destructors,
and the way that it's done depends on the executable format.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]