xref: /haiku/src/system/runtime_loader/elf.cpp (revision 02e45e32ae6734100fa1d6f769148e0b551c034b)
1 /*
2  * Copyright 2008-2010, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Copyright 2003-2011, Axel Dörfler, axeld@pinc-software.de.
4  * Distributed under the terms of the MIT License.
5  *
6  * Copyright 2002, Manuel J. Petit. All rights reserved.
7  * Copyright 2001, Travis Geiselbrecht. All rights reserved.
8  * Distributed under the terms of the NewOS License.
9  */
10 
11 #include "runtime_loader_private.h"
12 
13 #include <ctype.h>
14 #include <dlfcn.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 
19 #include <OS.h>
20 
21 #include <syscalls.h>
22 #include <util/kernel_cpp.h>
23 
24 #include <locks.h>
25 
26 #include "add_ons.h"
27 #include "elf_load_image.h"
28 #include "elf_symbol_lookup.h"
29 #include "elf_tls.h"
30 #include "elf_versioning.h"
31 #include "errors.h"
32 #include "images.h"
33 
34 
35 // TODO: implement better locking strategy
36 // TODO: implement lazy binding
37 
38 // a handle returned by load_library() (dlopen())
39 #define RLD_GLOBAL_SCOPE	((void*)-2l)
40 
41 static const char* const kLockName = "runtime loader";
42 
43 
44 typedef void (*init_term_function)(image_id);
45 typedef void (*initfini_array_function)();
46 
47 bool gProgramLoaded = false;
48 image_t* gProgramImage;
49 
50 static image_t** sPreloadedAddons = NULL;
51 static uint32 sPreloadedAddonCount = 0;
52 
53 static recursive_lock sLock = RECURSIVE_LOCK_INITIALIZER(kLockName);
54 
55 
56 static const char *
57 find_dt_rpath(image_t *image)
58 {
59 	int i;
60 	elf_dyn *d = (elf_dyn *)image->dynamic_ptr;
61 
62 	for (i = 0; d[i].d_tag != DT_NULL; i++) {
63 		if (d[i].d_tag == DT_RPATH)
64 			return STRING(image, d[i].d_un.d_val);
65 	}
66 
67 	return NULL;
68 }
69 
70 
71 image_id
72 preload_image(char const* path, image_t **image)
73 {
74 	if (path == NULL)
75 		return B_BAD_VALUE;
76 
77 	KTRACE("rld: preload_image(\"%s\")", path);
78 
79 	status_t status = load_image(path, B_LIBRARY_IMAGE, NULL, NULL, image);
80 	if (status < B_OK) {
81 		KTRACE("rld: preload_image(\"%s\") failed to load container: %s", path,
82 			strerror(status));
83 		return status;
84 	}
85 
86 	if ((*image)->find_undefined_symbol == NULL)
87 		(*image)->find_undefined_symbol = find_undefined_symbol_global;
88 
89 	KTRACE("rld: preload_image(\"%s\") done: id: %" B_PRId32, path, (*image)->id);
90 
91 	return (*image)->id;
92 }
93 
94 
95 static void
96 preload_images(image_t **image, int32 *_count = NULL)
97 {
98 	const char* imagePaths = getenv("LD_PRELOAD");
99 	if (imagePaths == NULL) {
100 		if (_count != NULL)
101 			*_count = 0;
102 		return;
103 	}
104 
105 	int32 count = 0;
106 
107 	while (*imagePaths != '\0') {
108 		// find begin of image path
109 		while (*imagePaths != '\0' && isspace(*imagePaths))
110 			imagePaths++;
111 
112 		if (*imagePaths == '\0')
113 			break;
114 
115 		// find end of image path
116 		const char* imagePath = imagePaths;
117 		while (*imagePaths != '\0' && !isspace(*imagePaths))
118 			imagePaths++;
119 
120 		// extract the path
121 		char path[B_PATH_NAME_LENGTH];
122 		size_t pathLen = imagePaths - imagePath;
123 		if (pathLen > sizeof(path) - 1)
124 			continue;
125 
126 		if (image == NULL) {
127 			count++;
128 			continue;
129 		}
130 		memcpy(path, imagePath, pathLen);
131 		path[pathLen] = '\0';
132 
133 		// load the image
134 		preload_image(path, &image[count++]);
135 	}
136 
137 	KTRACE("rld: preload_images count: %d", count);
138 
139 	if (_count != NULL)
140 		*_count = count;
141 }
142 
143 
144 static status_t
145 load_immediate_dependencies(image_t *image, bool preload)
146 {
147 	elf_dyn *d = (elf_dyn *)image->dynamic_ptr;
148 	bool reportErrors = report_errors();
149 	status_t status = B_OK;
150 	uint32 i, j;
151 	const char *rpath;
152 
153 	if (!d || (image->flags & RFLAG_DEPENDENCIES_LOADED))
154 		return B_OK;
155 
156 	image->flags |= RFLAG_DEPENDENCIES_LOADED;
157 
158 	int32 preloadedCount = 0;
159 	if (preload) {
160 		preload_images(NULL, &preloadedCount);
161 		image->num_needed += preloadedCount;
162 	}
163 	if (image->num_needed == 0)
164 		return B_OK;
165 
166 	KTRACE("rld: load_dependencies(\"%s\", id: %" B_PRId32 ")", image->name,
167 		image->id);
168 
169 	image->needed = (image_t**)malloc(image->num_needed * sizeof(image_t *));
170 	if (image->needed == NULL) {
171 		FATAL("%s: Failed to allocate needed struct\n", image->path);
172 		KTRACE("rld: load_dependencies(\"%s\", id: %" B_PRId32
173 			") failed: no memory", image->name, image->id);
174 		return B_NO_MEMORY;
175 	}
176 
177 	memset(image->needed, 0, image->num_needed * sizeof(image_t *));
178 	if (preload)
179 		preload_images(image->needed);
180 	rpath = find_dt_rpath(image);
181 
182 	for (i = 0, j = preloadedCount; d[i].d_tag != DT_NULL; i++) {
183 		switch (d[i].d_tag) {
184 			case DT_NEEDED:
185 			{
186 				int32 neededOffset = d[i].d_un.d_val;
187 				const char *name = STRING(image, neededOffset);
188 
189 				status_t loadStatus = load_image(name, B_LIBRARY_IMAGE,
190 					rpath, image->path, &image->needed[j]);
191 				if (loadStatus < B_OK) {
192 					status = loadStatus;
193 					// correct error code in case the file could not been found
194 					if (status == B_ENTRY_NOT_FOUND) {
195 						status = B_MISSING_LIBRARY;
196 
197 						if (reportErrors)
198 							gErrorMessage.AddString("missing library", name);
199 					}
200 
201 					// Collect all missing libraries in case we report back
202 					if (!reportErrors) {
203 						KTRACE("rld: load_dependencies(\"%s\", id: %" B_PRId32
204 							") failed: %s", image->name, image->id,
205 							strerror(status));
206 						return status;
207 					}
208 				}
209 
210 				j += 1;
211 				break;
212 			}
213 
214 			default:
215 				// ignore any other tag
216 				continue;
217 		}
218 	}
219 
220 	if (status < B_OK) {
221 		KTRACE("rld: load_dependencies(\"%s\", id: %" B_PRId32 ") "
222 			"failed: %s", image->name, image->id,
223 			strerror(status));
224 		return status;
225 	}
226 
227 	if (j != image->num_needed) {
228 		FATAL("Internal error at load_dependencies()");
229 		KTRACE("rld: load_dependencies(\"%s\", id: %" B_PRId32 ") "
230 			"failed: internal error", image->name, image->id);
231 		return B_ERROR;
232 	}
233 
234 	KTRACE("rld: load_dependencies(\"%s\", id: %" B_PRId32 ") done",
235 		image->name, image->id);
236 
237 	return B_OK;
238 }
239 
240 
241 static status_t
242 load_dependencies(image_t* image, bool preload = false)
243 {
244 	// load dependencies (breadth-first)
245 	for (image_t* otherImage = image; otherImage != NULL;
246 			otherImage = otherImage->next) {
247 		status_t status = load_immediate_dependencies(otherImage, preload);
248 		if (status != B_OK)
249 			return status;
250 		preload = false;
251 	}
252 
253 	// Check the needed versions for the given image and all newly loaded
254 	// dependencies.
255 	for (image_t* otherImage = image; otherImage != NULL;
256 			otherImage = otherImage->next) {
257 		status_t status = check_needed_image_versions(otherImage);
258 		if (status != B_OK)
259 			return status;
260 	}
261 
262 	return B_OK;
263 }
264 
265 
266 static status_t
267 relocate_image(image_t *rootImage, image_t *image)
268 {
269 	SymbolLookupCache cache(image);
270 
271 	status_t status = arch_relocate_image(rootImage, image, &cache);
272 	if (status < B_OK) {
273 		FATAL("%s: Troubles relocating: %s\n", image->path, strerror(status));
274 		return status;
275 	}
276 
277 	_kern_image_relocated(image->id);
278 	image_event(image, IMAGE_EVENT_RELOCATED);
279 	return B_OK;
280 }
281 
282 
283 static status_t
284 relocate_dependencies(image_t *image)
285 {
286 	// get the images that still have to be relocated
287 	image_t **list;
288 	ssize_t count = get_sorted_image_list(image, &list, RFLAG_RELOCATED);
289 	if (count < B_OK)
290 		return count;
291 
292 	// relocate
293 	for (ssize_t i = 0; i < count; i++) {
294 		status_t status = relocate_image(image, list[i]);
295 		if (status < B_OK) {
296 			free(list);
297 			return status;
298 		}
299 	}
300 
301 	free(list);
302 	return B_OK;
303 }
304 
305 
306 static void
307 init_dependencies(image_t *image, bool initHead)
308 {
309 	image_t **initList = NULL;
310 	ssize_t count, i;
311 
312 	if (initHead && image->preinit_array) {
313 		uint count_preinit = image->preinit_array_len / sizeof(addr_t);
314 		for (uint j = 0; j < count_preinit; j++)
315 			((initfini_array_function)image->preinit_array[j])();
316 	}
317 
318 	count = get_sorted_image_list(image, &initList, RFLAG_INITIALIZED);
319 	if (count <= 0) {
320 		free(initList);
321 		return;
322 	}
323 
324 	if (!initHead) {
325 		// this removes the "calling" image
326 		image->flags &= ~RFLAG_INITIALIZED;
327 		initList[--count] = NULL;
328 	}
329 
330 	TRACE(("%ld: init dependencies\n", find_thread(NULL)));
331 	for (i = 0; i < count; i++) {
332 		image = initList[i];
333 
334 		TRACE(("%ld:  init: %s\n", find_thread(NULL), image->name));
335 
336 		init_term_function before;
337 		if (find_symbol(image,
338 				SymbolLookupInfo(B_INIT_BEFORE_FUNCTION_NAME, B_SYMBOL_TYPE_TEXT),
339 				(void**)&before) == B_OK) {
340 			before(image->id);
341 		}
342 
343 		if (image->init_routine != 0)
344 			((init_term_function)image->init_routine)(image->id);
345 
346 		if (image->init_array) {
347 			uint count_init = image->init_array_len / sizeof(addr_t);
348 			for (uint j = 0; j < count_init; j++)
349 				((initfini_array_function)image->init_array[j])();
350 		}
351 
352 		init_term_function after;
353 		if (find_symbol(image,
354 				SymbolLookupInfo(B_INIT_AFTER_FUNCTION_NAME, B_SYMBOL_TYPE_TEXT),
355 				(void**)&after) == B_OK) {
356 			after(image->id);
357 		}
358 
359 		image_event(image, IMAGE_EVENT_INITIALIZED);
360 	}
361 	TRACE(("%ld: init done.\n", find_thread(NULL)));
362 
363 	free(initList);
364 }
365 
366 
367 static void
368 call_term_functions(image_t* image)
369 {
370 	init_term_function before;
371 	if (find_symbol(image,
372 			SymbolLookupInfo(B_TERM_BEFORE_FUNCTION_NAME, B_SYMBOL_TYPE_TEXT),
373 			(void**)&before) == B_OK) {
374 		before(image->id);
375 	}
376 
377 	if (image->term_array) {
378 		uint count_term = image->term_array_len / sizeof(addr_t);
379 		for (uint i = count_term; i-- > 0;)
380 			((initfini_array_function)image->term_array[i])();
381 	}
382 
383 	if (image->term_routine)
384 		((init_term_function)image->term_routine)(image->id);
385 
386 	init_term_function after;
387 	if (find_symbol(image,
388 			SymbolLookupInfo(B_TERM_AFTER_FUNCTION_NAME, B_SYMBOL_TYPE_TEXT),
389 			(void**)&after) == B_OK) {
390 		after(image->id);
391 	}
392 }
393 
394 
395 static void
396 inject_runtime_loader_api(image_t* rootImage)
397 {
398 	// We patch any exported __gRuntimeLoader symbols to point to our private
399 	// API.
400 	image_t* image;
401 	void* _export;
402 	if (find_symbol_breadth_first(rootImage,
403 			SymbolLookupInfo("__gRuntimeLoader", B_SYMBOL_TYPE_DATA), &image,
404 			&_export) == B_OK) {
405 		*(void**)_export = &gRuntimeLoader;
406 	}
407 }
408 
409 
410 static status_t
411 add_preloaded_addon(image_t* image)
412 {
413 	// We realloc() everytime -- not particularly efficient, but good enough for
414 	// small number of preloaded addons.
415 	image_t** newArray = (image_t**)realloc(sPreloadedAddons,
416 		sizeof(image_t*) * (sPreloadedAddonCount + 1));
417 	if (newArray == NULL)
418 		return B_NO_MEMORY;
419 
420 	sPreloadedAddons = newArray;
421 	newArray[sPreloadedAddonCount++] = image;
422 
423 	return B_OK;
424 }
425 
426 
427 image_id
428 preload_addon(char const* path)
429 {
430 	if (path == NULL)
431 		return B_BAD_VALUE;
432 
433 	KTRACE("rld: preload_addon(\"%s\")", path);
434 
435 	image_t *image = NULL;
436 	status_t status = load_image(path, B_LIBRARY_IMAGE, NULL, NULL, &image);
437 	if (status < B_OK) {
438 		KTRACE("rld: preload_addon(\"%s\") failed to load container: %s", path,
439 			strerror(status));
440 		return status;
441 	}
442 
443 	if (image->find_undefined_symbol == NULL)
444 		image->find_undefined_symbol = find_undefined_symbol_global;
445 
446 	status = load_dependencies(image);
447 	if (status < B_OK)
448 		goto err;
449 
450 	set_image_flags_recursively(image, RTLD_GLOBAL);
451 
452 	status = relocate_dependencies(image);
453 	if (status < B_OK)
454 		goto err;
455 
456 	status = add_preloaded_addon(image);
457 	if (status < B_OK)
458 		goto err;
459 
460 	inject_runtime_loader_api(image);
461 
462 	remap_images();
463 	init_dependencies(image, true);
464 
465 	// if the image contains an add-on, register it
466 	runtime_loader_add_on* addOnStruct;
467 	if (find_symbol(image,
468 			SymbolLookupInfo("__gRuntimeLoaderAddOn", B_SYMBOL_TYPE_DATA),
469 			(void**)&addOnStruct) == B_OK) {
470 		add_add_on(image, addOnStruct);
471 	}
472 
473 	KTRACE("rld: preload_addon(\"%s\") done: id: %" B_PRId32, path, image->id);
474 
475 	return image->id;
476 
477 err:
478 	KTRACE("rld: preload_addon(\"%s\") failed: %s", path, strerror(status));
479 
480 	dequeue_loaded_image(image);
481 	delete_image(image);
482 	return status;
483 }
484 
485 
486 static void
487 preload_addons()
488 {
489 	const char* imagePaths = getenv("LD_PRELOAD_ADDONS");
490 	if (imagePaths == NULL)
491 		return;
492 
493 	while (*imagePaths != '\0') {
494 		// find begin of image path
495 		while (*imagePaths != '\0' && isspace(*imagePaths))
496 			imagePaths++;
497 
498 		if (*imagePaths == '\0')
499 			break;
500 
501 		// find end of image path
502 		const char* imagePath = imagePaths;
503 		while (*imagePaths != '\0' && !isspace(*imagePaths))
504 			imagePaths++;
505 
506 		// extract the path
507 		char path[B_PATH_NAME_LENGTH];
508 		size_t pathLen = imagePaths - imagePath;
509 		if (pathLen > sizeof(path) - 1)
510 			continue;
511 		memcpy(path, imagePath, pathLen);
512 		path[pathLen] = '\0';
513 
514 		// load the image
515 		preload_addon(path);
516 	}
517 }
518 
519 
520 //	#pragma mark - libroot.so exported functions
521 
522 
523 image_id
524 load_program(char const *path, void **_entry)
525 {
526 	status_t status;
527 	image_t *image;
528 
529 	KTRACE("rld: load_program(\"%s\")", path);
530 
531 	RecursiveLocker _(sLock);
532 		// for now, just do stupid simple global locking
533 
534 	preload_addons();
535 
536 	TRACE(("rld: load %s\n", path));
537 
538 	status = load_image(path, B_APP_IMAGE, NULL, NULL, &gProgramImage);
539 	if (status < B_OK)
540 		goto err;
541 
542 	if (gProgramImage->find_undefined_symbol == NULL)
543 		gProgramImage->find_undefined_symbol = find_undefined_symbol_global;
544 
545 	status = load_dependencies(gProgramImage, true);
546 	if (status < B_OK)
547 		goto err;
548 
549 	// Set RTLD_GLOBAL on all libraries including the program.
550 	// This results in the desired symbol resolution for dlopen()ed libraries.
551 	set_image_flags_recursively(gProgramImage, RTLD_GLOBAL);
552 
553 	status = relocate_dependencies(gProgramImage);
554 	if (status < B_OK)
555 		goto err;
556 
557 	inject_runtime_loader_api(gProgramImage);
558 
559 	remap_images();
560 	init_dependencies(gProgramImage, true);
561 
562 	// Since the images are initialized now, we no longer should use our
563 	// getenv(), but use the one from libroot.so
564 	find_symbol_breadth_first(gProgramImage,
565 		SymbolLookupInfo("getenv", B_SYMBOL_TYPE_TEXT), &image,
566 		(void**)&gGetEnv);
567 
568 	if (gProgramImage->entry_point == 0) {
569 		status = B_NOT_AN_EXECUTABLE;
570 		goto err;
571 	}
572 
573 	*_entry = (void *)(gProgramImage->entry_point);
574 
575 	gProgramLoaded = true;
576 
577 	KTRACE("rld: load_program(\"%s\") done: entry: %p, id: %" B_PRId32 , path,
578 		*_entry, gProgramImage->id);
579 
580 	return gProgramImage->id;
581 
582 err:
583 	KTRACE("rld: load_program(\"%s\") failed: %s", path, strerror(status));
584 
585 	delete_image(gProgramImage);
586 
587 	if (report_errors()) {
588 		// send error message
589 		gErrorMessage.AddInt32("error", status);
590 		gErrorMessage.SetDeliveryInfo(gProgramArgs->error_token,
591 			-1, 0, find_thread(NULL));
592 
593 		_kern_write_port_etc(gProgramArgs->error_port, 'KMSG',
594 			gErrorMessage.Buffer(), gErrorMessage.ContentSize(), 0, 0);
595 	}
596 	_kern_loading_app_failed(status);
597 
598 	return status;
599 }
600 
601 
602 image_id
603 load_library(char const *path, uint32 flags, bool addOn, void** _handle)
604 {
605 	image_t *image = NULL;
606 	image_type type = (addOn ? B_ADD_ON_IMAGE : B_LIBRARY_IMAGE);
607 	status_t status;
608 
609 	if (path == NULL && addOn)
610 		return B_BAD_VALUE;
611 
612 	KTRACE("rld: load_library(\"%s\", %#" B_PRIx32 ", %d)", path, flags, addOn);
613 
614 	RecursiveLocker _(sLock);
615 		// for now, just do stupid simple global locking
616 
617 	// have we already loaded this library?
618 	// Checking it at this stage saves loading its dependencies again
619 	if (!addOn) {
620 		// a NULL path is fine -- it means the global scope shall be opened
621 		if (path == NULL) {
622 			*_handle = RLD_GLOBAL_SCOPE;
623 			return 0;
624 		}
625 
626 		image = find_loaded_image_by_name(path, APP_OR_LIBRARY_TYPE);
627 		if (image != NULL && (flags & RTLD_GLOBAL) != 0)
628 			set_image_flags_recursively(image, RTLD_GLOBAL);
629 
630 		if (image) {
631 			atomic_add(&image->ref_count, 1);
632 			KTRACE("rld: load_library(\"%s\"): already loaded: %" B_PRId32,
633 				path, image->id);
634 			*_handle = image;
635 			return image->id;
636 		}
637 	}
638 
639 	status = load_image(path, type, NULL, NULL, &image);
640 	if (status < B_OK) {
641 		KTRACE("rld: load_library(\"%s\") failed to load container: %s", path,
642 			strerror(status));
643 		return status;
644 	}
645 
646 	if (image->find_undefined_symbol == NULL) {
647 		if (addOn)
648 			image->find_undefined_symbol = find_undefined_symbol_add_on;
649 		else
650 			image->find_undefined_symbol = find_undefined_symbol_global;
651 	}
652 
653 	status = load_dependencies(image);
654 	if (status < B_OK)
655 		goto err;
656 
657 	// If specified, set the RTLD_GLOBAL flag recursively on this image and all
658 	// dependencies. If not specified, we temporarily set
659 	// RFLAG_USE_FOR_RESOLVING so that the dependencies will correctly be used
660 	// for undefined symbol resolution.
661 	if ((flags & RTLD_GLOBAL) != 0)
662 		set_image_flags_recursively(image, RTLD_GLOBAL);
663 	else
664 		set_image_flags_recursively(image, RFLAG_USE_FOR_RESOLVING);
665 
666 	status = relocate_dependencies(image);
667 	if (status < B_OK)
668 		goto err;
669 
670 	if ((flags & RTLD_GLOBAL) == 0)
671 		clear_image_flags_recursively(image, RFLAG_USE_FOR_RESOLVING);
672 
673 	remap_images();
674 	init_dependencies(image, true);
675 
676 	KTRACE("rld: load_library(\"%s\") done: id: %" B_PRId32, path, image->id);
677 
678 	*_handle = image;
679 	return image->id;
680 
681 err:
682 	KTRACE("rld: load_library(\"%s\") failed: %s", path, strerror(status));
683 
684 	dequeue_loaded_image(image);
685 	delete_image(image);
686 	return status;
687 }
688 
689 
690 status_t
691 unload_library(void* handle, image_id imageID, bool addOn)
692 {
693 	image_t *image;
694 	image_type type = addOn ? B_ADD_ON_IMAGE : B_LIBRARY_IMAGE;
695 
696 	if (handle == NULL && imageID < 0)
697 		return B_BAD_IMAGE_ID;
698 
699 	if (handle == RLD_GLOBAL_SCOPE)
700 		return B_OK;
701 
702 	RecursiveLocker _(sLock);
703 		// for now, just do stupid simple global locking
704 
705 	if (gInvalidImageIDs) {
706 		// After fork, we lazily rebuild the image IDs of all loaded images
707 		update_image_ids();
708 	}
709 
710 	// we only check images that have been already initialized
711 
712 	status_t status = B_BAD_IMAGE_ID;
713 
714 	if (handle != NULL) {
715 		image = (image_t*)handle;
716 		put_image(image);
717 		status = B_OK;
718 	} else {
719 		image = find_loaded_image_by_id(imageID, true);
720 		if (image != NULL) {
721 			// unload image
722 			if (type == image->type) {
723 				put_image(image);
724 				status = B_OK;
725 			} else
726 				status = B_BAD_VALUE;
727 		}
728 	}
729 
730 	if (status == B_OK) {
731 		while ((image = get_disposable_images().head) != NULL) {
732 			// Call the exit hooks that live in this image.
733 			// Note: With the Itanium ABI this shouldn't really be done this
734 			// way anymore, since global destructors are registered via
735 			// __cxa_atexit() (the ones that are registered dynamically) and the
736 			// termination routine should call __cxa_finalize() for the image.
737 			// The reason why we still do it is that hooks registered with
738 			// atexit() aren't associated with the image. We could find out
739 			// there which image the hooks lives in and register it
740 			// respectively, but since that would be done always, that's
741 			// probably more expensive than calling
742 			// call_atexit_hooks_for_range() only here, which happens only when
743 			// libraries are unloaded dynamically.
744 			if (gRuntimeLoader.call_atexit_hooks_for_range) {
745 				gRuntimeLoader.call_atexit_hooks_for_range(
746 					image->regions[0].vmstart, image->regions[0].vmsize);
747 			}
748 
749 			image_event(image, IMAGE_EVENT_UNINITIALIZING);
750 
751 			call_term_functions(image);
752 
753 			TLSBlockTemplates::Get().Unregister(image->dso_tls_id);
754 
755 			dequeue_disposable_image(image);
756 			unmap_image(image);
757 
758 			image_event(image, IMAGE_EVENT_UNLOADING);
759 
760 			delete_image(image);
761 		}
762 	}
763 
764 	return status;
765 }
766 
767 
768 status_t
769 get_nth_symbol(image_id imageID, int32 num, char *nameBuffer,
770 	int32 *_nameLength, int32 *_type, void **_location)
771 {
772 	int32 count = 0, j;
773 	uint32 i;
774 	image_t *image;
775 
776 	RecursiveLocker _(sLock);
777 
778 	// get the image from those who have been already initialized
779 	image = find_loaded_image_by_id(imageID, false);
780 	if (image == NULL)
781 		return B_BAD_IMAGE_ID;
782 
783 	// iterate through all the hash buckets until we've found the one
784 	for (i = 0; i < HASHTABSIZE(image); i++) {
785 		for (j = HASHBUCKETS(image)[i]; j != STN_UNDEF; j = HASHCHAINS(image)[j]) {
786 			elf_sym *symbol = &image->syms[j];
787 
788 			if (count == num) {
789 				const char* symbolName = SYMNAME(image, symbol);
790 				strlcpy(nameBuffer, symbolName, *_nameLength);
791 				*_nameLength = strlen(symbolName);
792 
793 				void* location = (void*)(symbol->st_value
794 					+ image->regions[0].delta);
795 				int32 type;
796 				if (symbol->Type() == STT_FUNC)
797 					type = B_SYMBOL_TYPE_TEXT;
798 				else if (symbol->Type() == STT_OBJECT)
799 					type = B_SYMBOL_TYPE_DATA;
800 				else
801 					type = B_SYMBOL_TYPE_ANY;
802 					// TODO: check with the return types of that BeOS function
803 
804 				patch_defined_symbol(image, symbolName, &location, &type);
805 
806 				if (_type != NULL)
807 					*_type = type;
808 				if (_location != NULL)
809 					*_location = location;
810 				goto out;
811 			}
812 			count++;
813 		}
814 	}
815 out:
816 	if (num != count)
817 		return B_BAD_INDEX;
818 
819 	return B_OK;
820 }
821 
822 
823 status_t
824 get_nearest_symbol_at_address(void* address, image_id* _imageID,
825 	char** _imagePath, char** _imageName, char** _symbolName, int32* _type,
826 	void** _location, bool* _exactMatch)
827 {
828 	RecursiveLocker _(sLock);
829 
830 	image_t* image = find_loaded_image_by_address((addr_t)address);
831 	if (image == NULL)
832 		return B_BAD_VALUE;
833 
834 	if (_imageID != NULL)
835 		*_imageID = image->id;
836 	if (_imagePath != NULL)
837 		*_imagePath = image->path;
838 	if (_imageName != NULL)
839 		*_imageName = image->name;
840 
841 	// If the caller does not want the actual symbol name, only the image,
842 	// we can just return immediately.
843 	if (_symbolName == NULL && _type == NULL && _location == NULL)
844 		return B_OK;
845 
846 	bool exactMatch = false;
847 	elf_sym* foundSymbol = NULL;
848 	addr_t foundLocation = (addr_t)NULL;
849 
850 	for (uint32 i = 0; i < HASHTABSIZE(image) && !exactMatch; i++) {
851 		for (int32 j = HASHBUCKETS(image)[i]; j != STN_UNDEF;
852 				j = HASHCHAINS(image)[j]) {
853 			elf_sym *symbol = &image->syms[j];
854 			addr_t location = symbol->st_value + image->regions[0].delta;
855 
856 			if (location <= (addr_t)address	&& location >= foundLocation) {
857 				foundSymbol = symbol;
858 				foundLocation = location;
859 
860 				// jump out if we have an exact match
861 				if (location + symbol->st_size > (addr_t)address) {
862 					exactMatch = true;
863 					break;
864 				}
865 			}
866 		}
867 	}
868 
869 	if (_exactMatch != NULL)
870 		*_exactMatch = exactMatch;
871 
872 	if (foundSymbol != NULL) {
873 		*_symbolName = SYMNAME(image, foundSymbol);
874 
875 		if (_type != NULL) {
876 			if (foundSymbol->Type() == STT_FUNC)
877 				*_type = B_SYMBOL_TYPE_TEXT;
878 			else if (foundSymbol->Type() == STT_OBJECT)
879 				*_type = B_SYMBOL_TYPE_DATA;
880 			else
881 				*_type = B_SYMBOL_TYPE_ANY;
882 			// TODO: check with the return types of that BeOS function
883 		}
884 
885 		if (_location != NULL)
886 			*_location = (void*)foundLocation;
887 	} else {
888 		*_symbolName = NULL;
889 		if (_location != NULL)
890 			*_location = NULL;
891 	}
892 
893 	return B_OK;
894 }
895 
896 
897 status_t
898 get_symbol(image_id imageID, char const *symbolName, int32 symbolType,
899 	bool recursive, image_id *_inImage, void **_location)
900 {
901 	status_t status = B_OK;
902 	image_t *image;
903 
904 	if (imageID < B_OK)
905 		return B_BAD_IMAGE_ID;
906 	if (symbolName == NULL)
907 		return B_BAD_VALUE;
908 
909 	// Previously, these functions were called in __haiku_init_before
910 	// and __haiku_init_after. Now we call them inside runtime_loader,
911 	// so we prevent applications from fetching them.
912 	if (strcmp(symbolName, B_INIT_BEFORE_FUNCTION_NAME) == 0
913 		|| strcmp(symbolName, B_INIT_AFTER_FUNCTION_NAME) == 0
914 		|| strcmp(symbolName, B_TERM_BEFORE_FUNCTION_NAME) == 0
915 		|| strcmp(symbolName, B_TERM_AFTER_FUNCTION_NAME) == 0)
916 		return B_BAD_VALUE;
917 
918 	RecursiveLocker _(sLock);
919 		// for now, just do stupid simple global locking
920 
921 	// get the image from those who have been already initialized
922 	image = find_loaded_image_by_id(imageID, false);
923 	if (image != NULL) {
924 		if (recursive) {
925 			// breadth-first search in the given image and its dependencies
926 			status = find_symbol_breadth_first(image,
927 				SymbolLookupInfo(symbolName, symbolType, NULL,
928 					LOOKUP_FLAG_DEFAULT_VERSION),
929 				&image, _location);
930 		} else {
931 			status = find_symbol(image,
932 				SymbolLookupInfo(symbolName, symbolType, NULL,
933 					LOOKUP_FLAG_DEFAULT_VERSION),
934 				_location);
935 		}
936 
937 		if (status == B_OK && _inImage != NULL)
938 			*_inImage = image->id;
939 	} else
940 		status = B_BAD_IMAGE_ID;
941 
942 	return status;
943 }
944 
945 
946 status_t
947 get_library_symbol(void* handle, void* caller, const char* symbolName,
948 	void **_location)
949 {
950 	status_t status = B_ENTRY_NOT_FOUND;
951 
952 	if (symbolName == NULL)
953 		return B_BAD_VALUE;
954 
955 	RecursiveLocker _(sLock);
956 		// for now, just do stupid simple global locking
957 
958 	if (handle == RTLD_DEFAULT || handle == RLD_GLOBAL_SCOPE) {
959 		// look in the default scope
960 		image_t* image;
961 		elf_sym* symbol = find_undefined_symbol_global(gProgramImage,
962 			gProgramImage,
963 			SymbolLookupInfo(symbolName, B_SYMBOL_TYPE_ANY, NULL,
964 				LOOKUP_FLAG_DEFAULT_VERSION),
965 			&image);
966 		if (symbol != NULL) {
967 			*_location = (void*)(symbol->st_value + image->regions[0].delta);
968 			int32 symbolType = symbol->Type() == STT_FUNC
969 				? B_SYMBOL_TYPE_TEXT : B_SYMBOL_TYPE_DATA;
970 			patch_defined_symbol(image, symbolName, _location, &symbolType);
971 			status = B_OK;
972 		}
973 	} else if (handle == RTLD_NEXT) {
974 		// Look in the default scope, but also in the dependencies of the
975 		// calling image. Return the next after the caller symbol.
976 
977 		// First of all, find the caller image.
978 		image_t* callerImage = get_loaded_images().head;
979 		for (; callerImage != NULL; callerImage = callerImage->next) {
980 			elf_region_t& text = callerImage->regions[0];
981 			if ((addr_t)caller >= text.vmstart
982 				&& (addr_t)caller < text.vmstart + text.vmsize) {
983 				// found the image
984 				break;
985 			}
986 		}
987 
988 		if (callerImage != NULL) {
989 			// found the caller -- now search the global scope until we find
990 			// the next symbol
991 			bool hitCallerImage = false;
992 			set_image_flags_recursively(callerImage, RFLAG_USE_FOR_RESOLVING);
993 
994 			elf_sym* candidateSymbol = NULL;
995 			image_t* candidateImage = NULL;
996 
997 			image_t* image = get_loaded_images().head;
998 			for (; image != NULL; image = image->next) {
999 				// skip the caller image
1000 				if (image == callerImage) {
1001 					hitCallerImage = true;
1002 					continue;
1003 				}
1004 
1005 				// skip all images up to the caller image; also skip add-on
1006 				// images and those not marked above for resolution
1007 				if (!hitCallerImage || image->type == B_ADD_ON_IMAGE
1008 					|| (image->flags
1009 						& (RTLD_GLOBAL | RFLAG_USE_FOR_RESOLVING)) == 0) {
1010 					continue;
1011 				}
1012 
1013 				elf_sym *symbol = find_symbol(image,
1014 					SymbolLookupInfo(symbolName, B_SYMBOL_TYPE_TEXT, NULL,
1015 						LOOKUP_FLAG_DEFAULT_VERSION));
1016 				if (symbol == NULL)
1017 					continue;
1018 
1019 				// found a symbol
1020 				bool isWeak = symbol->Bind() == STB_WEAK;
1021 				if (candidateImage == NULL || !isWeak) {
1022 					candidateSymbol = symbol;
1023 					candidateImage = image;
1024 
1025 					if (!isWeak)
1026 						break;
1027 				}
1028 
1029 				// symbol is weak, so we need to continue
1030 			}
1031 
1032 			if (candidateSymbol != NULL) {
1033 				// found the symbol
1034 				*_location = (void*)(candidateSymbol->st_value
1035 					+ candidateImage->regions[0].delta);
1036 				int32 symbolType = B_SYMBOL_TYPE_TEXT;
1037 				patch_defined_symbol(candidateImage, symbolName, _location,
1038 					&symbolType);
1039 				status = B_OK;
1040 			}
1041 
1042 			clear_image_flags_recursively(callerImage, RFLAG_USE_FOR_RESOLVING);
1043 		}
1044 	} else {
1045 		// breadth-first search in the given image and its dependencies
1046 		image_t* inImage;
1047 		status = find_symbol_breadth_first((image_t*)handle,
1048 			SymbolLookupInfo(symbolName, B_SYMBOL_TYPE_ANY, NULL,
1049 				LOOKUP_FLAG_DEFAULT_VERSION),
1050 			&inImage, _location);
1051 	}
1052 
1053 	return status;
1054 }
1055 
1056 
1057 status_t
1058 get_next_image_dependency(image_id id, uint32 *cookie, const char **_name)
1059 {
1060 	uint32 i, j, searchIndex = *cookie;
1061 	elf_dyn *dynamicSection;
1062 	image_t *image;
1063 
1064 	if (_name == NULL)
1065 		return B_BAD_VALUE;
1066 
1067 	RecursiveLocker _(sLock);
1068 
1069 	image = find_loaded_image_by_id(id, false);
1070 	if (image == NULL)
1071 		return B_BAD_IMAGE_ID;
1072 
1073 	dynamicSection = (elf_dyn *)image->dynamic_ptr;
1074 	if (dynamicSection == NULL || image->num_needed <= searchIndex)
1075 		return B_ENTRY_NOT_FOUND;
1076 
1077 	for (i = 0, j = 0; dynamicSection[i].d_tag != DT_NULL; i++) {
1078 		if (dynamicSection[i].d_tag != DT_NEEDED)
1079 			continue;
1080 
1081 		if (j++ == searchIndex) {
1082 			int32 neededOffset = dynamicSection[i].d_un.d_val;
1083 
1084 			*_name = STRING(image, neededOffset);
1085 			*cookie = searchIndex + 1;
1086 			return B_OK;
1087 		}
1088 	}
1089 
1090 	return B_ENTRY_NOT_FOUND;
1091 }
1092 
1093 
1094 //	#pragma mark - runtime_loader private exports
1095 
1096 
1097 /*! Read and verify the ELF header */
1098 status_t
1099 elf_verify_header(void *header, size_t length)
1100 {
1101 	int32 programSize, sectionSize;
1102 
1103 	if (length < sizeof(elf_ehdr))
1104 		return B_NOT_AN_EXECUTABLE;
1105 
1106 	return parse_elf_header((elf_ehdr *)header, &programSize, &sectionSize);
1107 }
1108 
1109 
1110 #ifdef _COMPAT_MODE
1111 #ifdef __x86_64__
1112 status_t
1113 elf32_verify_header(void *header, size_t length)
1114 {
1115 	int32 programSize, sectionSize;
1116 
1117 	if (length < sizeof(Elf32_Ehdr))
1118 		return B_NOT_AN_EXECUTABLE;
1119 
1120 	return parse_elf32_header((Elf32_Ehdr *)header, &programSize, &sectionSize);
1121 }
1122 #else
1123 status_t
1124 elf64_verify_header(void *header, size_t length)
1125 {
1126 	int32 programSize, sectionSize;
1127 
1128 	if (length < sizeof(Elf64_Ehdr))
1129 		return B_NOT_AN_EXECUTABLE;
1130 
1131 	return parse_elf64_header((Elf64_Ehdr *)header, &programSize, &sectionSize);
1132 }
1133 #endif	// __x86_64__
1134 #endif	// _COMPAT_MODE
1135 
1136 
1137 void
1138 terminate_program(void)
1139 {
1140 	image_t **termList;
1141 	ssize_t count, i;
1142 
1143 	count = get_sorted_image_list(NULL, &termList, RFLAG_TERMINATED);
1144 	if (count < B_OK)
1145 		return;
1146 
1147 	if (gInvalidImageIDs) {
1148 		// After fork, we lazily rebuild the image IDs of all loaded images
1149 		update_image_ids();
1150 	}
1151 
1152 	TRACE(("%ld: terminate dependencies\n", find_thread(NULL)));
1153 	for (i = count; i-- > 0;) {
1154 		image_t *image = termList[i];
1155 
1156 		TRACE(("%ld:  term: %s\n", find_thread(NULL), image->name));
1157 
1158 		image_event(image, IMAGE_EVENT_UNINITIALIZING);
1159 
1160 		call_term_functions(image);
1161 
1162 		image_event(image, IMAGE_EVENT_UNLOADING);
1163 	}
1164 	TRACE(("%ld:  term done.\n", find_thread(NULL)));
1165 
1166 	free(termList);
1167 }
1168 
1169 
1170 void
1171 rldelf_init(void)
1172 {
1173 	init_add_ons();
1174 
1175 	// create the debug area
1176 	{
1177 		size_t size = TO_PAGE_SIZE(sizeof(runtime_loader_debug_area));
1178 
1179 		runtime_loader_debug_area *area;
1180 		area_id areaID = _kern_create_area(RUNTIME_LOADER_DEBUG_AREA_NAME,
1181 			(void **)&area, B_RANDOMIZED_ANY_ADDRESS, size, B_NO_LOCK,
1182 			B_READ_AREA | B_WRITE_AREA | B_CLONEABLE_AREA);
1183 		if (areaID < B_OK) {
1184 			FATAL("Failed to create debug area.\n");
1185 			_kern_loading_app_failed(areaID);
1186 		}
1187 
1188 		area->loaded_images = &get_loaded_images();
1189 	}
1190 
1191 	// initialize error message if needed
1192 	if (report_errors()) {
1193 		void *buffer = malloc(1024);
1194 		if (buffer == NULL)
1195 			return;
1196 
1197 		gErrorMessage.SetTo(buffer, 1024, 'Rler');
1198 	}
1199 }
1200 
1201 
1202 status_t
1203 elf_reinit_after_fork(void)
1204 {
1205 	recursive_lock_init(&sLock, kLockName);
1206 
1207 	// We also need to update the IDs of our images. We are the child and
1208 	// and have cloned images with different IDs. Since in most cases (fork()
1209 	// + exec*()) this would just increase the fork() overhead with no one
1210 	// caring, we do that lazily, when first doing something different.
1211 	gInvalidImageIDs = true;
1212 
1213 	return B_OK;
1214 }
1215