3 @c ---------------------------------------------------------------------
5 @c ---------------------------------------------------------------------
7 @setfilename porting.info
8 @settitle Porting libstdc++-v3
12 This file explains how to port libstdc++-v3 (the GNU C++ library) to
15 Copyright (c) 2000, 2001 Free Software Foundation, Inc.
18 @c ---------------------------------------------------------------------
20 @c ---------------------------------------------------------------------
23 @title Porting libstdc++-v3
26 @vskip 0pt plus 1filll
27 Copyright @copyright{} 2000, 2001 Free Software Foundation, Inc.
29 Permission is granted to copy, distribute and/or modify this document
30 under the terms of the GNU Free Documentation License, Version 1.1 or
31 any later version published by the Free Software Foundation; with the
32 Invariant Sections being ``GNU General Public License'', the Front-Cover
33 texts being (a) (see below), and with the Back-Cover Texts being (b)
34 (see below). A copy of the license is included in the section entitled
35 ``GNU Free Documentation License''.
37 (a) The FSF's Front-Cover Text is:
41 (b) The FSF's Back-Cover Text is:
43 You have freedom to copy and modify this GNU Manual, like GNU
44 software. Copies published by the Free Software Foundation raise
45 funds for GNU development.
48 @c ---------------------------------------------------------------------
50 @c ---------------------------------------------------------------------
53 @top Porting libstdc++-v3
55 This document explains how to port libstdc++-v3 (the GNU C++ library) to
58 In order to make the GNU C++ library (libstdc++-v3) work with a new
59 target, you must edit some configuration files and provide some new
62 Before you get started, make sure that you have a working C library on
63 your target. The C library need not precisely comply with any
64 particular standard, but should generally conform to the requirements
65 imposed by the ANSI/ISO standard.
67 In addition, you should try to verify that the C++ compiler generally
68 works. It is difficult to test the C++ compiler without a working
69 library, but you should at least try some minimal test cases.
71 Here are the primary steps required to port the library:
74 * Operating system:: Configuring for your operating system.
75 * Character types:: Implementing character classification.
76 * Thread safety:: Implementing atomic operations.
77 * Libtool:: Using libtool.
78 * GNU Free Documentation License:: How you can copy and share this manual.
81 @c ---------------------------------------------------------------------
83 @c ---------------------------------------------------------------------
85 @node Operating system
86 @chapter Operating system
88 If you are porting to a new operating-system (as opposed to a new chip
89 using an existing operating system), you will need to create a new
90 directory in the @file{config/os} hierarchy. For example, the IRIX
91 configuration files are all in @file{config/os/irix}. There is no set
92 way to organize the OS configuration directory. For example,
93 @file{config/os/solaris/solaris-2.6} and
94 @file{config/os/solaris/solaris-2.7} are used as configuration
95 directories for these two versions of Solaris. On the other hand, both
96 Solaris 2.7 and Solaris 2.8 use the @file{config/os/solaris/solaris-2.7}
97 directory. The important information is that there needs to be a
98 directory under @file{config/os} to store the files for your operating
101 You'll have to change the @file{configure.target} file to ensure that
102 your new directory is activated. Look for the switch statement that
103 sets @code{os_include_dir}, and add a pattern to handle your operating
104 system. The switch statement switches on only the OS portion of the
105 standard target triplet; e.g., the @code{solaris2.8} in
106 @code{sparc-sun-solaris2.8}.
108 The first file to create in this directory, should be called
109 @file{bits/os_defines.h}. This file contains basic macro definitions
110 that are required to allow the C++ library to work with your C library.
111 This file should provide macro definitions for @code{__off_t},
112 @code{__off64_t}, and @code{__ssize_t}. Typically, this just looks
116 #define __off_t off_t
117 #define __off64_t off64_t
118 #define __ssize_t ssize_t
122 You don't have to provide these definitions if your system library
123 already defines these types -- but the only library known to provide
124 these types is the GNU C Library, so you will almost certainly have to
125 provide these macros. Note that this file does not have to include a
126 header file that defines @code{off_t}, or the other types; you simply
127 have to provide the macros.
129 In addition, several libstdc++-v3 source files unconditionally define
130 the macro @code{_POSIX_SOURCE}. On many systems, defining this macro
131 causes large portions of the C library header files to be eliminated
132 at preprocessing time. Therefore, you may have to @code{#undef} this
133 macro, or define other macros (like @code{_LARGEFILE_SOURCE} or
134 @code{__EXTENSIONS__}). You won't know what macros to define or
135 undefine at this point; you'll have to try compiling the library and
136 seeing what goes wrong. If you see errors about calling functions
137 that have not been declared, look in your C library headers to see if
138 the functions are declared there, and then figure out what macros you
139 need to define. You will need to add them to the
140 @code{CPLUSPLUS_CPP_SPEC} macro in the GCC configuration file for your
141 target. It will not work to simply define these macros in
144 Finally, you should bracket the entire file in an include-guard, like
148 #ifndef _GLIBCPP_OS_DEFINES
149 #define _GLIBCPP_OS_DEFINES
154 We recommend copying an existing @file{bits/os_defines.h} to use as a
157 @c ---------------------------------------------------------------------
159 @c ---------------------------------------------------------------------
161 @node Character types
162 @chapter Character types
164 The library requires that you provide three header files to implement
165 character classification, analagous to that provided by the C libraries
166 @file{<ctype.h>} header. You can model these on the files provided in
167 @file{config/os/generic/bits}. However, these files will almost
168 certainly need some modification.
170 The first file to write is @file{bits/ctype_base.h}. This file provides
171 some very basic information about character classification. The libstdc++-v3
172 library assumes that your C library implements @file{<ctype.h>} by using
173 a table (indexed by character code) containing integers, where each of
174 these integers is a bit-mask indicating whether the charcter is
175 upper-case, lower-case, alphabetic, etc. The @file{bits/ctype_base.h}
176 file gives the type of the integer, and the values of the various bit
177 masks. You will have to peer at your own @file{<ctype.h>} to figure out
178 how to define the values required by this file.
180 The @file{bits/ctype_base.h} header file does not need include guards.
181 It should contain a single @code{struct} definition called
182 @code{ctype_base}. This @code{struct} should contain two type
183 declarations, and one enumeration declaration, like this example, taken
184 from the IRIX configuration:
189 typedef unsigned int mask;
190 typedef int* __to_type;
210 The @code{mask} type is the type of the elements in the table. If your
211 C library uses a table to map lower-case numbers to upper-case numbers,
212 and vice versa, you should define @code{__to_type} to be the type of the
213 elements in that table. If you don't mind taking a minor performance
214 penalty, or if your library doesn't implement @code{toupper} and
215 @code{tolower} in this way, you can pick any pointer-to-integer type,
216 but you must still define the type.
218 The enumeration should give definitions for all the values in the above
219 example, using the values from your native @file{<ctype.h>}. They can
220 be given symbolically (as above), or numerically, if you prefer. You do
221 not have to include @file{<ctype.h>} in this header; it will always be
222 included before @file{bits/ctype_base.h} is included.
224 The next file to write is @file{bits/ctype_noninline.h}, which also does
225 not require include guards. This file defines a few member functions
226 that will be included in @file{include/bits/locale_facets.h}. The first
227 function that must be written is the @code{ctype<char>::ctype}
228 constructor. Here is the IRIX example:
231 ctype<char>::ctype(const mask* __table = 0, bool __del = false,
233 : _Ctype_nois<char>(__refs), _M_del(__table != 0 && __del),
238 ? (const mask*) (__libc_attr._ctype_tbl->_class + 1)
244 There are two parts of this that you might choose to alter. The first,
245 and most important, is the line involving @code{__libc_attr}. That is
246 IRIX system-dependent code that gets the base of the table mapping
247 character codes to attributes. You need to substitute code that obtains
248 the address of this table on your system. If you want to use your
249 operating system's tables to map upper-case letters to lower-case, and
250 vice versa, you should initialize @code{_M_toupper} and
251 @code{_M_tolower} with those tables, in similar fashion.
253 Now, you have to write two functions to convert from upper-case to
254 lower-case, and vice versa. Here are the IRIX versions:
258 ctype<char>::do_toupper(char __c) const
259 @{ return _toupper(__c); @}
262 ctype<char>::do_tolower(char __c) const
263 @{ return _tolower(__c); @}
267 Your C library provides equivalents to IRIX's @code{_toupper} and
268 @code{_tolower}. If you initialized @code{_M_toupper} and
269 @code{_M_tolower} above, then you could use those tables instead.
271 Finally, you have to provide two utility functions that convert strings
272 of characters. The versions provided here will always work -- but you
273 could use specialized routines for greater performance if you have
274 machinery to do that on your system:
278 ctype<char>::do_toupper(char* __low, const char* __high) const
280 while (__low < __high)
282 *__low = do_toupper(*__low);
289 ctype<char>::do_tolower(char* __low, const char* __high) const
291 while (__low < __high)
293 *__low = do_tolower(*__low);
300 You must also provide the @file{bits/ctype_inline.h} file, which
301 contains a few more functions. On most systems, you can just copy
302 @file{config/os/generic/ctype_inline.h} and use it on your system.
304 In detail, the functions provided test characters for particular
305 properties; they are analagous to the functions like @code{isalpha} and
306 @code{islower} provided by the C library.
308 The first function is implemented like this on IRIX:
313 is(mask __m, char __c) const throw()
314 @{ return (_M_table)[(unsigned char)(__c)] & __m; @}
318 The @code{_M_table} is the table passed in above, in the constructor.
319 This is the table that contains the bitmasks for each character. The
320 implementation here should work on all systems.
322 The next function is:
327 is(const char* __low, const char* __high, mask* __vec) const throw()
329 while (__low < __high)
330 *__vec++ = (_M_table)[(unsigned char)(*__low++)];
336 This function is similar; it copies the masks for all the characters
337 from @code{__low} up until @code{__high} into the vector given by
340 The last two functions again are entirely generic:
345 scan_is(mask __m, const char* __low, const char* __high) const throw()
347 while (__low < __high && !this->is(__m, *__low))
354 scan_not(mask __m, const char* __low, const char* __high) const throw()
356 while (__low < __high && this->is(__m, *__low))
362 @c ---------------------------------------------------------------------
364 @c ---------------------------------------------------------------------
367 @chapter Thread safety
369 The C++ library string functionality requires a couple of atomic
370 operations to provide thread-safety. If you don't take any special
371 action, the library will use stub versions of these functions that are
372 not thread-safe. They will work fine, unless your applications are
375 If you want to provide custom, safe, versions of these functions, there
376 are two distinct approaches. One is to provide a version for your CPU,
377 using assembly language constructs. The other is to use the
378 thread-safety primitives in your operating system. In either case, you
379 make a file called @file{bits/atomicity.h}.
381 If you are using the assembly-language approach, put this code in
382 @file{config/cpu/<chip>/bits/atomicity.h}, where chip is the name of
383 your processor. In that case, edit the switch statement in
384 @file{configure.target} to set the @code{cpu_include_dir}. In either
385 case, set the switch statement that sets @code{ATOMICITYH} to be the
386 directory containing @file{bits/atomicity.h}.
388 With those bits out of the way, you have to actually write
389 @file{bits/atomicity.h} itself. This file should be wrapped in an
390 include guard named @code{_BITS_ATOMICITY_H}. It should define one
391 type, and two functions.
393 The type is @code{_Atomic_word}. Here is the version used on IRIX:
396 typedef long _Atomic_word;
400 This type must be a signed integral type supporting atomic operations.
401 If you're using the OS approach, use the same type used by your system's
402 primitives. Otherwise, use the type for which your CPU provides atomic
405 Then, you must provide two functions. The bodies of these functions
406 must be equivalent to those provided here, but using atomic operations:
409 static inline _Atomic_word
410 __attribute__ ((__unused__))
411 __exchange_and_add (_Atomic_word* __mem, int __val)
413 _Atomic_word __result = *__mem;
419 __attribute__ ((__unused__))
420 __atomic_add (_Atomic_word* __mem, int __val)
426 @c ---------------------------------------------------------------------
428 @c ---------------------------------------------------------------------
433 The C++ library is compiled, archived and linked with libtool.
434 Explaining the full workings of libtool is beyond the scope of this
435 document, but there are a few, particular bits that are necessary for
438 Some parts of the libstdc++-v3 library are compiled with the libtool
439 @code{--tags CXX} option (the C++ definitions for libtool). Therefore,
440 @file{ltcf-cxx.sh} in the top-level directory needs to have the correct
441 logic to compile and archive objects equivalent to the C version of libtool,
442 @file{ltcf-c.sh}. Some libtool targets have definitions for C but not
443 for C++, or C++ definitions which have not been kept up to date.
445 The C++ run-time library contains initialization code that needs to be
446 run as the library is loaded. Often, that requires linking in special
447 object files when the C++ library is built as a shared library, or
448 taking other system-specific actions.
450 The libstdc++-v3 library is linked with the C version of libtool, even though it
451 is a C++ library. Therefore, the C version of libtool needs to ensure
452 that the run-time library initializers are run. The usual way to do
453 this is to build the library using @code{gcc -shared}.
455 If you need to change how the library is linked, look at
456 @file{ltcf-c.sh} in the top-level directory. Find the switch statement
457 that sets @code{archive_cmds}. Here, adjust the setting for your
460 @c ---------------------------------------------------------------------
462 @c ---------------------------------------------------------------------
466 @c ---------------------------------------------------------------------
468 @c ---------------------------------------------------------------------