RetroLinker
Linker for several 8-bit, 16-bit and 32-bit formats
Loading...
Searching...
No Matches
segment_manager.h
1#ifndef SEGMENT_MANAGER_H
2#define SEGMENT_MANAGER_H
3
4#include <fstream>
5#include <memory>
6#include <optional>
7#include <string>
8#include <variant>
9#include "format.h"
10#include "../script/script.h"
11
12namespace Script
13{
14 class List;
15 class Node;
16}
17
18namespace Linker
19{
20 class Location;
21 class Module;
22 class OutputFormat;
23 class Reader;
24 class Section;
25 class Segment;
26 class Writer;
27
31 class SegmentManager : public virtual OutputFormat
32 {
33 protected:
37 offset_t current_address = 0;
38
46 bool current_is_template = false;
52 offset_t template_counter = 0;
56 bool condition_failed = false;
61 public:
68 offset_t current_base = 0;
72 std::vector<std::shared_ptr<Segment>> segment_vector;
76 std::map<std::string, std::shared_ptr<Segment>> segment_map;
80 std::shared_ptr<Segment> current_segment;
84 std::map<std::string, Location> linker_parameters;
88 std::string linker_script;
89
90 void ClearSegmentManager();
91
95 void SetLinkScript(std::string script_file, std::map<std::string, std::string>& options) override;
96
100 std::unique_ptr<Script::List> GetScript(Module& module);
101
105 offset_t GetCurrentAddress() const;
106
110 void SetCurrentAddress(offset_t address);
111
115 void AlignCurrentAddress(offset_t align);
116
120 void SetLatestBase(offset_t address);
121
126
131 virtual void OnNewSegment(std::shared_ptr<Segment> segment);
132
136 virtual void OnCallDirective(std::string identifier);
137
142 std::shared_ptr<Segment> AppendSegment(std::string name);
143
147 void AppendSegment(std::shared_ptr<Segment> segment);
148
152 std::shared_ptr<Segment> FetchSegment(std::string name);
153
157 void AppendSection(std::shared_ptr<Section> section);
158
163 void ProcessScript(std::unique_ptr<Script::List>& directives, Module& module);
164 void ProcessAction(std::unique_ptr<Script::Node>& action, Module& module);
165 void PostProcessAction(std::unique_ptr<Script::Node>& action, Module& module);
166 void ProcessCommand(std::unique_ptr<Script::Node>& command, Module& module);
167 bool CheckPredicate(std::unique_ptr<Script::Node>& predicate, std::shared_ptr<Section> section, Module& module);
168 offset_t EvaluateExpression(std::unique_ptr<Script::Node>& expression, Module& module);
169 };
170}
171
172#endif /* SEGMENT_MANAGER_H */
Encodes an object module file as a collection of sections, symbols and relocations.
Definition module.h:24
A class that provides a general interface to setting up generation for a format.
Definition format.h:64
A helper class to collect sections into segments.
Definition segment_manager.h:32
std::vector< std::shared_ptr< Segment > > segment_vector
Ordered sequence of segments.
Definition segment_manager.h:72
void ProcessScript(std::unique_ptr< Script::List > &directives, Module &module)
Executes a parsed linker script on a module and collects segments The function OnNewSegment can be de...
Definition segment_manager.cc:164
void AlignCurrentAddress(offset_t align)
Aligns current address to alignment, using SetCurrentAddress.
Definition segment_manager.cc:96
void AppendSection(std::shared_ptr< Section > section)
Adds a new section to the current segment, sets the base to the same as the segment.
Definition segment_manager.cc:158
std::shared_ptr< Segment > AppendSegment(std::string name)
Creates a new segment and attaches it to the image.
Definition segment_manager.cc:138
bool condition_failed
Condition to signal end of current for statement.
Definition segment_manager.h:56
std::map< std::string, std::shared_ptr< Segment > > segment_map
Map of segments from their names.
Definition segment_manager.h:76
std::shared_ptr< Segment > current_segment
Currently processed segment.
Definition segment_manager.h:80
bool current_is_template
Set during evaluation of body in a for <guard> { <body> } statement.
Definition segment_manager.h:46
void FinishCurrentSegment()
Closes the current segment, sets current_segment to null.
Definition segment_manager.cc:108
virtual void OnCallDirective(std::string identifier)
Callback function for the CALL directive.
Definition segment_manager.cc:125
offset_t template_counter
Counts how many times a for statement has been invoked.
Definition segment_manager.h:52
std::string current_template_name
Records the name of the currently generating segment for a for statement.
Definition segment_manager.h:60
std::map< std::string, Location > linker_parameters
Parameters that permit customizing the linker script.
Definition segment_manager.h:84
void SetCurrentAddress(offset_t address)
Moves the current address pointer further, and if the current segment already contains data,...
Definition segment_manager.cc:80
offset_t current_address
Holds the current address value when there is no current_segment.
Definition segment_manager.h:37
void SetLatestBase(offset_t address)
Sets the base of the current section (the value from which offsets are counted from)
Definition segment_manager.cc:101
offset_t GetCurrentAddress() const
Retrieves current address pointer.
Definition segment_manager.cc:68
std::string linker_script
Contents of the linker script.
Definition segment_manager.h:88
offset_t current_base
The base address of the current section.
Definition segment_manager.h:68
std::unique_ptr< Script::List > GetScript(Module &module)
Compiles the linker script into an internal format.
Definition segment_manager.cc:49
bool current_is_template_head
Set during evaluation of guard in a for <guard> { <body> } statement.
Definition segment_manager.h:42
void SetLinkScript(std::string script_file, std::map< std::string, std::string > &options) override
Sets up the linker script and linker parameters.
Definition segment_manager.cc:25
virtual void OnNewSegment(std::shared_ptr< Segment > segment)
Callback function when allocating a new segment When the linker script runs, it creates segments cons...
Definition segment_manager.cc:120
std::shared_ptr< Segment > FetchSegment(std::string name)
Attempts to fetch a segment, returns null if not found.
Definition segment_manager.cc:145