elfloader-x86.c

00001 /*
00002  * Copyright (c) 2005, Swedish Institute of Computer Science
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the Institute nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  *
00029  * This file is part of the Contiki operating system.
00030  *
00031  * @(#)$Id: elfloader-x86.c,v 1.3 2009/02/27 14:28:02 nvt-se Exp $
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      Given value addr is S
00084      
00085      S = runtime address of destination = addr
00086      A = rela->r_addend 
00087      P = absolute address of relocation (section base address and rela->r_offset)
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     /* printf("elfloader-x86.c: relocation calculation completed (none) %d\n", type); */
00096     break;
00097   case R_386_32:
00098     addr += rela->r_addend; /* +A */
00099     
00100     cfs_seek(fd, sectionoffset + rela->r_offset, CFS_SEEK_SET);
00101     cfs_write(fd, (char *)&addr, 4);
00102     /*printf("elfloader-x86.c: performed relocation type S + A (%d)\n", type);*/
00103     break;
00104   case R_386_PC32:
00105     addr -= (sectionaddress + rela->r_offset); /* -P */
00106     addr += rela->r_addend; /* +A */
00107     
00108     cfs_seek(fd, sectionoffset + rela->r_offset, CFS_SEEK_SET);
00109     cfs_write(fd, (char *)&addr, 4);
00110     /*printf("elfloader-x86.c: performed relocation type S + A - P (%d)\n", type);*/
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 /*---------------------------------------------------------------------------*/

Generated on Mon Apr 11 14:23:28 2011 for Contiki 2.5 by  doxygen 1.6.1