Tobias Bosch | cfa8c24 | 2019-07-19 03:29:40 -0700 | [diff] [blame] | 1 | // Copyright 2019 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 5 | package main |
| 6 | |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 7 | import ( |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 8 | "strconv" |
| 9 | ) |
| 10 | |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 11 | type config struct { |
Tobias Bosch | 31dec2c | 2019-07-18 07:34:03 -0700 | [diff] [blame^] | 12 | // TODO: Refactor this flag into more generic configuration properties. |
| 13 | isHostWrapper bool |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 14 | // Whether to use ccache. |
| 15 | useCCache bool |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 16 | // Flags to add to gcc and clang. |
| 17 | commonFlags []string |
| 18 | // Flags to add to gcc only. |
| 19 | gccFlags []string |
| 20 | // Flags to add to clang only. |
| 21 | clangFlags []string |
| 22 | // Toolchain root path relative to the wrapper binary. |
| 23 | rootRelPath string |
| 24 | // Path of the old wrapper using the toolchain root. |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 25 | oldWrapperPath string |
| 26 | // Whether to mock out the calls that the old wrapper does. |
| 27 | mockOldWrapperCmds bool |
Tobias Bosch | f6d9f4f | 2019-07-09 08:09:01 -0700 | [diff] [blame] | 28 | // Directory to store errors that were prevented with -Wno-error. |
| 29 | newWarningsDir string |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 30 | } |
| 31 | |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 32 | // UseCCache can be set via a linker flag. |
| 33 | // Value will be passed to strconv.ParseBool. |
| 34 | // E.g. go build -ldflags '-X config.UseCCache=true'. |
| 35 | var UseCCache = "unknown" |
| 36 | |
| 37 | // ConfigName can be set via a linker flag. |
| 38 | // Value has to be one of: |
| 39 | // - "cros.hardened" |
| 40 | // - "cros.nonhardened" |
| 41 | var ConfigName = "unknown" |
| 42 | |
| 43 | // Returns the configuration matching the UseCCache and ConfigName. |
| 44 | func getRealConfig() (*config, error) { |
| 45 | useCCache, err := strconv.ParseBool(UseCCache) |
| 46 | if err != nil { |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 47 | return nil, wrapErrorwithSourceLocf(err, "invalid format for UseCCache") |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 48 | } |
| 49 | config, err := getConfig(useCCache, ConfigName) |
| 50 | if err != nil { |
| 51 | return nil, err |
| 52 | } |
| 53 | return config, nil |
| 54 | } |
| 55 | |
| 56 | func getConfig(useCCache bool, configName string) (*config, error) { |
| 57 | switch configName { |
| 58 | case "cros.hardened": |
| 59 | return getCrosHardenedConfig(useCCache), nil |
| 60 | case "cros.nonhardened": |
| 61 | return getCrosNonHardenedConfig(useCCache), nil |
Tobias Bosch | 31dec2c | 2019-07-18 07:34:03 -0700 | [diff] [blame^] | 62 | case "cros.host": |
| 63 | return getCrosHostConfig(), nil |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 64 | default: |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 65 | return nil, newErrorwithSourceLocf("unknown config name: %s", configName) |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 69 | // Full hardening. |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 70 | func getCrosHardenedConfig(useCCache bool) *config { |
| 71 | // Temporarily disable function splitting because of chromium:434751. |
| 72 | return &config{ |
| 73 | useCCache: useCCache, |
| 74 | rootRelPath: "../../../../..", |
| 75 | oldWrapperPath: "./sysroot_wrapper.hardened.old", |
| 76 | commonFlags: []string{ |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 77 | "-fstack-protector-strong", |
Tobias Bosch | 198a3c9 | 2019-07-17 04:22:34 -0700 | [diff] [blame] | 78 | "-fPIE", |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 79 | "-pie", |
Tobias Bosch | 198a3c9 | 2019-07-17 04:22:34 -0700 | [diff] [blame] | 80 | "-D_FORTIFY_SOURCE=2", |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 81 | "-fno-omit-frame-pointer", |
| 82 | }, |
| 83 | gccFlags: []string{ |
Tobias Bosch | 198a3c9 | 2019-07-17 04:22:34 -0700 | [diff] [blame] | 84 | "-fno-reorder-blocks-and-partition", |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 85 | "-Wno-unused-local-typedefs", |
| 86 | "-Wno-maybe-uninitialized", |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 87 | }, |
| 88 | // Temporarily disable tautological-*-compare chromium:778316. |
| 89 | // Temporarily add no-unknown-warning-option to deal with old clang versions. |
| 90 | // Temporarily disable Wsection since kernel gets a bunch of these. chromium:778867 |
| 91 | // Disable "-faddrsig" since it produces object files that strip doesn't understand, chromium:915742. |
| 92 | clangFlags: []string{ |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 93 | "-Qunused-arguments", |
| 94 | "-grecord-gcc-switches", |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 95 | "-fno-addrsig", |
| 96 | "-Wno-tautological-constant-compare", |
Tobias Bosch | 198a3c9 | 2019-07-17 04:22:34 -0700 | [diff] [blame] | 97 | "-Wno-tautological-unsigned-enum-zero-compare", |
| 98 | "-Wno-unknown-warning-option", |
| 99 | "-Wno-section", |
Tobias Bosch | e23905c | 2019-07-19 02:56:31 -0700 | [diff] [blame] | 100 | "-static-libgcc", |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 101 | }, |
Tobias Bosch | f6d9f4f | 2019-07-09 08:09:01 -0700 | [diff] [blame] | 102 | newWarningsDir: "/tmp/fatal_clang_warnings", |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 103 | } |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | // Flags to be added to non-hardened toolchain. |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 107 | func getCrosNonHardenedConfig(useCCache bool) *config { |
| 108 | return &config{ |
| 109 | useCCache: useCCache, |
| 110 | rootRelPath: "../../../../..", |
| 111 | oldWrapperPath: "./sysroot_wrapper.old", |
| 112 | commonFlags: []string{}, |
| 113 | gccFlags: []string{ |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 114 | "-Wno-maybe-uninitialized", |
Tobias Bosch | 198a3c9 | 2019-07-17 04:22:34 -0700 | [diff] [blame] | 115 | "-Wno-unused-local-typedefs", |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 116 | "-Wno-deprecated-declarations", |
Tobias Bosch | 198a3c9 | 2019-07-17 04:22:34 -0700 | [diff] [blame] | 117 | "-Wtrampolines", |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 118 | }, |
| 119 | // Temporarily disable tautological-*-compare chromium:778316. |
| 120 | // Temporarily add no-unknown-warning-option to deal with old clang versions. |
| 121 | // Temporarily disable Wsection since kernel gets a bunch of these. chromium:778867 |
| 122 | clangFlags: []string{ |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 123 | "-Qunused-arguments", |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 124 | "-Wno-tautological-constant-compare", |
Tobias Bosch | 198a3c9 | 2019-07-17 04:22:34 -0700 | [diff] [blame] | 125 | "-Wno-tautological-unsigned-enum-zero-compare", |
| 126 | "-Wno-unknown-warning-option", |
| 127 | "-Wno-section", |
Tobias Bosch | e23905c | 2019-07-19 02:56:31 -0700 | [diff] [blame] | 128 | "-static-libgcc", |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 129 | }, |
Tobias Bosch | f6d9f4f | 2019-07-09 08:09:01 -0700 | [diff] [blame] | 130 | newWarningsDir: "/tmp/fatal_clang_warnings", |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 131 | } |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 132 | } |
Tobias Bosch | 31dec2c | 2019-07-18 07:34:03 -0700 | [diff] [blame^] | 133 | |
| 134 | // Flags to be added to host toolchain. |
| 135 | func getCrosHostConfig() *config { |
| 136 | return &config{ |
| 137 | isHostWrapper: true, |
| 138 | useCCache: false, |
| 139 | rootRelPath: "../..", |
| 140 | oldWrapperPath: "./host_wrapper.old", |
| 141 | commonFlags: []string{}, |
| 142 | gccFlags: []string{}, |
| 143 | // Temporarily disable tautological-*-compare chromium:778316. |
| 144 | // Temporarily add no-unknown-warning-option to deal with old clang versions. |
| 145 | clangFlags: []string{ |
| 146 | "-Qunused-arguments", |
| 147 | "-grecord-gcc-switches", |
| 148 | "-fno-addrsig", |
| 149 | "-Wno-unused-local-typedefs", |
| 150 | "-Wno-deprecated-declarations", |
| 151 | "-Wno-tautological-constant-compare", |
| 152 | "-Wno-tautological-unsigned-enum-zero-compare", |
| 153 | "-Wno-unknown-warning-option", |
| 154 | }, |
| 155 | newWarningsDir: "/tmp/fatal_clang_warnings", |
| 156 | } |
| 157 | } |