In order to allow some sections to exist either in split debug-info or in the main binary,
don't replace non-empty sections with empty sections.
This looks like a perfect case for lldb-test. You will run into a couple of problems which will prevent this from working out of the box. I tried to fix those in https://reviews.llvm.org/D42955, but that turned into a major refactor. Until that CL lands, we should be able to make a quick hack in lldb-test which will make this testable:
The thing you will need to modify is to explicitly call module->GetSymbolVendor() before dumping out the sections (so that the symbol vendor populates these), and avoid setting the SymbolFileSpec on the ModuleSpec (so that the symbol vendor will find the external symbol file.
Ok, as of r326805 it should be very easy to write a test for this. You can look at the test in that commit for inspiration.
Btw, do you happen to know why we have two copies of the section merging code? While working on that patch I noticed an issue that is caused (in part*) by the fact that we merge the sections three times. I've tried to remove the copy in ObjectFileELF, and everything seems to work fine, but our test support coverage for the debug-info-in-separate-file is not that awesome. If you have any other tests for this, I'd appreciate if you can run them before I check that in (I'll send you a patch soon).
(*) The problem is that we are replacing the sections by their IDs, but (ELF?) section IDs are unique only if they all come from a single object file. So when we start replacing them the second time round, we end up overwriting random sections. When we replace the sections only once, the code will be technically correct, but the replacement-by-id idea seems still a bit sketchy.