RetroLinker
Linker for several 8-bit, 16-bit and 32-bit formats
Loading...
Searching...
No Matches
common.h
1#ifndef COMMON_H
2#define COMMON_H
3
4#include <cassert>
5#include <cstdint>
6#include <cstddef>
7#include <map>
8#include <memory>
9#include <iostream>
10#include <string>
11
12typedef uint64_t offset_t;
13typedef int64_t relative_offset_t;
14typedef size_t number_t;
15
16enum EndianType
17{
18 UndefinedEndian,
19 LittleEndian,
20 BigEndian,
21 PDP11Endian, /* little endian within a 16-bit word, big endian between words */
22 AntiPDP11Endian,
23};
24
25static constexpr EndianType ByteSwapped(EndianType endian_type)
26{
27 switch(endian_type)
28 {
29 case UndefinedEndian:
30 return UndefinedEndian;
31 case LittleEndian:
32 return BigEndian;
33 case BigEndian:
34 return LittleEndian;
35 case PDP11Endian:
36 return AntiPDP11Endian;
37 case AntiPDP11Endian:
38 return PDP11Endian;
39 }
40 assert(false);
41}
42
43extern EndianType DefaultEndianType;
44
48size_t GetOffset(EndianType endiantype, size_t bytes, size_t index);
49
53offset_t AlignTo(offset_t value, offset_t align);
54
58uint16_t Swap16(uint16_t value);
59
63uint16_t FromLittleEndian16(uint16_t value);
64
68uint16_t FromBigEndian16(uint16_t value);
69
73uint32_t Swap32(uint32_t value);
74
78uint32_t Swap32words(uint32_t value);
79
83uint32_t FromLittleEndian32(uint32_t value);
84
88uint32_t FromBigEndian32(uint32_t value);
89
93uint32_t FromPDP11Endian32(uint32_t value);
94
98uint64_t Swap64(uint64_t value);
99
103uint64_t Swap64words(uint64_t value);
104
108uint64_t FromLittleEndian64(uint64_t value);
109
110uint64_t FromBigEndian64(uint64_t value);
111
112uint64_t FromPDP11Endian64(uint64_t value);
113
117uint64_t ReadUnsigned(size_t bytes, size_t maximum, uint8_t const * data, EndianType endiantype);
118
122int64_t SignExtend(size_t bytes, int64_t value);
123
127int64_t ReadSigned(size_t bytes, size_t maximum, uint8_t const * data, EndianType endiantype);
128
132void WriteWord(size_t bytes, size_t maximum, uint8_t * data, uint64_t value, EndianType endiantype);
133
134bool LookupOption(std::map<std::string, std::string>& options, std::string key, std::string& value);
135
137{
138 inline bool operator()(const std::string& first, const std::string& second) const
139 {
140 return std::lexicographical_compare(first.begin(), first.end(), second.begin(), second.end(),
141 [](char c1, char c2) -> bool { return std::tolower(static_cast<unsigned char>(c1)) < std::tolower(static_cast<unsigned char>(c2)); });
142 }
143};
144
150bool starts_with(std::string str, std::string start);
151
157bool ends_with(std::string str, std::string end);
158
159namespace Linker
160{
162 {
163 public:
164 std::string message;
165 Exception(std::string message) : message(message)
166 {
167 }
168 };
169
170 /* TODO: implement these properly */
171 extern std::ostream Debug;
172 extern std::ostream Warning;
173 extern std::ostream Error;
174
175 [[noreturn]] void FatalError(std::string message);
176
177 class Section;
178 class Location;
179 typedef std::map<std::shared_ptr<Section>, Location> Displacement;
180}
181
182#endif /* COMMON_H */
Definition common.h:162
Represents a single offset within a section, or an absolute location in memory if the section is null...
Definition location.h:17
A section of data as read from an object file.
Definition section.h:28
Definition common.h:137