RetroLinker
Linker for several 8-bit, 16-bit and 32-bit formats
Loading...
Searching...
No Matches
peexe.h
1#ifndef PEEXE_H
2#define PEEXE_H
3
4#include "../common.h"
5#include "../dumper/dumper.h"
6#include "../linker/options.h"
7#include "../linker/segment_manager.h"
8#include "coff.h"
9#include "mzexe.h"
10
11namespace Microsoft
12{
17 {
18 public:
19 char pe_signature[4];
20
23 {
24 uint16_t major, minor;
25 };
26
29 {
30 public:
32 static constexpr uint16_t ROM32 = 0x0107;
34 static constexpr uint16_t EXE32 = 0x010B;
36 static constexpr uint16_t EXE64 = 0x020B;
37
39 static constexpr offset_t Win32Base = 0x00400000;
41 static constexpr offset_t Dll32Base = 0x10000000;
43 static constexpr offset_t WinCEBase = 0x00010000;
45 static constexpr offset_t Win64Base = 0x140000000;
47 static constexpr offset_t Dll64Base = 0x180000000;
48
52 offset_t image_base = 0;
56 uint32_t section_align = 0;
60 uint32_t file_align = 0;
76 uint32_t win32_version = 0;
80 uint32_t total_image_size = 0;
84 uint32_t total_headers_size = 0;
88 uint32_t checksum = 0;
90 enum SubsystemType : uint16_t
91 {
92 Unknown = 0,
93 Native = 1,
94 WindowsGUI = 2,
95 WindowsCUI = 3,
96 OS2CUI = 5,
97 POSIXCUI = 7,
98 NativeWin95 = 8,
99 WinCEGUI = 9,
100 EFIApplication = 10,
101 EFIBootServiceDriver = 11,
102 EFIRuntimeDriver = 12,
103 EFIROM = 13,
104 Xbox = 14,
105 WindowsBootApplication = 16,
106 };
114 uint16_t flags = 0;
126 offset_t reserved_heap_size = 0;
134 uint32_t loader_flags = 0;
135
138 {
139 uint32_t address = 0, size = 0;
140 };
141
142 enum
143 {
144 DirExportTable,
145 DirImportTable,
146 DirResourceTable,
147 DirExceptionTable,
148 DirCertificateTable,
149 DirBaseRelocationTable,
150 DirDebug,
151 DirArchitecture, // reserved
152 DirGlobalPointer,
153 DirTLSTable,
154 DirLoadConfigTable,
155 DirBoundImport,
156 DirIAT,
157 DirDelayImportDescriptor,
158 DirCLRRuntimeHeader,
159 DirReserved,
160 DirTotalCount,
161 };
162
166 std::vector<DataDirectory> data_directories;
167
169 bool Is64Bit() const;
170
172 : AOutHeader()
173 {
174 }
175
176 uint32_t GetSize() const override;
177
178 void ReadFile(Linker::Reader& rd) override;
179
180 void WriteFile(Linker::Writer& wr) const override;
181
182 offset_t CalculateValues(COFFFormat& coff) override;
183
185 uint32_t AddressToRVA(offset_t address) const;
187 offset_t RVAToAddress(uint32_t rva, bool suppress_on_zero = false) const;
188
189 protected:
190 void DumpFields(const COFFFormat& coff, Dumper::Dumper& dump, Dumper::Region& header_region) const override;
191 };
192
194 PEOptionalHeader& GetOptionalHeader();
196 const PEOptionalHeader& GetOptionalHeader() const;
197
199 bool Is64Bit() const;
200
202 uint32_t AddressToRVA(offset_t address) const;
204 offset_t RVAToAddress(uint32_t rva, bool suppress_on_zero = false) const;
205
208 {
209 public:
210 // TODO: other flags
212 static constexpr uint32_t DISCARDABLE = 0x02000000;
214 static constexpr uint32_t EXECUTE = 0x20000000;
216 static constexpr uint32_t READ = 0x40000000;
218 static constexpr uint32_t WRITE = 0x80000000;
219
220 Section(uint32_t flags = 0, std::shared_ptr<Linker::Image> image = nullptr)
221 : COFFFormat::Section(flags, image)
222 {
223 }
224
226 constexpr offset_t& virtual_size() { return physical_address; }
228 constexpr const offset_t& virtual_size() const { return physical_address; }
229
230 void ReadSectionData(Linker::Reader& rd, const COFFFormat& coff_format) override;
231 void WriteSectionData(Linker::Writer& wr, const COFFFormat& coff_format) const override;
232 uint32_t ImageSize(const COFFFormat& coff_format) const override;
233
235 virtual void ReadSectionData(Linker::Reader& rd, const PEFormat& fmt);
237 virtual void WriteSectionData(Linker::Writer& wr, const PEFormat& fmt) const;
239 virtual uint32_t ImageSize(const PEFormat& fmt) const;
241 virtual uint32_t MemorySize(const PEFormat& fmt) const;
242 };
243
246 {
247 public:
248 // TODO: this is just a sketch
249
251 typedef std::variant<std::string, uint32_t> Reference;
252
254 static constexpr size_t Level_Type = 0;
256 static constexpr size_t Level_Name = 1;
258 static constexpr size_t Level_Language = 2;
260 static constexpr size_t Level_Count = 3;
261
263 std::vector<Reference> full_identifier;
265 uint32_t data_rva = 0;
267 uint32_t codepage = 0;
269 uint32_t reserved = 0;
270 };
271
273 {
274 public:
275 // TODO: this is just a sketch
276
278 template <typename Key>
279 class Entry
280 {
281 public:
285 std::variant<std::shared_ptr<Resource>, std::shared_ptr<ResourceDirectory>> content;
287 uint32_t content_rva = 0;
288 };
289
291 uint32_t flags = 0;
292 uint32_t timestamp = 0;
293 version_type version = { };
295 std::vector<Entry<std::string>> name_entries;
297 std::vector<Entry<uint32_t>> id_entries;
298
304 void AddResource(std::shared_ptr<Resource>& resource, size_t level = 0);
305 };
306
309 {
310 public:
312 : Section(DATA | READ)
313 {
314 name = ".rsrc";
315 }
316
317 using Section::ReadSectionData;
318 using Section::WriteSectionData;
319 using Section::ImageSize;
320
321 bool IsPresent() const;
322 void Generate(PEFormat& fmt);
323 void ReadSectionData(Linker::Reader& rd, const PEFormat& fmt) override;
324 void WriteSectionData(Linker::Writer& wr, const PEFormat& fmt) const override;
325 uint32_t ImageSize(const PEFormat& fmt) const override;
326 uint32_t MemorySize(const PEFormat& fmt) const override;
327 };
328
330 std::shared_ptr<ResourcesSection> resources = std::make_shared<ResourcesSection>();
331
334 {
335 public:
336 typedef uint16_t Ordinal;
337
339 class Name
340 {
341 public:
343 uint16_t hint;
345 std::string name = "";
347 uint32_t rva = 0;
348
349 Name(std::string name, uint16_t hint = 0)
350 : hint(hint), name(name)
351 {
352 }
353 };
354
356 typedef std::variant<Ordinal, Name> ImportTableEntry;
357
359 uint32_t lookup_table_rva = 0;
361 uint32_t address_table_rva = 0;
362 uint32_t timestamp = 0;
363 uint32_t forwarder_chain = 0;
365 std::string name;
367 uint32_t name_rva = 0;
368
370 std::vector<ImportTableEntry> import_table;
372 std::map<std::string, size_t> imports_by_name;
374 std::map<Ordinal, size_t> imports_by_ordinal;
375
377 void AddImportByName(std::string entry_name, uint16_t hint);
379 void AddImportByOrdinal(uint16_t ordinal);
380
382 offset_t GetImportByNameAddress(const PEFormat& fmt, std::string name);
384 offset_t GetImportByOrdinalAddress(const PEFormat& fmt, uint16_t ordinal);
385
386 ImportDirectory(std::string name)
387 : name(name)
388 {
389 }
390 };
391
393 class ImportsSection : public Section
394 {
395 public:
397 std::vector<ImportDirectory> directories;
399 std::map<std::string, size_t> library_indexes;
400
405
407 : Section(DATA | READ | WRITE)
408 {
409 name = ".idata";
410 }
411
412 using Section::ReadSectionData;
413 using Section::WriteSectionData;
414 using Section::ImageSize;
415
416 bool IsPresent() const;
417 void Generate(PEFormat& fmt);
418 void ReadSectionData(Linker::Reader& rd, const PEFormat& fmt) override;
419 void WriteSectionData(Linker::Writer& wr, const PEFormat& fmt) const override;
420 uint32_t ImageSize(const PEFormat& fmt) const override;
421 uint32_t MemorySize(const PEFormat& fmt) const override;
422 };
423
425 std::shared_ptr<ImportsSection> imports = std::make_shared<ImportsSection>();
426
429 {
430 public:
437 {
438 public:
440 std::string dll_name;
442 std::variant<std::string, uint32_t> reference;
444 std::string reference_name;
446 uint32_t rva;
447 };
448
450 class Name
451 {
452 public:
454 std::string name;
456 uint32_t rva = 0;
457
458 Name(std::string name)
459 : name(name)
460 {
461 }
462 };
463
465 std::variant<uint32_t, Forwarder> value;
467 std::optional<Name> name;
468
469 ExportedEntry(uint32_t rva)
470 : value(rva), name()
471 {
472 }
473
474 ExportedEntry(uint32_t rva, std::string name)
475 : value(rva), name(name)
476 {
477 }
478 };
479
481 class ExportsSection : public Section
482 {
483 public:
484 uint32_t flags = 0;
485 uint32_t timestamp = 0;
486 version_type version = { };
488 std::string dll_name;
490 uint32_t dll_name_rva = 0;
492 uint32_t ordinal_base = 1;
494 uint32_t address_table_rva = 0;
496 uint32_t name_table_rva = 0;
498 uint32_t ordinal_table_rva = 0;
500 std::vector<std::optional<std::shared_ptr<ExportedEntry>>> entries;
502 std::map<std::string, uint32_t> named_exports;
503
505 void SetEntry(uint32_t ordinal, std::shared_ptr<ExportedEntry> entry);
507 void AddEntry(std::shared_ptr<ExportedEntry> entry);
508
510 : Section(DATA | READ)
511 {
512 name = ".edata";
513 // TODO: flags and other fields
514 }
515
516 using Section::ReadSectionData;
517 using Section::WriteSectionData;
518 using Section::ImageSize;
519
520 bool IsPresent() const;
521 void Generate(PEFormat& fmt);
522 void ReadSectionData(Linker::Reader& rd, const PEFormat& fmt) override;
523 void WriteSectionData(Linker::Writer& wr, const PEFormat& fmt) const override;
524 uint32_t ImageSize(const PEFormat& fmt) const override;
525 uint32_t MemorySize(const PEFormat& fmt) const override;
526 };
527
529 std::shared_ptr<ExportsSection> exports = std::make_shared<ExportsSection>();
530
581
584 {
585 public:
587 static constexpr uint32_t PAGE_SIZE = 0x1000;
589 uint32_t page_rva;
591 uint32_t block_size;
593 std::vector<BaseRelocation> relocations_list;
595 std::map<uint16_t, BaseRelocation> relocations_map;
596
597 BaseRelocationBlock(uint32_t page_rva = 0)
598 : page_rva(page_rva & ~(PAGE_SIZE - 1))
599 {
600 }
601 };
602
605 {
606 public:
608 std::vector<std::shared_ptr<BaseRelocationBlock>> blocks_list;
610 std::map<uint32_t, std::shared_ptr<BaseRelocationBlock>> blocks_map;
611
614 {
615 name = ".reloc";
616 }
617
618 using Section::ReadSectionData;
619 using Section::WriteSectionData;
620 using Section::ImageSize;
621
622 bool IsPresent() const;
623 void Generate(PEFormat& fmt);
624 void ReadSectionData(Linker::Reader& rd, const PEFormat& fmt) override;
625 void WriteSectionData(Linker::Writer& wr, const PEFormat& fmt) const override;
626 uint32_t ImageSize(const PEFormat& fmt) const override;
627 uint32_t MemorySize(const PEFormat& fmt) const override;
628 };
629
631 std::shared_ptr<BaseRelocationsSection> base_relocations = std::make_shared<BaseRelocationsSection>();
632
639 void AddBaseRelocation(uint32_t rva, BaseRelocation::relocation_type type, uint16_t low_ref = 0);
640
641 mutable MZStubWriter stub;
642
643 void ReadFile(Linker::Reader& rd) override;
644
645 ::EndianType GetMachineEndianType() const;
646 void CalculateValues() override;
647
649 offset_t WriteFile(Linker::Writer& wr) const override;
650 void Dump(Dumper::Dumper& dump) const override;
651
652 PEFormat()
653 : COFFFormat(WINDOWS, PECOFF, ::LittleEndian)
654 {
655 optional_header = std::make_unique<PEOptionalHeader>();
656 }
657
658 public:
659 /* * * Writer members * * */
660
663 {
671 TargetMacintosh, // TODO: not supported
675 TargetTNT, // TODO: not supported
677 TargetEFI, // TODO: not supported
679 TargetDotNET, // TODO: not supported
681 TargetXbox, // TODO: not supported
682 //TargetBeOS, // TODO: possible target
683 //TargetSkyOS, // TODO: possible target
684 //TargetHXDOS, // TODO: possible target
685 };
688
699 /*** @brief Whether to generate an executable, library or system file */
700 output_type output = OUTPUT_EXE;
701
704
707
710
713
715 bool option_debug_info = false;
716
718 {
719 public:
720 class TargetEnumeration : public Linker::Enumeration<target_type>
721 {
722 public:
724 : Enumeration(
725 "WIN9X", TargetWin9x,
726 "WINNT", TargetWinNT,
727 "WINCE", TargetWinCE,
728 "MACINTOSH", TargetMacintosh,
729 "WIN32S", TargetWin32s,
730 "TNT", TargetTNT,
731 "EFI", TargetEFI,
732 "DOTNET", TargetDotNET,
733 "XBOX", TargetXbox)
734 {
735 }
736 };
737
738 enum subsystem_determination
739 {
740 Subsystem_Windows = 1,
741 Subsystem_Console,
742 Subsystem_Native,
743 Subsystem_OS2,
744 Subsystem_POSIX,
745 Subsystem_EFIApplication,
746 Subsystem_EFIBootServiceDriver,
747 Subsystem_EFIRuntimeDriver,
748 Subsystem_EFI_ROM,
749 Subsystem_BootApplication,
750 };
751
752 class SubsystemEnumeration : public Linker::Enumeration<subsystem_determination>
753 {
754 public:
756 : Enumeration(
757 "WINDOWS", Subsystem_Windows,
758 "CONSOLE", Subsystem_Console,
759 "NATIVE", Subsystem_Native,
760 "OS2", Subsystem_OS2,
761 "POSIX", Subsystem_POSIX,
762 "EFI_APPLICATION", Subsystem_EFIApplication,
763 "EFI_BOOT_SERVICE_DRIVER", Subsystem_EFIBootServiceDriver,
764 "EFI_RUNTIME_DRIVER", Subsystem_EFIRuntimeDriver,
765 "EFI_ROM", Subsystem_EFI_ROM,
766 "BOOT_APPLICATION", Subsystem_BootApplication)
767 {
768 }
769 };
770
771 class OutputTypeEnumeration : public Linker::Enumeration<output_type>
772 {
773 public:
775 : Enumeration(
776 "EXE", OUTPUT_EXE,
777 "DLL", OUTPUT_DLL,
778 "SYS", OUTPUT_SYS)
779 {
780 }
781 };
782
783 Linker::Option<std::string> stub{"stub", "Filename for stub that gets prepended to executable"};
784 Linker::Option<Linker::ItemOf<TargetEnumeration>> target{"target", "Windows target type"};
785 Linker::Option<Linker::ItemOf<SubsystemEnumeration>> subsystem{"subsystem", "Windows subsystem to target"};
786 Linker::Option<Linker::ItemOf<OutputTypeEnumeration>> output{"output", "Output types"};
787 Linker::Option<offset_t> image_base{"base", "Base address of image, used for calculating relative virtual addresses"};
788 Linker::Option<offset_t> section_align{"section_align", "Section alignment"};
789
790 PEOptionCollector()
791 {
792 InitializeFields(stub, target, subsystem, output, image_base, section_align);
793 }
794 };
795
796 bool FormatSupportsLibraries() const override;
797
798 unsigned FormatAdditionalSectionFlags(std::string section_name) const override;
799
800 std::shared_ptr<Resource>& AddResource(std::shared_ptr<Resource>& resource);
801
802 ImportDirectory& FetchImportLibrary(std::string library_name, bool create_if_not_present = false);
803 void AddImportByName(std::string library_name, std::string entry_name, uint16_t hint);
804 void AddImportByOrdinal(std::string library_name, uint16_t ordinal);
805
806 static std::vector<Linker::OptionDescription<void> *> ParameterNames;
807 std::vector<Linker::OptionDescription<void> *> GetLinkerScriptParameterNames() override;
808 std::shared_ptr<Linker::OptionCollector> GetOptions() override;
809
811 enum
812 {
814 FLAG_NO_RELOCATIONS = 0x0001, // already defined in COFF
816 FLAG_EXECUTABLE = 0x0002, // already defined in COFF
818 FLAG_NO_LINE_NUMBERS = 0x0004, // already defined in COFF
820 FLAG_NO_SYMBOLS = 0x0008, // already defined in COFF
828 FLAG_32BIT = 0x0100,
836 FLAG_SYSTEM = 0x1000,
838 FLAG_LIBRARY = 0x2000,
843 };
844
845 void SetOptions(std::map<std::string, std::string>& options) override;
846 void OnNewSegment(std::shared_ptr<Linker::Segment> segment) override;
847 std::unique_ptr<Script::List> GetScript(Linker::Module& module);
848 void Link(Linker::Module& module);
849 void ProcessModule(Linker::Module& module) override;
850 void GenerateFile(std::string filename, Linker::Module& module) override;
852 std::string GetDefaultExtension(Linker::Module& module, std::string filename) const override;
853 };
854}
855
856#endif /* PEEXE_H */
A standard 28 byte a.out optional header, used by DJGPP.
Definition coff.h:677
A COFF section.
Definition coff.h:468
std::string name
The name of the section (COFF name: s_name)
Definition coff.h:473
static constexpr uint32_t DATA
COFF section flag: Section contains initialized data (COFF name: STYP_DATA)
Definition coff.h:531
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 flags
COFF section flags, determines the type of the section (text, data, bss, etc.) (COFF name: s_flags)
Definition coff.h:511
The UNIX COFF file format.
Definition coff.h:27
@ PECOFF
Microsoft PE/COFF variant.
Definition coff.h:246
@ WINDOWS
Windows Portable Executable (used only by PE)
Definition coff.h:1191
format_type type
A representation of the format to generate.
Definition coff.h:1196
offset_t ImageSize() const override
Retrieves size of stored data.
Definition coff.cc:1498
std::unique_ptr< OptionalHeader > optional_header
The optional header instance used for reading/writing the COFF file.
Definition coff.h:631
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 representation of an enumeration with associated string representations for each value.
Definition options.h:15
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
Helper class that contains the options interpreted by the format.
Definition options.h:467
Documents and handles command line options.
Definition options.h:303
virtual std::string GetDefaultExtension(Module &module, std::string filename) const
Appends a default extension to the filename.
A helper class, encapsulating functionality needed to import binary data.
Definition reader.h:16
A helper class, encapsulating functionality needed to export binary data.
Definition writer.h:15
Definition mzexe.h:300
A block of base relocations, corresponding to a single page in the image.
Definition peexe.h:584
std::vector< BaseRelocation > relocations_list
Sequence of relocations in this block, filled in by the linker once all the relocations have been col...
Definition peexe.h:593
static constexpr uint32_t PAGE_SIZE
Pages are 4 KiB long.
Definition peexe.h:587
std::map< uint16_t, BaseRelocation > relocations_map
Collection of relocations, accessed via their page offset.
Definition peexe.h:595
uint32_t page_rva
The relative virtual address of the page this block corresponds to, with the least significant 12 bit...
Definition peexe.h:589
uint32_t block_size
Number of bytes in this block, including the initial 8 bytes for the relative virtual address of the ...
Definition peexe.h:591
Represents a base relocation, as stored in the image.
Definition peexe.h:533
size_t GetRelocationSize(const PEFormat *format) const
The number of bytes impacted by this relocation (typically 2 or 4 or 8)
Definition peexe.cc:704
relocation_type type
The type of the relocation, stored as the 4 most significant bits of a 16-bit base relocation entry.
Definition peexe.h:570
uint16_t parameter
For relocation type HighAdj, the 16-bit value stored in the following base relocation entry.
Definition peexe.h:574
size_t GetEntryCount(const PEFormat *format) const
The number of base relocation entries required to store this base relocation, typically 1.
Definition peexe.cc:693
relocation_type
The type of the relocation, partly dependent on the current CPU.
Definition peexe.h:537
@ RelRISCVHigh20
RISC-V specific.
Definition peexe.h:553
@ RelLow
The least significant 16 bits of the address.
Definition peexe.h:543
@ RelAbsolute
No relocation.
Definition peexe.h:539
@ RelLoongArch64MarkLA
LoongArch specific.
Definition peexe.h:563
@ RelThumbMov32
Thumb (ARM) specific.
Definition peexe.h:555
@ RelDir64
A full 64-bit address.
Definition peexe.h:567
@ RelLoongArch32MarkLA
LoongArch specific.
Definition peexe.h:561
@ RelRISCVLow12I
RISC-V specific.
Definition peexe.h:557
@ RelHigh
The most significant 16 bits of the address.
Definition peexe.h:541
@ RelMIPSJmpAddr16
MIPS specific.
Definition peexe.h:565
@ RelHighAdj
The most significant 16 bits of the address, with the currently assumed least significant 16 bits sto...
Definition peexe.h:547
@ RelMIPSJmpAddr
MIPS specific.
Definition peexe.h:549
@ RelRISCVLow12S
RISC-V specific.
Definition peexe.h:559
@ RelHighLow
A full 32-bit address.
Definition peexe.h:545
@ RelARMMov32
ARM specific.
Definition peexe.h:551
uint16_t offset
The offset of the relocation within the page corresponding to this base relocation block,...
Definition peexe.h:572
Represents a .reloc section for the base relocations for the binary.
Definition peexe.h:605
uint32_t MemorySize(const PEFormat &fmt) const override
Retrieves the size of the section, as loaded into memory.
Definition peexe.cc:842
void ReadSectionData(Linker::Reader &rd, const PEFormat &fmt) override
Reads the contents of the section in the file.
Definition peexe.cc:809
void WriteSectionData(Linker::Writer &wr, const PEFormat &fmt) const override
Writes the contents of the section to the file.
Definition peexe.cc:814
std::map< uint32_t, std::shared_ptr< BaseRelocationBlock > > blocks_map
Collection of relocation blocks, accessed via the relative virtual address of the page.
Definition peexe.h:610
std::vector< std::shared_ptr< BaseRelocationBlock > > blocks_list
Sequence of relocation blocks, filled in by the linker once all the relocations have been collected.
Definition peexe.h:608
Encompasses the information necessary to encode a forwarder exported reference.
Definition peexe.h:437
std::variant< std::string, uint32_t > reference
The name or ordinal to the entry that is being reexported, this field will not appear directly in the...
Definition peexe.h:442
std::string reference_name
A string representation of the forwarder, to appear in the image.
Definition peexe.h:444
uint32_t rva
The relative virtual address of the reference name.
Definition peexe.h:446
std::string dll_name
The name of the library whose entry is being reexported, this field will not appear directly in the i...
Definition peexe.h:440
Represents a name by which an exported entry is identified.
Definition peexe.h:451
uint32_t rva
The relative virtual address of the name string.
Definition peexe.h:456
std::string name
The name, as a string.
Definition peexe.h:454
Represents a single exported entry in the file.
Definition peexe.h:429
std::optional< Name > name
An optional name by which the exported entry is identified, otherwise its position in the export tabl...
Definition peexe.h:467
std::variant< uint32_t, Forwarder > value
Represents the associated value of the entry: either the relative virtual address of the object being...
Definition peexe.h:465
Represents an .edata section for the exported entries in the binary.
Definition peexe.h:482
void WriteSectionData(Linker::Writer &wr, const PEFormat &fmt) const override
Writes the contents of the section to the file.
Definition peexe.cc:600
uint32_t MemorySize(const PEFormat &fmt) const override
Retrieves the size of the section, as loaded into memory.
Definition peexe.cc:688
void AddEntry(std::shared_ptr< ExportedEntry > entry)
Adds an entry to an unused ordinal (for speed, we access the highest taken ordinal and assign this en...
Definition peexe.cc:519
std::string dll_name
The name of this library.
Definition peexe.h:488
uint32_t ordinal_base
The starting ordinal for the export table.
Definition peexe.h:492
uint32_t address_table_rva
Relative virtual address of the export address table, containing the exported entries for each succes...
Definition peexe.h:494
uint32_t name_table_rva
Relative virtual address of the name pointer table, containing the relative virtual addresses of the ...
Definition peexe.h:496
void SetEntry(uint32_t ordinal, std::shared_ptr< ExportedEntry > entry)
Sets a specific entry, corresponding to some ordinal.
Definition peexe.cc:496
std::map< std::string, uint32_t > named_exports
A collection of exports by name, with each string being associated with its ordinal.
Definition peexe.h:502
uint32_t dll_name_rva
The relative virtual address of the library name string.
Definition peexe.h:490
void ReadSectionData(Linker::Reader &rd, const PEFormat &fmt) override
Reads the contents of the section in the file.
Definition peexe.cc:595
std::vector< std::optional< std::shared_ptr< ExportedEntry > > > entries
Sequence of entries, the first entry corresponds to ordinal_base.
Definition peexe.h:500
uint32_t ordinal_table_rva
Relative virtual address of the ordinal table, containing the ordinals for each entry exported by nam...
Definition peexe.h:498
Represents an entry in the hint-name table, corresponding to imports by name.
Definition peexe.h:340
std::string name
The name by which the entry is identified.
Definition peexe.h:345
uint32_t rva
The relative virtual address of the name of the entry.
Definition peexe.h:347
uint16_t hint
The hint (ordinal) corresponding to this entry.
Definition peexe.h:343
A collection of the imported names for a specific dynamic linking library.
Definition peexe.h:334
std::variant< Ordinal, Name > ImportTableEntry
Represents a single entry in the import directory, either an ordinal or a name.
Definition peexe.h:356
offset_t GetImportByOrdinalAddress(const PEFormat &fmt, uint16_t ordinal)
Retrieves the virtual address of an already registered entry that is imported by ordinal.
Definition peexe.cc:1083
std::map< std::string, size_t > imports_by_name
A convenience field to quickly access the imported entry index via its name, must be kept synchronize...
Definition peexe.h:372
uint32_t address_table_rva
Relative virtual address for the address table, used to access the imported functions,...
Definition peexe.h:361
std::map< Ordinal, size_t > imports_by_ordinal
A convenience field to quickly access the imported entry index via its ordinal (only for import by or...
Definition peexe.h:374
offset_t GetImportByNameAddress(const PEFormat &fmt, std::string name)
Retrieves the virtual address of an already registered entry that is imported by name.
Definition peexe.cc:1072
uint32_t name_rva
Relative virtual address of the name.
Definition peexe.h:367
void AddImportByName(std::string entry_name, uint16_t hint)
Adds a new imported entry by name and hint, unless an entry by the same name already exists,...
Definition peexe.cc:1051
std::string name
Name of the dynamic linking library included.
Definition peexe.h:365
uint32_t lookup_table_rva
Relative virtual address for the lookup table, preserved during execution time.
Definition peexe.h:359
std::vector< ImportTableEntry > import_table
List of imported entries.
Definition peexe.h:370
void AddImportByOrdinal(uint16_t ordinal)
Adds a new imported entry by ordinal, unless an entry by the same ordinal already exists,...
Definition peexe.cc:1061
Represents an .idata section for imported DLLs in the binary.
Definition peexe.h:394
std::map< std::string, size_t > library_indexes
A convenience field to access the DLL index by its name.
Definition peexe.h:399
void ReadSectionData(Linker::Reader &rd, const PEFormat &fmt) override
Reads the contents of the section in the file.
Definition peexe.cc:392
uint32_t MemorySize(const PEFormat &fmt) const override
Retrieves the size of the section, as loaded into memory.
Definition peexe.cc:491
std::vector< ImportDirectory > directories
The sequence of all import directories, one for each DLL.
Definition peexe.h:397
void WriteSectionData(Linker::Writer &wr, const PEFormat &fmt) const override
Writes the contents of the section to the file.
Definition peexe.cc:397
uint32_t address_table_size
Size of the import address table, to be stored in the optional header.
Definition peexe.h:404
uint32_t address_table_rva
Relative virtual address of the import address table, to be stored in the optional header.
Definition peexe.h:402
The PE optional header as specified by Microsoft.
Definition peexe.h:29
version_type image_version
Binary image version.
Definition peexe.h:68
uint16_t flags
DLL flags (in PE terminology, characteristics)
Definition peexe.h:114
version_type os_version
Required operating system version.
Definition peexe.h:64
uint32_t win32_version
Reserved value.
Definition peexe.h:76
uint32_t loader_flags
Reserved.
Definition peexe.h:134
static constexpr offset_t Win32Base
Conventional image base for 32-bit Windows executables.
Definition peexe.h:39
uint32_t section_align
Section alignment.
Definition peexe.h:56
uint32_t file_align
File alignment.
Definition peexe.h:60
uint32_t checksum
Checksum.
Definition peexe.h:88
offset_t RVAToAddress(uint32_t rva, bool suppress_on_zero=false) const
Converts an image base relative virtual address into a virtual address.
Definition peexe.cc:32
static constexpr offset_t Dll32Base
Conventional image base for 32-bit Windows dynamic linking libraries.
Definition peexe.h:41
std::vector< DataDirectory > data_directories
PE specific areas in the file, each one has a specific purpose.
Definition peexe.h:166
static constexpr uint16_t EXE64
Magic number for 64-bit binaries, also signifies 8-byte entries in the file (PE32+)
Definition peexe.h:36
static constexpr offset_t WinCEBase
Conventional image base for Windows CE executables.
Definition peexe.h:43
static constexpr offset_t Win64Base
Conventional image base for 64-bit Windows executables.
Definition peexe.h:45
bool Is64Bit() const
Whether the file is in the PE32+ (64-bit) format.
Definition peexe.cc:8
offset_t reserved_stack_size
How much of stack should be reserved at launch.
Definition peexe.h:118
uint32_t total_headers_size
Cumulative size of all the headers, including the stub.
Definition peexe.h:84
offset_t committed_stack_size
How many pages of stack are actually available at launch.
Definition peexe.h:122
static constexpr uint16_t EXE32
Magic number for 32-bit binaries, also signifies 4-byte entries in the file (PE32)
Definition peexe.h:34
offset_t image_base
Preferred base address of image, all relative virtual addresses are calculate relative to this value.
Definition peexe.h:52
SubsystemType subsystem
The Windows subsystem this program runs on.
Definition peexe.h:110
offset_t reserved_heap_size
How much of heap should be reserved at launch.
Definition peexe.h:126
uint32_t total_image_size
Size of the entire image, including headers.
Definition peexe.h:80
static constexpr offset_t Dll64Base
Conventional image base for 64-bit Windows dynamic linking libraries.
Definition peexe.h:47
offset_t committed_heap_size
How many pages of heap are actually available at launch.
Definition peexe.h:130
uint32_t AddressToRVA(offset_t address) const
Converts a virtual address into an image base relative virtual address.
Definition peexe.cc:18
uint32_t GetSize() const override
Returns size of optional header.
Definition peexe.cc:13
static constexpr uint16_t ROM32
Magic number for ROM images, according to Microsoft.
Definition peexe.h:32
version_type subsystem_version
Version of the subsystem.
Definition peexe.h:72
Represents a single entry in the resource directory.
Definition peexe.h:280
Key identifier
The value that identifiers the resource at the current level.
Definition peexe.h:283
std::variant< std::shared_ptr< Resource >, std::shared_ptr< ResourceDirectory > > content
The contents at this level, either the resource itself, or another level of resource directory.
Definition peexe.h:285
uint32_t content_rva
The relative virtual address of the contents.
Definition peexe.h:287
void AddResource(std::shared_ptr< Resource > &resource, size_t level=0)
Inserts a resource into the directory.
Definition peexe.cc:235
std::vector< Entry< uint32_t > > id_entries
Entries that are identified via a number.
Definition peexe.h:297
uint32_t flags
Resource directory characteristics.
Definition peexe.h:291
std::vector< Entry< std::string > > name_entries
Entries that are identified via a string.
Definition peexe.h:295
Represents a resource inside the image.
Definition peexe.h:246
uint32_t codepage
Codepage of the resource.
Definition peexe.h:267
uint32_t reserved
Reserved entry in the resource table.
Definition peexe.h:269
uint32_t data_rva
The relative virtual address of the resource data.
Definition peexe.h:265
static constexpr size_t Level_Name
The second level of the resource tree corresponds to the resource name.
Definition peexe.h:256
static constexpr size_t Level_Type
The first level of the resource tree corresponds to the resource type.
Definition peexe.h:254
std::vector< Reference > full_identifier
The sequence of IDs that identifies this resource, conventionally corresponding to the resource type,...
Definition peexe.h:263
std::variant< std::string, uint32_t > Reference
Represents a resource identifier, either a string or a 32-bit value.
Definition peexe.h:251
static constexpr size_t Level_Count
Total number of levels used.
Definition peexe.h:260
static constexpr size_t Level_Language
The third level of the resource tree corresponds to the language (locale) for the resource.
Definition peexe.h:258
Represents an .rsrc resource section in the binary.
Definition peexe.h:309
void WriteSectionData(Linker::Writer &wr, const PEFormat &fmt) const override
Writes the contents of the section to the file.
Definition peexe.cc:326
void ReadSectionData(Linker::Reader &rd, const PEFormat &fmt) override
Reads the contents of the section in the file.
Definition peexe.cc:321
uint32_t MemorySize(const PEFormat &fmt) const override
Retrieves the size of the section, as loaded into memory.
Definition peexe.cc:336
A section, with the PE extensions.
Definition peexe.h:208
static constexpr uint32_t WRITE
The section contents are writable to the program.
Definition peexe.h:218
static constexpr uint32_t READ
The section contents are readable to the program.
Definition peexe.h:216
static constexpr uint32_t DISCARDABLE
The section is discardable.
Definition peexe.h:212
virtual uint32_t MemorySize(const PEFormat &fmt) const
Retrieves the size of the section, as loaded into memory.
Definition peexe.cc:230
constexpr const offset_t & virtual_size() const
The COFF s_paddr field is redefined to contain the size of the section as loaded into memory.
Definition peexe.h:228
static constexpr uint32_t EXECUTE
The section contains executable code.
Definition peexe.h:214
constexpr offset_t & virtual_size()
The COFF s_paddr field is redefined to contain the size of the section as loaded into memory.
Definition peexe.h:226
Microsoft PE .EXE portable executable file format.
Definition peexe.h:17
target_type target
The expected target system, only used for setting up default values.
Definition peexe.h:687
bool option_coff_local_symbols
Include COFF local symbols (TODO: not implemented)
Definition peexe.h:712
uint32_t AddressToRVA(offset_t address) const
Converts a virtual address into an image base relative virtual address.
Definition peexe.cc:862
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 peexe.cc:1376
output_type
Represents the target image type.
Definition peexe.h:691
@ OUTPUT_EXE
An executable image, conventionally taking the suffix .exe
Definition peexe.h:693
@ OUTPUT_SYS
A system file or driver, conventionally taking the suffix .sys
Definition peexe.h:697
@ OUTPUT_DLL
A dynamically linked library, conventionally taking the suffix .dll
Definition peexe.h:695
std::shared_ptr< BaseRelocationsSection > base_relocations
The collection of base relocations.
Definition peexe.h:631
bool option_debug_info
Include debug information (TODO: not implemented)
Definition peexe.h:715
std::shared_ptr< ImportsSection > imports
The collection of imports in the file.
Definition peexe.h:425
void GenerateFile(std::string filename, Linker::Module &module) override
The main function that handles processing, calculating and generating the final image.
Definition peexe.cc:1763
std::shared_ptr< ExportsSection > exports
The collection of exported symbols.
Definition peexe.h:529
void AddBaseRelocation(uint32_t rva, BaseRelocation::relocation_type type, uint16_t low_ref=0)
Adds a base relocation at the specified relative virtual address.
Definition peexe.cc:872
@ FLAG_ON_REMOVABLE
IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP.
Definition peexe.h:832
@ FLAG_NO_LINE_NUMBERS
(deprecated) IMAGE_FILE_LINE_NUMS_STRIPPED (F_LNNO)
Definition peexe.h:818
@ FLAG_32BIT
IMAGE_FILE_32BIT_MACHINE (redefined from COFF F_AR32WR)
Definition peexe.h:828
@ FLAG_LIBRARY
IMAGE_FILE_DLL.
Definition peexe.h:838
@ FLAG_LITTLE_ENDIAN
(deprecated) IMAGE_FILE_BYTES_REVERSED_LO (redefined from COFF F_AR16WR)
Definition peexe.h:826
@ FLAG_NO_DEBUG
IMAGE_FILE_DEBUG_STRIPPED (redefined from COFF F_AR32W)
Definition peexe.h:830
@ FLAG_SYSTEM
IMAGE_FILE_SYSTEM.
Definition peexe.h:836
@ FLAG_BIG_ENDIAN
(deprecated) IMAGE_FILE_BYTES_REVERSED_HI
Definition peexe.h:842
@ FLAG_ON_NETWORK
IMAGE_FILE_NET_RUN_FROM_SWAP.
Definition peexe.h:834
@ FLAG_EXECUTABLE
IMAGE_FILE_EXECUTABLE_IMAGE (F_EXEC)
Definition peexe.h:816
@ FLAG_NO_SYMBOLS
(deprecated) IMAGE_FILE_LOCAL_SYMS_STRIPPED (F_LSYMS)
Definition peexe.h:820
@ FLAG_TRIM_WORKING_SET
(deprecated) IMAGE_FILE_AGGRESSIVE_WS_TRIM (no COFF meaning)
Definition peexe.h:822
@ FLAG_NO_RELOCATIONS
IMAGE_FILE_RELOCS_STRIPPED (F_RELFLG)
Definition peexe.h:814
@ FLAG_UNIPROCESSOR_ONLY
IMAGE_FILE_UP_SYSTEM_ONLY.
Definition peexe.h:840
@ FLAG_LARGE_ADDRESS
IMAGE_FILE_LARGE_ADDRESS_AWARE (no COFF meaning)
Definition peexe.h:824
bool option_include_deprecated_flags
Certain header flags are deprecated and should be zero, this option sets them if applicable (TODO: ex...
Definition peexe.h:706
std::string GetDefaultExtension(Linker::Module &module, std::string filename) const override
Appends a default extension to the filename.
Definition peexe.cc:1887
PEOptionalHeader & GetOptionalHeader()
Retrieves the optional header.
Definition peexe.cc:847
std::shared_ptr< ResourcesSection > resources
The resources in this file.
Definition peexe.h:330
bool Is64Bit() const
Whether the file is in the PE32+ (64-bit) format.
Definition peexe.cc:857
bool option_coff_line_numbers
Include COFF line numbers (TODO: not implemented)
Definition peexe.h:709
bool option_relocatable
Make generated image relocatable (TODO: expose as command line option)
Definition peexe.h:703
void SetOptions(std::map< std::string, std::string > &options) override
Passes command line parameters as settings over to format object.
Definition peexe.cc:1119
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition peexe.cc:893
std::vector< Linker::OptionDescription< void > * > GetLinkerScriptParameterNames() override
Returns a list of the parameters used in the linker scripts, used for documentation.
Definition peexe.cc:1109
target_type
Represents settings and assumptions about the target.
Definition peexe.h:663
@ TargetWin32s
Win32s compatibility library for Windows 3.1.
Definition peexe.h:673
@ TargetEFI
EFI and UEFI firmware.
Definition peexe.h:677
@ TargetWin9x
Windows 95/98/ME.
Definition peexe.h:665
@ TargetTNT
Phar Lap TNT DOS Extender.
Definition peexe.h:675
@ TargetXbox
Xbox.
Definition peexe.h:681
@ TargetWinNT
Windows NT kernel.
Definition peexe.h:667
@ TargetMacintosh
Windows Portability Library.
Definition peexe.h:671
@ TargetDotNET
.NET environment
Definition peexe.h:679
@ TargetWinCE
Windows CE kernel.
Definition peexe.h:669
std::shared_ptr< Linker::OptionCollector > GetOptions() override
Returns object containing a sequence of option fields provided with the -S command line flag.
Definition peexe.cc:1114
void ProcessModule(Linker::Module &module) override
Processes the module object and initializes format fields.
Definition peexe.cc:1467
void CalculateValues() override
Intermediate step between processing module and generating output file to set up headers and manageme...
Definition peexe.cc:921
bool FormatSupportsLibraries() const override
Whether the format supports libraries.
Definition peexe.cc:1015
offset_t RVAToAddress(uint32_t rva, bool suppress_on_zero=false) const
Converts an image base relative virtual address into a virtual address.
Definition peexe.cc:867
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition peexe.cc:1001
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition peexe.cc:991
A data directory entry.
Definition peexe.h:138
Represents a version entry with major and minor versions.
Definition peexe.h:23