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
528 enum
529 {
531 TEXT = 0x0020,
533 DATA = 0x0040,
535 BSS = 0x0080,
536 };
537
538 void Clear();
539
540 Section(uint32_t flags = 0, std::shared_ptr<Linker::Image> image = nullptr)
542 {
543 }
544
545 ~Section()
546 {
547 Clear();
548 }
549
550 void ReadSectionHeader(Linker::Reader& rd, COFFVariantType coff_variant);
551
552 void WriteSectionHeader(Linker::Writer& wr, COFFVariantType coff_variant);
553
554 uint32_t ImageSize() const;
555 };
556
560 std::vector<std::unique_ptr<Section>> sections;
561
565 uint16_t section_count = 0;
569 uint32_t timestamp = 0;
577 uint32_t symbol_count = 0;
581 std::vector<std::unique_ptr<Symbol>> symbols;
589 uint16_t flags = 0;
593 uint16_t target = 0;
594
599 {
600 public:
601 virtual ~OptionalHeader();
605 virtual uint32_t GetSize() const = 0;
606 virtual void ReadFile(Linker::Reader& rd) = 0;
607 virtual void WriteFile(Linker::Writer& wr) const = 0;
613 virtual offset_t CalculateValues(COFFFormat& coff) = 0;
617 virtual void PostReadFile(COFFFormat& coff, Linker::Reader& rd);
621 virtual void PostWriteFile(const COFFFormat& coff, Linker::Writer& wr) const;
622
623 virtual void Dump(const COFFFormat& coff, Dumper::Dumper& dump) const;
624 };
625
629 std::unique_ptr<OptionalHeader> optional_header = nullptr;
630
634 std::map<uint32_t, size_t> relocations; /* CDOS68K */
635
640 {
641 public:
642 std::unique_ptr<Linker::Buffer> buffer = nullptr;
643
644 UnknownOptionalHeader(offset_t size)
645 : buffer(std::make_unique<Linker::Buffer>(size))
646 {
647 }
648
649 uint32_t GetSize() const override;
650
651 void ReadFile(Linker::Reader& rd) override;
652
653 void WriteFile(Linker::Writer& wr) const override;
654
655 offset_t CalculateValues(COFFFormat& coff) override;
656
657 void Dump(const COFFFormat& coff, Dumper::Dumper& dump) const override;
658 };
659
664 {
665 public:
669 uint16_t magic = 0;
673 uint16_t version_stamp = 0;
674
678 uint32_t code_size = 0;
682 uint32_t data_size = 0;
686 uint32_t bss_size = 0;
690 uint32_t entry_address = 0;
694 uint32_t code_address = 0;
698 uint32_t data_address = 0;
699
700 AOutHeader(uint16_t magic = 0)
701 : magic(magic)
702 {
703 }
704
705 uint32_t GetSize() const override;
706
707 void ReadFile(Linker::Reader& rd) override;
708
709 void WriteFile(Linker::Writer& wr) const override;
710
711 offset_t CalculateValues(COFFFormat& coff) override;
712
713 protected:
714 virtual void DumpFields(const COFFFormat& coff, Dumper::Dumper& dump, Dumper::Region& header_region) const;
715
716 public:
717 void Dump(const COFFFormat& coff, Dumper::Dumper& dump) const override;
718 };
719
725 {
726 public:
730 uint32_t relocations_offset = 0;
734 uint32_t stack_size = 0;
735
736 /* TODO: magic not needed for CDOS68K? */
737
738 uint32_t GetSize() const override;
739
740 void ReadFile(Linker::Reader& rd) override;
741
742 void WriteFile(Linker::Writer& wr) const override;
743
744 offset_t CalculateValues(COFFFormat& coff) override;
745
746 void PostReadFile(COFFFormat& coff, Linker::Reader& rd) override;
747
748 void PostWriteFile(const COFFFormat& coff, Linker::Writer& wr) const override;
749
750 protected:
751 void DumpFields(const COFFFormat& coff, Dumper::Dumper& dump, Dumper::Region& header_region) const override;
752 };
753
759 {
760 public:
761 /* Note: untested */
762 uint32_t info = 0;
763 uint32_t code_size = 0;
764 uint32_t data_size = 0;
765 uint32_t bss_size = 0;
766 uint32_t symbol_table_size = 0;
767 uint32_t entry_address = 0;
768 uint32_t code_relocation_size = 0;
769 uint32_t data_relocation_size = 0;
770
771 uint32_t GetSize() const override;
772
773 void ReadFile(Linker::Reader& wr) override;
774
775 void WriteFile(Linker::Writer& wr) const override;
776
777 offset_t CalculateValues(COFFFormat& coff) override;
778
779 void Dump(const COFFFormat& coff, Dumper::Dumper& dump) const override;
780 };
781
788 {
789 public:
790 // https://web.archive.org/web/20140723105157/http://www-scf.usc.edu/~csci402/ncode/coff_8h-source.html
791 /* bss_start */
792 uint32_t bss_address;
793 /* gpr_mask */
794 uint32_t gpr_mask;
795 /* cprmask */
796 uint32_t cpr_mask[4];
797 /* gp_value */
798 uint32_t gp_value;
799
800 MIPSAOutHeader(uint16_t magic = 0)
802 {
803 }
804
805 uint32_t GetSize() const override;
806
807 void ReadFile(Linker::Reader& rd) override;
808
809 void WriteFile(Linker::Writer& wr) const override;
810
811 offset_t CalculateValues(COFFFormat& coff) override;
812
813 protected:
814 void DumpFields(const COFFFormat& coff, Dumper::Dumper& dump, Dumper::Region& header_region) const override;
815 };
816
823 {
824 public:
825 static constexpr uint16_t OMAGIC = 0x0107;
826 static constexpr uint16_t NMAGIC = 0x0108;
827 static constexpr uint16_t ZMAGIC = 0x010B;
831 uint16_t magic = 0;
832 static constexpr uint16_t SYM_STAMP = 0x030D;
836 uint16_t version_stamp = SYM_STAMP;
840 uint16_t build_revision = 0;
841
845 uint64_t code_size = 0;
849 uint64_t data_size = 0;
853 uint64_t bss_size = 0;
857 uint64_t entry_address = 0;
861 uint64_t code_address = 0;
865 uint64_t data_address = 0;
869 uint64_t bss_address = 0;
871 uint32_t gpr_mask = 0;
873 uint32_t fpr_mask = 0;
875 uint64_t global_pointer = 0;
876
877 ECOFFAOutHeader(uint16_t magic = 0)
878 : magic(magic)
879 {
880 }
881
882 uint32_t GetSize() const override;
883
884 void ReadFile(Linker::Reader& rd) override;
885
886 void WriteFile(Linker::Writer& wr) const override;
887
888 offset_t CalculateValues(COFFFormat& coff) override;
889
890 void Dump(const COFFFormat& coff, Dumper::Dumper& dump) const override;
891 };
892
899 {
900 public:
902 bool is64 = false;
906 uint16_t magic = 0;
910 uint16_t version_stamp = 0;
914 uint64_t code_size = 0;
918 uint64_t data_size = 0;
922 uint64_t bss_size = 0;
926 uint64_t entry_address = 0;
930 uint64_t code_address = 0;
934 uint64_t data_address = 0;
938 uint64_t toc_address = 0;
942 uint16_t entry_section = 0;
946 uint16_t code_section = 0;
950 uint16_t data_section = 0;
954 uint16_t toc_section = 0;
958 uint16_t loader_section = 0;
962 uint16_t bss_section = 0;
966 uint16_t code_align = 0;
970 uint16_t data_align = 0;
974 uint16_t module_type = 0;
978 uint8_t cpu_flags = 0;
982 uint8_t cpu_type = 0;
986 uint64_t maximum_stack_size = 0;
990 uint64_t maximum_data_size = 0;
994 uint32_t debugger_data = 0;
998 uint8_t code_page_size = 0;
1002 uint8_t text_page_size = 0;
1006 uint8_t stack_page_size = 0;
1010 uint8_t flags = 0;
1014 uint16_t tdata_section = 0;
1018 uint16_t tbss_section = 0;
1022 uint32_t xcoff64_flags = 0;
1023#if 0
1027 uint8_t shared_memory_page = 0;
1028#endif
1029
1030 XCOFFAOutHeader(bool is64, uint16_t magic = 0)
1031 : is64(is64), magic(magic)
1032 {
1033 }
1034
1035 uint32_t GetSize() const override;
1036
1037 void ReadFile(Linker::Reader& rd) override;
1038
1039 void WriteFile(Linker::Writer& wr) const override;
1040
1041 offset_t CalculateValues(COFFFormat& coff) override;
1042
1043 void Dump(const COFFFormat& coff, Dumper::Dumper& dump) const override;
1044 };
1045
1046 void Clear() override;
1047
1048 void AssignMagicValue();
1049
1050 COFFVariantType coff_variant = AnyCOFFVariant;
1051
1057 cpu cpu_type = CPU_UNKNOWN;
1058
1062 ::EndianType endiantype = ::UndefinedEndian;
1063
1064 bool DetectCpuType(::EndianType expected);
1065
1066 void DetectCpuType();
1067
1068 void ReadFile(Linker::Reader& rd) override;
1069
1070 protected:
1071 void ReadCOFFHeader(Linker::Reader& rd);
1072 void ReadOptionalHeader(Linker::Reader& rd);
1073 void ReadRestOfFile(Linker::Reader& rd);
1074
1075 public:
1076 offset_t ImageSize() const override;
1077
1079 offset_t WriteFile(Linker::Writer& wr) const override;
1080
1081 protected:
1082 offset_t WriteFileContents(Linker::Writer& wr) const;
1083
1084 public:
1085 void Dump(Dumper::Dumper& dump) const override;
1086
1087 /* * * Reader members * * */
1088
1089 void SetupOptions(std::shared_ptr<Linker::OutputFormat> format) override;
1090
1091 bool option_segmentation = false;
1092
1093 bool FormatRequiresDataStreamFix() const override;
1094
1095 private:
1096 /* symbols */
1097 std::string segment_prefix();
1098
1099 std::string segment_of_prefix();
1100
1104 std::string segmented_address_prefix();
1105
1106#if 0
1107 // TODO: can this be used?
1108 std::string segment_difference_prefix();
1109#endif
1110
1111 enum
1112 {
1113 /* section number */
1114 N_UNDEF = 0,
1115 N_ABS = 0xFFFF,
1116 N_DEBUG = 0xFFFE,
1117
1118 /* storage class */
1119 C_EXT = 2,
1120 C_STAT = 3,
1121 C_LABEL = 6,
1122 };
1123
1124 public:
1126 void GenerateModule(Linker::Module& module) const override;
1127
1128 /* * * Writer members * * */
1129
1131 {
1132 public:
1133 Linker::Option<std::string> stub{"stub", "Filename for stub that gets prepended to executable"};
1134
1136 {
1137 InitializeFields(stub);
1138 }
1139 };
1140
1141 // for DJGPP binaries
1142 mutable Microsoft::MZSimpleStubWriter stub;
1143
1170
1175
1179 uint32_t stub_size = 0;
1180
1184 std::shared_ptr<Linker::Segment> stack;
1188 uint32_t entry_address = 0; /* TODO */
1193
1194 COFFFormat(format_type type = GENERIC, COFFVariantType coff_variant = COFF, EndianType endiantype = ::UndefinedEndian)
1195 : coff_variant(coff_variant), endiantype(endiantype), type(type)
1196 {
1197 }
1198
1199 ~COFFFormat()
1200 {
1201 Clear();
1202 }
1203
1204 unsigned FormatAdditionalSectionFlags(std::string section_name) const override;
1205
1206 static std::vector<Linker::OptionDescription<void> *> ParameterNames;
1207 std::vector<Linker::OptionDescription<void> *> GetLinkerScriptParameterNames() override;
1208
1209 std::shared_ptr<Linker::OptionCollector> GetOptions() override;
1210
1211 void SetOptions(std::map<std::string, std::string>& options) override;
1212
1214 enum
1215 {
1230
1231 OMAGIC = 0x0107,
1232 NMAGIC = 0x0108,
1236 ZMAGIC = 0x010B,
1237 };
1238
1239 void OnNewSegment(std::shared_ptr<Linker::Segment> segment) override;
1240
1241 void CreateDefaultSegments();
1242
1243 std::unique_ptr<Script::List> GetScript(Linker::Module& module);
1244
1245 void Link(Linker::Module& module);
1246
1248 std::shared_ptr<Linker::Segment> GetSegment(std::unique_ptr<Section>& section);
1249
1250 std::shared_ptr<Linker::Segment> GetCodeSegment();
1251
1252 std::shared_ptr<Linker::Segment> GetDataSegment();
1253
1254 std::shared_ptr<Linker::Segment> GetBssSegment();
1255
1256 void ProcessModule(Linker::Module& module) override;
1257
1258 void CalculateValues() override;
1259
1260 void GenerateFile(std::string filename, Linker::Module& module) override;
1261
1263 std::string GetDefaultExtension(Linker::Module& module, std::string filename) const override;
1264 };
1265
1266}
1267
1268#endif /* COFF_H */
A standard 28 byte a.out optional header, used by DJGPP.
Definition coff.h:664
uint32_t code_address
unused (COFF name: text_start)
Definition coff.h:694
uint16_t version_stamp
unused (COFF name: vstamp)
Definition coff.h:673
uint32_t entry_address
Initial value of eip (COFF name: entry)
Definition coff.h:690
uint16_t magic
Type of executable, most typically ZMAGIC (COFF name: magic)
Definition coff.h:669
uint32_t code_size
unused (COFF name: tsize)
Definition coff.h:678
uint32_t GetSize() const override
Returns size of optional header.
Definition coff.cc:709
uint32_t data_size
unused (COFF name: dsize)
Definition coff.h:682
uint32_t bss_size
unused (COFF name: bsize)
Definition coff.h:686
uint32_t data_address
unused (COFF name: data_start)
Definition coff.h:698
ECOFF optional header.
Definition coff.h:823
uint64_t bss_address
Base address for bss segment (ECOFF name: bss_start)
Definition coff.h:869
uint32_t gpr_mask
unused (ECOFF name: gprmask)
Definition coff.h:871
uint16_t magic
Type of executable.
Definition coff.h:831
uint64_t data_size
Size of data segment (ECOFF name: dsize)
Definition coff.h:849
uint32_t GetSize() const override
Returns size of optional header.
Definition coff.cc:947
uint16_t version_stamp
Object file version stamp (ECOFF name: vstamp)
Definition coff.h:836
uint64_t bss_size
Size of bss segment (ECOFF name: bsize)
Definition coff.h:853
uint32_t fpr_mask
unused (ECOFF name: fprmask)
Definition coff.h:873
uint64_t global_pointer
Initial global pointer value (ECOFF name: gp_value)
Definition coff.h:875
uint64_t data_address
Base address for data segment (ECOFF name: data_start)
Definition coff.h:865
uint64_t code_address
Base address for code segment (ECOFF name: text_start)
Definition coff.h:861
uint16_t build_revision
Revision build of system tools (ECOFF name: bldrev)
Definition coff.h:840
uint64_t code_size
Size of code segment (ECOFF name: tsize)
Definition coff.h:845
uint64_t entry_address
Virtual address of execution start (ECOFF name: entry)
Definition coff.h:857
Concurrent DOS 68K/FlexOS 386 optional header Concurrent DOS 68K uses the typical a....
Definition coff.h:725
uint32_t GetSize() const override
Returns size of optional header.
Definition coff.cc:794
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:821
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:826
uint32_t stack_size
Size of stack for execution.
Definition coff.h:734
uint32_t relocations_offset
The offset to the crunched relocation data within the file.
Definition coff.h:730
GNU a.out optional header TODO.
Definition coff.h:759
uint32_t GetSize() const override
Returns size of optional header.
Definition coff.cc:840
56 byte long header used on MIPS
Definition coff.h:788
uint32_t GetSize() const override
Returns size of optional header.
Definition coff.cc:901
An abstract class to represent the optional header.
Definition coff.h:599
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:672
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:676
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
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
@ DATA
Section contains initialized data (COFF name: STYP_DATA)
Definition coff.h:533
@ BSS
Section contains uninitialized data (COFF name: STYP_BSS)
Definition coff.h:535
@ TEXT
Section contains executable (COFF name: STYP_TEXT)
Definition coff.h:531
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
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:640
uint32_t GetSize() const override
Returns size of optional header.
Definition coff.cc:684
XCOFF optional header.
Definition coff.h:899
uint16_t code_align
Maximum alignment of text (XCOFF name: algntext)
Definition coff.h:966
uint16_t version_stamp
Object file version stamp (XCOFF name: vstamp)
Definition coff.h:910
bool is64
False for XCOFF32, true for XCOFF64.
Definition coff.h:902
uint64_t maximum_stack_size
Maximum stack size (XCOFF name: maxstack)
Definition coff.h:986
uint16_t magic
Type of executable (XCOFF name: mflag)
Definition coff.h:906
uint64_t data_size
Size of data segment (XCOFF name: dsize)
Definition coff.h:918
uint32_t xcoff64_flags
XCOFF64 specific flags (XCOFF64 name: x64flags)
Definition coff.h:1022
uint16_t toc_section
Section number for TOC (XCOFF name: sntoc)
Definition coff.h:954
uint32_t debugger_data
Reserved for debugger (XCOFF name: debugger)
Definition coff.h:994
uint64_t code_size
Size of code segment (XCOFF name: tsize)
Definition coff.h:914
uint16_t entry_section
Section number for entry point (XCOFF name: snentry)
Definition coff.h:942
uint8_t text_page_size
Requested data page size (XCOFF name: datapsize)
Definition coff.h:1002
uint64_t data_address
Base address for data segment (XCOFF name: data_start)
Definition coff.h:934
uint16_t tbss_section
Section number for tbss (XCOFF name: sntbss)
Definition coff.h:1018
uint16_t loader_section
Section number for loader data (XCOFF name: snloader)
Definition coff.h:958
uint64_t bss_size
Size of bss segment (XCOFF name: bsize)
Definition coff.h:922
uint8_t cpu_type
CPU type (XCOFF name: cputype)
Definition coff.h:982
uint8_t stack_page_size
Requested stack page size (XCOFF name: stackpsize)
Definition coff.h:1006
uint8_t cpu_flags
CPU flags (XCOFF name: cpuflag)
Definition coff.h:978
uint16_t module_type
Module type (XCOFF name: modtype)
Definition coff.h:974
uint16_t data_section
Section number for data (XCOFF name: sndata)
Definition coff.h:950
uint64_t code_address
Base address for code segment (XCOFF name: text_start)
Definition coff.h:930
uint16_t data_align
Maximum alignment of data (XCOFF name: algndata)
Definition coff.h:970
uint16_t code_section
Section number for text (XCOFF name: sntext)
Definition coff.h:946
uint64_t entry_address
Virtual address of execution start (XCOFF name: entry)
Definition coff.h:926
uint64_t toc_address
Address of TOC (XCOFF name: toc)
Definition coff.h:938
uint8_t flags
Flags.
Definition coff.h:1010
uint8_t code_page_size
Requested code page size (XCOFF name: textpsize)
Definition coff.h:998
uint32_t GetSize() const override
Returns size of optional header.
Definition coff.cc:998
uint16_t bss_section
Section number for bss (XCOFF name: snbss)
Definition coff.h:962
uint16_t tdata_section
Section number for tdata (XCOFF name: sntdata)
Definition coff.h:1014
uint64_t maximum_data_size
Maximum data size (XCOFF name: maxdata)
Definition coff.h:990
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:1139
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:1685
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
void CalculateValues() override
Intermediate step between processing module and generating output file to set up headers and manageme...
Definition coff.cc:2319
format_type
Represents the type of target system, which will determine the CPU type and several other fields.
Definition coff.h:1148
@ CDOS386
FlexOS 386 executable (unknown)
Definition coff.h:1164
@ GENERIC
An unspecified value, probably will not work.
Definition coff.h:1152
@ DJGPP
DJGPP COFF executable.
Definition coff.h:1156
@ CDOS68K
Concurrent DOS 68K executable (untested but confident)
Definition coff.h:1160
uint32_t optional_header_size
The size of the optional header (COFF: f_opthdr)
Definition coff.h:585
@ FLAG_NO_SYMBOLS
F_LSYMS.
Definition coff.h:1223
@ FLAG_NO_RELOCATIONS
F_RELFLG.
Definition coff.h:1217
@ FLAG_32BIT_LITTLE_ENDIAN
F_AR32WR.
Definition coff.h:1227
@ ZMAGIC
Stored as the magic of the a.out header.
Definition coff.h:1236
@ FLAG_32BIT_BIG_ENDIAN
F_AR32W.
Definition coff.h:1229
@ FLAG_NO_LINE_NUMBERS
F_LNNO.
Definition coff.h:1221
@ FLAG_PDP11_ENDIAN
F_AR16WR.
Definition coff.h:1225
@ FLAG_EXECUTABLE
F_EXEC.
Definition coff.h:1219
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:634
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:2051
std::vector< Linker::OptionDescription< void > * > GetLinkerScriptParameterNames() override
Returns a list of the parameters used in the linker scripts, used for documentation.
Definition coff.cc:2046
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition coff.cc:1531
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition coff.cc:1442
format_type type
A representation of the format to generate.
Definition coff.h:1169
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:581
bool FormatRequiresDataStreamFix() const override
Whether the generated file might contain bugs that require fixing.
Definition coff.cc:1680
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:1062
uint32_t relocations_offset
Concurrent DOS 68K: Offset to relocations.
Definition coff.h:1192
bool option_no_relocation
Suppress relocation generation, only relevant for Concurrent DOS 68K, since the other target formats ...
Definition coff.h:1174
uint32_t stub_size
Size of MZ stub, only used for DJGPP COFF executables.
Definition coff.h:1179
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:1184
void SetupOptions(std::shared_ptr< Linker::OutputFormat > format) override
Initializes the reader for linking purposes.
Definition coff.cc:1675
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition coff.cc:1212
void GenerateFile(std::string filename, Linker::Module &module) override
The main function that handles processing, calculating and generating the final image.
Definition coff.cc:2421
uint16_t section_count
Section count (COFF name: f_nscns)
Definition coff.h:565
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:2224
uint16_t target
TI system target ID.
Definition coff.h:593
uint32_t entry_address
Entry address, gets stored in optional header later.
Definition coff.h:1188
std::vector< std::unique_ptr< Section > > sections
The list of COFF sections.
Definition coff.h:560
uint32_t symbol_count
The number of symbols (COFF name: f_nsyms)
Definition coff.h:577
void SetOptions(std::map< std::string, std::string > &options) override
Passes command line parameters as settings over to format object.
Definition coff.cc:2056
void ProcessModule(Linker::Module &module) override
Processes the module object and initializes format fields.
Definition coff.cc:2259
::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:573
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:2066
std::string GetDefaultExtension(Linker::Module &module, std::string filename) const override
Appends a default extension to the filename.
Definition coff.cc:2459
offset_t ImageSize() const override
Retrieves size of stored data.
Definition coff.cc:1454
std::unique_ptr< OptionalHeader > optional_header
The optional header instance used for reading/writing the COFF file.
Definition coff.h:629
uint16_t flags
COFF flags, such as whether the file is executable (f_flags)
Definition coff.h:589
cpu cpu_type
The CPU type, reflected by the first 16-bit word of a COFF file.
Definition coff.h:1057
uint32_t timestamp
Time stamp, unused (COFF name: f_timdat)
Definition coff.h:569
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:308
Documents and handles command line options.
Definition options.h:196
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