File: xcoff2yaml.cpp

package info (click to toggle)
llvm-toolchain-18 1%3A18.1.8-18
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,908,340 kB
  • sloc: cpp: 6,667,937; ansic: 1,440,452; asm: 883,619; python: 230,549; objc: 76,880; f90: 74,238; lisp: 35,989; pascal: 16,571; sh: 10,229; perl: 7,459; ml: 5,047; awk: 3,523; makefile: 2,987; javascript: 2,149; xml: 892; fortran: 649; cs: 573
file content (406 lines) | stat: -rw-r--r-- 14,990 bytes parent folder | download
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//===------ xcoff2yaml.cpp - XCOFF YAMLIO implementation --------*- C++ -*-===//
//
// 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 "obj2yaml.h"
#include "llvm/Object/XCOFFObjectFile.h"
#include "llvm/ObjectYAML/XCOFFYAML.h"
#include "llvm/Support/Errc.h"
#include "llvm/Support/YAMLTraits.h"

using namespace llvm;
using namespace llvm::object;
namespace {

class XCOFFDumper {
  const object::XCOFFObjectFile &Obj;
  XCOFFYAML::Object YAMLObj;
  void dumpHeader();
  Error dumpSections();
  Error dumpSymbols();
  template <typename Shdr, typename Reloc>
  Error dumpSections(ArrayRef<Shdr> Sections);

  // Dump auxiliary symbols.
  Error dumpFileAuxSym(XCOFFYAML::Symbol &Sym,
                       const XCOFFSymbolRef &SymbolEntRef);
  Error dumpStatAuxSym(XCOFFYAML::Symbol &Sym,
                       const XCOFFSymbolRef &SymbolEntRef);
  Error dumpBlockAuxSym(XCOFFYAML::Symbol &Sym,
                        const XCOFFSymbolRef &SymbolEntRef);
  Error dumpDwarfAuxSym(XCOFFYAML::Symbol &Sym,
                        const XCOFFSymbolRef &SymbolEntRef);
  Error dumpAuxSyms(XCOFFYAML::Symbol &Sym, const XCOFFSymbolRef &SymbolEntRef);
  void dumpFuncAuxSym(XCOFFYAML::Symbol &Sym, const uintptr_t AuxAddress);
  void dumpExpAuxSym(XCOFFYAML::Symbol &Sym, const uintptr_t AuxAddress);
  void dumpCsectAuxSym(XCOFFYAML::Symbol &Sym,
                       const object::XCOFFCsectAuxRef &AuxEntPtr);

public:
  XCOFFDumper(const object::XCOFFObjectFile &obj) : Obj(obj) {}
  Error dump();
  XCOFFYAML::Object &getYAMLObj() { return YAMLObj; }

  template <typename T> const T *getAuxEntPtr(uintptr_t AuxAddress) {
    Obj.checkSymbolEntryPointer(AuxAddress);
    return reinterpret_cast<const T *>(AuxAddress);
  }
};
} // namespace

Error XCOFFDumper::dump() {
  dumpHeader();
  if (Error E = dumpSections())
    return E;
  return dumpSymbols();
}

void XCOFFDumper::dumpHeader() {
  YAMLObj.Header.Magic = Obj.getMagic();
  YAMLObj.Header.NumberOfSections = Obj.getNumberOfSections();
  YAMLObj.Header.TimeStamp = Obj.getTimeStamp();
  YAMLObj.Header.SymbolTableOffset = Obj.is64Bit()
                                         ? Obj.getSymbolTableOffset64()
                                         : Obj.getSymbolTableOffset32();
  YAMLObj.Header.NumberOfSymTableEntries =
      Obj.is64Bit() ? Obj.getNumberOfSymbolTableEntries64()
                    : Obj.getRawNumberOfSymbolTableEntries32();
  YAMLObj.Header.AuxHeaderSize = Obj.getOptionalHeaderSize();
  YAMLObj.Header.Flags = Obj.getFlags();
}

Error XCOFFDumper::dumpSections() {
  if (Obj.is64Bit())
    return dumpSections<XCOFFSectionHeader64, XCOFFRelocation64>(
        Obj.sections64());
  return dumpSections<XCOFFSectionHeader32, XCOFFRelocation32>(
      Obj.sections32());
}

