tinycc-devel
[Top][All Lists]
Advanced

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

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


From: Dmitry Selyutin
Subject: Re: [Tinycc-devel] Shared libraries: startup and cleanup
Date: Thu, 10 Sep 2015 15:04:27 +0300

Hello Jared,

I've already tried to follow this approach before (and did it again just now) and it seems that it doesn't work either.
Both section(".init")/section(".fini") and section(".ctor")/section(".dtor") were tried.
No success even if I put __attribute__ before function's return type.
I took and built tcc from tinycc repository (tcc version 0.9.26 (x86-64 Linux)).
From what I see it seems that __attribute__ is ignored entirely.
Below is a simple code which I used to test this functionality.
Thank you for your advice!


#include <stddef.h>
#include <stdio.h>


void init(void)
__attribute__((constructor)) __attribute__((section(".init")))
{
        printf("init\n");
}


void fini(void)
__attribute__((destructor)) __attribute__((section(".fini")))
{
        printf("fini\n");
}


int main(void)
{
        printf("main\n");
        return 0;
}


2015-09-10 7:40 GMT+03:00 Jared Maddox <address@hidden>:
> 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.

_______________________________________________
Tinycc-devel mailing list
address@hidden
https://lists.nongnu.org/mailman/listinfo/tinycc-devel



--
With best regards,
Dmitry Selyutin

reply via email to

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