6#include "../dumper/dumper.h"
7#include "../linker/options.h"
8#include "../linker/segment_manager.h"
21 std::array<char, 4> pe_signature;
26 uint16_t major, minor;
34 static constexpr uint16_t
ROM32 = 0x0107;
36 static constexpr uint16_t
EXE32 = 0x010B;
38 static constexpr uint16_t
EXE64 = 0x020B;
103 EFIBootServiceDriver = 11,
104 EFIRuntimeDriver = 12,
107 WindowsBootApplication = 16,
141 uint32_t address = 0, size = 0;
151 DirBaseRelocationTable,
159 DirDelayImportDescriptor,
178 uint32_t
GetSize()
const override;
180 void ReadFile(
const std::shared_ptr<Linker::Reader>& rd)
override;
182 void WriteFile(
const std::shared_ptr<Linker::Writer>& wr)
const override;
189 offset_t
RVAToAddress(uint32_t rva,
bool suppress_on_zero =
false)
const;
191 std::string GetHeaderFormatName(
const COFFFormat& coff)
const override;
208 offset_t
RVAToAddress(uint32_t rva,
bool suppress_on_zero =
false)
const;
220 static constexpr uint32_t
EXECUTE = 0x20000000;
222 static constexpr uint32_t
READ = 0x40000000;
224 static constexpr uint32_t
WRITE = 0x80000000;
236 void ReadSectionData(
const std::shared_ptr<Linker::Reader>& rd,
const COFFFormat& coff_format)
override;
237 void WriteSectionData(
const std::shared_ptr<Linker::Writer>& wr,
const COFFFormat& coff_format)
const override;
238 uint32_t
ImageSize(
const COFFFormat& coff_format)
const override;
239 void Dump(
Dumper::Dumper& dump,
const COFFFormat& format,
unsigned section_index)
const override;
242 virtual void ReadSectionData(
const std::shared_ptr<Linker::Reader>& rd,
const PEFormat& fmt);
244 virtual void WriteSectionData(
const std::shared_ptr<Linker::Writer>& wr,
const PEFormat& fmt)
const;
250 size_t ReadData(
size_t bytes,
size_t offset,
void * buffer)
const;
259 std::vector<std::shared_ptr<COFFFormat::Section>>::iterator coff_iterator;
260 bool operator !=(
const iterator& other)
const;
262 std::shared_ptr<PEFormat::Section> operator*()
const;
266 std::vector<std::shared_ptr<COFFFormat::Section>>::const_iterator coff_iterator;
269 std::shared_ptr<const PEFormat::Section> operator*()
const;
273 std::vector<std::shared_ptr<COFFFormat::Section>>::const_reference coff_reference;
302 size_t MapRVAToSectionData(uint32_t rva,
size_t bytes, std::shared_ptr<Section>& found_section,
size_t& section_offset)
const;
305 size_t ReadData(
size_t bytes, uint32_t rva,
void * buffer)
const;
311 std::string
ReadASCII(uint32_t rva,
char terminator,
size_t maximum =
size_t(-1))
const;
313 std::string
ReadData(uint32_t rva,
size_t count)
const;
316 template <
typename Clock = POSIX_clock>
319 if constexpr(std::is_signed_v<typename Clock::rep>)
333 : fmt(fmt), rva(rva), size(size)
338 size_t ReadData(
size_t bytes, offset_t offset,
void * buffer)
const override;
372 std::shared_ptr<Linker::Section>
section =
nullptr;
386 void WriteDirectories(
const std::shared_ptr<Linker::Writer>& wr,
const PEFormat& fmt, uint32_t section_pointer)
const;
388 offset_t
WriteResource(
const std::shared_ptr<Linker::Writer>& wr,
const PEFormat& fmt, uint32_t rva_to_offset)
const;
391 class ResourcesSection;
402 Name(std::string name =
"")
409 template <
typename Key>
416 std::variant<std::shared_ptr<Resource>, std::shared_ptr<ResourceDirectory>>
content;
420 bool IsSubdirectory()
const {
return std::get_if<std::shared_ptr<ResourceDirectory>>(&
content); }
425 ::Timestamp<POSIX_clock> timestamp = { };
437 void AddResource(std::shared_ptr<Resource>& resource,
size_t level = 0);
450 void WriteDirectories(
const std::shared_ptr<Linker::Writer>& wr,
const PEFormat& fmt, uint32_t section_pointer)
const;
452 offset_t
WriteResources(
const std::shared_ptr<Linker::Writer>& wr,
const PEFormat& fmt, uint32_t rva_to_offset)
const;
465 size_t max_depth = 0;
467 std::vector<std::string> string_table;
468 std::map<std::string, uint32_t> string_table_map;
469 uint32_t string_table_offset = 0;
470 uint32_t string_table_end_offset = 0;
472 uint32_t FetchString(std::string s);
474 using Section::ReadSectionData;
475 using Section::WriteSectionData;
476 using Section::ImageSize;
478 bool IsPresent()
const;
492 std::shared_ptr<ResourcesSection>
resources = std::make_shared<ResourcesSection>();
498 typedef uint16_t Ordinal;
524 ::Timestamp<POSIX_clock> timestamp = { };
525 uint32_t forwarder_chain = 0;
574 using Section::ReadSectionData;
575 using Section::WriteSectionData;
576 using Section::ImageSize;
578 bool IsPresent()
const;
592 std::shared_ptr<ImportsSection>
imports = std::make_shared<ImportsSection>();
632 std::variant<uint32_t, Forwarder>
value;
646 ExportedEntry(Forwarder fwd)
657 ::Timestamp<POSIX_clock> timestamp = { };
674 std::map<uint32_t, std::shared_ptr<ExportedEntry>>
entries;
679 void SetEntry(
PEFormat& fmt, uint32_t ordinal, std::shared_ptr<ExportedEntry> entry);
690 using Section::ReadSectionData;
691 using Section::WriteSectionData;
692 using Section::ImageSize;
694 bool IsPresent()
const;
708 std::shared_ptr<ExportsSection>
exports = std::make_shared<ExportsSection>();
789 std::map<uint32_t, std::shared_ptr<BaseRelocationBlock>>
blocks_map;
797 using Section::ReadSectionData;
798 using Section::WriteSectionData;
799 using Section::ImageSize;
801 bool IsPresent()
const;
815 std::shared_ptr<BaseRelocationsSection>
base_relocations = std::make_shared<BaseRelocationsSection>();
836 using Section::ReadSectionData;
837 using Section::WriteSectionData;
838 using Section::ImageSize;
852 std::shared_ptr<CLRHeaderSection> clr_header = std::make_shared<CLRHeaderSection>();
856 void ReadFile(
const std::shared_ptr<Linker::Reader>& rd)
override;
858 ::EndianType GetMachineEndianType()
const;
862 offset_t
WriteFile(
const std::shared_ptr<Linker::Writer>& wr)
const override;
953 std::map<std::pair<std::string, std::string>, uint32_t> import_thunks_by_name;
954 std::map<std::pair<std::string, uint16_t>, uint32_t> import_thunks_by_ordinal;
977 {
TargetWinCE,
"targets Windows CE/Windows Phone/Windows Mobile (pre-8)" },
978 {
TargetMacintosh,
"targets the Windows Portability Library on Macintosh (not implemented)" },
979 {
TargetWin32s,
"targets the Win32s layer for Windows 3.11" },
980 {
TargetTNT,
"targets the Phar Lap TNT DOS Extender (not implemented)" },
981 {
TargetEFI,
"targets EFI/UEFI (not implemented)" },
982 {
TargetDotNET,
"targets the .NET environment (not implemented)" },
983 {
TargetXbox,
"targets Xbox (not implemented)" },
991 enum subsystem_determination
993 Subsystem_Windows = 1,
998 Subsystem_EFIApplication,
999 Subsystem_EFIBootServiceDriver,
1000 Subsystem_EFIRuntimeDriver,
1002 Subsystem_BootApplication,
1010 "WINDOWS", Subsystem_Windows,
1011 "CONSOLE", Subsystem_Console,
1012 "NATIVE", Subsystem_Native,
1013 "OS2", Subsystem_OS2,
1014 "POSIX", Subsystem_POSIX,
1015 "EFI_APPLICATION", Subsystem_EFIApplication,
1016 "EFI_BOOT_SERVICE_DRIVER", Subsystem_EFIBootServiceDriver,
1017 "EFI_RUNTIME_DRIVER", Subsystem_EFIRuntimeDriver,
1018 "EFI_ROM", Subsystem_EFI_ROM,
1019 "BOOT_APPLICATION", Subsystem_BootApplication)
1022 { Subsystem_Windows,
"creates a graphical program" },
1023 { Subsystem_Console,
"creates a console (text mode) program" },
1024 { Subsystem_Native,
"creates a native Windows NT kernel program" },
1025 { Subsystem_OS2,
"creates a program that uses the OS/2 personality" },
1026 { Subsystem_POSIX,
"creates a program that uses the POSIX personality" },
1027 { Subsystem_EFIApplication,
"creates an EFI application" },
1028 { Subsystem_EFIBootServiceDriver,
"creates an EFI boot service driver" },
1029 { Subsystem_EFIRuntimeDriver,
"creates an EFI run-time driver" },
1030 { Subsystem_EFI_ROM,
"creates an EFI ROM image" },
1031 { Subsystem_BootApplication,
"creates a Windows boot application" },
1047 {
OUTPUT_DLL,
"creates a dynamic linking library (DLL)" },
1069 {
CompatibleGNU,
"mimics the GNU (MinGW) linker (not implemented)" },
1079 Linker::Option<offset_t> image_base{
"base",
"Base address of image, used for calculating relative virtual addresses"};
1081 Linker::Option<bool> import_thunks{
"import_thunks",
"Create thunk procedures for imported names"};
1086 InitializeFields(stub, target, subsystem, output, compat, image_base, section_align, import_thunks);
1104 unsigned FormatAdditionalSectionFlags(std::string section_name)
const override;
1106 std::shared_ptr<Resource>& AddResource(std::shared_ptr<Resource>& resource);
1108 ImportedLibrary& FetchImportLibrary(std::string library_name,
bool create_if_not_present =
false);
1109 void AddImportByName(std::string library_name, std::string entry_name, uint16_t hint);
1110 void AddImportByOrdinal(std::string library_name, uint16_t ordinal);
1112 static std::vector<Linker::OptionDescription<void> *> ParameterNames;
1114 std::shared_ptr<Linker::OptionCollector>
GetOptions()
override;
1151 void SetOptions(std::map<std::string, std::string>& options)
override;
1154 void OnNewSegment(std::shared_ptr<Linker::Segment> segment)
override;
1204 offset_t GetMemoryImageEnd()
const;
1219 : pe(pe), width(width)
1223 void DisplayValue(
Dumper::Dumper& dump, std::tuple<offset_t> values)
override;
1226 std::shared_ptr<RVADisplay> MakeRVADisplay(
unsigned width = 8)
const;
1232 typedef ResourceFile::Identifier Identifier;
1234 static void ReadIdentifier(
const std::shared_ptr<Linker::Reader>& rd, Identifier&
id);
1235 static void WriteIdentifier(
const std::shared_ptr<Linker::Writer>& wr,
const Identifier&
id);
1236 static offset_t GetIdentifierSize(
const Identifier&
id);
1241 uint32_t header_size;
1242 uint32_t data_version;
1243 uint16_t language_id;
1245 uint32_t characteristics;
1248 offset_t file_offset;
1249 std::vector<Resource> resources;
1251 void ReadFile(
const std::shared_ptr<Linker::Reader>& rd)
override;
1252 void ReadFile(
const std::shared_ptr<Linker::Reader>& rd, offset_t size);
1253 void CalculateValues();
1255 offset_t
WriteFile(
const std::shared_ptr<Linker::Writer>& wr)
const override;
A record whose values should be displayed together, as a collection.
Definition dumper.h:797
This class represents an entry that can be displayed in a file dump.
Definition dumper.h:198
An abstract interface that separates structure and presentation of the data inside a file.
Definition dumper.h:1009
A record that represents a region within the file.
Definition dumper.h:902
A representation of an enumeration with associated string representations for each value.
Definition options.h:15
std::map< value_type, std::string > descriptions
An empty dictionary that explains the value types in detail.
Definition options.h:20
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:499
Documents and handles command line options.
Definition options.h:306
offset_t WriteFile(const std::shared_ptr< Linker::Writer > &wr) const override
Stores data in memory to file.
Definition peexe.cc:4217
void ReadFile(const std::shared_ptr< Linker::Reader > &rd) override
Loads file into memory.
Definition peexe.cc:4199
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition peexe.cc:4245
Encompasses the information necessary to encode a forwarder exported reference.
Definition peexe.h:604
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:609
std::string reference_name
A string representation of the forwarder, to appear in the image.
Definition peexe.h:611
uint32_t rva
The relative virtual address of the reference name.
Definition peexe.h:613
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:607
Represents a name by which an exported entry is identified.
Definition peexe.h:618
uint32_t rva
The relative virtual address of the name string.
Definition peexe.h:623
std::string name
The name, as a string.
Definition peexe.h:621
Represents a single exported entry in the file.
Definition peexe.h:596
std::optional< Name > name
An optional name by which the exported entry is identified, otherwise its position in the export tabl...
Definition peexe.h:634
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:632
Represents a single entry in the resource directory.
Definition peexe.h:411
Key identifier
The value that identifiers the resource at the current level.
Definition peexe.h:414
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:416
uint32_t content_offset
The offset of the contents within the section.
Definition peexe.h:418