reloc_arm.c revision 1.2 1 1.2 jmcneill /* $NetBSD: reloc_arm.c,v 1.2 2019/03/30 12:46:16 jmcneill Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /* reloc_arm.c - position independent x86 ELF shared object relocator
4 1.1 jmcneill Copyright (C) 2014 Linaro Ltd. <ard.biesheuvel (at) linaro.org>
5 1.1 jmcneill Copyright (C) 1999 Hewlett-Packard Co.
6 1.1 jmcneill Contributed by David Mosberger <davidm (at) hpl.hp.com>.
7 1.1 jmcneill
8 1.1 jmcneill All rights reserved.
9 1.1 jmcneill
10 1.1 jmcneill Redistribution and use in source and binary forms, with or without
11 1.1 jmcneill modification, are permitted provided that the following conditions
12 1.1 jmcneill are met:
13 1.1 jmcneill
14 1.1 jmcneill * Redistributions of source code must retain the above copyright
15 1.1 jmcneill notice, this list of conditions and the following disclaimer.
16 1.1 jmcneill * Redistributions in binary form must reproduce the above
17 1.1 jmcneill copyright notice, this list of conditions and the following
18 1.1 jmcneill disclaimer in the documentation and/or other materials
19 1.1 jmcneill provided with the distribution.
20 1.1 jmcneill * Neither the name of Hewlett-Packard Co. nor the names of its
21 1.1 jmcneill contributors may be used to endorse or promote products derived
22 1.1 jmcneill from this software without specific prior written permission.
23 1.1 jmcneill
24 1.1 jmcneill THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
25 1.1 jmcneill CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
26 1.1 jmcneill INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 1.1 jmcneill MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 1.1 jmcneill DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
29 1.1 jmcneill BE LIABLE FOR ANYDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
30 1.1 jmcneill OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 1.1 jmcneill PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 1.1 jmcneill PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 1.1 jmcneill THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
34 1.1 jmcneill TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
35 1.1 jmcneill THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.1 jmcneill SUCH DAMAGE.
37 1.1 jmcneill */
38 1.1 jmcneill
39 1.1 jmcneill #include <efi.h>
40 1.1 jmcneill #include <efilib.h>
41 1.1 jmcneill
42 1.2 jmcneill #ifdef __NetBSD__
43 1.2 jmcneill #include <sys/types.h>
44 1.2 jmcneill #include <sys/exec_elf.h>
45 1.2 jmcneill #else
46 1.1 jmcneill #include <elf.h>
47 1.2 jmcneill #endif
48 1.2 jmcneill
49 1.2 jmcneill EFI_STATUS _relocate (long, Elf32_Dyn *, EFI_HANDLE, EFI_SYSTEM_TABLE *);
50 1.1 jmcneill
51 1.1 jmcneill EFI_STATUS _relocate (long ldbase, Elf32_Dyn *dyn,
52 1.1 jmcneill EFI_HANDLE image EFI_UNUSED,
53 1.1 jmcneill EFI_SYSTEM_TABLE *systab EFI_UNUSED)
54 1.1 jmcneill {
55 1.1 jmcneill long relsz = 0, relent = 0;
56 1.1 jmcneill Elf32_Rel *rel = 0;
57 1.1 jmcneill unsigned long *addr;
58 1.1 jmcneill int i;
59 1.1 jmcneill
60 1.1 jmcneill for (i = 0; dyn[i].d_tag != DT_NULL; ++i) {
61 1.1 jmcneill switch (dyn[i].d_tag) {
62 1.1 jmcneill case DT_REL:
63 1.1 jmcneill rel = (Elf32_Rel*)
64 1.1 jmcneill ((unsigned long)dyn[i].d_un.d_ptr
65 1.1 jmcneill + ldbase);
66 1.1 jmcneill break;
67 1.1 jmcneill
68 1.1 jmcneill case DT_RELSZ:
69 1.1 jmcneill relsz = dyn[i].d_un.d_val;
70 1.1 jmcneill break;
71 1.1 jmcneill
72 1.1 jmcneill case DT_RELENT:
73 1.1 jmcneill relent = dyn[i].d_un.d_val;
74 1.1 jmcneill break;
75 1.1 jmcneill
76 1.1 jmcneill default:
77 1.1 jmcneill break;
78 1.1 jmcneill }
79 1.1 jmcneill }
80 1.1 jmcneill
81 1.1 jmcneill if (!rel && relent == 0)
82 1.1 jmcneill return EFI_SUCCESS;
83 1.1 jmcneill
84 1.1 jmcneill if (!rel || relent == 0)
85 1.1 jmcneill return EFI_LOAD_ERROR;
86 1.1 jmcneill
87 1.1 jmcneill while (relsz > 0) {
88 1.1 jmcneill /* apply the relocs */
89 1.1 jmcneill switch (ELF32_R_TYPE (rel->r_info)) {
90 1.1 jmcneill case R_ARM_NONE:
91 1.1 jmcneill break;
92 1.1 jmcneill
93 1.1 jmcneill case R_ARM_RELATIVE:
94 1.1 jmcneill addr = (unsigned long *)
95 1.1 jmcneill (ldbase + rel->r_offset);
96 1.1 jmcneill *addr += ldbase;
97 1.1 jmcneill break;
98 1.1 jmcneill
99 1.1 jmcneill default:
100 1.1 jmcneill break;
101 1.1 jmcneill }
102 1.1 jmcneill rel = (Elf32_Rel*) ((char *) rel + relent);
103 1.1 jmcneill relsz -= relent;
104 1.1 jmcneill }
105 1.1 jmcneill return EFI_SUCCESS;
106 1.1 jmcneill }
107