blob: 89f9903c0fd2e6698b07d0a8d0c89316897d3fa6 [file] [log] [blame]
Martin Storsjoe84a0b52018-12-19 07:24:38 +00001//===- Object.h -------------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef LLVM_TOOLS_OBJCOPY_COFF_OBJECT_H
11#define LLVM_TOOLS_OBJCOPY_COFF_OBJECT_H
12
13#include "llvm/ADT/ArrayRef.h"
14#include "llvm/ADT/StringRef.h"
15#include "llvm/BinaryFormat/COFF.h"
16#include "llvm/Object/COFF.h"
17#include <cstddef>
18#include <cstdint>
19#include <vector>
20
21namespace llvm {
22namespace objcopy {
23namespace coff {
24
25struct Section {
26 object::coff_section Header;
27 ArrayRef<uint8_t> Contents;
28 std::vector<object::coff_relocation> Relocs;
29 StringRef Name;
30};
31
32struct Symbol {
33 object::coff_symbol32 Sym;
34 StringRef Name;
35 ArrayRef<uint8_t> AuxData;
36};
37
38struct Object {
39 bool IsPE = false;
40
41 object::dos_header DosHeader;
42 ArrayRef<uint8_t> DosStub;
43
44 object::coff_file_header CoffFileHeader;
45
46 bool Is64 = false;
47 object::pe32plus_header PeHeader;
48 uint32_t BaseOfData = 0; // pe32plus_header lacks this field.
49
50 std::vector<object::data_directory> DataDirectories;
51 std::vector<Section> Sections;
52 std::vector<Symbol> Symbols;
53};
54
55// Copy between coff_symbol16 and coff_symbol32.
56// The source and destination files can use either coff_symbol16 or
57// coff_symbol32, while we always store them as coff_symbol32 in the
58// intermediate data structure.
59template <class Symbol1Ty, class Symbol2Ty>
60void copySymbol(Symbol1Ty &Dest, const Symbol2Ty &Src) {
61 static_assert(sizeof(Dest.Name.ShortName) == sizeof(Src.Name.ShortName),
62 "Mismatched name sizes");
63 memcpy(Dest.Name.ShortName, Src.Name.ShortName, sizeof(Dest.Name.ShortName));
64 Dest.Value = Src.Value;
65 Dest.SectionNumber = Src.SectionNumber;
66 Dest.Type = Src.Type;
67 Dest.StorageClass = Src.StorageClass;
68 Dest.NumberOfAuxSymbols = Src.NumberOfAuxSymbols;
69}
70
71// Copy between pe32_header and pe32plus_header.
72// We store the intermediate state in a pe32plus_header.
73template <class PeHeader1Ty, class PeHeader2Ty>
74void copyPeHeader(PeHeader1Ty &Dest, const PeHeader2Ty &Src) {
75 Dest.Magic = Src.Magic;
76 Dest.MajorLinkerVersion = Src.MajorLinkerVersion;
77 Dest.MinorLinkerVersion = Src.MinorLinkerVersion;
78 Dest.SizeOfCode = Src.SizeOfCode;
79 Dest.SizeOfInitializedData = Src.SizeOfInitializedData;
80 Dest.SizeOfUninitializedData = Src.SizeOfUninitializedData;
81 Dest.AddressOfEntryPoint = Src.AddressOfEntryPoint;
82 Dest.BaseOfCode = Src.BaseOfCode;
83 Dest.ImageBase = Src.ImageBase;
84 Dest.SectionAlignment = Src.SectionAlignment;
85 Dest.FileAlignment = Src.FileAlignment;
86 Dest.MajorOperatingSystemVersion = Src.MajorOperatingSystemVersion;
87 Dest.MinorOperatingSystemVersion = Src.MinorOperatingSystemVersion;
88 Dest.MajorImageVersion = Src.MajorImageVersion;
89 Dest.MinorImageVersion = Src.MinorImageVersion;
90 Dest.MajorSubsystemVersion = Src.MajorSubsystemVersion;
91 Dest.MinorSubsystemVersion = Src.MinorSubsystemVersion;
92 Dest.Win32VersionValue = Src.Win32VersionValue;
93 Dest.SizeOfImage = Src.SizeOfImage;
94 Dest.SizeOfHeaders = Src.SizeOfHeaders;
95 Dest.CheckSum = Src.CheckSum;
96 Dest.Subsystem = Src.Subsystem;
97 Dest.DLLCharacteristics = Src.DLLCharacteristics;
98 Dest.SizeOfStackReserve = Src.SizeOfStackReserve;
99 Dest.SizeOfStackCommit = Src.SizeOfStackCommit;
100 Dest.SizeOfHeapReserve = Src.SizeOfHeapReserve;
101 Dest.SizeOfHeapCommit = Src.SizeOfHeapCommit;
102 Dest.LoaderFlags = Src.LoaderFlags;
103 Dest.NumberOfRvaAndSize = Src.NumberOfRvaAndSize;
104}
105
106} // end namespace coff
107} // end namespace objcopy
108} // end namespace llvm
109
110#endif // LLVM_TOOLS_OBJCOPY_COFF_OBJECT_H