Slide 1

Slide 1 text

System.loadLibrary( “Unix, Native Code and why bother”)

Slide 2

Slide 2 text

Library static shared static dynamic loading (.o, .a, ...) (.so, .dll, .dylib, ...)

Slide 3

Slide 3 text

Program A Program B static libs (.a) static libs (.a) static linking compile-time

Slide 4

Slide 4 text

Program A Program B Program A Program B static libs (.a) static libs (.a) shared libs (.so) static linking dynamic linking compile-time run-time

Slide 5

Slide 5 text

Library static shared bookstore library Paul Richter

Slide 6

Slide 6 text

source code compiler

Slide 7

Slide 7 text

source code compiler objects (.o) .o .o .o .a archive .o

Slide 8

Slide 8 text

source code compiler linker objects (.o) .o .o .o .a archive .o executable .so .so .so .so

Slide 9

Slide 9 text

source code compiler linker loader objects (.o) .o .o .o .a archive .o executable in-memory image .so .so .so .so

Slide 10

Slide 10 text

Building the objects

Slide 11

Slide 11 text

lib1.c

Slide 12

Slide 12 text

main.c

Slide 13

Slide 13 text

Static Libraries

Slide 14

Slide 14 text

Static Libraries

Slide 15

Slide 15 text

Shared Libraries: Static Loading

Slide 16

Slide 16 text

Shared Libraries: Static Loading LD_LIBRARY_PATH

Slide 17

Slide 17 text

Shared Libraries: Dynamic Loading

Slide 18

Slide 18 text

main.c

Slide 19

Slide 19 text

main.c

Slide 20

Slide 20 text

Android

Slide 21

Slide 21 text

Kernel Display driver USB driver Camera driver WiFi driver Power Mgmt. Binder IPC ... Application Framework Apps Home Contacts Dialer Clock Your app 3rd party app ... Activity Manager Window Manager Package Manager Location Manager View System Content Providers ... Libraries Android Runtime (Dalvik/ART) Core Libraries Dalvik Virtual Machine (DVM) Surface Manager OpenGL Media Framework SQLite WebKit SSL ... Bionic

Slide 22

Slide 22 text

Kernel Display driver USB driver Camera driver WiFi driver Power Mgmt. Binder IPC ...

Slide 23

Slide 23 text

Kernel Display driver USB driver Camera driver WiFi driver Power Mgmt. Binder IPC ... Libraries Surface Manager OpenGL Media Framework SQLite WebKit SSL ... Bionic

Slide 24

Slide 24 text

Android Bionic

Slide 25

Slide 25 text

Bionic libc libm libdl Linker libstdc++

Slide 26

Slide 26 text

Bionic libc libm libdl Linker libstdc++ License Size Speed

Slide 27

Slide 27 text

Bionic libc libm libdl Linker libstdc++ BSD + Rewritten code + Android specific services

Slide 28

Slide 28 text

Bionic libc libm libdl Linker libstdc++

Slide 29

Slide 29 text

Bionic libc libm libdl Linker libstdc++ Completely from scratch! dlopen() dlsym() dlerror() dlclose() {

Slide 30

Slide 30 text

Bionic libc libm libdl Linker libstdc++

Slide 31

Slide 31 text

Kernel Display driver USB driver Camera driver WiFi driver Power Mgmt. Binder IPC ... Libraries Android Runtime (Dalvik/ART) Core Libraries Dalvik Virtual Machine (DVM) Surface Manager OpenGL Media Framework SQLite WebKit SSL ... Bionic

Slide 32

Slide 32 text

Kernel Display driver USB driver Camera driver WiFi driver Power Mgmt. Binder IPC ... Application Framework Apps Home Contacts Dialer Clock Your app 3rd party app ... Activity Manager Window Manager Package Manager Location Manager View System Content Providers ... Libraries Android Runtime (Dalvik/ART) Core Libraries Dalvik Virtual Machine (DVM) Surface Manager OpenGL Media Framework SQLite WebKit SSL ... Bionic

Slide 33

Slide 33 text

Kernel Audio driver Application Framework Apps Your app Media Player Libraries Android Runtime (Dalvik/ART) Core Libraries Dalvik Virtual Machine (DVM) Media Framework Media Player Audio Flinger libaudio.so JNI dlopen()

Slide 34

Slide 34 text

System.loadLibrary

Slide 35

Slide 35 text

System.loadLibrary Runtime.loadLibrary

Slide 36

Slide 36 text

Runtime.java

Slide 37

Slide 37 text

Runtime.java

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

Win: main.dll Linux: libmain.so Mac: libmain.dylib

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

LD_LIBRARY_PATH

Slide 43

Slide 43 text

Runtime.java

Slide 44

Slide 44 text

//TODO Runtime::doLoad Runtime.java

Slide 45

Slide 45 text

//TODO Runtime::doLoad

Slide 46

Slide 46 text

//TODO Runtime::doLoad LD_LIBRARY_PATH

Slide 47

Slide 47 text

ClassLoader search (for application libraries) VM search (for system libraries)

Slide 48

Slide 48 text

Runtime.java

Slide 49

Slide 49 text

Workaround for zygote’s LD_LIBRARY_PATH Runtime.java

Slide 50

Slide 50 text

Runtime.cpp

Slide 51

Slide 51 text

Native.cpp

Slide 52

Slide 52 text

Native.cpp

Slide 53

Slide 53 text

Native.cpp

Slide 54

Slide 54 text

Kernel Application Framework Apps Your app Runtime.java Libraries Android Runtime Dalvik Virtual Machine (DVM) Native.cpp Runtime.cpp yourlib.so JNI dlopen()

Slide 55

Slide 55 text

We learned... ● Static vs Shared Libraries ● Dynamic Loading ● Library paths (+ difference on android) ● Android’s internals ○ Bionic ○ Dynamic Loading with Dalvik ○ System.loadLibrary

Slide 56

Slide 56 text

Thanks! @juliozynger soundcloud.com/jobs