Skip to content

Commit ef902c6

Browse files
committed
[llvm-objcopy][WebAssembly] Allow --strip-debug to operate 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. Bug: #102002
1 parent a2acea5 commit ef902c6

File tree

7 files changed

+90
-10
lines changed

7 files changed

+90
-10
lines changed

lld/wasm/Writer.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@ void Writer::calculateCustomSections() {
138138
// Exclude COMDAT sections that are not selected for inclusion
139139
if (section->discarded)
140140
continue;
141+
// Ignore empty custom sections. In particular objcopy/strip will
142+
// sometimes replace stripped sections with empty custom sections to
143+
// avoid section re-numbering.
144+
if (section->getSize() == 0)
145+
continue;
141146
StringRef name = section->name;
142147
// These custom sections are known the linker and synthesized rather than
143148
// blindly copied.

llvm/lib/ObjCopy/wasm/WasmObjcopy.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ using namespace object;
2222
using SectionPred = std::function<bool(const Section &Sec)>;
2323

2424
static bool isDebugSection(const Section &Sec) {
25-
return Sec.Name.starts_with(".debug");
25+
return Sec.Name.starts_with(".debug") || Sec.Name.starts_with("reloc..debug");
2626
}
2727

2828
static bool isLinkerSection(const Section &Sec) {

llvm/lib/ObjCopy/wasm/WasmObject.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,22 @@ void Object::addSectionWithOwnedContents(
2525
}
2626

2727
void Object::removeSections(function_ref<bool(const Section &)> ToRemove) {
28-
// TODO: remove reloc sections for the removed section, handle symbols, etc.
29-
llvm::erase_if(Sections, ToRemove);
28+
if (isRelocatableObject) {
29+
// For relocatable objects, avoid actually removing any sections,
30+
// since that can invalidate the symbol table and relocation sections.
31+
// TODO: Allow removal of sections by re-generating symbol table and
32+
// relocation sections here instead.
33+
for (auto &Sec : Sections) {
34+
if (ToRemove(Sec)) {
35+
Sec.Name = ".objcopy.removed";
36+
Sec.SectionType = wasm::WASM_SEC_CUSTOM;
37+
Sec.Contents = {};
38+
Sec.HeaderSecSizeEncodingLen = std::nullopt;
39+
}
40+
}
41+
} else {
42+
llvm::erase_if(Sections, ToRemove);
43+
}
3044
}
3145

3246
} // end namespace wasm

llvm/lib/ObjCopy/wasm/WasmObject.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ struct Object {
3232
llvm::wasm::WasmObjectHeader Header;
3333
// For now don't discriminate between kinds of sections.
3434
std::vector<Section> Sections;
35+
bool isRelocatableObject = false;
3536

3637
void addSectionWithOwnedContents(Section NewSection,
3738
std::unique_ptr<MemoryBuffer> &&Content);

llvm/lib/ObjCopy/wasm/WasmReader.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ using namespace llvm::wasm;
1818
Expected<std::unique_ptr<Object>> Reader::create() const {
1919
auto Obj = std::make_unique<Object>();
2020
Obj->Header = WasmObj.getHeader();
21+
Obj->isRelocatableObject = WasmObj.isRelocatableObject();
2122
std::vector<Section> Sections;
2223
Obj->Sections.reserve(WasmObj.getNumSections());
2324
for (const SectionRef &Sec : WasmObj.sections()) {

llvm/lib/ObjectYAML/WasmEmitter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ bool WasmWriter::writeWasm(raw_ostream &OS) {
604604
if (auto S = dyn_cast<WasmYAML::CustomSection>(Sec.get()))
605605
SecName = S->Name;
606606
if (!Checker.isValidSectionOrder(Sec->Type, SecName)) {
607-
reportError("out of order section type: " + Twine(Sec->Type));
607+
reportError("out of order section type: " + wasm::sectionTypeToString(Sec->Type));
608608
return false;
609609
}
610610
encodeULEB128(Sec->Type, OS);

llvm/test/tools/llvm-objcopy/wasm/strip-debug.test

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,65 @@
1-
## Test that debug sections (but not linking or names) are stripped with --strip-debug
21
# RUN: yaml2obj %s -o %t
3-
# RUN: llvm-strip --strip-debug %t
4-
# RUN: obj2yaml %t | FileCheck --implicit-check-not=.debug %s
2+
# RUN: cp %t %t3
3+
# RUN: llvm-objcopy --strip-debug %t %t2
4+
## Test that debug sections (but not linking or names) are stripped with --strip-debug
5+
# RUN: obj2yaml %t2 | FileCheck --implicit-check-not=.debug %s
6+
#
7+
# RUN: llvm-objcopy -g %t %t2g
8+
# Verify that --strip-debug and -g produce the same output
9+
# RUN: cmp %t2 %t2g
10+
11+
# RUN: llvm-strip --strip-debug %t3
12+
# RUN: cmp %t2 %t3
13+
14+
# RUN: cp %t %t4
15+
# RUN: llvm-strip -d %t4
16+
# RUN: cmp %t2 %t4
17+
18+
# RUN: cp %t %t5
19+
# RUN: llvm-strip -g %t5
20+
# RUN: cmp %t2 %t5
21+
22+
# RUN: cp %t %t6
23+
# RUN: llvm-strip -S %t6
24+
# RUN: cmp %t2 %t6
25+
26+
# Verify that an archive with multiple object files is handled correctly.
27+
# RUN: cp %t %t.duplicate
28+
# RUN: cp %t2 %t.duplicate.stripped
29+
# RUN: rm -f %t.multiple-stripped-obj.a
30+
# RUN: llvm-ar crs %t.multiple-stripped-obj.a %t2 %t.duplicate.stripped
31+
# RUN: rm -f %t.multiple-obj.a
32+
# RUN: llvm-ar crs %t.multiple-obj.a %t %t.duplicate
33+
# RUN: llvm-objcopy --strip-debug %t.multiple-obj.a %t.multiple-obj.stripped.a
34+
# RUN: llvm-ar p %t.multiple-stripped-obj.a > %t.multiple-stripped-obj.a.dump
35+
# RUN: llvm-ar p %t.multiple-obj.stripped.a > %t.multiple-obj.stripped.a.dump
36+
# RUN: cmp %t.multiple-stripped-obj.a.dump %t.multiple-obj.stripped.a.dump
537

638
# CHECK: Sections:
739
# CHECK-NEXT: - Type: TYPE
8-
# CHECK: Name: linking
40+
# CHECK: - Type: CUSTOM
41+
## We expect the linking section to be preceeded by the removed `.debug_info`
42+
## section.
43+
# CHECK-NEXT: Name: .objcopy.removed
44+
# CHECK-NEXT: Payload: ''
45+
# CHECK-NEXT: - Type: CUSTOM
46+
# CHECK-NEXT: Name: linking
947
# CHECK: Name: name
1048
# CHECK-NEXT: FunctionNames:
1149
# CHECK: Name: producers
50+
## Following the producers section we expect to find three removed sections.
51+
## The `.debug_line` section that two reloction section corresponding to the
52+
## two debug sections.
53+
# CHECK: - Type: CUSTOM
54+
# CHECK-NEXT: Name: .objcopy.removed
55+
# CHECK-NEXT: Payload: ''
56+
# CHECK-NEXT: - Type: CUSTOM
57+
# CHECK-NEXT: Name: .objcopy.removed
58+
# CHECK-NEXT: Payload: ''
59+
# CHECK-NEXT: - Type: CUSTOM
60+
# CHECK-NEXT: Name: .objcopy.removed
61+
# CHECK-NEXT: Payload: ''
62+
1263

1364
--- !WASM
1465
FileHeader:
@@ -28,7 +79,11 @@ Sections:
2879
Body: 0B
2980
- Type: CUSTOM
3081
Name: .debug_info
31-
Payload: CAFE1234
82+
Payload: 'CAFE123456'
83+
Relocations:
84+
- Type: R_WASM_FUNCTION_INDEX_LEB
85+
Index: 0
86+
Offset: 0x0000000
3287
- Type: CUSTOM
3388
Name: linking
3489
Version: 2
@@ -50,4 +105,8 @@ Sections:
50105
Version: 9.0.0
51106
- Type: CUSTOM
52107
Name: .debug_line
53-
Payload: DEADBEEF
108+
Payload: 'DEADBEEF01'
109+
Relocations:
110+
- Type: R_WASM_FUNCTION_INDEX_LEB
111+
Index: 0
112+
Offset: 0x0000000

0 commit comments

Comments
 (0)