Skip to content

Commit 85c6060

Browse files
fixup! [llvm-objcopy] Error with relevant message when adding invalid notes
1 parent 1575c38 commit 85c6060

File tree

7 files changed

+41
-12
lines changed

7 files changed

+41
-12
lines changed

llvm/docs/CommandGuide/llvm-objcopy.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,12 @@ them.
512512
specified format. See `SUPPORTED FORMATS`_ for a list of valid ``<format>``
513513
values.
514514

515+
.. option:: --verify-note-sections, --no-verify-note-sections
516+
517+
When adding note sections, verify if the section format is valid. Defaults to
518+
``--verify-note-sections``
519+
520+
515521
.. option:: --weaken-symbol <symbol>, -W
516522

517523
Mark global symbols named ``<symbol>`` as weak symbols in the output. Can

llvm/include/llvm/ObjCopy/CommonConfig.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,6 @@ struct CommonConfig {
258258
bool StripNonAlloc = false;
259259
bool StripSections = false;
260260
bool StripUnneeded = false;
261-
bool VerifyNoteSections = true;
262261
bool Weaken = false;
263262
bool DecompressDebugSections = false;
264263

llvm/include/llvm/ObjCopy/ELF/ELFConfig.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ struct ELFConfig {
3030
bool AllowBrokenLinks = false;
3131
bool KeepFileSymbols = false;
3232
bool LocalizeHidden = false;
33+
bool VerifyNoteSections = true;
3334
};
3435

3536
} // namespace objcopy

llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ static Error verifyNoteSection(StringRef Name, endianness Endianness,
653653
uint32_t NameSizeValue = *reinterpret_cast<const uint32_t *>(NameSize.data());
654654
uint32_t DescSizeValue = *reinterpret_cast<const uint32_t *>(DescSize.data());
655655

656-
if (Endianness != llvm::endianness::native) {
656+
if (Endianness != endianness::native) {
657657
NameSizeValue = byteswap(NameSizeValue);
658658
DescSizeValue = byteswap(DescSizeValue);
659659
}
@@ -737,7 +737,7 @@ static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig,
737737
Obj.addSection<OwnedDataSection>(Name, Data);
738738
if (Name.starts_with(".note") && Name != ".note.GNU-stack") {
739739
NewSection.Type = SHT_NOTE;
740-
if (Config.VerifyNoteSections)
740+
if (ELFConfig.VerifyNoteSections)
741741
return verifyNoteSection(Name, E, Data);
742742
}
743743
return Error::success();

llvm/test/tools/llvm-objcopy/ELF/add-invalid-note.test

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,46 @@
11
## Verify that --add-section warns on invalid note sections.
22

33
# Add [namesz, descsz, type, name, desc] for a build id.
4-
#
4+
5+
## Notes should be padded on 8 bits
56
# RUN: echo -e -n "\x04\x00\x00\x00" > %t-miss-padding-note.bin
67
# RUN: echo -e -n "\x07\x00\x00\x00" >> %t-miss-padding-note.bin
78
# RUN: echo -e -n "\x03\x00\x00\x00" >> %t-miss-padding-note.bin
89
# RUN: echo -e -n "GNU\x00" >> %t-miss-padding-note.bin
910
# RUN: echo -e -n "\x0c\x0d\x0e" >> %t-miss-padding-note.bin
10-
#
11+
12+
## The namesz field bit is incorrect
1113
# RUN: echo -e -n "\x08\x00\x00\x00" > %t-invalid-size-note.bin
1214
# RUN: echo -e -n "\x07\x00\x00\x00" >> %t-invalid-size-note.bin
1315
# RUN: echo -e -n "\x03\x00\x00\x00" >> %t-invalid-size-note.bin
1416
# RUN: echo -e -n "GNU\x00" >> %t-invalid-size-note.bin
1517
# RUN: echo -e -n "\x0c\x0d\x0e\x00" >> %t-invalid-size-note.bin
1618

19+
## Missing type field
1720
# RUN: echo -e -n "\x08\x00\x00\x00" > %t-short-note.bin
1821
# RUN: echo -e -n "\x07\x00\x00\x00" >> %t-short-note.bin
1922

2023
# RUN: yaml2obj %s -o %t.o
24+
25+
## test each invalid note
2126
# RUN: not llvm-objcopy --add-section=.note.miss.padding=%t-miss-padding-note.bin %t.o %t-with-note.o 2>&1 | FileCheck --check-prefix=CHECK-MISS-PADDING %s
27+
# CHECK-MISS-PADDING: .note.miss.padding data size must be a multiple of 4 bytes
28+
2229
# RUN: not llvm-objcopy --add-section=.note.invalid.size=%t-invalid-size-note.bin %t.o %t-with-note.o 2>&1 | FileCheck --check-prefix=CHECK-INVALID-SIZE %s
30+
# CHECK-INVALID-SIZE: .note.invalid.size data size is incompatible with the content of the name and description size fields: expecting 28, found 20
31+
2332
# RUN: not llvm-objcopy --add-section=.note.short=%t-short-note.bin %t.o %t-with-note.o 2>&1 | FileCheck --check-prefix=CHECK-SHORT %s
33+
# CHECK-SHORT: .note.short data must be either empty or at least 12 bytes long
34+
35+
## test that verification can be disabled
2436
# RUN: llvm-objcopy --add-section=.note.short=%t-short-note.bin --no-verify-note-sections %t.o %t-with-note.o
2537

38+
## test that last argument has precedence
39+
# RUN: llvm-objcopy --add-section=.note.short=%t-short-note.bin --verify-note-section --no-verify-note-sections %t.o %t-with-note.o
40+
2641
!ELF
2742
FileHeader:
2843
Class: ELFCLASS64
2944
Data: ELFDATA2LSB
3045
Type: ET_REL
3146

32-
# CHECK-MISS-PADDING: .note.miss.padding data size must be a multiple of 4 bytes
33-
# CHECK-INVALID-SIZE: .note.invalid.size data size is incompatible with the content of the name and description size fields: expecting 28, found 20
34-
# CHECK-SHORT: .note.short data must be either empty or at least 12 bytes long

llvm/tools/llvm-objcopy/ObjcopyOptions.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -949,8 +949,12 @@ objcopy::parseObjcopyOptions(ArrayRef<const char *> RawArgsArr,
949949
? DiscardType::All
950950
: DiscardType::Locals;
951951
}
952-
Config.VerifyNoteSections =
953-
!InputArgs.hasArg(OBJCOPY_no_verify_note_sections);
952+
953+
const bool ShouldVerifyNoteSectionsByDefault = true;
954+
ELFConfig.VerifyNoteSections = InputArgs.hasFlag(
955+
OBJCOPY_verify_note_sections, OBJCOPY_no_verify_note_sections,
956+
ShouldVerifyNoteSectionsByDefault);
957+
954958
Config.OnlyKeepDebug = InputArgs.hasArg(OBJCOPY_only_keep_debug);
955959
ELFConfig.KeepFileSymbols = InputArgs.hasArg(OBJCOPY_keep_file_symbols);
956960
MachOConfig.KeepUndefined = InputArgs.hasArg(OBJCOPY_keep_undefined);

llvm/tools/llvm-objcopy/ObjcopyOpts.td

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
include "CommonOpts.td"
22

3+
multiclass B<string name, string help1, string help2> {
4+
def NAME: Flag<["--"], name>, HelpText<help1>;
5+
def no_ # NAME: Flag<["--"], "no-" # name>, HelpText<help2>;
6+
}
7+
8+
39
defm binary_architecture
410
: Eq<"binary-architecture", "Ignored for compatibility">;
511
def B : JoinedOrSeparate<["-"], "B">,
@@ -176,8 +182,9 @@ def G : JoinedOrSeparate<["-"], "G">,
176182
Alias<keep_global_symbol>,
177183
HelpText<"Alias for --keep-global-symbol">;
178184

179-
def no_verify_note_sections : Flag<["--"], "no-verify-note-sections">,
180-
HelpText<"Disable note section validation">;
185+
defm verify_note_sections : B<"verify-note-sections",
186+
"Validate note sections",
187+
"Don't validate note sections">;
181188

182189
defm keep_global_symbols
183190
: Eq<"keep-global-symbols",

0 commit comments

Comments
 (0)