RetroLinker
Linker for several 8-bit, 16-bit and 32-bit formats
Loading...
Searching...
No Matches
coff.h
1#ifndef COFF_H
2#define COFF_H
3
4#include <map>
5#include "cpm68k.h"
6#include "mzexe.h"
7#include "../common.h"
8#include "../linker/module.h"
9#include "../linker/segment.h"
10#include "../linker/segment_manager.h"
11#include "../linker/writer.h"
12
13namespace COFF
14{
25 class COFFFormat : public virtual Linker::InputFormat, public virtual Linker::SegmentManager
26 {
27 public:
28 /* * * General members * * */
29
33 enum cpu
34 {
35 CPU_UNKNOWN = 0,
36
38
43 CPU_I386 = 0x014C,
44
49 CPU_M68K = 0x0150,
50
52
57 CPU_W65 = 0x6500,
58
63 CPU_Z80 = 0x805A,
64
69 CPU_Z8K = 0x8000,
70
72
77 CPU_I86 = 0x0148,
78
83 CPU_NS32K = 0x0154,
84
89 CPU_I370 = 0x0158,
90
95 CPU_MIPS = 0x0160,
96
101 CPU_M88K = 0x016D,
102
107 CPU_WE32K = 0x0170,
108
113 CPU_VAX = 0x0178,
114
119 CPU_AM29K = 0x017A,
120
125 CPU_ALPHA = 0x0183,
126
131 CPU_PPC = 0x01DF,
132
137 CPU_PPC64 = 0x01F7,
138
140 CPU_SHARC = 0x521C,
141
143
148 CPU_I860 = 0x014D,
149
154 CPU_SH = 0x01A2,
155
160 CPU_ARM = 0x01C0,
161
166 CPU_AM33 = 0x01D3,
167
172 CPU_IA64 = 0x0200,
173
178 CPU_HPPA = 0x0290,
179
184 CPU_EFI = 0x0EBC,
185
190 CPU_RISCV32 = 0x5032,
191
196 CPU_RISCV64 = 0x5064,
197
202 CPU_RISCV128 = 0x5128,
203
208 CPU_AMD64 = 0x8664,
209
214 CPU_M32R = 0x9041,
215
220 CPU_ARM64 = 0xAA64,
221
223 // for now, there are no overloaded values
224 };
225
226 enum COFFVariantType
227 {
228 COFF = 1,
229 ECOFF = 2,
230 XCOFF32 = 3,
231 XCOFF64 = 4,
232 PECOFF = 5,
233 };
234
236 {
237 cpu actual_cpu;
238 ::EndianType endian;
239 };
240
241 static const std::map<uint32_t, MachineType> MACHINE_TYPES;
242
243 offset_t file_size;
244
246 char signature[2] = { };
247
251 ::EndianType GetEndianType() const;
252
257 {
258 public:
259 virtual ~Relocation();
260 virtual offset_t GetAddress() const = 0;
261 virtual size_t GetSize() const = 0;
262 virtual size_t GetEntrySize() const = 0;
263 virtual void WriteFile(Linker::Writer& wr) const = 0;
264 virtual void FillEntry(Dumper::Entry& entry) const = 0;
265 };
266
271 {
272 public:
274 static constexpr uint16_t R_ABS = 0;
276 static constexpr uint16_t R_DIR16 = 1;
278 static constexpr uint16_t R_REL16 = 2;
280 static constexpr uint16_t R_DIR32 = 6;
282 static constexpr uint16_t R_DIR32NB = 7;
284 static constexpr uint16_t R_SEG12 = 9;
286 static constexpr uint16_t R_SECTION = 10;
288 static constexpr uint16_t R_SECREL = 11;
290 static constexpr uint16_t R_TOKEN = 12;
292 static constexpr uint16_t R_SECREL7 = 13;
294 static constexpr uint16_t R_REL32 = 20;
295
296 COFFVariantType coff_variant;
297 cpu cpu_type;
298
299 offset_t address = 0;
300 uint32_t symbol_index = 0;
301 uint16_t type = 0;
302 uint32_t information = 0;
303
304 UNIXRelocation(COFFVariantType coff_variant, cpu cpu_type)
305 : coff_variant(coff_variant), cpu_type(cpu_type)
306 {
307 }
308
309 void Read(Linker::Reader& rd);
310
311 offset_t GetAddress() const override;
312
313 size_t GetSize() const override;
314
315 size_t GetEntrySize() const override;
316
317 void WriteFile(Linker::Writer& wr) const override;
318
319 void FillEntry(Dumper::Entry& entry) const override;
320 };
321
326 {
327 public:
328 // https://github.com/aixoss/binutils/blob/master/include/coff/internal.h
329 // https://github.com/aixoss/binutils/blob/master/include/coff/z80.h
330 // https://github.com/aixoss/binutils/blob/master/include/coff/z8k.h
331
332 static constexpr uint16_t R_Z80_IMM8 = 0x22;
333 static constexpr uint16_t R_Z80_IMM16 = 0x01;
334 static constexpr uint16_t R_Z80_IMM24 = 0x33;
335 static constexpr uint16_t R_Z80_IMM32 = 0x17;
336 static constexpr uint16_t R_Z80_OFF8 = 0x32;
337 static constexpr uint16_t R_Z80_JR = 0x02;
338
339 static constexpr uint16_t R_Z8K_IMM4L = 0x23;
340 static constexpr uint16_t R_Z8K_IMM4H = 0x24;
341 static constexpr uint16_t R_Z8K_DISP7 = 0x25; /* djnz */
342 static constexpr uint16_t R_Z8K_IMM8 = 0x22;
343 static constexpr uint16_t R_Z8K_IMM16 = 0x01;
344 static constexpr uint16_t R_Z8K_REL16 = 0x04;
345 static constexpr uint16_t R_Z8K_IMM32 = 0x11;
346 static constexpr uint16_t R_Z8K_JR = 0x02; /* jr */
347 static constexpr uint16_t R_Z8K_CALLR = 0x05; /* callr */
348
349 static constexpr uint16_t R_W65_ABS8 = 0x01;
350 static constexpr uint16_t R_W65_ABS16 = 0x02;
351 static constexpr uint16_t R_W65_ABS24 = 0x03;
352 static constexpr uint16_t R_W65_ABS8S8 = 0x04;
353 static constexpr uint16_t R_W65_ABS8S16 = 0x05;
354 static constexpr uint16_t R_W65_ABS16S8 = 0x06;
355 static constexpr uint16_t R_W65_ABS16S16 = 0x07;
356 static constexpr uint16_t R_W65_PCR8 = 0x08;
357 static constexpr uint16_t R_W65_PCR16 = 0x09;
358 static constexpr uint16_t R_W65_DP = 0x0A;
359
360 cpu cpu_type;
361
365 uint32_t address;
369 uint32_t symbol_index;
373 uint32_t offset;
377 uint16_t type;
381 uint16_t data;
382
383 ZilogRelocation(cpu cpu_type)
384 : cpu_type(cpu_type)
385 {
386 }
387
388 void Read(Linker::Reader& rd);
389
390 offset_t GetAddress() const override;
391
392 size_t GetSize() const override;
393
394 size_t GetEntrySize() const override;
395
396 void WriteFile(Linker::Writer& wr) const override;
397
398 void FillEntry(Dumper::Entry& entry) const override;
399 };
400
404 class Symbol
405 {
406 public:
410 std::string name;
414 uint32_t name_index;
418 uint32_t value;
426 uint16_t type;
437
438 void Read(Linker::Reader& rd);
439
440 bool IsExternal() const;
441 };
442
447 {
448 public:
452 std::string name;
458 offset_t physical_address = 0;
462 offset_t address = 0;
466 offset_t size = 0;
470 offset_t section_pointer = 0;
474 offset_t relocation_pointer = 0;
482 uint32_t relocation_count = 0;
486 uint32_t line_number_count = 0;
490 uint32_t flags = 0;
491
495 std::shared_ptr<Linker::Image> image;
496
500 std::vector<std::unique_ptr<Relocation>> relocations;
501
503 enum
504 {
506 TEXT = 0x0020,
508 DATA = 0x0040,
510 BSS = 0x0080,
511 };
512
513 void Clear();
514
515 Section(uint32_t flags = 0, std::shared_ptr<Linker::Image> image = nullptr)
517 {
518 }
519
520 ~Section()
521 {
522 Clear();
523 }
524
525 void ReadSectionHeader(Linker::Reader& rd, COFFVariantType coff_variant);
526
527 void WriteSectionHeader(Linker::Writer& wr, COFFVariantType coff_variant);
528
529 uint32_t ImageSize() const;
530 };
531
535 std::vector<std::unique_ptr<Section>> sections;
536
540 uint16_t section_count = 0;
544 uint32_t timestamp = 0;
552 uint32_t symbol_count = 0;
556 std::vector<std::unique_ptr<Symbol>> symbols;
564 uint16_t flags = 0;
565
570 {
571 public:
572 virtual ~OptionalHeader();
576 virtual uint32_t GetSize() const = 0;
577 virtual void ReadFile(Linker::Reader& rd) = 0;
578 virtual void WriteFile(Linker::Writer& wr) const = 0;
584 virtual offset_t CalculateValues(COFFFormat& coff) = 0;
588 virtual void PostReadFile(COFFFormat& coff, Linker::Reader& rd);
592 virtual void PostWriteFile(const COFFFormat& coff, Linker::Writer& wr) const;
593
594 virtual void Dump(const COFFFormat& coff, Dumper::Dumper& dump) const;
595 };
596
600 std::unique_ptr<OptionalHeader> optional_header = nullptr;
601
605 std::map<uint32_t, size_t> relocations; /* CDOS68K */
606
611 {
612 public:
613 std::unique_ptr<Linker::Buffer> buffer = nullptr;
614
615 UnknownOptionalHeader(offset_t size)
616 : buffer(std::make_unique<Linker::Buffer>(size))
617 {
618 }
619
620 uint32_t GetSize() const override;
621
622 void ReadFile(Linker::Reader& rd) override;
623
624 void WriteFile(Linker::Writer& wr) const override;
625
626 offset_t CalculateValues(COFFFormat& coff) override;
627
628 void Dump(const COFFFormat& coff, Dumper::Dumper& dump) const override;
629 };
630
635 {
636 public:
640 uint16_t magic = 0;
644 uint16_t version_stamp = 0;
645
649 uint32_t code_size = 0;
653 uint32_t data_size = 0;
657 uint32_t bss_size = 0;
661 uint32_t entry_address = 0;
665 uint32_t code_address = 0;
669 uint32_t data_address = 0;
670
671 AOutHeader(uint16_t magic = 0)
672 : magic(magic)
673 {
674 }
675
676 uint32_t GetSize() const override;
677
678 void ReadFile(Linker::Reader& rd) override;
679
680 void WriteFile(Linker::Writer& wr) const override;
681
682 offset_t CalculateValues(COFFFormat& coff) override;
683
684 protected:
685 virtual void DumpFields(const COFFFormat& coff, Dumper::Dumper& dump, Dumper::Region& header_region) const;
686
687 public:
688 void Dump(const COFFFormat& coff, Dumper::Dumper& dump) const override;
689 };
690
696 {
697 public:
701 uint32_t relocations_offset = 0;
705 uint32_t stack_size = 0;
706
707 /* TODO: magic not needed for CDOS68K? */
708
709 uint32_t GetSize() const override;
710
711 void ReadFile(Linker::Reader& rd) override;
712
713 void WriteFile(Linker::Writer& wr) const override;
714
715 offset_t CalculateValues(COFFFormat& coff) override;
716
717 void PostReadFile(COFFFormat& coff, Linker::Reader& rd) override;
718
719 void PostWriteFile(const COFFFormat& coff, Linker::Writer& wr) const override;
720
721 protected:
722 void DumpFields(const COFFFormat& coff, Dumper::Dumper& dump, Dumper::Region& header_region) const override;
723 };
724
730 {
731 public:
732 /* Note: untested */
733 uint32_t info = 0;
734 uint32_t code_size = 0;
735 uint32_t data_size = 0;
736 uint32_t bss_size = 0;
737 uint32_t symbol_table_size = 0;
738 uint32_t entry_address = 0;
739 uint32_t code_relocation_size = 0;
740 uint32_t data_relocation_size = 0;
741
742 uint32_t GetSize() const override;
743
744 void ReadFile(Linker::Reader& wr) override;
745
746 void WriteFile(Linker::Writer& wr) const override;
747
748 offset_t CalculateValues(COFFFormat& coff) override;
749
750 void Dump(const COFFFormat& coff, Dumper::Dumper& dump) const override;
751 };
752
759 {
760 public:
761 // https://web.archive.org/web/20140723105157/http://www-scf.usc.edu/~csci402/ncode/coff_8h-source.html
762 /* bss_start */
763 uint32_t bss_address;
764 /* gpr_mask */
765 uint32_t gpr_mask;
766 /* cprmask */
767 uint32_t cpr_mask[4];
768 /* gp_value */
769 uint32_t gp_value;
770
771 MIPSAOutHeader(uint16_t magic = 0)
773 {
774 }
775
776 uint32_t GetSize() const override;
777
778 void ReadFile(Linker::Reader& rd) override;
779
780 void WriteFile(Linker::Writer& wr) const override;
781
782 offset_t CalculateValues(COFFFormat& coff) override;
783
784 protected:
785 void DumpFields(const COFFFormat& coff, Dumper::Dumper& dump, Dumper::Region& header_region) const override;
786 };
787
794 {
795 public:
796 static constexpr uint16_t OMAGIC = 0x0107;
797 static constexpr uint16_t NMAGIC = 0x0108;
798 static constexpr uint16_t ZMAGIC = 0x010B;
802 uint16_t magic = 0;
803 static constexpr uint16_t SYM_STAMP = 0x030D;
807 uint16_t version_stamp = SYM_STAMP;
811 uint16_t build_revision = 0;
812
816 uint64_t code_size = 0;
820 uint64_t data_size = 0;
824 uint64_t bss_size = 0;
828 uint64_t entry_address = 0;
832 uint64_t code_address = 0;
836 uint64_t data_address = 0;
840 uint64_t bss_address = 0;
842 uint32_t gpr_mask = 0;
844 uint32_t fpr_mask = 0;
846 uint64_t global_pointer = 0;
847
848 ECOFFAOutHeader(uint16_t magic = 0)
849 : magic(magic)
850 {
851 }
852
853 uint32_t GetSize() const override;
854
855 void ReadFile(Linker::Reader& rd) override;
856
857 void WriteFile(Linker::Writer& wr) const override;
858
859 offset_t CalculateValues(COFFFormat& coff) override;
860
861 void Dump(const COFFFormat& coff, Dumper::Dumper& dump) const override;
862 };
863
870 {
871 public:
873 bool is64 = false;
877 uint16_t magic = 0;
881 uint16_t version_stamp = 0;
885 uint64_t code_size = 0;
889 uint64_t data_size = 0;
893 uint64_t bss_size = 0;
897 uint64_t entry_address = 0;
901 uint64_t code_address = 0;
905 uint64_t data_address = 0;
909 uint64_t toc_address = 0;
913 uint16_t entry_section = 0;
917 uint16_t code_section = 0;
921 uint16_t data_section = 0;
925 uint16_t toc_section = 0;
929 uint16_t loader_section = 0;
933 uint16_t bss_section = 0;
937 uint16_t code_align = 0;
941 uint16_t data_align = 0;
945 uint16_t module_type = 0;
949 uint8_t cpu_flags = 0;
953 uint8_t cpu_type = 0;
957 uint64_t maximum_stack_size = 0;
961 uint64_t maximum_data_size = 0;
965 uint32_t debugger_data = 0;
969 uint8_t code_page_size = 0;
973 uint8_t text_page_size = 0;
977 uint8_t stack_page_size = 0;
981 uint8_t flags = 0;
985 uint16_t tdata_section = 0;
989 uint16_t tbss_section = 0;
993 uint32_t xcoff64_flags = 0;
994#if 0
998 uint8_t shared_memory_page = 0;
999#endif
1000
1001 XCOFFAOutHeader(bool is64, uint16_t magic = 0)
1002 : is64(is64), magic(magic)
1003 {
1004 }
1005
1006 uint32_t GetSize() const override;
1007
1008 void ReadFile(Linker::Reader& rd) override;
1009
1010 void WriteFile(Linker::Writer& wr) const override;
1011
1012 offset_t CalculateValues(COFFFormat& coff) override;
1013
1014 void Dump(const COFFFormat& coff, Dumper::Dumper& dump) const override;
1015 };
1016
1017 void Clear() override;
1018
1019 void AssignMagicValue();
1020
1021 COFFVariantType coff_variant = COFFVariantType(0);
1022
1028 cpu cpu_type = CPU_UNKNOWN;
1029
1033 ::EndianType endiantype = ::UndefinedEndian;
1034
1035 bool DetectCpuType(::EndianType expected);
1036
1037 void DetectCpuType();
1038
1039 void ReadFile(Linker::Reader& rd) override;
1040
1041 protected:
1042 void ReadCOFFHeader(Linker::Reader& rd);
1043 void ReadOptionalHeader(Linker::Reader& rd);
1044 void ReadRestOfFile(Linker::Reader& rd);
1045
1046 public:
1047 offset_t ImageSize() const override;
1048
1050 offset_t WriteFile(Linker::Writer& wr) const override;
1051
1052 protected:
1053 offset_t WriteFileContents(Linker::Writer& wr) const;
1054
1055 public:
1056 void Dump(Dumper::Dumper& dump) const override;
1057
1058 /* * * Reader members * * */
1059
1060 void SetupOptions(std::shared_ptr<Linker::OutputFormat> format) override;
1061
1062 bool option_segmentation = false;
1063
1064 bool FormatRequiresDataStreamFix() const override;
1065
1066 private:
1067 /* symbols */
1068 std::string segment_prefix();
1069
1070 std::string segment_of_prefix();
1071
1075 std::string segmented_address_prefix();
1076
1077#if 0
1078 // TODO: can this be used?
1079 std::string segment_difference_prefix();
1080#endif
1081
1082 enum
1083 {
1084 /* section number */
1085 N_UNDEF = 0,
1086 N_ABS = 0xFFFF,
1087 N_DEBUG = 0xFFFE,
1088
1089 /* storage class */
1090 C_EXT = 2,
1091 C_STAT = 3,
1092 C_LABEL = 6,
1093 };
1094
1095 public:
1097 void GenerateModule(Linker::Module& module) const override;
1098
1099 /* * * Writer members * * */
1100
1101 // for DJGPP binaries
1102 mutable Microsoft::MZSimpleStubWriter stub;
1103
1130
1135
1139 uint32_t stub_size = 0;
1140
1144 std::shared_ptr<Linker::Segment> stack;
1148 uint32_t entry_address = 0; /* TODO */
1153
1154 COFFFormat(format_type type = GENERIC, COFFVariantType coff_variant = COFF)
1155 : coff_variant(coff_variant), type(type)
1156 {
1157 }
1158
1159 ~COFFFormat()
1160 {
1161 Clear();
1162 }
1163
1164 unsigned FormatAdditionalSectionFlags(std::string section_name) const override;
1165
1166 static std::shared_ptr<COFFFormat> CreateWriter(format_type type);
1167
1168 void SetOptions(std::map<std::string, std::string>& options) override;
1169
1171 enum
1172 {
1173 FLAG_NO_RELOCATIONS = 0x0001,
1174 FLAG_EXECUTABLE = 0x0002,
1175 FLAG_NO_LINE_NUMBERS = 0x0004,
1176 FLAG_NO_SYMBOLS = 0x0008,
1177 FLAG_32BIT_LITTLE_ENDIAN = 0x0100,
1178 FLAG_32BIT_BIG_ENDIAN = 0x0200,
1179
1183 ZMAGIC = 0x010B,
1184 };
1185
1186 void OnNewSegment(std::shared_ptr<Linker::Segment> segment) override;
1187
1188 void CreateDefaultSegments();
1189
1190 std::unique_ptr<Script::List> GetScript(Linker::Module& module);
1191
1192 void Link(Linker::Module& module);
1193
1195 std::shared_ptr<Linker::Segment> GetSegment(std::unique_ptr<Section>& section);
1196
1197 std::shared_ptr<Linker::Segment> GetCodeSegment();
1198
1199 std::shared_ptr<Linker::Segment> GetDataSegment();
1200
1201 std::shared_ptr<Linker::Segment> GetBssSegment();
1202
1203 void ProcessModule(Linker::Module& module) override;
1204
1205 void CalculateValues() override;
1206
1207 void GenerateFile(std::string filename, Linker::Module& module) override;
1208
1210 std::string GetDefaultExtension(Linker::Module& module, std::string filename) const override;
1211 };
1212
1213}
1214
1215#endif /* COFF_H */
A standard 28 byte a.out optional header, used by DJGPP.
Definition coff.h:635
uint32_t code_address
unused (COFF name: text_start)
Definition coff.h:665
uint16_t version_stamp
unused (COFF name: vstamp)
Definition coff.h:644
uint32_t entry_address
Initial value of eip (COFF name: entry)
Definition coff.h:661
uint16_t magic
Type of executable, most typically ZMAGIC (COFF name: magic)
Definition coff.h:640
uint32_t code_size
unused (COFF name: tsize)
Definition coff.h:649
uint32_t GetSize() const override
Returns size of optional header.
Definition coff.cc:590
uint32_t data_size
unused (COFF name: dsize)
Definition coff.h:653
uint32_t bss_size
unused (COFF name: bsize)
Definition coff.h:657
uint32_t data_address
unused (COFF name: data_start)
Definition coff.h:669
ECOFF optional header.
Definition coff.h:794
uint64_t bss_address
Base address for bss segment (ECOFF name: bss_start)
Definition coff.h:840
uint32_t gpr_mask
unused (ECOFF name: gprmask)
Definition coff.h:842
uint16_t magic
Type of executable.
Definition coff.h:802
uint64_t data_size
Size of data segment (ECOFF name: dsize)
Definition coff.h:820
uint32_t GetSize() const override
Returns size of optional header.
Definition coff.cc:824
uint16_t version_stamp
Object file version stamp (ECOFF name: vstamp)
Definition coff.h:807
uint64_t bss_size
Size of bss segment (ECOFF name: bsize)
Definition coff.h:824
uint32_t fpr_mask
unused (ECOFF name: fprmask)
Definition coff.h:844
uint64_t global_pointer
Initial global pointer value (ECOFF name: gp_value)
Definition coff.h:846
uint64_t data_address
Base address for data segment (ECOFF name: data_start)
Definition coff.h:836
uint64_t code_address
Base address for code segment (ECOFF name: text_start)
Definition coff.h:832
uint16_t build_revision
Revision build of system tools (ECOFF name: bldrev)
Definition coff.h:811
uint64_t code_size
Size of code segment (ECOFF name: tsize)
Definition coff.h:816
uint64_t entry_address
Virtual address of execution start (ECOFF name: entry)
Definition coff.h:828
Concurrent DOS 68K/FlexOS 386 optional header Concurrent DOS 68K uses the typical a....
Definition coff.h:696
uint32_t GetSize() const override
Returns size of optional header.
Definition coff.cc:671
void PostReadFile(COFFFormat &coff, Linker::Reader &rd) override
Retrieves any additional data from the file corresponding to this type of optional header.
Definition coff.cc:698
void PostWriteFile(const COFFFormat &coff, Linker::Writer &wr) const override
Stores any additional data in the file corresponding to this type of optional header.
Definition coff.cc:703
uint32_t stack_size
Size of stack for execution.
Definition coff.h:705
uint32_t relocations_offset
The offset to the crunched relocation data within the file.
Definition coff.h:701
GNU a.out optional header TODO.
Definition coff.h:730
uint32_t GetSize() const override
Returns size of optional header.
Definition coff.cc:717
56 byte long header used on MIPS
Definition coff.h:759
uint32_t GetSize() const override
Returns size of optional header.
Definition coff.cc:778
An abstract class to represent the optional header.
Definition coff.h:570
virtual uint32_t GetSize() const =0
Returns size of optional header.
virtual offset_t CalculateValues(COFFFormat &coff)=0
Sets up fields to be consistent.
virtual void PostReadFile(COFFFormat &coff, Linker::Reader &rd)
Retrieves any additional data from the file corresponding to this type of optional header.
Definition coff.cc:553
virtual void PostWriteFile(const COFFFormat &coff, Linker::Writer &wr) const
Stores any additional data in the file corresponding to this type of optional header.
Definition coff.cc:557
A generic COFF relocation.
Definition coff.h:257
A COFF section.
Definition coff.h:447
std::string name
The name of the section (COFF name: s_name)
Definition coff.h:452
offset_t relocation_pointer
Offset to COFF relocations (COFF name: s_relptr)
Definition coff.h:474
uint32_t relocation_count
COFF relocation count (COFF name: s_nreloc)
Definition coff.h:482
offset_t size
The size of the section (COFF name: s_size)
Definition coff.h:466
offset_t address
The virtual address of the section (COFF name: s_vaddr)
Definition coff.h:462
std::vector< std::unique_ptr< Relocation > > relocations
Collection of COFF relocations.
Definition coff.h:500
offset_t line_number_pointer
unused (COFF name: s_lnnoptr)
Definition coff.h:478
@ DATA
Section contains initialized data (COFF name: STYP_DATA)
Definition coff.h:508
@ BSS
Section contains uninitialized data (COFF name: STYP_BSS)
Definition coff.h:510
@ TEXT
Section contains executable (COFF name: STYP_TEXT)
Definition coff.h:506
offset_t section_pointer
Offset of stored image data from COFF header start (COFF name: s_scnptr)
Definition coff.h:470
offset_t physical_address
The physical address of the section (expected to be identical to the virtual address) (COFF name: s_p...
Definition coff.h:458
std::shared_ptr< Linker::Image > image
The stored image data.
Definition coff.h:495
uint32_t line_number_count
unused (COFF name: s_nlnno)
Definition coff.h:486
uint32_t flags
COFF section flags, determines the type of the section (text, data, bss, etc.) (COFF name: s_flags)
Definition coff.h:490
A COFF symbol.
Definition coff.h:405
std::string name
Symbol name (COFF name: n_name, if it fits inside field)
Definition coff.h:410
uint16_t section_number
The number of the section, with special values 0 (N_UNDEF), 1 (N_ABS) and 2 (N_DEBUG) (COFF name: n_s...
Definition coff.h:422
uint16_t type
The symbol type (COFF name: n_type)
Definition coff.h:426
uint8_t storage_class
COFF name: n_sclass, typical values are 2 (C_EXT), 3 (C_STAT)
Definition coff.h:432
uint32_t name_index
The index of the symbol name within the string table, if not stored directly in the entry,...
Definition coff.h:414
uint8_t auxiliary_count
Signifies how many extra entries are present, these should be skipped, 0 is a typical value (COFF nam...
Definition coff.h:436
uint32_t value
The actual value of the symbol (COFF name: n_value)
Definition coff.h:418
The standard UNIX COFF relocation format.
Definition coff.h:271
static constexpr uint16_t R_SECTION
(Microsoft, debugging) 16-bit section index
Definition coff.h:286
static constexpr uint16_t R_REL16
16-bit relative address of symbol
Definition coff.h:278
static constexpr uint16_t R_ABS
No relocation.
Definition coff.h:274
static constexpr uint16_t R_DIR32NB
(Microsoft COFF) 32-bit relative virtual address of symbol
Definition coff.h:282
static constexpr uint16_t R_SECREL7
(Microsoft) 7-bit offset from section base
Definition coff.h:292
static constexpr uint16_t R_REL32
32-bit relative address of symbol
Definition coff.h:294
static constexpr uint16_t R_SEG12
(Intel x86) 16-bit segment selector of symbol
Definition coff.h:284
static constexpr uint16_t R_SECREL
(Microsoft, debugging) 32-bit offset from section start
Definition coff.h:288
static constexpr uint16_t R_TOKEN
(Microsoft) CLR token
Definition coff.h:290
static constexpr uint16_t R_DIR16
16-bit virtual address of symbol
Definition coff.h:276
static constexpr uint16_t R_DIR32
32-bit virtual address of symbol
Definition coff.h:280
A simplified class to represent an optional header of unknown structure.
Definition coff.h:611
uint32_t GetSize() const override
Returns size of optional header.
Definition coff.cc:565
XCOFF optional header.
Definition coff.h:870
uint16_t code_align
Maximum alignment of text (XCOFF name: algntext)
Definition coff.h:937
uint16_t version_stamp
Object file version stamp (XCOFF name: vstamp)
Definition coff.h:881
bool is64
False for XCOFF32, true for XCOFF64.
Definition coff.h:873
uint64_t maximum_stack_size
Maximum stack size (XCOFF name: maxstack)
Definition coff.h:957
uint16_t magic
Type of executable (XCOFF name: mflag)
Definition coff.h:877
uint64_t data_size
Size of data segment (XCOFF name: dsize)
Definition coff.h:889
uint32_t xcoff64_flags
XCOFF64 specific flags (XCOFF64 name: x64flags)
Definition coff.h:993
uint16_t toc_section
Section number for TOC (XCOFF name: sntoc)
Definition coff.h:925
uint32_t debugger_data
Reserved for debugger (XCOFF name: debugger)
Definition coff.h:965
uint64_t code_size
Size of code segment (XCOFF name: tsize)
Definition coff.h:885
uint16_t entry_section
Section number for entry point (XCOFF name: snentry)
Definition coff.h:913
uint8_t text_page_size
Requested data page size (XCOFF name: datapsize)
Definition coff.h:973
uint64_t data_address
Base address for data segment (XCOFF name: data_start)
Definition coff.h:905
uint16_t tbss_section
Section number for tbss (XCOFF name: sntbss)
Definition coff.h:989
uint16_t loader_section
Section number for loader data (XCOFF name: snloader)
Definition coff.h:929
uint64_t bss_size
Size of bss segment (XCOFF name: bsize)
Definition coff.h:893
uint8_t cpu_type
CPU type (XCOFF name: cputype)
Definition coff.h:953
uint8_t stack_page_size
Requested stack page size (XCOFF name: stackpsize)
Definition coff.h:977
uint8_t cpu_flags
CPU flags (XCOFF name: cpuflag)
Definition coff.h:949
uint16_t module_type
Module type (XCOFF name: modtype)
Definition coff.h:945
uint16_t data_section
Section number for data (XCOFF name: sndata)
Definition coff.h:921
uint64_t code_address
Base address for code segment (XCOFF name: text_start)
Definition coff.h:901
uint16_t data_align
Maximum alignment of data (XCOFF name: algndata)
Definition coff.h:941
uint16_t code_section
Section number for text (XCOFF name: sntext)
Definition coff.h:917
uint64_t entry_address
Virtual address of execution start (XCOFF name: entry)
Definition coff.h:897
uint64_t toc_address
Address of TOC (XCOFF name: toc)
Definition coff.h:909
uint8_t flags
Flags.
Definition coff.h:981
uint8_t code_page_size
Requested code page size (XCOFF name: textpsize)
Definition coff.h:969
uint32_t GetSize() const override
Returns size of optional header.
Definition coff.cc:875
uint16_t bss_section
Section number for bss (XCOFF name: snbss)
Definition coff.h:933
uint16_t tdata_section
Section number for tdata (XCOFF name: sntdata)
Definition coff.h:985
uint64_t maximum_data_size
Maximum data size (XCOFF name: maxdata)
Definition coff.h:961
A relocation, as stored by the Z80/Z8000 backend.
Definition coff.h:326
uint32_t symbol_index
Index of symbol in symbol table (COFF name: r_symndx)
Definition coff.h:369
uint16_t data
unknown (COFF name: r_stuff)
Definition coff.h:381
uint32_t address
Address of the relocation (COFF name: r_vaddr)
Definition coff.h:365
uint16_t type
Type of relocation (COFF name: r_type)
Definition coff.h:377
uint32_t offset
(COFF name: r_offset)
Definition coff.h:373
The UNIX COFF file format.
Definition coff.h:26
void Clear() override
Resets all fields to their default values, deallocate memory.
Definition coff.cc:1016
void GenerateModule(Linker::Module &module) const override
Loads the information into a module object, a convenience method when there is a single module genera...
Definition coff.cc:1542
void CalculateValues() override
Intermediate step between processing module and generating output file to set up headers and manageme...
Definition coff.cc:2154
format_type
Represents the type of target system, which will determine the CPU type and several other fields.
Definition coff.h:1108
@ CDOS386
FlexOS 386 executable (unknown)
Definition coff.h:1124
@ GENERIC
An unspecified value, probably will not work.
Definition coff.h:1112
@ DJGPP
DJGPP COFF executable.
Definition coff.h:1116
@ CDOS68K
Concurrent DOS 68K executable (untested but confident)
Definition coff.h:1120
uint32_t optional_header_size
The size of the optional header (COFF: f_opthdr)
Definition coff.h:560
@ ZMAGIC
Stored as the magic of the a.out header.
Definition coff.h:1183
std::map< uint32_t, size_t > relocations
Concurrent DOS 68K requires a special block of data to represent "crunched" relocations (see CPM68KWr...
Definition coff.h:605
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition coff.cc:1410
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition coff.cc:1327
format_type type
A representation of the format to generate.
Definition coff.h:1129
char signature[2]
The actual value of the magic number (COFF name: f_magic)
Definition coff.h:246
std::vector< std::unique_ptr< Symbol > > symbols
The symbols stored inside the COFF file.
Definition coff.h:556
bool FormatRequiresDataStreamFix() const override
Whether the generated file might contain bugs that require fixing.
Definition coff.cc:1537
cpu
Represents the first 16-bit word of a COFF file.
Definition coff.h:34
@ CPU_IA64
Intel Itanium architecture, also known as IA-64.
Definition coff.h:172
@ CPU_ALPHA
DEC Alpha.
Definition coff.h:125
@ CPU_PPC
IBM PowerPC, 32-bit.
Definition coff.h:131
@ CPU_AMD64
x86-64, introduced by AMD
Definition coff.h:208
@ CPU_VAX
DEC VAX.
Definition coff.h:113
@ CPU_I86
Intel x86-16, introduced with the Intel 8086.
Definition coff.h:77
@ CPU_M32R
Mitsubishi M32R.
Definition coff.h:214
@ CPU_AM33
Matsushita AM33.
Definition coff.h:166
@ CPU_RISCV32
RISC-V 32.
Definition coff.h:190
@ CPU_Z8K
Zilog Z8000.
Definition coff.h:69
@ CPU_Z80
Zilog Z80.
Definition coff.h:63
@ CPU_I370
IBM System/370.
Definition coff.h:89
@ CPU_NS32K
National Semiconductor NS32000.
Definition coff.h:83
@ CPU_PPC64
IBM PowerPC, 64-bit.
Definition coff.h:137
@ CPU_I386
Intel x86-32, introduced with the Intel 80386.
Definition coff.h:43
@ CPU_ARM
ARM, also known as ARM32 or AArch32; also represents Thumb.
Definition coff.h:160
@ CPU_EFI
EFI bytecode.
Definition coff.h:184
@ CPU_M68K
Motorola 68000 and compatibles.
Definition coff.h:49
@ CPU_I860
Intel i860.
Definition coff.h:148
@ CPU_WE32K
AT&T Bellmac 32, Western Electric 32000 and family.
Definition coff.h:107
@ CPU_M88K
Motorola 88000.
Definition coff.h:101
@ CPU_SH
Hitachi SuperH family.
Definition coff.h:154
@ CPU_HPPA
Hewlett-Packard PA-RISC.
Definition coff.h:178
@ CPU_SHARC
SHARC from Analog Devices.
Definition coff.h:140
@ CPU_ARM64
ARM64, also known as AArch64.
Definition coff.h:220
@ CPU_MIPS
MIPS architecture.
Definition coff.h:95
@ CPU_W65
WDC 65C816.
Definition coff.h:57
@ CPU_RISCV64
RISC-V 64.
Definition coff.h:196
@ CPU_RISCV128
RISC-V 128.
Definition coff.h:202
@ CPU_AM29K
AMD 29000.
Definition coff.h:119
::EndianType endiantype
The byte order.
Definition coff.h:1033
uint32_t relocations_offset
Concurrent DOS 68K: Offset to relocations.
Definition coff.h:1152
bool option_no_relocation
Suppress relocation generation, only relevant for Concurrent DOS 68K, since the other target formats ...
Definition coff.h:1134
uint32_t stub_size
Size of MZ stub, only used for DJGPP COFF executables.
Definition coff.h:1139
std::shared_ptr< Linker::Segment > stack
Concurrent DOS 68K and FlexOS 386: The stack segment, not stored as part of any section.
Definition coff.h:1144
void SetupOptions(std::shared_ptr< Linker::OutputFormat > format) override
Initializes the reader for linking purposes.
Definition coff.cc:1532
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition coff.cc:1086
void GenerateFile(std::string filename, Linker::Module &module) override
The main function that handles processing, calculating and generating the final image.
Definition coff.cc:2229
uint16_t section_count
Section count (COFF name: f_nscns)
Definition coff.h:540
std::shared_ptr< Linker::Segment > GetSegment(std::unique_ptr< Section > &section)
Return the segment stored inside the section, note that this only works for binary generation.
Definition coff.cc:2063
uint32_t entry_address
Entry address, gets stored in optional header later.
Definition coff.h:1148
std::vector< std::unique_ptr< Section > > sections
The list of COFF sections.
Definition coff.h:535
uint32_t symbol_count
The number of symbols (COFF name: f_nsyms)
Definition coff.h:552
void SetOptions(std::map< std::string, std::string > &options) override
Passes command line parameters as settings over to format object.
Definition coff.cc:1897
void ProcessModule(Linker::Module &module) override
Processes the module object and initializes format fields.
Definition coff.cc:2098
::EndianType GetEndianType() const
Retrieves the natural byte order for the architecture.
Definition coff.cc:121
offset_t symbol_table_offset
Offset to the first symbol (COFF name: f_symptr)
Definition coff.h:548
void OnNewSegment(std::shared_ptr< Linker::Segment > segment) override
Callback function when allocating a new segment When the linker script runs, it creates segments cons...
Definition coff.cc:1905
std::string GetDefaultExtension(Linker::Module &module, std::string filename) const override
Appends a default extension to the filename.
Definition coff.cc:2267
offset_t ImageSize() const override
Retrieves size of stored data.
Definition coff.cc:1339
std::unique_ptr< OptionalHeader > optional_header
The optional header instance used for reading/writing the COFF file.
Definition coff.h:600
uint16_t flags
COFF flags, such as whether the file is executable (f_flags)
Definition coff.h:564
cpu cpu_type
The CPU type, reflected by the first 16-bit word of a COFF file.
Definition coff.h:1028
uint32_t timestamp
Time stamp, unused (COFF name: f_timdat)
Definition coff.h:544
An abstract interface that separates structure and presentation of the data inside a file.
Definition dumper.h:586
A brief record, such as a relocation or imported library.
Definition dumper.h:506
A record that represents a region within the file.
Definition dumper.h:485
offset_t WriteFile(Writer &wr) const override=0
Stores data in memory to file.
A class that provides a general interface to loading a module.
Definition format.h:166
virtual void GenerateModule(ModuleCollector &linker, std::string file_name, bool is_library=false) const
Loads the information into a module object.
Definition format.cc:180
Encodes an object module file as a collection of sections, symbols and relocations.
Definition module.h:24
virtual std::string GetDefaultExtension(Module &module, std::string filename) const
Appends a default extension to the filename.
A helper class, encapsulating functionality needed to import binary data.
Definition reader.h:16
A helper class to collect sections into segments.
Definition segment_manager.h:32
A helper class, encapsulating functionality needed to export binary data.
Definition writer.h:15
Definition mzexe.h:248
Definition coff.h:236