2010-03-18 21:30:47 +01:00
|
|
|
/*
|
|
|
|
* File elf_private.h - definitions for processing of ELF files
|
|
|
|
*
|
|
|
|
* Copyright (C) 1996, Eric Youngdale.
|
|
|
|
* 1999-2007 Eric Pouech
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_ELF_H
|
|
|
|
# include <elf.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_ELF32_H
|
|
|
|
# include <sys/elf32.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_EXEC_ELF_H
|
|
|
|
# include <sys/exec_elf.h>
|
|
|
|
#endif
|
|
|
|
#if !defined(DT_NUM)
|
|
|
|
# if defined(DT_COUNT)
|
|
|
|
# define DT_NUM DT_COUNT
|
|
|
|
# else
|
|
|
|
/* this seems to be a satisfactory value on Solaris, which doesn't support this AFAICT */
|
|
|
|
# define DT_NUM 24
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_LINK_H
|
|
|
|
# include <link.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_LINK_H
|
|
|
|
# include <sys/link.h>
|
|
|
|
#endif
|
2015-06-25 00:17:45 +02:00
|
|
|
#ifdef HAVE_MACH_O_LOADER_H
|
|
|
|
#include <mach-o/loader.h>
|
|
|
|
#endif
|
2010-03-18 21:30:47 +01:00
|
|
|
|
|
|
|
#define IMAGE_NO_MAP ((void*)-1)
|
|
|
|
|
2018-06-04 23:51:33 +02:00
|
|
|
#ifndef __ELF__
|
2010-03-18 21:31:03 +01:00
|
|
|
#ifndef SHT_NULL
|
|
|
|
#define SHT_NULL 0
|
|
|
|
#endif
|
2020-03-17 20:10:40 +01:00
|
|
|
#ifndef NT_GNU_BUILD_ID
|
|
|
|
#define NT_GNU_BUILD_ID 3
|
|
|
|
#endif
|
2010-03-18 21:31:03 +01:00
|
|
|
#endif
|
2010-03-18 21:30:47 +01:00
|
|
|
|
|
|
|
/* structure holding information while handling an ELF image
|
|
|
|
* allows one by one section mapping for memory savings
|
|
|
|
*/
|
2010-03-18 21:31:03 +01:00
|
|
|
struct image_file_map
|
2010-03-18 21:30:47 +01:00
|
|
|
{
|
2010-03-18 21:31:03 +01:00
|
|
|
enum module_type modtype;
|
2020-03-16 16:05:39 +01:00
|
|
|
const struct image_file_map_ops *ops;
|
2012-02-13 21:41:34 +01:00
|
|
|
unsigned addr_size; /* either 16 (not used), 32 or 64 */
|
2020-03-16 16:05:23 +01:00
|
|
|
struct image_file_map* alternate; /* another file linked to this one */
|
2010-03-18 21:31:03 +01:00
|
|
|
union
|
2010-03-18 21:30:47 +01:00
|
|
|
{
|
2010-03-18 21:31:03 +01:00
|
|
|
struct elf_file_map
|
|
|
|
{
|
|
|
|
size_t elf_size;
|
|
|
|
size_t elf_start;
|
2020-03-12 16:18:39 +01:00
|
|
|
HANDLE handle;
|
2010-03-18 21:31:03 +01:00
|
|
|
const char* shstrtab;
|
2010-12-27 15:03:30 +01:00
|
|
|
char* target_copy;
|
2010-03-18 21:31:03 +01:00
|
|
|
#ifdef __ELF__
|
2018-06-01 00:26:23 +02:00
|
|
|
Elf64_Ehdr elfhdr;
|
2010-03-18 21:31:03 +01:00
|
|
|
struct
|
|
|
|
{
|
2018-06-01 00:26:24 +02:00
|
|
|
Elf64_Shdr shdr;
|
2010-03-18 21:31:03 +01:00
|
|
|
const char* mapped;
|
|
|
|
}* sect;
|
2010-03-18 21:30:55 +01:00
|
|
|
#endif
|
2010-03-18 21:31:03 +01:00
|
|
|
} elf;
|
2015-06-25 00:17:45 +02:00
|
|
|
struct macho_file_map
|
|
|
|
{
|
|
|
|
size_t segs_size;
|
|
|
|
size_t segs_start;
|
|
|
|
int fd;
|
2015-06-29 03:45:11 +02:00
|
|
|
struct image_file_map* dsym; /* the debug symbols file associated with this one */
|
2015-06-25 00:17:45 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_MACH_O_LOADER_H
|
2018-06-14 23:58:14 +02:00
|
|
|
struct mach_header mach_header;
|
2018-07-16 22:44:36 +02:00
|
|
|
size_t header_size; /* size of real header in file */
|
2015-06-25 00:17:45 +02:00
|
|
|
const struct load_command* load_commands;
|
2015-06-29 03:45:11 +02:00
|
|
|
const struct uuid_command* uuid;
|
2015-06-25 00:17:45 +02:00
|
|
|
|
|
|
|
/* The offset in the file which is this architecture. mach_header was
|
|
|
|
* read from arch_offset. */
|
|
|
|
unsigned arch_offset;
|
|
|
|
|
|
|
|
int num_sections;
|
|
|
|
struct
|
|
|
|
{
|
2018-06-14 23:58:17 +02:00
|
|
|
struct section_64 section;
|
2015-06-25 00:17:47 +02:00
|
|
|
const char* mapped;
|
2015-08-04 09:40:38 +02:00
|
|
|
unsigned int ignored : 1;
|
2015-06-25 00:17:45 +02:00
|
|
|
}* sect;
|
|
|
|
#endif
|
|
|
|
} macho;
|
2010-03-18 21:31:03 +01:00
|
|
|
struct pe_file_map
|
|
|
|
{
|
|
|
|
HANDLE hMap;
|
|
|
|
IMAGE_NT_HEADERS ntheader;
|
2020-03-10 14:54:31 +01:00
|
|
|
BOOL builtin;
|
2010-03-18 21:31:03 +01:00
|
|
|
unsigned full_count;
|
|
|
|
void* full_map;
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
IMAGE_SECTION_HEADER shdr;
|
|
|
|
const char* mapped;
|
|
|
|
}* sect;
|
|
|
|
const char* strtable;
|
|
|
|
} pe;
|
|
|
|
} u;
|
2010-03-18 21:30:55 +01:00
|
|
|
};
|
2010-03-18 21:30:47 +01:00
|
|
|
|
2010-03-18 21:31:03 +01:00
|
|
|
struct image_section_map
|
2010-03-18 21:30:47 +01:00
|
|
|
{
|
2010-03-18 21:31:03 +01:00
|
|
|
struct image_file_map* fmap;
|
2010-03-18 21:30:47 +01:00
|
|
|
long sidx;
|
|
|
|
};
|
|
|
|
|
2020-03-17 20:10:40 +01:00
|
|
|
BOOL image_check_alternate(struct image_file_map* fmap, const struct module* module) DECLSPEC_HIDDEN;
|
|
|
|
|
|
|
|
BOOL elf_map_handle(HANDLE handle, struct image_file_map* fmap) DECLSPEC_HIDDEN;
|
2020-03-17 17:21:58 +01:00
|
|
|
BOOL pe_map_file(HANDLE file, struct image_file_map* fmap, enum module_type mt) DECLSPEC_HIDDEN;
|
2020-03-17 20:10:40 +01:00
|
|
|
|
2020-03-16 16:05:39 +01:00
|
|
|
struct image_file_map_ops
|
|
|
|
{
|
|
|
|
const char* (*map_section)(struct image_section_map* ism);
|
|
|
|
void (*unmap_section)(struct image_section_map* ism);
|
|
|
|
BOOL (*find_section)(struct image_file_map* fmap, const char* name, struct image_section_map* ism);
|
|
|
|
DWORD_PTR (*get_map_rva)(const struct image_section_map* ism);
|
|
|
|
unsigned (*get_map_size)(const struct image_section_map* ism);
|
2020-03-16 16:05:54 +01:00
|
|
|
void (*unmap_file)(struct image_file_map *fmap);
|
2020-03-16 16:05:39 +01:00
|
|
|
};
|
2010-03-18 21:31:03 +01:00
|
|
|
|
|
|
|
static inline BOOL image_find_section(struct image_file_map* fmap, const char* name,
|
|
|
|
struct image_section_map* ism)
|
2010-03-18 21:30:55 +01:00
|
|
|
{
|
2020-03-16 16:05:23 +01:00
|
|
|
while (fmap)
|
2010-03-18 21:31:03 +01:00
|
|
|
{
|
2020-03-16 16:05:39 +01:00
|
|
|
if (fmap->ops->find_section(fmap, name, ism)) return TRUE;
|
2020-03-16 16:05:23 +01:00
|
|
|
fmap = fmap->alternate;
|
2010-03-18 21:31:03 +01:00
|
|
|
}
|
2020-03-16 16:05:23 +01:00
|
|
|
ism->fmap = NULL;
|
|
|
|
ism->sidx = -1;
|
|
|
|
return FALSE;
|
2010-03-18 21:31:03 +01:00
|
|
|
}
|
|
|
|
|
2020-03-16 16:05:54 +01:00
|
|
|
static inline void image_unmap_file(struct image_file_map* fmap)
|
|
|
|
{
|
|
|
|
while (fmap)
|
|
|
|
{
|
|
|
|
fmap->ops->unmap_file(fmap);
|
|
|
|
fmap = fmap->alternate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-18 21:31:03 +01:00
|
|
|
static inline const char* image_map_section(struct image_section_map* ism)
|
|
|
|
{
|
2020-03-16 16:05:39 +01:00
|
|
|
return ism->fmap ? ism->fmap->ops->map_section(ism) : NULL;
|
2010-03-18 21:31:03 +01:00
|
|
|
}
|
2010-03-18 21:30:55 +01:00
|
|
|
|
2010-03-18 21:31:03 +01:00
|
|
|
static inline void image_unmap_section(struct image_section_map* ism)
|
|
|
|
{
|
2020-03-16 16:05:39 +01:00
|
|
|
if (ism->fmap) ism->fmap->ops->unmap_section(ism);
|
2010-03-18 21:31:03 +01:00
|
|
|
}
|
2010-03-18 21:30:47 +01:00
|
|
|
|
2010-08-28 17:43:10 +02:00
|
|
|
static inline DWORD_PTR image_get_map_rva(const struct image_section_map* ism)
|
2010-03-18 21:31:19 +01:00
|
|
|
{
|
2020-03-16 16:05:39 +01:00
|
|
|
return ism->fmap ? ism->fmap->ops->get_map_rva(ism) : 0;
|
2010-03-18 21:31:19 +01:00
|
|
|
}
|
|
|
|
|
2010-08-28 17:43:10 +02:00
|
|
|
static inline unsigned image_get_map_size(const struct image_section_map* ism)
|
2010-03-18 21:31:03 +01:00
|
|
|
{
|
2020-03-16 16:05:39 +01:00
|
|
|
return ism->fmap ? ism->fmap->ops->get_map_size(ism) : 0;
|
2010-03-18 21:31:03 +01:00
|
|
|
}
|