diff --git a/compiler/GHC/Driver/Session.hs b/compiler/GHC/Driver/Session.hs index 352a344dd49b88922deff3c0e2669c9233dd1c4e..80a8d27f99ad345b2f980c5d486407a63f0f9901 100644 --- a/compiler/GHC/Driver/Session.hs +++ b/compiler/GHC/Driver/Session.hs @@ -3524,7 +3524,17 @@ picCCOpts dflags = -- objects, but can't without -fPIC. See -- https://gitlab.haskell.org/ghc/ghc/wikis/commentary/position-independent-code | gopt Opt_PIC dflags || ways dflags `hasWay` WayDyn -> - ["-fPIC", "-U__PIC__", "-D__PIC__"] + ["-fPIC", "-U__PIC__", "-D__PIC__"] ++ + -- Clang defaults to -fvisibility=hidden for wasm targets, + -- but we need these compile-time flags to generate PIC + -- objects that can be properly linked by wasm-ld using + -- --export-dynamic; without these flags we would need + -- -Wl,--export-all at .so link-time which will export + -- internal symbols as well, and that severely pollutes the + -- global symbol namespace. + (if platformArch (targetPlatform dflags) == ArchWasm32 + then [ "-fvisibility=default", "-fvisibility-inlines-hidden" ] + else []) -- gcc may be configured to have PIC on by default, let's be -- explicit here, see #15847 | otherwise -> ["-fno-PIC"]