RetroLinker
Linker for several 8-bit, 16-bit and 32-bit formats
Loading...
Searching...
No Matches
macos.h
1#ifndef MACOS_H
2#define MACOS_H
3
4#include <filesystem>
5#include <optional>
6#include <set>
7#include <vector>
8#include "../dumper/dumper.h"
9#include "../linker/module.h"
10#include "../linker/segment.h"
11#include "../linker/segment_manager.h"
12#include "../linker/writer.h"
13
14/* Classic 68000 Mac OS file formats */
15
16namespace Apple
17{
18 typedef char OSType[4];
19
20 static constexpr uint32_t OSTypeToUInt32(char type0, char type1, char type2, char type3)
21 {
22 return
23 (uint32_t(uint8_t(type0)) << 24)
24 | (uint32_t(uint8_t(type1)) << 16)
25 | (uint32_t(uint8_t(type2)) << 8)
26 | uint32_t(uint8_t(type3));
27 }
28
29 uint32_t OSTypeToUInt32(const OSType& type);
30 void UInt32ToOSType(OSType& type, uint32_t value);
31
32 /* TODO: rework with Linker::Format */
33
46 {
47 public:
48 offset_t ImageSize() const override;
49 void ReadFile(Linker::Reader& rd) override;
50
51 bool FormatSupportsResources() const override;
52
53 enum format_type
54 {
55 SINGLE = 0x00051600,
56 DOUBLE = 0x00051607,
57 };
58 format_type type = DOUBLE;
59 unsigned version = 2;
60
61 /* Only relevant for version 1 */
62 enum hfs_type
63 {
64 HFS_UNDEFINED,
65 HFS_Macintosh,
66 HFS_ProDOS,
67 HFS_MSDOS,
68 HFS_UNIX,
69 HFS_VAX_VMS,
70 };
71 protected:
72 hfs_type home_file_system;
73 public:
74 hfs_type GetHomeFileSystem() const;
75 void SetHomeFileSystem(hfs_type type);
76
77 char home_file_system_string[16] = "";
78
79 class Entry : public virtual Linker::Format
80 {
81 public:
82 const uint32_t id;
83 offset_t file_offset = 0;
84 offset_t image_size = 0;
85 protected:
86 Entry(uint32_t id)
87 : id(id)
88 {
89 }
90 public:
91 offset_t ImageSize() const override = 0;
92 static std::shared_ptr<Entry> ReadEntry(Linker::Reader& rd, hfs_type home_file_system);
93 void ReadFile(Linker::Reader& rd) override = 0;
95 offset_t WriteFile(Linker::Writer& out) const override = 0;
96 void Dump(Dumper::Dumper& dump) const override = 0;
97
98 void DumpEntry(Dumper::Dumper& dump, unsigned index) const;
99
100 virtual void ProcessModule(Linker::Module& module);
101 virtual void CalculateValues();
102 };
103
104 class UnknownEntry : public Entry
105 {
106 public:
107 std::shared_ptr<Linker::Image> image;
108
109 UnknownEntry(uint32_t id)
110 : Entry(id)
111 {
112 }
113
114 offset_t ImageSize() const override;
115
116 void ReadFile(Linker::Reader& rd) override;
117
119 offset_t WriteFile(Linker::Writer& out) const override;
120
121 void Dump(Dumper::Dumper& dump) const override;
122
123 void CalculateValues() override;
124 };
125
126 private:
127 static const char TXT_undefined[16];
128 static const char TXT_Macintosh[16];
129 static const char TXT_ProDOS[16];
130 static const char TXT_MS_DOS[16];
131 static const char TXT_UNIX[16];
132 static const char TXT_VAX_VMS[16];
133
134 public:
135 std::vector<std::shared_ptr<Entry>> entries;
136 offset_t image_size = offset_t(-1);
137
138 enum
139 {
140 ID_DataFork = 1,
141 ID_ResourceFork,
142 ID_RealName,
143 ID_Comment,
144 ID_IconBW,
145 ID_IconColor,
146 ID_FileInfo, /* v1 only */
147 ID_FileDatesInfo, /* v2 only */
148 ID_FinderInfo,
149 ID_MacintoshFileInfo, /* v2 only */
150 ID_ProDOSFileInfo, /* v2 only */
151 ID_MSDOSFileInfo, /* v2 only */
152 ID_AFPShortName, /* v2 only */
153 ID_AFPFileInfo, /* v2 only */
154 ID_AFPDirectoryID, /* v2 only */
155 };
156
157 explicit AppleSingleDouble()
158 {
159 SetHomeFileSystem(HFS_UNDEFINED);
160 }
161
162 AppleSingleDouble(format_type type, unsigned version, hfs_type home_file_system)
163 : type(type), version(version)
164 {
165 assert(type == SINGLE || type == DOUBLE);
166 SetHomeFileSystem(version == 1 ? home_file_system : HFS_UNDEFINED);
167 }
168
169 AppleSingleDouble(format_type type, hfs_type home_file_system)
170 : type(type), version(1), home_file_system(home_file_system)
171 {
172 assert(type == SINGLE || type == DOUBLE);
173 SetHomeFileSystem(home_file_system);
174 }
175
176 AppleSingleDouble(format_type type, unsigned version = 2)
177 : type(type), version(version), home_file_system(HFS_UNDEFINED)
178 {
179 assert(type == SINGLE || type == DOUBLE);
180 SetHomeFileSystem(HFS_UNDEFINED);
181 }
182
183 /* TODO: a destructor might remove the entries of the other object as well */
184 explicit AppleSingleDouble(AppleSingleDouble& other, format_type type)
185 : type(type), version(other.version), home_file_system(other.home_file_system)
186 {
187 SetHomeFileSystem(other.GetHomeFileSystem());
188 if(type == SINGLE && other.type == DOUBLE)
189 {
190 GetDataFork();
191 }
192 for(auto entry : other.entries)
193 {
194 if(type == DOUBLE && entry->id == ID_DataFork)
195 continue;
196 entries.push_back(entry);
197 }
198 }
199
200 explicit AppleSingleDouble(AppleSingleDouble& other)
201 : type(other.type), version(other.version), home_file_system(other.home_file_system)
202 {
203 for(auto entry : other.entries)
204 {
205 entries.push_back(entry);
206 }
207 }
208
209 void SetOptions(std::map<std::string, std::string>& options) override;
210 std::vector<Linker::OptionDescription<void>> GetMemoryModelNames() override;
211 void SetModel(std::string model) override;
212 void SetLinkScript(std::string script_file, std::map<std::string, std::string>& options) override;
213
214 std::shared_ptr<const Entry> FindEntry(uint32_t id) const;
215 std::shared_ptr<Entry> FindEntry(uint32_t id);
216
217 protected:
218 std::shared_ptr<Entry> GetFileDatesInfo();
219 std::shared_ptr<Entry> GetMacintoshFileInfo();
220 std::shared_ptr<Entry> GetAUXFileInfo();
221 std::shared_ptr<Entry> GetProDOSFileInfo();
222 std::shared_ptr<Entry> GetMSDOSFileInfo();
223
224 std::shared_ptr<Entry> GetDataFork();
225 std::shared_ptr<Entry> GetResourceFork();
226 std::shared_ptr<Entry> GetFinderInfo();
227
228 public:
229 void SetCreationDate(uint32_t CreationDate);
230 void SetModificationDate(uint32_t ModificationDate);
231 void SetBackupDate(uint32_t BackupDate);
232 void SetAccessDate(uint32_t AccessDate);
233 void SetMacintoshAttributes(uint32_t Attributes);
234 void SetProDOSAccess(uint16_t Access);
235 void SetProDOSFileType(uint16_t FileType);
236 void SetProDOSAUXType(uint32_t AUXType);
237 void SetMSDOSAttributes(uint16_t Attributes);
238
239 uint32_t GetCreationDate();
240 uint32_t GetModificationDate();
241 uint32_t GetMacintoshAttributes();
242
243 void ProcessModule(Linker::Module& module) override;
244 void CalculateValues() override;
246 offset_t WriteFile(Linker::Writer& wr) const override;
247 void Dump(Dumper::Dumper& dump) const override;
248
249 std::string PrefixFilename(std::string prefix, std::string filename);
250 std::string PrefixFilename(std::string prefix, std::string filename, size_t limit);
251 std::string ReplaceExtension(std::string filename, std::string extension, size_t limit);
252
253 std::string GetUNIXDoubleFilename(std::string filename);
254 std::string GetMacOSXDoubleFilename(std::string filename);
255 std::string GetProDOSDoubleFilename(std::string filename);
256 std::string GetMSDOSDoubleFilename(std::string filename);
257
258 void GenerateFile(std::string filename, Linker::Module& module) override;
259
261 std::string GetDefaultExtension(Linker::Module& module) const override;
262 };
263
265 {
266 public:
267 std::shared_ptr<Linker::Image> image;
268
269 DataFork()
270 : Entry(AppleSingleDouble::ID_DataFork)
271 {
272 }
273
274 offset_t ImageSize() const override;
275
276 void ReadFile(Linker::Reader& rd) override;
277
279 offset_t WriteFile(Linker::Writer& out) const override;
280
281 void Dump(Dumper::Dumper& dump) const override;
282
283 void CalculateValues() override;
284 };
285
294 class ResourceFork : public virtual AppleSingleDouble::Entry, public virtual Linker::SegmentManager
295 {
296 public:
297 bool FormatSupportsResources() const override
298 {
299 return true;
300 }
301
302 enum memory_model_t
303 {
304 MODEL_DEFAULT,
305 MODEL_TINY,
306 };
307 memory_model_t memory_model = MODEL_DEFAULT;
308
309 void SetOptions(std::map<std::string, std::string>& options) override;
310
311 static std::vector<Linker::OptionDescription<void>> MemoryModelNames;
312 std::vector<Linker::OptionDescription<void>> GetMemoryModelNames() override;
313 void SetModel(std::string model) override;
314
316 {
317 public:
319 virtual void ReadFile(Linker::Reader& rd, offset_t length) = 0;
320 void Dump(Dumper::Dumper& dump) const override;
321 virtual void Dump(Dumper::Dumper& dump, offset_t file_offset) const;
322 virtual void AddFields(Dumper::Dumper& dump, Dumper::Region& region, offset_t file_offset) const;
323 virtual std::unique_ptr<Dumper::Region> CreateRegion(std::string name, offset_t offset, offset_t length, unsigned display_width) const;
324
325 protected:
326 Resource(const char type[4], uint16_t id, uint8_t attributes = 0)
327 : id(id), attributes(attributes)
328 {
329 memcpy(this->type, type, 4);
330 }
331
332 Resource(const char type[4], uint16_t id, std::string name, uint8_t attributes = 0)
333 : id(id), name(name), attributes(attributes)
334 {
335 memcpy(this->type, type, 4);
336 }
337
338 public:
339 char type[4];
340 uint16_t id;
341
342 std::optional<std::string> name;
343 uint8_t attributes;
344
345 offset_t ImageSize() const override = 0;
346 };
347
349 {
350 public:
351 GenericResource(const char type[4], uint16_t id)
352 : Resource(type, id)
353 {
354 }
355
356 std::shared_ptr<Linker::Image> image;
357
358 void ProcessModule(Linker::Module& module) override;
359
360 void CalculateValues() override;
361
362 offset_t ImageSize() const override;
363
364 void ReadFile(Linker::Reader& rd) override;
365 void ReadFile(Linker::Reader& rd, offset_t length) override;
366
368 offset_t WriteFile(Linker::Writer& wr) const override;
369 using Resource::Dump;
370 std::unique_ptr<Dumper::Region> CreateRegion(std::string name, offset_t offset, offset_t length, unsigned display_width) const override;
371 };
372
374 {
375 public:
377 : Resource("CODE", 0)
378 {
379 }
380
381 struct Entry
382 {
383 uint16_t segment;
384 uint32_t offset;
385 };
386
387 uint32_t above_a5 = 0;
388 uint32_t below_a5 = 0;
389 uint32_t jump_table_offset = 32;
390 std::vector<Entry> near_entries;
391 std::vector<Entry> far_entries;
392
393 void ProcessModule(Linker::Module& module) override;
394
395 void CalculateValues() override;
396
397 offset_t ImageSize() const override;
398
399 enum
400 {
401 MOVE_DATA_SP = 0x3F3C,
402 LOADSEG = 0xA9F0,
403 };
404
405 void ReadFile(Linker::Reader& rd) override;
406 void ReadFile(Linker::Reader& rd, offset_t length) override;
407
409 offset_t WriteFile(Linker::Writer& wr) const override;
411 void Dump(Dumper::Dumper& dump, offset_t file_offset) const override;
412 void AddFields(Dumper::Dumper& dump, Dumper::Region& region, offset_t file_offset) const override;
413 };
414
415 class CodeResource : public Resource
416 {
417 public:
418 static constexpr uint32_t OSType = OSTypeToUInt32('C', 'O', 'D', 'E');
419
420 std::shared_ptr<JumpTableCodeResource> jump_table;
421 std::shared_ptr<Linker::Image> image;
422 uint32_t zero_fill = 0; // used for code generation
423
424 CodeResource(uint16_t id, std::shared_ptr<JumpTableCodeResource> jump_table = nullptr)
425 : Resource("CODE", id), jump_table(jump_table)/*, image("code")*/
426 {
427 }
428
429 bool is_far = false; /* TODO: test far segments thoroughly */
430 uint32_t a5_address = 0; /* TODO: meaning */
431 uint32_t base_address = 0; /* TODO: meaning */
432
433 // used for generation
434 std::set<uint16_t> near_entries;
435 uint16_t near_entry_count = 0;
436 // used for generation
437 std::set<uint32_t> far_entries;
438 uint16_t far_entry_count = 0;
439 std::set<uint32_t> a5_relocations;
440 std::set<uint32_t> segment_relocations;
441
442 /* filled in after calculation */
443 uint32_t first_near_entry_offset;
444 uint32_t first_far_entry_offset;
445 uint32_t a5_relocation_offset;
446 uint32_t segment_relocation_offset;
447 uint32_t resource_size;
448
449 void ProcessModule(Linker::Module& module) override;
450
451 void CalculateValues() override;
452
453 offset_t ImageSize() const override;
454
455 void ReadFile(Linker::Reader& rd) override;
456 void ReadFile(Linker::Reader& rd, offset_t length) override;
457
458 uint32_t MeasureRelocations(std::set<uint32_t>& relocations) const;
459
460 void ReadRelocations(Linker::Reader& rd, std::set<uint32_t>& relocations) const;
461 void WriteRelocations(Linker::Writer& wr, const std::set<uint32_t>& relocations) const;
462
464 offset_t WriteFile(Linker::Writer& wr) const override;
466 void Dump(Dumper::Dumper& dump, offset_t file_offset) const override;
467 void AddFields(Dumper::Dumper& dump, Dumper::Region& region, offset_t file_offset) const override;
468 std::unique_ptr<Dumper::Region> CreateRegion(std::string name, offset_t offset, offset_t length, unsigned display_width) const override;
469 };
470
472 : Entry(AppleSingleDouble::ID_ResourceFork)/*, a5world(".bss")*/
473 {
474 }
475
477 {
478 uint16_t id = 0;
479 uint16_t name_offset = 0xFFFF;
480 std::optional<std::string> name;
481 uint8_t attributes = 0;
482 uint32_t data_offset = 0;
483 std::shared_ptr<Resource> data;
484 };
485
487 {
488 OSType type { 0, 0, 0, 0 };
489 uint32_t count = 0;
490 uint16_t offset = 0;
491 std::vector<ResourceReference> references;
492 };
493
494 static std::shared_ptr<Resource> ReadResource(Linker::Reader& rd, const ResourceType& type, const ResourceReference& reference);
495
496 uint16_t attributes = 0; /* TODO: parametrize */
498 std::vector<ResourceType> resource_types;
500 std::vector<std::string> resource_names;
501
503 std::map<uint32_t, std::map<uint16_t, std::shared_ptr<Resource>>> resources;
504
505 /* these will be calculated */
506 uint32_t data_offset = 0, data_length = 0, map_offset = 0, map_length = 0;
507 uint16_t resource_type_list_offset = 28;
508 uint16_t name_list_offset = 0;
509
510 /* filled in during generation */
511 std::shared_ptr<JumpTableCodeResource> jump_table;
512 std::vector<std::shared_ptr<CodeResource>> codes;
513 std::map<std::shared_ptr<Linker::Segment>, std::shared_ptr<CodeResource>> segments;
514 std::shared_ptr<Linker::Segment> a5world;
515
516 void AddResource(std::shared_ptr<Resource> resource);
517
518 void OnNewSegment(std::shared_ptr<Linker::Segment> segment) override;
519
520 std::unique_ptr<Script::List> GetScript(Linker::Module& module);
521
522 void Link(Linker::Module& module);
523
524 void ProcessModule(Linker::Module& module) override;
525
526 void CalculateValues() override;
527
528 offset_t ImageSize() const override;
529
530 void ReadFile(Linker::Reader& rd) override;
531
533 offset_t WriteFile(Linker::Writer& wr) const override;
534
535 void Dump(Dumper::Dumper& dump) const override;
536
537 void GenerateFile(std::string filename, Linker::Module& module) override;
538
540 std::string GetDefaultExtension(Linker::Module& module) const override;
541 };
542
544 {
545 public:
546 std::string name;
547
548 RealName(std::string name = "")
549 : Entry(AppleSingleDouble::ID_RealName), name(name)
550 {
551 }
552
553 offset_t ImageSize() const override;
554
555 void ReadFile(Linker::Reader& rd) override;
556
558 offset_t WriteFile(Linker::Writer& wr) const override;
559
560 void Dump(Dumper::Dumper& dump) const override;
561 };
562
564 {
565 public:
566 Comment()
567 : Entry(AppleSingleDouble::ID_Comment)
568 {
569 }
570 /* TODO - this is a stub */
571
572 offset_t ImageSize() const override;
573
574 void ReadFile(Linker::Reader& rd) override;
575
577 offset_t WriteFile(Linker::Writer& out) const override;
578
579 void Dump(Dumper::Dumper& dump) const override;
580 };
581
583 {
584 public:
585 IconBW()
586 : Entry(AppleSingleDouble::ID_IconBW)
587 {
588 }
589 /* TODO - this is a stub */
590
591 offset_t ImageSize() const override;
592
593 void ReadFile(Linker::Reader& rd) override;
595
596 offset_t WriteFile(Linker::Writer& out) const override;
597
598 void Dump(Dumper::Dumper& dump) const override;
599 };
600
602 {
603 public:
604 IconColor()
605 : Entry(AppleSingleDouble::ID_IconColor)
606 {
607 }
608 /* TODO - this is a stub */
609
610 offset_t ImageSize() const override;
611
612 void ReadFile(Linker::Reader& rd) override;
614
615 offset_t WriteFile(Linker::Writer& out) const override;
616
617 void Dump(Dumper::Dumper& dump) const override;
618 };
619
620 /* Version 1 only */
622 {
623 protected:
624 FileInfo()
625 : Entry(AppleSingleDouble::ID_FileInfo)
626 {
627 }
628
629 public:
630 class Macintosh;
631 class ProDOS;
632 class MSDOS;
633 class AUX;
634 };
635
637 {
638 public:
639 uint32_t CreationDate;
640 uint32_t ModificationDate;
641 uint32_t LastBackupDate;
642 uint32_t Attributes;
643
644 Macintosh(uint32_t CreationDate = 0,
645 uint32_t ModificationDate = 0,
646 uint32_t LastBackupDate = 0,
647 uint32_t Attributes = 0)
648 : CreationDate(CreationDate), ModificationDate(ModificationDate), LastBackupDate(LastBackupDate), Attributes(Attributes)
649 {
650 }
651
652 offset_t ImageSize() const override;
653
654 void ReadFile(Linker::Reader& rd) override;
655
657 offset_t WriteFile(Linker::Writer& wr) const override;
658
659 void Dump(Dumper::Dumper& dump) const override;
660 };
661
663 {
664 public:
665 uint32_t CreationDate;
666 uint32_t ModificationDate;
667 uint16_t Access;
668 uint16_t FileType;
669 uint32_t AUXType;
670
671 ProDOS(uint32_t CreationDate = 0,
672 uint32_t ModificationDate = 0,
673 uint16_t Access = 0,
674 uint16_t FileType = 0,
675 uint32_t AUXType = 0)
676 : CreationDate(CreationDate), ModificationDate(ModificationDate), Access(Access), FileType(FileType), AUXType(AUXType)
677 {
678 }
679
680 offset_t ImageSize() const override;
681
682 void ReadFile(Linker::Reader& rd) override;
683
685 offset_t WriteFile(Linker::Writer& wr) const override;
686
687 void Dump(Dumper::Dumper& dump) const override;
688 };
689
691 {
692 public:
693 uint32_t ModificationDate;
694 uint16_t Attributes;
695
696 MSDOS(uint32_t ModificationDate = 0,
697 uint16_t Attributes = 0)
698 : ModificationDate(ModificationDate), Attributes(Attributes)
699 {
700 }
701
702 offset_t ImageSize() const override;
703
704 void ReadFile(Linker::Reader& rd) override;
705
707 offset_t WriteFile(Linker::Writer& wr) const override;
708
709 void Dump(Dumper::Dumper& dump) const override;
710 };
711
712 class FileInfo::AUX : public FileInfo
713 {
714 public:
715 uint32_t CreationDate;
716 uint32_t AccessDate;
717 uint32_t ModificationDate;
718
719 AUX(uint32_t CreationDate = 0,
720 uint32_t AccessDate = 0,
721 uint32_t ModificationDate = 0)
722 : CreationDate(CreationDate), AccessDate(AccessDate), ModificationDate(ModificationDate)
723 {
724 }
725
726 offset_t ImageSize() const override;
727
728 void ReadFile(Linker::Reader& rd) override;
729
731 offset_t WriteFile(Linker::Writer& wr) const override;
732
733 void Dump(Dumper::Dumper& dump) const override;
734 };
735
736 /* Version 2 only */
738 {
739 public:
740 uint32_t CreationDate;
741 uint32_t ModificationDate;
742 uint32_t BackupDate;
743 uint32_t AccessDate;
744
746 uint32_t CreationDate = 0,
747 uint32_t ModificationDate = 0,
748 uint32_t BackupDate = 0,
749 uint32_t AccessDate = 0)
750 : Entry(AppleSingleDouble::ID_FileDatesInfo),
751 CreationDate(CreationDate), ModificationDate(ModificationDate), BackupDate(BackupDate), AccessDate(AccessDate)
752 {
753 }
754
755 offset_t ImageSize() const override;
756
757 void ReadFile(Linker::Reader& rd) override;
758
760 offset_t WriteFile(Linker::Writer& wr) const override;
761
762 void Dump(Dumper::Dumper& dump) const override;
763 };
764
766 {
767 public:
768 struct Point
769 {
770 uint16_t x, y;
771 };
772
773 char Type[4] = { '?', '?', '?', '?' };
774 char Creator[4] = { '?', '?', '?', '?' };
775 uint16_t Flags = 0;
776 Point Location = { 0, 0 };
777
778 FinderInfo()
779 : Entry(AppleSingleDouble::ID_FinderInfo)
780 {
781 }
782
783 offset_t ImageSize() const override;
784
785 void ReadFile(Linker::Reader& rd) override;
786
788 offset_t WriteFile(Linker::Writer& wr) const override;
789
790 void Dump(Dumper::Dumper& dump) const override;
791
792 void ProcessModule(Linker::Module& module) override;
793 };
794
795 /* Version 2 only */
797 {
798 public:
799 uint32_t Attributes;
800 MacintoshFileInfo(uint32_t Attributes = 0)
801 : Entry(AppleSingleDouble::ID_MacintoshFileInfo), Attributes(Attributes)
802 {
803 }
804
805 offset_t ImageSize() const override;
806
807 void ReadFile(Linker::Reader& rd) override;
808
810 offset_t WriteFile(Linker::Writer& wr) const override;
811
812 void Dump(Dumper::Dumper& dump) const override;
813 };
814
815 /* Version 2 only */
817 {
818 public:
819 uint16_t Access;
820 uint16_t FileType;
821 uint32_t AUXType;
822
823 ProDOSFileInfo(uint16_t Access = 0,
824 uint16_t FileType = 0,
825 uint32_t AUXType = 0)
826 : Entry(AppleSingleDouble::ID_ProDOSFileInfo), Access(Access), FileType(FileType), AUXType(AUXType)
827 {
828 }
829
830 offset_t ImageSize() const override;
831
832 void ReadFile(Linker::Reader& rd) override;
833
835 offset_t WriteFile(Linker::Writer& wr) const override;
836
837 void Dump(Dumper::Dumper& dump) const override;
838 };
839
840 /* Version 2 only */
842 {
843 public:
844 uint16_t Attributes;
845
846 MSDOSFileInfo(uint16_t Attributes = 0)
847 : Entry(AppleSingleDouble::ID_MSDOSFileInfo), Attributes(Attributes)
848 {
849 }
850
851 offset_t ImageSize() const override;
852
853 void ReadFile(Linker::Reader& rd) override;
854
856 offset_t WriteFile(Linker::Writer& wr) const override;
857
858 void Dump(Dumper::Dumper& dump) const override;
859 };
860
861 /* Version 2 only */
863 {
864 public:
866 : Entry(AppleSingleDouble::ID_AFPShortName)
867 {
868 }
869 /* TODO - this is a stub */
870
871 offset_t ImageSize() const override;
872
873 void ReadFile(Linker::Reader& rd) override;
874
876 offset_t WriteFile(Linker::Writer& wr) const override;
877
878 void Dump(Dumper::Dumper& dump) const override;
879 };
880
881 /* Version 2 only */
883 {
884 public:
886 : Entry(AppleSingleDouble::ID_AFPFileInfo)
887 {
888 }
889 /* TODO - this is a stub */
890
891 offset_t ImageSize() const override;
892
893 void ReadFile(Linker::Reader& rd) override;
894
896 offset_t WriteFile(Linker::Writer& wr) const override;
897
898 void Dump(Dumper::Dumper& dump) const override;
899 };
900
901 /* Version 2 only */
903 {
904 public:
906 : Entry(AppleSingleDouble::ID_AFPDirectoryID)
907 {
908 }
909 /* TODO - this is a stub */
910
911 offset_t ImageSize() const override;
912
913 void ReadFile(Linker::Reader& rd) override;
914
916 offset_t WriteFile(Linker::Writer& wr) const override;
917
918 void Dump(Dumper::Dumper& dump) const override;
919 };
920
925 {
926 public:
927 enum version_t
928 {
929 /* assigning values to the first two does not matter, because we don't generate the fields that hold them */
930 MACBIN1,
931 MACBIN1_GETINFO, /* extension */
932 MACBIN2 = 0x11,
933 MACBIN3 = 0x12,
934 };
935 version_t version, minimum_version;
936
937 uint16_t secondary_header_size = 0; /* TODO */
938 mutable uint16_t crc = 0;
939
940 uint8_t attributes = 0;
941 uint32_t creation = 0;
942 uint32_t modification = 0;
943
944 std::string generated_file_name;
945
946 MacBinary(version_t version = MACBIN3)
947 : AppleSingleDouble(AppleSingleDouble::DOUBLE), version(version), minimum_version(version <= MACBIN2 ? version : MACBIN2)
948 {
949 }
950
951 MacBinary(version_t version, version_t minimum_version)
952 : AppleSingleDouble(AppleSingleDouble::DOUBLE), version(version), minimum_version(version < minimum_version ? version : minimum_version)
953 {
954 }
955
956 explicit MacBinary(AppleSingleDouble& apple, version_t version, version_t minimum_version)
957 : AppleSingleDouble(apple, AppleSingleDouble::DOUBLE), version(version), minimum_version(version < minimum_version ? version : minimum_version)
958 {
959 }
960
961 /* CRC16-CCITT */
962 static uint16_t crc_step[256];
963
964 void CRC_Initialize() const;
965
966 void CRC_Step(uint8_t byte = 0) const;
967
968 void Skip(Linker::Writer& wr, size_t count) const;
969
970 void WriteData(Linker::Writer& wr, size_t count, const void * data) const;
971
972 void WriteData(Linker::Writer& wr, size_t count, std::string text) const;
973
974 void WriteWord(Linker::Writer& wr, size_t bytes, uint64_t value) const;
975
976 void WriteHeader(Linker::Writer& wr) const;
977
978 void CalculateValues() override;
979
980 void ReadFile(Linker::Reader& rd) override;
981
983 offset_t WriteFile(Linker::Writer& wr) const override;
984
985 void Dump(Dumper::Dumper& dump) const override;
986
987 void GenerateFile(std::string filename, Linker::Module& module) override;
988 };
989
998 {
999 public:
1000 /* format of "filename" */
1001 enum target_format_t
1002 {
1003 TARGET_NONE, /* do not generate main file */
1004 TARGET_DATA_FORK, /* main file is a data fork, typically empty */
1005 TARGET_RESOURCE_FORK, /* main file is a resource fork */
1006 TARGET_APPLE_SINGLE, /* main file is an AppleSingle */
1007 TARGET_MAC_BINARY, /* main file as a MacBinary */
1008 };
1009 target_format_t target;
1010
1011 /* other files to produce */
1012 enum produce_format_t
1013 {
1014 PRODUCE_RESOURCE_FORK = 1 << 0, /* under .rsrc */
1015 PRODUCE_FINDER_INFO = 1 << 1, /* under .finf */
1016 PRODUCE_APPLE_DOUBLE = 1 << 2, /* with % prefix */
1017 PRODUCE_MAC_BINARY = 1 << 3, /* with .mbin extension */
1018 };
1019 produce_format_t produce;
1020
1021 /* Typical combinations:
1022 * - Executor: Generate a data fork and an AppleDouble with % prefix
1023 * - Basilisk: Generate a data fork, a resource fork (under .rsrc) and a Finder Info file (under .finf)
1024 * - Generate a MacBinary with .mbin extension
1025 * - Generate an AppleSingle
1026 */
1027
1028 unsigned apple_single_double_version = 2;
1029 /* Only relevant for version 1 */
1030 AppleSingleDouble::hfs_type home_file_system = AppleSingleDouble::HFS_UNDEFINED;
1031
1032 MacBinary::version_t macbinary_version = MacBinary::MACBIN3, macbinary_minimum_version = MacBinary::MACBIN2;
1033
1034 MacDriver(target_format_t target = TARGET_DATA_FORK)
1035 : target(target),
1036 produce(target == TARGET_NONE ? PRODUCE_MAC_BINARY
1037 : target == TARGET_DATA_FORK ? PRODUCE_APPLE_DOUBLE
1038 : produce_format_t(0))
1039 {
1040 }
1041
1042 MacDriver(target_format_t target, int produce)
1043 : target(target), produce(produce_format_t(produce))
1044 {
1045 }
1046
1047 bool FormatSupportsResources() const override;
1048
1049 bool AddSupplementaryOutputFormat(std::string subformat) override;
1050
1051 private:
1052 std::variant<
1053 std::shared_ptr<ResourceFork>,
1054 std::shared_ptr<AppleSingleDouble>
1055 //std::shared_ptr<MacBinary> // stored as AppleSingleDouble
1056 > container;
1057
1058 std::shared_ptr<const ResourceFork> GetResourceFork() const;
1059 std::shared_ptr<ResourceFork> GetResourceFork();
1060 std::shared_ptr<const AppleSingleDouble> GetAppleSingleDouble() const;
1061 std::shared_ptr<AppleSingleDouble> GetAppleSingleDouble();
1062 std::shared_ptr<const MacBinary> GetMacBinary() const;
1063 std::shared_ptr<MacBinary> GetMacBinary();
1064
1065 std::map<std::string, std::string> options;
1066 std::string model;
1067 std::string script_file;
1068 std::map<std::string, std::string> script_options;
1069
1070 public:
1071 void SetOptions(std::map<std::string, std::string>& options) override;
1072
1073 std::vector<Linker::OptionDescription<void>> GetMemoryModelNames() override;
1074 void SetModel(std::string model) override;
1075
1076 void SetLinkScript(std::string script_file, std::map<std::string, std::string>& options) override;
1077
1078 void GenerateFile(std::string filename, Linker::Module& module) override;
1079
1080 void ReadFile(Linker::Reader& rd) override;
1081
1083 offset_t WriteFile(Linker::Writer& wr) const override;
1084
1085 void Dump(Dumper::Dumper& dump) const override;
1086 };
1087}
1088
1089#endif /* MACOS_H */
Definition macos.h:903
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition macos.cc:2334
offset_t ImageSize() const override
Retrieves size of stored data.
Definition macos.cc:2318
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition macos.cc:2323
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition macos.cc:2328
Definition macos.h:883
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition macos.cc:2300
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition macos.cc:2305
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition macos.cc:2311
offset_t ImageSize() const override
Retrieves size of stored data.
Definition macos.cc:2295
Definition macos.h:863
offset_t ImageSize() const override
Retrieves size of stored data.
Definition macos.cc:2272
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition macos.cc:2288
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition macos.cc:2282
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition macos.cc:2277
Definition macos.h:80
offset_t WriteFile(Linker::Writer &out) const override=0
Stores data in memory to file.
offset_t ImageSize() const override=0
Retrieves size of stored data.
void ReadFile(Linker::Reader &rd) override=0
Loads file into memory.
void Dump(Dumper::Dumper &dump) const override=0
Display file contents in a nice manner.
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition macos.cc:265
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition macos.cc:247
offset_t WriteFile(Linker::Writer &out) const override
Stores data in memory to file.
Definition macos.cc:252
offset_t ImageSize() const override
Retrieves size of stored data.
Definition macos.cc:242
AppleSingle & AppleDouble.
Definition macos.h:46
void SetOptions(std::map< std::string, std::string > &options) override
Passes command line parameters as settings over to format object.
Definition macos.cc:283
bool FormatSupportsResources() const override
Whether the format supports resources.
Definition macos.cc:121
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition macos.cc:68
void ProcessModule(Linker::Module &module) override
Processes the module object and initializes format fields.
Definition macos.cc:771
void GenerateFile(std::string filename, Linker::Module &module) override
The main function that handles processing, calculating and generating the final image.
Definition macos.cc:901
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition macos.cc:831
void SetModel(std::string model) override
Sets the way memory is organized, typically modifying a built-in script.
Definition macos.cc:295
offset_t ImageSize() const override
Retrieves size of stored data.
Definition macos.cc:31
std::vector< Linker::OptionDescription< void > > GetMemoryModelNames() override
Returns a list of the supported memory models, used for documentation.
Definition macos.cc:290
std::string GetDefaultExtension(Linker::Module &module) const override
Provides a default filename for the output file.
Definition macos.cc:937
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition macos.cc:810
void CalculateValues() override
Intermediate step between processing module and generating output file to set up headers and manageme...
Definition macos.cc:797
void SetLinkScript(std::string script_file, std::map< std::string, std::string > &options) override
Selects a script file to use for linking.
Definition macos.cc:300
Definition macos.h:564
offset_t ImageSize() const override
Retrieves size of stored data.
Definition macos.cc:1951
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition macos.cc:1967
offset_t WriteFile(Linker::Writer &out) const override
Stores data in memory to file.
Definition macos.cc:1961
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition macos.cc:1956
Definition macos.h:265
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition macos.cc:949
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition macos.cc:967
offset_t ImageSize() const override
Retrieves size of stored data.
Definition macos.cc:944
offset_t WriteFile(Linker::Writer &out) const override
Stores data in memory to file.
Definition macos.cc:954
Definition macos.h:738
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition macos.cc:2140
offset_t ImageSize() const override
Retrieves size of stored data.
Definition macos.cc:2130
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition macos.cc:2151
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition macos.cc:2135
Definition macos.h:713
offset_t ImageSize() const override
Retrieves size of stored data.
Definition macos.cc:2103
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition macos.cc:2108
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition macos.cc:2113
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition macos.cc:2123
Definition macos.h:691
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition macos.cc:2082
offset_t ImageSize() const override
Retrieves size of stored data.
Definition macos.cc:2077
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition macos.cc:2087
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition macos.cc:2096
Definition macos.h:637
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition macos.cc:2025
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition macos.cc:2030
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition macos.cc:2041
offset_t ImageSize() const override
Retrieves size of stored data.
Definition macos.cc:2020
Definition macos.h:663
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition macos.cc:2058
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition macos.cc:2070
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition macos.cc:2053
offset_t ImageSize() const override
Retrieves size of stored data.
Definition macos.cc:2048
Definition macos.h:622
Definition macos.h:766
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition macos.cc:2163
offset_t ImageSize() const override
Retrieves size of stored data.
Definition macos.cc:2158
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition macos.cc:2168
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition macos.cc:2182
Definition macos.h:583
offset_t ImageSize() const override
Retrieves size of stored data.
Definition macos.cc:1974
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition macos.cc:1979
offset_t WriteFile(Linker::Writer &out) const override
Stores data in memory to file.
Definition macos.cc:1984
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition macos.cc:1990
Definition macos.h:602
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition macos.cc:2002
offset_t ImageSize() const override
Retrieves size of stored data.
Definition macos.cc:1997
offset_t WriteFile(Linker::Writer &out) const override
Stores data in memory to file.
Definition macos.cc:2007
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition macos.cc:2013
Definition macos.h:842
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition macos.cc:2257
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition macos.cc:2252
offset_t ImageSize() const override
Retrieves size of stored data.
Definition macos.cc:2247
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition macos.cc:2265
MacBinary is an alternative format to AppleSingle for representing a Macintosh file on a non-Macintos...
Definition macos.h:925
void CalculateValues() override
Intermediate step between processing module and generating output file to set up headers and manageme...
Definition macos.cc:2496
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition macos.cc:2509
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition macos.cc:2536
void GenerateFile(std::string filename, Linker::Module &module) override
The main function that handles processing, calculating and generating the final image.
Definition macos.cc:2547
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition macos.cc:2504
This is not actually a file format, but an interface to permit generating multiple binary outputs for...
Definition macos.h:998
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition macos.cc:2805
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition macos.cc:2879
bool FormatSupportsResources() const override
Whether the format supports resources.
Definition macos.cc:2555
std::vector< Linker::OptionDescription< void > > GetMemoryModelNames() override
Returns a list of the supported memory models, used for documentation.
Definition macos.cc:2650
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition macos.cc:2859
void SetLinkScript(std::string script_file, std::map< std::string, std::string > &options) override
Selects a script file to use for linking.
Definition macos.cc:2661
bool AddSupplementaryOutputFormat(std::string subformat) override
If the output format actually drives multiple output formats (resource file, apple double,...
Definition macos.cc:2560
void SetModel(std::string model) override
Sets the way memory is organized, typically modifying a built-in script.
Definition macos.cc:2656
void GenerateFile(std::string filename, Linker::Module &module) override
The main function that handles processing, calculating and generating the final image.
Definition macos.cc:2667
void SetOptions(std::map< std::string, std::string > &options) override
Passes command line parameters as settings over to format object.
Definition macos.cc:2645
Definition macos.h:797
offset_t ImageSize() const override
Retrieves size of stored data.
Definition macos.cc:2195
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition macos.cc:2205
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition macos.cc:2200
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition macos.cc:2213
Definition macos.h:817
offset_t ImageSize() const override
Retrieves size of stored data.
Definition macos.cc:2220
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition macos.cc:2225
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition macos.cc:2240
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition macos.cc:2230
Definition macos.h:544
offset_t ImageSize() const override
Retrieves size of stored data.
Definition macos.cc:1926
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition macos.cc:1944
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition macos.cc:1931
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition macos.cc:1936
Definition macos.h:416
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition macos.cc:1355
void ProcessModule(Linker::Module &module) override
Processes the module object and initializes format fields.
Definition macos.cc:1217
void CalculateValues() override
Intermediate step between processing module and generating output file to set up headers and manageme...
Definition macos.cc:1221
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition macos.cc:1322
offset_t ImageSize() const override
Retrieves size of stored data.
Definition macos.cc:1242
offset_t ImageSize() const override
Retrieves size of stored data.
Definition macos.cc:1049
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition macos.cc:1064
void CalculateValues() override
Intermediate step between processing module and generating output file to set up headers and manageme...
Definition macos.cc:1045
void ProcessModule(Linker::Module &module) override
Processes the module object and initializes format fields.
Definition macos.cc:1041
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition macos.cc:1054
void ProcessModule(Linker::Module &module) override
Processes the module object and initializes format fields.
Definition macos.cc:1074
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition macos.cc:1139
void CalculateValues() override
Intermediate step between processing module and generating output file to set up headers and manageme...
Definition macos.cc:1078
offset_t ImageSize() const override
Retrieves size of stored data.
Definition macos.cc:1091
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition macos.cc:1103
Definition macos.h:316
offset_t ImageSize() const override=0
Retrieves size of stored data.
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition macos.cc:1015
A Macintosh resource fork.
Definition macos.h:295
std::map< uint32_t, std::map< uint16_t, std::shared_ptr< Resource > > > resources
A convenient collection of resources.
Definition macos.h:503
std::string GetDefaultExtension(Linker::Module &module) const override
Provides a default filename for the output file.
Definition macos.cc:1898
void GenerateFile(std::string filename, Linker::Module &module) override
The main function that handles processing, calculating and generating the final image.
Definition macos.cc:1888
offset_t ImageSize() const override
Retrieves size of stored data.
Definition macos.cc:1671
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 macos.cc:1418
void SetModel(std::string model) override
Sets the way memory is organized, typically modifying a built-in script.
Definition macos.cc:996
void SetOptions(std::map< std::string, std::string > &options) override
Passes command line parameters as settings over to format object.
Definition macos.cc:980
std::vector< Linker::OptionDescription< void > > GetMemoryModelNames() override
Returns a list of the supported memory models, used for documentation.
Definition macos.cc:991
bool FormatSupportsResources() const override
Whether the format supports resources.
Definition macos.h:297
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition macos.cc:1676
std::vector< std::string > resource_names
A list of all resource names, as stored in the file.
Definition macos.h:500
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition macos.cc:1809
std::vector< ResourceType > resource_types
A list of all resource types, as stored in the file.
Definition macos.h:498
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition macos.cc:1751
An abstract interface that separates structure and presentation of the data inside a file.
Definition dumper.h:586
A record that represents a region within the file.
Definition dumper.h:485
A class to encode a general file format.
Definition format.h:29
virtual void Dump(Dumper::Dumper &dump) const
Display file contents in a nice manner.
Definition format.cc:10
virtual void ReadFile(Reader &rd)=0
Loads file into memory.
offset_t WriteFile(Writer &wr) const override=0
Stores data in memory to file.
Encodes an object module file as a collection of sections, symbols and relocations.
Definition module.h:24
A class that provides a general interface to setting up generation for a format.
Definition format.h:64
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 macos.h:769
Definition macos.h:487