Commit graph

45 commits

Author SHA1 Message Date
Kalesh Singh
862a23dfdf bionic: Add crt_pad_segment
crt_pad_segment provides a note of type NT_ANDROID_TYPE_PAD_SEGMENT.
It's intended when present is for the loader to pad segment gaps to
reduce kernel slab memory usage (due to additional vm_area_struct's
for gaps). crt_pad_segment.o retains control to the static linker so
that app developers can opt out (build with different flags) it there
are undesireable side effects.

Section Headers:
  [Nr] Name              Type            Address          Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            0000000000000000 000000 000000 00      0   0  0
  [ 1] .strtab           STRTAB          0000000000000000 0000f8 000066 00      0   0  1
  [ 2] .text             PROGBITS        0000000000000000 000040 000000 00  AX  0   0  4
  [ 3] .note.GNU-stack   PROGBITS        0000000000000000 000040 000000 00      0   0  1
  [ 4] .note.android.pad_segment NOTE            0000000000000000 000040 00001c 00   A  0   0  4
  [ 5] .rela.note.android.pad_segment RELA            0000000000000000 0000e0 000018 18   I  7   4  8
  [ 6] .debug_line       PROGBITS        0000000000000000 00005c 000052 00      0   0  1
  [ 7] .symtab           SYMTAB          0000000000000000 0000b0 000030 18      1   1  8
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings), I (info),
  L (link order), O (extra OS processing required), G (group), T (TLS),
  C (compressed), x (unknown), o (OS specific), E (exclude),
  D (mbind), l (large), p (processor specific)

Bug: 316403210
Test: m -j50 ndk
Test: find out/soong/ndk -name 'crt_pad_segment.o'
Test: readelf -SW crt_pag_segment.o
Change-Id: I94af5e85fd602e7ba5fd56788ae39277368229d8
Signed-off-by: Kalesh Singh <kaleshsingh@google.com>
2024-01-12 10:22:08 -08:00
Yabin Cui
744cfd3720 Let executables not rely on sentinels in preinit_array/init_array/fini_array
Currently, we use sentinels (starting with -1 and ending with 0) in
preinit_array/init_array/fini_array in executables. But after using LTO,
the sentinels can be reordered by LLD and no longer work. So make below
changes to not rely on them:
  1. In crtbegin.c, use symbols (like __init_array_start) inserted by the
     linker.
  2. Add array_count fields in structors_array_t.
  3. In static libc, use array_count fields to decide array lengths.
  4. To make new dynamic executables work with old libc.so, create a fake
     fini_array with sentinels, and pass it to __libc_init. The fake
     fini_array contains a function to call functions in real fini_array.
  5. To make old dynamic executables work with new libc.so, libc.so
     still uses sentinels to decide the length of fini_array.

Bug: 295944813
Bug: https://github.com/android/ndk/issues/1461
Test: run bionic-unit-tests-static
Test: test static executables manually
Test: boot cf_gwear_x86-trunk_staging-userdebug
Change-Id: I1ce31f07bcfe0e99b4237984898a8fc9e98ff426
2023-09-01 10:17:05 -07:00
Elliott Hughes
5ee6292872 Remove #if cruft.
There's negative value to explicitly calling out Linux and ELF here,
when Android -- and especially this project -- is always both anyway.

No functional change.

