diff options
Diffstat (limited to 'modules/objfmts/macho')
32 files changed, 7299 insertions, 0 deletions
diff --git a/modules/objfmts/macho/Makefile.inc b/modules/objfmts/macho/Makefile.inc new file mode 100644 index 0000000..39eff29 --- /dev/null +++ b/modules/objfmts/macho/Makefile.inc @@ -0,0 +1,7 @@ +libyasm_a_SOURCES += modules/objfmts/macho/macho-objfmt.c + +YASM_MODULES += objfmt_macho objfmt_macho32 objfmt_macho64 + +EXTRA_DIST += modules/objfmts/macho/tests/Makefile.inc + +include modules/objfmts/macho/tests/Makefile.inc diff --git a/modules/objfmts/macho/macho-objfmt.c b/modules/objfmts/macho/macho-objfmt.c new file mode 100644 index 0000000..1b00918 --- /dev/null +++ b/modules/objfmts/macho/macho-objfmt.c @@ -0,0 +1,1627 @@ +/* + * Mac OS X ABI Mach-O File Format + * + * Copyright (C) 2007 Henryk Richter, built upon xdf objfmt (C) Peter Johnson + * + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +/* + notes: This implementation is rather basic. There are several implementation + issues to be sorted out for full compliance and error resilience. + Some examples are given below (nasm syntax). + + 1) section placement + Mach-O requires BSS sections to be placed last in object files. This + has to be done manually. + Example: + + section .text + mov rax,[qword foo] + section .data + dw 0 + section .bss + foo dw 0 + + 2) addressing issues + + 2.1) symbol relative relocation (i.e. mov eax,[foo wrt bar]) + Not implemented yet. + + 2.2) data referencing in 64 bit mode + While ELF allows 32 bit absolute relocations in 64 bit mode, Mach-O + does not. Therefore code like + lea rbx,[_foo] ;48 8d 1c 25 00 00 00 00 + mov rcx,[_bar] ;48 8b 0c 25 00 00 00 00 + with a 32 bit address field cannot be relocated into an address >= 0x100000000 (OSX actually + uses that). + + Actually, the only register where a 64 bit displacement is allowed in x86-64, is rax + as in the example 1). + + A plausible workaround is either classic PIC (like in C), which is in turn + not implemented in this object format. The recommended was is PC relative + code (called RIP-relative in x86-64). So instead of the lines above, just write: + lea rbx,[_foo wrt rip] + mov rcx,[_bar wrt rip] + + 2.3) section/data alignment + Normally, you specify sections with a specific alignment + and get your data layed out as desired. Unfortunately, the + linker in MacOS X seems to ignore the section alignment requests. + The workaround is an explicit alignment at the end of the text section. + + section .text + movdqa xmm0,[_foo wrt rip] + + align 16 + section .data align=16 + _foo dw 32,32,32,32,32,32,32,32 + + FIXME: perform that operation implicitly! + + 2.4) cross section symbol differences unsupported in current implementation + [extern foo] + [extern bar] + section .data + dq bar-foo + + Will currently produce an error though the necessary means are provided + by the Mach-O specification. + +*/ + +#include <util.h> + +#include <libyasm.h> + +/* MACH-O DEFINES */ +/* Mach-O in-file header structure sizes (32 BIT, see below for 64 bit defs) */ +#define MACHO_HEADER_SIZE 28 +#define MACHO_SEGCMD_SIZE 56 +#define MACHO_SECTCMD_SIZE 68 +#define MACHO_SYMCMD_SIZE 24 +#define MACHO_NLIST_SIZE 12 +#define MACHO_RELINFO_SIZE 8 + +/* 64 bit sizes */ +#define MACHO_HEADER64_SIZE 32 +#define MACHO_SEGCMD64_SIZE 72 +#define MACHO_SECTCMD64_SIZE 80 +#define MACHO_NLIST64_SIZE 16 +#define MACHO_RELINFO64_SIZE 8 + + +/* Mach-O file header values */ +#define MH_MAGIC 0xfeedface +#define MH_MAGIC_64 0xfeedfacf + +/* CPU machine type */ +#define CPU_TYPE_I386 7 /* x86 platform */ +#define CPU_TYPE_X86_64 (CPU_TYPE_I386|CPU_ARCH_ABI64) +#define CPU_ARCH_ABI64 0x01000000 /* 64 bit ABI */ + +/* CPU machine subtype, e.g. processor */ +#define CPU_SUBTYPE_I386_ALL 3 /* all-x86 compatible */ +#define CPU_SUBTYPE_X86_64_ALL CPU_SUBTYPE_I386_ALL +#define CPU_SUBTYPE_386 3 +#define CPU_SUBTYPE_486 4 +#define CPU_SUBTYPE_486SX (4 + 128) +#define CPU_SUBTYPE_586 5 +#define CPU_SUBTYPE_INTEL(f, m) ((f) + ((m) << 4)) +#define CPU_SUBTYPE_PENT CPU_SUBTYPE_INTEL(5, 0) +#define CPU_SUBTYPE_PENTPRO CPU_SUBTYPE_INTEL(6, 1) +#define CPU_SUBTYPE_PENTII_M3 CPU_SUBTYPE_INTEL(6, 3) +#define CPU_SUBTYPE_PENTII_M5 CPU_SUBTYPE_INTEL(6, 5) +#define CPU_SUBTYPE_PENTIUM_4 CPU_SUBTYPE_INTEL(10, 0) + +#define CPU_SUBTYPE_INTEL_FAMILY(x) ((x) & 15) +#define CPU_SUBTYPE_INTEL_FAMILY_MAX 15 + +#define CPU_SUBTYPE_INTEL_MODEL(x) ((x) >> 4) +#define CPU_SUBTYPE_INTEL_MODEL_ALL 0 + +#define MH_OBJECT 0x1 /* object file */ + +#define LC_SEGMENT 0x1 /* segment load command */ +#define LC_SYMTAB 0x2 /* symbol table load command */ +#define LC_SEGMENT_64 0x19 /* segment load command */ + + +#define VM_PROT_NONE 0x00 +#define VM_PROT_READ 0x01 +#define VM_PROT_WRITE 0x02 +#define VM_PROT_EXECUTE 0x04 + +#define VM_PROT_DEFAULT (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE) +#define VM_PROT_ALL (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE) + +#define SECTION_TYPE 0x000000ff /* section type mask */ +#define SECTION_ATTRIBUTES 0xffffff00UL/* section attributes mask */ + +#define S_REGULAR 0x0 /* standard section */ +#define S_ZEROFILL 0x1 /* zerofill, in-memory only */ +#define S_CSTRING_LITERALS 0x2 /* literal C strings */ +#define S_4BYTE_LITERALS 0x3 /* only 4-byte literals */ +#define S_8BYTE_LITERALS 0x4 /* only 8-byte literals */ +#define S_LITERAL_POINTERS 0x5 /* only pointers to literals */ +#define S_NON_LAZY_SYMBOL_POINTERS 0x6 /* only non-lazy symbol pointers */ +#define S_LAZY_SYMBOL_POINTERS 0x7 /* only lazy symbol pointers */ +#define S_SYMBOL_STUBS 0x8 /* only symbol stubs; byte size of + * stub in the reserved2 field */ +#define S_MOD_INIT_FUNC_POINTERS 0x9 /* only function pointers for init */ +#define S_MOD_TERM_FUNC_POINTERS 0xa /* only function pointers for term */ +#define S_COALESCED 0xb /* symbols that are to be coalesced */ +#define S_GB_ZEROFILL 0xc /* >4GB zero fill on demand section */ +#define S_INTERPOSING 0xd /* only pairs of function pointers for + * interposing */ +#define S_16BYTE_LITERALS 0xe /* only 16 byte literals */ + +#define S_ATTR_DEBUG 0x02000000 /* a debug section */ +#define SECTION_ATTRIBUTES_SYS 0x00ffff00 /* system setable attributes */ +#define S_ATTR_SOME_INSTRUCTIONS 0x00000400 /* section contains some + * machine instructions */ +#define S_ATTR_EXT_RELOC 0x00000200 /* section has external + * relocation entries */ +#define S_ATTR_LOC_RELOC 0x00000100 /* section has local + * relocation entries */ + +#define SECTION_ATTRIBUTES_USR 0xff000000UL /* User setable attributes */ +#define S_ATTR_PURE_INSTRUCTIONS 0x80000000UL /* only true machine insns */ +#define S_ATTR_NO_TOC 0x40000000UL /* coalesced symbols that are + * not to be in a ranlib table + * of contents */ +#define S_ATTR_STRIP_STATIC_SYMS 0x20000000UL /* ok to strip static symbols + * in this section in files + * with the MH_DYLDLINK flag */ +#define S_ATTR_NO_DEAD_STRIP 0x10000000UL /* no dead stripping */ +#define S_ATTR_LIVE_SUPPORT 0x08000000UL /* blocks are live if they + * reference live blocks */ +#define S_ATTR_SELF_MODIFYING_CODE 0x04000000UL /* Used with i386 code stubs + * written on by dyld */ + +/* macho references symbols in different ways whether they are linked at + * runtime (LAZY, read library functions) or at link time (NON_LAZY, mostly + * data) + * + * TODO: proper support for dynamically linkable modules would require the + * __import sections as well as the dsymtab command + */ +#define REFERENCE_FLAG_UNDEFINED_NON_LAZY 0x0 +#define REFERENCE_FLAG_UNDEFINED_LAZY 0x1 + +#define align(x, y) \ + (((x) + (y) - 1) & ~((y) - 1)) /* align x to multiple of y */ + +#define align32(x) \ + align(x, 4) /* align x to 32 bit boundary */ + +#define macho_MAGIC 0x87654322 + +/* Symbol table type field bit masks */ +#define N_STAB 0xe0 /* mask indicating stab entry */ +#define N_PEXT 0x10 /* private external bit */ +#define N_TYPE 0x0e /* mask for all the type bits */ +#define N_EXT 0x01 /* external (global) bit */ + +/* Symbol table type field values */ +#define N_UNDF 0x00 /* undefined */ +#define N_ABS 0x02 /* absolute address */ +#define N_SECT 0x0e /* symbol is defined in a section */ + +#define NO_SECT 0 /* no section for symbol in nlist */ + +#define REGULAR_OUTBUF_SIZE 1024 + + +typedef struct macho_reloc { + yasm_reloc reloc; + int pcrel; + int length; + int ext; + enum reloc_type_x86_64 { + /* x86 relocations */ + GENERIC_RELOC_VANILLA = 0, /* generic relocation */ + GENERIC_RELOC_PAIR = 1, /* Only follows a GENERIC_RELOC_SECTDIFF */ + GENERIC_RELOC_SECTDIFF = 2, + GENERIC_RELOC_PB_LA_PTR = 3, /* prebound lazy pointer */ + GENERIC_RELOC_LOCAL_SECTDIFF = 4, + + /* x86-64 relocations */ + X86_64_RELOC_UNSIGNED = 0, /* for absolute addresses */ + X86_64_RELOC_SIGNED = 1, /* for signed 32-bit displacement */ + X86_64_RELOC_BRANCH = 2, /* a CALL/JMP insn with 32-bit disp */ + X86_64_RELOC_GOT_LOAD = 3, /* a MOVQ load of a GOT entry */ + X86_64_RELOC_GOT = 4, /* other GOT references */ + X86_64_RELOC_SUBTRACTOR = 5, /* must be followed by a X86_64_RELOC_UNSIGNED */ + X86_64_RELOC_SIGNED_1 = 6, /* signed 32-bit disp, -1 addend */ + X86_64_RELOC_SIGNED_2 = 7, /* signed 32-bit disp, -2 addend */ + X86_64_RELOC_SIGNED_4 = 8 /* signed 32-bit disp, -4 addend */ + } type; +} macho_reloc; + +typedef struct macho_section_data { + /*@dependent@*/ yasm_symrec *sym; /* symbol created for this section */ + long scnum; /* section number (0=first section) */ + /*@only@*/ char *segname; /* segment name in file */ + /*@only@*/ char *sectname; /* section name in file */ + unsigned long flags; /* S_* flags */ + unsigned long size; /* size of raw data (section data) in bytes */ + unsigned long offset; /* offset in raw data within file in bytes */ + unsigned long vmoff; /* memory offset */ + unsigned long nreloc; /* number of relocation entries */ + unsigned int extreloc; /* external relocations present (0/1) */ +} macho_section_data; + + +typedef struct macho_symrec_data { + unsigned long index; /* index in output order */ + yasm_intnum *value; /* valid after writing symtable to file */ + unsigned long length; /* length + 1 (plus auto underscore) */ +} macho_symrec_data; + + +typedef struct yasm_objfmt_macho { + yasm_objfmt_base objfmt; /* base structure */ + + long parse_scnum; /* sect numbering in parser */ + int bits; /* 32 / 64 */ + + yasm_symrec *gotpcrel_sym; /* ..gotpcrel */ +} yasm_objfmt_macho; + + +typedef struct macho_objfmt_output_info { + yasm_object *object; + yasm_objfmt_macho *objfmt_macho; + yasm_errwarns *errwarns; + /*@dependent@ */ FILE *f; + /*@only@ */ unsigned char *buf; + yasm_section *sect; + /*@dependent@ */ macho_section_data *msd; + + unsigned int is_64; /* write object in 64 bit mode */ + + /* vmsize and filesize available after traversing section count routine */ + unsigned long vmsize; /* raw size of all sections (including BSS) */ + unsigned long filesize; /* size of sections in file (excluding BSS) */ + unsigned long offset; /* offset within file */ + + /* forward offset tracking */ + unsigned long rel_base; /* first relocation in file */ + unsigned long s_reloff; /* in-file offset to relocations */ + + unsigned long indx; /* current symbol size in bytes (name length+1) */ + unsigned long symindex; /* current symbol index in output order */ + int all_syms; /* outputting all symbols? */ + unsigned long strlength; /* length of all strings */ +} macho_objfmt_output_info; + + +static void macho_section_data_destroy(/*@only@*/ void *d); +static void macho_section_data_print(void *data, FILE *f, int indent_level); + +static const yasm_assoc_data_callback macho_section_data_cb = { + macho_section_data_destroy, + macho_section_data_print +}; + +static void macho_symrec_data_destroy(/*@only@*/ void *d); +static void macho_symrec_data_print(void *data, FILE *f, int indent_level); + +static const yasm_assoc_data_callback macho_symrec_data_cb = { + macho_symrec_data_destroy, + macho_symrec_data_print +}; + +yasm_objfmt_module yasm_macho_LTX_objfmt; +yasm_objfmt_module yasm_macho32_LTX_objfmt; +yasm_objfmt_module yasm_macho64_LTX_objfmt; + +static yasm_objfmt * +macho_objfmt_create_common(yasm_object *object, yasm_objfmt_module *module, + int bits_pref) +{ + yasm_objfmt_macho *objfmt_macho = yasm_xmalloc(sizeof(yasm_objfmt_macho)); + + objfmt_macho->objfmt.module = module; + + /* Only support x86 arch for now */ + if (yasm__strcasecmp(yasm_arch_keyword(object->arch), "x86") != 0) { + yasm_xfree(objfmt_macho); + return NULL; + } + + /* Support x86 and amd64 machines of x86 arch */ + if (yasm__strcasecmp(yasm_arch_get_machine(object->arch), "x86") == 0 && + (bits_pref == 0 || bits_pref == 32)) { + objfmt_macho->bits = 32; + objfmt_macho->gotpcrel_sym = NULL; + } else if (yasm__strcasecmp(yasm_arch_get_machine(object->arch), + "amd64") == 0 && + (bits_pref == 0 || bits_pref == 64)) { + objfmt_macho->bits = 64; + /* FIXME: misuse of NULL bytecode */ + objfmt_macho->gotpcrel_sym = + yasm_symtab_define_label(object->symtab, "..gotpcrel", NULL, 0, 0); + } else { + yasm_xfree(objfmt_macho); + return NULL; + } + + objfmt_macho->parse_scnum = 0; /* section numbering starts at 0 */ + return (yasm_objfmt *)objfmt_macho; +} + +static yasm_objfmt * +macho_objfmt_create(yasm_object *object) +{ + yasm_objfmt *objfmt; + yasm_objfmt_macho *objfmt_macho; + + objfmt = macho_objfmt_create_common(object, &yasm_macho_LTX_objfmt, 0); + if (objfmt) { + objfmt_macho = (yasm_objfmt_macho *)objfmt; + /* Figure out which bitness of object format to use */ + if (objfmt_macho->bits == 32) + objfmt_macho->objfmt.module = &yasm_macho32_LTX_objfmt; + else if (objfmt_macho->bits == 64) + objfmt_macho->objfmt.module = &yasm_macho64_LTX_objfmt; + } + return objfmt; +} + +static yasm_objfmt * +macho32_objfmt_create(yasm_object *object) +{ + return macho_objfmt_create_common(object, &yasm_macho32_LTX_objfmt, 32); +} + +static yasm_objfmt * +macho64_objfmt_create(yasm_object *object) +{ + return macho_objfmt_create_common(object, &yasm_macho64_LTX_objfmt, 64); +} + +static int +macho_objfmt_output_value(yasm_value *value, unsigned char *buf, + unsigned int destsize, unsigned long offset, + yasm_bytecode *bc, int warn, /*@null@*/ void *d) +{ + /*@null@*/ macho_objfmt_output_info *info = (macho_objfmt_output_info *)d; + yasm_objfmt_macho *objfmt_macho; + /*@dependent@*/ /*@null@*/ yasm_intnum *intn; + unsigned long intn_minus = 0, intn_plus = 0; + int retval; + unsigned int valsize = value->size; + macho_reloc *reloc = NULL; + + assert(info != NULL); + objfmt_macho = info->objfmt_macho; + + if (value->abs) + value->abs = yasm_expr_simplify(value->abs, 1); + + /* Try to output constant and PC-relative section-local first. + * Note this does NOT output any value with a SEG, WRT, external, + * cross-section, or non-PC-relative reference (those are handled below). + */ + switch (yasm_value_output_basic(value, buf, destsize, bc, warn, + info->object->arch)) { + case -1: + return 1; + case 0: + break; + default: + return 0; + } + + if (value->section_rel) { + yasm_error_set(YASM_ERROR_TOO_COMPLEX, + N_("macho: relocation too complex for current implementation")); + return 1; + } + + if (value->rel) { + yasm_sym_vis vis = yasm_symrec_get_visibility(value->rel); + + reloc = yasm_xcalloc(sizeof(macho_reloc), 1); + reloc->reloc.addr = yasm_intnum_create_uint(bc->offset + offset); + reloc->reloc.sym = value->rel; + switch (valsize) { + case 64: + reloc->length = 3; + break; + case 32: + reloc->length = 2; + break; + case 16: + reloc->length = 1; + break; + case 8: + reloc->length = 0; + break; + default: + yasm_error_set(YASM_ERROR_TOO_COMPLEX, + N_("macho: relocation size unsupported")); + yasm_xfree(reloc); + return 1; + } + reloc->pcrel = 0; + reloc->ext = 0; + reloc->type = GENERIC_RELOC_VANILLA; + /* R_ABS */ + + if (value->rshift > 0) { + yasm_error_set(YASM_ERROR_TOO_COMPLEX, + N_("macho: shifted relocations not supported")); + yasm_xfree(reloc); + return 1; + } + + if (value->seg_of) { + yasm_error_set(YASM_ERROR_TOO_COMPLEX, + N_("macho: SEG not supported")); + yasm_xfree(reloc); + return 1; + } + + if (value->curpos_rel && objfmt_macho->gotpcrel_sym && + value->wrt == objfmt_macho->gotpcrel_sym) { + reloc->type = X86_64_RELOC_GOT; + value->wrt = NULL; + } else if (value->wrt) { + yasm_error_set(YASM_ERROR_TOO_COMPLEX, + N_("macho: invalid WRT")); + yasm_xfree(reloc); + return 1; + } + + if (value->curpos_rel) { + reloc->pcrel = 1; + if (!info->is_64) { + /* Adjust to start of section, so subtract out the bytecode + * offset. + */ + intn_minus = bc->offset; + } else { + /* Add in the offset plus value size to end up with 0. */ + intn_plus = offset+destsize; + if (reloc->type == X86_64_RELOC_GOT) { + /* XXX: This is a hack */ + if (offset >= 2 && buf[-2] == 0x8B) + reloc->type = X86_64_RELOC_GOT_LOAD; + } else if (value->jump_target) + reloc->type = X86_64_RELOC_BRANCH; + else + reloc->type = X86_64_RELOC_SIGNED; + } + } else if (info->is_64) { + if (valsize == 32) { + yasm_error_set(YASM_ERROR_NOT_CONSTANT, + N_("macho: sorry, cannot apply 32 bit absolute relocations in 64 bit mode, consider \"[_symbol wrt rip]\" for mem access, \"qword\" and \"dq _foo\" for pointers.")); + return 1; + } + reloc->type = X86_64_RELOC_UNSIGNED; + } + + /* It seems that x86-64 objects need to have all extern relocs? */ + if (info->is_64) + reloc->ext = 1; + + if ((vis & YASM_SYM_EXTERN) || (vis & YASM_SYM_COMMON)) { + reloc->ext = 1; + info->msd->extreloc = 1; /* section has external relocations */ + } else if (!info->is_64) { + /*@dependent@*/ /*@null@*/ yasm_bytecode *sym_precbc; + + /* Local symbols need valued to their actual address */ + if (yasm_symrec_get_label(value->rel, &sym_precbc)) { + yasm_section *sym_sect = yasm_bc_get_section(sym_precbc); + /*@null@*/ macho_section_data *msd; + msd = yasm_section_get_data(sym_sect, &macho_section_data_cb); + assert(msd != NULL); + intn_plus += msd->vmoff + yasm_bc_next_offset(sym_precbc); + } + } + + info->msd->nreloc++; + /*printf("reloc %s type %d ",yasm_symrec_get_name(reloc->reloc.sym),reloc->type);*/ + yasm_section_add_reloc(info->sect, (yasm_reloc *)reloc, yasm_xfree); + } + + if (intn_minus <= intn_plus) + intn = yasm_intnum_create_uint(intn_plus-intn_minus); + else { + intn = yasm_intnum_create_uint(intn_minus-intn_plus); + yasm_intnum_calc(intn, YASM_EXPR_NEG, NULL); + } + + if (value->abs) { + yasm_intnum *intn2 = yasm_expr_get_intnum(&value->abs, 0); + + if (!intn2) { + yasm_error_set(YASM_ERROR_TOO_COMPLEX, + N_("macho: relocation too complex")); + yasm_intnum_destroy(intn); + return 1; + } + yasm_intnum_calc(intn, YASM_EXPR_ADD, intn2); + } + + retval = yasm_arch_intnum_tobytes(info->object->arch, intn, buf, destsize, + valsize, 0, bc, warn); + /*printf("val %ld\n",yasm_intnum_get_int(intn));*/ + yasm_intnum_destroy(intn); + return retval; +} + +static int +macho_objfmt_output_bytecode(yasm_bytecode *bc, /*@null@*/ void *d) +{ + /*@null@*/ macho_objfmt_output_info *info = (macho_objfmt_output_info *)d; + /*@null@*/ /*@only@*/ unsigned char *bigbuf; + unsigned long size = REGULAR_OUTBUF_SIZE; + int gap; + + assert(info != NULL); + + bigbuf = yasm_bc_tobytes(bc, info->buf, &size, &gap, info, + macho_objfmt_output_value, NULL); + + /* Don't bother doing anything else if size ended up being 0. */ + if (size == 0) { + if (bigbuf) + yasm_xfree(bigbuf); + return 0; + } + + /* Warn that gaps are converted to 0 and write out the 0's. */ + if (gap) { + unsigned long left; + + yasm_warn_set(YASM_WARN_UNINIT_CONTENTS, + N_("uninitialized space: zeroing")); + /* Write out in chunks */ + memset(info->buf, 0, REGULAR_OUTBUF_SIZE); + left = size; + while (left > REGULAR_OUTBUF_SIZE) { + fwrite(info->buf, REGULAR_OUTBUF_SIZE, 1, info->f); + left -= REGULAR_OUTBUF_SIZE; + } + fwrite(info->buf, left, 1, info->f); + } else { + /* Output buf (or bigbuf if non-NULL) to file */ + fwrite(bigbuf ? bigbuf : info->buf, (size_t) size, 1, info->f); + } + + /* If bigbuf was allocated, free it */ + if (bigbuf) + yasm_xfree(bigbuf); + + return 0; +} + +static int +macho_objfmt_output_section(yasm_section *sect, /*@null@ */ void *d) +{ + /*@null@ */ macho_objfmt_output_info *info = + (macho_objfmt_output_info *) d; + /*@dependent@ *//*@null@ */ macho_section_data *msd; + + assert(info != NULL); + msd = yasm_section_get_data(sect, &macho_section_data_cb); + assert(msd != NULL); + + if (!(msd->flags & S_ZEROFILL)) { + /* Output non-BSS sections */ + info->sect = sect; + info->msd = msd; + yasm_section_bcs_traverse(sect, info->errwarns, info, + macho_objfmt_output_bytecode); + } + return 0; +} + +static int +macho_objfmt_output_relocs(yasm_section *sect, /*@null@*/ void *d) +{ + /*@null@*/ macho_objfmt_output_info *info = (macho_objfmt_output_info *)d; + /*@dependent@*/ /*@null@*/ macho_section_data *msd; + macho_reloc *reloc; + + reloc = (macho_reloc *)yasm_section_relocs_first(sect); + while (reloc) { + unsigned char *localbuf = info->buf; + /*@null@*/ macho_symrec_data *xsymd; + unsigned long symnum; + + xsymd = yasm_symrec_get_data(reloc->reloc.sym, &macho_symrec_data_cb); + yasm_intnum_get_sized(reloc->reloc.addr, localbuf, 4, 32, 0, 0, 0); + localbuf += 4; /* address of relocation */ + + if (reloc->ext) + symnum = xsymd->index; + else { + /* find section where the symbol relates to */ + /*@dependent@*/ /*@null@*/ yasm_section *dsect; + /*@dependent@*/ /*@null@*/ yasm_bytecode *precbc; + symnum = 0; /* default to absolute */ + if (yasm_symrec_get_label(reloc->reloc.sym, &precbc) && + (dsect = yasm_bc_get_section(precbc)) && + (msd = yasm_section_get_data(dsect, &macho_section_data_cb))) + symnum = msd->scnum+1; + } + YASM_WRITE_32_L(localbuf, + (symnum & 0x00ffffff) | + (((unsigned long)reloc->pcrel & 1) << 24) | + (((unsigned long)reloc->length & 3) << 25) | + (((unsigned long)reloc->ext & 1) << 27) | + (((unsigned long)reloc->type & 0xf) << 28)); + fwrite(info->buf, 8, 1, info->f); + reloc = (macho_reloc *)yasm_section_reloc_next((yasm_reloc *)reloc); + } + + return 0; +} + +static int +exp2_to_bits(unsigned long val) +{ + int ret = 0; + + while (val) { + val >>= 1; + ret++; + } + ret = (ret > 0) ? ret - 1 : 0; + + return ret; +} + +static int +macho_objfmt_is_section_label(yasm_symrec *sym) +{ + /*@dependent@*/ /*@null@*/ yasm_section *sect; + /*@dependent@*/ /*@null@*/ yasm_bytecode *precbc; + + /* Look at symrec for value/scnum/etc. */ + if (yasm_symrec_get_label(sym, &precbc)) { + if (precbc) + sect = yasm_bc_get_section(precbc); + else + sect = NULL; + /* it's a label: get value and offset. + * If there is not a section, leave as debugging symbol. + */ + if (sect) { + /*@dependent@*/ /*@null@*/ macho_section_data *msd; + + msd = yasm_section_get_data(sect, &macho_section_data_cb); + if (msd) { + if (msd->sym == sym) + return 1; /* don't store section names */ + } + } + } + return 0; +} + +static int +macho_objfmt_output_secthead(yasm_section *sect, /*@null@*/ void *d) +{ + /*@null@*/ macho_objfmt_output_info *info = (macho_objfmt_output_info *)d; + yasm_objfmt_macho *objfmt_macho; + /*@dependent@*/ /*@null@*/ macho_section_data *msd; + unsigned char *localbuf; + + assert(info != NULL); + objfmt_macho = info->objfmt_macho; + msd = yasm_section_get_data(sect, &macho_section_data_cb); + assert(msd != NULL); + + localbuf = info->buf; + + memset(localbuf, 0, 16); + strncpy((char *)localbuf, msd->sectname, 16); + localbuf += 16; + memset(localbuf, 0, 16); + strncpy((char *)localbuf, msd->segname, 16); + localbuf += 16; + /* section address, size depend on 32/64 bit mode */ + YASM_WRITE_32_L(localbuf, msd->vmoff); /* address in memory */ + if (info->is_64) + YASM_WRITE_32_L(localbuf, 0); /* 64-bit mode: upper 32 bits = 0 */ + YASM_WRITE_32_L(localbuf, msd->size); /* size in memory */ + if (info->is_64) + YASM_WRITE_32_L(localbuf, 0); /* 64-bit mode: upper 32 bits = 0 */ + + /* offset,align,reloff,nreloc,flags,reserved1,reserved2 are 32 bit */ + if ((msd->flags & SECTION_TYPE) != S_ZEROFILL) { + YASM_WRITE_32_L(localbuf, msd->offset); + YASM_WRITE_32_L(localbuf, exp2_to_bits(yasm_section_get_align(sect))); + if (msd->nreloc) { + msd->flags |= S_ATTR_LOC_RELOC; + if (msd->extreloc) + msd->flags |= S_ATTR_EXT_RELOC; + YASM_WRITE_32_L(localbuf, + align32((long)(info->rel_base + info->s_reloff))); + YASM_WRITE_32_L(localbuf, msd->nreloc); /* nreloc */ + } else { + YASM_WRITE_32_L(localbuf, 0); + YASM_WRITE_32_L(localbuf, 0); + } + + info->s_reloff += msd->nreloc * MACHO_RELINFO_SIZE; /* nreloc */ + } else { + YASM_WRITE_32_L(localbuf, 0); /* these are zero in BSS */ + YASM_WRITE_32_L(localbuf, 0); + YASM_WRITE_32_L(localbuf, 0); + YASM_WRITE_32_L(localbuf, 0); + } + + YASM_WRITE_32_L(localbuf, msd->flags); /* flags */ + YASM_WRITE_32_L(localbuf, 0); /* reserved 1 */ + YASM_WRITE_32_L(localbuf, 0); /* reserved 2 */ + + if (info->is_64) + fwrite(info->buf, MACHO_SECTCMD64_SIZE, 1, info->f); + else + fwrite(info->buf, MACHO_SECTCMD_SIZE, 1, info->f); + + return 0; +} + + +static int +macho_objfmt_count_sym(yasm_symrec *sym, /*@null@*/ void *d) +{ + /*@null@*/ macho_objfmt_output_info *info = (macho_objfmt_output_info *)d; + /*@only@*/ char *name; + yasm_sym_vis vis = yasm_symrec_get_visibility(sym); + + assert(info != NULL); + if (info->all_syms || + vis & (YASM_SYM_GLOBAL | YASM_SYM_COMMON | YASM_SYM_EXTERN)) { + if (0 == macho_objfmt_is_section_label(sym)) { + /* Save index in symrec data */ + macho_symrec_data *sym_data = + yasm_symrec_get_data(sym, &macho_symrec_data_cb); + if (!sym_data) { + sym_data = yasm_xcalloc(sizeof(macho_symrec_data), 1); + yasm_symrec_add_data(sym, &macho_symrec_data_cb, sym_data); + } + sym_data->index = info->symindex; + info->symindex++; + + name = yasm_symrec_get_global_name(sym, info->object); + /*printf("%s\n",name); */ + /* name length + delimiter */ + sym_data->length = (unsigned long)strlen(name) + 1; + info->strlength += sym_data->length; + info->indx++; + yasm_xfree(name); + } + } + return 0; +} + + +static int +macho_objfmt_output_symtable(yasm_symrec *sym, /*@null@*/ void *d) +{ + /*@null@*/ macho_objfmt_output_info *info = (macho_objfmt_output_info *)d; + yasm_sym_vis vis = yasm_symrec_get_visibility(sym); + + assert(info != NULL); + + if (info->all_syms || + vis & (YASM_SYM_GLOBAL | YASM_SYM_COMMON | YASM_SYM_EXTERN)) { + const yasm_expr *equ_val; + const yasm_intnum *intn; + unsigned long value = 0; + long scnum = -3; /* -3 = debugging symbol */ + /*@dependent@*/ /*@null@*/ yasm_section *sect; + /*@dependent@*/ /*@null@*/ yasm_bytecode *precbc; + unsigned char *localbuf; + yasm_intnum *val; + unsigned int long_int_bytes = (info->is_64) ? 8 : 4; + unsigned int n_type = 0, n_sect = 0, n_desc = 0; + macho_symrec_data *symd; + + val = yasm_intnum_create_uint(0); + + symd = yasm_symrec_get_data(sym, &macho_symrec_data_cb); + + /* Look at symrec for value/scnum/etc. */ + if (yasm_symrec_get_label(sym, &precbc)) { + if (precbc) + sect = yasm_bc_get_section(precbc); + else + sect = NULL; + /* it's a label: get value and offset. + * If there is not a section, leave as debugging symbol. + */ + if (sect) { + /*@dependent@*/ /*@null@*/ macho_section_data *msd; + + msd = yasm_section_get_data(sect, &macho_section_data_cb); + if (msd) { + if (msd->sym == sym) { + /* don't store section names */ + yasm_intnum_destroy(val); + return 0; + } + scnum = msd->scnum; + n_type = N_SECT; + } else + yasm_internal_error(N_("didn't understand section")); + if (precbc) + value += yasm_bc_next_offset(precbc); + /* all values are subject to correction: base offset is first + * raw section, therefore add section offset + */ + if (msd) + value += msd->vmoff; + yasm_intnum_set_uint(val, value); + /*printf("%s offset %lx\n",name,value);*/ + } + } else if ((equ_val = yasm_symrec_get_equ(sym))) { + yasm_expr *equ_val_copy = yasm_expr_copy(equ_val); + + intn = yasm_expr_get_intnum(&equ_val_copy, 1); + if (!intn) { + if (vis & YASM_SYM_GLOBAL) { + yasm_error_set(YASM_ERROR_NOT_CONSTANT, + N_("global EQU value not an integer expression")); + yasm_errwarn_propagate(info->errwarns, equ_val->line); + } + } else + value = yasm_intnum_get_uint(intn); + yasm_expr_destroy(equ_val_copy); + yasm_intnum_set_uint(val, value); + n_type = N_ABS; + scnum = -2; /* -2 = absolute symbol */ + } + + if (vis & YASM_SYM_EXTERN) { + n_type = N_EXT; + scnum = -1; + /*n_desc = REFERENCE_FLAG_UNDEFINED_LAZY; * FIXME: see definition of REFERENCE_FLAG_* above */ + } else if (vis & YASM_SYM_COMMON) { + yasm_expr **csize = yasm_symrec_get_common_size(sym); + n_type = N_UNDF | N_EXT; + if (csize) { + intn = yasm_expr_get_intnum(csize, 1); + if (!intn) { + yasm_error_set(YASM_ERROR_NOT_CONSTANT, + N_("COMMON data size not an integer expression")); + yasm_errwarn_propagate(info->errwarns, (*csize)->line); + } else + yasm_intnum_set_uint(val, yasm_intnum_get_uint(intn)); + } + /*printf("common symbol %s val %lu\n", name, yasm_intnum_get_uint(val));*/ + } else if (vis & YASM_SYM_GLOBAL) { + yasm_valparamhead *valparams = + yasm_symrec_get_objext_valparams(sym); + + struct macho_global_data { + unsigned long flag; /* N_PEXT */ + } data; + + data.flag = 0; + + if (valparams) { + static const yasm_dir_help help[] = { + { "private_extern", 0, yasm_dir_helper_flag_set, + offsetof(struct macho_global_data, flag), N_PEXT }, + }; + yasm_dir_helper(sym, yasm_vps_first(valparams), + yasm_symrec_get_decl_line(sym), help, NELEMS(help), + &data, yasm_dir_helper_valparam_warn); + } + + n_type |= N_EXT | data.flag; + } + + localbuf = info->buf; + YASM_WRITE_32_L(localbuf, info->indx); /* offset in string table */ + YASM_WRITE_8(localbuf, n_type); /* type of symbol entry */ + n_sect = (scnum >= 0) ? scnum + 1 : NO_SECT; + YASM_WRITE_8(localbuf, n_sect); /* referring section where symbol is found */ + YASM_WRITE_16_L(localbuf, n_desc); /* extra description */ + yasm_intnum_get_sized(val, localbuf, long_int_bytes, ((long_int_bytes) << 3), 0, 0, 0); /* value/argument */ + localbuf += long_int_bytes; + if (symd) + symd->value = val; + else + yasm_intnum_destroy(val); + + info->indx += symd->length; + + fwrite(info->buf, 8 + long_int_bytes, 1, info->f); + } + + return 0; +} + + +static int +macho_objfmt_output_str(yasm_symrec *sym, /*@null@*/ void *d) +{ + /*@null@*/ macho_objfmt_output_info *info = (macho_objfmt_output_info *)d; + yasm_sym_vis vis = yasm_symrec_get_visibility(sym); + /*@null@*/ macho_symrec_data *xsymd; + + + assert(info != NULL); + + if (info->all_syms || + vis & (YASM_SYM_GLOBAL | YASM_SYM_COMMON | YASM_SYM_EXTERN)) { + if (0 == macho_objfmt_is_section_label(sym)) { + /*@only@*/ char *name = + yasm_symrec_get_global_name(sym, info->object); + size_t len = strlen(name); + + xsymd = yasm_symrec_get_data(sym, &macho_symrec_data_cb); + fwrite(name, len + 1, 1, info->f); + yasm_xfree(name); + } + } + return 0; +} + +static int +macho_objfmt_calc_sectsize(yasm_section *sect, /*@null@ */ void *d) +{ + /*@null@ */ macho_objfmt_output_info *info = + (macho_objfmt_output_info *) d; + /*@dependent@ *//*@null@ */ macho_section_data *msd; + unsigned long align; + + assert(info != NULL); + msd = yasm_section_get_data(sect, &macho_section_data_cb); + assert(msd != NULL); + + msd->size = yasm_bc_next_offset(yasm_section_bcs_last(sect)); + if (!(msd->flags & S_ZEROFILL)) { + msd->offset = info->offset; + info->offset += msd->size; + info->filesize += msd->size; + } + + /* accumulate size in memory */ + msd->vmoff = info->vmsize; + info->vmsize += msd->size; + + /* align both start and end of section */ + align = yasm_section_get_align(sect); + if (align != 0) { + unsigned long delta = msd->vmoff % align; + if (delta > 0) { + msd->vmoff += align - delta; + info->vmsize += align - delta; + } + } + + return 0; +} + +/* write object */ +static void +macho_objfmt_output(yasm_object *object, FILE *f, int all_syms, + yasm_errwarns *errwarns) +{ + yasm_objfmt_macho *objfmt_macho = (yasm_objfmt_macho *)object->objfmt; + macho_objfmt_output_info info; + unsigned char *localbuf; + unsigned long symtab_count = 0; + unsigned long headsize; + unsigned int macho_segcmdsize, macho_sectcmdsize, macho_nlistsize; + unsigned int macho_relinfosize, macho_segcmd; + unsigned int head_ncmds, head_sizeofcmds; + unsigned long fileoffset, fileoff_sections; + yasm_intnum *val; + unsigned long long_int_bytes; + const char pad_data[3] = "\0\0\0"; + + info.object = object; + info.objfmt_macho = objfmt_macho; + info.errwarns = errwarns; + info.f = f; + info.buf = yasm_xmalloc(REGULAR_OUTBUF_SIZE); + + if (objfmt_macho->parse_scnum == 0) { + yasm_internal_error(N_("no sections defined")); + /*@notreached@*/ + return; + } + + val = yasm_intnum_create_uint(0); + + /* + * MACH-O Header, Seg CMD, Sect CMDs, Sym Tab, Reloc Data + */ + info.is_64 = (objfmt_macho->bits == 32) ? 0 : 1; + if (info.is_64) { + /* this works only when SYMBOLS and SECTIONS present */ + headsize = + MACHO_HEADER64_SIZE + MACHO_SEGCMD64_SIZE + + (MACHO_SECTCMD64_SIZE * (objfmt_macho->parse_scnum)) + + MACHO_SYMCMD_SIZE; + macho_segcmd = LC_SEGMENT_64; + macho_segcmdsize = MACHO_SEGCMD64_SIZE; + macho_sectcmdsize = MACHO_SECTCMD64_SIZE; + macho_nlistsize = MACHO_NLIST64_SIZE; + macho_relinfosize = MACHO_RELINFO64_SIZE; + long_int_bytes = 8; + } else { + headsize = + MACHO_HEADER_SIZE + MACHO_SEGCMD_SIZE + + (MACHO_SECTCMD_SIZE * (objfmt_macho->parse_scnum)) + + MACHO_SYMCMD_SIZE; + macho_segcmd = LC_SEGMENT; + macho_segcmdsize = MACHO_SEGCMD_SIZE; + macho_sectcmdsize = MACHO_SECTCMD_SIZE; + macho_nlistsize = MACHO_NLIST_SIZE; + macho_relinfosize = MACHO_RELINFO_SIZE; + long_int_bytes = 4; + } + + /* Get number of symbols */ + info.symindex = 0; + info.indx = 0; + info.strlength = 1; /* string table starts with a zero byte */ + info.all_syms = all_syms || info.is_64; + /*info.all_syms = 1; * force all syms into symbol table */ + yasm_symtab_traverse(object->symtab, &info, macho_objfmt_count_sym); + symtab_count = info.indx; + + /* write raw section data first */ + if (fseek(f, (long)headsize, SEEK_SET) < 0) { + yasm__fatal(N_("could not seek on output file")); + /*@notreached@ */ + return; + } + + /* get size of sections in memory (including BSS) and size of sections + * in file (without BSS) + */ + info.vmsize = 0; + info.filesize = 0; + info.offset = headsize; + yasm_object_sections_traverse(object, &info, macho_objfmt_calc_sectsize); + + /* output sections to file */ + yasm_object_sections_traverse(object, &info, macho_objfmt_output_section); + + fileoff_sections = ftell(f); + + /* Write headers */ + if (fseek(f, 0, SEEK_SET) < 0) { + yasm__fatal(N_("could not seek on output file")); + /*@notreached@*/ + return; + } + + localbuf = info.buf; + + /* header size is common to 32 bit and 64 bit variants */ + if (info.is_64) { + YASM_WRITE_32_L(localbuf, MH_MAGIC_64); /* magic number */ + /* i386 64-bit ABI */ + YASM_WRITE_32_L(localbuf, CPU_ARCH_ABI64 | CPU_TYPE_I386); + } else { + YASM_WRITE_32_L(localbuf, MH_MAGIC); /* magic number */ + YASM_WRITE_32_L(localbuf, CPU_TYPE_I386); /* i386 32-bit ABI */ + } + /* i386 all cpu subtype compatible */ + YASM_WRITE_32_L(localbuf, CPU_SUBTYPE_I386_ALL); + YASM_WRITE_32_L(localbuf, MH_OBJECT); /* MACH file type */ + + /* calculate number of commands and their size, put to stream */ + head_ncmds = 0; + head_sizeofcmds = 0; + if (objfmt_macho->parse_scnum > 0) { + head_ncmds++; + head_sizeofcmds += + macho_segcmdsize + macho_sectcmdsize * objfmt_macho->parse_scnum; + } + if (symtab_count > 0) { + head_ncmds++; + head_sizeofcmds += MACHO_SYMCMD_SIZE; + } + + YASM_WRITE_32_L(localbuf, head_ncmds); + YASM_WRITE_32_L(localbuf, head_sizeofcmds); + YASM_WRITE_32_L(localbuf, 0); /* no flags (yet) */ + if (info.is_64) { + YASM_WRITE_32_L(localbuf, 0); /* reserved in 64 bit */ + fileoffset = MACHO_HEADER64_SIZE + head_sizeofcmds; + } else { + /* initial offset to first section */ + fileoffset = MACHO_HEADER_SIZE + head_sizeofcmds; + } + + /* --------------- write segment header command ---------------- */ + YASM_WRITE_32_L(localbuf, macho_segcmd); /* command LC_SEGMENT */ + /* size of load command including section load commands */ + YASM_WRITE_32_L(localbuf, + macho_segcmdsize + + macho_sectcmdsize * objfmt_macho->parse_scnum); + /* in an MH_OBJECT file all sections are in one unnamed (name all zeros) + * segment (16x0) + */ + YASM_WRITE_32_L(localbuf, 0); + YASM_WRITE_32_L(localbuf, 0); + YASM_WRITE_32_L(localbuf, 0); + YASM_WRITE_32_L(localbuf, 0); + + /* in-memory offset, in-memory size */ + yasm_intnum_set_uint(val, 0); /* offset in memory (vmaddr) */ + yasm_intnum_get_sized(val, localbuf, long_int_bytes, + ((long_int_bytes) << 3), 0, 0, 0); + localbuf += long_int_bytes; + yasm_intnum_set_uint(val, info.vmsize); /* size in memory (vmsize) */ + yasm_intnum_get_sized(val, localbuf, long_int_bytes, + ((long_int_bytes) << 3), 0, 0, 0); + localbuf += long_int_bytes; + /* offset in file to first section */ + yasm_intnum_set_uint(val, fileoffset); + yasm_intnum_get_sized(val, localbuf, long_int_bytes, + ((long_int_bytes) << 3), 0, 0, 0); + localbuf += long_int_bytes; + yasm_intnum_set_uint(val, info.filesize); /* overall size in file */ + yasm_intnum_get_sized(val, localbuf, long_int_bytes, + ((long_int_bytes) << 3), 0, 0, 0); + localbuf += long_int_bytes; + + YASM_WRITE_32_L(localbuf, VM_PROT_DEFAULT); /* VM protection, maximum */ + YASM_WRITE_32_L(localbuf, VM_PROT_DEFAULT); /* VM protection, initial */ + /* number of sections */ + YASM_WRITE_32_L(localbuf, objfmt_macho->parse_scnum); + YASM_WRITE_32_L(localbuf, 0); /* no flags */ + + /* write MACH-O header and segment command to outfile */ + fwrite(info.buf, (size_t) (localbuf - info.buf), 1, f); + + /* next: section headers */ + /* offset to relocs for first section */ + info.rel_base = align32((long)fileoff_sections); + info.s_reloff = 0; /* offset for relocs of following sections */ + yasm_object_sections_traverse(object, &info, macho_objfmt_output_secthead); + + localbuf = info.buf; + /* write out symbol command */ + YASM_WRITE_32_L(localbuf, LC_SYMTAB); /* cmd == LC_SYMTAB */ + YASM_WRITE_32_L(localbuf, MACHO_SYMCMD_SIZE); + /* symbol table offset */ + YASM_WRITE_32_L(localbuf, info.rel_base + info.s_reloff); + YASM_WRITE_32_L(localbuf, symtab_count); /* number of symbols */ + + YASM_WRITE_32_L(localbuf, macho_nlistsize * symtab_count + info.rel_base + + info.s_reloff); /* string table offset */ + YASM_WRITE_32_L(localbuf, info.strlength); /* string table size */ + /* write symbol command */ + fwrite(info.buf, (size_t)(localbuf - info.buf), 1, f); + + /*printf("num symbols %d, vmsize %d, filesize %d\n",symtab_count, + info.vmsize, info.filesize ); */ + + /* get back to end of raw section data */ + if (fseek(f, (long)fileoff_sections, SEEK_SET) < 0) { + yasm__fatal(N_("could not seek on output file")); + /*@notreached@*/ + return; + } + + /* padding to long boundary */ + if ((info.rel_base - fileoff_sections) > 0) { + fwrite(pad_data, info.rel_base - fileoff_sections, 1, f); + } + + /* relocation data */ + yasm_object_sections_traverse(object, &info, macho_objfmt_output_relocs); + + /* symbol table (NLIST) */ + info.indx = 1; /* restart symbol table indices */ + yasm_symtab_traverse(object->symtab, &info, macho_objfmt_output_symtable); + + /* symbol strings */ + fwrite(pad_data, 1, 1, f); + yasm_symtab_traverse(object->symtab, &info, macho_objfmt_output_str); + + yasm_intnum_destroy(val); + yasm_xfree(info.buf); +} + +static void +macho_objfmt_destroy(yasm_objfmt *objfmt) +{ + yasm_xfree(objfmt); +} + +static void +macho_objfmt_init_new_section(yasm_section *sect, unsigned long line) +{ + yasm_object *object = yasm_section_get_object(sect); + const char *sectname = yasm_section_get_name(sect); + yasm_objfmt_macho *objfmt_macho = (yasm_objfmt_macho *)object->objfmt; + macho_section_data *data; + yasm_symrec *sym; + + data = yasm_xmalloc(sizeof(macho_section_data)); + data->scnum = objfmt_macho->parse_scnum++; + data->segname = NULL; + data->sectname = NULL; + data->flags = S_REGULAR; + data->size = 0; + data->offset = 0; + data->vmoff = 0; + data->nreloc = 0; + data->extreloc = 0; + yasm_section_add_data(sect, &macho_section_data_cb, data); + + sym = yasm_symtab_define_label(object->symtab, sectname, + yasm_section_bcs_first(sect), 1, line); + data->sym = sym; +} + +static yasm_section * +macho_objfmt_add_default_section(yasm_object *object) +{ + yasm_section *retval; + macho_section_data *msd; + int isnew; + + retval = yasm_object_get_general(object, "LC_SEGMENT.__TEXT.__text", 0, 1, + 0, &isnew, 0); + if (isnew) { + msd = yasm_section_get_data(retval, &macho_section_data_cb); + msd->segname = yasm__xstrdup("__TEXT"); + msd->sectname = yasm__xstrdup("__text"); + msd->flags = S_ATTR_PURE_INSTRUCTIONS; + yasm_section_set_align(retval, 0, 0); + yasm_section_set_default(retval, 1); + } + return retval; +} + +static /*@observer@*/ /*@null@*/ yasm_section * +macho_objfmt_section_switch(yasm_object *object, yasm_valparamhead *valparams, + /*@unused@*/ /*@null@*/ + yasm_valparamhead *objext_valparams, + unsigned long line) +{ + yasm_valparam *vp; + yasm_section *retval; + int isnew; + /*@only@*/ char *f_sectname; + unsigned long flags; + unsigned long align; + int flags_override = 0; + const char *sectname; + char *realname; + int resonly = 0; + macho_section_data *msd; + size_t i; + + static const struct { + const char *in; + const char *seg; + const char *sect; + unsigned long flags; + unsigned long align; + } section_name_translation[] = { + {".text", "__TEXT", "__text", S_ATTR_PURE_INSTRUCTIONS, 0}, + {".const", "__TEXT", "__const", S_REGULAR, 0}, + {".static_const", "__TEXT", "__static_const", S_REGULAR, 0}, + {".cstring", "__TEXT", "__cstring", S_CSTRING_LITERALS, 0}, + {".literal4", "__TEXT", "__literal4", S_4BYTE_LITERALS, 4}, + {".literal8", "__TEXT", "__literal8", S_8BYTE_LITERALS, 8}, + {".literal16", "__TEXT", "__literal16", S_16BYTE_LITERALS, 16}, + {".constructor", "__TEXT", "__constructor", S_REGULAR, 0}, + {".destructor", "__TEXT", "__destructor", S_REGULAR, 0}, + {".fvmlib_init0", "__TEXT", "__fvmlib_init0", S_REGULAR, 0}, + {".fvmlib_init1", "__TEXT", "__fvmlib_init1", S_REGULAR, 0}, + {".mod_init_func", "__DATA", "__mod_init_func", + S_MOD_INIT_FUNC_POINTERS, 4}, + {".mod_term_func", "__DATA", "__mod_term_func", + S_MOD_TERM_FUNC_POINTERS, 4}, + {".dyld", "__DATA", "__dyld", S_REGULAR, 0}, + {".data", "__DATA", "__data", S_REGULAR, 0}, + {".static_data", "__DATA", "__static_data", S_REGULAR, 0}, + {".const_data", "__DATA", "__const", S_REGULAR, 0}, + {".rodata", "__DATA", "__const", S_REGULAR, 0}, + {".bss", "__DATA", "__bss", S_ZEROFILL, 0}, + {".objc_class_names", "__TEXT", "__cstring", S_CSTRING_LITERALS, 0}, + {".objc_meth_var_types","__TEXT", "__cstring", S_CSTRING_LITERALS, 0}, + {".objc_meth_var_names","__TEXT", "__cstring", S_CSTRING_LITERALS, 0}, + {".objc_selector_strs", "__OBJC", "__selector_strs", + S_CSTRING_LITERALS, 0}, + {".objc_class", "__OBJC", "__class", + S_ATTR_NO_DEAD_STRIP, 0}, + {".objc_meta_class", "__OBJC", "__meta_class", + S_ATTR_NO_DEAD_STRIP, 0}, + {".objc_string_object", "__OBJC", "__string_object", + S_ATTR_NO_DEAD_STRIP, 0}, + {".objc_protocol", "__OBJC", "__protocol", + S_ATTR_NO_DEAD_STRIP, 0}, + {".objc_cat_cls_meth", "__OBJC", "__cat_cls_meth", + S_ATTR_NO_DEAD_STRIP, 0}, + {".objc_cat_inst_meth", "__OBJC", "__cat_inst_meth", + S_ATTR_NO_DEAD_STRIP, 0}, + {".objc_cls_meth", "__OBJC", "__cls_meth", + S_ATTR_NO_DEAD_STRIP, 0}, + {".objc_inst_meth", "__OBJC", "__inst_meth", + S_ATTR_NO_DEAD_STRIP, 0}, + {".objc_message_refs", "__OBJC", "__message_refs", + S_LITERAL_POINTERS|S_ATTR_NO_DEAD_STRIP, 4}, + {".objc_cls_refs", "__OBJC", "__cls_refs", + S_LITERAL_POINTERS|S_ATTR_NO_DEAD_STRIP, 4}, + {".objc_module_info", "__OBJC", "__module_info", + S_ATTR_NO_DEAD_STRIP, 0}, + {".objc_symbols", "__OBJC", "__symbols", + S_ATTR_NO_DEAD_STRIP, 0}, + {".objc_category", "__OBJC", "__category", + S_ATTR_NO_DEAD_STRIP, 0}, + {".objc_class_vars", "__OBJC", "__class_vars", + S_ATTR_NO_DEAD_STRIP, 0}, + {".objc_instance_vars", "__OBJC", "__instance_vars", + S_ATTR_NO_DEAD_STRIP, 0} + }; + + struct macho_section_switch_data { + /*@only@*/ /*@null@*/ char *f_segname; + /*@only@*/ /*@null@*/ yasm_intnum *align_intn; + } data; + + static const yasm_dir_help help[] = { + { "segname", 1, yasm_dir_helper_string, + offsetof(struct macho_section_switch_data, f_segname), 0 }, + { "align", 1, yasm_dir_helper_intn, + offsetof(struct macho_section_switch_data, align_intn), 0 } + }; + + data.f_segname = NULL; + data.align_intn = NULL; + + vp = yasm_vps_first(valparams); + sectname = yasm_vp_string(vp); + if (!sectname) + return NULL; + vp = yasm_vps_next(vp); + + /* translate .text,.data,.bss to __text,__data,__bss... */ + for (i=0; i<NELEMS(section_name_translation); i++) { + if (yasm__strcasecmp(sectname, section_name_translation[i].in) == 0) + break; + } + + if (i == NELEMS(section_name_translation)) { + const char *s; + if (vp && !vp->val && (s = yasm_vp_string(vp))) { + /* Treat as SEGNAME, SECTNAME */ + if (strlen(sectname) > 16) + yasm_warn_set(YASM_WARN_GENERAL, + N_("segment name is too long, max 16 chars; truncating")); + data.f_segname = yasm__xstrndup(sectname, 16); + if (strlen(s) > 16) + yasm_warn_set(YASM_WARN_GENERAL, + N_("section name is too long, max 16 chars; truncating")); + f_sectname = yasm__xstrndup(s, 16); + flags = S_REGULAR; + align = 0; + + sectname = s; + vp = yasm_vps_next(vp); + } else { + data.f_segname = NULL; + if (strlen(sectname) > 16) + yasm_warn_set(YASM_WARN_GENERAL, + N_("section name is too long, max 16 chars; truncating")); + f_sectname = yasm__xstrndup(sectname, 16); + flags = S_ATTR_SOME_INSTRUCTIONS; + align = 0; + } + } else { + data.f_segname = yasm__xstrdup(section_name_translation[i].seg); + f_sectname = yasm__xstrdup(section_name_translation[i].sect); + flags = section_name_translation[i].flags; + align = section_name_translation[i].align; + } + + flags_override = yasm_dir_helper(object, vp, line, help, NELEMS(help), + &data, yasm_dir_helper_valparam_warn); + if (flags_override < 0) + return NULL; /* error occurred */ + + if (data.align_intn) { + align = yasm_intnum_get_uint(data.align_intn); + yasm_intnum_destroy(data.align_intn); + + /* Alignments must be a power of two. */ + if (!is_exp2(align)) { + yasm_error_set(YASM_ERROR_VALUE, + N_("argument to `%s' is not a power of two"), + vp->val); + return NULL; + } + + /* Check to see if alignment is supported size */ + if (align > 16384) { + yasm_error_set(YASM_ERROR_VALUE, + N_("macho implementation does not support alignments > 16384")); + return NULL; + } + } + + if (!data.f_segname) { + yasm_warn_set(YASM_WARN_GENERAL, + N_("Unknown section name, defaulting to __TEXT segment")); + data.f_segname = yasm__xstrdup("__TEXT"); + } + + /* Build a unique sectname from f_segname and f_sectname. */ + realname = yasm_xmalloc(strlen("LC_SEGMENT") + 1 + strlen(data.f_segname) + 1 + + strlen(f_sectname) + 1); + sprintf(realname, "LC_SEGMENT.%s.%s", data.f_segname, f_sectname); + retval = yasm_object_get_general(object, realname, align, 1, resonly, + &isnew, line); + yasm_xfree(realname); + + msd = yasm_section_get_data(retval, &macho_section_data_cb); + + if (isnew || yasm_section_is_default(retval)) { + yasm_section_set_default(retval, 0); + msd->segname = data.f_segname; + msd->sectname = f_sectname; + msd->flags = flags; + yasm_section_set_align(retval, align, line); + } else if (flags_override) { + /* align is the only value used from overrides. */ + if (yasm_section_get_align(retval) != align) { + yasm_warn_set(YASM_WARN_GENERAL, + N_("section flags ignored on section redeclaration")); + } + } + return retval; +} + +static /*@observer@*/ /*@null@*/ yasm_symrec * +macho_objfmt_get_special_sym(yasm_object *object, const char *name, + const char *parser) +{ + yasm_objfmt_macho *objfmt_macho = (yasm_objfmt_macho *)object->objfmt; + if (yasm__strcasecmp(name, "gotpcrel") == 0) { + return objfmt_macho->gotpcrel_sym; + } + return NULL; +} + +static void +macho_section_data_destroy(void *data) +{ + macho_section_data *msd = (macho_section_data *) data; + yasm_xfree(msd->segname); + yasm_xfree(msd->sectname); + yasm_xfree(data); +} + +static void +macho_section_data_print(void *data, FILE *f, int indent_level) +{ + macho_section_data *msd = (macho_section_data *) data; + + fprintf(f, "%*ssym=\n", indent_level, ""); + yasm_symrec_print(msd->sym, f, indent_level + 1); + fprintf(f, "%*sscnum=%ld\n", indent_level, "", msd->scnum); + fprintf(f, "%*sflags=0x%lx\n", indent_level, "", msd->flags); + fprintf(f, "%*ssize=%lu\n", indent_level, "", msd->size); + fprintf(f, "%*snreloc=%lu\n", indent_level, "", msd->nreloc); + fprintf(f, "%*soffset=%lu\n", indent_level, "", msd->offset); + fprintf(f, "%*sextreloc=%u\n", indent_level, "", msd->extreloc); +} + +static void +macho_symrec_data_destroy(void *data) +{ + yasm_xfree(data); +} + +static void +macho_symrec_data_print(void *data, FILE *f, int indent_level) +{ + macho_symrec_data *msd = (macho_symrec_data *)data; + + fprintf(f, "%*sindex=%ld\n", indent_level, "", msd->index); + fprintf(f, "%*svalue=", indent_level, ""); + if (msd->value) + fprintf(f, "%ld\n", yasm_intnum_get_int(msd->value)); + else + fprintf(f, "nil\n"); +} + + +/* Define valid debug formats to use with this object format */ +static const char *macho_objfmt_dbgfmt_keywords[] = { + "null", + NULL +}; + +/* Define objfmt structure -- see objfmt.h for details */ +yasm_objfmt_module yasm_macho_LTX_objfmt = { + "Mac OS X ABI Mach-O File Format", + "macho", + "o", + 32, + 0, + macho_objfmt_dbgfmt_keywords, + "null", + NULL, /* no directives */ + NULL, /* no standard macros */ + macho_objfmt_create, + macho_objfmt_output, + macho_objfmt_destroy, + macho_objfmt_add_default_section, + macho_objfmt_init_new_section, + macho_objfmt_section_switch, + macho_objfmt_get_special_sym +}; + +yasm_objfmt_module yasm_macho32_LTX_objfmt = { + "Mac OS X ABI Mach-O File Format (32-bit)", + "macho32", + "o", + 32, + 0, + macho_objfmt_dbgfmt_keywords, + "null", + NULL, /* no directives */ + NULL, /* no standard macros */ + macho32_objfmt_create, + macho_objfmt_output, + macho_objfmt_destroy, + macho_objfmt_add_default_section, + macho_objfmt_init_new_section, + macho_objfmt_section_switch, + macho_objfmt_get_special_sym +}; + +yasm_objfmt_module yasm_macho64_LTX_objfmt = { + "Mac OS X ABI Mach-O File Format (64-bit)", + "macho64", + "o", + 64, + 0, + macho_objfmt_dbgfmt_keywords, + "null", + NULL, /* no directives */ + NULL, /* no standard macros */ + macho64_objfmt_create, + macho_objfmt_output, + macho_objfmt_destroy, + macho_objfmt_add_default_section, + macho_objfmt_init_new_section, + macho_objfmt_section_switch, + macho_objfmt_get_special_sym +}; diff --git a/modules/objfmts/macho/tests/Makefile.inc b/modules/objfmts/macho/tests/Makefile.inc new file mode 100644 index 0000000..e8b4dc1 --- /dev/null +++ b/modules/objfmts/macho/tests/Makefile.inc @@ -0,0 +1,9 @@ +EXTRA_DIST += modules/objfmts/macho/tests/gas32/Makefile.inc +EXTRA_DIST += modules/objfmts/macho/tests/gas64/Makefile.inc +EXTRA_DIST += modules/objfmts/macho/tests/nasm32/Makefile.inc +EXTRA_DIST += modules/objfmts/macho/tests/nasm64/Makefile.inc + +include modules/objfmts/macho/tests/gas32/Makefile.inc +include modules/objfmts/macho/tests/gas64/Makefile.inc +include modules/objfmts/macho/tests/nasm32/Makefile.inc +include modules/objfmts/macho/tests/nasm64/Makefile.inc diff --git a/modules/objfmts/macho/tests/gas32/Makefile.inc b/modules/objfmts/macho/tests/gas32/Makefile.inc new file mode 100644 index 0000000..a02b3b3 --- /dev/null +++ b/modules/objfmts/macho/tests/gas32/Makefile.inc @@ -0,0 +1,5 @@ +TESTS += modules/objfmts/macho/tests/gas32/gas_macho32_test.sh + +EXTRA_DIST += modules/objfmts/macho/tests/gas32/gas_macho32_test.sh +EXTRA_DIST += modules/objfmts/macho/tests/gas32/gas-macho32.asm +EXTRA_DIST += modules/objfmts/macho/tests/gas32/gas-macho32.hex diff --git a/modules/objfmts/macho/tests/gas32/gas-macho32.asm b/modules/objfmts/macho/tests/gas32/gas-macho32.asm new file mode 100644 index 0000000..fad1e00 --- /dev/null +++ b/modules/objfmts/macho/tests/gas32/gas-macho32.asm @@ -0,0 +1,79 @@ +# test source file for assembling to MACH-O +# build with : +# yasm -f macho machotest.asm +# gcc -o machotest machotest.c machotest.o + +# This file should test the following: +# [1] Define and export a global text-section symbol +# [2] Define and export a global data-section symbol +# [3] Define and export a global BSS-section symbol +# [4] Define a non-global text-section symbol +# [5] Define a non-global data-section symbol +# [6] Define a non-global BSS-section symbol +# [7] Define a COMMON symbol +# [8] Define a NASM local label +# [9] Reference a NASM local label +# [10] Import an external symbol (note: printf replaced by another call) +# [11] Make a PC-relative call to an external symbol +# [12] Reference a text-section symbol in the text section +# [13] Reference a data-section symbol in the text section +# [14] Reference a BSS-section symbol in the text section +# [15] Reference a text-section symbol in the data section +# [16] Reference a data-section symbol in the data section +# [17] Reference a BSS-section symbol in the data section + +.globl _lrotate # [1] +.globl _greet # [1] +.globl _asmstr # [2] +.globl _textptr # [2] +.globl _selfptr # [2] +.globl _integer # [3] +#.extern _druck # [10] +.comm _commvar, 4 # [7] + +.text + +# prototype: long lrotate(long x, int num); +_lrotate: # [1] + pushl %ebp + movl %esp, %ebp + movl 8(%ebp), %eax + movl 12(%ebp), %ecx +Llabel: roll %eax # [4] [8] + loop Llabel # [9] [12] + movl %ebp, %esp + popl %ebp + ret + +# prototype: void greet(void); +_greet: + movl _integer, %eax # [14] + incl %eax + movl %eax, localint # [14] + pushl _commvar + movl localptr, %eax # [13] + pushl (%eax) + pushl _integer # [1] [14] + pushl _printfstr # [13] + calll _druck # [11] + addl 16, %esp + ret + +.data + +# a string +_asmstr: .asciz "hello, world" # [2] + +# a string for Printf +_printfstr: .asciz "integer==%d, localint==%d, commvar=%d\n" + +# some pointers +localptr: .long localint # [5] [17] +_textptr: .long _greet # [15] +_selfptr: .long _selfptr # [16] + +# an integer +.lcomm _integer, 4 # [3] + +# a local integer +.lcomm localint, 4 # [6] diff --git a/modules/objfmts/macho/tests/gas32/gas-macho32.hex b/modules/objfmts/macho/tests/gas32/gas-macho32.hex new file mode 100644 index 0000000..72cdabd --- /dev/null +++ b/modules/objfmts/macho/tests/gas32/gas-macho32.hex @@ -0,0 +1,688 @@ +ce +fa +ed +fe +07 +00 +00 +00 +03 +00 +00 +00 +01 +00 +00 +00 +02 +00 +00 +00 +1c +01 +00 +00 +00 +00 +00 +00 +01 +00 +00 +00 +04 +01 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +89 +00 +00 +00 +38 +01 +00 +00 +81 +00 +00 +00 +07 +00 +00 +00 +07 +00 +00 +00 +03 +00 +00 +00 +00 +00 +00 +00 +5f +5f +74 +65 +78 +74 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +5f +5f +54 +45 +58 +54 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +41 +00 +00 +00 +38 +01 +00 +00 +00 +00 +00 +00 +bc +01 +00 +00 +07 +00 +00 +00 +00 +03 +00 +80 +00 +00 +00 +00 +00 +00 +00 +00 +5f +5f +64 +61 +74 +61 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +5f +5f +44 +41 +54 +41 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +41 +00 +00 +00 +40 +00 +00 +00 +79 +01 +00 +00 +00 +00 +00 +00 +f4 +01 +00 +00 +03 +00 +00 +00 +00 +01 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +5f +5f +62 +73 +73 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +5f +5f +44 +41 +54 +41 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +81 +00 +00 +00 +08 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +01 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +02 +00 +00 +00 +18 +00 +00 +00 +0c +02 +00 +00 +08 +00 +00 +00 +6c +02 +00 +00 +44 +00 +00 +00 +55 +89 +e5 +8b +45 +08 +8b +4d +0c +d1 +c0 +e2 +fc +89 +ec +5d +c3 +a1 +81 +00 +00 +00 +40 +a3 +85 +00 +00 +00 +ff +35 +00 +00 +00 +00 +a1 +75 +00 +00 +00 +ff +30 +ff +35 +81 +00 +00 +00 +ff +35 +4e +00 +00 +00 +e8 +c6 +ff +ff +ff +03 +25 +10 +00 +00 +00 +c3 +68 +65 +6c +6c +6f +2c +20 +77 +6f +72 +6c +64 +00 +69 +6e +74 +65 +67 +65 +72 +3d +3d +25 +64 +2c +20 +6c +6f +63 +61 +6c +69 +6e +74 +3d +3d +25 +64 +2c +20 +63 +6f +6d +6d +76 +61 +72 +3d +25 +64 +0a +00 +85 +00 +00 +00 +11 +00 +00 +00 +7d +00 +00 +00 +00 +00 +00 +12 +00 +00 +00 +03 +00 +00 +04 +18 +00 +00 +00 +03 +00 +00 +04 +1e +00 +00 +00 +06 +00 +00 +0c +23 +00 +00 +00 +02 +00 +00 +04 +2b +00 +00 +00 +03 +00 +00 +04 +31 +00 +00 +00 +02 +00 +00 +04 +36 +00 +00 +00 +07 +00 +00 +0d +34 +00 +00 +00 +03 +00 +00 +04 +38 +00 +00 +00 +01 +00 +00 +04 +3c +00 +00 +00 +02 +00 +00 +04 +01 +00 +00 +00 +0f +01 +00 +00 +00 +00 +00 +00 +0a +00 +00 +00 +0f +01 +00 +00 +11 +00 +00 +00 +11 +00 +00 +00 +0f +02 +00 +00 +41 +00 +00 +00 +19 +00 +00 +00 +0f +02 +00 +00 +79 +00 +00 +00 +22 +00 +00 +00 +0f +02 +00 +00 +7d +00 +00 +00 +2b +00 +00 +00 +0f +03 +00 +00 +81 +00 +00 +00 +34 +00 +00 +00 +01 +00 +00 +00 +04 +00 +00 +00 +3d +00 +00 +00 +01 +00 +00 +00 +00 +00 +00 +00 +00 +5f +6c +72 +6f +74 +61 +74 +65 +00 +5f +67 +72 +65 +65 +74 +00 +5f +61 +73 +6d +73 +74 +72 +00 +5f +74 +65 +78 +74 +70 +74 +72 +00 +5f +73 +65 +6c +66 +70 +74 +72 +00 +5f +69 +6e +74 +65 +67 +65 +72 +00 +5f +63 +6f +6d +6d +76 +61 +72 +00 +5f +64 +72 +75 +63 +6b +00 diff --git a/modules/objfmts/macho/tests/gas32/gas_macho32_test.sh b/modules/objfmts/macho/tests/gas32/gas_macho32_test.sh new file mode 100755 index 0000000..45a1e10 --- /dev/null +++ b/modules/objfmts/macho/tests/gas32/gas_macho32_test.sh @@ -0,0 +1,3 @@ +#! /bin/sh +${srcdir}/out_test.sh macho_test modules/objfmts/macho/tests/gas32 "GAS 32-bit macho objfmt" "-f macho32 -p gas" ".o" +exit $? diff --git a/modules/objfmts/macho/tests/gas64/Makefile.inc b/modules/objfmts/macho/tests/gas64/Makefile.inc new file mode 100644 index 0000000..ec99a21 --- /dev/null +++ b/modules/objfmts/macho/tests/gas64/Makefile.inc @@ -0,0 +1,7 @@ +TESTS += modules/objfmts/macho/tests/gas64/gas_macho64_test.sh + +EXTRA_DIST += modules/objfmts/macho/tests/gas64/gas_macho64_test.sh +EXTRA_DIST += modules/objfmts/macho/tests/gas64/gas-macho64.asm +EXTRA_DIST += modules/objfmts/macho/tests/gas64/gas-macho64.hex +EXTRA_DIST += modules/objfmts/macho/tests/gas64/gas-macho64-pic.asm +EXTRA_DIST += modules/objfmts/macho/tests/gas64/gas-macho64-pic.hex diff --git a/modules/objfmts/macho/tests/gas64/gas-macho64-pic.asm b/modules/objfmts/macho/tests/gas64/gas-macho64-pic.asm new file mode 100644 index 0000000..c44502b --- /dev/null +++ b/modules/objfmts/macho/tests/gas64/gas-macho64-pic.asm @@ -0,0 +1,32 @@ +call _foo +# r_type=X86_64_RELOC_BRANCH, r_length=2, r_extern=1, r_pcrel=1, r_symbolnum=_foo +# E8 00 00 00 00 + +call _foo+4 +# r_type=X86_64_RELOC_BRANCH, r_length=2, r_extern=1, r_pcrel=1, r_symbolnum=_foo +# E8 04 00 00 00 + +movq _foo@GOTPCREL(%rip), %rax +# r_type=X86_64_RELOC_GOT_LOAD, r_length=2, r_extern=1, r_pcrel=1, r_symbolnum=_foo +# 48 8B 05 00 00 00 00 + +pushq _foo@GOTPCREL(%rip) +# r_type=X86_64_RELOC_GOT, r_length=2, r_extern=1, r_pcrel=1, r_symbolnum=_foo +# FF 35 00 00 00 00 + +movl _foo(%rip), %eax +# r_type=X86_64_RELOC_SIGNED, r_length=2, r_extern=1, r_pcrel=1, r_symbolnum=_foo +# 8B 05 00 00 00 00 + +movl _foo+4(%rip), %eax +# r_type=X86_64_RELOC_SIGNED, r_length=2, r_extern=1, r_pcrel=1, r_symbolnum=_foo +# 8B 05 04 00 00 00 + +movb $0x12, _foo(%rip) +# r_type=X86_64_RELOC_SIGNED, r_length=2, r_extern=1, r_pcrel=1, r_symbolnum=_foo +# C6 05 FF FF FF FF 12 + +movl $0x12345678, _foo(%rip) +# r_type=X86_64_RELOC_SIGNED, r_length=2, r_extern=1, r_pcrel=1, r_symbolnum=_foo +# C7 05 FC FF FF FF 78 56 34 12 + diff --git a/modules/objfmts/macho/tests/gas64/gas-macho64-pic.hex b/modules/objfmts/macho/tests/gas64/gas-macho64-pic.hex new file mode 100644 index 0000000..83bb7bb --- /dev/null +++ b/modules/objfmts/macho/tests/gas64/gas-macho64-pic.hex @@ -0,0 +1,346 @@ +cf +fa +ed +fe +07 +00 +00 +01 +03 +00 +00 +00 +01 +00 +00 +00 +02 +00 +00 +00 +b0 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +19 +00 +00 +00 +98 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +34 +00 +00 +00 +00 +00 +00 +00 +d0 +00 +00 +00 +00 +00 +00 +00 +34 +00 +00 +00 +00 +00 +00 +00 +07 +00 +00 +00 +07 +00 +00 +00 +01 +00 +00 +00 +00 +00 +00 +00 +5f +5f +74 +65 +78 +74 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +5f +5f +54 +45 +58 +54 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +34 +00 +00 +00 +00 +00 +00 +00 +d0 +00 +00 +00 +00 +00 +00 +00 +04 +01 +00 +00 +08 +00 +00 +00 +00 +03 +00 +80 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +02 +00 +00 +00 +18 +00 +00 +00 +44 +01 +00 +00 +01 +00 +00 +00 +54 +01 +00 +00 +06 +00 +00 +00 +e8 +00 +00 +00 +00 +e8 +04 +00 +00 +00 +48 +8b +05 +00 +00 +00 +00 +ff +35 +00 +00 +00 +00 +8b +05 +00 +00 +00 +00 +8b +05 +04 +00 +00 +00 +c6 +05 +ff +ff +ff +ff +12 +c7 +05 +fc +ff +ff +ff +78 +56 +34 +12 +01 +00 +00 +00 +00 +00 +00 +2d +06 +00 +00 +00 +00 +00 +00 +2d +0d +00 +00 +00 +00 +00 +00 +3d +13 +00 +00 +00 +00 +00 +00 +4d +19 +00 +00 +00 +00 +00 +00 +1d +1f +00 +00 +00 +00 +00 +00 +1d +25 +00 +00 +00 +00 +00 +00 +1d +2c +00 +00 +00 +00 +00 +00 +1d +01 +00 +00 +00 +01 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +5f +66 +6f +6f +00 diff --git a/modules/objfmts/macho/tests/gas64/gas-macho64.asm b/modules/objfmts/macho/tests/gas64/gas-macho64.asm new file mode 100644 index 0000000..1817e9b --- /dev/null +++ b/modules/objfmts/macho/tests/gas64/gas-macho64.asm @@ -0,0 +1,103 @@ + +call _foo +# r_type= X86_64_RELOC_BRANCH, r_length=2, r_extern=1, r_pcrel=1, r_symbolnum=_foo +# E8 00 00 00 00 + +call _foo+4 +# r_type=X86_64_RELOC_BRANCH, r_length=2, r_extern=1, r_pcrel=1, r_symbolnum=_foo +# E8 04 00 00 00 + +# TODO: movq _foo@GOTPCREL(%rip), %rax +# r_type=X86_64_RELOC_GOT_LOAD, r_length=2, r_extern=1, r_pcrel=1, r_symbolnum=_foo +# 48 8B 05 00 00 00 00 + +# TODO: pushq _foo@GOTPCREL(%rip) +# r_type=X86_64_RELOC_GOT, r_length=2, r_extern=1, r_pcrel=1, r_symbolnum=_foo +# FF 35 00 00 00 00 + +movl _foo(%rip), %eax +# r_type=X86_64_RELOC_SIGNED, r_length=2, r_extern=1, r_pcrel=1, r_symbolnum=_foo +# 8B 05 00 00 00 00 + +movl _foo+4(%rip), %eax +# r_type= X86_64_RELOC_SIGNED, r_length=2, r_extern=1, r_pcrel=1, r_symbolnum=_foo +# 8B 05 04 00 00 00 + +movb $0x12, _foo(%rip) +# r_type= X86_64_RELOC_SIGNED, r_length=2, r_extern=1, r_pcrel=1, r_symbolnum=_foo +# C6 05 FF FF FF FF 12 + +movl $0x12345678, _foo(%rip) +# r_type= X86_64_RELOC_SIGNED, r_length=2, r_extern=1, r_pcrel=1, r_symbolnum=_foo +# C7 05 FC FF FF FF 78 56 34 12 + +.quad _foo +# r_type=X86_64_RELOC_UNSIGNED,r_length=3, r_extern=1,r_pcrel=0, r_symbolnum=_foo +# 00 00 00 00 00 00 00 00 + +.quad _foo+4 +# r_type=X86_64_RELOC_UNSIGNED,r_length=3,r_extern=1,r_pcrel=0,r_symbolnum=_foo +# 04 00 00 00 00 00 00 00 + +# TODO: .quad _foo - _bar +# r_type=X86_64_RELOC_SUBTRACTOR,r_length=3,r_extern=1, r_pcrel=0,r_symbolnum=_bar +# r_type=X86_64_RELOC_UNSIGNED,r_length=3,r_extern=1, r_pcrel=0,r_symbolnum=_foo +# 00 00 00 00 00 00 00 00 + +# TODO: .quad _foo - _bar + 4 +# r_type=X86_64_RELOC_SUBTRACTOR,r_length=3, r_extern=1,r_pcrel=0,r_symbolnum=_bar +# r_type=X86_64_RELOC_UNSIGNED,r_length=3, r_extern=1,r_pcrel=0,r_symbolnum=_foo +# 04 00 00 00 00 00 00 00 + +# TODO: .long _foo - _bar +# r_type=X86_64_RELOC_SUBTRACTOR,r_length=2,r_extern=1,r_pcrel=0,r_symbolnum=_bar +# r_type=X86_64_RELOC_UNSIGNED,r_length=2,r_extern=1,r_pcrel=0,r_symbolnum=_foo +# 00 00 00 00 + +lea L1(%rip), %rax +# r_type=X86_64_RELOC_SIGNED, r_length=2, r_extern=1, r_pcrel=1, r_symbolnum=_prev +# 48 8d 05 12 00 00 00 +# Assumes that _prev is the first nonlocal label 0x12 bytes before L1. + +lea L0(%rip), %rax +# r_type= X86_64_RELOC_SIGNED, r_length=2, r_extern=0, r_pcrel=1, r_symbolnum=3 +# 48 8d 05 56 00 00 00 +# Assumes that L0 is in third section, and has an address of 0x00000056 +# in .o file, and no previous nonlocal label. + +.quad L1 +# r_type=X86_64_RELOC_UNSIGNED,r_length=3,r_extern=1,r_pcrel=0, r_symbolnum= _prev +# 12 00 00 00 00 00 00 00 +# Assumes that _prev is the first nonlocal label 0x12 bytes before L1. + +.quad L0 +# r_type=X86_64_RELOC_UNSIGNED,r_length=3, r_extern=0, r_pcrel=0, r_symbolnum= 3 +# 56 00 00 00 00 00 00 00 +# Assumes that L0 is in third section, and has address of 0x00000056 +# in .o file, and no previous nonlocal label. + +# TODO: .quad _foo - . +# r_type=X86_64_RELOC_SUBTRACTOR,r_length=3,r_extern=1,r_pcrel=0,r_symbolnum=_prev +# r_type=X86_64_RELOC_UNSIGNED,r_length=3,r_extern=1,r_pcrel=0,r_symbolnum=_foo +# EE FF FF FF FF FF FF FF +# Assumes that _prev is the first nonlocal label 0x12 bytes +# before this .quad + +# TODO: .quad _foo - L1 +# r_type=X86_64_RELOC_SUBTRACTOR,r_length=3,r_extern=1,r_pcrel=0,r_symbolnum=_prev +# r_type=X86_64_RELOC_UNSIGNED,r_length=3,r_extern=1,r_pcrel=0,r_symbolnum=_foo +# EE FF FF FF FF FF FF FF +# Assumes that _prev is the first nonlocal label 0x12 bytes before L1. + +.quad L1 - _prev +# No relocations. This is an assembly time constant. +# 12 00 00 00 00 00 00 00 +# Assumes that _prev is the first nonlocal label 0x12 bytes before L + +.data +.org 0x56 +L0: +_prev: +.quad 0, 0 +.byte 0, 0 +L1: diff --git a/modules/objfmts/macho/tests/gas64/gas-macho64.hex b/modules/objfmts/macho/tests/gas64/gas-macho64.hex new file mode 100644 index 0000000..e4fa488 --- /dev/null +++ b/modules/objfmts/macho/tests/gas64/gas-macho64.hex @@ -0,0 +1,666 @@ +cf +fa +ed +fe +07 +00 +00 +01 +03 +00 +00 +00 +01 +00 +00 +00 +02 +00 +00 +00 +00 +01 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +19 +00 +00 +00 +e8 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +c5 +00 +00 +00 +00 +00 +00 +00 +20 +01 +00 +00 +00 +00 +00 +00 +c5 +00 +00 +00 +00 +00 +00 +00 +07 +00 +00 +00 +07 +00 +00 +00 +02 +00 +00 +00 +00 +00 +00 +00 +5f +5f +74 +65 +78 +74 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +5f +5f +54 +45 +58 +54 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +5d +00 +00 +00 +00 +00 +00 +00 +20 +01 +00 +00 +00 +00 +00 +00 +e8 +01 +00 +00 +0c +00 +00 +00 +00 +03 +00 +80 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +5f +5f +64 +61 +74 +61 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +5f +5f +44 +41 +54 +41 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +5d +00 +00 +00 +00 +00 +00 +00 +68 +00 +00 +00 +00 +00 +00 +00 +7d +01 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +02 +00 +00 +00 +18 +00 +00 +00 +48 +02 +00 +00 +04 +00 +00 +00 +88 +02 +00 +00 +12 +00 +00 +00 +e8 +00 +00 +00 +00 +e8 +04 +00 +00 +00 +8b +05 +00 +00 +00 +00 +8b +05 +04 +00 +00 +00 +c6 +05 +ff +ff +ff +ff +12 +c7 +05 +fc +ff +ff +ff +78 +56 +34 +12 +00 +00 +00 +00 +00 +00 +00 +00 +04 +00 +00 +00 +00 +00 +00 +00 +48 +8d +05 +00 +00 +00 +00 +48 +8d +05 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +12 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +01 +00 +00 +00 +00 +00 +00 +2d +06 +00 +00 +00 +00 +00 +00 +2d +0c +00 +00 +00 +00 +00 +00 +1d +12 +00 +00 +00 +00 +00 +00 +1d +18 +00 +00 +00 +00 +00 +00 +1d +1f +00 +00 +00 +00 +00 +00 +1d +27 +00 +00 +00 +00 +00 +00 +0e +2f +00 +00 +00 +00 +00 +00 +0e +3a +00 +00 +00 +01 +00 +00 +1d +41 +00 +00 +00 +02 +00 +00 +1d +45 +00 +00 +00 +01 +00 +00 +0e +4d +00 +00 +00 +02 +00 +00 +0e +01 +00 +00 +00 +01 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +06 +00 +00 +00 +0e +02 +00 +00 +c5 +00 +00 +00 +00 +00 +00 +00 +09 +00 +00 +00 +0e +02 +00 +00 +b3 +00 +00 +00 +00 +00 +00 +00 +0c +00 +00 +00 +0e +02 +00 +00 +b3 +00 +00 +00 +00 +00 +00 +00 +00 +5f +66 +6f +6f +00 +4c +31 +00 +4c +30 +00 +5f +70 +72 +65 +76 +00 diff --git a/modules/objfmts/macho/tests/gas64/gas_macho64_test.sh b/modules/objfmts/macho/tests/gas64/gas_macho64_test.sh new file mode 100755 index 0000000..d3ead46 --- /dev/null +++ b/modules/objfmts/macho/tests/gas64/gas_macho64_test.sh @@ -0,0 +1,3 @@ +#! /bin/sh +${srcdir}/out_test.sh macho_test modules/objfmts/macho/tests/gas64 "GAS 64-bit macho objfmt" "-f macho64 -p gas" ".o" +exit $? diff --git a/modules/objfmts/macho/tests/nasm32/Makefile.inc b/modules/objfmts/macho/tests/nasm32/Makefile.inc new file mode 100644 index 0000000..2bba5de --- /dev/null +++ b/modules/objfmts/macho/tests/nasm32/Makefile.inc @@ -0,0 +1,17 @@ +TESTS += modules/objfmts/macho/tests/nasm32/macho32_test.sh + +EXTRA_DIST += modules/objfmts/macho/tests/nasm32/macho32_test.sh +EXTRA_DIST += modules/objfmts/macho/tests/nasm32/machotest.c +EXTRA_DIST += modules/objfmts/macho/tests/nasm32/machotest.asm +EXTRA_DIST += modules/objfmts/macho/tests/nasm32/machotest.hex +EXTRA_DIST += modules/objfmts/macho/tests/nasm32/macho-reloc.asm +EXTRA_DIST += modules/objfmts/macho/tests/nasm32/macho-reloc.hex +EXTRA_DIST += modules/objfmts/macho/tests/nasm32/macho32-pext.asm +EXTRA_DIST += modules/objfmts/macho/tests/nasm32/macho32-pext.hex +EXTRA_DIST += modules/objfmts/macho/tests/nasm32/macho32-pic.asm +EXTRA_DIST += modules/objfmts/macho/tests/nasm32/macho32-pic.hex +EXTRA_DIST += modules/objfmts/macho/tests/nasm32/macho32-sect.asm +EXTRA_DIST += modules/objfmts/macho/tests/nasm32/macho32-sect.errwarn +EXTRA_DIST += modules/objfmts/macho/tests/nasm32/macho32-sect.hex +EXTRA_DIST += modules/objfmts/macho/tests/nasm32/macho32-size.asm +EXTRA_DIST += modules/objfmts/macho/tests/nasm32/macho32-size.hex diff --git a/modules/objfmts/macho/tests/nasm32/macho-reloc.asm b/modules/objfmts/macho/tests/nasm32/macho-reloc.asm new file mode 100644 index 0000000..4e8b690 --- /dev/null +++ b/modules/objfmts/macho/tests/nasm32/macho-reloc.asm @@ -0,0 +1,24 @@ +[SECTION .data] + +uhoh db 5 + +[GLOBAL blah] + +blah dw 5 +[SECTION .text] + +[EXTERN hi] +[EXTERN hi] +[EXTERN bye] + mov eax, hi+2 + mov eax, bye + mov eax, [hi] + mov eax, [bye+2] + mov eax, $$ + mov eax, $ + mov eax, $+4 + mov eax, $-$$ +;mov eax, uhoh wrt $$ +;mov eax, hi+bye +;mov eax, bye+$ +;mov eax, hi-$ diff --git a/modules/objfmts/macho/tests/nasm32/macho-reloc.hex b/modules/objfmts/macho/tests/nasm32/macho-reloc.hex new file mode 100644 index 0000000..7ef6e8a --- /dev/null +++ b/modules/objfmts/macho/tests/nasm32/macho-reloc.hex @@ -0,0 +1,393 @@ +ce +fa +ed +fe +07 +00 +00 +00 +03 +00 +00 +00 +01 +00 +00 +00 +02 +00 +00 +00 +d8 +00 +00 +00 +00 +00 +00 +00 +01 +00 +00 +00 +c0 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +2b +00 +00 +00 +f4 +00 +00 +00 +2b +00 +00 +00 +07 +00 +00 +00 +07 +00 +00 +00 +02 +00 +00 +00 +00 +00 +00 +00 +5f +5f +74 +65 +78 +74 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +5f +5f +54 +45 +58 +54 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +28 +00 +00 +00 +f4 +00 +00 +00 +00 +00 +00 +00 +20 +01 +00 +00 +07 +00 +00 +00 +00 +03 +00 +80 +00 +00 +00 +00 +00 +00 +00 +00 +5f +5f +64 +61 +74 +61 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +5f +5f +44 +41 +54 +41 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +28 +00 +00 +00 +03 +00 +00 +00 +1c +01 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +02 +00 +00 +00 +18 +00 +00 +00 +58 +01 +00 +00 +03 +00 +00 +00 +7c +01 +00 +00 +0d +00 +00 +00 +b8 +02 +00 +00 +00 +b8 +00 +00 +00 +00 +a1 +00 +00 +00 +00 +a1 +02 +00 +00 +00 +b8 +00 +00 +00 +00 +b8 +19 +00 +00 +00 +b8 +22 +00 +00 +00 +b8 +23 +00 +00 +00 +05 +05 +00 +00 +01 +00 +00 +00 +01 +00 +00 +0c +06 +00 +00 +00 +02 +00 +00 +0c +0b +00 +00 +00 +01 +00 +00 +0c +10 +00 +00 +00 +02 +00 +00 +0c +15 +00 +00 +00 +01 +00 +00 +04 +1a +00 +00 +00 +01 +00 +00 +04 +1f +00 +00 +00 +01 +00 +00 +04 +01 +00 +00 +00 +0f +02 +00 +00 +29 +00 +00 +00 +06 +00 +00 +00 +01 +00 +00 +00 +00 +00 +00 +00 +09 +00 +00 +00 +01 +00 +00 +00 +00 +00 +00 +00 +00 +62 +6c +61 +68 +00 +68 +69 +00 +62 +79 +65 +00 diff --git a/modules/objfmts/macho/tests/nasm32/macho32-sect.asm b/modules/objfmts/macho/tests/nasm32/macho32-sect.asm new file mode 100644 index 0000000..b02f02a --- /dev/null +++ b/modules/objfmts/macho/tests/nasm32/macho32-sect.asm @@ -0,0 +1,9 @@ +section .data +section __x +section __y segname=__Y +section __Z, __z +section __Y __bar +section __reallylongname12345 +section __REALLYLONGSEGNAME __1 +section __2 __reallylongsectname +section __2REALLYLONGSEGNAME __2reallylongsectname diff --git a/modules/objfmts/macho/tests/nasm32/macho32-sect.errwarn b/modules/objfmts/macho/tests/nasm32/macho32-sect.errwarn new file mode 100644 index 0000000..620e878 --- /dev/null +++ b/modules/objfmts/macho/tests/nasm32/macho32-sect.errwarn @@ -0,0 +1,7 @@ +-:2: warning: Unknown section name, defaulting to __TEXT segment +-:6: warning: section name is too long, max 16 chars; truncating +-:6: warning: Unknown section name, defaulting to __TEXT segment +-:7: warning: segment name is too long, max 16 chars; truncating +-:8: warning: section name is too long, max 16 chars; truncating +-:9: warning: segment name is too long, max 16 chars; truncating +-:9: warning: section name is too long, max 16 chars; truncating diff --git a/modules/objfmts/macho/tests/nasm32/macho32-sect.hex b/modules/objfmts/macho/tests/nasm32/macho32-sect.hex new file mode 100644 index 0000000..aeaec6f --- /dev/null +++ b/modules/objfmts/macho/tests/nasm32/macho32-sect.hex @@ -0,0 +1,789 @@ +ce +fa +ed +fe +07 +00 +00 +00 +03 +00 +00 +00 +01 +00 +00 +00 +01 +00 +00 +00 +e0 +02 +00 +00 +00 +00 +00 +00 +01 +00 +00 +00 +e0 +02 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +fc +02 +00 +00 +00 +00 +00 +00 +07 +00 +00 +00 +07 +00 +00 +00 +0a +00 +00 +00 +00 +00 +00 +00 +5f +5f +74 +65 +78 +74 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +5f +5f +54 +45 +58 +54 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +14 +03 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +80 +00 +00 +00 +00 +00 +00 +00 +00 +5f +5f +64 +61 +74 +61 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +5f +5f +44 +41 +54 +41 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +14 +03 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +5f +5f +78 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +5f +5f +54 +45 +58 +54 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +14 +03 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +04 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +5f +5f +79 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +5f +5f +59 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +14 +03 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +04 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +5f +5f +7a +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +5f +5f +5a +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +14 +03 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +5f +5f +62 +61 +72 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +5f +5f +59 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +14 +03 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +5f +5f +72 +65 +61 +6c +6c +79 +6c +6f +6e +67 +6e +61 +6d +65 +5f +5f +54 +45 +58 +54 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +14 +03 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +04 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +5f +5f +31 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +5f +5f +52 +45 +41 +4c +4c +59 +4c +4f +4e +47 +53 +45 +47 +4e +00 +00 +00 +00 +00 +00 +00 +00 +14 +03 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +5f +5f +72 +65 +61 +6c +6c +79 +6c +6f +6e +67 +73 +65 +63 +74 +5f +5f +32 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +14 +03 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +5f +5f +32 +72 +65 +61 +6c +6c +79 +6c +6f +6e +67 +73 +65 +63 +5f +5f +32 +52 +45 +41 +4c +4c +59 +4c +4f +4e +47 +53 +45 +47 +00 +00 +00 +00 +00 +00 +00 +00 +14 +03 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +02 +00 +00 +00 +18 +00 +00 +00 +14 +03 +00 +00 +00 +00 +00 +00 +14 +03 +00 +00 +01 +00 +00 +00 +00 diff --git a/modules/objfmts/macho/tests/nasm32/macho32_test.sh b/modules/objfmts/macho/tests/nasm32/macho32_test.sh new file mode 100755 index 0000000..e5fdcc9 --- /dev/null +++ b/modules/objfmts/macho/tests/nasm32/macho32_test.sh @@ -0,0 +1,3 @@ +#! /bin/sh +${srcdir}/out_test.sh macho_test modules/objfmts/macho/tests/nasm32 "32-bit macho objfmt" "-f macho32" ".o" +exit $? diff --git a/modules/objfmts/macho/tests/nasm32/machotest.asm b/modules/objfmts/macho/tests/nasm32/machotest.asm new file mode 100644 index 0000000..660f714 --- /dev/null +++ b/modules/objfmts/macho/tests/nasm32/machotest.asm @@ -0,0 +1,87 @@ +; test source file for assembling to MACH-O +; build with : +; yasm -f macho machotest.asm +; gcc -o machotest machotest.c machotest.o + +; This file should test the following: +; [1] Define and export a global text-section symbol +; [2] Define and export a global data-section symbol +; [3] Define and export a global BSS-section symbol +; [4] Define a non-global text-section symbol +; [5] Define a non-global data-section symbol +; [6] Define a non-global BSS-section symbol +; [7] Define a COMMON symbol +; [8] Define a NASM local label +; [9] Reference a NASM local label +; [10] Import an external symbol (note: printf replaced by another call) +; [11] Make a PC-relative call to an external symbol +; [12] Reference a text-section symbol in the text section +; [13] Reference a data-section symbol in the text section +; [14] Reference a BSS-section symbol in the text section +; [15] Reference a text-section symbol in the data section +; [16] Reference a data-section symbol in the data section +; [17] Reference a BSS-section symbol in the data section + +[BITS 32] +[GLOBAL _lrotate] ; [1] +[GLOBAL _greet] ; [1] +[GLOBAL _asmstr] ; [2] +[GLOBAL _textptr] ; [2] +[GLOBAL _selfptr] ; [2] +[GLOBAL _integer] ; [3] +[EXTERN _druck] ; [10] +[COMMON _commvar 4] ; [7] + +[SECTION .text] + +; prototype: long lrotate(long x, int num); +_lrotate: ; [1] + push ebp + mov ebp,esp + mov eax,[ebp+8] + mov ecx,[ebp+12] +.label rol eax,1 ; [4] [8] + loop .label ; [9] [12] + mov esp,ebp + pop ebp + ret + +; prototype: void greet(void); +_greet + mov eax,[_integer] ; [14] + inc eax + mov [localint],eax ; [14] + push dword [_commvar] + mov eax,[localptr] ; [13] + push dword [eax] + push dword [_integer] ; [1] [14] + push dword _printfstr ; [13] + call _druck ; [11] + add esp,16 + ret + +; some internal calls + call _greet + call _lrotate.label + +[SECTION .data] + +; a string +_asmstr db 'hello, world', 0 ; [2] + +; a string for Printf +_printfstr db "integer==%d, localint==%d, commvar=%d" + db 10, 0 + +; some pointers +localptr dd localint ; [5] [17] +_textptr dd _greet ; [15] +_selfptr dd _selfptr ; [16] + +[SECTION .bss] + +; an integer +_integer resd 1 ; [3] + +; a local integer +localint resd 1 ; [6] diff --git a/modules/objfmts/macho/tests/nasm32/machotest.c b/modules/objfmts/macho/tests/nasm32/machotest.c new file mode 100644 index 0000000..42fc92c --- /dev/null +++ b/modules/objfmts/macho/tests/nasm32/machotest.c @@ -0,0 +1,45 @@ +/* + * test source file for assembling to ELF + * copied from cofftest.c; s/coff/elf/g + * build with (under Linux, for example): + * yasm -f elf elftest.asm + * gcc -o elftest elftest.c elftest.o + */ + +#include <stdio.h> + +extern int lrotate(long, int); +extern void greet(void); +extern char asmstr[]; +extern void *selfptr; +extern void *textptr; +extern int integer, commvar; + +int main(void) { + + printf("Testing lrotate: should get 0x00400000, 0x00000001\n"); + printf("lrotate(0x00040000, 4) = 0x%08lx\n", lrotate(0x40000,4)); + printf("lrotate(0x00040000, 14) = 0x%08lx\n", lrotate(0x40000,14)); + + printf("This string should read `hello, world': `%s'\n", asmstr); + + printf("The integers here should be 1234, 1235 and 4321:\n"); + integer = 1234; + commvar = 4321; + greet(); + + printf("These pointers should be equal: %p and %p\n", + &greet, textptr); + + printf("So should these: %p and %p\n", selfptr, &selfptr); +} + +/* + there is no support for dynamically linkable objects in current + mach-o module. Therefore put "printf" statement here and redirect + the asm call to druck() +*/ +void druck( char *string, int a, int b, int c ) +{ + printf(string,a,b,c); +} diff --git a/modules/objfmts/macho/tests/nasm32/machotest.hex b/modules/objfmts/macho/tests/nasm32/machotest.hex new file mode 100644 index 0000000..e6791e4 --- /dev/null +++ b/modules/objfmts/macho/tests/nasm32/machotest.hex @@ -0,0 +1,692 @@ +ce +fa +ed +fe +07 +00 +00 +00 +03 +00 +00 +00 +01 +00 +00 +00 +02 +00 +00 +00 +1c +01 +00 +00 +00 +00 +00 +00 +01 +00 +00 +00 +04 +01 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +8f +00 +00 +00 +38 +01 +00 +00 +87 +00 +00 +00 +07 +00 +00 +00 +07 +00 +00 +00 +03 +00 +00 +00 +00 +00 +00 +00 +5f +5f +74 +65 +78 +74 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +5f +5f +54 +45 +58 +54 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +47 +00 +00 +00 +38 +01 +00 +00 +00 +00 +00 +00 +c0 +01 +00 +00 +07 +00 +00 +00 +00 +03 +00 +80 +00 +00 +00 +00 +00 +00 +00 +00 +5f +5f +64 +61 +74 +61 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +5f +5f +44 +41 +54 +41 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +47 +00 +00 +00 +40 +00 +00 +00 +7f +01 +00 +00 +00 +00 +00 +00 +f8 +01 +00 +00 +03 +00 +00 +00 +00 +01 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +5f +5f +62 +73 +73 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +5f +5f +44 +41 +54 +41 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +87 +00 +00 +00 +08 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +01 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +02 +00 +00 +00 +18 +00 +00 +00 +10 +02 +00 +00 +08 +00 +00 +00 +70 +02 +00 +00 +44 +00 +00 +00 +55 +89 +e5 +8b +45 +08 +8b +4d +0c +d1 +c0 +e2 +fc +89 +ec +5d +c3 +a1 +87 +00 +00 +00 +40 +a3 +8b +00 +00 +00 +ff +35 +00 +00 +00 +00 +a1 +7b +00 +00 +00 +ff +30 +ff +35 +87 +00 +00 +00 +68 +54 +00 +00 +00 +e8 +c7 +ff +ff +ff +83 +c4 +10 +c3 +e8 +cf +ff +ff +ff +e8 +c2 +ff +ff +ff +68 +65 +6c +6c +6f +2c +20 +77 +6f +72 +6c +64 +00 +69 +6e +74 +65 +67 +65 +72 +3d +3d +25 +64 +2c +20 +6c +6f +63 +61 +6c +69 +6e +74 +3d +3d +25 +64 +2c +20 +63 +6f +6d +6d +76 +61 +72 +3d +25 +64 +0a +00 +8b +00 +00 +00 +11 +00 +00 +00 +83 +00 +00 +00 +00 +12 +00 +00 +00 +03 +00 +00 +04 +18 +00 +00 +00 +03 +00 +00 +04 +1e +00 +00 +00 +07 +00 +00 +0c +23 +00 +00 +00 +02 +00 +00 +04 +2b +00 +00 +00 +03 +00 +00 +04 +30 +00 +00 +00 +02 +00 +00 +04 +35 +00 +00 +00 +06 +00 +00 +0d +34 +00 +00 +00 +03 +00 +00 +04 +38 +00 +00 +00 +01 +00 +00 +04 +3c +00 +00 +00 +02 +00 +00 +04 +01 +00 +00 +00 +0f +01 +00 +00 +00 +00 +00 +00 +0a +00 +00 +00 +0f +01 +00 +00 +11 +00 +00 +00 +11 +00 +00 +00 +0f +02 +00 +00 +47 +00 +00 +00 +19 +00 +00 +00 +0f +02 +00 +00 +7f +00 +00 +00 +22 +00 +00 +00 +0f +02 +00 +00 +83 +00 +00 +00 +2b +00 +00 +00 +0f +03 +00 +00 +87 +00 +00 +00 +34 +00 +00 +00 +01 +00 +00 +00 +00 +00 +00 +00 +3b +00 +00 +00 +01 +00 +00 +00 +04 +00 +00 +00 +00 +5f +6c +72 +6f +74 +61 +74 +65 +00 +5f +67 +72 +65 +65 +74 +00 +5f +61 +73 +6d +73 +74 +72 +00 +5f +74 +65 +78 +74 +70 +74 +72 +00 +5f +73 +65 +6c +66 +70 +74 +72 +00 +5f +69 +6e +74 +65 +67 +65 +72 +00 +5f +64 +72 +75 +63 +6b +00 +5f +63 +6f +6d +6d +76 +61 +72 +00 diff --git a/modules/objfmts/macho/tests/nasm64/Makefile.inc b/modules/objfmts/macho/tests/nasm64/Makefile.inc new file mode 100644 index 0000000..93ec45e --- /dev/null +++ b/modules/objfmts/macho/tests/nasm64/Makefile.inc @@ -0,0 +1,10 @@ +TESTS += modules/objfmts/macho/tests/nasm64/macho64_test.sh + +EXTRA_DIST += modules/objfmts/macho/tests/nasm64/nasm-macho64-pic.asm +EXTRA_DIST += modules/objfmts/macho/tests/nasm64/nasm-macho64-pic.hex +EXTRA_DIST += modules/objfmts/macho/tests/nasm64/macho64_test.sh +EXTRA_DIST += modules/objfmts/macho/tests/nasm64/machotest64.c +EXTRA_DIST += modules/objfmts/macho/tests/nasm64/machotest64.asm +EXTRA_DIST += modules/objfmts/macho/tests/nasm64/machotest64.hex +EXTRA_DIST += modules/objfmts/macho/tests/nasm64/macho-reloc64-err.asm +EXTRA_DIST += modules/objfmts/macho/tests/nasm64/macho-reloc64-err.errwarn diff --git a/modules/objfmts/macho/tests/nasm64/macho-reloc64-err.asm b/modules/objfmts/macho/tests/nasm64/macho-reloc64-err.asm new file mode 100644 index 0000000..06595a5 --- /dev/null +++ b/modules/objfmts/macho/tests/nasm64/macho-reloc64-err.asm @@ -0,0 +1,32 @@ +[BITS 64] +[SECTION .data] + +uhoh db 5 + +[GLOBAL blah] + +blah dw 5 +[GLOBAL aha] +aha dq blah +aha2 dq blah+4 +aha3 dq blah-uhoh + +[SECTION .text] + +[EXTERN hi] +[EXTERN hi] +[EXTERN bye] +[BITS 64] + mov rax, hi+2 + mov rax, bye + mov rax, [qword hi] + mov rdi, [rip+ hi] + mov rax, [bye+2] + mov rax, $$ + mov rax, $ + mov rax, $+4 + mov rax, $-$$ +mov eax, uhoh wrt $$ +;mov eax, hi+bye +;mov eax, bye+$ +;mov eax, hi-$ diff --git a/modules/objfmts/macho/tests/nasm64/macho-reloc64-err.errwarn b/modules/objfmts/macho/tests/nasm64/macho-reloc64-err.errwarn new file mode 100644 index 0000000..1a11883 --- /dev/null +++ b/modules/objfmts/macho/tests/nasm64/macho-reloc64-err.errwarn @@ -0,0 +1,8 @@ +-:20: error: macho: sorry, cannot apply 32 bit absolute relocations in 64 bit mode, consider "[_symbol wrt rip]" for mem access, "qword" and "dq _foo" for pointers. +-:21: error: macho: sorry, cannot apply 32 bit absolute relocations in 64 bit mode, consider "[_symbol wrt rip]" for mem access, "qword" and "dq _foo" for pointers. +-:23: error: macho: sorry, cannot apply 32 bit absolute relocations in 64 bit mode, consider "[_symbol wrt rip]" for mem access, "qword" and "dq _foo" for pointers. +-:24: error: macho: sorry, cannot apply 32 bit absolute relocations in 64 bit mode, consider "[_symbol wrt rip]" for mem access, "qword" and "dq _foo" for pointers. +-:25: error: macho: sorry, cannot apply 32 bit absolute relocations in 64 bit mode, consider "[_symbol wrt rip]" for mem access, "qword" and "dq _foo" for pointers. +-:26: error: macho: sorry, cannot apply 32 bit absolute relocations in 64 bit mode, consider "[_symbol wrt rip]" for mem access, "qword" and "dq _foo" for pointers. +-:27: error: macho: sorry, cannot apply 32 bit absolute relocations in 64 bit mode, consider "[_symbol wrt rip]" for mem access, "qword" and "dq _foo" for pointers. +-:29: error: macho: invalid WRT diff --git a/modules/objfmts/macho/tests/nasm64/macho64_test.sh b/modules/objfmts/macho/tests/nasm64/macho64_test.sh new file mode 100755 index 0000000..f7c0bfb --- /dev/null +++ b/modules/objfmts/macho/tests/nasm64/macho64_test.sh @@ -0,0 +1,3 @@ +#! /bin/sh +${srcdir}/out_test.sh macho_test modules/objfmts/macho/tests/nasm64 "64-bit macho objfmt" "-f macho64" ".o" +exit $? diff --git a/modules/objfmts/macho/tests/nasm64/machotest64.asm b/modules/objfmts/macho/tests/nasm64/machotest64.asm new file mode 100644 index 0000000..e19e4fd --- /dev/null +++ b/modules/objfmts/macho/tests/nasm64/machotest64.asm @@ -0,0 +1,111 @@ +; test source file for assembling to MACH-O +; build with : +; yasm -f macho -m amd64 machotest64.asm +; gcc -m64 -o machotest64 machotest64.c machotest64.o + +; This file should test the following: +; [1] Define and export a global text-section symbol +; [2] Define and export a global data-section symbol +; [3] Define and export a global BSS-section symbol +; [4] Define a non-global text-section symbol +; [5] Define a non-global data-section symbol +; [6] Define a non-global BSS-section symbol +; [7] Define a COMMON symbol +; [8] Define a NASM local label +; [9] Reference a NASM local label +; [10] Import an external symbol (note: printf replaced by another call) +; [11] Make a PC-relative call to an external symbol +; [12] Reference a text-section symbol in the text section +; [13] Reference a data-section symbol in the text section +; [14] Reference a BSS-section symbol in the text section +; [15] Reference a text-section symbol in the data section +; [16] Reference a data-section symbol in the data section +; [17] Reference a BSS-section symbol in the data section +; [18] Perform a 64 Bit relocation in the text section + +[BITS 64] +[GLOBAL _lrotate] ; [1] +[GLOBAL _greet] ; [1] +[GLOBAL _asmstr] ; [2] +[GLOBAL _textptr] ; [2] +[GLOBAL _selfptr] ; [2] +[GLOBAL _integer] ; [3] +[EXTERN _druck] ; [10] +[COMMON _commvar 4] ; [7] +[GLOBAL _getstr] ; +[GLOBAL _readgreet] ; + +[SECTION .text] + +; prototype: long lrotate(long x, int num); +_lrotate: ; [1] + push rcx + mov rax,rdi + mov rcx,rsi +.label rol rax,1 ; [4] [8] + loop .label ; [9] [12] + pop rcx + ret + +_getstr: + mov rax,qword _asmstr + ret + +_readgreet: + mov rax,[qword localint] ; [18] + ret + +_retrievelabel: + mov rax,[qword localptr] + ret + +; prototype: void greet(void); +; calls "void druck(a,b,c,d); +_greet mov rax,[_integer wrt rip] ; [14] + inc rax + mov [localint wrt rip],rax ; [14] + push rdi + push rsi + push rdx + push rcx + mov rdi,qword _printfstr + mov rsi,[_integer wrt rip] + mov rdx,[localptr wrt rip] + mov rdx,[rdx] + mov rcx,[_commvar wrt rip] + call _druck + pop rcx + pop rdx + pop rsi + pop rdi + ret + +; some internal calls + call _greet + call _retrievelabel + +[SECTION .data] + +; a string for Printf +_printfstr db "integer==%d, localint==%d, commvar=%d" + db 10, 0 + +; some pointers +localptr dq localint ; [5] [17] +_textptr dq _greet ; [15] +_selfptr dq _selfptr ; [16] + +;[section .data2 align=16] + +; a string +_asmstr db 'hello, world', 0 ; [2] + + + +[SECTION .bss] + +; an integer +_integer resq 1 ; [3] + +; a local integer +localint resq 1 ; [6] diff --git a/modules/objfmts/macho/tests/nasm64/machotest64.c b/modules/objfmts/macho/tests/nasm64/machotest64.c new file mode 100644 index 0000000..c0f1e3c --- /dev/null +++ b/modules/objfmts/macho/tests/nasm64/machotest64.c @@ -0,0 +1,56 @@ +/* + * test source file for assembling to Mach-O + * copied from cofftest.c, adapted to current limitations + * in Mach-O module + * build with (under OSX Tiger/Leopard, for example): + * yasm -f macho -m amd64 machotest64.asm + * gcc -m64 -o machotest64 machotest64.c machotest64.o + */ + +#include <stdio.h> + +extern long lrotate(long, long); +extern void greet(void); +extern long readgreet(void); +extern char asmstr[]; +extern void *selfptr; +extern void *textptr; +extern int integer, commvar; +extern char *getstr(void); + +int main(void) { + + printf("Testing lrotate: should get 0x0000000000400000, 0x0000000000000001\n"); + printf("lrotate(0x00040000, 4 ) = 0x%016lx\n", lrotate(0x40000,4)); + printf("lrotate(0x00040000, 46) = 0x%016lx\n", lrotate(0x40000,46)); + + printf("This string should read `hello, world': `%s'\n", asmstr); + { + long a,b; + a = (long)asmstr; + b = (long)getstr(); + printf("The pointers %lx and %lx should be equal\n",a,b); + } + printf("This string should read `hello, world': `%s'\n", getstr()); + + printf("The integers here should be 1234, 1235 and 4321:\n"); + integer = 1234; + commvar = 4321; + greet(); + printf("The absolute addressing to the asm-local integer should yield in 1235:\n%ld\n",readgreet()); + + printf("These pointers should be equal: %p and %p\n", + &greet, textptr); + + printf("So should these: %p and %p\n", selfptr, &selfptr); +} + +/* + there is no support for dynamically linkable objects in current + mach-o module. Therefore put "printf" statement here and redirect + the asm call to druck() +*/ +void druck( char *string, int a, int b, int c ) +{ + printf(string,a,b,c); +} diff --git a/modules/objfmts/macho/tests/nasm64/machotest64.hex b/modules/objfmts/macho/tests/nasm64/machotest64.hex new file mode 100644 index 0000000..2139518 --- /dev/null +++ b/modules/objfmts/macho/tests/nasm64/machotest64.hex @@ -0,0 +1,1058 @@ +cf +fa +ed +fe +07 +00 +00 +01 +03 +00 +00 +00 +01 +00 +00 +00 +02 +00 +00 +00 +50 +01 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +19 +00 +00 +00 +38 +01 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +d6 +00 +00 +00 +00 +00 +00 +00 +70 +01 +00 +00 +00 +00 +00 +00 +c6 +00 +00 +00 +00 +00 +00 +00 +07 +00 +00 +00 +07 +00 +00 +00 +03 +00 +00 +00 +00 +00 +00 +00 +5f +5f +74 +65 +78 +74 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +5f +5f +54 +45 +58 +54 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +7a +00 +00 +00 +00 +00 +00 +00 +70 +01 +00 +00 +00 +00 +00 +00 +38 +02 +00 +00 +0a +00 +00 +00 +00 +03 +00 +80 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +5f +5f +64 +61 +74 +61 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +5f +5f +44 +41 +54 +41 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +7a +00 +00 +00 +00 +00 +00 +00 +4c +00 +00 +00 +00 +00 +00 +00 +ea +01 +00 +00 +00 +00 +00 +00 +88 +02 +00 +00 +03 +00 +00 +00 +00 +01 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +5f +5f +62 +73 +73 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +5f +5f +44 +41 +54 +41 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +c6 +00 +00 +00 +00 +00 +00 +00 +10 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +01 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +02 +00 +00 +00 +18 +00 +00 +00 +a0 +02 +00 +00 +0f +00 +00 +00 +90 +03 +00 +00 +92 +00 +00 +00 +51 +48 +89 +f8 +48 +89 +f1 +48 +d1 +c0 +e2 +fb +59 +c3 +48 +b8 +00 +00 +00 +00 +00 +00 +00 +00 +c3 +48 +a1 +00 +00 +00 +00 +00 +00 +00 +00 +c3 +48 +a1 +00 +00 +00 +00 +00 +00 +00 +00 +c3 +48 +8b +05 +00 +00 +00 +00 +48 +ff +c0 +48 +89 +05 +00 +00 +00 +00 +57 +56 +52 +51 +48 +bf +00 +00 +00 +00 +00 +00 +00 +00 +48 +8b +35 +00 +00 +00 +00 +48 +8b +15 +00 +00 +00 +00 +48 +8b +12 +48 +8b +0d +00 +00 +00 +00 +e8 +00 +00 +00 +00 +59 +5a +5e +5f +c3 +e8 +ba +ff +ff +ff +e8 +aa +ff +ff +ff +69 +6e +74 +65 +67 +65 +72 +3d +3d +25 +64 +2c +20 +6c +6f +63 +61 +6c +69 +6e +74 +3d +3d +25 +64 +2c +20 +63 +6f +6d +6d +76 +61 +72 +3d +25 +64 +0a +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +68 +65 +6c +6c +6f +2c +20 +77 +6f +72 +6c +64 +00 +00 +00 +10 +00 +00 +00 +02 +00 +00 +0e +1b +00 +00 +00 +0b +00 +00 +0e +26 +00 +00 +00 +0d +00 +00 +0e +32 +00 +00 +00 +05 +00 +00 +1d +3c +00 +00 +00 +0b +00 +00 +1d +46 +00 +00 +00 +0e +00 +00 +0e +51 +00 +00 +00 +05 +00 +00 +1d +58 +00 +00 +00 +0d +00 +00 +1d +62 +00 +00 +00 +07 +00 +00 +1d +67 +00 +00 +00 +06 +00 +00 +2d +27 +00 +00 +00 +0b +00 +00 +0e +2f +00 +00 +00 +01 +00 +00 +0e +37 +00 +00 +00 +04 +00 +00 +0e +01 +00 +00 +00 +0f +01 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +0a +00 +00 +00 +0f +01 +00 +00 +2f +00 +00 +00 +00 +00 +00 +00 +11 +00 +00 +00 +0f +02 +00 +00 +b9 +00 +00 +00 +00 +00 +00 +00 +19 +00 +00 +00 +0f +02 +00 +00 +a9 +00 +00 +00 +00 +00 +00 +00 +22 +00 +00 +00 +0f +02 +00 +00 +b1 +00 +00 +00 +00 +00 +00 +00 +2b +00 +00 +00 +0f +03 +00 +00 +c6 +00 +00 +00 +00 +00 +00 +00 +34 +00 +00 +00 +01 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +3b +00 +00 +00 +01 +00 +00 +00 +04 +00 +00 +00 +00 +00 +00 +00 +44 +00 +00 +00 +0f +01 +00 +00 +0e +00 +00 +00 +00 +00 +00 +00 +4c +00 +00 +00 +0f +01 +00 +00 +19 +00 +00 +00 +00 +00 +00 +00 +57 +00 +00 +00 +0e +01 +00 +00 +07 +00 +00 +00 +00 +00 +00 +00 +66 +00 +00 +00 +0e +03 +00 +00 +ce +00 +00 +00 +00 +00 +00 +00 +6f +00 +00 +00 +0e +01 +00 +00 +24 +00 +00 +00 +00 +00 +00 +00 +7e +00 +00 +00 +0e +02 +00 +00 +a1 +00 +00 +00 +00 +00 +00 +00 +87 +00 +00 +00 +0e +02 +00 +00 +7a +00 +00 +00 +00 +00 +00 +00 +00 +5f +6c +72 +6f +74 +61 +74 +65 +00 +5f +67 +72 +65 +65 +74 +00 +5f +61 +73 +6d +73 +74 +72 +00 +5f +74 +65 +78 +74 +70 +74 +72 +00 +5f +73 +65 +6c +66 +70 +74 +72 +00 +5f +69 +6e +74 +65 +67 +65 +72 +00 +5f +64 +72 +75 +63 +6b +00 +5f +63 +6f +6d +6d +76 +61 +72 +00 +5f +67 +65 +74 +73 +74 +72 +00 +5f +72 +65 +61 +64 +67 +72 +65 +65 +74 +00 +5f +6c +72 +6f +74 +61 +74 +65 +2e +6c +61 +62 +65 +6c +00 +6c +6f +63 +61 +6c +69 +6e +74 +00 +5f +72 +65 +74 +72 +69 +65 +76 +65 +6c +61 +62 +65 +6c +00 +6c +6f +63 +61 +6c +70 +74 +72 +00 +5f +70 +72 +69 +6e +74 +66 +73 +74 +72 +00 diff --git a/modules/objfmts/macho/tests/nasm64/nasm-macho64-pic.asm b/modules/objfmts/macho/tests/nasm64/nasm-macho64-pic.asm new file mode 100644 index 0000000..7b46163 --- /dev/null +++ b/modules/objfmts/macho/tests/nasm64/nasm-macho64-pic.asm @@ -0,0 +1,34 @@ +[extern _foo] + +call _foo +; r_type=X86_64_RELOC_BRANCH, r_length=2, r_extern=1, r_pcrel=1, r_symbolnum=_foo +; E8 00 00 00 00 + +call _foo+4 +; r_type=X86_64_RELOC_BRANCH, r_length=2, r_extern=1, r_pcrel=1, r_symbolnum=_foo +; E8 04 00 00 00 + +mov rax, [rel _foo wrt ..gotpcrel] +; r_type=X86_64_RELOC_GOT_LOAD, r_length=2, r_extern=1, r_pcrel=1, r_symbolnum=_foo +; 48 8B 05 00 00 00 00 + +push qword [rel _foo wrt ..gotpcrel] +; r_type=X86_64_RELOC_GOT, r_length=2, r_extern=1, r_pcrel=1, r_symbolnum=_foo +; FF 35 00 00 00 00 + +mov eax, [rel _foo] +; r_type=X86_64_RELOC_SIGNED, r_length=2, r_extern=1, r_pcrel=1, r_symbolnum=_foo +; 8B 05 00 00 00 00 + +mov eax, [rel _foo+4] +; r_type=X86_64_RELOC_SIGNED, r_length=2, r_extern=1, r_pcrel=1, r_symbolnum=_foo +; 8B 05 04 00 00 00 + +mov [rel _foo], byte 12h +; r_type=X86_64_RELOC_SIGNED, r_length=2, r_extern=1, r_pcrel=1, r_symbolnum=_foo +; C6 05 FF FF FF FF 12 + +mov dword [rel _foo], 0x12345678 +; r_type=X86_64_RELOC_SIGNED, r_length=2, r_extern=1, r_pcrel=1, r_symbolnum=_foo +; C7 05 FC FF FF FF 78 56 34 12 + diff --git a/modules/objfmts/macho/tests/nasm64/nasm-macho64-pic.hex b/modules/objfmts/macho/tests/nasm64/nasm-macho64-pic.hex new file mode 100644 index 0000000..83bb7bb --- /dev/null +++ b/modules/objfmts/macho/tests/nasm64/nasm-macho64-pic.hex @@ -0,0 +1,346 @@ +cf +fa +ed +fe +07 +00 +00 +01 +03 +00 +00 +00 +01 +00 +00 +00 +02 +00 +00 +00 +b0 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +19 +00 +00 +00 +98 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +34 +00 +00 +00 +00 +00 +00 +00 +d0 +00 +00 +00 +00 +00 +00 +00 +34 +00 +00 +00 +00 +00 +00 +00 +07 +00 +00 +00 +07 +00 +00 +00 +01 +00 +00 +00 +00 +00 +00 +00 +5f +5f +74 +65 +78 +74 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +5f +5f +54 +45 +58 +54 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +34 +00 +00 +00 +00 +00 +00 +00 +d0 +00 +00 +00 +00 +00 +00 +00 +04 +01 +00 +00 +08 +00 +00 +00 +00 +03 +00 +80 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +02 +00 +00 +00 +18 +00 +00 +00 +44 +01 +00 +00 +01 +00 +00 +00 +54 +01 +00 +00 +06 +00 +00 +00 +e8 +00 +00 +00 +00 +e8 +04 +00 +00 +00 +48 +8b +05 +00 +00 +00 +00 +ff +35 +00 +00 +00 +00 +8b +05 +00 +00 +00 +00 +8b +05 +04 +00 +00 +00 +c6 +05 +ff +ff +ff +ff +12 +c7 +05 +fc +ff +ff +ff +78 +56 +34 +12 +01 +00 +00 +00 +00 +00 +00 +2d +06 +00 +00 +00 +00 +00 +00 +2d +0d +00 +00 +00 +00 +00 +00 +3d +13 +00 +00 +00 +00 +00 +00 +4d +19 +00 +00 +00 +00 +00 +00 +1d +1f +00 +00 +00 +00 +00 +00 +1d +25 +00 +00 +00 +00 +00 +00 +1d +2c +00 +00 +00 +00 +00 +00 +1d +01 +00 +00 +00 +01 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +5f +66 +6f +6f +00 |