1c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner/*
2c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner * Copyright (C) 2012 The Android Open Source Project
3c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner * All rights reserved.
4c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner *
5c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner * Redistribution and use in source and binary forms, with or without
6c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner * modification, are permitted provided that the following conditions
7c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner * are met:
8c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner *  * Redistributions of source code must retain the above copyright
9c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner *    notice, this list of conditions and the following disclaimer.
10c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner *  * Redistributions in binary form must reproduce the above copyright
11c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner *    notice, this list of conditions and the following disclaimer in
12c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner *    the documentation and/or other materials provided with the
13c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner *    distribution.
14c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner *
15c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner * SUCH DAMAGE.
27c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner */
28c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner#ifndef LINKER_PHDR_H
29c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner#define LINKER_PHDR_H
30c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner
31c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner/* Declarations related to the ELF program header table and segments.
32c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner *
33c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner * The design goal is to provide an API that is as close as possible
34c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner * to the ELF spec, and does not depend on linker-specific data
35c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner * structures (e.g. the exact layout of struct soinfo).
36c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner */
37c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner
38c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner#include "linker.h"
39c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner
40650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughesclass ElfReader {
41650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes public:
4206700b2b5e92a365505b15d0d05e2d5b0706c511Dmitriy Ivanov  ElfReader(const char* name, int fd, off64_t file_offset, off64_t file_size);
43650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes  ~ElfReader();
44c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner
4512bbb9164578b6512b8b07a3fb093858244b7c7bTorne (Richard Coles)  bool Load(const android_dlextinfo* extinfo);
46c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner
47650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes  size_t phdr_count() { return phdr_num_; }
480266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Addr) load_start() { return reinterpret_cast<ElfW(Addr)>(load_start_); }
49c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes  size_t load_size() { return load_size_; }
500266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Addr) load_bias() { return load_bias_; }
510266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  const ElfW(Phdr)* loaded_phdr() { return loaded_phdr_; }
52c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner
53650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes private:
54650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes  bool ReadElfHeader();
55650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes  bool VerifyElfHeader();
56650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes  bool ReadProgramHeader();
5712bbb9164578b6512b8b07a3fb093858244b7c7bTorne (Richard Coles)  bool ReserveAddressSpace(const android_dlextinfo* extinfo);
58650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes  bool LoadSegments();
59650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes  bool FindPhdr();
600266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  bool CheckPhdr(ElfW(Addr));
61c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner
62650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes  const char* name_;
63650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes  int fd_;
6407e5bc152d8a3ad4c50808bb86f3c0f2c5e2f514Dmitriy Ivanov  off64_t file_offset_;
6506700b2b5e92a365505b15d0d05e2d5b0706c511Dmitriy Ivanov  off64_t file_size_;
66650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes
670266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Ehdr) header_;
68650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes  size_t phdr_num_;
69650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes
70650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes  void* phdr_mmap_;
710266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Phdr)* phdr_table_;
720266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Addr) phdr_size_;
73650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes
74650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes  // First page of reserved address space.
75650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes  void* load_start_;
76650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes  // Size in bytes of reserved address space.
77c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes  size_t load_size_;
78650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes  // Load bias.
790266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Addr) load_bias_;
80650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes
81650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes  // Loaded phdr.
820266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  const ElfW(Phdr)* loaded_phdr_;
83650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes};
84650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes
850266ae5f884d72da58f33a072e865ba131234a5eElliott Hughessize_t phdr_table_get_load_size(const ElfW(Phdr)* phdr_table, size_t phdr_count,
86851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov                                ElfW(Addr)* min_vaddr = nullptr, ElfW(Addr)* max_vaddr = nullptr);
87c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner
8856be6ed9e4ac99fdd920090ee89c57e3cf55e885Dimitry Ivanovint phdr_table_protect_segments(const ElfW(Phdr)* phdr_table,
8956be6ed9e4ac99fdd920090ee89c57e3cf55e885Dimitry Ivanov                                size_t phdr_count, ElfW(Addr) load_bias);
9056be6ed9e4ac99fdd920090ee89c57e3cf55e885Dimitry Ivanov
9156be6ed9e4ac99fdd920090ee89c57e3cf55e885Dimitry Ivanovint phdr_table_unprotect_segments(const ElfW(Phdr)* phdr_table, size_t phdr_count,
9256be6ed9e4ac99fdd920090ee89c57e3cf55e885Dimitry Ivanov                                  ElfW(Addr) load_bias);
9356be6ed9e4ac99fdd920090ee89c57e3cf55e885Dimitry Ivanov
9420d89cb5b0d5eb7546a8fe8da44bbd91564dbddaDmitriy Ivanovint phdr_table_protect_gnu_relro(const ElfW(Phdr)* phdr_table, size_t phdr_count,
9520d89cb5b0d5eb7546a8fe8da44bbd91564dbddaDmitriy Ivanov                                 ElfW(Addr) load_bias);
96c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner
9720d89cb5b0d5eb7546a8fe8da44bbd91564dbddaDmitriy Ivanovint phdr_table_serialize_gnu_relro(const ElfW(Phdr)* phdr_table, size_t phdr_count,
9820d89cb5b0d5eb7546a8fe8da44bbd91564dbddaDmitriy Ivanov                                   ElfW(Addr) load_bias, int fd);
99183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)
10020d89cb5b0d5eb7546a8fe8da44bbd91564dbddaDmitriy Ivanovint phdr_table_map_gnu_relro(const ElfW(Phdr)* phdr_table, size_t phdr_count,
10120d89cb5b0d5eb7546a8fe8da44bbd91564dbddaDmitriy Ivanov                             ElfW(Addr) load_bias, int fd);
102c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner
1034eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__arm__)
1040266ae5f884d72da58f33a072e865ba131234a5eElliott Hughesint phdr_table_get_arm_exidx(const ElfW(Phdr)* phdr_table, size_t phdr_count, ElfW(Addr) load_bias,
1051649e7ee5b9bb4122be67903d55c4c8cc0549bbaDmitriy Ivanov                             ElfW(Addr)** arm_exidx, size_t* arm_exidix_count);
106c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner#endif
107c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner
1080266ae5f884d72da58f33a072e865ba131234a5eElliott Hughesvoid phdr_table_get_dynamic_section(const ElfW(Phdr)* phdr_table, size_t phdr_count,
109e93be99da0614ff38cbf8b2bb0624ff1dc79b8d0Ningsheng Jian                                    ElfW(Addr) load_bias, ElfW(Dyn)** dynamic,
110e93be99da0614ff38cbf8b2bb0624ff1dc79b8d0Ningsheng Jian                                    ElfW(Word)* dynamic_flags);
111c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner
112c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner#endif /* LINKER_PHDR_H */
113