Serenity Operating System
at hosted 116 lines 4.7 kB view raw
1/* 2 * Copyright (c) 2019-2020, Andrew Kaster <andrewdkaster@gmail.com> 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/Assertions.h> 30#include <AK/OwnPtr.h> 31#include <AK/RefCounted.h> 32#include <AK/String.h> 33#include <LibELF/ELFDynamicObject.h> 34#include <LibELF/ELFImage.h> 35#include <LibELF/exec_elf.h> 36#include <sys/mman.h> 37 38#define ALIGN_ROUND_UP(x, align) ((((size_t)(x)) + align - 1) & (~(align - 1))) 39 40class ELFDynamicLoader : public RefCounted<ELFDynamicLoader> { 41public: 42 static NonnullRefPtr<ELFDynamicLoader> construct(const char* filename, int fd, size_t file_size); 43 44 ~ELFDynamicLoader(); 45 46 bool is_valid() const { return m_valid; } 47 48 // Load a full ELF image from file into the current process and create an ELFDynamicObject 49 // from the SHT_DYNAMIC in the file. 50 bool load_from_image(unsigned flags); 51 52 // Stage 2 of loading: relocations and init functions 53 // Assumes that the program headers have been loaded and that m_dynamic_object is initialized 54 // Splitting loading like this allows us to use the same code to relocate a main executable as an elf binary 55 bool load_stage_2(unsigned flags); 56 57 // Intended for use by dlsym or other internal methods 58 void* symbol_for_name(const char*); 59 60 void dump(); 61 62 // Will be called from _fixup_plt_entry, as part of the PLT trampoline 63 Elf32_Addr patch_plt_entry(u32 relocation_offset); 64 65private: 66 class ProgramHeaderRegion { 67 public: 68 void set_program_header(const Elf32_Phdr& header) { m_program_header = header; } 69 70 // Information from ELF Program header 71 u32 type() const { return m_program_header.p_type; } 72 u32 flags() const { return m_program_header.p_flags; } 73 u32 offset() const { return m_program_header.p_offset; } 74 VirtualAddress desired_load_address() const { return VirtualAddress(m_program_header.p_vaddr); } 75 u32 size_in_memory() const { return m_program_header.p_memsz; } 76 u32 size_in_image() const { return m_program_header.p_filesz; } 77 u32 alignment() const { return m_program_header.p_align; } 78 u32 mmap_prot() const; 79 bool is_readable() const { return flags() & PF_R; } 80 bool is_writable() const { return flags() & PF_W; } 81 bool is_executable() const { return flags() & PF_X; } 82 bool is_tls_template() const { return type() == PT_TLS; } 83 bool is_load() const { return type() == PT_LOAD; } 84 bool is_dynamic() const { return type() == PT_DYNAMIC; } 85 86 u32 required_load_size() { return ALIGN_ROUND_UP(m_program_header.p_memsz, m_program_header.p_align); } 87 88 private: 89 Elf32_Phdr m_program_header; // Explictly a copy of the PHDR in the image 90 }; 91 92 explicit ELFDynamicLoader(const char* filename, int fd, size_t file_size); 93 explicit ELFDynamicLoader(Elf32_Dyn* dynamic_location, Elf32_Addr load_address); 94 95 // Stage 1 96 void load_program_headers(const ELFImage& elf_image); 97 98 // Stage 2 99 void do_relocations(); 100 void setup_plt_trampoline(); 101 void call_object_init_functions(); 102 103 String m_filename; 104 size_t m_file_size { 0 }; 105 int m_image_fd { -1 }; 106 void* m_file_mapping { nullptr }; 107 bool m_valid { true }; 108 109 OwnPtr<ELFDynamicObject> m_dynamic_object; 110 111 VirtualAddress m_text_segment_load_address; 112 size_t m_text_segment_size; 113 114 VirtualAddress m_tls_segment_address; 115 VirtualAddress m_dynamic_section_address; 116};