aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/llvm14/lib/DebugInfo/DWARF/DWARFDataExtractor.cpp
blob: da6f6ad903f42ede980d5eb106879a993b9098ce (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
//===- DWARFDataExtractor.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 "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
#include "llvm/DebugInfo/DWARF/DWARFContext.h"

using namespace llvm;

std::pair<uint64_t, dwarf::DwarfFormat>
DWARFDataExtractor::getInitialLength(uint64_t *Off, Error *Err) const {
  ErrorAsOutParameter ErrAsOut(Err);
  if (Err && *Err)
    return {0, dwarf::DWARF32};

  Cursor C(*Off);
  uint64_t Length = getRelocatedValue(C, 4);
  dwarf::DwarfFormat Format = dwarf::DWARF32;
  if (Length == dwarf::DW_LENGTH_DWARF64) {
    Length = getRelocatedValue(C, 8);
    Format = dwarf::DWARF64;
  } else if (Length >= dwarf::DW_LENGTH_lo_reserved) {
    cantFail(C.takeError());
    if (Err)
      *Err = createStringError(
          errc::invalid_argument,
          "unsupported reserved unit length of value 0x%8.8" PRIx64, Length);
    return {0, dwarf::DWARF32};
  }

  if (C) {
    *Off = C.tell();
    return {Length, Format};
  }
  if (Err)
    *Err = C.takeError();
  else
    consumeError(C.takeError());
  return {0, dwarf::DWARF32};
}

uint64_t DWARFDataExtractor::getRelocatedValue(uint32_t Size, uint64_t *Off,
                                               uint64_t *SecNdx,
                                               Error *Err) const {
  if (SecNdx)
    *SecNdx = object::SectionedAddress::UndefSection;
  if (!Section)
    return getUnsigned(Off, Size, Err);

  ErrorAsOutParameter ErrAsOut(Err);
  Optional<RelocAddrEntry> E = Obj->find(*Section, *Off);
  uint64_t LocData = getUnsigned(Off, Size, Err);
  if (!E || (Err && *Err))
    return LocData;
  if (SecNdx)
    *SecNdx = E->SectionIndex;

  uint64_t R =
      object::resolveRelocation(E->Resolver, E->Reloc, E->SymbolValue, LocData);
  if (E->Reloc2)
    R = object::resolveRelocation(E->Resolver, *E->Reloc2, E->SymbolValue2, R);
  return R;
}

Optional<uint64_t>
DWARFDataExtractor::getEncodedPointer(uint64_t *Offset, uint8_t Encoding,
                                      uint64_t PCRelOffset) const {
  if (Encoding == dwarf::DW_EH_PE_omit)
    return None;

  uint64_t Result = 0;
  uint64_t OldOffset = *Offset;
  // First get value
  switch (Encoding & 0x0F) {
  case dwarf::DW_EH_PE_absptr:
    switch (getAddressSize()) {
    case 2:
    case 4:
    case 8:
      Result = getUnsigned(Offset, getAddressSize());
      break;
    default:
      return None;
    }
    break;
  case dwarf::DW_EH_PE_uleb128:
    Result = getULEB128(Offset);
    break;
  case dwarf::DW_EH_PE_sleb128:
    Result = getSLEB128(Offset);
    break;
  case dwarf::DW_EH_PE_udata2:
    Result = getUnsigned(Offset, 2);
    break;
  case dwarf::DW_EH_PE_udata4:
    Result = getUnsigned(Offset, 4);
    break;
  case dwarf::DW_EH_PE_udata8:
    Result = getUnsigned(Offset, 8);
    break;
  case dwarf::DW_EH_PE_sdata2:
    Result = getSigned(Offset, 2);
    break;
  case dwarf::DW_EH_PE_sdata4:
    Result = SignExtend64<32>(getRelocatedValue(4, Offset));
    break;
  case dwarf::DW_EH_PE_sdata8:
    Result = getRelocatedValue(8, Offset);
    break;
  default:
    return None;
  }
  // Then add relative offset, if required
  switch (Encoding & 0x70) {
  case dwarf::DW_EH_PE_absptr:
    // do nothing
    break;
  case dwarf::DW_EH_PE_pcrel:
    Result += PCRelOffset;
    break;
  case dwarf::DW_EH_PE_datarel:
  case dwarf::DW_EH_PE_textrel:
  case dwarf::DW_EH_PE_funcrel:
  case dwarf::DW_EH_PE_aligned:
  default:
    *Offset = OldOffset;
    return None;
  }

  return Result;
}