RetroLinker
Linker for several 8-bit, 16-bit and 32-bit formats
Loading...
Searching...
No Matches
section.h
1#ifndef SECTION_H
2#define SECTION_H
3
4#include <algorithm>
5#include <iostream>
6#include <string>
7#include <variant>
8#include <vector>
9#include "../common.h"
10#include "buffer.h"
11#include "position.h"
12#include "reader.h"
13#include "writable.h"
14
15namespace Linker
16{
17 class Segment;
18
24 class Section : public Buffer
25 {
26 public:
30 std::string name;
31 private:
32 /* depends on whether it is zero filled or not */
33 //std::vector<uint8_t> data; /* inherited */
34 offset_t size = 0; /* only use if ZeroFilled */
35 public:
40 {
44 Readable = 1 << 0,
48 Writable = 1 << 1,
52 Execable = 1 << 2,
63 Mergeable = 1 << 3,
70 ZeroFilled = 1 << 4, /* note: data should be empty if ZeroFilled is set */
76 Fixed = 1 << 5,
83 Resource = 1 << 6,
89 Optional = 1 << 7,
93 Stack = 1 << 8,
97 Heap = 1 << 9,
101 CustomFlag = 1 << 10,
102 };
107 private:
108 union
109 {
110 offset_t address; /* only use if fixed is true */
111 offset_t align; /* only use if fixed is false */
112 };
113 public:
125 offset_t bias = 0;
133 std::variant<std::string, uint16_t> resource_type = " ";
134 std::variant<std::string, uint16_t> resource_id = uint16_t(0);
135
141 std::weak_ptr<Segment> segment;
145 std::string collection_name;
146
147 Section(std::string name, int flags = Readable)
148 :
149 name(name),
151 align(1)
152 {
153 }
154
155 static std::shared_ptr<Section> ReadFromFile(Reader& rd, std::string name, int flags = Readable);
156 static std::shared_ptr<Section> ReadFromFile(Reader& rd, offset_t count, std::string name, int flags = Readable);
157
158 private:
159 void AlterFlags(bool state, unsigned flags_mask);
160
161 public:
167 void SetFlag(unsigned newflags);
168
169 unsigned GetFlags() const;
170
171 bool IsReadable() const;
172
173 void SetReadable(bool state);
174
175 bool IsWritable() const;
176
177 void SetWritable(bool state);
178
179 bool IsExecable() const;
180
181 void SetExecable(bool state);
182
183 bool IsMergeable() const;
184
185 void SetMergeable(bool state);
186
187 bool IsFixed() const;
188
189 bool IsZeroFilled() const;
190
191 offset_t SetZeroFilled(bool is_zero_filled);
192
193 offset_t GetAlign() const;
194
195 void SetAlign(offset_t new_align);
196
197 offset_t GetStartAddress() const;
198
208 offset_t SetAddress(offset_t new_address);
209
213 void ResetAddress(offset_t new_address);
214
215 offset_t Size() const;
216
223 offset_t Expand(offset_t new_size);
224
228 offset_t RealignEnd(offset_t align);
229
230 int GetByte(offset_t offset) override;
231
232 uint64_t ReadUnsigned(size_t bytes, offset_t offset, EndianType endiantype) const;
233
234 uint64_t ReadUnsigned(size_t bytes, offset_t offset) const;
235
236 int64_t ReadSigned(size_t bytes, offset_t offset, EndianType endiantype) const;
237
238 uint64_t ReadSigned(size_t bytes, offset_t offset) const;
239
240 void WriteWord(size_t bytes, offset_t offset, uint64_t value, EndianType endiantype);
241
242 void WriteWord(size_t bytes, offset_t offset, uint64_t value);
243
247 void WriteWord(size_t bytes, uint64_t value, EndianType endiantype);
248
252 void WriteWord(size_t bytes, uint64_t value);
253
257 offset_t Append(const void * new_data, size_t length);
258
262 offset_t Append(const char * new_data);
263
267 offset_t Append(const Section& other);
268
272 offset_t Append(Buffer& buffer);
273
277 Position Start() const;
278
284 Position Base() const;
285
286 using Linker::Buffer::ReadFile;
292 void ReadFile(std::istream& in);
293
299 void ReadFile(Reader& in);
300
310 offset_t WriteFile(std::ostream& out, offset_t size, offset_t offset = 0) const;
311
320 offset_t WriteFile(std::ostream& out) const;
321
325 void Reset();
326 };
327
328 std::ostream& operator<<(std::ostream& out, const Section& section);
329}
330
331#endif /* SECTION_H */
A buffer that can be used to read and store data from a file.
Definition buffer.h:22
Stores an absolute address along with the containing segment or address space.
Definition position.h:17
A helper class, encapsulating functionality needed to import binary data.
Definition reader.h:16
A section of data as read from an object file.
Definition section.h:25
void Reset()
Clear the section.
Definition section.cc:360
offset_t SetAddress(offset_t new_address)
For non-fixed segments, sets the starting address and makes the fixed.
Definition section.cc:141
Position Start() const
Retrieves the address of the first byte of the section.
Definition section.cc:320
Position Base() const
Retrieves the address of the start of the segment of the section.
Definition section.cc:328
void ReadFile(std::istream &in)
Overwrites section data with contents of input stream.
Definition section.cc:334
offset_t Expand(offset_t new_size)
Increases the size of the section by the specified amount.
Definition section.cc:173
void ResetAddress(offset_t new_address)
Forcibly alters the starting address.
Definition section.cc:159
std::string name
Name of the section.
Definition section.h:30
std::weak_ptr< Segment > segment
The segment a section belongs to.
Definition section.h:141
void SetFlag(unsigned newflags)
Sets the flags of the section.
Definition section.cc:28
offset_t bias
Difference between the first byte of the section and the zero address associated with the section.
Definition section.h:125
std::string collection_name
Section name that collects sections.
Definition section.h:145
int GetByte(offset_t offset) override
Retrieve byte at a certain offset (optional, might not be defined)
Definition section.cc:206
offset_t RealignEnd(offset_t align)
Expands the section to a size such that its end is at a specified alignment.
Definition section.cc:193
offset_t Append(const void *new_data, size_t length)
Appends data at the end of a section.
Definition section.cc:258
section_flags
Possible flags for the section.
Definition section.h:40
@ Mergeable
Sections of the same name with this flag are overlayed instead of appended.
Definition section.h:63
@ Stack
Stack section.
Definition section.h:93
@ Heap
Heap section.
Definition section.h:97
@ Resource
Section data represents a resource that has to be handled differently.
Definition section.h:83
@ Fixed
Section resides at a fixed address and cannot be moved.
Definition section.h:76
@ Execable
The section data can be used as instruction.
Definition section.h:52
@ Readable
The data in the section can be read at runtime.
Definition section.h:44
@ ZeroFilled
Section is filled with zeros.
Definition section.h:70
@ Optional
Section data may be unallocated if necessary.
Definition section.h:89
@ CustomFlag
Other flags.
Definition section.h:101
section_flags flags
The type of the section.
Definition section.h:106
std::variant< std::string, uint16_t > resource_type
The resource type and ID for a resource section.
Definition section.h:133
offset_t WriteFile(std::ostream &out, offset_t size, offset_t offset=0) const
Writes data into file.
Definition section.cc:346
Represents an abstract interface whose data can be written to a file.
Definition writable.h:15