Skip to content

Commit

Permalink
[llvm-objcopy][WebAssembly] Allow --strip-debug to operate on relocat…
Browse files Browse the repository at this point in the history
…able files.

This change is enough to allow `--strip-debug` to work on object files,
without breaking the relocation information or symbol table.

A more complete version of this change would instead reconstruct the
symbol table and relocation sections, but that is much larger change.

Bug: llvm#102002
  • Loading branch information
sbc100 committed Aug 12, 2024
1 parent a2acea5 commit 05a578b
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 9 deletions.
5 changes: 5 additions & 0 deletions lld/wasm/Writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ void Writer::calculateCustomSections() {
// Exclude COMDAT sections that are not selected for inclusion
if (section->discarded)
continue;
// Ignore empty custom sections. In particular objcopy/strip will
// sometimes replace stripped sections with empty custom sections to
// avoid section re-numbering.
if (section->getSize() == 0)
continue;
StringRef name = section->name;
// These custom sections are known the linker and synthesized rather than
// blindly copied.
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/ObjCopy/wasm/WasmObjcopy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ using namespace object;
using SectionPred = std::function<bool(const Section &Sec)>;

static bool isDebugSection(const Section &Sec) {
return Sec.Name.starts_with(".debug");
return Sec.Name.starts_with(".debug") || Sec.Name.starts_with("reloc..debug");
}

static bool isLinkerSection(const Section &Sec) {
Expand Down
18 changes: 16 additions & 2 deletions llvm/lib/ObjCopy/wasm/WasmObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,22 @@ void Object::addSectionWithOwnedContents(
}

void Object::removeSections(function_ref<bool(const Section &)> ToRemove) {
// TODO: remove reloc sections for the removed section, handle symbols, etc.
llvm::erase_if(Sections, ToRemove);
if (isRelocatableObject) {
// For relocatable objects, avoid actually removing any sections,
// since that can invalidate the symbol table and relocation sections.
// TODO: Allow removal of sections by re-generating symbol table and
// relocation sections here instead.
for (auto &Sec : Sections) {
if (ToRemove(Sec)) {
Sec.Name = ".objcopy.removed";
Sec.SectionType = wasm::WASM_SEC_CUSTOM;
Sec.Contents = {};
Sec.HeaderSecSizeEncodingLen = std::nullopt;
}
}
} else {
llvm::erase_if(Sections, ToRemove);
}
}

} // end namespace wasm
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/ObjCopy/wasm/WasmObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ struct Object {
llvm::wasm::WasmObjectHeader Header;
// For now don't discriminate between kinds of sections.
std::vector<Section> Sections;
bool isRelocatableObject = false;

void addSectionWithOwnedContents(Section NewSection,
std::unique_ptr<MemoryBuffer> &&Content);
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/ObjCopy/wasm/WasmReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ using namespace llvm::wasm;
Expected<std::unique_ptr<Object>> Reader::create() const {
auto Obj = std::make_unique<Object>();
Obj->Header = WasmObj.getHeader();
Obj->isRelocatableObject = WasmObj.isRelocatableObject();
std::vector<Section> Sections;
Obj->Sections.reserve(WasmObj.getNumSections());
for (const SectionRef &Sec : WasmObj.sections()) {
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/ObjectYAML/WasmEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ bool WasmWriter::writeWasm(raw_ostream &OS) {
if (auto S = dyn_cast<WasmYAML::CustomSection>(Sec.get()))
SecName = S->Name;
if (!Checker.isValidSectionOrder(Sec->Type, SecName)) {
reportError("out of order section type: " + Twine(Sec->Type));
reportError("out of order section type: " + wasm::sectionTypeToString(Sec->Type));
return false;
}
encodeULEB128(Sec->Type, OS);
Expand Down
50 changes: 45 additions & 5 deletions llvm/test/tools/llvm-objcopy/wasm/strip-debug.test
Original file line number Diff line number Diff line change
@@ -1,7 +1,39 @@
## Test that debug sections (but not linking or names) are stripped with --strip-debug
# RUN: yaml2obj %s -o %t
# RUN: llvm-strip --strip-debug %t
# RUN: obj2yaml %t | FileCheck --implicit-check-not=.debug %s
# RUN: cp %t %t3
# RUN: llvm-objcopy --strip-debug %t %t2
## Test that debug sections (but not linking or names) are stripped with --strip-debug
# RUN: obj2yaml %t2 | FileCheck --implicit-check-not=.debug %s
#
# RUN: llvm-objcopy -g %t %t2g
# Verify that --strip-debug and -g produce the same output
# RUN: cmp %t2 %t2g

# RUN: llvm-strip --strip-debug %t3
# RUN: cmp %t2 %t3

# RUN: cp %t %t4
# RUN: llvm-strip -d %t4
# RUN: cmp %t2 %t4

# RUN: cp %t %t5
# RUN: llvm-strip -g %t5
# RUN: cmp %t2 %t5

# RUN: cp %t %t6
# RUN: llvm-strip -S %t6
# RUN: cmp %t2 %t6

# Verify that an archive with multiple object files is handled correctly.
# RUN: cp %t %t.duplicate
# RUN: cp %t2 %t.duplicate.stripped
# RUN: rm -f %t.multiple-stripped-obj.a
# RUN: llvm-ar crs %t.multiple-stripped-obj.a %t2 %t.duplicate.stripped
# RUN: rm -f %t.multiple-obj.a
# RUN: llvm-ar crs %t.multiple-obj.a %t %t.duplicate
# RUN: llvm-objcopy --strip-debug %t.multiple-obj.a %t.multiple-obj.stripped.a
# RUN: llvm-ar p %t.multiple-stripped-obj.a > %t.multiple-stripped-obj.a.dump
# RUN: llvm-ar p %t.multiple-obj.stripped.a > %t.multiple-obj.stripped.a.dump
# RUN: cmp %t.multiple-stripped-obj.a.dump %t.multiple-obj.stripped.a.dump

# CHECK: Sections:
# CHECK-NEXT: - Type: TYPE
Expand All @@ -28,7 +60,11 @@ Sections:
Body: 0B
- Type: CUSTOM
Name: .debug_info
Payload: CAFE1234
Payload: 'CAFE123456'
Relocations:
- Type: R_WASM_FUNCTION_INDEX_LEB
Index: 0
Offset: 0x0000000
- Type: CUSTOM
Name: linking
Version: 2
Expand All @@ -50,4 +86,8 @@ Sections:
Version: 9.0.0
- Type: CUSTOM
Name: .debug_line
Payload: DEADBEEF
Payload: 'DEADBEEF01'
Relocations:
- Type: R_WASM_FUNCTION_INDEX_LEB
Index: 0
Offset: 0x0000000

0 comments on commit 05a578b

Please sign in to comment.