vinoski / erlsha2

SHA-224, SHA-256, SHA-384, SHA-512 implemented in Erlang NIFs.
Other
116 stars 45 forks source link

troubles compiling on half word machine #2

Closed hungryblank closed 13 years ago

hungryblank commented 13 years ago

Sorry if this is not an issue of this specific library.

I'm trying to compile this nif on a 64-bit halfword machine but compilation fails with the following errors, I tried to tweak CFLAGS and amend rebar configuration with no joy

/usr/bin/ld: skipping incompatible /usr/local/lib/erlang/lib/erl_interface-3.7.4/lib/liberl_interface.a when searching for -lerl_interface /usr/bin/ld: cannot find -lerl_interface /usr/bin/ld: skipping incompatible /usr/local/lib/erlang/lib/erl_interface-3.7.4/lib/libei.a when searching for -lei
/usr/bin/ld: cannot find -lei collect2: ld returned 1 exit status

vinoski commented 13 years ago

This is most likely caused by the wordsize of the compiled object files and .so for the nif not matching the wordsize of the erl_interface and ei object files stored in the .a archive files. Assuming you're on a *nix system, you can run

file ./c_src/erlsha2_nif.o

to see what the wordsize of your nif compiled object is, then run the following long command to check your ei library:

( mkdir /tmp/tmp$$ && cd /tmp/tmp$$ && \
  ar xv /usr/local/lib/erlang/lib/erl_interface-3.7.4/lib/libei.a ; \
  file *.o ; cd .. ; rm -rf /tmp/tmp$$ )

If would also be good if you could then enter an Erlang shell and run the following so I can see what a halfword emulator looks like system-wise. I expect the output to be:

1> erlang:system_info({wordsize, internal}).
4
2> erlang:system_info({wordsize, external}).
8

but it would be great if you could verify that for me.

If you can show me the output of these commands on your machine, I'm sure we can then figure out what needs to change to make this work.

hungryblank commented 13 years ago
file ./c_src/erlsha2_nif.o
./c_src/erlsha2_nif.o: ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV), not stripped

output of the long command - truncated since all files give same output

x - ei_decode_term.o
x - ei_format.o
x - ei_locking.o
x - ei_malloc.o
x - ei_portio.o
x - ei_printterm.o
x - ei_pthreads.o
x - ei_trace.o
x - ei_x_encode.o
ei_compat.o:           ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
ei_connect.o:          ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
ei_decode_term.o:      ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
ei_format.o:           ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
ei_locking.o:          ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
ei_malloc.o:           ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
eimd5.o:               ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
ei_portio.o:           ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
ei_printterm.o:        ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
ei_pthreads.o:         ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
eirecv.o:              ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
ei_resolve.o:          ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
ei_trace.o:            ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
ei_x_encode.o:         ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped

output of erlang shell

Eshell V5.8.4  (abort with ^G)
1> erlang:system_info({wordsize, internal}).
4
2> erlang:system_info({wordsize, external}).
8

Hope this helps

vinoski commented 13 years ago

OK, thanks. I believe I know how to fix this. Stay tuned.

vinoski commented 13 years ago

Hmm, I think we might need a patch to rebar to fix this. If you want a quick temporary fix, try this patch:

diff --git a/rebar.config b/rebar.config
index 23c632d..f724c2d 100644
--- a/rebar.config
+++ b/rebar.config
@@ -2,8 +2,8 @@

 {port_envs, [
             {"DRV_CFLAGS", "$DRV_CFLAGS -O3 -I."},
-            {"linux.*-32$", "CFLAGS", "-m32"},
-            {"linux.*-32$", "LDFLAGS", "-m32"},
+            {"linux.*-32$", "CFLAGS", "-m64"},
+            {"linux.*-32$", "LDFLAGS", "-m64"},
             {"linux.*-64$", "CFLAGS", "-m64"},
             {"linux.*-64$", "LDFLAGS", "-m64"}
             ]}.

This will hopefully force the build to 64-bit to match the object files in your Erlang installation. Let me know if it works.

UPDATE: another possible approach might be to just define CFLAGS and LDFLAGS on your rebar command line, like this:

CFLAGS=-m64 LDFLAGS=-m64 rebar compile
vinoski commented 13 years ago

Any chance you could restore any changes you might have made to erlsha2 and instead patch your rebar with the changes from my rebar fork? The patch is below, or you can see the differences here. If you try this, please let me know if it fixes the problem.

diff --git a/src/rebar_erlc_compiler.erl b/src/rebar_erlc_compiler.erl
index 38dd198..efbdf09 100644
--- a/src/rebar_erlc_compiler.erl
+++ b/src/rebar_erlc_compiler.erl
@@ -52,7 +52,8 @@
 %%
 %%                OtpRelease = erlang:system_info(otp_release).
 %%                SysArch = erlang:system_info(system_architecture).
-%%                Words = integer_to_list(8 * erlang:system_info(wordsize)).
+%%                Words = integer_to_list(8 *
+%%                            erlang:system_info({wordsize, external})).
 %%
 %%              E.g. to define HAVE_SENDFILE only on systems with
 %%              sendfile(), to define BACKLOG on Linux/FreeBSD as 128,
diff --git a/src/rebar_port_compiler.erl b/src/rebar_port_compiler.erl
index a9b73cc..72c5309 100644
--- a/src/rebar_port_compiler.erl
+++ b/src/rebar_port_compiler.erl
@@ -398,7 +398,8 @@ default_env() ->
      {"DRV_LDFLAGS", "-shared $ERL_LDFLAGS"},
      {"darwin", "DRV_LDFLAGS",
       "-bundle -flat_namespace -undefined suppress $ERL_LDFLAGS"},
-     {"ERLANG_ARCH", integer_to_list(8 * erlang:system_info(wordsize))},
+     {"ERLANG_ARCH",
+      integer_to_list(8 * erlang:system_info({wordsize, external}))},
      {"ERLANG_TARGET", rebar_utils:get_arch()},

      %% Solaris specific flags
diff --git a/src/rebar_utils.erl b/src/rebar_utils.erl
index 792c54f..558e230 100644
--- a/src/rebar_utils.erl
+++ b/src/rebar_utils.erl
@@ -62,7 +62,7 @@ is_arch(ArchRegex) ->
     end.

 get_arch() ->
-    Words = integer_to_list(8 * erlang:system_info(wordsize)),
+    Words = integer_to_list(8 * erlang:system_info({wordsize, external})),
     erlang:system_info(otp_release) ++ "-"
         ++ erlang:system_info(system_architecture) ++ "-" ++ Words.
hungryblank commented 13 years ago

I can confirm that this patch fixes the problem now the extension is compiled without problems.

Thanks

vinoski commented 13 years ago

Great, thanks. I'll close this issue now.