template <typename Shdr, typename Reloc>
Error XCOFFDumper::dumpSections(ArrayRef<Shdr> Sections) {
  std::vector<XCOFFYAML::Section> &YamlSections = YAMLObj.Sections;
  for (const Shdr &S : Sections) {
    XCOFFYAML::Section YamlSec;
    YamlSec.SectionName = S.getName();
    YamlSec.Address = S.PhysicalAddress;
    YamlSec.Size = S.SectionSize;
    YamlSec.NumberOfRelocations = S.NumberOfRelocations;
    YamlSec.NumberOfLineNumbers = S.NumberOfLineNumbers;
    YamlSec.FileOffsetToData = S.FileOffsetToRawData;
    YamlSec.FileOffsetToRelocations = S.FileOffsetToRelocationInfo;
    YamlSec.FileOffsetToLineNumbers = S.FileOffsetToLineNumberInfo;
    YamlSec.Flags = S.Flags;

    // Dump section data.
    if (S.FileOffsetToRawData) {
      DataRefImpl SectionDRI;
      SectionDRI.p = reinterpret_cast<uintptr_t>(&S);
      Expected<ArrayRef<uint8_t>> SecDataRefOrErr =
          Obj.getSectionContents(SectionDRI);
      if (!SecDataRefOrErr)
        return SecDataRefOrErr.takeError();
      YamlSec.SectionData = SecDataRefOrErr.get();
    }

    // Dump relocations.
    if (S.NumberOfRelocations) {
      auto RelRefOrErr = Obj.relocations<Shdr, Reloc>(S);
      if (!RelRefOrErr)
        return RelRefOrErr.takeError();
      for (const Reloc &R : RelRefOrErr.get()) {
        XCOFFYAML::Relocation YamlRel;
        YamlRel.Type = R.Type;
        YamlRel.Info = R.Info;
        YamlRel.SymbolIndex = R.SymbolIndex;
        YamlRel.VirtualAddress = R.VirtualAddress;
        YamlSec.Relocations.push_back(YamlRel);
      }
    }
    YamlSections.push_back(YamlSec);
  }
  return Error::success();
}

Error XCOFFDumper::dumpFileAuxSym(XCOFFYAML::Symbol &Sym,
                                  const XCOFFSymbolRef &SymbolEntRef) {
  for (uint8_t I = 1; I <= Sym.NumberOfAuxEntries; ++I) {
    uintptr_t AuxAddress = XCOFFObjectFile::getAdvancedSymbolEntryAddress(
        SymbolEntRef.getEntryAddress(), I);
    const XCOFFFileAuxEnt *FileAuxEntPtr =
        getAuxEntPtr<XCOFFFileAuxEnt>(AuxAddress);
    auto FileNameOrError = Obj.getCFileName(FileAuxEntPtr);
    if (!FileNameOrError)
      return FileNameOrError.takeError();

    XCOFFYAML::FileAuxEnt FileAuxSym;
    FileAuxSym.FileNameOrString = FileNameOrError.get();
    FileAuxSym.FileStringType = FileAuxEntPtr->Type;
    Sym.AuxEntries.push_back(
        std::make_unique<XCOFFYAML::FileAuxEnt>(FileAuxSym));
  }
  return Error::success();
}

Error XCOFFDumper::dumpStatAuxSym(XCOFFYAML::Symbol &Sym,
                                  const XCOFFSymbolRef &SymbolEntRef) {
  if (Sym.NumberOfAuxEntries != 1) {
    uint32_t SymbolIndex = Obj.getSymbolIndex(SymbolEntRef.getEntryAddress());
    return createError("failed to parse symbol \"" + Sym.SymbolName +
                       "\" with index of " + Twine(SymbolIndex) +
                       ": expected 1 aux symbol for C_STAT, while got " +
                       Twine(static_cast<uint32_t>(*Sym.NumberOfAuxEntries)));
  }

  const XCOFFSectAuxEntForStat *AuxEntPtr =
      getAuxEntPtr<XCOFFSectAuxEntForStat>(
          XCOFFObjectFile::getAdvancedSymbolEntryAddress(
              SymbolEntRef.getEntryAddress(), 1));
  XCOFFYAML::SectAuxEntForStat StatAuxSym;
  StatAuxSym.SectionLength = AuxEntPtr->SectionLength;
  StatAuxSym.NumberOfLineNum = AuxEntPtr->NumberOfLineNum;
  StatAuxSym.NumberOfRelocEnt = AuxEntPtr->NumberOfRelocEnt;
  Sym.AuxEntries.push_back(
      std::make_unique<XCOFFYAML::SectAuxEntForStat>(StatAuxSym));
  return Error::success();
}

