Serenity Operating System
at portability 376 lines 15 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#include <AK/StringBuilder.h> 28#include <LibELF/ELFDynamicLoader.h> 29 30#include <assert.h> 31#include <dlfcn.h> 32#include <mman.h> 33#include <stdio.h> 34#include <stdlib.h> 35 36#define DYNAMIC_LOAD_DEBUG 37//#define DYNAMIC_LOAD_VERBOSE 38 39#ifdef DYNAMIC_LOAD_VERBOSE 40# define VERBOSE(fmt, ...) dbgprintf(fmt, ##__VA_ARGS__) 41#else 42# define VERBOSE(fmt, ...) \ 43 do { \ 44 } while (0) 45#endif 46 47static bool s_always_bind_now = false; 48 49NonnullRefPtr<ELFDynamicLoader> ELFDynamicLoader::construct(const char* filename, int fd, size_t size) 50{ 51 return adopt(*new ELFDynamicLoader(filename, fd, size)); 52} 53 54ELFDynamicLoader::ELFDynamicLoader(const char* filename, int fd, size_t size) 55 : m_filename(filename) 56 , m_file_size(size) 57 , m_image_fd(fd) 58{ 59 String file_mmap_name = String::format("ELF_DYN: %s", m_filename.characters()); 60 61 // FIXME: When MAP_PRIVATE is implemented for file-backed regions, change to MAP_PRIVATE 62 m_file_mapping = mmap_with_name(nullptr, size, PROT_READ, MAP_SHARED, m_image_fd, 0, file_mmap_name.characters()); 63 if (MAP_FAILED == m_file_mapping) { 64 m_valid = false; 65 } 66} 67 68ELFDynamicLoader::~ELFDynamicLoader() 69{ 70 if (MAP_FAILED != m_file_mapping) 71 munmap(m_file_mapping, m_file_size); 72} 73 74void* ELFDynamicLoader::symbol_for_name(const char* name) 75{ 76 auto symbol = m_dynamic_object->hash_section().lookup_symbol(name); 77 78 if (symbol.is_undefined()) 79 return nullptr; 80 81 return m_dynamic_object->base_address().offset(symbol.value()).as_ptr(); 82} 83 84bool ELFDynamicLoader::load_from_image(unsigned flags) 85{ 86 ELFImage elf_image((u8*)m_file_mapping, m_file_size); 87 88 m_valid = elf_image.is_valid() && elf_image.is_dynamic(); 89 90 if (!m_valid) { 91 return false; 92 } 93 94#ifdef DYNAMIC_LOAD_VERBOSE 95 m_image->dump(); 96#endif 97 98 load_program_headers(elf_image); 99 100 m_dynamic_object = AK::make<ELFDynamicObject>(m_text_segment_load_address, m_dynamic_section_address); 101 102 return load_stage_2(flags); 103} 104 105bool ELFDynamicLoader::load_stage_2(unsigned flags) 106{ 107 ASSERT(flags & RTLD_GLOBAL); 108 ASSERT(flags & RTLD_LAZY); 109 110#ifdef DYNAMIC_LOAD_DEBUG 111 m_dynamic_object->dump(); 112#endif 113 114 if (m_dynamic_object->has_text_relocations()) { 115 ASSERT(m_text_segment_load_address.get() != 0); 116 if (0 > mprotect(m_text_segment_load_address.as_ptr(), m_text_segment_size, PROT_READ | PROT_WRITE)) { 117 perror("mprotect .text: PROT_READ | PROT_WRITE"); // FIXME: dlerror? 118 return false; 119 } 120 } 121 122 do_relocations(); 123 setup_plt_trampoline(); 124 125 // Clean up our setting of .text to PROT_READ | PROT_WRITE 126 if (m_dynamic_object->has_text_relocations()) { 127 if (0 > mprotect(m_text_segment_load_address.as_ptr(), m_text_segment_size, PROT_READ | PROT_EXEC)) { 128 perror("mprotect .text: PROT_READ | PROT_EXEC"); // FIXME: dlerror? 129 return false; 130 } 131 } 132 133 call_object_init_functions(); 134 135#ifdef DYNAMIC_LOAD_DEBUG 136 dbgprintf("Loaded %s\n", m_filename.characters()); 137#endif 138 return true; 139} 140 141void ELFDynamicLoader::load_program_headers(const ELFImage& elf_image) 142{ 143 size_t total_required_allocation_size = 0; // NOTE: If we don't have any TEXTREL, we can keep RO data RO, which would be nice 144 145 Vector<ProgramHeaderRegion> program_headers; 146 147 ProgramHeaderRegion* text_region_ptr = nullptr; 148 ProgramHeaderRegion* data_region_ptr = nullptr; 149 ProgramHeaderRegion* tls_region_ptr = nullptr; 150 VirtualAddress dynamic_region_desired_vaddr; 151 152 elf_image.for_each_program_header([&](const ELFImage::ProgramHeader& program_header) { 153 ProgramHeaderRegion new_region; 154 new_region.set_program_header(program_header.raw_header()); 155 if (new_region.is_load()) 156 total_required_allocation_size += new_region.required_load_size(); 157 program_headers.append(move(new_region)); 158 auto& region = program_headers.last(); 159 if (region.is_tls_template()) 160 tls_region_ptr = &region; 161 else if (region.is_load()) { 162 if (region.is_executable()) 163 text_region_ptr = &region; 164 else 165 data_region_ptr = &region; 166 } 167 else if (region.is_dynamic()) { 168 dynamic_region_desired_vaddr = region.desired_load_address(); 169 } 170 }); 171 172 ASSERT(text_region_ptr && data_region_ptr); 173 174 // Process regions in order: .text, .data, .tls 175 auto* region = text_region_ptr; 176 // FIXME: When MAP_PRIVATE is implemented for file-backed regions, change to MAP_PRIVATE without the mprotect and memcpy 177 //void* text_segment_begin = mmap_with_name(nullptr, region->required_load_size(), region->mmap_prot(), MAP_PRIVATE, m_image_fd, region->offset(), String::format(".text: %s", m_filename.characters()).characters()); 178 void* text_segment_begin = mmap_with_name(nullptr, region->required_load_size(), PROT_READ | PROT_WRITE , MAP_ANONYMOUS | MAP_PRIVATE, 0, 0, String::format(".text: %s", m_filename.characters()).characters()); 179 if (MAP_FAILED == text_segment_begin) { 180 ASSERT_NOT_REACHED(); 181 } 182 m_text_segment_size = region->required_load_size(); 183 m_text_segment_load_address = VirtualAddress { (u32)text_segment_begin }; 184 memcpy(m_text_segment_load_address.as_ptr(), (u8*)m_file_mapping + region->offset(), region->size_in_image()); 185 if (0 > mprotect(text_segment_begin, m_text_segment_size, region->mmap_prot())) { 186 perror("mprotect .text PROT_READ | PROT_EXEC"); 187 ASSERT_NOT_REACHED(); 188 } 189 190 m_dynamic_section_address = dynamic_region_desired_vaddr.offset(m_text_segment_load_address.get()); 191 192 region = data_region_ptr; 193 void* data_segment_begin = mmap_with_name((u8*)text_segment_begin + m_text_segment_size, region->required_load_size(), region->mmap_prot(), MAP_ANONYMOUS | MAP_PRIVATE, 0, 0, String::format(".data: %s", m_filename.characters()).characters()); 194 if (MAP_FAILED == data_segment_begin) { 195 ASSERT_NOT_REACHED(); 196 } 197 VirtualAddress data_segment_actual_addr = region->desired_load_address().offset((u32)text_segment_begin); 198 memcpy(data_segment_actual_addr.as_ptr(), (u8*)m_file_mapping + region->offset(), region->size_in_image()); 199 200 // FIXME: Do some kind of 'allocate TLS section' or some such from a per-application pool 201 if (tls_region_ptr) { 202 region = tls_region_ptr; 203 // FIXME: This can't be right either. TLS needs some real work i'd say :) 204 m_tls_segment_address = tls_region_ptr->desired_load_address(); 205 VirtualAddress tls_segment_actual_addr = region->desired_load_address().offset((u32)text_segment_begin); 206 memcpy(tls_segment_actual_addr.as_ptr(), (u8*)m_file_mapping + region->offset(), region->size_in_image()); 207 } 208} 209 210void ELFDynamicLoader::do_relocations() 211{ 212 u32 load_base_address = m_dynamic_object->base_address().get(); 213 214 // FIXME: We should really bail on undefined symbols here. 215 216 auto main_relocation_section = m_dynamic_object->relocation_section(); 217 218 main_relocation_section.for_each_relocation([&](const ELFDynamicObject::Relocation& relocation) { 219 VERBOSE("====== RELOCATION %d: offset 0x%08X, type %d, symidx %08X\n", relocation.offset_in_section() / main_relocation_section.entry_size(), relocation.offset(), relocation.type(), relocation.symbol_index()); 220 u32* patch_ptr = (u32*)(load_base_address + relocation.offset()); 221 switch (relocation.type()) { 222 case R_386_NONE: 223 // Apparently most loaders will just skip these? 224 // Seems if the 'link editor' generates one something is funky with your code 225 VERBOSE("None relocation. No symbol, no nothin.\n"); 226 break; 227 case R_386_32: { 228 auto symbol = relocation.symbol(); 229 VERBOSE("Absolute relocation: name: '%s', value: %p\n", symbol.name(), symbol.value()); 230 u32 symbol_address = symbol.value() + load_base_address; 231 *patch_ptr += symbol_address; 232 VERBOSE(" Symbol address: %p\n", *patch_ptr); 233 break; 234 } 235 case R_386_PC32: { 236 auto symbol = relocation.symbol(); 237 VERBOSE("PC-relative relocation: '%s', value: %p\n", symbol.name(), symbol.value()); 238 u32 relative_offset = (symbol.value() - relocation.offset()); 239 *patch_ptr += relative_offset; 240 VERBOSE(" Symbol address: %p\n", *patch_ptr); 241 break; 242 } 243 case R_386_GLOB_DAT: { 244 auto symbol = relocation.symbol(); 245 VERBOSE("Global data relocation: '%s', value: %p\n", symbol.name(), symbol.value()); 246 u32 symbol_location = load_base_address + symbol.value(); 247 *patch_ptr = symbol_location; 248 VERBOSE(" Symbol address: %p\n", *patch_ptr); 249 break; 250 } 251 case R_386_RELATIVE: { 252 // FIXME: According to the spec, R_386_relative ones must be done first. 253 // We could explicitly do them first using m_number_of_relocatoins from DT_RELCOUNT 254 // However, our compiler is nice enough to put them at the front of the relocations for us :) 255 VERBOSE("Load address relocation at offset %X\n", relocation.offset()); 256 VERBOSE(" patch ptr == %p, adding load base address (%p) to it and storing %p\n", *patch_ptr, load_base_address, *patch_ptr + load_base_address); 257 *patch_ptr += load_base_address; // + addend for RelA (addend for Rel is stored at addr) 258 break; 259 } 260 case R_386_TLS_TPOFF: { 261 VERBOSE("Relocation type: R_386_TLS_TPOFF at offset %X\n", relocation.offset()); 262 // FIXME: this can't be right? I have no idea what "negative offset into TLS storage" means... 263 // FIXME: Check m_has_static_tls and do something different for dynamic TLS 264 *patch_ptr = relocation.offset() - (u32)m_tls_segment_address.as_ptr() - *patch_ptr; 265 break; 266 } 267 default: 268 // Raise the alarm! Someone needs to implement this relocation type 269 dbgprintf("Found a new exciting relocation type %d\n", relocation.type()); 270 printf("ELFDynamicLoader: Found unknown relocation type %d\n", relocation.type()); 271 ASSERT_NOT_REACHED(); 272 break; 273 } 274 return IterationDecision::Continue; 275 }); 276 277 // Handle PLT Global offset table relocations. 278 m_dynamic_object->plt_relocation_section().for_each_relocation([&](const ELFDynamicObject::Relocation& relocation) { 279 // FIXME: Or BIND_NOW flag passed in? 280 if (m_dynamic_object->must_bind_now() || s_always_bind_now) { 281 // Eagerly BIND_NOW the PLT entries, doing all the symbol looking goodness 282 // The patch method returns the address for the LAZY fixup path, but we don't need it here 283 (void)patch_plt_entry(relocation.offset_in_section()); 284 } else { 285 // LAZY-ily bind the PLT slots by just adding the base address to the offsets stored there 286 // This avoids doing symbol lookup, which might be expensive 287 ASSERT(relocation.type() == R_386_JMP_SLOT); 288 289 u8* relocation_address = relocation.address().as_ptr(); 290 291 *(u32*)relocation_address += load_base_address; 292 } 293 return IterationDecision::Continue; 294 }); 295 296#ifdef DYNAMIC_LOAD_DEBUG 297 dbgprintf("Done relocating!\n"); 298#endif 299} 300 301// Defined in <arch>/plt_trampoline.S 302extern "C" void _plt_trampoline(void) __attribute__((visibility("hidden"))); 303 304void ELFDynamicLoader::setup_plt_trampoline() 305{ 306 VirtualAddress got_address = m_dynamic_object->plt_got_base_address(); 307 308 u32* got_u32_ptr = (u32*)got_address.as_ptr(); 309 got_u32_ptr[1] = (u32)this; 310 got_u32_ptr[2] = (u32)&_plt_trampoline; 311 312#ifdef DYNAMIC_LOAD_DEBUG 313 dbgprintf("Set GOT PLT entries at %p: [0] = %p [1] = %p, [2] = %p\n", got_u32_ptr, got_u32_ptr[0], got_u32_ptr[1], got_u32_ptr[2]); 314#endif 315} 316 317// Called from our ASM routine _plt_trampoline 318extern "C" Elf32_Addr _fixup_plt_entry(ELFDynamicLoader* object, u32 relocation_offset) 319{ 320 return object->patch_plt_entry(relocation_offset); 321} 322 323// offset is in PLT relocation table 324Elf32_Addr ELFDynamicLoader::patch_plt_entry(u32 relocation_offset) 325{ 326 auto relocation = m_dynamic_object->plt_relocation_section().relocation_at_offset(relocation_offset); 327 328 ASSERT(relocation.type() == R_386_JMP_SLOT); 329 330 auto sym = relocation.symbol(); 331 332 u8* relocation_address = relocation.address().as_ptr(); 333 u32 symbol_location = sym.address().get(); 334 335 VERBOSE("ELFDynamicLoader: Jump slot relocation: putting %s (%p) into PLT at %p\n", sym.name(), symbol_location, relocation_address); 336 337 *(u32*)relocation_address = symbol_location; 338 339 return symbol_location; 340} 341 342void ELFDynamicLoader::call_object_init_functions() 343{ 344 typedef void (*InitFunc)(); 345 auto init_function = (InitFunc)(m_dynamic_object->init_section().address().as_ptr()); 346 347#ifdef DYNAMIC_LOAD_DEBUG 348 dbgprintf("Calling DT_INIT at %p\n", init_function); 349#endif 350 (init_function)(); 351 352 auto init_array_section = m_dynamic_object->init_array_section(); 353 354 InitFunc* init_begin = (InitFunc*)(init_array_section.address().as_ptr()); 355 InitFunc* init_end = init_begin + init_array_section.entry_count(); 356 while (init_begin != init_end) { 357 // Android sources claim that these can be -1, to be ignored. 358 // 0 definitely shows up. Apparently 0/-1 are valid? Confusing. 359 if (!*init_begin || ((i32)*init_begin == -1)) 360 continue; 361#ifdef DYNAMIC_LOAD_DEBUG 362 dbgprintf("Calling DT_INITARRAY entry at %p\n", *init_begin); 363#endif 364 (*init_begin)(); 365 ++init_begin; 366 } 367} 368 369u32 ELFDynamicLoader::ProgramHeaderRegion::mmap_prot() const 370{ 371 int prot = 0; 372 prot |= is_executable() ? PROT_EXEC : 0; 373 prot |= is_readable() ? PROT_READ : 0; 374 prot |= is_writable() ? PROT_WRITE : 0; 375 return prot; 376}