Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions include/eld/Config/GeneralOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,15 @@ class GeneralOptions {

bool printGCSections() const { return BPrintGCSections; }

// --plugin-activity-file
void setPluginActivityLogFile(llvm::StringRef File) {
PluginActivityLogFile = File.str();
}

const std::optional<std::string> &getPluginActivityLogFile() const {
return PluginActivityLogFile;
}

// --ld-generated-unwind-info
void setGenUnwindInfo(bool PEnable = true) { BGenUnwindInfo = PEnable; }

Expand Down Expand Up @@ -1267,6 +1276,7 @@ class GeneralOptions {
std::string MapFile; // Mapfile
std::string TarFile; // --reproduce output tarfile name
std::string TimingStatsFile;
std::optional<std::string> PluginActivityLogFile; // --plugin-activity-file output path
std::string MappingFileName; // --Mapping-file
std::string MappingDumpFile; // --dump-mapping-file
std::string ResponseDumpFile; // --dump-response-file
Expand Down
55 changes: 55 additions & 0 deletions include/eld/Core/LinkState.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//===- LinkState.h--------------------------------------------------------===//
// Part of the eld Project, under the BSD License
// See https://github.com/qualcomm/eld/LICENSE.txt for license information.
// SPDX-License-Identifier: BSD-3-Clause
//===----------------------------------------------------------------------===//
#ifndef ELD_CORE_LINKSTATE_H
#define ELD_CORE_LINKSTATE_H

#include "llvm/ADT/StringRef.h"
#include "llvm/Support/ErrorHandling.h"
namespace eld {
/// Stages of the link process.
/// What actions a plugin can perform depends on the
/// link state. For example:
/// - Plugins can change the output section of an input
/// section only in BeforeLayout link state.
/// - Plugins can move chunks from one output section to
/// another only in CreatingSections link state.
/// - Plugins can only compute the output image layout checkum using
/// `LinkerWrapper::getImageLayoutChecksum` only in AfterLayout linker
/// state.
/// - Plugins can reassign virtual addresses using
/// `LinkerWrapper::reassignVirtualAddresses()` only in CreatingSegments
/// link state.
///
/// Plugin authors should ensure that the action being performed by the
/// plugin is meaningful in the link state in which it is executed.
/// Executing invalid actions for a link state can result in undefined
/// behavior.
enum LinkState : uint8_t {
Unknown,
Initializing,
BeforeLayout,
CreatingSections,
CreatingSegments,
AfterLayout,
};

static inline llvm::StringRef getLinkStateStrRef(LinkState State) {
#define ADD_CASE(S) \
case LinkState::S: \
return #S;
switch (State) {
ADD_CASE(Unknown)
ADD_CASE(Initializing)
ADD_CASE(BeforeLayout)
ADD_CASE(CreatingSections)
ADD_CASE(CreatingSegments)
ADD_CASE(AfterLayout)
}
#undef ADD_CASE
llvm_unreachable("Invalid LinkState");
}
} // namespace eld
#endif
47 changes: 16 additions & 31 deletions include/eld/Core/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@

#include "eld/Config/GeneralOptions.h"
#include "eld/Core/Linker.h"
#include "eld/Core/LinkState.h"
#include "eld/Input/InputFile.h"
#include "eld/LayoutMap/LayoutInfo.h"
#include "eld/Plugin/PluginActivityLog.h"
#include "eld/Plugin/PluginManager.h"
#include "eld/PluginAPI/LinkerWrapper.h"
#include "eld/Script/StrToken.h"
Expand Down Expand Up @@ -141,33 +143,6 @@ class Module {

typedef std::pair<uint64_t, uint64_t> DynamicListStartEndIndexPair;

/// Stages of the link process.
/// What actions a plugin can perform depends on the
/// link state. For example:
/// - Plugins can change the output section of an input
/// section only in BeforeLayout link state.
/// - Plugins can move chunks from one output section to
/// another only in CreatingSections link state.
/// - Plugins can only compute the output image layout checkum using
/// `LinkerWrapper::getImageLayoutChecksum` only in AfterLayout linker
/// state.
/// - Plugins can reassign virtual addresses using
/// `LinkerWrapper::reassignVirtualAddresses()` only in CreatingSegments
/// link state.
///
/// Plugin authors should ensure that the action being performed by the
/// plugin is meaningful in the link state in which it is executed.
/// Executing invalid actions for a link state can result in undefined
/// behavior.
enum LinkState : uint8_t {
Unknown,
Initializing,
BeforeLayout,
CreatingSections,
CreatingSegments,
AfterLayout,
};

public:
explicit Module(LinkerScript &CurScript, LinkerConfig &Config,
LayoutInfo *LayoutInfo);
Expand Down Expand Up @@ -638,19 +613,19 @@ class Module {
}

bool isLinkStateBeforeLayout() const {
return getState() == Module::LinkState::BeforeLayout;
return getState() == LinkState::BeforeLayout;
}

bool isLinkStateCreatingSections() const {
return getState() == Module::LinkState::CreatingSections;
return getState() == LinkState::CreatingSections;
}

bool isLinkStateCreatingSegments() const {
return getState() == Module::LinkState::CreatingSegments;
return getState() == LinkState::CreatingSegments;
}

bool isLinkStateAfterLayout() const {
return getState() == Module::LinkState::AfterLayout;
return getState() == LinkState::AfterLayout;
}

void setFragmentPaddingValue(Fragment *F, uint64_t V);
Expand All @@ -664,6 +639,14 @@ class Module {

bool isBackendInitialized() const;

void createPluginActivityLog() {
PluginActLog.emplace(ThisConfig.options(), UserLinkerScript.getPlugins());
}

std::optional<PluginActivityLog> &getPluginActivityLog() {
return PluginActLog;
}

private:
/// Verifies invariants of 'CreatingSections' linker state.
/// Invariants here means the conditions and rules that 'CreatingSections'
Expand Down Expand Up @@ -744,6 +727,8 @@ class Module {
llvm::DenseMap<Fragment *, uint64_t> FragmentPaddingValues;
PluginManager PM;
NamePool SymbolNamePool;

std::optional<PluginActivityLog> PluginActLog;
};

} // namespace eld
Expand Down
6 changes: 6 additions & 0 deletions include/eld/Driver/GnuLinkerOptions.td
Original file line number Diff line number Diff line change
Expand Up @@ -1173,6 +1173,12 @@ def noDefaultPlugins
HelpText<"Allow no plugins to be implicitly loaded">,
Group<grp_pluginopts>;

defm PluginActivityFile
: smDashWithOpt<"plugin-activity-file", "PluginActivityFile",
"Emit plugin activity JSON files">,
MetaVarName<"<filename>">,
Group<grp_pluginopts>;

//===----------------------------------------------------------------------===//
/// Other Features
//===----------------------------------------------------------------------===//
Expand Down
6 changes: 6 additions & 0 deletions include/eld/Fragment/Fragment.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace eld {

class DiagnosticEngine;
class ELFSection;
class GeneralOptions;
class LinkerConfig;
class Module;
class ResolveInfo;
Expand Down Expand Up @@ -127,6 +128,11 @@ class Fragment {

bool originatesFromPlugin(const Module &Module) const;

/// Returns a string containing basic fragment information, such as, section
/// and input file name. It is intended to be used for printing to log files,
/// diagnostics, or for quick debugging.
std::string str(const GeneralOptions &Options) const;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

toString ?

it might be easy to grep for the occurence and find out in the source code, str makes it very difficult.


private:
Fragment(const Fragment &); // DO NOT IMPLEMENT
Fragment &operator=(const Fragment &); // DO NOT IMPLEMENT
Expand Down
83 changes: 83 additions & 0 deletions include/eld/Plugin/PluginActivityLog.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
//===- PluginData.h--------------------------------------------------------===//
// Part of the eld Project, under the BSD License
// See https://github.com/qualcomm/eld/LICENSE.txt for license information.
// SPDX-License-Identifier: BSD-3-Clause
//===----------------------------------------------------------------------===//
#ifndef ELD_PLUGIN_PLUGINACTIVITYLOG_H
#define ELD_PLUGIN_PLUGINACTIVITYLOG_H
#include "eld/Plugin/PluginOp.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/JSON.h"
#include <functional>
#include <unordered_map>
#include <vector>

namespace eld {
class GeneralOptions;
class Plugin;

/// Records ordered plugin operations.
class PluginActivityLog {
public:
PluginActivityLog(const GeneralOptions &GO,
const std::vector<Plugin *> &pAllPlugins)
: Options(GO), AllPlugins(pAllPlugins) {}

void addPluginOperation(PluginOp &Op) {
PluginOperations.push_back(std::ref(Op));
}

/// Emit ordered plugin operations to a JSON file for analysis.
bool print(llvm::StringRef Filename) const;

void recordPluginLibrary(void *Handle, std::string Path) {
PluginLibraryHandleToPathMap[Handle] = Path;
}

std::string getPluginLibraryPath(void *LibHandle) const {
auto It = PluginLibraryHandleToPathMap.find(LibHandle);
if (It != PluginLibraryHandleToPathMap.end())
return It->second;
return "";
}

private:
// Convert a PluginOp to JSON based on its dynamic type.
llvm::json::Object toJSON(const PluginOp &Op) const;

// Overloads for each concrete PluginOp to extract a minimal JSON payload.
llvm::json::Object toJSON(const ChangeOutputSectionPluginOp &P) const;

llvm::json::Object toJSON(const AddChunkPluginOp &P) const;

llvm::json::Object toJSON(const RemoveChunkPluginOp &P) const;

llvm::json::Object toJSON(const UpdateChunksPluginOp &P) const;

llvm::json::Object toJSON(const RemoveSymbolPluginOp &P) const;

llvm::json::Object toJSON(const RelocationDataPluginOp &P) const;

llvm::json::Object toJSON(const UpdateLinkStatsPluginOp &P) const;

llvm::json::Object toJSON(const UpdateRulePluginOp &P) const;

llvm::json::Object toJSON(const ResetOffsetPluginOp &P) const;

llvm::json::Object toJSON(const UpdateLinkStateOp &P) const;

llvm::json::Object getBaseActivityJSONObject(const PluginOp &Op) const;

llvm::json::Array getPluginActivities() const;

llvm::json::Array getPluginsInfo() const;

private:
std::vector<std::reference_wrapper<PluginOp>> PluginOperations;
const GeneralOptions &Options;
const std::vector<Plugin *> &AllPlugins;
std::unordered_map<void *, std::string> PluginLibraryHandleToPathMap;
};
} // namespace eld

#endif
51 changes: 50 additions & 1 deletion include/eld/Plugin/PluginOp.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#ifndef ELD_PLUGIN_PLUGINOP_H
#define ELD_PLUGIN_PLUGINOP_H

#include "eld/Core/LinkState.h"
#include "llvm/ADT/StringRef.h"
#include <cstdint>
#include <string>

Expand Down Expand Up @@ -34,16 +36,39 @@ class PluginOp {
UpdateLinkStat,
UpdateRule,
RelocationData,
UpdateLinkState
};

explicit PluginOp(plugin::LinkerWrapper *, PluginOpType T,
std::string Annotation);

explicit PluginOp(PluginOpType T) : OpType(T) {}

PluginOpType getPluginOpType() const { return OpType; }

std::string getAnnotation() const { return Annotation; }

std::string getPluginName() const;
virtual std::string getPluginName() const;

llvm::StringRef getPluginOpTypeStrRef() const {
#define ADD_CASE(C) \
case C: \
return #C;
switch (OpType) {
ADD_CASE(ChangeOutputSection)
ADD_CASE(AddChunk)
ADD_CASE(RemoveChunk)
ADD_CASE(RemoveSymbol)
ADD_CASE(ResetOffset)
ADD_CASE(UpdateChunks)
ADD_CASE(UpdateLinkStat)
ADD_CASE(UpdateRule)
ADD_CASE(RelocationData)
ADD_CASE(UpdateLinkState)
}
#undef ADD_CASE
return "Unknown";
}

virtual std::string getPluginOpStr() const { return ""; }

Expand Down Expand Up @@ -143,6 +168,8 @@ class UpdateChunksPluginOp : public PluginOp {

RuleContainer *getRule() const { return Rule; }

Type getType() const { return T; }

private:
RuleContainer *Rule = nullptr;
Type T = Start;
Expand Down Expand Up @@ -192,6 +219,10 @@ class UpdateLinkStatsPluginOp : public PluginOp {
return P->getPluginOpType() == PluginOpType::UpdateLinkStat;
}

llvm::StringRef getStatName() const {
return StatName;
}

private:
std::string StatName;
};
Expand Down Expand Up @@ -234,6 +265,24 @@ class ResetOffsetPluginOp : public PluginOp {
uint32_t OldOffset;
};

// Pseudo plugin operation because it is not actually
// performed by a plugin, but is instead performed
// by the linker.
class UpdateLinkStateOp : public PluginOp {
public:
UpdateLinkStateOp(LinkState pNewState)
: PluginOp(PluginOpType::UpdateLinkState), NewState(pNewState) {}

LinkState getNewState() const { return NewState; }

std::string getPluginName() const override {
return "Linker";
}

private:
LinkState NewState = LinkState::Unknown;
};

} // namespace eld

#endif
2 changes: 1 addition & 1 deletion lib/Core/Linker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ bool Linker::layout() {
eld::RegisterTimer T("AfterLayout OutputSection Iterator", "Perform Layout",
ThisConfig->options().printTimingStats("plugin"));
// Run the output section iterator plugin after all the layout is done.
ThisModule->setLinkState(Module::LinkState::AfterLayout);
ThisModule->setLinkState(LinkState::AfterLayout);
Backend->finalizeLayout();

{
Expand Down
Loading