void XCOFFDumper::dumpFuncAuxSym(XCOFFYAML::Symbol &Sym,
                                 const uintptr_t AuxAddress) {
  XCOFFYAML::FunctionAuxEnt FunAuxSym;

  if (Obj.is64Bit()) {
    const XCOFFFunctionAuxEnt64 *AuxEntPtr =
        getAuxEntPtr<XCOFFFunctionAuxEnt64>(AuxAddress);
    FunAuxSym.PtrToLineNum = AuxEntPtr->PtrToLineNum;
    FunAuxSym.SizeOfFunction = AuxEntPtr->SizeOfFunction;
    FunAuxSym.SymIdxOfNextBeyond = AuxEntPtr->SymIdxOfNextBeyond;
  } else {
    const XCOFFFunctionAuxEnt32 *AuxEntPtr =
        getAuxEntPtr<XCOFFFunctionAuxEnt32>(AuxAddress);
    FunAuxSym.OffsetToExceptionTbl = AuxEntPtr->OffsetToExceptionTbl;
    FunAuxSym.PtrToLineNum = AuxEntPtr->PtrToLineNum;
    FunAuxSym.SizeOfFunction = AuxEntPtr->SizeOfFunction;
    FunAuxSym.SymIdxOfNextBeyond = AuxEntPtr->SymIdxOfNextBeyond;
  }

  Sym.AuxEntries.push_back(
      std::make_unique<XCOFFYAML::FunctionAuxEnt>(FunAuxSym));
}

void XCOFFDumper::dumpExpAuxSym(XCOFFYAML::Symbol &Sym,
                                const uintptr_t AuxAddress) {
  const XCOFFExceptionAuxEnt *AuxEntPtr =
      getAuxEntPtr<XCOFFExceptionAuxEnt>(AuxAddress);
  XCOFFYAML::ExcpetionAuxEnt ExceptAuxSym;
  ExceptAuxSym.OffsetToExceptionTbl = AuxEntPtr->OffsetToExceptionTbl;
  ExceptAuxSym.SizeOfFunction = AuxEntPtr->SizeOfFunction;
  ExceptAuxSym.SymIdxOfNextBeyond = AuxEntPtr->SymIdxOfNextBeyond;
  Sym.AuxEntries.push_back(
      std::make_unique<XCOFFYAML::ExcpetionAuxEnt>(ExceptAuxSym));
}

void XCOFFDumper::dumpCsectAuxSym(XCOFFYAML::Symbol &Sym,
                                  const object::XCOFFCsectAuxRef &AuxEntPtr) {
  XCOFFYAML::CsectAuxEnt CsectAuxSym;
  CsectAuxSym.ParameterHashIndex = AuxEntPtr.getParameterHashIndex();
  CsectAuxSym.TypeChkSectNum = AuxEntPtr.getTypeChkSectNum();
  CsectAuxSym.SymbolAlignmentAndType = AuxEntPtr.getSymbolAlignmentAndType();
  CsectAuxSym.StorageMappingClass = AuxEntPtr.getStorageMappingClass();

  if (Obj.is64Bit()) {
    CsectAuxSym.SectionOrLengthLo =
        static_cast<uint32_t>(AuxEntPtr.getSectionOrLength64());
    CsectAuxSym.SectionOrLengthHi =
        static_cast<uint32_t>(AuxEntPtr.getSectionOrLength64() >> 32);
  } else {
    CsectAuxSym.SectionOrLength = AuxEntPtr.getSectionOrLength32();
    CsectAuxSym.StabInfoIndex = AuxEntPtr.getStabInfoIndex32();
    CsectAuxSym.StabSectNum = AuxEntPtr.getStabSectNum32();
  }

  Sym.AuxEntries.push_back(
      std::make_unique<XCOFFYAML::CsectAuxEnt>(CsectAuxSym));
}

