1199767f8SToomas Soome /*-
2199767f8SToomas Soome * Copyright (c) 2008-2010 Rui Paulo <rpaulo@FreeBSD.org>
3199767f8SToomas Soome * All rights reserved.
4199767f8SToomas Soome *
5199767f8SToomas Soome * Redistribution and use in source and binary forms, with or without
6199767f8SToomas Soome * modification, are permitted provided that the following conditions
7199767f8SToomas Soome * are met:
8199767f8SToomas Soome * 1. Redistributions of source code must retain the above copyright
9199767f8SToomas Soome * notice, this list of conditions and the following disclaimer.
10199767f8SToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
11199767f8SToomas Soome * notice, this list of conditions and the following disclaimer in the
12199767f8SToomas Soome * documentation and/or other materials provided with the distribution.
13199767f8SToomas Soome *
14199767f8SToomas Soome * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15199767f8SToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16199767f8SToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17199767f8SToomas Soome * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18199767f8SToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19199767f8SToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20199767f8SToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21199767f8SToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22199767f8SToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23199767f8SToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24199767f8SToomas Soome * SUCH DAMAGE.
25199767f8SToomas Soome */
26199767f8SToomas Soome
27199767f8SToomas Soome #include <sys/cdefs.h>
28199767f8SToomas Soome
29199767f8SToomas Soome #include <sys/types.h>
30199767f8SToomas Soome #include <elf.h>
31199767f8SToomas Soome #include <bootstrap.h>
32199767f8SToomas Soome
3306624236SToomas Soome #if defined(__aarch64__) || defined(__amd64__)
34199767f8SToomas Soome #define ElfW_Rel Elf64_Rela
35199767f8SToomas Soome #define ElfW_Dyn Elf64_Dyn
36199767f8SToomas Soome #define ELFW_R_TYPE ELF64_R_TYPE
37199767f8SToomas Soome #define ELF_RELA
38199767f8SToomas Soome #elif defined(__arm__) || defined(__i386__)
39199767f8SToomas Soome #define ElfW_Rel Elf32_Rel
40199767f8SToomas Soome #define ElfW_Dyn Elf32_Dyn
41199767f8SToomas Soome #define ELFW_R_TYPE ELF32_R_TYPE
42199767f8SToomas Soome #else
43199767f8SToomas Soome #error architecture not supported
44199767f8SToomas Soome #endif
45199767f8SToomas Soome #if defined(__aarch64__)
46199767f8SToomas Soome #define RELOC_TYPE_NONE R_AARCH64_NONE
47199767f8SToomas Soome #define RELOC_TYPE_RELATIVE R_AARCH64_RELATIVE
48199767f8SToomas Soome #elif defined(__amd64__)
49199767f8SToomas Soome #define RELOC_TYPE_NONE R_X86_64_NONE
50199767f8SToomas Soome #define RELOC_TYPE_RELATIVE R_X86_64_RELATIVE
51199767f8SToomas Soome #elif defined(__arm__)
52199767f8SToomas Soome #define RELOC_TYPE_NONE R_ARM_NONE
53199767f8SToomas Soome #define RELOC_TYPE_RELATIVE R_ARM_RELATIVE
54199767f8SToomas Soome #elif defined(__i386__)
55199767f8SToomas Soome #define RELOC_TYPE_NONE R_386_NONE
56199767f8SToomas Soome #define RELOC_TYPE_RELATIVE R_386_RELATIVE
57199767f8SToomas Soome #endif
58199767f8SToomas Soome
59199767f8SToomas Soome void self_reloc(Elf_Addr baseaddr, ElfW_Dyn *dynamic);
60199767f8SToomas Soome
61199767f8SToomas Soome /*
62199767f8SToomas Soome * A simple elf relocator.
63199767f8SToomas Soome */
64199767f8SToomas Soome void
self_reloc(Elf_Addr baseaddr,ElfW_Dyn * dynamic)65199767f8SToomas Soome self_reloc(Elf_Addr baseaddr, ElfW_Dyn *dynamic)
66199767f8SToomas Soome {
67199767f8SToomas Soome Elf_Word relsz, relent;
68199767f8SToomas Soome Elf_Addr *newaddr;
69199767f8SToomas Soome ElfW_Rel *rel = 0;
70199767f8SToomas Soome ElfW_Dyn *dynp;
71199767f8SToomas Soome
72199767f8SToomas Soome /*
73199767f8SToomas Soome * Find the relocation address, its size and the relocation entry.
74199767f8SToomas Soome */
75199767f8SToomas Soome relsz = 0;
76199767f8SToomas Soome relent = 0;
77199767f8SToomas Soome for (dynp = dynamic; dynp->d_tag != DT_NULL; dynp++) {
78199767f8SToomas Soome switch (dynp->d_tag) {
79199767f8SToomas Soome case DT_REL:
80199767f8SToomas Soome case DT_RELA:
81199767f8SToomas Soome rel = (ElfW_Rel *)(dynp->d_un.d_ptr + baseaddr);
82199767f8SToomas Soome break;
83199767f8SToomas Soome case DT_RELSZ:
84199767f8SToomas Soome case DT_RELASZ:
85199767f8SToomas Soome relsz = dynp->d_un.d_val;
86199767f8SToomas Soome break;
87199767f8SToomas Soome case DT_RELENT:
88199767f8SToomas Soome case DT_RELAENT:
89199767f8SToomas Soome relent = dynp->d_un.d_val;
90199767f8SToomas Soome break;
91199767f8SToomas Soome default:
92199767f8SToomas Soome break;
93199767f8SToomas Soome }
94199767f8SToomas Soome }
95199767f8SToomas Soome
96199767f8SToomas Soome /*
9706624236SToomas Soome * Perform the actual relocation. We rely on the object having been
9806624236SToomas Soome * linked at 0, so that the difference between the load and link
9906624236SToomas Soome * address is the same as the load address.
100199767f8SToomas Soome */
101199767f8SToomas Soome for (; relsz > 0; relsz -= relent) {
102199767f8SToomas Soome switch (ELFW_R_TYPE(rel->r_info)) {
103199767f8SToomas Soome case RELOC_TYPE_NONE:
104199767f8SToomas Soome /* No relocation needs be performed. */
105199767f8SToomas Soome break;
106199767f8SToomas Soome
107199767f8SToomas Soome case RELOC_TYPE_RELATIVE:
108199767f8SToomas Soome newaddr = (Elf_Addr *)(rel->r_offset + baseaddr);
109199767f8SToomas Soome #ifdef ELF_RELA
11006624236SToomas Soome /* Addend relative to the base address. */
11106624236SToomas Soome *newaddr = baseaddr + rel->r_addend;
11206624236SToomas Soome #else
11306624236SToomas Soome /* Address relative to the base address. */
11406624236SToomas Soome *newaddr += baseaddr;
115199767f8SToomas Soome #endif
116199767f8SToomas Soome break;
117199767f8SToomas Soome default:
118199767f8SToomas Soome /* XXX: do we need other relocations ? */
119199767f8SToomas Soome break;
120199767f8SToomas Soome }
121199767f8SToomas Soome rel = (ElfW_Rel *)(void *)((caddr_t) rel + relent);
122199767f8SToomas Soome }
123199767f8SToomas Soome }
124