man beam_lib () - An Interface To the BEAM File Format

NAME

beam_lib - An Interface To the BEAM File Format

DESCRIPTION

beam_lib provides an interface to files created by the BEAM compiler ("BEAM files"). The format used, a variant of "EA IFF 1985" Standard for Interchange Format Files, divides data into chunks.

Chunk data can be returned as binaries or as compound terms. Compound terms are returned when chunks are referenced by names (atoms) rather than identifiers (strings). The names recognized and the corresponding identifiers are:

*
abstract_code ("Abst")
*
attributes ("Attr")
*
compile_info ("CInf")
*
exports ("ExpT")
*
labeled_exports ("ExpT")
*
imports ("ImpT")
*
indexed_imports ("ImpT")
*
locals ("LocT")
*
labeled_locals ("LocT")
*
atoms ("Atom")

ENCRYPTED ABSTRACT CODE

The abstract code can be encrypted in order to keep the source code secret, but still be able to use tools such as Xref or Debugger. See compile(3) for how to encrypt the abstract code.

To enable tools to use the abstract code, the key must be made available for beam_lib. There are two ways to provide the key:

1) Use the function crypto_key_fun/1 to register a fun that will be called whenever beam_lib needs to decrypt the abstract code.

2) Store the key in a text file named .erlang.crypt located in either the current directory or the home directory for the current user. beam_lib will search for and read the .erlang.crypt file if no crypto fun has been registered using crypto_key_fun/1. If the file exists and contains a key, beam_lib will implicitly create a crypto key fun and register it.

The .erlang.crypt file should contain a single list. The elements of the list should be tuples looking like this:

{debug_info, Mode, Module, Key}

Mode is the type of key; currently, the only allowed value is des3_cbc. Module is either an atom, in which case Key will only be used for the module Module, or [], in which case Key will be used for all modules. Key is a non-empty string.

The Key in the first tuple where both Mode and Module matches will be used.

Here is an example of an .erlang.crypt file that returns the same key for all modules:

[{debug_info, des3_cbc, [], "%>7}|pc/DM6Cga*68$Mw]L#&_Gejr]G^"}].

And here is a slightly more complicated example of an .erlang.crypt which provides one key for the module t, and another key for all other modules:

[{debug_info, des3_cbc, t, "My KEY"},
 {debug_info, des3_cbc, [], "%>7}|pc/DM6Cga*68$Mw]L#&_Gejr]G^"}].

Note:

Do not use any of the keys in these examples. Use your own keys.

DATA TYPES

beam() -> Module | Filename | binary()
  Module = atom()
  Filename = string() | atom()

Each of the functions described below accept either the module name, the filename, or a binary containing the beam module.

chunkdata() = {ChunkId, DataB} | {ChunkName, DataT}
  ChunkId = chunkid()
  DataB = binary()
  {ChunkName, DataT} =
        {abstract_code, AbstractCode}
      | {attributes, [{Attribute, [AttributeValue]}]}
      | {compile_info, [{InfoKey, [InfoValue]}]}
      | {exports, [{Function, Arity}]}
      | {labeled_exports, [{Function, Arity, Label}]}
      | {imports, [{Module, Function, Arity}]}
      | {indexed_imports, [{Index, Module, Function, Arity}]}
      | {locals, [{Function, Arity}]}]}
      | {labeled_locals, [{Function, Arity, Label}]}]}
      | {atoms, [{integer(), atom()}]}
  AbstractCode = {AbstVersion, Forms} | no_abstract_code
    AbstVersion = atom()
  Attribute = atom()
  AttributeValue = term()
  Module = Function = atom()
  Arity = int()
  Label = int()

It is not checked that the forms conform to the abstract format indicated by AbstVersion. no_abstract_code means that the "Abst" chunk is present, but empty.

The list of attributes is sorted on Attribute, and each attribute name occurs once in the list. The attribute values occur in the same order as in the file. The lists of functions are also sorted.

chunkid() = "Abst" | "Attr" | "CInf"
            | "ExpT" | "ImpT" | "LocT"
            | "Atom"

chunkname() = abstract_code | attributes | compile_info | exports | labeled_exports | imports | indexed_imports | locals | labeled_locals | atoms chunkref() = chunkname() | chunkid()

EXPORTS

chunks(Beam, [ChunkRef]) -> {ok, {Module, [ChunkData]}} | {error, beam_lib, Reason}

Types
Beam = beam()

ChunkRef = chunkref()

Module = atom()

ChunkData = chunkdata()

Reason = {unknown_chunk, Filename, atom()}

  | {key_missing_or_invalid, Filename, abstract_code}

  | Reason1 -- see info/1

 Filename = string()

Reads chunk data for selected chunks refs. The order of the returned list of chunk data is determined by the order of the list of chunks references.

version(Beam) -> {ok, {Module, [Version]}} | {error, beam_lib, Reason}

Types
Beam = beam()

Module = atom()

Version = term()

Reason -- see chunks/2

Returns the module version(s).

info(Beam) -> [{Item, Info}] | {error, beam_lib, Reason1}

Types
Beam = beam()

Item, Info -- see below

