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
23 class Section : public Buffer
24 {
25 public:
29 std::string name;
30 private:
31 /* depends on whether it is zero filled or not */
32 //std::vector<uint8_t> data; /* inherited */
33 offset_t size = 0; /* only use if ZeroFilled */
34 public:
39 {
43 Readable = 1 << 0,
47 Writable = 1 << 1,
51 Executable = 1 << 2,
62 Mergeable = 1 << 3,
69 ZeroFilled = 1 << 4, /* note: data should be empty if ZeroFilled is set */
75 Fixed = 1 << 5,
82 Resource = 1 << 6,
88 Optional = 1 << 7,
92 Stack = 1 << 8,
96 Heap = 1 << 9,
100 CustomFlag = 1 << 10,
101 };
106 private:
107 union
108 {
109 offset_t address; /* only use if fixed is true */
110 offset_t align; /* only use if fixed is false */
111 };
112 public:
124 offset_t bias = 0;
132 std::variant<std::string, uint16_t> resource_type = " ";
133 std::variant<std::string, uint16_t> resource_id = uint16_t(0);
134
140 std::weak_ptr<Segment> segment;
144 std::string collection_name;
145
146 Section(std::string name, int flags = Readable)
147 :
148 name(name),
150 align(1)
151 {
152 }
153
154 static std::shared_ptr<Section> ReadFromFile(Reader& rd, std::string name, int flags = Readable);
155 static std::shared_ptr<Section> ReadFromFile(Reader& rd, offset_t count, std::string name, int flags = Readable);
156
157 private:
158 void AlterFlags(bool state, unsigned flags_mask);
159
160 public:
166 void SetFlag(unsigned newflags);
167
168 unsigned GetFlags() const;
169
170 bool IsReadable() const;
171
172 void SetReadable(bool state);
173
174 bool IsWritable() const;
175
176 void SetWritable(bool state);
177
178 bool IsExecutable() const;
179
180 void SetExecutable(bool state);
181
182 bool IsMergeable() const;
183
184 void SetMergeable(bool state);
185
186 bool IsFixed() const;
187
188 bool IsZeroFilled() const;
189
190 offset_t SetZeroFilled(bool is_zero_filled);
191
192 offset_t GetAlign() const;
193
194 void SetAlign(offset_t new_align);
195
196 offset_t GetStartAddress() const;
197
207 offset_t SetAddress(offset_t new_address);
208
212 void ResetAddress(offset_t new_address);
213
214 offset_t Size() const;
215
222 offset_t Expand(offset_t new_size);
223
227 offset_t RealignEnd(offset_t align);
228
229 size_t ReadData(size_t bytes, offset_t offset, void * buffer) const override;
230
231 void WriteWord(size_t bytes, offset_t offset, uint64_t value, EndianType endiantype);
232
233 void WriteWord(size_t bytes, offset_t offset, uint64_t value);
234
238 void WriteWord(size_t bytes, uint64_t value, EndianType endiantype);
239
243 void WriteWord(size_t bytes, uint64_t value);
244
248 offset_t Append(const void * new_data, size_t length);
249
253 offset_t Append(const char * new_data);
254
258 offset_t Append(const Section& other);
259
263 offset_t Append(Buffer& buffer);
264
268 Position Start() const;
269
275 Position Base() const;
276
277 using Buffer::ReadFile;
283 void ReadFile(std::istream& in);
284
290 void ReadFile(Reader& rd);
291
292 using Buffer::WriteFile;
293
303 offset_t WriteFile(std::ostream& out, offset_t size, offset_t offset = 0) const;
304
313 offset_t WriteFile(std::ostream& out) const;
314
318 void Reset();
319 };
320
321 std::ostream& operator<<(std::ostream& out, const Section& section);
322}
323
324#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:43
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:24
void Reset()
Clear the section.
Definition section.cc:352
offset_t SetAddress(offset_t new_address)
For non-fixed segments, sets the starting address and makes the fixed.
Definition section.cc:144
Position Start() const
Retrieves the address of the first byte of the section.
Definition section.cc:312
Position Base() const
Retrieves the address of the start of the segment of the section.
Definition section.cc:320
void ReadFile(std::istream &in)
Overwrites section data with contents of input stream.
Definition section.cc:326
offset_t Expand(offset_t new_size)
Increases the size of the section by the specified amount.
Definition section.cc:176
void ResetAddress(offset_t new_address)
Forcibly alters the starting address.
Definition section.cc:162
std::string name
Name of the section.
Definition section.h:29
std::weak_ptr< Segment > segment
The segment a section belongs to.
Definition section.h:140
void SetFlag(unsigned newflags)
Sets the flags of the section.
Definition section.cc:31
offset_t bias
Difference between the first byte of the section and the zero address associated with the section.
Definition section.h:124
std::string collection_name
Section name that collects sections.
Definition section.h:144
size_t ReadData(size_t bytes, offset_t offset, void *buffer) const override
Attempts to fill a buffer with data.
Definition section.cc:209
offset_t RealignEnd(offset_t align)
Expands the section to a size such that its end is at a specified alignment.
Definition section.cc:196
offset_t Append(const void *new_data, size_t length)
Appends data at the end of a section.
Definition section.cc:250
section_flags
Possible flags for the section.
Definition section.h:39
@ Executable
The section data can be used as instruction.
Definition section.h:51
@ Mergeable
Sections of the same name with this flag are overlayed instead of appended.
Definition section.h:62
@ Writable
The section can be written to at runtime.
Definition section.h:47
@ Stack
Stack section.
Definition section.h:92
@ Heap
Heap section.
Definition section.h:96
@ Resource
Section data represents a resource that has to be handled differently.
Definition section.h:82
@ Fixed
Section resides at a fixed address and cannot be moved.
Definition section.h:75
@ Readable
The data in the section can be read at runtime.
Definition section.h:43
@ ZeroFilled
Section is filled with zeros.
Definition section.h:69
@ Optional
Section data may be unallocated if necessary.
Definition section.h:88
@ CustomFlag
Other flags.
Definition section.h:100
section_flags flags
The type of the section.
Definition section.h:105
std::variant< std::string, uint16_t > resource_type
The resource type and ID for a resource section.
Definition section.h:132
offset_t WriteFile(std::ostream &out, offset_t size, offset_t offset=0) const
Writes data into file.
Definition section.cc:338