bebbo / amiga-gcc

The GNU C-Compiler with Binutils and other useful tools for cross development for Amiga
GNU General Public License v2.0
319 stars 66 forks source link

Weird linker problem #288

Closed walkero-gr closed 2 years ago

walkero-gr commented 2 years ago

I am a little bit lost with a problem I experience with the gcc 6 for 68k. I try to move a project I am working on from vbcc to gcc for all the supported platform. It compiles fine for OS4 with gcc 8 cross-compiler but it fails with the gcc 6 for 68k at the linker stage, although the sources seem to compile fine.

Below is the output of the linker of gcc 6 for 68k, with -v flag

Target: m68k-amigaos
Configured with: /opt/amiga-gcc/projects/gcc/configure --prefix=/opt/amiga --target=m68k-amigaos --enable-languages=c,c++,objc --enable-version-specific-runtime-libs --disable-libssp --disable-nls --with-headers=/opt/amiga-gcc/projects/newlib-cygwin/newlib/libc/sys/amigaos/include/ --disable-shared --enable-threads=no --with-stage1-ldflags='-dynamic-libgcc -dynamic-libstdc++' --with-boot-ldflags='-dynamic-libgcc -dynamic-libstdc++'
Thread model: single
gcc version 6.5.0b 220609210256 (GCC) 
COMPILER_PATH=/opt/amiga/m68k-amigaos/libnix/lib/:/opt/amiga/libexec/gcc/m68k-amigaos/6.5.0b/:/opt/amiga/libexec/gcc/:/opt/amiga/lib/gcc/m68k-amigaos/6.5.0b/../../../../m68k-amigaos/bin/
LIBRARY_PATH=/opt/amiga/m68k-amigaos/libnix/lib/:/opt/amiga/lib/gcc/m68k-amigaos/6.5.0b/:/opt/amiga/lib/gcc/:/opt/amiga/lib/gcc/m68k-amigaos/6.5.0b/../../../../m68k-amigaos/lib/
COLLECT_GCC_OPTIONS='-v' '-L/opt/sdk/sqlite/lib' '-noixemul' '-o' 'iGame' '-mcpu=68000' '-B' '/opt/amiga/m68k-amigaos/libnix/lib/'
 /opt/amiga/libexec/gcc/m68k-amigaos/6.5.0b/collect2 -L/opt/amiga/lib -L/opt/amiga/m68k-amigaos/libnix/lib -o iGame /opt/amiga/m68k-amigaos/libnix/lib/ncrt0.o -L/opt/sdk/sqlite/lib -L/opt/amiga/m68k-amigaos/libnix/lib -L/opt/amiga/lib/gcc/m68k-amigaos/6.5.0b -L/opt/amiga/lib/gcc src/funcs.o src/iGameGUI.o src/iGameMain.o src/strfuncs.o src/fsfuncs.o src/sqldb.o -lamiga -lstubs -lsqlite3 -( -lnix20 -lnixmain -lnix -lstubs -lamiga -lgcc -) /opt/amiga/m68k-amigaos/libnix/lib/xcrt0.o
/opt/amiga/lib/gcc/m68k-amigaos/6.5.0b/../../../../m68k-amigaos/bin/ld: src/iGameGUI.o: in function `MUI_NewObject':
src/iGameGUI.o:(.text+0x0): multiple definition of `MUI_NewObject'; src/funcs.o:src/funcs.o:(.text+0x0): first defined here
/opt/amiga/lib/gcc/m68k-amigaos/6.5.0b/../../../../m68k-amigaos/bin/ld: src/iGameMain.o: in function `MUI_NewObject':
src/iGameMain.o:(.text+0x0): multiple definition of `MUI_NewObject'; src/funcs.o:src/funcs.o:(.text+0x0): first defined here
/opt/amiga/lib/gcc/m68k-amigaos/6.5.0b/../../../../m68k-amigaos/bin/ld: src/fsfuncs.o: in function `MUI_NewObject':
src/fsfuncs.o:(.text+0x0): multiple definition of `MUI_NewObject'; src/funcs.o:src/funcs.o:(.text+0x0): first defined here
collect2: error: ld returned 1 exit status
make: *** [make_includes/rules.inc:6: iGame] Error 1

