llvm-objcopy previously knew nothing about data in segments that wasn't covered by section headers, meaning that it wrote zeroes instead of what was there. As it is possible for this data to be useful to the loader, this patch causes llvm-objcopy to start preserving this data. Data in sections that are explicitly removed continues to be written as zeroes.
This fixes https://bugs.llvm.org/show_bug.cgi?id=41005.
The test is possibly more complicated than the current implementation warrants, but did pick up several problems with an earlier implementation, so I'd prefer to keep the test cases rather than reduce it down.
You could probably make this shorter just this in a loop, e.g.