This is an archive of the discontinued LLVM Phabricator instance.

[llvm-objcopy][ELF] Add OriginalType & OriginalFlags
ClosedPublic

Authored by MaskRay on Nov 1 2019, 2:03 PM.

Details

Summary

llvm::objcopy::elf::*Section::classof matches Type and Flags, yet Type
and Flags are mutable (setSectionFlagsAndTypes and upcoming
--only-keep-debug feature). Add OriginalType & OriginalFlags to be used
in classof, to prevent classof results from changing.

Diff Detail

Event Timeline

MaskRay created this revision.Nov 1 2019, 2:03 PM
MaskRay updated this revision to Diff 227536.Nov 1 2019, 2:16 PM

Delete a redundant assignment

For some section types, we call their classof (via isa or dyn_cast) before mutation, so using either OriginalType or Type does not matter, e.g. DynamicSymbolTableSection, GroupSection. Some classof methods are unused, e.g. DynamicSection::classof.

This seems like a good idea to me. It makes our classof results immutable after all, leading to a more robust program.

One thought I had: did you consider adding just a new "Kind" enum instead, with the various kinds of sections recorded? That would add one field to the section class rather than two. I don't know if the Original* flags really have any other usage, and it would allow us to add further types later on which are not identified by flag/type whilst simplifying the classof functions to a basic comparison.

This seems like a good idea to me. It makes our classof results immutable after all, leading to a more robust program.

One thought I had: did you consider adding just a new "Kind" enum instead, with the various kinds of sections recorded? That would add one field to the section class rather than two. I don't know if the Original* flags really have any other usage, and it would allow us to add further types later on which are not identified by flag/type whilst simplifying the classof functions to a basic comparison.

We are tackling an expression problem. classof member functions are indeed the only place that OriginalType and OriginalFlag are read. If we introduce SectionBase::Kind as a new member variable, we can avoid OriginalType and OriginalFlag, but the classof logic will have to be centralized in one place, because we need to initialize Kind when a section is created.

This revision is now accepted and ready to land.Nov 4 2019, 11:31 AM
jakehehrlich accepted this revision.Nov 4 2019, 11:37 AM

Nice. This will unblock --only-keep-debug. Thanks Fangrui!

This revision was automatically updated to reflect the committed changes.