linker_phdr.h revision 650be4e584eeab3591b9e273bfd6d169eea60853
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: 42650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes ElfReader(const char* name, int fd); 43650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes ~ElfReader(); 44c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner 45650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes bool Load(); 46c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner 47650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes size_t phdr_count() { return phdr_num_; } 48650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes Elf32_Addr load_start() { return reinterpret_cast<Elf32_Addr>(load_start_); } 49650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes Elf32_Addr load_size() { return load_size_; } 50650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes Elf32_Addr load_bias() { return load_bias_; } 51650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes const Elf32_Phdr* loaded_phdr() { return loaded_phdr_; } 52c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner 53650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes private: 54650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes bool ReadElfHeader(); 55650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes bool VerifyElfHeader(); 56650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes bool ReadProgramHeader(); 57650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes bool ReserveAddressSpace(); 58650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes bool LoadSegments(); 59650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes bool FindPhdr(); 60650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes bool CheckPhdr(Elf32_Addr); 61c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner 62650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes const char* name_; 63650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes int fd_; 64650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes 65650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes Elf32_Ehdr header_; 66650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes size_t phdr_num_; 67650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes 68650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes void* phdr_mmap_; 69650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes Elf32_Phdr* phdr_table_; 70650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes Elf32_Addr phdr_size_; 71650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes 72650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes // First page of reserved address space. 73650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes void* load_start_; 74650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes // Size in bytes of reserved address space. 75650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes Elf32_Addr load_size_; 76650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes // Load bias. 77650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes Elf32_Addr load_bias_; 78650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes 79650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes // Loaded phdr. 80650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes const Elf32_Phdr* loaded_phdr_; 81650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes}; 82650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes 83650be4e584eeab3591b9e273bfd6d169eea60853Elliott HughesElf32_Addr phdr_table_get_load_size(const Elf32_Phdr* phdr, size_t phnum); 84c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner 85c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turnerint 86c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turnerphdr_table_protect_segments(const Elf32_Phdr* phdr_table, 87c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner int phdr_count, 88c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner Elf32_Addr load_bias); 89c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner 90c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turnerint 91c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turnerphdr_table_unprotect_segments(const Elf32_Phdr* phdr_table, 92c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner int phdr_count, 93c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner Elf32_Addr load_bias); 94c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner 95c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turnerint 96c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turnerphdr_table_protect_gnu_relro(const Elf32_Phdr* phdr_table, 97c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner int phdr_count, 98c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner Elf32_Addr load_bias); 99c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner 100c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner 101c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner#ifdef ANDROID_ARM_LINKER 102c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turnerint 103c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turnerphdr_table_get_arm_exidx(const Elf32_Phdr* phdr_table, 104c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner int phdr_count, 105c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner Elf32_Addr load_bias, 106c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner Elf32_Addr** arm_exidx, 107c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner unsigned* arm_exidix_count); 108c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner#endif 109c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner 11012c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvelvoid 111c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turnerphdr_table_get_dynamic_section(const Elf32_Phdr* phdr_table, 112c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner int phdr_count, 11312c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel Elf32_Addr load_bias, 114d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom Elf32_Dyn** dynamic, 115cf23905a4bcc7bfdd109be5b6d69ad06877aa217Chris Dearman size_t* dynamic_count, 116cf23905a4bcc7bfdd109be5b6d69ad06877aa217Chris Dearman Elf32_Word* dynamic_flags); 117c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner 118c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner#endif /* LINKER_PHDR_H */ 119