blob: 992d95e56a0e77616945153f5c9354f593bd22b3 [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:
42 ElfReader(const char* name, int fd);
43 ~ElfReader();
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +020044
Elliott Hughes650be4e2013-03-05 18:47:58 -080045 bool Load();
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +020046
Elliott Hughes650be4e2013-03-05 18:47:58 -080047 size_t phdr_count() { return phdr_num_; }
48 Elf32_Addr load_start() { return reinterpret_cast<Elf32_Addr>(load_start_); }
49 Elf32_Addr load_size() { return load_size_; }
50 Elf32_Addr load_bias() { return load_bias_; }
51 const Elf32_Phdr* loaded_phdr() { return loaded_phdr_; }
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +020052
Elliott Hughes650be4e2013-03-05 18:47:58 -080053 private:
54 bool ReadElfHeader();
55 bool VerifyElfHeader();
56 bool ReadProgramHeader();
57 bool ReserveAddressSpace();
58 bool LoadSegments();
59 bool FindPhdr();
60 bool CheckPhdr(Elf32_Addr);
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +020061
Elliott Hughes650be4e2013-03-05 18:47:58 -080062 const char* name_;
63 int fd_;
64
65 Elf32_Ehdr header_;
66 size_t phdr_num_;
67
68 void* phdr_mmap_;
69 Elf32_Phdr* phdr_table_;
70 Elf32_Addr phdr_size_;
71
72 // First page of reserved address space.
73 void* load_start_;
74 // Size in bytes of reserved address space.
75 Elf32_Addr load_size_;
76 // Load bias.
77 Elf32_Addr load_bias_;
78
79 // Loaded phdr.
80 const Elf32_Phdr* loaded_phdr_;
81};
82
Brian Carlstrome7dffe12013-01-10 16:39:58 -080083size_t
84phdr_table_get_load_size(const Elf32_Phdr* phdr_table,
85 size_t phdr_count,
86 Elf32_Addr* min_vaddr = NULL,
87 Elf32_Addr* max_vaddr = NULL);
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +020088
89int
90phdr_table_protect_segments(const Elf32_Phdr* phdr_table,
91 int phdr_count,
92 Elf32_Addr load_bias);
93
94int
95phdr_table_unprotect_segments(const Elf32_Phdr* phdr_table,
96 int phdr_count,
97 Elf32_Addr load_bias);
98
99int
100phdr_table_protect_gnu_relro(const Elf32_Phdr* phdr_table,
101 int phdr_count,
102 Elf32_Addr load_bias);
103
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +0200104
105#ifdef ANDROID_ARM_LINKER
106int
107phdr_table_get_arm_exidx(const Elf32_Phdr* phdr_table,
108 int phdr_count,
109 Elf32_Addr load_bias,
110 Elf32_Addr** arm_exidx,
111 unsigned* arm_exidix_count);
112#endif
113
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200114void
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +0200115phdr_table_get_dynamic_section(const Elf32_Phdr* phdr_table,
116 int phdr_count,
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +0200117 Elf32_Addr load_bias,
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800118 Elf32_Dyn** dynamic,
Chris Dearmancf239052013-01-11 15:32:20 -0800119 size_t* dynamic_count,
120 Elf32_Word* dynamic_flags);
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +0200121
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +0200122#endif /* LINKER_PHDR_H */