2018-12-19 15:24:38 +08:00
|
|
|
//===- Object.h -------------------------------------------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-12-19 15:24:38 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_TOOLS_OBJCOPY_COFF_OBJECT_H
|
|
|
|
#define LLVM_TOOLS_OBJCOPY_COFF_OBJECT_H
|
|
|
|
|
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2019-01-11 05:28:24 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2019-01-22 18:58:09 +08:00
|
|
|
#include "llvm/ADT/Optional.h"
|
2018-12-19 15:24:38 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2019-01-11 05:28:24 +08:00
|
|
|
#include "llvm/ADT/iterator_range.h"
|
2018-12-19 15:24:38 +08:00
|
|
|
#include "llvm/BinaryFormat/COFF.h"
|
|
|
|
#include "llvm/Object/COFF.h"
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
namespace objcopy {
|
|
|
|
namespace coff {
|
|
|
|
|
2019-01-11 05:28:24 +08:00
|
|
|
struct Relocation {
|
2019-01-11 05:59:41 +08:00
|
|
|
Relocation() {}
|
|
|
|
Relocation(const object::coff_relocation& R) : Reloc(R) {}
|
|
|
|
|
2019-01-11 05:28:24 +08:00
|
|
|
object::coff_relocation Reloc;
|
|
|
|
size_t Target;
|
|
|
|
StringRef TargetName; // Used for diagnostics only
|
|
|
|
};
|
|
|
|
|
2018-12-19 15:24:38 +08:00
|
|
|
struct Section {
|
|
|
|
object::coff_section Header;
|
2019-01-11 05:28:24 +08:00
|
|
|
std::vector<Relocation> Relocs;
|
2018-12-19 15:24:38 +08:00
|
|
|
StringRef Name;
|
2019-01-20 03:42:35 +08:00
|
|
|
ssize_t UniqueId;
|
|
|
|
size_t Index;
|
2019-01-23 16:25:28 +08:00
|
|
|
|
|
|
|
ArrayRef<uint8_t> getContents() const {
|
|
|
|
if (!OwnedContents.empty())
|
|
|
|
return OwnedContents;
|
|
|
|
return ContentsRef;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setContentsRef(ArrayRef<uint8_t> Data) {
|
|
|
|
OwnedContents.clear();
|
|
|
|
ContentsRef = Data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setOwnedContents(std::vector<uint8_t> &&Data) {
|
|
|
|
ContentsRef = ArrayRef<uint8_t>();
|
|
|
|
OwnedContents = std::move(Data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void clearContents() {
|
|
|
|
ContentsRef = ArrayRef<uint8_t>();
|
|
|
|
OwnedContents.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ArrayRef<uint8_t> ContentsRef;
|
|
|
|
std::vector<uint8_t> OwnedContents;
|
2018-12-19 15:24:38 +08:00
|
|
|
};
|
|
|
|
|
[llvm-objcopy] [COFF] Fix handling of aux symbols for big objects
The aux symbols were stored in an opaque std::vector<uint8_t>,
with contents interpreted according to the rest of the symbol.
All aux symbol types but one fit in 18 bytes (sizeof(coff_symbol16)),
and if written to a bigobj, two extra padding bytes are written (as
sizeof(coff_symbol32) is 20). In the storage agnostic intermediate
representation, store the aux symbols as a series of coff_symbol16
sized opaque blobs. (In practice, all such aux symbols only consist
of one aux symbol, so this is more flexible than what reality needs.)
The special case is the file aux symbols, which are written in
potentially more than one aux symbol slot, without any padding,
as one single long string. This can't be stored in the same opaque
vector of fixed sized aux symbol entries. The file aux symbols will
occupy a different number of aux symbol slots depending on the type
of output object file. As nothing in the intermediate process needs
to have accurate raw symbol indices, updating that is moved into the
writer class.
Differential Revision: https://reviews.llvm.org/D57009
llvm-svn: 351947
2019-01-23 19:54:51 +08:00
|
|
|
struct AuxSymbol {
|
|
|
|
AuxSymbol(ArrayRef<uint8_t> In) {
|
|
|
|
assert(In.size() == sizeof(Opaque));
|
|
|
|
std::copy(In.begin(), In.end(), Opaque);
|
|
|
|
}
|
|
|
|
|
|
|
|
ArrayRef<uint8_t> getRef() const {
|
|
|
|
return ArrayRef<uint8_t>(Opaque, sizeof(Opaque));
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t Opaque[sizeof(object::coff_symbol16)];
|
|
|
|
};
|
|
|
|
|
2018-12-19 15:24:38 +08:00
|
|
|
struct Symbol {
|
|
|
|
object::coff_symbol32 Sym;
|
|
|
|
StringRef Name;
|
[llvm-objcopy] [COFF] Fix handling of aux symbols for big objects
The aux symbols were stored in an opaque std::vector<uint8_t>,
with contents interpreted according to the rest of the symbol.
All aux symbol types but one fit in 18 bytes (sizeof(coff_symbol16)),
and if written to a bigobj, two extra padding bytes are written (as
sizeof(coff_symbol32) is 20). In the storage agnostic intermediate
representation, store the aux symbols as a series of coff_symbol16
sized opaque blobs. (In practice, all such aux symbols only consist
of one aux symbol, so this is more flexible than what reality needs.)
The special case is the file aux symbols, which are written in
potentially more than one aux symbol slot, without any padding,
as one single long string. This can't be stored in the same opaque
vector of fixed sized aux symbol entries. The file aux symbols will
occupy a different number of aux symbol slots depending on the type
of output object file. As nothing in the intermediate process needs
to have accurate raw symbol indices, updating that is moved into the
writer class.
Differential Revision: https://reviews.llvm.org/D57009
llvm-svn: 351947
2019-01-23 19:54:51 +08:00
|
|
|
std::vector<AuxSymbol> AuxData;
|
|
|
|
StringRef AuxFile;
|
2019-01-20 03:42:35 +08:00
|
|
|
ssize_t TargetSectionId;
|
|
|
|
ssize_t AssociativeComdatTargetSectionId = 0;
|
2019-01-22 18:58:09 +08:00
|
|
|
Optional<size_t> WeakTargetSymbolId;
|
2019-01-11 05:28:24 +08:00
|
|
|
size_t UniqueId;
|
|
|
|
size_t RawIndex;
|
|
|
|
bool Referenced;
|
2018-12-19 15:24:38 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Object {
|
|
|
|
bool IsPE = false;
|
|
|
|
|
|
|
|
object::dos_header DosHeader;
|
|
|
|
ArrayRef<uint8_t> DosStub;
|
|
|
|
|
|
|
|
object::coff_file_header CoffFileHeader;
|
|
|
|
|
|
|
|
bool Is64 = false;
|
|
|
|
object::pe32plus_header PeHeader;
|
|
|
|
uint32_t BaseOfData = 0; // pe32plus_header lacks this field.
|
|
|
|
|
|
|
|
std::vector<object::data_directory> DataDirectories;
|
2019-01-11 05:28:24 +08:00
|
|
|
|
|
|
|
ArrayRef<Symbol> getSymbols() const { return Symbols; }
|
|
|
|
// This allows mutating individual Symbols, but not mutating the list
|
|
|
|
// of symbols itself.
|
|
|
|
iterator_range<std::vector<Symbol>::iterator> getMutableSymbols() {
|
|
|
|
return make_range(Symbols.begin(), Symbols.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
const Symbol *findSymbol(size_t UniqueId) const;
|
|
|
|
|
|
|
|
void addSymbols(ArrayRef<Symbol> NewSymbols);
|
|
|
|
void removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
|
|
|
|
|
|
|
|
// Set the Referenced field on all Symbols, based on relocations in
|
|
|
|
// all sections.
|
|
|
|
Error markSymbols();
|
|
|
|
|
2019-01-20 03:42:35 +08:00
|
|
|
ArrayRef<Section> getSections() const { return Sections; }
|
|
|
|
// This allows mutating individual Sections, but not mutating the list
|
|
|
|
// of symbols itself.
|
|
|
|
iterator_range<std::vector<Section>::iterator> getMutableSections() {
|
|
|
|
return make_range(Sections.begin(), Sections.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
const Section *findSection(ssize_t UniqueId) const;
|
|
|
|
|
|
|
|
void addSections(ArrayRef<Section> NewSections);
|
|
|
|
void removeSections(function_ref<bool(const Section &)> ToRemove);
|
2019-01-20 03:42:48 +08:00
|
|
|
void truncateSections(function_ref<bool(const Section &)> ToTruncate);
|
2019-01-20 03:42:35 +08:00
|
|
|
|
2019-01-11 05:28:24 +08:00
|
|
|
private:
|
2018-12-19 15:24:38 +08:00
|
|
|
std::vector<Symbol> Symbols;
|
2019-01-11 05:28:24 +08:00
|
|
|
DenseMap<size_t, Symbol *> SymbolMap;
|
|
|
|
|
|
|
|
size_t NextSymbolUniqueId = 0;
|
|
|
|
|
2019-01-20 03:42:35 +08:00
|
|
|
std::vector<Section> Sections;
|
|
|
|
DenseMap<ssize_t, Section *> SectionMap;
|
|
|
|
|
|
|
|
ssize_t NextSectionUniqueId = 1; // Allow a UniqueId 0 to mean undefined.
|
|
|
|
|
[llvm-objcopy] [COFF] Fix handling of aux symbols for big objects
The aux symbols were stored in an opaque std::vector<uint8_t>,
with contents interpreted according to the rest of the symbol.
All aux symbol types but one fit in 18 bytes (sizeof(coff_symbol16)),
and if written to a bigobj, two extra padding bytes are written (as
sizeof(coff_symbol32) is 20). In the storage agnostic intermediate
representation, store the aux symbols as a series of coff_symbol16
sized opaque blobs. (In practice, all such aux symbols only consist
of one aux symbol, so this is more flexible than what reality needs.)
The special case is the file aux symbols, which are written in
potentially more than one aux symbol slot, without any padding,
as one single long string. This can't be stored in the same opaque
vector of fixed sized aux symbol entries. The file aux symbols will
occupy a different number of aux symbol slots depending on the type
of output object file. As nothing in the intermediate process needs
to have accurate raw symbol indices, updating that is moved into the
writer class.
Differential Revision: https://reviews.llvm.org/D57009
llvm-svn: 351947
2019-01-23 19:54:51 +08:00
|
|
|
// Update SymbolMap.
|
2019-01-11 05:28:24 +08:00
|
|
|
void updateSymbols();
|
2019-01-20 03:42:35 +08:00
|
|
|
|
|
|
|
// Update SectionMap and Index in each Section.
|
|
|
|
void updateSections();
|
2018-12-19 15:24:38 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Copy between coff_symbol16 and coff_symbol32.
|
|
|
|
// The source and destination files can use either coff_symbol16 or
|
|
|
|
// coff_symbol32, while we always store them as coff_symbol32 in the
|
|
|
|
// intermediate data structure.
|
|
|
|
template <class Symbol1Ty, class Symbol2Ty>
|
|
|
|
void copySymbol(Symbol1Ty &Dest, const Symbol2Ty &Src) {
|
|
|
|
static_assert(sizeof(Dest.Name.ShortName) == sizeof(Src.Name.ShortName),
|
|
|
|
"Mismatched name sizes");
|
|
|
|
memcpy(Dest.Name.ShortName, Src.Name.ShortName, sizeof(Dest.Name.ShortName));
|
|
|
|
Dest.Value = Src.Value;
|
|
|
|
Dest.SectionNumber = Src.SectionNumber;
|
|
|
|
Dest.Type = Src.Type;
|
|
|
|
Dest.StorageClass = Src.StorageClass;
|
|
|
|
Dest.NumberOfAuxSymbols = Src.NumberOfAuxSymbols;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy between pe32_header and pe32plus_header.
|
|
|
|
// We store the intermediate state in a pe32plus_header.
|
|
|
|
template <class PeHeader1Ty, class PeHeader2Ty>
|
|
|
|
void copyPeHeader(PeHeader1Ty &Dest, const PeHeader2Ty &Src) {
|
|
|
|
Dest.Magic = Src.Magic;
|
|
|
|
Dest.MajorLinkerVersion = Src.MajorLinkerVersion;
|
|
|
|
Dest.MinorLinkerVersion = Src.MinorLinkerVersion;
|
|
|
|
Dest.SizeOfCode = Src.SizeOfCode;
|
|
|
|
Dest.SizeOfInitializedData = Src.SizeOfInitializedData;
|
|
|
|
Dest.SizeOfUninitializedData = Src.SizeOfUninitializedData;
|
|
|
|
Dest.AddressOfEntryPoint = Src.AddressOfEntryPoint;
|
|
|
|
Dest.BaseOfCode = Src.BaseOfCode;
|
|
|
|
Dest.ImageBase = Src.ImageBase;
|
|
|
|
Dest.SectionAlignment = Src.SectionAlignment;
|
|
|
|
Dest.FileAlignment = Src.FileAlignment;
|
|
|
|
Dest.MajorOperatingSystemVersion = Src.MajorOperatingSystemVersion;
|
|
|
|
Dest.MinorOperatingSystemVersion = Src.MinorOperatingSystemVersion;
|
|
|
|
Dest.MajorImageVersion = Src.MajorImageVersion;
|
|
|
|
Dest.MinorImageVersion = Src.MinorImageVersion;
|
|
|
|
Dest.MajorSubsystemVersion = Src.MajorSubsystemVersion;
|
|
|
|
Dest.MinorSubsystemVersion = Src.MinorSubsystemVersion;
|
|
|
|
Dest.Win32VersionValue = Src.Win32VersionValue;
|
|
|
|
Dest.SizeOfImage = Src.SizeOfImage;
|
|
|
|
Dest.SizeOfHeaders = Src.SizeOfHeaders;
|
|
|
|
Dest.CheckSum = Src.CheckSum;
|
|
|
|
Dest.Subsystem = Src.Subsystem;
|
|
|
|
Dest.DLLCharacteristics = Src.DLLCharacteristics;
|
|
|
|
Dest.SizeOfStackReserve = Src.SizeOfStackReserve;
|
|
|
|
Dest.SizeOfStackCommit = Src.SizeOfStackCommit;
|
|
|
|
Dest.SizeOfHeapReserve = Src.SizeOfHeapReserve;
|
|
|
|
Dest.SizeOfHeapCommit = Src.SizeOfHeapCommit;
|
|
|
|
Dest.LoaderFlags = Src.LoaderFlags;
|
|
|
|
Dest.NumberOfRvaAndSize = Src.NumberOfRvaAndSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace coff
|
|
|
|
} // end namespace objcopy
|
|
|
|
} // end namespace llvm
|
|
|
|
|
|
|
|
#endif // LLVM_TOOLS_OBJCOPY_COFF_OBJECT_H
|