LLVM: lib/ObjCopy/XCOFF/XCOFFWriter.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
11
12namespace llvm {
15
16using namespace object;
17
18void XCOFFWriter::finalizeHeaders() {
19
20 FileSize += sizeof(XCOFFFileHeader32);
21
22 FileSize += Obj.FileHeader.AuxHeaderSize;
23
24 FileSize += sizeof(XCOFFSectionHeader32) * Obj.Sections.size();
25}
26
27void XCOFFWriter::finalizeSections() {
28 for (const Section &Sec : Obj.Sections) {
29
30 FileSize += Sec.Contents.size();
31
32 FileSize +=
33 Sec.SectionHeader.NumberOfRelocations * sizeof(XCOFFRelocation32);
34 }
35}
36
37void XCOFFWriter::finalizeSymbolStringTable() {
38 assert(Obj.FileHeader.SymbolTableOffset >= FileSize);
39 FileSize = Obj.FileHeader.SymbolTableOffset;
40
41 FileSize +=
43
44 FileSize += Obj.StringTable.size();
45}
46
47void XCOFFWriter::finalize() {
48 FileSize = 0;
49 finalizeHeaders();
50 finalizeSections();
51 finalizeSymbolStringTable();
52}
53
54void XCOFFWriter::writeHeaders() {
55
56 uint8_t *Ptr = reinterpret_cast<uint8_t *>(Buf->getBufferStart());
57 memcpy(Ptr, &Obj.FileHeader, sizeof(XCOFFFileHeader32));
58 Ptr += sizeof(XCOFFFileHeader32);
59
60
61 if (Obj.FileHeader.AuxHeaderSize) {
62 memcpy(Ptr, &Obj.OptionalFileHeader, Obj.FileHeader.AuxHeaderSize);
63 Ptr += Obj.FileHeader.AuxHeaderSize;
64 }
65
66
67 for (const Section &Sec : Obj.Sections) {
68 memcpy(Ptr, &Sec.SectionHeader, sizeof(XCOFFSectionHeader32));
69 Ptr += sizeof(XCOFFSectionHeader32);
70 }
71}
72
73void XCOFFWriter::writeSections() {
74
75 for (const Section &Sec : Obj.Sections) {
76 uint8_t *Ptr = reinterpret_cast<uint8_t *>(Buf->getBufferStart()) +
77 Sec.SectionHeader.FileOffsetToRawData;
79 }
80
81
82 for (const Section &Sec : Obj.Sections) {
83 uint8_t *Ptr = reinterpret_cast<uint8_t *>(Buf->getBufferStart()) +
84 Sec.SectionHeader.FileOffsetToRelocationInfo;
85 for (const XCOFFRelocation32 &Rel : Sec.Relocations) {
86 memcpy(Ptr, &Rel, sizeof(XCOFFRelocation32));
87 Ptr += sizeof(XCOFFRelocation32);
88 }
89 }
90}
91
92void XCOFFWriter::writeSymbolStringTable() {
93
94 uint8_t *Ptr = reinterpret_cast<uint8_t *>(Buf->getBufferStart()) +
95 Obj.FileHeader.SymbolTableOffset;
96 for (const Symbol &Sym : Obj.Symbols) {
99
100 memcpy(Ptr, Sym.AuxSymbolEntries.data(), Sym.AuxSymbolEntries.size());
101 Ptr += Sym.AuxSymbolEntries.size();
102 }
103
104 memcpy(Ptr, Obj.StringTable.data(), Obj.StringTable.size());
105 Ptr += Obj.StringTable.size();
106}
107
111 if (!Buf)
113 "failed to allocate memory buffer of " +
115
116 writeHeaders();
117 writeSections();
118 writeSymbolStringTable();
119 Out.write(Buf->getBufferStart(), Buf->getBufferSize());
121}
122
123}
124}
125}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
static Twine utohexstr(uint64_t Val)
static LLVM_ABI std::unique_ptr< WritableMemoryBuffer > getNewMemBuffer(size_t Size, const Twine &BufferName="")
Allocate a new zero-initialized MemoryBuffer of the specified size.
Error write()
Definition XCOFFWriter.cpp:108
constexpr size_t SymbolTableEntrySize
This is an optimization pass for GlobalISel generic memory operations.
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
OutputIt copy(R &&Range, OutputIt Out)