<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><meta http-equiv="Content-Type" content="text/html; charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">Hi all,<div class=""><br class=""></div><div class="">I’m trying to load into the VM a copy of a module under a different name.</div><div class="">The way I’m trying to do it is:</div><div class=""><span class="Apple-tab-span" style="white-space:pre">  </span></div><div class=""><span class="Apple-tab-span" style="white-space:pre">  </span>- get the binary of the module with code:get_object_code(mod_name)</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>- replace the module name inside the binary (otherwise the code loader won’t accept it)</div><div class=""><span class="Apple-tab-span" style="white-space:pre"> </span>- reload it with code:load_binary(new_name, “”, RenamedBinary)</div><div class=""><br class=""></div><div class="">here is some sample code showing the approach: <a href="https://pastebin.com/HpdUR0eU" class="">https://pastebin.com/HpdUR0eU</a>.</div><div class=""><br class=""></div><div class="">This approach seems to work as long as the original module doesn’t contain any NIFs.</div><div class="">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.</div><div class=""><br class=""></div><div class="">For instance this is what happens with the lists module:</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><div class="">>> module_cp:load_module_copy(lists).</div></div><div class=""><div class="">{module,xists}</div></div><div class="">>> xists:seq(1,10).</div><div class="">[1,2,3,4,5,6,7,8,9,10]</div><div class=""><div class="">>> xists:reverse([1,2,3,4]).</div></div><div class=""><div class="">** exception error: undefined function erlang:nif_error/1</div></div><div class=""><div class="">     in function  xists:reverse/2 (xists.erl, line 95)</div></div></blockquote><div class=""><br class=""></div><div class="">For the erlang module instead, even the code loading fails:</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><div class="">>> module_cp:load_module_copy(erlang).</div></div><div class=""><div class="">{error,badfile}</div></div><div class=""><div class=""><br class=""></div></div><div class=""><div class="">=ERROR REPORT==== 26-Jul-2019::13:10:11 ===</div></div><div class=""><div class="">Loading of  failed: badfile</div></div><div class=""><div class=""><br class=""></div></div><div class=""><div class="">=ERROR REPORT==== 26-Jul-2019::13:10:11 ===</div></div><div class=""><div class="">beam/beam_load.c(2284): Error loading function xrlang:garbage_collect/2: op bif0 u x:</div></div><div class=""><div class="">  no specific operation found</div></div><div class=""><br class=""></div></blockquote><br class=""><div class="">I’m not very familiar with how NIFs work and are loaded, but is there a workaround for this?</div><div class="">Maybe renaming only the module attribute is not enough.</div><div class="">I’m on MacOS running Erlang/OTP 20 [erts-9.3].</div><div class=""><br class=""></div><div class="">Thank you</div><div class=""><br class=""></div><div class=""><br class=""></div></div></body></html>