[erlang-questions] Loading a copy of a module under a different name

Luca Tansini luca.tansini95@REDACTED
Mon Jul 29 10:28:34 CEST 2019


Hi all,

I’m trying to load into the VM a copy of a module under a different name.
The way I’m trying to do it is:
	
	- get the binary of the module with code:get_object_code(mod_name)
	- replace the module name inside the binary (otherwise the code loader won’t accept it)
	- reload it with code:load_binary(new_name, “”, RenamedBinary)

here is some sample code showing the approach: https://pastebin.com/HpdUR0eU <https://pastebin.com/HpdUR0eU>.

This approach seems to work as long as the original module doesn’t contain any NIFs.
In that case the module is still loaded (not always, I’ve found at least one exception, more below), but then gives runtime errors when NIFs are trying to be run.

For instance this is what happens with the lists module:

>> module_cp:load_module_copy(lists).
{module,xists}
>> xists:seq(1,10).
[1,2,3,4,5,6,7,8,9,10]
>> xists:reverse([1,2,3,4]).
** exception error: undefined function erlang:nif_error/1
     in function  xists:reverse/2 (xists.erl, line 95)

For the erlang module instead, even the code loading fails:

>> module_cp:load_module_copy(erlang).
{error,badfile}

=ERROR REPORT==== 26-Jul-2019::13:10:11 ===
Loading of  failed: badfile

=ERROR REPORT==== 26-Jul-2019::13:10:11 ===
beam/beam_load.c(2284): Error loading function xrlang:garbage_collect/2: op bif0 u x:
  no specific operation found


I’m not very familiar with how NIFs work and are loaded, but is there a workaround for this?
Maybe renaming only the module attribute is not enough.
I’m on MacOS running Erlang/OTP 20 [erts-9.3].

Thank you


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20190729/175a1869/attachment.htm>


More information about the erlang-questions mailing list