This is an archive of the discontinued LLVM Phabricator instance.

[Objcopy][Wasm] Allow selecting known sections by name
ClosedPublic

Authored by dschuff on May 26 2022, 4:54 PM.

Details

Summary

Currently, only custom sections can be selected by operations that use section
names, because only custom sections have explicit names (whereas known sections
have names defined by the spec and only use their indices in the binary format).
This CL makes objdopy use the spec-defined names for these sections, allowing
them to be used in operations such as dumping and removal.

This is a prerequisite for fixing
https://github.com/emscripten-core/emscripten/issues/13084

Diff Detail

Event Timeline

dschuff created this revision.May 26 2022, 4:54 PM
dschuff requested review of this revision.May 26 2022, 4:54 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 26 2022, 4:54 PM
sbc100 added inline comments.May 26 2022, 4:59 PM
llvm/lib/BinaryFormat/Wasm.cpp
74 ↗(On Diff #432420)

Looks like should be able to delete the copy of this function from lld/wasm/OutputSections.cpp?

And what looks like another version in WasmObjectFile::getSectionName

sbc100 added inline comments.May 26 2022, 5:01 PM
llvm/include/llvm/BinaryFormat/Wasm.h
475 ↗(On Diff #432420)

Seems like we should be consistent here with std::string vs StringRef.

dschuff updated this revision to Diff 432431.May 26 2022, 5:35 PM
dschuff marked an inline comment as done.

Deduplicate

llvm/include/llvm/BinaryFormat/Wasm.h
475 ↗(On Diff #432420)

agreed, although I think StringRef is probably the better one to use if we can. Also it looks like we have duplicate relocTypetoStrings too, so maybe that's better cleaned up in a separate CL.

llvm/lib/BinaryFormat/Wasm.cpp
74 ↗(On Diff #432420)

oops yes, I saw the one in lld but forgot to remove it. (didn't see the one in WasmObjectFile)

dschuff added inline comments.May 26 2022, 5:37 PM
llvm/lib/Object/WasmObjectFile.cpp
1732 ↗(On Diff #432431)

This is ugly now because it "knows" that TAG is the last known section id, which will be wrong when we add another. Do you prefer a fix of adding some kind of LAST_SECTION_ID in BinaryFormat/Wasm.h, maybe a function like isValidSectionId()?

Looks like there's no test case?

sbc100 added inline comments.May 27 2022, 7:03 AM
llvm/lib/Object/WasmObjectFile.cpp
1732 ↗(On Diff #432431)

Its still better than the status quo of duplicating the entire function. Are you planning on landing the refactoring/de-duplication change first/separately?

dschuff updated this revision to Diff 432572.May 27 2022, 8:34 AM
  • Merge branch 'sectionTypeToString' into objcopy-known

Looks like there's no test case?

Sorry, my second diff accidentally included just the refactoring. I opened https://reviews.llvm.org/D126553 for that, and this change is now just the tool change on top of that.

dschuff updated this revision to Diff 433102.May 31 2022, 8:52 AM
  • Merge branch 'main' into objcopy-known

OK, the prerequisite refactoring has landed, and this patch is now based on main, please take a look.

sbc100 accepted this revision.May 31 2022, 9:01 AM
sbc100 added inline comments.
llvm/test/tools/llvm-objcopy/wasm/dump-section.test
24

Just only empty line here?

llvm/test/tools/llvm-objcopy/wasm/remove-section.test
13

Why use --implicit-check-not instead of CHECK-NOT lines. (I've never seen implicit-check-not used before).

This revision is now accepted and ready to land.May 31 2022, 9:01 AM
dschuff updated this revision to Diff 433150.May 31 2022, 11:18 AM
dschuff marked an inline comment as done.
  • remove stray blank line
llvm/test/tools/llvm-objcopy/wasm/remove-section.test
13

implicit-check-not ensures that the text doesn't appear anywhere in the file at all. It's like having a CHECK-NOT in between all your CHECK lines, so it's much cleaner for that use case. In this particular case maybe it's not strictly necessary (since the type section could only go before the producers section) but it's used in several o the other objcopy tests, so it's consistent to use it here too.

sbc100 accepted this revision.May 31 2022, 11:49 AM

@jhenderson or others, would you like to review this too, or is sbc's review sufficient?

@jhenderson or others, would you like to review this too, or is sbc's review sufficient?

I'm happy for @sbc100 to review wasm-only changes like this patch, without contributing myself. If the change starts impacting generic llvm-objcopy code, I'd like to review it though.

llvm/test/tools/llvm-objcopy/wasm/dump-section.test
16
aheejin added inline comments.Jun 6 2022, 1:36 PM
llvm/lib/ObjCopy/wasm/WasmReader.cpp
30

Maybe create WASM_LAST_SEC or something?

llvm/test/tools/llvm-objcopy/wasm/only-keep-debug.test
17
dschuff added inline comments.Jun 6 2022, 1:48 PM
llvm/lib/ObjCopy/wasm/WasmReader.cpp
30

Yeah, I noticed that we don't have that anywhere else in BinaryFormat/Wasm.h but you're right that it's used elsewhere in LLVM. Maybe we should add it for this case anyway. There are 2 other places in the code where we compare this way and it does seem like it's a bug waiting to happen. I'll go ahead and do that in a followup.

dschuff marked an inline comment as done.Jun 6 2022, 1:49 PM

I'm happy for @sbc100 to review wasm-only changes like this patch, without contributing myself. If the change starts impacting generic llvm-objcopy code, I'd like to review it though.

Thanks I'll keep that in mind.

This revision was landed with ongoing or failed builds.Jun 6 2022, 1:55 PM
This revision was automatically updated to reflect the committed changes.
aheejin added inline comments.Jun 6 2022, 3:40 PM
llvm/test/tools/llvm-objcopy/wasm/only-keep-debug.test
17

Not something important, but gentle ping?

dschuff added inline comments.Jun 6 2022, 3:41 PM
llvm/test/tools/llvm-objcopy/wasm/only-keep-debug.test
17

oh, oops, sorry I missed that. Will fix.

aheejin added inline comments.Jun 6 2022, 4:58 PM
llvm/test/tools/llvm-objcopy/wasm/only-keep-debug.test
17
# CHECK-NOT:    - Type: TYPE

is still missing. Not important though.

dschuff added inline comments.Jun 6 2022, 5:07 PM
llvm/test/tools/llvm-objcopy/wasm/only-keep-debug.test
17

oops, I completely misread your comment, sorry about that 😓
Yes, I think having that check is a good idea.
It's a little weird because the FileCheck line that includes KEEPTYPE also includes CHECK as a prefix. So in that case KEEPTYPE line requires that line, and then the CHECK-NOT isn't needed. but it does still work.
Maybe even better would be to add --implicit-check-not=TYPE to ensure that the type section isn't anywhere in the file. Although that would be overkill since the line where this CHECK is, is the only place it could appear.
I'll just add the CHECK-NOT where you suggested.

aheejin added inline comments.Jun 6 2022, 8:33 PM
llvm/test/tools/llvm-objcopy/wasm/only-keep-debug.test
17

Oh, I didn't notice KEEPTYPE has also CHECK in the command. Then does my CHECK-NOT line work? I guess not but you said it works... Anyway nevermind if it doesn't work.

dschuff added inline comments.Jun 7 2022, 8:56 AM
llvm/test/tools/llvm-objcopy/wasm/only-keep-debug.test
17

It does work; I think it just checks that there isn't another instance of TYPE after it matches the one that's there. So that's not really necessary (in the sense that having the type section be duplicated seems an unlikely failure mode) but doesn't seem harmful either.