2
0

dlstart.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #include <stddef.h>
  2. #include "dynlink.h"
  3. #include "libc.h"
  4. #ifndef START
  5. #define START "_dlstart"
  6. #endif
  7. #define SHARED
  8. #include "crt_arch.h"
  9. #ifndef GETFUNCSYM
  10. #define GETFUNCSYM(fp, sym, got) do { \
  11. hidden void sym(); \
  12. static void (*static_func_ptr)() = sym; \
  13. __asm__ __volatile__ ( "" : "+m"(static_func_ptr) : : "memory"); \
  14. *(fp) = static_func_ptr; } while(0)
  15. #endif
  16. hidden void _dlstart_c(size_t *sp, size_t *dynv)
  17. {
  18. size_t i, aux[AUX_CNT], dyn[DYN_CNT];
  19. size_t *rel, rel_size, base;
  20. int argc = *sp;
  21. char **argv = (void *)(sp+1);
  22. for (i=argc+1; argv[i]; i++);
  23. size_t *auxv = (void *)(argv+i+1);
  24. for (i=0; i<AUX_CNT; i++) aux[i] = 0;
  25. for (i=0; auxv[i]; i+=2) if (auxv[i]<AUX_CNT)
  26. aux[auxv[i]] = auxv[i+1];
  27. #if DL_FDPIC
  28. struct fdpic_loadseg *segs, fakeseg;
  29. size_t j;
  30. if (dynv) {
  31. /* crt_arch.h entry point asm is responsible for reserving
  32. * space and moving the extra fdpic arguments to the stack
  33. * vector where they are easily accessible from C. */
  34. segs = ((struct fdpic_loadmap *)(sp[-1] ? sp[-1] : sp[-2]))->segs;
  35. } else {
  36. /* If dynv is null, the entry point was started from loader
  37. * that is not fdpic-aware. We can assume normal fixed-
  38. * displacement ELF loading was performed, but when ldso was
  39. * run as a command, finding the Ehdr is a heursitic: we
  40. * have to assume Phdrs start in the first 4k of the file. */
  41. base = aux[AT_BASE];
  42. if (!base) base = aux[AT_PHDR] & -4096;
  43. segs = &fakeseg;
  44. segs[0].addr = base;
  45. segs[0].p_vaddr = 0;
  46. segs[0].p_memsz = -1;
  47. Ehdr *eh = (void *)base;
  48. Phdr *ph = (void *)(base + eh->e_phoff);
  49. size_t phnum = eh->e_phnum;
  50. size_t phent = eh->e_phentsize;
  51. while (phnum-- && ph->p_type != PT_DYNAMIC)
  52. ph = (void *)((size_t)ph + phent);
  53. dynv = (void *)(base + ph->p_vaddr);
  54. }
  55. #endif
  56. for (i=0; i<DYN_CNT; i++) dyn[i] = 0;
  57. for (i=0; dynv[i]; i+=2) if (dynv[i]<DYN_CNT)
  58. dyn[dynv[i]] = dynv[i+1];
  59. #if DL_FDPIC
  60. for (i=0; i<DYN_CNT; i++) {
  61. if (i==DT_RELASZ || i==DT_RELSZ) continue;
  62. if (!dyn[i]) continue;
  63. for (j=0; dyn[i]-segs[j].p_vaddr >= segs[j].p_memsz; j++);
  64. dyn[i] += segs[j].addr - segs[j].p_vaddr;
  65. }
  66. base = 0;
  67. const Sym *syms = (void *)dyn[DT_SYMTAB];
  68. rel = (void *)dyn[DT_RELA];
  69. rel_size = dyn[DT_RELASZ];
  70. for (; rel_size; rel+=3, rel_size-=3*sizeof(size_t)) {
  71. if (!IS_RELATIVE(rel[1], syms)) continue;
  72. for (j=0; rel[0]-segs[j].p_vaddr >= segs[j].p_memsz; j++);
  73. size_t *rel_addr = (void *)
  74. (rel[0] + segs[j].addr - segs[j].p_vaddr);
  75. if (R_TYPE(rel[1]) == REL_FUNCDESC_VAL) {
  76. *rel_addr += segs[rel_addr[1]].addr
  77. - segs[rel_addr[1]].p_vaddr
  78. + syms[R_SYM(rel[1])].st_value;
  79. rel_addr[1] = dyn[DT_PLTGOT];
  80. } else {
  81. size_t val = syms[R_SYM(rel[1])].st_value;
  82. for (j=0; val-segs[j].p_vaddr >= segs[j].p_memsz; j++);
  83. *rel_addr = rel[2] + segs[j].addr - segs[j].p_vaddr + val;
  84. }
  85. }
  86. #else
  87. /* If the dynamic linker is invoked as a command, its load
  88. * address is not available in the aux vector. Instead, compute
  89. * the load address as the difference between &_DYNAMIC and the
  90. * virtual address in the PT_DYNAMIC program header. */
  91. base = aux[AT_BASE];
  92. if (!base) {
  93. size_t phnum = aux[AT_PHNUM];
  94. size_t phentsize = aux[AT_PHENT];
  95. Phdr *ph = (void *)aux[AT_PHDR];
  96. for (i=phnum; i--; ph = (void *)((char *)ph + phentsize)) {
  97. if (ph->p_type == PT_DYNAMIC) {
  98. base = (size_t)dynv - ph->p_vaddr;
  99. break;
  100. }
  101. }
  102. }
  103. /* MIPS uses an ugly packed form for GOT relocations. Since we
  104. * can't make function calls yet and the code is tiny anyway,
  105. * it's simply inlined here. */
  106. if (NEED_MIPS_GOT_RELOCS) {
  107. size_t local_cnt = 0;
  108. size_t *got = (void *)(base + dyn[DT_PLTGOT]);
  109. for (i=0; dynv[i]; i+=2) if (dynv[i]==DT_MIPS_LOCAL_GOTNO)
  110. local_cnt = dynv[i+1];
  111. for (i=0; i<local_cnt; i++) got[i] += base;
  112. }
  113. rel = (void *)(base+dyn[DT_REL]);
  114. rel_size = dyn[DT_RELSZ];
  115. for (; rel_size; rel+=2, rel_size-=2*sizeof(size_t)) {
  116. if (!IS_RELATIVE(rel[1], 0)) continue;
  117. size_t *rel_addr = (void *)(base + rel[0]);
  118. *rel_addr += base;
  119. }
  120. rel = (void *)(base+dyn[DT_RELA]);
  121. rel_size = dyn[DT_RELASZ];
  122. for (; rel_size; rel+=3, rel_size-=3*sizeof(size_t)) {
  123. if (!IS_RELATIVE(rel[1], 0)) continue;
  124. size_t *rel_addr = (void *)(base + rel[0]);
  125. *rel_addr = base + rel[2];
  126. }
  127. rel = (void *)(base+dyn[DT_RELR]);
  128. rel_size = dyn[DT_RELRSZ];
  129. size_t *relr_addr = 0;
  130. for (; rel_size; rel++, rel_size-=sizeof(size_t)) {
  131. if ((rel[0]&1) == 0) {
  132. relr_addr = (void *)(base + rel[0]);
  133. *relr_addr++ += base;
  134. } else {
  135. for (size_t i=0, bitmap=rel[0]; bitmap>>=1; i++)
  136. if (bitmap&1)
  137. relr_addr[i] += base;
  138. relr_addr += 8*sizeof(size_t)-1;
  139. }
  140. }
  141. #endif
  142. stage2_func dls2;
  143. GETFUNCSYM(&dls2, __dls2, base+dyn[DT_PLTGOT]);
  144. dls2((void *)base, sp);
  145. }