xref: /openbsd-src/share/man/man1/gcc-local.1 (revision 4c1e55dc91edd6e69ccc60ce855900fbc12cf34f)
1.\" $OpenBSD: gcc-local.1,v 1.30 2012/06/24 08:28:37 jmc 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: June 24 2012 $
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 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
51The
52.Fl pthread
53option should be used to link threaded code, isolating the program from
54operating system details.
55.It
56On most architectures,
57trampoline code marks the smallest possible area around the trampoline stub
58executable using
59.Xr mprotect 2 ,
60since the stack area is by default non-executable.
61.It
62Trampoline code generation is disabled by default.
63Code requiring trampolines will not compile without
64.Fl ftrampolines .
65The warning flag
66.Fl Wtrampolines
67can be used to locate trampoline instances if trampoline generation
68is re-enabled.
69.It
70The
71.Fl O2
72option does not include
73.Fl fstrict-aliasing ,
74as this option causes issues on some legacy code.
75.Fl fstrict-aliasing
76is very unsafe with code that plays tricks with casts, bypassing the
77already weak type system of C.
78.It
79The
80.Fl O2
81option does not include
82.Fl fstrict-overflow ,
83as this option causes issues on some legacy code.
84.Fl fstrict-overflow
85can cause surprising optimizations to occur, possibly deleting security
86critical overflow checks.
87.It
88The
89.Fl O2
90option does not include the
91.Fl ftree-vrp
92optimization as it is known to be broken in
93.Nm gcc 4.2.1 .
94.It
95The option
96.Fl fno-builtin-<function>
97was backported to
98.Nm gcc 2.95.3 ,
99and can be used without having to differentiate between
100compilers.
101.It
102.Nm gcc
103recognizes the extra format attribute syslog, to better match
104the definition of
105.Xr syslog 3 ,
106and silence erroneous warnings when used with
107.Fl pedantic .
108.It
109Even in 2.95.3,
110.Nm gcc
111recognizes the attribute
112.Dv __nonnull__ ,
113which can be used to mark arguments that can't be
114.Dv NULL .
115The printf format attribute does not imply
116.Dv __nonnull__
117for the format.
118This allows for correct format checking on the
119.Xr err 3
120function family.
121.It
122.Nm gcc
123recognizes the extra attribute
124.Dv __sentinel__ ,
125which can be used to mark varargs function that need a
126.Dv NULL
127pointer to mark argument termination, like
128.Xr execl 3 .
129This exposes latent bugs for 64-bit architectures,
130where a terminating 0 will expand to a 32-bit int, and not a full-fledged
13164-bits pointer.
132.It
133On some platforms,
134.Nm gcc
135still uses
136.Xr setjmp 3 /
137.Xr longjmp 3 -
138style exceptions, and so needs extra fixes beyond the pure 2.95.3 release.
139.It
140On a few
141platforms (mostly a.out),
142.Nm gcc
143uses a linker wrapper to write stubs that call global constructors and
144destructors.
145Those platforms use
146.Nm gcc 2.95.3 ,
147and those calls can be traced using
148.Fl Wl,-trace-ctors-dtors ,
149using
150.Xr syslog_r 3 .
151.It
152On alpha,
153.Fl mieee
154is enabled by default to enable full compliance with
155the IEEE floating point standard,
156although the
157.Dq inexact
158flag is not maintained.
159Additionally, rounding mode is dynamic.
160.It
161.Nm gcc
162comes with the
163.Dq ProPolice
164stack protection extension, which is enabled by default.
165This extension reorders local variable declarations and adds stack consistency
166checks at run time, in order to detect stack overflows, and will attempt to
167report the problem in the system logs by calling
168.Xr syslog 3
169with a
170.Dv LOG_CRIT
171priority message:
172.Dq stack overflow in function XXX ,
173and abort the faulting process.
174It can be turned off using the
175.Fl fno-stack-protector
176commandline option.
177Note that the stack protector relies on some support code in libc.
178Stand-alone programs not linked against libc must either provide their own
179support bits, or use the
180.Fl fno-stack-protector
181option.
182There is also a
183.Fl fstack-protector-all
184option, that turns stack protection code on for all functions,
185and disables any heuristic that flags some functions as safe.
186This extended checking has a moderate runtime cost, though.
187.It
188.Nm gcc
189recognizes a new flag,
190.Fl Wbounded ,
191to perform basic checks on functions which accept buffers and sizes.
192An extra attribute,
193.Dv __bounded__ ,
194has been added to mark functions that can be
195checked this way.
196.It
197.Nm gcc
198recognizes a new format attribute, kprintf, to deal with the extra format
199arguments
200.Ql %b ,
201.Ql %r ,
202and
203.Ql %z
204used in the
205.Ox
206kernel.
207.It
208.Nm gcc
209does not store its version string in objects.
210This behavior can be restored with
211.Fl fident .
212.It
213.Nm gcc
214will not move variables initialized with the value zero
215from the data section to the bss section.
216The default behaviour of
217.Nm gcc 4.2.1
218on other systems is to perform this action, which can be restored for
219.Ox
220with
221.Fl fzero-initialized-in-bss .
222.It
223.Nm gcc
224does not warn for cast expressions used as lvalues outside of
225.Fl pedantic .
226.It
227.Nm gcc 4.2.1
228does not warn for pointer arguments passing or assignment with
229different signedness outside of
230.Fl pedantic .
231This can be
232re-enabled with the
233.Fl Wpointer-sign
234flag.
235.It
236Even in 2.95.3,
237.Nm gcc
238recognizes the preprocessor flag
239.Fl CC
240that lets comments in macros pass through to the output (except in
241.Fl traditional
242mode).
243This is used to allow annotations in macros for
244lint.
245.It
246The warning option
247.Fl Wsystem-headers ,
248which makes
249.Nm gcc
250report warnings in systems headers,
251is enabled by default.
252.It
253.Nm gcc
254supports two extra warning options:
255.Bl -item
256.It
257.Fl Wstack-larger-than- Ns Va N
258will report functions using more than
259.Va N
260bytes of stack space for their local variables.
261Stack space used for other purposes (such as register window saving,
262callee-saved registers, or outbound arguments storage)
263is not taken into account for this check.
264.It
265.Fl Wvariable-decl
266will report automatic variable declarations whose size cannot be
267determined at compile-time.
268.El
269.El
270.Sh ATTRIBUTES
271The
272.Dv __bounded__
273attribute is used to type-check functions whose parameters pass fixed-length
274buffers and their sizes.
275The syntax for normal buffers is:
276.Pp
277.Li __attribute__ ((__bounded__ (
278.Dv __buffer__ ,
279.Va buffer ,
280.Va length
281.Li )))
282.Pp
283where
284.Fa buffer
285contains the parameter number (starting from 1) of the pointer to the buffer,
286and
287.Fa length
288contains the parameter number of the buffer length argument.
289.Pp
290.Nm gcc
291will emit a warning if the length argument is a constant larger than the
292actual size of the buffer.
293If the buffer is not a statically declared array of fixed length, no warnings
294will be generated.
295Refer to
296.Xr memcpy 3
297for an example of a function with this check.
298.Pp
299For checking strings, just use
300.Dv __string__
301instead of
302.Dv __buffer__ :
303.Pp
304.Li __attribute__ ((__bounded__ (
305.Dv __string__ ,
306.Va buffer ,
307.Va length
308.Li )))
309.Pp
310In addition to the checks described above, this also tests if the
311.Va length
312argument was wrongly derived from a
313.Fn sizeof "void *"
314operation.
315.Xr strlcpy 3
316is a good example of a string function with this check.
317.Pp
318Some functions specify the length as two arguments:
319the number of elements and the size of each element.
320In this case, use the
321.Dv __size__
322attribute:
323.Pp
324.Li __attribute__ ((__bounded__ (
325.Dv __size__ ,
326.Va buffer ,
327.Va nmemb ,
328.Va size
329.Li )))
330.Pp
331where
332.Va buffer
333contains the parameter number of the pointer to the buffer,
334.Va nmemb
335contains the parameter number of the number of members, and
336.Va size
337has the parameter number of the size of each element.
338The type checks performed by
339.Dv __size__
340are the same as the
341.Dv __buffer__
342attribute.
343See
344.Xr fread 3
345for an example of this type of function.
346.Pp
347If a function accepts a buffer parameter and specifies that it has to be of a
348minimum length, the __minbytes__ attribute can be used:
349.Pp
350.Li __attribute__ ((__bounded__ (
351.Dv __minbytes__ ,
352.Va buffer ,
353.Va minsize
354.Li )))
355.Pp
356where
357.Va buffer
358contains the parameter number of the pointer to the buffer, and
359.Va minsize
360specifies the minimum number of bytes that the buffer should be.
361.Xr ctime_r 3
362is an example of this type of function.
363.Pp
364If
365.Fl Wbounded
366is specified with
367.Fl Wformat ,
368additional checks are performed on
369.Xr sscanf 3
370format strings.
371The
372.Ql %s
373fields are checked for incorrect bound lengths by checking the size of the
374buffer associated with the format argument.
375.Sh SEE ALSO
376.Xr gcc 1
377.Pp
378.Pa http://www.research.ibm.com/trl/projects/security/ssp/
379.Sh CAVEATS
380The
381.Fl Wbounded
382flag only works with statically allocated fixed-size buffers.
383Since it is applied at compile-time, dynamically allocated memory buffers
384and non-constant arguments are ignored.
385