/*1* Copyright © 2018, M4xw2* Copyright © 2014, Owen Shepherd3*4* Permission to use, copy, modify, and/or distribute this software for any5* purpose with or without fee is hereby granted, provided that the above6* copyright notice appear in all copies.7*8* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH9* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY10* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,11* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM12* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR13* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR14* PERFORMANCE OF THIS SOFTWARE.15*/1617#ifndef ELFLOAD_H18#define ELFLOAD_H19#include <stddef.h>2021#include "elfarch.h"22#include "elf.h"2324#ifdef DEBUG25#include <gfx_utils.h>26#define EL_DEBUG(format, ...) \27gfx_printf(format __VA_OPT__(, ) __VA_ARGS__)28#else29#define EL_DEBUG(...) \30do \31{ \32} while (0)33#endif3435typedef enum36{37EL_OK = 0,3839EL_EIO,40EL_ENOMEM,4142EL_NOTELF,43EL_WRONGBITS,44EL_WRONGENDIAN,45EL_WRONGARCH,46EL_WRONGOS,47EL_NOTEXEC,48EL_NODYN,49EL_BADREL,5051} el_status;5253typedef struct el_ctx54{55bool (*pread)(struct el_ctx *ctx, void *dest, size_t nb, size_t offset);5657/* base_load_* -> address we are actually going to load at58*/59Elf_Addr60base_load_paddr,61base_load_vaddr;6263/* size in memory of binary */64Elf_Addr memsz;6566/* required alignment */67Elf_Addr align;6869/* ELF header */70Elf_Ehdr ehdr;7172// Section Header Str Table73Elf_Shdr shstr;74Elf_Shdr symtab;7576/* Offset of dynamic table (0 if not ET_DYN) */77Elf_Off dynoff;78/* Size of dynamic table (0 if not ET_DYN) */79Elf_Addr dynsize;80} el_ctx;8182el_status el_pread(el_ctx *ctx, void *def, size_t nb, size_t offset);8384el_status el_init(el_ctx *ctx);85typedef void *(*el_alloc_cb)(86el_ctx *ctx,87Elf_Addr phys,88Elf_Addr virt,89Elf_Addr size);9091el_status el_load(el_ctx *ctx, el_alloc_cb alloccb);9293/* find the next phdr of type \p type, starting at \p *i.94* On success, returns EL_OK with *i set to the phdr number, and the phdr loaded95* in *phdr.96*97* If the end of the phdrs table was reached, *i is set to -1 and the contents98* of *phdr are undefined99*/100el_status el_findphdr(el_ctx *ctx, Elf_Phdr *phdr, u32 type, unsigned *i);101102/* Relocate the loaded executable */103el_status el_relocate(el_ctx *ctx);104105/* find a dynamic table entry106* returns the entry on success, dyn->d_tag = DT_NULL on failure107*/108el_status el_finddyn(el_ctx *ctx, Elf_Dyn *dyn, u32 type);109110typedef struct111{112Elf_Off tableoff;113Elf_Addr tablesize;114Elf_Addr entrysize;115} el_relocinfo;116117/* find all information regarding relocations of a specific type.118*119* pass DT_REL or DT_RELA for type120* sets ri->entrysize = 0 if not found121*/122el_status el_findrelocs(el_ctx *ctx, el_relocinfo *ri, u32 type);123124#endif125126127