diff options
author | Cheng Shao <astrohavoc@gmail.com> | 2022-10-23 17:04:58 +0000 |
---|---|---|
committer | Marge Bot <ben+marge-bot@smart-cactus.org> | 2022-11-11 00:26:55 -0500 |
commit | 32ae62e61e811bf99def141a388a3f0abc8b7107 (patch) | |
tree | 7574a98cecdc79c108b3c3d47893f10bd6416574 /utils | |
parent | df7bfef8f72bb32663d3828bf096587525f09335 (diff) | |
download | haskell-32ae62e61e811bf99def141a388a3f0abc8b7107.tar.gz |
deriveConstants: parse .ll output for wasm32 due to broken nm
This patch makes deriveConstants emit and parse an .ll file when
targeting wasm. It's a necessary workaround for broken llvm-nm on
wasm, which isn't capable of reporting correct constant values when
parsing an object.
Diffstat (limited to 'utils')
-rw-r--r-- | utils/deriveConstants/Main.hs | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/utils/deriveConstants/Main.hs b/utils/deriveConstants/Main.hs index 65c3deb3e5..b98d8979f9 100644 --- a/utils/deriveConstants/Main.hs +++ b/utils/deriveConstants/Main.hs @@ -27,7 +27,8 @@ needing to run the program, by inspecting the object file using 'nm'. import Control.Monad (when, unless) import Data.Bits (shiftL) -import Data.List (stripPrefix, intercalate) +import Data.Char (toLower) +import Data.List (elemIndex, stripPrefix, intercalate) import Data.Map (Map) import qualified Data.Map as Map import Data.Maybe (catMaybes, mapMaybe, fromMaybe) @@ -692,15 +693,26 @@ getWanted verbose os tmpdir gccProgram gccFlags nmProgram mobjdumpProgram cFile = tmpdir </> "tmp.c" oFile = tmpdir </> "tmp.o" atomicWriteFile cFile cStuff - execute verbose gccProgram (gccFlags ++ ["-c", cFile, "-o", oFile]) + -- On wasm32-wasi, llvm-nm incorrectly reports length of + -- buffers defined in wasm object files as 0, see llvm ticket + -- #58839. Therefore we have to emit .ll file instead, and + -- use a very crude parser which is just enough to extract + -- the buffer length info we're interested in. + execute verbose gccProgram (gccFlags ++ ( + case os of + "wasi" -> ["-emit-llvm", "-S"] + _ -> ["-c"] + ) ++ [cFile, "-o", oFile]) xs <- case os of "openbsd" -> readProcess objdumpProgam ["--syms", oFile] "" "aix" -> readProcess objdumpProgam ["--syms", oFile] "" + "wasi" -> readFile oFile _ -> readProcess nmProgram ["-P", oFile] "" let ls = lines xs m = Map.fromList $ case os of "aix" -> parseAixObjdump ls + "wasi" -> mapMaybe parseLLLine ls _ -> mapMaybe parseNmLine ls case Map.lookup "CONTROL_GROUP_CONST_291" m of @@ -786,6 +798,13 @@ getWanted verbose os tmpdir gccProgram gccFlags nmProgram mobjdumpProgram doWanted (ClosurePayloadMacro {}) = [] doWanted (FieldTypeGcptrMacro {}) = [] + parseLLLine line = case words line of + ('@' : prefix_sym) : ts -> do + sym <- stripPrefix prefix prefix_sym + i <- elemIndex "x" ts + pure (sym, read (tail (ts !! (i - 1)))) + _ -> Nothing + -- parseNmLine parses "nm -P" output that looks like -- "derivedConstantMAX_Vanilla_REG C 0000000b 0000000b" (GNU nm) -- "_derivedConstantMAX_Vanilla_REG C b 0" (Mac OS X) |