As you can see it complains for multiple definition of a method from MUI 5 SDK

Here is the output of the gcc 8 linker, with -v flag

Target: ppc-amigaos
Configured with: /opt/adtools/gcc/repo/configure --with-bugurl=https://github.com/sba1/adtools/issues --with-pkgversion='adtools build 8.4.0' --target=ppc-amigaos --prefix=/opt/ppc-amigaos --enable-languages=c,c++ --enable-haifa --enable-sjlj-exceptions --disable-libstdcxx-pch --disable-tls --enable-threads=amigaos --enable-lto
Thread model: amigaos
gcc version 8.4.0 (adtools build 8.4.0) 
COMPILER_PATH=/opt/ppc-amigaos/libexec/gcc/ppc-amigaos/8.4.0/:/opt/ppc-amigaos/libexec/gcc/ppc-amigaos/8.4.0/:/opt/ppc-amigaos/libexec/gcc/ppc-amigaos/:/opt/ppc-amigaos/lib/gcc/ppc-amigaos/8.4.0/:/opt/ppc-amigaos/lib/gcc/ppc-amigaos/:/opt/ppc-amigaos/lib/gcc/ppc-amigaos/8.4.0/../../../../ppc-amigaos/bin/
LIBRARY_PATH=/opt/ppc-amigaos/lib/gcc/ppc-amigaos/8.4.0/:/opt/ppc-amigaos/lib/gcc/ppc-amigaos/8.4.0/../../../../ppc-amigaos/lib/
COLLECT_GCC_OPTIONS='-v' '-o' 'iGame.OS4'
 /opt/ppc-amigaos/libexec/gcc/ppc-amigaos/8.4.0/collect2 -plugin /opt/ppc-amigaos/libexec/gcc/ppc-amigaos/8.4.0/liblto_plugin.so -plugin-opt=/opt/ppc-amigaos/libexec/gcc/ppc-amigaos/8.4.0/lto-wrapper -plugin-opt=-fresolution=/tmp/ccV2xgPG.res -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_eh -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_eh --eh-frame-hdr --defsym __amigaos4__=1 -Bstatic -q -d -V -Qy -L/opt/ppc-amigaos/ppc-amigaos/SDK/newlib/lib -L/opt/ppc-amigaos/lib/gcc/ppc-amigaos/8.4.0/newlib/lib -L/opt/ppc-amigaos/ppc-amigaos/SDK/local/newlib/lib -L/opt/ppc-amigaos/ppc-amigaos/SDK/local/common/lib -o iGame.OS4 /opt/ppc-amigaos/ppc-amigaos/SDK/newlib/lib/crtbegin.o -L/opt/ppc-amigaos/lib/gcc/ppc-amigaos/8.4.0 -L/opt/ppc-amigaos/lib/gcc/ppc-amigaos/8.4.0/../../../../ppc-amigaos/lib src/funcs_OS4.o src/iGameGUI_OS4.o src/iGameMain_OS4.o src/strfuncs_OS4.o src/fsfuncs_OS4.o src/sqldb_OS4.o -lsqlite3 -lgcc -lgcc_eh --start-group -lc --end-group -lgcc -lgcc_eh /opt/ppc-amigaos/ppc-amigaos/SDK/newlib/lib/crtend.o
GNU ld (GNU Binutils) 2.23.2
  Supported emulations:
   amigaos
   elf32ppc
COLLECT_GCC_OPTIONS='-v' '-o' 'iGame.OS4'

Both of them are under linux, cross compiling the same code.

Any idea why the gcc 6 complains for multiple definitions and fails to build the final binary? Thank you for your help.

bebbo commented 2 years ago

The cause is presumably the way how the mui headers are used. This leads to a MUI_NewObject in each object file. If you do this correctly, the linker should complain about missing MUI_NewObject which is then resolved by adding -lmui. If you can share the source - public or per private email - I might be able to point you in the right direction.

