extable.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include <linux/module.h>
  2. #include <linux/sort.h>
  3. #include <asm/uaccess.h>
  4. /*
  5. * Search one exception table for an entry corresponding to the
  6. * given instruction address, and return the address of the entry,
  7. * or NULL if none is found.
  8. * We use a binary search, and thus we assume that the table is
  9. * already sorted.
  10. */
  11. const struct exception_table_entry *
  12. search_extable(const struct exception_table_entry *first,
  13. const struct exception_table_entry *last,
  14. unsigned long value)
  15. {
  16. const struct exception_table_entry *mid;
  17. unsigned long addr;
  18. while (first <= last) {
  19. mid = ((last - first) >> 1) + first;
  20. addr = extable_insn(mid);
  21. if (addr < value)
  22. first = mid + 1;
  23. else if (addr > value)
  24. last = mid - 1;
  25. else
  26. return mid;
  27. }
  28. return NULL;
  29. }
  30. /*
  31. * The exception table needs to be sorted so that the binary
  32. * search that we use to find entries in it works properly.
  33. * This is used both for the kernel exception table and for
  34. * the exception tables of modules that get loaded.
  35. *
  36. */
  37. static int cmp_ex(const void *a, const void *b)
  38. {
  39. const struct exception_table_entry *x = a, *y = b;
  40. /* This compare is only valid after normalization. */
  41. return x->insn - y->insn;
  42. }
  43. void sort_extable(struct exception_table_entry *start,
  44. struct exception_table_entry *finish)
  45. {
  46. struct exception_table_entry *p;
  47. int i;
  48. /* Normalize entries to being relative to the start of the section */
  49. for (p = start, i = 0; p < finish; p++, i += 8) {
  50. p->insn += i;
  51. p->fixup += i + 4;
  52. }
  53. sort(start, finish - start, sizeof(*start), cmp_ex, NULL);
  54. /* Denormalize all entries */
  55. for (p = start, i = 0; p < finish; p++, i += 8) {
  56. p->insn -= i;
  57. p->fixup -= i + 4;
  58. }
  59. }
  60. #ifdef CONFIG_MODULES
  61. /*
  62. * If the exception table is sorted, any referring to the module init
  63. * will be at the beginning or the end.
  64. */
  65. void trim_init_extable(struct module *m)
  66. {
  67. /* Trim the beginning */
  68. while (m->num_exentries &&
  69. within_module_init(extable_insn(&m->extable[0]), m)) {
  70. m->extable++;
  71. m->num_exentries--;
  72. }
  73. /* Trim the end */
  74. while (m->num_exentries &&
  75. within_module_init(extable_insn(&m->extable[m->num_exentries-1]), m))
  76. m->num_exentries--;
  77. }
  78. #endif /* CONFIG_MODULES */