Commit graph

1400 commits

Author SHA1 Message Date
Martin Stjernholm
95252eedf3 Add more linker debug.
- Show which executable is being linked, which linker config file is
  being read, and which section in it is being used with, enabled on
  $LD_DEBUG>=1.

- Show more info to follow the dlopen() process, enabled with "dlopen"
  in the debug.ld.xxx property.

Test: Flash, boot, and look at logcat after "adb shell setprop debug.ld.all dlopen"
Bug: 120430775
Change-Id: I5441c8ced26ec0e2f04620c3d2a1ae860b792154
2019-03-19 14:03:18 +00:00
Jiyong Park
036e09c36e Don't install *.mountpoint targets
The *.mountpoint targets that installs /bionic/lib/lib*.so and
/bionic/bin/linker* are no longer needed.

Now, /system/lib/lib*.so and /system/bin/linker* are simply symlinks to
the corresponding files in the runtime apex. For example,

/system/lib/libc.so -> /apex/com.android.runtime/lib/bionic/libc.so

This is made possible because we now activate APEXes even before the
data partition is mounted. Before the data partition mounting, the
APEXes from the system partition are ativated. After the data partition
is mounted, updated APEXes in the partition (if any) are activated. As a
result, the symlink always points to the valid path regardless of
whether /data is mounted or not.

Bug: 125549215
Test: device boots
Change-Id: Ie7d83686abe00b3c436f9f9db75d4244200a0fc9
2019-03-14 07:35:52 +09:00
Ryan Prichard
92b3e1b37b Cosmetic changes to linker allocator and config
Make the num_free_blocks calculation clearer. There are
sizeof(page->bytes) bytes available for blocks, which is equal to
(kAllocateSize - 16). The previous calculation overestimates the amount
of space by 8 or 12 bytes, but that doesn't change the result because
both kAllocateSize and block_size_ are multiples of 16.

Bug: none
Test: boot device, bionic unit tests
Change-Id: Ia880cd5abc2a7bb4e9e6a8c0441d985f4df8fdc4
2019-03-12 01:11:24 +00:00
Ryan Prichard
b0e2dd090b Allow building the linker with -O0
The tlsdesc_resolver_dynamic function is only defined on arm64, but the
code was taking its address on all targets. (Apparently the optimizer was
removing the entire deferred_tlsdesc_relocs loop?)

Bug: none
Test: bionic unit tests
Test: add -O0 to cc_defaults in linker/Android.bp, device boots
Change-Id: I899c78018cbe82aeaca7856c9f7b04300c1d3196
2019-03-11 17:53:21 -07:00
Peter Collingbourne
b39cb3c31d linker: Handle libraries with disjoint mappings correctly.
It's possible and sometimes beneficial for a library to have disjoint mappings
and for other libraries to be mapped into the gap between the mappings using
ANDROID_DLEXT_RESERVED_ADDRESS. See for example the proposal for partitioning
in lld [1].

Because the find_containing_library and do_dl_unwind_find_exidx functions use
a simple bounds check to figure out whether a pointer belongs to a library
they will, given a pointer into a library mapped into the gap of a library
with disjoint mappings, return a pointer to the soinfo for the outer library
instead of the inner one, because the outer library will appear before the
inner one in the solist.

From a user perspective this means that we won't be able to unwind the inner
library's frames on 32-bit ARM with libgcc, dladdr() will return information
for the outer library given a pointer to the inner one and dlopen() et al will
use the linker namespace of the outer library when called from the inner one
(although they will usually be the same).

To make this work correctly, make it so that once find_containing_library
sees a match for the bounds check, it examines the library's PT_LOADs to
make sure that there is a mapping for the given address. This is similar
to how libgcc and libunwind_llvm already handle finding the PT_GNU_EH_FRAME
on non-ARM32 platforms [2,3]. do_dl_unwind_find_exidx is reimplemented in
terms of find_containing_library.

[1] http://lists.llvm.org/pipermail/llvm-dev/2019-February/130583.html
[2] e739ac0e25/libunwind/src/AddressSpace.hpp (L523)
[3] https://android.googlesource.com/toolchain/gcc/+/master/gcc-4.9/libgcc/unwind-dw2-fde-dip.c#294

Test: /data/nativetest{,64}/bionic-unit-tests/bionic-unit-tests on walleye-userdebug
Change-Id: I368fe6ad3c470b3dff80f7d9b04253566d63a7d2
2019-03-08 18:34:34 -08:00
Treehugger Robot
7cdbd0d477 Merge "linker: allow using reserved space for multiple libraries." 2019-02-21 16:06:17 +00:00
Torne (Richard Coles)
efbe9a5eef linker: allow using reserved space for multiple libraries.
Introduce a new flag ANDROID_DLEXT_RESERVED_ADDRESS_RECURSIVE which
instructs the linker to use the reserved address space to load all of
the newly-loaded libraries required by a dlopen() call instead of only
the main library. They will be loaded consecutively into that region if
they fit. The RELRO sections of all the loaded libraries will also be
considered for reading/writing shared RELRO data.

This will allow the WebView implementation to potentially consist of
more than one .so file while still benefiting from the RELRO sharing
optimisation, which would otherwise only apply to the "root" .so file.

Test: bionic-unit-tests (existing and newly added)
Bug: 110790153
Change-Id: I61da775c29fd5017d9a1e2b6b3757c3d20a355b3
2019-02-20 17:29:36 -05:00
vichang
54c864052b Merge "Redirect /system/lib/libicuuc.so regardless of duplication in /system" 2019-02-19 10:14:31 +00:00
Elliott Hughes
d102cf62d1 Merge "Switch linker tests to Android.bp." 2019-02-16 01:11:56 +00:00
Elliott Hughes
f84d0a95bd Merge "libasync_safe: stop clobbering other folks' identifiers." 2019-02-16 00:12:51 +00:00
Elliott Hughes
15a2b7b17b Switch linker tests to Android.bp.
Life is easier if we just keep test code in the same directory as the
stuff it's testing...

Test: tests still build and pass
Change-Id: I9b35d689098bdc28a71d69645b0ca9fdd6ea0108
2019-02-15 14:40:08 -08:00
Elliott Hughes
3019d78d4a libasync_safe: stop clobbering other folks' identifiers.
The log priorities and ids are in an NDK header, available to everyone.

Move CHECK into its own header for now. This would be better if it was
more like the <android-base/logging.h> CHECK family, but I don't have an
easy way to do that without lots of copy & paste, so punting for now.

Bug: https://issuetracker.google.com/issues/119713191
Test: boots
Change-Id: I4566be8a0a024fede0e2d257c98b908ec67af2a8
2019-02-14 14:23:13 -08:00
Pirama Arumuga Nainar
fcd35380dd Disable native_coverage for ld-android and linker
Bug: http://b/116873221

The coverage library, which depends on libc, cannot link with libraries
since they opt of libc.

Test: m NATIVE_COVERAGE=true COVERAGE_PATHS=bionic
Change-Id: Iaaca8bc30c8ba59e5d4f7092b1a0fce8e8c14c72
2019-02-14 13:50:55 -08:00
Victor Chang
af12c94e79 Redirect /system/lib/libicuuc.so regardless of duplication in /system
The change is a workaround of app compat issue until the app fixes the
issue.

Likely, the app does not only dlopen(), but open() the libicuuc.so
with the absolute path.

Note that platform uses the stub libandroidicu.so, not libicuuc directly.
This CL topic moves (not duplicates) libandroidicu from /system to
/apex. Even though /system/lib/libicuuc.so is kept in the default
namespace, but no one should be using it.

Some debug info after this CL:
$ adb shell cat /proc/<zygote pid>/maps | grep libicuuc
$ adb shell cat /proc/<app pid>/maps | grep libicuuc
Only /apex/com.android.runtime/lib64/libicuuc.so is found, not the
/system.

Bug: 124218500
Test: The app can be launched successfully
Test: CtsJniTestCases
Change-Id: I67a50f0ad934be567a63d4038215546c9b4e5f7e
2019-02-13 15:31:06 +00:00
Jiyong Park
efc503d677 Greylist libraries are attempted to be loaded in the default ns
Some of the greylist libraries can be in directories other than
/system/lib. For example, libnativehelper.so is moved from the directory
to /apex/com.android.runtime/lib.

Previous behavior for greylist libraries is to find the lib under
/system/lib while "within" the current namespace which is usually the
classloader-namespace. Since the libs no longer exist there, linker
fails to load it and no further attempt is made.

This change fixes the problem by loading the greylist libs "from" the
default namespace. Since there is a link from the default namespace to
the namespace where the moved libraries are intended to be loaded (e.g.,
the 'runtime' namespace for libnativehelper.so), the call is successful.

Bug: 124201397
Test: bionic-unit-tests

Change-Id: I11d15993d18896bdf663c408f9e40d8a607c9b80
2019-02-12 23:07:08 +09:00
Ryan Prichard
808d176e7e Merge "Fix linker self-exec detection" 2019-02-08 22:33:11 +00:00
Ryan Prichard
1990ba5601 Fix linker self-exec detection
When the linker is invoked on itself, (`linker64 /system/bin/linker64`),
the linker prints an error, because self-invocation isn't allowed. The
current method for detecting self-invocation fails because the second
linker instance can crash in a constructor function before reaching
__linker_init.

Fix the problem by moving the error check into a constructor function,
which finishes initializing libc sufficiently to call async_safe_fatal.
The only important thing missing is __libc_sysinfo on 32-bit x86. The aux
vector isn't readily accessible, so use the fallback int 0x80.

Bug: http://b/123637025
Test: bionic unit tests (32-bit x86)
Change-Id: I8be6369e8be3938906628ae1f82be13e6c510119
2019-02-07 21:48:42 -08:00
Victor Chang
6cb719f0ed Revert "Revert "Allow dlopen("/system/lib/<soname>") for pre-Q""
This reverts commit 1906e77665
and fixes dlopen(nullptr)

It's enabled for the following .so libraries
libicuuc.so
libicui18n.so

This CL does not move the .so files into a APEX.
Thus, no visible effect before moving .so files.

Bug: 121248172
Test: dlfcn#dlopen_from_nullptr
Test: Perform the following test after moving libicuuc to /apex
      dlopen("/system/lib64/libicuuc.so") for targetSdkVersion < Q
Change-Id: Ia310ea566485fe00ca28e2203f4bffbd92243638
2019-02-07 01:17:37 +00:00
vichang
73ee7a6320 Merge "Revert "Allow dlopen("/system/lib/<soname>") for pre-Q"" 2019-02-06 17:03:41 +00:00
vichang
1906e77665 Revert "Allow dlopen("/system/lib/<soname>") for pre-Q"
This reverts commit f6e4cfd933.

Reason for revert: b/123972211

Change-Id: I1ea199c27e224bbd27dee7692f9f448ca9c2b6d1
2019-02-06 16:42:21 +00:00
vichang
9c48951a7d Merge "Allow dlopen("/system/lib/<soname>") for pre-Q" 2019-02-05 12:34:00 +00:00
Victor Chang
f6e4cfd933 Allow dlopen("/system/lib/<soname>") for pre-Q
It's enabled for the following .so libraries
libicuuc.so
libicui18n.so

This CL does not move the .so files into a APEX.
Thus, no visible effect before moving .so files.

Bug: 121248172
Test: Perform the following test after moving libicuuc to /apex
      dlopen("/system/lib64/libicuuc.so") for targetSdkVersion < Q
Change-Id: Ic00d281d59de924c01582c48d2a946e3f5f33a09
2019-02-04 23:45:19 +00:00
Vic Yang
bb7e1236f3 Purge linker block allocators before leaving linker
This is the second attempt to purge linker block allocators.  Unlike the
previously reverted change which purge allocators whenever all objects
are freed, we only purge right before control leaves the linker.  This
limits the performance impact to one munmap() call per dlopen(), in
most cases.

Bug: 112073665
Test: Boot and check memory usage with 'showmap'.
Test: Run camear cold start performance test.
Change-Id: I02c7c44935f768e065fbe7ff0389a84bd44713f0
2019-01-30 22:13:13 -08:00
Jiyong Park
fbc4c8edd6 Move the dynamic linker to ./bootstrap subdir
The dynamic linker installed to /system partition is only for
bootstraping, i.e. to be used only by early processes. The existing path
/system/bin/linker[64] will be a symlink to the mount pointpoint
/bionic/bin/linker[64] which will be bind-mounted with either the
bootstrap linker or the default linker in the runtime APEX. Since the
path is taken by the symlink, bootstrap linker is moved to ./boostrap
subdirectory.

Bug: 120266448
Test: m linker; the linker is found under /system/bin/bootstrap/
Change-Id: I163e0c8a1738b94565a6b60f48e65841ba058a91
2019-01-31 13:44:20 +09:00
Jiyong Park
a5f914aef1 Add bionic mount points under /bionic
This change adds following files and symlinks:

Files:
/bionic/lib[64]/lib{c|dl|m}.so
/bionic/bin/linker[64]

Symlinks:
/system/lib[64]/lib{c|dl|m}.so -> /bionic/lib[64]/lib{c|dl|m}.so
/system/bin/linker[64] -> /bionic/bin/linker[64]
/system/bin/linker_asan[64] -> /bionic/bin/linker[64]

The files serve as mount points for either the bootstrap Bionic or the
default Bionic from the runtime APEX. init does the bind-mounting during
booting.

The symlinks are there to not change the ordinary paths to the bionic
files; there are many places that the paths are implied or hard-coded,
e.g., dlopen("/system/lib/libc.so") or DT_INTERP pointing to
/system/bin/linker in the vendor prebuilts.

Bug: 120266448
Test: m blueline, cf_x86, aosp_arm
The aforementioned files and symlinks are found

Change-Id: I97e38c29409ac0610dde285db8df6e94a7930094
2019-01-31 13:44:20 +09:00
Vic Yang
7bb60fcbcd Revert "linker: Purge block allocator memory when possible"
This reverts commit fb78a4ac1b.

Reason for revert: Performance regression.

Change-Id: Ib12335fc7478dad933da00b8bc525366c9330a17
2019-01-29 20:21:41 -08:00
Ryan Prichard
ffaae70936 Implement arm64 TLSDESC
Each TLSDESC relocation relocates a 2-word descriptor in the GOT that
contains:
 - the address of a TLS resolver function
 - an argument to pass (indirectly) to the resolver function

(Specifically, the address of the 2-word descriptor is passed to the
resolver.)

The loader resolves R_GENERIC_TLSDESC relocations using one of three
resolver functions that it defines:
 - tlsdesc_resolver_static
 - tlsdesc_resolver_dynamic
 - tlsdesc_resolver_unresolved_weak

The resolver functions are written in assembly because they have a
restrictive calling convention. They're only allowed to modify x0 and
(apparently) the condition codes.

For a relocation to memory in static TLS (i.e. the executable or an solib
loaded initially), the loader uses a simple resolver function,
tlsdesc_resolver_static, that returns the static offset it receives from
the loader.

