While experimenting with some test cases for D67761 earlier today I found that I could crash the linker when I discarded all the InputSections, but not the .ARM.exidx sections that had a dependency on them. This was ostensibly down to the OutputSection code using a non-existing InputSection to obtain a OutputSection for the OutputSection link order dependency. Attempts to clumsily fix this resulted in other crashes as the executableSections and exidxSections weren't in synch.
The fix makes sure that we only add to the exidxSections when the section it depends on would be added to the executableSections. This allows us to remove the empty flag and then implement a more sophisticated needed() so that we discard the .ARM.exidx section if it wouldn't be valid to write it.
This is independent of the fix for D67761.
probably also works. finalizeContents() has logic to filter dead InputSections, so I'm thinking whether we should simplify the logic here.