The check for whether an extended symbol index table was required dropped the first SHN_LORESERVE sections from the sections array before checking whether the remaining sections had symbols. Unfortunately, the null section header is not present in this list, so the check was skipping the first section that might be important. If that section contained a symbol, and no subsequent ones did, the .symtab_shndx section would not be emitted, leading to a corrupt object.
Also consolidate and expand test coverage in the area to cover this bug and other aspects of the SYMTAB_SHNDX section.
Depends on D97660.