2019-07-04 23:14:51 +08:00
|
|
|
/*
|
2020-01-12 01:25:26 +08:00
|
|
|
* Copyright (C) 2017-2020 Intel Corporation
|
2019-07-04 23:14:51 +08:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: MIT
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
#include <limits>
|
|
|
|
#include <string>
|
|
|
|
#include <type_traits>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace NEO {
|
|
|
|
|
2020-07-14 22:44:28 +08:00
|
|
|
class Device;
|
|
|
|
class GraphicsAllocation;
|
|
|
|
|
2020-01-12 01:25:26 +08:00
|
|
|
enum class SegmentType : uint32_t {
|
|
|
|
Unknown,
|
|
|
|
GlobalConstants,
|
|
|
|
GlobalVariables,
|
|
|
|
Instructions,
|
|
|
|
};
|
|
|
|
|
|
|
|
inline const char *asString(SegmentType segment) {
|
|
|
|
switch (segment) {
|
|
|
|
default:
|
|
|
|
return "UNKOWN";
|
|
|
|
case SegmentType::GlobalConstants:
|
|
|
|
return "GLOBAL_CONSTANTS";
|
|
|
|
case SegmentType::GlobalVariables:
|
|
|
|
return "GLOBAL_VARIABLES";
|
|
|
|
case SegmentType::Instructions:
|
|
|
|
return "INSTRUCTIONS";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-04 23:14:51 +08:00
|
|
|
struct SymbolInfo {
|
|
|
|
uint32_t offset = std::numeric_limits<uint32_t>::max();
|
|
|
|
uint32_t size = std::numeric_limits<uint32_t>::max();
|
2020-01-12 01:25:26 +08:00
|
|
|
SegmentType segment = SegmentType::Unknown;
|
2019-07-04 23:14:51 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct LinkerInput {
|
|
|
|
union Traits {
|
2020-01-12 01:25:26 +08:00
|
|
|
enum PointerSize : uint8_t {
|
|
|
|
Ptr32bit = 0,
|
|
|
|
Ptr64bit = 1
|
|
|
|
};
|
2019-07-04 23:14:51 +08:00
|
|
|
Traits() : packed(0) {
|
2020-01-12 01:25:26 +08:00
|
|
|
pointerSize = (sizeof(void *) == 4) ? PointerSize::Ptr32bit : PointerSize::Ptr64bit;
|
2019-07-04 23:14:51 +08:00
|
|
|
}
|
|
|
|
struct {
|
|
|
|
bool exportsGlobalVariables : 1;
|
|
|
|
bool exportsGlobalConstants : 1;
|
|
|
|
bool exportsFunctions : 1;
|
|
|
|
bool requiresPatchingOfInstructionSegments : 1;
|
2020-01-12 01:25:26 +08:00
|
|
|
bool requiresPatchingOfGlobalVariablesBuffer : 1;
|
|
|
|
bool requiresPatchingOfGlobalConstantsBuffer : 1;
|
|
|
|
uint8_t pointerSize : 1;
|
2019-07-04 23:14:51 +08:00
|
|
|
};
|
|
|
|
uint32_t packed;
|
|
|
|
};
|
|
|
|
static_assert(sizeof(Traits) == sizeof(Traits::packed), "");
|
|
|
|
|
|
|
|
struct RelocationInfo {
|
2019-11-26 05:43:21 +08:00
|
|
|
enum class Type : uint32_t {
|
|
|
|
Unknown,
|
|
|
|
Address,
|
|
|
|
AddressHigh,
|
|
|
|
AddressLow
|
|
|
|
};
|
|
|
|
|
2019-07-04 23:14:51 +08:00
|
|
|
std::string symbolName;
|
2020-01-12 01:25:26 +08:00
|
|
|
uint64_t offset = std::numeric_limits<uint64_t>::max();
|
2019-11-26 05:43:21 +08:00
|
|
|
Type type = Type::Unknown;
|
2020-01-12 01:25:26 +08:00
|
|
|
SegmentType relocationSegment = SegmentType::Unknown;
|
|
|
|
SegmentType symbolSegment = SegmentType::Unknown;
|
2019-07-04 23:14:51 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
using Relocations = std::vector<RelocationInfo>;
|
|
|
|
using SymbolMap = std::unordered_map<std::string, SymbolInfo>;
|
|
|
|
using RelocationsPerInstSegment = std::vector<Relocations>;
|
|
|
|
|
2020-01-12 01:25:26 +08:00
|
|
|
virtual ~LinkerInput() = default;
|
|
|
|
|
|
|
|
MOCKABLE_VIRTUAL bool decodeGlobalVariablesSymbolTable(const void *data, uint32_t numEntries);
|
|
|
|
MOCKABLE_VIRTUAL bool decodeExportedFunctionsSymbolTable(const void *data, uint32_t numEntries, uint32_t instructionsSegmentId);
|
|
|
|
MOCKABLE_VIRTUAL bool decodeRelocationTable(const void *data, uint32_t numEntries, uint32_t instructionsSegmentId);
|
|
|
|
void addDataRelocationInfo(const RelocationInfo &relocationInfo);
|
2019-07-04 23:14:51 +08:00
|
|
|
|
|
|
|
const Traits &getTraits() const {
|
|
|
|
return traits;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t getExportedFunctionsSegmentId() const {
|
|
|
|
return exportedFunctionsSegmentId;
|
|
|
|
}
|
|
|
|
|
|
|
|
const SymbolMap &getSymbols() const {
|
|
|
|
return symbols;
|
|
|
|
}
|
|
|
|
|
2020-01-12 01:25:26 +08:00
|
|
|
const RelocationsPerInstSegment &getRelocationsInInstructionSegments() const {
|
2019-07-04 23:14:51 +08:00
|
|
|
return relocations;
|
|
|
|
}
|
|
|
|
|
2020-01-12 01:25:26 +08:00
|
|
|
const Relocations &getDataRelocations() const {
|
|
|
|
return dataRelocations;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setPointerSize(Traits::PointerSize pointerSize) {
|
|
|
|
traits.pointerSize = pointerSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isValid() const {
|
|
|
|
return valid;
|
|
|
|
}
|
|
|
|
|
2019-07-04 23:14:51 +08:00
|
|
|
protected:
|
|
|
|
Traits traits;
|
|
|
|
SymbolMap symbols;
|
|
|
|
RelocationsPerInstSegment relocations;
|
2020-01-12 01:25:26 +08:00
|
|
|
Relocations dataRelocations;
|
2019-07-04 23:14:51 +08:00
|
|
|
int32_t exportedFunctionsSegmentId = -1;
|
2020-01-12 01:25:26 +08:00
|
|
|
bool valid = true;
|
2019-07-04 23:14:51 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Linker {
|
|
|
|
using RelocationInfo = LinkerInput::RelocationInfo;
|
|
|
|
|
2020-01-12 01:25:26 +08:00
|
|
|
struct SegmentInfo {
|
2019-07-04 23:14:51 +08:00
|
|
|
uintptr_t gpuAddress = std::numeric_limits<uintptr_t>::max();
|
|
|
|
size_t segmentSize = std::numeric_limits<size_t>::max();
|
|
|
|
};
|
|
|
|
|
|
|
|
struct PatchableSegment {
|
|
|
|
void *hostPointer = nullptr;
|
|
|
|
size_t segmentSize = std::numeric_limits<size_t>::max();
|
|
|
|
};
|
|
|
|
|
|
|
|
struct UnresolvedExternal {
|
|
|
|
RelocationInfo unresolvedRelocation;
|
|
|
|
uint32_t instructionsSegmentId = std::numeric_limits<uint32_t>::max();
|
|
|
|
bool internalError = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct RelocatedSymbol {
|
|
|
|
SymbolInfo symbol;
|
|
|
|
uintptr_t gpuAddress = std::numeric_limits<uintptr_t>::max();
|
|
|
|
};
|
|
|
|
|
|
|
|
using RelocatedSymbolsMap = std::unordered_map<std::string, RelocatedSymbol>;
|
|
|
|
using PatchableSegments = std::vector<PatchableSegment>;
|
|
|
|
using UnresolvedExternals = std::vector<UnresolvedExternal>;
|
|
|
|
|
|
|
|
Linker(const LinkerInput &data)
|
|
|
|
: data(data) {
|
|
|
|
}
|
|
|
|
|
2020-01-12 01:25:26 +08:00
|
|
|
bool link(const SegmentInfo &globalVariablesSegInfo, const SegmentInfo &globalConstantsSegInfo, const SegmentInfo &exportedFunctionsSegInfo,
|
2020-07-14 22:44:28 +08:00
|
|
|
GraphicsAllocation *globalVariablesSeg, GraphicsAllocation *globalConstantsSeg, const PatchableSegments &instructionsSegments,
|
|
|
|
UnresolvedExternals &outUnresolvedExternals, Device *pDevice, const void *constantsInitData, const void *variablesInitData) {
|
2020-01-12 01:25:26 +08:00
|
|
|
bool success = data.isValid();
|
|
|
|
success = success && processRelocations(globalVariablesSegInfo, globalConstantsSegInfo, exportedFunctionsSegInfo);
|
|
|
|
success = success && patchInstructionsSegments(instructionsSegments, outUnresolvedExternals);
|
2020-07-14 22:44:28 +08:00
|
|
|
success = success && patchDataSegments(globalVariablesSegInfo, globalConstantsSegInfo, globalVariablesSeg, globalConstantsSeg,
|
|
|
|
outUnresolvedExternals, pDevice, constantsInitData, variablesInitData);
|
2020-01-12 01:25:26 +08:00
|
|
|
|
|
|
|
return success;
|
2019-07-04 23:14:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
RelocatedSymbolsMap extractRelocatedSymbols() {
|
|
|
|
return RelocatedSymbolsMap(std::move(relocatedSymbols));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
const LinkerInput &data;
|
|
|
|
RelocatedSymbolsMap relocatedSymbols;
|
|
|
|
|
2020-01-12 01:25:26 +08:00
|
|
|
bool processRelocations(const SegmentInfo &globalVariables, const SegmentInfo &globalConstants, const SegmentInfo &exportedFunctions);
|
2019-07-04 23:14:51 +08:00
|
|
|
|
|
|
|
bool patchInstructionsSegments(const std::vector<PatchableSegment> &instructionsSegments, std::vector<UnresolvedExternal> &outUnresolvedExternals);
|
2020-01-12 01:25:26 +08:00
|
|
|
|
|
|
|
bool patchDataSegments(const SegmentInfo &globalVariablesSegInfo, const SegmentInfo &globalConstantsSegInfo,
|
2020-07-14 22:44:28 +08:00
|
|
|
GraphicsAllocation *globalVariablesSeg, GraphicsAllocation *globalConstantsSeg,
|
|
|
|
std::vector<UnresolvedExternal> &outUnresolvedExternals, Device *pDevice,
|
|
|
|
const void *constantsInitData, const void *variablesInitData);
|
2019-07-04 23:14:51 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
std::string constructLinkerErrorMessage(const Linker::UnresolvedExternals &unresolvedExternals, const std::vector<std::string> &instructionsSegmentsNames);
|
2020-04-28 20:00:15 +08:00
|
|
|
std::string constructRelocationsDebugMessage(const Linker::RelocatedSymbolsMap &relocatedSymbols);
|
2019-07-04 23:14:51 +08:00
|
|
|
|
|
|
|
} // namespace NEO
|