dl-inlines.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /* Copyright (C) 2003, 2004 Red Hat, Inc.
  2. * Contributed by Alexandre Oliva <aoliva@redhat.com>
  3. *
  4. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  5. */
  6. #ifndef _dl_assert
  7. # define _dl_assert(expr)
  8. #endif
  9. /* Initialize a DL_LOADADDR_TYPE given a got pointer and a complete
  10. load map. */
  11. static __always_inline void
  12. __dl_init_loadaddr_map (struct elf32_fdpic_loadaddr *loadaddr, void *got_value,
  13. struct elf32_fdpic_loadmap *map)
  14. {
  15. if (map->version != 0)
  16. {
  17. SEND_EARLY_STDERR ("Invalid loadmap version number\n");
  18. _dl_exit(-1);
  19. }
  20. if (map->nsegs == 0)
  21. {
  22. SEND_EARLY_STDERR ("Invalid segment count in loadmap\n");
  23. _dl_exit(-1);
  24. }
  25. loadaddr->got_value = got_value;
  26. loadaddr->map = map;
  27. }
  28. /* Figure out how many LOAD segments there are in the given headers,
  29. and allocate a block for the load map big enough for them.
  30. got_value will be properly initialized later on, with INIT_GOT. */
  31. static __always_inline int
  32. __dl_init_loadaddr (struct elf32_fdpic_loadaddr *loadaddr, Elf32_Phdr *ppnt,
  33. int pcnt)
  34. {
  35. int count = 0, i;
  36. size_t size;
  37. for (i = 0; i < pcnt; i++)
  38. if (ppnt[i].p_type == PT_LOAD)
  39. count++;
  40. loadaddr->got_value = 0;
  41. size = sizeof (struct elf32_fdpic_loadmap)
  42. + sizeof (struct elf32_fdpic_loadseg) * count;
  43. loadaddr->map = _dl_malloc (size);
  44. if (! loadaddr->map)
  45. _dl_exit (-1);
  46. loadaddr->map->version = 0;
  47. loadaddr->map->nsegs = 0;
  48. return count;
  49. }
  50. /* Incrementally initialize a load map. */
  51. static __always_inline void
  52. __dl_init_loadaddr_hdr (struct elf32_fdpic_loadaddr loadaddr, void *addr,
  53. Elf32_Phdr *phdr, int maxsegs)
  54. {
  55. struct elf32_fdpic_loadseg *segdata;
  56. if (loadaddr.map->nsegs == maxsegs)
  57. _dl_exit (-1);
  58. segdata = &loadaddr.map->segs[loadaddr.map->nsegs++];
  59. segdata->addr = (Elf32_Addr) addr;
  60. segdata->p_vaddr = phdr->p_vaddr;
  61. segdata->p_memsz = phdr->p_memsz;
  62. #if defined (__SUPPORT_LD_DEBUG__)
  63. if (_dl_debug)
  64. _dl_dprintf(_dl_debug_file, "%i: mapped %x at %x, size %x\n",
  65. loadaddr.map->nsegs-1,
  66. segdata->p_vaddr, segdata->addr, segdata->p_memsz);
  67. #endif
  68. }
  69. /* Replace an existing entry in the load map. */
  70. static __always_inline void
  71. __dl_update_loadaddr_hdr (struct elf32_fdpic_loadaddr loadaddr, void *addr,
  72. Elf32_Phdr *phdr)
  73. {
  74. struct elf32_fdpic_loadseg *segdata;
  75. void *oldaddr;
  76. int i;
  77. for (i = 0; i < loadaddr.map->nsegs; i++)
  78. if (loadaddr.map->segs[i].p_vaddr == phdr->p_vaddr
  79. && loadaddr.map->segs[i].p_memsz == phdr->p_memsz)
  80. break;
  81. if (i == loadaddr.map->nsegs)
  82. _dl_exit (-1);
  83. segdata = loadaddr.map->segs + i;
  84. oldaddr = (void *)segdata->addr;
  85. _dl_munmap (oldaddr, segdata->p_memsz);
  86. segdata->addr = (Elf32_Addr) addr;
  87. #if defined (__SUPPORT_LD_DEBUG__)
  88. if (_dl_debug)
  89. _dl_dprintf(_dl_debug_file, "%i: changed mapping %x at %x (old %x), size %x\n",
  90. loadaddr.map->nsegs-1,
  91. segdata->p_vaddr, segdata->addr, oldaddr, segdata->p_memsz);
  92. #endif
  93. }
  94. static __always_inline void __dl_loadaddr_unmap
  95. (struct elf32_fdpic_loadaddr loadaddr, struct funcdesc_ht *funcdesc_ht);
  96. /* Figure out whether the given address is in one of the mapped
  97. segments. */
  98. static __always_inline int
  99. __dl_addr_in_loadaddr (void *p, struct elf32_fdpic_loadaddr loadaddr)
  100. {
  101. struct elf32_fdpic_loadmap *map = loadaddr.map;
  102. int c;
  103. for (c = 0; c < map->nsegs; c++)
  104. if ((void*)map->segs[c].addr <= p
  105. && (char*)p < (char*)map->segs[c].addr + map->segs[c].p_memsz)
  106. return 1;
  107. return 0;
  108. }
  109. static __always_inline void * _dl_funcdesc_for (void *entry_point, void *got_value);
  110. /* The hashcode handling code below is heavily inspired in libiberty's
  111. hashtab code, but with most adaptation points and support for
  112. deleting elements removed.
  113. Copyright (C) 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
  114. Contributed by Vladimir Makarov (vmakarov@cygnus.com). */
  115. static __always_inline unsigned long
  116. higher_prime_number (unsigned long n)
  117. {
  118. /* These are primes that are near, but slightly smaller than, a
  119. power of two. */
  120. static const unsigned long primes[] = {
  121. (unsigned long) 7,
  122. (unsigned long) 13,
  123. (unsigned long) 31,
  124. (unsigned long) 61,
  125. (unsigned long) 127,
  126. (unsigned long) 251,
  127. (unsigned long) 509,
  128. (unsigned long) 1021,
  129. (unsigned long) 2039,
  130. (unsigned long) 4093,
  131. (unsigned long) 8191,
  132. (unsigned long) 16381,
  133. (unsigned long) 32749,
  134. (unsigned long) 65521,
  135. (unsigned long) 131071,
  136. (unsigned long) 262139,
  137. (unsigned long) 524287,
  138. (unsigned long) 1048573,
  139. (unsigned long) 2097143,
  140. (unsigned long) 4194301,
  141. (unsigned long) 8388593,
  142. (unsigned long) 16777213,
  143. (unsigned long) 33554393,
  144. (unsigned long) 67108859,
  145. (unsigned long) 134217689,
  146. (unsigned long) 268435399,
  147. (unsigned long) 536870909,
  148. (unsigned long) 1073741789,
  149. (unsigned long) 2147483647,
  150. /* 4294967291L */
  151. ((unsigned long) 2147483647) + ((unsigned long) 2147483644),
  152. };
  153. const unsigned long *low = &primes[0];
  154. const unsigned long *high = &primes[sizeof(primes) / sizeof(primes[0])];
  155. while (low != high)
  156. {
  157. const unsigned long *mid = low + (high - low) / 2;
  158. if (n > *mid)
  159. low = mid + 1;
  160. else
  161. high = mid;
  162. }
  163. #if 0
  164. /* If we've run out of primes, abort. */
  165. if (n > *low)
  166. {
  167. fprintf (stderr, "Cannot find prime bigger than %lu\n", n);
  168. abort ();
  169. }
  170. #endif
  171. return *low;
  172. }
  173. struct funcdesc_ht
  174. {
  175. /* Table itself. */
  176. struct funcdesc_value **entries;
  177. /* Current size (in entries) of the hash table */
  178. size_t size;
  179. /* Current number of elements. */
  180. size_t n_elements;
  181. };
  182. static __always_inline int
  183. hash_pointer (const void *p)
  184. {
  185. return (int) ((long)p >> 3);
  186. }
  187. static __always_inline struct funcdesc_ht *
  188. htab_create (void)
  189. {
  190. struct funcdesc_ht *ht = _dl_malloc (sizeof (struct funcdesc_ht));
  191. if (! ht)
  192. return NULL;
  193. ht->size = 3;
  194. ht->entries = _dl_malloc (sizeof (struct funcdesc_ht_value *) * ht->size);
  195. if (! ht->entries)
  196. return NULL;
  197. ht->n_elements = 0;
  198. _dl_memset (ht->entries, 0, sizeof (struct funcdesc_ht_value *) * ht->size);
  199. return ht;
  200. }
  201. /* This is only called from _dl_loadaddr_unmap, so it's safe to call
  202. _dl_free(). See the discussion below. */
  203. static __always_inline void
  204. htab_delete (struct funcdesc_ht *htab)
  205. {
  206. int i;
  207. for (i = htab->size - 1; i >= 0; i--)
  208. if (htab->entries[i])
  209. _dl_free (htab->entries[i]);
  210. _dl_free (htab->entries);
  211. _dl_free (htab);
  212. }
  213. /* Similar to htab_find_slot, but without several unwanted side effects:
  214. - Does not call htab->eq_f when it finds an existing entry.
  215. - Does not change the count of elements/searches/collisions in the
  216. hash table.
  217. This function also assumes there are no deleted entries in the table.
  218. HASH is the hash value for the element to be inserted. */
  219. static __always_inline struct funcdesc_value **
  220. find_empty_slot_for_expand (struct funcdesc_ht *htab, int hash)
  221. {
  222. size_t size = htab->size;
  223. unsigned int index = hash % size;
  224. struct funcdesc_value **slot = htab->entries + index;
  225. int hash2;
  226. if (! *slot)
  227. return slot;
  228. hash2 = 1 + hash % (size - 2);
  229. for (;;)
  230. {
  231. index += hash2;
  232. if (index >= size)
  233. index -= size;
  234. slot = htab->entries + index;
  235. if (! *slot)
  236. return slot;
  237. }
  238. }
  239. /* The following function changes size of memory allocated for the
  240. entries and repeatedly inserts the table elements. The occupancy
  241. of the table after the call will be about 50%. Naturally the hash
  242. table must already exist. Remember also that the place of the
  243. table entries is changed. If memory allocation failures are allowed,
  244. this function will return zero, indicating that the table could not be
  245. expanded. If all goes well, it will return a non-zero value. */
  246. static __always_inline int
  247. htab_expand (struct funcdesc_ht *htab)
  248. {
  249. struct funcdesc_value **oentries;
  250. struct funcdesc_value **olimit;
  251. struct funcdesc_value **p;
  252. struct funcdesc_value **nentries;
  253. size_t nsize;
  254. oentries = htab->entries;
  255. olimit = oentries + htab->size;
  256. /* Resize only when table after removal of unused elements is either
  257. too full or too empty. */
  258. if (htab->n_elements * 2 > htab->size)
  259. nsize = higher_prime_number (htab->n_elements * 2);
  260. else
  261. nsize = htab->size;
  262. nentries = _dl_malloc (sizeof (struct funcdesc_value *) * nsize);
  263. _dl_memset (nentries, 0, sizeof (struct funcdesc_value *) * nsize);
  264. if (nentries == NULL)
  265. return 0;
  266. htab->entries = nentries;
  267. htab->size = nsize;
  268. p = oentries;
  269. do
  270. {
  271. if (*p)
  272. *find_empty_slot_for_expand (htab, hash_pointer ((*p)->entry_point))
  273. = *p;
  274. p++;
  275. }
  276. while (p < olimit);
  277. #if 0 /* We can't tell whether this was allocated by the _dl_malloc()
  278. built into ld.so or malloc() in the main executable or libc,
  279. and calling free() for something that wasn't malloc()ed could
  280. do Very Bad Things (TM). Take the conservative approach
  281. here, potentially wasting as much memory as actually used by
  282. the hash table, even if multiple growths occur. That's not
  283. so bad as to require some overengineered solution that would
  284. enable us to keep track of how it was allocated. */
  285. _dl_free (oentries);
  286. #endif
  287. return 1;
  288. }
  289. /* This function searches for a hash table slot containing an entry
  290. equal to the given element. To delete an entry, call this with
  291. INSERT = 0, then call htab_clear_slot on the slot returned (possibly
  292. after doing some checks). To insert an entry, call this with
  293. INSERT = 1, then write the value you want into the returned slot.
  294. When inserting an entry, NULL may be returned if memory allocation
  295. fails. */
  296. static __always_inline struct funcdesc_value **
  297. htab_find_slot (struct funcdesc_ht *htab, void *ptr, int insert)
  298. {
  299. unsigned int index;
  300. int hash, hash2;
  301. size_t size;
  302. struct funcdesc_value **entry;
  303. if (htab->size * 3 <= htab->n_elements * 4
  304. && htab_expand (htab) == 0)
  305. return NULL;
  306. hash = hash_pointer (ptr);
  307. size = htab->size;
  308. index = hash % size;
  309. entry = &htab->entries[index];
  310. if (!*entry)
  311. goto empty_entry;
  312. else if ((*entry)->entry_point == ptr)
  313. return entry;
  314. hash2 = 1 + hash % (size - 2);
  315. for (;;)
  316. {
  317. index += hash2;
  318. if (index >= size)
  319. index -= size;
  320. entry = &htab->entries[index];
  321. if (!*entry)
  322. goto empty_entry;
  323. else if ((*entry)->entry_point == ptr)
  324. return entry;
  325. }
  326. empty_entry:
  327. if (!insert)
  328. return NULL;
  329. htab->n_elements++;
  330. return entry;
  331. }
  332. void *
  333. _dl_funcdesc_for (void *entry_point, void *got_value)
  334. {
  335. struct elf_resolve *tpnt = ((void**)got_value)[2];
  336. struct funcdesc_ht *ht = tpnt->funcdesc_ht;
  337. struct funcdesc_value **entry;
  338. _dl_assert (got_value == tpnt->loadaddr.got_value);
  339. if (! ht)
  340. {
  341. ht = htab_create ();
  342. if (! ht)
  343. return (void*)-1;
  344. tpnt->funcdesc_ht = ht;
  345. }
  346. entry = htab_find_slot (ht, entry_point, 1);
  347. if (*entry)
  348. {
  349. _dl_assert ((*entry)->entry_point == entry_point);
  350. return _dl_stabilize_funcdesc (*entry);
  351. }
  352. *entry = _dl_malloc (sizeof (struct funcdesc_value));
  353. (*entry)->entry_point = entry_point;
  354. (*entry)->got_value = got_value;
  355. return _dl_stabilize_funcdesc (*entry);
  356. }
  357. static __always_inline void const *
  358. _dl_lookup_address (void const *address)
  359. {
  360. struct elf_resolve *rpnt;
  361. struct funcdesc_value const *fd;
  362. /* Make sure we don't make assumptions about its alignment. */
  363. __asm__ ("" : "+r" (address));
  364. if ((Elf32_Addr)address & 7)
  365. /* It's not a function descriptor. */
  366. return address;
  367. fd = (struct funcdesc_value const *)address;
  368. for (rpnt = _dl_loaded_modules; rpnt; rpnt = rpnt->next)
  369. {
  370. if (! rpnt->funcdesc_ht)
  371. continue;
  372. if (fd->got_value != rpnt->loadaddr.got_value)
  373. continue;
  374. address = htab_find_slot (rpnt->funcdesc_ht, (void*)fd->entry_point, 0);
  375. if (address && *(struct funcdesc_value *const*)address == fd)
  376. {
  377. address = (*(struct funcdesc_value *const*)address)->entry_point;
  378. break;
  379. }
  380. else
  381. address = fd;
  382. }
  383. return address;
  384. }
  385. void
  386. __dl_loadaddr_unmap (struct elf32_fdpic_loadaddr loadaddr,
  387. struct funcdesc_ht *funcdesc_ht)
  388. {
  389. int i;
  390. for (i = 0; i < loadaddr.map->nsegs; i++)
  391. _dl_munmap ((void*)loadaddr.map->segs[i].addr,
  392. loadaddr.map->segs[i].p_memsz);
  393. /* _dl_unmap is only called for dlopen()ed libraries, for which
  394. calling free() is safe, or before we've completed the initial
  395. relocation, in which case calling free() is probably pointless,
  396. but still safe. */
  397. _dl_free (loadaddr.map);
  398. if (funcdesc_ht)
  399. htab_delete (funcdesc_ht);
  400. }