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/options.h"
10#include "../linker/segment.h"
11#include "../linker/segment_manager.h"
12#include "../linker/writer.h"
13
14namespace COFF
15{
26 class COFFFormat : public virtual Linker::InputFormat, public virtual Linker::SegmentManager
27 {
28 public:
29 /* * * General members * * */
30
34 enum cpu
35 {
36 CPU_UNKNOWN = 0,
37
39
44 CPU_I386 = 0x014C,
45
50 CPU_M68K = 0x0150,
51
53
58 CPU_W65 = 0x6500,
59
64 CPU_Z80 = 0x805A,
65
70 CPU_Z8K = 0x8000,
71
73
78 CPU_I86 = 0x0148,
79
84 CPU_NS32K = 0x0154,
85
90 CPU_I370 = 0x0158,
91
96 CPU_MIPS = 0x0160,
97
102 CPU_M88K = 0x016D,
103
108 CPU_WE32K = 0x0170,
109
114 CPU_VAX = 0x0178,
115
120 CPU_AM29K = 0x017A,
121
126 CPU_ALPHA = 0x0183,
127
132 CPU_PPC = 0x01DF,
133
138 CPU_PPC64 = 0x01F7,
139
141 CPU_SHARC = 0x521C,
142
144
145 CPU_C5400 = 0x0098,
146 CPU_C6000 = 0x0099,
147 CPU_C5500 = 0x009C,
148 CPU_MSP430 = 0x00A0,
149
151
156 CPU_I860 = 0x014D,
157
162 CPU_SH = 0x01A2,
163
168 CPU_ARM = 0x01C0,
169
174 CPU_AM33 = 0x01D3,
175
180 CPU_IA64 = 0x0200,
181
186 CPU_HPPA = 0x0290,
187
192 CPU_EFI = 0x0EBC,
193
198 CPU_RISCV32 = 0x5032,
199
204 CPU_RISCV64 = 0x5064,
205
210 CPU_RISCV128 = 0x5128,
211
216 CPU_AMD64 = 0x8664,
217
222 CPU_M32R = 0x9041,
223
228 CPU_ARM64 = 0xAA64,
229
231 // for now, there are no overloaded values
232 };
233
236 {
238 COFF = 1,
240 ECOFF = 2,
251 };
252
253 static constexpr COFFVariantType AnyCOFFVariant = COFFVariantType(0);
254
256 {
257 cpu actual_cpu;
258 ::EndianType endian;
259 COFFVariantType coff_variant = AnyCOFFVariant;
260 };
261
262 static const std::map<uint32_t, MachineType> MACHINE_TYPES;
263
264 offset_t file_size;
265
267 char signature[2] = { };
268
272 ::EndianType GetEndianType() const;
273
278 {
279 public:
280 virtual ~Relocation();
281 virtual offset_t GetAddress() const = 0;
282 virtual size_t GetSize() const = 0;
283 virtual size_t GetEntrySize() const = 0;
284 virtual void WriteFile(Linker::Writer& wr) const = 0;
285 virtual void FillEntry(Dumper::Entry& entry) const = 0;
286 };
287
292 {
293 public:
295 static constexpr uint16_t R_ABS = 0;
297 static constexpr uint16_t R_DIR16 = 1;
299 static constexpr uint16_t R_REL16 = 2;
301 static constexpr uint16_t R_DIR32 = 6;
303 static constexpr uint16_t R_DIR32NB = 7;
305 static constexpr uint16_t R_SEG12 = 9;
307 static constexpr uint16_t R_SECTION = 10;
309 static constexpr uint16_t R_SECREL = 11;
311 static constexpr uint16_t R_TOKEN = 12;
313 static constexpr uint16_t R_SECREL7 = 13;
315 static constexpr uint16_t R_REL32 = 20;
316
317 COFFVariantType coff_variant;
318 cpu cpu_type;
319
320 offset_t address = 0;
321 uint32_t symbol_index = 0;
322 uint16_t type = 0;
323 uint32_t information = 0;
324
325 UNIXRelocation(COFFVariantType coff_variant, cpu cpu_type)
326 : coff_variant(coff_variant), cpu_type(cpu_type)
327 {
328 }
329
330 void Read(Linker::Reader& rd);
331
332 offset_t GetAddress() const override;
333
334 size_t GetSize() const override;
335
336 size_t GetEntrySize() const override;
337
338 void WriteFile(Linker::Writer& wr) const override;
339
340 void FillEntry(Dumper::Entry& entry) const override;
341 };
342
347 {
348 public:
349 // https://github.com/aixoss/binutils/blob/master/include/coff/internal.h
350 // https://github.com/aixoss/binutils/blob/master/include/coff/z80.h
351 // https://github.com/aixoss/binutils/blob/master/include/coff/z8k.h
352
353 static constexpr uint16_t R_Z80_IMM8 = 0x22;
354 static constexpr uint16_t R_Z80_IMM16 = 0x01;
355 static constexpr uint16_t R_Z80_IMM24 = 0x33;
356 static constexpr uint16_t R_Z80_IMM32 = 0x17;
357 static constexpr uint16_t R_Z80_OFF8 = 0x32;
358 static constexpr uint16_t R_Z80_JR = 0x02;
359
360 static constexpr uint16_t R_Z8K_IMM4L = 0x23;
361 static constexpr uint16_t R_Z8K_IMM4H = 0x24;
362 static constexpr uint16_t R_Z8K_DISP7 = 0x25; /* djnz */
363 static constexpr uint16_t R_Z8K_IMM8 = 0x22;
364 static constexpr uint16_t R_Z8K_IMM16 = 0x01;
365 static constexpr uint16_t R_Z8K_REL16 = 0x04;
366 static constexpr uint16_t R_Z8K_IMM32 = 0x11;
367 static constexpr uint16_t R_Z8K_JR = 0x02; /* jr */
368 static constexpr uint16_t R_Z8K_CALLR = 0x05; /* callr */
369
370 static constexpr uint16_t R_W65_ABS8 = 0x01;
371 static constexpr uint16_t R_W65_ABS16 = 0x02;
372 static constexpr uint16_t R_W65_ABS24 = 0x03;
373 static constexpr uint16_t R_W65_ABS8S8 = 0x04;
374 static constexpr uint16_t R_W65_ABS8S16 = 0x05;
375 static constexpr uint16_t R_W65_ABS16S8 = 0x06;
376 static constexpr uint16_t R_W65_ABS16S16 = 0x07;
377 static constexpr uint16_t R_W65_PCR8 = 0x08;
378 static constexpr uint16_t R_W65_PCR16 = 0x09;
379 static constexpr uint16_t R_W65_DP = 0x0A;
380
381 cpu cpu_type;
382
386 uint32_t address;
390 uint32_t symbol_index;
394 uint32_t offset;
398 uint16_t type;
402 uint16_t data;
403
404 ZilogRelocation(cpu cpu_type)
405 : cpu_type(cpu_type)
406 {
407 }
408
409 void Read(Linker::Reader& rd);
410
411 offset_t GetAddress() const override;
412
413 size_t GetSize() const override;
414
415 size_t GetEntrySize() const override;
416
417 void WriteFile(Linker::Writer& wr) const override;
418
419 void FillEntry(Dumper::Entry& entry) const override;
420 };
421
425 class Symbol
426 {
427 public:
431 std::string name;
435 uint32_t name_index;
439 uint32_t value;
447 uint16_t type;
458
459 void Read(Linker::Reader& rd);
460
461 bool IsExternal() const;
462 };
463
468 {
469 public:
473 std::string name;
479 offset_t physical_address = 0;
483 offset_t address = 0;
487 offset_t size = 0;
491 offset_t section_pointer = 0;
495 offset_t relocation_pointer = 0;
503 uint32_t relocation_count = 0;
507 uint32_t line_number_count = 0;
511 uint32_t flags = 0;
515 uint16_t memory_page_number = 0;
516
520 std::shared_ptr<Linker::Image> image;
521
525 std::vector<std::unique_ptr<Relocation>> relocations;
526
527 /* COFF section flags */
529 static constexpr uint32_t TEXT = 0x0020;
531 static constexpr uint32_t DATA = 0x0040;
533 static constexpr uint32_t BSS = 0x0080;
534
535 void Clear();
536
537 Section(uint32_t flags = 0, std::shared_ptr<Linker::Image> image = nullptr)
539 {
540 }
541
542 virtual ~Section();
543
545 void ReadSectionHeader(Linker::Reader& rd, COFFVariantType coff_variant);
546
548 void WriteSectionHeader(Linker::Writer& wr, COFFVariantType coff_variant);
549
551 virtual uint32_t ImageSize(const COFFFormat& coff_format) const;
552
554 virtual void ReadSectionData(Linker::Reader& rd, const COFFFormat& coff_format);
556 virtual void WriteSectionData(Linker::Writer& wr, const COFFFormat& coff_format) const;
557 };
558
562 std::vector<std::shared_ptr<Section>> sections;
563
567 uint16_t section_count = 0;
571 uint32_t timestamp = 0;
579 uint32_t symbol_count = 0;
583 std::vector<std::unique_ptr<Symbol>> symbols;
591 uint16_t flags = 0;
595 uint16_t target = 0;
596
601 {
602 public:
603 virtual ~OptionalHeader();
607 virtual uint32_t GetSize() const = 0;
608 virtual void ReadFile(Linker::Reader& rd) = 0;
609 virtual void WriteFile(Linker::Writer& wr) const = 0;
615 virtual offset_t CalculateValues(COFFFormat& coff) = 0;
619 virtual void PostReadFile(COFFFormat& coff, Linker::Reader& rd);
623 virtual void PostWriteFile(const COFFFormat& coff, Linker::Writer& wr) const;
624
625 virtual void Dump(const COFFFormat& coff, Dumper::Dumper& dump) const;
626 };
627
631 std::unique_ptr<OptionalHeader> optional_header = nullptr;
632
634 {
635 size_t size;
636 CDOS68K_Relocation(size_t size = 0)
637 : size(size)
638 {
639 }
640 operator size_t() const;
641 static CDOS68K_Relocation Create(size_t size, uint32_t offset, const COFFFormat& format);
642 };
643
647 std::map<uint32_t, CDOS68K_Relocation> relocations; /* CDOS68K */
648
653 {
654 public:
655 std::unique_ptr<Linker::Buffer> buffer = nullptr;
656
657 UnknownOptionalHeader(offset_t size)
658 : buffer(std::make_unique<Linker::Buffer>(size))
659 {
660 }
661
662 uint32_t GetSize() const override;
663
664 void ReadFile(Linker::Reader& rd) override;
665
666 void WriteFile(Linker::Writer& wr) const override;
667
668 offset_t CalculateValues(COFFFormat& coff) override;
669
670 void Dump(const COFFFormat& coff, Dumper::Dumper& dump) const override;
671 };
672
677 {
678 public:
682 uint16_t magic = 0;
686 uint16_t version_stamp = 0;
687
691 uint32_t code_size = 0;
695 uint32_t data_size = 0;
699 uint32_t bss_size = 0;
703 uint32_t entry_address = 0;
707 uint32_t code_address = 0;
711 uint32_t data_address = 0;
712
713 AOutHeader(uint16_t magic = 0)
714 : magic(magic)
715 {
716 }
717
718 uint32_t GetSize() const override;
719
720 void ReadFile(Linker::Reader& rd) override;
721
722 void WriteFile(Linker::Writer& wr) const override;
723
724 offset_t CalculateValues(COFFFormat& coff) override;
725
726 protected:
727 virtual void DumpFields(const COFFFormat& coff, Dumper::Dumper& dump, Dumper::Region& header_region) const;
728
729 public:
730 void Dump(const COFFFormat& coff, Dumper::Dumper& dump) const override;
731 };
732
738 {
739 public:
740 FlexOSAOutHeader(uint16_t magic = 0)
742 {
743 }
744
748 uint32_t relocations_offset = 0;
752 uint32_t stack_size = 0;
753
754 /* TODO: magic not needed for CDOS68K? */
755
756 uint32_t GetSize() const override;
757
758 void ReadFile(Linker::Reader& rd) override;
759
760 void WriteFile(Linker::Writer& wr) const override;
761
762 offset_t CalculateValues(COFFFormat& coff) override;
763
764 void PostReadFile(COFFFormat& coff, Linker::Reader& rd) override;
765
766 void PostWriteFile(const COFFFormat& coff, Linker::Writer& wr) const override;
767
768 protected:
769 void DumpFields(const COFFFormat& coff, Dumper::Dumper& dump, Dumper::Region& header_region) const override;
770 };
771
777 {
778 public:
779 /* Note: untested */
780 uint32_t info = 0;
781 uint32_t code_size = 0;
782 uint32_t data_size = 0;
783 uint32_t bss_size = 0;
784 uint32_t symbol_table_size = 0;
785 uint32_t entry_address = 0;
786 uint32_t code_relocation_size = 0;
787 uint32_t data_relocation_size = 0;
788
789 uint32_t GetSize() const override;
790
791 void ReadFile(Linker::Reader& wr) override;
792
793 void WriteFile(Linker::Writer& wr) const override;
794
795 offset_t CalculateValues(COFFFormat& coff) override;
796
797 void Dump(const COFFFormat& coff, Dumper::Dumper& dump) const override;
798 };
799
806 {
807 public:
808 // https://web.archive.org/web/20140723105157/http://www-scf.usc.edu/~csci402/ncode/coff_8h-source.html
809 /* bss_start */
810 uint32_t bss_address;
811 /* gpr_mask */
812 uint32_t gpr_mask;
813 /* cprmask */
814 uint32_t cpr_mask[4];
815 /* gp_value */
816 uint32_t gp_value;
817
818 MIPSAOutHeader(uint16_t magic = 0)
820 {
821 }
822
823 uint32_t GetSize() const override;
824
825 void ReadFile(Linker::Reader& rd) override;
826
827 void WriteFile(Linker::Writer& wr) const override;
828
829 offset_t CalculateValues(COFFFormat& coff) override;
830
831 protected:
832 void DumpFields(const COFFFormat& coff, Dumper::Dumper& dump, Dumper::Region& header_region) const override;
833 };
834
841 {
842 public:
843 static constexpr uint16_t OMAGIC = 0x0107;
844 static constexpr uint16_t NMAGIC = 0x0108;
845 static constexpr uint16_t ZMAGIC = 0x010B;
849 uint16_t magic = 0;
850 static constexpr uint16_t SYM_STAMP = 0x030D;
854 uint16_t version_stamp = SYM_STAMP;
858 uint16_t build_revision = 0;
859
863 uint64_t code_size = 0;
867 uint64_t data_size = 0;
871 uint64_t bss_size = 0;
875 uint64_t entry_address = 0;
879 uint64_t code_address = 0;
883 uint64_t data_address = 0;
887 uint64_t bss_address = 0;
889 uint32_t gpr_mask = 0;
891 uint32_t fpr_mask = 0;
893 uint64_t global_pointer = 0;
894
895 ECOFFAOutHeader(uint16_t magic = 0)
896 : magic(magic)
897 {
898 }
899
900 uint32_t GetSize() const override;
901
902 void ReadFile(Linker::Reader& rd) override;
903
904 void WriteFile(Linker::Writer& wr) const override;
905
906 offset_t CalculateValues(COFFFormat& coff) override;
907
908 void Dump(const COFFFormat& coff, Dumper::Dumper& dump) const override;
909 };
910
917 {
918 public:
920 bool is64 = false;
924 uint16_t magic = 0;
928 uint16_t version_stamp = 0;
932 uint64_t code_size = 0;
936 uint64_t data_size = 0;
940 uint64_t bss_size = 0;
944 uint64_t entry_address = 0;
948 uint64_t code_address = 0;
952 uint64_t data_address = 0;
956 uint64_t toc_address = 0;
960 uint16_t entry_section = 0;
964 uint16_t code_section = 0;
968 uint16_t data_section = 0;
972 uint16_t toc_section = 0;
976 uint16_t loader_section = 0;
980 uint16_t bss_section = 0;
984 uint16_t code_align = 0;
988 uint16_t data_align = 0;
992 uint16_t module_type = 0;
996 uint8_t cpu_flags = 0;
1000 uint8_t cpu_type = 0;
1008 uint64_t maximum_data_size = 0;
1012 uint32_t debugger_data = 0;
1016 uint8_t code_page_size = 0;
1020 uint8_t text_page_size = 0;
1024 uint8_t stack_page_size = 0;
1028 uint8_t flags = 0;
1032 uint16_t tdata_section = 0;
1036 uint16_t tbss_section = 0;
1040 uint32_t xcoff64_flags = 0;
1041#if 0
1045 uint8_t shared_memory_page = 0;
1046#endif
1047
1048 XCOFFAOutHeader(bool is64, uint16_t magic = 0)
1049 : is64(is64), magic(magic)
1050 {
1051 }
1052
1053 uint32_t GetSize() const override;
1054
1055 void ReadFile(Linker::Reader& rd) override;
1056
1057 void WriteFile(Linker::Writer& wr) const override;
1058
1059 offset_t CalculateValues(COFFFormat& coff) override;
1060
1061 void Dump(const COFFFormat& coff, Dumper::Dumper& dump) const override;
1062 };
1063
1064 void Clear() override;
1065
1067 void AssignMagicValue(uint16_t value, ::EndianType as_endian_type);
1069 void AssignMagicValue(uint16_t value);
1071 void AssignMagicValue();
1072
1073 COFFVariantType coff_variant = AnyCOFFVariant;
1074
1080 cpu cpu_type = CPU_UNKNOWN;
1081
1085 ::EndianType endiantype = ::UndefinedEndian;
1086
1087 bool DetectCpuType(::EndianType expected);
1088
1089 void DetectCpuType();
1090
1091 void ReadFile(Linker::Reader& rd) override;
1092
1093 protected:
1094 void ReadCOFFHeader(Linker::Reader& rd);
1095 void ReadOptionalHeader(Linker::Reader& rd);
1096 void ReadRestOfFile(Linker::Reader& rd);
1097
1098 public:
1099 offset_t ImageSize() const override;
1100
1102 offset_t WriteFile(Linker::Writer& wr) const override;
1103
1104 protected:
1105 offset_t WriteFileContents(Linker::Writer& wr) const;
1106
1107 public:
1108 void Dump(Dumper::Dumper& dump) const override;
1109
1110 /* * * Reader members * * */
1111
1112 void SetupOptions(std::shared_ptr<Linker::OutputFormat> format) override;
1113
1114 bool option_segmentation = false;
1115
1116 bool FormatRequiresDataStreamFix() const override;
1117
1118 private:
1119 /* symbols */
1120 std::string segment_prefix();
1121
1122 std::string segment_of_prefix();
1123
1127 std::string segmented_address_prefix();
1128
1129#if 0
1130 // TODO: can this be used?
1131 std::string segment_difference_prefix();
1132#endif
1133
1134 enum
1135 {
1136 /* section number */
1137 N_UNDEF = 0,
1138 N_ABS = 0xFFFF,
1139 N_DEBUG = 0xFFFE,
1140
1141 /* storage class */
1142 C_EXT = 2,
1143 C_STAT = 3,
1144 C_LABEL = 6,
1145 };
1146
1147 public:
1149 void GenerateModule(Linker::Module& module) const override;
1150
1151 /* * * Writer members * * */
1152
1154 {
1155 public:
1156 Linker::Option<std::string> stub{"stub", "Filename for stub that gets prepended to executable"};
1157
1159 {
1160 InitializeFields(stub);
1161 }
1162 };
1163
1164 // for DJGPP binaries
1165 mutable Microsoft::MZSimpleStubWriter stub;
1166
1197
1202
1206 uint32_t stub_size = 0;
1207
1211 std::shared_ptr<Linker::Segment> stack;
1215 uint32_t entry_address = 0; /* TODO */
1220
1221 COFFFormat(format_type type = GENERIC, COFFVariantType coff_variant = COFF, EndianType endiantype = ::UndefinedEndian)
1222 : coff_variant(coff_variant), endiantype(endiantype), type(type)
1223 {
1224 }
1225
1226 ~COFFFormat()
1227 {
1228 Clear();
1229 }
1230
1231 unsigned FormatAdditionalSectionFlags(std::string section_name) const override;
1232
1233 static std::vector<Linker::OptionDescription<void> *> ParameterNames;
1234 std::vector<Linker::OptionDescription<void> *> GetLinkerScriptParameterNames() override;
1235
1236 std::shared_ptr<Linker::OptionCollector> GetOptions() override;
1237
1238 void SetOptions(std::map<std::string, std::string>& options) override;
1239
1241 enum
1242 {
1257
1258 OMAGIC = 0x0107,
1259 NMAGIC = 0x0108,
1263 ZMAGIC = 0x010B,
1268 };
1269
1270 void OnNewSegment(std::shared_ptr<Linker::Segment> segment) override;
1271
1272 void CreateDefaultSegments();
1273
1274 std::unique_ptr<Script::List> GetScript(Linker::Module& module);
1275
1276 void Link(Linker::Module& module);
1277
1279 std::shared_ptr<Linker::Segment> GetSegment(std::shared_ptr<Section>& section);
1280
1281 std::shared_ptr<Linker::Segment> GetCodeSegment();
1282
1283 std::shared_ptr<Linker::Segment> GetDataSegment();
1284
1285 std::shared_ptr<Linker::Segment> GetBssSegment();
1286
1287 void ProcessModule(Linker::Module& module) override;
1288
1289 void CalculateValues() override;
1290
1291 void GenerateFile(std::string filename, Linker::Module& module) override;
1292
1294 std::string GetDefaultExtension(Linker::Module& module, std::string filename) const override;
1295 };
1296
1297}
1298
1299#endif /* COFF_H */
A standard 28 byte a.out optional header, used by DJGPP.
Definition coff.h:677
uint32_t code_address
Address of code (COFF name: text_start)
Definition coff.h:707
uint16_t version_stamp
Version stamp (COFF name: vstamp)
Definition coff.h:686
uint32_t entry_address
Initial value of eip (COFF name: entry)
Definition coff.h:703
uint16_t magic
Type of executable, most typically ZMAGIC (COFF name: magic)
Definition coff.h:682
uint32_t code_size
Code size (COFF name: tsize)
Definition coff.h:691
uint32_t GetSize() const override
Returns size of optional header.
Definition coff.cc:736
uint32_t data_size
Data size (COFF name: dsize)
Definition coff.h:695
uint32_t bss_size
BSS size (COFF name: bsize)
Definition coff.h:699
uint32_t data_address
Address of data (COFF name: data_start)
Definition coff.h:711
ECOFF optional header.
Definition coff.h:841
uint64_t bss_address
Base address for bss segment (ECOFF name: bss_start)
Definition coff.h:887
uint32_t gpr_mask
unused (ECOFF name: gprmask)
Definition coff.h:889
uint16_t magic
Type of executable.
Definition coff.h:849
uint64_t data_size
Size of data segment (ECOFF name: dsize)
Definition coff.h:867
uint32_t GetSize() const override
Returns size of optional header.
Definition coff.cc:982
uint16_t version_stamp
Object file version stamp (ECOFF name: vstamp)
Definition coff.h:854
uint64_t bss_size
Size of bss segment (ECOFF name: bsize)
Definition coff.h:871
uint32_t fpr_mask
unused (ECOFF name: fprmask)
Definition coff.h:891
uint64_t global_pointer
Initial global pointer value (ECOFF name: gp_value)
Definition coff.h:893
uint64_t data_address
Base address for data segment (ECOFF name: data_start)
Definition coff.h:883
uint64_t code_address
Base address for code segment (ECOFF name: text_start)
Definition coff.h:879
uint16_t build_revision
Revision build of system tools (ECOFF name: bldrev)
Definition coff.h:858
uint64_t code_size
Size of code segment (ECOFF name: tsize)
Definition coff.h:863
uint64_t entry_address
Virtual address of execution start (ECOFF name: entry)
Definition coff.h:875
Concurrent DOS 68K/FlexOS 386 optional header Concurrent DOS 68K uses the typical a....
Definition coff.h:738
uint32_t GetSize() const override
Returns size of optional header.
Definition coff.cc:821
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:848
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:857
uint32_t stack_size
Size of stack for execution.
Definition coff.h:752
uint32_t relocations_offset
The offset to the crunched relocation data within the file.
Definition coff.h:748
GNU a.out optional header TODO.
Definition coff.h:777
uint32_t GetSize() const override
Returns size of optional header.
Definition coff.cc:875
56 byte long header used on MIPS
Definition coff.h:806
uint32_t GetSize() const override
Returns size of optional header.
Definition coff.cc:936
An abstract class to represent the optional header.
Definition coff.h:601
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:689
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:693
A generic COFF relocation.
Definition coff.h:278
A COFF section.
Definition coff.h:468
std::string name
The name of the section (COFF name: s_name)
Definition coff.h:473
offset_t relocation_pointer
Offset to COFF relocations (COFF name: s_relptr)
Definition coff.h:495
uint32_t relocation_count
COFF relocation count (COFF name: s_nreloc)
Definition coff.h:503
offset_t size
The size of the section (COFF name: s_size)
Definition coff.h:487
offset_t address
The virtual address of the section (COFF name: s_vaddr)
Definition coff.h:483
void WriteSectionHeader(Linker::Writer &wr, COFFVariantType coff_variant)
Writes an entry in the section header table.
Definition coff.cc:593
static constexpr uint32_t BSS
COFF section flag: Section contains uninitialized data (COFF name: STYP_BSS)
Definition coff.h:533
std::vector< std::unique_ptr< Relocation > > relocations
Collection of COFF relocations.
Definition coff.h:525
uint16_t memory_page_number
TICOFF specific field.
Definition coff.h:515
offset_t line_number_pointer
unused (COFF name: s_lnnoptr)
Definition coff.h:499
static constexpr uint32_t DATA
COFF section flag: Section contains initialized data (COFF name: STYP_DATA)
Definition coff.h:531
void ReadSectionHeader(Linker::Reader &rd, COFFVariantType coff_variant)
Reads an entry in the section header table.
Definition coff.cc:506
virtual void WriteSectionData(Linker::Writer &wr, const COFFFormat &coff_format) const
Writes the section contents to a stream, can be overloaded by subclasses.
Definition coff.cc:679
virtual void ReadSectionData(Linker::Reader &rd, const COFFFormat &coff_format)
Reads the section contents from a stream, can be overloaded by subclasses.
Definition coff.cc:673
offset_t section_pointer
Offset of stored image data from COFF header start (COFF name: s_scnptr)
Definition coff.h:491
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:479
static constexpr uint32_t TEXT
COFF section flag: Section contains executable (COFF name: STYP_TEXT)
Definition coff.h:529
std::shared_ptr< Linker::Image > image
The stored image data.
Definition coff.h:520
uint32_t line_number_count
unused (COFF name: s_nlnno)
Definition coff.h:507
uint32_t flags
COFF section flags, determines the type of the section (text, data, bss, etc.) (COFF name: s_flags)
Definition coff.h:511
A COFF symbol.
Definition coff.h:426
std::string name
Symbol name (COFF name: n_name, if it fits inside field)
Definition coff.h:431
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:443
uint16_t type
The symbol type (COFF name: n_type)
Definition coff.h:447
uint8_t storage_class
COFF name: n_sclass, typical values are 2 (C_EXT), 3 (C_STAT)
Definition coff.h:453
uint32_t name_index
The index of the symbol name within the string table, if not stored directly in the entry,...
Definition coff.h:435
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:457
uint32_t value
The actual value of the symbol (COFF name: n_value)
Definition coff.h:439
The standard UNIX COFF relocation format.
Definition coff.h:292
static constexpr uint16_t R_SECTION
(Microsoft, debugging) 16-bit section index
Definition coff.h:307
static constexpr uint16_t R_REL16
16-bit relative address of symbol
Definition coff.h:299
static constexpr uint16_t R_ABS
No relocation.
Definition coff.h:295
static constexpr uint16_t R_DIR32NB
(Microsoft COFF) 32-bit relative virtual address of symbol
Definition coff.h:303
static constexpr uint16_t R_SECREL7
(Microsoft) 7-bit offset from section base
Definition coff.h:313
static constexpr uint16_t R_REL32
32-bit relative address of symbol
Definition coff.h:315
static constexpr uint16_t R_SEG12
(Intel x86) 16-bit segment selector of symbol
Definition coff.h:305
static constexpr uint16_t R_SECREL
(Microsoft, debugging) 32-bit offset from section start
Definition coff.h:309
static constexpr uint16_t R_TOKEN
(Microsoft) CLR token
Definition coff.h:311
static constexpr uint16_t R_DIR16
16-bit virtual address of symbol
Definition coff.h:297
static constexpr uint16_t R_DIR32
32-bit virtual address of symbol
Definition coff.h:301
A simplified class to represent an optional header of unknown structure.
Definition coff.h:653
uint32_t GetSize() const override
Returns size of optional header.
Definition coff.cc:711
XCOFF optional header.
Definition coff.h:917
uint16_t code_align
Maximum alignment of text (XCOFF name: algntext)
Definition coff.h:984
uint16_t version_stamp
Object file version stamp (XCOFF name: vstamp)
Definition coff.h:928
bool is64
False for XCOFF32, true for XCOFF64.
Definition coff.h:920
uint64_t maximum_stack_size
Maximum stack size (XCOFF name: maxstack)
Definition coff.h:1004
uint16_t magic
Type of executable (XCOFF name: mflag)
Definition coff.h:924
uint64_t data_size
Size of data segment (XCOFF name: dsize)
Definition coff.h:936
uint32_t xcoff64_flags
XCOFF64 specific flags (XCOFF64 name: x64flags)
Definition coff.h:1040
uint16_t toc_section
Section number for TOC (XCOFF name: sntoc)
Definition coff.h:972
uint32_t debugger_data
Reserved for debugger (XCOFF name: debugger)
Definition coff.h:1012
uint64_t code_size
Size of code segment (XCOFF name: tsize)
Definition coff.h:932
uint16_t entry_section
Section number for entry point (XCOFF name: snentry)
Definition coff.h:960
uint8_t text_page_size
Requested data page size (XCOFF name: datapsize)
Definition coff.h:1020
uint64_t data_address
Base address for data segment (XCOFF name: data_start)
Definition coff.h:952
uint16_t tbss_section
Section number for tbss (XCOFF name: sntbss)
Definition coff.h:1036
uint16_t loader_section
Section number for loader data (XCOFF name: snloader)
Definition coff.h:976
uint64_t bss_size
Size of bss segment (XCOFF name: bsize)
Definition coff.h:940
uint8_t cpu_type
CPU type (XCOFF name: cputype)
Definition coff.h:1000
uint8_t stack_page_size
Requested stack page size (XCOFF name: stackpsize)
Definition coff.h:1024
uint8_t cpu_flags
CPU flags (XCOFF name: cpuflag)
Definition coff.h:996
uint16_t module_type
Module type (XCOFF name: modtype)
Definition coff.h:992
uint16_t data_section
Section number for data (XCOFF name: sndata)
Definition coff.h:968
uint64_t code_address
Base address for code segment (XCOFF name: text_start)
Definition coff.h:948
uint16_t data_align
Maximum alignment of data (XCOFF name: algndata)
Definition coff.h:988
uint16_t code_section
Section number for text (XCOFF name: sntext)
Definition coff.h:964
uint64_t entry_address
Virtual address of execution start (XCOFF name: entry)
Definition coff.h:944
uint64_t toc_address
Address of TOC (XCOFF name: toc)
Definition coff.h:956
uint8_t flags
Flags.
Definition coff.h:1028
uint8_t code_page_size
Requested code page size (XCOFF name: textpsize)
Definition coff.h:1016
uint32_t GetSize() const override
Returns size of optional header.
Definition coff.cc:1033
uint16_t bss_section
Section number for bss (XCOFF name: snbss)
Definition coff.h:980
uint16_t tdata_section
Section number for tdata (XCOFF name: sntdata)
Definition coff.h:1032
uint64_t maximum_data_size
Maximum data size (XCOFF name: maxdata)
Definition coff.h:1008
A relocation, as stored by the Z80/Z8000 backend.
Definition coff.h:347
uint32_t symbol_index
Index of symbol in symbol table (COFF name: r_symndx)
Definition coff.h:390
uint16_t data
unknown (COFF name: r_stuff)
Definition coff.h:402
uint32_t address
Address of the relocation (COFF name: r_vaddr)
Definition coff.h:386
uint16_t type
Type of relocation (COFF name: r_type)
Definition coff.h:398
uint32_t offset
(COFF name: r_offset)
Definition coff.h:394
The UNIX COFF file format.
Definition coff.h:27
void Clear() override
Resets all fields to their default values, deallocate memory.
Definition coff.cc:1174
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:1729
COFFVariantType
Variants of the COFF file format.
Definition coff.h:236
@ COFF
Standard COFF variant.
Definition coff.h:238
@ ECOFF
ECOFF 64-bit version.
Definition coff.h:240
@ XCOFF32
32-bit XCOFF from IBM
Definition coff.h:242
@ TICOFF
Texas Instruments COFF variant (COFF2)
Definition coff.h:248
@ XCOFF64
64-bit XCOFF from IBM
Definition coff.h:244
@ PECOFF
Microsoft PE/COFF variant.
Definition coff.h:246
@ TICOFF1
Texas Instruments COFF variant (COFF1)
Definition coff.h:250
std::vector< std::shared_ptr< Section > > sections
The list of COFF sections.
Definition coff.h:562
void CalculateValues() override
Intermediate step between processing module and generating output file to set up headers and manageme...
Definition coff.cc:2396
format_type
Represents the type of target system, which will determine the CPU type and several other fields.
Definition coff.h:1171
@ CDOS386
FlexOS 386 executable.
Definition coff.h:1187
@ WINDOWS
Windows Portable Executable (used only by PE)
Definition coff.h:1191
@ GENERIC
An unspecified value, probably will not work.
Definition coff.h:1175
@ DJGPP
DJGPP COFF executable.
Definition coff.h:1179
@ CDOS68K
Concurrent DOS 68K executable (untested but confident)
Definition coff.h:1183
uint32_t optional_header_size
The size of the optional header (COFF: f_opthdr)
Definition coff.h:587
@ FLAG_NO_SYMBOLS
F_LSYMS.
Definition coff.h:1250
@ FLAG_NO_RELOCATIONS
F_RELFLG.
Definition coff.h:1244
@ MAGIC_FLEXOS386
Magic number required by FlexOS 386.
Definition coff.h:1267
@ FLAG_32BIT_LITTLE_ENDIAN
F_AR32WR.
Definition coff.h:1254
@ ZMAGIC
Stored as the magic of the a.out header.
Definition coff.h:1263
@ FLAG_32BIT_BIG_ENDIAN
F_AR32W.
Definition coff.h:1256
@ FLAG_NO_LINE_NUMBERS
F_LNNO.
Definition coff.h:1248
@ FLAG_PDP11_ENDIAN
F_AR16WR.
Definition coff.h:1252
@ FLAG_EXECUTABLE
F_EXEC.
Definition coff.h:1246
std::shared_ptr< Linker::Segment > GetSegment(std::shared_ptr< Section > &section)
Return the segment stored inside the section, note that this only works for binary generation.
Definition coff.cc:2301
std::shared_ptr< Linker::OptionCollector > GetOptions() override
Returns object containing a sequence of option fields provided with the -S command line flag.
Definition coff.cc:2096
std::vector< Linker::OptionDescription< void > * > GetLinkerScriptParameterNames() override
Returns a list of the parameters used in the linker scripts, used for documentation.
Definition coff.cc:2091
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition coff.cc:1575
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition coff.cc:1486
format_type type
A representation of the format to generate.
Definition coff.h:1196
char signature[2]
The actual value of the magic number (COFF name: f_magic)
Definition coff.h:267
std::vector< std::unique_ptr< Symbol > > symbols
The symbols stored inside the COFF file.
Definition coff.h:583
bool FormatRequiresDataStreamFix() const override
Whether the generated file might contain bugs that require fixing.
Definition coff.cc:1724
cpu
Represents the first 16-bit word of a COFF file.
Definition coff.h:35
@ CPU_IA64
Intel Itanium architecture, also known as IA-64.
Definition coff.h:180
@ CPU_ALPHA
DEC Alpha.
Definition coff.h:126
@ CPU_PPC
IBM PowerPC, 32-bit.
Definition coff.h:132
@ CPU_AMD64
x86-64, introduced by AMD
Definition coff.h:216
@ CPU_VAX
DEC VAX.
Definition coff.h:114
@ CPU_I86
Intel x86-16, introduced with the Intel 8086.
Definition coff.h:78
@ CPU_M32R
Mitsubishi M32R.
Definition coff.h:222
@ CPU_AM33
Matsushita AM33.
Definition coff.h:174
@ CPU_RISCV32
RISC-V 32.
Definition coff.h:198
@ CPU_Z8K
Zilog Z8000.
Definition coff.h:70
@ CPU_Z80
Zilog Z80.
Definition coff.h:64
@ CPU_I370
IBM System/370.
Definition coff.h:90
@ CPU_NS32K
National Semiconductor NS32000.
Definition coff.h:84
@ CPU_PPC64
IBM PowerPC, 64-bit.
Definition coff.h:138
@ CPU_I386
Intel x86-32, introduced with the Intel 80386.
Definition coff.h:44
@ CPU_ARM
ARM, also known as ARM32 or AArch32; also represents Thumb.
Definition coff.h:168
@ CPU_EFI
EFI bytecode.
Definition coff.h:192
@ CPU_M68K
Motorola 68000 and compatibles.
Definition coff.h:50
@ CPU_I860
Intel i860.
Definition coff.h:156
@ CPU_WE32K
AT&T Bellmac 32, Western Electric 32000 and family.
Definition coff.h:108
@ CPU_M88K
Motorola 88000.
Definition coff.h:102
@ CPU_SH
Hitachi SuperH family.
Definition coff.h:162
@ CPU_HPPA
Hewlett-Packard PA-RISC.
Definition coff.h:186
@ CPU_SHARC
SHARC from Analog Devices.
Definition coff.h:141
@ CPU_ARM64
ARM64, also known as AArch64.
Definition coff.h:228
@ CPU_MIPS
MIPS architecture.
Definition coff.h:96
@ CPU_W65
WDC 65C816.
Definition coff.h:58
@ CPU_RISCV64
RISC-V 64.
Definition coff.h:204
@ CPU_RISCV128
RISC-V 128.
Definition coff.h:210
@ CPU_AM29K
AMD 29000.
Definition coff.h:120
::EndianType endiantype
The byte order.
Definition coff.h:1085
uint32_t relocations_offset
Concurrent DOS 68K: Offset to relocations.
Definition coff.h:1219
bool option_no_relocation
Suppress relocation generation, only relevant for Concurrent DOS 68K, since the other target formats ...
Definition coff.h:1201
uint32_t stub_size
Size of MZ stub, only used for DJGPP COFF executables.
Definition coff.h:1206
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:1211
void SetupOptions(std::shared_ptr< Linker::OutputFormat > format) override
Initializes the reader for linking purposes.
Definition coff.cc:1719
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition coff.cc:1257
void GenerateFile(std::string filename, Linker::Module &module) override
The main function that handles processing, calculating and generating the final image.
Definition coff.cc:2511
uint16_t section_count
Section count (COFF name: f_nscns)
Definition coff.h:567
uint16_t target
TI system target ID.
Definition coff.h:595
uint32_t entry_address
Entry address, gets stored in optional header later.
Definition coff.h:1215
uint32_t symbol_count
The number of symbols (COFF name: f_nsyms)
Definition coff.h:579
void AssignMagicValue()
Loads the currently set CPU value in the currently set byte order into the COFF header signature.
Definition coff.cc:1196
void SetOptions(std::map< std::string, std::string > &options) override
Passes command line parameters as settings over to format object.
Definition coff.cc:2101
void ProcessModule(Linker::Module &module) override
Processes the module object and initializes format fields.
Definition coff.cc:2336
::EndianType GetEndianType() const
Retrieves the natural byte order for the architecture.
Definition coff.cc:128
offset_t symbol_table_offset
Offset to the first symbol (COFF name: f_symptr)
Definition coff.h:575
std::map< uint32_t, CDOS68K_Relocation > relocations
Concurrent DOS 68K requires a special block of data to represent "crunched" relocations (see CPM68KWr...
Definition coff.h:647
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:2111
std::string GetDefaultExtension(Linker::Module &module, std::string filename) const override
Appends a default extension to the filename.
Definition coff.cc:2553
offset_t ImageSize() const override
Retrieves size of stored data.
Definition coff.cc:1498
std::unique_ptr< OptionalHeader > optional_header
The optional header instance used for reading/writing the COFF file.
Definition coff.h:631
uint16_t flags
COFF flags, such as whether the file is executable (f_flags)
Definition coff.h:591
cpu cpu_type
The CPU type, reflected by the first 16-bit word of a COFF file.
Definition coff.h:1080
uint32_t timestamp
Time stamp, unused (COFF name: f_timdat)
Definition coff.h:571
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:193
virtual void GenerateModule(ModuleCollector &linker, std::string file_name, bool is_library=false) const
Loads the information into a module object.
Definition format.cc:215
Encodes an object module file as a collection of sections, symbols and relocations.
Definition module.h:24
Helper class that contains the options interpreted by the format.
Definition options.h:467
Documents and handles command line options.
Definition options.h:303
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:269
Definition coff.h:256