<div dir="ltr">Hi!<br>Maybe this approach to a module renaming will be useful for you: <a href="https://github.com/klajo/hacks/blob/master/beam/src/beam_renamer.erl">https://github.com/klajo/hacks/blob/master/beam/src/beam_renamer.erl</a><div>A long while ago I've used beam_renamer in my experiments: <a href="https://github.com/stolen/modhack">https://github.com/stolen/modhack</a> – to inject custom code in other module. I don't remember if that worked with lists module though.<br><br>As Dmitry said, it may be tricky to fiddle with NIFs and BIFs.<br><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Jul 29, 2019 at 2:56 PM Luca Tansini <<a href="mailto:luca.tansini95@gmail.com">luca.tansini95@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="overflow-wrap: break-word;"><div style="overflow-wrap: break-word;">Hi all,<div><br></div><div>I’m trying to load into the VM a copy of a module under a different name.</div><div>The way I’m trying to do it is:</div><div><span class="gmail-m_8314222996170133788Apple-tab-span" style="white-space:pre-wrap"> </span></div><div><span class="gmail-m_8314222996170133788Apple-tab-span" style="white-space:pre-wrap"> </span>- get the binary of the module with code:get_object_code(mod_name)</div><div><span class="gmail-m_8314222996170133788Apple-tab-span" style="white-space:pre-wrap"> </span>- replace the module name inside the binary (otherwise the code loader won’t accept it)</div><div><span class="gmail-m_8314222996170133788Apple-tab-span" style="white-space:pre-wrap"> </span>- reload it with code:load_binary(new_name, “”, RenamedBinary)</div><div><br></div><div>here is some sample code showing the approach: <a href="https://pastebin.com/HpdUR0eU" target="_blank">https://pastebin.com/HpdUR0eU</a>.</div><div><br></div><div>This approach seems to work as long as the original module doesn’t contain any NIFs.</div><div>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><br></div><div>For instance this is what happens with the lists module:</div><div><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><div>>> module_cp:load_module_copy(lists).</div></div><div><div>{module,xists}</div></div><div>>> xists:seq(1,10).</div><div>[1,2,3,4,5,6,7,8,9,10]</div><div><div>>> xists:reverse([1,2,3,4]).</div></div><div><div>** exception error: undefined function erlang:nif_error/1</div></div><div><div> in function xists:reverse/2 (xists.erl, line 95)</div></div></blockquote><div><br></div><div>For the erlang module instead, even the code loading fails:</div><div><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><div>>> module_cp:load_module_copy(erlang).</div></div><div><div>{error,badfile}</div></div><div><div><br></div></div><div><div>=ERROR REPORT==== 26-Jul-2019::13:10:11 ===</div></div><div><div>Loading of failed: badfile</div></div><div><div><br></div></div><div><div>=ERROR REPORT==== 26-Jul-2019::13:10:11 ===</div></div><div><div>beam/beam_load.c(2284): Error loading function xrlang:garbage_collect/2: op bif0 u x:</div></div><div><div> no specific operation found</div></div><div><br></div></blockquote><br><div>I’m not very familiar with how NIFs work and are loaded, but is there a workaround for this?</div><div>Maybe renaming only the module attribute is not enough.</div><div>I’m on MacOS running Erlang/OTP 20 [erts-9.3].</div><div><br></div><div>Thank you</div><div><br></div><div><br></div></div></div>_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" rel="noreferrer" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail_signature"><div dir="ltr"><div><font face="'courier new', monospace">Danil Zagoskin | <a href="mailto:z@gosk.in" target="_blank">z@gosk.in</a></font></div></div></div>