dl-inlines.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /* Copyright (C) 2003, 2004 Red Hat, Inc.
  2. * Contributed by Alexandre Oliva <aoliva@redhat.com>
  3. * Copyright (C) 2006-2011 Analog Devices, Inc.
  4. *
  5. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  6. */
  7. #include <inline-hashtab.h>
  8. /* Initialize a DL_LOADADDR_TYPE given a got pointer and a complete load map. */
  9. static __always_inline void
  10. __dl_init_loadaddr_map(struct elf32_fdpic_loadaddr *loadaddr, Elf32_Addr dl_boot_got_pointer,
  11. struct elf32_fdpic_loadmap *map)
  12. {
  13. if (map->version != 0) {
  14. SEND_EARLY_STDERR("Invalid loadmap version number\n");
  15. _dl_exit(-1);
  16. }
  17. if (map->nsegs == 0) {
  18. SEND_EARLY_STDERR("Invalid segment count in loadmap\n");
  19. _dl_exit(-1);
  20. }
  21. loadaddr->got_value = (void *)dl_boot_got_pointer;
  22. loadaddr->map = map;
  23. }
  24. /*
  25. * Figure out how many LOAD segments there are in the given headers,
  26. * and allocate a block for the load map big enough for them.
  27. * got_value will be properly initialized later on, with INIT_GOT.
  28. */
  29. static __always_inline int
  30. __dl_init_loadaddr(struct elf32_fdpic_loadaddr *loadaddr, Elf32_Phdr *ppnt,
  31. int pcnt)
  32. {
  33. int count = 0, i;
  34. size_t size;
  35. for (i = 0; i < pcnt; i++)
  36. if (ppnt[i].p_type == PT_LOAD)
  37. count++;
  38. loadaddr->got_value = 0;
  39. size = sizeof(struct elf32_fdpic_loadmap) +
  40. (sizeof(struct elf32_fdpic_loadseg) * count);
  41. loadaddr->map = _dl_malloc(size);
  42. if (!loadaddr->map)
  43. _dl_exit(-1);
  44. loadaddr->map->version = 0;
  45. loadaddr->map->nsegs = 0;
  46. return count;
  47. }
  48. /* Incrementally initialize a load map. */
  49. static __always_inline void
  50. __dl_init_loadaddr_hdr(struct elf32_fdpic_loadaddr loadaddr, void *addr,
  51. Elf32_Phdr *phdr, int maxsegs)
  52. {
  53. struct elf32_fdpic_loadseg *segdata;
  54. if (loadaddr.map->nsegs == maxsegs)
  55. _dl_exit(-1);
  56. segdata = &loadaddr.map->segs[loadaddr.map->nsegs++];
  57. segdata->addr = (Elf32_Addr)addr;
  58. segdata->p_vaddr = phdr->p_vaddr;
  59. segdata->p_memsz = phdr->p_memsz;
  60. #if defined(__SUPPORT_LD_DEBUG__)
  61. if (_dl_debug)
  62. _dl_dprintf(_dl_debug_file, "%i: mapped %x at %x, size %x\n",
  63. loadaddr.map->nsegs - 1,
  64. segdata->p_vaddr, segdata->addr, segdata->p_memsz);
  65. #endif
  66. }
  67. /* Replace an existing entry in the load map. */
  68. static __always_inline void
  69. __dl_update_loadaddr_hdr(struct elf32_fdpic_loadaddr loadaddr, void *addr,
  70. Elf32_Phdr *phdr)
  71. {
  72. struct elf32_fdpic_loadseg *segdata;
  73. void *oldaddr;
  74. int i;
  75. for (i = 0; i < loadaddr.map->nsegs; i++)
  76. if (loadaddr.map->segs[i].p_vaddr == phdr->p_vaddr &&
  77. loadaddr.map->segs[i].p_memsz == phdr->p_memsz)
  78. break;
  79. if (i == loadaddr.map->nsegs)
  80. _dl_exit(-1);
  81. segdata = loadaddr.map->segs + i;
  82. oldaddr = (void *)segdata->addr;
  83. _dl_munmap(oldaddr, segdata->p_memsz);
  84. segdata->addr = (Elf32_Addr)addr;
  85. #if defined (__SUPPORT_LD_DEBUG__)
  86. if (_dl_debug)
  87. _dl_dprintf(_dl_debug_file, "%i: changed mapping %x at %x (old %x), size %x\n",
  88. loadaddr.map->nsegs - 1,
  89. segdata->p_vaddr, segdata->addr, oldaddr, segdata->p_memsz);
  90. #endif
  91. }
  92. #ifndef __dl_loadaddr_unmap
  93. static __always_inline void
  94. __dl_loadaddr_unmap(struct elf32_fdpic_loadaddr loadaddr,
  95. struct funcdesc_ht *funcdesc_ht)
  96. {
  97. int i;
  98. for (i = 0; i < loadaddr.map->nsegs; i++)
  99. _dl_munmap((void *)loadaddr.map->segs[i].addr,
  100. loadaddr.map->segs[i].p_memsz);
  101. /*
  102. * _dl_unmap is only called for dlopen()ed libraries, for which
  103. * calling free() is safe, or before we've completed the initial
  104. * relocation, in which case calling free() is probably pointless,
  105. * but still safe.
  106. */
  107. _dl_free(loadaddr.map);
  108. if (funcdesc_ht)
  109. htab_delete(funcdesc_ht);
  110. }
  111. #endif
  112. /* Figure out whether the given address is in one of the mapped segments. */
  113. static __always_inline int
  114. __dl_addr_in_loadaddr(void *p, struct elf32_fdpic_loadaddr loadaddr)
  115. {
  116. struct elf32_fdpic_loadmap *map = loadaddr.map;
  117. int c;
  118. for (c = 0; c < map->nsegs; c++)
  119. if ((void *)map->segs[c].addr <= p &&
  120. (char *)p < (char *)map->segs[c].addr + map->segs[c].p_memsz)
  121. return 1;
  122. return 0;
  123. }
  124. static int
  125. hash_pointer(void *p)
  126. {
  127. return (int) ((long)p >> 3);
  128. }
  129. static int
  130. eq_pointer(void *p, void *q)
  131. {
  132. struct funcdesc_value *entry = p;
  133. return entry->entry_point == q;
  134. }
  135. static __always_inline void *
  136. _dl_funcdesc_for (void *entry_point, void *got_value)
  137. {
  138. struct elf_resolve *tpnt = ((void**)got_value)[2];
  139. struct funcdesc_ht *ht = tpnt->funcdesc_ht;
  140. struct funcdesc_value **entry;
  141. _dl_assert(got_value == tpnt->loadaddr.got_value);
  142. if (!ht) {
  143. ht = htab_create();
  144. if (!ht)
  145. return (void*)-1;
  146. tpnt->funcdesc_ht = ht;
  147. }
  148. entry = htab_find_slot(ht, entry_point, 1, hash_pointer, eq_pointer);
  149. if (entry == NULL)
  150. _dl_exit(1);
  151. if (*entry) {
  152. _dl_assert((*entry)->entry_point == entry_point);
  153. return _dl_stabilize_funcdesc(*entry);
  154. }
  155. *entry = _dl_malloc(sizeof(**entry));
  156. (*entry)->entry_point = entry_point;
  157. (*entry)->got_value = got_value;
  158. return _dl_stabilize_funcdesc(*entry);
  159. }
  160. static __always_inline void const *
  161. _dl_lookup_address(void const *address)
  162. {
  163. struct elf_resolve *rpnt;
  164. struct funcdesc_value const *fd;
  165. /* Make sure we don't make assumptions about its alignment. */
  166. __asm__ ("" : "+r" (address));
  167. if ((Elf32_Addr)address & 7)
  168. /* It's not a function descriptor. */
  169. return address;
  170. fd = address;
  171. for (rpnt = _dl_loaded_modules; rpnt; rpnt = rpnt->next) {
  172. if (!rpnt->funcdesc_ht)
  173. continue;
  174. if (fd->got_value != rpnt->loadaddr.got_value)
  175. continue;
  176. address = htab_find_slot(rpnt->funcdesc_ht, (void *)fd->entry_point, 0,
  177. hash_pointer, eq_pointer);
  178. if (address && *(struct funcdesc_value *const*)address == fd) {
  179. address = (*(struct funcdesc_value *const*)address)->entry_point;
  180. break;
  181. } else
  182. address = fd;
  183. }
  184. return address;
  185. }