Redesign dlopen() locks to be recursive per thread
picked from
https://github.com/android/platform_bionic/commit/f9c5afb1f9d8e615ab98774a10bbf117962db66d
bm: cherry-pick one patch from freebsd to fix logb() denormals issue

picked from
https://github.com/android/platform_bionic/commit/9c9b0fc7e1dff39baa8cdf2536be9776aa4af766
Prevent deadlock when using fork

When forking of a new process in bionic, it is critical that it
does not allocate any memory according to the comment in
java_lang_ProcessManager.c:
"Note: We cannot malloc() or free() after this point!
A no-longer-running thread may be holding on to the heap lock, and
an attempt to malloc() or free() would result in deadlock."
However, as fork is using standard lib calls when tracing it a bit,
they might allocate memory, and thus causing the deadlock.
This is a rewrite so that the function cpuacct_add, that fork calls,
will use system calls instead of standard lib calls.

Signed-off-by: christian bejram <christian.bejram@stericsson.com>

original identificator of the change is Iff22ea6b424ce9f9bf0ac8e9c76593f689e0cc86

picked from
https://android-review.googlesource.com/#/c/21627/
bionic: make static object initialization thread safe

Issue: we found the static C++ object will be treated as initialized
even the initialization has not been done yet.

root cause: bionic implemented the __cxa_guard_acquire/__cxa_guard_release
which are not compatible with gcc definition.

* In bionic, guard variable is defined as:
gv lock          the static object
0: free          not initialized
2: acquired      ?
6: waiting       ?
1: ?             initialized
In this implementation, we need to check the lowest bit (bit 0) to tell
whether the static object is initialized or not. However, gcc-x86 toolchain
(both android gcc-x86 and ubuntu gcc-x86) will generate the code to check the lowest byte:
Here is assembly code from x86 toolchain from ICS:
	08048550 <_Z12check_staticv>:
	  8048550:	55                   	push   %ebp
	  8048551:	89 e5                	mov    %esp,%ebp
	  8048553:	8d 64 24 e8          	lea    -0x18(%esp),%esp
	  8048557:	89 5d f8             	mov    %ebx,-0x8(%ebp)
	  804855a:	89 75 fc             	mov    %esi,-0x4(%ebp)
	  804855d:	e8 9f ff ff ff       	call   8048501 <__x86.get_pc_thunk.bx>
	  8048562:	81 c3 9a 12 00 00    	add    $0x129a,%ebx
	  8048568:	80 bb 4c 00 00 00 00 	cmpb   $0x0,0x4c(%ebx)
	  					~~~~ lowest BYTE check
	  804856f:	74 0f                	je     8048580 <_Z12check_staticv+0x30>
Then the static object will be treated as initialized when gv's value is
2 (acquired) or 6 (waiting) but the static object is not initialized.

* In ubuntu, it doesn't have that issue, because its gcc will define the
guard variable as:
byte 0: guard
byte 1: waiting status
byte 2: pending status
and gcc checks the lowest BYTE if the static object is initialized and
it can work properly on ubuntu.

* On ARM, it doesn't have that issue because gcc-arm defines the lowest
bit (bit 0) as the guard bit and gcc-arm generates the code to use bit 0
to tell whether the static object is initialized.
    Here is assembly code from ARM toolchain from ICS:
        000084fc <_Z12check_staticv>:
            84fc:       b570            push    {r4, r5, r6, lr}
            84fe:       4c09            ldr     r4, [pc, #36]   ; (8524
<_Z12check_staticv+0x28>)
            8500:       447c            add     r4, pc
            8502:       6823            ldr     r3, [r4, #0]
            8504:       07db            lsls    r3, r3, #31
                                        ~~~~ lowest bit check
            8506:       d40c            bmi.n   8522
<_Z12check_staticv+0x26>

To fix that issue in bionic, we should define the guard variable as same
as the ubuntu gcc.

original Id of the change I885e9adc7f088b9c0a78355bd752f1e6aeec9f07
Signed-off-by: Fengwei Yin <fengwei.yin@intel.com>
Signed-off-by: Jack Ren <jack.ren@intel.com>
Signed-off-by: Bruce Beare <bruce.j.beare@intel.com>

picked from
https://android-review.googlesource.com/34641