Reason1 = {chunk_too_big, Filename, ChunkId, ChunkSize, FileSize}

  | {invalid_beam_file, Filename, Pos}

  | {invalid_chunk, Filename, ChunkId}

  | {missing_chunk, Filename, ChunkId}

  | {not_a_beam_file, Filename}

  | {file_error, Filename, Posix}

 Filename = string()

 ChunkId = chunkid()

 ChunkSize = FileSize = int()

 Pos = int()

 Posix = posix() -- see file(3)

Returns a list containing some information about a BEAM file as tuples {Item, Info}:

{file, Filename} | {binary, Binary}: The name (string) of the BEAM file, or the binary from which the information was extracted.
{module, Module}: The name (atom) of the module.
{chunks, [{ChunkId, Pos, Size}]}: For each chunk, the identifier (string) and the position and size of the chunk data, in bytes.

cmp(Beam1, Beam2) -> ok | {error, beam_lib, Reason}

Types
Beam1 = Beam2 = beam()

Reason = {modules_different, Module1, Module2}

  | {chunks_different, ChunkId}

  | Reason1 -- see info/1

 Module1 = Module2 = atom()

 ChunkId = chunkid()

Compares the contents of two BEAM files. If the module names are the same, and the chunks with the identifiers "Code", "ExpT", "ImpT", "StrT", and "Atom" have the same contents in both files, ok is returned. Otherwise an error message is returned.

cmp_dirs(Dir1, Dir2) -> {Only1, Only2, Different} | {error, beam_lib, Reason1}

Types
Dir1 = Dir2 = string() | atom()

Different = [{Filename1, Filename2}]

Only1 = Only2 = [Filename]

Filename = Filename1 = Filename2 = string()

Reason1 -- see info/1

The cmp_dirs/2 function compares the BEAM files in two directories. Only files with extension ".beam" are compared. BEAM files that exist in directory Dir1 (Dir2) only are returned in Only1 (Only2). BEAM files that exist on both directories but are considered different by cmp/2 are returned as pairs {Filename1, Filename2} where Filename1 (Filename2) exists in directory Dir1 (Dir2).

diff_dirs(Dir1, Dir2) -> ok | {error, beam_lib, Reason1}

Types
Dir1 = Dir2 = string() | atom()

Reason1 -- see info/1

The diff_dirs/2 function compares the BEAM files in two directories the way cmp_dirs/2 does, but names of files that exist in only one directory or are different are presented on standard output.

strip(Beam1) -> {ok, {Module, Beam2}} | {error, beam_lib, Reason1}

Types
Beam1 = Beam2 = beam()

Module = atom()

Reason1 -- see info/1

The strip/1 function removes all chunks from a BEAM file except those needed by the loader. In particular, the abstract code is removed.

strip_files(Files) -> {ok, [{Module, Beam2}]} | {error, beam_lib, Reason1}

Types
Files = [Beam1]

 Beam1 = beam()

Module = atom()

Beam2 = beam()

Reason1 -- see info/1

The strip_files/1 function removes all chunks except those needed by the loader from BEAM files. In particular, the abstract code is removed. The returned list contains one element for each given file name, in the same order as in Files.

strip_release(Dir) -> {ok, [{Module, Filename]}} | {error, beam_lib, Reason1}

Types
Dir = string() | atom()

Module = atom()

Filename = string()

Reason1 -- see info/1

The strip_release/1 function removes all chunks except those needed by the loader from the BEAM files of a release. Dir should be the installation root directory. For example, the current OTP release can be stripped with the call beam_lib:strip_release(code:root_dir()).

format_error(Reason) -> Chars

Types
Reason -- see other functions

Chars = [char() | Chars]

Given the error returned by any function in this module, the function format_error returns a descriptive string of the error in English. For file errors, the function file:format_error(Posix) should be called.

crypto_key_fun(CryptoKeyFun) -> ok | {error, Reason}

Types
CryptoKeyFun = fun() -- see below

Reason = badfun | exists | term()

The crypto_key_fun/1 function registers a unary fun that will be called if beam_lib needs to read an abstract_code chunk that has been encrypted. The fun is held in a process that is started by the function.

If there already is a fun registered when attempting to register a fun, {error, exists} is returned.

The fun must handle the following arguments:

*
CryptoKeyFun(init): Called when the fun is registered, in the process that holds the fun. Here the crypto key fun can do any necessary initializations. Allowed return values:

-
ok
-
{ok, NewCryptoKeyFun} - NewCryptoKeyFun will be registered instead of CryptoKeyFun.
-
{error, Term} - the registration will be aborted and crypto_key_fun/1 will return {error, Term}, where Term can be any term.
*
CryptoKeyFun({debug_info, Mode, Module, Filename}): Called when the key is needed for module Module in the file Filename. Mode describes the method of encryption; currently the only allowed value is des3_cbc.

The fun should return the key as list of characters, or fail if there is no key available.

*
CryptoKeyFun(clear): Called just before the fun is unregistered. Do any needed cleanup here. The return value is not important, but will be passed back to the caller of clear_crypto_key_fun/0.

clear_crypto_key_fun() -> {ok, Result}

Types
Result = undefined | term()

Unregisters the crypto key fun and terminates the process holding it, started by crypto_key_fun/1.

The clear_crypto_key_fun/1 either returns {ok, undefined} if there was no crypto key fun registered, or {ok, Term}, where Term is the return value from CryptoKeyFun(clear), see crypto_key_fun/1.

AUTHOR

Hans Bolinder - support@erlang.ericsson.se