Understanding derivation override scope

In order to monitor the memory consumption during the compilation of a program, I want to make sure that every part of a binary have been compiled with the same compiler. Here in the example gnu-hello with gcc9. Using objdump -s --section .comment <binary> to verify the compiled program as gcc adds it’s version to this section. I expect to find only gcc9 in the output

Approach 1: Explicitly override default stdenv

Inspired by this wiki article

# shell-selective-override.nix
# Usage: nix-shell shell-selective-override.nix
with import (fetchTarball channel:nixos-24.05) {
  config = {
    replaceStdenv = ({ pkgs }: pkgs.gcc9Stdenv);
  };
};
let
  myglibc = glibc.override{ stdenv = pkgs.gcc9Stdenv; };
  mybinutils = binutils-unwrapped.override{ stdenv = gcc9Stdenv; }; 
  mygcc = gcc9.cc.override{ stdenv = gcc9Stdenv; }; #AKA gcc-unwrapped
  mycc = wrapCCWith {
    cc = mygcc;
    bintools = wrapBintoolsWith {
      bintools = mybinutils;
      libc = myglibc;
    };
  };
  mystdenv = overrideCC gcc9Stdenv mycc; 
  myhello = hello.override{ stdenv = mystdenv;};
in
mkShell {
  packages = [mycc myhello];
  shellHook = ''
objdump -s --section .comment `which hello`
'';
}

Approach 2: Global override

Also adapted from the wiki

# shell-override-all.nix
# Usage: nix-shell shell-override-all.nix
with import (fetchTarball channel:nixos-24.05) {
  config = {
    replaceStdenv = ({ pkgs }: pkgs.gcc9Stdenv);
  };
};
mkShell {
  packages = [gcc9 hello];
    shellHook = ''
objdump -s --section .comment `which hello`
'';
}

In both cases, I got that output:

Contents of section .comment:
 0000 4743433a 2028474e 55292031 332e322e  GCC: (GNU) 13.2.
 0010 30004743 433a2028 474e5529 20392e35  0.GCC: (GNU) 9.5
 0020 2e3000                               .0.      

which show that hello is a compound of gcc13 (stdenv default) and gcc9 (override) binaries. After some digging, i found that the gcc9 part from gcc’s crtbegin.o and gcc13 part comes from glibc’s crt1.o, which should have been overwritten.

How is that glibc keeps getting compiled by stdenv default compiler? Is gcc still using the default stdenv glibc when compiling gnu-hello? Am i missing something else?
Thanks for your time

FYI you’re using the wrong wiki (the official one is wiki.nixos.org) - though that probably doesn’t matter since both wikis are full of nonsense.

Anyway, replaceStdenv is somehow broken, so you’d better not use it. But even after removing it from option 1, the output does not change; I don’t think you’ll find an easy answer here.