dl-inlines.h 13 KB

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