Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions llvm/lib/ObjCopy/COFF/COFFObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ using namespace object;
void Object::addSymbols(ArrayRef<Symbol> NewSymbols) {
for (Symbol S : NewSymbols) {
S.UniqueId = NextSymbolUniqueId++;
S.OriginalRawIndex = NextSymbolOriginalIndex;
NextSymbolOriginalIndex += 1 + S.Sym.NumberOfAuxSymbols;
Symbols.emplace_back(S);
}
updateSymbols();
Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/ObjCopy/COFF/COFFObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ struct Symbol {
std::optional<size_t> WeakTargetSymbolId;
size_t UniqueId;
size_t RawIndex;
size_t OriginalRawIndex;
bool Referenced;
};

Expand Down Expand Up @@ -140,6 +141,7 @@ struct Object {
DenseMap<size_t, Symbol *> SymbolMap;

size_t NextSymbolUniqueId = 0;
size_t NextSymbolOriginalIndex = 0;

std::vector<Section> Sections;
DenseMap<ssize_t, Section *> SectionMap;
Expand Down
75 changes: 75 additions & 0 deletions llvm/lib/ObjCopy/COFF/COFFWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include "llvm/ADT/StringRef.h"
#include "llvm/BinaryFormat/COFF.h"
#include "llvm/Object/COFF.h"
#include "llvm/Support/CRC.h"
#include "llvm/Support/Endian.h"
#include "llvm/Support/Errc.h"
#include "llvm/Support/ErrorHandling.h"
#include <cstddef>
Expand Down Expand Up @@ -92,6 +94,77 @@ Error COFFWriter::finalizeSymbolContents() {
return Error::success();
}

Error COFFWriter::finalizeSymIdxContents() {
// CFGuards shouldn't be present in PE.
if (Obj.IsPE)
return Error::success();

// Currently handle only sections consisting only of .symidx.
// TODO: other sections such as .impcall and .hybmp$x require more complex
// handling as they have more complex layout.
auto IsSymIdxSection = [](StringRef Name) {
return Name == ".gljmp$y" || Name == ".giats$y" || Name == ".gfids$y" ||
Name == ".gehcont$y";
};

DenseMap<size_t, size_t> SymIdMap;
SmallDenseMap<ssize_t, coff_aux_section_definition *, 4> SecIdMap;
for (Symbol &Sym : Obj.getMutableSymbols()) {
SymIdMap[Sym.OriginalRawIndex] = Sym.RawIndex;

// We collect only definition symbols of the sections to update the
// checksums.
if (Sym.Sym.StorageClass == IMAGE_SYM_CLASS_STATIC &&
Sym.Sym.NumberOfAuxSymbols == 1 && Sym.Sym.Value == 0 &&
IsSymIdxSection(Sym.Name))
SecIdMap[Sym.TargetSectionId] =
reinterpret_cast<coff_aux_section_definition *>(
Sym.AuxData[0].Opaque);
}

for (Section &Sec : Obj.getMutableSections()) {
if (!IsSymIdxSection(Sec.Name))
continue;

ArrayRef<uint8_t> RawIds = Sec.getContents();
// Nothing to do and also the checksum will be -1 instead of 0 if we
// recalculate it on empty input.
if (RawIds.size() == 0)
continue;

auto SecDefIt = SecIdMap.find(Sec.UniqueId);
if (SecDefIt == SecIdMap.end())
return createStringError(object_error::invalid_symbol_index,
"section '%s' does not have the corresponding "
"symbol or the symbol has unexpected format",
Sec.Name.str().c_str());

// Create updated content.
ArrayRef<support::ulittle32_t> Ids(
reinterpret_cast<const support::ulittle32_t *>(RawIds.data()),
RawIds.size() / 4);
std::vector<support::ulittle32_t> NewIds;
for (support::ulittle32_t Id : Ids) {
auto SymIdIt = SymIdMap.find(Id);
if (SymIdIt == SymIdMap.end())
return createStringError(object_error::invalid_symbol_index,
"section '%s' contains a .symidx (%d) that is "
"incorrect or was stripped",
Sec.Name.str().c_str(), Id.value());
NewIds.push_back(support::ulittle32_t(SymIdIt->getSecond()));
}
ArrayRef<uint8_t> NewRawIds(reinterpret_cast<uint8_t *>(NewIds.data()),
RawIds.size());
// Update the checksum.
JamCRC JC(/*Init=*/0);
JC.update(NewRawIds);
SecDefIt->getSecond()->CheckSum = JC.getCRC();
// Set new content.
Sec.setOwnedContents(NewRawIds.vec());
}
return Error::success();
}

void COFFWriter::layoutSections() {
for (auto &S : Obj.getMutableSections()) {
if (S.Header.SizeOfRawData > 0)
Expand Down Expand Up @@ -183,6 +256,8 @@ Error COFFWriter::finalize(bool IsBigObj) {
return E;
if (Error E = finalizeSymbolContents())
return E;
if (Error E = finalizeSymIdxContents())
return E;

size_t SizeOfHeaders = 0;
FileAlignment = 1;
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/ObjCopy/COFF/COFFWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class COFFWriter {
template <class SymbolTy> std::pair<size_t, size_t> finalizeSymbolTable();
Error finalizeRelocTargets();
Error finalizeSymbolContents();
Error finalizeSymIdxContents();
void layoutSections();
Expected<size_t> finalizeStringTable();

Expand Down
188 changes: 188 additions & 0 deletions llvm/test/tools/llvm-objcopy/COFF/strip-invalid-symidx-section.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
## Test that we bail out if a section consisting of symidx is invalid.

## In this case, the symbol .gfids$y is not present at all.
# RUN: yaml2obj %s --docnum=1 -o %t1.in.o
# RUN: not llvm-objcopy --strip-debug %t1.in.o %t1.out.o 2>&1 | FileCheck %s --check-prefix=ERROR-NOSYM -DFILE=%t1.out.o

# ERROR-NOSYM: error: '[[FILE]]': section '.gfids$y' does not have the corresponding symbol or the symbol has unexpected format

--- !COFF
header:
Machine: IMAGE_FILE_MACHINE_AMD64
Characteristics: [ ]
sections:
- Name: .text
Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
- Name: '.gfids$y'
Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
Alignment: 4
SectionData: '04000000'
SizeOfRawData: 8
symbols:
- Name: .text
Value: 0
SectionNumber: 1
SimpleType: IMAGE_SYM_TYPE_NULL
ComplexType: IMAGE_SYM_DTYPE_NULL
StorageClass: IMAGE_SYM_CLASS_STATIC
SectionDefinition:
Length: 0
NumberOfRelocations: 4
NumberOfLinenumbers: 0
CheckSum: 0
Number: 1
- Name: foo
Value: 0
SectionNumber: 0
SimpleType: IMAGE_SYM_TYPE_NULL
ComplexType: IMAGE_SYM_DTYPE_NULL
StorageClass: IMAGE_SYM_CLASS_EXTERNAL
...

## In this case, the symbol .giats$y has a non-zero offset.
# RUN: yaml2obj %s --docnum=2 -o %t2.in.o
# RUN: not llvm-objcopy --strip-debug %t2.in.o %t2.out.o 2>&1 | FileCheck %s --check-prefix=ERROR-OFFSET -DFILE=%t2.out.o

# ERROR-OFFSET: error: '[[FILE]]': section '.giats$y' does not have the corresponding symbol or the symbol has unexpected format

--- !COFF
header:
Machine: IMAGE_FILE_MACHINE_AMD64
Characteristics: [ ]
sections:
- Name: .text
Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
- Name: '.giats$y'
Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
Alignment: 4
SectionData: '0600000010000000'
SizeOfRawData: 8
symbols:
- Name: .text
Value: 0
SectionNumber: 1
SimpleType: IMAGE_SYM_TYPE_NULL
ComplexType: IMAGE_SYM_DTYPE_NULL
StorageClass: IMAGE_SYM_CLASS_STATIC
SectionDefinition:
Length: 0
NumberOfRelocations: 0
NumberOfLinenumbers: 0
CheckSum: 0
Number: 1
- Name: '.giats$y'
Value: 42
SectionNumber: 2
SimpleType: IMAGE_SYM_TYPE_NULL
ComplexType: IMAGE_SYM_DTYPE_NULL
StorageClass: IMAGE_SYM_CLASS_STATIC
SectionDefinition:
Length: 8
NumberOfRelocations: 0
NumberOfLinenumbers: 0
CheckSum: 1167279533
Number: 5
- Name: foo
Value: 0
SectionNumber: 0
SimpleType: IMAGE_SYM_TYPE_NULL
ComplexType: IMAGE_SYM_DTYPE_NULL
StorageClass: IMAGE_SYM_CLASS_EXTERNAL
...

## In this case, the symbol .gljmp$y has a non-static storage class.
# RUN: yaml2obj %s --docnum=3 -o %t3.in.o
# RUN: not llvm-objcopy --strip-debug %t3.in.o %t3.out.o 2>&1 | FileCheck %s --check-prefix=ERROR-EXTERNAL -DFILE=%t3.out.o

# ERROR-EXTERNAL: error: '[[FILE]]': section '.gljmp$y' does not have the corresponding symbol or the symbol has unexpected format

--- !COFF
header:
Machine: IMAGE_FILE_MACHINE_AMD64
Characteristics: [ ]
sections:
- Name: .text
Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
- Name: '.gljmp$y'
Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
Alignment: 4
SectionData: '0600000010000000'
SizeOfRawData: 8
symbols:
- Name: .text
Value: 0
SectionNumber: 1
SimpleType: IMAGE_SYM_TYPE_NULL
ComplexType: IMAGE_SYM_DTYPE_NULL
StorageClass: IMAGE_SYM_CLASS_STATIC
SectionDefinition:
Length: 0
NumberOfRelocations: 0
NumberOfLinenumbers: 0
CheckSum: 0
Number: 1
- Name: '.gljmp$y'
Value: 0
SectionNumber: 2
SimpleType: IMAGE_SYM_TYPE_NULL
ComplexType: IMAGE_SYM_DTYPE_NULL
StorageClass: IMAGE_SYM_CLASS_EXTERNAL
- Name: foo
Value: 0
SectionNumber: 0
SimpleType: IMAGE_SYM_TYPE_NULL
ComplexType: IMAGE_SYM_DTYPE_NULL
StorageClass: IMAGE_SYM_CLASS_EXTERNAL
...

## In this case, .gfids$y contains a symbol index that is not present in the
## symbol table. Generally the behavior should be the same for every section consisting
## of .symidx directives, e.g .giats$y, .gljmp$y and .gehcont$y.
# RUN: yaml2obj %s --docnum=4 -o %t4.in.o
# RUN: not llvm-objcopy --strip-debug %t4.in.o %t4.out.o 2>&1 | FileCheck %s --check-prefix=ERROR-SYMIDX -DFILE=%t4.out.o

# ERROR-SYMIDX: error: '[[FILE]]': section '.gfids$y' contains a .symidx (16) that is incorrect or was stripped
--- !COFF
header:
Machine: IMAGE_FILE_MACHINE_AMD64
Characteristics: [ ]
sections:
- Name: .text
Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
- Name: '.gfids$y'
Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
Alignment: 4
SectionData: '0400000010000000'
SizeOfRawData: 8
symbols:
- Name: .text
Value: 0
SectionNumber: 1
SimpleType: IMAGE_SYM_TYPE_NULL
ComplexType: IMAGE_SYM_DTYPE_NULL
StorageClass: IMAGE_SYM_CLASS_STATIC
SectionDefinition:
Length: 0
NumberOfRelocations: 0
NumberOfLinenumbers: 0
CheckSum: 0
Number: 1
- Name: '.gfids$y'
Value: 0
SectionNumber: 2
SimpleType: IMAGE_SYM_TYPE_NULL
ComplexType: IMAGE_SYM_DTYPE_NULL
StorageClass: IMAGE_SYM_CLASS_STATIC
SectionDefinition:
Length: 8
NumberOfRelocations: 0
NumberOfLinenumbers: 0
CheckSum: 1167279533
Number: 5
- Name: foo
Value: 0
SectionNumber: 0
SimpleType: IMAGE_SYM_TYPE_NULL
ComplexType: IMAGE_SYM_DTYPE_NULL
StorageClass: IMAGE_SYM_CLASS_EXTERNAL
...
Loading