1/*
2 * Copyright 2006-2010, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 *		Axel D��rfler, axeld@pinc-software.de
7 *		Oliver Tappe, zooey@hirschkaefer.de
8 */
9
10
11#include <libroot_private.h>
12
13#include <OS.h>
14#include <image.h>
15
16#include <string.h>
17
18
19bool __gR5Compatibility = false;
20addr_t __gNetworkStart;
21addr_t __gNetworkEnd;
22
23
24static void
25set_own_image_location(image_id id)
26{
27	image_info info;
28	if (get_image_info(id, &info) == B_OK) {
29		__gNetworkStart = (addr_t)min_c(info.text, info.data);
30		__gNetworkEnd = min_c((addr_t)info.text + info.text_size,
31			(addr_t)info.data + info.data_size);
32	}
33}
34
35
36extern "C" void
37initialize_before(image_id our_image)
38{
39	// determine if we have to run in BeOS compatibility mode
40
41	// get image of executable
42	image_info info;
43	uint32 cookie = 0;
44	if (get_next_image_info(B_CURRENT_TEAM, (int32*)&cookie, &info) != B_OK)
45		return;
46
47	if (get_image_symbol(info.id, "__gHaikuStartupCode", B_SYMBOL_TYPE_DATA,
48			NULL) == B_OK) {
49		// we were linked on/for Haiku
50		return;
51	}
52
53	// We're using the BeOS startup code, check if BONE libraries are in
54	// use, and if not, enable the BeOS R5 compatibility layer.
55	// As dependencies to network libraries may be "hidden" in libraries, we
56	// may have to scan not only the executable, but every loaded image.
57	int enable = 0;
58	uint32 crumble;
59	const char *name;
60	do {
61		crumble = 0;
62		while (__get_next_image_dependency(info.id, &crumble, &name) == B_OK) {
63			if (!strcmp(name, "libbind.so")
64				|| !strcmp(name, "libsocket.so")
65				|| !strcmp(name, "libbnetapi.so")
66				|| !strcmp(name, "libnetwork.so"))
67				enable -= 2;
68			else if (!strcmp(name, "libnet.so")
69				|| !strcmp(name, "libnetapi.so"))
70				enable++;
71		}
72
73		if (enable > 0) {
74			__gR5Compatibility = true;
75			set_own_image_location(our_image);
76			return;
77		}
78	} while (enable == 0
79		&& get_next_image_info(B_CURRENT_TEAM, (int32*)&cookie, &info) == B_OK);
80}
81