Skip to content

Commit 9151dde

Browse files
committed
LoongArch: module: Use got/plt section indices for relocations
Instead of saving a pointer to the .got, .plt and .plt_idx sections to apply {got,plt}-based relocations, save and use their section indices instead. The mod->arch.{core,init}.{got,plt} pointers were problematic for live- patch because they pointed within temporary section headers (provided by the module loader via info->sechdrs) that would be freed after module load. Since livepatch modules may need to apply relocations post-module- load (for example, to patch a module that is loaded later), using section indices to offset into the section headers (instead of accessing them through a saved pointer) allows livepatch modules on LoongArch to pass in their own copy of the section headers to apply_relocate_add() to apply delayed relocations. The method used is same as commit c8ebf64 ("arm64/module: use plt section indices for relocations"). Signed-off-by: Hongchen Zhang <[email protected]> Signed-off-by: Huacai Chen <[email protected]>
1 parent 09f3360 commit 9151dde

File tree

3 files changed

+68
-47
lines changed

3 files changed

+68
-47
lines changed

arch/loongarch/include/asm/module.h

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#define RELA_STACK_DEPTH 16
1212

1313
struct mod_section {
14-
Elf_Shdr *shdr;
14+
int shndx;
1515
int num_entries;
1616
int max_entries;
1717
};
@@ -37,8 +37,8 @@ struct plt_idx_entry {
3737
Elf_Addr symbol_addr;
3838
};
3939

40-
Elf_Addr module_emit_got_entry(struct module *mod, Elf_Addr val);
41-
Elf_Addr module_emit_plt_entry(struct module *mod, Elf_Addr val);
40+
Elf_Addr module_emit_got_entry(struct module *mod, Elf_Shdr *sechdrs, Elf_Addr val);
41+
Elf_Addr module_emit_plt_entry(struct module *mod, Elf_Shdr *sechdrs, Elf_Addr val);
4242

4343
static inline struct got_entry emit_got_entry(Elf_Addr val)
4444
{
@@ -62,10 +62,10 @@ static inline struct plt_idx_entry emit_plt_idx_entry(unsigned long val)
6262
return (struct plt_idx_entry) { val };
6363
}
6464

