1.\" $OpenBSD: gcc-local.1,v 1.24 2011/07/25 19:24:03 tedu Exp $ 2.\" 3.\" Copyright (c) 2002 Marc Espie 4.\" Copyright (c) 2003 Anil Madhavapeddy 5.\" 6.\" All rights reserved. 7.\" 8.\" Redistribution and use in source and binary forms, with or without 9.\" modification, are permitted provided that the following conditions 10.\" are met: 11.\" 1. Redistributions of source code must retain the above copyright 12.\" notice, this list of conditions and the following disclaimer. 13.\" 2. Redistributions in binary form must reproduce the above copyright 14.\" notice, this list of conditions and the following disclaimer in the 15.\" documentation and/or other materials provided with the distribution. 16.\" 17.\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR 18.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20.\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT, 21.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27.\" 28.Dd $Mdocdate: July 25 2011 $ 29.Dt GCC-LOCAL 1 30.Os 31.Sh NAME 32.Nm gcc-local 33.Nd local modifications to gcc 34.Sh DESCRIPTION 35.Ox 36uses derivatives of 37.Xr gcc 1 38versions 2.95.3, 3.3.5, or 4.2.1, 39depending on machine architecture. 40In all cases, 41the software comes with specific modifications for 42.Ox : 43.Bl -dash 44.It 45.Nm gcc 46does not search under 47.Pa /usr/local 48for include files nor for libraries: 49as a system compiler, it only searches the system paths by default. 50.It 51On 52.Ox , 53the 54.Fl pthread 55option should be used to link threaded code, isolating the program from 56operating system details. 57.It 58On most architectures, 59trampoline code marks the smallest possible area around the trampoline stub 60executable using 61.Xr mprotect 2 , 62since the stack area is by default non-executable. 63.It 64On 65.Ox , 66by default, trampoline code generation is disabled. 67Code requiring trampolines will not compile without 68.Fl ftrampolines . 69The warning flag 70.Fl Wtrampolines 71can be used to locate trampoline instances if trampoline generation 72is re-enabled. 73.It 74The 75.Fl O2 76option does not include 77.Fl fstrict-aliasing , 78as this option causes issues on some legacy code. 79.Fl fstrict-aliasing 80is very unsafe with code that plays tricks with casts, bypassing the 81already weak type system of C. 82.It 83The 84.Fl O2 85option does not include 86.Fl fstrict-overflow , 87as this option causes issues on some legacy code. 88.Fl fstrict-overflow 89can cause surprising optimizations to occur, possibly deleting security 90critical overflow checks. 91.It 92The option 93.Fl fno-builtin-<function> 94was backported from 95.Nm gcc 3.3.5 , 96to 97.Nm gcc 2.95.3 , 98and can be used without having to differentiate between 99both compilers. 100.It 101.Nm gcc 102recognizes the extra format attribute syslog, to better match 103the definition of 104.Xr syslog 3 , 105and silence erroneous warnings when used with 106.Fl pedantic . 107.It 108Even in 2.95.3, 109.Nm gcc 110recognizes the attribute 111.Dv __nonnull__ , 112which can be used to mark arguments that can't be 113.Dv NULL . 114The printf format attribute does not imply 115.Dv __nonnull__ 116for the format. 117This allows for correct format checking on the 118.Xr err 3 119function family. 120.It 121.Nm gcc 122recognizes the extra attribute 123.Dv __sentinel__ , 124which can be used to mark varargs function that need a 125.Dv NULL 126pointer to mark argument termination, like 127.Xr execl 3 . 128This exposes latent bugs for 64-bit architectures, 129where a terminating 0 will expand to a 32-bit int, and not a full-fledged 13064-bits pointer. 131.It 132On some 133.Ox 134platforms, 135.Nm gcc 136still uses 137.Xr setjmp 3 / 138.Xr longjmp 3 - 139style exceptions, and so needs extra fixes beyond the pure 2.95.3 release. 140.It 141On a few 142platforms (mostly a.out), 143.Nm gcc 144uses a linker wrapper to write stubs that call global constructors and 145destructors. 146Those platforms use 147.Nm gcc 2.95.3 , 148and those calls can be traced using 149.Fl Wl,-trace-ctors-dtors , 150using 151.Xr syslog_r 3 . 152.It 153On alpha, 154.Fl mieee 155is enabled by default to enable full compliance with 156the IEEE floating point standard, 157although the 158.Dq inexact 159flag is not maintained. 160Additionally, rounding mode is dynamic. 161.It 162On 163.Ox , 164.Nm gcc 165comes with the 166.Dq ProPolice 167stack protection extension, which is enabled by default. 168This extension reorders local variable declarations and adds stack consistency 169checks at run time, in order to detect stack overflows, and will attempt to 170report the problem in the system logs by calling 171.Xr syslog 3 172with a 173.Dv LOG_CRIT 174priority message: 175.Dq stack overflow in function XXX , 176and abort the faulting process. 177It can be turned off using the 178.Fl fno-stack-protector 179commandline option. 180Note that the stack protector relies on some support code in libc. 181Stand-alone programs not linked against libc must either provide their own 182support bits, or use the 183.Fl fno-stack-protector 184option. 185There is also a 186.Fl fstack-protector-all 187option, that turns stack protection code on for all functions, 188and disables any heuristic that flags some functions as safe. 189This extended checking has a moderate runtime cost, though. 190.It 191.Nm gcc 192recognizes a new flag, 193.Fl Wbounded , 194to perform basic checks on functions which accept buffers and sizes. 195An extra attribute, 196.Dv __bounded__ , 197has been added to mark functions that can be 198checked this way. 199.It 200.Nm gcc 201recognizes a new format attribute, kprintf, to deal with the extra format 202arguments 203.Ql %b , 204.Ql %r , 205and 206.Ql %z 207used in the 208.Ox 209kernel. 210.It 211.Nm gcc 212does not store its version string in objects. 213This behavior can be restored with 214.Fl fident . 215.It 216On 217.Ox , 218.Nm gcc 219will not move variables initialized with the value zero 220from the data section to the bss section. 221The default behaviour of 222.Nm gcc 3.3.5 223on other systems is to perform this action, which can be restored for 224.Ox 225with 226.Fl fzero-initialized-in-bss . 227.It 228On 229.Ox , 230.Nm gcc 231does not warn for cast expressions used as lvalues outside of 232.Fl pedantic . 233.It 234On 235.Ox , 236.Nm gcc 4.2.1 237does not warn for pointer arguments passing or assignment with 238different signedness outside of 239.Fl pedantic . 240This can be 241re-enabled with the 242.Fl Wpointer-sign 243flag. 244.It 245Even in 2.95.3, 246.Nm gcc 247recognizes the preprocessor flag 248.Fl CC 249that lets comments in macros pass through to the output (except in 250.Fl traditional 251mode). 252This is used to allow annotations in macros for 253.Xr lint 1 . 254.It 255On 256.Ox , 257.Nm gcc 258supports two extra warning options: 259.Bl -item 260.It 261.Fl Wstack-larger-than- Ns Va N 262will report functions using more than 263.Va N 264bytes of stack space for their local variables. 265Stack space used for other purposes (such as register window saving, 266callee-saved registers, or outbound arguments storage) 267is not taken into account for this check. 268.It 269.Fl Wvariable-decl 270will report automatic variable declarations whose size cannot be 271determined at compile-time. 272.El 273.El 274.Sh ATTRIBUTES 275The 276.Dv __bounded__ 277attribute is used to type-check functions whose parameters pass fixed-length 278buffers and their sizes. 279The syntax for normal buffers is: 280.Pp 281.Li __attribute__ ((__bounded__ ( 282.Dv __buffer__ , 283.Va buffer , 284.Va length 285.Li ))) 286.Pp 287where 288.Fa buffer 289contains the parameter number (starting from 1) of the pointer to the buffer, 290and 291.Fa length 292contains the parameter number of the buffer length argument. 293.Pp 294.Nm gcc 295will emit a warning if the length argument is a constant larger than the 296actual size of the buffer. 297If the buffer is not a statically declared array of fixed length, no warnings 298will be generated. 299Refer to 300.Xr memcpy 3 301for an example of a function with this check. 302.Pp 303For checking strings, just use 304.Dv __string__ 305instead of 306.Dv __buffer__ : 307.Pp 308.Li __attribute__ ((__bounded__ ( 309.Dv __string__ , 310.Va buffer , 311.Va length 312.Li ))) 313.Pp 314In addition to the checks described above, this also tests if the 315.Va length 316argument was wrongly derived from a 317.Fn sizeof "void *" 318operation. 319.Xr strlcpy 3 320is a good example of a string function with this check. 321.Pp 322Some functions specify the length as two arguments: 323the number of elements and the size of each element. 324In this case, use the 325.Dv __size__ 326attribute: 327.Pp 328.Li __attribute__ ((__bounded__ ( 329.Dv __size__ , 330.Va buffer , 331.Va nmemb , 332.Va size 333.Li ))) 334.Pp 335where 336.Va buffer 337contains the parameter number of the pointer to the buffer, 338.Va nmemb 339contains the parameter number of the number of members, and 340.Va size 341has the parameter number of the size of each element. 342The type checks performed by 343.Dv __size__ 344are the same as the 345.Dv __buffer__ 346attribute. 347See 348.Xr fread 3 349for an example of this type of function. 350.Pp 351If a function accepts a buffer parameter and specifies that it has to be of a 352minimum length, the __minbytes__ attribute can be used: 353.Pp 354.Li __attribute__ ((__bounded__ ( 355.Dv __minbytes__ , 356.Va buffer , 357.Va minsize 358.Li ))) 359.Pp 360where 361.Va buffer 362contains the parameter number of the pointer to the buffer, and 363.Va minsize 364specifies the minimum number of bytes that the buffer should be. 365.Xr ctime_r 3 366is an example of this type of function. 367.Pp 368If 369.Fl Wbounded 370is specified with 371.Fl Wformat , 372additional checks are performed on 373.Xr sscanf 3 374format strings. 375The 376.Ql %s 377fields are checked for incorrect bound lengths by checking the size of the 378buffer associated with the format argument. 379.Sh SEE ALSO 380.Xr gcc 1 381.Pp 382.Pa http://www.research.ibm.com/trl/projects/security/ssp/ 383.Sh CAVEATS 384The 385.Fl Wbounded 386flag only works with statically allocated fixed-size buffers. 387Since it is applied at compile-time, dynamically allocated memory buffers 388and non-constant arguments are ignored. 389