dpkg-buildflags.1 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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 <https://www.gnu.org/licenses/>.
  19. .
  20. .TH dpkg\-buildflags 1 "2013-10-20" "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 OBJCFLAGS
  171. Options for the Objective C compiler. Same as \fBCFLAGS\fP.
  172. .TP
  173. .B OBJCXXFLAGS
  174. Options for the Objective C++ compiler. Same as \fBCXXFLAGS\fP.
  175. .TP
  176. .B GCJFLAGS
  177. Options for the GNU Java compiler (gcj). A subset of \fBCFLAGS\fP.
  178. .TP
  179. .B FFLAGS
  180. Options for the Fortran 77 compiler. A subset of \fBCFLAGS\fP.
  181. .TP
  182. .B FCFLAGS
  183. Options for the Fortran 9x compiler. Same as \fBFFLAGS\fP.
  184. .TP
  185. .B LDFLAGS
  186. Options passed to the compiler when linking executables or shared
  187. objects (if the linker is called directly, then
  188. .B \-Wl
  189. and
  190. .B ,
  191. have to be stripped from these options). Default value: empty.
  192. .PP
  193. New flags might be added in the future if the need arises (for example
  194. to support other languages).
  195. .
  196. .SH FEATURE AREAS
  197. .SS Hardening
  198. Several compile-time options (detailed below) can be used to help harden
  199. a resulting binary against memory corruption attacks, or provide
  200. additional warning messages during compilation. Except as noted below,
  201. these are enabled by default for architectures that support them.
  202. .P
  203. Each hardening feature can be enabled and disabled in the
  204. \fBDEB_BUILD_MAINT_OPTIONS\fP environment variable's \fBhardening\fP
  205. value with the "+" and "\-" modifier. For example, to enable the
  206. "pie" feature and disable the "fortify" feature you can do this
  207. in \fBdebian/rules\fP:
  208. .P
  209. export DEB_BUILD_MAINT_OPTIONS=hardening=+pie,\-fortify
  210. .P
  211. The special feature \fBall\fP can be used to enable or disable all
  212. hardening features at the same time. Thus disabling everything and
  213. enabling only "format" and "fortify" can be achieved with:
  214. .P
  215. export DEB_BUILD_MAINT_OPTIONS=hardening=\-all,+format,+fortify
  216. .
  217. .TP
  218. .B format
  219. This setting (enabled by default) adds
  220. .B \-Wformat \-Werror=format\-security
  221. to \fBCFLAGS\fP, \fBCXXFLAGS\fP, \fBOBJCFLAGS\fP and \fBOBJCXXFLAGS\fP.
  222. This will warn about improper format
  223. string uses, and will fail when format functions are used in a way
  224. that represent possible security problems. At present, this warns about
  225. calls to \fBprintf\fP and \fBscanf\fP functions where the format string is
  226. not a string literal and there are no format arguments, as in
  227. \fBprintf(foo);\fP instead of \fPprintf("%s", foo);\fP
  228. This may be a security hole if the format string came from untrusted
  229. input and contains "%n".
  230. .
  231. .TP
  232. .B fortify
  233. This setting (enabled by default) adds
  234. .B \-D_FORTIFY_SOURCE=2
  235. to \fBCPPFLAGS\fP. During code generation the compiler
  236. knows a great deal of information about buffer sizes (where possible), and
  237. attempts to replace insecure unlimited length buffer function calls with
  238. length-limited ones. This is especially useful for old, crufty code.
  239. Additionally, format strings in writable memory that contain '%n' are
  240. blocked. If an application depends on such a format string, it will need
  241. to be worked around.
  242. Note that for this option to have any effect, the source must also
  243. be compiled with \fB\-O1\fP or higher. If the environment variable
  244. \fBDEB_BUILD_OPTIONS\fP contains \fInoopt\fP, then \fBfortify\fP
  245. support will be disabled, due to new warnings being issued by
  246. glibc 2.16 and later.
  247. .TP
  248. .B stackprotector
  249. This setting (enabled by default if stackprotectorstrong is not in use) adds
  250. .B \-fstack\-protector \-\-param=ssp\-buffer\-size=4
  251. to \fBCFLAGS\fP, \fBCXXFLAGS\fP, \fBOBJCFLAGS\fP, \fBOBJCXXFLAGS\fP,
  252. \fBGCJFLAGS\fP, \fBFFLAGS\fP and \fBFCFLAGS\fP.
  253. This adds safety checks against stack
  254. overwrites. This renders many potential code injection attacks into
  255. aborting situations. In the best case this turns code injection
  256. vulnerabilities into denial of service or into non-issues (depending on
  257. the application).
  258. This feature requires linking against glibc (or another provider of
  259. \fB__stack_chk_fail\fP), so needs to be disabled when building with
  260. \fB\-nostdlib\fP or \fB\-ffreestanding\fP or similar.
  261. .
  262. .TP
  263. .B stackprotectorstrong
  264. This setting (enabled by default) adds
  265. .B \-fstack\-protector\-strong
  266. to \fBCFLAGS\fP, \fBCXXFLAGS\fP, \fBOBJCFLAGS\fP, \fBOBJCXXFLAGS\fP,
  267. \fBGCJFLAGS\fP, \fBFFLAGS\fP and \fBFCFLAGS\fP.
  268. This is a stronger variant of \fBstackprotector\fP, but without significant
  269. performance penalties.
  270. Disabling \fBstackprotector\fP will also disable this setting.
  271. This feature has the same requirements as \fBstackprotector\fP, and in
  272. addition also requires gcc 4.9 and later.
  273. .
  274. .TP
  275. .B relro
  276. This setting (enabled by default) adds
  277. .B \-Wl,\-z,relro
  278. to \fBLDFLAGS\fP. During program load, several ELF memory sections need
  279. to be written to by the linker. This flags the loader to turn these
  280. sections read-only before turning over control to the program. Most
  281. notably this prevents GOT overwrite attacks. If this option is disabled,
  282. \fBbindnow\fP will become disabled as well.
  283. .
  284. .TP
  285. .B bindnow
  286. This setting (disabled by default) adds
  287. .B \-Wl,\-z,now
  288. to \fBLDFLAGS\fP. During program load, all dynamic symbols are resolved,
  289. allowing for the entire PLT to be marked read-only (due to \fBrelro\fP
  290. above). The option cannot become enabled if \fBrelro\fP is not enabled.
  291. .
  292. .TP
  293. .B pie
  294. This setting (disabled by default) adds \fB\-fPIE\fP to \fBCFLAGS\fP,
  295. \fBCXXFLAGS\fP, \fBOBJCFLAGS\fP, \fBOBJCXXFLAGS\fP, \fBGCJFLAGS\fP,
  296. \fBFFLAGS\fP and \fBFCFLAGS\fP,
  297. and \fB\-fPIE \-pie\fP to \fBLDFLAGS\fP. Position Independent
  298. Executable are needed to take advantage of Address Space Layout
  299. Randomization, supported by some kernel versions. While ASLR can already
  300. be enforced for data areas in the stack and heap (brk and mmap), the code
  301. areas must be compiled as position-independent. Shared libraries already
  302. do this (\-fPIC), so they gain ASLR automatically, but binary .text
  303. regions need to be build PIE to gain ASLR. When this happens, ROP (Return
  304. Oriented Programming) attacks are much harder since there are no static
  305. locations to bounce off of during a memory corruption attack.
  306. This is not compatible with \fB\-fPIC\fP so care must be taken when
  307. building shared objects.
  308. Additionally, since PIE is implemented via a general register, some
  309. architectures (most notably i386) can see performance losses of up to
  310. 15% in very text-segment-heavy application workloads; most workloads
  311. see less than 1%. Architectures with more general registers (e.g. amd64)
  312. do not see as high a worst-case penalty.
  313. .
  314. .SH ENVIRONMENT
  315. There are 2 sets of environment variables doing the same operations, the
  316. first one (DEB_\fIflag\fP_\fIop\fP) should never be used within
  317. \fBdebian/rules\fP. It's meant for any user that wants to rebuild the
  318. source package with different build flags. The second set
  319. (DEB_\fIflag\fP_MAINT_\fIop\fP) should only be used in \fBdebian/rules\fP
  320. by package maintainers to change the resulting build flags.
  321. .TP
  322. .BI DEB_ flag _SET
  323. .TQ
  324. .BI DEB_ flag _MAINT_SET
  325. This variable can be used to force the value returned for the given
  326. \fIflag\fP.
  327. .TP
  328. .BI DEB_ flag _STRIP
  329. .TQ
  330. .BI DEB_ flag _MAINT_STRIP
  331. This variable can be used to provide a space separated list of options
  332. that will be stripped from the set of flags returned for the given
  333. \fIflag\fP.
  334. .TP
  335. .BI DEB_ flag _APPEND
  336. .TQ
  337. .BI DEB_ flag _MAINT_APPEND
  338. This variable can be used to append supplementary options to the value
  339. returned for the given \fIflag\fP.
  340. .TP
  341. .BI DEB_ flag _PREPEND
  342. .TQ
  343. .BI DEB_ flag _MAINT_PREPEND
  344. This variable can be used to prepend supplementary options to the value
  345. returned for the given \fIflag\fP.
  346. .TP
  347. .B DEB_BUILD_MAINT_OPTIONS
  348. This variable can be used to disable/enable various hardening build
  349. flags through the \fBhardening\fP option. See the \fBFEATURE AREAS\fP section
  350. for details.
  351. .
  352. .SH FILES
  353. .SS Configuration files
  354. .TP
  355. .B /etc/dpkg/buildflags.conf
  356. System wide configuration file.
  357. .TP
  358. .BR $XDG_CONFIG_HOME/dpkg/buildflags.conf " or " $HOME/.config/dpkg/buildflags.conf
  359. User configuration file.
  360. .SS Packaging support
  361. .TP
  362. .B /usr/share/dpkg/buildflags.mk
  363. Makefile snippet that will load (and optionally export) all flags
  364. supported by \fBdpkg-buildflags\fP into variables (since dpkg 1.16.1).
  365. .
  366. .SH EXAMPLES
  367. To pass build flags to a build command in a makefile:
  368. .PP
  369. .RS 4
  370. .nf
  371. $(MAKE) $(shell dpkg\-buildflags \-\-export=cmdline)
  372. \&./configure $(shell dpkg\-buildflags \-\-export=cmdline)
  373. .fi
  374. .RE
  375. .PP
  376. To set build flags in a shell script or shell fragment, "eval" can be
  377. used to interpret the output and to export the flags in the environment:
  378. .PP
  379. .RS 4
  380. .nf
  381. eval "$(dpkg\-buildflags \-\-export=sh)" && make
  382. .fi
  383. .RE
  384. .PP
  385. or to set the positional parameters to pass to a command:
  386. .PP
  387. .RS 4
  388. .nf
  389. eval "set \-\- $(dpkg\-buildflags \-\-export=cmdline)"
  390. for dir in a b c; do (cd $dir && ./configure "$@" && make); done
  391. .fi
  392. .RE
  393. .
  394. .SS Usage in debian/rules
  395. You should call \fBdpkg\-buildflags\fP or include \fBbuildflags.mk\fP
  396. from the \fBdebian/rules\fP file to obtain the needed build flags to
  397. pass to the build system.
  398. Note that older versions of \fBdpkg\-buildpackage\fP (before dpkg 1.16.1)
  399. exported these flags automatically. However, you should not rely on this,
  400. since this breaks manual invocation of \fBdebian/rules\fP.
  401. .PP
  402. For packages with autoconf-like build systems, you can pass the relevant
  403. options to configure or \fBmake\fP(1) directly, as shown above.
  404. .PP
  405. For other build systems, or when you need more fine-grained control
  406. about which flags are passed where, you can use \fB\-\-get\fP. Or you
  407. can include \fBbuildflags.mk\fP instead, which takes care of calling
  408. \fBdpkg\-buildflags\fP and storing the build flags in make variables.
  409. .PP
  410. If you want to export all buildflags into the environment (where they
  411. can be picked up by your build system):
  412. .PP
  413. .RS 4
  414. .nf
  415. DPKG_EXPORT_BUILDFLAGS = 1
  416. include /usr/share/dpkg/buildflags.mk
  417. .fi
  418. .RE
  419. .PP
  420. For some extra control over what is exported, you can manually export
  421. the variables (as none are exported by default):
  422. .PP
  423. .RS 4
  424. .nf
  425. include /usr/share/dpkg/buildflags.mk
  426. export CPPFLAGS CFLAGS LDFLAGS
  427. .fi
  428. .RE
  429. .PP
  430. And you can of course pass the flags to commands manually:
  431. .PP
  432. .RS 4
  433. .nf
  434. include /usr/share/dpkg/buildflags.mk
  435. build\-arch:
  436. \& $(CC) \-o hello hello.c $(CPPFLAGS) $(CFLAGS) $(LDFLAGS)
  437. .fi
  438. .RE