blob: 691293d94a9d7307dd8117bf7734b0ae01f9a629 [file] [log] [blame]
Ryan Prichard45d13492019-01-03 02:51:30 -08001/*
2 * Copyright (C) 2019 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include "private/bionic_elf_tls.h"
30
Ryan Prichard48097552019-01-06 18:24:10 -080031#include <async_safe/log.h>
Ryan Prichard361c1b42019-01-15 13:45:27 -080032#include <string.h>
Ryan Prichard45d13492019-01-03 02:51:30 -080033#include <sys/param.h>
Ryan Prichard48097552019-01-06 18:24:10 -080034#include <unistd.h>
Ryan Prichard45d13492019-01-03 02:51:30 -080035
Ryan Prichard361c1b42019-01-15 13:45:27 -080036#include "private/ScopedRWLock.h"
37#include "private/bionic_globals.h"
Ryan Prichard45d13492019-01-03 02:51:30 -080038#include "private/bionic_macros.h"
39#include "private/bionic_tls.h"
40
Ryan Prichard48097552019-01-06 18:24:10 -080041// Search for a TLS segment in the given phdr table. Returns true if it has a
42// TLS segment and false otherwise.
43bool __bionic_get_tls_segment(const ElfW(Phdr)* phdr_table, size_t phdr_count,
Ryan Prichard19883502019-01-16 23:13:38 -080044 ElfW(Addr) load_bias, TlsSegment* out) {
Ryan Prichard48097552019-01-06 18:24:10 -080045 for (size_t i = 0; i < phdr_count; ++i) {
46 const ElfW(Phdr)& phdr = phdr_table[i];
47 if (phdr.p_type == PT_TLS) {
Ryan Prichard48097552019-01-06 18:24:10 -080048 *out = TlsSegment {
49 phdr.p_memsz,
Ryan Prichard19883502019-01-16 23:13:38 -080050 phdr.p_align,
Ryan Prichard48097552019-01-06 18:24:10 -080051 reinterpret_cast<void*>(load_bias + phdr.p_vaddr),
52 phdr.p_filesz,
53 };
54 return true;
55 }
56 }
57 return false;
58}
59
Ryan Prichard19883502019-01-16 23:13:38 -080060// Return true if the alignment of a TLS segment is a valid power-of-two. Also
61// cap the alignment if it's too high.
62bool __bionic_check_tls_alignment(size_t* alignment) {
63 // N.B. The size does not need to be a multiple of the alignment. With
64 // ld.bfd (or after using binutils' strip), the TLS segment's size isn't
65 // rounded up.
66 if (*alignment == 0 || !powerof2(*alignment)) {
67 return false;
68 }
69 // Bionic only respects TLS alignment up to one page.
70 *alignment = MIN(*alignment, PAGE_SIZE);
71 return true;
72}
73
Ryan Prichard977e47d2019-01-14 21:52:14 -080074// Reserves space for the Bionic TCB and the executable's TLS segment. Returns
75// the offset of the executable's TLS segment.
76size_t StaticTlsLayout::reserve_exe_segment_and_tcb(const TlsSegment* exe_segment,
77 const char* progname __attribute__((unused))) {
78 // Special case: if the executable has no TLS segment, then just allocate a
79 // TCB and skip the minimum alignment check on ARM.
80 if (exe_segment == nullptr) {
81 offset_bionic_tcb_ = reserve_type<bionic_tcb>();
82 return 0;
83 }
84
85#if defined(__arm__) || defined(__aarch64__)
86
87 // First reserve enough space for the TCB before the executable segment.
88 reserve(sizeof(bionic_tcb), 1);
89
90 // Then reserve the segment itself.
91 const size_t result = reserve(exe_segment->size, exe_segment->alignment);
92
93 // The variant 1 ABI that ARM linkers follow specifies a 2-word TCB between
94 // the thread pointer and the start of the executable's TLS segment, but both
95 // the thread pointer and the TLS segment are aligned appropriately for the
96 // TLS segment. Calculate the distance between the thread pointer and the
97 // EXE's segment.
98 const size_t exe_tpoff = __BIONIC_ALIGN(sizeof(void*) * 2, exe_segment->alignment);
99
100 const size_t min_bionic_alignment = BIONIC_ROUND_UP_POWER_OF_2(MAX_TLS_SLOT) * sizeof(void*);
101 if (exe_tpoff < min_bionic_alignment) {
102 async_safe_fatal("error: \"%s\": executable's TLS segment is underaligned: "
103 "alignment is %zu, needs to be at least %zu for %s Bionic",
104 progname, exe_segment->alignment, min_bionic_alignment,
105 (sizeof(void*) == 4 ? "ARM" : "ARM64"));
106 }
107
108 offset_bionic_tcb_ = result - exe_tpoff - (-MIN_TLS_SLOT * sizeof(void*));
109 return result;
110
111#elif defined(__i386__) || defined(__x86_64__)
112
113 // x86 uses variant 2 TLS layout. The executable's segment is located just
114 // before the TCB.
115 static_assert(MIN_TLS_SLOT == 0, "First slot of bionic_tcb must be slot #0 on x86");
116 const size_t exe_size = round_up_with_overflow_check(exe_segment->size, exe_segment->alignment);
117 reserve(exe_size, 1);
118 const size_t max_align = MAX(alignof(bionic_tcb), exe_segment->alignment);
119 offset_bionic_tcb_ = reserve(sizeof(bionic_tcb), max_align);
120 return offset_bionic_tcb_ - exe_size;
121
122#else
123#error "Unrecognized architecture"
124#endif
Ryan Prichard45d13492019-01-03 02:51:30 -0800125}
126
127void StaticTlsLayout::reserve_bionic_tls() {
128 offset_bionic_tls_ = reserve_type<bionic_tls>();
129}
130
131void StaticTlsLayout::finish_layout() {
132 // Round the offset up to the alignment.
133 offset_ = round_up_with_overflow_check(offset_, alignment_);
Ryan Prichard977e47d2019-01-14 21:52:14 -0800134
135 if (overflowed_) {
136 async_safe_fatal("error: TLS segments in static TLS overflowed");
137 }
Ryan Prichard45d13492019-01-03 02:51:30 -0800138}
139
140// The size is not required to be a multiple of the alignment. The alignment
141// must be a positive power-of-two.
142size_t StaticTlsLayout::reserve(size_t size, size_t alignment) {
143 offset_ = round_up_with_overflow_check(offset_, alignment);
144 const size_t result = offset_;
145 if (__builtin_add_overflow(offset_, size, &offset_)) overflowed_ = true;
146 alignment_ = MAX(alignment_, alignment);
147 return result;
148}
149
150size_t StaticTlsLayout::round_up_with_overflow_check(size_t value, size_t alignment) {
151 const size_t old_value = value;
152 value = __BIONIC_ALIGN(value, alignment);
153 if (value < old_value) overflowed_ = true;
154 return value;
155}
Ryan Prichard361c1b42019-01-15 13:45:27 -0800156
157// Copy each TLS module's initialization image into a newly-allocated block of
158// static TLS memory. To reduce dirty pages, this function only writes to pages
159// within the static TLS that need initialization. The memory should already be
160// zero-initialized on entry.
161void __init_static_tls(void* static_tls) {
162 // The part of the table we care about (i.e. static TLS modules) never changes
163 // after startup, but we still need the mutex because the table could grow,
164 // moving the initial part. If this locking is too slow, we can duplicate the
165 // static part of the table.
166 TlsModules& modules = __libc_shared_globals()->tls_modules;
167 ScopedReadLock locker(&modules.rwlock);
168
169 for (size_t i = 0; i < modules.module_count; ++i) {
170 TlsModule& module = modules.module_table[i];
171 if (module.static_offset == SIZE_MAX) {
172 // All of the static modules come before all of the dynamic modules, so
173 // once we see the first dynamic module, we're done.
174 break;
175 }
176 if (module.segment.init_size == 0) {
177 // Skip the memcpy call for TLS segments with no initializer, which is
178 // common.
179 continue;
180 }
181 memcpy(static_cast<char*>(static_tls) + module.static_offset,
182 module.segment.init_ptr,
183 module.segment.init_size);
184 }
185}