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
39 bool current_is_template = false;
40 bool current_is_template_head = false;
41 offset_t template_counter = 0;
42 std::string current_template_name;
43 public:
50 offset_t current_base = 0;
54 std::vector<std::shared_ptr<Segment>> segment_vector;
58 std::map<std::string, std::shared_ptr<Segment>> segment_map;
62 std::shared_ptr<Segment> current_segment;
66 std::map<std::string, Location> linker_parameters;
70 std::string linker_script;
71
72 void ClearSegmentManager();
73
77 void SetLinkScript(std::string script_file, std::map<std::string, std::string>& options) override;
78
82 std::unique_ptr<Script::List> GetScript(Module& module);
83
87 offset_t GetCurrentAddress() const;
88
92 void SetCurrentAddress(offset_t address);
93
97 void AlignCurrentAddress(offset_t align);
98
102 void SetLatestBase(offset_t address);
103
108
113 virtual void OnNewSegment(std::shared_ptr<Segment> segment);
114
119 std::shared_ptr<Segment> AppendSegment(std::string name);
120
124 std::shared_ptr<Segment> FetchSegment(std::string name);
125
129 void AppendSection(std::shared_ptr<Section> section);
130
135 void ProcessScript(std::unique_ptr<Script::List>& directives, Module& module);
136 void ProcessAction(std::unique_ptr<Script::Node>& action, Module& module);
137 void PostProcessAction(std::unique_ptr<Script::Node>& action, Module& module);
138 void ProcessCommand(std::unique_ptr<Script::Node>& command, Module& module);
139 bool CheckPredicate(std::unique_ptr<Script::Node>& predicate, std::shared_ptr<Section> section, Module& module);
140 offset_t EvaluateExpression(std::unique_ptr<Script::Node>& expression, Module& module);
141 };
142
143 bool starts_with(std::string str, std::string start);
144 bool ends_with(std::string str, std::string end);
145}
146
147#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:61
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:54
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:151
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:145
std::shared_ptr< Segment > AppendSegment(std::string name)
Terminates the current segment (if there is one), creates a new segment and attaches it to the image.
Definition segment_manager.cc:123
std::map< std::string, std::shared_ptr< Segment > > segment_map
Map of segments from their names.
Definition segment_manager.h:58
std::shared_ptr< Segment > current_segment
Currently processed segment.
Definition segment_manager.h:62
void FinishCurrentSegment()
Closes the current segment, sets current_segment to null.
Definition segment_manager.cc:108
std::map< std::string, Location > linker_parameters
Parameters that permit customizing the linker script.
Definition segment_manager.h:66
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:70
offset_t current_base
The base address of the current section.
Definition segment_manager.h:50
std::unique_ptr< Script::List > GetScript(Module &module)
Compiles the linker script into an internal format.
Definition segment_manager.cc:49
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:118
std::shared_ptr< Segment > FetchSegment(std::string name)
Attempts to fetch a segment, returns null if not found.
Definition segment_manager.cc:132