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