dl-startup.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * Architecture specific code used by dl-startup.c
  3. * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
  4. *
  5. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  6. */
  7. #include <features.h>
  8. #include <bits/arm_bx.h>
  9. #if defined(__FDPIC__)
  10. #if !defined(__thumb__) || defined(__thumb2__)
  11. __asm__(
  12. " .arm\n"
  13. " .text\n"
  14. " .globl _start\n"
  15. " .type _start,%function\n"
  16. "_start:\n"
  17. /* We compute the parameters for __self_reloc:
  18. - r0 is a pointer to the loadmap (either from r8 or r7 if rtld is
  19. lauched in standalone mode)
  20. - r1 is a pointer to the start of .rofixup section
  21. - r2 is a pointer to the last word of .rofixup section
  22. __self_reloc will fix indirect addresses in .rofixup
  23. section and will return the relocated GOT value.
  24. */
  25. " sub r4, pc, #8\n"
  26. " ldr r1, .L__ROFIXUP_LIST__\n"
  27. " add r1, r1, r4\n"
  28. " ldr r2, .L__ROFIXUP_END__\n"
  29. " add r2, r2, r4\n"
  30. " movs r0, r8\n"
  31. " moveq r0, r7\n"
  32. " push {r7, r8, r9, r10}\n"
  33. " bl __self_reloc;\n"
  34. " pop {r7, r8, r9, r10}\n"
  35. /* We compute the parameters for dl_start(). See DL_START()
  36. macro below. The address of the user entry point is
  37. returned in dl_main_funcdesc (on stack). */
  38. " mov r1, r7\n"
  39. " mov r2, r8\n"
  40. " mov r3, r9\n"
  41. " mov r4, sp\n"
  42. " sub r5, sp, #8\n"
  43. " sub sp, sp, #16\n"
  44. " str r4, [sp, #4]\n"
  45. " str r5, [sp, #0]\n"
  46. " mov r9, r0\n"
  47. /* Save r9 into r4, to preserve the GOT pointer. */
  48. " mov r4, r9\n"
  49. " bl _dl_start;\n"
  50. /* Now compute parameters for entry point according to FDPIC ABI. */
  51. " ldr r10, .L_dl_fini_gotofffuncdesc\n"
  52. /* Save GOT value from r4. */
  53. " add r10, r10, r4\n"
  54. " ldr r5, [sp, #8]\n"
  55. " ldr r9, [sp, #12]\n"
  56. " add sp, sp, #16\n"
  57. " bx r5\n"
  58. ".loopforever:\n"
  59. " b .loopforever\n"
  60. ".L__ROFIXUP_LIST__:\n"
  61. " .word __ROFIXUP_LIST__ - _start\n"
  62. ".L__ROFIXUP_END__:\n"
  63. " .word __ROFIXUP_END__ - _start\n"
  64. ".L_dl_fini_gotofffuncdesc:\n"
  65. " .word _dl_fini(GOTOFFFUNCDESC)\n"
  66. " .size _start,.-_start\n"
  67. " .previous\n"
  68. );
  69. #else /* !defined(__thumb__) */
  70. #error Thumb-1 is not supported
  71. #endif /* !defined(__thumb__) */
  72. #else /* defined(__FDPIC__) */
  73. #if !defined(__thumb__)
  74. __asm__(
  75. " .text\n"
  76. " .globl _start\n"
  77. " .type _start,%function\n"
  78. " .hidden _start\n"
  79. "_start:\n"
  80. " @ at start time, all the args are on the stack\n"
  81. " mov r0, sp\n"
  82. " bl _dl_start\n"
  83. " @ returns user entry point in r0\n"
  84. " mov r6, r0\n"
  85. " @ we are PIC code, so get global offset table\n"
  86. " ldr sl, .L_GET_GOT\n"
  87. " add sl, pc, sl\n"
  88. ".L_GOT_GOT:\n"
  89. " @ See if we were run as a command with the executable file\n"
  90. " @ name as an extra leading argument.\n"
  91. " ldr r4, .L_SKIP_ARGS\n"
  92. " ldr r4, [sl, r4]\n"
  93. " @ get the original arg count\n"
  94. " ldr r1, [sp]\n"
  95. " @ subtract _dl_skip_args from it\n"
  96. " sub r1, r1, r4\n"
  97. " @ adjust the stack pointer to skip them\n"
  98. " add sp, sp, r4, lsl #2\n"
  99. " @ get the argv address\n"
  100. " add r2, sp, #4\n"
  101. " @ store the new argc in the new stack location\n"
  102. " str r1, [sp]\n"
  103. " @ compute envp\n"
  104. " add r3, r2, r1, lsl #2\n"
  105. " add r3, r3, #4\n"
  106. "\n\n"
  107. " @ load the finalizer function\n"
  108. " ldr r0, .L_FINI_PROC\n"
  109. " ldr r0, [sl, r0]\n"
  110. " @ jump to the user_s entry point\n"
  111. " " __stringify(BX(r6)) "\n"
  112. ".L_GET_GOT:\n"
  113. " .word _GLOBAL_OFFSET_TABLE_ - .L_GOT_GOT - 4\n"
  114. ".L_SKIP_ARGS:\n"
  115. " .word _dl_skip_args(GOTOFF)\n"
  116. ".L_FINI_PROC:\n"
  117. " .word _dl_fini(GOT)\n"
  118. "\n\n"
  119. " .size _start,.-_start\n"
  120. ".previous\n"
  121. );
  122. #else
  123. __asm__(
  124. " .text\n"
  125. " .arm\n"
  126. " .globl _start\n"
  127. " .type _start,%function\n"
  128. "_start:\n"
  129. " @ dumb: can't persuade the linker to make the start address\n"
  130. " @ odd, so use an arm function and change to thumb (_dl_start\n"
  131. " @ is thumb)\n"
  132. " adr r0, __dl_thumb_start+1\n"
  133. " bx r0\n"
  134. "\n\n"
  135. " .thumb\n"
  136. " .globl __dl_thumb_start\n"
  137. " .thumb_func\n"
  138. " .type __dl_thumb_start,%function\n"
  139. "__dl_thumb_start:\n"
  140. " @ at start time, all the args are on the stack\n"
  141. " mov r0, sp\n"
  142. " bl _dl_start\n"
  143. " @ returns user entry point in r0\n"
  144. " mov r6, r0\n"
  145. " @ we are PIC code, so get global offset table\n"
  146. " ldr r7, .L_GET_GOT\n"
  147. ".L_GOT_GOT:\n"
  148. " add r7, pc\n"
  149. " @ See if we were run as a command with the executable file\n"
  150. " @ name as an extra leading argument.\n"
  151. " ldr r4, .L_SKIP_ARGS\n"
  152. " ldr r4, [r7, r4]\n"
  153. " @ get the original arg count\n"
  154. " ldr r1, [sp]\n"
  155. " @ subtract _dl_skip_args from it\n"
  156. " sub r1, r1, r4\n"
  157. " @ adjust the stack pointer to skip them\n"
  158. " lsl r4, r4, #2\n"
  159. " add sp, r4\n"
  160. " @ get the argv address\n"
  161. " add r2, sp, #4\n"
  162. " @ store the new argc in the new stack location\n"
  163. " str r1, [sp]\n"
  164. " @ compute envp\n"
  165. " lsl r3, r1, #2\n"
  166. " add r3, r3, r2\n"
  167. " add r3, #4\n"
  168. "\n\n"
  169. " @ load the finalizer function\n"
  170. " ldr r0, .L_FINI_PROC\n"
  171. " ldr r0, [r7, r0]\n"
  172. " @ jump to the user_s entry point\n"
  173. " " __stringify(BX(r6)) "\n"
  174. "\n\n"
  175. ".L_GET_GOT:\n"
  176. " .word _GLOBAL_OFFSET_TABLE_ - .L_GOT_GOT - 4\n"
  177. ".L_SKIP_ARGS:\n"
  178. " .word _dl_skip_args(GOTOFF)\n"
  179. ".L_FINI_PROC:\n"
  180. " .word _dl_fini(GOT)\n"
  181. "\n\n"
  182. " .size _start,.-_start\n"
  183. ".previous\n"
  184. );
  185. #endif
  186. #endif /* defined(__FDPIC__) */
  187. /* Get a pointer to the argv array. On many platforms this can be just
  188. * the address of the first argument, on other platforms we need to
  189. * do something a little more subtle here. */
  190. #define GET_ARGV(ARGVP, ARGS) ARGVP = (((unsigned long*)ARGS)+1)
  191. /* Handle relocation of the symbols in the dynamic loader. */
  192. static __always_inline
  193. void PERFORM_BOOTSTRAP_RELOC(ELF_RELOC *rpnt, unsigned long *reloc_addr,
  194. unsigned long symbol_addr, DL_LOADADDR_TYPE load_addr, Elf32_Sym *symtab)
  195. {
  196. switch (ELF_R_TYPE(rpnt->r_info)) {
  197. case R_ARM_NONE:
  198. break;
  199. case R_ARM_ABS32:
  200. *reloc_addr += symbol_addr;
  201. break;
  202. case R_ARM_PC24:
  203. {
  204. unsigned long addend;
  205. long newvalue, topbits;
  206. addend = *reloc_addr & 0x00ffffff;
  207. if (addend & 0x00800000) addend |= 0xff000000;
  208. newvalue = symbol_addr - (unsigned long)reloc_addr + (addend << 2);
  209. topbits = newvalue & 0xfe000000;
  210. if (topbits != 0xfe000000 && topbits != 0x00000000)
  211. {
  212. #if 0
  213. /* Don't bother with this during ldso initilization... */
  214. newvalue = fix_bad_pc24(reloc_addr, symbol_addr)
  215. - (unsigned long)reloc_addr + (addend << 2);
  216. topbits = newvalue & 0xfe000000;
  217. if (unlikely(topbits != 0xfe000000 && topbits != 0x00000000))
  218. {
  219. SEND_STDERR("R_ARM_PC24 relocation out of range\n");
  220. _dl_exit(1);
  221. }
  222. #else
  223. SEND_STDERR("R_ARM_PC24 relocation out of range\n");
  224. _dl_exit(1);
  225. #endif
  226. }
  227. newvalue >>= 2;
  228. symbol_addr = (*reloc_addr & 0xff000000) | (newvalue & 0x00ffffff);
  229. *reloc_addr = symbol_addr;
  230. break;
  231. }
  232. case R_ARM_GLOB_DAT:
  233. case R_ARM_JUMP_SLOT:
  234. *reloc_addr = symbol_addr;
  235. break;
  236. case R_ARM_RELATIVE:
  237. *reloc_addr = DL_RELOC_ADDR(load_addr, *reloc_addr);
  238. break;
  239. case R_ARM_COPY:
  240. break;
  241. #ifdef __FDPIC__
  242. case R_ARM_FUNCDESC_VALUE:
  243. {
  244. struct funcdesc_value *dst = (struct funcdesc_value *) reloc_addr;
  245. dst->entry_point += symbol_addr;
  246. dst->got_value = load_addr.got_value;
  247. }
  248. break;
  249. #endif
  250. default:
  251. SEND_STDERR("Unsupported relocation type\n");
  252. _dl_exit(1);
  253. }
  254. }
  255. #ifdef __FDPIC__
  256. #undef DL_START
  257. #define DL_START(X) \
  258. static void __attribute__ ((used)) \
  259. _dl_start (Elf32_Addr dl_boot_got_pointer, \
  260. struct elf32_fdpic_loadmap *dl_boot_progmap, \
  261. struct elf32_fdpic_loadmap *dl_boot_ldsomap, \
  262. Elf32_Dyn *dl_boot_ldso_dyn_pointer, \
  263. struct funcdesc_value *dl_main_funcdesc, \
  264. X)
  265. /*
  266. * Transfer control to the user's application, once the dynamic loader
  267. * is done. We return the address of the function's entry point to
  268. * _dl_boot, see boot1_arch.h.
  269. */
  270. #define START() do { \
  271. struct elf_resolve *exec_mod = _dl_loaded_modules; \
  272. dl_main_funcdesc->entry_point = _dl_elf_main; \
  273. while (exec_mod->libtype != elf_executable) \
  274. exec_mod = exec_mod->next; \
  275. dl_main_funcdesc->got_value = exec_mod->loadaddr.got_value; \
  276. return; \
  277. } while (0)
  278. /* We use __aeabi_idiv0 in _dl_find_hash, so we need to have the raise
  279. symbol. */
  280. int raise(int sig)
  281. {
  282. _dl_exit(1);
  283. }
  284. #endif /* __FDPIC__ */