Error XCOFFDumper::dumpAuxSyms(XCOFFYAML::Symbol &Sym,
                               const XCOFFSymbolRef &SymbolEntRef) {
  auto ErrOrCsectAuxRef = SymbolEntRef.getXCOFFCsectAuxRef();
  if (!ErrOrCsectAuxRef)
    return ErrOrCsectAuxRef.takeError();
  XCOFFCsectAuxRef CsectAuxRef = ErrOrCsectAuxRef.get();

  for (uint8_t I = 1; I <= Sym.NumberOfAuxEntries; ++I) {

    if (I == Sym.NumberOfAuxEntries && !Obj.is64Bit()) {
      dumpCsectAuxSym(Sym, CsectAuxRef);
      return Error::success();
    }

    uintptr_t AuxAddress = XCOFFObjectFile::getAdvancedSymbolEntryAddress(
        SymbolEntRef.getEntryAddress(), I);

    if (Obj.is64Bit()) {
      XCOFF::SymbolAuxType Type = *Obj.getSymbolAuxType(AuxAddress);
      if (Type == XCOFF::SymbolAuxType::AUX_CSECT)
        dumpCsectAuxSym(Sym, CsectAuxRef);
      else if (Type == XCOFF::SymbolAuxType::AUX_FCN)
        dumpFuncAuxSym(Sym, AuxAddress);
      else if (Type == XCOFF::SymbolAuxType::AUX_EXCEPT)
        dumpExpAuxSym(Sym, AuxAddress);
      else {
        uint32_t SymbolIndex =
            Obj.getSymbolIndex(SymbolEntRef.getEntryAddress());
        return createError("failed to parse symbol \"" + Sym.SymbolName +
                           "\" with index of " + Twine(SymbolIndex) +
                           ": invalid auxiliary symbol type: " +
                           Twine(static_cast<uint32_t>(Type)));
      }

    } else
      dumpFuncAuxSym(Sym, AuxAddress);
  }

  return Error::success();
}

Error XCOFFDumper::dumpBlockAuxSym(XCOFFYAML::Symbol &Sym,
                                   const XCOFFSymbolRef &SymbolEntRef) {
  if (Sym.NumberOfAuxEntries != 1) {
    uint32_t SymbolIndex = Obj.getSymbolIndex(SymbolEntRef.getEntryAddress());
    return createError(
        "failed to parse symbol \"" + Sym.SymbolName + "\" with index of " +
        Twine(SymbolIndex) +
        ": expected 1 aux symbol for C_BLOCK or C_FCN, while got " +
        Twine(static_cast<uint32_t>(*Sym.NumberOfAuxEntries)));
  }

  uintptr_t AuxAddress = XCOFFObjectFile::getAdvancedSymbolEntryAddress(
      SymbolEntRef.getEntryAddress(), 1);
  XCOFFYAML::BlockAuxEnt BlockAuxSym;

  if (Obj.is64Bit()) {
    const XCOFFBlockAuxEnt64 *AuxEntPtr =
        getAuxEntPtr<XCOFFBlockAuxEnt64>(AuxAddress);
    BlockAuxSym.LineNum = AuxEntPtr->LineNum;
  } else {
    const XCOFFBlockAuxEnt32 *AuxEntPtr =
        getAuxEntPtr<XCOFFBlockAuxEnt32>(AuxAddress);
    BlockAuxSym.LineNumLo = AuxEntPtr->LineNumLo;
    BlockAuxSym.LineNumHi = AuxEntPtr->LineNumHi;
  }

  Sym.AuxEntries.push_back(
      std::make_unique<XCOFFYAML::BlockAuxEnt>(BlockAuxSym));
  return Error::success();
}

