759
802
#endif /* CONFIG_FORMAT_COFF */
804
#ifdef CONFIG_FORMAT_MACH
807
struct mach_header mach_hdr;
810
struct segment_command *segment = 0;
811
struct dysymtab_command *dysymtabcmd = 0;
812
struct symtab_command *symtabcmd = 0;
815
struct section *section_hdr;
816
struct section *text_sec_hdr;
820
struct relocation_info *relocs;
824
struct nlist *symtab_std;
827
/* indirect symbols */
830
/* Utility functions */
832
static inline char *find_str_by_index(int index)
837
/* Used by dyngen common code */
838
static char *get_sym_name(EXE_SYM *sym)
840
char *name = find_str_by_index(sym->n_un.n_strx);
842
if ( sym->n_type & N_STAB ) /* Debug symbols are ignored */
853
/* find a section index given its segname, sectname */
854
static int find_mach_sec_index(struct section *section_hdr, int shnum, const char *segname,
855
const char *sectname)
858
struct section *sec = section_hdr;
860
for(i = 0; i < shnum; i++, sec++) {
861
if (!sec->segname || !sec->sectname)
863
if (!strcmp(sec->sectname, sectname) && !strcmp(sec->segname, segname))
869
/* find a section header given its segname, sectname */
870
struct section *find_mach_sec_hdr(struct section *section_hdr, int shnum, const char *segname,
871
const char *sectname)
873
int index = find_mach_sec_index(section_hdr, shnum, segname, sectname);
876
return section_hdr+index;
880
static inline void fetch_next_pair_value(struct relocation_info * rel, unsigned int *value)
882
struct scattered_relocation_info * scarel;
884
if(R_SCATTERED & rel->r_address) {
885
scarel = (struct scattered_relocation_info*)rel;
886
if(scarel->r_type != PPC_RELOC_PAIR)
887
error("fetch_next_pair_value: looking for a pair which was not found (1)");
888
*value = scarel->r_value;
890
if(rel->r_type != PPC_RELOC_PAIR)
891
error("fetch_next_pair_value: looking for a pair which was not found (2)");
892
*value = rel->r_address;
896
/* find a sym name given its value, in a section number */
897
static const char * find_sym_with_value_and_sec_number( int value, int sectnum, int * offset )
901
for( i = 0 ; i < nb_syms; i++ )
903
if( !(symtab[i].n_type & N_STAB) && (symtab[i].n_type & N_SECT) &&
904
(symtab[i].n_sect == sectnum) && (symtab[i].st_value <= value) )
906
if( (ret<0) || (symtab[i].st_value >= symtab[ret].st_value) )
914
*offset = value - symtab[ret].st_value;
915
return get_sym_name(&symtab[ret]);
920
* Find symbol name given a (virtual) address, and a section which is of type
921
* S_NON_LAZY_SYMBOL_POINTERS or S_LAZY_SYMBOL_POINTERS or S_SYMBOL_STUBS
923
static const char * find_reloc_name_in_sec_ptr(int address, struct section * sec_hdr)
925
unsigned int tocindex, symindex, size;
926
const char *name = 0;
929
if(!( address >= sec_hdr->addr && address < (sec_hdr->addr + sec_hdr->size) ) )
932
if( sec_hdr->flags & S_SYMBOL_STUBS ){
933
size = sec_hdr->reserved2;
938
else if( sec_hdr->flags & S_LAZY_SYMBOL_POINTERS ||
939
sec_hdr->flags & S_NON_LAZY_SYMBOL_POINTERS)
940
size = sizeof(unsigned long);
944
/* Compute our index in toc */
945
tocindex = (address - sec_hdr->addr)/size;
946
symindex = tocdylib[sec_hdr->reserved1 + tocindex];
948
name = get_sym_name(&symtab[symindex]);
953
static const char * find_reloc_name_given_its_address(int address)
956
for(i = 0; i < segment->nsects ; i++)
958
const char * name = find_reloc_name_in_sec_ptr(address, §ion_hdr[i]);
965
static const char * get_reloc_name(EXE_RELOC * rel, int * sslide)
968
struct scattered_relocation_info * sca_rel = (struct scattered_relocation_info*)rel;
969
int sectnum = rel->r_symbolnum;
973
/* init the slide value */
976
if(R_SCATTERED & rel->r_address)
977
return (char *)find_reloc_name_given_its_address(sca_rel->r_value);
981
/* ignore debug sym */
982
if ( symtab[rel->r_symbolnum].n_type & N_STAB )
984
return get_sym_name(&symtab[rel->r_symbolnum]);
987
/* Intruction contains an offset to the symbols pointed to, in the rel->r_symbolnum section */
988
sectoffset = *(uint32_t *)(text + rel->r_address) & 0xffff;
990
if(sectnum==0xffffff)
994
if(sectnum > segment->nsects)
995
error("sectnum > segment->nsects");
999
case PPC_RELOC_LO16: fetch_next_pair_value(rel+1, &other_half); sectoffset = (sectoffset & 0xffff);
1001
case PPC_RELOC_HI16: fetch_next_pair_value(rel+1, &other_half); sectoffset = (other_half & 0xffff);
1003
case PPC_RELOC_HA16: fetch_next_pair_value(rel+1, &other_half); sectoffset = (other_half & 0xffff);
1005
case PPC_RELOC_BR24:
1006
sectoffset = ( *(uint32_t *)(text + rel->r_address) & 0x03fffffc );
1007
if (sectoffset & 0x02000000) sectoffset |= 0xfc000000;
1010
error("switch(rel->type) not found");
1014
sectoffset += rel->r_address;
1016
if (rel->r_type == PPC_RELOC_BR24)
1017
name = (char *)find_reloc_name_in_sec_ptr((int)sectoffset, §ion_hdr[sectnum-1]);
1019
/* search it in the full symbol list, if not found */
1021
name = (char *)find_sym_with_value_and_sec_number(sectoffset, sectnum, sslide);
1026
/* Used by dyngen common code */
1027
static const char * get_rel_sym_name(EXE_RELOC * rel)
1030
return get_reloc_name( rel, &sslide);
1033
/* Used by dyngen common code */
1034
static host_ulong get_rel_offset(EXE_RELOC *rel)
1036
struct scattered_relocation_info * sca_rel = (struct scattered_relocation_info*)rel;
1037
if(R_SCATTERED & rel->r_address)
1038
return sca_rel->r_address;
1040
return rel->r_address;
1043
/* load a mach-o object file */
1044
int load_object(const char *filename)
1047
unsigned int offset_to_segment = 0;
1048
unsigned int offset_to_dysymtab = 0;
1049
unsigned int offset_to_symtab = 0;
1050
struct load_command lc;
1053
struct nlist *syment;
1055
fd = open(filename, O_RDONLY);
1057
error("can't open file '%s'", filename);
1059
/* Read Mach header. */
1060
if (read(fd, &mach_hdr, sizeof (mach_hdr)) != sizeof (mach_hdr))
1061
error("unable to read file header");
1063
/* Check Mach identification. */
1064
if (!check_mach_header(mach_hdr)) {
1065
error("bad Mach header");
1068
if (mach_hdr.cputype != CPU_TYPE_POWERPC)
1069
error("Unsupported CPU");
1071
if (mach_hdr.filetype != MH_OBJECT)
1072
error("Unsupported Mach Object");
1074
/* read segment headers */
1075
for(i=0, j=sizeof(mach_hdr); i<mach_hdr.ncmds ; i++)
1077
if(read(fd, &lc, sizeof(struct load_command)) != sizeof(struct load_command))
1078
error("unable to read load_command");
1079
if(lc.cmd == LC_SEGMENT)
1081
offset_to_segment = j;
1082
lseek(fd, offset_to_segment, SEEK_SET);
1083
segment = malloc(sizeof(struct segment_command));
1084
if(read(fd, segment, sizeof(struct segment_command)) != sizeof(struct segment_command))
1085
error("unable to read LC_SEGMENT");
1087
if(lc.cmd == LC_DYSYMTAB)
1089
offset_to_dysymtab = j;
1090
lseek(fd, offset_to_dysymtab, SEEK_SET);
1091
dysymtabcmd = malloc(sizeof(struct dysymtab_command));
1092
if(read(fd, dysymtabcmd, sizeof(struct dysymtab_command)) != sizeof(struct dysymtab_command))
1093
error("unable to read LC_DYSYMTAB");
1095
if(lc.cmd == LC_SYMTAB)
1097
offset_to_symtab = j;
1098
lseek(fd, offset_to_symtab, SEEK_SET);
1099
symtabcmd = malloc(sizeof(struct symtab_command));
1100
if(read(fd, symtabcmd, sizeof(struct symtab_command)) != sizeof(struct symtab_command))
1101
error("unable to read LC_SYMTAB");
1105
lseek(fd, j, SEEK_SET);
1109
error("unable to find LC_SEGMENT");
1111
/* read section headers */
1112
section_hdr = load_data(fd, offset_to_segment + sizeof(struct segment_command), segment->nsects * sizeof(struct section));
1114
/* read all section data */
1115
sdata = (uint8_t **)malloc(sizeof(void *) * segment->nsects);
1116
memset(sdata, 0, sizeof(void *) * segment->nsects);
1118
/* Load the data in section data */
1119
for(i = 0; i < segment->nsects; i++) {
1120
sdata[i] = load_data(fd, section_hdr[i].offset, section_hdr[i].size);
1124
text_sec_hdr = find_mach_sec_hdr(section_hdr, segment->nsects, SEG_TEXT, SECT_TEXT);
1125
i = find_mach_sec_index(section_hdr, segment->nsects, SEG_TEXT, SECT_TEXT);
1126
if (i == -1 || !text_sec_hdr)
1127
error("could not find __TEXT,__text section");
1130
/* Make sure dysym was loaded */
1131
if(!(int)dysymtabcmd)
1132
error("could not find __DYSYMTAB segment");
1134
/* read the table of content of the indirect sym */
1135
tocdylib = load_data( fd, dysymtabcmd->indirectsymoff, dysymtabcmd->nindirectsyms * sizeof(uint32_t) );
1137
/* Make sure symtab was loaded */
1139
error("could not find __SYMTAB segment");
1140
nb_syms = symtabcmd->nsyms;
1142
symtab_std = load_data(fd, symtabcmd->symoff, symtabcmd->nsyms * sizeof(struct nlist));
1143
strtab = load_data(fd, symtabcmd->stroff, symtabcmd->strsize);
1145
symtab = malloc(sizeof(EXE_SYM) * nb_syms);
1147
/* Now transform the symtab, to an extended version, with the sym size, and the C name */
1148
for(i = 0, sym = symtab, syment = symtab_std; i < nb_syms; i++, sym++, syment++) {
1150
struct nlist *sym_follow, *sym_next = 0;
1152
name = find_str_by_index(sym->n_un.n_strx);
1153
memset(sym, 0, sizeof(*sym));
1155
if ( sym->n_type & N_STAB ) /* Debug symbols are skipped */
1158
memcpy(sym, syment, sizeof(*syment));
1160
/* Find the following symbol in order to get the current symbol size */
1161
for(j = 0, sym_follow = symtab_std; j < nb_syms; j++, sym_follow++) {
1162
if ( sym_follow->n_sect != 1 || sym_follow->n_type & N_STAB || !(sym_follow->n_value > sym->st_value))
1165
sym_next = sym_follow;
1168
if(!(sym_next->n_value > sym_follow->n_value))
1170
sym_next = sym_follow;
1173
sym->st_size = sym_next->n_value - sym->st_value;
1175
sym->st_size = text_sec_hdr->size - sym->st_value;
1179
relocs = load_data(fd, text_sec_hdr->reloff, text_sec_hdr->nreloc * sizeof(struct relocation_info));
1180
nb_relocs = text_sec_hdr->nreloc;
1186
#endif /* CONFIG_FORMAT_MACH */
763
1190
int arm_emit_ldr_info(const char *name, unsigned long start_offset,
1787
#elif defined(CONFIG_FORMAT_MACH)
1788
struct scattered_relocation_info *scarel;
1789
struct relocation_info * rel;
1790
char final_sym_name[256];
1791
const char *sym_name;
1796
for(i = 0, rel = relocs; i < nb_relocs; i++, rel++) {
1797
unsigned int offset, length, value = 0;
1798
unsigned int type, pcrel, isym = 0;
1799
unsigned int usesym = 0;
1801
if(R_SCATTERED & rel->r_address) {
1802
scarel = (struct scattered_relocation_info*)rel;
1803
offset = (unsigned int)scarel->r_address;
1804
length = scarel->r_length;
1805
pcrel = scarel->r_pcrel;
1806
type = scarel->r_type;
1807
value = scarel->r_value;
1809
value = isym = rel->r_symbolnum;
1810
usesym = (rel->r_extern);
1811
offset = rel->r_address;
1812
length = rel->r_length;
1813
pcrel = rel->r_pcrel;
1817
slide = offset - start_offset;
1819
if (!(offset >= start_offset && offset < start_offset + size))
1820
continue; /* not in our range */
1822
sym_name = get_reloc_name(rel, &sslide);
1824
if(usesym && symtab[isym].n_type & N_STAB)
1825
continue; /* don't handle STAB (debug sym) */
1827
if (sym_name && strstart(sym_name, "__op_jmp", &p)) {
1829
n = strtol(p, NULL, 10);
1830
fprintf(outfile, " jmp_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n",
1832
continue; /* Nothing more to do */
1837
fprintf(outfile, "/* #warning relocation not handled in %s (value 0x%x, %s, offset 0x%x, length 0x%x, %s, type 0x%x) */\n",
1838
name, value, usesym ? "use sym" : "don't use sym", offset, length, pcrel ? "pcrel":"", type);
1839
continue; /* dunno how to handle without final_sym_name */
1842
if (strstart(sym_name, "__op_param", &p)) {
1843
snprintf(final_sym_name, sizeof(final_sym_name), "param%s", p);
1845
snprintf(final_sym_name, sizeof(final_sym_name), "(long)(&%s)", sym_name);
1849
case PPC_RELOC_BR24:
1850
fprintf(outfile, "{\n");
1851
fprintf(outfile, " uint32_t imm = *(uint32_t *)(gen_code_ptr + %d) & 0x3fffffc;\n", slide);
1852
fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = (*(uint32_t *)(gen_code_ptr + %d) & ~0x03fffffc) | ((imm + ((long)%s - (long)gen_code_ptr) + %d) & 0x03fffffc);\n",
1853
slide, slide, name, sslide );
1854
fprintf(outfile, "}\n");
1856
case PPC_RELOC_HI16:
1857
fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d + 2) = (%s + %d) >> 16;\n",
1858
slide, final_sym_name, sslide);
1860
case PPC_RELOC_LO16:
1861
fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d + 2) = (%s + %d);\n",
1862
slide, final_sym_name, sslide);
1864
case PPC_RELOC_HA16:
1865
fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d + 2) = (%s + %d + 0x8000) >> 16;\n",
1866
slide, final_sym_name, sslide);
1869
error("unsupported powerpc relocation (%d)", type);
1873
#error unsupport object format
1341
1876
#elif defined(HOST_S390)