reloc.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include <string.h>
  2. #include <elf.h>
  3. #include <endian.h>
  4. #if __BYTE_ORDER == __LITTLE_ENDIAN
  5. #define ENDIAN_SUFFIX "el"
  6. #else
  7. #define ENDIAN_SUFFIX ""
  8. #endif
  9. #define LDSO_ARCH "mips" ENDIAN_SUFFIX
  10. #define IS_COPY(x) ((x)==R_MIPS_COPY)
  11. #define IS_PLT(x) 1
  12. static inline void do_single_reloc(
  13. struct dso *self, unsigned char *base_addr,
  14. size_t *reloc_addr, int type, size_t addend,
  15. Sym *sym, size_t sym_size,
  16. struct symdef def, size_t sym_val)
  17. {
  18. switch(type) {
  19. case R_MIPS_JUMP_SLOT:
  20. *reloc_addr = sym_val;
  21. break;
  22. case R_MIPS_REL32:
  23. if (sym_val) *reloc_addr += sym_val;
  24. else *reloc_addr += (size_t)base_addr;
  25. break;
  26. case R_MIPS_COPY:
  27. memcpy(reloc_addr, (void *)sym_val, sym_size);
  28. break;
  29. case R_MIPS_TLS_DTPMOD32:
  30. *reloc_addr = def.dso ? def.dso->tls_id : self->tls_id;
  31. break;
  32. case R_MIPS_TLS_DTPREL32:
  33. *reloc_addr += def.sym->st_value;
  34. break;
  35. case R_MIPS_TLS_TPREL32:
  36. *reloc_addr += def.sym
  37. ? def.sym->st_value + def.dso->tls_offset - 0x7000
  38. : self->tls_offset - 0x7000;
  39. break;
  40. }
  41. }
  42. void __reloc_self(int c, size_t *a, size_t *dynv, size_t *got)
  43. {
  44. char *base;
  45. size_t t[20], n;
  46. for (a+=c+1; *a; a++);
  47. for (a++; *a; a+=2) if (*a<20) t[*a] = a[1];
  48. base = (char *)t[AT_BASE];
  49. if (!base) base = (char *)(t[AT_PHDR] & -t[AT_PAGESZ]);
  50. for (a=dynv; *a; a+=2) if (*a-0x70000000UL<20) t[*a&31] = a[1];
  51. n = t[DT_MIPS_LOCAL_GOTNO - 0x70000000];
  52. for (a=got; n; a++, n--) *a += (size_t)base;
  53. }
  54. static void do_relocs(struct dso *dso, size_t *rel, size_t rel_size, size_t stride);
  55. static void do_arch_relocs(struct dso *this, struct dso *head)
  56. {
  57. unsigned char *base = this->base;
  58. size_t *dynv = this->dynv;
  59. size_t dyn[20] = {0};
  60. size_t i;
  61. size_t rel[2], got=0;
  62. Sym *sym;
  63. for (i=0; dynv[i]; i+=2) {
  64. if (dynv[i]-0x70000000UL<20)
  65. dyn[dynv[i]&31] = dynv[i+1];
  66. else if (dynv[i] == DT_PLTGOT)
  67. got = dynv[i+1];
  68. }
  69. i = dyn[DT_MIPS_LOCAL_GOTNO-0x70000000];
  70. if (this->shortname && !strcmp(this->shortname, "libc.so")) {
  71. got += sizeof(size_t) * i;
  72. } else {
  73. for (; i; i--, got+=sizeof(size_t))
  74. *(size_t *)(base+got) += (size_t)base;
  75. }
  76. sym = this->syms + dyn[DT_MIPS_GOTSYM-0x70000000];
  77. i = dyn[DT_MIPS_SYMTABNO-0x70000000] - dyn[DT_MIPS_GOTSYM-0x70000000];
  78. for (; i; i--, got+=sizeof(size_t), sym++) {
  79. rel[0] = got;
  80. rel[1] = sym-this->syms << 8 | R_MIPS_JUMP_SLOT;
  81. *(size_t *)(base+got) = 0;
  82. do_relocs(this, rel, sizeof rel, 2);
  83. }
  84. }
  85. #define NEED_ARCH_RELOCS 1
  86. #define DYNAMIC_IS_RO 1