summaryrefslogtreecommitdiff
path: root/build/linux/LibcWrapGenerator.vala
blob: 5a26638c1a5eb062d5fde6e7a3e847549381e731 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
 * Copyright (C) 2011 Jan Niklas Hasse <jhasse@gmail.com>
 * Copyright (C) Tristan Van Berkom <tristan@upstairslabs.com>
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public 
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * Authors:
 *   Jan Niklas Hasse <jhasse@gmail.com>
 *   Tristan Van Berkom <tristan@upstairslabs.com>
 *
 *
 * This program is used to generate a header safely selecting the ABI
 * glibc requirement.
 *
 * The resulting program can be run as such:
 *
 *      ./LibcWrapGenerator libcwrap.h 2.7 /path/to/libc/runtime/libraries
 *
 * This will generate a libcwrap.h which should be included by any
 * C/C++ sources before anything else, redirecting any references
 * to glibc symbols > 2.7 to symbols from previous versions.
 *
 * For symbols which are found to be new after 2.7, those will be
 * redirected to symbol@GLIBC_DONT_USE_THIS_SYMBOL_2.10 (or whichever
 * version the said symbol was actually added in)
 *
 * This will generate a link error at compile time, it is possible
 * however unlikely that these link errors will occur, if they do
 * you must patch the sources in such a way that those glibc symbols
 * which generated the link error are not accessed.
 *
 * Sources should also be compiled with -U_FORTIFY_SOURCE as some
 * compilers build in _FORTIFY_SOURCE by default, enabling some
 * glibc runtime checkers to be linked into your source code.
 *
 * We recommend disabling _FORTIFY_SOURCE since most of the runtime
 * checkers are only available in relatively recent versions of glibc.
 */
using GLib;

/***************************************************************
 *                      Debugging Facilities                   *
 ***************************************************************/
[Flags]
public enum DF { 
	COLLECT,
	FILTER
}

static const GLib.DebugKey[] libcwrap_debug_keys = {
	{ "collect", DF.COLLECT },
	{ "filter", DF.FILTER } 
};

private bool libcwrap_debug_initialized = false;
private uint libcwrap_debug_mask = 0;
public delegate void DebugFunc ();

public void libcwrap_note (int domain, DebugFunc debug_func)
{
	if (!libcwrap_debug_initialized) {
		string libcwrap_debug_env = GLib.Environment.get_variable ("LIBCWRAP_DEBUG");

		libcwrap_debug_mask = GLib.parse_debug_string (libcwrap_debug_env, libcwrap_debug_keys);
		libcwrap_debug_initialized = true;
	}

	if ((libcwrap_debug_mask & domain) != 0)
		debug_func ();
}

/***************************************************************
 *                      VersionNumber class                    *
 ***************************************************************/
class VersionNumber : Object
{
	private int major    { get; set; } // x.0.0
	private int minor    { get; set; } // 0.x.0
	private int revision { get; set; } // 0.0.x
	private string originalString;
	
	public VersionNumber(string version)
	{
		originalString = version;
		try
		{
			var regex = new Regex("([[:digit:]]*)\\.([[:digit:]]*)\\.*([[:digit:]]*)");
			var split = regex.split(version);
			assert(split.length > 1); // TODO: Don't use assert, print a nice error message instead
			major = int.parse (split[1]);
			if(split.length > 2)
			{
				minor = int.parse (split[2]);
			}
			else
			{
				minor = 0;
			}
			if(split.length > 3)
			{
				revision = int.parse (split[3]);
			}
			else
			{
				revision = 0;
			}
		}
		catch(GLib.RegexError e)
		{
			stdout.printf("Error compiling regular expression!");
			Posix.exit(-1);
		}
	}
	
	public bool newerThan(VersionNumber other)
	{
		if(major > other.major)
		{
			return true;
		}
		else if(major == other.major)
		{
			if(minor > other.minor)
			{
				return true;
			}
			else if(minor == other.minor)
			{
				if(revision > other.revision)
				{
					return true;
				}
			}
		}
		return false;
	}
	public string getString()
	{
		return originalString;
	}
}

/***************************************************************
 *                         Symbol output                       *
 ***************************************************************/
private void
append_symbols (StringBuilder headerFile, 
				VersionNumber minimumVersion,
				Gee.HashMap<string, VersionNumber> symbolMap,
				Gee.HashSet<string>filterMap,
				bool overrides)
{

	if (overrides)
		headerFile.append("\n/* Symbols introduced in newer glibc versions, which must not be used */\n");
	else
		headerFile.append("\n/* Symbols redirected to earlier glibc versions */\n");

	foreach (var it in symbolMap.keys)
	{
		var version = symbolMap.get (it);
		string versionToUse;

		/* If the symbol only has occurrences older than the minimum required glibc version,
		 * then there is no need to output anything for this symbol
		 */
		if (filterMap.contains (it))
			continue;

		if (overrides && version.newerThan (minimumVersion))
			versionToUse = "DONT_USE_THIS_VERSION_%s".printf (version.getString());
		else if (!overrides && !version.newerThan (minimumVersion))
			versionToUse = version.getString ();
		else
			continue;

		headerFile.append("__asm__(\".symver %s, %s@GLIBC_%s\");\n".printf(it, it, versionToUse));
	}
}

/***************************************************************
 *                             Main                            *
 ***************************************************************/
int main(string[] args)
{
	try
	{
		if(args.length != 4)
		{
			stdout.printf("Usage: buildlist <header file to create> <minimum glibc version> <system library directory>\n");
			return 1;
		}

		var minimumVersion = new VersionNumber(args[2]);
		var filename = args[1];
		var syslibs = args[3];

		stdout.printf ("Generating %s (glibc %s) from libs at '%s' .", filename, minimumVersion.getString(), syslibs);

		var headerFile = new StringBuilder ();
		headerFile.append ("/* glibc bindings for target ABI version glibc " + minimumVersion.getString() + " */\n");
		stdout.flush();

		string output, errorOutput;
		int returnCode;
		var libPath = File.new_for_path (syslibs);
		var enumerator = libPath.enumerate_children (FileAttribute.STANDARD_NAME, 0, null);
		FileInfo fileinfo;
		var counter = 0;

		/* This map will contain every symbol with new version as close to the minimum version as possible
		 * including symbols which have a higher version than the minimum
		 */
		var symbolMap = new Gee.HashMap<string, VersionNumber>((Gee.HashDataFunc<string>)GLib.str_hash,
															   (Gee.EqualDataFunc<string>)GLib.str_equal);

		/* This map will contain only symbols for which a version newer than the minimum was not found,
		 * these symbols are safe to exclude from the output
		 */
		var symbolsOlderThanMinimum = new Gee.HashSet<string>((Gee.HashDataFunc<string>)GLib.str_hash,
															  (Gee.EqualDataFunc<string>)GLib.str_equal);

		while ((fileinfo = enumerator.next_file(null)) != null)
		{
			if(++counter % 50 == 0)
			{
				stdout.printf(".");
				stdout.flush();
			}

			Process.spawn_command_line_sync("objdump -T " + syslibs + "/" + fileinfo.get_name(), 
											out output, out errorOutput, out returnCode);

			if (returnCode != 0)
				continue;

			foreach (var line in output.split("\n"))
			{
				var regex = new Regex("(.*)(GLIBC_)([0-9]+\\.([0-9]+\\.)*[0-9]+)(\\)?)([ ]*)(.+)");

				if (regex.match (line) && !("PRIVATE" in line))
				{
					var version = new VersionNumber (regex.split (line)[3]);
					var symbolName = regex.split (line)[7];
					var versionInMap = symbolMap.get (symbolName);

					/* Some versioning symbols exist in the objdump output, let's skip those */
					if (symbolName.has_prefix ("GLIBC"))
						continue;

					libcwrap_note (DF.COLLECT, () =>
							 stdout.printf ("Selected symbol '%s' version '%s' from objdump line %s\n", 
											symbolName, version.getString(), line));

					if (versionInMap == null)
					{
						symbolMap.set(symbolName, version);

						/* First occurance of the symbol, if it's older
						 * than the minimum required, put it in that table also
						 */
						if (minimumVersion.newerThan (version))
						{
							symbolsOlderThanMinimum.add(symbolName);

							libcwrap_note (DF.FILTER, () =>
									 stdout.printf ("Adding symbol '%s %s' to the filter\n", 
													symbolName, version.getString()));
						}
					}
					else
					{
						/* We want the newest possible version of a symbol which is older than the
						 * minimum glibc version specified (or the only version of the symbol if
						 * it's newer than the minimum version)
						 */
						if(version.newerThan (versionInMap) && minimumVersion.newerThan (version))
							symbolMap.set(symbolName, version);

						/* While trucking along through the huge symbol list, remove symbols from
						 * the 'safe to exclude' if there is a version found which is newer
						 * than the minimum requirement
						 */
						if (version.newerThan (minimumVersion))
						{
							symbolsOlderThanMinimum.remove(symbolName);

							libcwrap_note (DF.FILTER, () =>
									 stdout.printf ("Removing symbol '%s %s' from the filter\n", 
													symbolName, version.getString()));
						}
					}
				}
				else
				{
					libcwrap_note (DF.COLLECT, () => stdout.printf ("Rejected objdump line %s\n", line));
				}
			}
		}
		
		headerFile.append ("#if !defined (__LIBC_CUSTOM_BINDINGS_H__)\n");
		headerFile.append ("\n");
		headerFile.append ("#  if !defined (__OBJC__) && !defined (__ASSEMBLER__)\n");
		headerFile.append ("#    if defined (__cplusplus)\n");
		headerFile.append ("extern \"C\" {\n");
		headerFile.append ("#    endif\n");

		/* For prettier output, let's output the redirected symbols first, and
		 * then output the ones which must not be used (new in glibc > minimumVersion).
		 */
		append_symbols (headerFile, minimumVersion,
						symbolMap, symbolsOlderThanMinimum,
						false);

		append_symbols (headerFile, minimumVersion,
						symbolMap, symbolsOlderThanMinimum,
						true);

		headerFile.append ("\n");
		headerFile.append ("#    if defined (__cplusplus)\n");
		headerFile.append ("}\n");
		headerFile.append ("#    endif\n");
		headerFile.append ("#  endif /* !defined (__OBJC__) && !defined (__ASSEMBLER__) */\n");
		headerFile.append ("#endif\n");

		FileUtils.set_contents(filename, headerFile.str);
	}
	catch(Error e)
	{
		warning("%s", e.message);
		return 1;
	}
	stdout.printf(" OK\n");
	return 0;
}