[lld] [llvm] [llvm-objcopy][WebAssembly] Allow --strip-debug to operate on relocatable files. (PR #102978)
Sam Clegg via llvm-commits
llvm-commits at lists.llvm.org
Mon Aug 12 13:56:57 PDT 2024
https://github.com/sbc100 created https://github.com/llvm/llvm-project/pull/102978
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.
Fixes: #102002
>From 2a09d4c812efff1f7e8b12e8b28a003398aa15ac Mon Sep 17 00:00:00 2001
From: Sam Clegg <sbc at chromium.org>
Date: Mon, 12 Aug 2024 13:53:33 -0700
Subject: [PATCH] [llvm-objcopy][WebAssembly] Allow --strip-debug to operator
on relocatable 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.
Fixes: #102002
---
lld/wasm/Writer.cpp | 5 +++++
llvm/lib/ObjCopy/wasm/WasmObjcopy.cpp | 2 +-
llvm/lib/ObjCopy/wasm/WasmObject.cpp | 19 +++++++++++++++++--
llvm/lib/ObjCopy/wasm/WasmObject.h | 1 +
llvm/lib/ObjCopy/wasm/WasmReader.cpp | 1 +
5 files changed, 25 insertions(+), 3 deletions(-)
diff --git a/lld/wasm/Writer.cpp b/lld/wasm/Writer.cpp
index 6a66a29d2498c5..6beef81d39a381 100644
--- a/lld/wasm/Writer.cpp
+++ b/lld/wasm/Writer.cpp
@@ -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.
diff --git a/llvm/lib/ObjCopy/wasm/WasmObjcopy.cpp b/llvm/lib/ObjCopy/wasm/WasmObjcopy.cpp
index 5bba1dea9adf0e..cf3d884bee3bd8 100644
--- a/llvm/lib/ObjCopy/wasm/WasmObjcopy.cpp
+++ b/llvm/lib/ObjCopy/wasm/WasmObjcopy.cpp
@@ -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) {
diff --git a/llvm/lib/ObjCopy/wasm/WasmObject.cpp b/llvm/lib/ObjCopy/wasm/WasmObject.cpp
index 28a2de6e6e4f12..bee9c260645137 100644
--- a/llvm/lib/ObjCopy/wasm/WasmObject.cpp
+++ b/llvm/lib/ObjCopy/wasm/WasmObject.cpp
@@ -25,8 +25,23 @@ 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 an relocation sections.
+ // TODO: Allow removal of sections by re-generating symbol table and
+ // relocation sections here instead.
+ for (auto &Sec : Sections) {
+ if (ToRemove(Sec)) {
+ //static std::vector<char> empty;
+ 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
diff --git a/llvm/lib/ObjCopy/wasm/WasmObject.h b/llvm/lib/ObjCopy/wasm/WasmObject.h
index f860ec697e5684..374e523cd879b6 100644
--- a/llvm/lib/ObjCopy/wasm/WasmObject.h
+++ b/llvm/lib/ObjCopy/wasm/WasmObject.h
@@ -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);
diff --git a/llvm/lib/ObjCopy/wasm/WasmReader.cpp b/llvm/lib/ObjCopy/wasm/WasmReader.cpp
index 578e78955af3e4..420d17f9864323 100644
--- a/llvm/lib/ObjCopy/wasm/WasmReader.cpp
+++ b/llvm/lib/ObjCopy/wasm/WasmReader.cpp
@@ -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()) {
More information about the llvm-commits
mailing list