9#include "../dumper/dumper.h"
10#include "../linker/module.h"
11#include "../linker/segment.h"
12#include "../linker/segment_manager.h"
13#include "../linker/writer.h"
19 typedef char OSType[4];
21 static constexpr uint32_t OSTypeToUInt32(
char type0,
char type1,
char type2,
char type3)
24 (uint32_t(uint8_t(type0)) << 24)
25 | (uint32_t(uint8_t(type1)) << 16)
26 | (uint32_t(uint8_t(type2)) << 8)
27 | uint32_t(uint8_t(type3));
30 uint32_t OSTypeToUInt32(
const OSType& type);
31 void UInt32ToOSType(OSType& type, uint32_t value);
56 memory_model_t memory_model = MODEL_DEFAULT;
58 void SetOptions(std::map<std::string, std::string>& options)
override;
60 static std::vector<Linker::OptionDescription<void>> MemoryModelNames;
62 void SetModel(std::string model)
override;
70 virtual int GetDisplayOptions()
const;
73 virtual std::unique_ptr<Dumper::Region> CreateRegion(std::string name, offset_t offset, offset_t length,
unsigned display_width)
const;
76 Resource(
const char type[4], uint16_t
id, uint8_t attributes = 0)
77 : id(
id), attributes(attributes)
79 memcpy(this->type, type, 4);
82 Resource(
const char type[4], uint16_t
id, std::string name, uint8_t attributes = 0)
83 : id(
id), name(name), attributes(attributes)
85 memcpy(this->type, type, 4);
92 std::optional<std::string> name;
97 virtual void CalculateValues() = 0;
108 std::shared_ptr<Linker::Contents> image;
110 void CalculateValues()
override;
120 std::unique_ptr<Dumper::Region> CreateRegion(std::string name, offset_t offset, offset_t length,
unsigned display_width)
const override;
137 uint32_t above_a5 = 0;
138 uint32_t below_a5 = 0;
139 uint32_t jump_table_offset = 32;
140 std::vector<Entry> near_entries;
141 std::vector<Entry> far_entries;
143 void CalculateValues()
override;
149 MOVE_DATA_SP = 0x3F3C,
159 int GetDisplayOptions()
const override;
160 void Dump(
Dumper::Dumper& dump, offset_t file_offset)
const override;
167 static constexpr uint32_t OSType = OSTypeToUInt32(
'C',
'O',
'D',
'E');
169 std::shared_ptr<JumpTableCodeResource> jump_table;
170 std::shared_ptr<Linker::Contents> image;
171 uint32_t zero_fill = 0;
173 CodeResource(uint16_t
id, std::shared_ptr<JumpTableCodeResource> jump_table =
nullptr)
174 :
Resource(
"CODE",
id), jump_table(jump_table)
179 uint32_t a5_address = 0;
180 uint32_t base_address = 0;
183 std::set<uint16_t> near_entries;
184 uint16_t near_entry_count = 0;
186 std::set<uint32_t> far_entries;
187 uint16_t far_entry_count = 0;
188 std::set<uint32_t> a5_relocations;
189 std::set<uint32_t> segment_relocations;
192 uint32_t first_near_entry_offset;
193 uint32_t first_far_entry_offset;
194 uint32_t a5_relocation_offset;
195 uint32_t segment_relocation_offset;
196 uint32_t resource_size;
198 void CalculateValues()
override;
205 uint32_t MeasureRelocations(std::set<uint32_t>& relocations)
const;
207 void ReadRelocations(
Linker::Reader& rd, std::set<uint32_t>& relocations)
const;
208 void WriteRelocations(
Linker::Writer& wr,
const std::set<uint32_t>& relocations)
const;
213 int GetDisplayOptions()
const override;
214 void Dump(
Dumper::Dumper& dump, offset_t file_offset)
const override;
216 std::unique_ptr<Dumper::Region> CreateRegion(std::string name, offset_t offset, offset_t length,
unsigned display_width)
const override;
227 uint16_t name_offset = 0xFFFF;
228 std::optional<std::string> name;
229 uint8_t attributes = 0;
230 uint32_t data_offset = 0;
231 std::shared_ptr<Resource> data;
236 OSType type { 0, 0, 0, 0 };
239 std::vector<ResourceReference> references;
244 uint16_t attributes = 0;
251 std::map<uint32_t, std::map<uint16_t, std::shared_ptr<Resource>>>
resources;
254 uint32_t data_offset = 0, data_length = 0, map_offset = 0, map_length = 0;
255 uint16_t resource_type_list_offset = 28;
256 uint16_t name_list_offset = 0;
259 std::shared_ptr<JumpTableCodeResource> jump_table;
260 std::vector<std::shared_ptr<CodeResource>> codes;
261 std::map<std::shared_ptr<Linker::Segment>, std::shared_ptr<CodeResource>> segments;
262 std::shared_ptr<Linker::Segment> a5world;
264 void AddResource(std::shared_ptr<Resource> resource);
266 void OnNewSegment(std::shared_ptr<Linker::Segment> segment)
override;
297 std::shared_ptr<AppleSingleDouble> apple_single;
307 version_t version, minimum_version;
309 uint16_t secondary_header_size = 0;
310 mutable uint16_t crc = 0;
312 uint8_t attributes = 0;
313 uint32_t creation = 0;
314 uint32_t modification = 0;
317 uint32_t data_fork_length = 0;
318 uint32_t resource_fork_length = 0;
319 uint16_t comment_length = 0;
321 std::string generated_file_name;
324 : apple_single(std::make_shared<AppleSingleDouble>(AppleSingleDouble::DOUBLE)), version(version), minimum_version(version <= MACBIN2 ? version : MACBIN2)
328 MacBinary(version_t version, version_t minimum_version)
329 : apple_single(std::make_shared<AppleSingleDouble>(AppleSingleDouble::DOUBLE)), version(version), minimum_version(version < minimum_version ? version : minimum_version)
333 explicit MacBinary(std::shared_ptr<AppleSingleDouble> apple, version_t version, version_t minimum_version)
334 : apple_single(apple), version(version), minimum_version(version < minimum_version ? version : minimum_version)
339 static uint16_t crc_step[256];
341 void CRC_Initialize()
const;
343 void CRC_Step(uint8_t
byte = 0)
const;
347 void WriteData(
Linker::Writer& wr,
size_t count,
const void * data)
const;
349 void WriteData(
Linker::Writer& wr,
size_t count, std::string text)
const;
351 void WriteWord(
Linker::Writer& wr,
size_t bytes, uint64_t value)
const;
356 void CalculateValues();
417 unsigned apple_single_double_version = 2;
419 AppleSingleDouble::hfs_type home_file_system = AppleSingleDouble::HFS_UNDEFINED;
421 MacBinary::version_t macbinary_version = MacBinary::MACBIN3, macbinary_minimum_version = MacBinary::MACBIN2;
470 void GenerateFiles(std::string filename, std::shared_ptr<Contents> data_fork, std::shared_ptr<Contents> resource_fork);
500 std::shared_ptr<MacintoshResourceFileFormat> resource_fork;
501 std::shared_ptr<FinderInfo> finder_info;
503 std::map<std::string, std::string> options;
505 std::string script_file;
506 std::map<std::string, std::string> script_options;
509 void SetOptions(std::map<std::string, std::string>& options)
override;
512 void SetModel(std::string model)
override;
514 void SetLinkScript(std::string script_file, std::map<std::string, std::string>& options)
override;
Interface to generate files required for the Classic 68K Mac OS runtime.
Definition macos.h:484
void GenerateFile(std::string filename, Linker::Module &module) override
The main function that handles processing, calculating and generating the final image.
Definition macos.cc:1639
void OnCalculateValues() override
Called if there is no container allocated.
Definition macos.cc:1668
void OnContainerCreated() override
Called after the container is created.
Definition macos.cc:1656
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:1633
void OnReadFile(Linker::Reader &rd) override
Called if there is no container allocated.
Definition macos.cc:1673
void SetModel(std::string model) override
Sets the way memory is organized, typically modifying a built-in script.
Definition macos.cc:1628
bool FormatSupportsResources() const override
Whether the format supports resources.
Definition macos.cc:1612
void OnDump(Dumper::Dumper &dump) const override
Called if there is no container allocated.
Definition macos.cc:1692
offset_t OnWriteFile(Linker::Writer &wr) const override
Called if there is no container allocated.
Definition macos.cc:1687
std::string GetDefaultExtension(Linker::Module &module) const override
Provides a default filename for the output file.
Definition macos.cc:1707
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition macos.cc:1697
void SetOptions(std::map< std::string, std::string > &options) override
Passes command line parameters as settings over to format object.
Definition macos.cc:1617
std::vector< Linker::OptionDescription< void > > GetMemoryModelNames() override
Returns a list of the supported memory models, used for documentation.
Definition macos.cc:1622
MacBinary is an alternative format to AppleSingle for representing a Macintosh file on a non-Macintos...
Definition macos.h:295
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition macos.cc:1232
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition macos.cc:1259
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition macos.cc:1203
This is not actually a file format, but an interface to permit generating multiple binary outputs for...
Definition macos.h:374
void GenerateFiles(std::string filename, std::shared_ptr< Contents > data_fork, std::shared_ptr< Contents > resource_fork)
Tasked to create all the requested files.
Definition macos.cc:1366
produce_format_t produce
Bitset of other files to produce.
Definition macos.h:408
container_format_t container
The container type used to store metainformation while processing.
Definition macos.h:450
produce_format_t
Represents what additional files should be generated.
Definition macos.h:397
@ PRODUCE_MAC_BINARY
Creates a MacBinary with the .mbin extension.
Definition macos.h:405
@ PRODUCE_FINDER_INFO
Places a Finder Information file under the directory .finf.
Definition macos.h:401
@ PRODUCE_APPLE_DOUBLE
Creates an AppleDouble binary with the '' prefix.
Definition macos.h:403
@ PRODUCE_RESOURCE_FORK
Places a Macintosh format resource file under the directory .rsrc.
Definition macos.h:399
virtual void OnContainerCreated()
Called after the container is created.
Definition macos.cc:1343
virtual void OnDump(Dumper::Dumper &dump) const
Called if there is no container allocated.
Definition macos.cc:1361
target_format_t target
Format of "filename".
Definition macos.h:393
virtual void OnCalculateValues()
Called if there is no container allocated.
Definition macos.cc:1345
std::shared_ptr< AppleSingleDouble > apple_single
Container for all the necessary additional information.
Definition macos.h:453
std::shared_ptr< MacBinary > mac_binary
Container for MacBinary.
Definition macos.h:455
container_format_t
Format of container stored in memory.
Definition macos.h:441
@ CONTAINER_APPLE_SINGLE
Use an AppleSingle container.
Definition macos.h:445
@ CONTAINER_MAC_BINARY
Use a MacBinary container as well as an AppleSingle container.
Definition macos.h:447
@ CONTAINER_NONE
No container is stored.
Definition macos.h:443
virtual void OnReadFile(Linker::Reader &rd)
Called if there is no container allocated.
Definition macos.cc:1350
target_format_t
Represents the file type of the main file.
Definition macos.h:378
@ TARGET_NONE
Do not generate main file.
Definition macos.h:380
@ TARGET_RESOURCE_FORK
Main file is a resource fork.
Definition macos.h:384
@ TARGET_APPLE_SINGLE
Main file is an AppleSingle.
Definition macos.h:386
@ TARGET_APPLE_DOUBLE
Main file is an AppleDouble.
Definition macos.h:388
@ TARGET_MAC_BINARY
Main file as a MacBinary.
Definition macos.h:390
@ TARGET_DATA_FORK
Main file is a data fork, typically empty.
Definition macos.h:382
bool AddSupplementaryOutputFormat(std::string subformat) override
If the output format actually drives multiple output formats (resource file, apple double,...
Definition macos.cc:1312
virtual offset_t OnWriteFile(Linker::Writer &wr) const
Called if there is no container allocated.
Definition macos.cc:1355
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition macos.cc:1528
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition macos.cc:1577
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition macos.cc:1592
An abstract interface that separates structure and presentation of the data inside a file.
Definition dumper.h:828
A record that represents a region within the file.
Definition dumper.h:721
Encodes an object module file as a collection of sections, symbols and relocations.
Definition module.h:24
A helper class, encapsulating functionality needed to import binary data.
Definition reader.h:20
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