/*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{55el_status (*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/* original memory of binary */64Elf_Addr eaddr;6566/* size in memory of binary */67Elf_Addr memsz;6869/* required alignment */70Elf_Addr align;7172/* ELF header */73Elf_Ehdr ehdr;7475// Section Header Str Table76Elf_Shdr shstr;77Elf_Shdr symtab;7879/* Offset of dynamic table (0 if not ET_DYN) */80Elf_Off dynoff;81/* Size of dynamic table (0 if not ET_DYN) */82Elf_Addr dynsize;83} el_ctx;8485el_status el_pread(el_ctx *ctx, void *def, size_t nb, size_t offset);8687el_status el_init(el_ctx *ctx);88typedef void *(*el_alloc_cb)(89el_ctx *ctx,90Elf_Addr phys,91Elf_Addr virt,92Elf_Addr size);9394el_status el_load(el_ctx *ctx, el_alloc_cb alloccb);9596/* find the next phdr of type \p type, starting at \p *i.97* On success, returns EL_OK with *i set to the phdr number, and the phdr loaded98* in *phdr.99*100* If the end of the phdrs table was reached, *i is set to -1 and the contents101* of *phdr are undefined102*/103el_status el_findphdr(el_ctx *ctx, Elf_Phdr *phdr, u32 type, unsigned *i);104105/* Relocate the loaded executable */106el_status el_relocate(el_ctx *ctx);107108/* find a dynamic table entry109* returns the entry on success, dyn->d_tag = DT_NULL on failure110*/111el_status el_finddyn(el_ctx *ctx, Elf_Dyn *dyn, u32 type);112113typedef struct114{115Elf_Off tableoff;116Elf_Addr tablesize;117Elf_Addr entrysize;118} el_relocinfo;119120/* find all information regarding relocations of a specific type.121*122* pass DT_REL or DT_RELA for type123* sets ri->entrysize = 0 if not found124*/125el_status el_findrelocs(el_ctx *ctx, el_relocinfo *ri, u32 type);126127#endif128129130