xref: /aosp_15_r20/art/compiler/debug/elf_debug_loc_writer.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2016 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #ifndef ART_COMPILER_DEBUG_ELF_DEBUG_LOC_WRITER_H_
18*795d594fSAndroid Build Coastguard Worker #define ART_COMPILER_DEBUG_ELF_DEBUG_LOC_WRITER_H_
19*795d594fSAndroid Build Coastguard Worker 
20*795d594fSAndroid Build Coastguard Worker #include <cstring>
21*795d594fSAndroid Build Coastguard Worker #include <map>
22*795d594fSAndroid Build Coastguard Worker 
23*795d594fSAndroid Build Coastguard Worker #include "arch/instruction_set.h"
24*795d594fSAndroid Build Coastguard Worker #include "base/macros.h"
25*795d594fSAndroid Build Coastguard Worker #include "debug/method_debug_info.h"
26*795d594fSAndroid Build Coastguard Worker #include "dwarf/debug_info_entry_writer.h"
27*795d594fSAndroid Build Coastguard Worker #include "dwarf/register.h"
28*795d594fSAndroid Build Coastguard Worker #include "oat/stack_map.h"
29*795d594fSAndroid Build Coastguard Worker 
30*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
31*795d594fSAndroid Build Coastguard Worker namespace debug {
32*795d594fSAndroid Build Coastguard Worker using Reg = dwarf::Reg;
33*795d594fSAndroid Build Coastguard Worker 
GetDwarfCoreReg(InstructionSet isa,int machine_reg)34*795d594fSAndroid Build Coastguard Worker static Reg GetDwarfCoreReg(InstructionSet isa, int machine_reg) {
35*795d594fSAndroid Build Coastguard Worker   switch (isa) {
36*795d594fSAndroid Build Coastguard Worker     case InstructionSet::kArm:
37*795d594fSAndroid Build Coastguard Worker     case InstructionSet::kThumb2:
38*795d594fSAndroid Build Coastguard Worker       return Reg::ArmCore(machine_reg);
39*795d594fSAndroid Build Coastguard Worker     case InstructionSet::kArm64:
40*795d594fSAndroid Build Coastguard Worker       return Reg::Arm64Core(machine_reg);
41*795d594fSAndroid Build Coastguard Worker     case InstructionSet::kRiscv64:
42*795d594fSAndroid Build Coastguard Worker       return Reg::Riscv64Core(machine_reg);
43*795d594fSAndroid Build Coastguard Worker     case InstructionSet::kX86:
44*795d594fSAndroid Build Coastguard Worker       return Reg::X86Core(machine_reg);
45*795d594fSAndroid Build Coastguard Worker     case InstructionSet::kX86_64:
46*795d594fSAndroid Build Coastguard Worker       return Reg::X86_64Core(machine_reg);
47*795d594fSAndroid Build Coastguard Worker     case InstructionSet::kNone:
48*795d594fSAndroid Build Coastguard Worker       LOG(FATAL) << "No instruction set";
49*795d594fSAndroid Build Coastguard Worker   }
50*795d594fSAndroid Build Coastguard Worker   UNREACHABLE();
51*795d594fSAndroid Build Coastguard Worker }
52*795d594fSAndroid Build Coastguard Worker 
GetDwarfFpReg(InstructionSet isa,int machine_reg)53*795d594fSAndroid Build Coastguard Worker static Reg GetDwarfFpReg(InstructionSet isa, int machine_reg) {
54*795d594fSAndroid Build Coastguard Worker   switch (isa) {
55*795d594fSAndroid Build Coastguard Worker     case InstructionSet::kArm:
56*795d594fSAndroid Build Coastguard Worker     case InstructionSet::kThumb2:
57*795d594fSAndroid Build Coastguard Worker       return Reg::ArmFp(machine_reg);
58*795d594fSAndroid Build Coastguard Worker     case InstructionSet::kArm64:
59*795d594fSAndroid Build Coastguard Worker       return Reg::Arm64Fp(machine_reg);
60*795d594fSAndroid Build Coastguard Worker     case InstructionSet::kRiscv64:
61*795d594fSAndroid Build Coastguard Worker       return Reg::Riscv64Fp(machine_reg);
62*795d594fSAndroid Build Coastguard Worker     case InstructionSet::kX86:
63*795d594fSAndroid Build Coastguard Worker       return Reg::X86Fp(machine_reg);
64*795d594fSAndroid Build Coastguard Worker     case InstructionSet::kX86_64:
65*795d594fSAndroid Build Coastguard Worker       return Reg::X86_64Fp(machine_reg);
66*795d594fSAndroid Build Coastguard Worker     case InstructionSet::kNone:
67*795d594fSAndroid Build Coastguard Worker       LOG(FATAL) << "No instruction set";
68*795d594fSAndroid Build Coastguard Worker   }
69*795d594fSAndroid Build Coastguard Worker   UNREACHABLE();
70*795d594fSAndroid Build Coastguard Worker }
71*795d594fSAndroid Build Coastguard Worker 
72*795d594fSAndroid Build Coastguard Worker struct VariableLocation {
73*795d594fSAndroid Build Coastguard Worker   uint32_t low_pc;  // Relative to compilation unit.
74*795d594fSAndroid Build Coastguard Worker   uint32_t high_pc;  // Relative to compilation unit.
75*795d594fSAndroid Build Coastguard Worker   DexRegisterLocation reg_lo;  // May be None if the location is unknown.
76*795d594fSAndroid Build Coastguard Worker   DexRegisterLocation reg_hi;  // Most significant bits of 64-bit value.
77*795d594fSAndroid Build Coastguard Worker };
78*795d594fSAndroid Build Coastguard Worker 
79*795d594fSAndroid Build Coastguard Worker // Get the location of given dex register (e.g. stack or machine register).
80*795d594fSAndroid Build Coastguard Worker // Note that the location might be different based on the current pc.
81*795d594fSAndroid Build Coastguard Worker // The result will cover all ranges where the variable is in scope.
82*795d594fSAndroid Build Coastguard Worker // PCs corresponding to stackmap with dex register map are accurate,
83*795d594fSAndroid Build Coastguard Worker // all other PCs are best-effort only.
GetVariableLocations(const MethodDebugInfo * method_info,const std::vector<DexRegisterMap> & dex_register_maps,uint16_t vreg,bool is64bitValue,uint64_t compilation_unit_code_address,uint32_t dex_pc_low,uint32_t dex_pc_high,InstructionSet isa)84*795d594fSAndroid Build Coastguard Worker static std::vector<VariableLocation> GetVariableLocations(
85*795d594fSAndroid Build Coastguard Worker     const MethodDebugInfo* method_info,
86*795d594fSAndroid Build Coastguard Worker     const std::vector<DexRegisterMap>& dex_register_maps,
87*795d594fSAndroid Build Coastguard Worker     uint16_t vreg,
88*795d594fSAndroid Build Coastguard Worker     bool is64bitValue,
89*795d594fSAndroid Build Coastguard Worker     uint64_t compilation_unit_code_address,
90*795d594fSAndroid Build Coastguard Worker     uint32_t dex_pc_low,
91*795d594fSAndroid Build Coastguard Worker     uint32_t dex_pc_high,
92*795d594fSAndroid Build Coastguard Worker     InstructionSet isa) {
93*795d594fSAndroid Build Coastguard Worker   std::vector<VariableLocation> variable_locations;
94*795d594fSAndroid Build Coastguard Worker 
95*795d594fSAndroid Build Coastguard Worker   // Get stack maps sorted by pc (they might not be sorted internally).
96*795d594fSAndroid Build Coastguard Worker   // TODO(dsrbecky) Remove this once stackmaps get sorted by pc.
97*795d594fSAndroid Build Coastguard Worker   const CodeInfo code_info(method_info->code_info);
98*795d594fSAndroid Build Coastguard Worker   std::map<uint32_t, uint32_t> stack_maps;  // low_pc -> stack_map_index.
99*795d594fSAndroid Build Coastguard Worker   for (uint32_t s = 0; s < code_info.GetNumberOfStackMaps(); s++) {
100*795d594fSAndroid Build Coastguard Worker     StackMap stack_map = code_info.GetStackMapAt(s);
101*795d594fSAndroid Build Coastguard Worker     DCHECK(stack_map.IsValid());
102*795d594fSAndroid Build Coastguard Worker     if (!stack_map.HasDexRegisterMap()) {
103*795d594fSAndroid Build Coastguard Worker       // The compiler creates stackmaps without register maps at the start of
104*795d594fSAndroid Build Coastguard Worker       // basic blocks in order to keep instruction-accurate line number mapping.
105*795d594fSAndroid Build Coastguard Worker       // However, we never stop at those (breakpoint locations always have map).
106*795d594fSAndroid Build Coastguard Worker       // Therefore, for the purpose of local variables, we ignore them.
107*795d594fSAndroid Build Coastguard Worker       // The main reason for this is to save space by avoiding undefined gaps.
108*795d594fSAndroid Build Coastguard Worker       continue;
109*795d594fSAndroid Build Coastguard Worker     }
110*795d594fSAndroid Build Coastguard Worker     const uint32_t pc_offset = stack_map.GetNativePcOffset(isa);
111*795d594fSAndroid Build Coastguard Worker     DCHECK_LE(pc_offset, method_info->code_size);
112*795d594fSAndroid Build Coastguard Worker     DCHECK_LE(compilation_unit_code_address, method_info->code_address);
113*795d594fSAndroid Build Coastguard Worker     const uint32_t low_pc = dchecked_integral_cast<uint32_t>(
114*795d594fSAndroid Build Coastguard Worker         method_info->code_address + pc_offset - compilation_unit_code_address);
115*795d594fSAndroid Build Coastguard Worker     stack_maps.emplace(low_pc, s);
116*795d594fSAndroid Build Coastguard Worker   }
117*795d594fSAndroid Build Coastguard Worker 
118*795d594fSAndroid Build Coastguard Worker   // Create entries for the requested register based on stack map data.
119*795d594fSAndroid Build Coastguard Worker   for (auto it = stack_maps.begin(); it != stack_maps.end(); it++) {
120*795d594fSAndroid Build Coastguard Worker     const uint32_t low_pc = it->first;
121*795d594fSAndroid Build Coastguard Worker     const uint32_t stack_map_index = it->second;
122*795d594fSAndroid Build Coastguard Worker     const StackMap stack_map = code_info.GetStackMapAt(stack_map_index);
123*795d594fSAndroid Build Coastguard Worker     auto next_it = it;
124*795d594fSAndroid Build Coastguard Worker     next_it++;
125*795d594fSAndroid Build Coastguard Worker     const uint32_t high_pc = next_it != stack_maps.end()
126*795d594fSAndroid Build Coastguard Worker       ? next_it->first
127*795d594fSAndroid Build Coastguard Worker       : method_info->code_address + method_info->code_size - compilation_unit_code_address;
128*795d594fSAndroid Build Coastguard Worker     DCHECK_LE(low_pc, high_pc);
129*795d594fSAndroid Build Coastguard Worker     if (low_pc == high_pc) {
130*795d594fSAndroid Build Coastguard Worker       continue;  // Ignore if the address range is empty.
131*795d594fSAndroid Build Coastguard Worker     }
132*795d594fSAndroid Build Coastguard Worker 
133*795d594fSAndroid Build Coastguard Worker     // Check that the stack map is in the requested range.
134*795d594fSAndroid Build Coastguard Worker     uint32_t dex_pc = stack_map.GetDexPc();
135*795d594fSAndroid Build Coastguard Worker     if (!(dex_pc_low <= dex_pc && dex_pc < dex_pc_high)) {
136*795d594fSAndroid Build Coastguard Worker       // The variable is not in scope at this PC. Therefore omit the entry.
137*795d594fSAndroid Build Coastguard Worker       // Note that this is different to None() entry which means in scope, but unknown location.
138*795d594fSAndroid Build Coastguard Worker       continue;
139*795d594fSAndroid Build Coastguard Worker     }
140*795d594fSAndroid Build Coastguard Worker 
141*795d594fSAndroid Build Coastguard Worker     // Find the location of the dex register.
142*795d594fSAndroid Build Coastguard Worker     DexRegisterLocation reg_lo = DexRegisterLocation::None();
143*795d594fSAndroid Build Coastguard Worker     DexRegisterLocation reg_hi = DexRegisterLocation::None();
144*795d594fSAndroid Build Coastguard Worker     DCHECK_LT(stack_map_index, dex_register_maps.size());
145*795d594fSAndroid Build Coastguard Worker     DexRegisterMap dex_register_map = dex_register_maps[stack_map_index];
146*795d594fSAndroid Build Coastguard Worker     DCHECK(!dex_register_map.empty());
147*795d594fSAndroid Build Coastguard Worker     CodeItemDataAccessor accessor(*method_info->dex_file, method_info->code_item);
148*795d594fSAndroid Build Coastguard Worker     reg_lo = dex_register_map[vreg];
149*795d594fSAndroid Build Coastguard Worker     if (is64bitValue) {
150*795d594fSAndroid Build Coastguard Worker       reg_hi = dex_register_map[vreg + 1];
151*795d594fSAndroid Build Coastguard Worker     }
152*795d594fSAndroid Build Coastguard Worker 
153*795d594fSAndroid Build Coastguard Worker     // Add location entry for this address range.
154*795d594fSAndroid Build Coastguard Worker     if (!variable_locations.empty() &&
155*795d594fSAndroid Build Coastguard Worker         variable_locations.back().reg_lo == reg_lo &&
156*795d594fSAndroid Build Coastguard Worker         variable_locations.back().reg_hi == reg_hi &&
157*795d594fSAndroid Build Coastguard Worker         variable_locations.back().high_pc == low_pc) {
158*795d594fSAndroid Build Coastguard Worker       // Merge with the previous entry (extend its range).
159*795d594fSAndroid Build Coastguard Worker       variable_locations.back().high_pc = high_pc;
160*795d594fSAndroid Build Coastguard Worker     } else {
161*795d594fSAndroid Build Coastguard Worker       variable_locations.push_back({low_pc, high_pc, reg_lo, reg_hi});
162*795d594fSAndroid Build Coastguard Worker     }
163*795d594fSAndroid Build Coastguard Worker   }
164*795d594fSAndroid Build Coastguard Worker 
165*795d594fSAndroid Build Coastguard Worker   return variable_locations;
166*795d594fSAndroid Build Coastguard Worker }
167*795d594fSAndroid Build Coastguard Worker 
168*795d594fSAndroid Build Coastguard Worker // Write table into .debug_loc which describes location of dex register.
169*795d594fSAndroid Build Coastguard Worker // The dex register might be valid only at some points and it might
170*795d594fSAndroid Build Coastguard Worker // move between machine registers and stack.
WriteDebugLocEntry(const MethodDebugInfo * method_info,const std::vector<DexRegisterMap> & dex_register_maps,uint16_t vreg,bool is64bitValue,uint64_t compilation_unit_code_address,uint32_t dex_pc_low,uint32_t dex_pc_high,InstructionSet isa,dwarf::DebugInfoEntryWriter<> * debug_info,std::vector<uint8_t> * debug_loc_buffer,std::vector<uint8_t> * debug_ranges_buffer)171*795d594fSAndroid Build Coastguard Worker static void WriteDebugLocEntry(const MethodDebugInfo* method_info,
172*795d594fSAndroid Build Coastguard Worker                                const std::vector<DexRegisterMap>& dex_register_maps,
173*795d594fSAndroid Build Coastguard Worker                                uint16_t vreg,
174*795d594fSAndroid Build Coastguard Worker                                bool is64bitValue,
175*795d594fSAndroid Build Coastguard Worker                                uint64_t compilation_unit_code_address,
176*795d594fSAndroid Build Coastguard Worker                                uint32_t dex_pc_low,
177*795d594fSAndroid Build Coastguard Worker                                uint32_t dex_pc_high,
178*795d594fSAndroid Build Coastguard Worker                                InstructionSet isa,
179*795d594fSAndroid Build Coastguard Worker                                dwarf::DebugInfoEntryWriter<>* debug_info,
180*795d594fSAndroid Build Coastguard Worker                                std::vector<uint8_t>* debug_loc_buffer,
181*795d594fSAndroid Build Coastguard Worker                                std::vector<uint8_t>* debug_ranges_buffer) {
182*795d594fSAndroid Build Coastguard Worker   using Kind = DexRegisterLocation::Kind;
183*795d594fSAndroid Build Coastguard Worker   if (method_info->code_info == nullptr || dex_register_maps.empty()) {
184*795d594fSAndroid Build Coastguard Worker     return;
185*795d594fSAndroid Build Coastguard Worker   }
186*795d594fSAndroid Build Coastguard Worker 
187*795d594fSAndroid Build Coastguard Worker   std::vector<VariableLocation> variable_locations = GetVariableLocations(
188*795d594fSAndroid Build Coastguard Worker       method_info,
189*795d594fSAndroid Build Coastguard Worker       dex_register_maps,
190*795d594fSAndroid Build Coastguard Worker       vreg,
191*795d594fSAndroid Build Coastguard Worker       is64bitValue,
192*795d594fSAndroid Build Coastguard Worker       compilation_unit_code_address,
193*795d594fSAndroid Build Coastguard Worker       dex_pc_low,
194*795d594fSAndroid Build Coastguard Worker       dex_pc_high,
195*795d594fSAndroid Build Coastguard Worker       isa);
196*795d594fSAndroid Build Coastguard Worker 
197*795d594fSAndroid Build Coastguard Worker   // Write .debug_loc entries.
198*795d594fSAndroid Build Coastguard Worker   dwarf::Writer<> debug_loc(debug_loc_buffer);
199*795d594fSAndroid Build Coastguard Worker   const size_t debug_loc_offset = debug_loc.size();
200*795d594fSAndroid Build Coastguard Worker   const bool is64bit = Is64BitInstructionSet(isa);
201*795d594fSAndroid Build Coastguard Worker   std::vector<uint8_t> expr_buffer;
202*795d594fSAndroid Build Coastguard Worker   for (const VariableLocation& variable_location : variable_locations) {
203*795d594fSAndroid Build Coastguard Worker     // Translate dex register location to DWARF expression.
204*795d594fSAndroid Build Coastguard Worker     // Note that 64-bit value might be split to two distinct locations.
205*795d594fSAndroid Build Coastguard Worker     // (for example, two 32-bit machine registers, or even stack and register)
206*795d594fSAndroid Build Coastguard Worker     dwarf::Expression expr(&expr_buffer);
207*795d594fSAndroid Build Coastguard Worker     DexRegisterLocation reg_lo = variable_location.reg_lo;
208*795d594fSAndroid Build Coastguard Worker     DexRegisterLocation reg_hi = variable_location.reg_hi;
209*795d594fSAndroid Build Coastguard Worker     for (int piece = 0; piece < (is64bitValue ? 2 : 1); piece++) {
210*795d594fSAndroid Build Coastguard Worker       DexRegisterLocation reg_loc = (piece == 0 ? reg_lo : reg_hi);
211*795d594fSAndroid Build Coastguard Worker       const Kind kind = reg_loc.GetKind();
212*795d594fSAndroid Build Coastguard Worker       const int32_t value = reg_loc.GetValue();
213*795d594fSAndroid Build Coastguard Worker       if (kind == Kind::kInStack) {
214*795d594fSAndroid Build Coastguard Worker         // The stack offset is relative to SP. Make it relative to CFA.
215*795d594fSAndroid Build Coastguard Worker         expr.WriteOpFbreg(value - method_info->frame_size_in_bytes);
216*795d594fSAndroid Build Coastguard Worker         if (piece == 0 && reg_hi.GetKind() == Kind::kInStack &&
217*795d594fSAndroid Build Coastguard Worker             reg_hi.GetValue() == value + 4) {
218*795d594fSAndroid Build Coastguard Worker           break;  // the high word is correctly implied by the low word.
219*795d594fSAndroid Build Coastguard Worker         }
220*795d594fSAndroid Build Coastguard Worker       } else if (kind == Kind::kInRegister) {
221*795d594fSAndroid Build Coastguard Worker         expr.WriteOpReg(GetDwarfCoreReg(isa, value).num());
222*795d594fSAndroid Build Coastguard Worker         if (piece == 0 && reg_hi.GetKind() == Kind::kInRegisterHigh &&
223*795d594fSAndroid Build Coastguard Worker             reg_hi.GetValue() == value) {
224*795d594fSAndroid Build Coastguard Worker           break;  // the high word is correctly implied by the low word.
225*795d594fSAndroid Build Coastguard Worker         }
226*795d594fSAndroid Build Coastguard Worker       } else if (kind == Kind::kInFpuRegister) {
227*795d594fSAndroid Build Coastguard Worker         if ((isa == InstructionSet::kArm || isa == InstructionSet::kThumb2) &&
228*795d594fSAndroid Build Coastguard Worker             piece == 0 && reg_hi.GetKind() == Kind::kInFpuRegister &&
229*795d594fSAndroid Build Coastguard Worker             reg_hi.GetValue() == value + 1 && value % 2 == 0) {
230*795d594fSAndroid Build Coastguard Worker           // Translate S register pair to D register (e.g. S4+S5 to D2).
231*795d594fSAndroid Build Coastguard Worker           expr.WriteOpReg(Reg::ArmDp(value / 2).num());
232*795d594fSAndroid Build Coastguard Worker           break;
233*795d594fSAndroid Build Coastguard Worker         }
234*795d594fSAndroid Build Coastguard Worker         expr.WriteOpReg(GetDwarfFpReg(isa, value).num());
235*795d594fSAndroid Build Coastguard Worker         if (piece == 0 && reg_hi.GetKind() == Kind::kInFpuRegisterHigh &&
236*795d594fSAndroid Build Coastguard Worker             reg_hi.GetValue() == reg_lo.GetValue()) {
237*795d594fSAndroid Build Coastguard Worker           break;  // the high word is correctly implied by the low word.
238*795d594fSAndroid Build Coastguard Worker         }
239*795d594fSAndroid Build Coastguard Worker       } else if (kind == Kind::kConstant) {
240*795d594fSAndroid Build Coastguard Worker         expr.WriteOpConsts(value);
241*795d594fSAndroid Build Coastguard Worker         expr.WriteOpStackValue();
242*795d594fSAndroid Build Coastguard Worker       } else if (kind == Kind::kNone) {
243*795d594fSAndroid Build Coastguard Worker         break;
244*795d594fSAndroid Build Coastguard Worker       } else {
245*795d594fSAndroid Build Coastguard Worker         // kInStackLargeOffset and kConstantLargeValue are hidden by GetKind().
246*795d594fSAndroid Build Coastguard Worker         // kInRegisterHigh and kInFpuRegisterHigh should be handled by
247*795d594fSAndroid Build Coastguard Worker         // the special cases above and they should not occur alone.
248*795d594fSAndroid Build Coastguard Worker         LOG(WARNING) << "Unexpected register location: " << kind
249*795d594fSAndroid Build Coastguard Worker                      << " (This can indicate either a bug in the dexer when generating"
250*795d594fSAndroid Build Coastguard Worker                      << " local variable information, or a bug in ART compiler."
251*795d594fSAndroid Build Coastguard Worker                      << " Please file a bug at go/art-bug)";
252*795d594fSAndroid Build Coastguard Worker         break;
253*795d594fSAndroid Build Coastguard Worker       }
254*795d594fSAndroid Build Coastguard Worker       if (is64bitValue) {
255*795d594fSAndroid Build Coastguard Worker         // Write the marker which is needed by split 64-bit values.
256*795d594fSAndroid Build Coastguard Worker         // This code is skipped by the special cases.
257*795d594fSAndroid Build Coastguard Worker         expr.WriteOpPiece(4);
258*795d594fSAndroid Build Coastguard Worker       }
259*795d594fSAndroid Build Coastguard Worker     }
260*795d594fSAndroid Build Coastguard Worker 
261*795d594fSAndroid Build Coastguard Worker     if (expr.size() > 0) {
262*795d594fSAndroid Build Coastguard Worker       if (is64bit) {
263*795d594fSAndroid Build Coastguard Worker         debug_loc.PushUint64(variable_location.low_pc);
264*795d594fSAndroid Build Coastguard Worker         debug_loc.PushUint64(variable_location.high_pc);
265*795d594fSAndroid Build Coastguard Worker       } else {
266*795d594fSAndroid Build Coastguard Worker         debug_loc.PushUint32(variable_location.low_pc);
267*795d594fSAndroid Build Coastguard Worker         debug_loc.PushUint32(variable_location.high_pc);
268*795d594fSAndroid Build Coastguard Worker       }
269*795d594fSAndroid Build Coastguard Worker       // Write the expression.
270*795d594fSAndroid Build Coastguard Worker       debug_loc.PushUint16(expr.size());
271*795d594fSAndroid Build Coastguard Worker       debug_loc.PushData(expr.data());
272*795d594fSAndroid Build Coastguard Worker     } else {
273*795d594fSAndroid Build Coastguard Worker       // Do not generate .debug_loc if the location is not known.
274*795d594fSAndroid Build Coastguard Worker     }
275*795d594fSAndroid Build Coastguard Worker   }
276*795d594fSAndroid Build Coastguard Worker   // Write end-of-list entry.
277*795d594fSAndroid Build Coastguard Worker   if (is64bit) {
278*795d594fSAndroid Build Coastguard Worker     debug_loc.PushUint64(0);
279*795d594fSAndroid Build Coastguard Worker     debug_loc.PushUint64(0);
280*795d594fSAndroid Build Coastguard Worker   } else {
281*795d594fSAndroid Build Coastguard Worker     debug_loc.PushUint32(0);
282*795d594fSAndroid Build Coastguard Worker     debug_loc.PushUint32(0);
283*795d594fSAndroid Build Coastguard Worker   }
284*795d594fSAndroid Build Coastguard Worker 
285*795d594fSAndroid Build Coastguard Worker   // Write .debug_ranges entries.
286*795d594fSAndroid Build Coastguard Worker   // This includes ranges where the variable is in scope but the location is not known.
287*795d594fSAndroid Build Coastguard Worker   dwarf::Writer<> debug_ranges(debug_ranges_buffer);
288*795d594fSAndroid Build Coastguard Worker   size_t debug_ranges_offset = debug_ranges.size();
289*795d594fSAndroid Build Coastguard Worker   for (size_t i = 0; i < variable_locations.size(); i++) {
290*795d594fSAndroid Build Coastguard Worker     uint32_t low_pc = variable_locations[i].low_pc;
291*795d594fSAndroid Build Coastguard Worker     uint32_t high_pc = variable_locations[i].high_pc;
292*795d594fSAndroid Build Coastguard Worker     while (i + 1 < variable_locations.size() && variable_locations[i+1].low_pc == high_pc) {
293*795d594fSAndroid Build Coastguard Worker       // Merge address range with the next entry.
294*795d594fSAndroid Build Coastguard Worker       high_pc = variable_locations[++i].high_pc;
295*795d594fSAndroid Build Coastguard Worker     }
296*795d594fSAndroid Build Coastguard Worker     if (is64bit) {
297*795d594fSAndroid Build Coastguard Worker       debug_ranges.PushUint64(low_pc);
298*795d594fSAndroid Build Coastguard Worker       debug_ranges.PushUint64(high_pc);
299*795d594fSAndroid Build Coastguard Worker     } else {
300*795d594fSAndroid Build Coastguard Worker       debug_ranges.PushUint32(low_pc);
301*795d594fSAndroid Build Coastguard Worker       debug_ranges.PushUint32(high_pc);
302*795d594fSAndroid Build Coastguard Worker     }
303*795d594fSAndroid Build Coastguard Worker   }
304*795d594fSAndroid Build Coastguard Worker   // Write end-of-list entry.
305*795d594fSAndroid Build Coastguard Worker   if (is64bit) {
306*795d594fSAndroid Build Coastguard Worker     debug_ranges.PushUint64(0);
307*795d594fSAndroid Build Coastguard Worker     debug_ranges.PushUint64(0);
308*795d594fSAndroid Build Coastguard Worker   } else {
309*795d594fSAndroid Build Coastguard Worker     debug_ranges.PushUint32(0);
310*795d594fSAndroid Build Coastguard Worker     debug_ranges.PushUint32(0);
311*795d594fSAndroid Build Coastguard Worker   }
312*795d594fSAndroid Build Coastguard Worker 
313*795d594fSAndroid Build Coastguard Worker   // Simple de-duplication - check whether this entry is same as the last one (or tail of it).
314*795d594fSAndroid Build Coastguard Worker   size_t debug_ranges_entry_size = debug_ranges.size() - debug_ranges_offset;
315*795d594fSAndroid Build Coastguard Worker   if (debug_ranges_offset >= debug_ranges_entry_size) {
316*795d594fSAndroid Build Coastguard Worker     size_t previous_offset = debug_ranges_offset - debug_ranges_entry_size;
317*795d594fSAndroid Build Coastguard Worker     if (memcmp(debug_ranges_buffer->data() + previous_offset,
318*795d594fSAndroid Build Coastguard Worker                debug_ranges_buffer->data() + debug_ranges_offset,
319*795d594fSAndroid Build Coastguard Worker                debug_ranges_entry_size) == 0) {
320*795d594fSAndroid Build Coastguard Worker       // Remove what we have just written and use the last entry instead.
321*795d594fSAndroid Build Coastguard Worker       debug_ranges_buffer->resize(debug_ranges_offset);
322*795d594fSAndroid Build Coastguard Worker       debug_ranges_offset = previous_offset;
323*795d594fSAndroid Build Coastguard Worker     }
324*795d594fSAndroid Build Coastguard Worker   }
325*795d594fSAndroid Build Coastguard Worker 
326*795d594fSAndroid Build Coastguard Worker   // Write attributes to .debug_info.
327*795d594fSAndroid Build Coastguard Worker   debug_info->WriteSecOffset(dwarf::DW_AT_location, debug_loc_offset);
328*795d594fSAndroid Build Coastguard Worker   debug_info->WriteSecOffset(dwarf::DW_AT_start_scope, debug_ranges_offset);
329*795d594fSAndroid Build Coastguard Worker }
330*795d594fSAndroid Build Coastguard Worker 
331*795d594fSAndroid Build Coastguard Worker }  // namespace debug
332*795d594fSAndroid Build Coastguard Worker }  // namespace art
333*795d594fSAndroid Build Coastguard Worker 
334*795d594fSAndroid Build Coastguard Worker #endif  // ART_COMPILER_DEBUG_ELF_DEBUG_LOC_WRITER_H_
335*795d594fSAndroid Build Coastguard Worker 
336