blob: a30fe33038c31af150c42a0c19c64d30f52fe49b [file] [log] [blame]
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +02001/*
2 * Copyright (C) 2012 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 */
Elliott Hughescbc80ba2018-02-13 14:26:29 -080028
29#pragma once
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +020030
31/* Declarations related to the ELF program header table and segments.
32 *
33 * The design goal is to provide an API that is as close as possible
34 * to the ELF spec, and does not depend on linker-specific data
35 * structures (e.g. the exact layout of struct soinfo).
36 */
37
38#include "linker.h"
Dmitriy Ivanovcf1cbbe2015-10-19 16:57:46 -070039#include "linker_mapped_file_fragment.h"
Tamas Petz8d55d182020-02-24 14:15:25 +010040#include "linker_note_gnu_property.h"
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +020041
Elliott Hughes650be4e2013-03-05 18:47:58 -080042class ElfReader {
43 public:
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -070044 ElfReader();
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +020045
Elliott Hughes396868c2024-04-10 21:52:10 +000046 [[nodiscard]] bool Read(const char* name, int fd, off64_t file_offset, off64_t file_size);
47 [[nodiscard]] bool Load(address_space_params* address_space);
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +020048
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -070049 const char* name() const { return name_.c_str(); }
50 size_t phdr_count() const { return phdr_num_; }
51 ElfW(Addr) load_start() const { return reinterpret_cast<ElfW(Addr)>(load_start_); }
52 size_t load_size() const { return load_size_; }
Evgenii Stepanove0848bb2020-07-14 16:44:57 -070053 ElfW(Addr) gap_start() const { return reinterpret_cast<ElfW(Addr)>(gap_start_); }
54 size_t gap_size() const { return gap_size_; }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -070055 ElfW(Addr) load_bias() const { return load_bias_; }
56 const ElfW(Phdr)* loaded_phdr() const { return loaded_phdr_; }
57 const ElfW(Dyn)* dynamic() const { return dynamic_; }
58 const char* get_string(ElfW(Word) index) const;
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -080059 bool is_mapped_by_caller() const { return mapped_by_caller_; }
Ryan Prichard8f639a42018-10-01 23:10:05 -070060 ElfW(Addr) entry_point() const { return header_.e_entry + load_bias_; }
Kalesh Singh377f0b92024-01-31 20:23:39 -080061 bool should_pad_segments() const { return should_pad_segments_; }
Kalesh Singhb23787f2024-09-05 08:22:06 +000062 bool should_use_16kib_app_compat() const { return should_use_16kib_app_compat_; }
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +020063
Elliott Hughes650be4e2013-03-05 18:47:58 -080064 private:
Elliott Hughes396868c2024-04-10 21:52:10 +000065 [[nodiscard]] bool ReadElfHeader();
66 [[nodiscard]] bool VerifyElfHeader();
67 [[nodiscard]] bool ReadProgramHeaders();
68 [[nodiscard]] bool ReadSectionHeaders();
69 [[nodiscard]] bool ReadDynamicSection();
70 [[nodiscard]] bool ReadPadSegmentNote();
71 [[nodiscard]] bool ReserveAddressSpace(address_space_params* address_space);
Kalesh Singh86e04f62024-09-05 06:24:14 +000072 [[nodiscard]] bool MapSegment(size_t seg_idx, size_t len);
Kalesh Singh37bcaea2024-09-05 06:32:07 +000073 void ZeroFillSegment(const ElfW(Phdr)* phdr);
Kalesh Singhe0f4a372024-09-05 07:07:21 +000074 void DropPaddingPages(const ElfW(Phdr)* phdr, uint64_t seg_file_end);
Kalesh Singh138a9552024-09-05 08:05:56 +000075 [[nodiscard]] bool MapBssSection(const ElfW(Phdr)* phdr, ElfW(Addr) seg_page_end,
76 ElfW(Addr) seg_file_end);
Elliott Hughes396868c2024-04-10 21:52:10 +000077 [[nodiscard]] bool LoadSegments();
78 [[nodiscard]] bool FindPhdr();
79 [[nodiscard]] bool FindGnuPropertySection();
80 [[nodiscard]] bool CheckPhdr(ElfW(Addr));
81 [[nodiscard]] bool CheckFileRange(ElfW(Addr) offset, size_t size, size_t alignment);
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +020082
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -070083 bool did_read_;
84 bool did_load_;
85 std::string name_;
Elliott Hughes650be4e2013-03-05 18:47:58 -080086 int fd_;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070087 off64_t file_offset_;
Dmitriy Ivanov3f987f52015-06-25 15:51:41 -070088 off64_t file_size_;
Elliott Hughes650be4e2013-03-05 18:47:58 -080089
Elliott Hughes0266ae52014-02-10 17:46:57 -080090 ElfW(Ehdr) header_;
Elliott Hughes650be4e2013-03-05 18:47:58 -080091 size_t phdr_num_;
92
Dmitriy Ivanovcf1cbbe2015-10-19 16:57:46 -070093 MappedFileFragment phdr_fragment_;
94 const ElfW(Phdr)* phdr_table_;
Elliott Hughes650be4e2013-03-05 18:47:58 -080095
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -070096 MappedFileFragment shdr_fragment_;
97 const ElfW(Shdr)* shdr_table_;
98 size_t shdr_num_;
99
100 MappedFileFragment dynamic_fragment_;
101 const ElfW(Dyn)* dynamic_;
102
103 MappedFileFragment strtab_fragment_;
104 const char* strtab_;
105 size_t strtab_size_;
106
Elliott Hughes650be4e2013-03-05 18:47:58 -0800107 // First page of reserved address space.
108 void* load_start_;
109 // Size in bytes of reserved address space.
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700110 size_t load_size_;
Evgenii Stepanove0848bb2020-07-14 16:44:57 -0700111 // First page of inaccessible gap mapping reserved for this DSO.
112 void* gap_start_;
113 // Size in bytes of the gap mapping.
114 size_t gap_size_;
Elliott Hughes650be4e2013-03-05 18:47:58 -0800115 // Load bias.
Elliott Hughes0266ae52014-02-10 17:46:57 -0800116 ElfW(Addr) load_bias_;
Elliott Hughes650be4e2013-03-05 18:47:58 -0800117
118 // Loaded phdr.
Elliott Hughes0266ae52014-02-10 17:46:57 -0800119 const ElfW(Phdr)* loaded_phdr_;
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800120
121 // Is map owned by the caller
122 bool mapped_by_caller_;
Tamas Petz8d55d182020-02-24 14:15:25 +0100123
Kalesh Singh377f0b92024-01-31 20:23:39 -0800124 // Pad gaps between segments when memory mapping?
125 bool should_pad_segments_ = false;
126
Kalesh Singhb23787f2024-09-05 08:22:06 +0000127 // Use app compat mode when loading 4KiB max-page-size ELFs on 16KiB page-size devices?
128 bool should_use_16kib_app_compat_ = false;
129
Tamas Petz8d55d182020-02-24 14:15:25 +0100130 // Only used by AArch64 at the moment.
131 GnuPropertySection note_gnu_property_ __unused;
Elliott Hughes650be4e2013-03-05 18:47:58 -0800132};
133
Elliott Hughes0266ae52014-02-10 17:46:57 -0800134size_t phdr_table_get_load_size(const ElfW(Phdr)* phdr_table, size_t phdr_count,
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700135 ElfW(Addr)* min_vaddr = nullptr, ElfW(Addr)* max_vaddr = nullptr);
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +0200136
Collin Fijalkovich47d27aa2021-03-24 10:17:39 -0700137size_t phdr_table_get_maximum_alignment(const ElfW(Phdr)* phdr_table, size_t phdr_count);
Steven Morelandfc89c8a2024-08-01 21:20:33 +0000138size_t phdr_table_get_minimum_alignment(const ElfW(Phdr)* phdr_table, size_t phdr_count);
Collin Fijalkovich47d27aa2021-03-24 10:17:39 -0700139
Tamas Petz8d55d182020-02-24 14:15:25 +0100140int phdr_table_protect_segments(const ElfW(Phdr)* phdr_table, size_t phdr_count,
Kalesh Singh4084b552024-03-13 13:35:49 -0700141 ElfW(Addr) load_bias, bool should_pad_segments,
Kalesh Singhb23787f2024-09-05 08:22:06 +0000142 bool should_use_16kib_app_compat,
Kalesh Singh4084b552024-03-13 13:35:49 -0700143 const GnuPropertySection* prop = nullptr);
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +0000144
145int phdr_table_unprotect_segments(const ElfW(Phdr)* phdr_table, size_t phdr_count,
Kalesh Singhb23787f2024-09-05 08:22:06 +0000146 ElfW(Addr) load_bias, bool should_pad_segments,
147 bool should_use_16kib_app_compat);
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +0000148
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -0700149int phdr_table_protect_gnu_relro(const ElfW(Phdr)* phdr_table, size_t phdr_count,
Kalesh Singhb23787f2024-09-05 08:22:06 +0000150 ElfW(Addr) load_bias, bool should_pad_segments,
151 bool should_use_16kib_app_compat);
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +0200152
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -0700153int phdr_table_serialize_gnu_relro(const ElfW(Phdr)* phdr_table, size_t phdr_count,
Torne (Richard Coles)fa9f7f22019-04-02 17:04:42 -0400154 ElfW(Addr) load_bias, int fd, size_t* file_offset);
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000155
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -0700156int phdr_table_map_gnu_relro(const ElfW(Phdr)* phdr_table, size_t phdr_count,
Torne (Richard Coles)efbe9a52018-10-17 15:59:38 -0400157 ElfW(Addr) load_bias, int fd, size_t* file_offset);
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +0200158
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700159#if defined(__arm__)
Elliott Hughes0266ae52014-02-10 17:46:57 -0800160int phdr_table_get_arm_exidx(const ElfW(Phdr)* phdr_table, size_t phdr_count, ElfW(Addr) load_bias,
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -0800161 ElfW(Addr)** arm_exidx, size_t* arm_exidix_count);
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +0200162#endif
163
Elliott Hughes0266ae52014-02-10 17:46:57 -0800164void phdr_table_get_dynamic_section(const ElfW(Phdr)* phdr_table, size_t phdr_count,
Ningsheng Jiane93be992014-09-16 15:22:10 +0800165 ElfW(Addr) load_bias, ElfW(Dyn)** dynamic,
166 ElfW(Word)* dynamic_flags);
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +0200167
Tamas Petz8d55d182020-02-24 14:15:25 +0100168const char* phdr_table_get_interpreter_name(const ElfW(Phdr)* phdr_table, size_t phdr_count,
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700169 ElfW(Addr) load_bias);
Kalesh Singhc5c1d192024-04-09 16:27:56 -0700170
171bool page_size_migration_supported();