Home | History | Annotate | Download | only in linker
      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 #include "linker_mapped_file_fragment.h"
     40 
     41 class ElfReader {
     42  public:
     43   ElfReader();
     44 
     45   bool Read(const char* name, int fd, off64_t file_offset, off64_t file_size);
     46   bool Load(const android_dlextinfo* extinfo);
     47 
     48   const char* name() const { return name_.c_str(); }
     49   size_t phdr_count() const { return phdr_num_; }
     50   ElfW(Addr) load_start() const { return reinterpret_cast<ElfW(Addr)>(load_start_); }
     51   size_t load_size() const { return load_size_; }
     52   ElfW(Addr) load_bias() const { return load_bias_; }
     53   const ElfW(Phdr)* loaded_phdr() const { return loaded_phdr_; }
     54   const ElfW(Dyn)* dynamic() const { return dynamic_; }
     55   const char* get_string(ElfW(Word) index) const;
     56   bool is_mapped_by_caller() const { return mapped_by_caller_; }
     57 
     58  private:
     59   bool ReadElfHeader();
     60   bool VerifyElfHeader();
     61   bool ReadProgramHeaders();
     62   bool ReadSectionHeaders();
     63   bool ReadDynamicSection();
     64   bool ReserveAddressSpace(const android_dlextinfo* extinfo);
     65   bool LoadSegments();
     66   bool FindPhdr();
     67   bool CheckPhdr(ElfW(Addr));
     68   bool CheckFileRange(ElfW(Addr) offset, size_t size, size_t alignment);
     69 
     70   bool did_read_;
     71   bool did_load_;
     72   std::string name_;
     73   int fd_;
     74   off64_t file_offset_;
     75   off64_t file_size_;
     76 
     77   ElfW(Ehdr) header_;
     78   size_t phdr_num_;
     79 
     80   MappedFileFragment phdr_fragment_;
     81   const ElfW(Phdr)* phdr_table_;
     82 
     83   MappedFileFragment shdr_fragment_;
     84   const ElfW(Shdr)* shdr_table_;
     85   size_t shdr_num_;
     86 
     87   MappedFileFragment dynamic_fragment_;
     88   const ElfW(Dyn)* dynamic_;
     89 
     90   MappedFileFragment strtab_fragment_;
     91   const char* strtab_;
     92   size_t strtab_size_;
     93 
     94   // First page of reserved address space.
     95   void* load_start_;
     96   // Size in bytes of reserved address space.
     97   size_t load_size_;
     98   // Load bias.
     99   ElfW(Addr) load_bias_;
    100 
    101   // Loaded phdr.
    102   const ElfW(Phdr)* loaded_phdr_;
    103 
    104   // Is map owned by the caller
    105   bool mapped_by_caller_;
    106 };
    107 
    108 size_t phdr_table_get_load_size(const ElfW(Phdr)* phdr_table, size_t phdr_count,
    109                                 ElfW(Addr)* min_vaddr = nullptr, ElfW(Addr)* max_vaddr = nullptr);
    110 
    111 int phdr_table_protect_segments(const ElfW(Phdr)* phdr_table,
    112                                 size_t phdr_count, ElfW(Addr) load_bias);
    113 
    114 int phdr_table_unprotect_segments(const ElfW(Phdr)* phdr_table, size_t phdr_count,
    115                                   ElfW(Addr) load_bias);
    116 
    117 int phdr_table_protect_gnu_relro(const ElfW(Phdr)* phdr_table, size_t phdr_count,
    118                                  ElfW(Addr) load_bias);
    119 
    120 int phdr_table_serialize_gnu_relro(const ElfW(Phdr)* phdr_table, size_t phdr_count,
    121                                    ElfW(Addr) load_bias, int fd);
    122 
    123 int phdr_table_map_gnu_relro(const ElfW(Phdr)* phdr_table, size_t phdr_count,
    124                              ElfW(Addr) load_bias, int fd);
    125 
    126 #if defined(__arm__)
    127 int phdr_table_get_arm_exidx(const ElfW(Phdr)* phdr_table, size_t phdr_count, ElfW(Addr) load_bias,
    128                              ElfW(Addr)** arm_exidx, size_t* arm_exidix_count);
    129 #endif
    130 
    131 void phdr_table_get_dynamic_section(const ElfW(Phdr)* phdr_table, size_t phdr_count,
    132                                     ElfW(Addr) load_bias, ElfW(Dyn)** dynamic,
    133                                     ElfW(Word)* dynamic_flags);
    134 
    135 const char* phdr_table_get_interpreter_name(const ElfW(Phdr) * phdr_table, size_t phdr_count,
    136                                             ElfW(Addr) load_bias);
    137 
    138 #endif /* LINKER_PHDR_H */
    139