Error XCOFFDumper::dumpDwarfAuxSym(XCOFFYAML::Symbol &Sym,
                                   const XCOFFSymbolRef &SymbolEntRef) {
  if (Sym.NumberOfAuxEntries != 1) {
    uint32_t SymbolIndex = Obj.getSymbolIndex(SymbolEntRef.getEntryAddress());
    return createError("failed to parse symbol \"" + Sym.SymbolName +
                       "\" with index of " + Twine(SymbolIndex) +
                       ": expected 1 aux symbol for C_DWARF, while got " +
                       Twine(static_cast<uint32_t>(*Sym.NumberOfAuxEntries)));
  }

  uintptr_t AuxAddress = XCOFFObjectFile::getAdvancedSymbolEntryAddress(
      SymbolEntRef.getEntryAddress(), 1);
  XCOFFYAML::SectAuxEntForDWARF DwarfAuxSym;

  if (Obj.is64Bit()) {
    const XCOFFSectAuxEntForDWARF64 *AuxEntPtr =
        getAuxEntPtr<XCOFFSectAuxEntForDWARF64>(AuxAddress);
    DwarfAuxSym.LengthOfSectionPortion = AuxEntPtr->LengthOfSectionPortion;
    DwarfAuxSym.NumberOfRelocEnt = AuxEntPtr->NumberOfRelocEnt;
  } else {
    const XCOFFSectAuxEntForDWARF32 *AuxEntPtr =
        getAuxEntPtr<XCOFFSectAuxEntForDWARF32>(AuxAddress);
    DwarfAuxSym.LengthOfSectionPortion = AuxEntPtr->LengthOfSectionPortion;
    DwarfAuxSym.NumberOfRelocEnt = AuxEntPtr->NumberOfRelocEnt;
  }

  Sym.AuxEntries.push_back(
      std::make_unique<XCOFFYAML::SectAuxEntForDWARF>(DwarfAuxSym));
  return Error::success();
}

Error XCOFFDumper::dumpSymbols() {
  std::vector<XCOFFYAML::Symbol> &Symbols = YAMLObj.Symbols;

  for (const SymbolRef &S : Obj.symbols()) {
    DataRefImpl SymbolDRI = S.getRawDataRefImpl();
    const XCOFFSymbolRef SymbolEntRef = Obj.toSymbolRef(SymbolDRI);
    XCOFFYAML::Symbol Sym;

    Expected<StringRef> SymNameRefOrErr = Obj.getSymbolName(SymbolDRI);
    if (!SymNameRefOrErr) {
      return SymNameRefOrErr.takeError();
    }
    Sym.SymbolName = SymNameRefOrErr.get();

    Sym.Value = SymbolEntRef.getValue();

    Expected<StringRef> SectionNameRefOrErr =
        Obj.getSymbolSectionName(SymbolEntRef);
    if (!SectionNameRefOrErr)
      return SectionNameRefOrErr.takeError();

    Sym.SectionName = SectionNameRefOrErr.get();

    Sym.Type = SymbolEntRef.getSymbolType();
    Sym.StorageClass = SymbolEntRef.getStorageClass();
    Sym.NumberOfAuxEntries = SymbolEntRef.getNumberOfAuxEntries();

    if (Sym.NumberOfAuxEntries) {
      switch (Sym.StorageClass) {
      case XCOFF::C_FILE:
        if (Error E = dumpFileAuxSym(Sym, SymbolEntRef))
          return E;
        break;
      case XCOFF::C_STAT:
        if (Error E = dumpStatAuxSym(Sym, SymbolEntRef))
          return E;
        break;
      case XCOFF::C_EXT:
      case XCOFF::C_WEAKEXT:
      case XCOFF::C_HIDEXT:
        if (Error E = dumpAuxSyms(Sym, SymbolEntRef))
          return E;
        break;
      case XCOFF::C_BLOCK:
      case XCOFF::C_FCN:
        if (Error E = dumpBlockAuxSym(Sym, SymbolEntRef))
          return E;
        break;
      case XCOFF::C_DWARF:
        if (Error E = dumpDwarfAuxSym(Sym, SymbolEntRef))
          return E;
        break;
      default:
        break;
      }
    }

    Symbols.push_back(std::move(Sym));
  }

  return Error::success();
}

Error xcoff2yaml(raw_ostream &Out, const object::XCOFFObjectFile &Obj) {
  XCOFFDumper Dumper(Obj);

  if (Error E = Dumper.dump())
    return E;

  yaml::Output Yout(Out);
  Yout << Dumper.getYAMLObj();

  return Error::success();
}