[llvm] 556af19 - [llvm-objcopy] Use getNumberOfSymbols() instead of getRawNumberOfSymbols()
Daan De Meyer via llvm-commits
llvm-commits at lists.llvm.org
Mon Jan 2 04:29:20 PST 2023
Author: Daan De Meyer
Date: 2023-01-02T13:22:50+01:00
New Revision: 556af193309f6e283712602b9a8ca5d19adb1792
URL: https://github.com/llvm/llvm-project/commit/556af193309f6e283712602b9a8ca5d19adb1792
DIFF: https://github.com/llvm/llvm-project/commit/556af193309f6e283712602b9a8ca5d19adb1792.diff
LOG: [llvm-objcopy] Use getNumberOfSymbols() instead of getRawNumberOfSymbols()
getRawNumberOfSymbols() assumes that a symbol table exists, which isn't
always guaranteed, while getNumberOfSymbols() handles and tolerates objects
without a symbol table. When there is a symbol table, both methods return
the same value.
Also add a test to ensure we don't regress in this regard. The test
generates a basic COFF object with symbols and overrides the symbol table
pointer with zeros to craft the input required to verify llvm-objcopy works
as expected in this scenario.
Added:
llvm/test/tools/llvm-objcopy/COFF/Inputs/no-symbol-table.yaml
llvm/test/tools/llvm-objcopy/COFF/no-symbol-table.test
Modified:
llvm/lib/ObjCopy/COFF/COFFReader.cpp
Removed:
################################################################################
diff --git a/llvm/lib/ObjCopy/COFF/COFFReader.cpp b/llvm/lib/ObjCopy/COFF/COFFReader.cpp
index 44bf303078dd0..32aceb805a2a0 100644
--- a/llvm/lib/ObjCopy/COFF/COFFReader.cpp
+++ b/llvm/lib/ObjCopy/COFF/COFFReader.cpp
@@ -83,9 +83,9 @@ Error COFFReader::readSections(Object &Obj) const {
Error COFFReader::readSymbols(Object &Obj, bool IsBigObj) const {
std::vector<Symbol> Symbols;
- Symbols.reserve(COFFObj.getRawNumberOfSymbols());
+ Symbols.reserve(COFFObj.getNumberOfSymbols());
ArrayRef<Section> Sections = Obj.getSections();
- for (uint32_t I = 0, E = COFFObj.getRawNumberOfSymbols(); I < E;) {
+ for (uint32_t I = 0, E = COFFObj.getNumberOfSymbols(); I < E;) {
Expected<COFFSymbolRef> SymOrErr = COFFObj.getSymbol(I);
if (!SymOrErr)
return SymOrErr.takeError();
diff --git a/llvm/test/tools/llvm-objcopy/COFF/Inputs/no-symbol-table.yaml b/llvm/test/tools/llvm-objcopy/COFF/Inputs/no-symbol-table.yaml
new file mode 100644
index 0000000000000..364db51aebcb7
--- /dev/null
+++ b/llvm/test/tools/llvm-objcopy/COFF/Inputs/no-symbol-table.yaml
@@ -0,0 +1,17 @@
+--- !COFF
+header:
+ Machine: IMAGE_FILE_MACHINE_AMD64
+ Characteristics: []
+sections:
+ - Name: .text
+ Characteristics: []
+# We define a symbol here and override the symbol table pointer in the test to
+# get an object with symbols but without a symbol table.
+symbols:
+ - Name: .text
+ Value: 0
+ SectionNumber: 1
+ SimpleType: IMAGE_SYM_TYPE_NULL
+ ComplexType: IMAGE_SYM_DTYPE_NULL
+ StorageClass: IMAGE_SYM_CLASS_STATIC
+...
diff --git a/llvm/test/tools/llvm-objcopy/COFF/no-symbol-table.test b/llvm/test/tools/llvm-objcopy/COFF/no-symbol-table.test
new file mode 100644
index 0000000000000..d558bf244960e
--- /dev/null
+++ b/llvm/test/tools/llvm-objcopy/COFF/no-symbol-table.test
@@ -0,0 +1,20 @@
+## Test that llvm-objcopy can read a COFF object with symbols but without a
+## symbol table.
+
+# RUN: yaml2obj %p/Inputs/no-symbol-table.yaml -o %t.obj
+
+## Check that we report a single symbol before overriding the symbol table pointer.
+# RUN: llvm-readobj --file-headers %t.obj | FileCheck %s --check-prefix=BEFORE
+# BEFORE: SymbolCount: 1
+
+## Override the symbol table pointer with zeros.
+# RUN: %python -c "with open(r'%t.obj', 'r+b') as input: input.seek(8); input.write(b'\x00' * 4)"
+# RUN: llvm-readobj --file-headers %t.obj | FileCheck %s --check-prefix=POINTER
+# POINTER: PointerToSymbolTable: 0x0
+
+## Make sure we can run llvm-objcopy on the resulting object.
+# RUN: llvm-objcopy %t.obj
+
+## Check that the number of symbols is now reported as zero.
+# RUN: llvm-readobj --file-headers %t.obj | FileCheck %s --check-prefix=COUNT
+# COUNT: SymbolCount: 0
\ No newline at end of file
More information about the llvm-commits
mailing list