vaseboot/VasEBoot-core/loader/machoXX.c

385 lines
10 KiB
C

#include <VasEBoot/file.h>
#include <VasEBoot/mm.h>
#include <VasEBoot/misc.h>
#include <VasEBoot/i18n.h>
#define min(a,b) (((a) < (b)) ? (a) : (b))
static int
SUFFIX (VasEBoot_macho_contains_macho) (VasEBoot_macho_t macho)
{
return macho->offsetXX != -1;
}
void
SUFFIX (VasEBoot_macho_parse) (VasEBoot_macho_t macho, const char *filename)
{
union {
struct VasEBoot_macho_lzss_header lzss;
VasEBoot_macho_header_t macho;
} head;
/* Is there any candidate at all? */
if (macho->offsetXX == -1)
return;
/* Read header and check magic. */
if (VasEBoot_file_seek (macho->file, macho->offsetXX) == (VasEBoot_off_t) -1
|| VasEBoot_file_read (macho->file, &head, sizeof (head))
!= sizeof (head))
{
if (!VasEBoot_errno)
VasEBoot_error (VAS_EBOOT_ERR_BAD_OS, N_("premature end of file %s"),
filename);
macho->offsetXX = -1;
return;
}
if (VasEBoot_memcmp (head.lzss.magic, VAS_EBOOT_MACHO_LZSS_MAGIC,
sizeof (head.lzss.magic)) == 0)
{
macho->compressed_sizeXX = VasEBoot_be_to_cpu32 (head.lzss.compressed_size);
macho->uncompressed_sizeXX
= VasEBoot_be_to_cpu32 (head.lzss.uncompressed_size);
if (macho->uncompressed_sizeXX < sizeof (head.macho))
{
VasEBoot_error (VAS_EBOOT_ERR_BAD_OS, N_("premature end of file %s"),
filename);
macho->offsetXX = -1;
return;
}
/* Skip header check. */
macho->compressedXX = 1;
return;
}
if (head.macho.magic != VAS_EBOOT_MACHO_MAGIC)
{
VasEBoot_error (VAS_EBOOT_ERR_BAD_OS, "invalid Mach-O header");
macho->offsetXX = -1;
return;
}
/* Read commands. */
macho->ncmdsXX = head.macho.ncmds;
macho->cmdsizeXX = head.macho.sizeofcmds;
macho->cmdsXX = VasEBoot_malloc (macho->cmdsizeXX);
if (! macho->cmdsXX)
return;
if (VasEBoot_file_seek (macho->file, macho->offsetXX
+ sizeof (VasEBoot_macho_header_t)) == (VasEBoot_off_t) -1
|| VasEBoot_file_read (macho->file, macho->cmdsXX,
(VasEBoot_size_t) macho->cmdsizeXX)
!= (VasEBoot_ssize_t) macho->cmdsizeXX)
{
if (!VasEBoot_errno)
VasEBoot_error (VAS_EBOOT_ERR_BAD_OS, N_("premature end of file %s"),
filename);
macho->offsetXX = -1;
}
}
typedef int (*VasEBoot_macho_iter_hook_t)
(VasEBoot_macho_t , struct VasEBoot_macho_cmd *,
void *);
static VasEBoot_err_t
VasEBoot_macho_cmds_iterate (VasEBoot_macho_t macho,
VasEBoot_macho_iter_hook_t hook,
void *hook_arg,
const char *filename)
{
VasEBoot_uint8_t *hdrs;
int i;
if (macho->compressedXX && !macho->uncompressedXX)
{
VasEBoot_uint8_t *tmp;
VasEBoot_macho_header_t *head;
macho->uncompressedXX = VasEBoot_malloc (macho->uncompressed_sizeXX);
if (!macho->uncompressedXX)
return VasEBoot_errno;
tmp = VasEBoot_malloc (macho->compressed_sizeXX);
if (!tmp)
{
VasEBoot_free (macho->uncompressedXX);
macho->uncompressedXX = 0;
return VasEBoot_errno;
}
if (VasEBoot_file_seek (macho->file, macho->offsetXX
+ VAS_EBOOT_MACHO_LZSS_OFFSET) == (VasEBoot_off_t) -1
|| VasEBoot_file_read (macho->file, tmp,
(VasEBoot_size_t) macho->compressed_sizeXX)
!= (VasEBoot_ssize_t) macho->compressed_sizeXX)
{
if (!VasEBoot_errno)
VasEBoot_error (VAS_EBOOT_ERR_BAD_OS, N_("premature end of file %s"),
filename);
VasEBoot_free (tmp);
VasEBoot_free (macho->uncompressedXX);
macho->uncompressedXX = 0;
macho->offsetXX = -1;
return VasEBoot_errno;
}
if (VasEBoot_decompress_lzss (macho->uncompressedXX,
macho->uncompressedXX
+ macho->uncompressed_sizeXX,
tmp, tmp + macho->compressed_sizeXX)
!= macho->uncompressed_sizeXX)
{
if (!VasEBoot_errno)
VasEBoot_error (VAS_EBOOT_ERR_BAD_OS, N_("premature end of file %s"),
filename);
VasEBoot_free (tmp);
VasEBoot_free (macho->uncompressedXX);
macho->uncompressedXX = 0;
macho->offsetXX = -1;
return VasEBoot_errno;
}
VasEBoot_free (tmp);
head = (VasEBoot_macho_header_t *) macho->uncompressedXX;
macho->ncmdsXX = head->ncmds;
macho->cmdsizeXX = head->sizeofcmds;
macho->cmdsXX = macho->uncompressedXX + sizeof (VasEBoot_macho_header_t);
if (sizeof (VasEBoot_macho_header_t) + macho->cmdsizeXX
>= macho->uncompressed_sizeXX)
{
VasEBoot_error (VAS_EBOOT_ERR_BAD_OS, N_("premature end of file %s"),
filename);
VasEBoot_free (macho->uncompressedXX);
macho->uncompressedXX = 0;
macho->offsetXX = -1;
return VasEBoot_errno;
}
}
if (! macho->cmdsXX)
return VasEBoot_error (VAS_EBOOT_ERR_BAD_OS, "couldn't find Mach-O commands");
hdrs = macho->cmdsXX;
for (i = 0; i < macho->ncmdsXX; i++)
{
struct VasEBoot_macho_cmd *hdr = (struct VasEBoot_macho_cmd *) hdrs;
if (hook (macho, hdr, hook_arg))
break;
hdrs += hdr->cmdsize;
}
return VasEBoot_errno;
}
VasEBoot_size_t
SUFFIX (VasEBoot_macho_filesize) (VasEBoot_macho_t macho)
{
if (SUFFIX (VasEBoot_macho_contains_macho) (macho))
return macho->endXX - macho->offsetXX;
return 0;
}
VasEBoot_err_t
SUFFIX (VasEBoot_macho_readfile) (VasEBoot_macho_t macho,
const char *filename,
void *dest)
{
VasEBoot_ssize_t read;
if (! SUFFIX (VasEBoot_macho_contains_macho) (macho))
return VasEBoot_error (VAS_EBOOT_ERR_BAD_OS,
"couldn't read architecture-specific part");
if (VasEBoot_file_seek (macho->file, macho->offsetXX) == (VasEBoot_off_t) -1)
return VasEBoot_errno;
read = VasEBoot_file_read (macho->file, dest,
macho->endXX - macho->offsetXX);
if (read != (VasEBoot_ssize_t) (macho->endXX - macho->offsetXX))
{
if (!VasEBoot_errno)
VasEBoot_error (VAS_EBOOT_ERR_BAD_OS, N_("premature end of file %s"),
filename);
return VasEBoot_errno;
}
return VAS_EBOOT_ERR_NONE;
}
struct calcsize_ctx
{
int flags;
int nr_phdrs;
VasEBoot_macho_addr_t *segments_start;
VasEBoot_macho_addr_t *segments_end;
};
/* Run through the program headers to calculate the total memory size we
should claim. */
static int
calcsize (VasEBoot_macho_t _macho __attribute__ ((unused)),
struct VasEBoot_macho_cmd *hdr0,
void *_arg)
{
VasEBoot_macho_segment_t *hdr = (VasEBoot_macho_segment_t *) hdr0;
struct calcsize_ctx *ctx = _arg;
if (hdr->cmd != VAS_EBOOT_MACHO_CMD_SEGMENT)
return 0;
if (! hdr->vmsize)
return 0;
if (! hdr->filesize && (ctx->flags & VAS_EBOOT_MACHO_NOBSS))
return 0;
ctx->nr_phdrs++;
if (hdr->vmaddr < *ctx->segments_start)
*ctx->segments_start = hdr->vmaddr;
if (hdr->vmaddr + hdr->vmsize > *ctx->segments_end)
*ctx->segments_end = hdr->vmaddr + hdr->vmsize;
return 0;
}
/* Calculate the amount of memory spanned by the segments. */
VasEBoot_err_t
SUFFIX (VasEBoot_macho_size) (VasEBoot_macho_t macho, VasEBoot_macho_addr_t *segments_start,
VasEBoot_macho_addr_t *segments_end, int flags,
const char *filename)
{
struct calcsize_ctx ctx = {
.flags = flags,
.nr_phdrs = 0,
.segments_start = segments_start,
.segments_end = segments_end,
};
*segments_start = (VasEBoot_macho_addr_t) -1;
*segments_end = 0;
VasEBoot_macho_cmds_iterate (macho, calcsize, &ctx, filename);
if (ctx.nr_phdrs == 0)
return VasEBoot_error (VAS_EBOOT_ERR_BAD_OS, "no program headers present");
if (*segments_end < *segments_start)
/* Very bad addresses. */
return VasEBoot_error (VAS_EBOOT_ERR_BAD_OS, "bad program header load addresses");
return VAS_EBOOT_ERR_NONE;
}
struct do_load_ctx
{
int flags;
char *offset;
const char *filename;
int *darwin_version;
};
static int
do_load(VasEBoot_macho_t _macho,
struct VasEBoot_macho_cmd *hdr0,
void *_arg)
{
VasEBoot_macho_segment_t *hdr = (VasEBoot_macho_segment_t *) hdr0;
struct do_load_ctx *ctx = _arg;
if (hdr->cmd != VAS_EBOOT_MACHO_CMD_SEGMENT)
return 0;
if (! hdr->filesize && (ctx->flags & VAS_EBOOT_MACHO_NOBSS))
return 0;
if (! hdr->vmsize)
return 0;
if (hdr->filesize)
{
VasEBoot_ssize_t read, toread = min (hdr->filesize, hdr->vmsize);
if (_macho->uncompressedXX)
{
if (hdr->fileoff + (VasEBoot_size_t) toread
> _macho->uncompressed_sizeXX)
read = -1;
else
{
read = toread;
VasEBoot_memcpy (ctx->offset + hdr->vmaddr,
_macho->uncompressedXX + hdr->fileoff, read);
}
}
else
{
if (VasEBoot_file_seek (_macho->file, hdr->fileoff
+ _macho->offsetXX) == (VasEBoot_off_t) -1)
return 1;
read = VasEBoot_file_read (_macho->file, ctx->offset + hdr->vmaddr,
toread);
}
if (read != toread)
{
/* XXX How can we free memory from `load_hook'? */
if (!VasEBoot_errno)
VasEBoot_error (VAS_EBOOT_ERR_BAD_OS, N_("premature end of file %s"),
ctx->filename);
return 1;
}
if (ctx->darwin_version)
{
const char *ptr = ctx->offset + hdr->vmaddr;
const char *end = ptr + min (hdr->filesize, hdr->vmsize)
- (sizeof ("Darwin Kernel Version ") - 1);
for (; ptr < end; ptr++)
if (VasEBoot_memcmp (ptr, "Darwin Kernel Version ",
sizeof ("Darwin Kernel Version ") - 1) == 0)
{
ptr += sizeof ("Darwin Kernel Version ") - 1;
*ctx->darwin_version = 0;
end += (sizeof ("Darwin Kernel Version ") - 1);
while (ptr < end && VasEBoot_isdigit (*ptr))
*ctx->darwin_version = (*ptr++ - '0') + *ctx->darwin_version * 10;
break;
}
}
}
if (hdr->filesize < hdr->vmsize)
VasEBoot_memset (ctx->offset + hdr->vmaddr + hdr->filesize,
0, hdr->vmsize - hdr->filesize);
return 0;
}
/* Load every loadable segment into memory specified by `_load_hook'. */
VasEBoot_err_t
SUFFIX (VasEBoot_macho_load) (VasEBoot_macho_t macho, const char *filename,
char *offset, int flags, int *darwin_version)
{
struct do_load_ctx ctx = {
.flags = flags,
.offset = offset,
.filename = filename,
.darwin_version = darwin_version
};
if (darwin_version)
*darwin_version = 0;
VasEBoot_macho_cmds_iterate (macho, do_load, &ctx, filename);
return VasEBoot_errno;
}
static int
find_entry_point (VasEBoot_macho_t _macho __attribute__ ((unused)),
struct VasEBoot_macho_cmd *hdr,
void *_arg)
{
VasEBoot_macho_addr_t *entry_point = _arg;
if (hdr->cmd == VAS_EBOOT_MACHO_CMD_THREAD)
*entry_point = ((VasEBoot_macho_thread_t *) hdr)->entry_point;
return 0;
}
VasEBoot_macho_addr_t
SUFFIX (VasEBoot_macho_get_entry_point) (VasEBoot_macho_t macho, const char *filename)
{
VasEBoot_macho_addr_t entry_point = 0;
VasEBoot_macho_cmds_iterate (macho, find_entry_point, &entry_point, filename);
return entry_point;
}