Can you decompile .beam files back to .erl?

Hello. I have some .beam files that I want to decompile back to .erl.
Can you decompile it?

In some cases you do not even need to decompile it. You can read abstract tree out of these files if that chunk wasn’t removed.

But in general case - you partially can. It will not be fully accurate or readable, but it should be 100% possible to do so.

1 Like

how are you. please guide me. thank you.

How to decompile it. Can you help me with more detailed instructions. Thank you very much.

7.12 Is there a “reverse compiler” for BEAM files?
Or: I’ve lost/deleted/whatever the .erl files for my project, can I somehow recreate it from the .beam files?

If the code was compiled with the debug_info flag, then the .beam file contains a ‘partially compiled’ representation of the source—basically the parse tree.

Here is a simple module:

 -module(hw).
 -export([go/0]).

 go() when true ->
   "this is my function".

and the corresponding abstract code:

 3> {ok, {hw, [{abstract_code, Abs}]}} =  beam_lib:chunks("hw.beam", [abstract_code]), Abs.
     {raw_abstract_v1,[{attribute,1,file,{"./hw.erl",1}},
              {attribute,1,module,hw},
              {attribute,2,export,[{go,0}]},
              {function,4,
                        go,
                        0,
                        [{clause,4,
                                 [],
                                 [],
                                 [{string,5,"this is my function"}]}]},
              {eof,6}]}

Writing a decompiler which can turn the above example back to source is a fifteen minute job. Writing a decompiler which handles more complex Erlang code is more time consuming, but not much harder. The syntax_tools application can do most of the hard work.

If the abstract code is not present in the beam file, the problem gets much harder. It is possible to study the remaining information and draw some conclusions about what the original .erl file might have looked like, for instance which functions were exported. But a lot of other important information, such as variable names, is not present. In general, recreating the source code from a beam file without abstract code is not practical.

[Erlang -- Erlang Tools]

2 Likes

it’s not effective. it’s not the original .erl . file
It’s just visual contrast and it’s hard to rewrite.

you can’t get the original .erl back, even with debuf_info.

1 Like

then it’s really difficult. Because just looking at it like that is very tiring to write.