This is an archive of the discontinued LLVM Phabricator instance.

[llvm-objcopy] Consider section flags when adding section
ClosedPublic

Authored by alfonsosanchezbeato on Jul 28 2021, 3:21 AM.

Details

Summary

The --set-section-flags option was being ignored when adding a new
section. Take it into account if present.

Fixes https://llvm.org/PR51244

Diff Detail

Event Timeline

alfonsosanchezbeato requested review of this revision.Jul 28 2021, 3:21 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 28 2021, 3:21 AM

Could you please add a test case to the existing llvm-objcopy tests (llvm/test/tools/llvm-objcopy) that shows this fix works.

llvm/tools/llvm-objcopy/COFF/COFFObjcopy.cpp
232

Rather than modify flagsToCharacteristics, could you just move this loop above the SetSectionFlags block above?

llvm/tools/llvm-objcopy/COFF/COFFObjcopy.cpp
232

You need to change the characteristics before adding the section so addSection() takes them into account and can set VirtualSize, VirtualAddress, etc. Otherwise the flags would be set, but those parts of the section header would not get the right values.

jhenderson added a subscriber: mstorsjo.

@mstorsjo, could you take a look, please? My COFF knowledge isn't really sufficient to see if there's a better way of doing things. My initial thought is whether a separate later pass should be the one that sets virtual addresses, rather than addSection, but it may not make sense.

@mstorsjo, could you take a look, please? My COFF knowledge isn't really sufficient to see if there's a better way of doing things. My initial thought is whether a separate later pass should be the one that sets virtual addresses, rather than addSection, but it may not make sense.

I think approach is pragmatically sensible here.

We don't recalculate and set virtual addresses for all sections, only added ones. (We generally can't change the virtual memory layout of the linked image as there are lots of relative addresses encoded anywhere in the section contents without any relocation marker, as those relocations were fixed at link time. So we can remove sections from the end and add new ones at the end, but we generally can't remove ones in the middle leaving a gap.) So if we split out setting addresses to a separate pass, it would only run over the sections that were added, not all of them. So keeping it in addSection and just checking the flags to set like this patch does, probably is fine.

llvm/test/tools/llvm-objcopy/COFF/set-flags-on-setion-added.test
14 ↗(On Diff #363300)

I don't see any check here that actually verifies that the newly added sections get the right flags here?

Oh, also, @alfonsosanchezbeato, please include extra context in the diff when you upload (by doing e.g. git diff -U999) to make it easier to review here.

Test updated as requested by review.

The test looks good to me now, and I've given my opinion on the implementation form, so +1 on this from me now, but I'll let @jhenderson approve if he's ok with it.

jhenderson added inline comments.Aug 16 2021, 3:44 AM
llvm/test/tools/llvm-objcopy/COFF/set-flags-on-setion-added.test
7 ↗(On Diff #365182)

There's only one test case in this file, so there's no need for the --check-prefixes argument. Just use the default CHECK for your check prefix throughout the file.

9–24 ↗(On Diff #365182)

Let's clean up the spacing so that the values all line up nicely.

13 ↗(On Diff #365182)

Is there a reason you don't check the full set of Characteristics here (i.e. including the title etc)? Something like the following:

# CHECK:       Characteristics [
# CHECK-NEXT:   IMAGE_SCN_CNT_CODE (0x20)
# CHECK-NEXT:   IMAGE_SCN_MEM_EXECUTE (0x20000000)
# CHECK-NEXT:   IMAGE_SCN_MEM_READ (0x40000000)
# CHECK-NEXT:   IMAGE_SCN_MEM_WRITE (0x80000000)
# CHECK-NEXT: ]

This will ensure no spurious flags have been set too.

Same applies for the second section.

Thanks for the comments. I have improved the test as suggested.

jhenderson added inline comments.Aug 17 2021, 2:20 AM
llvm/test/tools/llvm-objcopy/COFF/set-flags-on-setion-added.test
13 ↗(On Diff #366840)

I'd omit the (0x...) bit from this line and the similar line below. This will a) make maintaining the test easier in the future (if the flag set changes), and more importantly b) will ensure this matches the Characteristics block for this section as opposed to some future section.

Ok, I've removed the hex bits from the comparison.

jhenderson accepted this revision.Aug 17 2021, 2:28 AM

Ok, I've removed the hex bits from the comparison.

FWIW, I was referring specifically to the Characteristics line, but I don't think it matters whether they are there or not for the other lines. LGTM.

This revision is now accepted and ready to land.Aug 17 2021, 2:28 AM
MaskRay accepted this revision.Aug 17 2021, 1:38 PM

Consider renaming the test to match ELF/add-section-and-set-flags.test

MaskRay retitled this revision from Consider section flags when adding section to [llvm-objcopy] Consider section flags when adding section.Aug 17 2021, 1:54 PM

@ alfonsosanchezbeato - This is approved but not pushed yet - I guess you don't have push access? If you provide your preferred git author line, "Real Name <email@address>", I can push it for you. (Also note @MaskRay 's request for renaming the test, but I guess I can do that before pushing too.)

@ alfonsosanchezbeato - This is approved but not pushed yet - I guess you don't have push access? If you provide your preferred git author line, "Real Name <email@address>", I can push it for you. (Also note @MaskRay 's request for renaming the test, but I guess I can do that before pushing too.)

I've renamed the test now. Right, I do not have push access, so I would appreciate if you can do that for me. The author line would be:

Author: Alfonso Sánchez-Beato <alfonso.sanchez-beato@canonical.com>

Thanks,
Alfonso