Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[llvm-objcopy] [COFF] Add support for removing symbols
Differential Revision: https://reviews.llvm.org/D55881 llvm-svn: 350893
- Loading branch information
Showing
11 changed files
with
290 additions
and
14 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
llvm/test/tools/llvm-objcopy/COFF/Inputs/strip-symbols.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
--- !COFF | ||
header: | ||
Machine: IMAGE_FILE_MACHINE_AMD64 | ||
Characteristics: [ ] | ||
sections: | ||
- Name: .text | ||
Characteristics: [ ] | ||
Alignment: 4 | ||
SectionData: 488B0500000000488B0500000000488B0500000000 | ||
Relocations: | ||
- VirtualAddress: 3 | ||
SymbolTableIndex: 0 | ||
Type: IMAGE_REL_AMD64_REL32 | ||
- VirtualAddress: 10 | ||
SymbolTableIndex: 1 | ||
Type: IMAGE_REL_AMD64_REL32 | ||
- VirtualAddress: 17 | ||
SymbolName: foo | ||
Type: IMAGE_REL_AMD64_REL32 | ||
- Name: .rdata | ||
Characteristics: [ ] | ||
Alignment: 1 | ||
SectionData: '00' | ||
- Name: .rdata | ||
Characteristics: [ ] | ||
Alignment: 1 | ||
SectionData: '01' | ||
symbols: | ||
- Name: .rdata | ||
Value: 0 | ||
SectionNumber: 2 | ||
SimpleType: IMAGE_SYM_TYPE_NULL | ||
ComplexType: IMAGE_SYM_DTYPE_NULL | ||
StorageClass: IMAGE_SYM_CLASS_STATIC | ||
- Name: .rdata | ||
Value: 0 | ||
SectionNumber: 3 | ||
SimpleType: IMAGE_SYM_TYPE_NULL | ||
ComplexType: IMAGE_SYM_DTYPE_NULL | ||
StorageClass: IMAGE_SYM_CLASS_STATIC | ||
- Name: mainfunc | ||
Value: 0 | ||
SectionNumber: 1 | ||
SimpleType: IMAGE_SYM_TYPE_NULL | ||
ComplexType: IMAGE_SYM_DTYPE_NULL | ||
StorageClass: IMAGE_SYM_CLASS_EXTERNAL | ||
- Name: foo | ||
Value: 0 | ||
SectionNumber: 3 | ||
SimpleType: IMAGE_SYM_TYPE_NULL | ||
ComplexType: IMAGE_SYM_DTYPE_NULL | ||
StorageClass: IMAGE_SYM_CLASS_EXTERNAL | ||
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# RUN: yaml2obj %p/Inputs/strip-symbols.yaml > %t.o | ||
# RUN: not llvm-objcopy -N foo %t.o 2>&1 | FileCheck %s --check-prefix=ERROR | ||
# RUN: not llvm-objcopy --strip-symbol foo %t.o 2>&1 | FileCheck %s --check-prefix=ERROR | ||
|
||
# ERROR: error: '{{.*}}/strip-reloc-symbol.test.tmp.o': not stripping symbol 'foo' because it is named in a relocation. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# RUN: yaml2obj %p/Inputs/strip-symbols.yaml > %t.in.o | ||
|
||
# RUN: llvm-readobj -relocations %t.in.o | FileCheck %s --check-prefixes=RELOCS,RELOCS-PRE | ||
# RUN: llvm-objdump -t %t.in.o | FileCheck %s --check-prefixes=SYMBOLS,SYMBOLS-PRE | ||
|
||
# RUN: llvm-objcopy -N mainfunc %t.in.o %t.out.o | ||
# RUN: llvm-readobj -relocations %t.out.o | FileCheck %s --check-prefixes=RELOCS,RELOCS-POST | ||
# RUN: llvm-objdump -t %t.out.o | FileCheck %s --check-prefix=SYMBOLS | ||
|
||
# RUN: llvm-objcopy --strip-symbol mainfunc %t.in.o %t.out.o | ||
# RUN: llvm-readobj -relocations %t.out.o | FileCheck %s --check-prefixes=RELOCS,RELOCS-POST | ||
# RUN: llvm-objdump -t %t.out.o | FileCheck %s --check-prefix=SYMBOLS | ||
|
||
# Explicitly listing the relocations for the input as well, to show | ||
# that the symbol index of the symbol foo is updated in the relocations, | ||
# while keeping relocations to two distinct .rdata symbols separate. | ||
|
||
# RELOCS: Relocations [ | ||
# RELOCS-NEXT: Section (1) .text { | ||
# RELOCS-NEXT: 0x3 IMAGE_REL_AMD64_REL32 .rdata (0) | ||
# RELOCS-NEXT: 0xA IMAGE_REL_AMD64_REL32 .rdata (1) | ||
# RELOCS-PRE-NEXT: 0x11 IMAGE_REL_AMD64_REL32 foo (3) | ||
# RELOCS-POST-NEXT: 0x11 IMAGE_REL_AMD64_REL32 foo (2) | ||
# RELOCS-NEXT: } | ||
# RELOCS-NEXT: ] | ||
|
||
# SYMBOLS: SYMBOL TABLE: | ||
# SYMBOLS-NEXT: .rdata | ||
# SYMBOLS-NEXT: .rdata | ||
# SYMBOLS-PRE-NEXT: mainfunc | ||
# SYMBOLS-NEXT: foo | ||
# SYMBOLS-EMPTY: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
//===- Object.cpp ---------------------------------------------------------===// | ||
// | ||
// The LLVM Compiler Infrastructure | ||
// | ||
// This file is distributed under the University of Illinois Open Source | ||
// License. See LICENSE.TXT for details. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "Object.h" | ||
#include <algorithm> | ||
|
||
namespace llvm { | ||
namespace objcopy { | ||
namespace coff { | ||
|
||
using namespace object; | ||
|
||
void Object::addSymbols(ArrayRef<Symbol> NewSymbols) { | ||
for (Symbol S : NewSymbols) { | ||
S.UniqueId = NextSymbolUniqueId++; | ||
Symbols.emplace_back(S); | ||
} | ||
updateSymbols(); | ||
} | ||
|
||
void Object::updateSymbols() { | ||
SymbolMap = DenseMap<size_t, Symbol *>(Symbols.size()); | ||
size_t RawSymIndex = 0; | ||
for (Symbol &Sym : Symbols) { | ||
SymbolMap[Sym.UniqueId] = &Sym; | ||
Sym.RawIndex = RawSymIndex; | ||
RawSymIndex += 1 + Sym.Sym.NumberOfAuxSymbols; | ||
} | ||
} | ||
|
||
const Symbol *Object::findSymbol(size_t UniqueId) const { | ||
auto It = SymbolMap.find(UniqueId); | ||
if (It == SymbolMap.end()) | ||
return nullptr; | ||
return It->second; | ||
} | ||
|
||
void Object::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) { | ||
Symbols.erase( | ||
std::remove_if(std::begin(Symbols), std::end(Symbols), | ||
[ToRemove](const Symbol &Sym) { return ToRemove(Sym); }), | ||
std::end(Symbols)); | ||
updateSymbols(); | ||
} | ||
|
||
Error Object::markSymbols() { | ||
for (Symbol &Sym : Symbols) | ||
Sym.Referenced = false; | ||
for (const Section &Sec : Sections) { | ||
for (const Relocation &R : Sec.Relocs) { | ||
auto It = SymbolMap.find(R.Target); | ||
if (It == SymbolMap.end()) | ||
return make_error<StringError>("Relocation target " + Twine(R.Target) + | ||
" not found", | ||
object_error::invalid_symbol_index); | ||
It->second->Referenced = true; | ||
} | ||
} | ||
return Error::success(); | ||
} | ||
|
||
} // end namespace coff | ||
} // end namespace objcopy | ||
} // end namespace llvm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.