sba1 / adtools

Experimental GNU toolchain for AmigaOS
31 stars 18 forks source link

Some shared object constructors are not called #139

Open capehill opened 1 year ago

capehill commented 1 year ago

Problem: only 1 of 2 constructors in shared object are called. Both constructors are called when library is linked statically. (Both destructors are called in all cases). Not sure if this problem is related to GCC or elf.library or something else.

Using GCC 11.2. Here is a small example:

-- lib.c

#include <stdio.h>

void __attribute__((constructor(101))) ctor()
{
    printf("%s\n", __func__);
}

void  __attribute__((destructor(101))) dtor()
{
    printf("%s\n", __func__);
}

void __attribute__((constructor(102))) ctor2()
{
    printf("%s\n", __func__);
}

void  __attribute__((destructor(102))) dtor2()
{
    printf("%s\n", __func__);
}

int function(int x)
{
    printf("%s %d\n", __func__, x);

    return x;
}

-- main.c

#include <stdio.h>

int function(int);

int main()
{
    int result = function(123);

    printf("%s result %d\n", __func__, result);

    return 0;
}

-- makefile

CC=gcc

all:
    $(CC) -o lib.o -c lib.c -fPIC
    $(CC) -o libf.so lib.o -shared
    ar cruv  libf.a lib.o
    ranlib libf.a
    $(CC) -o test_dyn main.c -use-dynld -L. -lf -athread=native
    $(CC) -o test_static main.c -L. -lf -athread=native

-- output

ctor2 call is missing here:

constructor> test_dyn
ctor
function 123
main result 123
dtor2
dtor
constructor> test_static
ctor
ctor2
function 123
main result 123
dtor2
dtor
afxgroup commented 1 year ago

I think that https://github.com/afxgroup/clib2/issues/57 could be related to this

afxgroup commented 1 year ago

New beta elf.library should fix this problem

raziel- commented 1 year ago

Hopefully this cures the random crashes i get with ScummVM on start.

otoh the timeframe for releasing the updated lib isn't encouraging

afxgroup commented 1 year ago

That's the main problem.. However I don't know. What you have in the crashlog? Because with actual elf.library the problem happens always and not sometimes

raziel- commented 1 year ago

As I said, it's random. Sometimes ISI, sometimes DSI, coming from everywhere in the code, all of the time on first start, most of the time after some other big programs ran beforehand, e.g. odyssey

Lately not so much, but every fix to the shared objects handling helps:-)

Great work

khval commented 1 year ago

If the address to interface structure is not set, you get DSI error, when its try’s read from bad address, and you read some random value located there, you press ignore, and it try jump to bad address. This when you get illegal instruction because there is no code there. so you get ISI error. Run Snoopy to see if any libraries failed to open. Of course, this should show that Odyssey has poor startup code, because it let program run, when it should not have.

man. 6. mar. 2023 kl. 10:06 skrev Hubert Maier @.***>:

As I said, it's random. Sometimes ISI, sometimes DSI, coming from everywhere in the code, all of the time on first start, most of the time after some other big programs ran beforehand, e.g. odyssey

Lately not so much, but every fix to the shared objects handling helps:-)

Great work

— Reply to this email directly, view it on GitHub https://github.com/sba1/adtools/issues/139#issuecomment-1455744327, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAIDFDPVVE33QGX4JXLKVP3W2WSJPANCNFSM6AAAAAATPBAGFE . You are receiving this because you are subscribed to this thread.Message ID: @.***>

khval commented 1 year ago

If the interface is not initialized, it can be because some libraries has replaced by 680x0 library, this library’s do not have interfaces. make sure the correct libraries are installed.

man. 6. mar. 2023 kl. 10:21 skrev Kjetil Hvalstrand < @.***>:

If the address to interface structure is not set, you get DSI error, when its try’s read from bad address, and you read some random value located there, you press ignore, and it try jump to bad address. This when you get illegal instruction because there is no code there. so you get ISI error. Run Snoopy to see if any libraries failed to open. Of course, this should show that Odyssey has poor startup code, because it let program run, when it should not have.

man. 6. mar. 2023 kl. 10:06 skrev Hubert Maier @.***>:

As I said, it's random. Sometimes ISI, sometimes DSI, coming from everywhere in the code, all of the time on first start, most of the time after some other big programs ran beforehand, e.g. odyssey

Lately not so much, but every fix to the shared objects handling helps:-)

Great work

— Reply to this email directly, view it on GitHub https://github.com/sba1/adtools/issues/139#issuecomment-1455744327, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAIDFDPVVE33QGX4JXLKVP3W2WSJPANCNFSM6AAAAAATPBAGFE . You are receiving this because you are subscribed to this thread.Message ID: @.***>