Coverage Report

Created: 2025-12-31 06:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/perfetto/buildtools/android-unwinding/libunwindstack/ElfInterfaceArm.cpp
Line
Count
Source
1
/*
2
 * Copyright (C) 2016 The Android Open Source Project
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
17
#include <elf.h>
18
#include <stdint.h>
19
20
#include <unwindstack/MachineArm.h>
21
#include <unwindstack/Memory.h>
22
#include <unwindstack/RegsArm.h>
23
24
#include "ArmExidx.h"
25
#include "ElfInterfaceArm.h"
26
27
namespace unwindstack {
28
29
0
bool ElfInterfaceArm::Init(int64_t* load_bias) {
30
0
  if (!ElfInterface32::Init(load_bias)) {
31
0
    return false;
32
0
  }
33
0
  load_bias_ = *load_bias;
34
0
  return true;
35
0
}
36
37
0
bool ElfInterfaceArm::FindEntry(uint32_t pc, uint64_t* entry_offset) {
38
0
  if (start_offset_ == 0 || total_entries_ == 0) {
39
0
    last_error_.code = ERROR_UNWIND_INFO;
40
0
    return false;
41
0
  }
42
43
0
  size_t first = 0;
44
0
  size_t last = total_entries_;
45
0
  while (first < last) {
46
0
    size_t current = (first + last) / 2;
47
0
    uint32_t addr = addrs_[current];
48
0
    if (addr == 0) {
49
0
      if (!GetPrel31Addr(start_offset_ + current * 8, &addr)) {
50
0
        return false;
51
0
      }
52
0
      addrs_[current] = addr;
53
0
    }
54
0
    if (pc == addr) {
55
0
      *entry_offset = start_offset_ + current * 8;
56
0
      return true;
57
0
    }
58
0
    if (pc < addr) {
59
0
      last = current;
60
0
    } else {
61
0
      first = current + 1;
62
0
    }
63
0
  }
64
0
  if (last != 0) {
65
0
    *entry_offset = start_offset_ + (last - 1) * 8;
66
0
    return true;
67
0
  }
68
0
  last_error_.code = ERROR_UNWIND_INFO;
69
0
  return false;
70
0
}
71
72
0
bool ElfInterfaceArm::GetPrel31Addr(uint32_t offset, uint32_t* addr) {
73
0
  uint32_t data;
74
0
  if (!memory_->Read32(offset, &data)) {
75
0
    last_error_.code = ERROR_MEMORY_INVALID;
76
0
    last_error_.address = offset;
77
0
    return false;
78
0
  }
79
80
  // Sign extend the value if necessary.
81
0
  int32_t value = (static_cast<int32_t>(data) << 1) >> 1;
82
0
  *addr = offset + value;
83
0
  return true;
84
0
}
85
86
#if !defined(PT_ARM_EXIDX)
87
#define PT_ARM_EXIDX 0x70000001
88
#endif
89
90
0
void ElfInterfaceArm::HandleUnknownType(uint32_t type, uint64_t ph_offset, uint64_t ph_filesz) {
91
0
  if (type != PT_ARM_EXIDX) {
92
0
    return;
93
0
  }
94
95
  // The offset already takes into account the load bias.
96
0
  start_offset_ = ph_offset;
97
98
  // Always use filesz instead of memsz. In most cases they are the same,
99
  // but some shared libraries wind up setting one correctly and not the other.
100
0
  total_entries_ = ph_filesz / 8;
101
0
}
102
103
bool ElfInterfaceArm::Step(uint64_t pc, Regs* regs, Memory* process_memory, bool* finished,
104
0
                           bool* is_signal_frame) {
105
  // Dwarf unwind information is precise about whether a pc is covered or not,
106
  // but arm unwind information only has ranges of pc. In order to avoid
107
  // incorrectly doing a bad unwind using arm unwind information for a
108
  // different function, always try and unwind with the dwarf information first.
109
0
  return ElfInterface32::Step(pc, regs, process_memory, finished, is_signal_frame) ||
110
0
         StepExidx(pc, regs, process_memory, finished);
111
0
}
112
113
0
bool ElfInterfaceArm::StepExidx(uint64_t pc, Regs* regs, Memory* process_memory, bool* finished) {
114
  // Adjust the load bias to get the real relative pc.
115
0
  if (pc < load_bias_) {
116
0
    last_error_.code = ERROR_UNWIND_INFO;
117
0
    return false;
118
0
  }
119
0
  pc -= load_bias_;
120
121
0
  RegsArm* regs_arm = reinterpret_cast<RegsArm*>(regs);
122
0
  uint64_t entry_offset;
123
0
  if (!FindEntry(pc, &entry_offset)) {
124
0
    return false;
125
0
  }
126
127
0
  ArmExidx arm(regs_arm, memory_.get(), process_memory);
128
0
  arm.set_cfa(regs_arm->sp());
129
0
  bool return_value = false;
130
0
  if (arm.ExtractEntryData(entry_offset) && arm.Eval()) {
131
    // If the pc was not set, then use the LR registers for the PC.
132
0
    if (!arm.pc_set()) {
133
0
      (*regs_arm)[ARM_REG_PC] = (*regs_arm)[ARM_REG_LR];
134
0
    }
135
0
    (*regs_arm)[ARM_REG_SP] = arm.cfa();
136
0
    return_value = true;
137
138
    // If the pc was set to zero, consider this the final frame.
139
0
    *finished = (regs_arm->pc() == 0) ? true : false;
140
0
  }
141
142
0
  if (arm.status() == ARM_STATUS_NO_UNWIND) {
143
0
    *finished = true;
144
0
    return true;
145
0
  }
146
147
0
  if (!return_value) {
148
0
    switch (arm.status()) {
149
0
      case ARM_STATUS_NONE:
150
0
      case ARM_STATUS_NO_UNWIND:
151
0
      case ARM_STATUS_FINISH:
152
0
        last_error_.code = ERROR_NONE;
153
0
        break;
154
155
0
      case ARM_STATUS_RESERVED:
156
0
      case ARM_STATUS_SPARE:
157
0
      case ARM_STATUS_TRUNCATED:
158
0
      case ARM_STATUS_MALFORMED:
159
0
      case ARM_STATUS_INVALID_ALIGNMENT:
160
0
      case ARM_STATUS_INVALID_PERSONALITY:
161
0
        last_error_.code = ERROR_UNWIND_INFO;
162
0
        break;
163
164
0
      case ARM_STATUS_READ_FAILED:
165
0
        last_error_.code = ERROR_MEMORY_INVALID;
166
0
        last_error_.address = arm.status_address();
167
0
        break;
168
0
    }
169
0
  }
170
0
  return return_value;
171
0
}
172
173
0
bool ElfInterfaceArm::GetFunctionName(uint64_t addr, SharedString* name, uint64_t* offset) {
174
  // For ARM, thumb function symbols have bit 0 set, but the address passed
175
  // in here might not have this bit set and result in a failure to find
176
  // the thumb function names. Adjust the address and offset to account
177
  // for this possible case.
178
0
  if (ElfInterface32::GetFunctionName(addr | 1, name, offset)) {
179
0
    *offset &= ~1;
180
0
    return true;
181
0
  }
182
0
  return false;
183
0
}
184
185
}  // namespace unwindstack