This is an archive of the discontinued LLVM Phabricator instance.

ObjectFileMachO: Prioritize the TEXT segment as the mach header segment, regardless of the order the segments appear in the file
ClosedPublic

Authored by jasonmolenda on May 9 2023, 7:05 PM.

Details

Summary

ObjectFileMachO::GetMachHeaderSection() is used to find the vmaddr of the mach-o header of a binary. It first scans all of the Mach-O segments looking for one at file offset 0. Then it finds no segments at file offset 0 (e.g. binaries in a shared cache), it looks for a segment called __TEXT.

I have an unusual situation where I need to work with a binary where it is laid out in virtual memory the traditional order, TEXT, DATA, LINKEDIT. But the Mach-O binary is ordered DATA, LINKEDIT, TEXT. I would like to argue that while technically allowed, no one does this, but there is no part of the Mach-O standard that forbids such a binary, and lldb needs to roll with it.

I'm moving the search for a segment called "TEXT" to be the first thing we try, and failing that, look for a segment with file offset 0. I don't have any such binary I can include as a test case publicly, so I can't add a test for this specific case right now.

Another possible approach would be to sort the segments by vmaddr order and take the one with the lowest vmaddr as being the mach-o header. It would be a valid approach too, but I haven't stress tested this idea and am reluctant to try something novel when the existing shared cache style search works correctly if we prioritize it ahead of fileoff==0 search.

rdar://109128418

Diff Detail

Event Timeline

jasonmolenda created this revision.May 9 2023, 7:05 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 9 2023, 7:05 PM
jasonmolenda requested review of this revision.May 9 2023, 7:05 PM
jingham accepted this revision.May 10 2023, 3:09 PM

LGTM for support of something that really should hurt a little more than you are making it...

This revision is now accepted and ready to land.May 10 2023, 3:09 PM