smx-smx / ASMTool

Firmware dumper and various utilities for ASMedia USB Controllers and related firmware
70 stars 10 forks source link

Probably doing something wrong but I'm getting mixed 64 and 32 bit binaries on linux. #10

Open hanetzer opened 1 year ago

hanetzer commented 1 year ago

So. The x370 chipset on ryzen is very much like asmedia stuff, and was hoping to poke it a bit in order to assist my port of coreboot to 'Asrock X370 Killer Sli/AC' (kind of in a working state; more to be done).

By issuing dotnet build I get a 64-bit ELF for AsmTool but the dlls come out as 32 bit. Attempting to run AsmTool nets me the following error:

Unhandled exception. System.BadImageFormatException: Could not load file or assembly '~/ASMTool/AsmTool/bin/Debug/net6.0/AsmTool.dll'. An attempt was made to load a program with
an incorrect format.

File name: '~/ASMTool/AsmTool/bin/Debug/net6.0/AsmTool.dll'
Aborted (core dumped)

libAsmIOLinux.so is compiled and in the directory next to AsmTool (it is also a 64-bit ELF)

rhn commented 1 year ago

I have the same issue. I used the CI dockerfile and commands, but the created artifact is unuseable.

voltagex commented 1 year ago

In the CMake files, there's a line that clears the target platform but it doesn't get set again on Linux.

Theoretically

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 4da64d4..1787fce 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -20,6 +20,7 @@ ADD_DOTNET(${CMAKE_SOURCE_DIR}/AsmTool.sln
 )

 if(UNIX)
+       set(target_platform PLATFORM x64)
        enable_language(C)
        add_subdirectory(AsmTool/Linux)
        add_dependencies(AsmTool_sln asmIoLinux)

should be enough to fix it, but

voltagex@BeeMovie:~/src/ASMTool/build/out$ file libasmIoLinux.so
libasmIoLinux.so: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, BuildID[sha1]=16e6d63384efb4239eb312f502242296346eae03, not stripped
voltagex@BeeMovie:~/src/ASMTool/build/out$ file AsmTool
AsmTool: ELF 64-bit LSB pie executable, x86-64, version 1 (GNU/Linux), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=10540531054f88680224e7bf543dc2dbef55c014, for GNU/Linux 2.6.32, stripped
voltagex@BeeMovie:~/src/ASMTool/build/out$ file AsmTool.dll
AsmTool.dll: PE32 executable (console) Intel 80386 Mono/.Net assembly, for MS Windows, 3 sections

@smx any idea what's going on here?

vrbadev commented 2 weeks ago

I have the same problem, any idea how to compile it properly on Linux?

EDIT: If you change the platform in AsmTool/AsmTool.csproj to <PlatformTarget>x64</PlatformTarget>, then the dotnet build command should give you 64-bit file: ./AsmTool/bin/Debug/net6.0/AsmTool.dll: PE32+ executable (console) x86-64 Mono/.Net assembly, for MS Windows.

When I run the executable now, it complains when the file libAsmIOLinux.so is not present. However, it always crashes later with this output (from valgrind):

[...]
==71853== More than 1000 different errors detected.  I'm not reporting any more.
==71853== Final error counts will be inaccurate.  Go fix your program!
==71853== Rerun with --error-limit=no to disable this cutoff.  Note
==71853== that errors may occur in your program without prior warning from
==71853== Valgrind, because errors are no longer being displayed.
==71853==
Unloading ASM Driver...
Loading ASM Driver...
Scanning for ASMedia ICs...
vex amd64->IR: unhandled instruction bytes: 0x48 0xCF 0x48 0x8D 0x64 0x24 0x30 0x5D 0xC3 0x90
vex amd64->IR:   REX=1 REX.W=1 REX.R=0 REX.X=0 REX.B=0
vex amd64->IR:   VEX=0 VEX.L=0 VEX.nVVVV=0x0 ESC=NONE
vex amd64->IR:   PFX.66=0 PFX.F2=0 PFX.F3=0
==71853== valgrind: Unrecognised instruction at address 0x57bac86.
==71853==    at 0x57BAC86: ??? (in /usr/share/dotnet/shared/Microsoft.NETCore.App/6.0.36/libcoreclr.so)
==71853==    by 0x57BAE33: ??? (in /usr/share/dotnet/shared/Microsoft.NETCore.App/6.0.36/libcoreclr.so)
==71853==    by 0x577E67F: ??? (in /usr/share/dotnet/shared/Microsoft.NETCore.App/6.0.36/libcoreclr.so)
==71853==    by 0x489F41F: ??? (in /usr/lib/x86_64-linux-gnu/libpthread-2.31.so)
==71853==    by 0x2961E1DA: outl (in /home/mrs/xilinx/ASMTool/AsmTool/bin/Debug/net6.0/libAsmIOLinux.so)
==71853== Your program just tried to execute an instruction that Valgrind
==71853== did not recognise.  There are two possible reasons for this.
==71853== 1. Your program has a bug and erroneously jumped to a non-code
==71853==    location.  If you are running Memcheck and you just saw a
==71853==    warning about a bad jump, it's probably your program's fault.
==71853== 2. The instruction is legitimate but Valgrind doesn't handle it,
==71853==    i.e. it's Valgrind's fault.  If you think this is the case or
==71853==    you are not sure, please let us know and we'll try to fix it.
==71853== Either way, Valgrind will now raise a SIGILL signal which will
==71853== probably kill your program.
==71853==
==71853== Process terminating with default action of signal 11 (SIGSEGV)
==71853==  Bad permissions for mapped region at address 0x484AFE8
==71853==    at 0x577EB9D: ??? (in /usr/share/dotnet/shared/Microsoft.NETCore.App/6.0.36/libcoreclr.so)
==71853==
==71853== Process terminating with default action of signal 11 (SIGSEGV)
==71853==  Bad permissions for mapped region at address 0x484AFE0
==71853==    at 0x4833134: _vgnU_freeres (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_core-amd64-linux.so)
==71853==
==71853== HEAP SUMMARY:
==71853==     in use at exit: 2,707,422 bytes in 6,128 blocks
==71853==   total heap usage: 19,193 allocs, 13,065 frees, 8,148,036 bytes allocated
==71853==
==71853== LEAK SUMMARY:
==71853==    definitely lost: 127 bytes in 3 blocks
==71853==    indirectly lost: 0 bytes in 0 blocks
==71853==      possibly lost: 5,231 bytes in 15 blocks
==71853==    still reachable: 2,702,064 bytes in 6,110 blocks
==71853==                       of which reachable via heuristic:
==71853==                         stdstring          : 132,221 bytes in 3,083 blocks
==71853==         suppressed: 0 bytes in 0 blocks
==71853== Rerun with --leak-check=full to see details of leaked memory
==71853==
==71853== Use --track-origins=yes to see where uninitialised values come from
==71853== For lists of detected and suppressed errors, rerun with: -s
==71853== ERROR SUMMARY: 13408 errors from 1000 contexts (suppressed: 0 from 0)
Segmentation fault (core dumped)

How to proceed from this point? I am on Ubuntu-20.04 with kernel 5.15.153.1, my dotnet SDK version is 6.0.428.