walkero-gr commented 2 years ago

@bebbo I really appreciate your help. The project is open source and you can find it at https://github.com/walkero-gr/iGame/tree/sqlite . I use the Makefile to compile it, where you can see the compiler and linker flags.

What I do not understand, and that is the reason I opened this issue, is why this linker issue appears only with this gcc 6 cross-compiling setup and I do not have it with GCC 8 cross compiler, or even with vbcc in cross compiler, GCC 6 and 11 PPC native (on my OS4 system), where I am able to compile the same code.

Thank you again for your help and time spent on this.

bebbo commented 2 years ago

I pushed a change to the mui.sdk to patch proto/muimaster.h. Now you only have to add -lmui to the linker options. And I strongly recommend to add -noixemul to the CFLAGS. Also pass the CFLAGS to the linker!

walkero-gr commented 2 years ago

@bebbo Unfortunately, nothing of the above worked. Could it be a problem that I compile the gcc with make min?

I was looking at the makefile and I wanted to ask you where exactly do you patch the gcc to work well with Amiga systems. In AmigaOS 4 compatible adtools there is a list of patches done before the build, which can be seen at https://github.com/sba1/adtools/tree/master/gcc/6/patches Your setup does not need something like that?

I also tried to compile your gcc 6 with make all but as soon as I tried to compile the application I got the following error:

In file included from src/funcs.c:54:0:
/opt/amiga/m68k-amigaos/sys-include/stdio.h:29:19: fatal error: _ansi.h: No such file or directory
 #include "_ansi.h"
                   ^
compilation terminated.

Which is weird, because I can't find that _ansi.h file anywhere.

bebbo commented 2 years ago

these are changes I needed:

git diff 
diff --git a/Makefile b/Makefile
index 02295ef..aed8962 100644
--- a/Makefile
+++ b/Makefile
@@ -40,7 +40,7 @@ CFLAGS_OS4    = -c -Os -fomit-frame-pointer -std=c99 -D__USE_INLINE__ -DCPU_VERS=Am
 # Builder settings
 ##########################################################################
 #MKLIB                 = join
-LIBFLAGS               = -v -lamiga -lstubs -o
+LIBFLAGS               = -v -lamiga -lstubs -lmui -o
 LIBFLAGS_MOS   = -v -lamiga -lstubs -o
 LIBFLAGS_OS4   = -v -lamiga -lstubs -o

diff --git a/src/fsfuncs.c b/src/fsfuncs.c
index 5519ceb..474aa76 100644
--- a/src/fsfuncs.c
+++ b/src/fsfuncs.c
@@ -562,7 +562,7 @@ void open_current_dir(void)
        }

        //Open path directory
-       OpenWorkbenchObject((char *)path_only);
+       OpenWorkbenchObject((char *)path_only, TAG_DONE);
        free(path_only); // get_directory_path uses malloc()
 }

diff --git a/src/funcs.c b/src/funcs.c
index ae27637..1b24b63 100644
--- a/src/funcs.c
+++ b/src/funcs.c
@@ -68,7 +68,7 @@
 extern struct ObjApp* app;
 extern struct Library *GfxBase;
 extern struct Library *IconBase;
-extern struct Library *IntuitionBase;
+extern struct IntuitionBase *IntuitionBase;
 extern char* executable_name;

 /* global variables */
walkero-gr commented 2 years ago

@bebbo I think you tested the default branch and not the one I gave you above. But if this compiled fine for you, then there is a possibility that my setup is wrong.

If I create a docker image with your Dockerfile at https://github.com/bebbo/amiga-gcc/blob/master/Dockerfile would that be able to work fine and compile the same code as you did, or this is not up to date?

bebbo commented 2 years ago

docker should work - I don't use that.,,

the sqlite branch fails for me because of

src/sqldb.h:5:21: fatal error: sqlite3.h: No such file or directory
 #include <sqlite3.h>
bebbo commented 2 years ago

ok, got that from aminet:

