Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 1 | package main |
| 2 | |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 3 | import ( |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 4 | "strconv" |
| 5 | ) |
| 6 | |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 7 | type config struct { |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 8 | // Whether to use ccache. |
| 9 | useCCache bool |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 10 | // Flags to add to gcc and clang. |
| 11 | commonFlags []string |
| 12 | // Flags to add to gcc only. |
| 13 | gccFlags []string |
| 14 | // Flags to add to clang only. |
| 15 | clangFlags []string |
| 16 | // Toolchain root path relative to the wrapper binary. |
| 17 | rootRelPath string |
| 18 | // Path of the old wrapper using the toolchain root. |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 19 | oldWrapperPath string |
| 20 | // Whether to mock out the calls that the old wrapper does. |
| 21 | mockOldWrapperCmds bool |
| 22 | // Whether to overwrite the config in the old wrapper. |
| 23 | overwriteOldWrapperCfg bool |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 24 | } |
| 25 | |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 26 | // UseCCache can be set via a linker flag. |
| 27 | // Value will be passed to strconv.ParseBool. |
| 28 | // E.g. go build -ldflags '-X config.UseCCache=true'. |
| 29 | var UseCCache = "unknown" |
| 30 | |
| 31 | // ConfigName can be set via a linker flag. |
| 32 | // Value has to be one of: |
| 33 | // - "cros.hardened" |
| 34 | // - "cros.nonhardened" |
| 35 | var ConfigName = "unknown" |
| 36 | |
| 37 | // Returns the configuration matching the UseCCache and ConfigName. |
| 38 | func getRealConfig() (*config, error) { |
| 39 | useCCache, err := strconv.ParseBool(UseCCache) |
| 40 | if err != nil { |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 41 | return nil, wrapErrorwithSourceLocf(err, "invalid format for UseCCache") |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 42 | } |
| 43 | config, err := getConfig(useCCache, ConfigName) |
| 44 | if err != nil { |
| 45 | return nil, err |
| 46 | } |
| 47 | return config, nil |
| 48 | } |
| 49 | |
| 50 | func getConfig(useCCache bool, configName string) (*config, error) { |
| 51 | switch configName { |
| 52 | case "cros.hardened": |
| 53 | return getCrosHardenedConfig(useCCache), nil |
| 54 | case "cros.nonhardened": |
| 55 | return getCrosNonHardenedConfig(useCCache), nil |
| 56 | default: |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 57 | return nil, newErrorwithSourceLocf("unknown config name: %s", configName) |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 61 | // Full hardening. |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 62 | func getCrosHardenedConfig(useCCache bool) *config { |
| 63 | // Temporarily disable function splitting because of chromium:434751. |
| 64 | return &config{ |
| 65 | useCCache: useCCache, |
| 66 | rootRelPath: "../../../../..", |
| 67 | oldWrapperPath: "./sysroot_wrapper.hardened.old", |
| 68 | commonFlags: []string{ |
| 69 | "-fPIE", |
| 70 | "-D_FORTIFY_SOURCE=2", |
| 71 | "-fstack-protector-strong", |
| 72 | "-pie", |
| 73 | "-fno-omit-frame-pointer", |
| 74 | }, |
| 75 | gccFlags: []string{ |
| 76 | "-Wno-unused-local-typedefs", |
| 77 | "-Wno-maybe-uninitialized", |
| 78 | "-fno-reorder-blocks-and-partition", |
| 79 | }, |
| 80 | // Temporarily disable tautological-*-compare chromium:778316. |
| 81 | // Temporarily add no-unknown-warning-option to deal with old clang versions. |
| 82 | // Temporarily disable Wsection since kernel gets a bunch of these. chromium:778867 |
| 83 | // Disable "-faddrsig" since it produces object files that strip doesn't understand, chromium:915742. |
| 84 | clangFlags: []string{ |
| 85 | "-Wno-tautological-unsigned-enum-zero-compare", |
| 86 | "-Qunused-arguments", |
| 87 | "-grecord-gcc-switches", |
| 88 | "-Wno-section", |
| 89 | "-Wno-unknown-warning-option", |
| 90 | "-fno-addrsig", |
| 91 | "-Wno-tautological-constant-compare", |
| 92 | }, |
| 93 | } |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | // Flags to be added to non-hardened toolchain. |
Tobias Bosch | aa31116 | 2019-06-20 17:47:19 -0700 | [diff] [blame] | 97 | func getCrosNonHardenedConfig(useCCache bool) *config { |
| 98 | return &config{ |
| 99 | useCCache: useCCache, |
| 100 | rootRelPath: "../../../../..", |
| 101 | oldWrapperPath: "./sysroot_wrapper.old", |
| 102 | commonFlags: []string{}, |
| 103 | gccFlags: []string{ |
| 104 | "-Wno-unused-local-typedefs", |
| 105 | "-Wno-maybe-uninitialized", |
| 106 | "-Wtrampolines", |
| 107 | "-Wno-deprecated-declarations", |
| 108 | }, |
| 109 | // Temporarily disable tautological-*-compare chromium:778316. |
| 110 | // Temporarily add no-unknown-warning-option to deal with old clang versions. |
| 111 | // Temporarily disable Wsection since kernel gets a bunch of these. chromium:778867 |
| 112 | clangFlags: []string{ |
| 113 | "-Wno-unknown-warning-option", |
| 114 | "-Qunused-arguments", |
| 115 | "-Wno-section", |
| 116 | "-Wno-tautological-unsigned-enum-zero-compare", |
| 117 | "-Wno-tautological-constant-compare", |
| 118 | }, |
| 119 | } |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 120 | } |