dpkg-buildflags.1 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. .\" dpkg manual page - dpkg-buildflags(1)
  2. .\"
  3. .\" Copyright © 2010-2011 Raphaël Hertzog <hertzog@debian.org>
  4. .\" Copyright © 2011 Kees Cook <kees@debian.org>
  5. .\" Copyright © 2011-2013 Guillem Jover <guillem@debian.org>
  6. .\"
  7. .\" This is free software; you can redistribute it and/or modify
  8. .\" it under the terms of the GNU General Public License as published by
  9. .\" the Free Software Foundation; either version 2 of the License, or
  10. .\" (at your option) any later version.
  11. .\"
  12. .\" This is distributed in the hope that it will be useful,
  13. .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. .\" GNU General Public License for more details.
  16. .\"
  17. .\" You should have received a copy of the GNU General Public License
  18. .\" along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. .
  20. .TH dpkg\-buildflags 1 "2013-03-28" "Debian Project" "dpkg suite"
  21. .SH NAME
  22. dpkg\-buildflags \- returns build flags to use during package build
  23. .
  24. .SH SYNOPSIS
  25. .B dpkg\-buildflags
  26. .RI [ option "...] [" command ]
  27. .
  28. .SH DESCRIPTION
  29. \fBdpkg\-buildflags\fP is a tool to retrieve compilation flags to use during
  30. build of Debian packages.
  31. .
  32. The default flags are defined by the vendor but they can be
  33. extended/overriden in several ways:
  34. .IP 1.
  35. system-wide with \fB/etc/dpkg/buildflags.conf\fP;
  36. .IP 2.
  37. for the current user with \fB$XDG_CONFIG_HOME/dpkg/buildflags.conf\fP
  38. where \fB$XDG_CONFIG_HOME\fP defaults to \fB$HOME/.config\fP;
  39. .IP 3.
  40. temporarily by the user with environment variables (see section
  41. \fBENVIRONMENT\fP);
  42. .IP 4.
  43. dynamically by the package maintainer with environment variables set via
  44. \fBdebian/rules\fP (see section \fBENVIRONMENT\fP).
  45. .P
  46. The configuration files can contain two types of directives:
  47. .TP
  48. .BI SET " flag value"
  49. Override the flag named \fIflag\fP to have the value \fIvalue\fP.
  50. .TP
  51. .BI STRIP " flag value"
  52. Strip from the flag named \fIflag\fP all the build flags listed in \fIvalue\fP.
  53. .TP
  54. .BI APPEND " flag value"
  55. Extend the flag named \fIflag\fP by appending the options given in \fIvalue\fP.
  56. A space is prepended to the appended value if the flag's current value is non-empty.
  57. .TP
  58. .BI PREPEND " flag value"
  59. Extend the flag named \fIflag\fP by prepending the options given in \fIvalue\fP.
  60. A space is appended to the prepended value if the flag's current value is non-empty.
  61. .P
  62. The configuration files can contain comments on lines starting with a hash
  63. (#). Empty lines are also ignored.
  64. .SH COMMANDS
  65. .TP
  66. .BI \-\-dump
  67. Print to standard output all compilation flags and their values. It prints
  68. one flag per line separated from its value by an equal sign
  69. ("\fIflag\fP=\fIvalue\fP"). This is the default action.
  70. .TP
  71. .BI \-\-list
  72. Print the list of flags supported by the current vendor
  73. (one per line). See the \fBSUPPORTED FLAGS\fP section for more
  74. information about them.
  75. .TP
  76. .BI \-\-status
  77. Display any information that can be useful to explain the behaviour of
  78. \fBdpkg\-buildflags\fP: relevant environment variables, current vendor,
  79. state of all feature flags. Also print the resulting compiler flags with
  80. their origin.
  81. This is intended to be run from \fBdebian/rules\fP, so that the build log
  82. keeps a clear trace of the build flags used. This can be useful to diagnose
  83. problems related to them.
  84. .TP
  85. .BI \-\-export= format
  86. Print to standard output commands that can be used to export all the
  87. compilation flags for some particular tool. If the \fIformat\fP value is not
  88. given, \fBsh\fP is assumed. Only compilation flags starting with an
  89. upper case character are included, others are assumed to not be suitable
  90. for the environment. Supported formats:
  91. .RS
  92. .TP
  93. .B sh
  94. Shell commands to set and export all the compilation flags in the
  95. environment. The flag values are quoted so the output is ready for
  96. evaluation by a shell.
  97. .TP
  98. .B cmdline
  99. Arguments to pass to a build program's command line to use all the
  100. compilation flags (since dpkg 1.17.0). The flag values are quoted in
  101. shell syntax.
  102. .TP
  103. .B configure
  104. This is a legacy alias for \fBcmdline\fP.
  105. .TP
  106. .B make
  107. Make directives to set and export all the compilation flags in the
  108. environment. Output can be written to a makefile fragment and
  109. evaluated using an \fBinclude\fP directive.
  110. .RE
  111. .TP
  112. .BI \-\-get " flag"
  113. Print the value of the flag on standard output. Exits with 0
  114. if the flag is known otherwise exits with 1.
  115. .TP
  116. .BI \-\-origin " flag"
  117. Print the origin of the value that is returned by \fB\-\-get\fP. Exits
  118. with 0 if the flag is known otherwise exits with 1. The origin can be one
  119. of the following values:
  120. .RS
  121. .TP
  122. .B vendor
  123. the original flag set by the vendor is returned;
  124. .TP
  125. .B system
  126. the flag is set/modified by a system-wide configuration;
  127. .TP
  128. .B user
  129. the flag is set/modified by a user-specific configuration;
  130. .TP
  131. .B env
  132. the flag is set/modified by an environment-specific configuration.
  133. .RE
  134. .TP
  135. .BI \-\-query\-features " area"
  136. Print the features enabled for a given area. The only currently recognized
  137. area is \fBhardening\fP, see the \fBFEATURE AREAS\fP section for more details.
  138. Exits with 0 if the area is known otherwise exits with 1.
  139. .IP
  140. The output is in RFC822 format, with one section per feature.
  141. For example:
  142. .IP
  143. .nf
  144. Feature: pie
  145. Enabled: no
  146. Feature: stackprotector
  147. Enabled: yes
  148. .fi
  149. .TP
  150. .B \-\-help
  151. Show the usage message and exit.
  152. .TP
  153. .B \-\-version
  154. Show the version and exit.
  155. .
  156. .SH SUPPORTED FLAGS
  157. .TP
  158. .B CFLAGS
  159. Options for the C compiler. The default value set by the vendor
  160. includes \fB\-g\fP and the default optimization level (\fB\-O2\fP usually,
  161. or \fB\-O0\fP if the \fBDEB_BUILD_OPTIONS\fP environment variable defines
  162. \fInoopt\fP).
  163. .TP
  164. .B CPPFLAGS
  165. Options for the C preprocessor. Default value: empty.
  166. .TP
  167. .B CXXFLAGS
  168. Options for the C++ compiler. Same as \fBCFLAGS\fP.
  169. .TP
  170. .B GCJFLAGS
  171. Options for the GNU Java compiler (gcj). A subset of \fBCFLAGS\fP.
  172. .TP
  173. .B FFLAGS
  174. Options for the Fortran compiler. Same as \fBCFLAGS\fP.
  175. .TP
  176. .B LDFLAGS
  177. Options passed to the compiler when linking executables or shared
  178. objects (if the linker is called directly, then
  179. .B \-Wl
  180. and
  181. .B ,
  182. have to be stripped from these options). Default value: empty.
  183. .PP
  184. New flags might be added in the future if the need arises (for example
  185. to support other languages).
  186. .
  187. .SH FEATURE AREAS
  188. .SS Hardening
  189. Several compile-time options (detailed below) can be used to help harden
  190. a resulting binary against memory corruption attacks, or provide
  191. additional warning messages during compilation. Except as noted below,
  192. these are enabled by default for architectures that support them.
  193. .P
  194. Each hardening feature can be enabled and disabled in the
  195. \fBDEB_BUILD_MAINT_OPTIONS\fP environment variable's \fBhardening\fP
  196. value with the "+" and "\-" modifier. For example, to enable the
  197. "pie" feature and disable the "fortify" feature you can do this
  198. in \fBdebian/rules\fP:
  199. .P
  200. export DEB_BUILD_MAINT_OPTIONS=hardening=+pie,\-fortify
  201. .P
  202. The special feature \fBall\fP can be used to enable or disable all
  203. hardening features at the same time. Thus disabling everything and
  204. enabling only "format" and "fortify" can be achieved with:
  205. .P
  206. export DEB_BUILD_MAINT_OPTIONS=hardening=\-all,+format,+fortify
  207. .
  208. .TP
  209. .B format
  210. This setting (enabled by default) adds
  211. .B \-Wformat \-Werror=format\-security
  212. to \fBCFLAGS\fP and \fBCXXFLAGS\fP. This will warn about improper format
  213. string uses, and will fail when format functions are used in a way
  214. that represent possible security problems. At present, this warns about
  215. calls to \fBprintf\fP and \fBscanf\fP functions where the format string is
  216. not a string literal and there are no format arguments, as in
  217. \fBprintf(foo);\fP instead of \fPprintf("%s", foo);\fP
  218. This may be a security hole if the format string came from untrusted
  219. input and contains "%n".
  220. .
  221. .TP
  222. .B fortify
  223. This setting (enabled by default) adds
  224. .B \-D_FORTIFY_SOURCE=2
  225. to \fBCPPFLAGS\fP. During code generation the compiler
  226. knows a great deal of information about buffer sizes (where possible), and
  227. attempts to replace insecure unlimited length buffer function calls with
  228. length-limited ones. This is especially useful for old, crufty code.
  229. Additionally, format strings in writable memory that contain '%n' are
  230. blocked. If an application depends on such a format string, it will need
  231. to be worked around.
  232. Note that for this option to have any effect, the source must also
  233. be compiled with \fB\-O1\fP or higher. If the environment variable
  234. \fBDEB_BUILD_OPTIONS\fP contains \fInoopt\fP, then \fBfortify\fP
  235. support will be disabled, due to new warnings being issued by
  236. glibc 2.16 and later.
  237. .TP
  238. .B stackprotector
  239. This setting (enabled by default) adds
  240. .B \-fstack-protector \-\-param=ssp\-buffer\-size=4
  241. to \fBCFLAGS\fP and \fBCXXFLAGS\fP. This adds safety checks against stack
  242. overwrites. This renders many potential code injection attacks into
  243. aborting situations. In the best case this turns code injection
  244. vulnerabilities into denial of service or into non-issues (depending on
  245. the application).
  246. This feature requires linking against glibc (or another provider of
  247. \fB__stack_chk_fail\fP), so needs to be disabled when building with
  248. \fB\-nostdlib\fP or \fB\-ffreestanding\fP or similar.
  249. .
  250. .TP
  251. .B relro
  252. This setting (enabled by default) adds
  253. .B \-Wl,\-z,relro
  254. to \fBLDFLAGS\fP. During program load, several ELF memory sections need
  255. to be written to by the linker. This flags the loader to turn these
  256. sections read-only before turning over control to the program. Most
  257. notably this prevents GOT overwrite attacks. If this option is disabled,
  258. \fBbindnow\fP will become disabled as well.
  259. .
  260. .TP
  261. .B bindnow
  262. This setting (disabled by default) adds
  263. .B \-Wl,\-z,now
  264. to \fBLDFLAGS\fP. During program load, all dynamic symbols are resolved,
  265. allowing for the entire PLT to be marked read-only (due to \fBrelro\fP
  266. above). The option cannot become enabled if \fBrelro\fP is not enabled.
  267. .
  268. .TP
  269. .B pie
  270. This setting (disabled by default) adds \fB\-fPIE\fP to \fBCFLAGS\fP and
  271. \fBCXXFLAGS\fP, and \fB\-fPIE \-pie\fP to \fBLDFLAGS\fP. Position Independent
  272. Executable are needed to take advantage of Address Space Layout
  273. Randomization, supported by some kernel versions. While ASLR can already
  274. be enforced for data areas in the stack and heap (brk and mmap), the code
  275. areas must be compiled as position-independent. Shared libraries already
  276. do this (\-fPIC), so they gain ASLR automatically, but binary .text
  277. regions need to be build PIE to gain ASLR. When this happens, ROP (Return
  278. Oriented Programming) attacks are much harder since there are no static
  279. locations to bounce off of during a memory corruption attack.
  280. This is not compatible with \fB\-fPIC\fP so care must be taken when
  281. building shared objects.
  282. Additionally, since PIE is implemented via a general register, some
  283. architectures (most notably i386) can see performance losses of up to
  284. 15% in very text-segment-heavy application workloads; most workloads
  285. see less than 1%. Architectures with more general registers (e.g. amd64)
  286. do not see as high a worst-case penalty.
  287. .
  288. .SH ENVIRONMENT
  289. There are 2 sets of environment variables doing the same operations, the
  290. first one (DEB_\fIflag\fP_\fIop\fP) should never be used within
  291. \fBdebian/rules\fP. It's meant for any user that wants to rebuild the
  292. source package with different build flags. The second set
  293. (DEB_\fIflag\fP_MAINT_\fIop\fP) should only be used in \fBdebian/rules\fP
  294. by package maintainers to change the resulting build flags.
  295. .TP
  296. .BI DEB_ flag _SET
  297. .TQ
  298. .BI DEB_ flag _MAINT_SET
  299. This variable can be used to force the value returned for the given
  300. \fIflag\fP.
  301. .TP
  302. .BI DEB_ flag _STRIP
  303. .TQ
  304. .BI DEB_ flag _MAINT_STRIP
  305. This variable can be used to provide a space separated list of options
  306. that will be stripped from the set of flags returned for the given
  307. \fIflag\fP.
  308. .TP
  309. .BI DEB_ flag _APPEND
  310. .TQ
  311. .BI DEB_ flag _MAINT_APPEND
  312. This variable can be used to append supplementary options to the value
  313. returned for the given \fIflag\fP.
  314. .TP
  315. .BI DEB_ flag _PREPEND
  316. .TQ
  317. .BI DEB_ flag _MAINT_PREPEND
  318. This variable can be used to prepend supplementary options to the value
  319. returned for the given \fIflag\fP.
  320. .TP
  321. .B DEB_BUILD_MAINT_OPTIONS
  322. This variable can be used to disable/enable various hardening build
  323. flags through the \fBhardening\fP option. See the \fBFEATURE AREAS\fP section
  324. for details.
  325. .
  326. .SH FILES
  327. .SS Configuration files
  328. .TP
  329. .B /etc/dpkg/buildflags.conf
  330. System wide configuration file.
  331. .TP
  332. .BR $XDG_CONFIG_HOME/dpkg/buildflags.conf " or " $HOME/.config/dpkg/buildflags.conf
  333. User configuration file.
  334. .SS Packaging support
  335. .TP
  336. .B /usr/share/dpkg/buildflags.mk
  337. Makefile snippet that will load (and optionally export) all flags
  338. supported by \fBdpkg-buildflags\fP into variables (since dpkg 1.16.1).
  339. .
  340. .SH EXAMPLES
  341. To pass build flags to a build command in a makefile:
  342. .PP
  343. .RS 4
  344. .nf
  345. $(MAKE) $(shell dpkg\-buildflags \-\-export=cmdline)
  346. \&./configure $(shell dpkg\-buildflags \-\-export=cmdline)
  347. .fi
  348. .RE
  349. .PP
  350. To set build flags in a shell script or shell fragment, "eval" can be
  351. used to interpret the output and to export the flags in the environment:
  352. .PP
  353. .RS 4
  354. .nf
  355. eval "$(dpkg\-buildflags \-\-export=sh)" && make
  356. .fi
  357. .RE
  358. .PP
  359. or to set the positional parameters to pass to a command:
  360. .PP
  361. .RS 4
  362. .nf
  363. eval "set \-\- $(dpkg\-buildflags \-\-export=cmdline)"
  364. for dir in a b c; do (cd $dir && ./configure "$@" && make); done
  365. .fi
  366. .RE
  367. .
  368. .SS Usage in debian/rules
  369. You should call \fBdpkg\-buildflags\fP or include \fBbuildflags.mk\fP
  370. from the \fBdebian/rules\fP file to obtain the needed build flags to
  371. pass to the build system.
  372. Note that older versions of \fBdpkg\-buildpackage\fP (before dpkg 1.16.1)
  373. exported these flags automatically. However, you should not rely on this,
  374. since this breaks manual invocation of \fBdebian/rules\fP.
  375. .PP
  376. For packages with autoconf-like build systems, you can pass the relevant
  377. options to configure or \fBmake\fP(1) directly, as shown above.
  378. .PP
  379. For other build systems, or when you need more fine-grained control
  380. about which flags are passed where, you can use \fB\-\-get\fP. Or you
  381. can include \fBbuildflags.mk\fP instead, which takes care of calling
  382. \fBdpkg\-buildflags\fP and storing the build flags in make variables.
  383. .PP
  384. If you want to export all buildflags into the environment (where they
  385. can be picked up by your build system):
  386. .PP
  387. .RS 4
  388. .nf
  389. DPKG_EXPORT_BUILDFLAGS = 1
  390. include /usr/share/dpkg/buildflags.mk
  391. .fi
  392. .RE
  393. .PP
  394. For some extra control over what is exported, you can manually export
  395. the variables (as none are exported by default):
  396. .PP
  397. .RS 4
  398. .nf
  399. include /usr/share/dpkg/buildflags.mk
  400. export CPPFLAGS CFLAGS LDFLAGS
  401. .fi
  402. .RE
  403. .PP
  404. And you can of course pass the flags to commands manually:
  405. .PP
  406. .RS 4
  407. .nf
  408. include /usr/share/dpkg/buildflags.mk
  409. build\-arch:
  410. \& $(CC) \-o hello hello.c $(CPPFLAGS) $(CFLAGS) $(LDFLAGS)
  411. .fi
  412. .RE