2022-02-22 22:56:22 +03:00
|
|
|
//===- COFFWriter.cpp -----------------------------------------------------===//
|
2018-12-19 07:24:38 +00:00
|
|
|
//
|
2019-01-19 08:50:56 +00: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 07:24:38 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2022-02-22 22:56:22 +03:00
|
|
|
#include "COFFWriter.h"
|
|
|
|
#include "COFFObject.h"
|
2018-12-19 07:24:38 +00:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/BinaryFormat/COFF.h"
|
|
|
|
#include "llvm/Object/COFF.h"
|
2020-10-24 17:35:55 +03:00
|
|
|
#include "llvm/Support/Errc.h"
|
2018-12-19 07:24:38 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
namespace objcopy {
|
|
|
|
namespace coff {
|
|
|
|
|
|
|
|
using namespace object;
|
|
|
|
using namespace COFF;
|
|
|
|
|
2019-01-10 21:28:24 +00:00
|
|
|
Error COFFWriter::finalizeRelocTargets() {
|
2019-01-19 19:42:35 +00:00
|
|
|
for (Section &Sec : Obj.getMutableSections()) {
|
2019-01-10 21:28:24 +00:00
|
|
|
for (Relocation &R : Sec.Relocs) {
|
|
|
|
const Symbol *Sym = Obj.findSymbol(R.Target);
|
|
|
|
if (Sym == nullptr)
|
2019-01-22 10:57:59 +00:00
|
|
|
return createStringError(object_error::invalid_symbol_index,
|
2019-05-22 13:23:26 +00:00
|
|
|
"relocation target '%s' (%zu) not found",
|
2019-01-22 10:57:59 +00:00
|
|
|
R.TargetName.str().c_str(), R.Target);
|
2019-01-10 21:28:24 +00:00
|
|
|
R.Reloc.SymbolTableIndex = Sym->RawIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2019-01-22 10:58:09 +00:00
|
|
|
Error COFFWriter::finalizeSymbolContents() {
|
2019-01-19 19:42:35 +00:00
|
|
|
for (Symbol &Sym : Obj.getMutableSymbols()) {
|
|
|
|
if (Sym.TargetSectionId <= 0) {
|
|
|
|
// Undefined, or a special kind of symbol. These negative values
|
|
|
|
// are stored in the SectionNumber field which is unsigned.
|
|
|
|
Sym.Sym.SectionNumber = static_cast<uint32_t>(Sym.TargetSectionId);
|
|
|
|
} else {
|
|
|
|
const Section *Sec = Obj.findSection(Sym.TargetSectionId);
|
|
|
|
if (Sec == nullptr)
|
2019-01-22 10:57:59 +00:00
|
|
|
return createStringError(object_error::invalid_symbol_index,
|
2019-05-22 13:23:26 +00:00
|
|
|
"symbol '%s' points to a removed section",
|
2019-01-22 10:57:59 +00:00
|
|
|
Sym.Name.str().c_str());
|
2019-01-19 19:42:35 +00:00
|
|
|
Sym.Sym.SectionNumber = Sec->Index;
|
|
|
|
|
|
|
|
if (Sym.Sym.NumberOfAuxSymbols == 1 &&
|
|
|
|
Sym.Sym.StorageClass == IMAGE_SYM_CLASS_STATIC) {
|
|
|
|
coff_aux_section_definition *SD =
|
[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 11:54:51 +00:00
|
|
|
reinterpret_cast<coff_aux_section_definition *>(
|
|
|
|
Sym.AuxData[0].Opaque);
|
2019-01-19 19:42:35 +00:00
|
|
|
uint32_t SDSectionNumber;
|
|
|
|
if (Sym.AssociativeComdatTargetSectionId == 0) {
|
|
|
|
// Not a comdat associative section; just set the Number field to
|
|
|
|
// the number of the section itself.
|
|
|
|
SDSectionNumber = Sec->Index;
|
|
|
|
} else {
|
|
|
|
Sec = Obj.findSection(Sym.AssociativeComdatTargetSectionId);
|
|
|
|
if (Sec == nullptr)
|
2019-01-22 10:57:59 +00:00
|
|
|
return createStringError(
|
|
|
|
object_error::invalid_symbol_index,
|
2019-05-22 13:23:26 +00:00
|
|
|
"symbol '%s' is associative to a removed section",
|
2019-01-22 10:57:59 +00:00
|
|
|
Sym.Name.str().c_str());
|
2019-01-19 19:42:35 +00:00
|
|
|
SDSectionNumber = Sec->Index;
|
|
|
|
}
|
|
|
|
// Update the section definition with the new section number.
|
|
|
|
SD->NumberLowPart = static_cast<uint16_t>(SDSectionNumber);
|
|
|
|
SD->NumberHighPart = static_cast<uint16_t>(SDSectionNumber >> 16);
|
|
|
|
}
|
|
|
|
}
|
2019-01-22 10:58:09 +00:00
|
|
|
// Check that we actually have got AuxData to match the weak symbol target
|
|
|
|
// we want to set. Only >= 1 would be required, but only == 1 makes sense.
|
|
|
|
if (Sym.WeakTargetSymbolId && Sym.Sym.NumberOfAuxSymbols == 1) {
|
|
|
|
coff_aux_weak_external *WE =
|
[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 11:54:51 +00:00
|
|
|
reinterpret_cast<coff_aux_weak_external *>(Sym.AuxData[0].Opaque);
|
2019-01-22 10:58:09 +00:00
|
|
|
const Symbol *Target = Obj.findSymbol(*Sym.WeakTargetSymbolId);
|
|
|
|
if (Target == nullptr)
|
|
|
|
return createStringError(object_error::invalid_symbol_index,
|
2019-05-22 13:23:26 +00:00
|
|
|
"symbol '%s' is missing its weak target",
|
2019-01-22 10:58:09 +00:00
|
|
|
Sym.Name.str().c_str());
|
|
|
|
WE->TagIndex = Target->RawIndex;
|
|
|
|
}
|
2019-01-19 19:42:35 +00:00
|
|
|
}
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2018-12-19 07:24:38 +00:00
|
|
|
void COFFWriter::layoutSections() {
|
2019-01-19 19:42:35 +00:00
|
|
|
for (auto &S : Obj.getMutableSections()) {
|
2018-12-19 07:24:38 +00:00
|
|
|
if (S.Header.SizeOfRawData > 0)
|
|
|
|
S.Header.PointerToRawData = FileSize;
|
2022-11-28 12:36:36 +02:00
|
|
|
else
|
|
|
|
S.Header.PointerToRawData = 0;
|
2018-12-19 07:24:38 +00:00
|
|
|
FileSize += S.Header.SizeOfRawData; // For executables, this is already
|
|
|
|
// aligned to FileAlignment.
|
2019-11-14 09:50:36 -08:00
|
|
|
if (S.Relocs.size() >= 0xffff) {
|
|
|
|
S.Header.Characteristics |= COFF::IMAGE_SCN_LNK_NRELOC_OVFL;
|
|
|
|
S.Header.NumberOfRelocations = 0xffff;
|
|
|
|
S.Header.PointerToRelocations = FileSize;
|
|
|
|
FileSize += sizeof(coff_relocation);
|
|
|
|
} else {
|
|
|
|
S.Header.NumberOfRelocations = S.Relocs.size();
|
|
|
|
S.Header.PointerToRelocations = S.Relocs.size() ? FileSize : 0;
|
|
|
|
}
|
|
|
|
|
2018-12-19 07:24:38 +00:00
|
|
|
FileSize += S.Relocs.size() * sizeof(coff_relocation);
|
|
|
|
FileSize = alignTo(FileSize, FileAlignment);
|
|
|
|
|
|
|
|
if (S.Header.Characteristics & IMAGE_SCN_CNT_INITIALIZED_DATA)
|
|
|
|
SizeOfInitializedData += S.Header.SizeOfRawData;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-19 20:28:38 +02:00
|
|
|
Expected<size_t> COFFWriter::finalizeStringTable() {
|
2019-01-19 19:42:35 +00:00
|
|
|
for (const auto &S : Obj.getSections())
|
2018-12-19 07:24:38 +00:00
|
|
|
if (S.Name.size() > COFF::NameSize)
|
|
|
|
StrTabBuilder.add(S.Name);
|
|
|
|
|
2019-01-10 21:28:24 +00:00
|
|
|
for (const auto &S : Obj.getSymbols())
|
2018-12-19 07:24:38 +00:00
|
|
|
if (S.Name.size() > COFF::NameSize)
|
|
|
|
StrTabBuilder.add(S.Name);
|
|
|
|
|
|
|
|
StrTabBuilder.finalize();
|
|
|
|
|
2019-01-19 19:42:35 +00:00
|
|
|
for (auto &S : Obj.getMutableSections()) {
|
2019-07-26 17:06:41 +00:00
|
|
|
memset(S.Header.Name, 0, sizeof(S.Header.Name));
|
2022-02-19 20:28:38 +02:00
|
|
|
if (S.Name.size() <= COFF::NameSize) {
|
|
|
|
// Short names can go in the field directly.
|
2019-07-26 17:06:41 +00:00
|
|
|
memcpy(S.Header.Name, S.Name.data(), S.Name.size());
|
2022-02-19 20:28:38 +02:00
|
|
|
} else {
|
|
|
|
// Offset of the section name in the string table.
|
|
|
|
size_t Offset = StrTabBuilder.getOffset(S.Name);
|
|
|
|
if (!COFF::encodeSectionName(S.Header.Name, Offset))
|
|
|
|
return createStringError(object_error::invalid_section_index,
|
|
|
|
"COFF string table is greater than 64GB, "
|
|
|
|
"unable to encode section name offset");
|
2018-12-19 07:24:38 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-10 21:28:24 +00:00
|
|
|
for (auto &S : Obj.getMutableSymbols()) {
|
2018-12-19 07:24:38 +00:00
|
|
|
if (S.Name.size() > COFF::NameSize) {
|
|
|
|
S.Sym.Name.Offset.Zeroes = 0;
|
|
|
|
S.Sym.Name.Offset.Offset = StrTabBuilder.getOffset(S.Name);
|
|
|
|
} else {
|
|
|
|
strncpy(S.Sym.Name.ShortName, S.Name.data(), COFF::NameSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return StrTabBuilder.getSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class SymbolTy>
|
|
|
|
std::pair<size_t, size_t> COFFWriter::finalizeSymbolTable() {
|
[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 11:54:51 +00:00
|
|
|
size_t RawSymIndex = 0;
|
|
|
|
for (auto &S : Obj.getMutableSymbols()) {
|
|
|
|
// Symbols normally have NumberOfAuxSymbols set correctly all the time.
|
|
|
|
// For file symbols, we need to know the output file's symbol size to be
|
|
|
|
// able to calculate the number of slots it occupies.
|
|
|
|
if (!S.AuxFile.empty())
|
|
|
|
S.Sym.NumberOfAuxSymbols =
|
|
|
|
alignTo(S.AuxFile.size(), sizeof(SymbolTy)) / sizeof(SymbolTy);
|
|
|
|
S.RawIndex = RawSymIndex;
|
|
|
|
RawSymIndex += 1 + S.Sym.NumberOfAuxSymbols;
|
|
|
|
}
|
|
|
|
return std::make_pair(RawSymIndex * sizeof(SymbolTy), sizeof(SymbolTy));
|
2018-12-19 07:24:38 +00:00
|
|
|
}
|
|
|
|
|
2019-01-10 21:28:24 +00:00
|
|
|
Error COFFWriter::finalize(bool IsBigObj) {
|
[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 11:54:51 +00:00
|
|
|
size_t SymTabSize, SymbolSize;
|
|
|
|
std::tie(SymTabSize, SymbolSize) = IsBigObj
|
|
|
|
? finalizeSymbolTable<coff_symbol32>()
|
|
|
|
: finalizeSymbolTable<coff_symbol16>();
|
|
|
|
|
2019-01-10 21:28:24 +00:00
|
|
|
if (Error E = finalizeRelocTargets())
|
|
|
|
return E;
|
2019-01-22 10:58:09 +00:00
|
|
|
if (Error E = finalizeSymbolContents())
|
2019-01-19 19:42:35 +00:00
|
|
|
return E;
|
2019-01-10 21:28:24 +00:00
|
|
|
|
2018-12-19 07:24:38 +00:00
|
|
|
size_t SizeOfHeaders = 0;
|
|
|
|
FileAlignment = 1;
|
|
|
|
size_t PeHeaderSize = 0;
|
|
|
|
if (Obj.IsPE) {
|
|
|
|
Obj.DosHeader.AddressOfNewExeHeader =
|
|
|
|
sizeof(Obj.DosHeader) + Obj.DosStub.size();
|
|
|
|
SizeOfHeaders += Obj.DosHeader.AddressOfNewExeHeader + sizeof(PEMagic);
|
|
|
|
|
|
|
|
FileAlignment = Obj.PeHeader.FileAlignment;
|
|
|
|
Obj.PeHeader.NumberOfRvaAndSize = Obj.DataDirectories.size();
|
|
|
|
|
|
|
|
PeHeaderSize = Obj.Is64 ? sizeof(pe32plus_header) : sizeof(pe32_header);
|
|
|
|
SizeOfHeaders +=
|
|
|
|
PeHeaderSize + sizeof(data_directory) * Obj.DataDirectories.size();
|
|
|
|
}
|
2019-01-19 19:42:35 +00:00
|
|
|
Obj.CoffFileHeader.NumberOfSections = Obj.getSections().size();
|
2018-12-19 07:24:38 +00:00
|
|
|
SizeOfHeaders +=
|
|
|
|
IsBigObj ? sizeof(coff_bigobj_file_header) : sizeof(coff_file_header);
|
2019-01-19 19:42:35 +00:00
|
|
|
SizeOfHeaders += sizeof(coff_section) * Obj.getSections().size();
|
2018-12-19 07:24:38 +00:00
|
|
|
SizeOfHeaders = alignTo(SizeOfHeaders, FileAlignment);
|
|
|
|
|
|
|
|
Obj.CoffFileHeader.SizeOfOptionalHeader =
|
|
|
|
PeHeaderSize + sizeof(data_directory) * Obj.DataDirectories.size();
|
|
|
|
|
|
|
|
FileSize = SizeOfHeaders;
|
|
|
|
SizeOfInitializedData = 0;
|
|
|
|
|
|
|
|
layoutSections();
|
|
|
|
|
|
|
|
if (Obj.IsPE) {
|
|
|
|
Obj.PeHeader.SizeOfHeaders = SizeOfHeaders;
|
|
|
|
Obj.PeHeader.SizeOfInitializedData = SizeOfInitializedData;
|
|
|
|
|
2019-01-19 19:42:35 +00:00
|
|
|
if (!Obj.getSections().empty()) {
|
|
|
|
const Section &S = Obj.getSections().back();
|
2018-12-19 07:24:38 +00:00
|
|
|
Obj.PeHeader.SizeOfImage =
|
|
|
|
alignTo(S.Header.VirtualAddress + S.Header.VirtualSize,
|
|
|
|
Obj.PeHeader.SectionAlignment);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the PE header had a checksum, clear it, since it isn't valid
|
|
|
|
// any longer. (We don't calculate a new one.)
|
|
|
|
Obj.PeHeader.CheckSum = 0;
|
|
|
|
}
|
|
|
|
|
2022-02-19 20:28:38 +02:00
|
|
|
Expected<size_t> StrTabSizeOrErr = finalizeStringTable();
|
|
|
|
if (!StrTabSizeOrErr)
|
|
|
|
return StrTabSizeOrErr.takeError();
|
|
|
|
|
|
|
|
size_t StrTabSize = *StrTabSizeOrErr;
|
2018-12-19 07:24:38 +00:00
|
|
|
|
|
|
|
size_t PointerToSymbolTable = FileSize;
|
|
|
|
// StrTabSize <= 4 is the size of an empty string table, only consisting
|
|
|
|
// of the length field.
|
2019-01-11 13:47:37 +00:00
|
|
|
if (SymTabSize == 0 && StrTabSize <= 4 && Obj.IsPE) {
|
|
|
|
// For executables, don't point to the symbol table and skip writing
|
|
|
|
// the length field, if both the symbol and string tables are empty.
|
2018-12-19 07:24:38 +00:00
|
|
|
PointerToSymbolTable = 0;
|
2019-01-11 13:47:37 +00:00
|
|
|
StrTabSize = 0;
|
2018-12-19 07:24:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t NumRawSymbols = SymTabSize / SymbolSize;
|
|
|
|
Obj.CoffFileHeader.PointerToSymbolTable = PointerToSymbolTable;
|
|
|
|
Obj.CoffFileHeader.NumberOfSymbols = NumRawSymbols;
|
|
|
|
FileSize += SymTabSize + StrTabSize;
|
|
|
|
FileSize = alignTo(FileSize, FileAlignment);
|
2019-01-10 21:28:24 +00:00
|
|
|
|
|
|
|
return Error::success();
|
2018-12-19 07:24:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void COFFWriter::writeHeaders(bool IsBigObj) {
|
2020-10-24 17:35:55 +03:00
|
|
|
uint8_t *Ptr = reinterpret_cast<uint8_t *>(Buf->getBufferStart());
|
2018-12-19 07:24:38 +00:00
|
|
|
if (Obj.IsPE) {
|
|
|
|
memcpy(Ptr, &Obj.DosHeader, sizeof(Obj.DosHeader));
|
|
|
|
Ptr += sizeof(Obj.DosHeader);
|
|
|
|
memcpy(Ptr, Obj.DosStub.data(), Obj.DosStub.size());
|
|
|
|
Ptr += Obj.DosStub.size();
|
|
|
|
memcpy(Ptr, PEMagic, sizeof(PEMagic));
|
|
|
|
Ptr += sizeof(PEMagic);
|
|
|
|
}
|
|
|
|
if (!IsBigObj) {
|
|
|
|
memcpy(Ptr, &Obj.CoffFileHeader, sizeof(Obj.CoffFileHeader));
|
|
|
|
Ptr += sizeof(Obj.CoffFileHeader);
|
|
|
|
} else {
|
|
|
|
// Generate a coff_bigobj_file_header, filling it in with the values
|
|
|
|
// from Obj.CoffFileHeader. All extra fields that don't exist in
|
|
|
|
// coff_file_header can be set to hardcoded values.
|
|
|
|
coff_bigobj_file_header BigObjHeader;
|
|
|
|
BigObjHeader.Sig1 = IMAGE_FILE_MACHINE_UNKNOWN;
|
|
|
|
BigObjHeader.Sig2 = 0xffff;
|
|
|
|
BigObjHeader.Version = BigObjHeader::MinBigObjectVersion;
|
|
|
|
BigObjHeader.Machine = Obj.CoffFileHeader.Machine;
|
|
|
|
BigObjHeader.TimeDateStamp = Obj.CoffFileHeader.TimeDateStamp;
|
|
|
|
memcpy(BigObjHeader.UUID, BigObjMagic, sizeof(BigObjMagic));
|
|
|
|
BigObjHeader.unused1 = 0;
|
|
|
|
BigObjHeader.unused2 = 0;
|
|
|
|
BigObjHeader.unused3 = 0;
|
|
|
|
BigObjHeader.unused4 = 0;
|
|
|
|
// The value in Obj.CoffFileHeader.NumberOfSections is truncated, thus
|
|
|
|
// get the original one instead.
|
2019-01-19 19:42:35 +00:00
|
|
|
BigObjHeader.NumberOfSections = Obj.getSections().size();
|
2018-12-19 07:24:38 +00:00
|
|
|
BigObjHeader.PointerToSymbolTable = Obj.CoffFileHeader.PointerToSymbolTable;
|
|
|
|
BigObjHeader.NumberOfSymbols = Obj.CoffFileHeader.NumberOfSymbols;
|
|
|
|
|
|
|
|
memcpy(Ptr, &BigObjHeader, sizeof(BigObjHeader));
|
|
|
|
Ptr += sizeof(BigObjHeader);
|
|
|
|
}
|
|
|
|
if (Obj.IsPE) {
|
|
|
|
if (Obj.Is64) {
|
|
|
|
memcpy(Ptr, &Obj.PeHeader, sizeof(Obj.PeHeader));
|
|
|
|
Ptr += sizeof(Obj.PeHeader);
|
|
|
|
} else {
|
|
|
|
pe32_header PeHeader;
|
|
|
|
copyPeHeader(PeHeader, Obj.PeHeader);
|
|
|
|
// The pe32plus_header (stored in Object) lacks the BaseOfData field.
|
|
|
|
PeHeader.BaseOfData = Obj.BaseOfData;
|
|
|
|
|
|
|
|
memcpy(Ptr, &PeHeader, sizeof(PeHeader));
|
|
|
|
Ptr += sizeof(PeHeader);
|
|
|
|
}
|
|
|
|
for (const auto &DD : Obj.DataDirectories) {
|
|
|
|
memcpy(Ptr, &DD, sizeof(DD));
|
|
|
|
Ptr += sizeof(DD);
|
|
|
|
}
|
|
|
|
}
|
2019-01-19 19:42:35 +00:00
|
|
|
for (const auto &S : Obj.getSections()) {
|
2018-12-19 07:24:38 +00:00
|
|
|
memcpy(Ptr, &S.Header, sizeof(S.Header));
|
|
|
|
Ptr += sizeof(S.Header);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void COFFWriter::writeSections() {
|
2019-01-19 19:42:35 +00:00
|
|
|
for (const auto &S : Obj.getSections()) {
|
2020-10-24 17:35:55 +03:00
|
|
|
uint8_t *Ptr = reinterpret_cast<uint8_t *>(Buf->getBufferStart()) +
|
|
|
|
S.Header.PointerToRawData;
|
2019-01-23 08:25:28 +00:00
|
|
|
ArrayRef<uint8_t> Contents = S.getContents();
|
|
|
|
std::copy(Contents.begin(), Contents.end(), Ptr);
|
2018-12-19 07:24:38 +00:00
|
|
|
|
|
|
|
// For executable sections, pad the remainder of the raw data size with
|
|
|
|
// 0xcc, which is int3 on x86.
|
|
|
|
if ((S.Header.Characteristics & IMAGE_SCN_CNT_CODE) &&
|
2019-01-23 08:25:28 +00:00
|
|
|
S.Header.SizeOfRawData > Contents.size())
|
|
|
|
memset(Ptr + Contents.size(), 0xcc,
|
|
|
|
S.Header.SizeOfRawData - Contents.size());
|
2018-12-19 07:24:38 +00:00
|
|
|
|
|
|
|
Ptr += S.Header.SizeOfRawData;
|
2019-11-14 09:50:36 -08:00
|
|
|
|
|
|
|
if (S.Relocs.size() >= 0xffff) {
|
|
|
|
object::coff_relocation R;
|
|
|
|
R.VirtualAddress = S.Relocs.size() + 1;
|
|
|
|
R.SymbolTableIndex = 0;
|
|
|
|
R.Type = 0;
|
|
|
|
memcpy(Ptr, &R, sizeof(R));
|
|
|
|
Ptr += sizeof(R);
|
|
|
|
}
|
2019-01-10 21:28:24 +00:00
|
|
|
for (const auto &R : S.Relocs) {
|
|
|
|
memcpy(Ptr, &R.Reloc, sizeof(R.Reloc));
|
|
|
|
Ptr += sizeof(R.Reloc);
|
|
|
|
}
|
2018-12-19 07:24:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class SymbolTy> void COFFWriter::writeSymbolStringTables() {
|
2020-10-24 17:35:55 +03:00
|
|
|
uint8_t *Ptr = reinterpret_cast<uint8_t *>(Buf->getBufferStart()) +
|
|
|
|
Obj.CoffFileHeader.PointerToSymbolTable;
|
2019-01-10 21:28:24 +00:00
|
|
|
for (const auto &S : Obj.getSymbols()) {
|
2018-12-19 07:24:38 +00:00
|
|
|
// Convert symbols back to the right size, from coff_symbol32.
|
|
|
|
copySymbol<SymbolTy, coff_symbol32>(*reinterpret_cast<SymbolTy *>(Ptr),
|
|
|
|
S.Sym);
|
|
|
|
Ptr += sizeof(SymbolTy);
|
[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 11:54:51 +00:00
|
|
|
if (!S.AuxFile.empty()) {
|
|
|
|
// For file symbols, just write the string into the aux symbol slots,
|
|
|
|
// assuming that the unwritten parts are initialized to zero in the memory
|
|
|
|
// mapped file.
|
|
|
|
std::copy(S.AuxFile.begin(), S.AuxFile.end(), Ptr);
|
|
|
|
Ptr += S.Sym.NumberOfAuxSymbols * sizeof(SymbolTy);
|
|
|
|
} else {
|
|
|
|
// For other auxillary symbols, write their opaque payload into one symbol
|
|
|
|
// table slot each. For big object files, the symbols are larger than the
|
|
|
|
// opaque auxillary symbol struct and we leave padding at the end of each
|
|
|
|
// entry.
|
|
|
|
for (const AuxSymbol &AuxSym : S.AuxData) {
|
|
|
|
ArrayRef<uint8_t> Ref = AuxSym.getRef();
|
|
|
|
std::copy(Ref.begin(), Ref.end(), Ptr);
|
|
|
|
Ptr += sizeof(SymbolTy);
|
|
|
|
}
|
|
|
|
}
|
2018-12-19 07:24:38 +00:00
|
|
|
}
|
|
|
|
if (StrTabBuilder.getSize() > 4 || !Obj.IsPE) {
|
|
|
|
// Always write a string table in object files, even an empty one.
|
|
|
|
StrTabBuilder.write(Ptr);
|
|
|
|
Ptr += StrTabBuilder.getSize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-30 20:35:43 +00:00
|
|
|
Error COFFWriter::write(bool IsBigObj) {
|
2019-01-10 21:28:24 +00:00
|
|
|
if (Error E = finalize(IsBigObj))
|
|
|
|
return E;
|
2018-12-19 07:24:38 +00:00
|
|
|
|
2020-10-24 17:35:55 +03:00
|
|
|
Buf = WritableMemoryBuffer::getNewMemBuffer(FileSize);
|
|
|
|
if (!Buf)
|
|
|
|
return createStringError(llvm::errc::not_enough_memory,
|
|
|
|
"failed to allocate memory buffer of " +
|
|
|
|
Twine::utohexstr(FileSize) + " bytes.");
|
2018-12-19 07:24:38 +00:00
|
|
|
|
|
|
|
writeHeaders(IsBigObj);
|
|
|
|
writeSections();
|
|
|
|
if (IsBigObj)
|
|
|
|
writeSymbolStringTables<coff_symbol32>();
|
|
|
|
else
|
|
|
|
writeSymbolStringTables<coff_symbol16>();
|
|
|
|
|
|
|
|
if (Obj.IsPE)
|
2018-12-30 20:35:43 +00:00
|
|
|
if (Error E = patchDebugDirectory())
|
|
|
|
return E;
|
2018-12-19 07:24:38 +00:00
|
|
|
|
2020-10-24 17:35:55 +03:00
|
|
|
// TODO: Implement direct writing to the output stream (without intermediate
|
|
|
|
// memory buffer Buf).
|
|
|
|
Out.write(Buf->getBufferStart(), Buf->getBufferSize());
|
|
|
|
return Error::success();
|
2018-12-19 07:24:38 +00:00
|
|
|
}
|
|
|
|
|
2020-04-27 14:30:42 +03:00
|
|
|
Expected<uint32_t> COFFWriter::virtualAddressToFileAddress(uint32_t RVA) {
|
|
|
|
for (const auto &S : Obj.getSections()) {
|
|
|
|
if (RVA >= S.Header.VirtualAddress &&
|
|
|
|
RVA < S.Header.VirtualAddress + S.Header.SizeOfRawData)
|
|
|
|
return S.Header.PointerToRawData + RVA - S.Header.VirtualAddress;
|
|
|
|
}
|
|
|
|
return createStringError(object_error::parse_failed,
|
|
|
|
"debug directory payload not found");
|
|
|
|
}
|
|
|
|
|
2018-12-19 07:24:38 +00:00
|
|
|
// Locate which sections contain the debug directories, iterate over all
|
|
|
|
// the debug_directory structs in there, and set the PointerToRawData field
|
|
|
|
// in all of them, according to their new physical location in the file.
|
2018-12-30 20:35:43 +00:00
|
|
|
Error COFFWriter::patchDebugDirectory() {
|
2021-09-10 09:55:26 +01:00
|
|
|
if (Obj.DataDirectories.size() <= DEBUG_DIRECTORY)
|
2018-12-30 20:35:43 +00:00
|
|
|
return Error::success();
|
2018-12-19 07:24:38 +00:00
|
|
|
const data_directory *Dir = &Obj.DataDirectories[DEBUG_DIRECTORY];
|
|
|
|
if (Dir->Size <= 0)
|
2018-12-30 20:35:43 +00:00
|
|
|
return Error::success();
|
2019-01-19 19:42:35 +00:00
|
|
|
for (const auto &S : Obj.getSections()) {
|
2018-12-19 07:24:38 +00:00
|
|
|
if (Dir->RelativeVirtualAddress >= S.Header.VirtualAddress &&
|
|
|
|
Dir->RelativeVirtualAddress <
|
|
|
|
S.Header.VirtualAddress + S.Header.SizeOfRawData) {
|
|
|
|
if (Dir->RelativeVirtualAddress + Dir->Size >
|
|
|
|
S.Header.VirtualAddress + S.Header.SizeOfRawData)
|
2019-01-22 10:57:59 +00:00
|
|
|
return createStringError(object_error::parse_failed,
|
2019-05-22 13:23:26 +00:00
|
|
|
"debug directory extends past end of section");
|
2018-12-19 07:24:38 +00:00
|
|
|
|
|
|
|
size_t Offset = Dir->RelativeVirtualAddress - S.Header.VirtualAddress;
|
2020-10-24 17:35:55 +03:00
|
|
|
uint8_t *Ptr = reinterpret_cast<uint8_t *>(Buf->getBufferStart()) +
|
|
|
|
S.Header.PointerToRawData + Offset;
|
2018-12-19 07:24:38 +00:00
|
|
|
uint8_t *End = Ptr + Dir->Size;
|
|
|
|
while (Ptr < End) {
|
|
|
|
debug_directory *Debug = reinterpret_cast<debug_directory *>(Ptr);
|
2021-08-02 22:17:53 -07:00
|
|
|
if (Debug->PointerToRawData) {
|
|
|
|
if (Expected<uint32_t> FilePosOrErr =
|
|
|
|
virtualAddressToFileAddress(Debug->AddressOfRawData))
|
|
|
|
Debug->PointerToRawData = *FilePosOrErr;
|
|
|
|
else
|
|
|
|
return FilePosOrErr.takeError();
|
|
|
|
}
|
2020-04-27 14:30:42 +03:00
|
|
|
Ptr += sizeof(debug_directory);
|
|
|
|
Offset += sizeof(debug_directory);
|
2018-12-19 07:24:38 +00:00
|
|
|
}
|
|
|
|
// Debug directory found and patched, all done.
|
2018-12-30 20:35:43 +00:00
|
|
|
return Error::success();
|
2018-12-19 07:24:38 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-22 10:57:59 +00:00
|
|
|
return createStringError(object_error::parse_failed,
|
2019-05-22 13:23:26 +00:00
|
|
|
"debug directory not found");
|
2018-12-19 07:24:38 +00:00
|
|
|
}
|
|
|
|
|
2018-12-30 20:35:43 +00:00
|
|
|
Error COFFWriter::write() {
|
2019-01-19 19:42:35 +00:00
|
|
|
bool IsBigObj = Obj.getSections().size() > MaxNumberOfSections16;
|
2018-12-19 07:24:38 +00:00
|
|
|
if (IsBigObj && Obj.IsPE)
|
2019-01-22 10:57:59 +00:00
|
|
|
return createStringError(object_error::parse_failed,
|
2019-05-22 13:23:26 +00:00
|
|
|
"too many sections for executable");
|
2018-12-30 20:35:43 +00:00
|
|
|
return write(IsBigObj);
|
2018-12-19 07:24:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace coff
|
|
|
|
} // end namespace objcopy
|
|
|
|
} // end namespace llvm
|