This patch is bringing the section name encoding of llvm-objcopy to the same level as what's done in llvm/lib/MC/WinCOFFObjectWriter.cpp, the base64 encoding code is taken from there.
The section name encoding for llvm-objcopy had two main issues, the first is that the size used for the snprintf in the original code is incorrect because snprintf adds a null byte, so this code was only able to encode offsets of 6 digits, /, \0 and 6 digits of the offset, rather than the 7 digits it should support.
And the second part is that it didn't support the base64 encoding for offsets larger than 7 digits.
This issue specifically showed up when using the clang-offload-bundler with a binary containing a lot of symbols/sections, since it uses llvm-objcopy to add the sections containing the offload code.
This patch may need some modifications and/or follow up.
It would be good to de-duplicate some of this code, at least the base64 encoding code, there's a base64 encoding function in support however it encodes bytes, rather than re-interpret a number into base64, but I'm not sure where it would go best.
And I'm also a little unsure on how to approach testing for this, yaml2obj, however it is also missing base64 encoding which should definitely be added, but even then the yaml file ends up very large to be able to test all the different encodings. The MC counterpart of this uses llvm-mc and assembler macros to generate the very large string tables, would it be okay to do the same in llvm-objcopy testing?
In terms of avoiding duplicated code, I think you should, in a separate prerequisite patch, move the duplicate code from WinCOFFObjectWriter.cpp to somewhere like the BinaryFormat or Object library. It's not immediately clear to me which is more appropriate though.