Test: treehugger
Change-Id: Ic7514d74a5b5f6f06e64c46486c12773995076cd
2023-03-01 20:26:41 +00:00
Elliott Hughes
7a19624698 Add riscv64 crtbegin assembler.
Signed-off-by: Mao Han <han_mao@linux.alibaba.com>
Signed-off-by: Xia Lifang <lifang_xia@linux.alibaba.com>
Signed-off-by: Chen Guoyin <chenguoyin.cgy@linux.alibaba.com>
Signed-off-by: Wang Chen <wangchen20@iscas.ac.cn>
Signed-off-by: Lu Xufan <luxufan@iscas.ac.cn>
Test: treehugger
Change-Id: I712a9a93a2df208d763bc46175e00ff763b68146
2022-10-17 21:06:19 +00:00
Mitch Phillips
93400371f7 [NFCI] Change Android's NT_TYPE to NT_ANDROID_TYPE.
Normally, platform-specific note types in the toolchain are prefixed
with the platform name. Because we're exposing the NT_TYPE_MEMTAG and
synthesizing the note in the toolchain in an upcoming patch
(https://reviews.llvm.org/D118948), it's been requested that we change
the name to include the platform prefix.

While NT_TYPE_IDENT and NT_TYPE_KUSER aren't known about or synthesized
by the toolchain, update those references as well for consistency.

Bug: N/A
Test: Build Android
Change-Id: I7742e4917ae275d59d7984991664ea48028053a1
2022-02-07 13:49:20 -08:00
Elliott Hughes
ef76860f22 Turns out we don't need to say calll any more.
I don't know when LLVM's x86 assembler started making the same
assumptions as GAS used to, but I'm happy to get rid of "calll".

Test: treehugger
Change-Id: I0a924993aebf7d701a846805fea9a015e8feb58a
2021-05-19 09:14:41 -07:00
Chris Wailes
559f27828c Clear the stack frame pointer in _start and __bionic_clone
This CL adds an instruction to the _start label that clears the frame
pointer.  This allows stack walking code to determine when it has
reached the end of the stack.

The __bionic_clone function is similarly modified, for architectures
that weren't already doing both.

Test: bionic-unit-tests
Test: CtsBionicTestCases
Change-Id: Iea3949f52c44f7931f9fff2d60d4d9e5c742c120
2021-05-07 15:00:55 -07:00
Elliott Hughes
5501003be7 Remove __stack_chk_fail_local() completely.
As far as I can tell, clang never implemented this GCC workaround for
32-bit x86's terrible PIC code. Since the whole point of
__stack_chk_fail_local() requires that it's in the same executable or
library as its callers, any prebuilt with a dependency on this (because
it was built by GCC) already has its own copy anyway. And clang isn't
creating any new ones, so I think this has been dead for several years
now.

Test: treehugger
Change-Id: I96997bbf912bbff506db44c285d9941fef9f86ce
2021-03-17 09:02:34 -07:00
Elliott Hughes
92da28625f Clean up __stack_chk_fail_local slightly.
Motivated by the fact that bazel doesn't like #include "../", but this
feels like it could use a deeper clean.

In fact, even after this change, I think we should remove this entirely,
since as far as I can tell Clang never implemented this GCC workaround
for 32-bit x86's awful PIC code.

Test: treehugger
Change-Id: I72715ee46f873f42d5707712aebe246ef78fcde1
2021-03-16 16:41:53 -07:00
Elliott Hughes
3e1d5563b6 PAC/BTI: no need to keep using hint.
The toolchain is new enough that should be able to use the actual
instructions now...

Test: treehugger
Change-Id: I30aafcdc5386268344c40dc6cc9a22caf591915a
2021-01-25 08:49:01 -08:00
Evgenii Stepanov
dec48bdd9e (NFC) Symbolic names for Android ELF note types.
Bug: b/135772972
Test: none

Change-Id: Ia972200337d4e9a2adeba196edd06dd98348b6cd
2021-01-05 15:25:39 -08:00
Matthew Maurer
de30635eb6 Put __*_ARRAY__ symbols before prioritized init/fini funcs
A constructor or destructor function with an integral priority is
placed in an .init_array or .fini_array section with the priority
suffixed to the section name:
 - __attribute__((constructor)) ==> .init_array
 - __attribute__((constructor(42))) ==> .init_array.42

The suffixed init/fini sections appear before the unsuffixed sections,
so the prioritized functions appeared before the __{INIT,FINI}_ARRAY__
symbols and were dropped when the symbols were used.

The (static) linker doesn't recognize priority suffixes on
.preinit_array.

This bug affected .init_array and .fini_array for static executables.
For dynamic executables, only .fini_array was affected, because
.init_array is handled by the dynamic loader instead, which uses
DT_INIT_ARRAY[SZ]. For DSOs, neither is affected, because the two
sections are only handled by the dynamic loader.

This patch also fixes a minor inconsistency where dynamic init/preinit
were passed argc/argv/envp, but static were not.

Bug: http://b/170983066
Test: bionic-unit-tests
Change-Id: I0fffa776e5d9bdb6f8af06b4c1af148236742fef
2020-11-02 17:28:41 -08:00
Ryan Prichard
9494caa7e8 Merge "Enable arm64 .eh_frame terminator, align it to 4" 2020-10-01 01:08:38 +00:00
Ryan Prichard
bcef897b6d Enable arm64 .eh_frame terminator, align it to 4
Every architecture that uses .eh_frame needs a terminator at the end to
ensure that an unwinder library doesn't read past the end of the
section. This can happen when the unwinder looks up a PC that doesn't
have a matching FDE. Both LLVM libunwind and libgcc fall back to an
.eh_frame scan if the .eh_frame_hdr search table scan fails.

By convention, this terminator section is aligned to 4, even on 64-bit
targets. e.g. Both libgcc and compiler-rt have a crtend with 4-byte
.eh_frame alignment on arm64 and x86_64.

In practice, lld strips off the terminator from input object files and
adds one unconditionally to the output, but bfd/gold still require a
crtend with a terminator.

References:
 - https://reviews.llvm.org/D86256
 - https://reviews.llvm.org/D87750
 - https://sourceware.org/bugzilla/show_bug.cgi?id=2655

Test: examine crtend.o and crtend_so.o with `readelf -SW` and \
  `objdump -Wf`
Change-Id: Ib8478ee446ad806898aa7147a6208d384c7516d5
2020-09-24 22:01:32 -07:00
Tamas Petz
18621fb30c Update crtbegin.c and crt*.S to support Armv8.5-A BTI
These files are linked to all ELF files therefore they must support BTI.

Test: Tested on FVP with BTI enabled using a patched clang.

Co-authored-by: Gabor Kertesz <gabor.kertesz@arm.com>
Co-authored-by: Daniel Kiss   <daniel.kiss@arm.com>
Co-authored-by: Tamas Petz    <tamas.petz@arm.com>

Change-Id: If5df0722e649bcdb8c4afb0531831dff42103c9c
2020-09-04 11:28:59 +02:00
Elliott Hughes
95c6cd753f Stop using the __ANDROID_API_x__ constants.
Historically we've made a few mistakes where they haven't matched the
right number. And most non-Googlers are much more familiar with the
numbers, so it seems to make sense to rely more on them. Especially in
header files, which we actually expect real people to have to read from
time to time.

Test: treehugger
Change-Id: I0d4a97454ee108de1d32f21df285315c5488d886
2019-12-20 13:26:14 -08:00
Ryan Prichard
85be9725c7 crtbegin: replace TPREL with R_*_NONE relocs
Bug: https://bugs.llvm.org/show_bug.cgi?id=41527
Test: bionic unit tests
Change-Id: Ia66c969b209c0f72d1e37573949f3fb59817ca71
2019-09-04 20:31:06 +00:00
Vic Yang
f1fde773e1 Move __dso_handle to RELRO
Most DSOs have a RELRO segment, but some of them have nothing except
__dso_handle in their .data section.  This means __dso_handle is taking
up a page as well as a vm_area_struct in kernel slabs for those DSOs.

Let's move it to the RELRO segment to save memory.

On a 32-bit device I tested on, I saw ~400 KB less slab usage on
vm_area_struct.  To control for difference in number of cached
processes, I also measured with Android stopped, and I saw ~100 KB less
slab usage.  Summing up all dirty pages attributed to shared libraries,
I saw 5.6 MB less dirty pages.

Test: Build and boot.
Change-Id: Ib88e49f1c72352e610affc19623895d954110d4e
2019-08-26 20:35:00 -07:00
Ryan Prichard
cc9b100e97 Overalign the TLS segment using crtbegin
Android's current lld build has a hack that overaligns TLS segments, but
it broke glibc when it produced TLS segments where (p_vaddr % p_align) was
non-zero. Move the hack into Bionic's crtbegin instead. It will emit a
0-sized, 64-byte alignment TLS segment into executables that don't use
TLS, but that should be harmless.

This variant of the hack is compatible with the gold and lld linkers. The
ld.bfd linker will optimize the .tdata output section out if its size is
zero, preventing the overalignment in an executable that only has .tbss
sections. This problem could be fixed by adding a ". = .;" statement
inside .tdata in ld.bfd's linker script.

See discussion on https://reviews.llvm.org/D61824.

Bug: https://bugs.llvm.org/show_bug.cgi?id=41527
Test: bionic unit tests, boot a device
Change-Id: I34df8b5594b6518d4590e4861e3d0b74d6fa754e
2019-05-23 14:21:55 -07:00
Dan Willemsen
d4e51a155a Change crtbrand for host bionic
Fixes a build error when PLATFORM_SDK_VERSION is not set.

Bug: 31559095
Test: attempt to build host bionic
Change-Id: I9cbdcea206ef1239c330a5adafbfa5cc797fef5e
2018-10-22 17:15:22 -07:00
Ryan Prichard
bcea0e2afd Call __emutls_unregister_key on dlclose
We want to delay the emutls pthread key deletion to keep
__thread/thread_local variables working as long as possible.

Currently, emutls has its own __attribute__((destructor)) function that
deletes its pthread key. That function runs even on process exit, and it
can run before other destructor functions and before C++ static object
destructors.

Move the destructor function to crtbegin_so.c, where an
__attribute__((destructor)) function is only called on dlclose. Use a
priority 0 destructor, which runs after every destructor with a default or
greater priority value.

__emutls_unregister_key will still run before destructor functions of
DT_NEEDED solibs. It also still leaks memory (both the emutls arrays and
each emutls object).

Bug: b/80453944
Test: manual
Change-Id: I6789bcf168415ab8badf2f64687c6a0136c5c917
2018-06-19 13:22:36 -07:00
Treehugger Robot
5bf2c20f43 Merge "crtbegin: Correctly align ESP to 16 for __i386__" 2018-02-09 06:56:31 +00:00
Ryan Prichard
a0d177d010 crtbegin: Correctly align ESP to 16 for __i386__
The (lowest) address of the argument area (aka ESP immediately prior to
the call instruction) must be aligned to 0 mod 16. Here, it is aligned to
12 mod 16.

From the SysV ABI doc (2.2.2 The Stack Frame)

"""The end of the input argument area shall be aligned on a 16 (32, if
__m256 is passed on stack) byte boundary. In other words, the value
(%esp + 4) is always a multiple of 16 (32) when control is transferred to
the function entry point."""

Test: extract code into a separate C file and verify stack alignment in a
  "start_main" function
Test: use the upcoming NDK r17-beta1 (with new Bionic crtbegin*.o files)
  with an M-23 x86 system image, check alignment in main (compiled with
  Clang not GCC, compiled w/o -mstackrealign)
Bug: b/73140672

Change-Id: Ia8d93fe5668d0a514a9fd22c40bf8362805111e6
2018-02-08 20:55:57 -08:00
Dan Albert
3e4d4d4cf2 Add _FORCE_CRT_ATFORK config for crtbegin.
libc.a is always the latest library regardless of target API level.

Test: treehugger
Bug: None
Change-Id: I844dc85a9283693b01d0fbdc26854bde82c750dc
2018-01-24 22:36:31 -08:00
Dan Albert
586b6761d0 Partial revert of "Remove obsolete __stack_chk_fail_local."
These sources are going to be used for the NDK as well, and the NDK
still uses GCC.

This partially reverts commit
4af220cfef. That commit also removed
this symbol from the linker's crtbegin (it has its own). That is
still only built with Clang, so we don't need to revert that part.

Test: treehugger
Bug: None
Change-Id: Iba231baf298e228135bdf48dfed87f9089975eb1
2018-01-24 16:37:48 -08:00
Dan Albert
2e2c72d61f Pre-M and GCC compatibility for crtbegin.
We're going to start using the bionic sources for the NDK CRT
objects, so we need to avoid using symbols that weren't around in
early versions of Android. The NDK is currently building the CRT
objects with GCC as well (there were some segfaults that have yet to
be diagnosed), so move `__used` to the GCC compatible location.

Test: treehugger
Bug: None
Change-Id: I1f5c23eafadc2e3bc0b84bc3305f79a04d35c7d8
2018-01-24 16:37:48 -08:00
Elliott Hughes
12533271fb Give ELF notes 4-byte alignment.
Use .balign because what the argument means doesn't vary between
architectures.

Bug: http://b/70166421
Test: builds
Change-Id: I1d54f2f6942dd2893f3fda30bac08d07ea9cd346
2017-12-04 13:21:01 -08:00
Elliott Hughes
3413a75460 Remove an obsolete aarch64 hack.
e210488e0d in 2013 added this, which seems
to have been a workaround for the linker at the time that was never removed.

Bug: N/A
Test: ran tests
Change-Id: I171c3f4b7c34638ab75b8e49616b3a9c24ab3fcb
2017-10-25 10:57:02 -07:00
Elliott Hughes
b348d5cda8 Build crtbegin with clang too.
If _start_main is static, clang optimizes it out and then generates an
undefined reference from the inline assembler, so let's explicitly tell
the compiler it's used.

Bug: N/A
Test: ran tests
Change-Id: Iba3d9c63abae9a0dc32f236b01783515f0190956
2017-07-25 11:54:33 -07: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
Elliott Hughes
94072fbb4e Switch to inline assembler in crtbegin.
Using __builtin_frame_address was clever, but didn't work for arm64 (for
reasons which were never investigated) and the ChromeOS folks claim it
causes trouble for x86 with ARC++ (though without a reproduceable test case).

