elfinterp.c 13 KB

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