View Single Post
Old 06-16-2014, 02:55 AM   #56
knc1
Going Viral
knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.
 
knc1's Avatar
 
Posts: 17,212
Karma: 18210809
Join Date: Feb 2012
Location: Central Texas
Device: No K1, PW2, KV, KOA
Mixed systems library example

The following example will (behind the scenes) dynamically load libz.so (from the Amazon soft-float libraries).

Since the C-ABI of libz does not pass floating point values - this will work.
I.E: This is a general rule, the "armhf-vfpv3d16" descriptor of our build only applies to parameter passing.

Note: This version of luajit only generates ARMv5-VFP code.
(Which also runs on our ARMv7-VFP3 machine).

Ref: http://luajit.org/ext_ffi_tutorial.html
The source of this example, includes a step-by-step description.

  • ssh into your Kindle
  • switch to the hard-float (and much newer) shell
    Code:
    [root@kindle root]# cd /mnt/us
    [root@kindle us]# exec esys/bin/busybox ash
    [root@kindle us]# export PATH=/mnt/us/esys/bin:/mnt/us/esys/usr/bin:$PATH
  • enter: nano zip-test.lua
  • cut&paste this code into the editor:
    Code:
    local ffi = require("ffi")
    ffi.cdef[[
    unsigned long compressBound(unsigned long sourceLen);
    int compress2(uint8_t *dest, unsigned long *destLen,
    	      const uint8_t *source, unsigned long sourceLen, int level);
    int uncompress(uint8_t *dest, unsigned long *destLen,
    	       const uint8_t *source, unsigned long sourceLen);
    ]]
    local zlib = ffi.load(ffi.os == "Windows" and "zlib1" or "z")
    
    local function compress(txt)
      local n = zlib.compressBound(#txt)
      local buf = ffi.new("uint8_t[?]", n)
      local buflen = ffi.new("unsigned long[1]", n)
      local res = zlib.compress2(buf, buflen, txt, #txt, 9)
      assert(res == 0)
      return ffi.string(buf, buflen[0])
    end
    
    local function uncompress(comp, n)
      local buf = ffi.new("uint8_t[?]", n)
      local buflen = ffi.new("unsigned long[1]", n)
      local res = zlib.uncompress(buf, buflen, comp, #comp)
      assert(res == 0)
      return ffi.string(buf, buflen[0])
    end
    
    -- Simple test code.
    local txt = string.rep("abcd", 1000)
    print("Uncompressed size: ", #txt)
    local c = compress(txt)
    print("Compressed size: ", #c)
    local txt2 = uncompress(c, #txt)
    assert(txt2 == txt)
  • write-out (ctrl-o) and exit (ctrl-x) the editor
  • compile and run: luajit zip-test.lua
  • or: time luajit zip-test.lua
  • or: LD_DEBUG=libs luajit zip-test.lua
    Follow along (scroll back as required) the library loading sequence.
    Note that our ld.so.3 has no problems loading the ld.so.2 format code.

Notes:
With the recommended path setting above, any command not defined in our esys hard-float tree will be run from Amazon's soft-float tree.
I.E: All of the Amazon functions we hack with continue to work un-changed.

With the FFI C interface of LuaJit, you can define and directly access the Amazon system libraries entry points.
**ANY** of them, as long as they don't pass floating point arguments.

Last edited by knc1; 06-16-2014 at 03:06 AM.
knc1 is offline   Reply With Quote