Naked functions turn out to be quite unevenly supported: some architectures
do the right thing, others don't; some architectures warn, others don't (and
the warnings don't always match the platforms that _actually_ have problems).

Inline assembler also removes the guessing games: everyone knows what the
couple of instructions _ought_ to be, and now we don't have to reason about
what the compiler will actually do (yet still keep the majority of the code
in C).

Bug: N/A
Test: builds, boots
Change-Id: I14207ef50ca46b6eca273c3cb7509c311146a3ca
2017-05-23 14:47:16 -07:00
Dmitriy Ivanov
af7e806cec Properly align init/fini_arrays for crtend.o
Also remove them from crtend_so.o

Bug: http://b/20532404
Change-Id: I56a0f80c4b3a83cf11d28bbf17791ff4ce10c5d2
(cherry picked from commit e543c7612e)
2015-05-07 12:56:15 -07:00
Dmitriy Ivanov
ea295f68f1 Unregister pthread_atfork handlers on dlclose()
Bug: http://b/20339788
Change-Id: I874c87faa377645fa9e0752f4fc166d81fd9ef7e
2015-04-24 17:57:37 -07:00
Dimitry Ivanov
094f58fb2a Revert "Unregister pthread_atfork handlers on dlclose()"
The visibility control in pthread_atfork.h is incorrect.
 It breaks 64bit libc.so by hiding pthread_atfork.

 This reverts commit 6df122f852.

Change-Id: I21e4b344d500c6f6de0ccb7420b916c4e233dd34
2015-04-24 03:46:57 +00:00
Dmitriy Ivanov
6df122f852 Unregister pthread_atfork handlers on dlclose()
Change-Id: I326fdf6bb06bed12743f08980b5c69d849c015b8
2015-04-22 19:19:37 -07:00
Colin Cross
e09ab2cc2f Make .note.android.ident section type SH_NOTE
The .note.android.ident section is only used by GDB, which doesn't
care what section type the section is, but it would be convenient
for readelf -n to be able to find the section too.

The old way of getting the .note.android.ident section to be of type
SH_NOTE involved compiling from .c to .s using gcc, running sed to
change progbits to note, and then compiling from .s to .o using gcc.
Since crtbrand.c only contains a section containing data, a
crtbrand.S can be checked in that will compile on all platforms,
avoiding the need for sed.

Also add crtbrand.o to crtbegin_so.o so that libraries also get
the note, and to the crt workaround in arm libc.so.

Change-Id: Ica71942a6af4553b56978ceaa288b3f4c15ebfa2
2015-02-12 21:37:20 -08:00
Dmitriy Ivanov
4b41555ba5 Revert "Register __libc_fini as early as possible."
This reverts commit e880c736d6.

Change-Id: Ide83e442eb5dbfef5298a15bc602c3fe1dda1862
2014-09-04 21:54:34 +00:00
Dmitriy Ivanov
e880c736d6 Register __libc_fini as early as possible.
We want __libc_fini to be called after all the destructors.

Bug: 14611536
Change-Id: Ibb83a94436795ec178fd605fa531ac29608f4a3e
2014-09-03 15:27:29 -07:00
Dmitriy Ivanov
6b56691a67 Fixes for __cxa_finalize
* Ability to register atexit handler from atexit handler
  * Correct way to handle both forms of atexit handler

Bug: https://code.google.com/p/android/issues/detail?id=66595
Bug: 4998315
Change-Id: I39529afaef97b6e1469c21389d54c0d7d175da28
2014-05-05 11:36:57 -07:00
Serban Constantinescu
e210488e0d AArch64: Add initial support for AArch64
This is the first patch out of a series of patches that add support for
AArch64, the new 64bit execution state of the ARMv8 Architecture. The
patches add support for LP64 programming model.

The patch adds:
* "arch-aarch64" to the architecture directories.
* "arch-aarch64/include" - headers used by libc
* "arch-aarch64/bionic":
    - crtbegin, crtend support;
    - aarch64 specific syscall stubs;
    - setjmp, clone, vfork assembly files.

Change-Id: If72b859f81928d03ad05d4ccfcb54c2f5dbf99a5
Signed-off-by: Serban Constantinescu <serban.constantinescu@arm.com>
2013-12-16 17:00:22 -08:00
Pavel Chupin
33a209e575 Fix 16-byte stack alignment requirement for x86_64 ABI
Change-Id: I43304803ac54c8688c61688bd96c7160614172d4
Signed-off-by: Pavel Chupin <pavel.v.chupin@intel.com>
2013-10-21 17:36:28 -07:00
Elliott Hughes
eb847bc866 Fix x86_64 build, clean up intermediate libraries.
The x86_64 build was failing because clone.S had a call to __thread_entry which
was being added to a different intermediate .a on the way to making libc.so,
and the linker couldn't guarantee statically that such a relocation would be
possible.

  ld: error: out/target/product/generic_x86_64/obj/STATIC_LIBRARIES/libc_common_intermediates/libc_common.a(clone.o): requires dynamic R_X86_64_PC32 reloc against '__thread_entry' which may overflow at runtime; recompile with -fPIC

This patch addresses that by ensuring that the caller and callee end up in the
same intermediate .a. While I'm here, I've tried to clean up some of the mess
that led to this situation too. In particular, this removes libc/private/ from
the default include path (except for the DNS code), and splits out the DNS
code into its own library (since it's a weird special case of upstream NetBSD
code that's diverged so heavily it's unlikely ever to get back in sync).

There's more cleanup of the DNS situation possible, but this is definitely a
step in the right direction, and it's more than enough to get x86_64 building
cleanly.

Change-Id: I00425a7245b7a2573df16cc38798187d0729e7c4
2013-10-09 16:00:17 -07:00
synergydev
589eaa4c72 libc: fix __cxa_atexit implicit declaration
Change-Id: Iacad18b332a717e4485c83df4bd42a850ff7699f
2013-10-07 11:33:20 -07:00
Pavel Chupin
719269db18 Fixed ASM_ALIGN macro
Got it all wrong on first patch. Somehow that didn't affect system
build, neither arm nor x86... something to think about.

Change-Id: I45416d843aad44af62841c6f6ab607ccf3f012ea
Signed-off-by: Pavel Chupin <pavel.v.chupin@intel.com>
2013-10-04 11:12:58 +04:00
Pavel Chupin
b49c17c2bf Move common arch-* code to arch-common directory
Will be helpful on adding x86_64

Change-Id: I96cf6fc7912c02f289c75f07ae0079c32d69173f
Signed-off-by: Pavel Chupin <pavel.v.chupin@intel.com>
2013-10-03 11:14:33 +04:00