Contiki 3.x
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  */
32 #include "elfloader-arch.h"
33 #include <sys/mman.h>
34 #include <fcntl.h>
35 #include <stdio.h>
36 
37 #define R_386_NONE 0
38 #define R_386_32 1
39 #define R_386_PC32 2
40 #define R_386_GOT32 3
41 #define R_386_PLT32 4
42 #define R_386_COPY 5
43 #define R_386_GLOB_DATA 6
44 #define R_386_JMP_SLOT 7
45 #define R_386_RELATIVE 8
46 #define R_386_GOTOFF 9
47 #define R_386_GOTPC 10
48 
49 #define ELF32_R_TYPE(info) ((unsigned char)(info))
50 
51 static char datamemory[ELFLOADER_DATAMEMORY_SIZE];
52 
53 /*---------------------------------------------------------------------------*/
54 void *
56 {
57  return (void *)datamemory;
58 }
59 /*---------------------------------------------------------------------------*/
60 void *
62 {
63  int fd = open("/dev/zero", O_RDWR);
64  char *mem = mmap(0, ELFLOADER_TEXTMEMORY_SIZE, PROT_WRITE | PROT_EXEC, MAP_PRIVATE, fd, 0);
65  return mem;
66 }
67 /*---------------------------------------------------------------------------*/
68 void
69 elfloader_arch_write_rom(int fd, unsigned short textoff, unsigned int size, char *mem)
70 {
71  cfs_seek(fd, textoff, CFS_SEEK_SET);
72  cfs_read(fd, (unsigned char *)mem, size);
73 }
74 /*---------------------------------------------------------------------------*/
75 void
76 elfloader_arch_relocate(int fd, unsigned int sectionoffset, char *sectionaddress,
77  struct elf32_rela *rela, char *addr)
78 {
79  unsigned int type;
80 
81  /*
82  Given value addr is S
83 
84  S = runtime address of destination = addr
85  A = rela->r_addend
86  P = absolute address of relocation (section base address and rela->r_offset)
87  */
88 
89  type = ELF32_R_TYPE(rela->r_info);
90 
91  switch(type) {
92  case R_386_NONE:
93  case R_386_COPY:
94  /* printf("elfloader-x86.c: relocation calculation completed (none) %d\n", type); */
95  break;
96  case R_386_32:
97  addr += rela->r_addend; /* +A */
98 
99  cfs_seek(fd, sectionoffset + rela->r_offset, CFS_SEEK_SET);
100  cfs_write(fd, (char *)&addr, 4);
101  /*printf("elfloader-x86.c: performed relocation type S + A (%d)\n", type);*/
102  break;
103  case R_386_PC32:
104  addr -= (sectionaddress + rela->r_offset); /* -P */
105  addr += rela->r_addend; /* +A */
106 
107  cfs_seek(fd, sectionoffset + rela->r_offset, CFS_SEEK_SET);
108  cfs_write(fd, (char *)&addr, 4);
109  /*printf("elfloader-x86.c: performed relocation type S + A - P (%d)\n", type);*/
110  break;
111  case R_386_GOT32:
112  printf("elfloader-x86.c: unsupported relocation type G + A - P (%d)\n", type);
113  break;
114  case R_386_PLT32:
115  printf("elfloader-x86.c: unsupported relocation type L + A - P (%d)\n", type);
116  break;
117  case R_386_GLOB_DATA:
118  case R_386_JMP_SLOT:
119  printf("elfloader-x86.c: unsupported relocation type S (%d)\n", type);
120  break;
121  case R_386_RELATIVE:
122  printf("elfloader-x86.c: unsupported relocation type B + A (%d)\n", type);
123  break;
124  case R_386_GOTOFF:
125  printf("elfloader-x86.c: unsupported relocation type S + A - GOT (%d)\n", type);
126  break;
127  case R_386_GOTPC:
128  printf("elfloader-x86.c: unsupported relocation type GOT + A - P (%d)\n", type);
129  break;
130  default:
131  printf("elfloader-x86.c: unknown type (%d)\n", type);
132  break;
133  }
134 
135 }
136 /*---------------------------------------------------------------------------*/
void elfloader_arch_relocate(int fd, unsigned int sectionoffset, char *sectionaddr, struct elf32_rela *rela, char *addr)
Perform a relocation.
cfs_offset_t cfs_seek(int fd, cfs_offset_t offset, int whence)
Seek to a specified position in an open file.
Definition: cfs-coffee.c:1042
Header file for the architecture specific parts of the Contiki ELF loader.
void * elfloader_arch_allocate_ram(int size)
Allocate RAM for a new module.
Definition: elfloader-avr.c:74
void * elfloader_arch_allocate_rom(int size)
Allocate program memory for a new module.
Definition: elfloader-avr.c:95
#define CFS_SEEK_SET
Specify that cfs_seek() should compute the offset from the beginning of the file. ...
Definition: cfs.h:127
void elfloader_arch_write_rom(int fd, unsigned short textoff, unsigned int size, char *mem)
Write to read-only memory (for example the text segment).