libdwarf_loc.c revision 260697
1155852Sgallatin/*-
2155852Sgallatin * Copyright (c) 2007 John Birrell (jb@freebsd.org)
3155852Sgallatin * All rights reserved.
4155852Sgallatin *
5155852Sgallatin * Redistribution and use in source and binary forms, with or without
6155852Sgallatin * modification, are permitted provided that the following conditions
7155852Sgallatin * are met:
8155852Sgallatin * 1. Redistributions of source code must retain the above copyright
9155852Sgallatin *    notice, this list of conditions and the following disclaimer.
10155852Sgallatin * 2. Redistributions in binary form must reproduce the above copyright
11155852Sgallatin *    notice, this list of conditions and the following disclaimer in the
12155852Sgallatin *    documentation and/or other materials provided with the distribution.
13155852Sgallatin *
14155852Sgallatin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15155852Sgallatin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16155852Sgallatin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17155852Sgallatin * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18155852Sgallatin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19155852Sgallatin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20155852Sgallatin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21155852Sgallatin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22155852Sgallatin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23155852Sgallatin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24155852Sgallatin * SUCH DAMAGE.
25155852Sgallatin */
26155852Sgallatin
27155852Sgallatin#include "_libdwarf.h"
28155852Sgallatin
29155852SgallatinELFTC_VCSID("$Id: libdwarf_loc.c 2070 2011-10-27 03:05:32Z jkoshy $");
30155852Sgallatin
31155852Sgallatin/*
32155852Sgallatin * Given an array of bytes of length 'len' representing a
33155852Sgallatin * DWARF expression, compute the number of operations based
34155852Sgallatin * on there being one byte describing the operation and
35159612Sgallatin * zero or more bytes of operands as defined in the standard
36159612Sgallatin * for each operation type. Also, if lbuf is non-null, store
37155852Sgallatin * the opcode and oprand in it.
38159612Sgallatin */
39159612Sgallatinstatic int
40159612Sgallatin_dwarf_loc_fill_loc(Dwarf_Debug dbg, Dwarf_Locdesc *lbuf, uint8_t pointer_size,
41159612Sgallatin    uint8_t *p, int len)
42155852Sgallatin{
43155852Sgallatin	int count;
44155852Sgallatin	uint64_t operand1;
45155852Sgallatin	uint64_t operand2;
46155852Sgallatin	uint8_t *ps, *pe;
47155852Sgallatin
48155852Sgallatin	count = 0;
49155852Sgallatin	ps = p;
50155852Sgallatin	pe = p + len;
51155852Sgallatin
52155852Sgallatin	/*
53159612Sgallatin	 * Process each byte. If an error occurs, then the
54155852Sgallatin	 * count will be set to -1.
55155852Sgallatin	 */
56159612Sgallatin	while (p < pe) {
57159612Sgallatin
58155852Sgallatin		operand1 = 0;
59159612Sgallatin		operand2 = 0;
60159612Sgallatin
61159612Sgallatin		if (lbuf != NULL) {
62159612Sgallatin			lbuf->ld_s[count].lr_atom = *p;
63159612Sgallatin			lbuf->ld_s[count].lr_offset = p - ps;
64159612Sgallatin		}
65155852Sgallatin
66155852Sgallatin		switch (*p++) {
67159612Sgallatin		/* Operations with no operands. */
68155852Sgallatin		case DW_OP_deref:
69155852Sgallatin		case DW_OP_reg0:
70155852Sgallatin		case DW_OP_reg1:
71155852Sgallatin		case DW_OP_reg2:
72155852Sgallatin		case DW_OP_reg3:
73155852Sgallatin		case DW_OP_reg4:
74159612Sgallatin		case DW_OP_reg5:
75155852Sgallatin		case DW_OP_reg6:
76155852Sgallatin		case DW_OP_reg7:
77159612Sgallatin		case DW_OP_reg8:
78159612Sgallatin		case DW_OP_reg9:
79155852Sgallatin		case DW_OP_reg10:
80155852Sgallatin		case DW_OP_reg11:
81159612Sgallatin		case DW_OP_reg12:
82155852Sgallatin		case DW_OP_reg13:
83155852Sgallatin		case DW_OP_reg14:
84159612Sgallatin		case DW_OP_reg15:
85159612Sgallatin		case DW_OP_reg16:
86155852Sgallatin		case DW_OP_reg17:
87155852Sgallatin		case DW_OP_reg18:
88155852Sgallatin		case DW_OP_reg19:
89155852Sgallatin		case DW_OP_reg20:
90155852Sgallatin		case DW_OP_reg21:
91155852Sgallatin		case DW_OP_reg22:
92155852Sgallatin		case DW_OP_reg23:
93155852Sgallatin		case DW_OP_reg24:
94155852Sgallatin		case DW_OP_reg25:
95155852Sgallatin		case DW_OP_reg26:
96155852Sgallatin		case DW_OP_reg27:
97155852Sgallatin		case DW_OP_reg28:
98155852Sgallatin		case DW_OP_reg29:
99155852Sgallatin		case DW_OP_reg30:
100159612Sgallatin		case DW_OP_reg31:
101155852Sgallatin
102155852Sgallatin		case DW_OP_lit0:
103155852Sgallatin		case DW_OP_lit1:
104155852Sgallatin		case DW_OP_lit2:
105159612Sgallatin		case DW_OP_lit3:
106159612Sgallatin		case DW_OP_lit4:
107159612Sgallatin		case DW_OP_lit5:
108159612Sgallatin		case DW_OP_lit6:
109159612Sgallatin		case DW_OP_lit7:
110159612Sgallatin		case DW_OP_lit8:
111159612Sgallatin		case DW_OP_lit9:
112159612Sgallatin		case DW_OP_lit10:
113159612Sgallatin		case DW_OP_lit11:
114155852Sgallatin		case DW_OP_lit12:
115159612Sgallatin		case DW_OP_lit13:
116159612Sgallatin		case DW_OP_lit14:
117155852Sgallatin		case DW_OP_lit15:
118159612Sgallatin		case DW_OP_lit16:
119155852Sgallatin		case DW_OP_lit17:
120155852Sgallatin		case DW_OP_lit18:
121159612Sgallatin		case DW_OP_lit19:
122159612Sgallatin		case DW_OP_lit20:
123155852Sgallatin		case DW_OP_lit21:
124159612Sgallatin		case DW_OP_lit22:
125159612Sgallatin		case DW_OP_lit23:
126155852Sgallatin		case DW_OP_lit24:
127155852Sgallatin		case DW_OP_lit25:
128159612Sgallatin		case DW_OP_lit26:
129155852Sgallatin		case DW_OP_lit27:
130155852Sgallatin		case DW_OP_lit28:
131159612Sgallatin		case DW_OP_lit29:
132155852Sgallatin		case DW_OP_lit30:
133159612Sgallatin		case DW_OP_lit31:
134159612Sgallatin
135155852Sgallatin		case DW_OP_dup:
136159612Sgallatin		case DW_OP_drop:
137159612Sgallatin
138159612Sgallatin		case DW_OP_over:
139155852Sgallatin
140155852Sgallatin		case DW_OP_swap:
141159612Sgallatin		case DW_OP_rot:
142155852Sgallatin		case DW_OP_xderef:
143155852Sgallatin
144159612Sgallatin		case DW_OP_abs:
145159612Sgallatin		case DW_OP_and:
146155852Sgallatin		case DW_OP_div:
147155852Sgallatin		case DW_OP_minus:
148155852Sgallatin		case DW_OP_mod:
149155852Sgallatin		case DW_OP_mul:
150162328Sgallatin		case DW_OP_neg:
151162328Sgallatin		case DW_OP_not:
152155852Sgallatin		case DW_OP_or:
153162328Sgallatin		case DW_OP_plus:
154162328Sgallatin
155162328Sgallatin		case DW_OP_shl:
156162328Sgallatin		case DW_OP_shr:
157162328Sgallatin		case DW_OP_shra:
158162328Sgallatin		case DW_OP_xor:
159162328Sgallatin
160162328Sgallatin		case DW_OP_eq:
161162328Sgallatin		case DW_OP_ge:
162162328Sgallatin		case DW_OP_gt:
163162328Sgallatin		case DW_OP_le:
164159612Sgallatin		case DW_OP_lt:
165159612Sgallatin		case DW_OP_ne:
166155852Sgallatin
167155852Sgallatin		case DW_OP_nop:
168159612Sgallatin		case DW_OP_form_tls_address:
169155852Sgallatin		case DW_OP_call_frame_cfa:
170155852Sgallatin		case DW_OP_stack_value:
171155852Sgallatin		case DW_OP_GNU_push_tls_address:
172159612Sgallatin			break;
173155852Sgallatin
174155852Sgallatin		/* Operations with 1-byte operands. */
175155852Sgallatin		case DW_OP_const1u:
176159612Sgallatin		case DW_OP_const1s:
177159612Sgallatin		case DW_OP_pick:
178155852Sgallatin		case DW_OP_deref_size:
179159612Sgallatin		case DW_OP_xderef_size:
180159612Sgallatin			operand1 = *p++;
181159612Sgallatin			break;
182155852Sgallatin
183155852Sgallatin		/* Operations with 2-byte operands. */
184155852Sgallatin		case DW_OP_call2:
185155852Sgallatin		case DW_OP_const2u:
186155852Sgallatin		case DW_OP_const2s:
187159612Sgallatin		case DW_OP_bra:
188159612Sgallatin		case DW_OP_skip:
189159612Sgallatin			operand1 = dbg->decode(&p, 2);
190159612Sgallatin			break;
191159612Sgallatin
192155852Sgallatin		/* Operations with 4-byte operands. */
193155852Sgallatin		case DW_OP_call4:
194155852Sgallatin		case DW_OP_const4u:
195155852Sgallatin		case DW_OP_const4s:
196159612Sgallatin			operand1 = dbg->decode(&p, 4);
197159612Sgallatin			break;
198155852Sgallatin
199155852Sgallatin		/* Operations with 8-byte operands. */
200155852Sgallatin		case DW_OP_const8u:
201155852Sgallatin		case DW_OP_const8s:
202155852Sgallatin			operand1 = dbg->decode(&p, 8);
203155852Sgallatin			break;
204159612Sgallatin
205155852Sgallatin		/* Operations with an unsigned LEB128 operand. */
206155852Sgallatin		case DW_OP_constu:
207155852Sgallatin		case DW_OP_plus_uconst:
208155852Sgallatin		case DW_OP_regx:
209159612Sgallatin		case DW_OP_piece:
210155852Sgallatin			operand1 = _dwarf_decode_uleb128(&p);
211155852Sgallatin			break;
212159612Sgallatin
213155852Sgallatin		/* Operations with a signed LEB128 operand. */
214155852Sgallatin		case DW_OP_consts:
215155852Sgallatin		case DW_OP_breg0:
216159612Sgallatin		case DW_OP_breg1:
217155852Sgallatin		case DW_OP_breg2:
218155852Sgallatin		case DW_OP_breg3:
219155852Sgallatin		case DW_OP_breg4:
220155852Sgallatin		case DW_OP_breg5:
221155852Sgallatin		case DW_OP_breg6:
222155852Sgallatin		case DW_OP_breg7:
223155852Sgallatin		case DW_OP_breg8:
224159612Sgallatin		case DW_OP_breg9:
225159612Sgallatin		case DW_OP_breg10:
226159612Sgallatin		case DW_OP_breg11:
227162328Sgallatin		case DW_OP_breg12:
228155852Sgallatin		case DW_OP_breg13:
229159612Sgallatin		case DW_OP_breg14:
230159612Sgallatin		case DW_OP_breg15:
231159612Sgallatin		case DW_OP_breg16:
232155852Sgallatin		case DW_OP_breg17:
233159612Sgallatin		case DW_OP_breg18:
234159612Sgallatin		case DW_OP_breg19:
235155852Sgallatin		case DW_OP_breg20:
236159612Sgallatin		case DW_OP_breg21:
237159612Sgallatin		case DW_OP_breg22:
238159612Sgallatin		case DW_OP_breg23:
239159612Sgallatin		case DW_OP_breg24:
240159612Sgallatin		case DW_OP_breg25:
241162328Sgallatin		case DW_OP_breg26:
242162328Sgallatin		case DW_OP_breg27:
243162328Sgallatin		case DW_OP_breg28:
244162328Sgallatin		case DW_OP_breg29:
245162328Sgallatin		case DW_OP_breg30:
246162328Sgallatin		case DW_OP_breg31:
247162328Sgallatin		case DW_OP_fbreg:
248162328Sgallatin			operand1 = _dwarf_decode_sleb128(&p);
249162328Sgallatin			break;
250162328Sgallatin
251162328Sgallatin		/*
252162328Sgallatin		 * Oeration with two unsigned LEB128 operands.
253162328Sgallatin		 */
254162328Sgallatin		case DW_OP_bit_piece:
255162328Sgallatin			operand1 = _dwarf_decode_uleb128(&p);
256162328Sgallatin			operand2 = _dwarf_decode_uleb128(&p);
257162328Sgallatin			break;
258162328Sgallatin
259162328Sgallatin		/*
260162328Sgallatin		 * Operations with an unsigned LEB128 operand
261159612Sgallatin		 * followed by a signed LEB128 operand.
262159612Sgallatin		 */
263155852Sgallatin		case DW_OP_bregx:
264155852Sgallatin			operand1 = _dwarf_decode_uleb128(&p);
265159612Sgallatin			operand2 = _dwarf_decode_sleb128(&p);
266159612Sgallatin			break;
267159612Sgallatin
268159612Sgallatin		/*
269159612Sgallatin		 * Operation with an unsigned LEB128 operand
270159612Sgallatin		 * followed by a block. Store a pointer to the
271159612Sgallatin		 * block in the operand2.
272159612Sgallatin		 */
273159612Sgallatin		case DW_OP_implicit_value:
274162328Sgallatin			operand1 = _dwarf_decode_uleb128(&p);
275162328Sgallatin			operand2 = (Dwarf_Unsigned) (uintptr_t) p;
276159612Sgallatin			p += operand1;
277159612Sgallatin			break;
278155852Sgallatin
279155852Sgallatin		/* Target address size operand. */
280162328Sgallatin		case DW_OP_addr:
281162328Sgallatin			operand1 = dbg->decode(&p, pointer_size);
282162328Sgallatin			break;
283162328Sgallatin
284162328Sgallatin		/*
285162328Sgallatin		 * XXX Opcode DW_OP_call_ref has an operand with size
286159612Sgallatin		 * "dwarf_size". Here we use dbg->dbg_offset_size
287159612Sgallatin		 * as "dwarf_size" to be compatible with SGI libdwarf.
288159612Sgallatin		 * However note that dbg->dbg_offset_size is just
289155852Sgallatin		 * a "guess" value so the parsing result of
290155852Sgallatin		 * DW_OP_call_ref might not be correct at all. XXX
291155852Sgallatin		 */
292155852Sgallatin		case DW_OP_call_ref:
293155852Sgallatin			operand1 = dbg->decode(&p, dbg->dbg_offset_size);
294155852Sgallatin			break;
295155852Sgallatin
296155852Sgallatin		/* All other operations cause an error. */
297155852Sgallatin		default:
298159612Sgallatin			count = -1;
299159612Sgallatin			break;
300159612Sgallatin		}
301159612Sgallatin
302159612Sgallatin		if (lbuf != NULL) {
303159612Sgallatin			lbuf->ld_s[count].lr_number = operand1;
304155852Sgallatin			lbuf->ld_s[count].lr_number2 = operand2;
305159612Sgallatin		}
306159612Sgallatin
307		count++;
308	}
309
310	return (count);
311}
312
313int
314_dwarf_loc_expr_add_atom(Dwarf_Debug dbg, uint8_t *out, uint8_t *end,
315    Dwarf_Small atom, Dwarf_Unsigned operand1, Dwarf_Unsigned operand2,
316    int *length, Dwarf_Error *error)
317{
318	uint8_t buf[64];
319	uint8_t *p, *pe;
320	uint64_t offset;
321	int len;
322
323	if (out != NULL && end != NULL) {
324		p = out;
325		pe = end;
326	} else {
327		p = out = buf;
328		pe = &buf[sizeof(buf)];
329	}
330
331	switch (atom) {
332	/* Operations with no operands. */
333	case DW_OP_deref:
334	case DW_OP_reg0:
335	case DW_OP_reg1:
336	case DW_OP_reg2:
337	case DW_OP_reg3:
338	case DW_OP_reg4:
339	case DW_OP_reg5:
340	case DW_OP_reg6:
341	case DW_OP_reg7:
342	case DW_OP_reg8:
343	case DW_OP_reg9:
344	case DW_OP_reg10:
345	case DW_OP_reg11:
346	case DW_OP_reg12:
347	case DW_OP_reg13:
348	case DW_OP_reg14:
349	case DW_OP_reg15:
350	case DW_OP_reg16:
351	case DW_OP_reg17:
352	case DW_OP_reg18:
353	case DW_OP_reg19:
354	case DW_OP_reg20:
355	case DW_OP_reg21:
356	case DW_OP_reg22:
357	case DW_OP_reg23:
358	case DW_OP_reg24:
359	case DW_OP_reg25:
360	case DW_OP_reg26:
361	case DW_OP_reg27:
362	case DW_OP_reg28:
363	case DW_OP_reg29:
364	case DW_OP_reg30:
365	case DW_OP_reg31:
366
367	case DW_OP_lit0:
368	case DW_OP_lit1:
369	case DW_OP_lit2:
370	case DW_OP_lit3:
371	case DW_OP_lit4:
372	case DW_OP_lit5:
373	case DW_OP_lit6:
374	case DW_OP_lit7:
375	case DW_OP_lit8:
376	case DW_OP_lit9:
377	case DW_OP_lit10:
378	case DW_OP_lit11:
379	case DW_OP_lit12:
380	case DW_OP_lit13:
381	case DW_OP_lit14:
382	case DW_OP_lit15:
383	case DW_OP_lit16:
384	case DW_OP_lit17:
385	case DW_OP_lit18:
386	case DW_OP_lit19:
387	case DW_OP_lit20:
388	case DW_OP_lit21:
389	case DW_OP_lit22:
390	case DW_OP_lit23:
391	case DW_OP_lit24:
392	case DW_OP_lit25:
393	case DW_OP_lit26:
394	case DW_OP_lit27:
395	case DW_OP_lit28:
396	case DW_OP_lit29:
397	case DW_OP_lit30:
398	case DW_OP_lit31:
399
400	case DW_OP_dup:
401	case DW_OP_drop:
402
403	case DW_OP_over:
404
405	case DW_OP_swap:
406	case DW_OP_rot:
407	case DW_OP_xderef:
408
409	case DW_OP_abs:
410	case DW_OP_and:
411	case DW_OP_div:
412	case DW_OP_minus:
413	case DW_OP_mod:
414	case DW_OP_mul:
415	case DW_OP_neg:
416	case DW_OP_not:
417	case DW_OP_or:
418	case DW_OP_plus:
419
420	case DW_OP_shl:
421	case DW_OP_shr:
422	case DW_OP_shra:
423	case DW_OP_xor:
424
425	case DW_OP_eq:
426	case DW_OP_ge:
427	case DW_OP_gt:
428	case DW_OP_le:
429	case DW_OP_lt:
430	case DW_OP_ne:
431
432	case DW_OP_nop:
433	case DW_OP_GNU_push_tls_address:
434		*p++ = atom;
435		break;
436
437	/* Operations with 1-byte operands. */
438	case DW_OP_const1u:
439	case DW_OP_const1s:
440	case DW_OP_pick:
441	case DW_OP_deref_size:
442	case DW_OP_xderef_size:
443		*p++ = atom;
444		*p++ = (uint8_t) operand1;
445		break;
446
447	/* Operations with 2-byte operands. */
448	case DW_OP_const2u:
449	case DW_OP_const2s:
450	case DW_OP_bra:
451	case DW_OP_skip:
452		*p++ = atom;
453		offset = 0;
454		dbg->write(p, &offset, operand1, 2);
455		p += 2;
456		break;
457
458	/* Operations with 4-byte operands. */
459	case DW_OP_const4u:
460	case DW_OP_const4s:
461		*p++ = atom;
462		offset = 0;
463		dbg->write(p, &offset, operand1, 4);
464		p += 4;
465		break;
466
467	/* Operations with 8-byte operands. */
468	case DW_OP_const8u:
469	case DW_OP_const8s:
470		*p++ = atom;
471		offset = 0;
472		dbg->write(p, &offset, operand1, 8);
473		p += 8;
474		break;
475
476	/* Operations with an unsigned LEB128 operand. */
477	case DW_OP_constu:
478	case DW_OP_plus_uconst:
479	case DW_OP_regx:
480	case DW_OP_piece:
481		*p++ = atom;
482		len = _dwarf_write_uleb128(p, pe, operand1);
483		assert(len > 0);
484		p += len;
485		break;
486
487	/* Operations with a signed LEB128 operand. */
488	case DW_OP_consts:
489	case DW_OP_breg0:
490	case DW_OP_breg1:
491	case DW_OP_breg2:
492	case DW_OP_breg3:
493	case DW_OP_breg4:
494	case DW_OP_breg5:
495	case DW_OP_breg6:
496	case DW_OP_breg7:
497	case DW_OP_breg8:
498	case DW_OP_breg9:
499	case DW_OP_breg10:
500	case DW_OP_breg11:
501	case DW_OP_breg12:
502	case DW_OP_breg13:
503	case DW_OP_breg14:
504	case DW_OP_breg15:
505	case DW_OP_breg16:
506	case DW_OP_breg17:
507	case DW_OP_breg18:
508	case DW_OP_breg19:
509	case DW_OP_breg20:
510	case DW_OP_breg21:
511	case DW_OP_breg22:
512	case DW_OP_breg23:
513	case DW_OP_breg24:
514	case DW_OP_breg25:
515	case DW_OP_breg26:
516	case DW_OP_breg27:
517	case DW_OP_breg28:
518	case DW_OP_breg29:
519	case DW_OP_breg30:
520	case DW_OP_breg31:
521	case DW_OP_fbreg:
522		*p++ = atom;
523		len = _dwarf_write_sleb128(p, pe, operand1);
524		assert(len > 0);
525		p += len;
526		break;
527
528	/*
529	 * Operations with an unsigned LEB128 operand
530	 * followed by a signed LEB128 operand.
531	 */
532	case DW_OP_bregx:
533		*p++ = atom;
534		len = _dwarf_write_uleb128(p, pe, operand1);
535		assert(len > 0);
536		p += len;
537		len = _dwarf_write_sleb128(p, pe, operand2);
538		assert(len > 0);
539		p += len;
540		break;
541
542	/* Target address size operand. */
543	case DW_OP_addr:
544		*p++ = atom;
545		offset = 0;
546		dbg->write(p, &offset, operand1, dbg->dbg_pointer_size);
547		p += dbg->dbg_pointer_size;
548		break;
549
550	/* All other operations cause an error. */
551	default:
552		DWARF_SET_ERROR(dbg, error, DW_DLE_LOC_EXPR_BAD);
553		return (DW_DLE_LOC_EXPR_BAD);
554	}
555
556	if (length)
557		*length = p - out;
558
559	return (DW_DLE_NONE);
560}
561
562int
563_dwarf_loc_fill_locdesc(Dwarf_Debug dbg, Dwarf_Locdesc *llbuf, uint8_t *in,
564    uint64_t in_len, uint8_t pointer_size, Dwarf_Error *error)
565{
566	int num;
567
568	assert(llbuf != NULL);
569	assert(in != NULL);
570	assert(in_len > 0);
571
572	/* Compute the number of locations. */
573	if ((num = _dwarf_loc_fill_loc(dbg, NULL, pointer_size, in, in_len)) <
574	    0) {
575		DWARF_SET_ERROR(dbg, error, DW_DLE_LOC_EXPR_BAD);
576		return (DW_DLE_LOC_EXPR_BAD);
577	}
578
579	llbuf->ld_cents = num;
580	if (num <= 0)
581		return (DW_DLE_NONE);
582
583	if ((llbuf->ld_s = calloc(num, sizeof(Dwarf_Loc))) == NULL) {
584		DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
585		return (DW_DLE_MEMORY);
586	}
587
588	(void) _dwarf_loc_fill_loc(dbg, llbuf, pointer_size, in, in_len);
589
590	return (DW_DLE_NONE);
591}
592
593int
594_dwarf_loc_fill_locexpr(Dwarf_Debug dbg, Dwarf_Locdesc **ret_llbuf, uint8_t *in,
595    uint64_t in_len, uint8_t pointer_size, Dwarf_Error *error)
596{
597	Dwarf_Locdesc *llbuf;
598	int ret;
599
600	if ((llbuf = malloc(sizeof(Dwarf_Locdesc))) == NULL) {
601		DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
602		return (DW_DLE_MEMORY);
603	}
604	llbuf->ld_lopc = 0;
605	llbuf->ld_hipc = ~0ULL;
606	llbuf->ld_s = NULL;
607
608	ret = _dwarf_loc_fill_locdesc(dbg, llbuf, in, in_len, pointer_size,
609	    error);
610	if (ret != DW_DLE_NONE) {
611		free(llbuf);
612		return (ret);
613	}
614
615	*ret_llbuf = llbuf;
616
617	return (ret);
618}
619
620int
621_dwarf_loc_add(Dwarf_Die die, Dwarf_Attribute at, Dwarf_Error *error)
622{
623	Dwarf_Debug dbg;
624	Dwarf_CU cu;
625	int ret;
626
627	assert(at->at_ld == NULL);
628	assert(at->u[1].u8p != NULL);
629	assert(at->u[0].u64 > 0);
630
631	cu = die->die_cu;
632	assert(cu != NULL);
633
634	dbg = cu->cu_dbg;
635	assert(dbg != NULL);
636
637	ret = _dwarf_loc_fill_locexpr(dbg, &at->at_ld, at->u[1].u8p,
638	    at->u[0].u64, cu->cu_pointer_size, error);
639
640	return (ret);
641}
642