blob: c67c22147266c1ee26d45d8eaf71e0ceea233c3e [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#pragma once
30
Ryan Prichard48097552019-01-06 18:24:10 -080031#include <link.h>
Ryan Prichard45d13492019-01-03 02:51:30 -080032#include <stdint.h>
Ryan Prichard48097552019-01-06 18:24:10 -080033#include <sys/cdefs.h>
34
35struct TlsSegment {
36 size_t size = 0;
37 size_t alignment = 1;
38 const void* init_ptr = ""; // Field is non-null even when init_size is 0.
39 size_t init_size = 0;
40};
41
42__LIBC_HIDDEN__ bool __bionic_get_tls_segment(const ElfW(Phdr)* phdr_table, size_t phdr_count,
43 ElfW(Addr) load_bias, const char* mod_name,
44 TlsSegment* out);
Ryan Prichard45d13492019-01-03 02:51:30 -080045
46struct StaticTlsLayout {
47 constexpr StaticTlsLayout() {}
48
49private:
50 size_t offset_ = 0;
51 size_t alignment_ = 1;
52 bool overflowed_ = false;
53
54 // Offsets to various Bionic TLS structs from the beginning of static TLS.
55 size_t offset_bionic_tcb_ = SIZE_MAX;
56 size_t offset_bionic_tls_ = SIZE_MAX;
57
58public:
59 size_t offset_bionic_tcb() const { return offset_bionic_tcb_; }
60 size_t offset_bionic_tls() const { return offset_bionic_tls_; }
61
62 size_t size() const { return offset_; }
63 size_t alignment() const { return alignment_; }
64 bool overflowed() const { return overflowed_; }
65
Ryan Prichard977e47d2019-01-14 21:52:14 -080066 size_t reserve_exe_segment_and_tcb(const TlsSegment* exe_segment, const char* progname);
Ryan Prichard45d13492019-01-03 02:51:30 -080067 void reserve_bionic_tls();
Ryan Prichard977e47d2019-01-14 21:52:14 -080068 size_t reserve_solib_segment(const TlsSegment& segment) {
69 return reserve(segment.size, segment.alignment);
70 }
Ryan Prichard45d13492019-01-03 02:51:30 -080071 void finish_layout();
72
73private:
74 size_t reserve(size_t size, size_t alignment);
75
76 template <typename T> size_t reserve_type() {
77 return reserve(sizeof(T), alignof(T));
78 }
79
80 size_t round_up_with_overflow_check(size_t value, size_t alignment);
81};