/opt/amiga/lib/gcc/m68k-amigaos/6.5.0b/../../../../m68k-amigaos/bin/ld: src/iGameMain.o: in function `main':
src/iGameMain.o:(.text+0x7ec): undefined reference to `createDB'
/opt/amiga/lib/gcc/m68k-amigaos/6.5.0b/../../../../m68k-amigaos/bin/ld: src/fsfuncs.o: in function `load_games_db_list':
src/fsfuncs.o:(.text+0x548): undefined reference to `sqlGetItems'
/opt/amiga/lib/gcc/m68k-amigaos/6.5.0b/../../../../m68k-amigaos/bin/ld: src/fsfuncs.o: in function `save_to_db':
src/fsfuncs.o:(.text+0x752): undefined reference to `sqlAddItem'
/opt/amiga/lib/gcc/m68k-amigaos/6.5.0b/../../../../m68k-amigaos/bin/ld: src/fsfuncs.o:(.text+0x706): undefined reference to `sqlDBOpen'
collect2: error: ld returned 1 exit status

my changes in the Makefile:

diff --git a/Makefile b/Makefile
index 321fe89..4fa40c3 100644
--- a/Makefile
+++ b/Makefile
@@ -33,7 +33,7 @@ INCLUDES      = -I$(NDK_INC) -I$(MUI50_INC) -I/opt/sdk/sqlite/include
 INCLUDES_OS4= -ISDK:MUI/C/include -I$(MUI50_INC)
 INCLUDES_MOS= -I$(NDK_INC) -I$(MUI50_INC)

-CFLAGS         = -c -O2 -fomit-frame-pointer -std=c99 -DCPU_VERS=68000 -DRELEASE_DATE=$(DATE)
+CFLAGS         = -c -noixemul -O2 -fomit-frame-pointer -std=c99 -DCPU_VERS=68000 -DRELEASE_DATE=$(DATE)
 CFLAGS_030     = -c -mcpu=68030 -Os -fomit-frame-pointer -std=c99 -DCPU_VERS=68030 -DRELEASE_DATE=$(DATE)
 CFLAGS_040     = -c -mcpu=68040 -Os -fomit-frame-pointer -std=c99 -DCPU_VERS=68040 -DRELEASE_DATE=$(DATE)
 CFLAGS_060     = -c -mcpu=68060 -Os -fomit-frame-pointer -std=c99 -DCPU_VERS=68060 -DRELEASE_DATE=$(DATE)
@@ -44,7 +44,7 @@ CFLAGS_OS4    = -c -O3 -D__USE_INLINE__ -DCPU_VERS=AmigaOS4 -DRELEASE_DATE=$(DATE)
 # Builder settings
 ##########################################################################
 #MKLIB                 = join
-LIBFLAGS               = -v -L/opt/sdk/sqlite/lib -lamiga -lstubs -lsqlite3 -noixemul -o
+LIBFLAGS               = -v -noixemul -L/opt/sdk/sqlite/lib -lamiga -lstubs -lsqlite3 -lmui -o
 LIBFLAGS_MOS   = -v -lamiga -lstubs -o
 LIBFLAGS_OS4   = -v -lsqlite3 -o

rest is up to you

bebbo commented 2 years ago

adding the additional src file results in a linked program:

diff --git a/make_includes/obj_000.inc b/make_includes/obj_000.inc
index 94776dd..3fc0277 100644
--- a/make_includes/obj_000.inc
+++ b/make_includes/obj_000.inc
@@ -16,3 +16,7 @@ src/strfuncs.o: src/strfuncs.c src/strfuncs.h

 src/fsfuncs.o: src/fsfuncs.c src/fsfuncs.h
        $(CC) $(CFLAGS) $(INCLUDES) -o $@ src/fsfuncs.c
+
+src/sqldb.o: src/sqldb.c src/sqldb.h
+       $(CC) $(CFLAGS) $(INCLUDES) -o $@ src/sqldb.c
+
diff --git a/make_includes/obj_files.inc b/make_includes/obj_files.inc
index 0980ef8..7c58ddf 100644
--- a/make_includes/obj_files.inc
+++ b/make_includes/obj_files.inc
@@ -2,7 +2,7 @@
 # Object files which are part of iGame
 ##########################################################################

-OBJS           = src/funcs.o src/iGameGUI.o src/iGameMain.o src/strfuncs.o src/fsfuncs.o
+OBJS           = src/funcs.o src/iGameGUI.o src/iGameMain.o src/strfuncs.o src/fsfuncs.o src/sqldb.o
 OBJS_030       = src/funcs_030.o src/iGameGUI_030.o src/iGameMain_030.o src/strfuncs_030.o src/fsfuncs_030.o
 OBJS_040       = src/funcs_040.o src/iGameGUI_040.o src/iGameMain_040.o src/strfuncs_040.o src/fsfuncs_040.o
 OBJS_060       = src/funcs_060.o src/iGameGUI_060.o src/iGameMain_060.o src/strfuncs_060.o src/fsfuncs_060.o
walkero-gr commented 2 years ago

Thank you @bebbo for your continues help. I tried today with the Dockerfile that exists at your repo, and with that I get the same linker issues. Of course I tested all your comments above, and the issue still remains.

So, either you have something completely different on your system where you do the compilations, or there is something missing from the repo and the compilation of the gcc is not going as it should.

So, my question is if you have anything on your system that is not synced with the repo? Have you tried recently to make a clean installation of this repo to a VM or somewhere and confirm it works fine?

bebbo commented 2 years ago

the real question is: WTF is flexcat... and why is it needed

walkero-gr commented 2 years ago

@bebbo Flexcat is the equivalent of catcomp and it is necessary to create the catalog and translation files. It is available for many systems and is used by many when using cross-compiling environments. Why is this a problem for you? Would you like me to upload in my repo the generated files from flexcat, so that you can do your tests without the need to use flexcat?

bebbo commented 2 years ago

@bebbo Flexcat is the equivalent of catcomp and it is necessary to create the catalog and translation files. It is available for many systems and is used by many when using cross-compiling environments. Why is this a problem for you? Would you like me to upload in my repo the generated files from flexcat, so that you can do your tests without the need to use flexcat?

@bebbo Flexcat is the equivalent of catcomp and it is necessary to create the catalog and translation files. It is available for many systems and is used by many when using cross-compiling environments. Why is this a problem for you? Would you like me to upload in my repo the generated files from flexcat, so that you can do your tests without the need to use flexcat?

If you really want help, then you should make it as easy as possible to reproduce the problem. Right now you can't build it out of the box, e.g. with the Dockerfile. Maybe you could provide a dockerfile for this problem here?

walkero-gr commented 2 years ago

You are right. I will make a dockerfile, based on your own, that will have everything for you to reproduce the problem.

walkero-gr commented 2 years ago

@bebbo Hello Stefan. I forked your repo today and edited the dockerfile to add iGame and MUI 5 examples, so to test compiling them with your gcc 6. In the dockerfile I left at the end comments on how I did the compiling and if you try iGame you will see the problems I mentioned before. Also, I was not able to compile the MUI examples.

https://github.com/walkero-gr/bebbo-amiga-gcc/blob/master/Dockerfile

Any help or ideas are well appreciated. Thank you in advance for your help.

walkero-gr commented 2 years ago

@bebbo I just did some more changes and this seems to compile now. Let me investigate it further and will get back to you.

walkero-gr commented 2 years ago

@bebbo OK... The problem actually was that I was using the original MUI SDK and not the one that is coming from you installation. Possibly some of the changes you have done are mandatory.

Thank you so much for your help. I will keep on tinkering with it.

bebbo commented 2 years ago

glad you solved it.

mheyer32 commented 1 year ago

I don't think your patch to proto/muimaster_lib.h is good as it basically prevents one from using the inline function path. In particular it disables functions like NewObject(). It would have been better if @walkero-gr made the choice to use or not use the INLINE macros in his projects.

I just ran into an issue with a MUI project that does not attempt to link against mui.lib and because of this patch runs into unresolved symbols.