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