Skip to content

Instantly share code, notes, and snippets.

@nathanchance
Last active January 8, 2024 13:01
Show Gist options
  • Save nathanchance/965028fc180b1cced015ac7530b2eaeb to your computer and use it in GitHub Desktop.
Save nathanchance/965028fc180b1cced015ac7530b2eaeb to your computer and use it in GitHub Desktop.

Introduction

Google has traditionally been against upgrading the compiler as it comes with new warnings and potentially changes behavior. However, these are actually good reasons to upgrade, they help shake out undefined behavior.

Process

In order to compile with a newer version of GCC, you'll need to do a few modifications to your kernel in addition to a new toolchain. I recommend using the ones available from Bootlin.

  1. Remove gcc-wrapper and any instances of -Werror: gcc-wrapper is CAF's shitty way of enabling -Werror, which is unnecessary since regular -Werror will suffice. We need to remove it for the time being because there will be new warnings to fix. Pick Google's revert of it (3.18, 4.4) then remove any other instances of -Werror (such as in prima, qcacld-2.0, and qcacld-3.0 in the Kbuild files).

  2. Pick the commit that actually allows the device to boot: Previously, I had figured out that enabling -fno-store-merging would allow the device to boot (original commit); however, please do not pick that commit as Google has a proper fix so this option can remained enabled globally (as it is good for memory acces). The commit can be picked from here.

  3. Start fixing warnings: There really isn't much point to compiling with a newer version of GCC if you are not going to address the warnings it presents you with. Here are some very general stategies for dealing with certain warnings:

  • -Wmisleading-indentation: This means there is a statement that is indentation below a conditional statement that looks like it may need to be guarded by it. Example:

    if (foo())
      statement_1;
      
      statement_2;

    There are two possible solutions:

    if (foo()) {
      statement_1;
      statement_2;
    }

    or

    if (foo())
      statement_1;
      
    statement_2;

    It's going to be contextual what the correct answer is.

  • -Wduplicate-decl-specifier: This is often seen with the following type of code: const struct struct_type const variable_name. The solution is just to remove the extra const: const struct struct_type variable_name.

  • -Warray-bounds: There is a possible of you accessing an array outside its size. Example:

    int array[5];
    
    for (int i = 0; i < 6; i++)
      printf("%d", array[i]);

    We have the potential to access array[5], which isn't possible. This may be a false positive if there is a check for the array bounds above the actual access and the warning can be disabled.

  • -Wbool-compare: This usually means you are comparing a variable with type bool to an integer. The easiest solution is generally to change the bool variable to an int.

I pushed a series of fixes to Sony Open Devices' GitHub here if you need ideas for getting warnings fixed.

  1. Re-enable -Werror: I really don't recommend disabling -Werror normally as it can help you catch warnings from creeping into your code. Google has a nifty config option that can be disabled on the fly with the scripts/config script like so (assuming .config is in an out folder, adjust it if not):
./scripts/config -f out/.config -d CC_WERROR

Getting help

https://t.me/LinuxKernelNewbies

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment