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;
189 offset_t
RVAToAddress(uint32_t rva,
bool suppress_on_zero =
false)
const;
206 offset_t
RVAToAddress(uint32_t rva,
bool suppress_on_zero =
false)
const;
218 static constexpr uint32_t
EXECUTE = 0x20000000;
220 static constexpr uint32_t
READ = 0x40000000;
222 static constexpr uint32_t
WRITE = 0x80000000;
234 void ReadSectionData(
Linker::Reader& rd,
const COFFFormat& coff_format)
override;
235 void WriteSectionData(
Linker::Writer& wr,
const COFFFormat& coff_format)
const override;
236 uint32_t
ImageSize(
const COFFFormat& coff_format)
const override;
237 void Dump(
Dumper::Dumper& dump,
const COFFFormat& format,
unsigned section_index)
const override;
248 size_t ReadData(
size_t bytes,
size_t offset,
void * buffer)
const;
257 std::vector<std::shared_ptr<COFFFormat::Section>>::iterator coff_iterator;
258 bool operator !=(
const iterator& other)
const;
260 std::shared_ptr<PEFormat::Section> operator*()
const;
264 std::vector<std::shared_ptr<COFFFormat::Section>>::const_iterator coff_iterator;
267 std::shared_ptr<const PEFormat::Section> operator*()
const;
271 std::vector<std::shared_ptr<COFFFormat::Section>>::const_reference coff_reference;
300 size_t MapRVAToSectionData(uint32_t rva,
size_t bytes, std::shared_ptr<Section>& found_section,
size_t& section_offset)
const;
303 size_t ReadData(
size_t bytes, uint32_t rva,
void * buffer)
const;
309 std::string
ReadASCII(uint32_t rva,
char terminator,
size_t maximum =
size_t(-1))
const;
311 std::string
ReadData(uint32_t rva,
size_t count)
const;
321 : fmt(fmt), rva(rva), size(size)
328 size_t ReadData(
size_t bytes, offset_t offset,
void * buffer)
const override;
362 std::shared_ptr<Linker::Section>
section =
nullptr;
381 class ResourcesSection;
392 Name(std::string name =
"")
399 template <
typename Key>
406 std::variant<std::shared_ptr<Resource>, std::shared_ptr<ResourceDirectory>>
content;
410 bool IsSubdirectory()
const {
return std::get_if<std::shared_ptr<ResourceDirectory>>(&
content); }
415 uint32_t timestamp = 0;
427 void AddResource(std::shared_ptr<Resource>& resource,
size_t level = 0);
455 size_t max_depth = 0;
457 std::vector<std::string> string_table;
458 std::map<std::string, uint32_t> string_table_map;
459 uint32_t string_table_offset = 0;
460 uint32_t string_table_end_offset = 0;
462 uint32_t FetchString(std::string s);
464 using Section::ReadSectionData;
465 using Section::WriteSectionData;
466 using Section::ImageSize;
468 bool IsPresent()
const;
482 std::shared_ptr<ResourcesSection>
resources = std::make_shared<ResourcesSection>();
488 typedef uint16_t Ordinal;
514 uint32_t timestamp = 0;
515 uint32_t forwarder_chain = 0;
564 using Section::ReadSectionData;
565 using Section::WriteSectionData;
566 using Section::ImageSize;
568 bool IsPresent()
const;
582 std::shared_ptr<ImportsSection>
imports = std::make_shared<ImportsSection>();
622 std::variant<uint32_t, Forwarder>
value;
636 ExportedEntry(Forwarder fwd)
647 uint32_t timestamp = 0;
664 std::map<uint32_t, std::shared_ptr<ExportedEntry>>
entries;
669 void SetEntry(
PEFormat& fmt, uint32_t ordinal, std::shared_ptr<ExportedEntry> entry);
680 using Section::ReadSectionData;
681 using Section::WriteSectionData;
682 using Section::ImageSize;
684 bool IsPresent()
const;
698 std::shared_ptr<ExportsSection>
exports = std::make_shared<ExportsSection>();
779 std::map<uint32_t, std::shared_ptr<BaseRelocationBlock>>
blocks_map;
787 using Section::ReadSectionData;
788 using Section::WriteSectionData;
789 using Section::ImageSize;
791 bool IsPresent()
const;
805 std::shared_ptr<BaseRelocationsSection>
base_relocations = std::make_shared<BaseRelocationsSection>();
819 ::EndianType GetMachineEndianType()
const;
915 std::map<std::pair<std::string, std::string>, uint32_t> import_thunks_by_name;
916 std::map<std::pair<std::string, uint16_t>, uint32_t> import_thunks_by_ordinal;
939 {
TargetWinCE,
"targets Windows CE/Windows Phone/Windows Mobile (pre-8)" },
940 {
TargetMacintosh,
"targets the Windows Portability Library on Macintosh (not implemented)" },
941 {
TargetWin32s,
"targets the Win32s layer for Windows 3.11" },
942 {
TargetTNT,
"targets the Phar Lap TNT DOS Extender (not implemented)" },
943 {
TargetEFI,
"targets EFI/UEFI (not implemented)" },
944 {
TargetDotNET,
"targets the .NET environment (not implemented)" },
945 {
TargetXbox,
"targets Xbox (not implemented)" },
953 enum subsystem_determination
955 Subsystem_Windows = 1,
960 Subsystem_EFIApplication,
961 Subsystem_EFIBootServiceDriver,
962 Subsystem_EFIRuntimeDriver,
964 Subsystem_BootApplication,
972 "WINDOWS", Subsystem_Windows,
973 "CONSOLE", Subsystem_Console,
974 "NATIVE", Subsystem_Native,
975 "OS2", Subsystem_OS2,
976 "POSIX", Subsystem_POSIX,
977 "EFI_APPLICATION", Subsystem_EFIApplication,
978 "EFI_BOOT_SERVICE_DRIVER", Subsystem_EFIBootServiceDriver,
979 "EFI_RUNTIME_DRIVER", Subsystem_EFIRuntimeDriver,
980 "EFI_ROM", Subsystem_EFI_ROM,
981 "BOOT_APPLICATION", Subsystem_BootApplication)
984 { Subsystem_Windows,
"creates a graphical program" },
985 { Subsystem_Console,
"creates a console (text mode) program" },
986 { Subsystem_Native,
"creates a native Windows NT kernel program" },
987 { Subsystem_OS2,
"creates a program that uses the OS/2 personality" },
988 { Subsystem_POSIX,
"creates a program that uses the POSIX personality" },
989 { Subsystem_EFIApplication,
"creates an EFI application" },
990 { Subsystem_EFIBootServiceDriver,
"creates an EFI boot service driver" },
991 { Subsystem_EFIRuntimeDriver,
"creates an EFI run-time driver" },
992 { Subsystem_EFI_ROM,
"creates an EFI ROM image" },
993 { Subsystem_BootApplication,
"creates a Windows boot application" },
1009 {
OUTPUT_DLL,
"creates a dynamic linking library (DLL)" },
1031 {
CompatibleGNU,
"mimics the GNU (MinGW) linker (not implemented)" },
1041 Linker::Option<offset_t> image_base{
"base",
"Base address of image, used for calculating relative virtual addresses"};
1043 Linker::Option<bool> import_thunks{
"import_thunks",
"Create thunk procedures for imported names"};
1048 InitializeFields(stub, target, subsystem, output, compat, image_base, section_align, import_thunks);
1066 unsigned FormatAdditionalSectionFlags(std::string section_name)
const override;
1068 std::shared_ptr<Resource>& AddResource(std::shared_ptr<Resource>& resource);
1070 ImportedLibrary& FetchImportLibrary(std::string library_name,
bool create_if_not_present =
false);
1071 void AddImportByName(std::string library_name, std::string entry_name, uint16_t hint);
1072 void AddImportByOrdinal(std::string library_name, uint16_t ordinal);
1074 static std::vector<Linker::OptionDescription<void> *> ParameterNames;
1076 std::shared_ptr<Linker::OptionCollector>
GetOptions()
override;
1113 void SetOptions(std::map<std::string, std::string>& options)
override;
1116 void OnNewSegment(std::shared_ptr<Linker::Segment> segment)
override;
1166 offset_t GetMemoryImageEnd()
const;
1181 : pe(pe), width(width)
1185 void DisplayValue(
Dumper::Dumper& dump, std::tuple<offset_t> values)
override;
1188 std::shared_ptr<RVADisplay> MakeRVADisplay(
unsigned width = 8)
const;
1194 typedef ResourceFile::Identifier Identifier;
1197 static void WriteIdentifier(
Linker::Writer& wr,
const Identifier&
id);
1198 static offset_t GetIdentifierSize(
const Identifier&
id);
1203 uint32_t header_size;
1204 uint32_t data_version;
1205 uint16_t language_id;
1207 uint32_t characteristics;
1210 offset_t file_offset;
1211 std::vector<Resource> resources;
1215 void CalculateValues();
A record whose values should be displayed together, as a collection.
Definition dumper.h:566
This class represents an entry that can be displayed in a file dump.
Definition dumper.h:92
An abstract interface that separates structure and presentation of the data inside a file.
Definition dumper.h:773
A record that represents a region within the file.
Definition dumper.h:666
virtual offset_t WriteFile(Writer &wr, offset_t count, offset_t offset=0) const =0
Writes data of non-zero filled sections.
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:474
Documents and handles command line options.
Definition options.h:306
A helper class, encapsulating functionality needed to import binary data.
Definition reader.h:20
A helper class, encapsulating functionality needed to export binary data.
Definition writer.h:15
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition peexe.cc:4170
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition peexe.cc:4198
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition peexe.cc:4152
Encompasses the information necessary to encode a forwarder exported reference.
Definition peexe.h:594
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:599
std::string reference_name
A string representation of the forwarder, to appear in the image.
Definition peexe.h:601
uint32_t rva
The relative virtual address of the reference name.
Definition peexe.h:603
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:597
Represents a name by which an exported entry is identified.
Definition peexe.h:608
uint32_t rva
The relative virtual address of the name string.
Definition peexe.h:613
std::string name
The name, as a string.
Definition peexe.h:611
Represents a single exported entry in the file.
Definition peexe.h:586
std::optional< Name > name
An optional name by which the exported entry is identified, otherwise its position in the export tabl...
Definition peexe.h:624
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:622
Represents a single entry in the resource directory.
Definition peexe.h:401
Key identifier
The value that identifiers the resource at the current level.
Definition peexe.h:404
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:406
uint32_t content_offset
The offset of the contents within the section.
Definition peexe.h:408