DUI0378E Libraries User Guide
DUI0378E Libraries User Guide
04 for µVision
Version 5
Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved.
ARM DUI0378E
ARM® Compiler v5.04 for µVision
Document History
A May 2007 Non-Confidential Release for RVCT v3.1 Release for µVision
B December 2008 Non-Confidential Release for RVCT v4.0 for µVision
C June 2011 Non-Confidential Release for ARM Compiler v4.1 for µVision
D July 2012 Non-Confidential Release for ARM Compiler v5.02 for µVision
E 30 May 2014 Non-Confidential Release for ARM Compiler v5.04 for µVision
Your access to the information in this document is conditional upon your acceptance that you will not use or permit others to use
the information for the purposes of determining whether implementations infringe any third party patents.
THIS DOCUMENT IS PROVIDED “AS IS”. ARM PROVIDES NO REPRESENTATIONS AND NO WARRANTIES,
EXPRESS, IMPLIED OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
MERCHANTABILITY, SATISFACTORY QUALITY, NON-INFRINGEMENT OR FITNESS FOR A PARTICULAR
PURPOSE WITH RESPECT TO THE DOCUMENT. For the avoidance of doubt, ARM makes no representation with respect to,
and has undertaken no analysis to identify or understand the scope and content of, third party patents, copyrights, trade secrets, or
other rights.
TO THE EXTENT NOT PROHIBITED BY LAW, IN NO EVENT WILL ARM BE LIABLE FOR ANY DAMAGES,
INCLUDING WITHOUT LIMITATION ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE, OR
CONSEQUENTIAL DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING
OUT OF ANY USE OF THIS DOCUMENT, EVEN IF ARM HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
DAMAGES.
This document consists solely of commercial items. You shall be responsible for ensuring that any use, duplication or disclosure of
this document complies fully with any relevant export laws and regulations to assure that this document or any portion thereof is
not exported, directly or indirectly, in violation of such export laws. Use of the word “partner” in reference to ARM’s customers is
not intended to create or refer to any partnership relationship with any other company. ARM may make changes to this document at
any time and without notice.
If any of the provisions contained in these terms conflict with any of the provisions of any signed written agreement covering this
document with ARM, then the signed written agreement prevails over and supersedes the conflicting provisions of these terms.
This document may be translated into other languages for convenience, and you agree that if there is any conflict between the
English version of this document and any translation, the terms of the English version of the Agreement shall prevail.
Words and logos marked with ® or ™ are registered trademarks or trademarks of ARM Limited or its affiliates in the EU and/or
elsewhere. All rights reserved. Other brands and names mentioned in this document may be the trademarks of their respective
owners. Please follow ARM’s trademark usage guidelines at http://www.arm.com/about/trademark-usage-guidelines.php
Copyright © [2007, 2008, 2011, 2012, 2014], ARM Limited or its affiliates. All rights reserved.
LES-PRE-20349
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 2
Non-Confidential
ARM® Compiler v5.04 for µVision
Additional Notices
Some material in this document is based on IEEE 754-1985 IEEE Standard for Binary Floating-Point Arithmetic. The IEEE
disclaims any responsibility or liability resulting from the placement and use in the described manner.
Confidentiality Status
This document is Non-Confidential. The right to use, copy and disclose this document may be subject to license restrictions in
accordance with the terms of the agreement entered into by ARM and the party that ARM delivered this document to.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 3
Non-Confidential
Contents
ARM® Compiler v5.04 for µVision ARM C and C++
Libraries and Floating-Point Support User Guide
Preface
About this book ..................................................... ..................................................... 14
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4
Non-Confidential
1.17 Re-implementation of legacy function __user_libspace() in the C library ................ 1-36
1.18 Management of locks in multithreaded applications ................................................ 1-37
1.19 How to ensure re-implemented mutex functions are called .................. .................. 1-39
1.20 Using the ARM C library in a multithreaded environment ........................................ 1-40
1.21 Thread safety in the ARM C library .................................... .................................... 1-42
1.22 Thread safety in the ARM C++ library .................................. .................................. 1-43
1.23 The floating-point status word in a multithreaded environment ............... ............... 1-45
1.24 Using the C library with an application .................................. .................................. 1-46
1.25 Using the C and C++ libraries with an application in a semihosting environment . . 1-47
1.26 Using $Sub$$ to mix semihosted and nonsemihosted I/O functionality .................. 1-48
1.27 Using the libraries in a nonsemihosting environment .............................................. 1-49
1.28 C++ exceptions in a non-semihosting environment ........................ ........................ 1-51
1.29 Direct semihosting C library function dependencies ................................................ 1-52
1.30 Indirect semihosting C library function dependencies ...................... ...................... 1-53
1.31 C library API definitions for targeting a different environment ................ ................ 1-54
1.32 Building an application without the C library ............................................................ 1-55
1.33 Creating an application as bare machine C without the C library ............................ 1-58
1.34 Integer and floating-point compiler functions and building an application without the C
library ........................................................... ........................................................... 1-59
1.35 Bare machine integer C ............................................. ............................................. 1-60
1.36 Bare machine C with floating-point processing ........................... ........................... 1-61
1.37 Customized C library startup code and access to C library functions .......... .......... 1-62
1.38 Using low-level functions when exploiting the C library ..................... ..................... 1-63
1.39 Using high-level functions when exploiting the C library .................... .................... 1-64
1.40 Using malloc() when exploiting the C library ............................. ............................. 1-65
1.41 Tailoring the C library to a new execution environment ..................... ..................... 1-66
1.42 Initialization of the execution environment and execution of the application ..... ..... 1-67
1.43 C++ initialization, construction and destruction ........................... ........................... 1-68
1.44 Legacy support for C$$pi_ctorvec instead of .init_array .................... .................... 1-69
1.45 Exceptions system initialization ....................................... ....................................... 1-70
1.46 Emergency buffer memory for exceptions ............................... ............................... 1-71
1.47 Library functions called from main() .................................... .................................... 1-72
1.48 Program exit and the assert macro .................................... .................................... 1-73
1.49 Assembler macros that tailor locale functions in the C library ................ ................ 1-74
1.50 Link time selection of the locale subsystem in the C library .................................... 1-75
1.51 ISO8859-1 implementation ...................................................................................... 1-76
1.52 Shift-JIS and UTF-8 implementation ................................... ................................... 1-77
1.53 Runtime selection of the locale subsystem in the C library .................. .................. 1-78
1.54 Definition of locale data blocks in the C library ........................................................ 1-79
1.55 LC_CTYPE data block .............................................. .............................................. 1-81
1.56 LC_COLLATE data block ........................................................................................ 1-84
1.57 LC_MONETARY data block .................................................................................... 1-85
1.58 LC_NUMERIC data block ........................................................................................ 1-86
1.59 LC_TIME data block ................................................................................................ 1-87
1.60 Modification of C library functions for error signaling, error handling, and program exit ..
.................................................................................................................................. 1-89
1.61 Avoiding the heap and heap-using library functions supplied by ARM .................... 1-90
1.62 C library support for memory allocation functions ......................... ......................... 1-91
1.63 Heap1, standard heap implementation .................................................................... 1-92
1.64 Heap2, alternative heap implementation ................................ ................................ 1-93
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 5
Non-Confidential
1.65 Using a heap implementation from bare machine C ....................... ....................... 1-94
1.66 Stack pointer initialization and heap bounds ............................. ............................. 1-95
1.67 Defining __initial_sp, __heap_base and __heap_limit ............................................ 1-97
1.68 Extending heap size at runtime ....................................... ....................................... 1-98
1.69 Legacy support for __user_initial_stackheap() ........................................................ 1-99
1.70 Tailoring input/output functions in the C and C++ libraries .................................... 1-100
1.71 Target dependencies on low-level functions in the C and C++ libraries ................ 1-101
1.72 The C library printf family of functions ................................. ................................. 1-103
1.73 The C library scanf family of functions ................................. ................................. 1-104
1.74 Redefining low-level library functions to enable direct use of high-level library functions
in the C library ................................................... ................................................... 1-105
1.75 The C library functions fread(), fgets() and gets() .................................................. 1-107
1.76 Re-implementing __backspace() in the C library ......................... ......................... 1-108
1.77 Re-implementing __backspacewc() in the C library .............................................. 1-109
1.78 Redefining target-dependent system I/O functions in the C library ........... ........... 1-110
1.79 Tailoring non-input/output C library functions ........................................................ 1-111
1.80 Real-time integer division in the ARM libraries ...................................................... 1-112
1.81 Selecting real-time division in the ARM libraries ......................... ......................... 1-113
1.82 How the ARM C library fulfills ISO C specification requirements ............. ............. 1-114
1.83 mathlib error handling ............................................................................................ 1-115
1.84 ISO-compliant implementation of signals supported by the signal() function in the C
library and additional type arguments .................................................................... 1-117
1.85 ISO-compliant C library input/output characteristics .............................................. 1-119
1.86 Standard C++ library implementation definition .......................... .......................... 1-121
1.87 C library functions and extensions .................................... .................................... 1-122
1.88 Persistence of C and C++ library names across releases of the ARM compilation
tools ........................................................... ........................................................... 1-123
1.89 Link time selection of C and C++ libraries .............................. .............................. 1-124
1.90 Managing projects that have explicit C or C++ library names in makefiles ..... ..... 1-125
1.91 Compiler generated and library-resident helper functions .................. .................. 1-126
1.92 C and C++ library naming conventions ................................ ................................ 1-127
1.93 Using macro__ARM_WCHAR_NO_IO to disable FILE declaration and wide I/O
function prototypes ................................................................................................ 1-130
1.94 Using library functions with execute-only memory ........................ ........................ 1-131
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 6
Non-Confidential
3.4 fplib arithmetic on numbers in a particular format .................................................. 3-149
3.5 fplib conversions between floats, long longs, doubles, and ints ............................ 3-151
3.6 fplib comparisons between floats and doubles ...................................................... 3-153
3.7 fplib C99 functions ................................................ ................................................ 3-155
3.8 Controlling the ARM floating-point environment .................................................... 3-156
3.9 Floating-point functions for compatibility with Microsoft products .......................... 3-157
3.10 C99-compatible functions for controlling the ARM floating-point environment ...... 3-158
3.11 C99 rounding mode and floating-point exception macros .................. .................. 3-159
3.12 Exception flag handling ............................................ ............................................ 3-160
3.13 Functions for handling rounding modes ................................ ................................ 3-161
3.14 Functions for saving and restoring the whole floating-point environment .............. 3-162
3.15 Functions for temporarily disabling exceptions ...................................................... 3-163
3.16 ARM floating-point compiler extensions to the C99 interface ................................ 3-164
3.17 Writing a custom exception trap handler ............................... ............................... 3-165
3.18 Example of a custom exception handler ................................................................ 3-170
3.19 Exception trap handling by signals ........................................................................ 3-172
3.20 Using C99 signalling NaNs provided by mathlib (_WANT_SNAN) ........................ 3-173
3.21 mathlib double and single-precision floating-point functions ................ ................ 3-174
3.22 IEEE 754 arithmetic ............................................... ............................................... 3-175
3.23 Basic data types for IEEE 754 arithmetic .............................................................. 3-176
3.24 Single precision data type for IEEE 754 arithmetic ....................... ....................... 3-177
3.25 Double precision data type for IEEE 754 arithmetic .............................................. 3-179
3.26 Sample single precision floating-point values for IEEE 754 arithmetic .................. 3-180
3.27 Sample double precision floating-point values for IEEE 754 arithmetic ................ 3-182
3.28 IEEE 754 arithmetic and rounding .................................... .................................... 3-184
3.29 Exceptions arising from IEEE 754 floating-point arithmetic ................. ................. 3-185
3.30 Exception types recognized by the ARM floating-point environment .......... .......... 3-186
3.31 Using the Vector Floating-Point (VFP) support libraries ........................................ 3-188
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 7
Non-Confidential
4.19 __raise() ........................................................ ........................................................ 4-210
4.20 _rand_r() ................................................................................................................ 4-211
4.21 remove() ................................................................................................................ 4-212
4.22 rename() ................................................................................................................ 4-213
4.23 __rt_entry ....................................................... ....................................................... 4-214
4.24 __rt_errno_addr() ................................................. ................................................. 4-215
4.25 __rt_exit() ....................................................... ....................................................... 4-216
4.26 __rt_fp_status_addr() ............................................................................................ 4-217
4.27 __rt_heap_extend() ............................................... ............................................... 4-218
4.28 __rt_lib_init() .......................................................................................................... 4-219
4.29 __rt_lib_shutdown() ............................................... ............................................... 4-220
4.30 __rt_raise() ............................................................................................................ 4-221
4.31 __rt_stackheap_init() .............................................. .............................................. 4-222
4.32 setlocale() .............................................................................................................. 4-223
4.33 _srand_r() .............................................................................................................. 4-225
4.34 strcasecmp() .......................................................................................................... 4-226
4.35 strncasecmp() ........................................................................................................ 4-227
4.36 strlcat() ......................................................... ......................................................... 4-228
4.37 strlcpy() .................................................................................................................. 4-229
4.38 _sys_close() ..................................................... ..................................................... 4-230
4.39 _sys_command_string() ........................................................................................ 4-231
4.40 _sys_ensure() ........................................................................................................ 4-232
4.41 _sys_exit() ...................................................... ...................................................... 4-233
4.42 _sys_flen() ...................................................... ...................................................... 4-234
4.43 _sys_istty() ...................................................... ...................................................... 4-235
4.44 _sys_open() ..................................................... ..................................................... 4-236
4.45 _sys_read() ............................................................................................................ 4-237
4.46 _sys_seek() ..................................................... ..................................................... 4-238
4.47 _sys_tmpnam() ...................................................................................................... 4-239
4.48 _sys_write() ..................................................... ..................................................... 4-240
4.49 system() ........................................................ ........................................................ 4-241
4.50 time() .......................................................... .......................................................... 4-242
4.51 _ttywrch() ....................................................... ....................................................... 4-243
4.52 __user_heap_extend() ............................................. ............................................. 4-244
4.53 __user_heap_extent() ............................................. ............................................. 4-245
4.54 __user_setup_stackheap() .................................................................................... 4-246
4.55 __vectab_stack_and_reset .................................................................................... 4-247
4.56 wcscasecmp() ........................................................................................................ 4-248
4.57 wcsncasecmp() ...................................................................................................... 4-249
4.58 wcstombs() ............................................................................................................ 4-250
4.59 Thread-safe C library functions ...................................... ...................................... 4-251
4.60 C library functions that are not thread-safe ............................. ............................. 4-253
4.61 Legacy function __user_initial_stackheap() ............................. ............................. 4-255
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 8
Non-Confidential
5.6 j0(), j1(), jn(), Bessel functions of the first kind ........................... ........................... 5-266
5.7 significand(), fractional part of a number ............................... ............................... 5-267
5.8 _statusfp() .............................................................................................................. 5-268
5.9 y0(), y1(), yn(), Bessel functions of the second kind .............................................. 5-269
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 9
Non-Confidential
List of Figures
ARM® Compiler v5.04 for µVision ARM C and C++
Libraries and Floating-Point Support User Guide
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 10
Non-Confidential
List of Tables
ARM® Compiler v5.04 for µVision ARM C and C++
Libraries and Floating-Point Support User Guide
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 11
Non-Confidential
Table 3-9 Sample single-precision floating-point values ..................................................................... 3-180
Table 3-10 Sample double-precision floating-point values .................................................................... 3-182
Table 4-1 Functions that are thread-safe ............................................................................................. 4-251
Table 4-2 Functions that are not thread-safe ....................................................................................... 4-253
Table 5-1 _controlfp argument macros ............................................................................................... 5-258
Table 5-2 Status word bit modification ................................................................................................. 5-263
Table 5-3 Rounding mode control ....................................................................................................... 5-264
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 12
Non-Confidential
Preface
This preface introduces the ARM® Compiler v5.04 for µVision ARM C and C++ Libraries and Floating-
Point Support User Guide.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 13
Non-Confidential
Preface
About this book
Glossary
The ARM Glossary is a list of terms used in ARM documentation, together with definitions for those
terms. The ARM Glossary does not contain terms that are industry standard unless the ARM meaning
differs from the generally accepted meaning.
See the ARM Glossary for more information.
Typographic conventions
italic
Introduces special terminology, denotes cross-references, and citations.
bold
Highlights interface elements, such as menu names. Denotes signal names. Also used for terms
in descriptive lists, where appropriate.
monospace
Denotes text that you can enter at the keyboard, such as commands, file and program names,
and source code.
monospace
Denotes a permitted abbreviation for a command or option. You can enter the underlined text
instead of the full command or option name.
monospace italic
Denotes arguments to monospace text where the argument is to be replaced by a specific value.
monospace bold
Denotes language keywords when used outside example code.
<and>
Encloses replaceable terms for assembler syntax where they appear in code or code fragments.
For example:
MRC p15, 0 <Rd>, <CRn>, <CRm>, <Opcode_2>
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 14
Non-Confidential
Preface
About this book
SMALL CAPITALS
Used in body text for a few terms that have specific technical meanings, that are defined in the
ARM glossary. For example, IMPLEMENTATION DEFINED, IMPLEMENTATION SPECIFIC, UNKNOWN, and
UNPREDICTABLE.
Feedback
Feedback on content
If you have comments on content then send an e-mail to [email protected]. Give:
• The title.
• The number ARM DUI0378E.
• The page number(s) to which your comments refer.
• A concise explanation of your comments.
ARM also welcomes general suggestions for additions and improvements.
Other information
• ARM Information Center.
• ARM Technical Support Knowledge Articles.
• Support and Maintenance.
• ARM Glossary.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 15
Non-Confidential
Chapter 1
The ARM C and C++ Libraries
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-16
Non-Confidential
1 The ARM C and C++ Libraries
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-17
Non-Confidential
1 The ARM C and C++ Libraries
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-18
Non-Confidential
1 The ARM C and C++ Libraries
1.1 Mandatory linkage with the C library
Related concepts
1.2 C and C++ runtime libraries on page 1-20.
1.27 Using the libraries in a nonsemihosting environment on page 1-49.
Related references
1.32 Building an application without the C library on page 1-55.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-19
Non-Confidential
1 The ARM C and C++ Libraries
1.2 C and C++ runtime libraries
Related concepts
1.1 Mandatory linkage with the C library on page 1-19.
Related references
1.32 Building an application without the C library on page 1-55.
1 The ARM C and C++ Libraries on page 1-16.
2 The ARM C Micro-library on page 2-132.
Related information
ISO C library standard.
IEEE Standard for Floating-Point Arithmetic (IEEE 754), 1985 version.
What is Semihosting?.
Rogue Wave documentation.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-20
Non-Confidential
1 The ARM C and C++ Libraries
1.3 C and C++ library features
Related information
What is Semihosting?.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-21
Non-Confidential
1 The ARM C and C++ Libraries
1.4 Library heap usage requirements of the ARM C and C++ libraries
1.4 Library heap usage requirements of the ARM C and C++ libraries
Functions such as malloc() and other dynamic memory allocation functions explicitly allocate memory
when used. However, some library functions and mechanisms implicitly allocate memory from the heap.
If heap usage requirements are significant to your code development (for example, you might be
developing code for an embedded system with a tiny memory footprint), you must be aware of both
implicit and explicit heap requirements.
In C standardlib, implicit heap usage occurs as a result of:
• Calling the library function fopen() and the first time that an I/O operation is applied to the resulting
stream.
• Passing command-line arguments into the main() function.
The size of heap memory allocated for fopen() is 80 bytes for the FILE structure. When the first I/O
operation occurs, and not until the operation occurs, an additional default of 512 bytes of heap memory is
allocated for a buffer associated with the operation. You can reconfigure the size of this buffer using
setvbuf().
When fclose() is called, the default 80 bytes of memory is kept on a freelist for possible re-use. The
512-byte buffer is freed on fclose().
Declaring main() to take arguments requires 256 bytes of implicitly allocated memory from the heap.
This memory is never freed because it is required for the duration of main(). In microlib, main() must
not be declared to take arguments, so this heap usage requirement only applies to standardlib. In the
standardlib context, it only applies if you have a heap.
Note
The memory sizes quoted might change in future releases.
Related concepts
2.3 Library heap usage requirements of the ARM C micro-library on page 2-135.
1.63 Heap1, standard heap implementation on page 1-92.
1.64 Heap2, alternative heap implementation on page 1-93.
1.65 Using a heap implementation from bare machine C on page 1-94.
Related references
1.61 Avoiding the heap and heap-using library functions supplied by ARM on page 1-90.
1.62 C library support for memory allocation functions on page 1-91.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-22
Non-Confidential
1 The ARM C and C++ Libraries
1.5 Compliance with the Application Binary Interface (ABI) for the ARM architecture
1.5 Compliance with the Application Binary Interface (ABI) for the ARM
architecture
The ABI for the ARM Architecture is a family of specifications that describes the processor-specific
aspects of the translation of a source program into object files.
Object files produced by any toolchain that conforms to the relevant aspects of the ABI can be linked
together to produce a final executable image or library.
Each document in the specification covers a specific area of compatibility. For example, the C Library
ABI for the ARM Architecture (CLIBABI) describes the parts of the C library that are expected to be
common to all conforming implementations.
The ABI documents contain several areas that are marked as platform specific. To define a complete
execution environment these platform-specific details have to be provided. This gives rise to a number of
supplemental specifications, for example the ARM GNU/Linux ABI supplement.
The Base Standard ABI for the ARM Architecture (BSABI) enables you to use ARM and Thumb® objects
and libraries from different producers that support the ABI for the ARM Architecture. The ARM
compilation tools fully support the BSABI, including support for Debug With Arbitrary Record Format
(DWARF) 3 debug tables (DWARF Debugging Standard Version 3).
The ARM C and C++ libraries conform to the standards described in the BSABI, the CLIBABI, and the
C++ ABI (CPPABI) for the ARM Architecture.
Related tasks
1.6 Increasing portability of object files to other CLIBABI implementations on page 1-24.
Related information
Application Binary Interface (ABI) for the ARM Architecture.
DWARF Debugging Standard.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-23
Non-Confidential
1 The ARM C and C++ Libraries
1.6 Increasing portability of object files to other CLIBABI implementations
Note
This reduces the performance of some library operations.
There are a number of methods you can use to request full CLIBABI portability.
Procedure
• Specify #define _AEABI_PORTABILITY_LEVEL 1 before you #include any library headers, such as
<stdlib.h>.
• Specify -D_AEABI_PORTABILITY_LEVEL=1 on the compiler command line.
Related concepts
1.5 Compliance with the Application Binary Interface (ABI) for the ARM architecture on page 1-23.
Related information
Application Binary Interface (ABI) for the ARM Architecture.
-Dname[(parm-list)][=def] compiler option.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-24
Non-Confidential
1 The ARM C and C++ Libraries
1.7 ARM C and C++ library directory structure
The environment variable ARMCC5LIB must be set to point to the lib directory, or if this variable is not
set, ARMLIB.
Alternatively, use the --libpath argument to the linker to identify the directory holding the library
subdirectories. You must not identify the armlib and cpplib directories separately because this
directory structure might change in future releases. The linker finds them from the location of lib.
Note
• The ARM C libraries are supplied in binary form only.
• The ARM libraries must not be modified. If you want to create a new implementation of a library
function, place the new function in an object file, or your own library, and include it when you link
the application. Your version of the function is used instead of the standard library version.
• Normally, only a few functions in the ISO C library require re-implementation to create a target-
dependent application.
• The source for the Rogue Wave Standard C++ Library is not freely distributable. It can be obtained
from Rogue Wave Software Inc., or through ARM, for an additional license fee.
Related information
Rogue Wave documentation.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-25
Non-Confidential
1 The ARM C and C++ Libraries
1.8 Selection of ARM C and C++ library variants based on build options
1.8 Selection of ARM C and C++ library variants based on build options
When you build your application, you must make certain choices such as the target architecture,
instruction set, and byte order. You communicate these choices to the compiler using build options. The
linker then selects appropriate C and C++ library variants compatible with these build options.
Choices that influence the ARM C and C++ library variant include the following:
Target Architecture and instruction set
ARM or Thumb instruction sets.
Byte order
Big-endian or little-endian.
Floating-point support
Software (SoftVFP), hardware (VFP), software or hardware with half-precision or double-
precision extensions, or no floating-point support.
Position independence
Different ways to access your data are as follows:
• By absolute address.
• Relative to sb (read/write position-independent).
• Relative to pc (fpic).
Different ways to access your code are as follows:
• By absolute address when appropriate.
• Relative to pc (read-only position independent).
The standard C libraries provide variants to support all of these options.
Position-independent C++ code can only be achieved with --apcs=/fpic.
Note
Position independence is not supported in microlib.
When you link your assembler code, C or C++ code, the linker selects appropriate C and C++ library
variants compatible with the build options you specified. There is a variant of the ISO C library for each
combination of major build options.
Related information
Code compatibility between separately compiled and assembled modules.
--apcs=qualifier...qualifier compiler option.
--arm compiler option.
--bigend compiler option.
--fpu=name compiler option.
--littleend compiler option.
--thumb compiler option.
--fpu=name linker option.
--ropi linker option.
--rwpi linker option.
--arm assembler option.
--bigend assembler option.
--fpu assembler option.
--littleend assembler option.
--thumb assembler option.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-26
Non-Confidential
1 The ARM C and C++ Libraries
1.9 Thumb C libraries
Note
• The Thumb C library used for ARMv6-M targets contains only 16-bit Thumb code.
• The Thumb C library used for ARMv7-M targets contains both 16-bit and 32-bit Thumb code.
Related information
Cortex-A series processors.
Cortex-R series processors.
Cortex-M series processors.
--arm compiler option.
--thumb compiler option.
--arm_only compiler option.
#pragma thumb.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-27
Non-Confidential
1 The ARM C and C++ Libraries
1.10 C++ and C libraries and the std namespace
This means that you must qualify all the library names using one of the following methods:
• Specify the standard namespace, for example:
std::printf("example\n");
• Use the C++ keyword using to import a name to the global namespace:
using namespace std;
printf("example\n");
• Use the compiler option --using_std.
Note
errno is a macro, so it is not necessary to qualify it with a namespace.
Related information
--using_std, --no_using_std compiler option.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-28
Non-Confidential
1 The ARM C and C++ Libraries
1.11 ARM C libraries and multithreading
Related concepts
1.12 ARM C libraries and reentrant functions on page 1-30.
1.13 ARM C libraries and thread-safe functions on page 1-31.
Related references
1.20 Using the ARM C library in a multithreaded environment on page 1-40.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-29
Non-Confidential
1 The ARM C and C++ Libraries
1.12 ARM C libraries and reentrant functions
Note
A reentrant function must not call non-reentrant functions.
Related concepts
1.13 ARM C libraries and thread-safe functions on page 1-31.
1.11 ARM C libraries and multithreading on page 1-29.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-30
Non-Confidential
1 The ARM C and C++ Libraries
1.13 ARM C libraries and thread-safe functions
Related concepts
1.12 ARM C libraries and reentrant functions on page 1-30.
1.11 ARM C libraries and multithreading on page 1-29.
1.11 ARM C libraries and multithreading on page 1-29.
1.12 ARM C libraries and reentrant functions on page 1-30.
1.21 Thread safety in the ARM C library on page 1-42.
1.22 Thread safety in the ARM C++ library on page 1-43.
Related references
1.18 Management of locks in multithreaded applications on page 1-37.
1.20 Using the ARM C library in a multithreaded environment on page 1-40.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-31
Non-Confidential
1 The ARM C and C++ Libraries
1.14 Use of static data in the C libraries
Note
Exactly which functions use static data in their definitions might change in future releases.
Callouts from the C library enable access to static data. C library functions that use static data can be
categorized as:
• Functions that do not use any static data of any kind, for example fprintf().
• Functions that manage a static state, such as malloc(), rand(), and strtok().
• Functions that do not manage a static state, but use static data in a way that is specific to the
implementation in the ARM compiler, for example isalpha().
When the C library does something that requires implicit static data, it uses a callout to a function you
can replace. These functions are shown in the following table. They do not use semihosting.
Function Description
__rt_errno_addr() Called to get the address of the variable errno
__rt_fp_status_addr() Called by the floating-point support code to get the address of the floating-point status word
locale functions The function __user_libspace() creates a block of private static data for the library
The default implementation of __user_libspace creates a 96-byte block in the ZI region. Even if your
application does not have a main() function, the __user_libspace() function does not normally have
to be redefined.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-32
Non-Confidential
1 The ARM C and C++ Libraries
1.14 Use of static data in the C libraries
Note
Exactly which functions use static data in their definitions might change in future releases.
Related concepts
1.17 Re-implementation of legacy function __user_libspace() in the C library on page 1-36.
1.49 Assembler macros that tailor locale functions in the C library on page 1-74.
1.11 ARM C libraries and multithreading on page 1-29.
Related references
4.24 __rt_errno_addr() on page 4-215.
4.26 __rt_fp_status_addr() on page 4-217.
Related information
Code compatibility between separately compiled and assembled modules.
--apcs=qualifier...qualifier compiler option.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-33
Non-Confidential
1 The ARM C and C++ Libraries
1.15 Use of the __user_libspace static data area by the C libraries
The __user_libspace static data area holds the static data for the C libraries. This is a block of 96 bytes
of zero-initialized data, supplied by the C library. It is also used as a temporary stack during C library
initialization.
The default ARM C libraries use the __user_libspace area to hold:
• errno, used by any function that is capable of setting errno. By default, __rt_errno_addr() returns
a pointer to errno.
• The Floating-Point (FP) status word for software floating-point (exception flags, rounding mode). It
is unused in hardware floating-point. By default, __rt_fp_status_addr() returns a pointer to the FP
status word.
• A pointer to the base of the heap (that is, the __Heap_Descriptor), used by all the malloc-related
functions.
• The current locale settings, used by functions such as setlocale(), but also used by all other library
functions that depend on them. For example, the ctype.h functions have to access the LC_CTYPE
setting.
The C++ libraries use the __user_libspace area to hold:
• The new_handler field and ddtor_pointer:
— The new_handler field keeps track of the value passed to std::set_new_handler().
— The ddtor_pointer, that points to a list of destructions to be performed on program exit. For
example, objects constructed outside function scope exist for the duration of the program, but
require destruction on program exit. The ddtor_pointer is used by __cxa_atexit() and
__aeabi_atexit().
• C++ exception handling information for functions such as std::set_terminate() and
std::set_unexpected().
Note
How the C and C++ libraries use the __user_libspace area might change in future releases.
Related concepts
1.16 C library functions to access subsections of the __user_libspace static data area on page 1-35.
Related information
__aeabi_atexit() in C++ ABI for the ARM Architecture.
std::set_terminate(), std::set_unexpected(), in Exception Handling ABI for the ARM Architecture.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-34
Non-Confidential
1 The ARM C and C++ Libraries
1.16 C library functions to access subsections of the __user_libspace static data area
Related concepts
1.17 Re-implementation of legacy function __user_libspace() in the C library on page 1-36.
Related references
1.15 Use of the __user_libspace static data area by the C libraries on page 1-34.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-35
Non-Confidential
1 The ARM C and C++ Libraries
1.17 Re-implementation of legacy function __user_libspace() in the C library
Related concepts
1.16 C library functions to access subsections of the __user_libspace static data area on page 1-35.
1.16 C library functions to access subsections of the __user_libspace static data area on page 1-35.
1.14 Use of static data in the C libraries on page 1-32.
Related references
1.20 Using the ARM C library in a multithreaded environment on page 1-40.
1.15 Use of the __user_libspace static data area by the C libraries on page 1-34.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-36
Non-Confidential
1 The ARM C and C++ Libraries
1.18 Management of locks in multithreaded applications
This function accepts a pointer to a 32-bit word and initializes it as a valid mutex.
By default, _mutex_initialize() returns zero for a nonthreaded application. Therefore, in a
multithreaded application, _mutex_initialize() must return a nonzero value on success so
that at runtime, the library knows that it is being used in a multithreaded environment.
Ensure that _mutex_initialize() initializes the mutex to an unlocked state.
This function must be supplied if you are using mutexes.
_mutex_acquire()
void _mutex_acquire(mutex *m);
This function causes the calling thread to obtain a lock on the supplied mutex.
_mutex_acquire() returns immediately if the mutex has no owner. If the mutex is owned by
another thread, _mutex_acquire() must block it until it becomes available.
_mutex_acquire() is not called by the thread that already owns the mutex.
This function causes the calling thread to release the lock on a mutex acquired by
_mutex_acquire().
This function causes the calling thread to free the supplied mutex. Any operating system
resources associated with the mutex are freed. The mutex is destroyed and cannot be reused.
_mutex_free() assumes that the mutex is owned by the calling thread.
This function is optional. If you do not supply this function, the C library does not attempt to
call it.
The mutex data structure type that is used as the parameter to the _mutex_*() functions is not defined
in any of the ARM compiler toolchain header files, but must be defined elsewhere. Typically, it is
defined as part of RTOS code.
Functions that call _mutex_*() functions create 4 bytes of memory for holding the mutex data structure.
__Heap_Initialize() is one such function.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-37
Non-Confidential
1 The ARM C and C++ Libraries
1.18 Management of locks in multithreaded applications
For the C library, a mutex is specified as a single 32-bit word of memory that can be placed anywhere.
However, if your mutex implementation requires more space than this, or demands that the mutex be in a
special memory area, then you must treat the default mutex as a pointer to a real mutex.
A typical example of a re-implementation framework for _mutex_initialize(), _mutex_acquire(),
and _mutex_release() is as follows, where SEMAPHORE_ID, CreateLock(), AcquireLock(), and
ReleaseLock() are defined in the RTOS, and (...) implies additional parameters:
Note
• _mutex_release() releases the lock on the mutex that was acquired by _mutex_acquire(), but the
mutex still exists, and can be re-locked by a subsequent call to _mutex_acquire().
• It is only when the optional wrapper function _mutex_free() is called that the mutex is destroyed.
After the mutex is destroyed, it cannot be used without first calling _mutex_initialize() to set it
up again.
Related concepts
1.19 How to ensure re-implemented mutex functions are called on page 1-39.
1.21 Thread safety in the ARM C library on page 1-42.
1.22 Thread safety in the ARM C++ library on page 1-43.
Related references
1.20 Using the ARM C library in a multithreaded environment on page 1-40.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-38
Non-Confidential
1 The ARM C and C++ Libraries
1.19 How to ensure re-implemented mutex functions are called
Related concepts
1.21 Thread safety in the ARM C library on page 1-42.
1.22 Thread safety in the ARM C++ library on page 1-43.
Related references
1.20 Using the ARM C library in a multithreaded environment on page 1-40.
1.18 Management of locks in multithreaded applications on page 1-37.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-39
Non-Confidential
1 The ARM C and C++ Libraries
1.20 Using the ARM C library in a multithreaded environment
Note
The ARM C libraries supply no thread management functions of their own so you must supply any
that are required.
• A thread-switching mechanism.
Note
The ARM C libraries supply no thread-switching mechanisms of their own. This is because there are
many different ways to do this and the libraries are designed to work with all of them.
You only have to provide implementations of the mutex functions if you require them to be called.
In some applications, the mutex functions might not be useful. For example, a co-operatively threaded
program does not have to take steps to ensure data integrity, provided it avoids calling its yield function
during a critical section. However, in other types of application, for example where you are
implementing preemptive scheduling, or in a Symmetric Multi-Processor (SMP) model, these functions
play an important part in handling locks.
If all of these requirements are met, you can use the ARM C library in your multithreaded environment.
The following behavior applies:
• Some functions work independently in each thread.
• Some functions automatically use the mutex functions to mediate multiple accesses to a shared
resource.
• Some functions are still nonreentrant so a reentrant equivalent is supplied.
• A few functions remain nonreentrant and no alternative is available.
Related concepts
1.11 ARM C libraries and multithreading on page 1-29.
1.19 How to ensure re-implemented mutex functions are called on page 1-39.
1.21 Thread safety in the ARM C library on page 1-42.
1.22 Thread safety in the ARM C++ library on page 1-43.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-40
Non-Confidential
1 The ARM C and C++ Libraries
1.20 Using the ARM C library in a multithreaded environment
Related references
1.18 Management of locks in multithreaded applications on page 1-37.
4.59 Thread-safe C library functions on page 4-251.
4.60 C library functions that are not thread-safe on page 4-253.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-41
Non-Confidential
1 The ARM C and C++ Libraries
1.21 Thread safety in the ARM C library
Related concepts
1.19 How to ensure re-implemented mutex functions are called on page 1-39.
1.22 Thread safety in the ARM C++ library on page 1-43.
Related references
1.20 Using the ARM C library in a multithreaded environment on page 1-40.
1.18 Management of locks in multithreaded applications on page 1-37.
4.59 Thread-safe C library functions on page 4-251.
4.60 C library functions that are not thread-safe on page 4-253.
Related information
Rogue Wave documentation.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-42
Non-Confidential
1 The ARM C and C++ Libraries
1.22 Thread safety in the ARM C++ library
— The default C++ runtime library implementations of the following use malloc() and free() and
are thread-safe with respect to each other. They are not thread-safe with respect to
std::set_new_handler(). You are permitted to replace them:
::operator new(std::size_t)
::operator new[](std::size_t)
::operator delete(void*)
::operator delete[](void*)
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-43
Non-Confidential
1 The ARM C and C++ Libraries
1.22 Thread safety in the ARM C++ library
You must protect shared objects while using the iostream and locale classes, and the
std::random_shuffle function. To do this, you might use mutex functions, or co-operative threading.
As an example, in a typical case of a pre-emptive multitasking environment, one that uses mutex
functions with containers, this means that:
• Reader threads can safely share a container if no thread writes to it during the reads.
• While a thread writes to a shared container, you must apply locking around the use of the container.
• Writer threads can write to different containers safely.
• You must apply locking around the use of random_shuffle.
• Multiple threads cannot use iostream and locale classes safely unless you apply locking around the
use of their objects.
Related concepts
1.19 How to ensure re-implemented mutex functions are called on page 1-39.
1.21 Thread safety in the ARM C library on page 1-42.
Related references
1.20 Using the ARM C library in a multithreaded environment on page 1-40.
1.18 Management of locks in multithreaded applications on page 1-37.
Related information
__cxa_*, __aeabi_* functions, C++ ABI for the ARM Architecture.
Exception Handling ABI for the ARM Architecture.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-44
Non-Confidential
1 The ARM C and C++ Libraries
1.23 The floating-point status word in a multithreaded environment
Note
In a hardware floating-point environment, the floating-point status word is stored in a Vector Floating-
Point (VFP) register. In this case, your thread-switching mechanism must keep a separate copy of this
register for each thread.
Related concepts
1.21 Thread safety in the ARM C library on page 1-42.
Related information
--fpmode=model compiler option.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-45
Non-Confidential
1 The ARM C and C++ Libraries
1.24 Using the C library with an application
Related concepts
1.25 Using the C and C++ libraries with an application in a semihosting environment on page 1-47.
1.27 Using the libraries in a nonsemihosting environment on page 1-49.
Related references
1.32 Building an application without the C library on page 1-55.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-46
Non-Confidential
1 The ARM C and C++ Libraries
1.25 Using the C and C++ libraries with an application in a semihosting environment
Related references
1.26 Using $Sub$$ to mix semihosted and nonsemihosted I/O functionality on page 1-48.
1.29 Direct semihosting C library function dependencies on page 1-52.
Related information
What is Semihosting?.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-47
Non-Confidential
1 The ARM C and C++ Libraries
1.26 Using $Sub$$ to mix semihosted and nonsemihosted I/O functionality
Related concepts
1.25 Using the C and C++ libraries with an application in a semihosting environment on page 1-47.
1.27 Using the libraries in a nonsemihosting environment on page 1-49.
Related information
ELF for the ARM Architecture.
Use of $Super$$ and $Sub$$ to patch symbol definitions.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-48
Non-Confidential
1 The ARM C and C++ Libraries
1.27 Using the libraries in a nonsemihosting environment
Note
IMPORT __use_no_semihosting is only required to be added to a single assembly source file. Similarly,
#pragma import(__use_no_semihosting) is only required to be added to a single C source file. It is
unnecessary to add these inserts to every single source file.
If you include a library function that uses semihosting and also reference __use_no_semihosting, the
library detects the conflicting symbols and the linker reports an error. To determine which objects are
using semihosting:
1. Link with armlink --verbose --list err.txt
2. Search err.txt for occurrences of __I$use$semihosting
For example:
...
Loading member sys_exit.o from c_4.l.
reference : __I$use$semihosting
definition: _sys_exit
...
This shows that the semihosting-using function _sys_exit is linked-in from the C library. To prevent
this, you must provide your own implementation of this function.
There are no target-dependent functions in the C++ library, although some C++ functions use underlying
C library functions that are target-dependent.
Related concepts
1.1 Mandatory linkage with the C library on page 1-19.
1.28 C++ exceptions in a non-semihosting environment on page 1-51.
Related references
1.26 Using $Sub$$ to mix semihosted and nonsemihosted I/O functionality on page 1-48.
1.30 Indirect semihosting C library function dependencies on page 1-53.
1.31 C library API definitions for targeting a different environment on page 1-54.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-49
Non-Confidential
1 The ARM C and C++ Libraries
1.27 Using the libraries in a nonsemihosting environment
Related information
What is Semihosting?.
--list=filename linker option.
--verbose linker option.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-50
Non-Confidential
1 The ARM C and C++ Libraries
1.28 C++ exceptions in a non-semihosting environment
Related concepts
1.27 Using the libraries in a nonsemihosting environment on page 1-49.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-51
Non-Confidential
1 The ARM C and C++ Libraries
1.29 Direct semihosting C library function dependencies
Function Description
__user_setup_stackheap() Sets up and returns the locations of the stack and the heap. You might have to re-
implement this function if you are using a scatter file at the link stage.
_sys_exit() Error signaling, error handling, and program exit.
_ttywrch()
_sys_iserror()
_sys_istty()
_sys_flen()
_sys_open()
_sys_read()
_sys_seek()
_sys_write()
_sys_tmpnam()
remove()
rename()
system()
time()
Related concepts
1.25 Using the C and C++ libraries with an application in a semihosting environment on page 1-47.
1.42 Initialization of the execution environment and execution of the application on page 1-67.
Related references
1.60 Modification of C library functions for error signaling, error handling, and program exit
on page 1-89.
1.70 Tailoring input/output functions in the C and C++ libraries on page 1-100.
1.30 Indirect semihosting C library function dependencies on page 1-53.
1.79 Tailoring non-input/output C library functions on page 1-111.
4.54 __user_setup_stackheap() on page 4-246.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-52
Non-Confidential
1 The ARM C and C++ Libraries
1.30 Indirect semihosting C library function dependencies
Function Usage
__raise() Catching, handling, or diagnosing C library exceptions, without C signal
support. (Tailoring error signaling, error handling, and program exit.)
__default_signal_handler() Catching, handling, or diagnosing C library exceptions, with C signal
support. (Tailoring error signaling, error handling, and program exit.)
__Heap_Initialize() Choosing or redefining memory allocation. Avoiding the heap and heap-
using C library functions supplied by ARM.
ferror(), fputc(), __stdout Re-implementing the printf family. (Tailoring input/output functions in the
C and C++ libraries.).
__backspace(), fgetc(), __stdin Re-implementing the scanf family. (Tailoring input/output functions in the
C and C++ libraries.).
fwrite(), fputs(), puts(), fread(), Re-implementing the stream output family. (Tailoring input/output
fgets(), gets(), ferror() functions in the C and C++ libraries.).
Related concepts
1.27 Using the libraries in a nonsemihosting environment on page 1-49.
Related references
1.29 Direct semihosting C library function dependencies on page 1-52.
1.61 Avoiding the heap and heap-using library functions supplied by ARM on page 1-90.
1.60 Modification of C library functions for error signaling, error handling, and program exit
on page 1-89.
1.70 Tailoring input/output functions in the C and C++ libraries on page 1-100.
1.79 Tailoring non-input/output C library functions on page 1-111.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-53
Non-Confidential
1 The ARM C and C++ Libraries
1.31 C library API definitions for targeting a different environment
LC_CTYPE locale Defines the character properties for the local alphabet
rt_sys.h A C header file describing all the functions whose default (semihosted)
implementations use semihosting calls
rt_heap.h A C header file describing the storage management abstract data type
rt_locale.h A C header file describing the five locale category filing systems, and defining
some macros that are useful for describing the contents of locale categories
rt_misc.h A C header file describing miscellaneous unrelated public interfaces to the C
library
rt_memory.s An empty, but commented, prototype implementation of the memory model
If you are re-implementing a function that exists in the standard ARM library, the linker uses an object or
library from your project rather than the standard ARM library.
Caution
Do not replace or delete libraries supplied by ARM. You must not overwrite the supplied library files.
Place your re-implemented functions in separate object files or libraries instead.
Related concepts
1.27 Using the libraries in a nonsemihosting environment on page 1-49.
1.49 Assembler macros that tailor locale functions in the C library on page 1-74.
Related information
--list=filename linker option.
--verbose linker option.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-54
Non-Confidential
1 The ARM C and C++ Libraries
1.32 Building an application without the C library
Function Description
alloca.h Functions in this file work without any library initialization or function re-implementation. You must know
how to build an application with the C library to use this header file.
assert.h Functions listed in this file require high-level stdio, __rt_raise(), and _sys_exit(). You must be
familiar with tailoring error signaling, error handling, and program exit to use this header file.
ctype.h Functions listed in this file require the locale functions.
errno.h Functions in this file work without the requirement for any library initialization or function re-
implementation.
fenv.h Functions in this file work without the requirement for any library initialization and only require the re-
implementation of __rt_raise().
float.h This file does not contain any code. The definitions in the file do not require library initialization or
function re-implementation.
inttypes.h Functions listed in this file require the locale functions.
limits.h Functions in this file work without the requirement for any library initialization or function re-
implementation.
locale.h Call setlocale() before calling any function that uses locale functions. For example:
setlocale(LC_ALL, "C");
See the contents of locale.h for more information on the following functions and data structures:
• setlocale() selects the appropriate locale as specified by the category and locale arguments.
• lconv is the structure used by locale functions for formatting numeric quantities according to the rules
of the current locale.
• localeconv() creates an lconv structure and returns a pointer to it.
• _get_lconv() fills the lconv structure pointed to by the parameter. This ISO extension removes the
requirement for static data within the library.
locale.h also contains constant declarations used with locale functions.
math.h For functions in this file to work, you must first call _fp_init() and re-implement __rt_raise().
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-55
Non-Confidential
1 The ARM C and C++ Libraries
1.32 Building an application without the C library
Function Description
setjmp.h Functions in this file work without any library initialization or function re-implementation.
signal.h Functions listed in this file are not available without library initialization. You must know how to build an
application with the C library to use this header file.
__rt_raise() can be re-implemented for error and exit handling. You must be familiar with tailoring error
signaling, error handling, and program exit.
stdarg.h Functions listed in this file work without any library initialization or function re-implementation.
stddef.h This file does not contain any code. The definitions in the file do not require library initialization or
function re-implementation.
stdint.h This file does not contain any code. The definitions in the file do not require library initialization or
function re-implementation.
stdio.h The following dependencies or limitations apply to these functions:
• The high-level functions such as printf(), scanf(), puts(), fgets(), fread(), fwrite(), and
perror() depend on lower-level stdio functions fgetc(), fputc(), and __backspace(). You must re-
implement these lower-level functions when using the standalone C library.
However, you cannot re-implement the _sys_ prefixed functions (for example, _sys_read()) when
using the standalone C library because they require library initialization.
You must be familiar with tailoring the input/output functions in the C and C++ libraries.
• The printf() and scanf() family of functions require locale.
• The remove() and rename() functions are system-specific and probably not usable in your application.
stdlib.h Most functions in this file work without any library initialization or function re-implementation. The
following functions depend on other functions being instantiated correctly:
• ato*() requires locale.
• strto*() requires locale.
• malloc(), calloc(), realloc(), and free() require heap functions.
• atexit() is not available when building an application without the C library.
string.h Functions in this file work without any library initialization, with the exception of strcoll() and
strxfrm(), that require locale.
time() and clock() are system-specific and are probably not usable unless re-implemented
wchar.h Wide character library functions added to ISO C by Normative Addendum 1 in 1994.
• Support for wide-character output and format strings, swprintf(), vswprintf(), swscanf(), and
vswscanf().
• All the conversion functions (for example, btowc, wctob, mbrtowc, and wcrtomb) require locale.
• wcscoll() and wcsxfrm() require locale.
wctype.h Wide character library functions added to ISO C by Normative Addendum 1 in 1994. This requires locale.
Related concepts
1.1 Mandatory linkage with the C library on page 1-19.
1.2 C and C++ runtime libraries on page 1-20.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-56
Non-Confidential
1 The ARM C and C++ Libraries
1.32 Building an application without the C library
1.33 Creating an application as bare machine C without the C library on page 1-58.
1.49 Assembler macros that tailor locale functions in the C library on page 1-74.
1.34 Integer and floating-point compiler functions and building an application without the C library
on page 1-59.
1.39 Using high-level functions when exploiting the C library on page 1-64.
1.38 Using low-level functions when exploiting the C library on page 1-63.
Related references
1.70 Tailoring input/output functions in the C and C++ libraries on page 1-100.
1.60 Modification of C library functions for error signaling, error handling, and program exit
on page 1-89.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-57
Non-Confidential
1 The ARM C and C++ Libraries
1.33 Creating an application as bare machine C without the C library
Related concepts
1.34 Integer and floating-point compiler functions and building an application without the C library
on page 1-59.
1.37 Customized C library startup code and access to C library functions on page 1-62.
1.38 Using low-level functions when exploiting the C library on page 1-63.
1.39 Using high-level functions when exploiting the C library on page 1-64.
1.35 Bare machine integer C on page 1-60.
1.36 Bare machine C with floating-point processing on page 1-61.
1.40 Using malloc() when exploiting the C library on page 1-65.
Related references
1.32 Building an application without the C library on page 1-55.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-58
Non-Confidential
1 The ARM C and C++ Libraries
1.34 Integer and floating-point compiler functions and building an application without the C library
Related concepts
1.33 Creating an application as bare machine C without the C library on page 1-58.
1.91 Compiler generated and library-resident helper functions on page 1-126.
Related references
1.32 Building an application without the C library on page 1-55.
Related information
Application Binary Interface (ABI) for the ARM Architecture.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-59
Non-Confidential
1 The ARM C and C++ Libraries
1.35 Bare machine integer C
Related concepts
1.33 Creating an application as bare machine C without the C library on page 1-58.
1.36 Bare machine C with floating-point processing on page 1-61.
Related references
1.32 Building an application without the C library on page 1-55.
1.15 Use of the __user_libspace static data area by the C libraries on page 1-34.
Related information
--fpu=name compiler option.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-60
Non-Confidential
1 The ARM C and C++ Libraries
1.36 Bare machine C with floating-point processing
Related concepts
1.33 Creating an application as bare machine C without the C library on page 1-58.
1.35 Bare machine integer C on page 1-60.
1.14 Use of static data in the C libraries on page 1-32.
Related references
1.32 Building an application without the C library on page 1-55.
1.15 Use of the __user_libspace static data area by the C libraries on page 1-34.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-61
Non-Confidential
1 The ARM C and C++ Libraries
1.37 Customized C library startup code and access to C library functions
Related concepts
1.33 Creating an application as bare machine C without the C library on page 1-58.
1.24 Using the C library with an application on page 1-46.
1.35 Bare machine integer C on page 1-60.
1.38 Using low-level functions when exploiting the C library on page 1-63.
1.39 Using high-level functions when exploiting the C library on page 1-64.
1.40 Using malloc() when exploiting the C library on page 1-65.
Related references
1.32 Building an application without the C library on page 1-55.
1.15 Use of the __user_libspace static data area by the C libraries on page 1-34.
1.70 Tailoring input/output functions in the C and C++ libraries on page 1-100.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-62
Non-Confidential
1 The ARM C and C++ Libraries
1.38 Using low-level functions when exploiting the C library
Caution
__rt_raise() is essential if you are using the heap.
Note
If rand() is called, srand() must be called first. This is done automatically during library initialization
but not when you avoid the library initialization.
Related concepts
1.39 Using high-level functions when exploiting the C library on page 1-64.
1.33 Creating an application as bare machine C without the C library on page 1-58.
1.37 Customized C library startup code and access to C library functions on page 1-62.
Related references
1.32 Building an application without the C library on page 1-55.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-63
Non-Confidential
1 The ARM C and C++ Libraries
1.39 Using high-level functions when exploiting the C library
Anything that uses locale must not be called before first calling setlocale().setlocale() selects the
appropriate locale. For example, setlocale(LC_ALL, "C"), where LC_ALL means that the call to
setlocale() affects all locale categories, and "C" specifies the minimal environment for C translation.
Locale-using functions include the functions in ctype.h and locale.h, the printf() family, the
scanf() family, ato*, strto*, strcoll/strxfrm, and most of time.h.
Related concepts
1.38 Using low-level functions when exploiting the C library on page 1-63.
1.33 Creating an application as bare machine C without the C library on page 1-58.
1.37 Customized C library startup code and access to C library functions on page 1-62.
Related references
1.32 Building an application without the C library on page 1-55.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-64
Non-Confidential
1 The ARM C and C++ Libraries
1.40 Using malloc() when exploiting the C library
_init_alloc() must be called first to supply initial heap bounds, and __rt_heap_extend() must be
provided even if it only returns failure. Without __rt_heap_extend(), certain library functionality is
included that causes problems when you are writing bare machine C.
Prototypes for both _init_alloc() and __rt_heap_extend() are in rt_heap.h.
Related concepts
1.33 Creating an application as bare machine C without the C library on page 1-58.
1.37 Customized C library startup code and access to C library functions on page 1-62.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-65
Non-Confidential
1 The ARM C and C++ Libraries
1.41 Tailoring the C library to a new execution environment
Related concepts
1.43 C++ initialization, construction and destruction on page 1-68.
1.44 Legacy support for C$$pi_ctorvec instead of .init_array on page 1-69.
1.47 Library functions called from main() on page 1-72.
1.42 Initialization of the execution environment and execution of the application on page 1-67.
1.46 Emergency buffer memory for exceptions on page 1-71.
1.48 Program exit and the assert macro on page 1-73.
1.45 Exceptions system initialization on page 1-70.
1.47 Library functions called from main() on page 1-72.
Related references
4.23 __rt_entry on page 4-214.
4.25 __rt_exit() on page 4-216.
4.28 __rt_lib_init() on page 4-219.
4.29 __rt_lib_shutdown() on page 4-220.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-66
Non-Confidential
1 The ARM C and C++ Libraries
1.42 Initialization of the execution environment and execution of the application
IMPORT __rt_entry
EXPORT __main
ENTRY
__main
B __rt_entry
END
Related concepts
1.47 Library functions called from main() on page 1-72.
1.41 Tailoring the C library to a new execution environment on page 1-66.
1.43 C++ initialization, construction and destruction on page 1-68.
Related references
1.29 Direct semihosting C library function dependencies on page 1-52.
4.23 __rt_entry on page 4-214.
4.28 __rt_lib_init() on page 4-219.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-67
Non-Confidential
1 The ARM C and C++ Libraries
1.43 C++ initialization, construction and destruction
This pseudocode is for illustration only. To see the code that is generated, compile the C++ source code
with armcc -c --cpp -S.
The linker collects each .init_array from the various translation units together. It is important that
the .init_array is accumulated in the same order.
The library routine __cpp_initialize__aeabi_ is called from the C library startup code,
__rt_lib_init, before main. __cpp_initialize__aeabi_ walks through the .init_array calling
each function in turn. On exit, __rt_lib_shutdown calls __cxa_finalize.
Usually, there is at most one function for T::T(), mangled name _ZN1TC1Ev, one function for T::~T(),
mangled name _ZN1TD1Ev, one __sti__ function, and four bytes of .init_array for each translation
unit. The mangled name for the function f() is _Z1fv. There is no way to determine the initialization
order between translation units.
Function-local static objects with destructors are also handled using __aeabi_atexit.
.init_array sections must be placed contiguously within the same region for their base and limit
symbols to be accessible. If they are not, the linker generates an error.
Related concepts
1.44 Legacy support for C$$pi_ctorvec instead of .init_array on page 1-69.
1.41 Tailoring the C library to a new execution environment on page 1-66.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-68
Non-Confidential
1 The ARM C and C++ Libraries
1.44 Legacy support for C$$pi_ctorvec instead of .init_array
Related concepts
1.43 C++ initialization, construction and destruction on page 1-68.
1.41 Tailoring the C library to a new execution environment on page 1-66.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-69
Non-Confidential
1 The ARM C and C++ Libraries
1.45 Exceptions system initialization
The default behavior is to initialize on demand. To initialize the exceptions system before main is
entered, include the following function in the link:
extern "C" void __cxa_get_globals(void);
extern "C" void __ARM_exceptions_init(void)
{
__cxa_get_globals();
}
Although you can place the call to __cxa_get_globals directly in your code, placing it in
__ARM_exceptions_init ensures that it is called as early as possible. That is, before any global
variables are initialized and before main is entered.
__ARM_exceptions_init is weakly referenced by the library initialization mechanism, and is called if it
is present as part of __rt_lib_init.
Note
The exception system is initialized by calls to various library functions, for example,
std::set_terminate(). Therefore, you might not have to initialize before the entry to main.
Related concepts
1.41 Tailoring the C library to a new execution environment on page 1-66.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-70
Non-Confidential
1 The ARM C and C++ Libraries
1.46 Emergency buffer memory for exceptions
Related concepts
1.41 Tailoring the C library to a new execution environment on page 1-66.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-71
Non-Confidential
1 The ARM C and C++ Libraries
1.47 Library functions called from main()
Related concepts
1.42 Initialization of the execution environment and execution of the application on page 1-67.
1.41 Tailoring the C library to a new execution environment on page 1-66.
1.41 Tailoring the C library to a new execution environment on page 1-66.
1.49 Assembler macros that tailor locale functions in the C library on page 1-74.
Related references
1.70 Tailoring input/output functions in the C and C++ libraries on page 1-100.
1.79 Tailoring non-input/output C library functions on page 1-111.
1.60 Modification of C library functions for error signaling, error handling, and program exit
on page 1-89.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-72
Non-Confidential
1 The ARM C and C++ Libraries
1.48 Program exit and the assert macro
where:
• expr points to the string representation of the expression that was not TRUE.
• file and line identify the source location of the assertion.
The behavior for __aeabi_assert() supplied in the ARM C library is to print a message on stderr and
call abort().
You can restore the default behavior for __aeabi_assert() at higher optimization levels by defining
__ASSERT_MSG.
Related concepts
1.41 Tailoring the C library to a new execution environment on page 1-66.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-73
Non-Confidential
1 The ARM C and C++ Libraries
1.49 Assembler macros that tailor locale functions in the C library
Related concepts
1.47 Library functions called from main() on page 1-72.
1.53 Runtime selection of the locale subsystem in the C library on page 1-78.
Related references
1.31 C library API definitions for targeting a different environment on page 1-54.
1.32 Building an application without the C library on page 1-55.
1.50 Link time selection of the locale subsystem in the C library on page 1-75.
1.54 Definition of locale data blocks in the C library on page 1-79.
1.55 LC_CTYPE data block on page 1-81.
1.56 LC_COLLATE data block on page 1-84.
1.57 LC_MONETARY data block on page 1-85.
1.58 LC_NUMERIC data block on page 1-86.
1.59 LC_TIME data block on page 1-87.
4.14 lconv structure on page 4-204.
4.9 _get_lconv() on page 4-199.
4.15 localeconv() on page 4-206.
4.32 setlocale() on page 4-223.
4.7 _findlocale() on page 4-197.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-74
Non-Confidential
1 The ARM C and C++ Libraries
1.50 Link time selection of the locale subsystem in the C library
Related concepts
1.49 Assembler macros that tailor locale functions in the C library on page 1-74.
1.52 Shift-JIS and UTF-8 implementation on page 1-77.
Related references
1.51 ISO8859-1 implementation on page 1-76.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-75
Non-Confidential
1 The ARM C and C++ Libraries
1.51 ISO8859-1 implementation
Symbol Description
__use_iso8859_ctype Selects the ISO8859-1 (Latin-1) classification of characters. This is essentially 7-bit ASCII,
except that the character codes 160-255 represent a selection of useful European punctuation
characters, letters, and accented letters.
__use_iso8859_collate Selects the strcoll/strxfrm collation table appropriate to the Latin-1 alphabet. The default
C locale does not require a collation table.
__use_iso8859_monetary Selects the Sterling monetary category using Latin-1 coding.
__use_iso8859_numeric Selects separation of thousands with commas in the printing of numeric values.
__use_iso8859_locale Selects all the ISO8859-1 selections described in this table.
Related references
1.50 Link time selection of the locale subsystem in the C library on page 1-75.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-76
Non-Confidential
1 The ARM C and C++ Libraries
1.52 Shift-JIS and UTF-8 implementation
Function Description
__use_sjis_ctype Sets the character set to the Shift-JIS multibyte encoding of Japanese characters
__use_utf8_ctype Sets the character set to the UTF-8 multibyte encoding of all Unicode characters
The following list describes the effects of Shift-JIS and UTF-8 encoding:
• The ordinary ctype functions behave correctly on any byte value that is a self-contained character in
Shift-JIS. For example, half-width katakana characters that Shift-JIS encodes as single bytes between
0xA6 and 0xDF are treated as alphabetic by isalpha().
UTF-8 encoding uses the same set of self-contained characters as the ASCII character set.
• The multibyte conversion functions such as mbrtowc(), mbsrtowcs(), and wcrtomb(), all convert
between wide strings in Unicode and multibyte character strings in Shift-JIS or UTF-8.
• printf("%ls") converts a Unicode wide string into Shift-JIS or UTF-8 output, and scanf("%ls")
converts Shift-JIS or UTF-8 input into a Unicode wide string.
You can arbitrarily switch between multibyte locales and single-byte locales at runtime if you include
more than one in your application. By default, only one locale at a time is included.
Related references
1.50 Link time selection of the locale subsystem in the C library on page 1-75.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-77
Non-Confidential
1 The ARM C and C++ Libraries
1.53 Runtime selection of the locale subsystem in the C library
Related concepts
1.49 Assembler macros that tailor locale functions in the C library on page 1-74.
Related references
4.32 setlocale() on page 4-223.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-78
Non-Confidential
1 The ARM C and C++ Libraries
1.54 Definition of locale data blocks in the C library
where:
TYPE
is one of the following:
• CTYPE
• COLLATE
• MONETARY
• NUMERIC
• TIME.
prefix
is the prefix for the assembler symbols defined within the locale data
name
is the textual name for the locale data.
Where:
TYPE
is one of the following:
• CTYPE
• COLLATE
• MONETARY
• NUMERIC
• TIME.
function
is a specific function, table(), full_wctype(), or multibyte(), related to your locale data.
When specifying locale data, you must call the macro repeatedly for each respective function.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-79
Non-Confidential
1 The ARM C and C++ Libraries
1.54 Definition of locale data blocks in the C library
where:
TYPE
is one of the following:
• CTYPE
• COLLATE
• MONETARY
• NUMERIC
• TIME.
To write a fixed function that always returns the same locale, you can use the _start symbol name
defined by the macros. The following shows how this is implemented for the CTYPE locale:
GET rt_locale.s
AREA my_locales, DATA, READONLY
LC_CTYPE_begin my_ctype_locale, "MyLocale"
... ; include other LC_CTYPE_xxx macros here
LC_CTYPE_end
AREA my_locale_func, CODE, READONLY
_get_lc_ctype FUNCTION
LDR r0, =my_ctype_locale_start
BX lr
ENDFUNC
Contiguous locale blocks suitable for passing to the _findlocale() function must be declared in
sequence. You must call the macro LC_index_end to end the sequence of locale blocks. The following
shows how this is implemented for the CTYPE locale:
GET rt_locale.s
AREA my_locales, DATA, READONLY
my_ctype_locales
LC_CTYPE_begin my_first_ctype_locale, "MyLocale1"
... ; include other LC_CTYPE_xxx macros here
LC_CTYPE_end
LC_CTYPE_begin my_second_ctype_locale, "MyLocale2"
... ; include other LC_CTYPE_xxx macros here
LC_CTYPE_end
LC_index_end
AREA my_locale_func, CODE, READONLY
IMPORT _findlocale
_get_lc_ctype FUNCTION
LDR r0, =my_ctype_locales
B _findlocale
ENDFUNC
Related concepts
1.49 Assembler macros that tailor locale functions in the C library on page 1-74.
Related references
1.55 LC_CTYPE data block on page 1-81.
1.56 LC_COLLATE data block on page 1-84.
1.57 LC_MONETARY data block on page 1-85.
1.58 LC_NUMERIC data block on page 1-86.
1.59 LC_TIME data block on page 1-87.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-80
Non-Confidential
1 The ARM C and C++ Libraries
1.55 LC_CTYPE data block
Note
A printable space character is defined as any character where the result of both isprint() and
isspace() is true.
__A must not be specified for the same character as either __N or __X.
• LC_CTYPE_full_wctype. Calling this macro without arguments causes the C99 wide-character
ctype functions (iswalpha(), iswupper(), ...) to return useful values across the full range of
Unicode when this LC_CTYPE locale is active. If this macro is not specified, the wide ctype
functions treat the first 256 wchar_t values as the same as the 256 char values, and the rest of the
wchar_t range as containing illegal characters.
• LC_CTYPE_multibyte defines this locale to be a multibyte character set. Call this macro with
three arguments. The first two arguments are the names of functions that perform conversion
between the multibyte character set and Unicode wide characters. The last argument is the value
that must be taken by the C macro MB_CUR_MAX for the respective character set. The two function
arguments have the following prototypes:
size_t internal_mbrtowc(wchar_t *pwc, char c, mbstate_t *pstate);
size_t internal_wcrtomb(char *s, wchar_t w, mbstate_t *pstate);
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-81
Non-Confidential
1 The ARM C and C++ Libraries
1.55 LC_CTYPE data block
internal_mbrtowc()
takes one byte, c, as input, and updates the mbstate_t pointed to by pstate as a result of
reading that byte. If the byte completes the encoding of a multibyte character, it writes
the corresponding wide character into the location pointed to by pwc, and returns 1 to
indicate that it has done so. If not, it returns -2 to indicate the state change of mbstate_t
and that no character is output. Otherwise, it returns -1 to indicate that the encoded input
is invalid.
internal_wcrtomb()
takes one wide character, w, as input, and writes some number of bytes into the memory
pointed to by s. It returns the number of bytes output, or -1 to indicate that the input
character has no valid representation in the multibyte character set.
4. Call LC_CTYPE_end, without arguments, to finish the locale block definition.
Related concepts
1.49 Assembler macros that tailor locale functions in the C library on page 1-74.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-82
Non-Confidential
1 The ARM C and C++ Libraries
1.55 LC_CTYPE data block
Related references
1.54 Definition of locale data blocks in the C library on page 1-79.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-83
Non-Confidential
1 The ARM C and C++ Libraries
1.56 LC_COLLATE data block
Related concepts
1.49 Assembler macros that tailor locale functions in the C library on page 1-74.
Related references
1.54 Definition of locale data blocks in the C library on page 1-79.
1.51 ISO8859-1 implementation on page 1-76.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-84
Non-Confidential
1 The ARM C and C++ Libraries
1.57 LC_MONETARY data block
Related concepts
1.49 Assembler macros that tailor locale functions in the C library on page 1-74.
Related references
1.54 Definition of locale data blocks in the C library on page 1-79.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-85
Non-Confidential
1 The ARM C and C++ Libraries
1.58 LC_NUMERIC data block
Related concepts
1.49 Assembler macros that tailor locale functions in the C library on page 1-74.
Related references
1.54 Definition of locale data blocks in the C library on page 1-79.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-86
Non-Confidential
1 The ARM C and C++ Libraries
1.59 LC_TIME data block
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-87
Non-Confidential
1 The ARM C and C++ Libraries
1.59 LC_TIME data block
LC_TIME_week_short "Sat"
LC_TIME_end
Related concepts
1.49 Assembler macros that tailor locale functions in the C library on page 1-74.
Related references
1.54 Definition of locale data blocks in the C library on page 1-79.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-88
Non-Confidential
1 The ARM C and C++ Libraries
1.60 Modification of C library functions for error signaling, error handling, and program exit
1.60 Modification of C library functions for error signaling, error handling, and
program exit
All trap or error signals raised by the C library go through the __raise() function. You can re-
implement this function or the lower-level functions that it uses.
Caution
The IEEE 754 standard for floating-point processing states that the default response to an exception is to
proceed without a trap. You can modify floating-point error handling by tailoring the functions and
definitions in fenv.h.
Function Description
_sys_exit() Called, eventually, by all exits from the library.
errno Is a static variable used with error handling.
__rt_errno_addr() Is called to obtain the address of the variable errno.
__raise() Raises a signal to indicate a runtime anomaly.
__rt_raise() Raises a signal to indicate a runtime anomaly.
__default_signal_handler() Displays an error indication to the user.
Related concepts
1.47 Library functions called from main() on page 1-72.
Related references
1.29 Direct semihosting C library function dependencies on page 1-52.
1.30 Indirect semihosting C library function dependencies on page 1-53.
1.32 Building an application without the C library on page 1-55.
4.41 _sys_exit() on page 4-233.
4.6 errno on page 4-196.
4.24 __rt_errno_addr() on page 4-215.
4.19 __raise() on page 4-210.
4.30 __rt_raise() on page 4-221.
4.5 __default_signal_handler() on page 4-195.
4.51 _ttywrch() on page 4-243.
4.26 __rt_fp_status_addr() on page 4-217.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-89
Non-Confidential
1 The ARM C and C++ Libraries
1.61 Avoiding the heap and heap-using library functions supplied by ARM
1.61 Avoiding the heap and heap-using library functions supplied by ARM
If you are developing embedded systems that have limited RAM or that provide their own heap
management (for example, an operating system), you might require a system that does not define a heap
area.
To avoid using the heap you can either:
• Re-implement the functions in your own application.
• Write the application so that it does not call any heap-using function.
You can reference the __use_no_heap or __use_no_heap_region symbols in your code to guarantee
that no heap-using functions are linked in from the ARM library. You are only required to import these
symbols once in your application, for example, using either:
• IMPORT __use_no_heap from assembly language.
• #pragma import(__use_no_heap) from C.
If you include a heap-using function and also reference __use_no_heap or __use_no_heap_region, the
linker reports an error. For example, the following sample code results in the linker error shown:
#include <stdio.h>
#include <stdlib.h>
#pragma import(__use_no_heap)
void main()
{
char *p = malloc(256);
...
}
Error: L6915E: Library reports error: __use_no_heap was requested, but malloc was referenced
To find out which objects are using the heap, link with --verbose --list=out.txt, search the output
for the relevant symbol (in this case malloc), and find out what object referenced it.
__use_no_heap guards against the use of malloc(), realloc(), free(), and any function that uses
those functions. For example, calloc() and other stdio functions.
__use_no_heap_region has the same properties as __use_no_heap, but in addition, guards against other
things that use the heap memory region. For example, if you declare main() as a function taking
arguments, the heap region is used for collecting argc and argv.
Related concepts
1.63 Heap1, standard heap implementation on page 1-92.
1.64 Heap2, alternative heap implementation on page 1-93.
1.65 Using a heap implementation from bare machine C on page 1-94.
Related references
1.30 Indirect semihosting C library function dependencies on page 1-53.
1.62 C library support for memory allocation functions on page 1-91.
Related information
--list=filename linker option.
--verbose linker option.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-90
Non-Confidential
1 The ARM C and C++ Libraries
1.62 C library support for memory allocation functions
Related concepts
1.63 Heap1, standard heap implementation on page 1-92.
1.64 Heap2, alternative heap implementation on page 1-93.
1.65 Using a heap implementation from bare machine C on page 1-94.
Related references
1.61 Avoiding the heap and heap-using library functions supplied by ARM on page 1-90.
4.2 alloca() on page 4-192.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-91
Non-Confidential
1 The ARM C and C++ Libraries
1.63 Heap1, standard heap implementation
Related concepts
1.64 Heap2, alternative heap implementation on page 1-93.
1.65 Using a heap implementation from bare machine C on page 1-94.
Related references
1.61 Avoiding the heap and heap-using library functions supplied by ARM on page 1-90.
1.62 C library support for memory allocation functions on page 1-91.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-92
Non-Confidential
1 The ARM C and C++ Libraries
1.64 Heap2, alternative heap implementation
The Heap2 real-time heap implementation must know the maximum address space that the heap spans.
The smaller the address range, the more efficient the algorithm is.
By default, the heap extent is taken to be 16MB starting at the beginning of the heap (defined as the start
of the first chunk of memory given to the heap manager by __rt_initial_stackheap() or
__rt_heap_extend()).
The input parameters are the default values that are used if this routine is not defined. You can, for
example, leave the default base value unchanged and only adjust the size.
Note
The size field returned must be a power of two. The library does not check this and fails in unexpected
ways if this requirement is not met. If you return a size of zero, the extent of the heap is set to 4GB.
Related concepts
1.63 Heap1, standard heap implementation on page 1-92.
1.65 Using a heap implementation from bare machine C on page 1-94.
Related references
1.61 Avoiding the heap and heap-using library functions supplied by ARM on page 1-90.
1.62 C library support for memory allocation functions on page 1-91.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-93
Non-Confidential
1 The ARM C and C++ Libraries
1.65 Using a heap implementation from bare machine C
Note
The parameters of _init_alloc(base, top) must be eight-byte aligned.
2. Define the function unsigned __rt_heap_extend(unsigned size, void **block) to handle calls
to extend the heap when it becomes full.
Related concepts
1.63 Heap1, standard heap implementation on page 1-92.
1.64 Heap2, alternative heap implementation on page 1-93.
Related references
1.61 Avoiding the heap and heap-using library functions supplied by ARM on page 1-90.
1.62 C library support for memory allocation functions on page 1-91.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-94
Non-Confidential
1 The ARM C and C++ Libraries
1.66 Stack pointer initialization and heap bounds
Note
The above two methods are the only methods that microlib supports for defining where the stack
pointer starts and for defining the heap bounds.
• Implement __user_setup_stackheap() to set up the stack pointer and return the bounds of the
initial heap region.
• If you are using legacy code that uses __user_initial_stackheap(), and you do not want to
replace __user_initial_stackheap() with __user_setup_stackheap(), continue to use
__user_initial_stackheap().
Note
ARM recommends that you switch to using __user_setup_stackheap() if you are still using
__user_initial_stackheap(), unless your implementation of __user_initial_stackheap() is:
— Specialized in some way such that it is complex enough to require its own temporary stack to run
on before it has created the proper stack.
— Has some user-specific special requirement that means it has to be implemented in C rather than
in assembly language.
Note
The memory allocation functions (malloc(), realloc(), calloc(), posix_memalign()) attempt to
detect allocations that collide with the current stack pointer. Such detection cannot be guaranteed to
always be successful.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-95
Non-Confidential
1 The ARM C and C++ Libraries
1.66 Stack pointer initialization and heap bounds
Although it is possible to automatically detect expansion of the heap into the stack, it is not possible to
automatically detect expansion of the stack into heap memory.
For legacy purposes, it is possible for you to bypass all of these methods and behavior. You can do this
by defining the following functions to perform your own stack and heap memory management:
• __rt_stackheap_init()
• __rt_heap_extend()
Related concepts
1.69 Legacy support for __user_initial_stackheap() on page 1-99.
1.67 Defining __initial_sp, __heap_base and __heap_limit on page 1-97.
Related tasks
1.68 Extending heap size at runtime on page 1-98.
Related references
4.52 __user_heap_extend() on page 4-244.
4.53 __user_heap_extent() on page 4-245.
4.61 Legacy function __user_initial_stackheap() on page 4-255.
4.27 __rt_heap_extend() on page 4-218.
4.31 __rt_stackheap_init() on page 4-222.
4.54 __user_setup_stackheap() on page 4-246.
4.55 __vectab_stack_and_reset on page 4-247.
4.54 __user_setup_stackheap() on page 4-246.
Related information
Specifying stack and heap using the scatter file.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-96
Non-Confidential
1 The ARM C and C++ Libraries
1.67 Defining __initial_sp, __heap_base and __heap_limit
You can define these symbols in an assembly language file, or by using the embedded assembler in C.
For example:
__asm void dummy_function(void)
{
EXPORT __initial_sp
EXPORT __heap_base
EXPORT __heap_limit
__initial_sp EQU STACK_BASE
__heap_base EQU HEAP_BASE
__heap_limit EQU (HEAP_BASE + HEAP_SIZE)
}
The constants STACK_BASE, HEAP_BASE and HEAP_SIZE can be defined in a header file, for example
stack.h, as follows:
/* stack.h */
#define HEAP_BASE 0x20100000 /* Example memory addresses */
#define STACK_BASE 0x20200000
#define HEAP_SIZE ((STACK_BASE-HEAP_BASE)/2)
#define STACK_SIZE ((STACK_BASE-HEAP_BASE)/2)
Note
This method of specifying the initial stack pointer and heap bounds is supported by both the standard C
library (standardlib) and the micro C library (microlib).
Related concepts
1.66 Stack pointer initialization and heap bounds on page 1-95.
Related tasks
1.68 Extending heap size at runtime on page 1-98.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-97
Non-Confidential
1 The ARM C and C++ Libraries
1.68 Extending heap size at runtime
Related concepts
1.67 Defining __initial_sp, __heap_base and __heap_limit on page 1-97.
1.66 Stack pointer initialization and heap bounds on page 1-95.
Related references
4.27 __rt_heap_extend() on page 4-218.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-98
Non-Confidential
1 The ARM C and C++ Libraries
1.69 Legacy support for __user_initial_stackheap()
Note
ARM recommends that you use __user_setup_stackheap() in preference to
__user_initial_stackheap().
Related concepts
1.66 Stack pointer initialization and heap bounds on page 1-95.
Related references
4.61 Legacy function __user_initial_stackheap() on page 4-255.
4.54 __user_setup_stackheap() on page 4-246.
4.54 __user_setup_stackheap() on page 4-246.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-99
Non-Confidential
1 The ARM C and C++ Libraries
1.70 Tailoring input/output functions in the C and C++ libraries
Related concepts
1.47 Library functions called from main() on page 1-72.
1.72 The C library printf family of functions on page 1-103.
1.73 The C library scanf family of functions on page 1-104.
1.74 Redefining low-level library functions to enable direct use of high-level library functions in the C
library on page 1-105.
1.75 The C library functions fread(), fgets() and gets() on page 1-107.
1.76 Re-implementing __backspace() in the C library on page 1-108.
1.77 Re-implementing __backspacewc() in the C library on page 1-109.
1.78 Redefining target-dependent system I/O functions in the C library on page 1-110.
Related references
1.29 Direct semihosting C library function dependencies on page 1-52.
1.30 Indirect semihosting C library function dependencies on page 1-53.
1.32 Building an application without the C library on page 1-55.
1.71 Target dependencies on low-level functions in the C and C++ libraries on page 1-101.
4.38 _sys_close() on page 4-230.
4.39 _sys_command_string() on page 4-231.
4.40 _sys_ensure() on page 4-232.
4.42 _sys_flen() on page 4-234.
4.43 _sys_istty() on page 4-235.
4.44 _sys_open() on page 4-236.
4.45 _sys_read() on page 4-237.
4.46 _sys_seek() on page 4-238.
4.47 _sys_tmpnam() on page 4-239.
4.48 _sys_write() on page 4-240.
4.18 #pragma import(_main_redirection) on page 4-209.
4.8 _fisatty() on page 4-198.
4.43 _sys_istty() on page 4-235.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-100
Non-Confidential
1 The ARM C and C++ Libraries
1.71 Target dependencies on low-level functions in the C and C++ libraries
Note
You must provide definitions of __stdin and __stdout if you use any of their associated high-level
functions. This applies even if your re-implementations of other functions, such as fgetc() and
fputc(), do not reference any data stored in __stdin and __stdout.
Table key:
1. __FILE, the file structure.
2. __stdin, the standard input object of type __FILE.
3. __stdout, the standard output object of type __FILE.
4. fputc(), outputs a character to a file.
5. ferror(), returns the error status accumulated during file I/O.
6. fgetc(), gets a character from a file.
7. fgetwc()
8. fputwc()
9. __backspace(), moves the file pointer to the previous character.
10. __backspacewc().
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-101
Non-Confidential
1 The ARM C and C++ Libraries
1.71 Target dependencies on low-level functions in the C and C++ libraries
Note
If you choose to re-implement fgetc(), fputc(), and __backspace(), be aware that fopen() and
related functions use the ARM layout for the __FILE structure. You might also have to re-implement
fopen() and related functions if you define your own version of __FILE.
Related concepts
1.72 The C library printf family of functions on page 1-103.
1.73 The C library scanf family of functions on page 1-104.
1.74 Redefining low-level library functions to enable direct use of high-level library functions in the C
library on page 1-105.
1.75 The C library functions fread(), fgets() and gets() on page 1-107.
1.76 Re-implementing __backspace() in the C library on page 1-108.
1.77 Re-implementing __backspacewc() in the C library on page 1-109.
1.78 Redefining target-dependent system I/O functions in the C library on page 1-110.
Related references
1.70 Tailoring input/output functions in the C and C++ libraries on page 1-100.
Related information
ISO C Reference.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-102
Non-Confidential
1 The ARM C and C++ Libraries
1.72 The C library printf family of functions
All these functions use __FILE opaquely and depend only on the functions fputc() and ferror(). The
functions _printf() and _fprintf() are identical to printf() and fprintf() except that they cannot
format floating-point values.
The standard output functions of the form _printf(...) are equivalent to:
fprintf(& __stdout, ...)
Related concepts
1.73 The C library scanf family of functions on page 1-104.
1.74 Redefining low-level library functions to enable direct use of high-level library functions in the C
library on page 1-105.
1.75 The C library functions fread(), fgets() and gets() on page 1-107.
1.76 Re-implementing __backspace() in the C library on page 1-108.
1.77 Re-implementing __backspacewc() in the C library on page 1-109.
1.78 Redefining target-dependent system I/O functions in the C library on page 1-110.
Related references
1.70 Tailoring input/output functions in the C and C++ libraries on page 1-100.
1.71 Target dependencies on low-level functions in the C and C++ libraries on page 1-101.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-103
Non-Confidential
1 The ARM C and C++ Libraries
1.73 The C library scanf family of functions
Related concepts
1.72 The C library printf family of functions on page 1-103.
1.74 Redefining low-level library functions to enable direct use of high-level library functions in the C
library on page 1-105.
1.75 The C library functions fread(), fgets() and gets() on page 1-107.
1.76 Re-implementing __backspace() in the C library on page 1-108.
1.77 Re-implementing __backspacewc() in the C library on page 1-109.
1.78 Redefining target-dependent system I/O functions in the C library on page 1-110.
Related references
1.70 Tailoring input/output functions in the C and C++ libraries on page 1-100.
1.71 Target dependencies on low-level functions in the C and C++ libraries on page 1-101.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-104
Non-Confidential
1 The ARM C and C++ Libraries
1.74 Redefining low-level library functions to enable direct use of high-level library functions in the C library
These examples show you how to do this. However, consider modifying the system I/O functions instead
of these low-level library functions if you require real file handling.
You are not required to re-implement every function shown in these examples. Only re-implement the
functions that are used in your application.
Retargeting printf()
#include <stdio.h>
struct __FILE
{
int handle;
/* Whatever you require here. If the only file you are using is */
/* standard output using printf() for debugging, no file handling */
/* is required. */
};
/* FILE is typedef’d in stdio.h. */
FILE __stdout;
int fputc(int ch, FILE *f)
{
/* Your implementation of fputc(). */
return ch;
}
int ferror(FILE *f)
{
/* Your implementation of ferror(). */
return 0;
}
void test(void)
{
printf("Hello world\n");
}
Note
Be aware of endianness with fputc(). fputc() takes an int parameter, but contains only a character.
Whether the character is in the first or the last byte of the integer variable depends on the endianness.
The following code sample avoids problems with endianness:
extern void sendchar(char *ch);
int fputc(int ch, FILE *f)
{
/* example: write a character to an LCD */
char tempch = ch; // temp char avoids endianness issue
sendchar(&tempch);
return ch;
}
Retargeting cout
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-105
Non-Confidential
1 The ARM C and C++ Libraries
1.74 Redefining low-level library functions to enable direct use of high-level library functions in the C library
/* is required. */
};
FILE __stdout;
FILE __stdin;
FILE __stderr;
int fgetc(FILE *f)
{
/* Your implementation of fgetc(). */
return 0;
};
int fputc(int c, FILE *stream)
{
/* Your implementation of fputc(). */
}
int ferror(FILE *stream)
{
/* Your implementation of ferror(). */
}
long int ftell(FILE *stream)
{
/* Your implementation of ftell(). */
}
int fclose(FILE *f)
{
/* Your implementation of fclose(). */
return 0;
}
int fseek(FILE *f, long nPos, int nMode)
{
/* Your implementation of fseek(). */
return 0;
}
int fflush(FILE *f)
{
/* Your implementation of fflush(). */
return 0;
}
}
By default, fread() and fwrite() call fast block input/output functions that are part of the ARM stream
implementation. If you define your own __FILE structure instead of using the ARM stream
implementation, fread() and fwrite() call fgetc() instead of calling the block input/output functions.
Related concepts
1.72 The C library printf family of functions on page 1-103.
1.73 The C library scanf family of functions on page 1-104.
1.75 The C library functions fread(), fgets() and gets() on page 1-107.
1.76 Re-implementing __backspace() in the C library on page 1-108.
1.77 Re-implementing __backspacewc() in the C library on page 1-109.
1.78 Redefining target-dependent system I/O functions in the C library on page 1-110.
Related references
1.70 Tailoring input/output functions in the C and C++ libraries on page 1-100.
1.71 Target dependencies on low-level functions in the C and C++ libraries on page 1-101.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-106
Non-Confidential
1 The ARM C and C++ Libraries
1.75 The C library functions fread(), fgets() and gets()
If you provide your own implementation of __FILE, __stdin (for gets()), fgetc(), and ferror(), you
can use these functions, and the C++ object std::cin directly from the library.
Related concepts
1.72 The C library printf family of functions on page 1-103.
1.73 The C library scanf family of functions on page 1-104.
1.74 Redefining low-level library functions to enable direct use of high-level library functions in the C
library on page 1-105.
1.76 Re-implementing __backspace() in the C library on page 1-108.
1.77 Re-implementing __backspacewc() in the C library on page 1-109.
1.78 Redefining target-dependent system I/O functions in the C library on page 1-110.
Related references
1.70 Tailoring input/output functions in the C and C++ libraries on page 1-100.
1.71 Target dependencies on low-level functions in the C and C++ libraries on page 1-101.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-107
Non-Confidential
1 The ARM C and C++ Libraries
1.76 Re-implementing __backspace() in the C library
Note
Normally, you are not required to call __backspace() directly, unless you are implementing your own
scanf-like function.
__backspace(stream) must only be called after reading a character from the stream. You must not call
it after a write, a seek, or immediately after opening the file, for example. It returns to the stream the last
character that was read from the stream, so that the same character can be read from the stream again by
the next read operation. This means that a character that was read from the stream by scanf but that is
not required (that is, it terminates the scanf operation) is read correctly by the next function that reads
from the stream.
__backspace is separate from ungetc(). This is to guarantee that a single character can be pushed back
after the scanf family of functions has finished.
The value returned by __backspace() is either 0 (success) or EOF (failure). It returns EOF only if used
incorrectly, for example, if no characters have been read from the stream. When used correctly,
__backspace() must always return 0, because the scanf family of functions do not check the error
return.
The interaction between __backspace() and ungetc() is:
• If you apply __backspace() to a stream and then ungetc() a character into the same stream,
subsequent calls to fgetc() must return first the character returned by ungetc(), and then the
character returned by __backspace().
• If you ungetc() a character back to a stream, then read it with fgetc(), and then backspace it, the
next character read by fgetc() must be the same character that was returned to the stream. That is
the __backspace() operation must cancel the effect of the fgetc() operation. However, another call
to ungetc() after the call to __backspace() is not required to succeed.
• The situation where you ungetc() a character into a stream and then __backspace() another one
immediately, with no intervening read, never arises. __backspace() must only be called after
fgetc(), so this sequence of calls is illegal. If you are writing __backspace() implementations, you
can assume that the unget() of a character into a stream followed immediately by a __backspace()
with no intervening read, never occurs.
Related concepts
1.72 The C library printf family of functions on page 1-103.
1.73 The C library scanf family of functions on page 1-104.
1.74 Redefining low-level library functions to enable direct use of high-level library functions in the C
library on page 1-105.
1.75 The C library functions fread(), fgets() and gets() on page 1-107.
1.77 Re-implementing __backspacewc() in the C library on page 1-109.
1.78 Redefining target-dependent system I/O functions in the C library on page 1-110.
Related references
1.70 Tailoring input/output functions in the C and C++ libraries on page 1-100.
1.71 Target dependencies on low-level functions in the C and C++ libraries on page 1-101.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-108
Non-Confidential
1 The ARM C and C++ Libraries
1.77 Re-implementing __backspacewc() in the C library
__backspacewc() behaves in the same way as __backspace() except that it pushes back the last wide
character instead of a narrow character.
Related concepts
1.72 The C library printf family of functions on page 1-103.
1.73 The C library scanf family of functions on page 1-104.
1.74 Redefining low-level library functions to enable direct use of high-level library functions in the C
library on page 1-105.
1.75 The C library functions fread(), fgets() and gets() on page 1-107.
1.76 Re-implementing __backspace() in the C library on page 1-108.
1.78 Redefining target-dependent system I/O functions in the C library on page 1-110.
Related references
1.70 Tailoring input/output functions in the C and C++ libraries on page 1-100.
1.71 Target dependencies on low-level functions in the C and C++ libraries on page 1-101.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-109
Non-Confidential
1 The ARM C and C++ Libraries
1.78 Redefining target-dependent system I/O functions in the C library
rt_sys.h defines the type FILEHANDLE. The value of FILEHANDLE is returned by _sys_open() and
identifies an open file on the host system.
If the system I/O functions are redefined, both normal character I/O and wide character I/O work. That
is, you are not required to do anything extra with these functions for wide character I/O to work.
Related concepts
1.72 The C library printf family of functions on page 1-103.
1.73 The C library scanf family of functions on page 1-104.
1.74 Redefining low-level library functions to enable direct use of high-level library functions in the C
library on page 1-105.
1.75 The C library functions fread(), fgets() and gets() on page 1-107.
1.76 Re-implementing __backspace() in the C library on page 1-108.
1.77 Re-implementing __backspacewc() in the C library on page 1-109.
Related references
1.70 Tailoring input/output functions in the C and C++ libraries on page 1-100.
1.71 Target dependencies on low-level functions in the C and C++ libraries on page 1-101.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-110
Non-Confidential
1 The ARM C and C++ Libraries
1.79 Tailoring non-input/output C library functions
Related concepts
1.47 Library functions called from main() on page 1-72.
Related references
1.29 Direct semihosting C library function dependencies on page 1-52.
1.30 Indirect semihosting C library function dependencies on page 1-53.
4.3 clock() on page 4-193.
4.4 _clock_init() on page 4-194.
4.50 time() on page 4-242.
4.21 remove() on page 4-212.
4.22 rename() on page 4-213.
4.49 system() on page 4-241.
4.10 getenv() on page 4-200.
4.11 _getenv_init() on page 4-201.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-111
Non-Confidential
1 The ARM C and C++ Libraries
1.80 Real-time integer division in the ARM libraries
Note
Related concepts
1.81 Selecting real-time division in the ARM libraries on page 1-113.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-112
Non-Confidential
1 The ARM C and C++ Libraries
1.81 Selecting real-time division in the ARM libraries
Related references
1.80 Real-time integer division in the ARM libraries on page 1-112.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-113
Non-Confidential
1 The ARM C and C++ Libraries
1.82 How the ARM C library fulfills ISO C specification requirements
Note
The behavior of the assert macro depends on the conditions in operation at the most recent
occurrence of #include <assert.h>.
• The following functions test for character values in the range EOF (-1) to 255 inclusive:
— isalnum()
— isalpha()
— iscntrl()
— islower()
— isprint()
— isupper()
— ispunct()
• The fully POSIX-compliant functions remquo(), remquof() and remquol() return the remainder of
the division of x by y and store the quotient of the division in the pointer *quo. An implementation-
defined integer value defines the number of bits of the quotient that are stored. In the ARM C library,
this value is set to 4.
• C99 behavior, with respect to mathlib error handling, is enabled by default.
Note
In RVCT 4.0, this behavior is not enabled by default, but is enabled through the use of IMPORT
__use_c99_matherr in assembly language, or #pragma import __use_c99_matherr in C.
Related concepts
1.85 ISO-compliant C library input/output characteristics on page 1-119.
1.48 Program exit and the assert macro on page 1-73.
Related references
1.83 mathlib error handling on page 1-115.
1.84 ISO-compliant implementation of signals supported by the signal() function in the C library and
additional type arguments on page 1-117.
1.86 Standard C++ library implementation definition on page 1-121.
1.92 C and C++ library naming conventions on page 1-127.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-114
Non-Confidential
1 The ARM C and C++ Libraries
1.83 mathlib error handling
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-115
Non-Confidential
1 The ARM C and C++ Libraries
1.83 mathlib error handling
HUGE_VAL is an alias for Inf. Consult the errno variable for the error number. Other than the cases
shown in this table, all functions return QNaN when passed QNaN and throw an invalid operation exception
when passed SNaN.
The string passed to C99 nan() is ignored, and the same Not a Number (NaN) is always returned,
namely the one with all fraction bits clear except the topmost one. The sign bit is also clear. Passing
strings of the form NAN(xxxx) to strtod has the same effect.
Related concepts
1.85 ISO-compliant C library input/output characteristics on page 1-119.
1.82 How the ARM C library fulfills ISO C specification requirements on page 1-114.
Related references
1.84 ISO-compliant implementation of signals supported by the signal() function in the C library and
additional type arguments on page 1-117.
1.86 Standard C++ library implementation definition on page 1-121.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-116
Non-Confidential
1 The ARM C and C++ Libraries
1.84 ISO-compliant implementation of signals supported by the signal() function in the C library and additional type arguments
SIGFPE 2 Signals any arithmetic exception, for example, A set of bits from
division by zero. Used by hard and soft floating-point FE_EX_INEXACT,
and by integer division. FE_EX_UNDERFLOW,
FE_EX_OVERFLOW,
FE_EX_DIVBYZERO,
FE_EX_INVALID, DIVBYZERO a
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-117
Non-Confidential
1 The ARM C and C++ Libraries
1.84 ISO-compliant implementation of signals supported by the signal() function in the C library and additional type arguments
Although SIGSTAK exists in signal.h, this signal is not generated by the C library and is considered
obsolete.
A signal number greater than SIGUSR2 can be passed through __raise() and caught by the default
signal handler, but it cannot be caught by a handler registered using signal().
signal() returns an error code if you try to register a handler for a signal number greater than
SIGUSR2.
The default handling of all recognized signals is to print a diagnostic message and call exit(). This
default behavior applies at program startup and until you change it.
Caution
The IEEE 754 standard for floating-point processing states that the default action to an exception is to
proceed without a trap. A raised exception in floating-point calculations does not, by default, generate
SIGFPE. You can modify floating-point error handling by tailoring the functions and definitions in
fenv.h. However, you must compile these functions with a non-default FP model, such as --
fpmode=ieee_fixed and upwards.
For all the signals in the above table, when a signal occurs, if the handler points to a function, the
equivalent of signal(sig, SIG_DFL) is executed before the call to the handler.
If the SIGILL signal is received by a handler specified to by the signal() function, the default handling
is reset.
Related concepts
1.85 ISO-compliant C library input/output characteristics on page 1-119.
1.82 How the ARM C library fulfills ISO C specification requirements on page 1-114.
3.30 Exception types recognized by the ARM floating-point environment on page 3-186.
Related references
1.83 mathlib error handling on page 1-115.
1.86 Standard C++ library implementation definition on page 1-121.
1.60 Modification of C library functions for error signaling, error handling, and program exit
on page 1-89.
4.19 __raise() on page 4-210.
4.30 __rt_raise() on page 4-221.
Related information
IEEE Standard for Floating-Point Arithmetic (IEEE 754), 1985 version.
--fpmode=model compiler option.
--exceptions, --no_exceptions compiler option.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-118
Non-Confidential
1 The ARM C and C++ Libraries
1.85 ISO-compliant C library input/output characteristics
Error Message
0 No error (errno = 0)
The following characteristics are unspecified in the ARM C library. They must be specified in an ISO-
compliant implementation:
• The validity of a filename.
• Whether remove() can remove an open file.
• The effect of calling the rename() function when the new name already exists.
• The effect of calling getenv() (the default is to return NULL, no value available).
• The effect of calling system().
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-119
Non-Confidential
1 The ARM C and C++ Libraries
1.85 ISO-compliant C library input/output characteristics
Related concepts
1.82 How the ARM C library fulfills ISO C specification requirements on page 1-114.
Related references
1.83 mathlib error handling on page 1-115.
1.84 ISO-compliant implementation of signals supported by the signal() function in the C library and
additional type arguments on page 1-117.
1.86 Standard C++ library implementation definition on page 1-121.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-120
Non-Confidential
1 The ARM C and C++ Libraries
1.86 Standard C++ library implementation definition
Related information
Rogue Wave documentation.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-121
Non-Confidential
1 The ARM C and C++ Libraries
1.87 C library functions and extensions
Related references
4.56 wcscasecmp() on page 4-248.
4.57 wcsncasecmp() on page 4-249.
4.58 wcstombs() on page 4-250.
4.2 alloca() on page 4-192.
4.36 strlcat() on page 4-228.
4.37 strlcpy() on page 4-229.
4.34 strcasecmp() on page 4-226.
4.35 strncasecmp() on page 4-227.
4.8 _fisatty() on page 4-198.
4.12 __heapstats() on page 4-202.
4.13 __heapvalid() on page 4-203.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-122
Non-Confidential
1 The ARM C and C++ Libraries
1.88 Persistence of C and C++ library names across releases of the ARM compilation tools
1.88 Persistence of C and C++ library names across releases of the ARM
compilation tools
The library naming convention described in this documentation applies to the current release of the ARM
compilation tools.
Note
Do not rely on C and C++ library names. They might change in future releases.
Related concepts
1.89 Link time selection of C and C++ libraries on page 1-124.
1.91 Compiler generated and library-resident helper functions on page 1-126.
Related tasks
1.90 Managing projects that have explicit C or C++ library names in makefiles on page 1-125.
Related references
1.92 C and C++ library naming conventions on page 1-127.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-123
Non-Confidential
1 The ARM C and C++ Libraries
1.89 Link time selection of C and C++ libraries
Related concepts
1.91 Compiler generated and library-resident helper functions on page 1-126.
1.88 Persistence of C and C++ library names across releases of the ARM compilation tools
on page 1-123.
Related tasks
1.90 Managing projects that have explicit C or C++ library names in makefiles on page 1-125.
Related references
1.92 C and C++ library naming conventions on page 1-127.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-124
Non-Confidential
1 The ARM C and C++ Libraries
1.90 Managing projects that have explicit C or C++ library names in makefiles
1.90 Managing projects that have explicit C or C++ library names in makefiles
If library names are explicitly named in a makefile, you must rebuild your project.
Procedure
1. Remove the explicit references to the old library names from the linker command-line.
2. Add --info libraries to the linker command-line and rebuild the project. This produces a list of
all the libraries in use.
3. Add the new list of libraries to the linker command-line.
Related concepts
1.89 Link time selection of C and C++ libraries on page 1-124.
1.91 Compiler generated and library-resident helper functions on page 1-126.
1.88 Persistence of C and C++ library names across releases of the ARM compilation tools
on page 1-123.
Related references
1.92 C and C++ library naming conventions on page 1-127.
Related information
--info=topic[,topic,...] linker option.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-125
Non-Confidential
1 The ARM C and C++ Libraries
1.91 Compiler generated and library-resident helper functions
Related concepts
1.89 Link time selection of C and C++ libraries on page 1-124.
1.88 Persistence of C and C++ library names across releases of the ARM compilation tools
on page 1-123.
Related tasks
1.90 Managing projects that have explicit C or C++ library names in makefiles on page 1-125.
Related references
1.92 C and C++ library naming conventions on page 1-127.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-126
Non-Confidential
1 The ARM C and C++ Libraries
1.92 C and C++ library naming conventions
root
armlib
An ARM C library. The arm_linux subdirectory contains libraries used for building
ARM Linux applications.
cpplib
An ARM C++ library.
prefix
armlinux
Libraries used when building ARM Linux applications.
c
ISO C and C++ basic runtime support.
cpp
Rogue Wave C++ library.
cpprt
The ARM C++ runtime libraries.
f
--fpmode=ieee_fixed.
IEEE-compliant library with a fixed rounding mode (round to nearest) and no inexact
exceptions.
fj
--fpmode=ieee_no_fenv.
Behaves like the fj library, but additionally flushes denormals and infinities to zero.
This library behaves like the ARM VFP in Fast mode. This is the default.
g
--fpmode=ieee_full.
IEEE-compliant library with configurable rounding mode and all IEEE exceptions.
h
Compiler support (helper) library.
m
Transcendental math functions.
mc
Non ISO C-compliant ISO C micro-library basic runtime support.
mf
Non IEEE 754 floating-point compliant micro-library support.
arch
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-127
Non-Confidential
1 The ARM C and C++ Libraries
1.92 C and C++ library naming conventions
4
An ARM only library for use with ARMv4.
t
An ARM/Thumb interworking library for use with ARMv4T.
5
An ARM/Thumb interworking library for use with ARMv5T and later.
w
A Thumb only library for use with Cortex-M3.
p
A Thumb only library for use with Cortex-M1 and Cortex-M0.
2
A combined ARM and Thumb library for use with Cortex-A and Cortex-R series
processors. You can prevent this library being selected using the linker option --
no_thumb2_library.
fpu
m
A variant of the library for processors that have single-precision hardware floating-
point only, such as Cortex-M4.
v
Uses VFP instruction set.
s
Soft VFP.
Note
If none of v, m, or s are present in a library name, the library provides no floating-point
support.
entrant
e
Position-independent access to static data.
f
FPIC addressing is enabled.
Note
If neither e nor f is present in a library name, the library either:
• Uses position-dependent access to static data. This is the case for the main C
libraries with prefixes c or mc.
• Does not access static data, or does so only with the help of the main C library. This
is the case for fplib and mathlib libraries with prefixes fz, fj, f, g, mf, or m.
enum
n
Compatible with the compiler option, --enum_is_int.
wchar
u
Indicates the size of wchar_t.When present, the library is compatible with the compiler
option, --wchar32. Otherwise, it is compatible with --wchar16
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-128
Non-Confidential
1 The ARM C and C++ Libraries
1.92 C and C++ library naming conventions
endian
l
Little-endian.
b
Big-endian.
For example:
*armlib/c_4.b
*cpplib/cpprt_5f.l
Note
Not all variant/name combinations are valid. See the armlib and cpplib directories for the libraries that
are supplied with the ARM compilation tools.
The linker command-line option --info libraries provides information on every library that is
automatically selected for the link stage.
Related concepts
1.89 Link time selection of C and C++ libraries on page 1-124.
1.91 Compiler generated and library-resident helper functions on page 1-126.
1.88 Persistence of C and C++ library names across releases of the ARM compilation tools
on page 1-123.
Related tasks
1.90 Managing projects that have explicit C or C++ library names in makefiles on page 1-125.
Related information
--enum_is_int compiler option.
--wchar16 compiler option.
--wchar32 compiler option.
--info=topic[,topic,...] linker option.
--thumb2_library linker option.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-129
Non-Confidential
1 The ARM C and C++ Libraries
1.93 Using macro__ARM_WCHAR_NO_IO to disable FILE declaration and wide I/O function prototypes
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-130
Non-Confidential
1 The ARM C and C++ Libraries
1.94 Using library functions with execute-only memory
If your application calls library functions, the library objects included in the image are not execute-only
compliant. You must ensure these objects are not assigned to an execute-only memory region.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 1-131
Non-Confidential
Chapter 2
The ARM C Micro-library
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 2-132
Non-Confidential
2 The ARM C Micro-library
2.1 About microlib
Note
Microlib does not attempt to be an ISO C-compliant library.
Microlib is highly optimized for small code size. It has less functionality than the default C library and
some ISO C features are completely missing. Some library functions are also slower.
Functions in microlib are responsible for:
• Creating an environment that a C program can execute in. This includes:
— Creating a stack.
— Creating a heap, if required.
— Initializing the parts of the library the program uses.
• Starting execution by calling main().
Related concepts
2.2 Differences between microlib and the default C library on page 2-134.
2.3 Library heap usage requirements of the ARM C micro-library on page 2-135.
2.4 ISO C features missing from microlib on page 2-136.
2.5 Building an application with microlib on page 2-138.
2.8 Entering and exiting programs linked with microlib on page 2-142.
Related tasks
2.6 Creating an initial stack pointer for use with microlib on page 2-140.
2.7 Creating the heap for use with microlib on page 2-141.
Related references
2.9 Tailoring the microlib input/output functions on page 2-143.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 2-133
Non-Confidential
2 The ARM C Micro-library
2.2 Differences between microlib and the default C library
Related concepts
2.1 About microlib on page 2-133.
2.3 Library heap usage requirements of the ARM C micro-library on page 2-135.
2.4 ISO C features missing from microlib on page 2-136.
2.5 Building an application with microlib on page 2-138.
2.8 Entering and exiting programs linked with microlib on page 2-142.
Related tasks
2.6 Creating an initial stack pointer for use with microlib on page 2-140.
2.7 Creating the heap for use with microlib on page 2-141.
Related references
2.9 Tailoring the microlib input/output functions on page 2-143.
Related information
--fpmode=model compiler option.
#pragma import(__use_full_stdio).
#pragma import(__use_smaller_memcpy).
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 2-134
Non-Confidential
2 The ARM C Micro-library
2.3 Library heap usage requirements of the ARM C micro-library
Note
The size of heap memory allocated for fopen() might change in future releases.
Related concepts
1.4 Library heap usage requirements of the ARM C and C++ libraries on page 1-22.
2.1 About microlib on page 2-133.
2.2 Differences between microlib and the default C library on page 2-134.
2.4 ISO C features missing from microlib on page 2-136.
2.5 Building an application with microlib on page 2-138.
2.8 Entering and exiting programs linked with microlib on page 2-142.
Related tasks
2.6 Creating an initial stack pointer for use with microlib on page 2-140.
2.7 Creating the heap for use with microlib on page 2-141.
Related references
2.9 Tailoring the microlib input/output functions on page 2-143.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 2-135
Non-Confidential
2 The ARM C Micro-library
2.4 ISO C features missing from microlib
Related concepts
2.1 About microlib on page 2-133.
2.2 Differences between microlib and the default C library on page 2-134.
2.3 Library heap usage requirements of the ARM C micro-library on page 2-135.
2.5 Building an application with microlib on page 2-138.
2.8 Entering and exiting programs linked with microlib on page 2-142.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 2-136
Non-Confidential
2 The ARM C Micro-library
2.4 ISO C features missing from microlib
Related tasks
2.6 Creating an initial stack pointer for use with microlib on page 2-140.
2.7 Creating the heap for use with microlib on page 2-141.
Related references
2.9 Tailoring the microlib input/output functions on page 2-143.
1.31 C library API definitions for targeting a different environment on page 1-54.
1.32 Building an application without the C library on page 1-55.
4.3 clock() on page 4-193.
4.4 _clock_init() on page 4-194.
Related information
#pragma import(__use_full_stdio).
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 2-137
Non-Confidential
2 The ARM C Micro-library
2.5 Building an application with microlib
Compiler option
Assembler option
armcc -c main.c
armcc -c extra.c
armasm --library_type=microlib more.s
armlink -o image.axf main.o extra.o more.o
The request to the linker to use microlib is made as a result of assembling more.s with --
library_type=microlib.
Linker option
armcc -c main.c
armcc -c extra.c
armlink --library_type=microlib -o image.axf main.o extra.o
Neither object file contains the attribute requesting that the linker link against microlib, so the linker
selects microlib as a result of being explicitly asked to do so on the command line.
Related concepts
2.1 About microlib on page 2-133.
2.2 Differences between microlib and the default C library on page 2-134.
2.3 Library heap usage requirements of the ARM C micro-library on page 2-135.
2.4 ISO C features missing from microlib on page 2-136.
2.8 Entering and exiting programs linked with microlib on page 2-142.
Related tasks
2.6 Creating an initial stack pointer for use with microlib on page 2-140.
2.7 Creating the heap for use with microlib on page 2-141.
Related references
2.9 Tailoring the microlib input/output functions on page 2-143.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 2-138
Non-Confidential
2 The ARM C Micro-library
2.5 Building an application with microlib
Related information
--library_type=lib compiler option.
input-file-list linker option.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 2-139
Non-Confidential
2 The ARM C Micro-library
2.6 Creating an initial stack pointer for use with microlib
Procedure
• Use a scatter file.
The scatter file method uses ARM_LIB_STACK and ARM_LIB_STACKHEAP.
• Define a symbol, __initial_sp, to be equal to the top of the stack.
The initial stack pointer must be aligned to a multiple of eight bytes.
Examples
Related concepts
2.1 About microlib on page 2-133.
2.2 Differences between microlib and the default C library on page 2-134.
2.3 Library heap usage requirements of the ARM C micro-library on page 2-135.
2.4 ISO C features missing from microlib on page 2-136.
2.5 Building an application with microlib on page 2-138.
2.8 Entering and exiting programs linked with microlib on page 2-142.
1.67 Defining __initial_sp, __heap_base and __heap_limit on page 1-97.
Related tasks
2.7 Creating the heap for use with microlib on page 2-141.
Related references
2.9 Tailoring the microlib input/output functions on page 2-143.
Related information
About scatter-loading.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 2-140
Non-Confidential
2 The ARM C Micro-library
2.7 Creating the heap for use with microlib
Procedure
• Use a scatter file.
The scatter file method uses ARM_LIB_HEAP and ARM_LIB_STACKHEAP.
• Define symbols __heap_base and __heap_limit.
The __heap_limit symbol must point to the byte beyond the last byte in the heap region.
Examples
Related concepts
2.1 About microlib on page 2-133.
2.2 Differences between microlib and the default C library on page 2-134.
2.3 Library heap usage requirements of the ARM C micro-library on page 2-135.
2.4 ISO C features missing from microlib on page 2-136.
2.5 Building an application with microlib on page 2-138.
2.8 Entering and exiting programs linked with microlib on page 2-142.
1.67 Defining __initial_sp, __heap_base and __heap_limit on page 1-97.
Related tasks
2.6 Creating an initial stack pointer for use with microlib on page 2-140.
Related references
2.9 Tailoring the microlib input/output functions on page 2-143.
Related information
About scatter-loading.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 2-141
Non-Confidential
2 The ARM C Micro-library
2.8 Entering and exiting programs linked with microlib
Related concepts
2.1 About microlib on page 2-133.
2.2 Differences between microlib and the default C library on page 2-134.
2.3 Library heap usage requirements of the ARM C micro-library on page 2-135.
2.4 ISO C features missing from microlib on page 2-136.
2.5 Building an application with microlib on page 2-138.
Related tasks
2.6 Creating an initial stack pointer for use with microlib on page 2-140.
2.7 Creating the heap for use with microlib on page 2-141.
Related references
2.9 Tailoring the microlib input/output functions on page 2-143.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 2-142
Non-Confidential
2 The ARM C Micro-library
2.9 Tailoring the microlib input/output functions
Note
Conversions that are not supported in microlib are %lc, %ls and %a.
Related concepts
2.1 About microlib on page 2-133.
2.2 Differences between microlib and the default C library on page 2-134.
2.3 Library heap usage requirements of the ARM C micro-library on page 2-135.
2.4 ISO C features missing from microlib on page 2-136.
2.5 Building an application with microlib on page 2-138.
2.8 Entering and exiting programs linked with microlib on page 2-142.
Related tasks
2.6 Creating an initial stack pointer for use with microlib on page 2-140.
2.7 Creating the heap for use with microlib on page 2-141.
Related references
1.70 Tailoring input/output functions in the C and C++ libraries on page 1-100.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 2-143
Non-Confidential
Chapter 3
Floating-point Support
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 3-144
Non-Confidential
3 Floating-point Support
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 3-145
Non-Confidential
3 Floating-point Support
3.1 About floating-point support
Related concepts
3.2 The software floating-point library, fplib on page 3-147.
3.22 IEEE 754 arithmetic on page 3-175.
Related information
--fpu=name compiler option.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 3-146
Non-Confidential
3 Floating-point Support
3.2 The software floating-point library, fplib
Related concepts
3.3 Calling fplib routines on page 3-148.
Related references
3.4 fplib arithmetic on numbers in a particular format on page 3-149.
3.5 fplib conversions between floats, long longs, doubles, and ints on page 3-151.
3.6 fplib comparisons between floats and doubles on page 3-153.
3.7 fplib C99 functions on page 3-155.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 3-147
Non-Confidential
3 Floating-point Support
3.3 Calling fplib routines
Note
For a double in registers r0 and r1, the register that holds the high 32 bits of the double depends on
whether your program is little-endian or big-endian.
Software floating-point library routines are declared in one of two header files:
• A small number of fplib routines that implement C99 functionality are declared in the standard
header file math.h.
• All other fplib routines are declared in the header file rt_fp.h. You can include this file if you want
to call an fplib routine directly.
To call a function from assembler, the software floating-point function is named fn. For example, to call
the nextafter() function, implement the following code:
IMPORT nextafter
BL nextafter
Related concepts
3.2 The software floating-point library, fplib on page 3-147.
Related references
3.4 fplib arithmetic on numbers in a particular format on page 3-149.
3.5 fplib conversions between floats, long longs, doubles, and ints on page 3-151.
3.6 fplib comparisons between floats and doubles on page 3-153.
3.7 fplib C99 functions on page 3-155.
Related information
Application Binary Interface (ABI) for the ARM Architecture.
Compiler support for floating-point computations and linkage.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 3-148
Non-Confidential
3 Floating-point Support
3.4 fplib arithmetic on numbers in a particular format
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 3-149
Non-Confidential
3 Floating-point Support
3.4 fplib arithmetic on numbers in a particular format
Related concepts
3.2 The software floating-point library, fplib on page 3-147.
3.3 Calling fplib routines on page 3-148.
Related references
3.5 fplib conversions between floats, long longs, doubles, and ints on page 3-151.
3.6 fplib comparisons between floats and doubles on page 3-153.
3.7 fplib C99 functions on page 3-155.
Related information
Application Binary Interface (ABI) for the ARM Architecture.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 3-150
Non-Confidential
3 Floating-point Support
3.5 fplib conversions between floats, long longs, doubles, and ints
3.5 fplib conversions between floats, long longs, doubles, and ints
fplib provides a number of routines to perform conversions between number formats.
The following table describes these routines.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 3-151
Non-Confidential
3 Floating-point Support
3.5 fplib conversions between floats, long longs, doubles, and ints
Notes on rounding
a
Rounded toward zero, independently of the current rounding mode. This is because the C
standard requires implicit conversions to integers to round this way, so it is convenient not to
have to change the rounding mode to do so. Each function has a corresponding function with _r
on the end of its name, that performs the same operation but rounds according to the current
mode.
Related concepts
3.2 The software floating-point library, fplib on page 3-147.
3.3 Calling fplib routines on page 3-148.
Related references
3.4 fplib arithmetic on numbers in a particular format on page 3-149.
3.6 fplib comparisons between floats and doubles on page 3-153.
3.7 fplib C99 functions on page 3-155.
Related information
Application Binary Interface (ABI) for the ARM Architecture.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 3-152
Non-Confidential
3 Floating-point Support
3.6 fplib comparisons between floats and doubles
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 3-153
Non-Confidential
3 Floating-point Support
3.6 fplib comparisons between floats and doubles
b
Causes an Invalid Operation exception if either argument is a NaN, even a quiet NaN. Other
functions only cause Invalid Operation if an argument is an SNaN. QNaNs return not equal
when compared to anything, including other QNaNs (so comparing a QNaN to the same QNaN
still returns not equal).
c
Returns VFP-type status flags in the CPSR. Also returns VFP-type status flags in the top four bits
of r0, meaning that it is possible to use this function from C. This function is declared in
rt_fp.h.
Related concepts
3.2 The software floating-point library, fplib on page 3-147.
3.3 Calling fplib routines on page 3-148.
Related references
3.4 fplib arithmetic on numbers in a particular format on page 3-149.
3.5 fplib conversions between floats, long longs, doubles, and ints on page 3-151.
3.7 fplib C99 functions on page 3-155.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 3-154
Non-Confidential
3 Floating-point Support
3.7 fplib C99 functions
nexttoward double, long double double Next representable value after x towards y 7.12.11.4
nexttowardf float, long double float Next representable value after x towards y 7.12.11.4
nexttowardl 2 long double long double Next representable value after x towards y 7.12.11.4
Related concepts
3.2 The software floating-point library, fplib on page 3-147.
3.3 Calling fplib routines on page 3-148.
Related references
3.4 fplib arithmetic on numbers in a particular format on page 3-149.
3.5 fplib conversions between floats, long longs, doubles, and ints on page 3-151.
3.6 fplib comparisons between floats and doubles on page 3-153.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 3-155
Non-Confidential
3 Floating-point Support
3.8 Controlling the ARM floating-point environment
Related concepts
3.9 Floating-point functions for compatibility with Microsoft products on page 3-157.
3.17 Writing a custom exception trap handler on page 3-165.
3.18 Example of a custom exception handler on page 3-170.
3.19 Exception trap handling by signals on page 3-172.
3.29 Exceptions arising from IEEE 754 floating-point arithmetic on page 3-185.
Related references
3.10 C99-compatible functions for controlling the ARM floating-point environment on page 3-158.
3.16 ARM floating-point compiler extensions to the C99 interface on page 3-164.
5.5 __ieee_status() on page 5-263.
5.3 __fp_status() on page 5-260.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 3-156
Non-Confidential
3 Floating-point Support
3.9 Floating-point functions for compatibility with Microsoft products
Related concepts
3.8 Controlling the ARM floating-point environment on page 3-156.
Related references
5.1 _clearfp() on page 5-257.
5.2 _controlfp() on page 5-258.
5.8 _statusfp() on page 5-268.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 3-157
Non-Confidential
3 Floating-point Support
3.10 C99-compatible functions for controlling the ARM floating-point environment
Note
This functionality requires you to select a floating-point model that supports exceptions, such as --
fpmode=ieee_full or --fpmode=ieee_fixed.
C99 defines two data types, fenv_t and fexcept_t. The C99 standard does not give information about
these types, so for portable code you must treat them as opaque. The compiler defines them to be
structure types.
The type fenv_t is defined to hold all the information about the current floating-point environment. This
comprises:
• The rounding mode.
• The exception sticky flags.
• Whether each exception is masked.
• What handlers are installed, if any.
The type fexcept_t is defined to hold all the information relevant to a given set of exceptions.
Related concepts
3.8 Controlling the ARM floating-point environment on page 3-156.
3.12 Exception flag handling on page 3-160.
3.13 Functions for handling rounding modes on page 3-161.
3.14 Functions for saving and restoring the whole floating-point environment on page 3-162.
3.15 Functions for temporarily disabling exceptions on page 3-163.
3.17 Writing a custom exception trap handler on page 3-165.
3.18 Example of a custom exception handler on page 3-170.
3.19 Exception trap handling by signals on page 3-172.
3.29 Exceptions arising from IEEE 754 floating-point arithmetic on page 3-185.
Related references
3.11 C99 rounding mode and floating-point exception macros on page 3-159.
3.16 ARM floating-point compiler extensions to the C99 interface on page 3-164.
Related information
--fpmode=model compiler option.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 3-158
Non-Confidential
3 Floating-point Support
3.11 C99 rounding mode and floating-point exception macros
Note
The following functionality requires you to select a floating-point model that supports exceptions, such
as --fpmode=ieee_full or --fpmode=ieee_fixed.
The exception macros are bit fields. The macro FE_ALL_EXCEPT is the bitwise OR of all of them.
Related concepts
3.13 Functions for handling rounding modes on page 3-161.
Related references
3.10 C99-compatible functions for controlling the ARM floating-point environment on page 3-158.
Related information
--fpmode=model compiler option.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 3-159
Non-Confidential
3 Floating-point Support
3.12 Exception flag handling
Note
The following functionality requires you to select a floating-point model that supports exceptions, such
as --fpmode=ieee_full or --fpmode=ieee_fixed.
The feclearexcept() function clears the sticky flags for the given exceptions. The fetestexcept()
function returns the bitwise OR of the sticky flags for the given exceptions, so that if the Overflow flag
was set but the Underflow flag was not, then calling fetestexcept(FE_OVERFLOW|FE_UNDERFLOW)
would return FE_OVERFLOW.
The feraiseexcept() function raises the given exceptions, in unspecified order. If an exception trap is
enabled for an exception raised this way, it is called.
C99 also provides functions to save and restore all information about a given exception. This includes
the sticky flag, whether the exception is trapped, and the address of the trap handler, if any. These
functions are:
void fegetexceptflag(fexcept_t *flagp, int excepts);
The fegetexceptflag() function copies all the information relating to the given exceptions into the
fexcept_t variable provided. The fesetexceptflag() function copies all the information relating to
the given exceptions from the fexcept_t variable into the current floating-point environment.
Note
You can use fesetexceptflag() to set the sticky flag of a trapped exception to 1 without calling the
trap handler, whereas feraiseexcept() calls the trap handler for any trapped exception.
Related references
3.10 C99-compatible functions for controlling the ARM floating-point environment on page 3-158.
Related information
--fpmode=model compiler option.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 3-160
Non-Confidential
3 Floating-point Support
3.13 Functions for handling rounding modes
Note
The following functionality requires you to select a floating-point model that supports exceptions, such
as --fpmode=ieee_full or --fpmode=ieee_fixed.
The fegetround() function returns the current rounding mode. The current rounding mode has a value
equal to one of the C99 rounding mode macros or exceptions.
The fesetround() function sets the current rounding mode to the value provided. fesetround() returns
zero for success, or nonzero if its argument is not a valid rounding mode.
Related references
3.10 C99-compatible functions for controlling the ARM floating-point environment on page 3-158.
3.11 C99 rounding mode and floating-point exception macros on page 3-159.
Related information
--fpmode=model compiler option.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 3-161
Non-Confidential
3 Floating-point Support
3.14 Functions for saving and restoring the whole floating-point environment
3.14 Functions for saving and restoring the whole floating-point environment
The fegetenv and fesetenv functions let you save and restore the entire floating-point environment.
Note
The following functionality requires you to select a floating-point model that supports exceptions, such
as --fpmode=ieee_full or --fpmode=ieee_fixed.
The fegetenv() function stores the current state of the floating-point environment into the fenv_t
variable provided. The fesetenv() function restores the environment from the variable provided.
Like fesetexceptflag(), fesetenv() does not call trap handlers when it sets the sticky flags for
trapped exceptions.
Related references
3.10 C99-compatible functions for controlling the ARM floating-point environment on page 3-158.
Related information
--fpmode=model compiler option.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 3-162
Non-Confidential
3 Floating-point Support
3.15 Functions for temporarily disabling exceptions
Note
The following functionality requires you to select a floating-point model that supports exceptions, such
as --fpmode=ieee_full or --fpmode=ieee_fixed.
These functions let you avoid risking exception traps when executing code that might cause exceptions.
This is useful when, for example, trapped exceptions are using the ARM default behavior. The default is
to cause SIGFPE and terminate the application.
int feholdexcept(fenv_t *envp);
The feholdexcept() function saves the current floating-point environment in the fenv_t variable
provided, sets all exceptions to be untrapped, and clears all the exception sticky flags. You can then
execute code that might cause unwanted exceptions, and make sure the sticky flags for those exceptions
are cleared. Then you can call feupdateenv(). This restores any exception traps and calls them if
necessary. For example, suppose you have a function, frob(), that might cause the Underflow or Invalid
Operation exceptions (assuming both exceptions are trapped). You are not interested in Underflow, but
you want to know if an invalid operation is attempted. You can implement the following code to do this:
fenv_t env;
feholdexcept(&env);
frob();
feclearexcept(FE_UNDERFLOW);
feupdateenv(&env);
Then, if the frob() function raises Underflow, it is cleared again by feclearexcept(), so no trap
occurs when feupdateenv() is called. However, if frob() raises Invalid Operation, the sticky flag is set
when feupdateenv() is called, so the trap handler is invoked.
This mechanism is provided by C99 because C99 specifies no way to change exception trapping for
individual exceptions. A better method is to use __ieee_status() to disable the Underflow trap while
leaving the Invalid Operation trap enabled. This has the advantage that the Invalid Operation trap handler
is provided with all the information about the invalid operation (that is, what operation was being
performed, and on what data), and can invent a result for the operation. Using the C99 method, the
Invalid Operation trap handler is called after the fact, receives no information about the cause of the
exception, and is called too late to provide a substitute result.
Related references
3.10 C99-compatible functions for controlling the ARM floating-point environment on page 3-158.
5.5 __ieee_status() on page 5-263.
Related information
--fpmode=model compiler option.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 3-163
Non-Confidential
3 Floating-point Support
3.16 ARM floating-point compiler extensions to the C99 interface
Note
The following functionality requires you to select a floating-point model that supports exceptions, such
as --fpmode=ieee_full or --fpmode=ieee_fixed.
The types fenv_t and fexcept_t are not defined by C99 to be anything in particular. The ARM
compiler defines them both to be the same structure type:
typedef struct{
unsigned statusword;
__ieee_handler_t __invalid_handler;
__ieee_handler_t __divbyzero_handler;
__ieee_handler_t __overflow_handler;
__ieee_handler_t __underflow_handler;
__ieee_handler_t __inexact_handler;
} fenv_t, fexcept_t;
Related concepts
3.8 Controlling the ARM floating-point environment on page 3-156.
3.17 Writing a custom exception trap handler on page 3-165.
3.18 Example of a custom exception handler on page 3-170.
3.17 Writing a custom exception trap handler on page 3-165.
3.18 Example of a custom exception handler on page 3-170.
3.19 Exception trap handling by signals on page 3-172.
3.29 Exceptions arising from IEEE 754 floating-point arithmetic on page 3-185.
Related references
3.10 C99-compatible functions for controlling the ARM floating-point environment on page 3-158.
5.5 __ieee_status() on page 5-263.
Related information
--fpmode=model compiler option.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 3-164
Non-Confidential
3 Floating-point Support
3.17 Writing a custom exception trap handler
Note
The following functionality requires you to select a floating-point model that supports exceptions, such
as --fpmode=ieee_full or --fpmode=ieee_fixed.
If you want to install a custom exception trap handler, declare it as a function like this:
__softfp __ieee_value_t myhandler(__ieee_value_t op1,
__ieee_value_t op2,
__ieee_edata_t edata);
The value returned from this function is of type __ieee_value_t and is used as the result of the
operation that caused the exception.
The function must be declared __softfp in order to be usable as a handler.
The parameters to this function are:
op1, op2
These specify the operands, or the intermediate result, for the operation that caused the
exception:
• For the Invalid Operation and Divide by Zero exceptions, the original operands are supplied.
• For the Inexact Result exception, all that is supplied is the ordinary result that would have
been returned anyway. This is provided in op1.
• For the Overflow exception, an intermediate result is provided. This result is calculated by
working out what the operation would have returned if the exponent range had been big
enough, and then adjusting the exponent so that it fits in the format. The exponent is adjusted
by 192 (0xC0) in single-precision, and by 1536 (0x600) in double-precision.
If Overflow happens when converting a double to a float, the result is supplied in double
format, rounded to single-precision, with the exponent biased by 192.
• For the Underflow exception, a similar intermediate result is produced, but the bias value is
added to the exponent instead of being subtracted. The edata parameter also contains a flag
to show whether the intermediate result has had to be rounded up, down, or not at all.
The type __ieee_value_t is defined as a union of all the possible types that an operand can be
passed as:
typedef union{
float __f;
float __s;
double __d;
short __h;
unsigned short __uh;
int __i;
unsigned int __ui;
long long __l;
unsigned long long __ul;
...
/* __STRICT_ANSI__ */
struct { int __word1, __word2; } __str;
} __ieee_value_t; /* in and out values passed to traps */
Note
If you do not compile with --strict, and you have code that used the older definition of
__ieee_value_t, your older code still works. See the file fenv.h for more information.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 3-165
Non-Confidential
3 Floating-point Support
3.17 Writing a custom exception trap handler
edata
This contains flags that give information about the exception that occurred, and what operation
was being performed. (The type __ieee_edata_t is a synonym for unsigned int.)
• Use the same handler function for more than one exception type (the function can test these
bits to tell what exception it is supposed to handle).
• Determine whether Overflow and Underflow intermediate results have been rounded or are
exact.
Because the FE_EX_INEXACT bit can be set in combination with either FE_EX_OVERFLOW or
FE_EX_UNDERFLOW, you must determine the type of exception that actually occurred by testing
Overflow and Underflow before testing Inexact.
edata & FE_EX_FLUSHZERO
This is nonzero if the FZ bit was set when the operation was performed.
edata & FE_EX_ROUND_MASK
This gives the rounding mode that applies to the operation. This is normally the same as the
current rounding mode, unless the operation that caused the exception was a routine such as
_ffix, that always rounds toward zero. The available rounding mode values are
FE_EX_ROUND_NEAREST, FE_EX_ROUND_PLUSINF, FE_EX_ROUND_MINUSINF and
FE_EX_ROUND_ZERO.
edata & FE_EX_INTYPE_MASK
This gives the type of the operands to the function, as one of the type values shown in The
following table.
FE_EX_INTYPE_DOUBLE double
FE_EX_INTYPE_HALF short
FE_EX_INTYPE_INT int
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 3-166
Non-Confidential
3 Floating-point Support
3.17 Writing a custom exception trap handler
FE_EX_OUTTYPE_DOUBLE double
FE_EX_OUTTYPE_HALF short
FE_EX_OUTTYPE_INT int
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 3-167
Non-Confidential
3 Floating-point Support
3.17 Writing a custom exception trap handler
Note
The FE_EX_INTYPE_MASK flag only specifies the type of the first operand. The second operand is
always an int.
Note
Both operands are the same type. Calls to nexttoward cause the value of the second operand to
change to a value that is of the same type as the first operand. This does not affect the result.
FE_EX_FN_RAISE The exception was raised explicitly, by feraiseexcept() or feupdateenv(). In this case,
almost nothing in the edata word is valid.
When the operation is a comparison, the result must be returned as if it were an int, and must
be one of the four values shown in the following table.
Input and output types are the same for all operations except Compare and Convert.
Flag Comparison
FE_EX_CMPRET_LESS op1 is less than op2
FE_EX_CMPRET_EQUAL op1 is equal to op2
FE_EX_CMPRET_GREATER op1 is greater than op2
FE_EX_CMPRET_UNORDERED op1 and op2 are not comparable
Related concepts
3.18 Example of a custom exception handler on page 3-170.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 3-168
Non-Confidential
3 Floating-point Support
3.17 Writing a custom exception trap handler
Related references
3.16 ARM floating-point compiler extensions to the C99 interface on page 3-164.
3.16 ARM floating-point compiler extensions to the C99 interface on page 3-164.
3.10 C99-compatible functions for controlling the ARM floating-point environment on page 3-158.
5.5 __ieee_status() on page 5-263.
Related information
--fpmode=model compiler option.
--strict, --no_strict compiler option.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 3-169
Non-Confidential
3 Floating-point Support
3.18 Example of a custom exception handler
Note
The following functionality requires you to select a floating-point model that supports exceptions, such
as --fpmode=ieee_full or --fpmode=ieee_fixed.
Suppose you are converting some Fortran code into C. The Fortran numerical standard requires 0 divided
by 0 to be 1, whereas IEEE 754 defines 0 divided by 0 to be an Invalid Operation and so by default it
returns a quiet NaN. The Fortran code is likely to rely on this behavior, and rather than modifying the
code, it is probably easier to make 0 divided by 0 return 1.
After the handler is installed, dividing 0.0 by 0.0 returns 1.0.
Custom exception handler
#include <fenv.h>
#include <signal.h>
#include <stdio.h>
__softfp __ieee_value_t myhandler(__ieee_value_t op1, __ieee_value_t op2,
__ieee_edata_t edata)
{
__ieee_value_t ret;
if ((edata & FE_EX_FN_MASK) == FE_EX_FN_DIV)
{
if ((edata & FE_EX_INTYPE_MASK) == FE_EX_INTYPE_FLOAT)
{
if (op1.f == 0.0 && op2.f == 0.0)
{
ret.f = 1.0;
return ret;
}
}
if ((edata & FE_EX_INTYPE_MASK) == FE_EX_INTYPE_DOUBLE)
{
if (op1.d == 0.0 && op2.d == 0.0)
{
ret.d = 1.0;
return ret;
}
}
}
/* For all other invalid operations, raise SIGFPE as usual */
raise(SIGFPE);
}
int main(void)
{
float i, j, k;
fenv_t env;
fegetenv(&env);
env.statusword |= FE_IEEE_MASK_INVALID;
env.invalid_handler = myhandler;
fesetenv(&env);
i = 0.0;
j = 0.0;
k = i/j;
printf("k is %f\n", k);
}
Related concepts
3.17 Writing a custom exception trap handler on page 3-165.
3.19 Exception trap handling by signals on page 3-172.
3.29 Exceptions arising from IEEE 754 floating-point arithmetic on page 3-185.
3.8 Controlling the ARM floating-point environment on page 3-156.
Related references
3.16 ARM floating-point compiler extensions to the C99 interface on page 3-164.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 3-170
Non-Confidential
3 Floating-point Support
3.18 Example of a custom exception handler
3.16 ARM floating-point compiler extensions to the C99 interface on page 3-164.
3.10 C99-compatible functions for controlling the ARM floating-point environment on page 3-158.
Related information
--fpmode=model compiler option.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 3-171
Non-Confidential
3 Floating-point Support
3.19 Exception trap handling by signals
Note
The following functionality requires you to select a floating-point model that supports exceptions, such
as --fpmode=ieee_full or --fpmode=ieee_fixed.
If an exception is trapped but the trap handler address is set to NULL, a default trap handler is used.
The default trap handler raises a SIGFPE signal. The default handler for SIGFPE prints an error
message and terminates the program.
If you trap SIGFPE, you can declare your signal handler function to have a second parameter that tells
you the type of floating-point exception that occurred. This feature is provided for compatibility with
Microsoft products. The values are _FPE_INVALID, _FPE_ZERODIVIDE, _FPE_OVERFLOW,
_FPE_UNDERFLOW and _FPE_INEXACT. They are defined in float.h. For example:
To generate your own SIGFPE signals with this extra information, you can call the function
__rt_raise() instead of the ISO function raise(). For example:
__rt_raise(SIGFPE, _FPE_INVALID);
Related concepts
3.17 Writing a custom exception trap handler on page 3-165.
3.18 Example of a custom exception handler on page 3-170.
3.29 Exceptions arising from IEEE 754 floating-point arithmetic on page 3-185.
3.8 Controlling the ARM floating-point environment on page 3-156.
Related references
3.16 ARM floating-point compiler extensions to the C99 interface on page 3-164.
3.10 C99-compatible functions for controlling the ARM floating-point environment on page 3-158.
4.30 __rt_raise() on page 4-221.
Related information
--fpmode=model compiler option.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 3-172
Non-Confidential
3 Floating-point Support
3.20 Using C99 signalling NaNs provided by mathlib (_WANT_SNAN)
This macro must be defined before you include any standard C headers. If your application is comprised
of two or more translation units, either all or none of them must define _WANT_SNAN. That is, the
definition must be consistent for any given application.
You must also use the relevant command-line option when you compile your source code. This is
associated with the predefined macro, __SUPPORT_SNAN__.
Related information
WG14 - C N965, Optional support for Signaling NaNs.
Predefined macros.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 3-173
Non-Confidential
3 Floating-point Support
3.21 mathlib double and single-precision floating-point functions
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 3-174
Non-Confidential
3 Floating-point Support
3.22 IEEE 754 arithmetic
Related concepts
3.24 Single precision data type for IEEE 754 arithmetic on page 3-177.
3.25 Double precision data type for IEEE 754 arithmetic on page 3-179.
3.28 IEEE 754 arithmetic and rounding on page 3-184.
3.29 Exceptions arising from IEEE 754 floating-point arithmetic on page 3-185.
Related references
3.23 Basic data types for IEEE 754 arithmetic on page 3-176.
3.26 Sample single precision floating-point values for IEEE 754 arithmetic on page 3-180.
3.27 Sample double precision floating-point values for IEEE 754 arithmetic on page 3-182.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 3-175
Non-Confidential
3 Floating-point Support
3.23 Basic data types for IEEE 754 arithmetic
Related concepts
3.22 IEEE 754 arithmetic on page 3-175.
3.24 Single precision data type for IEEE 754 arithmetic on page 3-177.
3.25 Double precision data type for IEEE 754 arithmetic on page 3-179.
3.28 IEEE 754 arithmetic and rounding on page 3-184.
3.29 Exceptions arising from IEEE 754 floating-point arithmetic on page 3-185.
Related references
3.26 Sample single precision floating-point values for IEEE 754 arithmetic on page 3-180.
3.27 Sample double precision floating-point values for IEEE 754 arithmetic on page 3-182.
Related information
IEEE Standard for Floating-Point Arithmetic (IEEE 754), 1985 version.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 3-176
Non-Confidential
3 Floating-point Support
3.24 Single precision data type for IEEE 754 arithmetic
31 30 23 22 0
S Exp Frac
The S field gives the sign of the number. It is 0 for positive, or 1 for negative.
The Exp field gives the exponent of the number, as a power of two. It is biased by 0x7F (127), so that
very small numbers have exponents near zero and very large numbers have exponents near 0xFF (255).
So, for example:
• If Exp = 0x7D (125), the number is between 0.25 and 0.5 (not including 0.5).
• If Exp = 0x7E (126), the number is between 0.5 and 1.0 (not including 1.0).
• If Exp = 0x7F (127), the number is between 1.0 and 2.0 (not including 2.0).
• If Exp = 0x80 (128), the number is between 2.0 and 4.0 (not including 4.0).
• If Exp = 0x81 (129), the number is between 4.0 and 8.0 (not including 8.0).
The Frac field gives the fractional part of the number. It usually has an implicit 1 bit on the front that is
not stored to save space.
So if Exp is 0x7F, for example:
• If Frac = 00000000000000000000000 (binary), the number is 1.0.
• If Frac = 10000000000000000000000 (binary), the number is 1.5.
• If Frac = 01000000000000000000000 (binary), the number is 1.25.
• If Frac = 11000000000000000000000 (binary), the number is 1.75.
So in general, the numeric value of a bit pattern in this format is given by the formula:
(–1)S * 2(Exp–0x7F) * (1 + Frac * 2–23)
Numbers stored in this form are called normalized numbers.
The maximum and minimum exponent values, 0 and 255, are special cases. Exponent 255 can represent
infinity and store Not a Number (NaN) values. Infinity can occur as a result of dividing by zero, or as a
result of computing a value that is too large to store in this format. NaN values are used for special
purposes. Infinity is stored by setting Exp to 255 and Frac to all zeros. If Exp is 255 and Frac is nonzero,
the bit pattern represents a NaN.
Exponent 0 can represent very small numbers in a special way. If Exp is zero, then the Frac field has no
implicit 1 on the front. This means that the format can store 0.0, by setting both Exp and Frac to all 0
bits. It also means that numbers that are too small to store using Exp >= 1 are stored with less precision
than the ordinary 23 bits. These are called denormals.
Related concepts
3.22 IEEE 754 arithmetic on page 3-175.
3.25 Double precision data type for IEEE 754 arithmetic on page 3-179.
3.28 IEEE 754 arithmetic and rounding on page 3-184.
3.29 Exceptions arising from IEEE 754 floating-point arithmetic on page 3-185.
Related references
3.23 Basic data types for IEEE 754 arithmetic on page 3-176.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 3-177
Non-Confidential
3 Floating-point Support
3.24 Single precision data type for IEEE 754 arithmetic
3.26 Sample single precision floating-point values for IEEE 754 arithmetic on page 3-180.
3.27 Sample double precision floating-point values for IEEE 754 arithmetic on page 3-182.
Related information
IEEE Standard for Floating-Point Arithmetic (IEEE 754), 1985 version.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 3-178
Non-Confidential
3 Floating-point Support
3.25 Double precision data type for IEEE 754 arithmetic
63 62 52 51 0
S Exp Frac
As with single-precision float data types, S is the sign, Exp the exponent, and Frac the fraction. Most of
the detail of float values remains true for double values, except that:
• The Exp field is biased by 0x3FF (1023) instead of 0x7F, so numbers between 1.0 and 2.0 have an Exp
field of 0x3FF.
• The Exp value representing infinity and NaNs is 0x7FF (2047) instead of 0xFF.
Related concepts
3.22 IEEE 754 arithmetic on page 3-175.
3.24 Single precision data type for IEEE 754 arithmetic on page 3-177.
3.28 IEEE 754 arithmetic and rounding on page 3-184.
3.29 Exceptions arising from IEEE 754 floating-point arithmetic on page 3-185.
Related references
3.23 Basic data types for IEEE 754 arithmetic on page 3-176.
3.26 Sample single precision floating-point values for IEEE 754 arithmetic on page 3-180.
3.27 Sample double precision floating-point values for IEEE 754 arithmetic on page 3-182.
Related information
IEEE Standard for Floating-Point Arithmetic (IEEE 754), 1985 version.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 3-179
Non-Confidential
3 Floating-point Support
3.26 Sample single precision floating-point values for IEEE 754 arithmetic
3.26 Sample single precision floating-point values for IEEE 754 arithmetic
Sample float bit patterns, together with their mathematical values.
Related concepts
3.22 IEEE 754 arithmetic on page 3-175.
3.24 Single precision data type for IEEE 754 arithmetic on page 3-177.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 3-180
Non-Confidential
3 Floating-point Support
3.26 Sample single precision floating-point values for IEEE 754 arithmetic
3.25 Double precision data type for IEEE 754 arithmetic on page 3-179.
3.28 IEEE 754 arithmetic and rounding on page 3-184.
3.29 Exceptions arising from IEEE 754 floating-point arithmetic on page 3-185.
Related references
3.23 Basic data types for IEEE 754 arithmetic on page 3-176.
3.27 Sample double precision floating-point values for IEEE 754 arithmetic on page 3-182.
Related information
IEEE Standard for Floating-Point Arithmetic (IEEE 754), 1985 version.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 3-181
Non-Confidential
3 Floating-point Support
3.27 Sample double precision floating-point values for IEEE 754 arithmetic
3.27 Sample double precision floating-point values for IEEE 754 arithmetic
Sample double bit patterns, together with their mathematical values.
Related concepts
3.22 IEEE 754 arithmetic on page 3-175.
3.24 Single precision data type for IEEE 754 arithmetic on page 3-177.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 3-182
Non-Confidential
3 Floating-point Support
3.27 Sample double precision floating-point values for IEEE 754 arithmetic
3.25 Double precision data type for IEEE 754 arithmetic on page 3-179.
3.28 IEEE 754 arithmetic and rounding on page 3-184.
3.29 Exceptions arising from IEEE 754 floating-point arithmetic on page 3-185.
Related references
3.23 Basic data types for IEEE 754 arithmetic on page 3-176.
3.26 Sample single precision floating-point values for IEEE 754 arithmetic on page 3-180.
Related information
IEEE Standard for Floating-Point Arithmetic (IEEE 754), 1985 version.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 3-183
Non-Confidential
3 Floating-point Support
3.28 IEEE 754 arithmetic and rounding
Related concepts
3.22 IEEE 754 arithmetic on page 3-175.
3.24 Single precision data type for IEEE 754 arithmetic on page 3-177.
3.25 Double precision data type for IEEE 754 arithmetic on page 3-179.
3.29 Exceptions arising from IEEE 754 floating-point arithmetic on page 3-185.
Related references
3.23 Basic data types for IEEE 754 arithmetic on page 3-176.
3.26 Sample single precision floating-point values for IEEE 754 arithmetic on page 3-180.
3.27 Sample double precision floating-point values for IEEE 754 arithmetic on page 3-182.
Related information
IEEE Standard for Floating-Point Arithmetic (IEEE 754), 1985 version.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 3-184
Non-Confidential
3 Floating-point Support
3.29 Exceptions arising from IEEE 754 floating-point arithmetic
Related concepts
3.17 Writing a custom exception trap handler on page 3-165.
3.18 Example of a custom exception handler on page 3-170.
3.19 Exception trap handling by signals on page 3-172.
3.8 Controlling the ARM floating-point environment on page 3-156.
3.22 IEEE 754 arithmetic on page 3-175.
3.24 Single precision data type for IEEE 754 arithmetic on page 3-177.
3.25 Double precision data type for IEEE 754 arithmetic on page 3-179.
3.28 IEEE 754 arithmetic and rounding on page 3-184.
Related references
3.16 ARM floating-point compiler extensions to the C99 interface on page 3-164.
3.10 C99-compatible functions for controlling the ARM floating-point environment on page 3-158.
3.23 Basic data types for IEEE 754 arithmetic on page 3-176.
3.26 Sample single precision floating-point values for IEEE 754 arithmetic on page 3-180.
3.27 Sample double precision floating-point values for IEEE 754 arithmetic on page 3-182.
Related information
IEEE Standard for Floating-Point Arithmetic (IEEE 754), 1985 version.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 3-185
Non-Confidential
3 Floating-point Support
3.30 Exception types recognized by the ARM floating-point environment
If Overflow is not trapped, the operation returns infinity, or the largest finite number, depending
on the rounding mode.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 3-186
Non-Confidential
3 Floating-point Support
3.30 Exception types recognized by the ARM floating-point environment
Underflow exception
This can occur when the result of an operation is too small to be represented as a normalized
number (with Exp at least 1).
The situations that cause Underflow depend on whether it is trapped or not:
• If Underflow is trapped, it occurs whenever a result is too small to be represented as a
normalized number.
• If Underflow is not trapped, it only occurs if the result requires rounding. So, for example,
dividing the float number 0x00800000 by 2 does not signal Underflow, because the result
0x00400000 is exact. However, trying to multiply the float number 0x00000001 by 1.5 does
signal Underflow.
Note
For readers familiar with the IEEE 754 specification, the chosen implementation options in
the ARM compiler are to detect tininess before rounding, and to detect loss of accuracy as an
inexact result.
If Underflow is not trapped, the result is rounded to one of the two nearest representable
denormal numbers, according to the current rounding mode. The loss of precision is ignored
and the system returns the best result it can.
• The Inexact Result exception happens whenever the result of an operation requires rounding.
This would cause significant loss of speed if it had to be detected on every operation in
software, so the ordinary floating-point libraries do not support the Inexact Result exception.
The enhanced floating-point libraries, and hardware floating-point systems, all support
Inexact Result.
If Inexact Result is not trapped, the system rounds the result in the usual way.
The flag for Inexact Result is also set by Overflow and Underflow if either one of those is
not trapped.
All exceptions are untrapped by default.
Related concepts
3.17 Writing a custom exception trap handler on page 3-165.
3.12 Exception flag handling on page 3-160.
3.18 Example of a custom exception handler on page 3-170.
3.19 Exception trap handling by signals on page 3-172.
3.22 IEEE 754 arithmetic on page 3-175.
3.29 Exceptions arising from IEEE 754 floating-point arithmetic on page 3-185.
Related references
1.84 ISO-compliant implementation of signals supported by the signal() function in the C library and
additional type arguments on page 1-117.
3.26 Sample single precision floating-point values for IEEE 754 arithmetic on page 3-180.
Related information
IEEE Standard for Floating-Point Arithmetic (IEEE 754), 1985 version.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 3-187
Non-Confidential
3 Floating-point Support
3.31 Using the Vector Floating-Point (VFP) support libraries
Related information
Using VFP with RVDS, Application Note 133.
Limitations on hardware handling of floating-point arithmetic.
Implementation of Vector Floating-Point (VFP) support code.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 3-188
Non-Confidential
Chapter 4
The C and C++ Library Functions
Documents standard C and C++ library functions that are extensions to the C Standard or that differ in
some way to the standard.
Some of the standard functions interact with the ARM retargetable semihosting environment. Such
functions are also documented.
It contains the following sections:
• 4.1 __aeabi_errno_addr() on page 4-191.
• 4.2 alloca() on page 4-192.
• 4.3 clock() on page 4-193.
• 4.4 _clock_init() on page 4-194.
• 4.5 __default_signal_handler() on page 4-195.
• 4.6 errno on page 4-196.
• 4.7 _findlocale() on page 4-197.
• 4.8 _fisatty() on page 4-198.
• 4.9 _get_lconv() on page 4-199.
• 4.10 getenv() on page 4-200.
• 4.11 _getenv_init() on page 4-201.
• 4.12 __heapstats() on page 4-202.
• 4.13 __heapvalid() on page 4-203.
• 4.14 lconv structure on page 4-204.
• 4.15 localeconv() on page 4-206.
• 4.16 _membitcpybl(), _membitcpybb(), _membitcpyhl(), _membitcpyhb(), _membitcpywl(),
_membitcpywb(), _membitmovebl(), _membitmovebb(), _membitmovehl(), _membitmovehb(),
_membitmovewl(), _membitmovewb() on page 4-207.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-189
Non-Confidential
4 The C and C++ Library Functions
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-190
Non-Confidential
4 The C and C++ Library Functions
4.1 __aeabi_errno_addr()
4.1 __aeabi_errno_addr()
The __aeabi_errno_addr() returns the address of the C library errno variable when the C library
attempts to read or write errno.
The library provides a default implementation. It is unlikely that you have to re-implement this function.
This function is not part of the C library standard, but the ARM C library supports it as an extension.
Syntax
volatile int *__aeabi_errno_addr(void);
Related references
4.6 errno on page 4-196.
4.24 __rt_errno_addr() on page 4-215.
Related information
C Library ABI for the ARM Architecture.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-191
Non-Confidential
4 The C and C++ Library Functions
4.2 alloca()
4.2 alloca()
Defined in alloca.h, the alloca() function allocates local storage in a function. It returns a pointer to
size bytes of memory.
The default implementation returns an eight-byte aligned block of memory on the stack.
Memory returned from alloca() must never be passed to free(). Instead, the memory is de-allocated
automatically when the function that called alloca() returns.
Note
alloca() must not be called through a function pointer. You must take care when using alloca() and
setjmp() in the same function, because memory allocated by alloca() between calling setjmp() and
longjmp() is de-allocated by the call to longjmp().
Syntax
void *alloca(size_t size);
Related concepts
1.13 ARM C libraries and thread-safe functions on page 1-31.
Related references
4.59 Thread-safe C library functions on page 4-251.
1.32 Building an application without the C library on page 1-55.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-192
Non-Confidential
4 The C and C++ Library Functions
4.3 clock()
4.3 clock()
This is the standard C library clock function from time.h.
Syntax
clock_t clock(void);
Usage
The default implementation of this function uses semihosting.
If the units of clock_t differ from the default of centiseconds, you must define __CLK_TCK on the
compiler command line or in your own header file. The value in the definition is used for CLK_TCK and
CLOCKS_PER_SEC. The default value is 100 for centiseconds.
Note
If you re-implement clock() you must also re-implement _clock_init().
Returns
The returned value is an unsigned integer.
Related references
1.29 Direct semihosting C library function dependencies on page 1-52.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-193
Non-Confidential
4 The C and C++ Library Functions
4.4 _clock_init()
4.4 _clock_init()
Defined in rt_misc.h, the _clock_init() function is an initialization function for clock().
It is not part of the C library standard, but the ARM C library supports it as an extension.
Syntax
void _clock_init(void);
Usage
This is a function that you can re-implement in an implementation-specific way. It is called from the
library initialization code, so you do not have to call it from your application code.
Note
You must re-implement this function if you re-implement clock().
The initialization that _clock_init() applies enables clock() to return the time that has elapsed since
the program was started.
An example of how you might re-implement _clock_init() might be to set the timer to zero. However,
if your implementation of clock() relies on a system timer that cannot be reset, then _clock_init()
could instead read the time at startup (when called from the library initialization code), with clock()
subsequently subtracting the time that was read at initialization, from the current value of the timer. In
both cases, some form of initialization is required of _clock_init().
Related references
1.29 Direct semihosting C library function dependencies on page 1-52.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-194
Non-Confidential
4 The C and C++ Library Functions
4.5 __default_signal_handler()
4.5 __default_signal_handler()
Defined in rt_misc.h, the __default_signal_handler() function handles a raised signal. The default
action is to print an error message and exit.
This function is not part of the C library standard, but the ARM C library supports it as an extension.
Syntax
int __default_signal_handler(int signal, int type);
Usage
The default signal handler returns a nonzero value to indicate that the caller has to arrange for the
program to exit. You can replace the default signal handler by defining:
int __default_signal_handler(int signal, int type);
The interface is the same as __raise(), but this function is only called after the C signal handling
mechanism has declined to process the signal.
A complete list of the defined signals is in signal.h.
Note
The signals used by the libraries might change in future releases of the product.
Related references
1.30 Indirect semihosting C library function dependencies on page 1-53.
4.19 __raise() on page 4-210.
4.51 _ttywrch() on page 4-243.
4.41 _sys_exit() on page 4-233.
4.30 __rt_raise() on page 4-221.
1.84 ISO-compliant implementation of signals supported by the signal() function in the C library and
additional type arguments on page 1-117.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-195
Non-Confidential
4 The C and C++ Library Functions
4.6 errno
4.6 errno
The C library errno variable is defined in the implicit static data area of the library.
This area is identified by __user_libspace(). The function that returns the address of errno is:
(*(volatile int *) __aeabi_errno_addr())
You can define __aeabi_errno_addr() if you want to place errno at a user-defined location instead of
the default location identified by __user_libspace().
Note
Legacy versions of errno.h might define errno in terms of __rt_errno_addr() rather than
__aeabi_errno_addr(). The function name __rt_errno_addr() is a legacy from pre-ABI versions of
the tools, and is still supported to ensure that object files generated with those tools link successfully.
Returns
The return value is a pointer to a variable of type int, containing the currently applicable instance of
errno.
Related concepts
1.14 Use of static data in the C libraries on page 1-32.
Related references
4.1 __aeabi_errno_addr() on page 4-191.
4.24 __rt_errno_addr() on page 4-215.
1.15 Use of the __user_libspace static data area by the C libraries on page 1-34.
Related information
Application Binary Interface for the ARM Architecture.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-196
Non-Confidential
4 The C and C++ Library Functions
4.7 _findlocale()
4.7 _findlocale()
Defined in rt_locale.h, _findlocale() searches a set of contiguous locale data blocks for the
requested locale, and returns a pointer to that locale.
This function is not part of the C library standard, but the ARM C library supports it as an extension.
Syntax
void const *_findlocale(void const *index, const char *name);
Where:
index
is a pointer to a set of locale data blocks that are contiguous in memory and that end with a
terminating value (set by the LC_index_end macro).
name
is the name of the locale to find.
Usage
You can use _findlocale() as an optional helper function when defining your own locale setup.
The _get_lc_*() functions, for example, _get_lc_ctype(), are expected to return a pointer to a locale
definition created using the assembler macros. If you only want to write one locale definition, you can
write an implementation of _get_lc_ctype() that always returns the same pointer. However, if you
want to use different locale definitions at runtime, then the _get_lc_*() functions have to be able to
return a different data block depending on the name passed to them as an argument. _findlocale()
provides an easy way to do this.
Returns
Returns a pointer to the requested data block.
Related concepts
1.49 Assembler macros that tailor locale functions in the C library on page 1-74.
1.49 Assembler macros that tailor locale functions in the C library on page 1-74.
1.53 Runtime selection of the locale subsystem in the C library on page 1-78.
Related references
1.50 Link time selection of the locale subsystem in the C library on page 1-75.
1.54 Definition of locale data blocks in the C library on page 1-79.
4.14 lconv structure on page 4-204.
4.9 _get_lconv() on page 4-199.
4.15 localeconv() on page 4-206.
4.32 setlocale() on page 4-223.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-197
Non-Confidential
4 The C and C++ Library Functions
4.8 _fisatty()
4.8 _fisatty()
Defined in stdio.h, the _fisatty() function determines whether the given stdio stream is attached to
a terminal device or a normal file.
It calls the _sys_istty() low-level function on the underlying file handle.
This function is not part of the C library standard, but the ARM C library supports it as an extension.
Syntax
int _fisatty(FILE *stream);
Related references
4.43 _sys_istty() on page 4-235.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-198
Non-Confidential
4 The C and C++ Library Functions
4.9 _get_lconv()
4.9 _get_lconv()
Defined in locale.h, _get_lconv() performs the same function as the standard C library function,
localeconv(), except that it delivers the result in user-provided memory instead of an internal static
variable.
_get_lconv() sets the components of an lconv structure with values appropriate for the formatting of
numeric quantities.
Syntax
void _get_lconv(struct lconv *lc);
Usage
This extension to the ISO C library does not use any static data. If you are building an application that
must conform strictly to the ISO C standard, use localeconv() instead.
Returns
The existing lconv structure lc is filled with formatting data.
Related concepts
1.49 Assembler macros that tailor locale functions in the C library on page 1-74.
Related references
4.7 _findlocale() on page 4-197.
4.14 lconv structure on page 4-204.
4.15 localeconv() on page 4-206.
4.32 setlocale() on page 4-223.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-199
Non-Confidential
4 The C and C++ Library Functions
4.10 getenv()
4.10 getenv()
This is the standard C library getenv() function from stdlib.h. It gets the value of a specified
environment variable.
Syntax
char *getenv(const char *name);
Usage
The default implementation returns NULL, indicating that no environment information is available.
If you re-implement getenv(), ARM recommends that you re-implement it in such a way that it
searches some form of environment list for the input string, name. The set of environment names and the
method for altering the environment list are implementation-defined. getenv() does not depend on any
other function, and no other function depends on getenv().
A function closely associated with getenv() is _getenv_init(). _getenv_init() is called during
startup if it is defined, to enable a user re-implementation of getenv() to initialize itself.
Returns
The return value is a pointer to a string associated with the matched list member. The array pointed to
must not be modified by the program, but might be overwritten by a subsequent call to getenv().
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-200
Non-Confidential
4 The C and C++ Library Functions
4.11 _getenv_init()
4.11 _getenv_init()
Defined in rt_misc.h, the _getenv_init() function enables a user version of getenv() to initialize
itself.
It is not part of the C library standard, but the ARM C library supports it as an extension.
Syntax
void _getenv_init(void);
Usage
If this function is defined, the C library initialization code calls it when the library is initialized, that is,
before main() is entered.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-201
Non-Confidential
4 The C and C++ Library Functions
4.12 __heapstats()
4.12 __heapstats()
Defined in stdlib.h, the __heapstats() function displays statistics on the state of the storage
allocation heap.
The default implementation in the compiler gives information on how many free blocks exist, and
estimates their size ranges.
The __heapstats() function generates output as follows:
32272 bytes in 2 free blocks (avge size 16136)
1 blocks 2^12+1 to 2^13
1 blocks 2^13+1 to 2^14
Line 1 of the output displays the total number of bytes, the number of free blocks, and the average size.
The following lines give an estimate of the size of each block in bytes, expressed as a range.
__heapstats() does not give information on the number of used blocks.
The function outputs its results by calling the output function dprint(), that must work like fprintf().
The first parameter passed to dprint() is the supplied pointer param. You can pass fprintf() itself,
provided you cast it to the right function pointer type. This type is defined as a typedef for convenience.
It is called __heapprt. For example:
__heapstats((__heapprt)fprintf, stderr);
Note
If you call fprintf() on a stream that you have not already sent output to, the library calls malloc()
internally to create a buffer for the stream. If this happens in the middle of a call to __heapstats(), the
heap might be corrupted. Therefore, you must ensure you have already sent some output to stderr.
If you are using the default one-region memory model, heap memory is allocated only as it is required.
This means that the amount of free heap changes as you allocate and deallocate memory. For example,
the sequence:
int *ip;
__heapstats((__heapprt)fprintf,stderr); // print initial free heap size
ip = malloc(200000);
free(ip);
__heapstats((__heapprt)fprintf,stderr); // print heap size after freeing
This function is not part of the C library standard, but the ARM C library supports it as an extension.
Syntax
void __heapstats(int (*dprint)(void *param, char const *format,...), void *param);
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-202
Non-Confidential
4 The C and C++ Library Functions
4.13 __heapvalid()
4.13 __heapvalid()
Defined in stdlib.h, the __heapvalid() function performs a consistency check on the heap.
It outputs full information about every free block if the verbose parameter is nonzero. Otherwise, it only
outputs errors.
The function outputs its results by calling the output function dprint(), that must work like fprintf().
The first parameter passed to dprint() is the supplied pointer param. You can pass fprintf() itself,
provided you cast it to the right function pointer type. This type is defined as a typedef for convenience.
It is called __heapprt. For example:
__heapvalid((__heapprt) fprintf, stderr, 0);
Note
If you call fprintf() on a stream that you have not already sent output to, the library calls malloc()
internally to create a buffer for the stream. If this happens in the middle of a call to __heapvalid(), the
heap might be corrupted. You must therefore ensure you have already sent some output to stderr. The
example code fails if you have not already written to the stream.
This function is not part of the C library standard, but the ARM C library supports it as an extension.
Syntax
int __heapvalid(int (*dprint)(void *param, char const *format,...), void *param, int
verbose);
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-203
Non-Confidential
4 The C and C++ Library Functions
4.14 lconv structure
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-204
Non-Confidential
4 The C and C++ Library Functions
4.14 lconv structure
other
The value is the number of digits that comprise the current group. The next element is examined
to determine the size of the next group of digits to the left of the current group.
The value of p_sign_posn and n_sign_posn are interpreted as follows:
0
Parentheses surround the quantity and currency symbol.
1
The sign string precedes the quantity and currency symbol.
2
The sign string is after the quantity and currency symbol.
3
The sign string immediately precedes the currency symbol.
4
The sign string immediately succeeds the currency symbol.
Related concepts
1.49 Assembler macros that tailor locale functions in the C library on page 1-74.
Related references
4.7 _findlocale() on page 4-197.
4.9 _get_lconv() on page 4-199.
4.15 localeconv() on page 4-206.
4.32 setlocale() on page 4-223.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-205
Non-Confidential
4 The C and C++ Library Functions
4.15 localeconv()
4.15 localeconv()
Defined in stdlib.h, localeconv() creates and sets the components of an lconv structure with values
appropriate for the formatting of numeric quantities according to the rules of the current locale.
Syntax
struct lconv *localeconv(void);
Usage
The members of the structure with type char * are strings. Any of these, except for decimal_point, can
point to an empty string, "", to indicate that the value is not available in the current locale or is of zero
length.
The members with type char are non-negative numbers. Any of the members can be CHAR_MAX to
indicate that the value is not available in the current locale.
This function is not thread-safe, because it uses an internal static buffer. _get_lconv() provides a
thread-safe alternative.
Returns
The function returns a pointer to the filled-in object. The structure pointed to by the return value is not
modified by the program, but might be overwritten by a subsequent call to the localeconv() function.
In addition, calls to the setlocale() function with categories LC_ALL, LC_MONETARY, or LC_NUMERIC
might overwrite the contents of the structure.
Related concepts
1.49 Assembler macros that tailor locale functions in the C library on page 1-74.
Related references
4.7 _findlocale() on page 4-197.
4.14 lconv structure on page 4-204.
4.9 _get_lconv() on page 4-199.
4.32 setlocale() on page 4-223.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-206
Non-Confidential
4 The C and C++ Library Functions
4.16 _membitcpybl(), _membitcpybb(), _membitcpyhl(), _membitcpyhb(), _membitcpywl(), _membitcpywb(), _membitmovebl(),
_membitmovebb(), _membitmovehl(), _membitmovehb(), _membitmovewl(), _membitmovewb()
Syntax
void _membitcpy[b|h|w][b|l](void *dest, const void *src, int dest_offset, int
src_offset, size_t nbits);
Usage
The number of contiguous bits specified by nbits is copied, or moved (depending on the function being
used), from a memory location starting src_offset bits after (or before if a negative offset) the address
pointed to by src, to a location starting dest_offset bits after (or before if a negative offset) the address
pointed to by dest.
To define a contiguous sequence of bits, a form of ordering is required. The variants of each function
define this order, as follows:
• Functions whose second-last character is b, for example _membitcpybl(), are byte-oriented. Byte-
oriented functions consider all of the bits in one byte to come before the bits in the next byte.
• Functions whose second-last character is h are halfword-oriented.
• Functions whose second-last character is w are word-oriented.
Within each byte, halfword, or word, the bits can be considered to go in different order depending on the
endianness. Functions ending in b, for example _membitmovewb(), are bitwise big-endian. This means
that the Most Significant Bit (MSB) of each byte, halfword, or word (as appropriate) is considered to be
the first bit in the word, and the Least Significant Bit (LSB) is considered to be the last. Functions ending
in l are bitwise little-endian. They consider the LSB to come first and the MSB to come last.
As with memcpy() and memmove(), the bitwise memory copying functions copy as fast as they can in
their assumption that source and destination memory regions do not overlap, whereas the bitwise
memory move functions ensure that source data in overlapping regions is copied before being
overwritten.
On a little-endian platform, the bitwise big-endian functions are distinct, but the bitwise little-endian
functions use the same bit ordering, so they are synonymous symbols that refer to the same function. On
a big-endian platform, the bitwise big-endian functions are all effectively the same, but the bitwise little-
endian functions are distinct.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-207
Non-Confidential
4 The C and C++ Library Functions
4.17 posix_memalign()
4.17 posix_memalign()
Defined in stdlib.h, the posix_memalign() function provides aligned memory allocation.
It is fully POSIX-compliant.
Syntax
int posix_memalign(void **memptr, size_t alignment, size_t size);
Usage
This function allocates size bytes of memory at an address that is a multiple of alignment.
The value of alignment must be a power of two and a multiple of sizeof(void *).
You can free memory allocated by posix_memalign() using the standard C library free() function.
Returns
The returned address is written to the void * variable pointed to by memptr.
The integer return value from the function is zero on success, or an error code on failure.
If no block of memory can be found with the requested size and alignment, the function returns ENOMEM
and the value of *memptr is undefined.
Related information
The Open Group Base Specifications, IEEE Std 1003.1.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-208
Non-Confidential
4 The C and C++ Library Functions
4.18 #pragma import(_main_redirection)
Syntax
#pragma import(_main_redirection)
Related information
Environment.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-209
Non-Confidential
4 The C and C++ Library Functions
4.19 __raise()
4.19 __raise()
Defined in rt_misc.h, the __raise() function raises a signal to indicate a runtime anomaly.
It is not part of the C library standard, but the ARM C library supports it as an extension.
Syntax
int __raise(int signal, int type);
where:
signal
is an integer that holds the signal number.
type
is an integer, string constant or variable that provides additional information about the
circumstances that the signal was raised in, for some kinds of signal.
Usage
If the user has configured the handling of the signal by calling signal() then __raise() takes the action
specified by the user. That is, either to ignore the signal or to call the user-provided handler function.
Otherwise, __raise() calls __default_signal_handler(), which provides the default signal handling
behavior.
You can replace the __raise() function by defining:
int __raise(int signal, int type);
This enables you to bypass the C signal mechanism and its data-consuming signal handler vector, but
otherwise gives essentially the same interface as:
int __default_signal_handler(int signal, int type);
The default signal handler of the library uses the type parameter of __raise() to vary the messages it
outputs.
Returns
There are three possibilities for a __raise() return condition:
no return
The handler performs a long jump or restart.
0
The signal was handled.
nonzero
The calling code must pass that return value to the exit code. The default library implementation
calls _sys_exit(rc) if __raise() returns a nonzero return code rc.
Related concepts
1.21 Thread safety in the ARM C library on page 1-42.
Related references
1.30 Indirect semihosting C library function dependencies on page 1-53.
4.5 __default_signal_handler() on page 4-195.
4.51 _ttywrch() on page 4-243.
4.41 _sys_exit() on page 4-233.
4.30 __rt_raise() on page 4-221.
1.84 ISO-compliant implementation of signals supported by the signal() function in the C library and
additional type arguments on page 1-117.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-210
Non-Confidential
4 The C and C++ Library Functions
4.20 _rand_r()
4.20 _rand_r()
Defined in stdlib.h, the _rand_r() function is a reentrant version of the rand() function.
Syntax
int _rand_r(struct _rand_state * buffer);
where:
buffer
is a pointer to a user-supplied buffer storing the state of the random number generator.
Usage
This function enables you to explicitly supply your own buffer in thread-local storage.
Related references
4.60 C library functions that are not thread-safe on page 4-253.
4.33 _srand_r() on page 4-225.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-211
Non-Confidential
4 The C and C++ Library Functions
4.21 remove()
4.21 remove()
This is the standard C library remove() function from stdio.h.
Syntax
int remove(const char *filename);
Usage
The default implementation of this function uses semihosting.
remove() causes the file whose name is the string pointed to by filename to be removed. Subsequent
attempts to open the file result in failure, unless it is created again. If the file is open, the behavior of the
remove() function is implementation-defined.
Returns
Returns zero if the operation succeeds or nonzero if it fails.
Related references
1.29 Direct semihosting C library function dependencies on page 1-52.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-212
Non-Confidential
4 The C and C++ Library Functions
4.22 rename()
4.22 rename()
This is the standard C library rename() function from stdio.h.
Syntax
int rename(const char *old, const char *new);
Usage
The default implementation of this function uses semihosting.
rename() causes the file whose name is the string pointed to by old to be subsequently known by the
name given by the string pointed to by new. The file named old is effectively removed. If a file named by
the string pointed to by new exists prior to the call of the rename() function, the behavior is
implementation-defined.
Returns
Returns zero if the operation succeeds or nonzero if it fails. If the operation returns nonzero and the file
existed previously it is still known by its original name.
Related references
1.29 Direct semihosting C library function dependencies on page 1-52.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-213
Non-Confidential
4 The C and C++ Library Functions
4.23 __rt_entry
4.23 __rt_entry
The symbol __rt_entry is the starting point for a program using the ARM C library.
Control passes to __rt_entry after all scatter-loaded regions have been relocated to their execution
addresses.
Usage
The default implementation of __rt_entry:
1. Sets up the heap and stack.
2. Initializes the C library by calling __rt_lib_init.
3. Calls main().
4. Shuts down the C library, by calling __rt_lib_shutdown.
5. Exits.
__rt_entry must end with a call to one of the following functions:
exit()
Calls atexit()-registered functions and shuts down the library.
__rt_exit()
Shuts down the library but does not call atexit() functions.
_sys_exit()
Exits directly to the execution environment. It does not shut down the library and does not call
atexit() functions.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-214
Non-Confidential
4 The C and C++ Library Functions
4.24 __rt_errno_addr()
4.24 __rt_errno_addr()
The __rt_errno_addr() function is called to get the address of the C library errno variable when the C
library attempts to read or write errno.
The library provides a default implementation. It is unlikely that you have to reimplement this function.
This function is not part of the C library standard, but the ARM C library supports it as an extension.
Note
This function is associated with pre-ABI versions of the compilation tools. However, it remains
supported to ensure that object files compiled with those tools link successfully. Unless you are working
with object files compiled with pre-ABI versions of the tools, use __aeabi_errno_addr() instead of
__rt_errno_addr().
Syntax
volatile int *__rt_errno_addr(void);
Related references
4.1 __aeabi_errno_addr() on page 4-191.
4.6 errno on page 4-196.
Related information
Application Binary Interface for the ARM Architecture.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-215
Non-Confidential
4 The C and C++ Library Functions
4.25 __rt_exit()
4.25 __rt_exit()
Defined in rt_misc.h, the __rt_exit() function shuts down the library but does not call functions
registered with atexit().
atexit()-registered functions are called by exit().
__rt_exit() is not part of the C library standard, but the ARM C library supports it as an extension.
Syntax
void __rt_exit(int code);
Usage
Shuts down the C library by calling __rt_lib_shutdown(), and then calls _sys_exit() to terminate the
application. Reimplement _sys_exit() rather than __rt_exit().
Returns
This function does not return.
Related references
4.41 _sys_exit() on page 4-233.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-216
Non-Confidential
4 The C and C++ Library Functions
4.26 __rt_fp_status_addr()
4.26 __rt_fp_status_addr()
Defined in rt_fp.h, the __rt_fp_status_addr() function returns the address of the floating-point
status word.
By default, the floating-point status word resides in __user_libspace.
It is not part of the C library standard, but the ARM C library supports it as an extension.
Syntax
Syntax
unsigned *__rt_fp_status_addr(void);
Usage
If __rt_fp_status_addr() is not defined, the default implementation from the C library is used. The
value is initialized when __rt_lib_init() calls _fp_init(). The constants for the status word are
listed in fenv.h. The default floating-point status is 0.
Returns
The address of the floating-point status word.
Related concepts
1.21 Thread safety in the ARM C library on page 1-42.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-217
Non-Confidential
4 The C and C++ Library Functions
4.27 __rt_heap_extend()
4.27 __rt_heap_extend()
Defined in rt_heap.h, the __rt_heap_extend() function returns a new eight-byte aligned block of
memory to add to the heap, if possible.
If you reimplement __rt_stackheap_init(), you must reimplement this function. An incomplete
prototype implementation is in rt_memory.s.
This function is not part of the C library standard, but the ARM C library supports it as an extension.
Syntax
extern unsigned __rt_heap_extend(unsigned size, void **block);
Usage
The calling convention is ordinary AAPCS. On entry, r0 is the minimum size of the block to add, and
r1 holds a pointer to a location to store the base address.
Returns
The default implementation extends the heap if there is sufficient free heap memory. If it cannot, it calls
__user_heap_extend() if it is implemented. On exit, r0 is the size of the block acquired, or 0 if
nothing could be obtained, and the memory location r1 pointed to on entry contains the base address of
the block.
Related concepts
1.66 Stack pointer initialization and heap bounds on page 1-95.
Related references
4.31 __rt_stackheap_init() on page 4-222.
4.52 __user_heap_extend() on page 4-244.
4.53 __user_heap_extent() on page 4-245.
4.54 __user_setup_stackheap() on page 4-246.
Related information
Procedure Call Standard for the ARM Architecture.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-218
Non-Confidential
4 The C and C++ Library Functions
4.28 __rt_lib_init()
4.28 __rt_lib_init()
Defined in rt_misc.h, this is the library initialization function and is the companion to
__rt_lib_shutdown().
Syntax
extern value_in_regs struct __argc_argv __rt_lib_init(unsigned heapbase, unsigned
heaptop);
where:
heapbase
is the start of the heap memory block.
heaptop
is the end of the heap memory block.
Usage
This function is called immediately after __rt_stackheap_init() and is passed an initial chunk of
memory to use as a heap. This function is the standard ARM C library initialization function and it must
not be reimplemented.
Returns
This function returns argc and argv ready to be passed to main(). The structure is returned in the
registers as:
struct __argc_argv
{ int argc;
char **argv;
int r2, r3; // optional extra arguments that on entry to main() are
}; // found in registers R2 and R3.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-219
Non-Confidential
4 The C and C++ Library Functions
4.29 __rt_lib_shutdown()
4.29 __rt_lib_shutdown()
Defined in rt_misc.h, __rt_lib_shutdown() is the library shutdown function and is the companion to
__rt_lib_init().
Syntax
void __rt_lib_shutdown(void);
Usage
This function is provided in case a user must call it directly. This is the standard ARM C library
shutdown function and it must not be reimplemented.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-220
Non-Confidential
4 The C and C++ Library Functions
4.30 __rt_raise()
4.30 __rt_raise()
Defined in rt_misc.h, the __rt_raise() function raises a signal to indicate a runtime anomaly.
It is not part of the C library standard, but the ARM C library supports it as an extension.
Syntax
void __rt_raise(int signal, int type);
where:
signal
is an integer that holds the signal number.
type
is an integer, string constant or variable that provides additional information about the
circumstances that the signal was raised in, for some kinds of signal.
Usage
Redefine this function to replace the entire signal handling mechanism for the library. The default
implementation calls __raise().
Depending on the value returned from __raise():
no return
The handler performed a long jump or restart and __rt_raise() does not regain control.
0
The signal was handled and __rt_raise() exits.
nonzero
The default library implementation calls _sys_exit(rc) if __raise() returns a nonzero return
code rc.
Related references
4.5 __default_signal_handler() on page 4-195.
4.19 __raise() on page 4-210.
4.51 _ttywrch() on page 4-243.
4.41 _sys_exit() on page 4-233.
1.84 ISO-compliant implementation of signals supported by the signal() function in the C library and
additional type arguments on page 1-117.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-221
Non-Confidential
4 The C and C++ Library Functions
4.31 __rt_stackheap_init()
4.31 __rt_stackheap_init()
Defined in rt_misc.h, the __rt_stackheap_init() function sets up the stack pointer and returns a
region of memory for use as the initial heap.
It is called from the library initialization code.
On return from this function, SP must point to the top of the stack region, r0 must point to the base of
the heap region, and r1 must point to the limit of the heap region.
A user-defined memory model (that is, __rt_stackheap_init() and __rt_heap_extend()) is allocated
16 bytes of storage from the __user_perproc_libspace area if wanted. It accesses this storage by
calling __rt_stackheap_storage() to return a pointer to its 16-byte region.
This function is not part of the C library standard, but the ARM C library supports it as an extension.
Related concepts
1.66 Stack pointer initialization and heap bounds on page 1-95.
Related references
4.27 __rt_heap_extend() on page 4-218.
4.52 __user_heap_extend() on page 4-244.
4.53 __user_heap_extent() on page 4-245.
4.54 __user_setup_stackheap() on page 4-246.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-222
Non-Confidential
4 The C and C++ Library Functions
4.32 setlocale()
4.32 setlocale()
Defined in locale.h, the setlocale() function selects the appropriate locale as specified by the
category and locale arguments.
Syntax
char *setlocale(int category, const char *locale);
Usage
Use the setlocale() function to change or query part or all of the current locale. The effect of the
category argument for each value is:
LC_COLLATE
Affects the behavior of strcoll().
LC_CTYPE
Affects the behavior of the character handling functions.
LC_MONETARY
Affects the monetary formatting information returned by localeconv().
LC_NUMERIC
Affects the decimal-point character for the formatted input/output functions and the string
conversion functions and the numeric formatting information returned by localeconv().
LC_TIME
Can affect the behavior of strftime(). For currently supported locales, the option has no
effect.
LC_ALL
Affects all locale categories. This is the bitwise OR of all the locale categories.
A value of "C" for locale specifies the minimal environment for C translation. An empty string, "", for
locale specifies the implementation-defined native environment. At program startup, the equivalent of
setlocale(LC_ALL, "C") is executed.
Valid locale values depend on which __use_X_ctype symbols are imported (__use_iso8859_ctype,
__use_sjis_ctype, __use_utf8_ctypte), and on user-defined locales.
Returns
If a pointer to a string is given for locale and the selection is valid, the string associated with the
specified category for the new locale is returned. If the selection cannot be honored, a null pointer is
returned and the locale is not changed.
A null pointer for locale causes the string associated with the category for the current locale to be
returned and the locale is not changed.
If category is LC_ALL and the most recent successful locale-setting call uses a category other than
LC_ALL, a composite string might be returned. The string returned when used in a subsequent call with its
associated category restores that part of the program locale. The string returned is not modified by the
program, but might be overwritten by a subsequent call to setlocale().
Related concepts
1.49 Assembler macros that tailor locale functions in the C library on page 1-74.
1.52 Shift-JIS and UTF-8 implementation on page 1-77.
Related references
4.7 _findlocale() on page 4-197.
4.14 lconv structure on page 4-204.
4.9 _get_lconv() on page 4-199.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-223
Non-Confidential
4 The C and C++ Library Functions
4.32 setlocale()
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-224
Non-Confidential
4 The C and C++ Library Functions
4.33 _srand_r()
4.33 _srand_r()
Defined in stdlib.h, this is a reentrant version of the srand() function.
Syntax
int _srand_r(struct _rand_state * buffer, unsigned int seed);
where:
buffer
is a pointer to a user-supplied buffer storing the state of the random number generator.
seed
is a seed for a new sequence of pseudo-random numbers to be returned by subsequent calls to
_rand_r().
Usage
This function enables you to explicitly supply your own buffer that can be used for thread-local storage.
If _srand_r() is repeatedly called with the same seed value, the same sequence of pseudo-random
numbers is repeated. If _rand_r() is called before any calls to _srand_r() have been made with the
same buffer, undefined behavior occurs because the buffer is not initialized.
Related references
4.60 C library functions that are not thread-safe on page 4-253.
4.20 _rand_r() on page 4-211.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-225
Non-Confidential
4 The C and C++ Library Functions
4.34 strcasecmp()
4.34 strcasecmp()
Defined in string.h, the strcasecmp() function performs a case-insensitive string comparison test.
Syntax
extern _ARMABI int strcasecmp(const char *s1, const char *s2);
Related information
Application Binary Interface for the ARM Architecture.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-226
Non-Confidential
4 The C and C++ Library Functions
4.35 strncasecmp()
4.35 strncasecmp()
Defined in string.h, the strncasecmp() function performs a case-insensitive string comparison test of
not more than a specified number of characters.
Syntax
extern _ARMABI int strncasecmp(const char *s1, const char *s2, size_t n);
Related information
Application Binary Interface for the ARM Architecture.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-227
Non-Confidential
4 The C and C++ Library Functions
4.36 strlcat()
4.36 strlcat()
Defined in string.h, the strlcat() function concatenates two strings.
It appends up to size-strlen(dst)-1 bytes from the NUL-terminated string src to the end of dst. It
takes the full size of the buffer, not only the length, and terminates the result with NUL as long as size is
greater than 0. Include a byte for the NUL in your size value.
The strlcat() function returns the total length of the string that would have been created if there was
unlimited space. This might or might not be equal to the length of the string actually created, depending
on whether there was enough space. This means that you can call strlcat() once to find out how much
space is required, then allocate it if you do not have enough, and finally call strlcat() a second time to
create the required string.
This function is a common BSD-derived extension to many C libraries.
Syntax
extern size_t strlcat(char *dst, *src, size_t size);
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-228
Non-Confidential
4 The C and C++ Library Functions
4.37 strlcpy()
4.37 strlcpy()
Defined in string.h, the strlcpy() function copies up to size-1 characters from the NUL-terminated
string src to dst.
It takes the full size of the buffer, not only the length, and terminates the result with NUL as long as size
is greater than 0. Include a byte for the NUL in your size value.
The strlcpy() function returns the total length of the string that would have been copied if there was
unlimited space. This might or might not be equal to the length of the string actually copied, depending
on whether there was enough space. This means that you can call strlcpy() once to find out how much
space is required, then allocate it if you do not have enough, and finally call strlcpy() a second time to
do the required copy.
This function is a common BSD-derived extension to many C libraries.
Syntax
extern size_t strlcpy(char *dst, const char *src, size_t size);
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-229
Non-Confidential
4 The C and C++ Library Functions
4.38 _sys_close()
4.38 _sys_close()
Defined in rt_sys.h, the _sys_close() function closes a file previously opened with _sys_open().
Syntax
int _sys_close(FILEHANDLE fh);
Usage
This function must be defined if any input/output function is to be used.
Returns
The return value is 0 if successful. A nonzero value indicates an error.
Related references
1.29 Direct semihosting C library function dependencies on page 1-52.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-230
Non-Confidential
4 The C and C++ Library Functions
4.39 _sys_command_string()
4.39 _sys_command_string()
Defined in rt_sys.h, the _sys_command_string() function retrieves the command line that invoked the
current application from the environment that called the application.
Syntax
char *_sys_command_string(char *cmd, int len);
where:
cmd
is a pointer to a buffer that can store the command line. It is not required that the command line
is stored in cmd.
len
is the length of the buffer.
Usage
This function is called by the library startup code to set up argv and argc to pass to main().
Note
You must not assume that the C library is fully initialized when this function is called. For example, you
must not call malloc() from within this function. This is because the C library startup sequence calls this
function before the heap is fully configured.
Returns
The function must return either:
• A pointer to the command line, if successful. This can be either a pointer to the cmd buffer if it is
used, or a pointer to wherever else the command line is stored.
• NULL, if not successful.
Related references
1.29 Direct semihosting C library function dependencies on page 1-52.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-231
Non-Confidential
4 The C and C++ Library Functions
4.40 _sys_ensure()
4.40 _sys_ensure()
This function is deprecated. It is never called by any other library function, and you are not required to
re-implement it if you are retargeting standard I/O (stdio).
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-232
Non-Confidential
4 The C and C++ Library Functions
4.41 _sys_exit()
4.41 _sys_exit()
Defined in rt_sys.h, this is the library exit function. All exits from the library eventually call
_sys_exit().
Syntax
void _sys_exit(int return_code);
Usage
This function must not return. You can intercept application exit at a higher level by either:
• Implementing the C library function exit() as part of your application. You lose atexit()
processing and library shutdown if you do this.
• Implementing the function __rt_exit(int n) as part of your application. You lose library shutdown
if you do this, but atexit() processing is still performed when exit() is called or main() returns.
Returns
The return code is advisory. An implementation might attempt to pass it to the execution environment.
Related references
1.29 Direct semihosting C library function dependencies on page 1-52.
4.5 __default_signal_handler() on page 4-195.
4.19 __raise() on page 4-210.
4.51 _ttywrch() on page 4-243.
4.30 __rt_raise() on page 4-221.
4.25 __rt_exit() on page 4-216.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-233
Non-Confidential
4 The C and C++ Library Functions
4.42 _sys_flen()
4.42 _sys_flen()
Defined in rt_sys.h, the _sys_flen() function returns the current length of a file.
Syntax
long _sys_flen(FILEHANDLE fh);
Usage
This function is used by _sys_seek() to convert an offset relative to the end of a file into an offset
relative to the beginning of the file.
You do not have to define _sys_flen() if you do not intend to use fseek().
If you retarget at system _sys_*() level, you must supply _sys_flen(), even if the underlying system
directly supports seeking relative to the end of a file.
Returns
This function returns the current length of the file fh, or a negative error indicator.
Related references
1.29 Direct semihosting C library function dependencies on page 1-52.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-234
Non-Confidential
4 The C and C++ Library Functions
4.43 _sys_istty()
4.43 _sys_istty()
Defined in rt_sys.h, the _sys_istty() function determines if a file handle identifies a terminal.
Syntax
int _sys_istty(FILEHANDLE fh);
Usage
When a file is connected to a terminal device, this function provides unbuffered behavior by default (in
the absence of a call to set(v)buf) and prohibits seeking.
Returns
The return value is one of the following values:
0
There is no interactive device.
1
There is an interactive device.
other
An error occurred.
Related references
1.29 Direct semihosting C library function dependencies on page 1-52.
4.8 _fisatty() on page 4-198.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-235
Non-Confidential
4 The C and C++ Library Functions
4.44 _sys_open()
4.44 _sys_open()
Defined in rt_sys.h, the _sys_open() function opens a file.
Syntax
FILEHANDLE _sys_open(const char *name, int openmode);
Usage
The _sys_open() function is required by fopen() and freopen(). These functions in turn are required
if any file input/output function is to be used.
The openmode parameter is a bitmap whose bits mostly correspond directly to the ISO mode
specification. Target-dependent extensions are possible, but freopen() must also be extended.
Returns
The return value is –1 if an error occurs.
Related references
1.29 Direct semihosting C library function dependencies on page 1-52.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-236
Non-Confidential
4 The C and C++ Library Functions
4.45 _sys_read()
4.45 _sys_read()
Defined in rt_sys.h, the _sys_read() function reads the contents of a file into a buffer.
Syntax
int _sys_read(FILEHANDLE fh, unsigned char *buf, unsigned len, int mode);
Note
The mode parameter is here for historical reasons. It contains nothing useful and must be ignored.
Returns
The return value is one of the following:
• The number of bytes not read (that is, len - result number of bytes were read).
• An error indication.
• An EOF indicator. The EOF indication involves the setting of 0x80000000 in the normal result.
Reading up to and including the last byte of data does not turn on the EOF indicator. The EOF indicator is
only reached when an attempt is made to read beyond the last byte of data. The target-independent code
is capable of handling:
• The EOF indicator being returned in the same read as the remaining bytes of data that precede the
EOF.
• The EOF indicator being returned on its own after the remaining bytes of data have been returned in a
previous read.
Related references
1.29 Direct semihosting C library function dependencies on page 1-52.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-237
Non-Confidential
4 The C and C++ Library Functions
4.46 _sys_seek()
4.46 _sys_seek()
Defined in rt_sys.h, the _sys_seek() function puts the file pointer at offset pos from the beginning of
the file.
Syntax
int _sys_seek(FILEHANDLE fh, long pos);
Usage
This function sets the current read or write position to the new location pos relative to the start of the
current file fh.
Returns
The result is:
• Negative if an error occurs.
• Non-negative if no error occurs.
Related references
1.29 Direct semihosting C library function dependencies on page 1-52.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-238
Non-Confidential
4 The C and C++ Library Functions
4.47 _sys_tmpnam()
4.47 _sys_tmpnam()
Defined in rt_sys.h, the _sys_tmpnam() function converts the file number fileno for a temporary file
to a unique filename, for example, tmp0001.
Syntax
void _sys_tmpnam(char *name, int fileno, unsigned maxlength);
Usage
The function must be defined if tmpnam() or tmpfile() is used.
Returns
Returns the filename in name.
Related references
1.29 Direct semihosting C library function dependencies on page 1-52.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-239
Non-Confidential
4 The C and C++ Library Functions
4.48 _sys_write()
4.48 _sys_write()
Defined in rt_sys.h, the _sys_write() function writes the contents of a buffer to a file previously
opened with _sys_open().
Syntax
int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode);
Note
The mode parameter is here for historical reasons. It contains nothing useful and must be ignored.
Returns
The return value is either:
• A positive number representing the number of characters not written (so any nonzero return value
denotes a failure of some sort).
• A negative number indicating an error.
Related references
1.29 Direct semihosting C library function dependencies on page 1-52.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-240
Non-Confidential
4 The C and C++ Library Functions
4.49 system()
4.49 system()
This is the standard C library system() function from stdlib.h.
Syntax
int system(const char *string);
Usage
The default implementation of this function uses semihosting.
system() passes the string pointed to by string to the host environment to be executed by a command
processor in an implementation-defined manner. A null pointer can be used for string, to inquire
whether a command processor exists.
Returns
If the argument is a null pointer, the system function returns nonzero only if a command processor is
available.
If the argument is not a null pointer, the system() function returns an implementation-defined value.
Related references
1.29 Direct semihosting C library function dependencies on page 1-52.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-241
Non-Confidential
4 The C and C++ Library Functions
4.50 time()
4.50 time()
This is the standard C library time() function from time.h.
The default implementation of this function uses semihosting.
Syntax
time_t time(time_t *timer);
Returns
The value -1 is returned if the calendar time is not available. If timer is not a NULL pointer, the return
value is also stored in timer.
Related references
1.29 Direct semihosting C library function dependencies on page 1-52.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-242
Non-Confidential
4 The C and C++ Library Functions
4.51 _ttywrch()
4.51 _ttywrch()
Defined in rt_sys.h, the _ttywrch() function writes a character to the console.
The console might have been redirected. You can use this function as a last resort error handling routine.
Syntax
void _ttywrch(int ch);
Usage
The default implementation of this function uses semihosting.
You can redefine this function, or __raise(), even if there is no other input/output. For example, it
might write an error message to a log kept in nonvolatile memory.
Related references
1.29 Direct semihosting C library function dependencies on page 1-52.
4.5 __default_signal_handler() on page 4-195.
4.19 __raise() on page 4-210.
4.41 _sys_exit() on page 4-233.
4.30 __rt_raise() on page 4-221.
4.19 __raise() on page 4-210.
Related information
What is Semihosting?.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-243
Non-Confidential
4 The C and C++ Library Functions
4.52 __user_heap_extend()
4.52 __user_heap_extend()
Defined in rt_misc.h, the __user_heap_extend() function can be defined to return extra blocks of
memory, separate from the initial one, to be used by the heap.
If defined, this function must return the size and base address of an eight-byte aligned heap extension
block.
Syntax
extern unsigned __user_heap_extend(int var0, void **base, unsigned requested_size);
Usage
There is no default implementation of this function. If you define this function, it must have the
following characteristics:
• The returned size must be either:
— A multiple of eight bytes of at least the requested size.
— 0, denoting that the request cannot be honored.
• Size is measured in bytes.
• The function is subject only to ARM Architecture Procedure Call Standard (AAPCS) constraints.
• The first argument is always zero on entry and can be ignored. The base is returned in the register
holding this argument.
• The returned base address must be aligned on an eight-byte boundary.
Returns
This function places a pointer to a block of at least the requested size in *base and returns the size of the
block. 0 is returned if no such block can be returned, in which case the value stored at *base is never
used.
Related concepts
1.66 Stack pointer initialization and heap bounds on page 1-95.
Related references
4.27 __rt_heap_extend() on page 4-218.
4.31 __rt_stackheap_init() on page 4-222.
4.53 __user_heap_extent() on page 4-245.
4.54 __user_setup_stackheap() on page 4-246.
Related information
Procedure Call Standard for the ARM Architecture.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-244
Non-Confidential
4 The C and C++ Library Functions
4.53 __user_heap_extent()
4.53 __user_heap_extent()
If defined, the __user_heap_extent() function returns the base address and maximum range of the
heap.
See rt_misc.h.
Syntax
extern __value_in_regs struct __heap_extent __user_heap_extent(unsigned ignore1,
unsigned ignore2);
Usage
There is no default implementation of this function. The values of the parameters ignore1 and ignore2
are not used by the function.
Related concepts
1.66 Stack pointer initialization and heap bounds on page 1-95.
Related references
4.27 __rt_heap_extend() on page 4-218.
4.31 __rt_stackheap_init() on page 4-222.
4.52 __user_heap_extend() on page 4-244.
4.54 __user_setup_stackheap() on page 4-246.
1.62 C library support for memory allocation functions on page 1-91.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-245
Non-Confidential
4 The C and C++ Library Functions
4.54 __user_setup_stackheap()
4.54 __user_setup_stackheap()
__user_setup_stackheap() sets up and returns the locations of the initial stack and heap.
If you define this function, it is called by the C library during program start-up.
When __user_setup_stackheap() is called, sp has the same value it had on entry to the application. If
this was set to a valid value before calling the C library initialization code, it can be left at this value. If
sp is not valid, __user_setup_stackheap() must change this value before using any stack and before
returning.
__user_setup_stackheap() returns the:
Note
Any re-implementation of __user_setup_stackheap() must be in assembler.
Related concepts
1.66 Stack pointer initialization and heap bounds on page 1-95.
1.66 Stack pointer initialization and heap bounds on page 1-95.
1.69 Legacy support for __user_initial_stackheap() on page 1-99.
Related references
1.29 Direct semihosting C library function dependencies on page 1-52.
4.27 __rt_heap_extend() on page 4-218.
4.31 __rt_stackheap_init() on page 4-222.
4.52 __user_heap_extend() on page 4-244.
4.53 __user_heap_extent() on page 4-245.
4.61 Legacy function __user_initial_stackheap() on page 4-255.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-246
Non-Confidential
4 The C and C++ Library Functions
4.55 __vectab_stack_and_reset
4.55 __vectab_stack_and_reset
__vectab_stack_and_reset is a library section that provides a way for the initial values of sp and pc
to be placed in the vector table, starting at address 0 for M-profile processors, such as Cortex-M1 and
Cortex-M3 embedded applications.
__vectab_stack_and_reset requires the existence of a main() function in your source code. Without a
main() function, if you place the __vectab_stack_and_reset section in a scatter file, an error is
generated to the following effect:
Error: L6236E: No section matches selector - no section to be FIRST/LAST
If the normal start-up code is bypassed, that is, if there is intentionally no main() function, you are
responsible for setting up the vector table without __vectab_stack_and_reset.
The following segment is part of a scatter file. It includes a minimal vector table illustrating the use of
__vectab_stack_and_reset to place the initial sp and pc values at addresses 0x0 and 0x4 in the vector
table:
;; Maximum of 256 exceptions (256*4 bytes == 0x400)
VECTORS 0x0 0x400
{
; First two entries provided by library
; Remaining entries provided by the user in exceptions.c
* (:gdef:__vectab_stack_and_reset, +FIRST)
* (exceptions_area)
}
CODE 0x400 FIXED
{
* (+RO)
}
Related concepts
1.66 Stack pointer initialization and heap bounds on page 1-95.
Related information
About scatter-loading.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-247
Non-Confidential
4 The C and C++ Library Functions
4.56 wcscasecmp()
4.56 wcscasecmp()
Defined in wchar.h, the wcscasecmp() function performs a case-insensitive string comparison test on
wide characters.
It is a GNU extension to the libraries. It is not POSIX-standardized.
Syntax
int wcscasecmp(const wchar_t * __restrict s1, const wchar_t * __restrict s2);
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-248
Non-Confidential
4 The C and C++ Library Functions
4.57 wcsncasecmp()
4.57 wcsncasecmp()
Defined in wchar.h, the wcsncasecmp() function performs a case-insensitive string comparison test of
not more than a specified number of wide characters.
It is a GNU extension to the libraries. It is not POSIX-standardized.
Syntax
int wcsncasecmp(const wchar_t * __restrict s1, const wchar_t * __restrict s2, size_t n);
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-249
Non-Confidential
4 The C and C++ Library Functions
4.58 wcstombs()
4.58 wcstombs()
Defined in wchar.h, the wcstombs() function works as described in the ISO C standard, with extended
functionality as specified by POSIX.
That is, if s is a NULL pointer, wcstombs() returns the length required to convert the entire array
regardless of the value of n, but no values are stored.
Syntax
size_t wcstombs(char *s, const wchar_t *pwcs, size_t n);
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-250
Non-Confidential
4 The C and C++ Library Functions
4.59 Thread-safe C library functions
Functions Description
calloc(), free(), malloc(), The heap functions are thread-safe if the _mutex_* functions are implemented.
realloc()
All threads share a single heap and use mutexes to avoid data corruption when
there is concurrent access. Each heap implementation is responsible for doing its
own locking. If you supply your own allocator, it must also do its own locking.
This enables it to do fine-grained locking if required, rather than protecting the
entire heap with a single mutex (coarse-grained locking).
abort(), raise(), signal(), The ARM signal handling functions and floating-point exception traps are
fenv.h thread-safe.
The settings for signal handlers and floating-point traps are global across the
entire process and are protected by locks. Data corruption does not occur if
multiple threads call signal() or an fenv.h function at the same time. However,
be aware that the effects of the call act on all threads and not only on the calling
thread.
clearerr(), fclose(), The stdio library is thread-safe if the _mutex_* functions are implemented.
feof(),ferror(), fflush(),
Each individual stream is protected by a lock, so two threads can each open their
fgetc(),fgetpos(), fgets(),
own stdio stream and use it, without interfering with one another.
fopen(),fputc(), fputs(),
fread(),freopen(), fseek(), If two threads both want to read or write the same stream, locking at the fgetc()
fsetpos(),ftell(), fwrite(), and fputc() level prevents data corruption, but it is possible that the individual
getc(),getchar(), gets(), characters output by each thread might be interleaved in a confusing way.
perror(),putc(), putchar(),
puts(),rewind(), setbuf(), Note
setvbuf(),tmpfile(), tmpnam(), tmpnam() also contains a static buffer but this is only used if the argument is
ungetc() NULL. To ensure that your use of tmpnam() is thread-safe, supply your own
buffer space.
clock() clock() contains static data that is written once at program startup and then only
ever read. Therefore, clock() is thread-safe provided no extra threads are
already running at the time that the library is initialized.
errno errno is thread-safe.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-251
Non-Confidential
4 The C and C++ Library Functions
4.59 Thread-safe C library functions
Functions Description
atexit() The list of exit functions maintained by atexit() is process-global and protected
by a lock.
In the worst case, if more than one thread calls atexit(), the order that exit
functions are called cannot be guaranteed.
longjmp(), setjmp() Although setjmp() and longjmp() keep data in __user_libspace, they call the
__alloca_* functions, that are thread-safe.
remove(), rename(), time() These functions use interrupts that communicate with the ARM debugging
environments. Typically, you have to reimplement these for a real-world
application.
snprintf(), sprintf(), When using these functions, the string-based functions read the locale settings.
vsnprintf(),vsprintf(), Typically, they are thread-safe. However, if you change locale in mid-session,
sscanf(), isalnum(),isalpha(), you must ensure that these functions are not affected.
iscntrl(), isdigit(),isgraph(),
The string-based functions, such as sprintf() and sscanf(), do not depend on
islower(), isprint(),ispunct(),
the stdio library.
isspace(),
isupper(),isxdigit(),
tolower(), toupper(),strcoll(),
strtod(), strtol(),strtoul(),
strftime()
Related concepts
1.21 Thread safety in the ARM C library on page 1-42.
Related references
4.2 alloca() on page 4-192.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-252
Non-Confidential
4 The C and C++ Library Functions
4.60 C library functions that are not thread-safe
Functions Description
asctime(), These functions are all thread-unsafe. Each contains a static buffer that might be overwritten by
localtime(), another thread between a call to the function and the subsequent use of its return value.
strtok()
ARM supplies reentrant versions, _asctime_r(), _localtime_r(), and _strtok_r(). ARM
recommends that you use these functions instead to ensure safety.
Note
These reentrant versions take additional parameters. _asctime_r() takes an additional parameter that
is a pointer to a buffer that the output string is written into. _localtime_r() takes an additional
parameter that is a pointer to a struct tm, that the result is written into. _strtok_r() takes an
additional parameter that is a pointer to a char pointer to the next token.
exit() Do not call exit() in a multithreaded program even if you have provided an implementation of the
underlying _sys_exit() that actually terminates all threads.
In this case, exit() cleans up before calling _sys_exit() so disrupts other threads.
gamma(), These extended mathlib functions use a global variable, _signgam, so are not thread-safe.
lgamma(),
lgammaf(),
lgammal() d
mbrlen(), The C89 multibyte conversion functions (defined in stdlib.h) are not thread-safe, for example
mbsrtowcs(), mblen() and mbtowc(), because they contain internal static state that is shared between all threads
mbrtowc(), without locking.
wcrtomb(),
However, the extended restartable versions (defined in wchar.h) are thread-safe, for example
wcsrtombs()
mbrtowc() and wcrtomb(), provided you pass in a pointer to your own mbstate_t object. You must
exclusively use these functions with non-NULL mbstate_t * parameters if you want to ensure
thread-safety when handling multibyte strings.
d If migrating from RVCT, be aware that gamma() is deprecated in ARM Compiler 4.1 and later.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-253
Non-Confidential
4 The C and C++ Library Functions
4.60 C library functions that are not thread-safe
Functions Description
rand(), These functions keep internal state that is both global and unprotected. This means that calls to
srand() rand() are never thread-safe.
• Use the reentrant versions _rand_r() and _srand_r() supplied by ARM. These use user-
provided buffers instead of static data within the C library.
• Use your own locking to ensure that only one thread ever calls rand() at a time, for example, by
defining $Sub$$rand() if you want to avoid changing your code.
• Arrange that only one thread ever needs to generate random numbers.
• Supply your own random number generator that can have multiple independent instances.
Note
_rand_r() and _srand_r() both take an additional parameter that is a pointer to a buffer storing the
state of the random number generator.
setlocale(), setlocale() is used for setting and reading locale settings. The locale settings are global across all
localeconv() threads, and are not protected by a lock. If two threads call setlocale() to simultaneously modify
the locale settings, or if one thread reads the settings while another thread is modifying them, data
corruption might occur. Also, many other functions, for example strtod() and sprintf(), read the
current locale settings. Therefore, if one thread calls setlocale() concurrently with another thread
calling such a function, there might be unexpected results.
Multiple threads reading the settings simultaneously is thread-safe in simple cases and if no other
thread is simultaneously modifying those settings, but where internally an intermediate buffer is
required for more complicated returned results, unexpected results can occur unless you use a
reentrant version of setlocale().
ARM recommends that you either:
• Choose the locale you want and call setlocale() once to initialize it. Do this before creating any
additional threads in your program so that any number of threads can read the locale settings
concurrently without interfering with one another.
• Use the reentrant version _setlocale_r() supplied by ARM. This returns a string that is either a
pointer to a constant string, or a pointer to a string stored in a user-supplied buffer that can be
used for thread-local storage, rather than using memory within the C library.
Be aware that _setlocale_r() is not fully thread-safe when accessed concurrently to change locale
settings. This access is not lock-protected.
Also, be aware that localeconv() is not thread-safe. Call the ARM function _get_lconv() with a
pointer to a user-supplied buffer instead.
Related concepts
1.21 Thread safety in the ARM C library on page 1-42.
Related references
4.20 _rand_r() on page 4-211.
4.33 _srand_r() on page 4-225.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-254
Non-Confidential
4 The C and C++ Library Functions
4.61 Legacy function __user_initial_stackheap()
Syntax
extern __value_in_regs struct __initial_stackheap __user_initial_stackheap(unsigned R0,
unsigned SP, unsigned R2, unsigned SL);
Usage
__user_initial_stackheap() returns the:
Note
The value of stack_base is 0x1 greater than the highest address used by the stack because a full-
descending stack is used.
Related concepts
1.66 Stack pointer initialization and heap bounds on page 1-95.
1.69 Legacy support for __user_initial_stackheap() on page 1-99.
Related references
1.29 Direct semihosting C library function dependencies on page 1-52.
4.54 __user_setup_stackheap() on page 4-246.
1.29 Direct semihosting C library function dependencies on page 1-52.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 4-255
Non-Confidential
Chapter 5
Floating-point Support Functions
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 5-256
Non-Confidential
5 Floating-point Support Functions
5.1 _clearfp()
5.1 _clearfp()
Defined in float.h, the _clearfp() function is provided for compatibility with Microsoft products.
_clearfp() clears all five exception sticky flags and returns their previous values. You can use the
_controlfp() argument macros, for example _EM_INVALID and _EM_ZERODIVIDE, to test bits of the
returned result.
The function prototype for _clearfp() is:
unsigned _clearfp(void);
Note
This function requires you to select a floating-point model that supports exceptions. For example, --
fpmode=ieee_full or --fpmode=ieee_fixed.
Related concepts
3.9 Floating-point functions for compatibility with Microsoft products on page 3-157.
Related references
5.2 _controlfp() on page 5-258.
5.8 _statusfp() on page 5-268.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 5-257
Non-Confidential
5 Floating-point Support Functions
5.2 _controlfp()
5.2 _controlfp()
Defined in float.h, the _controlfp() function is provided for compatibility with Microsoft products. It
enables you to control exception traps and rounding modes.
The function prototype for _controlfp() is:
unsigned int _controlfp(unsigned int new, unsigned int mask);
Note
This function requires you to select a floating-point model that supports exceptions. For example, --
fpmode=ieee_full or --fpmode=ieee_fixed.
_controlfp() also modifies a control word using a mask to isolate the bits to modify. For every bit of
mask that is zero, the corresponding control word bit is unchanged. For every bit of mask that is nonzero,
the corresponding control word bit is set to the value of the corresponding bit of new. The return value is
the previous state of the control word.
Note
This is different behavior to that of __ieee_status() or __fp_status(), where you can toggle a bit by
setting a zero in the mask word and a one in the flags word.
The following table describes the macros you can use to form the arguments to _controlfp().
Macro Description
_MCW_EM Mask containing all exception bits
_EM_INVALID Bit describing the Invalid Operation exception
_EM_ZERODIVIDE Bit describing the Divide by Zero exception
Note
The values of these macros are not guaranteed to remain the same in future versions of ARM products.
To ensure that your code continues to work if the value changes in future releases, use the macro rather
than its value.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 5-258
Non-Confidential
5 Floating-point Support Functions
5.2 _controlfp()
To trap the Invalid Operation exception and untrap all other exceptions:
_controlfp(_EM_INVALID, _MCW_EM);
Related concepts
3.9 Floating-point functions for compatibility with Microsoft products on page 3-157.
Related references
5.1 _clearfp() on page 5-257.
5.8 _statusfp() on page 5-268.
5.5 __ieee_status() on page 5-263.
5.3 __fp_status() on page 5-260.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 5-259
Non-Confidential
5 Floating-point Support Functions
5.3 __fp_status()
5.3 __fp_status()
Some older versions of the ARM libraries implemented a function called __fp_status() that
manipulated a status word in the floating-point environment.
This is the same as __ieee_status() but it uses an older style of status word layout. The compiler still
supports the __fp_status() function for backwards compatibility. __fp_status() is defined in
stdlib.h.
Note
This function requires you to select a floating-point model that supports exceptions. For example, --
fpmode=ieee_full or --fpmode=ieee_fixed.
31 24 23 21 20 16 15 13 12 8 7 5 4 0
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 5-260
Non-Confidential
5 Floating-point Support Functions
5.3 __fp_status()
For example, to trap the Invalid Operation exception and untrap all other exceptions, you would call
__fp_status() with the following input parameters:
Related concepts
3.8 Controlling the ARM floating-point environment on page 3-156.
3.8 Controlling the ARM floating-point environment on page 3-156.
Related references
5.5 __ieee_status() on page 5-263.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 5-261
Non-Confidential
5 Floating-point Support Functions
5.4 gamma(), gamma_r()
Note
Despite their names, these functions compute the logarithm of the gamma function, not the gamma
function itself. To compute the gamma function itself, use tgamma().
Note
If you are migrating from RVCT, be aware that these functions are deprecated in ARM Compiler 4.1 and
later.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 5-262
Non-Confidential
5 Floating-point Support Functions
5.5 __ieee_status()
5.5 __ieee_status()
The ARM compiler toolchain supports an interface to the status word in the floating-point environment.
This interface is provided as function __ieee_status() and it is generally the most efficient function to
use for modifying the status word for VFP.
__ieee_status() is defined in fenv.h.
Note
This function requires you to select a floating-point model that supports exceptions. For example, --
fpmode=ieee_full or --fpmode=ieee_fixed.
__ieee_status() modifies the writable parts of the status word according to the parameters, and returns
the previous value of the whole word.
The writable bits are modified by setting them to:
new = (old & ~mask) ^ flags;
Four different operations can be performed on each bit of the status word, depending on the
corresponding bits in mask and flags.
31 28 27 26 25 24 23 22 21 20 19 18 16 15 13 12 8 7 5 4 0
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 5-263
Non-Confidential
5 Floating-point Support Functions
5.5 __ieee_status()
• Bits 8 to 12 (values 0x100 to 0x1000) are the exception masks. These control whether each exception
is trapped or not. If a bit is set to 1, the corresponding exception is trapped. If a bit is set to 0, the
corresponding exception sets its sticky flag and returns a plausible result.
• Bits 16 to 18, and bits 20 and 21, are used by VFP hardware to control the VFP vector capability. The
__ieee_status() call does not let you modify these bits.
• Bits 22 and 23 control the rounding mode. See the following table.
Note
The fz*, fj* and f* library variants support only the round-to-nearest rounding mode. If you
require support for the other rounding modes, you must use the full IEEE g* libraries. (The relevant
compiler options are --fpmode=std, --fpmode=ieee_no_fenv and --fpmode=ieee_fixed.)
• Bit 24 enables FZ (Flush to Zero) mode if it is set. In FZ mode, denormals are forced to zero to speed
up processing because denormals can be difficult to work with and slow down floating-point systems.
Setting this bit reduces accuracy but might increase speed.
Note
— The FZ bit in the IEEE status word is not supported by any of the fplib variants. This means that
switching between flushing to zero and not flushing to zero is not possible with any variant of
fplib at runtime. However, flushing to zero or not flushing to zero can be set at compile time as a
result of the library you choose to build with.
— Some functions are not provided in hardware. They exist only in the software floating-point
libraries. So these functions cannot support the FZ mode, even when you are compiling for a
hardware VFP architecture. As a result, behavior of the floating-point libraries is not consistent
across all functions when you change the FZ mode dynamically.
• Bit 27 indicates that saturation has occurred in an advanced SIMD saturating integer operation. This
is accessible through the __ieee_status() call.
• Bits marked R are reserved. They cannot be written to by the __ieee_status() call, and you must
ignore anything you find in them.
In addition to defining the __ieee_status() call itself, fenv.h also defines the following constants to
be used for the arguments:
#define FE_IEEE_FLUSHZERO (0x01000000)
#define FE_IEEE_ROUND_TONEAREST (0x00000000)
#define FE_IEEE_ROUND_UPWARD (0x00400000)
#define FE_IEEE_ROUND_DOWNWARD (0x00800000)
#define FE_IEEE_ROUND_TOWARDZERO (0x00C00000)
#define FE_IEEE_ROUND_MASK (0x00C00000)
#define FE_IEEE_MASK_INVALID (0x00000100)
#define FE_IEEE_MASK_DIVBYZERO (0x00000200)
#define FE_IEEE_MASK_OVERFLOW (0x00000400)
#define FE_IEEE_MASK_UNDERFLOW (0x00000800)
#define FE_IEEE_MASK_INEXACT (0x00001000)
#define FE_IEEE_MASK_ALL_EXCEPT (0x00001F00)
#define FE_IEEE_INVALID (0x00000001)
#define FE_IEEE_DIVBYZERO (0x00000002)
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 5-264
Non-Confidential
5 Floating-point Support Functions
5.5 __ieee_status()
For example, to set the rounding mode to round down, you would call:
__ieee_status(FE_IEEE_ROUND_MASK, FE_IEEE_ROUND_DOWNWARD);
To trap the Invalid Operation exception and untrap all other exceptions:
__ieee_status(FE_IEEE_MASK_ALL_EXCEPT, FE_IEEE_MASK_INVALID);
Related concepts
3.8 Controlling the ARM floating-point environment on page 3-156.
3.8 Controlling the ARM floating-point environment on page 3-156.
3.29 Exceptions arising from IEEE 754 floating-point arithmetic on page 3-185.
Related references
3.16 ARM floating-point compiler extensions to the C99 interface on page 3-164.
5.3 __fp_status() on page 5-260.
1.92 C and C++ library naming conventions on page 1-127.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 5-265
Non-Confidential
5 Floating-point Support Functions
5.6 j0(), j1(), jn(), Bessel functions of the first kind
If the absolute value of x exceeds pi times 2^52, these functions return an ERANGE error, denoting total
loss of significance in the result.
Note
If you are migrating from RVCT, be aware that these functions are deprecated in ARM Compiler 4.1 and
later.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 5-266
Non-Confidential
5 Floating-point Support Functions
5.7 significand(), fractional part of a number
Note
If you are migrating from RVCT, be aware that this function is deprecated in ARM Compiler 4.1 and
later.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 5-267
Non-Confidential
5 Floating-point Support Functions
5.8 _statusfp()
5.8 _statusfp()
Defined in float.h, the _statusfp() function is provided for compatibility with Microsoft products. It
returns the current value of the exception sticky flags.
You can use the _controlfp() argument macros, for example _EM_INVALID and _EM_ZERODIVIDE, to
test bits of the returned result.
The function prototype for _statusfp() is:
unsigned _statusfp(void);
Note
This function requires you to select a floating-point model that supports exceptions. For example, --
fpmode=ieee_full or --fpmode=ieee_fixed.
Related concepts
3.9 Floating-point functions for compatibility with Microsoft products on page 3-157.
Related references
5.1 _clearfp() on page 5-257.
5.2 _controlfp() on page 5-258.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 5-268
Non-Confidential
5 Floating-point Support Functions
5.9 y0(), y1(), yn(), Bessel functions of the second kind
If x is positive and exceeds pi times 2^52, these functions return an ERANGE error, denoting total loss of
significance in the result.
Note
If you are migrating from RVCT, be aware that these functions are deprecated in ARM Compiler 4.1 and
later.
ARM DUI0378E Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved. 5-269
Non-Confidential