#ifndef RLD
#include <stdio.h>
#include <string.h>
#include "stuff/ofile.h"
#include "stuff/breakout.h"
#include "stuff/round.h"
static void check_object(
struct arch *arch,
struct member *member,
struct object *object);
static void symbol_string_at_end(
struct arch *arch,
struct member *member,
struct object *object);
static void dyld_order(
struct arch *arch,
struct member *member,
struct object *object);
static void order_error(
struct arch *arch,
struct member *member,
char *reason);
__private_extern__
void
checkout(
struct arch *archs,
unsigned long narchs)
{
unsigned long i, j;
for(i = 0; i < narchs; i++){
if(archs[i].type == OFILE_ARCHIVE){
for(j = 0; j < archs[i].nmembers; j++){
if(archs[i].members[j].type == OFILE_Mach_O){
check_object(archs + i, archs[i].members + j,
archs[i].members[j].object);
}
}
}
else if(archs[i].type == OFILE_Mach_O){
check_object(archs + i, NULL, archs[i].object);
}
}
}
static
void
check_object(
struct arch *arch,
struct member *member,
struct object *object)
{
unsigned long i, ncmds, flags;
struct load_command *lc;
struct segment_command *sg;
struct segment_command_64 *sg64;
struct dylib_command *dl_id;
object->st = NULL;
object->dyst = NULL;
object->hints_cmd = NULL;
object->seg_linkedit = NULL;
object->seg_linkedit64 = NULL;
dl_id = NULL;
lc = object->load_commands;
if(object->mh != NULL){
ncmds = object->mh->ncmds;
flags = object->mh->flags;
}
else{
ncmds = object->mh64->ncmds;
flags = object->mh64->flags;
}
for(i = 0; i < ncmds; i++){
if(lc->cmd == LC_SYMTAB){
if(object->st != NULL)
fatal_arch(arch, member, "malformed file (more than one "
"LC_SYMTAB load command): ");
object->st = (struct symtab_command *)lc;
}
else if(lc->cmd == LC_DYSYMTAB){
if(object->dyst != NULL)
fatal_arch(arch, member, "malformed file (more than one "
"LC_DYSYMTAB load command): ");
object->dyst = (struct dysymtab_command *)lc;
}
else if(lc->cmd == LC_TWOLEVEL_HINTS){
if(object->hints_cmd != NULL)
fatal_arch(arch, member, "malformed file (more than one "
"LC_TWOLEVEL_HINTS load command): ");
object->hints_cmd = (struct twolevel_hints_command *)lc;
}
else if(lc->cmd == LC_SEGMENT){
sg = (struct segment_command *)lc;
if(strcmp(sg->segname, SEG_LINKEDIT) == 0){
if(object->seg_linkedit != NULL)
fatal_arch(arch, member, "malformed file (more than "
"one " SEG_LINKEDIT "segment): ");
object->seg_linkedit = sg;
}
}
else if(lc->cmd == LC_SEGMENT_64){
sg64 = (struct segment_command_64 *)lc;
if(strcmp(sg64->segname, SEG_LINKEDIT) == 0){
if(object->seg_linkedit64 != NULL)
fatal_arch(arch, member, "malformed file (more than "
"one " SEG_LINKEDIT "segment): ");
object->seg_linkedit64 = sg64;
}
}
else if(lc->cmd == LC_ID_DYLIB){
if(dl_id != NULL)
fatal_arch(arch, member, "malformed file (more than one "
"LC_ID_DYLIB load command): ");
dl_id = (struct dylib_command *)lc;
if(dl_id->dylib.name.offset >= dl_id->cmdsize)
fatal_arch(arch, member, "malformed file (name.offset of "
"load command %lu extends past the end of the load "
"command): ", i);
}
lc = (struct load_command *)((char *)lc + lc->cmdsize);
}
if((object->mh_filetype == MH_DYLIB ||
object->mh_filetype == MH_DYLIB_STUB) && dl_id == NULL)
fatal_arch(arch, member, "malformed file (no LC_ID_DYLIB load "
"command in %s file): ", object->mh_filetype == MH_DYLIB ?
"MH_DYLIB" : "MH_DYLIB_STUB");
if(object->hints_cmd != NULL){
if(object->dyst == NULL && object->hints_cmd->nhints != 0)
fatal_arch(arch, member, "malformed file (LC_TWOLEVEL_HINTS "
"load command present without an LC_DYSYMTAB load command):");
if(object->hints_cmd->nhints != 0 &&
object->hints_cmd->nhints != object->dyst->nundefsym)
fatal_arch(arch, member, "malformed file (LC_TWOLEVEL_HINTS "
"load command's nhints does not match LC_DYSYMTAB load "
"command's nundefsym):");
}
if(object->dyst == NULL){
symbol_string_at_end(arch, member, object);
}
else{
if(object->mh_filetype == MH_DYLIB ){
dyld_order(arch, member, object);
}
else if(flags & MH_DYLDLINK){
dyld_order(arch, member, object);
}
else{
symbol_string_at_end(arch, member, object);
}
}
}
static
void
dyld_order(
struct arch *arch,
struct member *member,
struct object *object)
{
unsigned long offset, isym;
if(object->mh != NULL){
if(object->seg_linkedit == NULL)
fatal_arch(arch, member, "malformed file (no " SEG_LINKEDIT
" segment): ");
if(object->seg_linkedit->filesize != 0 &&
object->seg_linkedit->fileoff +
object->seg_linkedit->filesize != object->object_size)
fatal_arch(arch, member, "the " SEG_LINKEDIT " segment "
"does not cover the end of the file (can't "
"be processed) in: ");
offset = object->seg_linkedit->fileoff;
}
else{
if(object->seg_linkedit64 == NULL)
fatal_arch(arch, member, "malformed file (no " SEG_LINKEDIT
" segment): ");
if(object->seg_linkedit64->filesize != 0 &&
object->seg_linkedit64->fileoff +
object->seg_linkedit64->filesize != object->object_size)
fatal_arch(arch, member, "the " SEG_LINKEDIT " segment "
"does not cover the end of the file (can't "
"be processed) in: ");
offset = object->seg_linkedit64->fileoff;
}
if(object->dyst->nlocrel != 0){
if(object->dyst->locreloff != offset)
order_error(arch, member, "local relocation entries "
"out of place");
offset += object->dyst->nlocrel *
sizeof(struct relocation_info);
}
if(object->st->nsyms != 0){
if(object->st->symoff != offset)
order_error(arch, member, "symbol table out of place");
if(object->mh != NULL)
offset += object->st->nsyms * sizeof(struct nlist);
else
offset += object->st->nsyms * sizeof(struct nlist_64);
}
isym = 0;
if(object->dyst->nlocalsym != 0){
if(object->dyst->ilocalsym != isym)
order_error(arch, member, "local symbols out of place");
isym += object->dyst->nlocalsym;
}
if(object->dyst->nextdefsym != 0){
if(object->dyst->iextdefsym != isym)
order_error(arch, member, "externally defined symbols out of "
"place");
isym += object->dyst->nextdefsym;
}
if(object->dyst->nundefsym != 0){
if(object->dyst->iundefsym != isym)
order_error(arch, member, "undefined symbols out of place");
isym += object->dyst->nundefsym;
}
if(object->hints_cmd != NULL && object->hints_cmd->nhints != 0){
if(object->hints_cmd->offset != offset)
order_error(arch, member, "hints table out of place");
offset += object->hints_cmd->nhints * sizeof(struct twolevel_hint);
}
if(object->dyst->nextrel != 0){
if(object->dyst->extreloff != offset)
order_error(arch, member, "external relocation entries"
" out of place");
offset += object->dyst->nextrel *
sizeof(struct relocation_info);
}
if(object->dyst->nindirectsyms != 0){
if(object->dyst->indirectsymoff != offset)
order_error(arch, member, "indirect symbol table "
"out of place");
offset += object->dyst->nindirectsyms *
sizeof(unsigned long);
}
if(object->dyst->ntoc != 0){
if(object->dyst->tocoff != offset)
order_error(arch, member, "table of contents out of place");
offset += object->dyst->ntoc *
sizeof(struct dylib_table_of_contents);
}
if(object->dyst->nmodtab != 0){
if(object->dyst->modtaboff != offset)
order_error(arch, member, "module table out of place");
if(object->mh != NULL)
offset += object->dyst->nmodtab *
sizeof(struct dylib_module);
else
offset += object->dyst->nmodtab *
sizeof(struct dylib_module_64);
}
if(object->dyst->nextrefsyms != 0){
if(object->dyst->extrefsymoff != offset)
order_error(arch, member, "reference table out of place");
offset += object->dyst->nextrefsyms *
sizeof(struct dylib_reference);
}
if(object->st->strsize != 0){
if(object->st->stroff != offset)
order_error(arch, member, "string table out of place");
offset += object->st->strsize;
}
if(offset != object->object_size)
order_error(arch, member, "string table out of place");
}
static
void
order_error(
struct arch *arch,
struct member *member,
char *reason)
{
fatal_arch(arch, member, "file not in an order that can be processed "
"(%s): ", reason);
}
static
void
symbol_string_at_end(
struct arch *arch,
struct member *member,
struct object *object)
{
unsigned long end, strend, rounded_strend;
if(object->st != NULL && object->st->nsyms != 0){
end = object->object_size;
if(object->st->strsize != 0){
strend = object->st->stroff + object->st->strsize;
rounded_strend = round(strend, 8);
if(strend != end && rounded_strend != end)
fatal_arch(arch, member, "string table not at the end "
"of the file (can't be processed) in file: ");
if(rounded_strend != strend)
object->object_size = strend;
end = object->st->stroff;
}
if(object->dyst != NULL &&
object->dyst->nindirectsyms != 0 &&
object->st->nsyms != 0 &&
object->dyst->indirectsymoff > object->st->symoff){
if(object->dyst->indirectsymoff +
object->dyst->nindirectsyms * sizeof(unsigned long) != end){
fatal_arch(arch, member, "indirect symbol table does not "
"directly preceed the string table (can't be "
"processed) in file: ");
}
end = object->dyst->indirectsymoff;
if(object->mh != NULL){
if(object->st->symoff +
object->st->nsyms * sizeof(struct nlist) != end)
fatal_arch(arch, member, "symbol table does not "
"directly preceed the indirect symbol table (can't "
"be processed) in file: ");
}
else{
if(object->st->symoff +
object->st->nsyms * sizeof(struct nlist_64) != end)
fatal_arch(arch, member, "symbol table does not "
"directly preceed the indirect symbol table (can't "
"be processed) in file: ");
}
}
else{
if(object->mh != NULL){
if(object->st->symoff +
object->st->nsyms * sizeof(struct nlist) != end)
fatal_arch(arch, member, "symbol table and string "
"table not at the end of the file (can't be "
"processed) in file: ");
}
else{
if(object->st->symoff +
object->st->nsyms * sizeof(struct nlist_64) != end)
fatal_arch(arch, member, "symbol table and string "
"table not at the end of the file (can't be "
"processed) in file: ");
}
}
if(object->seg_linkedit != NULL &&
(object->seg_linkedit->flags & SG_FVMLIB) != SG_FVMLIB &&
object->seg_linkedit->filesize != 0){
if(object->seg_linkedit->fileoff +
object->seg_linkedit->filesize != object->object_size)
fatal_arch(arch, member, "the " SEG_LINKEDIT " segment "
"does not cover the symbol and string table (can't "
"be processed) in file: ");
}
}
}
#endif