elfinterp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /* vi: set sw=4 ts=4: */
  2. /* powerpc shared library loader suppport
  3. *
  4. * Copyright (C) 2001-2002 David A. Schleef
  5. * Copyright (C) 2003-2004 Erik Andersen
  6. * Copyright (C) 2004 Joakim Tjernlund
  7. *
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. The name of the above contributors may not be
  16. * used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  25. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. * SUCH DAMAGE.
  30. */
  31. #include "ldso.h"
  32. extern int _dl_linux_resolve(void);
  33. void _dl_init_got(unsigned long *plt,struct elf_resolve *tpnt)
  34. {
  35. Elf32_Word *tramp;
  36. Elf32_Word num_plt_entries;
  37. Elf32_Word data_words;
  38. Elf32_Word rel_offset_words;
  39. Elf32_Word dlrr = (Elf32_Word) _dl_linux_resolve;
  40. if (tpnt->dynamic_info[DT_JMPREL] == 0)
  41. return;
  42. if (tpnt->dynamic_info[DT_PPC_GOT_IDX] != 0) {
  43. tpnt->dynamic_info[DT_PPC_GOT_IDX] += tpnt->loadaddr;
  44. return;
  45. }
  46. num_plt_entries = tpnt->dynamic_info[DT_PLTRELSZ] / sizeof(ELF_RELOC);
  47. rel_offset_words = PLT_DATA_START_WORDS(num_plt_entries);
  48. data_words = (Elf32_Word) (plt + rel_offset_words);
  49. tpnt->data_words = data_words;
  50. plt[PLT_LONGBRANCH_ENTRY_WORDS] = OPCODE_ADDIS_HI(11, 11, data_words);
  51. plt[PLT_LONGBRANCH_ENTRY_WORDS+1] = OPCODE_LWZ(11,data_words,11);
  52. plt[PLT_LONGBRANCH_ENTRY_WORDS+2] = OPCODE_MTCTR(11);
  53. plt[PLT_LONGBRANCH_ENTRY_WORDS+3] = OPCODE_BCTR();
  54. /* [4] */
  55. /* [5] */
  56. tramp = (Elf32_Word *) (plt + PLT_TRAMPOLINE_ENTRY_WORDS);
  57. /* For the long entries, subtract off data_words. */
  58. tramp[0] = OPCODE_ADDIS_HI(11,11,-data_words);
  59. tramp[1] = OPCODE_ADDI(11,11,-data_words);
  60. /* Multiply index of entry by 3 (in r11). */
  61. tramp[2] = OPCODE_SLWI(12,11,1);
  62. tramp[3] = OPCODE_ADD(11,12,11);
  63. if (dlrr <= 0x01fffffc || dlrr >= 0xfe000000) {
  64. /* Load address of link map in r12. */
  65. tramp[4] = OPCODE_LI (12, (Elf32_Word) tpnt);
  66. tramp[5] = OPCODE_ADDIS_HI (12, 12, (Elf32_Word) tpnt);
  67. /* Call _dl_linux_resolve . */
  68. tramp[6] = OPCODE_BA (dlrr);
  69. } else {
  70. /* Get address of _dl_linux_resolve in CTR. */
  71. tramp[4] = OPCODE_LI(12,dlrr);
  72. tramp[5] = OPCODE_ADDIS_HI(12,12,dlrr);
  73. tramp[6] = OPCODE_MTCTR(12);
  74. /* Load address of link map in r12. */
  75. tramp[7] = OPCODE_LI(12,(Elf32_Word) tpnt);
  76. tramp[8] = OPCODE_ADDIS_HI(12,12,(Elf32_Word) tpnt);
  77. /* Call _dl_linux_resolve. */
  78. tramp[9] = OPCODE_BCTR();
  79. }
  80. /* [16] unused */
  81. /* [17] unused */
  82. PPC_DCBST(plt);
  83. PPC_DCBST(plt+4);
  84. PPC_DCBST(plt+8);
  85. PPC_DCBST(plt+12);
  86. PPC_DCBST(plt+16-1);
  87. PPC_SYNC;
  88. PPC_ICBI(plt);
  89. PPC_ICBI(plt+16-1);
  90. PPC_ISYNC;
  91. }
  92. unsigned long _dl_linux_resolver(struct elf_resolve *tpnt, int reloc_entry)
  93. {
  94. ELF_RELOC *this_reloc;
  95. char *strtab;
  96. Elf32_Sym *symtab;
  97. ELF_RELOC *rel_addr;
  98. int symtab_index;
  99. char *symname;
  100. Elf32_Addr *reloc_addr;
  101. Elf32_Addr finaladdr;
  102. Elf32_Sword delta;
  103. rel_addr = (ELF_RELOC *)tpnt->dynamic_info[DT_JMPREL];
  104. this_reloc = (void *)rel_addr + reloc_entry;
  105. symtab_index = ELF32_R_SYM(this_reloc->r_info);
  106. symtab = (Elf32_Sym *)tpnt->dynamic_info[DT_SYMTAB];
  107. strtab = (char *)tpnt->dynamic_info[DT_STRTAB];
  108. symname = strtab + symtab[symtab_index].st_name;
  109. debug_sym(symtab,strtab,symtab_index);
  110. debug_reloc(symtab,strtab,this_reloc);
  111. #if defined (__SUPPORT_LD_DEBUG__)
  112. if (unlikely(ELF32_R_TYPE(this_reloc->r_info) != R_PPC_JMP_SLOT)) {
  113. _dl_dprintf(2, "%s: Incorrect relocation type in jump relocation\n", _dl_progname);
  114. _dl_exit(1);
  115. }
  116. #endif
  117. /* Address of dump instruction to fix up */
  118. reloc_addr = (Elf32_Addr *) (tpnt->loadaddr + this_reloc->r_offset);
  119. #if defined (__SUPPORT_LD_DEBUG__)
  120. if (_dl_debug_reloc && _dl_debug_detail)
  121. _dl_dprintf(_dl_debug_file, "\n\tResolving symbol %s %x --> ", symname, (Elf32_Addr)reloc_addr);
  122. #endif
  123. /* Get the address of the GOT entry */
  124. finaladdr = (Elf32_Addr) _dl_find_hash(symname,
  125. tpnt->symbol_scope, tpnt, ELF_RTYPE_CLASS_PLT);
  126. if (unlikely(!finaladdr)) {
  127. _dl_dprintf(2, "%s: can't resolve symbol '%s' in lib '%s'.\n", _dl_progname, symname, tpnt->libname);
  128. _dl_exit(1);
  129. }
  130. finaladdr += this_reloc->r_addend;
  131. #if defined (__SUPPORT_LD_DEBUG__)
  132. if (_dl_debug_reloc && _dl_debug_detail)
  133. _dl_dprintf(_dl_debug_file, "%x\n", finaladdr);
  134. #endif
  135. if (tpnt->dynamic_info[DT_PPC_GOT_IDX] != 0) {
  136. *reloc_addr = finaladdr;
  137. } else {
  138. delta = finaladdr - (Elf32_Word)reloc_addr;
  139. if (delta<<6>>6 == delta) {
  140. *reloc_addr = OPCODE_B(delta);
  141. } else if (finaladdr <= 0x01fffffc) {
  142. *reloc_addr = OPCODE_BA (finaladdr);
  143. } else {
  144. /* Warning: we don't handle double-sized PLT entries */
  145. Elf32_Word *plt, *data_words, index, offset;
  146. plt = (Elf32_Word *)tpnt->dynamic_info[DT_PLTGOT];
  147. offset = reloc_addr - plt;
  148. index = (offset - PLT_INITIAL_ENTRY_WORDS)/2;
  149. data_words = (Elf32_Word *)tpnt->data_words;
  150. reloc_addr += 1;
  151. data_words[index] = finaladdr;
  152. PPC_SYNC;
  153. *reloc_addr = OPCODE_B ((PLT_LONGBRANCH_ENTRY_WORDS - (offset+1)) * 4);
  154. }
  155. /* instructions were modified */
  156. PPC_DCBST(reloc_addr);
  157. PPC_SYNC;
  158. PPC_ICBI(reloc_addr);
  159. PPC_ISYNC;
  160. }
  161. return finaladdr;
  162. }
  163. static __inline__ int
  164. _dl_do_reloc (struct elf_resolve *tpnt,struct dyn_elf *scope,
  165. ELF_RELOC *rpnt, Elf32_Sym *symtab, char *strtab)
  166. {
  167. int reloc_type;
  168. int symtab_index;
  169. char *symname;
  170. Elf32_Addr *reloc_addr;
  171. Elf32_Addr finaladdr;
  172. unsigned long symbol_addr;
  173. #if defined (__SUPPORT_LD_DEBUG__)
  174. unsigned long old_val;
  175. #endif
  176. reloc_addr = (Elf32_Addr *)(intptr_t) (tpnt->loadaddr + (unsigned long) rpnt->r_offset);
  177. reloc_type = ELF32_R_TYPE(rpnt->r_info);
  178. symbol_addr = tpnt->loadaddr; /* For R_PPC_RELATIVE */
  179. symtab_index = ELF32_R_SYM(rpnt->r_info);
  180. symname = strtab + symtab[symtab_index].st_name;
  181. if (symtab_index) {
  182. symbol_addr = (unsigned long) _dl_find_hash(symname, scope, tpnt,
  183. elf_machine_type_class(reloc_type));
  184. /* We want to allow undefined references to weak symbols - this might
  185. * have been intentional. We should not be linking local symbols
  186. * here, so all bases should be covered.
  187. */
  188. if (unlikely(!symbol_addr && ELF32_ST_BIND(symtab[symtab_index].st_info) != STB_WEAK))
  189. return 1;
  190. }
  191. #if defined (__SUPPORT_LD_DEBUG__)
  192. old_val = *reloc_addr;
  193. #endif
  194. finaladdr = (Elf32_Addr) (symbol_addr + rpnt->r_addend);
  195. switch (reloc_type) {
  196. case R_PPC_RELATIVE:
  197. case R_PPC_ADDR32:
  198. case R_PPC_GLOB_DAT:
  199. *reloc_addr = finaladdr;
  200. goto out_nocode; /* No code code modified */
  201. case R_PPC_JMP_SLOT:
  202. {
  203. if (tpnt->dynamic_info[DT_PPC_GOT_IDX] != 0) {
  204. *reloc_addr = finaladdr;
  205. goto out_nocode; /* No code code modified */
  206. } else {
  207. Elf32_Sword delta = finaladdr - (Elf32_Word)reloc_addr;
  208. if (delta<<6>>6 == delta) {
  209. *reloc_addr = OPCODE_B(delta);
  210. } else if (finaladdr <= 0x01fffffc) {
  211. *reloc_addr = OPCODE_BA (finaladdr);
  212. } else {
  213. /* Warning: we don't handle double-sized PLT entries */
  214. Elf32_Word *plt, *data_words, index, offset;
  215. plt = (Elf32_Word *)tpnt->dynamic_info[DT_PLTGOT];
  216. offset = reloc_addr - plt;
  217. index = (offset - PLT_INITIAL_ENTRY_WORDS)/2;
  218. data_words = (Elf32_Word *)tpnt->data_words;
  219. data_words[index] = finaladdr;
  220. reloc_addr[0] = OPCODE_LI(11,index*4);
  221. reloc_addr[1] = OPCODE_B((PLT_LONGBRANCH_ENTRY_WORDS - (offset+1)) * 4);
  222. /* instructions were modified */
  223. PPC_DCBST(reloc_addr+1);
  224. PPC_SYNC;
  225. PPC_ICBI(reloc_addr+1);
  226. }
  227. }
  228. break;
  229. }
  230. case R_PPC_COPY:
  231. #if defined (__SUPPORT_LD_DEBUG__)
  232. if (_dl_debug_move)
  233. _dl_dprintf(_dl_debug_file,"\n%s move %x bytes from %x to %x",
  234. symname, symtab[symtab_index].st_size,
  235. symbol_addr, reloc_addr);
  236. #endif
  237. _dl_memcpy((char *) reloc_addr, (char *) finaladdr, symtab[symtab_index].st_size);
  238. goto out_nocode; /* No code code modified */
  239. case R_PPC_ADDR16_HA:
  240. finaladdr += 0x8000; /* fall through. */
  241. case R_PPC_ADDR16_HI:
  242. finaladdr >>= 16; /* fall through. */
  243. case R_PPC_ADDR16_LO:
  244. *(short *)reloc_addr = finaladdr;
  245. break;
  246. case R_PPC_REL24:
  247. #if 0
  248. {
  249. Elf32_Sword delta = finaladdr - (Elf32_Word)reloc_addr;
  250. if (unlikely(delta<<6>>6 != delta)) {
  251. _dl_dprintf(2, "%s: symbol '%s' R_PPC_REL24 is out of range.\n\t"
  252. "Compile shared libraries with -fPIC!\n",
  253. _dl_progname, symname);
  254. _dl_exit(1);
  255. }
  256. *reloc_addr = (*reloc_addr & 0xfc000003) | (delta & 0x3fffffc);
  257. break;
  258. }
  259. #else
  260. _dl_dprintf(2,"R_PPC_REL24: Compile shared libraries with -fPIC!\n");
  261. return -1;
  262. #endif
  263. case R_PPC_NONE:
  264. goto out_nocode; /* No code code modified */
  265. default:
  266. _dl_dprintf(2, "%s: can't handle reloc type ", _dl_progname);
  267. #if defined (__SUPPORT_LD_DEBUG__)
  268. _dl_dprintf(2, "%s ", _dl_reltypes(reloc_type));
  269. #endif
  270. if (symtab_index)
  271. _dl_dprintf(2, "'%s'\n", symname);
  272. return -1;
  273. }
  274. /* instructions were modified */
  275. PPC_DCBST(reloc_addr);
  276. PPC_SYNC;
  277. PPC_ICBI(reloc_addr);
  278. PPC_ISYNC;
  279. out_nocode:
  280. #if defined (__SUPPORT_LD_DEBUG__)
  281. if (_dl_debug_reloc && _dl_debug_detail)
  282. _dl_dprintf(_dl_debug_file, "\tpatched: %x ==> %x @ %x", old_val, *reloc_addr, reloc_addr);
  283. #endif
  284. return 0;
  285. }
  286. void _dl_parse_lazy_relocation_information(struct dyn_elf *rpnt,
  287. unsigned long rel_addr, unsigned long rel_size)
  288. {
  289. struct elf_resolve *tpnt = rpnt->dyn;
  290. Elf32_Word *plt, offset, i, num_plt_entries, rel_offset_words;
  291. num_plt_entries = rel_size / sizeof(ELF_RELOC);
  292. plt = (Elf32_Word *)tpnt->dynamic_info[DT_PLTGOT];
  293. if (tpnt->dynamic_info[DT_PPC_GOT_IDX] != 0) {
  294. /* Secure PLT */
  295. Elf32_Addr *got = (Elf32_Addr *)tpnt->dynamic_info[DT_PPC_GOT_IDX];
  296. Elf32_Word dlrr = (Elf32_Word) _dl_linux_resolve;
  297. got[1] = (Elf32_Addr) dlrr;
  298. got[2] = (Elf32_Addr) tpnt;
  299. /* Relocate everything in .plt by the load address offset. */
  300. while (num_plt_entries-- != 0)
  301. *plt++ += tpnt->loadaddr;
  302. return;
  303. }
  304. rel_offset_words = PLT_DATA_START_WORDS(num_plt_entries);
  305. /* Set up the lazy PLT entries. */
  306. offset = PLT_INITIAL_ENTRY_WORDS;
  307. i = 0;
  308. /* Warning: we don't handle double-sized PLT entries */
  309. while (i < num_plt_entries) {
  310. plt[offset ] = OPCODE_LI(11, i * 4);
  311. plt[offset+1] = OPCODE_B((PLT_TRAMPOLINE_ENTRY_WORDS + 2 - (offset+1)) * 4);
  312. i++;
  313. offset += 2;
  314. }
  315. /* Now, we've modified code. We need to write the changes from
  316. the data cache to a second-level unified cache, then make
  317. sure that stale data in the instruction cache is removed.
  318. (In a multiprocessor system, the effect is more complex.)
  319. Most of the PLT shouldn't be in the instruction cache, but
  320. there may be a little overlap at the start and the end.
  321. Assumes that dcbst and icbi apply to lines of 16 bytes or
  322. more. Current known line sizes are 16, 32, and 128 bytes. */
  323. for (i = 0; i < rel_offset_words; i += 4)
  324. PPC_DCBST (plt + i);
  325. PPC_DCBST (plt + rel_offset_words - 1);
  326. PPC_SYNC;
  327. PPC_ICBI (plt);
  328. PPC_ICBI (plt + rel_offset_words - 1);
  329. PPC_ISYNC;
  330. }
  331. static __inline__ int
  332. _dl_parse(struct elf_resolve *tpnt, struct dyn_elf *scope,
  333. unsigned long rel_addr, unsigned long rel_size,
  334. int (*reloc_fnc) (struct elf_resolve *tpnt, struct dyn_elf *scope,
  335. ELF_RELOC *rpnt, Elf32_Sym *symtab, char *strtab))
  336. {
  337. unsigned int i;
  338. char *strtab;
  339. Elf32_Sym *symtab;
  340. ELF_RELOC *rpnt;
  341. int symtab_index;
  342. /* Now parse the relocation information */
  343. rpnt = (ELF_RELOC *)(intptr_t)rel_addr;
  344. rel_size = rel_size / sizeof(ELF_RELOC);
  345. symtab = (Elf32_Sym *)(intptr_t)tpnt->dynamic_info[DT_SYMTAB];
  346. strtab = (char *)tpnt->dynamic_info[DT_STRTAB];
  347. for (i = 0; i < rel_size; i++, rpnt++) {
  348. int res;
  349. symtab_index = ELF32_R_SYM(rpnt->r_info);
  350. debug_sym(symtab,strtab,symtab_index);
  351. debug_reloc(symtab,strtab,rpnt);
  352. res = reloc_fnc (tpnt, scope, rpnt, symtab, strtab);
  353. if (res==0) continue;
  354. _dl_dprintf(2, "\n%s: ",_dl_progname);
  355. if (symtab_index)
  356. _dl_dprintf(2, "symbol '%s': ", strtab + symtab[symtab_index].st_name);
  357. if (unlikely(res <0))
  358. {
  359. int reloc_type = ELF32_R_TYPE(rpnt->r_info);
  360. #if defined (__SUPPORT_LD_DEBUG__)
  361. _dl_dprintf(2, "can't handle reloc type '%s' in lib '%s'\n", _dl_reltypes(reloc_type), tpnt->libname);
  362. #else
  363. _dl_dprintf(2, "can't handle reloc type %x in lib '%s'\n", reloc_type, tpnt->libname);
  364. #endif
  365. return res;
  366. }
  367. if (unlikely(res >0))
  368. {
  369. _dl_dprintf(2, "can't resolve symbol in lib '%s'.\n", tpnt->libname);
  370. return res;
  371. }
  372. }
  373. return 0;
  374. }
  375. int _dl_parse_relocation_information(struct dyn_elf *rpnt,
  376. unsigned long rel_addr, unsigned long rel_size)
  377. {
  378. return _dl_parse(rpnt->dyn, rpnt->dyn->symbol_scope, rel_addr, rel_size, _dl_do_reloc);
  379. }