Peter Korsgaard | 85dc57f | 2011-04-29 13:09:26 +0200 | [diff] [blame] | 1 | /** |
Arnout Vandecappelle | f6ae243 | 2015-10-04 13:28:41 +0100 | [diff] [blame] | 2 | * Buildroot wrapper for toolchains. This simply executes the real toolchain |
| 3 | * with a number of arguments (sysroot/arch/..) hardcoded, to ensure the |
| 4 | * toolchain uses the correct configuration. |
Arnout Vandecappelle | befb9a3 | 2012-07-15 01:12:05 +0000 | [diff] [blame] | 5 | * The hardcoded path arguments are defined relative to the actual location |
| 6 | * of the binary. |
Peter Korsgaard | 85dc57f | 2011-04-29 13:09:26 +0200 | [diff] [blame] | 7 | * |
| 8 | * (C) 2011 Peter Korsgaard <jacmet@sunsite.dk> |
Daniel Nyström | e8c46b1 | 2011-06-21 21:54:27 +0200 | [diff] [blame] | 9 | * (C) 2011 Daniel Nyström <daniel.nystrom@timeterminal.se> |
Arnout Vandecappelle | befb9a3 | 2012-07-15 01:12:05 +0000 | [diff] [blame] | 10 | * (C) 2012 Arnout Vandecappelle (Essensium/Mind) <arnout@mind.be> |
Spenser Gilliland | aa86b52 | 2013-07-19 18:31:58 -0500 | [diff] [blame] | 11 | * (C) 2013 Spenser Gilliland <spenser@gillilanding.com> |
Peter Korsgaard | 85dc57f | 2011-04-29 13:09:26 +0200 | [diff] [blame] | 12 | * |
| 13 | * This file is licensed under the terms of the GNU General Public License |
| 14 | * version 2. This program is licensed "as is" without any warranty of any |
| 15 | * kind, whether express or implied. |
| 16 | */ |
| 17 | |
Thomas Petazzoni | 5aa205e | 2014-12-10 23:53:46 +0100 | [diff] [blame] | 18 | #define _GNU_SOURCE |
Peter Korsgaard | 85dc57f | 2011-04-29 13:09:26 +0200 | [diff] [blame] | 19 | #include <stdio.h> |
| 20 | #include <string.h> |
| 21 | #include <limits.h> |
| 22 | #include <unistd.h> |
Daniel Nyström | e8c46b1 | 2011-06-21 21:54:27 +0200 | [diff] [blame] | 23 | #include <stdlib.h> |
Thomas Petazzoni | 5aa205e | 2014-12-10 23:53:46 +0100 | [diff] [blame] | 24 | #include <errno.h> |
Yann E. MORIN | 6393b69 | 2017-10-21 22:31:02 +0200 | [diff] [blame] | 25 | #include <time.h> |
| 26 | #include <stdbool.h> |
Peter Korsgaard | 85dc57f | 2011-04-29 13:09:26 +0200 | [diff] [blame] | 27 | |
Arnout Vandecappelle | d82f69c | 2015-10-04 16:23:56 +0100 | [diff] [blame] | 28 | #ifdef BR_CCACHE |
| 29 | static char ccache_path[PATH_MAX]; |
| 30 | #endif |
Arnout Vandecappelle | befb9a3 | 2012-07-15 01:12:05 +0000 | [diff] [blame] | 31 | static char path[PATH_MAX]; |
| 32 | static char sysroot[PATH_MAX]; |
Yann E. MORIN | 6393b69 | 2017-10-21 22:31:02 +0200 | [diff] [blame] | 33 | /* As would be defined by gcc: |
| 34 | * https://gcc.gnu.org/onlinedocs/cpp/Standard-Predefined-Macros.html |
| 35 | * sizeof() on string literals includes the terminating \0. */ |
| 36 | static char _time_[sizeof("-D__TIME__=\"HH:MM:SS\"")]; |
| 37 | static char _date_[sizeof("-D__DATE__=\"MMM DD YYYY\"")]; |
Peter Korsgaard | 85dc57f | 2011-04-29 13:09:26 +0200 | [diff] [blame] | 38 | |
Spenser Gilliland | aa86b52 | 2013-07-19 18:31:58 -0500 | [diff] [blame] | 39 | /** |
| 40 | * GCC errors out with certain combinations of arguments (examples are |
Baruch Siach | 5c76842 | 2014-04-10 08:46:25 +0300 | [diff] [blame] | 41 | * -mfloat-abi={hard|soft} and -m{little|big}-endian), so we have to ensure |
Spenser Gilliland | aa86b52 | 2013-07-19 18:31:58 -0500 | [diff] [blame] | 42 | * that we only pass the predefined one to the real compiler if the inverse |
| 43 | * option isn't in the argument list. |
| 44 | * This specifies the worst case number of extra arguments we might pass |
Yann E. MORIN | 6393b69 | 2017-10-21 22:31:02 +0200 | [diff] [blame] | 45 | * Currently, we may have: |
Yann E. MORIN | 2c1dc32 | 2014-01-07 23:46:05 +0100 | [diff] [blame] | 46 | * -mfloat-abi= |
| 47 | * -march= |
Yann E. MORIN | 2c1dc32 | 2014-01-07 23:46:05 +0100 | [diff] [blame] | 48 | * -mcpu= |
Yann E. MORIN | 6393b69 | 2017-10-21 22:31:02 +0200 | [diff] [blame] | 49 | * -D__TIME__= |
| 50 | * -D__DATE__= |
| 51 | * -Wno-builtin-macro-redefined |
Matt Weber | 7484c1c | 2018-09-17 16:21:49 -0500 | [diff] [blame] | 52 | * -Wl,-z,now |
| 53 | * -Wl,-z,relro |
| 54 | * -fPIE |
| 55 | * -pie |
Spenser Gilliland | aa86b52 | 2013-07-19 18:31:58 -0500 | [diff] [blame] | 56 | */ |
Matt Weber | 7484c1c | 2018-09-17 16:21:49 -0500 | [diff] [blame] | 57 | #define EXCLUSIVE_ARGS 10 |
Spenser Gilliland | aa86b52 | 2013-07-19 18:31:58 -0500 | [diff] [blame] | 58 | |
Daniel Nyström | e8c46b1 | 2011-06-21 21:54:27 +0200 | [diff] [blame] | 59 | static char *predef_args[] = { |
Arnout Vandecappelle | d82f69c | 2015-10-04 16:23:56 +0100 | [diff] [blame] | 60 | #ifdef BR_CCACHE |
| 61 | ccache_path, |
| 62 | #endif |
Peter Korsgaard | 85dc57f | 2011-04-29 13:09:26 +0200 | [diff] [blame] | 63 | path, |
Arnout Vandecappelle | befb9a3 | 2012-07-15 01:12:05 +0000 | [diff] [blame] | 64 | "--sysroot", sysroot, |
Peter Korsgaard | 85dc57f | 2011-04-29 13:09:26 +0200 | [diff] [blame] | 65 | #ifdef BR_ABI |
| 66 | "-mabi=" BR_ABI, |
| 67 | #endif |
Vicente Olivert Riera | 2d8f3fc | 2017-06-28 16:17:10 +0100 | [diff] [blame] | 68 | #ifdef BR_NAN |
| 69 | "-mnan=" BR_NAN, |
| 70 | #endif |
Spenser Gilliland | 27c9370 | 2013-07-19 18:31:57 -0500 | [diff] [blame] | 71 | #ifdef BR_FPU |
Thomas Petazzoni | d774551 | 2013-07-16 10:03:12 +0200 | [diff] [blame] | 72 | "-mfpu=" BR_FPU, |
| 73 | #endif |
Peter Korsgaard | 85dc57f | 2011-04-29 13:09:26 +0200 | [diff] [blame] | 74 | #ifdef BR_SOFTFLOAT |
| 75 | "-msoft-float", |
| 76 | #endif /* BR_SOFTFLOAT */ |
Thomas Petazzoni | 85d0769 | 2013-07-16 10:03:22 +0200 | [diff] [blame] | 77 | #ifdef BR_MODE |
| 78 | "-m" BR_MODE, |
| 79 | #endif |
Arnout Vandecappelle (Essensium/Mind) | a22dc0f | 2012-03-13 23:30:00 +0100 | [diff] [blame] | 80 | #ifdef BR_64 |
| 81 | "-m64", |
| 82 | #endif |
Ray Kinsella | 968f5d5 | 2015-10-19 11:02:52 +0000 | [diff] [blame] | 83 | #ifdef BR_OMIT_LOCK_PREFIX |
| 84 | "-Wa,-momit-lock-prefix=yes", |
| 85 | #endif |
Vicente Olivert Riera | 240564a | 2016-11-09 16:16:57 +0000 | [diff] [blame] | 86 | #ifdef BR_NO_FUSED_MADD |
| 87 | "-mno-fused-madd", |
| 88 | #endif |
Waldemar Brodkorb | 5e58509 | 2018-03-31 19:54:20 +0200 | [diff] [blame] | 89 | #ifdef BR_FP_CONTRACT_OFF |
| 90 | "-ffp-contract=off", |
| 91 | #endif |
Sonic Zhang | 5713382 | 2013-05-03 00:39:34 +0000 | [diff] [blame] | 92 | #ifdef BR_BINFMT_FLAT |
| 93 | "-Wl,-elf2flt", |
| 94 | #endif |
Markos Chandras | f3a2b80 | 2013-10-14 10:52:25 +0100 | [diff] [blame] | 95 | #ifdef BR_MIPS_TARGET_LITTLE_ENDIAN |
| 96 | "-EL", |
| 97 | #endif |
Alexey Brodkin | 2b93fe5 | 2015-03-10 14:50:24 +0300 | [diff] [blame] | 98 | #if defined(BR_MIPS_TARGET_BIG_ENDIAN) || defined(BR_ARC_TARGET_BIG_ENDIAN) |
Markos Chandras | f3a2b80 | 2013-10-14 10:52:25 +0100 | [diff] [blame] | 99 | "-EB", |
| 100 | #endif |
Thomas Petazzoni | b95e436 | 2011-12-31 12:09:33 +0100 | [diff] [blame] | 101 | #ifdef BR_ADDITIONAL_CFLAGS |
| 102 | BR_ADDITIONAL_CFLAGS |
| 103 | #endif |
Peter Korsgaard | 85dc57f | 2011-04-29 13:09:26 +0200 | [diff] [blame] | 104 | }; |
| 105 | |
Yann E. MORIN | 31c093e | 2016-12-04 10:21:55 +0100 | [diff] [blame] | 106 | /* A {string,length} tuple, to avoid computing strlen() on constants. |
| 107 | * - str must be a \0-terminated string |
| 108 | * - len does not account for the terminating '\0' |
| 109 | */ |
| 110 | struct str_len_s { |
| 111 | const char *str; |
Yann E. MORIN | 61cb120 | 2016-08-29 17:53:59 +0200 | [diff] [blame] | 112 | size_t len; |
| 113 | }; |
| 114 | |
Yann E. MORIN | 31c093e | 2016-12-04 10:21:55 +0100 | [diff] [blame] | 115 | /* Define a {string,length} tuple. Takes an unquoted constant string as |
| 116 | * parameter. sizeof() on a string literal includes the terminating \0, |
| 117 | * but we don't want to count it. |
| 118 | */ |
| 119 | #define STR_LEN(s) { #s, sizeof(#s)-1 } |
| 120 | |
Yann E. MORIN | 3eccf76 | 2016-12-04 10:21:56 +0100 | [diff] [blame] | 121 | /* List of paths considered unsafe for cross-compilation. |
| 122 | * |
| 123 | * An unsafe path is one that points to a directory with libraries or |
| 124 | * headers for the build machine, which are not suitable for the target. |
| 125 | */ |
| 126 | static const struct str_len_s unsafe_paths[] = { |
| 127 | STR_LEN(/lib), |
| 128 | STR_LEN(/usr/include), |
| 129 | STR_LEN(/usr/lib), |
| 130 | STR_LEN(/usr/local/include), |
| 131 | STR_LEN(/usr/local/lib), |
| 132 | { NULL, 0 }, |
| 133 | }; |
| 134 | |
Yann E. MORIN | 61cb120 | 2016-08-29 17:53:59 +0200 | [diff] [blame] | 135 | /* Unsafe options are options that specify a potentialy unsafe path, |
| 136 | * that will be checked by check_unsafe_path(), below. |
Yann E. MORIN | 61cb120 | 2016-08-29 17:53:59 +0200 | [diff] [blame] | 137 | */ |
Yann E. MORIN | 31c093e | 2016-12-04 10:21:55 +0100 | [diff] [blame] | 138 | static const struct str_len_s unsafe_opts[] = { |
| 139 | STR_LEN(-I), |
| 140 | STR_LEN(-idirafter), |
| 141 | STR_LEN(-iquote), |
| 142 | STR_LEN(-isystem), |
| 143 | STR_LEN(-L), |
Yann E. MORIN | 61cb120 | 2016-08-29 17:53:59 +0200 | [diff] [blame] | 144 | { NULL, 0 }, |
| 145 | }; |
| 146 | |
Yann E. MORIN | 105a8c1 | 2016-08-29 17:53:58 +0200 | [diff] [blame] | 147 | /* Check if path is unsafe for cross-compilation. Unsafe paths are those |
| 148 | * pointing to the standard native include or library paths. |
| 149 | * |
| 150 | * We print the arguments leading to the failure. For some options, gcc |
| 151 | * accepts the path to be concatenated to the argument (e.g. -I/foo/bar) |
| 152 | * or separated (e.g. -I /foo/bar). In the first case, we need only print |
| 153 | * the argument as it already contains the path (arg_has_path), while in |
| 154 | * the second case we need to print both (!arg_has_path). |
| 155 | * |
| 156 | * If paranoid, exit in error instead of just printing a warning. |
| 157 | */ |
| 158 | static void check_unsafe_path(const char *arg, |
| 159 | const char *path, |
| 160 | int paranoid, |
| 161 | int arg_has_path) |
Thomas Petazzoni | 5aa205e | 2014-12-10 23:53:46 +0100 | [diff] [blame] | 162 | { |
Yann E. MORIN | 3eccf76 | 2016-12-04 10:21:56 +0100 | [diff] [blame] | 163 | const struct str_len_s *p; |
Thomas Petazzoni | 5aa205e | 2014-12-10 23:53:46 +0100 | [diff] [blame] | 164 | |
Yann E. MORIN | 3eccf76 | 2016-12-04 10:21:56 +0100 | [diff] [blame] | 165 | for (p=unsafe_paths; p->str; p++) { |
| 166 | if (strncmp(path, p->str, p->len)) |
Thomas Petazzoni | 5aa205e | 2014-12-10 23:53:46 +0100 | [diff] [blame] | 167 | continue; |
Yann E. MORIN | 105a8c1 | 2016-08-29 17:53:58 +0200 | [diff] [blame] | 168 | fprintf(stderr, |
| 169 | "%s: %s: unsafe header/library path used in cross-compilation: '%s%s%s'\n", |
| 170 | program_invocation_short_name, |
| 171 | paranoid ? "ERROR" : "WARNING", |
| 172 | arg, |
| 173 | arg_has_path ? "" : "' '", /* close single-quote, space, open single-quote */ |
| 174 | arg_has_path ? "" : path); /* so that arg and path are properly quoted. */ |
| 175 | if (paranoid) |
| 176 | exit(1); |
Thomas Petazzoni | 5aa205e | 2014-12-10 23:53:46 +0100 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
Yann E. MORIN | 6393b69 | 2017-10-21 22:31:02 +0200 | [diff] [blame] | 180 | /* Returns false if SOURCE_DATE_EPOCH was not defined in the environment. |
| 181 | * |
| 182 | * Returns true if SOURCE_DATE_EPOCH is in the environment and represent |
| 183 | * a valid timestamp, in which case the timestamp is formatted into the |
| 184 | * global variables _date_ and _time_. |
| 185 | * |
| 186 | * Aborts if SOURCE_DATE_EPOCH was set in the environment but did not |
| 187 | * contain a valid timestamp. |
| 188 | * |
| 189 | * Valid values are defined in the spec: |
| 190 | * https://reproducible-builds.org/specs/source-date-epoch/ |
| 191 | * but we further restrict them to be positive or null. |
| 192 | */ |
| 193 | bool parse_source_date_epoch_from_env(void) |
| 194 | { |
| 195 | char *epoch_env, *endptr; |
| 196 | time_t epoch; |
| 197 | struct tm epoch_tm; |
| 198 | |
| 199 | if ((epoch_env = getenv("SOURCE_DATE_EPOCH")) == NULL) |
| 200 | return false; |
| 201 | errno = 0; |
| 202 | epoch = (time_t) strtoll(epoch_env, &endptr, 10); |
| 203 | /* We just need to test if it is incorrect, but we do not |
| 204 | * care why it is incorrect. |
| 205 | */ |
| 206 | if ((errno != 0) || !*epoch_env || *endptr || (epoch < 0)) { |
| 207 | fprintf(stderr, "%s: invalid SOURCE_DATE_EPOCH='%s'\n", |
| 208 | program_invocation_short_name, |
| 209 | epoch_env); |
| 210 | exit(1); |
| 211 | } |
| 212 | tzset(); /* For localtime_r(), below. */ |
| 213 | if (localtime_r(&epoch, &epoch_tm) == NULL) { |
| 214 | fprintf(stderr, "%s: cannot parse SOURCE_DATE_EPOCH=%s\n", |
| 215 | program_invocation_short_name, |
| 216 | getenv("SOURCE_DATE_EPOCH")); |
| 217 | exit(1); |
| 218 | } |
| 219 | if (!strftime(_time_, sizeof(_time_), "-D__TIME__=\"%T\"", &epoch_tm)) { |
| 220 | fprintf(stderr, "%s: cannot set time from SOURCE_DATE_EPOCH=%s\n", |
| 221 | program_invocation_short_name, |
| 222 | getenv("SOURCE_DATE_EPOCH")); |
| 223 | exit(1); |
| 224 | } |
| 225 | if (!strftime(_date_, sizeof(_date_), "-D__DATE__=\"%b %e %Y\"", &epoch_tm)) { |
| 226 | fprintf(stderr, "%s: cannot set date from SOURCE_DATE_EPOCH=%s\n", |
| 227 | program_invocation_short_name, |
| 228 | getenv("SOURCE_DATE_EPOCH")); |
| 229 | exit(1); |
| 230 | } |
| 231 | return true; |
| 232 | } |
| 233 | |
Peter Korsgaard | 85dc57f | 2011-04-29 13:09:26 +0200 | [diff] [blame] | 234 | int main(int argc, char **argv) |
| 235 | { |
Arnout Vandecappelle | 792f127 | 2015-10-04 13:28:56 +0100 | [diff] [blame] | 236 | char **args, **cur, **exec_args; |
Arnout Vandecappelle | befb9a3 | 2012-07-15 01:12:05 +0000 | [diff] [blame] | 237 | char *relbasedir, *absbasedir; |
| 238 | char *progpath = argv[0]; |
| 239 | char *basename; |
Yann E. MORIN | 60cb290 | 2013-09-21 00:00:30 +0200 | [diff] [blame] | 240 | char *env_debug; |
Thomas Petazzoni | 5aa205e | 2014-12-10 23:53:46 +0100 | [diff] [blame] | 241 | char *paranoid_wrapper; |
| 242 | int paranoid; |
Matt Weber | 7484c1c | 2018-09-17 16:21:49 -0500 | [diff] [blame] | 243 | int ret, i, count = 0, debug, found_shared = 0; |
Arnout Vandecappelle | befb9a3 | 2012-07-15 01:12:05 +0000 | [diff] [blame] | 244 | |
| 245 | /* Calculate the relative paths */ |
| 246 | basename = strrchr(progpath, '/'); |
| 247 | if (basename) { |
| 248 | *basename = '\0'; |
| 249 | basename++; |
| 250 | relbasedir = malloc(strlen(progpath) + 7); |
| 251 | if (relbasedir == NULL) { |
| 252 | perror(__FILE__ ": malloc"); |
| 253 | return 2; |
| 254 | } |
Arnout Vandecappelle | 14151d7 | 2017-07-04 16:03:53 +0200 | [diff] [blame] | 255 | sprintf(relbasedir, "%s/..", argv[0]); |
Arnout Vandecappelle | befb9a3 | 2012-07-15 01:12:05 +0000 | [diff] [blame] | 256 | absbasedir = realpath(relbasedir, NULL); |
| 257 | } else { |
| 258 | basename = progpath; |
Patrick Ziegler | 74ae7af | 2013-05-28 23:41:19 +0000 | [diff] [blame] | 259 | absbasedir = malloc(PATH_MAX + 1); |
| 260 | ret = readlink("/proc/self/exe", absbasedir, PATH_MAX); |
| 261 | if (ret < 0) { |
| 262 | perror(__FILE__ ": readlink"); |
| 263 | return 2; |
| 264 | } |
| 265 | absbasedir[ret] = '\0'; |
| 266 | for (i = ret; i > 0; i--) { |
| 267 | if (absbasedir[i] == '/') { |
| 268 | absbasedir[i] = '\0'; |
Arnout Vandecappelle | 015d68c | 2017-07-07 09:43:30 +0200 | [diff] [blame] | 269 | if (++count == 2) |
Patrick Ziegler | 74ae7af | 2013-05-28 23:41:19 +0000 | [diff] [blame] | 270 | break; |
| 271 | } |
| 272 | } |
Arnout Vandecappelle | befb9a3 | 2012-07-15 01:12:05 +0000 | [diff] [blame] | 273 | } |
| 274 | if (absbasedir == NULL) { |
| 275 | perror(__FILE__ ": realpath"); |
| 276 | return 2; |
| 277 | } |
| 278 | |
| 279 | /* Fill in the relative paths */ |
| 280 | #ifdef BR_CROSS_PATH_REL |
Arnout Vandecappelle | 5ce73dc | 2015-10-14 23:05:55 +0200 | [diff] [blame] | 281 | ret = snprintf(path, sizeof(path), "%s/" BR_CROSS_PATH_REL "/%s" BR_CROSS_PATH_SUFFIX, absbasedir, basename); |
Peter Korsgaard | ccdb179 | 2015-10-05 08:25:17 +0200 | [diff] [blame] | 282 | #elif defined(BR_CROSS_PATH_ABS) |
Arnout Vandecappelle | 5ce73dc | 2015-10-14 23:05:55 +0200 | [diff] [blame] | 283 | ret = snprintf(path, sizeof(path), BR_CROSS_PATH_ABS "/%s" BR_CROSS_PATH_SUFFIX, basename); |
| 284 | #else |
Arnout Vandecappelle | 2a47bd3 | 2017-07-10 01:21:18 +0200 | [diff] [blame] | 285 | ret = snprintf(path, sizeof(path), "%s/bin/%s" BR_CROSS_PATH_SUFFIX, absbasedir, basename); |
Arnout Vandecappelle | befb9a3 | 2012-07-15 01:12:05 +0000 | [diff] [blame] | 286 | #endif |
| 287 | if (ret >= sizeof(path)) { |
| 288 | perror(__FILE__ ": overflow"); |
| 289 | return 3; |
| 290 | } |
Arnout Vandecappelle | d82f69c | 2015-10-04 16:23:56 +0100 | [diff] [blame] | 291 | #ifdef BR_CCACHE |
Arnout Vandecappelle | 2a47bd3 | 2017-07-10 01:21:18 +0200 | [diff] [blame] | 292 | ret = snprintf(ccache_path, sizeof(ccache_path), "%s/bin/ccache", absbasedir); |
Arnout Vandecappelle | d82f69c | 2015-10-04 16:23:56 +0100 | [diff] [blame] | 293 | if (ret >= sizeof(ccache_path)) { |
| 294 | perror(__FILE__ ": overflow"); |
| 295 | return 3; |
| 296 | } |
| 297 | #endif |
Arnout Vandecappelle | befb9a3 | 2012-07-15 01:12:05 +0000 | [diff] [blame] | 298 | ret = snprintf(sysroot, sizeof(sysroot), "%s/" BR_SYSROOT, absbasedir); |
| 299 | if (ret >= sizeof(sysroot)) { |
| 300 | perror(__FILE__ ": overflow"); |
| 301 | return 3; |
| 302 | } |
Peter Korsgaard | 85dc57f | 2011-04-29 13:09:26 +0200 | [diff] [blame] | 303 | |
Spenser Gilliland | aa86b52 | 2013-07-19 18:31:58 -0500 | [diff] [blame] | 304 | cur = args = malloc(sizeof(predef_args) + |
| 305 | (sizeof(char *) * (argc + EXCLUSIVE_ARGS))); |
Daniel Nyström | e8c46b1 | 2011-06-21 21:54:27 +0200 | [diff] [blame] | 306 | if (args == NULL) { |
| 307 | perror(__FILE__ ": malloc"); |
| 308 | return 2; |
Peter Korsgaard | 85dc57f | 2011-04-29 13:09:26 +0200 | [diff] [blame] | 309 | } |
| 310 | |
Daniel Nyström | e8c46b1 | 2011-06-21 21:54:27 +0200 | [diff] [blame] | 311 | /* start with predefined args */ |
| 312 | memcpy(cur, predef_args, sizeof(predef_args)); |
| 313 | cur += sizeof(predef_args) / sizeof(predef_args[0]); |
| 314 | |
Spenser Gilliland | aa86b52 | 2013-07-19 18:31:58 -0500 | [diff] [blame] | 315 | #ifdef BR_FLOAT_ABI |
| 316 | /* add float abi if not overridden in args */ |
| 317 | for (i = 1; i < argc; i++) { |
| 318 | if (!strncmp(argv[i], "-mfloat-abi=", strlen("-mfloat-abi=")) || |
| 319 | !strcmp(argv[i], "-msoft-float") || |
| 320 | !strcmp(argv[i], "-mhard-float")) |
| 321 | break; |
| 322 | } |
| 323 | |
| 324 | if (i == argc) |
| 325 | *cur++ = "-mfloat-abi=" BR_FLOAT_ABI; |
| 326 | #endif |
| 327 | |
Vicente Olivert Riera | 9a0a0a9 | 2017-06-28 16:17:11 +0100 | [diff] [blame] | 328 | #ifdef BR_FP32_MODE |
| 329 | /* add fp32 mode if soft-float is not args or hard-float overrides soft-float */ |
| 330 | int add_fp32_mode = 1; |
| 331 | for (i = 1; i < argc; i++) { |
| 332 | if (!strcmp(argv[i], "-msoft-float")) |
| 333 | add_fp32_mode = 0; |
| 334 | else if (!strcmp(argv[i], "-mhard-float")) |
| 335 | add_fp32_mode = 1; |
| 336 | } |
| 337 | |
| 338 | if (add_fp32_mode == 1) |
| 339 | *cur++ = "-mfp" BR_FP32_MODE; |
| 340 | #endif |
| 341 | |
Yann E. MORIN | 2c1dc32 | 2014-01-07 23:46:05 +0100 | [diff] [blame] | 342 | #if defined(BR_ARCH) || \ |
Yann E. MORIN | 2c1dc32 | 2014-01-07 23:46:05 +0100 | [diff] [blame] | 343 | defined(BR_CPU) |
Thomas De Schampheleire | b62cb78 | 2015-07-26 12:53:07 +0200 | [diff] [blame] | 344 | /* Add our -march/cpu flags, but only if none of |
| 345 | * -march/mtune/mcpu are already specified on the commandline |
Yann E. MORIN | 2c1dc32 | 2014-01-07 23:46:05 +0100 | [diff] [blame] | 346 | */ |
| 347 | for (i = 1; i < argc; i++) { |
| 348 | if (!strncmp(argv[i], "-march=", strlen("-march=")) || |
Thomas De Schampheleire | b62cb78 | 2015-07-26 12:53:07 +0200 | [diff] [blame] | 349 | !strncmp(argv[i], "-mtune=", strlen("-mtune=")) || |
Yann E. MORIN | 2c1dc32 | 2014-01-07 23:46:05 +0100 | [diff] [blame] | 350 | !strncmp(argv[i], "-mcpu=", strlen("-mcpu=" ))) |
| 351 | break; |
| 352 | } |
| 353 | if (i == argc) { |
| 354 | #ifdef BR_ARCH |
| 355 | *cur++ = "-march=" BR_ARCH; |
| 356 | #endif |
Yann E. MORIN | 2c1dc32 | 2014-01-07 23:46:05 +0100 | [diff] [blame] | 357 | #ifdef BR_CPU |
| 358 | *cur++ = "-mcpu=" BR_CPU; |
| 359 | #endif |
| 360 | } |
Thomas Petazzoni | 5715d2d | 2014-10-21 22:27:16 +0200 | [diff] [blame] | 361 | #endif /* ARCH || CPU */ |
Yann E. MORIN | 2c1dc32 | 2014-01-07 23:46:05 +0100 | [diff] [blame] | 362 | |
Yann E. MORIN | 6393b69 | 2017-10-21 22:31:02 +0200 | [diff] [blame] | 363 | if (parse_source_date_epoch_from_env()) { |
| 364 | *cur++ = _time_; |
| 365 | *cur++ = _date_; |
| 366 | /* This has existed since gcc-4.4.0. */ |
| 367 | *cur++ = "-Wno-builtin-macro-redefined"; |
| 368 | } |
| 369 | |
Matt Weber | 7484c1c | 2018-09-17 16:21:49 -0500 | [diff] [blame] | 370 | #ifdef BR2_RELRO_FULL |
| 371 | /* Patterned after Fedora/Gentoo hardening approaches. |
| 372 | * https://fedoraproject.org/wiki/Changes/Harden_All_Packages |
| 373 | * https://wiki.gentoo.org/wiki/Hardened/Toolchain#Position_Independent_Executables_.28PIEs.29 |
| 374 | * |
| 375 | * A few checks are added to allow disabling of PIE |
| 376 | * 1) -fno-pie and -no-pie are used by other distros to disable PIE in |
| 377 | * cases where the compiler enables it by default. The logic below |
| 378 | * maintains that behavior. |
| 379 | * Ref: https://wiki.ubuntu.com/SecurityTeam/PIE |
| 380 | * 2) A check for -fno-PIE has been used in older Linux Kernel builds |
| 381 | * in a similar way to -fno-pie or -no-pie. |
| 382 | * 3) A check is added for Kernel and U-boot defines |
| 383 | * (-D__KERNEL__ and -D__UBOOT__). |
| 384 | */ |
| 385 | for (i = 1; i < argc; i++) { |
| 386 | /* Apply all incompatible link flag and disable checks first */ |
| 387 | if (!strcmp(argv[i], "-r") || |
| 388 | !strcmp(argv[i], "-Wl,-r") || |
| 389 | !strcmp(argv[i], "-static") || |
| 390 | !strcmp(argv[i], "-D__KERNEL__") || |
| 391 | !strcmp(argv[i], "-D__UBOOT__") || |
| 392 | !strcmp(argv[i], "-fno-pie") || |
| 393 | !strcmp(argv[i], "-fno-PIE") || |
| 394 | !strcmp(argv[i], "-no-pie")) |
| 395 | break; |
| 396 | /* Record that shared was present which disables -pie but don't |
| 397 | * break out of loop as a check needs to occur that possibly |
| 398 | * still allows -fPIE to be set |
| 399 | */ |
| 400 | if (!strcmp(argv[i], "-shared")) |
| 401 | found_shared = 1; |
| 402 | } |
| 403 | |
| 404 | if (i == argc) { |
| 405 | /* Compile and link condition checking have been kept split |
| 406 | * between these two loops, as there maybe already are valid |
| 407 | * compile flags set for position independence. In that case |
| 408 | * the wrapper just adds the -pie for link. |
| 409 | */ |
| 410 | for (i = 1; i < argc; i++) { |
| 411 | if (!strcmp(argv[i], "-fpie") || |
| 412 | !strcmp(argv[i], "-fPIE") || |
| 413 | !strcmp(argv[i], "-fpic") || |
| 414 | !strcmp(argv[i], "-fPIC")) |
| 415 | break; |
| 416 | } |
| 417 | /* Both args below can be set at compile/link time |
| 418 | * and are ignored correctly when not used |
| 419 | */ |
| 420 | if(i == argc) |
| 421 | *cur++ = "-fPIE"; |
| 422 | |
| 423 | if (!found_shared) |
| 424 | *cur++ = "-pie"; |
| 425 | } |
| 426 | #endif |
| 427 | /* Are we building the Linux Kernel or U-Boot? */ |
| 428 | for (i = 1; i < argc; i++) { |
| 429 | if (!strcmp(argv[i], "-D__KERNEL__") || |
| 430 | !strcmp(argv[i], "-D__UBOOT__")) |
| 431 | break; |
| 432 | } |
| 433 | if (i == argc) { |
| 434 | /* https://wiki.gentoo.org/wiki/Hardened/Toolchain#Mark_Read-Only_Appropriate_Sections */ |
| 435 | #ifdef BR2_RELRO_PARTIAL |
| 436 | *cur++ = "-Wl,-z,relro"; |
| 437 | #endif |
| 438 | #ifdef BR2_RELRO_FULL |
| 439 | *cur++ = "-Wl,-z,now"; |
| 440 | *cur++ = "-Wl,-z,relro"; |
| 441 | #endif |
| 442 | } |
| 443 | |
Thomas Petazzoni | 5aa205e | 2014-12-10 23:53:46 +0100 | [diff] [blame] | 444 | paranoid_wrapper = getenv("BR_COMPILER_PARANOID_UNSAFE_PATH"); |
| 445 | if (paranoid_wrapper && strlen(paranoid_wrapper) > 0) |
| 446 | paranoid = 1; |
| 447 | else |
| 448 | paranoid = 0; |
| 449 | |
| 450 | /* Check for unsafe library and header paths */ |
| 451 | for (i = 1; i < argc; i++) { |
Yann E. MORIN | 31c093e | 2016-12-04 10:21:55 +0100 | [diff] [blame] | 452 | const struct str_len_s *opt; |
| 453 | for (opt=unsafe_opts; opt->str; opt++ ) { |
Yann E. MORIN | 61cb120 | 2016-08-29 17:53:59 +0200 | [diff] [blame] | 454 | /* Skip any non-unsafe option. */ |
Yann E. MORIN | 31c093e | 2016-12-04 10:21:55 +0100 | [diff] [blame] | 455 | if (strncmp(argv[i], opt->str, opt->len)) |
Thomas Petazzoni | 5aa205e | 2014-12-10 23:53:46 +0100 | [diff] [blame] | 456 | continue; |
Yann E. MORIN | 61cb120 | 2016-08-29 17:53:59 +0200 | [diff] [blame] | 457 | |
| 458 | /* Handle both cases: |
| 459 | * - path is a separate argument, |
| 460 | * - path is concatenated with option. |
| 461 | */ |
| 462 | if (argv[i][opt->len] == '\0') { |
| 463 | i++; |
| 464 | if (i == argc) |
| 465 | break; |
| 466 | check_unsafe_path(argv[i-1], argv[i], paranoid, 0); |
| 467 | } else |
| 468 | check_unsafe_path(argv[i], argv[i] + opt->len, paranoid, 1); |
Thomas Petazzoni | 5aa205e | 2014-12-10 23:53:46 +0100 | [diff] [blame] | 469 | } |
| 470 | } |
| 471 | |
Daniel Nyström | e8c46b1 | 2011-06-21 21:54:27 +0200 | [diff] [blame] | 472 | /* append forward args */ |
| 473 | memcpy(cur, &argv[1], sizeof(char *) * (argc - 1)); |
| 474 | cur += argc - 1; |
| 475 | |
| 476 | /* finish with NULL termination */ |
| 477 | *cur = NULL; |
Peter Korsgaard | 85dc57f | 2011-04-29 13:09:26 +0200 | [diff] [blame] | 478 | |
Arnout Vandecappelle | 792f127 | 2015-10-04 13:28:56 +0100 | [diff] [blame] | 479 | exec_args = args; |
| 480 | #ifdef BR_CCACHE |
| 481 | if (getenv("BR_NO_CCACHE")) |
| 482 | /* Skip the ccache call */ |
| 483 | exec_args++; |
| 484 | #endif |
| 485 | |
Yann E. MORIN | 60cb290 | 2013-09-21 00:00:30 +0200 | [diff] [blame] | 486 | /* Debug the wrapper to see actual arguments passed to |
| 487 | * the compiler: |
| 488 | * unset, empty, or 0: do not trace |
| 489 | * set to 1 : trace all arguments on a single line |
| 490 | * set to 2 : trace one argument per line |
| 491 | */ |
Yann E. MORIN | 35c666e | 2014-03-03 23:55:05 +0100 | [diff] [blame] | 492 | if ((env_debug = getenv("BR2_DEBUG_WRAPPER"))) { |
Yann E. MORIN | 60cb290 | 2013-09-21 00:00:30 +0200 | [diff] [blame] | 493 | debug = atoi(env_debug); |
| 494 | if (debug > 0) { |
| 495 | fprintf(stderr, "Toolchain wrapper executing:"); |
Arnout Vandecappelle | f4682cf | 2015-10-04 16:25:00 +0100 | [diff] [blame] | 496 | #ifdef BR_CCACHE_HASH |
| 497 | fprintf(stderr, "%sCCACHE_COMPILERCHECK='string:" BR_CCACHE_HASH "'", |
| 498 | (debug == 2) ? "\n " : " "); |
| 499 | #endif |
Arnout Vandecappelle | 1e97b27 | 2015-10-04 16:25:32 +0100 | [diff] [blame] | 500 | #ifdef BR_CCACHE_BASEDIR |
| 501 | fprintf(stderr, "%sCCACHE_BASEDIR='" BR_CCACHE_BASEDIR "'", |
| 502 | (debug == 2) ? "\n " : " "); |
| 503 | #endif |
Arnout Vandecappelle | 792f127 | 2015-10-04 13:28:56 +0100 | [diff] [blame] | 504 | for (i = 0; exec_args[i]; i++) |
Yann E. MORIN | 60cb290 | 2013-09-21 00:00:30 +0200 | [diff] [blame] | 505 | fprintf(stderr, "%s'%s'", |
Arnout Vandecappelle | 792f127 | 2015-10-04 13:28:56 +0100 | [diff] [blame] | 506 | (debug == 2) ? "\n " : " ", exec_args[i]); |
Yann E. MORIN | 60cb290 | 2013-09-21 00:00:30 +0200 | [diff] [blame] | 507 | fprintf(stderr, "\n"); |
| 508 | } |
Yann E. MORIN | aaa06aa | 2013-07-18 23:54:50 +0200 | [diff] [blame] | 509 | } |
| 510 | |
Arnout Vandecappelle | f4682cf | 2015-10-04 16:25:00 +0100 | [diff] [blame] | 511 | #ifdef BR_CCACHE_HASH |
| 512 | /* Allow compilercheck to be overridden through the environment */ |
| 513 | if (setenv("CCACHE_COMPILERCHECK", "string:" BR_CCACHE_HASH, 0)) { |
| 514 | perror(__FILE__ ": Failed to set CCACHE_COMPILERCHECK"); |
| 515 | return 3; |
| 516 | } |
| 517 | #endif |
Arnout Vandecappelle | 1e97b27 | 2015-10-04 16:25:32 +0100 | [diff] [blame] | 518 | #ifdef BR_CCACHE_BASEDIR |
| 519 | /* Allow compilercheck to be overridden through the environment */ |
| 520 | if (setenv("CCACHE_BASEDIR", BR_CCACHE_BASEDIR, 0)) { |
| 521 | perror(__FILE__ ": Failed to set CCACHE_BASEDIR"); |
| 522 | return 3; |
| 523 | } |
| 524 | #endif |
Arnout Vandecappelle | f4682cf | 2015-10-04 16:25:00 +0100 | [diff] [blame] | 525 | |
Arnout Vandecappelle | 792f127 | 2015-10-04 13:28:56 +0100 | [diff] [blame] | 526 | if (execv(exec_args[0], exec_args)) |
Peter Korsgaard | 85dc57f | 2011-04-29 13:09:26 +0200 | [diff] [blame] | 527 | perror(path); |
| 528 | |
Daniel Nyström | e8c46b1 | 2011-06-21 21:54:27 +0200 | [diff] [blame] | 529 | free(args); |
| 530 | |
Peter Korsgaard | 85dc57f | 2011-04-29 13:09:26 +0200 | [diff] [blame] | 531 | return 2; |
| 532 | } |