blob: a54f29e76162330e141eaaea4ccf65e99fecb6bf [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 Prichard45d13492019-01-03 02:51:30 -080032#include <sys/param.h>
Ryan Prichard48097552019-01-06 18:24:10 -080033#include <unistd.h>
Ryan Prichard45d13492019-01-03 02:51:30 -080034
35#include "private/bionic_macros.h"
36#include "private/bionic_tls.h"
37
Ryan Prichard48097552019-01-06 18:24:10 -080038// Search for a TLS segment in the given phdr table. Returns true if it has a
39// TLS segment and false otherwise.
40bool __bionic_get_tls_segment(const ElfW(Phdr)* phdr_table, size_t phdr_count,
41 ElfW(Addr) load_bias, const char* mod_name,
42 TlsSegment* out) {
43 for (size_t i = 0; i < phdr_count; ++i) {
44 const ElfW(Phdr)& phdr = phdr_table[i];
45 if (phdr.p_type == PT_TLS) {
46 // N.B. The size does not need to be a multiple of the alignment. With
47 // ld.bfd (or after using binutils' strip), the TLS segment's size isn't
48 // rounded up.
49 size_t alignment = phdr.p_align;
50 if (alignment == 0 || !powerof2(alignment)) {
51 async_safe_fatal("error: \"%s\": TLS segment alignment is not a power of 2: %zu",
52 mod_name, alignment);
53 }
54 // Bionic only respects TLS alignment up to one page.
55 alignment = MIN(alignment, PAGE_SIZE);
56 *out = TlsSegment {
57 phdr.p_memsz,
58 alignment,
59 reinterpret_cast<void*>(load_bias + phdr.p_vaddr),
60 phdr.p_filesz,
61 };
62 return true;
63 }
64 }
65 return false;
66}
67
Ryan Prichard977e47d2019-01-14 21:52:14 -080068// Reserves space for the Bionic TCB and the executable's TLS segment. Returns
69// the offset of the executable's TLS segment.
70size_t StaticTlsLayout::reserve_exe_segment_and_tcb(const TlsSegment* exe_segment,
71 const char* progname __attribute__((unused))) {
72 // Special case: if the executable has no TLS segment, then just allocate a
73 // TCB and skip the minimum alignment check on ARM.
74 if (exe_segment == nullptr) {
75 offset_bionic_tcb_ = reserve_type<bionic_tcb>();
76 return 0;
77 }
78
79#if defined(__arm__) || defined(__aarch64__)
80
81 // First reserve enough space for the TCB before the executable segment.
82 reserve(sizeof(bionic_tcb), 1);
83
84 // Then reserve the segment itself.
85 const size_t result = reserve(exe_segment->size, exe_segment->alignment);
86
87 // The variant 1 ABI that ARM linkers follow specifies a 2-word TCB between
88 // the thread pointer and the start of the executable's TLS segment, but both
89 // the thread pointer and the TLS segment are aligned appropriately for the
90 // TLS segment. Calculate the distance between the thread pointer and the
91 // EXE's segment.
92 const size_t exe_tpoff = __BIONIC_ALIGN(sizeof(void*) * 2, exe_segment->alignment);
93
94 const size_t min_bionic_alignment = BIONIC_ROUND_UP_POWER_OF_2(MAX_TLS_SLOT) * sizeof(void*);
95 if (exe_tpoff < min_bionic_alignment) {
96 async_safe_fatal("error: \"%s\": executable's TLS segment is underaligned: "
97 "alignment is %zu, needs to be at least %zu for %s Bionic",
98 progname, exe_segment->alignment, min_bionic_alignment,
99 (sizeof(void*) == 4 ? "ARM" : "ARM64"));
100 }
101
102 offset_bionic_tcb_ = result - exe_tpoff - (-MIN_TLS_SLOT * sizeof(void*));
103 return result;
104
105#elif defined(__i386__) || defined(__x86_64__)
106
107 // x86 uses variant 2 TLS layout. The executable's segment is located just
108 // before the TCB.
109 static_assert(MIN_TLS_SLOT == 0, "First slot of bionic_tcb must be slot #0 on x86");
110 const size_t exe_size = round_up_with_overflow_check(exe_segment->size, exe_segment->alignment);
111 reserve(exe_size, 1);
112 const size_t max_align = MAX(alignof(bionic_tcb), exe_segment->alignment);
113 offset_bionic_tcb_ = reserve(sizeof(bionic_tcb), max_align);
114 return offset_bionic_tcb_ - exe_size;
115
116#else
117#error "Unrecognized architecture"
118#endif
Ryan Prichard45d13492019-01-03 02:51:30 -0800119}
120
121void StaticTlsLayout::reserve_bionic_tls() {
122 offset_bionic_tls_ = reserve_type<bionic_tls>();
123}
124
125void StaticTlsLayout::finish_layout() {
126 // Round the offset up to the alignment.
127 offset_ = round_up_with_overflow_check(offset_, alignment_);
Ryan Prichard977e47d2019-01-14 21:52:14 -0800128
129 if (overflowed_) {
130 async_safe_fatal("error: TLS segments in static TLS overflowed");
131 }
Ryan Prichard45d13492019-01-03 02:51:30 -0800132}
133
134// The size is not required to be a multiple of the alignment. The alignment
135// must be a positive power-of-two.
136size_t StaticTlsLayout::reserve(size_t size, size_t alignment) {
137 offset_ = round_up_with_overflow_check(offset_, alignment);
138 const size_t result = offset_;
139 if (__builtin_add_overflow(offset_, size, &offset_)) overflowed_ = true;
140 alignment_ = MAX(alignment_, alignment);
141 return result;
142}
143
144size_t StaticTlsLayout::round_up_with_overflow_check(size_t value, size_t alignment) {
145 const size_t old_value = value;
146 value = __BIONIC_ALIGN(value, alignment);
147 if (value < old_value) overflowed_ = true;
148 return value;
149}