[llvm] [llvm-objcopy][ELF] Add an option to remove notes (PR #118739)

Igor Kudrin via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 20 23:05:16 PST 2024


igorkudrin wrote:

> I'm concerned that this approach seems to only work with notes in segments, when the option seems to apply reasonably to ET_REL objects too, where the notes are in sections only. Indeed, more generally llvm-objcopy is designed to work primarily with sections, with direct interactions with segments being very limited. I think it would more naturally fit with llvm-objcopy's design to work with note sections first. You could swap the section contents is the same way as other replace section operations work, so that much of the existing writing behaviour works unchanged.

We can't rely on the existing code to update `SHT_NOTE` sections that reside in `PT_NOTE` segments. Currently, when the contents of such a section are replaced, the new data is written over the current segment contents, and if the section is shortened, the gap retains the old data. For `PT_NOTE` segments, this means that either some notes are preserved or duplicated (if the gap starts at the bound of a note), or the note entries in the segment starting from that point are corrupted. Thus, the offsets and sizes of such sections and the size of such segments should be adjusted.

However, the existing code is suitable for sections that are not associated with segments. I've updated the patch, so sections in relocatable objects are also supported now.

> Clearly, something special would need to be done for PT_NOTE segments, especially those without sections. One option might be to create an artificial section that covers the whole segment (or any parts of the segment not covered by an existing section at least) so that the section-based approach outlined above would still work, then have special handling to adjust the file/memsz fields after the sections have been updated.

This would either expose these temporary sections in the output file, or require some non-trivial changes to the code. And yet this does not solve the issue of gaps in `PT_NOTE` segments.

> Somewhat related to the previous point, I'm concerned with how `updateSegmentData` might interact with `--remove-section` for sections in segments. The current approach of the latter is that removed section data is overwritten with null bytes, preserving the segment size. I understand that for a PT_NOTE this may not be quite so desirable, but I think we need to do something to prevent mixing the two behaviours, because what the end result should be is non-obvious. One way would be to emit an error if a note section/segment is touched by both --remove-section and --remove-note.

I suspect that there may also be some problems with using the new command together with `--add-section` and `--update-section`, as the correct order of execution is not clear. I think the simplest solution is just to reject such combinations of options, so I've updated the patch accordingly.

https://github.com/llvm/llvm-project/pull/118739


More information about the llvm-commits mailing list