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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define IMAGE_NO_MAP ((void*)-1)
|
|
|
|
|
2020-03-31 18:21:37 +02:00
|
|
|
struct elf_header
|
|
|
|
{
|
|
|
|
UINT8 e_ident[16]; /* Magic number and other info */
|
|
|
|
UINT16 e_type; /* Object file type */
|
|
|
|
UINT16 e_machine; /* Architecture */
|
|
|
|
UINT32 e_version; /* Object file version */
|
|
|
|
UINT64 e_entry; /* Entry point virtual address */
|
|
|
|
UINT64 e_phoff; /* Program header table file offset */
|
|
|
|
UINT64 e_shoff; /* Section header table file offset */
|
|
|
|
UINT32 e_flags; /* Processor-specific flags */
|
|
|
|
UINT16 e_ehsize; /* ELF header size in bytes */
|
|
|
|
UINT16 e_phentsize; /* Program header table entry size */
|
|
|
|
UINT16 e_phnum; /* Program header table entry count */
|
|
|
|
UINT16 e_shentsize; /* Section header table entry size */
|
|
|
|
UINT16 e_shnum; /* Section header table entry count */
|
|
|
|
UINT16 e_shstrndx; /* Section header string table index */
|
|
|
|
};
|
|
|
|
|
2020-03-31 18:21:51 +02:00
|
|
|
struct elf_section_header
|
|
|
|
{
|
|
|
|
UINT32 sh_name; /* Section name (string tbl index) */
|
|
|
|
UINT32 sh_type; /* Section type */
|
|
|
|
UINT64 sh_flags; /* Section flags */
|
|
|
|
UINT64 sh_addr; /* Section virtual addr at execution */
|
|
|
|
UINT64 sh_offset; /* Section file offset */
|
|
|
|
UINT64 sh_size; /* Section size in bytes */
|
|
|
|
UINT32 sh_link; /* Link to another section */
|
|
|
|
UINT32 sh_info; /* Additional section information */
|
|
|
|
UINT64 sh_addralign; /* Section alignment */
|
|
|
|
UINT64 sh_entsize; /* Entry size if section holds table */
|
|
|
|
};
|
|
|
|
|
2020-04-01 20:28:10 +02:00
|
|
|
struct macho_load_command
|
|
|
|
{
|
|
|
|
UINT32 cmd; /* type of load command */
|
|
|
|
UINT32 cmdsize; /* total size of command in bytes */
|
|
|
|
};
|
|
|
|
|
|
|
|
struct macho_uuid_command
|
|
|
|
{
|
|
|
|
UINT32 cmd; /* LC_UUID */
|
|
|
|
UINT32 cmdsize;
|
|
|
|
UINT8 uuid[16];
|
|
|
|
};
|
|
|
|
|
2020-04-01 20:28:32 +02:00
|
|
|
struct macho_section
|
|
|
|
{
|
|
|
|
char sectname[16]; /* name of this section */
|
|
|
|
char segname[16]; /* segment this section goes in */
|
|
|
|
UINT64 addr; /* memory address of this section */
|
|
|
|
UINT64 size; /* size in bytes of this section */
|
|
|
|
UINT32 offset; /* file offset of this section */
|
|
|
|
UINT32 align; /* section alignment (power of 2) */
|
|
|
|
UINT32 reloff; /* file offset of relocation entries */
|
|
|
|
UINT32 nreloc; /* number of relocation entries */
|
|
|
|
UINT32 flags; /* flags (section type and attributes)*/
|
|
|
|
UINT32 reserved1; /* reserved (for offset or index) */
|
|
|
|
UINT32 reserved2; /* reserved (for count or sizeof) */
|
|
|
|
UINT32 reserved3; /* reserved */
|
|
|
|
};
|
|
|
|
|
|
|
|
struct macho_section32
|
|
|
|
{
|
|
|
|
char sectname[16]; /* name of this section */
|
|
|
|
char segname[16]; /* segment this section goes in */
|
|
|
|
UINT32 addr; /* memory address of this section */
|
|
|
|
UINT32 size; /* size in bytes of this section */
|
|
|
|
UINT32 offset; /* file offset of this section */
|
|
|
|
UINT32 align; /* section alignment (power of 2) */
|
|
|
|
UINT32 reloff; /* file offset of relocation entries */
|
|
|
|
UINT32 nreloc; /* number of relocation entries */
|
|
|
|
UINT32 flags; /* flags (section type and attributes)*/
|
|
|
|
UINT32 reserved1; /* reserved (for offset or index) */
|
|
|
|
UINT32 reserved2; /* reserved (for count or sizeof) */
|
|
|
|
};
|
|
|
|
|
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;
|
2020-03-31 18:21:37 +02:00
|
|
|
struct elf_header elfhdr;
|
2010-03-18 21:31:03 +01:00
|
|
|
struct
|
|
|
|
{
|
2020-03-31 18:21:51 +02:00
|
|
|
struct elf_section_header shdr;
|
2010-03-18 21:31:03 +01:00
|
|
|
const char* mapped;
|
|
|
|
}* sect;
|
|
|
|
} elf;
|
2015-06-25 00:17:45 +02:00
|
|
|
struct macho_file_map
|
|
|
|
{
|
|
|
|
size_t segs_size;
|
|
|
|
size_t segs_start;
|
2020-03-19 18:35:54 +01:00
|
|
|
HANDLE handle;
|
2015-06-29 03:45:11 +02:00
|
|
|
struct image_file_map* dsym; /* the debug symbols file associated with this one */
|
2020-04-01 20:27:59 +02:00
|
|
|
size_t header_size; /* size of real header in file */
|
|
|
|
size_t commands_size;
|
2020-04-06 19:42:12 +02:00
|
|
|
unsigned int commands_count;
|
2015-06-25 00:17:45 +02:00
|
|
|
|
2020-04-01 20:28:10 +02:00
|
|
|
const struct macho_load_command* load_commands;
|
|
|
|
const struct macho_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
|
|
|
|
{
|
2020-04-01 20:28:32 +02:00
|
|
|
struct macho_section 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;
|
|
|
|
} macho;
|
2010-03-18 21:31:03 +01:00
|
|
|
struct pe_file_map
|
|
|
|
{
|
|
|
|
HANDLE hMap;
|
2021-08-31 13:20:26 +02:00
|
|
|
IMAGE_FILE_HEADER file_header;
|
2021-08-31 13:20:33 +02:00
|
|
|
union
|
|
|
|
{
|
|
|
|
IMAGE_OPTIONAL_HEADER32 header32;
|
|
|
|
IMAGE_OPTIONAL_HEADER64 header64;
|
|
|
|
} opt;
|
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;
|
2020-03-26 16:51:49 +01:00
|
|
|
LONG_PTR sidx;
|
2010-03-18 21:30:47 +01:00
|
|
|
};
|
|
|
|
|
2020-03-29 21:02:23 +02:00
|
|
|
struct stab_nlist
|
|
|
|
{
|
|
|
|
unsigned n_strx;
|
|
|
|
unsigned char n_type;
|
|
|
|
char n_other;
|
|
|
|
short n_desc;
|
|
|
|
unsigned n_value;
|
|
|
|
};
|
|
|
|
|
2020-03-29 21:03:04 +02:00
|
|
|
struct macho64_nlist
|
|
|
|
{
|
|
|
|
unsigned n_strx;
|
|
|
|
unsigned char n_type;
|
|
|
|
char n_other;
|
|
|
|
short n_desc;
|
|
|
|
UINT64 n_value;
|
|
|
|
};
|
|
|
|
|
2020-03-17 20:10:40 +01:00
|
|
|
BOOL image_check_alternate(struct image_file_map* fmap, const struct module* module) DECLSPEC_HIDDEN;
|
2021-09-16 11:09:51 +02:00
|
|
|
struct image_file_map* image_load_debugaltlink(struct image_file_map* fmap, struct module* module) DECLSPEC_HIDDEN;
|
2020-03-17 20:10:40 +01:00
|
|
|
|
|
|
|
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
|
|
|
}
|