dyngen Mach-O support for i386 and x86_64 v2
PATCH 1/4 dyngen Mach-O support for i386 and x86_64 v2
This patch extends the existing support for the Mach-O binary format in dyngen from PowerPC to PowerPC, i386 and x86_64.
v2 now includes support for TCG
dyngen.c renamed from dyngen.c.orig +322 -44
@@ -196,16 +196,40 @@#include <mach-o/nlist.h> #include <mach-o/reloc.h> #include <mach-o/ppc/reloc.h> +#include <mach-o/x86_64/reloc.h> +#if defined(HOST_PPC ) || defined(HOST_I386 ) +# if defined(HOST_I386 ) +# define mach_check_cputype (x) ((x) == CPU_TYPE_I386) +# else +# define mach_check_cputype (x) ((x) == CPU_TYPE_POWERPC) +# endif # define check_mach_header (x) (x.magic == MH_MAGIC) +# define SEGMENT_COMMAND segment_command +# define MACH_HEADER mach_header +# define SECTION section +# define NLIST nlist +#elif defined(HOST_X86_64 ) +# define mach_check_cputype (x) ((x) == CPU_TYPE_X86_64) +# define check_mach_header (x) (x.magic == MH_MAGIC_64) +# define SEGMENT_COMMAND segment_command_64 +# define MACH_HEADER mach_header_64 +# define SECTION section_64 +# define NLIST nlist_64 +#endif +typedef int32_t host_long ;typedef uint32_t host_ulong ;struct nlist_extended {union { +#ifdef HOST_X86_64 +unsigned int n_strx; +#else char *n_name;long n_strx; +#endif } n_un;unsigned char n_type;unsigned char n_sect;
@@ -833,16 +857,16 @@#ifdef CONFIG_FORMAT_MACH /* File Header */ -struct mach_header mach_hdr; +struct MACH_HEADER mach_hdr;/* commands */ -struct segment_command *segment =0 ; +struct SEGMENT_COMMAND *segment =0 ;struct dysymtab_command *dysymtabcmd =0 ;struct symtab_command *symtabcmd =0 ;/* section */ -struct section *section_hdr; -struct section *text_sec_hdr; +struct SECTION *section_hdr; +struct SECTION *text_sec_hdr;uint8_t **sdata;/* relocs */
@@ -850,7 +874,7 @@/* symbols */ EXE_SYM *symtab; -struct nlist *symtab_std; +struct NLIST *symtab_std;char *strtab;/* indirect symbols */
@@ -880,11 +904,11 @@ }/* find a section index given its segname, sectname */ -static int find_mach_sec_index (struct section *section_hdr,int shnum,const char *segname, +static int find_mach_sec_index (struct SECTION *section_hdr,int shnum,const char *segname,const char *sectname) {int i; -struct section *sec = section_hdr; +struct SECTION *sec = section_hdr;for (i =0 ; i < shnum; i++, sec++) {if (!sec->segname || !sec->sectname)
@@ -896,7 +920,7 @@ }/* find a section header given its segname, sectname */ -struct section *find_mach_sec_hdr (struct section *section_hdr,int shnum,const char *segname, +struct SECTION *find_mach_sec_hdr (struct SECTION *section_hdr,int shnum,const char *segname,const char *sectname) {int index =find_mach_sec_index (section_hdr, shnum, segname, sectname);
@@ -949,26 +973,28 @@* Find symbol name given a (virtual) address, and a section which is of type * S_NON_LAZY_SYMBOL_POINTERS or S_LAZY_SYMBOL_POINTERS or S_SYMBOL_STUBS */ -static const char *find_reloc_name_in_sec_ptr (int address,struct section * sec_hdr) +static const char *find_reloc_name_in_sec_ptr (int address,struct SECTION * sec_hdr) {unsigned int tocindex, symindex, size;const char *name =0 ; +int section_type;/* Sanity check */ if (!( address >= sec_hdr->addr && address < (sec_hdr->addr + sec_hdr->size) ) )return (char *)0 ; -if ( sec_hdr->flags &S_SYMBOL_STUBS ){ + section_type = sec_hdr->flags &SECTION_TYPE ; +if ( section_type ==S_SYMBOL_STUBS ){ size = sec_hdr->reserved2;if (size ==0 )error ("size = 0" ); } -else if ( sec_hdr->flags &S_LAZY_SYMBOL_POINTERS || - sec_hdr->flags &S_NON_LAZY_SYMBOL_POINTERS ) +else if ( section_type ==S_LAZY_SYMBOL_POINTERS || + section_type ==S_NON_LAZY_SYMBOL_POINTERS ) size =sizeof (unsigned long );else -return 0 ; +return NULL ;/* Compute our index in toc */ tocindex = (address - sec_hdr->addr)/size;
@@ -1000,10 +1026,39 @@int other_half=0 ;/* init the slide value */ +#ifdef HOST_X86_64 /* no scattered on x86_64 */ +switch (rel->r_length) + { +case 0 : *sslide = *(uint8_t *)(text + rel->r_address);break ; +case 1 : *sslide = *(uint16_t *)(text + rel->r_address);break ; +case 2 : *sslide = *(uint32_t *)(text + rel->r_address);break ; +case 3 : *sslide = *(uint64_t *)(text + rel->r_address);break ; + } +#else *sslide =0 ; -if (R_SCATTERED & rel->r_address) -return (char *)find_reloc_name_given_its_address (sca_rel->r_value); +if (R_SCATTERED & rel->r_address) { +char *name = (char *)find_reloc_name_given_its_address (sca_rel->r_value); + +/* search it in the full symbol list, if not found */ +if (!name) { +int i; +for (i =0 ; i < nb_syms; i++) { +EXE_SYM *sym = &symtab[i]; +if (sym->st_value == sca_rel->r_value) { + name =get_sym_name (sym); +switch (sca_rel->r_type) { +case GENERIC_RELOC_VANILLA : + *sslide = *(uint32_t *)(text + sca_rel->r_address) - sca_rel->r_value; +break ; + } +break ; + } + } + } +return name; + } +#endif if (rel->r_extern) {
@@ -1035,14 +1090,21 @@ sectoffset = ( *(uint32_t *)(text + rel->r_address) &0x03fffffc );if (sectoffset &0x02000000 ) sectoffset |=0xfc000000 ;break ; +case GENERIC_RELOC_VANILLA : + sectoffset = *(uint32_t *)(text + rel->r_address); +break ;default :error ("switch(rel->type) not found" ); } -if (rel->r_pcrel) +if (rel->r_pcrel) { sectoffset += rel->r_address; +#if defined(HOST_I386 ) + sectoffset += (1 << rel->r_length); +#endif + } -if (rel->r_type ==PPC_RELOC_BR24 ) +if (rel->r_type ==PPC_RELOC_BR24 || rel->r_pcrel) name = (char *)find_reloc_name_in_sec_ptr ((int )sectoffset, §ion_hdr[sectnum-1 ]);/* search it in the full symbol list, if not found */
@@ -1079,7 +1141,7 @@struct load_command lc;unsigned int i, j;EXE_SYM *sym; -struct nlist *syment; +struct NLIST *syment; fd =open (filename,O_RDONLY );if (fd <0 )
@@ -1094,7 +1156,7 @@error ("bad Mach header" ); } -if (mach_hdr.cputype !=CPU_TYPE_POWERPC ) +if (!mach_check_cputype (mach_hdr.cputype))error ("Unsupported CPU" );if (mach_hdr.filetype !=MH_OBJECT )
@@ -1105,29 +1167,37 @@ {if (read (fd, &lc,sizeof (struct load_command )) !=sizeof (struct load_command ))error ("unable to read load_command" ); -if (lc.cmd ==LC_SEGMENT ) - { +switch (lc.cmd) { +#ifdef HOST_X86_64 +case LC_SEGMENT_64 : +#else +case LC_SEGMENT : +#endif offset_to_segment = j;lseek (fd, offset_to_segment,SEEK_SET ); - segment =malloc (sizeof (struct segment_command )); -if (read (fd, segment,sizeof (struct segment_command )) !=sizeof (struct segment_command )) + segment =malloc (sizeof (struct SEGMENT_COMMAND )); +if (read (fd, segment,sizeof (struct SEGMENT_COMMAND )) !=sizeof (struct SEGMENT_COMMAND ))error ("unable to read LC_SEGMENT" ); - } -if (lc.cmd ==LC_DYSYMTAB ) - { +break ; + +case LC_DYSYMTAB : offset_to_dysymtab = j;lseek (fd, offset_to_dysymtab,SEEK_SET ); dysymtabcmd =malloc (sizeof (struct dysymtab_command ));if (read (fd, dysymtabcmd,sizeof (struct dysymtab_command )) !=sizeof (struct dysymtab_command ))error ("unable to read LC_DYSYMTAB" ); - } -if (lc.cmd ==LC_SYMTAB ) - { +break ; + +case LC_SYMTAB : offset_to_symtab = j;lseek (fd, offset_to_symtab,SEEK_SET ); symtabcmd =malloc (sizeof (struct symtab_command ));if (read (fd, symtabcmd,sizeof (struct symtab_command )) !=sizeof (struct symtab_command ))error ("unable to read LC_SYMTAB" ); +break ; + +default : +error ("LC: unknown command: %#x" , lc.cmd); } j+=lc.cmdsize;
@@ -1138,21 +1208,21 @@error ("unable to find LC_SEGMENT" );/* read section headers */ - section_hdr =load_data (fd, offset_to_segment +sizeof (struct segment_command ), segment->nsects *sizeof (struct section )); + section_hdr =load_data (fd, offset_to_segment +sizeof (struct SEGMENT_COMMAND ), segment->nsects *sizeof (struct SECTION ));/* read all section data */ sdata = (uint8_t **)malloc (sizeof (void *) * segment->nsects);memset (sdata,0 ,sizeof (void *) * segment->nsects); -/* Load the data in section data */ -for (i =0 ; i < segment->nsects; i++) { +/* Load the data in section data */ +for (i =0 ; i < segment->nsects; i++) { sdata[i] =load_data (fd, section_hdr[i].offset, section_hdr[i].size); }/* text section */ - text_sec_hdr =find_mach_sec_hdr (section_hdr, segment->nsects,SEG_TEXT ,SECT_TEXT ); - i =find_mach_sec_index (section_hdr, segment->nsects,SEG_TEXT ,SECT_TEXT ); -if (i ==-1 || !text_sec_hdr) + text_sec_hdr =find_mach_sec_hdr (section_hdr, segment->nsects,SEG_TEXT ,SECT_TEXT ); + i =find_mach_sec_index (section_hdr, segment->nsects,SEG_TEXT ,SECT_TEXT ); +if (i ==-1 || !text_sec_hdr)error ("could not find __TEXT,__text section" ); text = sdata[i];
@@ -1168,23 +1238,26 @@error ("could not find __SYMTAB segment" ); nb_syms = symtabcmd->nsyms; - symtab_std =load_data (fd, symtabcmd->symoff, symtabcmd->nsyms *sizeof (struct nlist )); + symtab_std =load_data (fd, symtabcmd->symoff, symtabcmd->nsyms *sizeof (struct NLIST )); strtab =load_data (fd, symtabcmd->stroff, symtabcmd->strsize); - symtab =malloc (sizeof (EXE_SYM ) * nb_syms); + symtab =malloc (sizeof (EXE_SYM ) * nb_syms); -/* Now transform the symtab, to an extended version, with the sym size, and the C name */ -for (i =0 , sym = symtab, syment = symtab_std; i < nb_syms; i++, sym++, syment++) { -struct nlist *sym_follow, *sym_next =0 ; +/* Now transform the symtab, to an extended version, with the sym size, and the C name */ +for (i =0 , sym = symtab, syment = symtab_std; i < nb_syms; i++, sym++, syment++) { +struct NLIST *sym_follow, *sym_next =0 ;unsigned int j; -memset (sym,0 ,sizeof (*sym)); +char *ns; +memset (sym,0 ,sizeof (*sym)); -if ( syment->n_type &N_STAB )/* Debug symbols are skipped */ +if ( syment->n_type &N_STAB )/* Debug symbols are skipped */ +continue ; +if ( ns =strchr ((char *)(strtab + syment->n_un.n_strx),'.' ) )/* no Exception handlers */ continue ; -memcpy (sym, syment,sizeof (*syment)); +memcpy (sym, syment,sizeof (*syment)); -/* Find the following symbol in order to get the current symbol size */ +/* Find the following symbol in order to get the current symbol size */ for (j =0 , sym_follow = symtab_std; j < nb_syms; j++, sym_follow++) {if ( sym_follow->n_sect !=1 || sym_follow->n_type &N_STAB || !(sym_follow->n_value > sym->st_value))continue ;
@@ -1848,6 +1921,96 @@/* patch relocations */ #if defined(HOST_I386 ) { +#ifdef CONFIG_FORMAT_MACH +struct scattered_relocation_info *scarel; +struct relocation_info * rel; +char final_sym_name[256 ]; +const char *sym_name; +const char *p; +int slide, sslide; +int i; + +for (i =0 , rel = relocs; i < nb_relocs; i++, rel++) { +unsigned int offset, length, value =0 ; +unsigned int type, pcrel, isym =0 ; +unsigned int usesym =0 ; + +if (R_SCATTERED & rel->r_address) { + scarel = (struct scattered_relocation_info *)rel; + offset = (unsigned int )scarel->r_address; + length = scarel->r_length; + pcrel = scarel->r_pcrel; + type = scarel->r_type; + value = scarel->r_value; + } +else { + value = isym = rel->r_symbolnum; + usesym = (rel->r_extern); + offset = rel->r_address; + length = rel->r_length; + pcrel = rel->r_pcrel; + type = rel->r_type; + } + + slide = offset - start_offset; + +if (!(offset >= start_offset && offset < start_offset + size)) +continue ;/* not in our range */ + + sym_name =get_reloc_name (rel, &sslide); + +if (usesym && symtab[isym].n_type &N_STAB ) +continue ;/* don't handle STAB (debug sym) */ + +if (sym_name &&strstart (sym_name,"__op_jmp" , &p)) { +int n; + n =strtol (p,NULL ,10 ); +fprintf (outfile," jmp_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n" , n, slide); +fprintf (outfile," label_offsets[%d] = 5 + (gen_code_ptr - gen_code_buf);\n" , n); +continue ;/* Nothing more to do */ + } + +if (!sym_name) { +fprintf (outfile,"/* #warning relocation not handled in %s (value 0x%x, %s, offset 0x%x, length 0x%x, %s, type 0x%x) */\n" , + name, value, usesym ?"use sym" :"don't use sym" , offset, length, pcrel ?"pcrel" :"" , type); +continue ;/* dunno how to handle without final_sym_name */ + } + +get_reloc_expr (final_sym_name,sizeof (final_sym_name), + sym_name); + +if (length !=2 ) +error ("unsupported %d-bit relocation" ,8 * (1 << length)); + +if (strstart (sym_name,"__op_gen_label" ,&p)) { +char relname[256 ]; +int is_label; + is_label =get_reloc_expr (relname,sizeof (relname), sym_name); +if (!is_label)error ("label is no label?" ); +if (pcrel) { +fprintf (outfile," tcg_out_reloc(s, gen_code_ptr + %d, %d, %s, %d);\n" , + slide,2 , relname, sslide -4 ); + }else { +fprintf (outfile," tcg_out_reloc(s, gen_code_ptr + %d, %d, %s, %d);\n" , + slide,1 , relname, sslide); + } + + }else switch (type) { +case GENERIC_RELOC_VANILLA : +if (pcrel) { +fprintf (outfile," *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) - 4;\n" , + slide, final_sym_name, slide); + } +else { +fprintf (outfile," *(uint32_t *)(gen_code_ptr + %d) = (%s + %d);\n" , + slide, final_sym_name, sslide); + } +break ; +default : +error ("unsupported i386 relocation (%d)" , type); + } + } +#else char relname[256 ];int type, is_label;int addend;
@@ -1931,9 +2094,123 @@ #endif } } +#endif }#elif defined(HOST_X86_64) { +#ifdef CONFIG_FORMAT_MACH +struct relocation_info * rel; +char final_sym_name[256 ]; +const char *sym_name; +const char *p; +int slide, sslide; +int i, bitsize; +char *bitlength; + +for (i =0 , rel = relocs; i < nb_relocs; i++, rel++) { +unsigned int offset, length, value =0 ; +unsigned int type, pcrel, isym =0 ; +unsigned int usesym =0 ; + + value = isym = rel->r_symbolnum; + usesym = (rel->r_extern); + offset = rel->r_address; + length = rel->r_length; + pcrel = rel->r_pcrel; + type = rel->r_type; + + slide = offset - start_offset; + +if (!(offset >= start_offset && offset < start_offset + size)) +continue ;/* not in our range */ + + sym_name =get_reloc_name (rel, &sslide); + +if (usesym && symtab[isym].n_type &N_STAB ) +continue ;/* don't handle STAB (debug sym) */ + +if (sym_name &&strstart (sym_name,"__op_jmp" , &p)) { +int n; + n =strtol (p,NULL ,10 ); +fprintf (outfile," jmp_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n" , n, slide); +fprintf (outfile," label_offsets[%d] = 5 + (gen_code_ptr - gen_code_buf);\n" , n); +fprintf (outfile," *(uint32_t *)(gen_code_ptr + %d) = 0l;\n" , slide); +continue ;/* Nothing more to do */ + } + +if (!sym_name) { +fprintf (outfile,"/* #warning relocation not handled in %s (value 0x%x, %s, offset 0x%x, length 0x%x, %s, type 0x%x) */\n" , + name, value, usesym ?"use sym" :"don't use sym" , offset, length, pcrel ?"pcrel" :"" , type); +continue ;/* dunno how to handle without final_sym_name */ + } + +get_reloc_expr (final_sym_name,sizeof (final_sym_name), + sym_name); + +if (length !=2 ) +error ("unsupported %d-bit relocation" ,8 * (1 << length)); + +switch (length) { +case 0 :// 8 bit + bitlength ="8" ; + bitsize =1 ; +break ; +case 1 :// 16 bit + bitlength ="16" ; + bitsize =2 ; +break ; +case 2 :// 32 bit + bitlength ="32" ; + bitsize =4 ; +break ; +case 3 :// 64 bit + bitlength ="64" ; + bitsize =8 ; +break ; + } + +if (strstart (sym_name,"__op_gen_label" ,&p)) { +char relname[256 ]; +int is_label; + is_label =get_reloc_expr (relname,sizeof (relname), sym_name); +if (!is_label)error ("label is no label?" ); +if (length !=2 )error ("label not 32 bits" ); +if (pcrel) { +fprintf (outfile," tcg_out_reloc(s, gen_code_ptr + %d, %d, %s, %d);\n" , + slide,2 , relname, sslide - bitsize); + }else { +fprintf (outfile," tcg_out_reloc(s, gen_code_ptr + %d, %d, %s, %d);\n" , + slide,1 , relname, sslide); + } + + }else if (pcrel) { +switch (type) { +case X86_64_RELOC_UNSIGNED : +case X86_64_RELOC_BRANCH : +case X86_64_RELOC_SIGNED : +case X86_64_RELOC_GOT_LOAD : +fprintf (outfile," *(uint%s_t *)(gen_code_ptr + %d) = (int%s_t)((long)%s - (long)(gen_code_ptr + %d + %d)) + %d;\n" , + bitlength, slide, bitlength, final_sym_name, slide, bitsize, sslide); +break ; +default : +printf ("unsupported x86_64 relocation (%d) in %s\n" , type, sym_name); + } + }else { +switch (type) { +case X86_64_RELOC_UNSIGNED : +fprintf (outfile," *(uint%s_t *)(gen_code_ptr + %d) = (uint%s_t)%s + %d;\n" , + bitlength, slide, bitlength, final_sym_name, sslide); +break ; +case X86_64_RELOC_SIGNED : +fprintf (outfile," *(uint%s_t *)(gen_code_ptr + %d) = (int%s_t)%s + %d;\n" , + bitlength, slide, bitlength, final_sym_name, sslide); +break ; +default : +printf ("unsupported x86_64 relocation (%d) in %s\n" , type, sym_name); + } + } + } +#else char relname[256 ];int type, is_label;int addend;
@@ -1977,6 +2254,7 @@ } } } +#endif }#elif defined(HOST_PPC) {