Skip to content

Commit

Permalink
Hide unwinder symbols on all architectures.
Browse files Browse the repository at this point in the history
Test: readelf
Bug: android/ndk#1166
Change-Id: I28b64492e5a66a66608874133ddd955ea2f53d7f
  • Loading branch information
DanAlbert committed Mar 6, 2020
1 parent 0749ef5 commit 3977774
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
7 changes: 7 additions & 0 deletions build/cmake/android.toolchain.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,13 @@ if(ANDROID_LD STREQUAL lld)
endif()

# Don't re-export libgcc symbols in every binary.
list(APPEND ANDROID_LINKER_FLAGS -Wl,--exclude-libs,libgcc.a)
# arm32 currently uses a linker script in place of libgcc to ensure that
# libunwind is linked in the correct order. --exclude-libs does not propagate to
# the contents of the linker script and can't be specified within the linker
# script. Hide both regardless of architecture to future-proof us in case we
# move other architectures to a linker script (which we may want to do so we
# automatically link libclangrt on other architectures).
list(APPEND ANDROID_LINKER_FLAGS -Wl,--exclude-libs,libgcc_real.a)
list(APPEND ANDROID_LINKER_FLAGS -Wl,--exclude-libs,libatomic.a)

Expand Down
8 changes: 7 additions & 1 deletion build/core/default-build-commands.mk
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,13 @@ endef

cmd-strip = $(PRIVATE_STRIP) $(PRIVATE_STRIP_MODE) $(call host-path,$1)

TARGET_LIBGCC = -lgcc -Wl,--exclude-libs,libgcc_real.a
# arm32 currently uses a linker script in place of libgcc to ensure that
# libunwind is linked in the correct order. --exclude-libs does not propagate to
# the contents of the linker script and can't be specified within the linker
# script. Hide both regardless of architecture to future-proof us in case we
# move other architectures to a linker script (which we may want to do so we
# automatically link libclangrt on other architectures).
TARGET_LIBGCC = -lgcc -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libgcc_real.a
TARGET_LIBATOMIC = -latomic -Wl,--exclude-libs,libatomic.a
TARGET_LDLIBS := -lc -lm

Expand Down
18 changes: 11 additions & 7 deletions docs/BuildSystemMaintainers.md
Original file line number Diff line number Diff line change
Expand Up @@ -410,10 +410,11 @@ preserved. By default, only symbols in used sections will be included in the
linked binary.

If this behavior is not desired for your build system, ensure that these flags
are at least used for `libgcc_real.a` (`libgcc.a` is a linker script, and
`--exclude-libs` does not have any effect on the contents of linker scripts) and
`libunwind.a` (libunwind is only used for ARM32). This is necessary to avoid
unwinding bugs on ARM32. See [Unwinding] for more information.
are at least used for `libgcc.a` (`libgcc_real.a` on Arm32, where `libgcc.a` is
a linker script, as `--exclude-libs` does not have any effect on the contents of
linker scripts) and `libunwind.a` (libunwind is only used for ARM32). This is
necessary to avoid unwinding bugs on Arm32. See [Unwinding] for more
information.

[visibility]: https://gcc.gnu.org/wiki/Visibility

Expand Down Expand Up @@ -513,9 +514,12 @@ these symbols from a shared library. If this library was built with the wrong
unwinder, it is possible for one unwinder to call into the other. As they are
not compatible, this will likely result in either a crash or a failed unwind. To
avoid this problem, libraries should always be built with
`-Wl,--exclude-libs,libgcc_real.a` and `-Wl,--exclude-libs,libunwind.a` (the
latter is only necessary for 32-bit ARM) to ensure that unwind symbols are not
re-exported from shared libraries.
`-Wl,--exclude-libs,libgcc.a`, `-Wl,--exclude-libs,libgcc_real.a` and
`-Wl,--exclude-libs,libunwind.a` (the latter is only necessary for 32-bit Arm)
to ensure that unwind symbols are not re-exported from shared libraries. Note
that `libgcc_real.a` is needed because on some architectures (currently only
32-bit Arm) `libgcc.a` is a linker script and `--exclude-libs` does not extend
to the contents of linker scripts.

Even with the above precautions, it is still possible for an improperly built
external dependency to provide an incorrect unwind implementation as described
Expand Down
3 changes: 3 additions & 0 deletions docs/changelogs/Changelog-r21.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ For Android Studio issues, follow the docs on the [Android Studio site].
Pure C/C++ executables were fine, but this effectively broke all app
debugging. The error from gdb that confirms you were affected by this was
`gdb-8.3/gdb/psymtab.c:316: internal-error: sect_index_text not initialized`.
* [Issue 1166]: Rehid unwinder symbols all architectures.

[Issue 1166]: https://github.com/android/ndk/issues/1166

## Changes

Expand Down

0 comments on commit 3977774

Please sign in to comment.