2015-05-29 03:09:30 +08:00
|
|
|
//===- Chunks.h -----------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLD_COFF_CHUNKS_H
|
|
|
|
#define LLD_COFF_CHUNKS_H
|
|
|
|
|
|
|
|
#include "lld/Core/LLVM.h"
|
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
|
|
#include "llvm/Object/COFF.h"
|
|
|
|
#include <map>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace lld {
|
|
|
|
namespace coff {
|
|
|
|
|
|
|
|
using llvm::COFF::ImportDirectoryTableEntry;
|
|
|
|
using llvm::object::COFFSymbolRef;
|
|
|
|
using llvm::object::SectionRef;
|
|
|
|
using llvm::object::coff_relocation;
|
|
|
|
using llvm::object::coff_section;
|
|
|
|
using llvm::sys::fs::file_magic;
|
|
|
|
|
|
|
|
class Defined;
|
|
|
|
class DefinedImportData;
|
|
|
|
class ObjectFile;
|
|
|
|
class OutputSection;
|
|
|
|
|
|
|
|
// A Chunk represents a chunk of data that will occupy space in the
|
|
|
|
// output (if the resolver chose that). It may or may not be backed by
|
|
|
|
// a section of an input file. It could be linker-created data, or
|
|
|
|
// doesn't even have actual data (if common or bss).
|
|
|
|
class Chunk {
|
|
|
|
public:
|
|
|
|
virtual ~Chunk() = default;
|
|
|
|
|
|
|
|
// Returns the size of this chunk (even if this is a common or BSS.)
|
|
|
|
virtual size_t getSize() const = 0;
|
|
|
|
|
2015-06-06 12:07:39 +08:00
|
|
|
// Write this chunk to a mmap'ed file, assuming Buf is pointing to
|
|
|
|
// beginning of the file. Because this function may use RVA values
|
|
|
|
// of other chunks for relocations, you need to set them properly
|
|
|
|
// before calling this function.
|
2015-05-29 03:45:43 +08:00
|
|
|
virtual void writeTo(uint8_t *Buf) {}
|
|
|
|
|
2015-05-29 03:09:30 +08:00
|
|
|
// The writer sets and uses the addresses.
|
|
|
|
uint64_t getRVA() { return RVA; }
|
|
|
|
uint64_t getFileOff() { return FileOff; }
|
|
|
|
uint32_t getAlign() { return Align; }
|
|
|
|
void setRVA(uint64_t V) { RVA = V; }
|
|
|
|
void setFileOff(uint64_t V) { FileOff = V; }
|
|
|
|
|
2015-05-29 03:45:43 +08:00
|
|
|
// Returns true if this has non-zero data. BSS chunks return
|
|
|
|
// false. If false is returned, the space occupied by this chunk
|
|
|
|
// will be filled with zeros.
|
2015-05-29 03:09:30 +08:00
|
|
|
virtual bool hasData() const { return true; }
|
|
|
|
|
|
|
|
// Returns readable/writable/executable bits.
|
|
|
|
virtual uint32_t getPermissions() const { return 0; }
|
|
|
|
|
|
|
|
// Returns the section name if this is a section chunk.
|
|
|
|
// It is illegal to call this function on non-section chunks.
|
|
|
|
virtual StringRef getSectionName() const {
|
|
|
|
llvm_unreachable("unimplemented getSectionName");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called if the garbage collector decides to not include this chunk
|
2015-06-08 13:43:50 +08:00
|
|
|
// in a final output. It's supposed to print out a log message to stdout.
|
2015-05-29 03:09:30 +08:00
|
|
|
// It is illegal to call this function on non-section chunks because
|
|
|
|
// only section chunks are subject of garbage collection.
|
|
|
|
virtual void printDiscardedMessage() {
|
|
|
|
llvm_unreachable("unimplemented printDiscardedMessage");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if this is a COMDAT section. Usually, it is an error
|
|
|
|
// if there are more than one defined symbols having the same name,
|
|
|
|
// but symbols at begining of COMDAT sections allowed to duplicate.
|
|
|
|
virtual bool isCOMDAT() const { return false; }
|
|
|
|
|
|
|
|
// Used by the garbage collector.
|
2015-06-10 12:21:47 +08:00
|
|
|
bool isRoot() { return Root; }
|
|
|
|
bool isLive() { return Live; }
|
|
|
|
void markLive() { if (!Live) mark(); }
|
2015-05-29 03:09:30 +08:00
|
|
|
|
|
|
|
// An output section has pointers to chunks in the section, and each
|
|
|
|
// chunk has a back pointer to an output section.
|
|
|
|
void setOutputSection(OutputSection *O) { Out = O; }
|
|
|
|
OutputSection *getOutputSection() { return Out; }
|
|
|
|
|
2015-06-15 09:23:58 +08:00
|
|
|
// Windows-specific.
|
|
|
|
// Collect all locations that contain absolute addresses for base relocations.
|
|
|
|
virtual void getBaserels(std::vector<uint32_t> *Res, Defined *ImageBase) {}
|
|
|
|
|
2015-05-29 03:09:30 +08:00
|
|
|
protected:
|
|
|
|
// The RVA of this chunk in the output. The writer sets a value.
|
|
|
|
uint64_t RVA = 0;
|
|
|
|
|
|
|
|
// The offset from beginning of the output file. The writer sets a value.
|
|
|
|
uint64_t FileOff = 0;
|
|
|
|
|
2015-06-10 12:21:47 +08:00
|
|
|
// The output section for this chunk.
|
|
|
|
OutputSection *Out = nullptr;
|
|
|
|
|
2015-05-29 03:09:30 +08:00
|
|
|
// The alignment of this chunk. The writer uses the value.
|
|
|
|
uint32_t Align = 1;
|
|
|
|
|
2015-06-10 12:21:47 +08:00
|
|
|
// Used by the garbage collector.
|
|
|
|
virtual void mark() {}
|
|
|
|
bool Live = true;
|
|
|
|
bool Root = false;
|
2015-05-29 03:09:30 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// A chunk corresponding a section of an input file.
|
|
|
|
class SectionChunk : public Chunk {
|
|
|
|
public:
|
|
|
|
SectionChunk(ObjectFile *File, const coff_section *Header,
|
|
|
|
uint32_t SectionIndex);
|
|
|
|
size_t getSize() const override { return Header->SizeOfRawData; }
|
2015-05-29 03:45:43 +08:00
|
|
|
void writeTo(uint8_t *Buf) override;
|
2015-05-29 03:09:30 +08:00
|
|
|
bool hasData() const override;
|
|
|
|
uint32_t getPermissions() const override;
|
|
|
|
StringRef getSectionName() const override { return SectionName; }
|
|
|
|
void printDiscardedMessage() override;
|
|
|
|
bool isCOMDAT() const override;
|
2015-06-15 09:23:58 +08:00
|
|
|
void getBaserels(std::vector<uint32_t> *Res, Defined *ImageBase) override;
|
2015-05-29 03:09:30 +08:00
|
|
|
|
|
|
|
// Adds COMDAT associative sections to this COMDAT section. A chunk
|
|
|
|
// and its children are treated as a group by the garbage collector.
|
|
|
|
void addAssociative(SectionChunk *Child);
|
|
|
|
|
|
|
|
private:
|
2015-06-10 12:21:47 +08:00
|
|
|
void mark() override;
|
2015-05-29 03:09:30 +08:00
|
|
|
SectionRef getSectionRef();
|
|
|
|
void applyReloc(uint8_t *Buf, const coff_relocation *Rel);
|
|
|
|
|
|
|
|
// A file this chunk was created from.
|
|
|
|
ObjectFile *File;
|
|
|
|
|
|
|
|
const coff_section *Header;
|
|
|
|
uint32_t SectionIndex;
|
|
|
|
StringRef SectionName;
|
|
|
|
std::vector<Chunk *> AssocChildren;
|
|
|
|
};
|
|
|
|
|
|
|
|
// A chunk for common symbols. Common chunks don't have actual data.
|
|
|
|
class CommonChunk : public Chunk {
|
|
|
|
public:
|
2015-06-08 11:17:07 +08:00
|
|
|
CommonChunk(const COFFSymbolRef Sym);
|
2015-05-29 03:09:30 +08:00
|
|
|
size_t getSize() const override { return Sym.getValue(); }
|
|
|
|
bool hasData() const override { return false; }
|
|
|
|
uint32_t getPermissions() const override;
|
|
|
|
StringRef getSectionName() const override { return ".bss"; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
const COFFSymbolRef Sym;
|
|
|
|
};
|
|
|
|
|
|
|
|
// A chunk for linker-created strings.
|
|
|
|
class StringChunk : public Chunk {
|
|
|
|
public:
|
2015-05-29 03:45:43 +08:00
|
|
|
explicit StringChunk(StringRef S) : Str(S) {}
|
|
|
|
size_t getSize() const override { return Str.size() + 1; }
|
|
|
|
void writeTo(uint8_t *Buf) override;
|
2015-05-29 03:09:30 +08:00
|
|
|
|
|
|
|
private:
|
2015-05-29 03:45:43 +08:00
|
|
|
StringRef Str;
|
2015-05-29 03:09:30 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static const uint8_t ImportThunkData[] = {
|
|
|
|
0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // JMP *0x0
|
|
|
|
};
|
|
|
|
|
2015-06-07 09:15:04 +08:00
|
|
|
// Windows-specific.
|
2015-05-29 03:09:30 +08:00
|
|
|
// A chunk for DLL import jump table entry. In a final output, it's
|
|
|
|
// contents will be a JMP instruction to some __imp_ symbol.
|
|
|
|
class ImportThunkChunk : public Chunk {
|
|
|
|
public:
|
|
|
|
explicit ImportThunkChunk(Defined *S) : ImpSymbol(S) {}
|
|
|
|
size_t getSize() const override { return sizeof(ImportThunkData); }
|
2015-05-29 03:45:43 +08:00
|
|
|
void writeTo(uint8_t *Buf) override;
|
2015-05-29 03:09:30 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
Defined *ImpSymbol;
|
|
|
|
};
|
|
|
|
|
2015-06-15 09:23:58 +08:00
|
|
|
// Windows-specific.
|
|
|
|
// This class represents a block in .reloc section.
|
|
|
|
// See the PE/COFF spec 5.6 for details.
|
|
|
|
class BaserelChunk : public Chunk {
|
|
|
|
public:
|
|
|
|
BaserelChunk(uint32_t Page, uint32_t *Begin, uint32_t *End);
|
|
|
|
size_t getSize() const override { return Data.size(); }
|
|
|
|
void writeTo(uint8_t *Buf) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<uint8_t> Data;
|
|
|
|
};
|
|
|
|
|
2015-05-29 03:09:30 +08:00
|
|
|
} // namespace coff
|
|
|
|
} // namespace lld
|
|
|
|
|
|
|
|
#endif
|