elfloader-x86.c
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include "elfloader-arch.h"
00034 #include <sys/mman.h>
00035 #include <fcntl.h>
00036 #include <stdio.h>
00037
00038 #define R_386_NONE 0
00039 #define R_386_32 1
00040 #define R_386_PC32 2
00041 #define R_386_GOT32 3
00042 #define R_386_PLT32 4
00043 #define R_386_COPY 5
00044 #define R_386_GLOB_DATA 6
00045 #define R_386_JMP_SLOT 7
00046 #define R_386_RELATIVE 8
00047 #define R_386_GOTOFF 9
00048 #define R_386_GOTPC 10
00049
00050 #define ELF32_R_TYPE(info) ((unsigned char)(info))
00051
00052 static char datamemory[ELFLOADER_DATAMEMORY_SIZE];
00053
00054
00055 void *
00056 elfloader_arch_allocate_ram(int size)
00057 {
00058 return (void *)datamemory;
00059 }
00060
00061 void *
00062 elfloader_arch_allocate_rom(int size)
00063 {
00064 int fd = open("/dev/zero", O_RDWR);
00065 char *mem = mmap(0, ELFLOADER_TEXTMEMORY_SIZE, PROT_WRITE | PROT_EXEC, MAP_PRIVATE, fd, 0);
00066 return mem;
00067 }
00068
00069 void
00070 elfloader_arch_write_rom(int fd, unsigned short textoff, unsigned int size, char *mem)
00071 {
00072 cfs_seek(fd, textoff, CFS_SEEK_SET);
00073 cfs_read(fd, (unsigned char *)mem, size);
00074 }
00075
00076 void
00077 elfloader_arch_relocate(int fd, unsigned int sectionoffset, char *sectionaddress,
00078 struct elf32_rela *rela, char *addr)
00079 {
00080 unsigned int type;
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090 type = ELF32_R_TYPE(rela->r_info);
00091
00092 switch(type) {
00093 case R_386_NONE:
00094 case R_386_COPY:
00095
00096 break;
00097 case R_386_32:
00098 addr += rela->r_addend;
00099
00100 cfs_seek(fd, sectionoffset + rela->r_offset, CFS_SEEK_SET);
00101 cfs_write(fd, (char *)&addr, 4);
00102
00103 break;
00104 case R_386_PC32:
00105 addr -= (sectionaddress + rela->r_offset);
00106 addr += rela->r_addend;
00107
00108 cfs_seek(fd, sectionoffset + rela->r_offset, CFS_SEEK_SET);
00109 cfs_write(fd, (char *)&addr, 4);
00110
00111 break;
00112 case R_386_GOT32:
00113 printf("elfloader-x86.c: unsupported relocation type G + A - P (%d)\n", type);
00114 break;
00115 case R_386_PLT32:
00116 printf("elfloader-x86.c: unsupported relocation type L + A - P (%d)\n", type);
00117 break;
00118 case R_386_GLOB_DATA:
00119 case R_386_JMP_SLOT:
00120 printf("elfloader-x86.c: unsupported relocation type S (%d)\n", type);
00121 break;
00122 case R_386_RELATIVE:
00123 printf("elfloader-x86.c: unsupported relocation type B + A (%d)\n", type);
00124 break;
00125 case R_386_GOTOFF:
00126 printf("elfloader-x86.c: unsupported relocation type S + A - GOT (%d)\n", type);
00127 break;
00128 case R_386_GOTPC:
00129 printf("elfloader-x86.c: unsupported relocation type GOT + A - P (%d)\n", type);
00130 break;
00131 default:
00132 printf("elfloader-x86.c: unknown type (%d)\n", type);
00133 break;
00134 }
00135
00136 }
00137