turbolent / w2c2

Translates WebAssembly modules to portable C
MIT License
709 stars 37 forks source link

w2c2/debug.c: incompatible types when initializing dwarf struct on libdwarf 0.10.1, arm64-apple-darwin23.5.0; hacky solution for my platform att. #100

Open rplacd opened 1 month ago

rplacd commented 1 month ago

Hi there – I'm compiling on arm64-apple-darwin23.5.0 (see end), and using libdwarf 0.10.1 (see end.) Apparently, homebrew, the package manager, aliases libdwarf to dwarfutils. Assuming the version of libdwarf/dwarfutils is what you intended, we get a incompatible function pointer type error when compiling w2c2/debug.c (see end). Specifically, we use a Dwarf_Half when libdwarf expects Dwarf_Unsigned, in function pointers that used to initialise static const struct Dwarf_Obj_Access_Methods_a_s dwarfAccessMethods.

Assuming that we don't change your intended semantics by using a type with a bigger range, I simply replaced Dwarf_Half with Dwarf_Unsigned in debug.c (patch at end). Then w2c2_debug compiles without raising any warnings, and w2c2_test passes all tests. Indeed, the diff between the version of debug.c that compiles and the original (based on commit 22a9ade) is trivial (see end.)

I don't have enough experience to know what the root root cause of this issue is; maybe the type of Dwarf_Obj_Access_Methods_a_s is platform-conditional. So I simply wanted to report this issue on my platform, and how I resolved it, and leave it up to your best judgment on what to do next.

Thanks for this wonderful project. I am getting up to fun adventures with it. rplacd

clang platform info

h@Hs-Laptop-2 ~/D/G/u/d/w/w2c2 (main)> gcc --verbose
Apple clang version 15.0.0 (clang-1500.3.9.4)
Target: arm64-apple-darwin23.5.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

libdwarf info

h@Hs-Laptop-2 ~/D/G/u/d/w/w2c2 (main)> brew info libdwarf
==> dwarfutils: stable 0.10.1 (bottled), HEAD
Dump and produce DWARF debug information in ELF objects
https://www.prevandx`x``ers.net/dwarf.html
Installed
/opt/homebrew/Cellar/dwarfutils/0.10.1 (18 files, 2MB) *
  Poured from bottle using the formulae.brew.sh API on 2024-07-23 at 21:48:33
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/d/dwarfutils.rb
License: BSD-2-Clause and LGPL-2.1-or-later and GPL-2.0-or-later
==> Dependencies
Build: pkg-config ✔
==> Options
--HEAD
    Install HEAD version
==> Analytics
install: 709 (30 days), 1,224 (90 days), 3,368 (365 days)
install-on-request: 194 (30 days), 384 (90 days), 1,298 (365 days)
build-error: 0 (30 days)

cmake error

h@Hs-Laptop-2 ~/D/G/u/d/w/w2c2 (main)> cmake --build build
[  2%] Building C object CMakeFiles/w2c2.dir/debug.c.o
/Users/h/Documents/GitHub/uARM-mac68k/deps/w2c2/w2c2/debug.c:480:5: error: incompatible function pointer types initializing 'int (*)(void *, Dwarf_Unsigned, Dwarf_Obj_Access_Section_a *, int *)' (aka 'int (*)(void *, unsigned long long, struct Dwarf_Obj_Access_Section_a_s *, int *)') with an expression of type 'int (void *, Dwarf_Half, Dwarf_Obj_Access_Section_a *, int *)' (aka 'int (void *, unsigned short, struct Dwarf_Obj_Access_Section_a_s *, int *)') [-Wincompatible-function-pointer-types]
    dwarfAccessGetSectionInfo,
    ^~~~~~~~~~~~~~~~~~~~~~~~~
/Users/h/Documents/GitHub/uARM-mac68k/deps/w2c2/w2c2/debug.c:486:5: error: incompatible function pointer types initializing 'int (*)(void *, Dwarf_Unsigned, Dwarf_Small **, int *)' (aka 'int (*)(void *, unsigned long long, unsigned char **, int *)') with an expression of type 'int (void *, Dwarf_Half, Dwarf_Small **, int *)' (aka 'int (void *, unsigned short, unsigned char **, int *)') [-Wincompatible-function-pointer-types]
    dwarfAccessLoadSection,
    ^~~~~~~~~~~~~~~~~~~~~~
/Users/h/Documents/GitHub/uARM-mac68k/deps/w2c2/w2c2/debug.c:487:5: error: incompatible function pointer types initializing 'int (*)(void *, Dwarf_Unsigned, Dwarf_Debug, int *)' (aka 'int (*)(void *, unsigned long long, struct Dwarf_Debug_s *, int *)') with an expression of type 'int (void *, Dwarf_Half, Dwarf_Debug, int *)' (aka 'int (void *, unsigned short, struct Dwarf_Debug_s *, int *)') [-Wincompatible-function-pointer-types]
    dwarfAccessRelocateASection
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~

Patch

391c391
<     Dwarf_Unsigned sectionIndex,
---
>     Dwarf_Half sectionIndex,
456c456
<     Dwarf_Unsigned sectionIndex,
---
>     Dwarf_Half sectionIndex,
472c472
<     Dwarf_Unsigned UNUSED(sectionIndex),
---
>     Dwarf_Half UNUSED(sectionIndex),
rplacd commented 1 month ago

PS – I believe long long int and unsigned long long int is not strictly in C89, but is a GCC extension of C89, so w2c2_base.h will not compile on a few non-GCC C89-conforming compilers I have at hand (e.g. MetroWerks C 1.1 for Mac 68k.) This isn't a problem for my specific purposes, but it's worth noting.