1
/* The nasty work of reading 32 and 64-bit modules is in here. */
14
#include "zlibsupport.h"
18
/* Symbol types, returned by load_dep_syms */
19
static const char *weak_sym = "W";
20
static const char *undef_sym = "U";
22
/* dump_modversions helper */
23
static const char *skip_dot(const char *str)
25
/* For our purposes, .foo matches foo. PPC64 needs this. */
26
if (str && str[0] == '.')
32
#include "elfops_core.c"
36
#include "elfops_core.c"
40
* Check ELF file header.
42
static int elf_ident(void *file, unsigned long fsize, int *conv)
44
/* "\177ELF" <byte> where byte = 001 for 32-bit, 002 for 64 */
45
unsigned char *ident = file;
47
if (fsize < EI_CLASS || memcmp(file, ELFMAG, SELFMAG) != 0)
48
return -ENOEXEC; /* Not an ELF object */
49
if (ident[EI_DATA] == 0 || ident[EI_DATA] > 2)
50
return -EINVAL; /* Unknown endianness */
53
*conv = native_endianness() != ident[EI_DATA];
54
return ident[EI_CLASS];
58
* grab_elf_file - read ELF file into memory
59
* @pathame: file to load
61
* Returns NULL, and errno set on error.
63
struct elf_file *grab_elf_file(const char *pathname)
67
struct elf_file *file;
69
fd = open(pathname, O_RDONLY, 0);
72
file = grab_elf_file_fd(pathname, fd);
81
* grab_elf_file_fd - read ELF file from file descriptor into memory
82
* @pathame: name of file to load
83
* @fd: file descriptor of file to load
85
* Returns NULL, and errno set on error.
87
struct elf_file *grab_elf_file_fd(const char *pathname, int fd)
89
struct elf_file *file;
91
file = malloc(sizeof(*file));
96
file->pathname = strdup(pathname);
97
if (!file->pathname) {
102
file->data = grab_fd(fd, &file->len);
106
switch (elf_ident(file->data, file->len, &file->conv)) {
108
file->ops = &mod_ops32;
111
file->ops = &mod_ops64;
113
case -ENOEXEC: /* Not an ELF object */
114
case -EINVAL: /* Unknown endianness */
115
default: /* Unknown word size */
121
release_elf_file(file);
125
void release_elf_file(struct elf_file *file)
132
release_file(file->data, file->len);
133
free(file->pathname);