aboutsummaryrefslogtreecommitdiff
path: root/tools/llvm-objcopy/COFF/Reader.cpp
blob: 2fcec0057c036d78a9faf0415ddb12f0dae872fb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
//===- Reader.cpp ---------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "Reader.h"
#include "Object.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/BinaryFormat/COFF.h"
#include "llvm/Object/COFF.h"
#include "llvm/Support/ErrorHandling.h"
#include <cstddef>
#include <cstdint>

namespace llvm {
namespace objcopy {
namespace coff {

using namespace object;
using namespace COFF;

Error COFFReader::readExecutableHeaders(Object &Obj) const {
  const dos_header *DH = COFFObj.getDOSHeader();
  Obj.Is64 = COFFObj.is64();
  if (!DH)
    return Error::success();

  Obj.IsPE = true;
  Obj.DosHeader = *DH;
  if (DH->AddressOfNewExeHeader > sizeof(*DH))
    Obj.DosStub = ArrayRef<uint8_t>(reinterpret_cast<const uint8_t *>(&DH[1]),
                                    DH->AddressOfNewExeHeader - sizeof(*DH));

  if (COFFObj.is64()) {
    Obj.PeHeader = *COFFObj.getPE32PlusHeader();
  } else {
    const pe32_header *PE32 = COFFObj.getPE32Header();
    copyPeHeader(Obj.PeHeader, *PE32);
    // The pe32plus_header (stored in Object) lacks the BaseOfData field.
    Obj.BaseOfData = PE32->BaseOfData;
  }

  for (size_t I = 0; I < Obj.PeHeader.NumberOfRvaAndSize; I++) {
    const data_directory *Dir;
    if (auto EC = COFFObj.getDataDirectory(I, Dir))
      return errorCodeToError(EC);
    Obj.DataDirectories.emplace_back(*Dir);
  }
  return Error::success();
}

Error COFFReader::readSections(Object &Obj) const {
  std::vector<Section> Sections;
  // Section indexing starts from 1.
  for (size_t I = 1, E = COFFObj.getNumberOfSections(); I <= E; I++) {
    const coff_section *Sec;
    if (auto EC = COFFObj.getSection(I, Sec))
      return errorCodeToError(EC);
    Sections.push_back(Section());
    Section &S = Sections.back();
    S.Header = *Sec;
    ArrayRef<uint8_t> Contents;
    if (Error E = COFFObj.getSectionContents(Sec, Contents))
      return E;
    S.setContentsRef(Contents);
    ArrayRef<coff_relocation> Relocs = COFFObj.getRelocations(Sec);
    for (const coff_relocation &R : Relocs)
      S.Relocs.push_back(R);
    if (Expected<StringRef> NameOrErr = COFFObj.getSectionName(Sec))
      S.Name = *NameOrErr;
    else
      return NameOrErr.takeError();
    if (Sec->hasExtendedRelocations())
      return createStringError(object_error::parse_failed,
                               "extended relocations not supported yet");
  }
  Obj.addSections(Sections);
  return Error::success();
}

Error COFFReader::readSymbols(Object &Obj, bool IsBigObj) const {
  std::vector<Symbol> Symbols;
  Symbols.reserve(COFFObj.getRawNumberOfSymbols());
  ArrayRef<Section> Sections = Obj.getSections();
  for (uint32_t I = 0, E = COFFObj.getRawNumberOfSymbols(); I < E;) {
    Expected<COFFSymbolRef> SymOrErr = COFFObj.getSymbol(I);
    if (!SymOrErr)
      return SymOrErr.takeError();
    COFFSymbolRef SymRef = *SymOrErr;

    Symbols.push_back(Symbol());
    Symbol &Sym = Symbols.back();
    // Copy symbols from the original form into an intermediate coff_symbol32.
    if (IsBigObj)
      copySymbol(Sym.Sym,
                 *reinterpret_cast<const coff_symbol32 *>(SymRef.getRawPtr()));
    else
      copySymbol(Sym.Sym,
                 *reinterpret_cast<const coff_symbol16 *>(SymRef.getRawPtr()));
    if (auto EC = COFFObj.getSymbolName(SymRef, Sym.Name))
      return errorCodeToError(EC);

    ArrayRef<uint8_t> AuxData = COFFObj.getSymbolAuxData(SymRef);
    size_t SymSize = IsBigObj ? sizeof(coff_symbol32) : sizeof(coff_symbol16);
    assert(AuxData.size() == SymSize * SymRef.getNumberOfAuxSymbols());
    // The auxillary symbols are structs of sizeof(coff_symbol16) each.
    // In the big object format (where symbols are coff_symbol32), each
    // auxillary symbol is padded with 2 bytes at the end. Copy each
    // auxillary symbol to the Sym.AuxData vector. For file symbols,
    // the whole range of aux symbols are interpreted as one null padded
    // string instead.
    if (SymRef.isFileRecord())
      Sym.AuxFile = StringRef(reinterpret_cast<const char *>(AuxData.data()),
                              AuxData.size())
                        .rtrim('\0');
    else
      for (size_t I = 0; I < SymRef.getNumberOfAuxSymbols(); I++)
        Sym.AuxData.push_back(AuxData.slice(I * SymSize, sizeof(AuxSymbol)));

    // Find the unique id of the section
    if (SymRef.getSectionNumber() <=
        0) // Special symbol (undefined/absolute/debug)
      Sym.TargetSectionId = SymRef.getSectionNumber();
    else if (static_cast<uint32_t>(SymRef.getSectionNumber() - 1) <
             Sections.size())
      Sym.TargetSectionId = Sections[SymRef.getSectionNumber() - 1].UniqueId;
    else
      return createStringError(object_error::parse_failed,
                               "section number out of range");
    // For section definitions, check if it is comdat associative, and if
    // it is, find the target section unique id.
    const coff_aux_section_definition *SD = SymRef.getSectionDefinition();
    const coff_aux_weak_external *WE = SymRef.getWeakExternal();
    if (SD && SD->Selection == IMAGE_COMDAT_SELECT_ASSOCIATIVE) {
      int32_t Index = SD->getNumber(IsBigObj);
      if (Index <= 0 || static_cast<uint32_t>(Index - 1) >= Sections.size())
        return createStringError(object_error::parse_failed,
                                 "unexpected associative section index");
      Sym.AssociativeComdatTargetSectionId = Sections[Index - 1].UniqueId;
    } else if (WE) {
      // This is a raw symbol index for now, but store it in the Symbol
      // until we've added them to the Object, which assigns the final
      // unique ids.
      Sym.WeakTargetSymbolId = WE->TagIndex;
    }
    I += 1 + SymRef.getNumberOfAuxSymbols();
  }
  Obj.addSymbols(Symbols);
  return Error::success();
}

Error COFFReader::setSymbolTargets(Object &Obj) const {
  std::vector<const Symbol *> RawSymbolTable;
  for (const Symbol &Sym : Obj.getSymbols()) {
    RawSymbolTable.push_back(&Sym);
    for (size_t I = 0; I < Sym.Sym.NumberOfAuxSymbols; I++)
      RawSymbolTable.push_back(nullptr);
  }
  for (Symbol &Sym : Obj.getMutableSymbols()) {
    // Convert WeakTargetSymbolId from the original raw symbol index to
    // a proper unique id.
    if (Sym.WeakTargetSymbolId) {
      if (*Sym.WeakTargetSymbolId >= RawSymbolTable.size())
        return createStringError(object_error::parse_failed,
                                 "weak external reference out of range");
      const Symbol *Target = RawSymbolTable[*Sym.WeakTargetSymbolId];
      if (Target == nullptr)
        return createStringError(object_error::parse_failed,
                                 "invalid SymbolTableIndex");
      Sym.WeakTargetSymbolId = Target->UniqueId;
    }
  }
  for (Section &Sec : Obj.getMutableSections()) {
    for (Relocation &R : Sec.Relocs) {
      if (R.Reloc.SymbolTableIndex >= RawSymbolTable.size())
        return createStringError(object_error::parse_failed,
                                 "SymbolTableIndex out of range");
      const Symbol *Sym = RawSymbolTable[R.Reloc.SymbolTableIndex];
      if (Sym == nullptr)
        return createStringError(object_error::parse_failed,
                                 "invalid SymbolTableIndex");
      R.Target = Sym->UniqueId;
      R.TargetName = Sym->Name;
    }
  }
  return Error::success();
}

Expected<std::unique_ptr<Object>> COFFReader::create() const {
  auto Obj = std::make_unique<Object>();

  bool IsBigObj = false;
  if (const coff_file_header *CFH = COFFObj.getCOFFHeader()) {
    Obj->CoffFileHeader = *CFH;
  } else {
    const coff_bigobj_file_header *CBFH = COFFObj.getCOFFBigObjHeader();
    if (!CBFH)
      return createStringError(object_error::parse_failed,
                               "no COFF file header returned");
    // Only copying the few fields from the bigobj header that we need
    // and won't recreate in the end.
    Obj->CoffFileHeader.Machine = CBFH->Machine;
    Obj->CoffFileHeader.TimeDateStamp = CBFH->TimeDateStamp;
    IsBigObj = true;
  }

  if (Error E = readExecutableHeaders(*Obj))
    return std::move(E);
  if (Error E = readSections(*Obj))
    return std::move(E);
  if (Error E = readSymbols(*Obj, IsBigObj))
    return std::move(E);
  if (Error E = setSymbolTargets(*Obj))
    return std::move(E);

  return std::move(Obj);
}

} // end namespace coff
} // end namespace objcopy
} // end namespace llvm