1530d68baSNigel Croxon /* reloc_ia32.c - position independent x86 ELF shared object relocator
2530d68baSNigel Croxon Copyright (C) 1999 Hewlett-Packard Co.
3530d68baSNigel Croxon Contributed by David Mosberger <davidm@hpl.hp.com>.
4530d68baSNigel Croxon
5530d68baSNigel Croxon All rights reserved.
6530d68baSNigel Croxon
7530d68baSNigel Croxon Redistribution and use in source and binary forms, with or without
8530d68baSNigel Croxon modification, are permitted provided that the following conditions
9530d68baSNigel Croxon are met:
10530d68baSNigel Croxon
11530d68baSNigel Croxon * Redistributions of source code must retain the above copyright
12530d68baSNigel Croxon notice, this list of conditions and the following disclaimer.
13530d68baSNigel Croxon * Redistributions in binary form must reproduce the above
14530d68baSNigel Croxon copyright notice, this list of conditions and the following
15530d68baSNigel Croxon disclaimer in the documentation and/or other materials
16530d68baSNigel Croxon provided with the distribution.
17530d68baSNigel Croxon * Neither the name of Hewlett-Packard Co. nor the names of its
18530d68baSNigel Croxon contributors may be used to endorse or promote products derived
19530d68baSNigel Croxon from this software without specific prior written permission.
20530d68baSNigel Croxon
21530d68baSNigel Croxon THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
22530d68baSNigel Croxon CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
23530d68baSNigel Croxon INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24530d68baSNigel Croxon MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25530d68baSNigel Croxon DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
26530d68baSNigel Croxon BE LIABLE FOR ANYDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
27530d68baSNigel Croxon OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28530d68baSNigel Croxon PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29530d68baSNigel Croxon PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30530d68baSNigel Croxon THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
31530d68baSNigel Croxon TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
32530d68baSNigel Croxon THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33530d68baSNigel Croxon SUCH DAMAGE.
34530d68baSNigel Croxon */
35530d68baSNigel Croxon
36530d68baSNigel Croxon #include <efi.h>
37530d68baSNigel Croxon #include <efilib.h>
38530d68baSNigel Croxon
39530d68baSNigel Croxon #include <elf.h>
40530d68baSNigel Croxon
_relocate(long ldbase,Elf32_Dyn * dyn,EFI_HANDLE image EFI_UNUSED,EFI_SYSTEM_TABLE * systab EFI_UNUSED)413dd05c7bSNigel Croxon EFI_STATUS _relocate (long ldbase, Elf32_Dyn *dyn,
42*5ff74ecaSNigel Croxon EFI_HANDLE image EFI_UNUSED,
43*5ff74ecaSNigel Croxon EFI_SYSTEM_TABLE *systab EFI_UNUSED)
44530d68baSNigel Croxon {
45530d68baSNigel Croxon long relsz = 0, relent = 0;
46530d68baSNigel Croxon Elf32_Rel *rel = 0;
47530d68baSNigel Croxon unsigned long *addr;
48530d68baSNigel Croxon int i;
49530d68baSNigel Croxon
50530d68baSNigel Croxon for (i = 0; dyn[i].d_tag != DT_NULL; ++i) {
51530d68baSNigel Croxon switch (dyn[i].d_tag) {
52530d68baSNigel Croxon case DT_REL:
53530d68baSNigel Croxon rel = (Elf32_Rel*)
54530d68baSNigel Croxon ((unsigned long)dyn[i].d_un.d_ptr
55530d68baSNigel Croxon + ldbase);
56530d68baSNigel Croxon break;
57530d68baSNigel Croxon
58530d68baSNigel Croxon case DT_RELSZ:
59530d68baSNigel Croxon relsz = dyn[i].d_un.d_val;
60530d68baSNigel Croxon break;
61530d68baSNigel Croxon
62530d68baSNigel Croxon case DT_RELENT:
63530d68baSNigel Croxon relent = dyn[i].d_un.d_val;
64530d68baSNigel Croxon break;
65530d68baSNigel Croxon
66530d68baSNigel Croxon case DT_RELA:
67530d68baSNigel Croxon break;
68530d68baSNigel Croxon
69530d68baSNigel Croxon default:
70530d68baSNigel Croxon break;
71530d68baSNigel Croxon }
72530d68baSNigel Croxon }
73530d68baSNigel Croxon
74530d68baSNigel Croxon if (!rel && relent == 0)
75530d68baSNigel Croxon return EFI_SUCCESS;
76530d68baSNigel Croxon
77530d68baSNigel Croxon if (!rel || relent == 0)
78530d68baSNigel Croxon return EFI_LOAD_ERROR;
79530d68baSNigel Croxon
80530d68baSNigel Croxon while (relsz > 0) {
81530d68baSNigel Croxon /* apply the relocs */
82530d68baSNigel Croxon switch (ELF32_R_TYPE (rel->r_info)) {
83530d68baSNigel Croxon case R_386_NONE:
84530d68baSNigel Croxon break;
85530d68baSNigel Croxon
86530d68baSNigel Croxon case R_386_RELATIVE:
87530d68baSNigel Croxon addr = (unsigned long *)
88530d68baSNigel Croxon (ldbase + rel->r_offset);
89530d68baSNigel Croxon *addr += ldbase;
90530d68baSNigel Croxon break;
91530d68baSNigel Croxon
92530d68baSNigel Croxon default:
93530d68baSNigel Croxon break;
94530d68baSNigel Croxon }
95530d68baSNigel Croxon rel = (Elf32_Rel*) ((char *) rel + relent);
96530d68baSNigel Croxon relsz -= relent;
97530d68baSNigel Croxon }
98530d68baSNigel Croxon return EFI_SUCCESS;
99530d68baSNigel Croxon }
100