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
12namespace Linker
13{
14 class Position;
15 class Reader;
16 class Segment;
17
18 typedef uint32_t ResourceIdentifier_Integer;
19 typedef std::string ResourceIdentifier_String;
20 typedef std::variant<ResourceIdentifier_String, ResourceIdentifier_Integer> ResourceIdentifier;
21
27 class Section : public Buffer
28 {
29 public:
33 std::string name;
34 private:
35 /* depends on whether it is zero filled or not */
36 //std::vector<uint8_t> data; /* inherited */
37 offset_t size = 0; /* only use if ZeroFilled */
38 public:
43 {
47 Readable = 1 << 0,
51 Writable = 1 << 1,
55 Executable = 1 << 2,
66 Mergeable = 1 << 3,
73 ZeroFilled = 1 << 4, /* note: data should be empty if ZeroFilled is set */
79 Fixed = 1 << 5,
86 Resource = 1 << 6,
92 Optional = 1 << 7,
96 Stack = 1 << 8,
100 Heap = 1 << 9,
104 CustomFlag = 1 << 10,
105 };
110 private:
111 union
112 {
113 offset_t address; /* only use if fixed is true */
114 offset_t align; /* only use if fixed is false */
115 };
116 public:
128 offset_t bias = 0;
129
137 ResourceIdentifier resource_type = " ";
138 ResourceIdentifier resource_id = uint16_t(0);
139 ResourceIdentifier resource_language = uint16_t(0); // TODO: not currently used
140
146 std::weak_ptr<Segment> segment;
150 std::string collection_name;
151
152 Section(std::string name, int flags = Readable)
153 :
154 name(name),
156 align(1)
157 {
158 }
159
160 static std::shared_ptr<Section> ReadFromFile(Reader& rd, std::string name, int flags = Readable);
161 static std::shared_ptr<Section> ReadFromFile(Reader& rd, offset_t count, std::string name, int flags = Readable);
162
163 private:
164 void AlterFlags(bool state, unsigned flags_mask);
165
166 public:
172 void SetFlag(unsigned newflags);
173
174 unsigned GetFlags() const;
175
176 bool IsReadable() const;
177
178 void SetReadable(bool state);
179
180 bool IsWritable() const;
181
182 void SetWritable(bool state);
183
184 bool IsExecutable() const;
185
186 void SetExecutable(bool state);
187
188 bool IsMergeable() const;
189
190 void SetMergeable(bool state);
191
192 bool IsFixed() const;
193
194 bool IsZeroFilled() const;
195
196 virtual offset_t SetZeroFilled(bool is_zero_filled);
197
198 offset_t GetAlign() const;
199
200 void SetAlign(offset_t new_align);
201
202 offset_t GetStartAddress() const;
203
207 offset_t GetEndAddress() const;
208
218 offset_t SetAddress(offset_t new_address);
219
223 void ResetAddress(offset_t new_address);
224
225 virtual offset_t Size() const;
226
233 offset_t Expand(offset_t new_size);
234
238 offset_t RealignEnd(offset_t align);
239
240 size_t ReadData(size_t bytes, offset_t offset, void * buffer) const override;
241
247 offset_t WriteData(size_t bytes, offset_t offset, const void * buffer);
248
254 offset_t WriteWord(size_t bytes, offset_t offset, uint64_t value, EndianType endiantype);
255
261 offset_t WriteWord(size_t bytes, offset_t offset, uint64_t value);
262
268 offset_t WriteWord(size_t bytes, uint64_t value, EndianType endiantype);
269
275 offset_t WriteWord(size_t bytes, uint64_t value);
276
280 offset_t Append(const void * new_data, size_t length);
281
285 offset_t Append(const char * new_data);
286
290 offset_t Append(const Section& other);
291
295 offset_t Append(Buffer& buffer);
296
300 Position Start() const;
301
307 Position Base() const;
308
309 using Buffer::ReadFile;
315 virtual void ReadFile(std::istream& in);
316
322 void ReadFile(Reader& rd) override;
323
324 using Buffer::WriteFile;
325
335 virtual offset_t WriteFile(std::ostream& out, offset_t bytes, offset_t offset = 0) const;
336
345 offset_t WriteFile(std::ostream& out) const;
346
350 virtual void Reset();
351 };
352
353 std::ostream& operator<<(std::ostream& out, const Section& section);
354}
355
356#endif /* SECTION_H */
A buffer that can be used to read and store data from a file.
Definition buffer.h:22
offset_t WriteFile(Writer &wr, offset_t count, offset_t offset=0) const override
Writes data of non-zero filled sections.
Definition buffer.cc:53
virtual void ReadFile(Reader &rd)
Overwrites buffer data with contents of reader.
Definition buffer.cc:24
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:20
A section of data as read from an object file.
Definition section.h:28
offset_t GetEndAddress() const
Returns end address (GetStartAddress() + Size())
Definition section.cc:144
virtual void Reset()
Clear the section.
Definition section.cc:383
virtual offset_t WriteFile(std::ostream &out, offset_t bytes, offset_t offset=0) const
Writes data into file.
Definition section.cc:369
offset_t SetAddress(offset_t new_address)
For non-fixed segments, sets the starting address and makes the fixed.
Definition section.cc:149
Position Start() const
Retrieves the address of the first byte of the section.
Definition section.cc:343
Position Base() const
Retrieves the address of the start of the segment of the section.
Definition section.cc:351
virtual void ReadFile(std::istream &in)
Overwrites section data with contents of input stream.
Definition section.cc:357
offset_t Expand(offset_t new_size)
Increases the size of the section by the specified amount.
Definition section.cc:181
offset_t WriteData(size_t bytes, offset_t offset, const void *buffer)
Writes data into the section image.
Definition section.cc:236
void ResetAddress(offset_t new_address)
Forcibly alters the starting address.
Definition section.cc:167
std::string name
Name of the section.
Definition section.h:33
ResourceIdentifier resource_type
The resource type, ID and language for a resource section.
Definition section.h:137
std::weak_ptr< Segment > segment
The segment a section belongs to.
Definition section.h:146
void SetFlag(unsigned newflags)
Sets the flags of the section.
Definition section.cc:31
offset_t WriteWord(size_t bytes, offset_t offset, uint64_t value, EndianType endiantype)
Writes a value into the section image.
Definition section.cc:247
offset_t bias
Difference between the first byte of the section and the zero address associated with the section.
Definition section.h:128
std::string collection_name
Section name that collects sections.
Definition section.h:150
size_t ReadData(size_t bytes, offset_t offset, void *buffer) const override
Attempts to fill a buffer with data.
Definition section.cc:216
offset_t RealignEnd(offset_t align)
Expands the section to a size such that its end is at a specified alignment.
Definition section.cc:203
offset_t Append(const void *new_data, size_t length)
Appends data at the end of a section.
Definition section.cc:277
section_flags
Possible flags for the section.
Definition section.h:43
@ Executable
The section data can be used as instruction.
Definition section.h:55
@ Mergeable
Sections of the same name with this flag are overlayed instead of appended.
Definition section.h:66
@ Writable
The section can be written to at runtime.
Definition section.h:51
@ Stack
Stack section.
Definition section.h:96
@ Heap
Heap section.
Definition section.h:100
@ Resource
Section data represents a resource that has to be handled differently.
Definition section.h:86
@ Fixed
Section resides at a fixed address and cannot be moved.
Definition section.h:79
@ Readable
The data in the section can be read at runtime.
Definition section.h:47
@ ZeroFilled
Section is filled with zeros.
Definition section.h:73
@ Optional
Section data may be unallocated if necessary.
Definition section.h:92
@ CustomFlag
Other flags.
Definition section.h:104
section_flags flags
The type of the section.
Definition section.h:109