On Thu, Sep 26, 2024 at 11:20:15PM +0200, Enrico via Gcc wrote: > I am trying to understand how 'flag_pic' works. > It is used extensively in TARGET_OPTION_OVERRIDE functions in the form 'if > (flag_pic) ... '. > The flags fPic and fpic have a default value of -1, so as far as I > understand, if the two flags are not set in the command line, all 'if > (flag_pic)' will be true because of the default value -1 (since I can see > that flag_pic is a define to global_options.x_flag_pic) > > It doesn't look correct to me, but this test is used so many times that I > am sure I am missing something.
Yes, you are missing gcc/opts.cc (finish_options) if (!opts->x_flag_opts_finished) { /* We initialize opts->x_flag_pie to -1 so that targets can set a default value. */ if (opts->x_flag_pie == -1) { /* We initialize opts->x_flag_pic to -1 so that we can tell if -fpic, -fPIC, -fno-pic or -fno-PIC is used. */ if (opts->x_flag_pic == -1) opts->x_flag_pie = (opts->x_flag_hardened ? /*-fPIE*/ 2 : DEFAULT_FLAG_PIE); else opts->x_flag_pie = 0; } /* If -fPIE or -fpie is used, turn on PIC. */ if (opts->x_flag_pie) opts->x_flag_pic = opts->x_flag_pie; else if (opts->x_flag_pic == -1) opts->x_flag_pic = 0; if (opts->x_flag_pic && !opts->x_flag_pie) opts->x_flag_shlib = 1; opts->x_flag_opts_finished = true; } The -1 value just means the state of this option hasn't been finalized yet. Jakub