+ require Dpkg::Arch;
+ my ($abi, $os, $cpu) = Dpkg::Arch::debarch_to_debtriplet($arch);
+
+ # Disable unsupported hardening options.
+ if ($cpu =~ /^(?:ia64|alpha|mips|mipsel|hppa)$/ or $arch eq 'arm') {
+ $harden_stack = 0;
+ }
+ if ($cpu =~ /^(?:ia64|hppa|avr32)$/) {
+ $harden_relro = 0;
+ $harden_bindnow = 0;
+ }
+ }
+
+ # Default values.
+ my @cflags = @def_cflags;
+ my @cxxflags = @def_cxxflags;
+ my @cppflags = @def_cppflags;
+ my @ldflags = @def_ldflags;
+ # Check the specified hardening options, same order as dpkg-buildflags.
+ if ($harden_pie) {
+ @cflags = (@cflags, @def_cflags_pie);
+ @cxxflags = (@cxxflags, @def_cflags_pie);
+ @ldflags = (@ldflags, @def_ldflags_pie);
+ }
+ if ($harden_stack) {
+ @cflags = (@cflags, @def_cflags_stack);
+ @cxxflags = (@cxxflags, @def_cflags_stack);
+ }
+ if ($harden_fortify) {
+ @cflags = (@cflags, @def_cflags_fortify);
+ @cxxflags = (@cxxflags, @def_cflags_fortify);
+ @cppflags = (@cppflags, @def_cppflags_fortify);
+ }
+ if ($harden_format) {
+ @cflags = (@cflags, @def_cflags_format);
+ @cxxflags = (@cxxflags, @def_cflags_format);
+ }
+ if ($harden_relro) {
+ @ldflags = (@ldflags, @def_ldflags_relro);
+ }
+ if ($harden_bindnow) {
+ @ldflags = (@ldflags, @def_ldflags_bindnow);
+ }
+
+ # Ignore flags for this arch if requested.
+ if ($arch and exists $option_ignore_arch_flag{$arch}) {
+ my @flag_refs = (\@cflags, \@cxxflags, \@cppflags, \@ldflags);
+
+ remove_flags(\@flag_refs,
+ \%flag_renames,
+ @{$option_ignore_arch_flag{$arch}});
+ }
+
+ my @ignore_line = @option_ignore_line;
+ # Ignore lines for this arch if requested.
+ if ($arch and exists $option_ignore_arch_line{$arch}) {
+ @ignore_line = (@ignore_line, @{$option_ignore_arch_line{$arch}});
+ }
+
+LINE:
+ for (my $i = 0; $i < scalar @input; $i++) {
+ my $line = $input[$i];
+
+ # Ignore line if requested.
+ foreach my $ignore (@ignore_line) {
+ next LINE if $line =~ /$ignore/;
+ }
+
+ my $skip = 0;
+ if (is_non_verbose_build($line, $input[$i + 1], \$skip)) {
+ if (not $option_buildd) {
+ error_non_verbose_build($line);
+ } else {
+ $statistics{commands_nonverbose}++;
+ }
+ $exit |= $exit_code{non_verbose_build};
+ next;
+ }
+ # Even if it's a verbose build, we might have to skip this line.
+ next if $skip;
+
+ # Remove everything until and including the compiler command. Makes
+ # checks easier and faster.
+ $line =~ s/^.*?$cc_regex//o;
+ # "([...] test.c)" is not detected as 'test.c' - fix this by removing
+ # the brace and similar characters.
+ $line =~ s/['")]+$//;
+
+ # Skip unnecessary tests when only preprocessing.
+ my $flag_preprocess = 0;
+
+ my $dependency = 0;
+ my $preprocess = 0;
+ my $compile = 0;
+ my $link = 0;
+
+ # Preprocess, compile, assemble.
+ if ($line =~ /\s(-E|-S|-c)\b/) {
+ $preprocess = 1;
+ $flag_preprocess = 1 if $1 eq '-E';
+ $compile = 1 if $1 eq '-S' or $1 eq '-c';
+ # Dependency generation for Makefiles. The other flags (-MF -MG -MP
+ # -MT -MQ) are always used with -M/-MM.
+ } elsif ($line =~ /\s(?:-M|-MM)\b/) {
+ $dependency = 1;
+ # Otherwise assume we are linking.
+ } else {
+ $link = 1;
+ }
+
+ # -MD/-MMD also cause dependency generation, but they don't imply -E!
+ if ($line =~ /\s(?:-MD|-MMD)\b/) {
+ $dependency = 0;
+ $flag_preprocess = 0;
+ }
+
+ # Dependency generation for Makefiles, no preprocessing or other flags
+ # needed.
+ next if $dependency;
+
+ # Get all file extensions on this line.
+ my @extensions = $line =~ /$file_extension_regex/go;
+ # Ignore all unknown extensions to speedup the search below.
+ @extensions = grep { exists $extension{$_} } @extensions;
+
+ # These file types don't require preprocessing.
+ if (extension_found(\%extensions_no_preprocess, @extensions)) {
+ $preprocess = 0;
+ }
+ # These file types require preprocessing.
+ if (extension_found(\%extensions_preprocess, @extensions)) {
+ $preprocess = 1;
+ }
+
+ # If there are source files then it's compiling/linking in one step
+ # and we must check both. We only check for source files here, because
+ # header files cause too many false positives.
+ if (not $flag_preprocess
+ and extension_found(\%extensions_compile_link, @extensions)) {
+ # Assembly files don't need CFLAGS.
+ if (not extension_found(\%extensions_compile, @extensions)
+ and extension_found(\%extensions_no_compile, @extensions)) {
+ $compile = 0;
+ # But the rest does.
+ } else {
+ $compile = 1;
+ }
+ }
+
+ # Assume CXXFLAGS are required when a C++ file is specified in the
+ # compiler line.
+ my $compile_cpp = 0;
+ if ($compile
+ and extension_found(\%extensions_compile_cpp, @extensions)) {
+ $compile = 0;
+ $compile_cpp = 1;
+ }
+
+ if ($option_buildd) {
+ $statistics{preprocess}++ if $preprocess;
+ $statistics{compile}++ if $compile;
+ $statistics{compile_cpp}++ if $compile_cpp;
+ $statistics{link}++ if $link;
+ }
+
+ # Check hardening flags.
+ my @missing;
+ if ($compile and not all_flags_used($line, \@missing, @cflags)
+ # Libraries linked with -fPIC don't have to (and can't) be
+ # linked with -fPIE as well. It's no error if only PIE flags
+ # are missing.
+ and not pic_pie_conflict($line, $harden_pie, \@missing, @def_cflags_pie)
+ # Assume dpkg-buildflags returns the correct flags.
+ and index($line, '`dpkg-buildflags --get CFLAGS`') == -1) {
+ if (not $option_buildd) {
+ error_flags('CFLAGS missing', \@missing, \%flag_renames, $input[$i]);
+ } else {
+ $statistics{compile_missing}++;
+ }
+ $exit |= $exit_code{flags_missing};
+ } elsif ($compile_cpp and not all_flags_used($line, \@missing, @cflags)
+ # Libraries linked with -fPIC don't have to (and can't) be
+ # linked with -fPIE as well. It's no error if only PIE flags
+ # are missing.
+ and not pic_pie_conflict($line, $harden_pie, \@missing, @def_cflags_pie)
+ # Assume dpkg-buildflags returns the correct flags.
+ and index($line, '`dpkg-buildflags --get CXXFLAGS`') == -1) {
+ if (not $option_buildd) {
+ error_flags('CXXFLAGS missing', \@missing, \%flag_renames, $input[$i]);
+ } else {
+ $statistics{compile_cpp_missing}++;
+ }
+ $exit |= $exit_code{flags_missing};
+ }
+ if ($preprocess
+ and (not all_flags_used($line, \@missing, @cppflags)
+ # The fortify flag might be overwritten, detect that.
+ or ($harden_fortify
+ and cppflags_fortify_broken($line, \@missing)))
+ # Assume dpkg-buildflags returns the correct flags.
+ and index($line, '`dpkg-buildflags --get CPPFLAGS`') == -1) {
+ if (not $option_buildd) {
+ error_flags('CPPFLAGS missing', \@missing, \%flag_renames, $input[$i]);
+ } else {
+ $statistics{preprocess_missing}++;
+ }
+ $exit |= $exit_code{flags_missing};
+ }
+ if ($link and not all_flags_used($line, \@missing, @ldflags)
+ # Same here, -fPIC conflicts with -fPIE.
+ and not pic_pie_conflict($line, $harden_pie, \@missing, @def_ldflags_pie)
+ # Assume dpkg-buildflags returns the correct flags.
+ and index($line, '`dpkg-buildflags --get LDFLAGS`') == -1) {
+ if (not $option_buildd) {
+ error_flags('LDFLAGS missing', \@missing, \%flag_renames, $input[$i]);
+ } else {
+ $statistics{link_missing}++;
+ }
+ $exit |= $exit_code{flags_missing};
+ }
+ }