Skip to content

Commit 2860c31

Browse files
committed
Print a warning for note segments and note sections in segments v2
1 parent 1ee4850 commit 2860c31

File tree

4 files changed

+80
-4
lines changed

4 files changed

+80
-4
lines changed

llvm/include/llvm/ObjCopy/CommonConfig.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,11 @@ struct CommonConfig {
281281

282282
SmallVector<std::pair<NameMatcher, llvm::DebugCompressionType>, 0>
283283
compressSections;
284+
285+
// ErrorCallback is used to handle recoverable errors. An Error returned
286+
// by the callback aborts the execution and is then returned to the caller.
287+
// If the callback is not set, the errors are not issued.
288+
std::function<Error(Error)> ErrorCallback;
284289
};
285290

286291
} // namespace objcopy

llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -671,11 +671,32 @@ RemoveNoteDetail::updateData(ArrayRef<uint8_t> OldData,
671671
}
672672

673673
static Error removeNotes(Object &Obj, endianness Endianness,
674-
ArrayRef<RemoveNoteInfo> NotesToRemove) {
674+
ArrayRef<RemoveNoteInfo> NotesToRemove,
675+
function_ref<Error(Error)> ErrorCallback) {
676+
// TODO: Support note segments.
677+
if (ErrorCallback) {
678+
for (Segment &Seg : Obj.segments()) {
679+
if (Seg.Type == PT_NOTE) {
680+
if (Error E = ErrorCallback(createStringError(
681+
errc::not_supported, "note segments are not supported")))
682+
return E;
683+
break;
684+
}
685+
}
686+
}
675687
for (auto &Sec : Obj.sections()) {
676-
// TODO: Support note sections in segments
677-
if (Sec.Type != SHT_NOTE || Sec.ParentSegment || !Sec.hasContents())
688+
if (Sec.Type != SHT_NOTE || !Sec.hasContents())
689+
continue;
690+
// TODO: Support note sections in segments.
691+
if (Sec.ParentSegment) {
692+
if (ErrorCallback)
693+
if (Error E = ErrorCallback(createStringError(
694+
errc::not_supported,
695+
"cannot remove note(s) from " + Sec.Name +
696+
": sections in segments are not supported")))
697+
return E;
678698
continue;
699+
}
679700
ArrayRef<uint8_t> OldData = Sec.getContents();
680701
size_t Align = std::max<size_t>(4, Sec.Align);
681702
// Note: notes for both 32-bit and 64-bit ELF files use 4-byte words in the
@@ -885,7 +906,8 @@ static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig,
885906
: endianness::big;
886907

887908
if (!ELFConfig.NotesToRemove.empty()) {
888-
if (Error Err = removeNotes(Obj, E, ELFConfig.NotesToRemove))
909+
if (Error Err =
910+
removeNotes(Obj, E, ELFConfig.NotesToRemove, Config.ErrorCallback))
889911
return Err;
890912
}
891913

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# RUN: yaml2obj --docnum=1 %s -o %t1
2+
# RUN: llvm-objcopy --remove-note=1 %t1 %t1o 2>&1 | FileCheck %s --check-prefix=NOTE_SEGMENT
3+
# NOTE_SEGMENT: warning: note segments are not supported
4+
# NOTE_SEGMENT-NOT: note segments are not supported
5+
6+
--- !ELF
7+
FileHeader:
8+
Class: ELFCLASS64
9+
Data: ELFDATA2LSB
10+
Type: ET_EXEC
11+
Machine: EM_X86_64
12+
ProgramHeaders:
13+
- Type: PT_NOTE
14+
FirstSec: .data0
15+
LastSec: .data0
16+
- Type: PT_NOTE
17+
FirstSec: .data1
18+
LastSec: .data1
19+
Sections:
20+
- Name: .data0
21+
Type: SHT_PROGBITS
22+
AddressAlign: 4
23+
Content: "1122334455"
24+
- Name: .data1
25+
Type: SHT_PROGBITS
26+
AddressAlign: 4
27+
Content: "1122334455"
28+
29+
# RUN: yaml2obj --docnum=2 %s -o %t2
30+
# RUN: llvm-objcopy --remove-note=1 %t2 %t2o 2>&1 | FileCheck %s --check-prefix=NOTE_SECTION
31+
# NOTE_SECTION: warning: cannot remove note(s) from .note: sections in segments are not supported
32+
33+
--- !ELF
34+
FileHeader:
35+
Class: ELFCLASS64
36+
Data: ELFDATA2LSB
37+
Type: ET_EXEC
38+
Machine: EM_X86_64
39+
ProgramHeaders:
40+
- Type: PT_LOAD
41+
FirstSec: .note
42+
LastSec: .note
43+
Sections:
44+
- Name: .note
45+
Type: SHT_NOTE
46+
AddressAlign: 4
47+
Content: "1122334455"

llvm/tools/llvm-objcopy/llvm-objcopy.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ int llvm_objcopy_main(int argc, char **argv, const llvm::ToolContext &) {
248248
return 1;
249249
}
250250
for (ConfigManager &ConfigMgr : DriverConfig->CopyConfigs) {
251+
assert(!ConfigMgr.Common.ErrorCallback);
252+
ConfigMgr.Common.ErrorCallback = reportWarning;
251253
if (Error E = executeObjcopy(ConfigMgr)) {
252254
logAllUnhandledErrors(std::move(E), WithColor::error(errs(), ToolName));
253255
return 1;

0 commit comments

Comments
 (0)