This is an archive of the discontinued LLVM Phabricator instance.

[yaml2obj] - Allow overriding sh_offset field from the YAML.
ClosedPublic

Authored by grimar on Jun 27 2019, 7:04 AM.

Details

Summary

Some of our test cases are using objects which
has sections with a broken sh_offset field.

There was no way to set it from YAML until this patch.

Diff Detail

Repository
rL LLVM

Event Timeline

grimar created this revision.Jun 27 2019, 7:04 AM

I'm not sure the correct behaviour for an explicit offset is clear-cut. I'd think that it causes the section contents to be written at the specified offset, not that it just writes an arbitrary value in sh_offset. I've had times when I want to put my sections at a specific offset, but could only do this with tricks to do with alignment or padding sections. I'd expect an Offset field would resolve this issue (note that this is what happens for ProgramHeaders already).

Unfortunately, I think this change conflicts with that desire, and I don't see any easy way of reconciling the differences.

lib/ObjectYAML/ELFYAML.cpp
915 ↗(On Diff #206858)

This assert seems a bit out-of-place?

916 ↗(On Diff #206858)

I'd call this "Offset" not "ShOffset", to better match the other fields.

I'm not sure the correct behaviour for an explicit offset is clear-cut. I'd think that it causes the section contents to be written at the specified offset, not that it just writes an arbitrary value in sh_offset. I've had times when I want to put my sections at a specific offset, but could only do this with tricks to do with alignment or padding sections. I'd expect an Offset field would resolve this issue (note that this is what happens for ProgramHeaders already).

Yes, I had the same concerns. But I think it is fine generally: at any time we still can introduce a new field that might have a behavior you describe.
Its name can be SecOffset. See: currently all YAML fields names we have do not match the ELF fields name directly. E.g. we have Name but it doesn't match sh_name.
In this patch I named the field ShOffset to match the sh_offset. We can have an agreement that if a field matches the ELF field name then its intention os to
override the field's value. It is in line with D63771 btw.

Yes, I had the same concerns. But I think it is fine generally: at any time we still can introduce a new field that might have a behavior you describe.
Its name can be SecOffset. See: currently all YAML fields names we have do not match the ELF fields name directly. E.g. we have Name but it doesn't match sh_name.
In this patch I named the field ShOffset to match the sh_offset. We can have an agreement that if a field matches the ELF field name then its intention os to
override the field's value. It is in line with D63771 btw.

Okay, I can buy that, but this needs clear comments in the code, stating that this does NOT place the section data at the specified offset.

test/tools/yaml2obj/elf-override-shoffset.yaml
1 ↗(On Diff #206858)

I wonder if it would be worth somehow showing in a test case that this field doesn't place section data at the specified offset? What do you think? Relatedly, having an offset outside the file and showing that the file size doesn't increase would probably be worthwhile.

grimar updated this revision to Diff 207025.Jun 28 2019, 3:30 AM
grimar marked 5 inline comments as done.
  • Addressed review comments.
lib/ObjectYAML/ELFYAML.cpp
915 ↗(On Diff #206858)

Yes, it was a bit incomplete. Fixed.

916 ↗(On Diff #206858)

So seems we agreed "ShOffset" makes sence, I leaved it as is.

MaskRay added inline comments.Jun 28 2019, 4:07 AM
lib/ObjectYAML/ELFYAML.cpp
916 ↗(On Diff #206858)

Since the fields are already spelled this way: Flags.. I'll not bother asking you to change that. ShOffset seems fine. It is better than Offset I think.

(I'd prefer unmangled elf.h names: sh_flags, sh_offset, etc)

jhenderson added inline comments.Jun 28 2019, 4:33 AM
lib/ObjectYAML/ELFYAML.cpp
915 ↗(On Diff #207025)

I'm still not clear why this assert exists specifically for ShOffset, and not for the other fields. It needs commenting or some sort of message needs adding to the assert explaining it.

test/tools/yaml2obj/elf-override-shoffset.yaml
76 ↗(On Diff #207025)

You don't need this rm here. The first od line will replace its contents.

grimar updated this revision to Diff 207049.Jun 28 2019, 6:04 AM
grimar marked 2 inline comments as done.
  • Addressed review comments.
jhenderson accepted this revision.Jul 1 2019, 2:39 AM

LGTM, with the comment fix.

lib/ObjectYAML/ELFYAML.cpp
916 ↗(On Diff #207049)

"when we are producing",
yam2obj -> yaml2obj

This revision is now accepted and ready to land.Jul 1 2019, 2:39 AM
MaskRay accepted this revision.Jul 1 2019, 4:22 AM
This revision was automatically updated to reflect the committed changes.
Herald added a project: Restricted Project. · View Herald TranscriptJul 2 2019, 3:21 AM