1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Kernel module help for Alpha.
3 Copyright (C) 2002 Richard Henderson.
6 #include <linux/moduleloader.h>
8 #include <linux/vmalloc.h>
10 #include <linux/string.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
17 #define DEBUGP(fmt...)
20 /* Allocate the GOT at the end of the core sections. */
23 struct got_entry *next;
24 Elf64_Sxword r_addend;
29 process_reloc_for_got(Elf64_Rela *rela,
30 struct got_entry *chains, Elf64_Xword *poffset)
32 unsigned long r_sym = ELF64_R_SYM (rela->r_info);
33 unsigned long r_type = ELF64_R_TYPE (rela->r_info);
34 Elf64_Sxword r_addend = rela->r_addend;
37 if (r_type != R_ALPHA_LITERAL)
40 for (g = chains + r_sym; g ; g = g->next)
41 if (g->r_addend == r_addend) {
42 if (g->got_offset == 0) {
43 g->got_offset = *poffset;
49 g = kmalloc (sizeof (*g), GFP_KERNEL);
50 g->next = chains[r_sym].next;
51 g->r_addend = r_addend;
52 g->got_offset = *poffset;
54 chains[r_sym].next = g;
57 /* Trick: most of the ELF64_R_TYPE field is unused. There are
58 42 valid relocation types, and a 32-bit field. Co-opt the
59 bits above 256 to store the got offset for this reloc. */
60 rela->r_info |= g->got_offset << 8;
64 module_frob_arch_sections(Elf64_Ehdr *hdr, Elf64_Shdr *sechdrs,
65 char *secstrings, struct module *me)
67 struct got_entry *chains;
69 Elf64_Shdr *esechdrs, *symtab, *s, *got;
70 unsigned long nsyms, nrela, i;
72 esechdrs = sechdrs + hdr->e_shnum;
75 /* Find out how large the symbol table is. Allocate one got_entry
76 head per symbol. Normally this will be enough, but not always.
77 We'll chain different offsets for the symbol down each head. */
78 for (s = sechdrs; s < esechdrs; ++s)
79 if (s->sh_type == SHT_SYMTAB)
81 else if (!strcmp(".got", secstrings + s->sh_name)) {
83 me->arch.gotsecindex = s - sechdrs;
87 printk(KERN_ERR "module %s: no symbol table\n", me->name);
91 printk(KERN_ERR "module %s: no got section\n", me->name);
95 nsyms = symtab->sh_size / sizeof(Elf64_Sym);
96 chains = kcalloc(nsyms, sizeof(struct got_entry), GFP_KERNEL);
99 "module %s: no memory for symbol chain buffer\n",
105 got->sh_addralign = 8;
106 got->sh_type = SHT_NOBITS;
108 /* Examine all LITERAL relocations to find out what GOT entries
109 are required. This sizes the GOT section as well. */
110 for (s = sechdrs; s < esechdrs; ++s)
111 if (s->sh_type == SHT_RELA) {
112 nrela = s->sh_size / sizeof(Elf64_Rela);
113 rela = (void *)hdr + s->sh_offset;
114 for (i = 0; i < nrela; ++i)
115 process_reloc_for_got(rela+i, chains,
119 /* Free the memory we allocated. */
120 for (i = 0; i < nsyms; ++i) {
121 struct got_entry *g, *n;
122 for (g = chains[i].next; g ; g = n) {
133 apply_relocate_add(Elf64_Shdr *sechdrs, const char *strtab,
134 unsigned int symindex, unsigned int relsec,
137 Elf64_Rela *rela = (void *)sechdrs[relsec].sh_addr;
138 unsigned long i, n = sechdrs[relsec].sh_size / sizeof(*rela);
139 Elf64_Sym *symtab, *sym;
140 void *base, *location;
141 unsigned long got, gp;
143 DEBUGP("Applying relocate section %u to %u\n", relsec,
144 sechdrs[relsec].sh_info);
146 base = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr;
147 symtab = (Elf64_Sym *)sechdrs[symindex].sh_addr;
149 /* The small sections were sorted to the end of the segment.
150 The following should definitely cover them. */
151 gp = (u64)me->core_layout.base + me->core_layout.size - 0x8000;
152 got = sechdrs[me->arch.gotsecindex].sh_addr;
154 for (i = 0; i < n; i++) {
155 unsigned long r_sym = ELF64_R_SYM (rela[i].r_info);
156 unsigned long r_type = ELF64_R_TYPE (rela[i].r_info);
157 unsigned long r_got_offset = r_type >> 8;
158 unsigned long value, hi, lo;
161 /* This is where to make the change. */
162 location = base + rela[i].r_offset;
164 /* This is the symbol it is referring to. Note that all
165 unresolved symbols have been resolved. */
166 sym = symtab + r_sym;
167 value = sym->st_value + rela[i].r_addend;
172 case R_ALPHA_REFLONG:
173 *(u32 *)location = value;
175 case R_ALPHA_REFQUAD:
176 /* BUG() can produce misaligned relocations. */
177 ((u32 *)location)[0] = value;
178 ((u32 *)location)[1] = value >> 32;
180 case R_ALPHA_GPREL32:
182 if ((int)value != value)
184 *(u32 *)location = value;
186 case R_ALPHA_LITERAL:
187 hi = got + r_got_offset;
191 *(u16 *)location = lo;
197 value = gp - (u64)location;
199 hi = (int)(value - lo);
200 if (hi + lo != value)
202 *(u16 *)location = hi >> 16;
203 *(u16 *)(location + rela[i].r_addend) = lo;
206 /* BRSGP is only allowed to bind to local symbols.
207 If the section is undef, this means that the
208 value was resolved from somewhere else. */
209 if (sym->st_shndx == SHN_UNDEF)
211 if ((sym->st_other & STO_ALPHA_STD_GPLOAD) ==
212 STO_ALPHA_STD_GPLOAD)
213 /* Omit the prologue. */
217 value -= (u64)location + 4;
220 value = (long)value >> 2;
221 if (value + (1<<21) >= 1<<22)
224 value |= *(u32 *)location & ~0x1fffff;
225 *(u32 *)location = value;
230 value -= (u64)location;
231 if ((int)value != value)
233 *(u32 *)location = value;
236 value -= (u64)location;
237 *(u64 *)location = value;
239 case R_ALPHA_GPRELHIGH:
240 value = (long)(value - gp + 0x8000) >> 16;
241 if ((short) value != value)
243 *(u16 *)location = value;
245 case R_ALPHA_GPRELLOW:
247 *(u16 *)location = value;
249 case R_ALPHA_GPREL16:
251 if ((short) value != value)
253 *(u16 *)location = value;
256 printk(KERN_ERR "module %s: Unknown relocation: %lu\n",
260 if (ELF64_ST_TYPE (sym->st_info) == STT_SECTION)
262 "module %s: Relocation (type %lu) overflow vs section %d\n",
263 me->name, r_type, sym->st_shndx);
266 "module %s: Relocation (type %lu) overflow vs %s\n",
267 me->name, r_type, strtab + sym->st_name);