Contiki 2.5
elfloader-x86.c
1 /*
2  * Copyright (c) 2005, Swedish Institute of Computer Science
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  * @(#)$Id: elfloader-x86.c,v 1.3 2009/02/27 14:28:02 nvt-se Exp $
32  */
33 #include "elfloader-arch.h"
34 #include <sys/mman.h>
35 #include <fcntl.h>
36 #include <stdio.h>
37 
38 #define R_386_NONE 0
39 #define R_386_32 1
40 #define R_386_PC32 2
41 #define R_386_GOT32 3
42 #define R_386_PLT32 4
43 #define R_386_COPY 5
44 #define R_386_GLOB_DATA 6
45 #define R_386_JMP_SLOT 7
46 #define R_386_RELATIVE 8
47 #define R_386_GOTOFF 9
48 #define R_386_GOTPC 10
49 
50 #define ELF32_R_TYPE(info) ((unsigned char)(info))
51 
52 static char datamemory[ELFLOADER_DATAMEMORY_SIZE];
53 
54 /*---------------------------------------------------------------------------*/
55 void *
57 {
58  return (void *)datamemory;
59 }
60 /*---------------------------------------------------------------------------*/
61 void *
63 {
64  int fd = open("/dev/zero", O_RDWR);
65  char *mem = mmap(0, ELFLOADER_TEXTMEMORY_SIZE, PROT_WRITE | PROT_EXEC, MAP_PRIVATE, fd, 0);
66  return mem;
67 }
68 /*---------------------------------------------------------------------------*/
69 void
70 elfloader_arch_write_rom(int fd, unsigned short textoff, unsigned int size, char *mem)
71 {
72  cfs_seek(fd, textoff, CFS_SEEK_SET);
73  cfs_read(fd, (unsigned char *)mem, size);
74 }
75 /*---------------------------------------------------------------------------*/
76 void
77 elfloader_arch_relocate(int fd, unsigned int sectionoffset, char *sectionaddress,
78  struct elf32_rela *rela, char *addr)
79 {
80  unsigned int type;
81 
82  /*
83  Given value addr is S
84 
85  S = runtime address of destination = addr
86  A = rela->r_addend
87  P = absolute address of relocation (section base address and rela->r_offset)
88  */
89 
90  type = ELF32_R_TYPE(rela->r_info);
91 
92  switch(type) {
93  case R_386_NONE:
94  case R_386_COPY:
95  /* printf("elfloader-x86.c: relocation calculation completed (none) %d\n", type); */
96  break;
97  case R_386_32:
98  addr += rela->r_addend; /* +A */
99 
100  cfs_seek(fd, sectionoffset + rela->r_offset, CFS_SEEK_SET);
101  cfs_write(fd, (char *)&addr, 4);
102  /*printf("elfloader-x86.c: performed relocation type S + A (%d)\n", type);*/
103  break;
104  case R_386_PC32:
105  addr -= (sectionaddress + rela->r_offset); /* -P */
106  addr += rela->r_addend; /* +A */
107 
108  cfs_seek(fd, sectionoffset + rela->r_offset, CFS_SEEK_SET);
109  cfs_write(fd, (char *)&addr, 4);
110  /*printf("elfloader-x86.c: performed relocation type S + A - P (%d)\n", type);*/
111  break;
112  case R_386_GOT32:
113  printf("elfloader-x86.c: unsupported relocation type G + A - P (%d)\n", type);
114  break;
115  case R_386_PLT32:
116  printf("elfloader-x86.c: unsupported relocation type L + A - P (%d)\n", type);
117  break;
118  case R_386_GLOB_DATA:
119  case R_386_JMP_SLOT:
120  printf("elfloader-x86.c: unsupported relocation type S (%d)\n", type);
121  break;
122  case R_386_RELATIVE:
123  printf("elfloader-x86.c: unsupported relocation type B + A (%d)\n", type);
124  break;
125  case R_386_GOTOFF:
126  printf("elfloader-x86.c: unsupported relocation type S + A - GOT (%d)\n", type);
127  break;
128  case R_386_GOTPC:
129  printf("elfloader-x86.c: unsupported relocation type GOT + A - P (%d)\n", type);
130  break;
131  default:
132  printf("elfloader-x86.c: unknown type (%d)\n", type);
133  break;
134  }
135 
136 }
137 /*---------------------------------------------------------------------------*/