xref: /haiku/src/system/libnetwork/init.cpp (revision fc7456e9b1ec38c941134ed6d01c438cf289381e)
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 
19 #ifdef __HAIKU_BEOS_COMPATIBLE
20 
21 
22 bool __gR5Compatibility = false;
23 addr_t __gNetworkStart;
24 addr_t __gNetworkEnd;
25 
26 
27 static void
28 set_own_image_location(image_id id)
29 {
30 	image_info info;
31 	if (get_image_info(id, &info) == B_OK) {
32 		__gNetworkStart = (addr_t)min_c(info.text, info.data);
33 		__gNetworkEnd = min_c((addr_t)info.text + info.text_size,
34 			(addr_t)info.data + info.data_size);
35 	}
36 }
37 
38 
39 extern "C" void
40 initialize_before(image_id our_image)
41 {
42 	// determine if we have to run in BeOS compatibility mode
43 
44 	// get image of executable
45 	image_info info;
46 	uint32 cookie = 0;
47 	if (get_next_image_info(B_CURRENT_TEAM, (int32*)&cookie, &info) != B_OK)
48 		return;
49 
50 	if (get_image_symbol(info.id, "__gHaikuStartupCode", B_SYMBOL_TYPE_DATA,
51 			NULL) == B_OK) {
52 		// we were linked on/for Haiku
53 		return;
54 	}
55 
56 	// We're using the BeOS startup code, check if BONE libraries are in
57 	// use, and if not, enable the BeOS R5 compatibility layer.
58 	// As dependencies to network libraries may be "hidden" in libraries, we
59 	// may have to scan not only the executable, but every loaded image.
60 	int enable = 0;
61 	uint32 crumble;
62 	const char *name;
63 	do {
64 		crumble = 0;
65 		while (__get_next_image_dependency(info.id, &crumble, &name) == B_OK) {
66 			if (!strcmp(name, "libbind.so")
67 				|| !strcmp(name, "libsocket.so")
68 				|| !strcmp(name, "libbnetapi.so")
69 				|| !strcmp(name, "libnetwork.so"))
70 				enable -= 2;
71 			else if (!strcmp(name, "libnet.so")
72 				|| !strcmp(name, "libnetapi.so"))
73 				enable++;
74 		}
75 
76 		if (enable > 0) {
77 			__gR5Compatibility = true;
78 			set_own_image_location(our_image);
79 			return;
80 		}
81 	} while (enable == 0
82 		&& get_next_image_info(B_CURRENT_TEAM, (int32*)&cookie, &info) == B_OK);
83 }
84 
85 
86 #endif
87