1 /* 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 */ 28 #ifndef LINKER_PHDR_H 29 #define LINKER_PHDR_H 30 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" 39 40 class ElfReader { 41 public: 42 ElfReader(const char* name, int fd, off64_t file_offset, off64_t file_size); 43 ~ElfReader(); 44 45 bool Load(const android_dlextinfo* extinfo); 46 phdr_count()47 size_t phdr_count() { return phdr_num_; } load_start()48 ElfW(Addr) load_start() { return reinterpret_cast<ElfW(Addr)>(load_start_); } load_size()49 size_t load_size() { return load_size_; } load_bias()50 ElfW(Addr) load_bias() { return load_bias_; } ElfW(Phdr)51 const ElfW(Phdr)* loaded_phdr() { return loaded_phdr_; } 52 53 private: 54 bool ReadElfHeader(); 55 bool VerifyElfHeader(); 56 bool ReadProgramHeader(); 57 bool ReserveAddressSpace(const android_dlextinfo* extinfo); 58 bool LoadSegments(); 59 bool FindPhdr(); 60 bool CheckPhdr(ElfW(Addr)); 61 62 const char* name_; 63 int fd_; 64 off64_t file_offset_; 65 off64_t file_size_; 66 67 ElfW(Ehdr) header_; 68 size_t phdr_num_; 69 70 void* phdr_mmap_; 71 ElfW(Phdr)* phdr_table_; 72 ElfW(Addr) phdr_size_; 73 74 // First page of reserved address space. 75 void* load_start_; 76 // Size in bytes of reserved address space. 77 size_t load_size_; 78 // Load bias. 79 ElfW(Addr) load_bias_; 80 81 // Loaded phdr. 82 const ElfW(Phdr)* loaded_phdr_; 83 }; 84 85 size_t phdr_table_get_load_size(const ElfW(Phdr)* phdr_table, size_t phdr_count, 86 ElfW(Addr)* min_vaddr = nullptr, ElfW(Addr)* max_vaddr = nullptr); 87 88 int phdr_table_protect_segments(const ElfW(Phdr)* phdr_table, 89 size_t phdr_count, ElfW(Addr) load_bias); 90 91 int phdr_table_unprotect_segments(const ElfW(Phdr)* phdr_table, size_t phdr_count, 92 ElfW(Addr) load_bias); 93 94 int phdr_table_protect_gnu_relro(const ElfW(Phdr)* phdr_table, size_t phdr_count, 95 ElfW(Addr) load_bias); 96 97 int phdr_table_serialize_gnu_relro(const ElfW(Phdr)* phdr_table, size_t phdr_count, 98 ElfW(Addr) load_bias, int fd); 99 100 int phdr_table_map_gnu_relro(const ElfW(Phdr)* phdr_table, size_t phdr_count, 101 ElfW(Addr) load_bias, int fd); 102 103 #if defined(__arm__) 104 int phdr_table_get_arm_exidx(const ElfW(Phdr)* phdr_table, size_t phdr_count, ElfW(Addr) load_bias, 105 ElfW(Addr)** arm_exidx, size_t* arm_exidix_count); 106 #endif 107 108 void phdr_table_get_dynamic_section(const ElfW(Phdr)* phdr_table, size_t phdr_count, 109 ElfW(Addr) load_bias, ElfW(Dyn)** dynamic, 110 ElfW(Word)* dynamic_flags); 111 112 #endif /* LINKER_PHDR_H */ 113