For relocations to dynamic TLS memory (i.e. memory in a dlopen'ed solib),
the loader uses tlsdesc_resolver_dynamic, which allocates TLS memory on
demand. It inlines the fast path of __tls_get_addr, then falls back to
__tls_get_addr when it needs to allocate memory. The loader handles these
dynamic TLS relocations in two passes:
 - In the first pass, it allocates a table of TlsDynamicResolverArg
   objects, one per dynamic TLSDESC relocation.
 - In the second pass, once the table is finalized, it writes the
   addresses of the TlsDynamicResolverArg objects into the TLSDESC
   relocations.

tlsdesc_resolver_unresolved_weak returns a negated thread pointer so that
taking the address of an unresolved weak TLS symbols produces NULL.

The loader handles R_GENERIC_TLSDESC in a target-independent way, but
only for arm64, because Bionic has only implemented the resolver functions
for arm64.

Bug: http://b/78026329
Test: bionic unit tests
Test: check that backtrace works inside a resolver function and inside
  __tls_get_addr called from a resolver
  (gdbclient.py, b __tls_get_addr, bt)
Merged-In: I752e59ff986292449892c449dad2546e6f0ff7b6
Change-Id: I752e59ff986292449892c449dad2546e6f0ff7b6
2019-01-29 08:33:09 +00:00
Ryan Prichard
16455b5100 Implement dynamic TLS accesses and allocation
Initialize a thread's DTV to an empty zeroed DTV. Allocate the DTV and
any ELF module's TLS segment on-demand in __tls_get_addr. Use a generation
counter, incremented in the linker, to signal when threads should
update/reallocate their DTV objects.

A generation count of 0 always indicates the constant zero DTV.

Once a DTV is allocated, it isn't freed until the thread exits, because
a signal handler could interrupt the fast path of __tls_get_addr between
accessing the DTV slot and reading a field of the DTV. Bionic keeps a
linked list of DTV objects so it can free them at thread-exit.

Dynamic TLS memory is allocated using a BionicAllocator instance in
libc_shared_globals. For async-signal safety, access to the
linker/libc-shared state is protected by first blocking signals, then by
acquiring the reader-writer lock, TlsModules::rwlock. A write lock is
needed to allocate or free memory.

In pthread_exit, unconditionally block signals before freeing dynamic
TLS memory or freeing the shadow call stack.

ndk_cruft.cpp: Avoid including pthread_internal.h inside an extern "C".
(The header now includes a C++ template that doesn't compile inside
extern "C".)

Bug: http://b/78026329
Bug: http://b/123094171
Test: bionic unit tests
Change-Id: I3c9b12921c9e68b33dcc1d1dd276bff364eff5d7
2019-01-25 17:53:01 -08:00
Ryan Prichard
3b463cf7f4 Implement TLS_DTPMOD and TLS_DTPREL relocations
Generalize the omitted symbol and missing-TLS-segment behaviors to all TLS
relocations.

R_GENERIC_TLS_DTPMOD is a module ID, which starts at 1 for the executable.

R_GENERIC_TLS_DTPREL is an offset from the start of a module to a specific
TLS symbol.

binutils currently disagrees with Bionic about the values of
R_AARCH64_TLS_DTPMOD64 and R_AARCH64_TLS_DTPREL64, so disable
DTPMOD/DTPREL for now on arm64.

Bug: http://b/78026329
Test: bionic unit tests (Tests for dynamic TLS will be added later)
Change-Id: I05c28d6a1036bdd6127f605036679b7475689445
2019-01-25 17:53:01 -08:00
Ryan Prichard
ea41ab0548 Ignore DT_TLSDESC_GOT / DT_TLSDESC_PLT
These dynamic table entries are used for lazy relocation of TLSDESC
relocations. Bionic relocates everything eagerly and can ignore these
entries.

Bug: http://b/78026329
Test: bionic unit tests
Test: manual
Change-Id: I788c4a95041fae66de2fbeab3babe0205631999c
2019-01-25 17:53:01 -08:00
Ryan Prichard
083d850b30 Move the linker allocator into libc
Rename LinkerMemoryAllocator -> BionicAllocator
Rename LinkerSmallObjectAllocator -> BionicSmallObjectAllocator

libc and the linker need to share an instance of the allocator for
allocating and freeing dynamic ELF TLS memory (DTVs and segments). The
linker also continues to use this allocator.

Bug: http://b/78026329
Test: /data/nativetest/bionic-unit-tests-static
Test: /data/nativetest64/bionic-unit-tests-static
Test: /data/nativetest/linker-unit-tests/linker-unit-tests32
Test: /data/nativetest64/linker-unit-tests/linker-unit-tests64
Change-Id: I2da037006ddf8041a75f3eba2071a8fcdcc223ce
2019-01-25 15:31:35 -08:00
Ryan Prichard
52165b34b9 Replace some of linker_allocator's header includes
The <async_safe/log.h> include in linker_allocator.h was made
unnecessary when the STL dependency was removed.

Replace the linker_*.h header includes with private/bionic_*.h
includes.

This change prepares LinkerMemoryAllocator for use in libc allocating
dynamic ELF TLS memory.

Bug: http://b/78026329
Test: bionic unit tests
Change-Id: I6ad62bd513e71dd3f91a4b724a92b19a1eb6c3b4
2019-01-25 14:48:00 -08:00
Treehugger Robot
70ee1825d0 Merge "Call realpath(3) only when the path is accessible for read" 2019-01-25 22:41:50 +00:00
Treehugger Robot
fbd6f3c796 Merge changes Iece631a5,I53769e0e
* changes:
  linker: Reduce number of mmap()/prctl() calls in block allocator
  linker: Purge block allocator memory when possible
2019-01-25 22:39:27 +00:00
Jiyong Park
42e81985b1 Call realpath(3) only when the path is accessible for read
Suppress the SELinux denial log spam by not calling realpath(3) when the
path does not exist or is not accessible for read, and then not auditing
access(2) failure.

Bug: 120996057
Test: copy ping to /data/local/tmp, run it, verify no errors
Test: run bionic-unit-tests, the tests pass
Change-Id: Ie6058bfc9524a9b5c50fc7183fdddea6a8fb9200
2019-01-25 20:48:12 +09:00
Jiyong Park
57b9d1e6c0 support LIB in runpath
In addition to ORIGIN, LIB is now expanded to either lib or lib64
depending on the ABI in the DT_RUNPATH.

Test: To be added (can't be added to AOSP directly since there are
internal tests that have to be updated as well)

Change-Id: I34a8316f1765a27c731bc270ee25c0ce287d7eed
2019-01-24 17:00:37 +09:00
Vic Yang
dac328ad51 linker: Reduce number of mmap()/prctl() calls in block allocator
Given that the block allocator does not free allocated memory unless
all objects are freed, we can allocate a large chunk of memory at a
time.  This gives us:
  1. Higher space efficiency, especially when the objects being
     allocated has a large size.
  2. Much less mmap()/prctl() calls.
  3. Much less vm_area_struct objects.
While this increases the size of virtual memory allocated, the number
of dirty pages would be unchanged or less.

Test result on a Go device:
  - Zygote starts 24ms faster on average.
  - Kernel vm_area_struct usage 138KB less, across the entire system.

Bug: 112073665
Test: Boot and check the result from 'showmap'.
Change-Id: Iece631a5081151dd1e5bf25dbddac9b264bcdcff
2019-01-23 14:33:02 -08:00
Vic Yang
fb78a4ac1b linker: Purge block allocator memory when possible
If all allocated memory from a block allocator is freed, it is a good
opportunity to purge all the pages allocated to reduce lingering dirty
pages.

Memory saving varies with the platform and what processes are running.
Measuring right after boot, this saves ~1.8MB on cuttelfish and ~1.3MB
on a 32-bit ARM device.

Bug: 112073665
Test: Boot and check memory usage with 'showmap'.
Change-Id: I53769e0ec9699f0b3645cdf281a2c0bbffb98676
2019-01-23 12:24:46 -08:00
Ryan Prichard
314c0f7989 Merge "Fix soinfo_tls::module dangling reference" 2019-01-18 04:15:07 +00:00
Ryan Prichard
bf427f4225 Fix soinfo_tls::module dangling reference
The field was pointing into an element of an std::vector, but the address
of a vector element is invalidated when the vector is resized.

This bug was caught by the new elftls.shared_ie and
elftls_dl.dlopen_shared_var_ie tests.

Bug: http://b/78026329
Test: bionic unit tests
Change-Id: I7232f6d703a9e339fe8966a95b7a68bae2c9c420
2019-01-17 17:13:53 -08:00
Josh Gao
190626872a Merge "linker: only generate log messages when logging is enabled" 2019-01-18 00:50:23 +00:00
Ryan Prichard
9a238653c1 Merge changes Ia08e1b5c,I60e589dd,Ib7edb665,Ibf1bf5ec,Ibd623857
* changes:
  Handle R_GENERIC_TLS_TPREL relocations
  Avoid a dlopen abort on an invalid TLS alignment
  Initialize static TLS memory using module list
  Record TLS modules and layout static TLS memory
  StaticTlsLayout: add exe/tcb and solib layout
2019-01-17 23:10:51 +00:00
Tim Murray
a022034da2 linker: only generate log messages when logging is enabled
Avoids some unnecessary work during dlopen/dlclose/dlsym for most
cases.

Bug: 122471935
Test: fewer page faults during app startup
Change-Id: Ie886e1e671066af3c6f3a895f9a8126f209d6660
2019-01-17 14:24:14 -08:00
Ryan Prichard
fb8730d495 Handle R_GENERIC_TLS_TPREL relocations
This relocation is used for static TLS's initial-exec (IE) accesses.

A TLS symbol's value is its offset from the start of the ELF module's
TLS segment. It doesn't make sense to add the load_bias to this value,
so skip the call to soinfo::resolve_symbol_address.

Allow TLS relocations to refer to an unresolved weak symbol. In that case,
sym will be non-zero, but lsi will be nullptr. The dynamic linker resolves
the TPREL relocation to 0, making &missing_weak_symbol equal the thread
pointer.

Recognize Gold-style relocations to STB_LOCAL TLS symbols/sections and
issue an error.

Remove the "case R_AARCH64_TLS_TPREL64", because the R_GENERIC_TLS_TPREL
case handles it.

Remove the no-op R_AARCH64_TLSDESC handler. It's better to issue an error.
dlopen_library_with_ELF_TLS now fails with a consistent error about an
unimplemented dynamic TLS relocation.

Bug: http://b/78026329
Test: bionic unit tests (elftls tests are added in a later CL)
Change-Id: Ia08e1b5c8098117e12143d3b4ebb4dfaa5ca46ec
2019-01-17 00:45:55 -08:00
Ryan Prichard
1988350d1c Avoid a dlopen abort on an invalid TLS alignment
If the alignment of a TLS segment in a shared object is invalid, return
an error through dlerror() rather than aborting the process.

Bug: http://b/78026329
Test: bionic unit tests
Change-Id: I60e589ddd8ca897f485d55af089f08bd3ff5b1fa
2019-01-16 23:19:40 -08:00
Treehugger Robot
5bccde719f Merge "Fix fd leak in android_dlopen_ext." 2019-01-17 04:16:43 +00:00
Ryan Prichard
e5e69e0912 Record TLS modules and layout static TLS memory
Bug: http://b/78026329
Test: bionic unit tests
Change-Id: Ibf1bf5ec864c7830e4cd1cb882842b644e6182ae
2019-01-16 16:52:47 -08:00
Martin Stjernholm
de853ffa7f Fix fd leak in android_dlopen_ext.
It can happen e.g. if android_dlopen_ext is called with an absolute path to
a file that is not accessible in the current namespace. The first
load_library call in find_library_internal will then open the file and
assign its fd to the task and return false. Then linked namespaces are
searched, and load_library gets called again and opens the same file and
overwrites the fd in the task without closing it first. (In one of the later
calls the namespace config might very well allow the file to be loaded and
the android_dlopen_ext call eventually returns successfully, and the process
continues with the leaked fd.)

The code could perhaps be changed to avoid opening the file repeatedly in
these cases, but the LoadTask class should arguably keep its state clean
anyway.

Bug: 113373927
Test: Flash and boot device with (and without) http://r.android.com/812674,
  which moves libart.so to the runtime namespace and thus makes it load
  /system/framework/*/boot*.oat files across the namespace boundary from
  runtime to default.
Change-Id: Iae91b7c743c5f3f973506153ba52898ae72e6fee
2019-01-17 00:48:18 +00:00
Ryan Prichard
977e47d018 StaticTlsLayout: add exe/tcb and solib layout
Replace reserve_tcb with reserve_exe_segment_and_tcb, which lays out both
the TCB and the executable's TLS segment, accounting for the difference in
layout between variant 1 and variant 2 targets.

The function isn't actually called with a non-null TlsSegment* yet.

Bug: http://b/78026329
Test: bionic unit tests
Change-Id: Ibd6238577423a7d0451f36da7e64912046959796
2019-01-16 15:54:52 -08:00
Ryan Prichard
9ee8069046 Build the linker with -D_USING_LIBCXX
This macro is necessary for using stdatomic.h and <atomic> in the same
libc++-based program. <atomic> is used implicitly by other STL headers,
and stdatomic.h is used (or will be used) in headers shared with libc.
libc only has access to stdatomic.h because it can't use an STL.

Bug: http://b/78026329
Test: bionic unit tests
Change-Id: I89b128df08b478287ef4e2867319e74dbcc30d4e
2019-01-16 01:11:26 -08:00
Ryan Prichard
45d1349c63 Reorganize static TLS memory for ELF TLS
For ELF TLS "local-exec" accesses, the static linker assumes that an
executable's TLS segment is located at a statically-known offset from the
thread pointer (i.e. "variant 1" for ARM and "variant 2" for x86).
Because these layouts are incompatible, Bionic generally needs to allocate
its TLS slots differently between different architectures.

To allow per-architecture TLS slots:
 - Replace the TLS_SLOT_xxx enumerators with macros. New ARM slots are
   generally negative, while new x86 slots are generally positive.
 - Define a bionic_tcb struct that provides two things:
    - a void* raw_slots_storage[BIONIC_TLS_SLOTS] field
    - an inline accessor function: void*& tls_slot(size_t tpindex);

For ELF TLS, it's necessary to allocate a temporary TCB (i.e. TLS slots),
because the runtime linker doesn't know how large the static TLS area is
until after it has loaded all of the initial solibs.

To accommodate Golang, it's necessary to allocate the pthread keys at a
fixed, small, positive offset from the thread pointer.

This CL moves the pthread keys into bionic_tls, then allocates a single
mapping per thread that looks like so:
 - stack guard
 - stack [omitted for main thread and with pthread_attr_setstack]
 - static TLS:
    - bionic_tcb [exec TLS will either precede or succeed the TCB]
    - bionic_tls [prefixed by the pthread keys]
    - [solib TLS segments will be placed here]
 - guard page

As before, if the new mapping includes a stack, the pthread_internal_t
is allocated on it.

At startup, Bionic allocates a temporary bionic_tcb object on the stack,
then allocates a temporary bionic_tls object using mmap. This mmap is
delayed because the linker can't currently call async_safe_fatal() before
relocating itself.

Later, Bionic allocates a stack-less thread mapping for the main thread,
and copies slots from the temporary TCB to the new TCB.
(See *::copy_from_bootstrap methods.)

Bug: http://b/78026329
Test: bionic unit tests
Test: verify that a Golang app still works
Test: verify that a Golang app crashes if bionic_{tls,tcb} are swapped
Merged-In: I6543063752f4ec8ef6dc9c7f2a06ce2a18fc5af3
Change-Id: I6543063752f4ec8ef6dc9c7f2a06ce2a18fc5af3
(cherry picked from commit 1e660b70da)
2019-01-11 15:34:22 -08:00
Ryan Prichard
6b55cc36d3 dir.${section}: downgrade "can't resolve" diag
When parsing a dir.${section} line in ld.config.txt, the linker uses
realpath() to resolve each directory path. If realpath() fails, the linker
uses the non-resolved path instead. Previously, it issued a warning to
stderr and logcat, but realpath() is expected to fail on /postinstall when
running a binary in /data/local/tmp, so downgrade the warning to an INFO()
notice.

Bug: http://b/120996057
Test: copy ping to /data/local/tmp, run it, verify no errors
Test: bionic unit tests
Change-Id: I2104084e12ea18840230997408559299bf3bddac
2019-01-04 15:05:44 -08:00
Ryan Prichard
2ccb2f04e0 Add generic TLS linker reloc macros
Bug: http://b/78026329
Test: build bionic
Change-Id: I7f9d3a250f27c31d0b8d9a3a306468fdf723f9c8
2019-01-02 14:36:26 -08:00
Vic Yang
259429bd85 Further improve of linker memory allocators
This includes:
  - Blocks allocated by small object allocators are now aligned to
    their block size.
  - Remove redundant free_blocks_cnt and allocated_blocks_cnt, as they
    sum up to a fixed number.
  - Remove a redundant padding word in small_object_page_info on 32-bit
    platform.

Test: Build and boot cuttlefish.
Change-Id: Ib922d7af739e3709e8a162c7f68e7f99bf95a914
2018-12-12 17:39:07 -08:00
Vic Yang
5493851e1b Reduce LinkerSmallObjectAllocator memory overhead
The current implementation of LinkerSmallObjectAllocator keeps record
of pages in a vector, which uses its own page(s).  This is at least a
page overhead per LinkerSmallObjectAllocator.

This change removes the page record vector by managing the pages in a
doubly linked list.

We also fix a bug where we are actually keeping up to 2 free pages
instead of just one.

The memory used by small objects when running 'dd', before this change:
    72 KB  [anon:linker_alloc_small_objects]
    28 KB  [anon:linker_alloc_vector]

After this change:
    60 KB  [anon:linker_alloc_small_objects]

Test: Boot cuttlefish and check memory used by linker.
Change-Id: I3468fa4d853c78b4bc02bfb84a3531653f74fb17
2018-12-12 15:53:55 -08:00
Chih-Hung Hsieh
0218e92329 Fix performance-for-range-copy warnings
Bug: 30413223
Test: make with WITH_TIDY=1 DEFAULT_GLOBAL_TIDY_CHECKS=-*,performance*
Change-Id: I41cc70d161468c5586ca8be3185578e65681a9c5
2018-12-11 10:22:11 -08:00
Elliott Hughes
34583c1089 Move dlerror out of a TLS slot and into a pthread_internal_t member.
Bug: N/A
Test: boots, tests pass.
Change-Id: Idf25d2ee457a5d26d0bdd6281cee72c345c8b755
2018-12-06 05:19:57 +00:00
Ryan Prichard
07440a8773 Replace TLS_SLOT_BIONIC_PREINIT w/ shared globals
Instead of passing the address of a KernelArgumentBlock to libc.so for
initialization, use __loader_shared_globals() to initialize globals.

Most of the work happened in the previous CLs. This CL switches a few
KernelArgumentBlock::getauxval calls to [__bionic_]getauxval and stops
routing the KernelArgumentBlock address through the libc init functions.

Bug: none
Test: bionic unit tests
Change-Id: I96c7b02c21d55c454558b7a5a9243c682782f2dd
Merged-In: I96c7b02c21d55c454558b7a5a9243c682782f2dd
(cherry picked from commit 746ad15912)
2018-12-04 13:51:56 -08:00
Ryan Prichard
9cfca866ce Split main thread init into early+late functions
Split __libc_init_main_thread into __libc_init_main_thread_early and
__libc_init_main_thread_late. The early function is called very early in
the startup of the dynamic linker and static executables. It initializes
the global auxv pointer and enough TLS memory to do system calls, access
errno, and run -fstack-protector code (but with a zero cookie because the
code for generating a cookie is complex).

After the linker is relocated, __libc_init_main_thread_late finishes
thread initialization.

Bug: none
Test: bionic unit tests
Change-Id: I6fcd8d7587a380f8bd649c817b40a3a6cc1d2ee0
Merged-In: I6fcd8d7587a380f8bd649c817b40a3a6cc1d2ee0
(cherry picked from commit 39bc44bb0e)
2018-12-04 13:51:36 -08:00
Dan Willemsen
6b3be172d6 Prevent dependency cycle due to system_shared_libs expansion
It was discovered that we were building some objects inconsistently due
to an optimization in cc_library to only build objects once and use them
for both the static and shared libraries. But static libraries didn't
get system_shared_libs set automatically, and we didn't notice that we
would have built the objects differently.

So static libraries now get the default system_shared_libs, we allow
adjusting that for static vs shared in a cc_library, and we disable the
optimization if the linked libraries are configured differently between
static and shared in a single cc_library.

This triggers dependency cycles for static libraries that libc/libdl
use, so fix those cycles here.

Test: treehugger
Change-Id: I3cf7fda161a05ec32e0c1e871999720d12a4d38e
2018-12-03 15:41:56 -08:00
Ryan Prichard
6b70fda682 Merge changes I376d7695,Ied443375,I614d25e7
* changes:
  Use shared globals to init __progname + environ
  Move the abort message to libc_shared_globals
  Expose libc_shared_globals to libc.so with symbol
2018-11-30 02:06:52 +00:00
Tom Cherry
c7cbef4f2d Merge "linker: changes to init work arounds" 2018-11-29 16:50:37 +00:00
Ryan Prichard
48b1159bb8 Use shared globals to init __progname + environ
Initialize the __progname and environ global variables using
libc_shared_globals rather than KernelArgumentBlock.

Also: suppose the linker is invoked on an executable:

    linker prog [args...]

The first argument passed to main() and constructor functions is "prog"
rather than "linker". For consistency, this CL changes the BSD
__progname global from "linker" to "prog".

Bug: none
Test: bionic unit tests
Change-Id: I376d76953c9436706dbc53911ef6585c1acc1c31
2018-11-28 14:26:14 -08:00
Ryan Prichard
7752bcb234 Move the abort message to libc_shared_globals
__libc_shared_globals() is available in dynamic modules as soon as
relocation has finished (i.e. after ifuncs run). Before ifuncs have run,
the android_set_abort_message() function already doesn't work because it
calls public APIs via the PLT. (If this matters, we can use a static
bool variable to enable android_set_abort_message after libc
initialization).

__libc_shared_globals() is hidden, so it's available in the linker
immediately (i.e. before relocation). TLS memory (e.g. errno) currently
isn't accessible until after relocation, but a later patch fixes that.

Bug: none
Test: bionic unit tests
Change-Id: Ied4433758ed2da9ee404c6158e319cf502d05a53
2018-11-28 14:26:14 -08:00
Ryan Prichard
abf736a780 Expose libc_shared_globals to libc.so with symbol
Previously, the address of the global variable was communicated from the
dynamic linker to libc.so using a field of KernelArgumentBlock, which is
communicated using the TLS_SLOT_BIONIC_PREINIT slot.

As long as this function isn't called during relocations (i.e. while
executing an ifunc), it always return a non-NULL value. If it's called
before its PLT entry is relocated, I expect a crash.

I removed the __libc_init_shared_globals function. It's currently empty,
and I don't think there's one point in libc's initialization where
shared globals should be initialized.

Bug: http://b/25751302
Test: bionic unit tests
Change-Id: I614d25e7ef5e0d2ccc40d5c821dee10f1ec61c2e
2018-11-28 14:26:14 -08:00
Vic Yang
de69660bbd Fix free pages count in LinkerSmallObjectAllocator
Free pages count should be incremented by 1 when a new page is
allocated.  Without this fix, free pages count underflows and the
allocator ends up freeing a free page whenever the last object in that
page is freed.  In other words, it doesn't hold onto a free page as
expected and thus we may see more mmap/munmap calls.

Test: Set breakpoint at the end of __linker_init and check
      free_pages_cnt values are either 0 or 1.

Change-Id: I259a3a27329aab6835c21b4aa7ddda89dac9655b
2018-11-27 13:34:44 -08:00
Ryan Prichard
0ff8df5f6a Merge "Cleanup: __libc_init_AT_SECURE, auxv, sysinfo" 2018-11-27 21:23:21 +00:00
Ivan Lozano
f17fd1d68a Disable XOM in linker, libc, and libm.
These modules have issues running with execute-only memory. Disable it
in them until we can resolve the issues.

Bug: 77958880
Test: No more XOM-related crashes in these binaries

Change-Id: Ie6c957731155566c2bbe7dbb7a91b9583d9aff93
2018-11-27 07:56:17 -08:00
Ryan Prichard
701bd0cc88 Cleanup: __libc_init_AT_SECURE, auxv, sysinfo
__sanitize_environment_variables is only called when getauxval(AT_SECURE)
is true.

Instead of scanning __libc_auxv, reuse getauxval. If the entry is missing,
getauxval will set errno to ENOENT.

Reduce the number of times that __libc_sysinfo and __libc_auxv are
initialized. (Previously, __libc_sysinfo was initialized 3 times for the
linker's copy). The two variables are initialized in these places:
 - __libc_init_main_thread for libc.a (including the linker copy)
 - __libc_preinit_impl for libc.so
 - __linker_init: the linker's copy of __libc_sysinfo is still initialized
   twice, because __libc_init_main_thread runs after relocation. A later
   CL consolidates the linker's two initializations.

Bug: none
Test: bionic unit tests
Change-Id: I196f4c9011b0d803ee85c07afb415fcb146f4d65
2018-11-26 18:37:13 -08:00
Jiyong Park
a4f3625112 Fix: search path is not added when one of its parent is not accessible
When /foo/bar/baz is added to the search paths and if getattr (stat())
is not allowed on one of its parent paths, i.e., /foo and /foo/baz, the
path was thought as non-existent and wasn't added to the search paths of
the namespace.

Fixing the bug by adding the path if the path (though not the parents)
does exist.

Bug: 119656753
Test: m apex.test; m; device boots.
Change-Id: I21bca1fee9aa20688ce9b72192d3173821ad91a3
2018-11-16 21:05:10 +09:00
Mark Salyzyn
ba1a723ad1 switch to using android-base/file.h instead of android-base/test_utils.h
Test: compile
Bug: 119313545
Change-Id: I664fb32522d01909c603d7b903475c4e9aea9223
2018-11-14 15:46:49 -08:00
Mark Salyzyn
33c3a066ae linker unit tests requires liblog
Test: compile
Bug: 119313545
Change-Id: Ic6263c7db55d27ccb5075478aedae30c58ad0044
2018-11-14 13:05:27 -08:00
Elliott Hughes
ff1428a48e Move API levels from uint32_t to int.
(cherrypick of a6c71a09670ca636cca5cfea9d74b03a951e2b5e.)

Bug: N/A
Test: builds
Change-Id: I9c414e30e3c4fe2a4e16a2fe4ce18eae85fe4844
2018-11-13 21:25:07 -08:00
Tom Cherry
66bc428f93 linker: changes to init work arounds
Change three things regarding the work around to the fact that init is
special:

1) Only first stage init is special, so we change the check to include
   accessing /proc/self/exe, which if is available, means that we're
   not first stage init and do not need any work arounds.
2) Fix the fact that /init may be a symlink and may need readlink()
3) Suppress errors from realpath_fd() since these are expected to fail
   due to /proc not being mounted.

Bug: 80395578
Test: sailfish boots without the audit generated from calling stat()
      on /init and without the errors from realpath_fd()

Change-Id: I266f1486b142cb9a41ec791eba74122bdf38cf12
2018-11-08 21:50:19 +00:00
Chih-hung Hsieh
bbd465a230 Merge "Revert "Disable lld, failed to link bionic/linker."" 2018-11-08 04:58:14 +00:00
Chih-hung Hsieh
34d1f2ddf2 Revert "Disable lld, failed to link bionic/linker."
This reverts commit 73de62e461.

Reason for revert: bug was fixed upstream and included into AOSP lld
Bug: 78115263
Test: make checkbuild

Change-Id: Ib9e4af1efdcdb547c7bd65cce4ce48d7f09ed217
2018-11-07 21:55:18 +00:00
Elliott Hughes
8178c417f6 ART isn't using the ART-specific linker features any more.
Bug: N/A
Test: ran tests
Change-Id: Ide3c295035687019608a2c4716a4a21cb889d121
2018-11-06 11:15:17 -08:00
Jiyong Park
358334145e ld.config.txt for APEX
When executing an executable in an APEX (i.e., /apex/<name>/bin),
ld.config.txt file is read from the same APEX, not from /system/etc.

Bug: 115787633
Test: m apex.test; adb push ...apex.test.apex /data/apex; adb reboot
Test: adb root; adb shell /apex/com.android.example.apex/bin/dex2oat
is runnable.

Change-Id: I6400251f99d24f2379dbaf655ecd84da02490617
2018-10-31 12:21:02 +09:00
Elliott Hughes
04164f6d05 Merge "Clean up bionic_macros.h a bit." 2018-10-26 00:09:15 +00:00
Elliott Hughes
5e62b34c0d Clean up bionic_macros.h a bit.
Use <android-base/macros.h> instead where possible, and move the bionic
macros out of the way of the libbase ones. Yes, there are folks who manage
to end up with both included at once (thanks OpenGL!), and cleaning that
up doesn't seem nearly as practical as just making this change.

Bug: N/A
Test: builds
Change-Id: I23fc544f39d5addf81dc61471771a5438778895b
2018-10-25 11:00:00 -07:00
Vic Yang
48b6911397 linker: Fix fd leak
Close the file descriptor before throwing it away.

Test: mmma bionic
Change-Id: I1690c1bb8b619f82070503151b1de73302882310
2018-10-24 14:14:26 -07:00
Treehugger Robot
d84f8b5eb2 Merge "Rework the linker_wrapper to work with lld" 2018-10-23 03:43:28 +00:00
Dan Willemsen
5038ef6748 Workaround host bionic libs that are missing DT_RUNPATH
We don't have a host bionic version of
libclang_rt.asan-x86_64-android.so, so I'm using the android version,
which can't load liblog.so, since it's missing DT_RUNPATH that would
normally load liblog.so from a relative path to the .so.

Bug: 118058804
Test: run ASAN host_bionic
Change-Id: I58badcd5ed35bd1c7b786b4f1e2367a1011ff08d
2018-10-22 15:55:56 -07:00
Dan Willemsen
d6bf019204 Rework the linker_wrapper to work with lld
This is use by Host Bionic to bootstrap into an embedded copy of the
linker by tweaking the AT_* values before calling in to the linker entry.

Similarly to 9729f35922, get the base
address from AT_PHDR, so that we're not relying on the relative offset
before relocation, which doesn't work with lld (at least with the
standard flags).

To find the offset to the linker code, we can still use an absolute
symbol created by extract_linker (which is currently hardcoded to 0x1000).

Instead of relying on something similar for the linker entry point,
we're now just reading the entry point from the linker's ELF header.

Then we get the address to the real _start function using
host_bionic_inject, which injects the value into a global variable after
the link step is finished. It also uses that opportunity to verify that
the linker is embedded as we expect it to be.

Bug: 31559095
Test: build with host bionic
Change-Id: I9d81ea77c51c079de06905da1ebe421fead1dc3b
2018-10-22 22:52:25 +00:00
Ryan Prichard
8f639a4096 Allow invoking the linker on an executable.
The executable can be inside a zip file using the same syntax used for
shared objects: path.zip!/libentry.so.

The linker currently requires an absolute path. This restriction could be
loosened, but it didn't seem important? If it allowed non-absolute paths,
we'd need to decide how to handle:
 - foo/bar      (relative to CWD?)
 - foo          (search PATH / LD_LIBRARY_PATH, or also relative to CWD?)
 - foo.zip!/bar (normalize_path() requires an absolute path)

The linker adjusts the argc/argv passed to main() and to constructor
functions to hide the initial linker argument, but doesn't adjust the auxv
vector or files like /proc/self/{exe,cmdline,auxv,stat}. Those files will
report that the kernel loaded the linker as an executable.

I think the linker_logger.cpp change guarding against (g_argv == NULL)
isn't actually necessary, but it seemed like a good idea given that I'm
delaying initialization of g_argv until after C++ constructors have run.

Bug: http://b/112050209
Test: bionic unit tests
Change-Id: I846faf98b16fd34218946f6167e8b451897debe5
2018-10-10 14:31:06 -07:00
Ryan Prichard
269bb496c5 Fix normalize_path's handling of "/.."
Currently it normalizes the path to a string with a single uninitialized
byte. It should instead normalize it to "/".

Bug: none
Test: /data/nativetest/linker-unit-tests/linker-unit-tests32
Test: /data/nativetest64/linker-unit-tests/linker-unit-tests64
Change-Id: I06e0f7598d16acfa21875dad53efbc293cfeb44d
2018-10-08 13:27:16 -07:00
Ryan Prichard
0adf09b370 linker: fix invalid zip file handling
The argument to CloseArchive has type ZipArchiveHandle, but we're
passing it a ZipArchiveHandle*. The compiler doesn't detect the type
mismatch because ZipArchiveHandle is a typedef for void*.

Remove a duplicate close() call:

The fourth argument to OpenArchiveFd is "bool assume_ownership = true".
Even if the function fails, ownership of the fd is still transferred to
a ZipArchive object that's deleted when this code calls CloseArchive.

AFAIK, this code path is rarely or never hit.

Bug: none
Test: manual (eventually, 'linker64 /system!/foo')
Change-Id: I95d79809b6e118fb3c39c7b98b8055c8e324db1a
2018-10-08 13:27:16 -07:00
Yi Kong
c15baefc0a Merge "Exclude libclang_rt.builtins symbols" 2018-10-04 22:09:55 +00:00
Yi Kong
7786a344ce Exclude libclang_rt.builtins symbols
Similar to libgcc, libclang_rt.builtins symbols need to be excluded.

Bug: 29275768
Change-Id: Iaf7381de3b4dbd92997abd03667dea0baaab98e1
2018-10-03 10:07:13 +00:00
Elliott Hughes
b177085ce7 Add reallocarray(3).
Originally a BSD extension, now in glibc too. We've used it internally
for a while.

(cherry-pick of e4b13f7e3ca68edfcc5faedc5e7d4e13c4e8edb9.)

Bug: http://b/112163459
Test: ran tests
Change-Id: I813c3a62b13ddb91ba41e32a5a853d09207ea6bc
Merged-In: I813c3a62b13ddb91ba41e32a5a853d09207ea6bc
2018-09-26 14:24:18 -07:00
Dimitry Ivanov
e4e3de819d Merge "linker: extract defaults and source files" 2018-09-26 09:03:45 +00:00
Chih-Hung Hsieh
9a64b56334 Add noexcept to move constructors and assignment operators.
Bug: 116614593
Test: build with WITH_TIDY=1
Change-Id: I9f8760cddb3c25255cd24604606af84d837d55e9
2018-09-25 14:00:44 -07:00
dimitry
b8b3a76606 linker: extract defaults and source files
Move source files to filegroup and extract c/ldflags and
version-script to linker_defaults.

Bug: http://b/71494052
Test: make
Change-Id: Ic82885e1c006f91a8446978fabd214f87cd20510
2018-09-25 16:17:24 +02:00
Elliott Hughes
d16cface55 State clearly that an ELF file has the wrong architecture.
Before:

  "libx.so" has unexpected e_machine: 40 (EM_ARM)

After:

  "libx.so" is for EM_X86_64 (62) instead of EM_AARCH64 (183)

Bug: N/A
Test: `LD_PRELOAD=/system/lib64/libm-x86_64.so date` on arm64
Change-Id: I5bb40c435bd22b4e11fe802615925e10db7fb631
2018-09-17 15:50:09 -07:00
Treehugger Robot
dbf54d2270 Merge "Fix linker's _r_debug (gdb) info" 2018-08-22 20:18:30 +00:00
Elliott Hughes
99d54656bd Add PR_SET_VMA and PR_SET_VMA_ANON_NAME to <sys/prctl.h>.
We've copied & pasted these to too many places. And if we're going to
have another go at upstreaming these, that's probably yet another reason
to have the *values* in just one place. (Even if upstream wants different
names, we'll likely keep the legacy names around for a while for source
compatibility.)

Bug: http://b/111903542
Test: ran tests
Change-Id: I8ccc557453d69530e5b74f865cbe0b458c84e3ba
2018-08-22 10:36:23 -07:00
Ryan Prichard
0489645e00 Fix linker's _r_debug (gdb) info
* Initialize the exe's l_ld correctly, and initialize its l_addr field
   earlier.

 * Copy the phdr/phnum fields from the linker's temporary soinfo to its
   final soinfo. This change ensures that dl_iterate_phdr shows the phdr
   table for the linker.

 * Change init_linker_info_for_gdb a little: use an soinfo's fields to
   init the soinfo::link_map_head field, then reuse the new
   init_link_map_head function to handle the linker and the executable.

Test: manual
Test: bionic-unit-tests
Bug: https://issuetracker.google.com/112627083
Bug: http://b/110967431
Change-Id: I40fad2c4d48f409347aaa1ccb98d96db89da1dfe
2018-08-21 17:20:07 -07:00
Treehugger Robot
8f2298e73f Merge "Move [vdso] after exe/linker in _r_debug" 2018-08-22 00:13:00 +00:00
Ryan Prichard
14dd9923b0 Move [vdso] after exe/linker in _r_debug
gdbserver assumes that the first entry is the exe, so it must come
first.

Fixes debugging of executables with gdb.

Bug: https://issuetracker.google.com/112627083
Bug: http://b/110967431
Test: gdbclient.py -r toybox
Change-Id: I7b30398d679c3f8b92d8d02572f9073ae0fce798
2018-08-20 22:07:27 -07:00
Evgenii Stepanov
be551f596f HWASan support in bionic.
* Allow sanitization of libc (excluding existing global sanitizers)
  and disallow sanitization of linker. The latter has not been
  necessary before because HWASan is the first sanitizer to support
  static binaries (with the exception of CFI, which is not used
  globally).
* Static binary startup: initialize HWASan shadow very early so that
  almost entire libc can be sanitized. The rest of initialization is
  done in a global constructor; until that is done sanitized code can
  run but can't report errors (will simply crash with SIGTRAP).
* Switch malloc_common from je_*  to __sanitizer_*.
* Call hwasan functions when entering and leaving threads. We can not
  intercept pthread_create when libc depends on libclang_rt.hwasan.
  An alternative to this would be a callback interface like requested
  here:
    https://sourceware.org/glibc/wiki/ThreadPropertiesAPI

All of the above is behind a compile-time check
__has_feature(hwaddress_sanitizer). This means that HWASan actually
requires libc to be instrumented, and would not work otherwise. It's
an implementation choice that greatly reduces complexity of the tool.
Instrumented libc also guarantees that hwasan is present and
initialized in every process, which allows piecemeal sanitization
(i.e. library w/o main executable, or even individual static
libraries), unlike ASan.

Change-Id: If44c46b79b15049d1745ba46ec910ae4f355d19c
2018-08-21 00:15:47 +00:00
Ryan Prichard
006d137d0d Remove old workaround for unloading unversioned soinfo
This code path is/was a workaround for poorly-behaved apps. AFAIK it isn't
needed anymore.

The (needed != nullptr) condition should have been (needed == nullptr), so
rather than unload a library, it tends to do nothing instead. If it can't
find the library, it would segfault.

Bug: http://b/112154263
Test: /data/nativetest/bionic-unit-tests/bionic-unit-tests
Test: /data/nativetest/linker-unit-tests/linker-unit-tests32
Test: apply patch to pi-dev, verify that apps from b/72143978 still work
Change-Id: Ic598cb3dcead9f88005764a9b8746ed6b35f5f38
2018-08-03 13:50:29 -07:00
Yi Kong
32bc0fcf69 Modernize codebase by replacing NULL with nullptr
Fixes -Wzero-as-null-pointer-constant warning.

Test: m
Bug: 68236239
Change-Id: I5b4123bc6709641315120a191e36cc57541349b2
2018-08-02 18:09:44 -07:00
Treehugger Robot
78b4a82d7d Merge "linker: find AT_BASE using AT_PHDR/AT_PHNUM" 2018-07-31 21:01:49 +00:00
Ryan Prichard
c1c8a188b2 Fix arm32/64 dynamic TLS relocations
arm32: Add a relocation for TLS descriptors (e.g. gcc's
-mtls-dialect=gnu2).

arm64: Add all the dynamic TLS relocations.

Two of the relocations here are obsolete:

 - ARM documents R_ARM_SWI24 as an obsolete static relocation without
   saying what it did. It's been replaced by R_ARM_TLS_DESC, a dynamic
   relocation. We could probably remove it, but I left it because arm32
   is old, and I see the macro in other libc's. It's probably analogous
   to R_ARM_THM_SWI8, which is also an obsolete relocation reserved for
   a future dynamic relocation.

 - I couldn't find any ARM documentation at all for
   R_AARCH64_TLS_DTPREL32. It seems to have been part of three
   relocations:

    - R_AARCH64_TLS_DTPREL32 1031
    - R_AARCH64_TLS_DTPMOD32 1032
    - R_AARCH64_TLS_TPREL32 1033

Bug: b/78026329
Test: run bionic unit tests
Change-Id: I5e7432f6e3e906152dc489be5e812fd8defcbafd
2018-07-30 23:49:19 -07:00
Ryan Prichard
9729f35922 linker: find AT_BASE using AT_PHDR/AT_PHNUM
When the linker is invoked directly, rather than as an interpreter for a
real program, the AT_BASE value is 0. To find the linker's base address,
the linker currently relies on the static linker populating the target of
a RELA relocation with an offset rather than leaving it zero. (With lld,
it will require a special flag, --apply-dynamic-relocs.)

Instead, do something more straightforward: the linker already finds the
executable's base address using its PHDR table, so do the same thing when
the linker is run by itself.

Bug: http://b/72789859
Test: boots, run linker/linker64 by itself
Change-Id: I4da5c346ca164ea6f4fbc011f8c3db4e6a829456
2018-07-26 20:31:47 -07:00
Josh Gao
f6e5b58260 Introduce api to track fd ownership in libc.
Add two functions to allow objects that own a file descriptor to
enforce that only they can close their file descriptor.

Use them in FILE* and DIR*.

Bug: http://b/110100358
Test: bionic_unit_tests
Test: aosp/master boots without errors
Test: treehugger
Change-Id: Iecd6e8b26c62217271e0822dc3d2d7888b091a45
2018-07-19 14:28:54 -07:00
Elliott Hughes
a897151426 linker: improve "bad ELF magic" error.
Include what we actually saw.

Bug: http://b/79463184
Test: ran tests
Change-Id: Id5f8aa3ab94b411ea839b0a1ada9beda9053a10f
2018-06-27 14:39:06 -07:00
Jiyong Park
d3c08f28a3 Don't emit warning on missing directories
Some directories (e.g., /product/bin, etc.) in ld.config.txt may
not exist in some devices. Since many of them are optional directories,
don't emit warning when realpath() gives ENOENT for the paths.

Test: m -j
Change-Id: Ic4fa7db05bde53d3aa5df47291e83b4cdc09aa1f
2018-06-22 00:59:23 +09:00
Inseob Kim
216323bd84 Resolve paths of dir.${section} if possible
Some devices place some of their partitions under /system. If the linker
resolves that paths, verboseness of ld.config.txt will be reduced as we
don't need to add both /system/{partition} and /{partition}.

Bug: http://b/80422611
Test: m -j, boot on taimen, atest
Change-Id: I6b712170bb89229b764026e2cc517c426e6e6063
2018-06-18 19:16:34 +09:00
Jiyong Park
31cd08f9eb dynamic linker is running for init
init is now built as a dynamic executable, so the dynamic linker has to
be able to run in the init process. However, since init is launched so
early, even /dev/* and /proc/* file systems are not mounted and thus
some APIs that rely on the paths do not work. The dynamic linker now
goes alternative path when it is running in the init process.

For example, /proc/self/exe is not read for the init since we always now
the path of the init (/init). Also, arc4random* APIs are not used since
the APIs rely on /dev/urandom. Linker now does not randomize library
loading order and addresses when running in the init process.

Bug: 80454183
Test: `adb reboot recovery; adb devices` shows the device ID
Change-Id: I29b6d70e4df5f7f690876126d5fe81258c1d3115
2018-06-08 14:50:14 +09:00
Jiyong Park
8d7866c58f Build recovery variant of the dynamic linker
In order to support shared libraries in the recovery mode, the dynamic
linker is now built with recovery_available: true option.

In addition, a few more modules (such as libasync, etc.) are also marked
as recovery_available: true as they are transitive dependencies of the
dynamic linker.

Bug: 63673171
Test: `adb reboot recovery; adb devices` shows the device ID
Test: `adb root && adb shell` and then
$ lsof -p `pidof adbd` shows that libm.so, libc.so, etc. are loaded from
the /lib directory.

Change-Id: Idd981d8cf25568a85b24032cf78e50adfd5f4a7f
2018-06-08 14:49:56 +09:00
Ryan Prichard
742982d3a0 Avoid post-reloc GOT usage in __linker_init
A GOT lookup happening prior to soinfo::link_image causes a segfault. With
-O0, the compiler moves GOT lookups from after __linker_init's link_image
call to the start of __linker_init.

Rename the existing __linker_init_post_relocation to linker_main, then
extract the existing post-link_image code to a new
__linker_init_post_relocation function.

Bug: http://b/80503879
Test: /data/nativetest64/bionic-unit-tests/bionic-unit-tests
Test: manual
Change-Id: If8a470f8360acbe35e2a308b0fbff570de6131cf
2018-05-31 17:04:52 -07:00
Ryan Prichard
27475b5105 Initialize __libc_sysinfo early on.
__libc_sysinfo is hidden, so accessing it doesn't require a relocated GOT.
It is important not to have a relocatable initializer on __libc_sysinfo,
because if it did have one, and if we initialized it before relocating the
linker, then on 32-bit x86 (which uses REL rather than RELA), the
relocation step would calculate the wrong addend and overwrite
__libc_sysinfo with garbage.

Asides:

 * It'd be simpler to keep the __libc_sysinfo initializer for static
   executables, but the loader pulls in libc_init_static (even though it
   uses almost none of the code in that file, like __libc_init).

 * The loader has called __libc_init_sysinfo three times by the time it
   has relocated itself. A static executable calls it twice, while libc.so
   calls it only once.

Bug: none
Test: lunch aosp_x86-userdebug ; emulator
Test: adb shell /data/nativetest/bionic-unit-tests/bionic-unit-tests
Test: adb shell /data/nativetest/bionic-unit-tests-static/bionic-unit-tests-static
Change-Id: I5944f57847db7191608f4f83dde22b49e279e6cb
2018-05-30 16:44:23 -07:00
Ryan Prichard
6631f9b03d Clean up TLS_SLOT_BIONIC_PREINIT usage a bit
- It is only needed for dynamic executables, so move the initialization
   out of __libc_init_main_thread and just before the solib constructor
   calls. For static executables, the slot was initialized, then never
   used or cleared. Instead, leave it clear.

 - For static executables, __libc_init_main_thread already initialized the
   stack guard, so remove the redundant __init_thread_stack_guard call.

 - Simplify the slot access/clearing a bit in __libc_preinit.

 - Remove the "__libc_init_common() will change the TLS area so the old one
   won't be accessible anyway." comment. AFAICT, it's incorrect -- the
   main thread's TLS area in a dynamic executable is initialized to a
   static pthread_internal_t object in the linker, then reused by libc.so.

Test: adb shell /data/nativetest/bionic-unit-tests/bionic-unit-tests
Test: adb shell /data/nativetest/bionic-unit-tests-static/bionic-unit-tests-static
Change-Id: Ie2da6f5be3ad563fa65b38eaadf8ba6ecc6a64b6
2018-05-30 15:43:43 -07:00
Dimitry Ivanov
aa922bbaf3 Merge "Add secondary counter in place of tls_nodelete flag" 2018-05-30 15:32:40 +00:00
dimitry
55547db434 Add secondary counter in place of tls_nodelete flag
The tls_nodelete state should apply to load_group not
isolated soinfo. This actually also means that multiple
soinfos may have tls_counter on their dso_handles.

This change replaces TLS_NODELETE flag with secondary counter.
Note that access to the secondary counter (located inside soinfo)
is pretty expensive because it requires soinfo lookup by dso_handle
whereas dso_handle counter is much faster. This is why it is updated
only when dso_handle counter starts or hits 0.

Bug: http://b/80278285
Test: bionic-unit-tests --gtest_filter=dl*
Change-Id: I535583f6714e45fa2a7eaf7bb3126da20ee7cba9
2018-05-30 10:56:59 +02:00
Dimitry Ivanov
d980cc0c51 Merge "Remove libskia.so from the greylist" 2018-05-30 08:28:15 +00:00
dimitry
6e6772dcb2 Remove libskia.so from the greylist
The library is no longer available on the devices.

Bug: http://b/31971097
Test: make
Change-Id: Ie87bb8a8c200d086d272ca923242bfbab6397126
2018-05-25 10:37:49 +02:00
Ryan Prichard
7046f391d1 Reenable __work_around_b_24465209__ for x86 linker
Bug: b/80258696
Test: build aosp_x86-userdebug ; run emulator
Change-Id: If7de24c3fc91a143c0bd17c6364c56ec04dfb08e
(cherry picked from commit dac8340c43)
2018-05-24 20:14:56 -07:00
Treehugger Robot
b886592923 Merge "Mark as recovery_available: true" 2018-05-22 05:16:20 +00:00
Logan Chien
bcfe3cf06e Use ro.vndk.lite to determine the ld.config.txt
This commit adds an extra check on `ro.vndk.lite`.  If `ro.vndk.lite` is
true, the linker will pick `/system/etc/ld.config.vndk_lite.txt`.

The purpose of this change is to distinguish:

1. Master-GSI + Master-VENDOR (w/o BOARD_VNDK_VERSION for upgrading devices)
2. Master-GSI + O-MR1-VENDOR (w/ BOARD_VNDK_VERSION)

Bug: 78605339
Test: aosp_sailfish Master-SYSTEM + Master-VENDOR boots
Test: aosp_sailfish Master-GSI    + Master-VENDOR boots
Test: aosp_walleye  Master-SYSTEM + Master-VENDOR boots
Test: aosp_walleye  Master-GSI    + Master-VENDOR boots
Test: aosp_walleye  Master-GSI    + O-MR1-VENDOR boots
Change-Id: I34f243f73c173ca2e882d3738ccb81e3fad3a9da
Merged-In: I34f243f73c173ca2e882d3738ccb81e3fad3a9da
(cherry picked from commit dd18472e56)
2018-05-17 14:27:00 +08:00
Logan Chien
886b96ef9a Extract ld.config.txt lookup code
This commit extracts ld.config.txt lookup code into
`get_ld_config_file_path()`.

Bug: 78605339
Test: aosp_walleye-userdebug builds and boots
Change-Id: I129f19cd032de02a56bda57231521c02a4b4e4c0
Merged-In: I129f19cd032de02a56bda57231521c02a4b4e4c0
(cherry picked from commit 21e496cf29)
2018-05-17 14:26:54 +08:00
Jiyong Park
5603c6e6b9 Mark as recovery_available: true
Libraries that are direct or indirect dependencies of modules installed
to recovery partition (e.g. toybox) are marked as recovery_available:
true. This allows a recovery variant of the lib is created when it is
depended by other recovery or recovery_available modules.

Bug: 67916654
Bug: 64960723
Bug: 63673171
Bug: 29921292
Test: m -j
Change-Id: I59bf859a10a218af6591025a0fe7d1853e328405
2018-05-14 18:08:01 +09:00
dimitry
8b142566e3 vdso should be available in all namespaces
vdso should be available in all namespaces when present. This
bug went undetected because the way libc currently uses vdso (it
does all the lookups itself). This makes it available for the
programs that want to take advantage by dlopening it.

Bug: http://b/73105445
Bug: http://b/79561555
Test: adb shell /data/nativetest/arm/bionic-unit-tests/bionic-unit-tests --gtest_filter=dl.exec_with_ld_config_file
Test: adb shell /data/nativetest/bionic-unit-tests/bionic-unit-tests --gtest_filter=dl*
Change-Id: I8eae0c9848f256190d1c9ec85d10dc6ce383a8bc
(cherry picked from commit 69c68c46ac)
2018-05-12 10:58:00 +02:00
Elliott Hughes
c01b849c9a Remove obsolete workaround.
We removed the cast in question in
d390df1dbc.

Bug: N/A
Test: builds
Change-Id: I56a034432fd11aeca00c09cb11226bdae465366f
2018-05-09 09:54:30 -07:00
dimitry
7217a092a6 Libraries without dt_soname are inaccessible
When linker tries to check if a library without dt_soname is accessible
it crashes. This change fixes this problem to return false instead
(making them inaccessible from other namespaces)

This went unnoticed because vendor libraries on current
devices all have dt_soname set. This was only discovered
on one of the newer devices which has a vendor prebuilt
library without a soname.

Bug: http://b/78661414
Bug: https://issuetracker.google.com/77287902
Test: cts-tradefed run commandAndExit cts -m CtsJniTestCases
Change-Id: Idb192b4ed7a810840ba2a9177bad2360ffbb75e2
(cherry picked from commit 94f7a87510)
2018-04-29 13:39:46 +02:00
Chih-Hung Hsieh
73de62e461 Disable lld, failed to link bionic/linker.
* Upstream bug: https://bugs.llvm.org/show_bug.cgi?id=36295

Bug: 78115263
Test: make checkbuild
Change-Id: Id38d90f90ca2e3505a47e40c1a3fe0647e7f3a32
2018-04-16 09:51:50 -07:00
Ryan Prichard
470b66644b Break libdl.so and ld-android.so's dependency on libc.so.
* Specify "nocrt: true" to avoid calling __cxa_finalize.

 * Define a dummy __aeabi_unwind_cpp_pr0 for arm32.

Bug: b/62815515
Bug: b/73873002
Test: boot AOSP hikey960-userdebug
Test: run ndk_cxa_example in special /system/bin/debug namespace
Test: run Bionic unit tests
Change-Id: I59bcb100a2753286b59c51a47d7a183507219a07
2018-03-30 13:56:03 -07:00
Elliott Hughes
9724e93c19 Reject .so files using ELF TLS.
Bug: http://b/74361956
Test: ran tests
Change-Id: I53e71252eb08c607c2c436dcba433374c8c53887
2018-03-23 18:46:07 -07:00
Elliott Hughes
9076b0c4e7 Be clearer about linker warnings.
Explicitly say "warning" for warnings, explicitly say what action
we're going to take (such as "(ignoring)"), always provide a link to
our documentation when there is one, explicitly say what API level the
behavior changes at, and explicitly say why we're allowing the misbehavior
for now.

Bug: http://b/71852862
Test: ran tests, looked at logcat
Change-Id: I1795a5af45deb904332b866d7d666690dae4340b
2018-02-28 12:37:28 -08:00
dimitry
153168c855 Fix RTLD_NEXT/DEFAULT lookup
RTLD_NEXT/DEFAULT lookup should not skip handle lookup
in the case when the load group is RTLD_GLOBAL.

Note that there is a difference between load group is local group.
The local group includes externally referenced libraries where
the load group does not. The external reference in this context is a
DT_NEEDED library that belongs to a previously loaded group.

Bug: http://b/72237367
Test: bionic-unit-tests --gtest_filter=dl*
Change-Id: I8997cc961c13a5396f1756161798b45ed1cab16c
2018-02-20 17:10:00 +01:00
Elliott Hughes
4e6bf9e9fa Remove obsolete debuggerd32/debuggerd64 hack.
Bug: N/A
Test: builds
Change-Id: I159eea4e1c0d88e5e51184479ed020e7597390ba
2018-02-16 10:53:03 -08:00
Elliott Hughes
cbc80ba9d8 Switch the rest of our internal headers to #pragma once.
We've been using #pragma once for new internal files, but let's be more bold.

Bug: N/A
Test: builds
Change-Id: I7e2ee2730043bd884f9571cdbd8b524043030c07
2018-02-13 14:27:17 -08:00
Ryan Prichard
f857d59635 Switch x86 begin.c to asm; align ESP correctly
Every other architecture already uses an assembly file here.

The previous code aligned ESP incorrectly, but it doesn't really matter
because everything is built with Clang's -mstackrealign, which realigns
ESP in every function prologue.

Bug: http://b/73140672#comment4
Test: lunch aosp_x86-eng; m; emulator; device boots
Test: manual
Change-Id: I921fd7848cdc611b4f8f13d1176d1983ffea952d
2018-02-12 21:43:12 -08:00
Treehugger Robot
6fa9bddbc7 Merge "linker_memory: return success in enable_fallback_allocator." 2018-02-10 01:44:07 +00:00
Elliott Hughes
d50a1de565 Run bpfmt manually.
Bug: N/A
Test: builds
Change-Id: I0cf145c3b699ac8ef170a63366832f63a9cc1a91
2018-02-05 17:30:57 -08:00
dimitry
321476a3ec Test ld-android.so directly
This CL addresses review comments from
https://android-review.googlesource.com/c/platform/bionic/+/595067

Test: bionic-unit-tests --gtest_filter=dl*
Change-Id: I2c0fdf2a89fe6ff134308e202fb99a74080ee0ed
2018-01-29 15:32:37 +01:00
Treehugger Robot
2e2a8930e5 Merge "linker: simpler encoding for SHT_RELR sections." 2018-01-27 00:00:17 +00:00
Rahul Chaudhry
f16b65932b linker: simpler encoding for SHT_RELR sections.
This change modifies the encoding used in SHT_RELR sections to a simpler
version that gives better results. This encoding was suggested by Andrew
Grieve and is described in this post on generic-abi@googlegroups.com:
    https://groups.google.com/d/msg/generic-abi/bX460iggiKg/Pi9aSwwABgAJ

Bug: None
Test: Built image for marlin, flashed on device, ran arm and
      aarch64 binaries containing '.relr.dyn' sections using
      the new encoding.

Change-Id: I266affe0fbad91dc375995985a221cb02499447b
2018-01-26 11:46:47 -08:00
Logan Chien
9ee4591cb4 linker: Allow link namespaces without name filters
This commit allows users to create a link without soname filters between
two linker namespaces.

The motivation is to establish one-way shared library isolation.  For
example, assume that there are two linker namespaces `default` and
`vndk`.  We would like to limit the shared libraries that can be used by
the `default` namespace.  In the meanwhile, we would like to allow the
`vndk` namespace to use shared libs from the `default` namespace if the
soname cannot be find in the search path or loaded sonames of the `vndk`
namespace.

          shared_libs  = %VNDK_CORE_LIBRARIES%
          shared_libs += %VNDK_SAMEPROCESS_LIBRARIES%
    vndk <-------------------------------------------- default
       \_______________________________________________/^
                allow_all_shared_libs = true

android_link_namespaces_all_libs() is added to libdl, but it is
versioned as LIBC_PRIVATE.  android_link_namespaces_all_libs() is only
for unit tests.

Bug: 69824336

Test: adb shell /data/nativetest/linker-unit-tests/linker-unit-tests32
Test: adb shell /data/nativetest64/linker-unit-tests/linker-unit-tests64

Test: adb shell /data/nativetest/bionic-unit-tests/bionic-unit-tests
Test: adb shell /data/nativetest64/bionic-unit-tests/bionic-unit-tests

Test: Update /system/etc/ld.config*.txt and check whether the vndk
linker namespace of the vendor process can access the shared libs from
the default linker namespace.

Change-Id: I2879f0c5f5af60c7e56f8f743ebd2872e552286b
2018-01-25 14:45:29 +08:00
Josh Gao
72282add20 linker_memory: return success in enable_fallback_allocator.
Instead of aborting when in use, return a bool instead.

Test: debuggerd_test
Change-Id: Ifd2e4439303c95054298b0a05e0cb648ded1306c
2018-01-24 15:08:53 -08:00
Elliott Hughes
d00d38b36b Merge "linker: add experimental support for SHT_RELR sections." 2018-01-23 00:24:26 +00:00
Rahul Chaudhry
b7feec7454 linker: add experimental support for SHT_RELR sections.
This change adds experimental support for SHT_RELR sections, proposed
here: https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg

Definitions for the new ELF section type and dynamic array tags, as well
as the encoding used in the new section are all under discussion and are
subject to change. Use with caution!

Bug: None
Test: Built image for marlin, flashed on device, ran arm and
      aarch64 binaries containing '.relr.dyn' sections.

Change-Id: I2953ae932d3c42ae394e71f8fa058013758a1778
2018-01-22 12:41:27 -08:00
Elliott Hughes
5cec377f49 Address a bunch of clang-tidy complaints.
There were a bunch more unreasonable/incorrect ones, but these ones
seemed legit. Nothing very interesting, though.

Bug: N/A
Test: ran tests, benchmarks
Change-Id: If66971194d4a7b4bf6d0251bedb88e8cdc88a76f
2018-01-19 15:56:12 -08:00
Treehugger Robot
39ee5a4b47 Merge "Adapt to the new libc++/libc++abi update." 2018-01-11 04:29:33 +00:00
dimitry
06016f226e Fix dlclose for libraries with thread_local dtors
Introduce new flag to mark soinfo as TLS_NODELETE when
there are thread_local dtors associated with dso_handle
belonging to it.

Test: bionic-unit-tests --gtest_filter=dl*
Test: bionic-unit-tests-glibc --gtest_filter=dl*
Bug: https://github.com/android-ndk/ndk/issues/360
Change-Id: I724ef89fc899788f95c47e6372c38b3313f18fed
2018-01-10 10:24:06 +01:00
dimitry
581723ebc7 Make ld-android.so export linker symbols
ld-android.so exports exact same set of symbols
as linker. Since it is not supposed to be loaded
every symbol points to fail() method.

Test: make
Test: bionic-unit-tests --gtest_filter=dl*
Change-Id: I23bec365b302ce4ddf5f08832e665ae2b181cf8a
2018-01-09 11:27:23 +01:00
dimitry
11da1dcd8c Move ld-android.so build under linker/
This will allow us to apply linker's version script to the "fake"
version of ld-android.so.

Test: make
Change-Id: I55645cc0fc90e6c65e2269ba2340a908aeed1eaa
2018-01-09 11:13:40 +01:00
dimitry
8e8c2c0013 Unhardcode linker soname
Set and use DT_SONAME instead.

Test: bionic-unit-tests
Change-Id: I38a246c8a43664792424e8fef96ae2ff5f743ba6
2018-01-09 11:11:46 +01:00
Dan Albert
4d1cc9df11 Adapt to the new libc++/libc++abi update.
C++17 removed a handful of things that are used in the libclang
headers, which are used by the versioner. Enable the flag that
re-exposes these until we have a C++17 compatible libclang.

Add a stub posix_memalign to the linker. libc++abi uses posix_memalign
when allocating exceptions, which the linker does not use.

Test: make checkbuild
Bug: None
Change-Id: I32f9d0591ef99a610f27efed90a5c9fd150f0d3e
2018-01-08 14:44:42 -08:00
dimitry
965d06da1e Fix logic in loading dependencies crossing namespace boundaries
This change addresses multiple problems introduced by
02586a2a34

1. In the case of unsuccessful dlopen the failure guard is triggered
for two namespaces which leads to double unload.

2. In the case where load_tasks includes libraries from 3 and more
namespaces it results in incorrect linking of libraries shared between
second and third/forth and so on namespaces.

The root cause of these problems was recursive call to find_libraries.
It does not do what it is expected to do. It does not form new load_tasks
list and immediately jumps to linking local_group. Not only this skips
reference counting it also will include unlinked but accessible library
from third (and fourth and fifth) namespaces in invalid local group. The
best case scenario here is that for 3 or more namesapces this will
fail to link. The worse case scenario it will link the library
incorrectly with will lead to very hard to catch bugs.

This change removes recursive call and replaces it with explicit list of
local_groups which should be linked. It also revisits the way we do
reference counting - with this change the reference counts are updated after
after libraries are successfully loaded.

Also update soinfo_free to abort in case when linker tries to free same
soinfo for the second time - this makes linker behavior less undefined.

Test: bionic-unit-tests
Bug: http://b/69787209
Change-Id: Iea25ced181a98c6503cce6e2b832c91d697342d5
2017-12-10 22:04:55 +01:00
Alin Jerpelea
816b259880 Merge "linker: add support for odm partition"
am: 7b5073767d

Change-Id: I0c1356f53cd33e0cd6a78f0798fed8b395893859
2017-12-06 04:09:37 +00:00
Treehugger Robot
7b5073767d Merge "linker: add support for odm partition" 2017-12-06 04:03:07 +00:00
Jiyong Park
2573be637c Merge "Allow property += value in ld.config.txt"
am: bfd06f63e5

Change-Id: Id536cea8a7161c5dc31f654d1989eb61ff966a2e
2017-12-04 01:52:14 +00:00
Treehugger Robot
bfd06f63e5 Merge "Allow property += value in ld.config.txt" 2017-12-04 01:43:27 +00:00
Justin Yun
bf9cf750a4 Merge "Use ld.config.$VER.txt when current VNDK version is $VER"
am: 86a3803799

Change-Id: I350a75a183717e3cd0a68d3a9ed524f19f077a2f
2017-12-03 14:32:36 +00:00
Justin Yun
53ce74288c Use ld.config.$VER.txt when current VNDK version is $VER
When ro.vndk.version is set to a specific version, not "current",
use ld.config.$VER.txt as a linker namespace configuration file,
where $VER is the VNDK version set by ro.vndk.version.

Because ro.vndk.version is set by the vendor partition, the
configuration file will be automatically selected by the VNDK version
of vendor patition.

If ro.vndk.version is current or not set, ld.config.txt will be used
as before.

Bug: 69531793
Test: Build for a Pixel2 device.
  In the out/target/product/<device> directory,
    rename system/etc/ld.config.txt to system/etc/ld.config.27.1.0.txt
    rename system/lib[64]/vndk to system/lib[64]/vndk-27.1.0
    copy system/lib[64]/vndk-sp to system/lib[64]/vndk-sp-27.1.0
    set ro.vndk.version to 27.1.0 in vendor/default.prop
  Build system and vendor images with "make snod" and "make vnod".
  Disble vbmeta using avbtool.
  Flash a device and check boot.

Change-Id: Ic55bb0a741d434e5fa93e109be15df9d9de3f105
2017-12-03 23:26:53 +09:00
Jiyong Park
8b02951135 Allow property += value in ld.config.txt
ld.config.txt currently does not support split line. As the file gets
larger, this limitation makes the file very unreadable. Now, long lines
can be avoided by breaking one line into multiple lines using +=
operator.

ex)

namespace.default.search.paths = /system/${LIB}
namespace.default.search.paths += /system/${LIB}

Delimitor (':' for *.paths and *.shared_libs, and ',' for *.links) is
automatically added.

Bug: 69888716
Test: linker-unit-tests passes

Change-Id: I4b94fd4e7f8a76d59db8d1096c86aa2118e46625
2017-12-01 10:58:10 +09:00
Alin Jerpelea
074a9fd3da linker: add support for odm partition
If libraries are on the odm partition they will fail to load
This patch adds the odm path as a search path for linker.

Test: libraries load from the odm partition
Change-Id: Ia7786e047cc565d74d25c025dacf9266b3763650
Signed-off-by: Alin Jerpelea <alin.jerpelea@son.com>
2017-12-01 06:21:17 +09:00
Elliott Hughes
65ba81b4e8 Merge "Revert "Fix bug with double unload on unsuccessful dlopen""
am: 4eb332439a

Change-Id: I21ba611931b2e5794ef49cae9bcafc8748ebe804
2017-11-29 19:34:10 +00:00
Elliott Hughes
27f1806b90 Revert "Fix bug with double unload on unsuccessful dlopen"
This reverts commit 58554ccb8a.

causes /vendor/bin/qseecomd to hit the new abort:

[    8.983301] c5    603 DEBUG: Abort message: 'soinfo=0x7147894cd0 is not in soinfo_list (double unload?)'

Bug: http://b/69909887
Bug: http://b/69787209
Change-Id: Ied38f797e0a071a1acc5ed41adf1b45e855143c7
2017-11-29 18:48:33 +00:00
Dimitry Ivanov
f1e645c67b Merge "Fix bug with double unload on unsuccessful dlopen"
am: dc89324e93

Change-Id: Ibff72e8a13cdc737d950dc71f2612fc99740109f
2017-11-29 09:37:01 +00:00
dimitry
58554ccb8a Fix bug with double unload on unsuccessful dlopen
In the case of unsuccessful dlopen the failure guard is triggered
for two namespaces which leads to double unload.

Also update soinfo_free to abort in case when linker tries to free same
soinfo for the second time - this makes linker behavior less undefined.

Test: bionic-unit-tests
Bug: http://b/69787209
Change-Id: I886787ee021b050667f967bce7aa2708390886ea
2017-11-28 21:47:01 +01:00
Xin Li
74d57a37cb Merge commit 'e3d21c84af0b8e3244e4c67a873d2a16bfb85034' into HEAD
Change-Id: I09b21c2e27a0dfbef10633b20d8ee7adbedc1597
2017-11-14 13:14:23 -08:00
dimitry
c92ce715c5 Log dlclose calls and library unloads
Bug: http://b/29458203
Bug: http://b/68262627
Test: make && run bionic-unit-tests
Test: set debug.ld.all to dlopen and check the logs
Change-Id: I9a5495843a4145f267cc3a4714294d05b1e1fe90
2017-10-27 15:38:15 +02:00
Dimitry Ivanov
0b1c8be3be Merge "linker: fix error message for inaccessible libs" 2017-10-24 06:44:19 +00:00
Treehugger Robot
8ab16c5da6 Merge "Set search path to ro.vndk.version property" 2017-10-24 01:17:00 +00:00
dimitry
8db36a51ff linker: fix error message for inaccessible libs
Added a test to make sure linker produces correct error message
when user attempts to load a library in a linked namespace and fails.

Bug: http://b/67866190
Bug: http://b/64950640
Bug: http://b/64888291
Test: bionic-unit-test --gtest_filter=dlext*
Change-Id: I5b5c2070d1388eff123118350b2b5c8fc7571a29
2017-10-23 15:14:01 +02:00
Josh Gao
27242c642e Actually correctly call vector::erase after remove_if.
vector::erase(iterator) erases the element that that iterator points
to, vector::erase(iterator a, iterator b) erases the range [a, b), with
a == b being a no-op.

Test: LD_PRELOAD=libc.so sh
Change-Id: I6a85c1cfaa8eb67756cb75d421f332d5c9a43a33
2017-10-20 17:47:29 -07:00
Sundong Ahn
8fc5032ed2 Set search path to ro.vndk.version property
The permitted and search paths should be changed according to VNDK
version for VNDK snapshot. So, {VNDK_VER} value is added in ld.config.txt
file and this value is set through the linker.
If ro.vndk.version is not set, search paths are vndk and vndk-sp as
before.

Bug: 66074376
Test: build & run
Change-Id: I266b66fe1bc95d8925053ef497db11ac0a57c082
2017-10-20 13:41:14 +09:00
Treehugger Robot
9f69fc714a Merge "Correctly call vector::erase after std::remove_if." 2017-10-20 02:29:58 +00:00
Jiyong Park
01162f2469 Fix: linker segfault on dlopen of a DF_1_GLOBAL so
Added the missing null check routine.

Bug: 67755729
Test: dlfcn.dlopen_df_1_global added and it passes
Change-Id: Ibe8db18b0b5a481e2e9937041abef6d6b179dd87
2017-10-19 15:13:54 +09:00
Josh Gao
44f6e189d9 Correctly call vector::erase after std::remove_if.
std::remove_if moves removed elements to the end, without actually
resizing the collection. To do so, you have to call erase on its
returned iterator.

Test: mma
Change-Id: Iae7f2f194166408f2b101d0c1cfc95202d8bbe63
2017-10-18 17:29:39 -07:00
Christopher Ferris
68039e5b67 Merge "Force _start to be the top frame for the linker." am: 5f1a8440ad am: 30ed16f7c7
am: c46317416a

Change-Id: I48f4dc665c22f3391c80509b6712ff63cbf76925
2017-10-17 18:06:58 +00:00
Christopher Ferris
aa81761c5e Force _start to be the top frame for the linker.
I noticed that sometimes the old unwinder will add an extra PC 0 frame
after this change, but the new unwinder works in all cases. I'm not going
to fix the old unwinder since I plan to remove it very soon.

Bug: 67784501

Test: Forced a crash in the linker and verified that the unwind
Test: stops in __dl_start. Tested on arm/aarch64/x86/x86_64.

Change-Id: Id6585768023256be5c1d341df7b06b786a220b40
2017-10-16 14:43:34 -07:00
Elliott Hughes
1f62be3dcf Merge "Fix mip64 build." am: 92b0f36acc am: 7d8623a4d6
am: c67348fa04

Change-Id: I21b1005762e2b0bb8342f117dbe75d6d5d0b9ea5
2017-10-15 04:28:02 +00:00
Goran Ferenc
1622b1c5fa Merge "MIPS: Fix MIPS linker VDSO issues" am: 73651cf23f am: 76c07c50af
am: ce67915364

Change-Id: If352ae2e944de5e67ce32192fd96f93a2cd0b9a0
2017-10-15 04:26:57 +00:00
Elliott Hughes
ab413c535c Fix mip64 build.
Fallout from the unified sysroot work.

Bug: N/A
Test: builds
Change-Id: If0595a241b9ce0d8c8c7137ddaf8fca932487b7c
2017-10-13 13:22:24 -07:00
Goran Ferenc
e8c76b7a30 MIPS: Fix MIPS linker VDSO issues
This patch resolves two issues:
(1) AOSP MIPS linker crash with SIGSEGV while relocating VDSO GOT
(2) Missing of MIPS_ABI_FP_ANY flag while ckecking & adjusting MIPS FP modes

(1): AOSP MIPS linker crash with SIGSEGV while relocating VDSO GOT

During bringup and testing of the kernel VDSO support on the emulator we
encountered an issue where all userspace processes were crashing due to
linker crash with SIGSEGV during VDSO GOT relocation.

The mentioned scenario will trigger SIGSEGV in the linker only if the kernel
code base contains VDSO implementation introduced with the following commits:

a7f4df4 MIPS: VDSO: Add implementations of gettimeofday() and clock_gettime()
c0a9f72 irqchip: irq-mips-gic: Provide function to map GIC user section
ebb5e78 MIPS: Initial implementation of a VDSO

Another prerequisite is that the linker contains the following commit from AOSP:
https://android-review.googlesource.com/#/c/264857/

The above commit introduces auxvec.h header containing definition of
AT_SYSINFO_EHDR in:
https://android.googlesource.com/platform/bionic/+/master/libc/kernel/uapi/asm-mips/asm/auxvec.h,
which in turn activates VDSO GOT relocations in mips_relocate_got():
https://android.googlesource.com/platform/bionic/+/master/linker/linker_mips.cpp#149

Since VDSO is mapped as a RO region, writing anything to its page will result
in SIGSEGV.

Removing this 0xdeadbeef cookies writes to the got[0]/got[1] solved SIGSEGV issue.
We also compared with the glibc linker code and we haven't seen anything similar
like writing some cookie values into GOT entries.

(2): Missing of MIPS_ABI_FP_ANY flag while checking & adjusting MIPS FP modes

This issue was found during testing of the patch:
https://android-review.googlesource.com/#/c/platform/bionic/+/494440/

This patch adds a bionic dlfcn.dlopen_vdso test which tries to open "linux-vdso.so.1"
(virtual ELF shared library) and expects to succeed.

dlopen fails in the mips linker part due to the following error:
"Unsupported MIPS32 FloatPt ABI 0 found in linux.vdso.so.1"

According to:
https://dmz-portal.imgtec.com/wiki/MIPS_O32_ABI_-_FR0_and_FR1_Interlinking#A.2._O32_FPXX_code_calling_FPXX,
ABI 0 is:
Val_GNU_MIPS_ABI_FP_ANY = 0 /* Not tagged or not using any ABIs affected by the differences.  */

The issue appeared because MIPS_ABI_FP_ANY is not supposed to be checked as
a possible FP ABI-variant compatibility flag.

This patch adds logic to MIPS linker to handle this case appropriately,
after which dlfcn.dlopen_vdso test passes.

Test:
* bionic-unit-tests --gtest_filter=dlfcn.dlopen_vdso
* Boot android in emulator with kernel which supports VDSO optimizations

Change-Id: Icbcd9879beea1b38fbe8d97b3b205058eaa861f4
Signed-off-by: Goran Ferenc <goran.ferenc@imgtec.com>
2017-10-13 12:08:30 +02:00
Dan Albert
9db212cda3 Merge "Add missing includes." am: 36f7b8b789 am: 19ecbbd6a1
am: b4b0e42c0d

Change-Id: Ib0c31b5e29cf2b78e3e5f68bd82318ba570236a3
2017-10-11 21:02:21 +00:00
Dan Albert
1c78cb0fee Add missing includes.
Test: mma
Bug: None
Change-Id: I0221b213e08d07cc5ac0b704a86e98ae8c0f456f
2017-10-11 11:25:52 -07:00
Jiyong Park
1a524d6248 Revert "linker: disable ld.config.txt in ASAN mode"
This reverts commit 81b175747d.

Reason: 02586a2a34 fixed the bug that
prevented us from using LD_PRELOAD with multiple namespaces.

Bug: 38114603
Test: 1. ./external/compiler-rt/lib/asan/scripts/asan_device_setup --lib
prebuilts/clang/host/linux-x86/clang-stable/lib64/clang/5.0/lib/linux
2. enable talkback shortcut
3. in the home screen, hold vol-up/down together
4. device does not reboots and talkback shortcut is toggled
Test: bionic-unit-tests and linker-unit-tests successful

(cherry picked from commit 6ab40bbf53)

Merged-In: I25a05927ffbb28b9fa72303652893f43918ccec6
Change-Id: If264f0ce5d7187816d3acc790b6e31d479b628ee
2017-10-04 07:32:28 +09:00
Dan Willemsen
3491d65b3f Merge "Rename target.linux[_x86[_64]] to target.linux_glibc[_x86[_64]]" am: c6021960a4 am: 735fef090d
am: c423d7afec

Change-Id: Ibb8b2bbc1fa73c44313ec52e996a7f10de152029
2017-10-03 03:28:54 +00:00
Dan Willemsen
c6021960a4 Merge "Rename target.linux[_x86[_64]] to target.linux_glibc[_x86[_64]]" 2017-10-03 02:30:05 +00:00
Chih-Hung Hsieh
dfbff07c92 Merge "Use -Werror in bionic" am: 9385d778fd am: 7887421aed
am: a4fcd22ffd

Change-Id: I40302d02f9f70c2d020d029336c4c73816636de8
2017-10-02 22:28:38 +00:00
Treehugger Robot
9385d778fd Merge "Use -Werror in bionic" 2017-10-02 22:04:48 +00:00
Chih-Hung Hsieh
84f0dcd59e Use -Werror in bionic
Bug: 66996870
Test: build with WITH_TIDY=1
Change-Id: Ic68141a5c50880c485646e38349f94b866267bd9
2017-10-02 13:21:22 -07:00
Dan Willemsen
3a3982d779 Rename target.linux[_x86[_64]] to target.linux_glibc[_x86[_64]]
In the future, target.linux will apply to all targets running a linux kernel
(android, linux_glibc, linux_bionic). So move all current users to the specific
linux_glibc.

There will be another cleanup pass later that will move some instances back to
target.linux if the properties should be shared with target.android and
target.linux_bionic, but target.linux needs to be removed first.

Test: out/soong/build.ninja identical before/after
Change-Id: I56c84d206fb1e98cd7ea0d8fb875de0ebd87ea5e
Exempt-From-Owner-Approval: build system cleanup
2017-10-02 10:41:07 -07:00
dimitry
0de4982099 Merge "Replace abort with exit(1) in __linker_cannot_link" am: 95f90784ea am: 2161b032e0
am: 4eae0fc50a

Change-Id: I3ffe725ed2977cb211ba96d02dca03e7d4046547
2017-09-29 20:05:17 +00:00
dimitry
04f7a798cf Replace abort with exit(1) in __linker_cannot_link
Bug: http://b/67038409
Test: bionic-unit-tests
Change-Id: I7d39b44f2da8c5111ac8a9faf3416f19d5a35c05
2017-09-29 19:48:46 +02:00
dimitry
df775eda92 Merge "Link vdso before linking the main executable" am: 62767bc31d am: 348161ed3c
am: ce97d243f1

Change-Id: Ic65207315cd9cb03d77f305428d73d6cb0936bac
2017-09-26 17:20:29 +00:00
dimitry
c18de1bd47 Link vdso before linking the main executable
Also set linked flag after it is linked and
call constructors. Set RTLD_NODELETE flag to
prevent accidental unloads.

Test: bionic-unit-tests --gtest_filter=dl*:Dl*
Test: bionic-unit-tests-glibc --gtest_filter=dl*
Change-Id: Ib2178849b918cbefd6f8fcfe6d1f78889fe0bf76
2017-09-26 14:34:18 +02:00
Jiyong Park
93b277a6d2 Merge "Don't resolve permitted.paths" into oc-mr1-dev
am: 52cd1e7a42

Change-Id: I249a82481761739704fc236a25642ef6204c084b
2017-09-22 02:38:38 +00:00
Jiyong Park
0f33f23a76 Don't resolve permitted.paths
Linker resolves the paths in /system/etc/ld.config.txt to canonicalize
the paths and to ensure they exist. However, as permitted paths for the
default namespace contain directories such as /vendor/app, /mnt/expand,
and etc., the resolving causes selinux denial on some processes which do
not have access to some of the permitted paths.

In order to silence the bogus selinux denial, resolution is skipped for
permitted paths.

Note that the resolution is not strictly required especially for
Treble-ized devices where permitted paths are already canonicalized (i.e
/vendor isn't a symlink to /system/vendor).

Bug: 65843095
Test: no selinux denial on /vendor/app, /vendor/framework, etc. while
booting.

Merged-In: I1a9921e45f4c15b08cdf8f1caee64c4cb0761e1f
Change-Id: I1a9921e45f4c15b08cdf8f1caee64c4cb0761e1f
(cherry picked from commit 527757e16b)
2017-09-22 11:34:43 +09:00
Jiyong Park
527757e16b Don't resolve permitted.paths
Linker resolves the paths in /system/etc/ld.config.txt to canonicalize
the paths and to ensure they exist. However, as permitted paths for the
default namespace contain directories such as /vendor/app, /mnt/expand,
and etc., the resolving causes selinux denial on some processes which do
not have access to some of the permitted paths.

In order to silence the bogus selinux denial, resolution is skipped for
permitted paths.

Note that the resolution is not strictly required especially for
Treble-ized devices where permitted paths are already canonicalized (i.e
/vendor isn't a symlink to /system/vendor).

Bug: 65843095
Test: no selinux denial on /vendor/app, /vendor/framework, etc. while
booting.

Change-Id: I1a9921e45f4c15b08cdf8f1caee64c4cb0761e1f
2017-09-21 11:48:06 +09:00
Dan Willemsen
39b65475d9 Merge changes from topic "host_bionic" am: 6e6e1abb89 am: 9938cdeee8
am: 23ec4fd7e6

Change-Id: Ieb803f66f1f0f7bd29fea16df7e48f3c0195e3bf
2017-09-21 00:56:16 +00:00
Dan Willemsen
60b8ad3b86 linker: Only link to debuggerd on Android am: 4326d84d21 am: 7e78df9a62
am: b4a71637e2

Change-Id: I6fb98a3ea925f363431bf85f6afcbe5c8a41f030
2017-09-21 00:56:11 +00:00
Dan Willemsen
7ccc50d2e4 Use an embedded linker for host bionic
The linux kernel requires that the ELF interpreter (runtime linker)
that's referenced by PT_INTERP be either an absolute path, or a relative
path from the current working directory. We'd prefer a relative path
from the binary, similarly to how we handle looking up shared libraries,
but that's not supported.

Instead, extract the LOAD segments from the runtime linker ELF binary
and embed them into each host bionic binary, omitting the PT_INTERP
declaration. The kernel will treat it as a static binary, and we'll use
a special entry point (linker_wrapper) to fix up the arguments passed by
the kernel before jumping to the embedded linker. From the linker's
point of view, it looks like the kernel loaded the linker like normal.

Bug: 31559095
Test: Enable host bionic, build and run libdemangle_test
Change-Id: I1753401ef91eecbf0ae3376faca31eec1c53842b
2017-09-20 13:59:13 -07:00
Dan Willemsen
4326d84d21 linker: Only link to debuggerd on Android
Don't link to it when building with bionic for the host.

Also add libasync_safe, which is used by linker_globals.h even when
debuggerd isn't used.

Bug: 31559095
Test: mma
Test: Attempt to build host bionic
Change-Id: I374e2c2c288133875da82de780b27917ca524240
2017-09-20 13:16:13 -07:00
Elliott Hughes
f810219ce7 Merge "Always log errno when aborting." am: 0c9ea17e0c am: 002b30843b
am: 5d1cf56ce5

Change-Id: I1999c656307aea0a0a0372248d65a05657546e59
2017-09-18 21:44:57 +00:00
Elliott Hughes
7b0af7ad82 Always log errno when aborting.
(Where errno is relevant.)

Also consistently use -1 as the fd for anonymous mmaps. (It doesn't matter,
but it's more common, and potentially more intention-revealing.)

Bug: http://b/65608572
Test: ran tests
Change-Id: Ie9a207632d8242f42086ba3ca862519014c3c102
2017-09-15 16:18:49 -07:00
Elliott Hughes
4fde457022 Merge "Explicitly name DT_RPATH." am: e0e2798f11 am: e68ca24650
am: 1b64c2c4f0

Change-Id: I59b094b9a1c76d77f01f8a25102b3e0a6dc6f257
2017-08-30 20:03:08 +00:00
Elliott Hughes
6eae4cc57b Explicitly name DT_RPATH.
The specific case of finding a DT_RPATH entry is a pretty common harmless
warning. An alternative to this change would be to just add a case to the
switch for DT_RPATH to just silently ignore it, since it's never been
supported and is deprecated anyway.

Bug: N/A
Test: builds
Change-Id: I01986da8f1f8d411fc2ea32d492c53b9f4488c72
2017-08-30 09:02:33 -07:00
Dimitry Ivanov
cd13aabe45 Merge changes from topic "version-script-for-binaries" am: 0d5d0746e8 am: fcf482294a
am: f612be6f3b

Change-Id: Iab24817b2ba1c0c6bb8011e08e8b5188b17df7f5
2017-08-29 19:15:41 +00:00
dimitry
383e2a86bf Implement __gnu_Unwind_Find_exidx/__cxa_type_match am: 25bf29bbad am: 64403340fe
am: 4a980dc27e

Change-Id: I289496536f4d1f3e1f8a7ed064a2345f00036486
2017-08-29 19:15:36 +00:00
dimitry
7abea57ba5 Replace artificial symbol table with proper one
This change also replaces elf-hash with gnu-hash.

Test: make
Test: bionic-unit-tests --gtest_filter=dl*:Dl*
Change-Id: Ibc4026f7abc7e8002f69c33eebaf6a193f1d22eb
2017-08-29 18:18:28 +02:00
dimitry
25bf29bbad Implement __gnu_Unwind_Find_exidx/__cxa_type_match
Leaving these unimplemented causes linker to have 2 weak undefined
symbols which in turn results in 3 unwanted relocations for arm arch.
Implementing them removes unwanted relocations from resulted dynamic
object

Test: make && readelf --dyn-sym linker
Test: linker-unit-tests && bionic-unit-tests --gtest_filter=dl*:Dl*
Test: flash and boot angler
Change-Id: I10120271936c770659239898aaf852d31c5483c2
2017-08-29 18:18:27 +02:00
Dimitry Ivanov
5050503a5a Merge "Add support of architecture specific ld.configs" am: 506f3f11f1 am: bfec477beb
am: 595ac73b12

Change-Id: Id5e8ddaf0b6cbabf3973fa5601e0531e6d7739a0
2017-08-24 09:50:23 +00:00
dimitry
fe1b27cf84 Add support of architecture specific ld.configs
Prefer architecture specific ld.config.<arch>.txt if it exists;
fall back to default ld.config.txt files if arch-specific one is
not present.

Bug: http://b/64061157
Test: bionic-unit-tests && linker-unit-tests
Change-Id: I83a298a932f2e4af7acb2049a7641fb86908736c
2017-08-23 10:47:07 +02:00
Justin Yun
6cd3989598 Make default namespace handle the 'visible' flag.
'visible' flag was interpreted only the namespaces other than 'default'.
The flag has to be handled without exception once it is set.

Bug: 63411330
Test: Build and boot without errors.
Test: Add `namespace.default.visible = true` to [vendor] process in
      'ld.config.txt' and reboot the device.

Merged-In: Ia363dfadf0200317b875e26bb4b2fae849af1384
Change-Id: Ia363dfadf0200317b875e26bb4b2fae849af1384
(cherry picked from commit 90de9f081e)
2017-08-17 10:12:51 +09:00
Justin Yun
3396d1616f Merge "Make default namespace handle the 'visible' flag." am: f2c203f188 am: 01a2910758 am: 897b6dabef
am: 07cdf5e9a6

Change-Id: Ib56be3f3b4ad716dc023be3cfec531afe29246bb
2017-08-16 17:58:18 +00:00
Justin Yun
01a2910758 Merge "Make default namespace handle the 'visible' flag."
am: f2c203f188

Change-Id: Ia95b8d4179265d49ab51cd0dee57f52919e4f549
2017-08-16 17:18:42 +00:00
Justin Yun
90de9f081e Make default namespace handle the 'visible' flag.
'visible' flag was interpreted only the namespaces other than 'default'.
The flag has to be handled without exception once it is set.

Bug: 63411330
Test: Build and boot without errors.
Test: Add `namespace.default.visible = true` to [vendor] process in
      'ld.config.txt' and reboot the device.

Change-Id: Ia363dfadf0200317b875e26bb4b2fae849af1384
2017-08-16 14:38:07 +09:00
Jiyong Park
150452f0f6 Revert "linker: disable ld.config.txt in ASAN mode"
am: f1e5dbd1cc

Change-Id: I6db4ffcb76bc928409cfb560d3bc68db236234f1
2017-08-04 20:27:15 +00:00
Jiyong Park
f1e5dbd1cc Revert "linker: disable ld.config.txt in ASAN mode"
This reverts commit 81b175747d.

Reason: 02586a2a34 fixed the bug that
prevented us from using LD_PRELOAD with multiple namespaces.

Bug: 38114603
Test: 1. ./external/compiler-rt/lib/asan/scripts/asan_device_setup --lib
prebuilts/clang/host/linux-x86/clang-stable/lib64/clang/5.0/lib/linux
2. enable talkback shortcut
3. in the home screen, hold vol-up/down together
4. device does not reboots and talkback shortcut is toggled
Test: bionic-unit-tests and linker-unit-tests successful

Merged-In: I25a05927ffbb28b9fa72303652893f43918ccec6
Change-Id: Ib7568d65c2524fd2307d01eb3d50fc308feced9c
(cherry picked from commit 6ab40bbf53)
2017-08-04 14:15:20 +09:00
Jiyong Park
34a4acd801 linker: the global group is added to all built-in namespaces
With ld.config.txt, we now have multiple built-in namespaces other than
the default namespace. Libs (and their dependents) listed in LD_PRELOAD
must be visible to those additional namespaces as well.

This also adds a debugging only feature: path to the linker config file
can be customized via LD_CONFIG_FILE environment variable. This works
only for debuggable builds.

Bug: 38114603
Bug: 62815515
Test: 1. ./external/compiler-rt/lib/asan/scripts/asan_device_setup --lib
prebuilts/clang/host/linux-x86/clang-stable/lib64/clang/5.0/lib/linux
2. enable talkback shortcut
3. in the home screen, hold vol-up/down together
4. device does not reboots and talkback shortcut is toggled
Test: bionic-unit-tests and linker-unit-tests successful

Merged-In: I9a03591053f4a9caea82f0dcb23e7a3d324bb9bd
Change-Id: I9a03591053f4a9caea82f0dcb23e7a3d324bb9bd
(cherry picked from commit 02586a2a34)
2017-08-04 14:14:09 +09:00
Jiyong Park
d741e5ef43 Merge "linker: the global group is added to all built-in namespaces" am: c9fb66209d am: ec8c79aaa1 am: 0a51b36ad6
am: e247953f1b

Change-Id: I26521ec9cc11b9ee9ce55a61ac2bfe3152a0dd7f
2017-08-03 02:47:38 +00:00
Jiyong Park
ec8c79aaa1 Merge "linker: the global group is added to all built-in namespaces"
am: c9fb66209d

Change-Id: I9c45ba3b6878a9a098634eae4b9cb97360161495
2017-08-03 02:32:37 +00:00
Evgenii Stepanov
91cf283bd4 Merge "Fix asan path translation loading a library twice." into oc-mr1-dev
am: cc28f6ea09

Change-Id: Ib6083f142d7cd07d19234de7e062585c3e2ebcd9
2017-08-02 18:24:19 +00:00
Evgenii Stepanov
cc28f6ea09 Merge "Fix asan path translation loading a library twice." into oc-mr1-dev 2017-08-02 18:11:47 +00:00
Jiyong Park
02586a2a34 linker: the global group is added to all built-in namespaces
With ld.config.txt, we now have multiple built-in namespaces other than
the default namespace. Libs (and their dependents) listed in LD_PRELOAD
must be visible to those additional namespaces as well.

This also adds a debugging only feature: path to the linker config file
can be customized via LD_CONFIG_FILE environment variable. This works
only for debuggable builds.

Bug: 38114603
Bug: 62815515
Test: 1. ./external/compiler-rt/lib/asan/scripts/asan_device_setup --lib
prebuilts/clang/host/linux-x86/clang-stable/lib64/clang/5.0/lib/linux
2. enable talkback shortcut
3. in the home screen, hold vol-up/down together
4. device does not reboots and talkback shortcut is toggled
Test: bionic-unit-tests and linker-unit-tests successful

Change-Id: I9a03591053f4a9caea82f0dcb23e7a3d324bb9bd
2017-08-03 01:02:07 +09:00
Elliott Hughes
f54872e5bf Merge "Clean up linker fatals slightly."
am: 1bac61f09f

Change-Id: Icf7d5b5b0def92ddb60c5c8a5c15e8867e76b2ad
2017-08-01 20:57:43 +00:00
Elliott Hughes
ad2d0380a6 Clean up linker fatals slightly.
Use __linker_cannot_link more consistently, and fix a comment.

Bug: http://b/22798163
Test: ran tests
Change-Id: Id6d868f459997eaa67b8cbbf85b0b0f2749bf43d
2017-08-01 10:06:10 -07:00
Evgenii Stepanov
9e77a6468b Fix asan path translation loading a library twice.
An ASan binary may load a non-ASan library from /system if /data is not mounted yet.
A dlopen() call for the same library later, when /data/ is available, will translate the path and attempt to load
an ASan copy of the library from /data/asan/system. This way we may end up loading both ASan and non-ASan copies of
the library in the same process, which is a very bad thing.

This change adds a check for a loaded library with the non-translated real path before applying path translation.

Bug: 63622872
Test: hide/rename a library in /data/asan; dlopen; restore the library; dlopen; check that the library from /data/asan is NOT loaded.
Change-Id: I17060837f08dc3c665cab803dd89979d88f0a019
2017-07-27 15:02:08 -07:00
Dimitry Ivanov
66479d88d1 Merge "The ifdef check for AT_SYSINFO_EHDR is no longer needed"
am: 9d2d21d636

Change-Id: I00cfa9f85bafb069f3ce029baea71b496e4c9b6e
2017-07-06 15:20:05 +00:00
dimitry
f9abbf6983 The ifdef check for AT_SYSINFO_EHDR is no longer needed
AT_SYSINFO_EHDR is present in all architectures

Test: make
Change-Id: I0f4c115bb48e47ea156e7fca936960f5f10c618e
2017-07-06 12:24:33 +02:00
George Burgess IV
f437c90407 Merge "bionic: fix assorted static analyzer warnings"
am: e202036e03

Change-Id: I88dce02b7f483fb7bc2819406d800d5056c2fc48
2017-06-29 16:34:13 +00:00
George Burgess IV
705910094d bionic: fix assorted static analyzer warnings
Warnings:

bionic/libc/bionic/fts.c:722:5: warning: Null passed to a callee that
requires a non-null 1st parameter

bionic/libc/bionic/sched_cpualloc.c:34:25: warning: Result of 'malloc'
is converted to a pointer of type 'cpu_set_t', which is incompatible
with sizeof operand type 'unsigned long'

bionic/linker/linker_main.cpp:315:7: warning: Access to field 'e_type'
results in a dereference of a null pointer (loaded from variable
'elf_hdr')

bionic/linker/linker_main.cpp:493:66: warning: Access to field 'e_phoff'
results in a dereference of a null pointer (loaded from variable
'elf_hdr')

bionic/linker/linker_main.cpp:90:14: warning: Access to field 'next'
results in a dereference of a null pointer (loaded from variable 'prev')

Bug: None
Test: mma; analyzer warnings are gone. CtsBionicTestCases pass.
Change-Id: I699a60c2c6f64c50b9ea06848a680c98a8abb44a
2017-06-28 15:03:15 -07:00
Jaesung Chung
bfda5770c7 Merge "linker_config_test: allow search paths of systems without a vendor partition" am: b1b7eda11e
am: 12b44daa11

Change-Id: I7ec1a1f6a541cfd951093a63da175f521babcb4d
2017-06-19 00:22:55 +00:00
Jaesung Chung
9d97008a68 linker_config_test: allow search paths of systems without a vendor partition
linker_config#smoke and linker_config.asan_smoke are trying to find
paths under the /vendor directory. If there is no vendor partition,
the real path of them is started with /system/vendor.

This CL allows those paths in the tests by getting the resolved paths
for systems without a vendor partition.

Bug: http://b/62562515
Test: linker_config_test passes without a vendor partition.
Change-Id: Id6d16ef623efd81ab9083c3e819da2ad22a28bf8
2017-06-19 00:12:45 +00:00
Dimitry Ivanov
c3c83d9f4b Merge "Make dl_iterate_phdr return correct name for first entry" am: 533cf06474 am: 1e55a0420e
am: c27ac0df9b

Change-Id: Ida956645eaf002113587960e931acc570e44c7a3
2017-06-01 07:24:29 +00:00
Dimitry Ivanov
c27ac0df9b Merge "Make dl_iterate_phdr return correct name for first entry" am: 533cf06474
am: 1e55a0420e

Change-Id: I80c1b5f9169c5d7ae7e139d52643fce981c2f7a3
2017-06-01 07:22:28 +00:00
Dimitry Ivanov
cd510cbed9 Make dl_iterate_phdr return correct name for first entry
Test: bionic-unit-tests --gtest_filter=link*
Change-Id: Ib3f1e0fbc76fed9a5f27ffdd9bbf847a3e8d3665
2017-05-31 23:45:15 +00:00
Jiyong Park
08d3f97e7f Merge "linker: disable ld.config.txt in ASAN mode" into oc-dev
am: 41e0ceb5c9

Change-Id: I6e9faedbb26d94aab7064c2c8620362c776143be
2017-05-27 01:50:49 +00:00
Jiyong Park
81b175747d linker: disable ld.config.txt in ASAN mode
Currently, multiple namespaces does not support ASAN mode where some
symbols should be intercepted via LD_PRELOAD; LD_PRELOADed libs are not
preloaded into the linked namespaces other than the default namespace.
Until we fix the problem, we temporarily disable ld.config.txt in ASAN
mode.

Bug: 38114603
Test: 1. ./external/compiler-rt/lib/asan/scripts/asan_device_setup --lib
prebuilts/clang/host/linux-x86/clang-stable/lib64/clang/3.8/lib/linux
2. enable talkback shortcut
3. in the home screen, hold vol-up/down together
4. device does not reboots and talkback shortcut is toggled
Test: bionic-unit-tests and linker-unit-tests successful

Change-Id: I091874dd36b36f56dff2ad899d76ff86931909ed
2017-05-27 08:40:12 +09:00
Elliott Hughes
f14633f708 Merge "Remove obsolete __stack_chk_fail_local." am: 6c01208e90 am: c80d8405da
am: a6c583f3bb

Change-Id: I2da57be8f2028ccf540d8fb8c637e89817a89242
2017-05-24 15:15:48 +00:00
Elliott Hughes
a6c583f3bb Merge "Remove obsolete __stack_chk_fail_local." am: 6c01208e90
am: c80d8405da

Change-Id: I7f98e7bf679fab61e7d48893a059c2ce75691f13
2017-05-24 15:13:19 +00:00
Elliott Hughes
4af220cfef Remove obsolete __stack_chk_fail_local.
Clang doesn't use this.

Bug: N/A
Test: x86 emulator builds and boots
Change-Id: I2865c0d568a644f61f34bdea539daff5224896bc
2017-05-23 16:43:30 -07:00
Dimitry Ivanov
fa6ebae9d9 Merge "Revert "linker: remove link from external library on unload"" into oc-dev
am: f80b2bae2f

Change-Id: Id7e4908181fb869de30676fc5c5e6abe3d24d383
2017-05-18 16:33:57 +00:00
Dimitry Ivanov
ec5ddc0a23 Revert "linker: remove link from external library on unload"
This reverts commit b37d10cc80.

Was merged from nyc-dev to master and oc-dev by mistake.

Bug: http://b/38384727
Test: bionic-unit-tests
Change-Id: I7cd352baceb3180342abb6ffec1dfa96a217961f
2017-05-18 01:02:54 +00:00
Dimitry Ivanov
5ef13ba0ca linker: remove link from external library on unload am: b37d10cc80 am: 3a644889b2 am: cb40eec87c am: 0399816edb
am: 85efd3b591

Change-Id: I115a7adb07feedf81064c0e740cfc617263eb689
2017-05-16 22:21:23 +00:00
Dimitry Ivanov
efbc04475e linker: remove link from external library on unload am: b37d10cc80 am: 3a644889b2 am: cb40eec87c am: 0399816edb
am: 85efd3b591

Change-Id: I0b41cdffdccab8b79a4d9c6327dc0f6f521c1acf
2017-05-16 22:21:22 +00:00
Dimitry Ivanov
cb40eec87c linker: remove link from external library on unload am: b37d10cc80
am: 3a644889b2

Change-Id: I7c022194739391c24a736a3a5deb906433fc5649
2017-05-16 22:12:22 +00:00
Dimitry Ivanov
3a644889b2 linker: remove link from external library on unload
am: b37d10cc80

Change-Id: I48901a8fbf4bc25fff1462e6086fc23b76ca2ba3
2017-05-16 22:08:52 +00:00
Dimitry Ivanov
b37d10cc80 linker: remove link from external library on unload
When unloading library make sure the soinfo_link to parent
from linked external libraries is removed as well.

Bug: 36104177
Bug: http://b/37433850
Test: manual
Change-Id: I601b54144acecac54744805b38313c46045c54cb
(cherry picked from commit ee398f820700de30941fd82e38bd917e1513cbc6)
2017-05-15 21:23:32 -07:00
Jiyong Park
37b91af0f3 greylist is no longer enabled by default.
greylist is needed only for application namepaces. Since we started using
linker-namesapces for vendors and other platform apps linker should not
enable greylist workaround by default.

Bug: http://b/37731053
Bug: https://issuetracker.google.com/38146125
Test: sailfish builds and boots
Test: bionic-unit-tests and linker-unit-tests pass
Change-Id: Iee83db6fb1ae754f5ade18491321d9bca3b5ead4
(cherry picked from commit e8ffe56a9c)
2017-05-09 17:59:00 -07:00
Jiyong Park
e8ffe56a9c greylist is no longer enabled by default.
greylist is needed only for application namepaces. Since we started using
linker-namesapces for vendors and other platform apps linker should not
enable greylist workaround by default.

Bug: http://b/37731053
Bug: https://issuetracker.google.com/38146125
Test: sailfish builds and boots
Test: bionic-unit-tests and linker-unit-tests pass
Change-Id: Iee83db6fb1ae754f5ade18491321d9bca3b5ead4
2017-05-08 23:22:16 -07:00
Christopher Ferris
5725481f1e Merge "Move libc_log code into libasync_safe." 2017-05-03 17:43:35 +00:00
Christopher Ferris
7a3681e5b6 Move libc_log code into libasync_safe.
This library is used by a number of different libraries in the system.
Make it easy for platform libraries to use this library and create
an actual exported include file.

Change the names of the functions to reflect the new name of the library.

Run clang_format on the async_safe_log.cpp file since the formatting is
all over the place.

Bug: 31919199

Test: Compiled for angler/bullhead, and booted.
Test: Ran bionic unit tests.
Test: Ran the malloc debug tests.
Change-Id: I8071bf690c17b0ea3bc8dc5749cdd5b6ad58478a
2017-05-03 08:50:43 -07:00
Dimitry Ivanov
f1cb669a9c Shared namespaces inherit parent ns properties
Make shared namespace inherit namespace links and
search/permitted paths from the parent namespace.

Bug: http://b/37854032
Test: bionic_unit_tests --gtest_filter=dl*:Dl*
Change-Id: I174661d4a1dd0cbe4a378179073719aa955f3592
(cherry picked from commit ec43dd6c36)
2017-05-02 16:42:28 -07:00