This is an archive of the discontinued LLVM Phabricator instance.

[NFC][XCOFF] Use common function to calculate file offset
ClosedPublic

Authored by Jake-Egan on Jul 13 2023, 6:38 AM.

Details

Summary

The file offset code is repeated in nearly identical form for every derivation of SectionEntry, so make it into a method in SectionEntry instead.

Diff Detail

Event Timeline

Jake-Egan created this revision.Jul 13 2023, 6:38 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 13 2023, 6:38 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
Jake-Egan requested review of this revision.Jul 13 2023, 6:38 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 13 2023, 6:38 AM
Jake-Egan edited the summary of this revision. (Show Details)Jul 13 2023, 6:41 AM
scott.linder added inline comments.Jul 13 2023, 3:03 PM
llvm/lib/MC/XCOFFObjectWriter.cpp
125

Generally best to pass primitive scalars like this by value when they are read-only

125

As there are already virtual methods I don't think the cost of making this virtual is too high, and it is what I would expect. I'd be surprised if a call to SectionEntry::advanceFileOffset was just wrong if the object were a DwarfSectionEntry.

Alternatively this method could make a call to a virtual method which returns the "RawSize" of the section, which appears to be different for DwarfSectionEntry (i.e. for every other section it is Size, for DwarfSectionEntry it is MemorySize)

193

I'm not sure why DwarfSectionEntry doesn't have a reset override which also clears MemorySize?

Jake-Egan updated this revision to Diff 540250.Jul 13 2023, 7:10 PM

Update patch

Jake-Egan marked 2 inline comments as done.Jul 13 2023, 7:13 PM
stephenpeckham added inline comments.Jul 14 2023, 9:15 AM
llvm/lib/MC/XCOFFObjectWriter.cpp
125

Would it make more sense for this function to return the new RawPointer instead of using reference to the variable?

201

I think this special treatment of Dwarf sections is unnecessary, but changing it probably needs a separate patch. Loadable sections (e.g., .text, .data) may need to be aligned. Other sections generally don't need any alignment, but if they're aligned, the RawPointer should be adjusted before writing the section. Then a dwarf-specific function wouldn't be needed. Also when compiling in 64-bit mode, the assert can never be triggered, because MaxRawDataSize is UINT64_MAX.

scott.linder accepted this revision.Jul 17 2023, 11:34 AM

LGTM, although I'm also fine with a more "functional" approach where RawPointer is returned rather than mutated via reference! I'll leave that between you and @stephenpeckham

Also, if you want to add a TODO for the cleanup @stephenpeckham suggests feel free to do so when landing! I'm at least happy that the function is now virtual, rather than just aliased in a subclass.

This revision is now accepted and ready to land.Jul 17 2023, 11:34 AM
Jake-Egan updated this revision to Diff 541311.Jul 17 2023, 9:12 PM

-Return the new RawPointer
-Added a TODO

Jake-Egan marked 2 inline comments as done.Jul 17 2023, 9:13 PM