blob: f7b1caf895abdcb7c54bdf5aa4f93ebe887f4cd4 [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"
Dmitriy Ivanovcf1cbbe2015-10-19 16:57:46 -070039#include "linker_mapped_file_fragment.h"
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +020040
Elliott Hughes650be4e2013-03-05 18:47:58 -080041class ElfReader {
42 public:
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -070043 ElfReader();
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +020044
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -070045 bool Read(const char* name, int fd, off64_t file_offset, off64_t file_size);
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000046 bool Load(const android_dlextinfo* extinfo);
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +020047
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -070048 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;
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +020056
Elliott Hughes650be4e2013-03-05 18:47:58 -080057 private:
58 bool ReadElfHeader();
59 bool VerifyElfHeader();
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -070060 bool ReadProgramHeaders();
61 bool ReadSectionHeaders();
62 bool ReadDynamicSection();
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000063 bool ReserveAddressSpace(const android_dlextinfo* extinfo);
Elliott Hughes650be4e2013-03-05 18:47:58 -080064 bool LoadSegments();
65 bool FindPhdr();
Elliott Hughes0266ae52014-02-10 17:46:57 -080066 bool CheckPhdr(ElfW(Addr));
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +020067
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -070068 bool did_read_;
69 bool did_load_;
70 std::string name_;
Elliott Hughes650be4e2013-03-05 18:47:58 -080071 int fd_;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070072 off64_t file_offset_;
Dmitriy Ivanov3f987f52015-06-25 15:51:41 -070073 off64_t file_size_;
Elliott Hughes650be4e2013-03-05 18:47:58 -080074
Elliott Hughes0266ae52014-02-10 17:46:57 -080075 ElfW(Ehdr) header_;
Elliott Hughes650be4e2013-03-05 18:47:58 -080076 size_t phdr_num_;
77
Dmitriy Ivanovcf1cbbe2015-10-19 16:57:46 -070078 MappedFileFragment phdr_fragment_;
79 const ElfW(Phdr)* phdr_table_;
Elliott Hughes650be4e2013-03-05 18:47:58 -080080
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -070081 MappedFileFragment shdr_fragment_;
82 const ElfW(Shdr)* shdr_table_;
83 size_t shdr_num_;
84
85 MappedFileFragment dynamic_fragment_;
86 const ElfW(Dyn)* dynamic_;
87
88 MappedFileFragment strtab_fragment_;
89 const char* strtab_;
90 size_t strtab_size_;
91
Elliott Hughes650be4e2013-03-05 18:47:58 -080092 // First page of reserved address space.
93 void* load_start_;
94 // Size in bytes of reserved address space.
Elliott Hughesc00f2cb2013-10-04 17:01:33 -070095 size_t load_size_;
Elliott Hughes650be4e2013-03-05 18:47:58 -080096 // Load bias.
Elliott Hughes0266ae52014-02-10 17:46:57 -080097 ElfW(Addr) load_bias_;
Elliott Hughes650be4e2013-03-05 18:47:58 -080098
99 // Loaded phdr.
Elliott Hughes0266ae52014-02-10 17:46:57 -0800100 const ElfW(Phdr)* loaded_phdr_;
Elliott Hughes650be4e2013-03-05 18:47:58 -0800101};
102
Elliott Hughes0266ae52014-02-10 17:46:57 -0800103size_t phdr_table_get_load_size(const ElfW(Phdr)* phdr_table, size_t phdr_count,
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700104 ElfW(Addr)* min_vaddr = nullptr, ElfW(Addr)* max_vaddr = nullptr);
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +0200105
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +0000106int phdr_table_protect_segments(const ElfW(Phdr)* phdr_table,
107 size_t phdr_count, ElfW(Addr) load_bias);
108
109int phdr_table_unprotect_segments(const ElfW(Phdr)* phdr_table, size_t phdr_count,
110 ElfW(Addr) load_bias);
111
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -0700112int phdr_table_protect_gnu_relro(const ElfW(Phdr)* phdr_table, size_t phdr_count,
113 ElfW(Addr) load_bias);
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +0200114
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -0700115int phdr_table_serialize_gnu_relro(const ElfW(Phdr)* phdr_table, size_t phdr_count,
116 ElfW(Addr) load_bias, int fd);
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000117
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -0700118int phdr_table_map_gnu_relro(const ElfW(Phdr)* phdr_table, size_t phdr_count,
119 ElfW(Addr) load_bias, int fd);
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +0200120
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700121#if defined(__arm__)
Elliott Hughes0266ae52014-02-10 17:46:57 -0800122int phdr_table_get_arm_exidx(const ElfW(Phdr)* phdr_table, size_t phdr_count, ElfW(Addr) load_bias,
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -0800123 ElfW(Addr)** arm_exidx, size_t* arm_exidix_count);
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +0200124#endif
125
Elliott Hughes0266ae52014-02-10 17:46:57 -0800126void phdr_table_get_dynamic_section(const ElfW(Phdr)* phdr_table, size_t phdr_count,
Ningsheng Jiane93be992014-09-16 15:22:10 +0800127 ElfW(Addr) load_bias, ElfW(Dyn)** dynamic,
128 ElfW(Word)* dynamic_flags);
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +0200129
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700130const char* phdr_table_get_interpreter_name(const ElfW(Phdr) * phdr_table, size_t phdr_count,
131 ElfW(Addr) load_bias);
132
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +0200133#endif /* LINKER_PHDR_H */