Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
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 are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#pragma once
28
29#include <AK/HashMap.h>
30#include <AK/OwnPtr.h>
31#include <AK/String.h>
32#include <LibBareMetal/Memory/VirtualAddress.h>
33#include <LibELF/exec_elf.h>
34
35class ELFImage {
36public:
37 explicit ELFImage(const u8*, size_t);
38 ~ELFImage();
39 void dump() const;
40 bool is_valid() const { return m_valid; }
41 bool parse();
42
43 bool is_within_image(const void* address, size_t size) const
44 {
45 if (address < m_buffer)
46 return false;
47 if (((const u8*)address + size) > m_buffer + m_size)
48 return false;
49 return true;
50 }
51
52 class Section;
53 class RelocationSection;
54 class Symbol;
55 class Relocation;
56
57 class Symbol {
58 public:
59 Symbol(const ELFImage& image, unsigned index, const Elf32_Sym& sym)
60 : m_image(image)
61 , m_sym(sym)
62 , m_index(index)
63 {
64 }
65
66 ~Symbol() {}
67
68 StringView name() const { return m_image.table_string(m_sym.st_name); }
69 unsigned section_index() const { return m_sym.st_shndx; }
70 unsigned value() const { return m_sym.st_value; }
71 unsigned size() const { return m_sym.st_size; }
72 unsigned index() const { return m_index; }
73 unsigned type() const { return ELF32_ST_TYPE(m_sym.st_info); }
74 unsigned bind() const { return ELF32_ST_BIND(m_sym.st_info); }
75 const Section section() const { return m_image.section(section_index()); }
76
77 private:
78 const ELFImage& m_image;
79 const Elf32_Sym& m_sym;
80 const unsigned m_index;
81 };
82
83 class ProgramHeader {
84 public:
85 ProgramHeader(const ELFImage& image, unsigned program_header_index)
86 : m_image(image)
87 , m_program_header(image.program_header_internal(program_header_index))
88 , m_program_header_index(program_header_index)
89 {
90 }
91 ~ProgramHeader() {}
92
93 unsigned index() const { return m_program_header_index; }
94 u32 type() const { return m_program_header.p_type; }
95 u32 flags() const { return m_program_header.p_flags; }
96 u32 offset() const { return m_program_header.p_offset; }
97 VirtualAddress vaddr() const { return VirtualAddress(m_program_header.p_vaddr); }
98 u32 size_in_memory() const { return m_program_header.p_memsz; }
99 u32 size_in_image() const { return m_program_header.p_filesz; }
100 u32 alignment() const { return m_program_header.p_align; }
101 bool is_readable() const { return flags() & PF_R; }
102 bool is_writable() const { return flags() & PF_W; }
103 bool is_executable() const { return flags() & PF_X; }
104 const char* raw_data() const { return m_image.raw_data(m_program_header.p_offset); }
105 Elf32_Phdr raw_header() const { return m_program_header; }
106
107 private:
108 const ELFImage& m_image;
109 const Elf32_Phdr& m_program_header;
110 unsigned m_program_header_index { 0 };
111 };
112
113 class Section {
114 public:
115 Section(const ELFImage& image, unsigned sectionIndex)
116 : m_image(image)
117 , m_section_header(image.section_header(sectionIndex))
118 , m_section_index(sectionIndex)
119 {
120 }
121 ~Section() {}
122
123 StringView name() const { return m_image.section_header_table_string(m_section_header.sh_name); }
124 unsigned type() const { return m_section_header.sh_type; }
125 unsigned offset() const { return m_section_header.sh_offset; }
126 unsigned size() const { return m_section_header.sh_size; }
127 unsigned entry_size() const { return m_section_header.sh_entsize; }
128 unsigned entry_count() const { return !entry_size() ? 0 : size() / entry_size(); }
129 u32 address() const { return m_section_header.sh_addr; }
130 const char* raw_data() const { return m_image.raw_data(m_section_header.sh_offset); }
131 bool is_undefined() const { return m_section_index == SHN_UNDEF; }
132 const RelocationSection relocations() const;
133 u32 flags() const { return m_section_header.sh_flags; }
134 bool is_writable() const { return flags() & SHF_WRITE; }
135 bool is_executable() const { return flags() & PF_X; }
136
137 protected:
138 friend class RelocationSection;
139 const ELFImage& m_image;
140 const Elf32_Shdr& m_section_header;
141 unsigned m_section_index;
142 };
143
144 class RelocationSection : public Section {
145 public:
146 RelocationSection(const Section& section)
147 : Section(section.m_image, section.m_section_index)
148 {
149 }
150 unsigned relocation_count() const { return entry_count(); }
151 const Relocation relocation(unsigned index) const;
152 template<typename F>
153 void for_each_relocation(F) const;
154 };
155
156 class Relocation {
157 public:
158 Relocation(const ELFImage& image, const Elf32_Rel& rel)
159 : m_image(image)
160 , m_rel(rel)
161 {
162 }
163
164 ~Relocation() {}
165
166 unsigned offset() const { return m_rel.r_offset; }
167 unsigned type() const { return ELF32_R_TYPE(m_rel.r_info); }
168 unsigned symbol_index() const { return ELF32_R_SYM(m_rel.r_info); }
169 const Symbol symbol() const { return m_image.symbol(symbol_index()); }
170
171 private:
172 const ELFImage& m_image;
173 const Elf32_Rel& m_rel;
174 };
175
176 unsigned symbol_count() const;
177 unsigned section_count() const;
178 unsigned program_header_count() const;
179
180 const Symbol symbol(unsigned) const;
181 const Section section(unsigned) const;
182 const ProgramHeader program_header(unsigned const) const;
183
184 template<typename F>
185 void for_each_section(F) const;
186 template<typename F>
187 void for_each_section_of_type(unsigned, F) const;
188 template<typename F>
189 void for_each_symbol(F) const;
190 template<typename F>
191 void for_each_program_header(F) const;
192
193 // NOTE: Returns section(0) if section with name is not found.
194 // FIXME: I don't love this API.
195 const Section lookup_section(const String& name) const;
196
197 bool is_executable() const { return header().e_type == ET_EXEC; }
198 bool is_relocatable() const { return header().e_type == ET_REL; }
199 bool is_dynamic() const { return header().e_type == ET_DYN; }
200
201 VirtualAddress entry() const { return VirtualAddress(header().e_entry); }
202
203 static bool validate_elf_header(const Elf32_Ehdr& elf_header, size_t file_size);
204 static bool validate_program_headers(const Elf32_Ehdr& elf_header, size_t file_size, u8* buffer, size_t buffer_size, String& interpreter_path);
205
206private:
207 bool parse_header();
208 const char* raw_data(unsigned offset) const;
209 const Elf32_Ehdr& header() const;
210 const Elf32_Shdr& section_header(unsigned) const;
211 const Elf32_Phdr& program_header_internal(unsigned) const;
212 StringView table_string(unsigned offset) const;
213 StringView section_header_table_string(unsigned offset) const;
214 StringView section_index_to_string(unsigned index) const;
215 StringView table_string(unsigned table_index, unsigned offset) const;
216
217 const u8* m_buffer { nullptr };
218 size_t m_size { 0 };
219 HashMap<String, unsigned> m_sections;
220 bool m_valid { false };
221 unsigned m_symbol_table_section_index { 0 };
222 unsigned m_string_table_section_index { 0 };
223};
224
225template<typename F>
226inline void ELFImage::for_each_section(F func) const
227{
228 auto section_count = this->section_count();
229 for (unsigned i = 0; i < section_count; ++i)
230 func(section(i));
231}
232
233template<typename F>
234inline void ELFImage::for_each_section_of_type(unsigned type, F func) const
235{
236 auto section_count = this->section_count();
237 for (unsigned i = 0; i < section_count; ++i) {
238 auto& section = this->section(i);
239 if (section.type() == type) {
240 if (func(section) == IterationDecision::Break)
241 break;
242 }
243 }
244}
245
246template<typename F>
247inline void ELFImage::RelocationSection::for_each_relocation(F func) const
248{
249 auto relocation_count = this->relocation_count();
250 for (unsigned i = 0; i < relocation_count; ++i) {
251 if (func(relocation(i)) == IterationDecision::Break)
252 break;
253 }
254}
255
256template<typename F>
257inline void ELFImage::for_each_symbol(F func) const
258{
259 auto symbol_count = this->symbol_count();
260 for (unsigned i = 0; i < symbol_count; ++i) {
261 if (func(symbol(i)) == IterationDecision::Break)
262 break;
263 }
264}
265
266template<typename F>
267inline void ELFImage::for_each_program_header(F func) const
268{
269 auto program_header_count = this->program_header_count();
270 for (unsigned i = 0; i < program_header_count; ++i)
271 func(program_header(i));
272}