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

Bug#980204: gdc: Resulting executables segfault on mipsel architecture (signal 11)



Excerpts from Iain Buclaw's message of January 22, 2021 8:14 pm:
> Excerpts from Matija Nalis's message of January 22, 2021 5:51 pm:
>> On Fri, Jan 22, 2021 at 12:59:34PM +0100, Iain Buclaw wrote:
>>> Also, are you linking to the static or shared libphobos library?
>> 
>> shared (default):
>> 
>> (mipsel-chroot):/tmp/w$ dpkg -l gdc | grep gdc
>> ii  gdc            4:10.2.1-1   mipsel       D compiler (language version 2), based on the GCC backend
>> 
>> (mipsel-chroot):/tmp/w$ printf 'import std.stdio;\nvoid main()  { writeln("Hello, World!"); }\n' > hello.d ; gdc hello.d && ./a.out
>> qemu: uncaught target signal 11 (Segmentation fault) - core dumped
>> Segmentation fault
>> 
>> (mipsel-chroot):/tmp/w$ file a.out
>> a.out: ELF 32-bit LSB pie executable, MIPS, MIPS32 rel2 version 1 (SYSV), dynamically linked, interpreter /lib/ld.so.1, BuildID[sha1]=36c5576b94519b416c1996018760159ae925bc34, for GNU/Linux 3.2.0, not stripped
>> 
>> (mipsel-chroot):/tmp/w$ ldd a.out
>>         libgphobos.so.1 => /lib/mipsel-linux-gnu/libgphobos.so.1 (0x7f1a3000)
>>         libgcc_s.so.1 => /lib/mipsel-linux-gnu/libgcc_s.so.1 (0x7f16b000)
>>         libc.so.6 => /lib/mipsel-linux-gnu/libc.so.6 (0x7efd1000)
>>         libm.so.6 => /lib/mipsel-linux-gnu/libm.so.6 (0x7ef52000)
>>         libpthread.so.0 => /lib/mipsel-linux-gnu/libpthread.so.0 (0x7ef21000)
>>         libdl.so.2 => /lib/mipsel-linux-gnu/libdl.so.2 (0x7ef0d000)
>>         libz.so.1 => /lib/mipsel-linux-gnu/libz.so.1 (0x7eee2000)
>>         /lib/ld.so.1 (0x7ffc9000)
>> 
>> 
>> But good point, when I try to link this "hello world" example statically, it
>> throws warnings, but works!
>> 
>> (mipsel-chroot):/tmp/w$ printf 'import std.stdio;\nvoid main()  { writeln("Hello, World!"); }\n' > hello.d ; gdc -static hello.d && ./a.out
>> /usr/bin/ld: /usr/lib/gcc/mipsel-linux-gnu/10/libgphobos.a(elf_shared.o): in function `_D3gcc8sections10elf_shared18pinLoadedLibrariesFNbNiZPv':
>> /build/gcc-10-XdUysA/gcc-10-10.2.1/build/mipsel-linux-gnu/libphobos/libdruntime/../../../../src/libphobos/libdruntime/gcc/sections/elf_shared.d:250: warning: Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
>> /usr/bin/ld: /usr/lib/gcc/mipsel-linux-gnu/10/libgphobos.a(stdio.o): in function `_D3std5stdio11openNetworkFAyatZS3std5stdio4File':
>> /build/gcc-10-XdUysA/gcc-10-10.2.1/build/mipsel-linux-gnu/libphobos/src/../../../../src/libphobos/src/std/stdio.d:5137: warning: Using 'gethostbyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
>> Hello, World!
>> 
> 
> I've just checked the testsuite result, and the only failed tests are
> those that use -shared-libphobos.
> 
>     FAIL: gdc.test/runnable/implicit.d -shared-libphobos   execution test
>     FAIL: gdc.test/runnable/test31.d -shared-libphobos   execution test
>     FAIL: gdc.test/runnable/testarray.d -shared-libphobos   execution test
>     FAIL: gdc.test/runnable/Same.d -shared-libphobos   execution test
>     FAIL: gdc.test/runnable/s2ir.d -shared-libphobos   execution test
>     FAIL: gdc.test/runnable/test9309.d -shared-libphobos   execution test
>     FAIL: gdc.test/runnable/test15.d -shared-libphobos   execution test
>     etc...
> 
> 

So the crux of the matter is that on MIPS, dynamic sections are read-only
(glibc sets DL_RO_DYN_SECTION 1), which requires special handling when pulling
data from dl_phdr_info.

Re-running with the attached patch applied.

Iain.

--- a/libphobos/libdruntime/gcc/sections/elf_shared.d
+++ b/libphobos/libdruntime/gcc/sections/elf_shared.d
@@ -22,6 +22,8 @@
 
 module gcc.sections.elf_shared;
 
+version (MIPS32)  version = MIPS_Any;
+version (MIPS64)  version = MIPS_Any;
 version (RISCV32) version = RISCV_Any;
 version (RISCV64) version = RISCV_Any;
 version (S390)    version = IBMZ_Any;
@@ -763,6 +765,8 @@ version (Shared)
                     // in glibc: #define DL_RO_DYN_SECTION 1
                     version (RISCV_Any)
                         strtab = cast(const(char)*)(info.dlpi_addr + dyn.d_un.d_ptr); // relocate
+                    else version (MIPS_Any)
+                        strtab = cast(const(char)*)(info.dlpi_addr + dyn.d_un.d_ptr); // relocate
                     else
                         strtab = cast(const(char)*)dyn.d_un.d_ptr;
                 }


Reply to: