Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Identity transformation may corrupt file for some usecases #4

Open
last-g opened this issue Apr 19, 2018 · 1 comment
Open

Identity transformation may corrupt file for some usecases #4

last-g opened this issue Apr 19, 2018 · 1 comment

Comments

@last-g
Copy link

last-g commented Apr 19, 2018

I wrote a simple script which applies identity transformation to test this lib. So the code is super simple and inspired by tests:

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
module Main
  (
    main,
    strip_ghc_symbols,
  ) where


import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as L
import qualified Data.ElfEdit as EE
import qualified System.IO as IO

in_filename :: FilePath
in_filename = "./in.elf"

out_filename :: FilePath
out_filename = "./out.elf"


strip_ghc_symbols :: FilePath -> FilePath -> IO ()
strip_ghc_symbols in_path out_path =
  IO.withBinaryFile in_filename IO.ReadMode $ \in_h -> do
    bs <- B.hGetContents in_h
    withElf bs $ \e -> do

      let e_final = e
      -- write to output
      IO.withBinaryFile out_filename IO.WriteMode $ \out_h -> do
        B.hPut out_h $ L.toStrict (EE.renderElf e_final)


main :: IO ()
main = do
  putStrLn "hello, kitty"
  strip_ghc_symbols in_filename out_filename


--
withElf :: B.ByteString -> (forall w . EE.Elf w -> IO ()) -> IO ()
withElf bs f =
  case EE.parseElf bs of
    EE.Elf32Res err e32
      | null err  -> f e32
      | otherwise -> fail ("Failed to parse elf file: " ++ show err)
    EE.Elf64Res err e64
      | null err  -> f e64
      | otherwise -> fail ("Failed to parse elf file: " ++ show err)
    EE.ElfHeaderError _ e -> fail $ "Failed to parse elf file: " ++ show e

Unfortunately, the generated file doesn't look as correct as the original one.

lastg-mbp:fix-ghc-symbols lastg$ objdump -t in.elf > /dev/null
lastg-mbp:fix-ghc-symbols lastg$ echo $?
0
lastg-mbp:fix-ghc-symbols lastg$ objdump -t out.elf
/Applications/Xcode_9.3.0_fb.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/objdump: 'out.elf': Invalid data was encountered while parsing the file
lastg-mbp:fix-ghc-symbols lastg$ echo $?
1
lastg-mbp:fix-ghc-symbols lastg$ objdump -version
Apple LLVM version 9.1.0 (clang-902.0.39.1)
  Optimized build.
  Default target: x86_64-apple-darwin17.5.0
  Host CPU: skylake

  Registered Targets:
    aarch64    - AArch64 (little endian)
    aarch64_be - AArch64 (big endian)
    arm        - ARM
    arm64      - ARM64 (little endian)
    armeb      - ARM (big endian)
    thumb      - Thumb
    thumbeb    - Thumb (big endian)
    x86        - 32-bit X86: Pentium-Pro and above
    x86-64     - 64-bit X86: EM64T and AMD64

But in the same time out.elf seems to be valid for readelf and eu-readelf. And it's still runnable.

bins.zip

@joehendrix
Copy link
Contributor

Do you any way of dumping more detailed error messages our from OSX' LLVM objdump?

I found one fix for .bss section alignment, but the symbol table differences do not affect readelf or GNU objdump?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants