Skip to content

Commit a272858

Browse files
Ard Biesheuvelctmarinas
authored andcommitted
extable: add support for relative extables to search and sort routines
This adds support to the generic search_extable() and sort_extable() implementations for dealing with exception table entries whose fields contain relative offsets rather than absolute addresses. Acked-by: Helge Deller <deller@gmx.de> Acked-by: Heiko Carstens <heiko.carstens@de.ibm.com> Acked-by: H. Peter Anvin <hpa@linux.intel.com> Acked-by: Tony Luck <tony.luck@intel.com> Acked-by: Will Deacon <will.deacon@arm.com> Acked-by: Richard Henderson <rth@twiddle.net> Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org> Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
1 parent 7b957b6 commit a272858

File tree

1 file changed

+41
-9
lines changed

1 file changed

+41
-9
lines changed

lib/extable.c

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,37 @@
1414
#include <linux/sort.h>
1515
#include <asm/uaccess.h>
1616

17+
#ifndef ARCH_HAS_RELATIVE_EXTABLE
18+
#define ex_to_insn(x) ((x)->insn)
19+
#else
20+
static inline unsigned long ex_to_insn(const struct exception_table_entry *x)
21+
{
22+
return (unsigned long)&x->insn + x->insn;
23+
}
24+
#endif
25+
1726
#ifndef ARCH_HAS_SORT_EXTABLE
27+
#ifndef ARCH_HAS_RELATIVE_EXTABLE
28+
#define swap_ex NULL
29+
#else
30+
static void swap_ex(void *a, void *b, int size)
31+
{
32+
struct exception_table_entry *x = a, *y = b, tmp;
33+
int delta = b - a;
34+
35+
tmp = *x;
36+
x->insn = y->insn + delta;
37+
y->insn = tmp.insn - delta;
38+
39+
#ifdef swap_ex_entry_fixup
40+
swap_ex_entry_fixup(x, y, tmp, delta);
41+
#else
42+
x->fixup = y->fixup + delta;
43+
y->fixup = tmp.fixup - delta;
44+
#endif
45+
}
46+
#endif /* ARCH_HAS_RELATIVE_EXTABLE */
47+
1848
/*
1949
* The exception table needs to be sorted so that the binary
2050
* search that we use to find entries in it works properly.
@@ -26,9 +56,9 @@ static int cmp_ex(const void *a, const void *b)
2656
const struct exception_table_entry *x = a, *y = b;
2757

2858
/* avoid overflow */
29-
if (x->insn > y->insn)
59+
if (ex_to_insn(x) > ex_to_insn(y))
3060
return 1;
31-
if (x->insn < y->insn)
61+
if (ex_to_insn(x) < ex_to_insn(y))
3262
return -1;
3363
return 0;
3464
}
@@ -37,7 +67,7 @@ void sort_extable(struct exception_table_entry *start,
3767
struct exception_table_entry *finish)
3868
{
3969
sort(start, finish - start, sizeof(struct exception_table_entry),
40-
cmp_ex, NULL);
70+
cmp_ex, swap_ex);
4171
}
4272

4373
#ifdef CONFIG_MODULES
@@ -48,13 +78,15 @@ void sort_extable(struct exception_table_entry *start,
4878
void trim_init_extable(struct module *m)
4979
{
5080
/*trim the beginning*/
51-
while (m->num_exentries && within_module_init(m->extable[0].insn, m)) {
81+
while (m->num_exentries &&
82+
within_module_init(ex_to_insn(&m->extable[0]), m)) {
5283
m->extable++;
5384
m->num_exentries--;
5485
}
5586
/*trim the end*/
5687
while (m->num_exentries &&
57-
within_module_init(m->extable[m->num_exentries-1].insn, m))
88+
within_module_init(ex_to_insn(&m->extable[m->num_exentries - 1]),
89+
m))
5890
m->num_exentries--;
5991
}
6092
#endif /* CONFIG_MODULES */
@@ -81,13 +113,13 @@ search_extable(const struct exception_table_entry *first,
81113
* careful, the distance between value and insn
82114
* can be larger than MAX_LONG:
83115
*/
84-
if (mid->insn < value)
116+
if (ex_to_insn(mid) < value)
85117
first = mid + 1;
86-
else if (mid->insn > value)
118+
else if (ex_to_insn(mid) > value)
87119
last = mid - 1;
88120
else
89121
return mid;
90-
}
91-
return NULL;
122+
}
123+
return NULL;
92124
}
93125
#endif

0 commit comments

Comments
 (0)