blob: ee50e8f39fd3c31972746a1df7619b6c79e50834 [file] [log] [blame]
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +02001/*
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
Elliott Hughes650be4e2013-03-05 18:47:58 -080040class ElfReader {
41 public:
Dmitriy Ivanovde017802014-10-03 17:52:44 -070042 ElfReader(const char* name, int fd, off64_t file_offset);
Elliott Hughes650be4e2013-03-05 18:47:58 -080043 ~ElfReader();
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +020044
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000045 bool Load(const android_dlextinfo* extinfo);
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +020046
Elliott Hughes650be4e2013-03-05 18:47:58 -080047 size_t phdr_count() { return phdr_num_; }
Elliott Hughes0266ae52014-02-10 17:46:57 -080048 ElfW(Addr) load_start() { return reinterpret_cast<ElfW(Addr)>(load_start_); }
Elliott Hughesc00f2cb2013-10-04 17:01:33 -070049 size_t load_size() { return load_size_; }
Elliott Hughes0266ae52014-02-10 17:46:57 -080050 ElfW(Addr) load_bias() { return load_bias_; }
Pawit Pornkitprasanbdfedaa2012-11-23 12:27:25 +070051#ifdef ENABLE_PRELINK_SUPPORT
52 Elf32_Addr required_base() { return required_base_; }
53#endif
Elliott Hughes0266ae52014-02-10 17:46:57 -080054 const ElfW(Phdr)* loaded_phdr() { return loaded_phdr_; }
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +020055
Elliott Hughes650be4e2013-03-05 18:47:58 -080056 private:
57 bool ReadElfHeader();
58 bool VerifyElfHeader();
59 bool ReadProgramHeader();
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000060 bool ReserveAddressSpace(const android_dlextinfo* extinfo);
Elliott Hughes650be4e2013-03-05 18:47:58 -080061 bool LoadSegments();
62 bool FindPhdr();
Elliott Hughes0266ae52014-02-10 17:46:57 -080063 bool CheckPhdr(ElfW(Addr));
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +020064
Elliott Hughes650be4e2013-03-05 18:47:58 -080065 const char* name_;
66 int fd_;
Dmitriy Ivanovde017802014-10-03 17:52:44 -070067 off64_t file_offset_;
Elliott Hughes650be4e2013-03-05 18:47:58 -080068
Elliott Hughes0266ae52014-02-10 17:46:57 -080069 ElfW(Ehdr) header_;
Elliott Hughes650be4e2013-03-05 18:47:58 -080070 size_t phdr_num_;
71
72 void* phdr_mmap_;
Elliott Hughes0266ae52014-02-10 17:46:57 -080073 ElfW(Phdr)* phdr_table_;
74 ElfW(Addr) phdr_size_;
Elliott Hughes650be4e2013-03-05 18:47:58 -080075
76 // First page of reserved address space.
77 void* load_start_;
78 // Size in bytes of reserved address space.
Elliott Hughesc00f2cb2013-10-04 17:01:33 -070079 size_t load_size_;
Elliott Hughes650be4e2013-03-05 18:47:58 -080080 // Load bias.
Elliott Hughes0266ae52014-02-10 17:46:57 -080081 ElfW(Addr) load_bias_;
Pawit Pornkitprasanbdfedaa2012-11-23 12:27:25 +070082#ifdef ENABLE_PRELINK_SUPPORT
83 // For prelinked libraries, mandatory load address of the first
84 // loadable segment. 0 otherwise.
85 Elf32_Addr required_base_;
86#endif
Elliott Hughes650be4e2013-03-05 18:47:58 -080087
88 // Loaded phdr.
Elliott Hughes0266ae52014-02-10 17:46:57 -080089 const ElfW(Phdr)* loaded_phdr_;
Elliott Hughes650be4e2013-03-05 18:47:58 -080090};
91
Elliott Hughes0266ae52014-02-10 17:46:57 -080092size_t phdr_table_get_load_size(const ElfW(Phdr)* phdr_table, size_t phdr_count,
Dmitriy Ivanovcfad7ae2014-08-29 12:02:36 -070093 ElfW(Addr)* min_vaddr = nullptr, ElfW(Addr)* max_vaddr = nullptr);
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +020094
Elliott Hughes0266ae52014-02-10 17:46:57 -080095int phdr_table_protect_segments(const ElfW(Phdr)* phdr_table, size_t phdr_count, ElfW(Addr) load_bias);
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +020096
Elliott Hughes0266ae52014-02-10 17:46:57 -080097int phdr_table_unprotect_segments(const ElfW(Phdr)* phdr_table, size_t phdr_count, ElfW(Addr) load_bias);
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +020098
Elliott Hughes0266ae52014-02-10 17:46:57 -080099int phdr_table_protect_gnu_relro(const ElfW(Phdr)* phdr_table, size_t phdr_count, ElfW(Addr) load_bias);
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +0200100
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000101int phdr_table_serialize_gnu_relro(const ElfW(Phdr)* phdr_table, size_t phdr_count, ElfW(Addr) load_bias,
102 int fd);
103
104int phdr_table_map_gnu_relro(const ElfW(Phdr)* phdr_table, size_t phdr_count, ElfW(Addr) load_bias,
105 int fd);
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +0200106
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700107#if defined(__arm__)
Elliott Hughes0266ae52014-02-10 17:46:57 -0800108int phdr_table_get_arm_exidx(const ElfW(Phdr)* phdr_table, size_t phdr_count, ElfW(Addr) load_bias,
109 ElfW(Addr)** arm_exidx, unsigned* arm_exidix_count);
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +0200110#endif
111
Elliott Hughes0266ae52014-02-10 17:46:57 -0800112void phdr_table_get_dynamic_section(const ElfW(Phdr)* phdr_table, size_t phdr_count,
Ningsheng Jian04f5f412014-09-16 15:22:10 +0800113 ElfW(Addr) load_bias, ElfW(Dyn)** dynamic,
114 ElfW(Word)* dynamic_flags);
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +0200115
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +0200116#endif /* LINKER_PHDR_H */