Contiki 2.5
elfloader-arm.c
1 #include <stdlib.h>
2 #include <malloc.h>
4 
5 #if 0
6 #include <stdio.h>
7 #define PRINTF(...) printf(__VA_ARGS__)
8 #else
9 #define PRINTF(...) do {} while (0)
10 #endif
11 
12 #define ELF32_R_TYPE(info) ((unsigned char)(info))
13 
14 /* Supported relocations */
15 
16 #define R_ARM_ABS32 2
17 #define R_ARM_THM_CALL 10
18 
19 /* Adapted from elfloader-avr.c */
20 
21 int
23  struct elfloader_output *output,
24  unsigned int sectionoffset,
25  char *sectionaddr,
26  struct elf32_rela *rela, char *addr)
27 {
28  unsigned int type;
29 
30  type = ELF32_R_TYPE(rela->r_info);
31 
32  cfs_seek(input_fd, sectionoffset + rela->r_offset, CFS_SEEK_SET);
33 
34 /* PRINTF("elfloader_arch_relocate: type %d\n", type); */
35 /* PRINTF("Addr: %p, Addend: %ld\n", addr, rela->r_addend); */
36  switch(type) {
37  case R_ARM_ABS32:
38  {
39  int32_t addend;
40  cfs_read(input_fd, (char*)&addend, 4);
41  addr += addend;
42  elfloader_output_write_segment(output,(char*) &addr, 4);
43  PRINTF("%p: addr: %p\n", sectionaddr +rela->r_offset,
44  addr);
45  }
46  break;
47  case R_ARM_THM_CALL:
48  {
49  uint16_t instr[2];
50  int32_t offset;
51  char *base;
52  cfs_read(input_fd, (char*)instr, 4);
53  /* Ignore the addend since it will be zero for calls to symbols,
54  and I can't think of a case when doing a relative call to
55  a non-symbol position */
56  base = sectionaddr + (rela->r_offset + 4);
57 
58  if (((instr[1]) & 0xe800) == 0xe800) {
59  /* BL or BLX */
60  if (((uint32_t)addr) & 0x1) {
61  /* BL */
62  instr[1] |= 0x1800;
63  } else {
64 #if defined(__ARM_ARCH_4T__)
66 #else
67  /* BLX */
68  instr[1] &= ~0x1800;
69  instr[1] |= 0x0800;
70 #endif
71  }
72  }
73  /* Adjust address for BLX */
74  if ((instr[1] & 0x1800) == 0x0800) {
75  addr = (char*)((((uint32_t)addr) & 0xfffffffd)
76  | (((uint32_t)base) & 0x00000002));
77  }
78  offset = addr - (sectionaddr + (rela->r_offset + 4));
79  if (offset < -(1<<22) || offset >= (1<<22)) {
80  PRINTF("elfloader-arm.c: offset %d too large for relative call\n",
81  (int)offset);
82  }
83  /* PRINTF("%p: %04x %04x offset: %d addr: %p\n", sectionaddr +rela->r_offset, instr[0], instr[1], (int)offset, addr); */
84  instr[0] = (instr[0] & 0xf800) | ((offset>>12)&0x07ff);
85  instr[1] = (instr[1] & 0xf800) | ((offset>>1)&0x07ff);
86  elfloader_output_write_segment(output, (char*)instr, 4);
87  /* PRINTF("cfs_write: %04x %04x\n",instr[0], instr[1]); */
88  }
89  break;
90 
91  default:
92  PRINTF("elfloader-arm.c: unsupported relocation type %d\n", type);
94  }
95  return ELFLOADER_OK;
96 }