1*8d67ca89SAndroid Build Coastguard Worker /* 2*8d67ca89SAndroid Build Coastguard Worker * Copyright (C) 2012 The Android Open Source Project 3*8d67ca89SAndroid Build Coastguard Worker * All rights reserved. 4*8d67ca89SAndroid Build Coastguard Worker * 5*8d67ca89SAndroid Build Coastguard Worker * Redistribution and use in source and binary forms, with or without 6*8d67ca89SAndroid Build Coastguard Worker * modification, are permitted provided that the following conditions 7*8d67ca89SAndroid Build Coastguard Worker * are met: 8*8d67ca89SAndroid Build Coastguard Worker * * Redistributions of source code must retain the above copyright 9*8d67ca89SAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer. 10*8d67ca89SAndroid Build Coastguard Worker * * Redistributions in binary form must reproduce the above copyright 11*8d67ca89SAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer in 12*8d67ca89SAndroid Build Coastguard Worker * the documentation and/or other materials provided with the 13*8d67ca89SAndroid Build Coastguard Worker * distribution. 14*8d67ca89SAndroid Build Coastguard Worker * 15*8d67ca89SAndroid Build Coastguard Worker * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16*8d67ca89SAndroid Build Coastguard Worker * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17*8d67ca89SAndroid Build Coastguard Worker * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 18*8d67ca89SAndroid Build Coastguard Worker * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 19*8d67ca89SAndroid Build Coastguard Worker * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20*8d67ca89SAndroid Build Coastguard Worker * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21*8d67ca89SAndroid Build Coastguard Worker * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 22*8d67ca89SAndroid Build Coastguard Worker * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23*8d67ca89SAndroid Build Coastguard Worker * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24*8d67ca89SAndroid Build Coastguard Worker * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 25*8d67ca89SAndroid Build Coastguard Worker * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26*8d67ca89SAndroid Build Coastguard Worker * SUCH DAMAGE. 27*8d67ca89SAndroid Build Coastguard Worker */ 28*8d67ca89SAndroid Build Coastguard Worker 29*8d67ca89SAndroid Build Coastguard Worker #pragma once 30*8d67ca89SAndroid Build Coastguard Worker 31*8d67ca89SAndroid Build Coastguard Worker /* Declarations related to the ELF program header table and segments. 32*8d67ca89SAndroid Build Coastguard Worker * 33*8d67ca89SAndroid Build Coastguard Worker * The design goal is to provide an API that is as close as possible 34*8d67ca89SAndroid Build Coastguard Worker * to the ELF spec, and does not depend on linker-specific data 35*8d67ca89SAndroid Build Coastguard Worker * structures (e.g. the exact layout of struct soinfo). 36*8d67ca89SAndroid Build Coastguard Worker */ 37*8d67ca89SAndroid Build Coastguard Worker 38*8d67ca89SAndroid Build Coastguard Worker #include "linker.h" 39*8d67ca89SAndroid Build Coastguard Worker #include "linker_mapped_file_fragment.h" 40*8d67ca89SAndroid Build Coastguard Worker #include "linker_note_gnu_property.h" 41*8d67ca89SAndroid Build Coastguard Worker 42*8d67ca89SAndroid Build Coastguard Worker #include <list> 43*8d67ca89SAndroid Build Coastguard Worker 44*8d67ca89SAndroid Build Coastguard Worker #define MAYBE_MAP_FLAG(x, from, to) (((x) & (from)) ? (to) : 0) 45*8d67ca89SAndroid Build Coastguard Worker #define PFLAGS_TO_PROT(x) (MAYBE_MAP_FLAG((x), PF_X, PROT_EXEC) | \ 46*8d67ca89SAndroid Build Coastguard Worker MAYBE_MAP_FLAG((x), PF_R, PROT_READ) | \ 47*8d67ca89SAndroid Build Coastguard Worker MAYBE_MAP_FLAG((x), PF_W, PROT_WRITE)) 48*8d67ca89SAndroid Build Coastguard Worker 49*8d67ca89SAndroid Build Coastguard Worker static constexpr size_t kCompatPageSize = 0x1000; 50*8d67ca89SAndroid Build Coastguard Worker 51*8d67ca89SAndroid Build Coastguard Worker class ElfReader { 52*8d67ca89SAndroid Build Coastguard Worker public: 53*8d67ca89SAndroid Build Coastguard Worker ElfReader(); 54*8d67ca89SAndroid Build Coastguard Worker 55*8d67ca89SAndroid Build Coastguard Worker [[nodiscard]] bool Read(const char* name, int fd, off64_t file_offset, off64_t file_size); 56*8d67ca89SAndroid Build Coastguard Worker [[nodiscard]] bool Load(address_space_params* address_space); 57*8d67ca89SAndroid Build Coastguard Worker name()58*8d67ca89SAndroid Build Coastguard Worker const char* name() const { return name_.c_str(); } phdr_count()59*8d67ca89SAndroid Build Coastguard Worker size_t phdr_count() const { return phdr_num_; } load_start()60*8d67ca89SAndroid Build Coastguard Worker ElfW(Addr) load_start() const { return reinterpret_cast<ElfW(Addr)>(load_start_); } load_size()61*8d67ca89SAndroid Build Coastguard Worker size_t load_size() const { return load_size_; } gap_start()62*8d67ca89SAndroid Build Coastguard Worker ElfW(Addr) gap_start() const { return reinterpret_cast<ElfW(Addr)>(gap_start_); } gap_size()63*8d67ca89SAndroid Build Coastguard Worker size_t gap_size() const { return gap_size_; } load_bias()64*8d67ca89SAndroid Build Coastguard Worker ElfW(Addr) load_bias() const { return load_bias_; } ElfW(Phdr)65*8d67ca89SAndroid Build Coastguard Worker const ElfW(Phdr)* loaded_phdr() const { return loaded_phdr_; } ElfW(Dyn)66*8d67ca89SAndroid Build Coastguard Worker const ElfW(Dyn)* dynamic() const { return dynamic_; } 67*8d67ca89SAndroid Build Coastguard Worker const char* get_string(ElfW(Word) index) const; is_mapped_by_caller()68*8d67ca89SAndroid Build Coastguard Worker bool is_mapped_by_caller() const { return mapped_by_caller_; } entry_point()69*8d67ca89SAndroid Build Coastguard Worker ElfW(Addr) entry_point() const { return header_.e_entry + load_bias_; } should_pad_segments()70*8d67ca89SAndroid Build Coastguard Worker bool should_pad_segments() const { return should_pad_segments_; } should_use_16kib_app_compat()71*8d67ca89SAndroid Build Coastguard Worker bool should_use_16kib_app_compat() const { return should_use_16kib_app_compat_; } compat_relro_start()72*8d67ca89SAndroid Build Coastguard Worker ElfW(Addr) compat_relro_start() const { return compat_relro_start_; } compat_relro_size()73*8d67ca89SAndroid Build Coastguard Worker ElfW(Addr) compat_relro_size() const { return compat_relro_size_; } 74*8d67ca89SAndroid Build Coastguard Worker 75*8d67ca89SAndroid Build Coastguard Worker private: 76*8d67ca89SAndroid Build Coastguard Worker [[nodiscard]] bool ReadElfHeader(); 77*8d67ca89SAndroid Build Coastguard Worker [[nodiscard]] bool VerifyElfHeader(); 78*8d67ca89SAndroid Build Coastguard Worker [[nodiscard]] bool ReadProgramHeaders(); 79*8d67ca89SAndroid Build Coastguard Worker [[nodiscard]] bool ReadSectionHeaders(); 80*8d67ca89SAndroid Build Coastguard Worker [[nodiscard]] bool ReadDynamicSection(); 81*8d67ca89SAndroid Build Coastguard Worker [[nodiscard]] bool ReadPadSegmentNote(); 82*8d67ca89SAndroid Build Coastguard Worker [[nodiscard]] bool ReserveAddressSpace(address_space_params* address_space); 83*8d67ca89SAndroid Build Coastguard Worker [[nodiscard]] bool MapSegment(size_t seg_idx, size_t len); 84*8d67ca89SAndroid Build Coastguard Worker [[nodiscard]] bool CompatMapSegment(size_t seg_idx, size_t len); 85*8d67ca89SAndroid Build Coastguard Worker void ZeroFillSegment(const ElfW(Phdr)* phdr); 86*8d67ca89SAndroid Build Coastguard Worker void DropPaddingPages(const ElfW(Phdr)* phdr, uint64_t seg_file_end); 87*8d67ca89SAndroid Build Coastguard Worker [[nodiscard]] bool MapBssSection(const ElfW(Phdr)* phdr, ElfW(Addr) seg_page_end, 88*8d67ca89SAndroid Build Coastguard Worker ElfW(Addr) seg_file_end); 89*8d67ca89SAndroid Build Coastguard Worker [[nodiscard]] bool IsEligibleFor16KiBAppCompat(ElfW(Addr)* vaddr); 90*8d67ca89SAndroid Build Coastguard Worker [[nodiscard]] bool HasAtMostOneRelroSegment(const ElfW(Phdr)** relro_phdr); 91*8d67ca89SAndroid Build Coastguard Worker [[nodiscard]] bool Setup16KiBAppCompat(); 92*8d67ca89SAndroid Build Coastguard Worker [[nodiscard]] bool LoadSegments(); 93*8d67ca89SAndroid Build Coastguard Worker [[nodiscard]] bool FindPhdr(); 94*8d67ca89SAndroid Build Coastguard Worker [[nodiscard]] bool FindGnuPropertySection(); 95*8d67ca89SAndroid Build Coastguard Worker [[nodiscard]] bool CheckPhdr(ElfW(Addr)); 96*8d67ca89SAndroid Build Coastguard Worker [[nodiscard]] bool CheckFileRange(ElfW(Addr) offset, size_t size, size_t alignment); 97*8d67ca89SAndroid Build Coastguard Worker 98*8d67ca89SAndroid Build Coastguard Worker bool did_read_; 99*8d67ca89SAndroid Build Coastguard Worker bool did_load_; 100*8d67ca89SAndroid Build Coastguard Worker std::string name_; 101*8d67ca89SAndroid Build Coastguard Worker int fd_; 102*8d67ca89SAndroid Build Coastguard Worker off64_t file_offset_; 103*8d67ca89SAndroid Build Coastguard Worker off64_t file_size_; 104*8d67ca89SAndroid Build Coastguard Worker 105*8d67ca89SAndroid Build Coastguard Worker ElfW(Ehdr) header_; 106*8d67ca89SAndroid Build Coastguard Worker size_t phdr_num_; 107*8d67ca89SAndroid Build Coastguard Worker 108*8d67ca89SAndroid Build Coastguard Worker MappedFileFragment phdr_fragment_; 109*8d67ca89SAndroid Build Coastguard Worker const ElfW(Phdr)* phdr_table_; 110*8d67ca89SAndroid Build Coastguard Worker 111*8d67ca89SAndroid Build Coastguard Worker MappedFileFragment shdr_fragment_; 112*8d67ca89SAndroid Build Coastguard Worker const ElfW(Shdr)* shdr_table_; 113*8d67ca89SAndroid Build Coastguard Worker size_t shdr_num_; 114*8d67ca89SAndroid Build Coastguard Worker 115*8d67ca89SAndroid Build Coastguard Worker MappedFileFragment dynamic_fragment_; 116*8d67ca89SAndroid Build Coastguard Worker const ElfW(Dyn)* dynamic_; 117*8d67ca89SAndroid Build Coastguard Worker 118*8d67ca89SAndroid Build Coastguard Worker MappedFileFragment strtab_fragment_; 119*8d67ca89SAndroid Build Coastguard Worker const char* strtab_; 120*8d67ca89SAndroid Build Coastguard Worker size_t strtab_size_; 121*8d67ca89SAndroid Build Coastguard Worker 122*8d67ca89SAndroid Build Coastguard Worker // First page of reserved address space. 123*8d67ca89SAndroid Build Coastguard Worker void* load_start_; 124*8d67ca89SAndroid Build Coastguard Worker // Size in bytes of reserved address space. 125*8d67ca89SAndroid Build Coastguard Worker size_t load_size_; 126*8d67ca89SAndroid Build Coastguard Worker // First page of inaccessible gap mapping reserved for this DSO. 127*8d67ca89SAndroid Build Coastguard Worker void* gap_start_; 128*8d67ca89SAndroid Build Coastguard Worker // Size in bytes of the gap mapping. 129*8d67ca89SAndroid Build Coastguard Worker size_t gap_size_; 130*8d67ca89SAndroid Build Coastguard Worker // Load bias. 131*8d67ca89SAndroid Build Coastguard Worker ElfW(Addr) load_bias_; 132*8d67ca89SAndroid Build Coastguard Worker 133*8d67ca89SAndroid Build Coastguard Worker // Loaded phdr. 134*8d67ca89SAndroid Build Coastguard Worker const ElfW(Phdr)* loaded_phdr_; 135*8d67ca89SAndroid Build Coastguard Worker 136*8d67ca89SAndroid Build Coastguard Worker // Is map owned by the caller 137*8d67ca89SAndroid Build Coastguard Worker bool mapped_by_caller_; 138*8d67ca89SAndroid Build Coastguard Worker 139*8d67ca89SAndroid Build Coastguard Worker // Pad gaps between segments when memory mapping? 140*8d67ca89SAndroid Build Coastguard Worker bool should_pad_segments_ = false; 141*8d67ca89SAndroid Build Coastguard Worker 142*8d67ca89SAndroid Build Coastguard Worker // Use app compat mode when loading 4KiB max-page-size ELFs on 16KiB page-size devices? 143*8d67ca89SAndroid Build Coastguard Worker bool should_use_16kib_app_compat_ = false; 144*8d67ca89SAndroid Build Coastguard Worker 145*8d67ca89SAndroid Build Coastguard Worker // RELRO region for 16KiB compat loading 146*8d67ca89SAndroid Build Coastguard Worker ElfW(Addr) compat_relro_start_ = 0; 147*8d67ca89SAndroid Build Coastguard Worker ElfW(Addr) compat_relro_size_ = 0; 148*8d67ca89SAndroid Build Coastguard Worker 149*8d67ca89SAndroid Build Coastguard Worker // Only used by AArch64 at the moment. 150*8d67ca89SAndroid Build Coastguard Worker GnuPropertySection note_gnu_property_ __unused; 151*8d67ca89SAndroid Build Coastguard Worker }; 152*8d67ca89SAndroid Build Coastguard Worker 153*8d67ca89SAndroid Build Coastguard Worker size_t phdr_table_get_load_size(const ElfW(Phdr)* phdr_table, size_t phdr_count, 154*8d67ca89SAndroid Build Coastguard Worker ElfW(Addr)* min_vaddr = nullptr, ElfW(Addr)* max_vaddr = nullptr); 155*8d67ca89SAndroid Build Coastguard Worker 156*8d67ca89SAndroid Build Coastguard Worker size_t phdr_table_get_maximum_alignment(const ElfW(Phdr)* phdr_table, size_t phdr_count); 157*8d67ca89SAndroid Build Coastguard Worker size_t phdr_table_get_minimum_alignment(const ElfW(Phdr)* phdr_table, size_t phdr_count); 158*8d67ca89SAndroid Build Coastguard Worker 159*8d67ca89SAndroid Build Coastguard Worker int phdr_table_protect_segments(const ElfW(Phdr)* phdr_table, size_t phdr_count, 160*8d67ca89SAndroid Build Coastguard Worker ElfW(Addr) load_bias, bool should_pad_segments, 161*8d67ca89SAndroid Build Coastguard Worker bool should_use_16kib_app_compat, 162*8d67ca89SAndroid Build Coastguard Worker const GnuPropertySection* prop = nullptr); 163*8d67ca89SAndroid Build Coastguard Worker 164*8d67ca89SAndroid Build Coastguard Worker int phdr_table_unprotect_segments(const ElfW(Phdr)* phdr_table, size_t phdr_count, 165*8d67ca89SAndroid Build Coastguard Worker ElfW(Addr) load_bias, bool should_pad_segments, 166*8d67ca89SAndroid Build Coastguard Worker bool should_use_16kib_app_compat); 167*8d67ca89SAndroid Build Coastguard Worker 168*8d67ca89SAndroid Build Coastguard Worker int phdr_table_protect_gnu_relro(const ElfW(Phdr)* phdr_table, size_t phdr_count, 169*8d67ca89SAndroid Build Coastguard Worker ElfW(Addr) load_bias, bool should_pad_segments, 170*8d67ca89SAndroid Build Coastguard Worker bool should_use_16kib_app_compat); 171*8d67ca89SAndroid Build Coastguard Worker 172*8d67ca89SAndroid Build Coastguard Worker int phdr_table_protect_gnu_relro_16kib_compat(ElfW(Addr) start, ElfW(Addr) size); 173*8d67ca89SAndroid Build Coastguard Worker 174*8d67ca89SAndroid Build Coastguard Worker int phdr_table_serialize_gnu_relro(const ElfW(Phdr)* phdr_table, size_t phdr_count, 175*8d67ca89SAndroid Build Coastguard Worker ElfW(Addr) load_bias, int fd, size_t* file_offset); 176*8d67ca89SAndroid Build Coastguard Worker 177*8d67ca89SAndroid Build Coastguard Worker int phdr_table_map_gnu_relro(const ElfW(Phdr)* phdr_table, size_t phdr_count, 178*8d67ca89SAndroid Build Coastguard Worker ElfW(Addr) load_bias, int fd, size_t* file_offset); 179*8d67ca89SAndroid Build Coastguard Worker 180*8d67ca89SAndroid Build Coastguard Worker #if defined(__arm__) 181*8d67ca89SAndroid Build Coastguard Worker int phdr_table_get_arm_exidx(const ElfW(Phdr)* phdr_table, size_t phdr_count, ElfW(Addr) load_bias, 182*8d67ca89SAndroid Build Coastguard Worker ElfW(Addr)** arm_exidx, size_t* arm_exidix_count); 183*8d67ca89SAndroid Build Coastguard Worker #endif 184*8d67ca89SAndroid Build Coastguard Worker 185*8d67ca89SAndroid Build Coastguard Worker void phdr_table_get_dynamic_section(const ElfW(Phdr)* phdr_table, size_t phdr_count, 186*8d67ca89SAndroid Build Coastguard Worker ElfW(Addr) load_bias, ElfW(Dyn)** dynamic, 187*8d67ca89SAndroid Build Coastguard Worker ElfW(Word)* dynamic_flags); 188*8d67ca89SAndroid Build Coastguard Worker 189*8d67ca89SAndroid Build Coastguard Worker const char* phdr_table_get_interpreter_name(const ElfW(Phdr)* phdr_table, size_t phdr_count, 190*8d67ca89SAndroid Build Coastguard Worker ElfW(Addr) load_bias); 191*8d67ca89SAndroid Build Coastguard Worker 192*8d67ca89SAndroid Build Coastguard Worker bool page_size_migration_supported(); 193*8d67ca89SAndroid Build Coastguard Worker 194*8d67ca89SAndroid Build Coastguard Worker int remap_memtag_globals_segments(const ElfW(Phdr) * phdr_table, size_t phdr_count, 195*8d67ca89SAndroid Build Coastguard Worker ElfW(Addr) load_bias); 196*8d67ca89SAndroid Build Coastguard Worker 197*8d67ca89SAndroid Build Coastguard Worker void protect_memtag_globals_ro_segments(const ElfW(Phdr) * phdr_table, size_t phdr_count, 198*8d67ca89SAndroid Build Coastguard Worker ElfW(Addr) load_bias); 199*8d67ca89SAndroid Build Coastguard Worker 200*8d67ca89SAndroid Build Coastguard Worker void name_memtag_globals_segments(const ElfW(Phdr) * phdr_table, size_t phdr_count, 201*8d67ca89SAndroid Build Coastguard Worker ElfW(Addr) load_bias, const char* soname, 202*8d67ca89SAndroid Build Coastguard Worker std::list<std::string>* vma_names); 203