65-
static inline int get_plt_idx(unsigned long val, const struct mod_section *sec)
65+
static inline int get_plt_idx(unsigned long val, Elf_Shdr *sechdrs, const struct mod_section *sec)
6666
{
6767
int i;
68-
struct plt_idx_entry *plt_idx = (struct plt_idx_entry *)sec->shdr->sh_addr;
68+
struct plt_idx_entry *plt_idx = (struct plt_idx_entry *)sechdrs[sec->shndx].sh_addr;
6969

7070
for (i = 0; i < sec->num_entries; i++) {
7171
if (plt_idx[i].symbol_addr == val)
@@ -76,11 +76,12 @@ static inline int get_plt_idx(unsigned long val, const struct mod_section *sec)
7676
}
7777

7878
static inline struct plt_entry *get_plt_entry(unsigned long val,
79-
const struct mod_section *sec_plt,
80-
const struct mod_section *sec_plt_idx)
79+
Elf_Shdr *sechdrs,
80+
const struct mod_section *sec_plt,
81+
const struct mod_section *sec_plt_idx)
8182
{
82-
int plt_idx = get_plt_idx(val, sec_plt_idx);
83-
struct plt_entry *plt = (struct plt_entry *)sec_plt->shdr->sh_addr;
83+
int plt_idx = get_plt_idx(val, sechdrs, sec_plt_idx);
84+
struct plt_entry *plt = (struct plt_entry *)sechdrs[sec_plt->shndx].sh_addr;
8485

8586
if (plt_idx < 0)
8687
return NULL;
@@ -89,10 +90,11 @@ static inline struct plt_entry *get_plt_entry(unsigned long val,
8990
}
9091

9192
static inline struct got_entry *get_got_entry(Elf_Addr val,
93+
Elf_Shdr *sechdrs,
9294
const struct mod_section *sec)
9395
{
94-
struct got_entry *got = (struct got_entry *)sec->shdr->sh_addr;
9596
int i;
97+
struct got_entry *got = (struct got_entry *)sechdrs[sec->shndx].sh_addr;
9698

9799
for (i = 0; i < sec->num_entries; i++)
98100
if (got[i].symbol_addr == val)

arch/loongarch/kernel/module-sections.c

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@
77
#include <linux/kernel.h>
88
#include <linux/module.h>
99

10-
Elf_Addr module_emit_got_entry(struct module *mod, Elf_Addr val)
10+
Elf_Addr module_emit_got_entry(struct module *mod, Elf_Shdr *sechdrs, Elf_Addr val)
1111
{
1212
struct mod_section *got_sec = &mod->arch.got;
1313
int i = got_sec->num_entries;
14-
struct got_entry *got = get_got_entry(val, got_sec);
14+
struct got_entry *got = get_got_entry(val, sechdrs, got_sec);
1515

1616
if (got)
1717
return (Elf_Addr)got;
1818

1919
/* There is no GOT entry for val yet, create a new one. */
20-
got = (struct got_entry *)got_sec->shdr->sh_addr;
20+
got = (struct got_entry *)sechdrs[got_sec->shndx].sh_addr;
2121
got[i] = emit_got_entry(val);
2222

2323
got_sec->num_entries++;
@@ -33,12 +33,12 @@ Elf_Addr module_emit_got_entry(struct module *mod, Elf_Addr val)
3333
return (Elf_Addr)&got[i];
3434
}
3535

36-
Elf_Addr module_emit_plt_entry(struct module *mod, Elf_Addr val)
36+
Elf_Addr module_emit_plt_entry(struct module *mod, Elf_Shdr *sechdrs, Elf_Addr val)
3737
{
3838
int nr;
3939
struct mod_section *plt_sec = &mod->arch.plt;
4040
struct mod_section *plt_idx_sec = &mod->arch.plt_idx;
41-
struct plt_entry *plt = get_plt_entry(val, plt_sec, plt_idx_sec);
41+
struct plt_entry *plt = get_plt_entry(val, sechdrs, plt_sec, plt_idx_sec);
4242
struct plt_idx_entry *plt_idx;
4343

4444
if (plt)
@@ -47,9 +47,9 @@ Elf_Addr module_emit_plt_entry(struct module *mod, Elf_Addr val)
4747
nr = plt_sec->num_entries;
4848

4949
/* There is no duplicate entry, create a new one */
50-
plt = (struct plt_entry *)plt_sec->shdr->sh_addr;
50+
plt = (struct plt_entry *)sechdrs[plt_sec->shndx].sh_addr;
5151
plt[nr] = emit_plt_entry(val);
52-
plt_idx = (struct plt_idx_entry *)plt_idx_sec->shdr->sh_addr;
52+
plt_idx = (struct plt_idx_entry *)sechdrs[plt_idx_sec->shndx].sh_addr;
5353
plt_idx[nr] = emit_plt_idx_entry(val);
5454

5555
plt_sec->num_entries++;
@@ -103,28 +103,29 @@ int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
103103
char *secstrings, struct module *mod)
104104
{
105105
unsigned int i, num_plts = 0, num_gots = 0;
106+
Elf_Shdr *got_sec, *plt_sec, *plt_idx_sec;
106107

107108
/*
108109
* Find the empty .plt sections.
109110
*/
110111
for (i = 0; i < ehdr->e_shnum; i++) {
111112
if (!strcmp(secstrings + sechdrs[i].sh_name, ".got"))
112-
mod->arch.got.shdr = sechdrs + i;
113+
mod->arch.got.shndx = i;
113114
else if (!strcmp(secstrings + sechdrs[i].sh_name, ".plt"))
114-
mod->arch.plt.shdr = sechdrs + i;
115+
mod->arch.plt.shndx = i;
115116
else if (!strcmp(secstrings + sechdrs[i].sh_name, ".plt.idx"))
116-
mod->arch.plt_idx.shdr = sechdrs + i;
117+
mod->arch.plt_idx.shndx = i;
117118
}
118119

119-
if (!mod->arch.got.shdr) {
120+
if (!mod->arch.got.shndx) {
120121
pr_err("%s: module GOT section(s) missing\n", mod->name);
121122
return -ENOEXEC;
122123
}
123-
if (!mod->arch.plt.shdr) {
124+
if (!mod->arch.plt.shndx) {
124125
pr_err("%s: module PLT section(s) missing\n", mod->name);
125126
return -ENOEXEC;
126127
}
127-
if (!mod->arch.plt_idx.shdr) {
128+
if (!mod->arch.plt_idx.shndx) {
128129
pr_err("%s: module PLT.IDX section(s) missing\n", mod->name);
129130
return -ENOEXEC;
130131
}
@@ -145,24 +146,27 @@ int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
145146
count_max_entries(relas, num_rela, &num_plts, &num_gots);
146147
}
147148

148-
mod->arch.got.shdr->sh_type = SHT_NOBITS;
149-
mod->arch.got.shdr->sh_flags = SHF_ALLOC;
150-
mod->arch.got.shdr->sh_addralign = L1_CACHE_BYTES;
151-
mod->arch.got.shdr->sh_size = (num_gots + 1) * sizeof(struct got_entry);
149+
got_sec = sechdrs + mod->arch.got.shndx;
150+
got_sec->sh_type = SHT_NOBITS;
151+
got_sec->sh_flags = SHF_ALLOC;
152+
got_sec->sh_addralign = L1_CACHE_BYTES;
153+
got_sec->sh_size = (num_gots + 1) * sizeof(struct got_entry);
152154
mod->arch.got.num_entries = 0;
153155
mod->arch.got.max_entries = num_gots;
154156

155-
mod->arch.plt.shdr->sh_type = SHT_NOBITS;
156-
mod->arch.plt.shdr->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
157-
mod->arch.plt.shdr->sh_addralign = L1_CACHE_BYTES;
158-
mod->arch.plt.shdr->sh_size = (num_plts + 1) * sizeof(struct plt_entry);
157+
plt_sec = sechdrs + mod->arch.plt.shndx;
158+
plt_sec->sh_type = SHT_NOBITS;
159+
plt_sec->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
160+
plt_sec->sh_addralign = L1_CACHE_BYTES;
161+
plt_sec->sh_size = (num_plts + 1) * sizeof(struct plt_entry);
159162
mod->arch.plt.num_entries = 0;
160163
mod->arch.plt.max_entries = num_plts;
161164

162-
mod->arch.plt_idx.shdr->sh_type = SHT_NOBITS;
163-
mod->arch.plt_idx.shdr->sh_flags = SHF_ALLOC;
164-
mod->arch.plt_idx.shdr->sh_addralign = L1_CACHE_BYTES;
165-
mod->arch.plt_idx.shdr->sh_size = (num_plts + 1) * sizeof(struct plt_idx_entry);
165+
plt_idx_sec = sechdrs + mod->arch.plt_idx.shndx;
166+
plt_idx_sec->sh_type = SHT_NOBITS;
167+
plt_idx_sec->sh_flags = SHF_ALLOC;
168+
plt_idx_sec->sh_addralign = L1_CACHE_BYTES;
169+
plt_idx_sec->sh_size = (num_plts + 1) * sizeof(struct plt_idx_entry);
166170
mod->arch.plt_idx.num_entries = 0;
167171
mod->arch.plt_idx.max_entries = num_plts;
168172

arch/loongarch/kernel/module.c

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,17 @@ static int apply_r_larch_sop_push_dup(struct module *mod, u32 *location, Elf_Add
9999
return 0;
100100
}
101101

102-
static int apply_r_larch_sop_push_plt_pcrel(struct module *mod, u32 *location, Elf_Addr v,
102+
static int apply_r_larch_sop_push_plt_pcrel(struct module *mod,
103+
Elf_Shdr *sechdrs, u32 *location, Elf_Addr v,
103104
s64 *rela_stack, size_t *rela_stack_top, unsigned int type)
104105
{
105106
ptrdiff_t offset = (void *)v - (void *)location;
106107

107108
if (offset >= SZ_128M)
108-
v = module_emit_plt_entry(mod, v);
109+
v = module_emit_plt_entry(mod, sechdrs, v);
109110

110111
if (offset < -SZ_128M)
111-
v = module_emit_plt_entry(mod, v);
112+
v = module_emit_plt_entry(mod, sechdrs, v);
112113

113114
return apply_r_larch_sop_push_pcrel(mod, location, v, rela_stack, rela_stack_top, type);
114115
}
@@ -272,17 +273,18 @@ static int apply_r_larch_add_sub(struct module *mod, u32 *location, Elf_Addr v,
272273
}
273274
}
274275

275-
static int apply_r_larch_b26(struct module *mod, u32 *location, Elf_Addr v,
276+
static int apply_r_larch_b26(struct module *mod,
277+
Elf_Shdr *sechdrs, u32 *location, Elf_Addr v,
276278
s64 *rela_stack, size_t *rela_stack_top, unsigned int type)
277279
{
278280
ptrdiff_t offset = (void *)v - (void *)location;
279281
union loongarch_instruction *insn = (union loongarch_instruction *)location;
280282

281283
if (offset >= SZ_128M)
282-
v = module_emit_plt_entry(mod, v);
284+
v = module_emit_plt_entry(mod, sechdrs, v);
283285

284286
if (offset < -SZ_128M)
285-
v = module_emit_plt_entry(mod, v);
287+
v = module_emit_plt_entry(mod, sechdrs, v);
286288

287289
offset = (void *)v - (void *)location;
288290

@@ -339,10 +341,11 @@ static int apply_r_larch_pcala(struct module *mod, u32 *location, Elf_Addr v,
339341
return 0;
340342
}
341343

342-
static int apply_r_larch_got_pc(struct module *mod, u32 *location, Elf_Addr v,
344+
static int apply_r_larch_got_pc(struct module *mod,
345+
Elf_Shdr *sechdrs, u32 *location, Elf_Addr v,
343346
s64 *rela_stack, size_t *rela_stack_top, unsigned int type)
344347
{
345-
Elf_Addr got = module_emit_got_entry(mod, v);
348+
Elf_Addr got = module_emit_got_entry(mod, sechdrs, v);
346349

347350
if (!got)
348351
return -EINVAL;
@@ -387,13 +390,10 @@ static reloc_rela_handler reloc_rela_handlers[] = {
387390
[R_LARCH_SOP_PUSH_PCREL] = apply_r_larch_sop_push_pcrel,
388391
[R_LARCH_SOP_PUSH_ABSOLUTE] = apply_r_larch_sop_push_absolute,
389392
[R_LARCH_SOP_PUSH_DUP] = apply_r_larch_sop_push_dup,
390-
[R_LARCH_SOP_PUSH_PLT_PCREL] = apply_r_larch_sop_push_plt_pcrel,
391393
[R_LARCH_SOP_SUB ... R_LARCH_SOP_IF_ELSE] = apply_r_larch_sop,
392394
[R_LARCH_SOP_POP_32_S_10_5 ... R_LARCH_SOP_POP_32_U] = apply_r_larch_sop_imm_field,
393395
[R_LARCH_ADD32 ... R_LARCH_SUB64] = apply_r_larch_add_sub,
394-
[R_LARCH_B26] = apply_r_larch_b26,
395396
[R_LARCH_PCALA_HI20...R_LARCH_PCALA64_HI12] = apply_r_larch_pcala,
396-
[R_LARCH_GOT_PC_HI20...R_LARCH_GOT_PC_LO12] = apply_r_larch_got_pc,
397397
};
398398

399399
int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
@@ -444,7 +444,22 @@ int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
444444
sym->st_value, rel[i].r_addend, (u64)location);
445445

446446
v = sym->st_value + rel[i].r_addend;
447-
err = handler(mod, location, v, rela_stack, &rela_stack_top, type);
447+
switch (type) {
448+
case R_LARCH_B26:
449+
err = apply_r_larch_b26(mod, sechdrs, location,
450+
v, rela_stack, &rela_stack_top, type);
451+
break;
452+
case R_LARCH_GOT_PC_HI20...R_LARCH_GOT_PC_LO12:
453+
err = apply_r_larch_got_pc(mod, sechdrs, location,
454+
v, rela_stack, &rela_stack_top, type);
455+
break;
456+
case R_LARCH_SOP_PUSH_PLT_PCREL:
457+
err = apply_r_larch_sop_push_plt_pcrel(mod, sechdrs, location,
458+
v, rela_stack, &rela_stack_top, type);
459+
break;
460+
default:
461+
err = handler(mod, location, v, rela_stack, &rela_stack_top, type);
462+
}
448463
if (err)
449464
return err;
450465
}

0 commit comments

Comments
 (0)