blob: db15ac9696c57005e63d62bdf510e4ac7e53c1bd [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_; }
Elliott Hughesc6200592013-09-30 18:43:46 -070048 Elf_Addr load_start() { return reinterpret_cast<Elf_Addr>(load_start_); }
Elliott Hughesc00f2cb2013-10-04 17:01:33 -070049 size_t load_size() { return load_size_; }
Elliott Hughesc6200592013-09-30 18:43:46 -070050 Elf_Addr load_bias() { return load_bias_; }
51 const Elf_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();
Elliott Hughesc6200592013-09-30 18:43:46 -070060 bool CheckPhdr(Elf_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
Elliott Hughesc6200592013-09-30 18:43:46 -070065 Elf_Ehdr header_;
Elliott Hughes650be4e2013-03-05 18:47:58 -080066 size_t phdr_num_;
67
68 void* phdr_mmap_;
Elliott Hughesc6200592013-09-30 18:43:46 -070069 Elf_Phdr* phdr_table_;
70 Elf_Addr phdr_size_;
Elliott Hughes650be4e2013-03-05 18:47:58 -080071
72 // First page of reserved address space.
73 void* load_start_;
74 // Size in bytes of reserved address space.
Elliott Hughesc00f2cb2013-10-04 17:01:33 -070075 size_t load_size_;
Elliott Hughes650be4e2013-03-05 18:47:58 -080076 // Load bias.
Elliott Hughesc6200592013-09-30 18:43:46 -070077 Elf_Addr load_bias_;
Elliott Hughes650be4e2013-03-05 18:47:58 -080078
79 // Loaded phdr.
Elliott Hughesc6200592013-09-30 18:43:46 -070080 const Elf_Phdr* loaded_phdr_;
Elliott Hughes650be4e2013-03-05 18:47:58 -080081};
82
Elliott Hughesc6200592013-09-30 18:43:46 -070083size_t phdr_table_get_load_size(const Elf_Phdr* phdr_table, size_t phdr_count,
84 Elf_Addr* min_vaddr = NULL, Elf_Addr* max_vaddr = NULL);
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +020085
Elliott Hughesc6200592013-09-30 18:43:46 -070086int phdr_table_protect_segments(const Elf_Phdr* phdr_table, size_t phdr_count, Elf_Addr load_bias);
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +020087
Elliott Hughesc6200592013-09-30 18:43:46 -070088int phdr_table_unprotect_segments(const Elf_Phdr* phdr_table, size_t phdr_count, Elf_Addr load_bias);
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +020089
Elliott Hughesc6200592013-09-30 18:43:46 -070090int phdr_table_protect_gnu_relro(const Elf_Phdr* phdr_table, size_t phdr_count, Elf_Addr load_bias);
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +020091
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +020092
Elliott Hughes4eeb1f12013-10-25 17:38:02 -070093#if defined(__arm__)
Elliott Hughesc6200592013-09-30 18:43:46 -070094int phdr_table_get_arm_exidx(const Elf_Phdr* phdr_table, size_t phdr_count, Elf_Addr load_bias,
95 Elf_Addr** arm_exidx, unsigned* arm_exidix_count);
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +020096#endif
97
Elliott Hughesc6200592013-09-30 18:43:46 -070098void phdr_table_get_dynamic_section(const Elf_Phdr* phdr_table, size_t phdr_count,
99 Elf_Addr load_bias,
100 Elf_Dyn** dynamic, size_t* dynamic_count, Elf_Word* dynamic_flags);
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +0200101
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +0200102#endif /* LINKER_PHDR_H */